aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2024-01-26 17:09:20 +0900
committerYawning Angel <yawning@schwanenlied.me>2024-02-07 00:37:18 +0900
commit1d151c4c92e1570b345c402c12a2667b7ec8b8a8 (patch)
treeb7957784da32cbbf5064c688f6d72051d84496ca /tests
parent899fab64d989363ecc39e3bf651946dfc8d3b45f (diff)
tests/core/crypto: Cleanup/modernize a bit
Diffstat (limited to 'tests')
-rw-r--r--tests/core/crypto/test_core_crypto.odin39
-rw-r--r--tests/core/crypto/test_core_crypto_hash.odin164
-rw-r--r--tests/core/crypto/test_core_crypto_modern.odin412
3 files changed, 366 insertions, 249 deletions
diff --git a/tests/core/crypto/test_core_crypto.odin b/tests/core/crypto/test_core_crypto.odin
index 7bcc71476..0aaf2d854 100644
--- a/tests/core/crypto/test_core_crypto.odin
+++ b/tests/core/crypto/test_core_crypto.odin
@@ -12,20 +12,20 @@ package test_core_crypto
Where possible, the official test vectors are used to validate the implementation.
*/
-import "core:testing"
import "core:fmt"
+import "core:testing"
import "core:crypto/siphash"
import "core:os"
TEST_count := 0
-TEST_fail := 0
+TEST_fail := 0
when ODIN_TEST {
- expect :: testing.expect
- log :: testing.log
+ expect :: testing.expect
+ log :: testing.log
} else {
- expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
+ expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
TEST_count += 1
if !condition {
TEST_fail += 1
@@ -60,16 +60,6 @@ main :: proc() {
}
}
-hex_string :: proc(bytes: []byte, allocator := context.temp_allocator) -> string {
- lut: [16]byte = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
- buf := make([]byte, len(bytes) * 2, allocator)
- for i := 0; i < len(bytes); i += 1 {
- buf[i * 2 + 0] = lut[bytes[i] >> 4 & 0xf]
- buf[i * 2 + 1] = lut[bytes[i] & 0xf]
- }
- return string(buf)
-}
-
@(test)
test_siphash_2_4 :: proc(t: ^testing.T) {
// Test vectors from
@@ -94,19 +84,28 @@ test_siphash_2_4 :: proc(t: ^testing.T) {
}
key: [16]byte
- for i in 0..<16 {
+ for i in 0 ..< 16 {
key[i] = byte(i)
}
- for i in 0..<len(test_vectors) {
+ for i in 0 ..< len(test_vectors) {
data := make([]byte, i)
- for j in 0..<i {
+ for j in 0 ..< i {
data[j] = byte(j)
}
- vector := test_vectors[i]
+ vector := test_vectors[i]
computed := siphash.sum_2_4(data[:], key[:])
- expect(t, computed == vector, fmt.tprintf("Expected: 0x%x for input of %v, but got 0x%x instead", vector, data, computed))
+ expect(
+ t,
+ computed == vector,
+ fmt.tprintf(
+ "Expected: 0x%x for input of %v, but got 0x%x instead",
+ vector,
+ data,
+ computed,
+ ),
+ )
}
}
diff --git a/tests/core/crypto/test_core_crypto_hash.odin b/tests/core/crypto/test_core_crypto_hash.odin
index bcbcdbe88..653d3fc34 100644
--- a/tests/core/crypto/test_core_crypto_hash.odin
+++ b/tests/core/crypto/test_core_crypto_hash.odin
@@ -1,6 +1,7 @@
package test_core_crypto
import "core:bytes"
+import "core:encoding/hex"
import "core:fmt"
import "core:strings"
import "core:testing"
@@ -24,24 +25,24 @@ test_hash :: proc(t: ^testing.T) {
digest: [64]byte // 512-bits is enough for every digest for now.
test_vectors := [?]TestHash {
// BLAKE2b
- TestHash {
+ {
hash.Algorithm.BLAKE2B,
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce",
"",
},
- TestHash {
+ {
hash.Algorithm.BLAKE2B,
"a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918",
"The quick brown fox jumps over the lazy dog",
},
// BLAKE2s
- TestHash {
+ {
hash.Algorithm.BLAKE2S,
"69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9",
"",
},
- TestHash{
+ {
hash.Algorithm.BLAKE2S,
"606beeec743ccbeff6cbcdf5d5302aa855c256c29b88c8ed331ea1a6bf3c8812",
"The quick brown fox jumps over the lazy dog",
@@ -51,27 +52,27 @@ test_hash :: proc(t: ^testing.T) {
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
// - https://datatracker.ietf.org/doc/html/rfc3874#section-3.3
- TestHash {
+ {
hash.Algorithm.SHA224,
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA224,
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA224,
"75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA224,
"c97ca9a559850ce97a04a96def6d99a9e0e0e2ab14e6b8df265fc0b3",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
},
- TestHash {
+ {
hash.Algorithm.SHA224,
"20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67",
data_1_000_000_a,
@@ -80,22 +81,22 @@ test_hash :: proc(t: ^testing.T) {
// SHA-256
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA256,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA256,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA256,
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA256,
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -104,22 +105,22 @@ test_hash :: proc(t: ^testing.T) {
// SHA-384
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA384,
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA384,
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA384,
"3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA384,
"09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -128,34 +129,34 @@ test_hash :: proc(t: ^testing.T) {
// SHA-512
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA512,
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA512,
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA512,
"204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA512,
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
},
// SHA-512/256
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
- TestHash {
+ {
hash.Algorithm.SHA512_256,
"53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA512_256,
"3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -165,42 +166,42 @@ test_hash :: proc(t: ^testing.T) {
//
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"10241ac5187380bd501192e4e56b5280908727dd8fe0d10d4e5ad91e",
"abcdbcdecdefdefgefghfghighijhi",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"fd645fe07d814c397e85e85f92fe58b949f55efa4d3468b2468da45a",
"jkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"9e86ff69557ca95f405f081269685b38e3a819b309ee942f482b6a8b",
"a",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"6961f694b2ff3ed6f0c830d2c66da0c5e7ca9445f7c0dca679171112",
"01234567012345670123456701234567",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"8a24108b154ada21c9fd5574494479ba5c7e7ab76ef264ead0fcce33",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_224,
"543e6868e1666c1a643630df77367ae5a62a85070a51c14cbf665cbc",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -209,42 +210,42 @@ test_hash :: proc(t: ^testing.T) {
// SHA3-256
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"565ada1ced21278cfaffdde00dea0107964121ac25e4e978abc59412be74550a",
"abcdbcdecdefdefgefghfghighijhi",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"8cc1709d520f495ce972ece48b0d2e1f74ec80d53bc5c47457142158fae15d98",
"jkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"80084bf2fba02475726feb2cab2d8215eab14bc6bdd8bfb2c8151257032ecd8b",
"a",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"e4786de5f88f7d374b7288f225ea9f2f7654da200bab5d417e1fb52d49202767",
"01234567012345670123456701234567",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_256,
"916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -253,42 +254,42 @@ test_hash :: proc(t: ^testing.T) {
// SHA3-384
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"9aa92dbb716ebb573def0d5e3cdd28d6add38ada310b602b8916e690a3257b7144e5ddd3d0dbbc559c48480d34d57a9a",
"abcdbcdecdefdefgefghfghighijhi",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"77c90323d7392bcdee8a3e7f74f19f47b7d1b1a825ac6a2d8d882a72317879cc26597035f1fc24fe65090b125a691282",
"jkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"1815f774f320491b48569efec794d249eeb59aae46d22bf77dafe25c5edc28d7ea44f93ee1234aa88f61c91912a4ccd9",
"a",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"51072590ad4c51b27ff8265590d74f92de7cc55284168e414ca960087c693285b08a283c6b19d77632994cb9eb93f1be",
"01234567012345670123456701234567",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"991c665755eb3a4b6bbdfb75c78a492e8c56a22c5c4d7e429bfdbc32b9d4ad5aa04a1f076e62fea19eef51acd0657c22",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_384,
"79407d3b5916b59c3e30b09822974791c313fb9ecc849e406f23592d04f625dc8c709b98b43b3852b337216179aa7fc7",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -297,42 +298,42 @@ test_hash :: proc(t: ^testing.T) {
// SHA3-512
// https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
"",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"9f9a327944a35988d67effc4fa748b3c07744f736ac70b479d8e12a3d10d6884d00a7ef593690305462e9e9030a67c51636fd346fd8fa0ee28a5ac2aee103d2e",
"abcdbcdecdefdefgefghfghighijhi",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"dbb124a0deda966eb4d199d0844fa0beb0770ea1ccddabcd335a7939a931ac6fb4fa6aebc6573f462ced2e4e7178277803be0d24d8bc2864626d9603109b7891",
"jkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"697f2d856172cb8309d6b8b97dac4de344b549d4dee61edfb4962d8698b7fa803f4f93ff24393586e28b5b957ac3d1d369420ce53332712f997bd336d09ab02a",
"a",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"5679e353bc8eeea3e801ca60448b249bcfd3ac4a6c3abe429a807bcbd4c9cd12da87a5a9dc74fde64c0d44718632cae966b078397c6f9ec155c6a238f2347cf1",
"01234567012345670123456701234567",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"04a371e84ecfb5b8b77cb48610fca8182dd457ce6f326a0fd3d7ec2f1e91636dee691fbe0c985302ba1b0d8dc78c086346b533b49c030d99a27daf1139d6e75e",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.SHA3_512,
"afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -369,27 +370,27 @@ test_hash :: proc(t: ^testing.T) {
},
// SM3
- TestHash {
+ {
hash.Algorithm.SM3,
"1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b",
"",
},
- TestHash {
+ {
hash.Algorithm.SM3,
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
"abc",
},
- TestHash {
+ {
hash.Algorithm.SM3,
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd",
},
- TestHash {
+ {
hash.Algorithm.SM3,
"5fdfe814b8573ca021983970fc79b2218c9570369b4859684e2e4c3fc76cb8ea",
"The quick brown fox jumps over the lazy dog",
},
- TestHash {
+ {
hash.Algorithm.SM3,
"ca27d14a42fc04c1e5ecf574a95a8c2d70ecb5805e9b429026ccac8f28b20098",
"The quick brown fox jumps over the lazy cog",
@@ -398,12 +399,12 @@ test_hash :: proc(t: ^testing.T) {
// Keccak-224 (Legacy)
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_224,
"f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd",
"",
},
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_224,
"c30411768506ebe1c2871b1ee2e87d38df342317300a9b97a95ec6a8",
"abc",
@@ -412,12 +413,12 @@ test_hash :: proc(t: ^testing.T) {
// Keccak-256 (Legacy)
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_256,
"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"",
},
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_256,
"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
"abc",
@@ -426,12 +427,12 @@ test_hash :: proc(t: ^testing.T) {
// Keccak-384 (Legacy)
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_384,
"2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff",
"",
},
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_384,
"f7df1165f033337be098e7d288ad6a2f74409d7a60b49c36642218de161b1f99f8c681e4afaf31a34db29fb763e3c28e",
"abc",
@@ -440,12 +441,12 @@ test_hash :: proc(t: ^testing.T) {
// Keccak-512 (Legacy)
// - https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/examples/sha_all.pdf
// - https://www.di-mgt.com.au/sha_testvectors.html
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_512,
"0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e",
"",
},
- TestHash {
+ {
hash.Algorithm.Legacy_KECCAK_512,
"18587dc2ea106b9a1563e32b3312421ca164c7f1f07bc922a9c83d77cea3a1e5d0c69910739025372dc14ac9642629379540c17e2a65b19d77aa511a9d00bb96",
"abc",
@@ -456,22 +457,22 @@ test_hash :: proc(t: ^testing.T) {
TestHash{hash.Algorithm.Insecure_MD5, "d41d8cd98f00b204e9800998ecf8427e", ""},
TestHash{hash.Algorithm.Insecure_MD5, "0cc175b9c0f1b6a831c399e269772661", "a"},
TestHash{hash.Algorithm.Insecure_MD5, "900150983cd24fb0d6963f7d28e17f72", "abc"},
- TestHash {
+ {
hash.Algorithm.Insecure_MD5,
"f96b697d7cb7938d525a2f31aaf161d0",
"message digest",
},
- TestHash {
+ {
hash.Algorithm.Insecure_MD5,
"c3fcd3d76192e4007dfb496cca67e13b",
"abcdefghijklmnopqrstuvwxyz",
},
- TestHash {
+ {
hash.Algorithm.Insecure_MD5,
"d174ab98d277d9f5a5611c2c9f419d9f",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
},
- TestHash {
+ {
hash.Algorithm.Insecure_MD5,
"57edf4a22be3c955ac49da2e2107b67a",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
@@ -482,28 +483,28 @@ test_hash :: proc(t: ^testing.T) {
// - https://www.di-mgt.com.au/sha_testvectors.html
TestHash{hash.Algorithm.Insecure_SHA1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", ""},
TestHash{hash.Algorithm.Insecure_SHA1, "a9993e364706816aba3e25717850c26c9cd0d89d", "abc"},
- TestHash {
+ {
hash.Algorithm.Insecure_SHA1,
"f9537c23893d2014f365adf8ffe33b8eb0297ed1",
"abcdbcdecdefdefgefghfghighijhi",
},
- TestHash {
+ {
hash.Algorithm.Insecure_SHA1,
"346fb528a24b48f563cb061470bcfd23740427ad",
"jkijkljklmklmnlmnomnopnopq",
},
TestHash{hash.Algorithm.Insecure_SHA1, "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a"},
- TestHash {
+ {
hash.Algorithm.Insecure_SHA1,
"c729c8996ee0a6f74f4f3248e8957edf704fb624",
"01234567012345670123456701234567",
},
- TestHash {
+ {
hash.Algorithm.Insecure_SHA1,
"84983e441c3bd26ebaae4aa1f95129e5e54670f1",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
},
- TestHash {
+ {
hash.Algorithm.Insecure_SHA1,
"a49b2446a02c645bf419f995b67091253a04a259",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
@@ -520,7 +521,7 @@ test_hash :: proc(t: ^testing.T) {
hash.update(&ctx, data)
hash.final(&ctx, dst)
- dst_str := hex_string(dst)
+ dst_str := string(hex.encode(dst, context.temp_allocator))
expect(
t,
@@ -533,7 +534,6 @@ test_hash :: proc(t: ^testing.T) {
dst_str,
),
)
-
}
for algo in hash.Algorithm {
@@ -552,7 +552,8 @@ test_hash :: proc(t: ^testing.T) {
digest_a, _ := hash.hash_stream(algo, st, context.temp_allocator)
digest_b := hash.hash_string(algo, data_1_000_000_a, context.temp_allocator)
- a_str, b_str := hex_string(digest_a), hex_string(digest_b)
+ a_str := string(hex.encode(digest_a, context.temp_allocator))
+ b_str := string(hex.encode(digest_b, context.temp_allocator))
expect(
t,
@@ -597,7 +598,8 @@ test_hash :: proc(t: ^testing.T) {
hash.final(&ctx, digest_a, true)
hash.final(&ctx, digest_b)
- a_str, b_str = hex_string(digest_a), hex_string(digest_b)
+ a_str = string(hex.encode(digest_a, context.temp_allocator))
+ b_str = string(hex.encode(digest_b, context.temp_allocator))
expect(
t,
diff --git a/tests/core/crypto/test_core_crypto_modern.odin b/tests/core/crypto/test_core_crypto_modern.odin
index 8f7abb5e8..50da734c1 100644
--- a/tests/core/crypto/test_core_crypto_modern.odin
+++ b/tests/core/crypto/test_core_crypto_modern.odin
@@ -1,37 +1,17 @@
package test_core_crypto
-import "core:testing"
+import "core:crypto"
+import "core:encoding/hex"
import "core:fmt"
import "core:mem"
+import "core:testing"
import "core:time"
-import "core:crypto"
import "core:crypto/chacha20"
import "core:crypto/chacha20poly1305"
import "core:crypto/poly1305"
import "core:crypto/x25519"
-_digit_value :: proc(r: rune) -> int {
- ri := int(r)
- v: int = 16
- switch r {
- case '0'..='9': v = ri-'0'
- case 'a'..='z': v = ri-'a'+10
- case 'A'..='Z': v = ri-'A'+10
- }
- return v
-}
-
-_decode_hex32 :: proc(s: string) -> [32]byte{
- b: [32]byte
- for i := 0; i < len(s); i = i + 2 {
- hi := _digit_value(rune(s[i]))
- lo := _digit_value(rune(s[i+1]))
- b[i/2] = byte(hi << 4 | lo)
- }
- return b
-}
-
_PLAINTEXT_SUNSCREEN_STR := "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."
@(test)
@@ -41,19 +21,19 @@ test_chacha20 :: proc(t: ^testing.T) {
// Test cases taken from RFC 8439, and draft-irtf-cfrg-xchacha-03
plaintext := transmute([]byte)(_PLAINTEXT_SUNSCREEN_STR)
- key := [chacha20.KEY_SIZE]byte{
+ key := [chacha20.KEY_SIZE]byte {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
}
- nonce := [chacha20.NONCE_SIZE]byte{
+ nonce := [chacha20.NONCE_SIZE]byte {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a,
0x00, 0x00, 0x00, 0x00,
}
- ciphertext := [114]byte{
+ ciphertext := [114]byte {
0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80,
0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d, 0x69, 0x81,
0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2,
@@ -70,7 +50,7 @@ test_chacha20 :: proc(t: ^testing.T) {
0xb4, 0x0b, 0x8e, 0xed, 0xf2, 0x78, 0x5e, 0x42,
0x87, 0x4d,
}
- ciphertext_str := hex_string(ciphertext[:])
+ ciphertext_str := string(hex.encode(ciphertext[:], context.temp_allocator))
derived_ciphertext: [114]byte
ctx: chacha20.Context = ---
@@ -78,23 +58,31 @@ test_chacha20 :: proc(t: ^testing.T) {
chacha20.seek(&ctx, 1) // The test vectors start the counter at 1.
chacha20.xor_bytes(&ctx, derived_ciphertext[:], plaintext[:])
- derived_ciphertext_str := hex_string(derived_ciphertext[:])
- expect(t, derived_ciphertext_str == ciphertext_str, fmt.tprintf("Expected %s for xor_bytes(plaintext_str), but got %s instead", ciphertext_str, derived_ciphertext_str))
+ derived_ciphertext_str := string(hex.encode(derived_ciphertext[:], context.temp_allocator))
+ expect(
+ t,
+ derived_ciphertext_str == ciphertext_str,
+ fmt.tprintf(
+ "Expected %s for xor_bytes(plaintext_str), but got %s instead",
+ ciphertext_str,
+ derived_ciphertext_str,
+ ),
+ )
- xkey := [chacha20.KEY_SIZE]byte{
+ xkey := [chacha20.KEY_SIZE]byte {
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
}
- xnonce := [chacha20.XNONCE_SIZE]byte{
+ xnonce := [chacha20.XNONCE_SIZE]byte {
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
}
- xciphertext := [114]byte{
+ xciphertext := [114]byte {
0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b,
0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc,
@@ -111,14 +99,22 @@ test_chacha20 :: proc(t: ^testing.T) {
0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
0xb5, 0x2e,
}
- xciphertext_str := hex_string(xciphertext[:])
+ xciphertext_str := string(hex.encode(xciphertext[:], context.temp_allocator))
chacha20.init(&ctx, xkey[:], xnonce[:])
chacha20.seek(&ctx, 1)
chacha20.xor_bytes(&ctx, derived_ciphertext[:], plaintext[:])
- derived_ciphertext_str = hex_string(derived_ciphertext[:])
- expect(t, derived_ciphertext_str == xciphertext_str, fmt.tprintf("Expected %s for xor_bytes(plaintext_str), but got %s instead", xciphertext_str, derived_ciphertext_str))
+ derived_ciphertext_str = string(hex.encode(derived_ciphertext[:], context.temp_allocator))
+ expect(
+ t,
+ derived_ciphertext_str == xciphertext_str,
+ fmt.tprintf(
+ "Expected %s for xor_bytes(plaintext_str), but got %s instead",
+ xciphertext_str,
+ derived_ciphertext_str,
+ ),
+ )
}
@(test)
@@ -126,38 +122,38 @@ test_poly1305 :: proc(t: ^testing.T) {
log(t, "Testing poly1305")
// Test cases taken from poly1305-donna.
- key := [poly1305.KEY_SIZE]byte{
- 0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
- 0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
- 0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
- 0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
- }
-
- msg := [131]byte{
- 0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
- 0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
- 0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
- 0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
- 0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
- 0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
- 0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
- 0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
- 0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
- 0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
- 0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
- 0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
- 0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
- 0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
- 0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
- 0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
- 0xe3,0x55,0xa5,
- }
-
- tag := [poly1305.TAG_SIZE]byte{
- 0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
- 0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9,
- }
- tag_str := hex_string(tag[:])
+ key := [poly1305.KEY_SIZE]byte {
+ 0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91,
+ 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d, 0x3c, 0x25,
+ 0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65,
+ 0x2d, 0x65, 0x1f, 0xa4, 0xc8, 0xcf, 0xf8, 0x80,
+ }
+
+ msg := [131]byte {
+ 0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73,
+ 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce,
+ 0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4,
+ 0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a,
+ 0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b,
+ 0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72,
+ 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2,
+ 0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38,
+ 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7, 0xcc, 0x8a,
+ 0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae,
+ 0x90, 0x22, 0x43, 0x68, 0x51, 0x7a, 0xcf, 0xea,
+ 0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda,
+ 0x99, 0x83, 0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde,
+ 0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3,
+ 0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6,
+ 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4, 0x5a, 0x74,
+ 0xe3, 0x55, 0xa5,
+ }
+
+ tag := [poly1305.TAG_SIZE]byte {
+ 0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5,
+ 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9,
+ }
+ tag_str := string(hex.encode(tag[:], context.temp_allocator))
// Verify - oneshot + compare
ok := poly1305.verify(tag[:], msg[:], key[:])
@@ -166,8 +162,12 @@ test_poly1305 :: proc(t: ^testing.T) {
// Sum - oneshot
derived_tag: [poly1305.TAG_SIZE]byte
poly1305.sum(derived_tag[:], msg[:], key[:])
- derived_tag_str := hex_string(derived_tag[:])
- expect(t, derived_tag_str == tag_str, fmt.tprintf("Expected %s for sum(msg, key), but got %s instead", tag_str, derived_tag_str))
+ derived_tag_str := string(hex.encode(derived_tag[:], context.temp_allocator))
+ expect(
+ t,
+ derived_tag_str == tag_str,
+ fmt.tprintf("Expected %s for sum(msg, key), but got %s instead", tag_str, derived_tag_str),
+ )
// Incremental
mem.zero(&derived_tag, size_of(derived_tag))
@@ -176,13 +176,21 @@ test_poly1305 :: proc(t: ^testing.T) {
read_lengths := [11]int{32, 64, 16, 8, 4, 2, 1, 1, 1, 1, 1}
off := 0
for read_length in read_lengths {
- to_read := msg[off:off+read_length]
+ to_read := msg[off:off + read_length]
poly1305.update(&ctx, to_read)
off = off + read_length
}
poly1305.final(&ctx, derived_tag[:])
- derived_tag_str = hex_string(derived_tag[:])
- expect(t, derived_tag_str == tag_str, fmt.tprintf("Expected %s for init/update/final - incremental, but got %s instead", tag_str, derived_tag_str))
+ derived_tag_str = string(hex.encode(derived_tag[:], context.temp_allocator))
+ expect(
+ t,
+ derived_tag_str == tag_str,
+ fmt.tprintf(
+ "Expected %s for init/update/final - incremental, but got %s instead",
+ tag_str,
+ derived_tag_str,
+ ),
+ )
}
@(test)
@@ -191,24 +199,24 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
plaintext := transmute([]byte)(_PLAINTEXT_SUNSCREEN_STR)
- aad := [12]byte{
+ aad := [12]byte {
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7,
}
- key := [chacha20poly1305.KEY_SIZE]byte{
+ key := [chacha20poly1305.KEY_SIZE]byte {
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
}
- nonce := [chacha20poly1305.NONCE_SIZE]byte{
- 0x07, 0x00, 0x00, 0x00,
- 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ nonce := [chacha20poly1305.NONCE_SIZE]byte {
+ 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
+ 0x44, 0x45, 0x46, 0x47,
}
- ciphertext := [114]byte{
+ ciphertext := [114]byte {
0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
@@ -225,37 +233,89 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
0x61, 0x16,
}
- ciphertext_str := hex_string(ciphertext[:])
+ ciphertext_str := string(hex.encode(ciphertext[:], context.temp_allocator))
- tag := [chacha20poly1305.TAG_SIZE]byte{
+ tag := [chacha20poly1305.TAG_SIZE]byte {
0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91,
}
- tag_str := hex_string(tag[:])
+ tag_str := string(hex.encode(tag[:], context.temp_allocator))
derived_tag: [chacha20poly1305.TAG_SIZE]byte
derived_ciphertext: [114]byte
- chacha20poly1305.encrypt(derived_ciphertext[:], derived_tag[:], key[:], nonce[:], aad[:], plaintext)
+ chacha20poly1305.encrypt(
+ derived_ciphertext[:],
+ derived_tag[:],
+ key[:],
+ nonce[:],
+ aad[:],
+ plaintext,
+ )
- derived_ciphertext_str := hex_string(derived_ciphertext[:])
- expect(t, derived_ciphertext_str == ciphertext_str, fmt.tprintf("Expected ciphertext %s for encrypt(aad, plaintext), but got %s instead", ciphertext_str, derived_ciphertext_str))
+ derived_ciphertext_str := string(hex.encode(derived_ciphertext[:], context.temp_allocator))
+ expect(
+ t,
+ derived_ciphertext_str == ciphertext_str,
+ fmt.tprintf(
+ "Expected ciphertext %s for encrypt(aad, plaintext), but got %s instead",
+ ciphertext_str,
+ derived_ciphertext_str,
+ ),
+ )
- derived_tag_str := hex_string(derived_tag[:])
- expect(t, derived_tag_str == tag_str, fmt.tprintf("Expected tag %s for encrypt(aad, plaintext), but got %s instead", tag_str, derived_tag_str))
+ derived_tag_str := string(hex.encode(derived_tag[:], context.temp_allocator))
+ expect(
+ t,
+ derived_tag_str == tag_str,
+ fmt.tprintf(
+ "Expected tag %s for encrypt(aad, plaintext), but got %s instead",
+ tag_str,
+ derived_tag_str,
+ ),
+ )
derived_plaintext: [114]byte
- ok := chacha20poly1305.decrypt(derived_plaintext[:], tag[:], key[:], nonce[:], aad[:], ciphertext[:])
+ ok := chacha20poly1305.decrypt(
+ derived_plaintext[:],
+ tag[:],
+ key[:],
+ nonce[:],
+ aad[:],
+ ciphertext[:],
+ )
derived_plaintext_str := string(derived_plaintext[:])
expect(t, ok, "Expected true for decrypt(tag, aad, ciphertext)")
- expect(t, derived_plaintext_str == _PLAINTEXT_SUNSCREEN_STR, fmt.tprintf("Expected plaintext %s for decrypt(tag, aad, ciphertext), but got %s instead", _PLAINTEXT_SUNSCREEN_STR, derived_plaintext_str))
+ expect(
+ t,
+ derived_plaintext_str == _PLAINTEXT_SUNSCREEN_STR,
+ fmt.tprintf(
+ "Expected plaintext %s for decrypt(tag, aad, ciphertext), but got %s instead",
+ _PLAINTEXT_SUNSCREEN_STR,
+ derived_plaintext_str,
+ ),
+ )
derived_ciphertext[0] ~= 0xa5
- ok = chacha20poly1305.decrypt(derived_plaintext[:], tag[:], key[:], nonce[:], aad[:], derived_ciphertext[:])
+ ok = chacha20poly1305.decrypt(
+ derived_plaintext[:],
+ tag[:],
+ key[:],
+ nonce[:],
+ aad[:],
+ derived_ciphertext[:],
+ )
expect(t, !ok, "Expected false for decrypt(tag, aad, corrupted_ciphertext)")
aad[0] ~= 0xa5
- ok = chacha20poly1305.decrypt(derived_plaintext[:], tag[:], key[:], nonce[:], aad[:], ciphertext[:])
+ ok = chacha20poly1305.decrypt(
+ derived_plaintext[:],
+ tag[:],
+ key[:],
+ nonce[:],
+ aad[:],
+ ciphertext[:],
+ )
expect(t, !ok, "Expected false for decrypt(tag, corrupted_aad, ciphertext)")
}
@@ -270,44 +330,64 @@ test_x25519 :: proc(t: ^testing.T) {
log(t, "Testing X25519")
// Local copy of this so that the base point doesn't need to be exported.
- _BASE_POINT: [32]byte = {
+ _BASE_POINT: [32]byte = {
9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
test_vectors := [?]TestECDH {
// Test vectors from RFC 7748
- TestECDH{
+ TestECDH {
"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4",
"e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c",
"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552",
},
- TestECDH{
+ TestECDH {
"4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d",
"e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493",
"95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957",
},
}
for v, _ in test_vectors {
- scalar := _decode_hex32(v.scalar)
- point := _decode_hex32(v.point)
+ scalar, _ := hex.decode(transmute([]byte)(v.scalar), context.temp_allocator)
+ point, _ := hex.decode(transmute([]byte)(v.point), context.temp_allocator)
derived_point: [x25519.POINT_SIZE]byte
x25519.scalarmult(derived_point[:], scalar[:], point[:])
- derived_point_str := hex_string(derived_point[:])
-
- expect(t, derived_point_str == v.product, fmt.tprintf("Expected %s for %s * %s, but got %s instead", v.product, v.scalar, v.point, derived_point_str))
+ derived_point_str := string(hex.encode(derived_point[:], context.temp_allocator))
+
+ expect(
+ t,
+ derived_point_str == v.product,
+ fmt.tprintf(
+ "Expected %s for %s * %s, but got %s instead",
+ v.product,
+ v.scalar,
+ v.point,
+ derived_point_str,
+ ),
+ )
// Abuse the test vectors to sanity-check the scalar-basepoint multiply.
p1, p2: [x25519.POINT_SIZE]byte
x25519.scalarmult_basepoint(p1[:], scalar[:])
x25519.scalarmult(p2[:], scalar[:], _BASE_POINT[:])
- p1_str, p2_str := hex_string(p1[:]), hex_string(p2[:])
- expect(t, p1_str == p2_str, fmt.tprintf("Expected %s for %s * basepoint, but got %s instead", p2_str, v.scalar, p1_str))
- }
-
- // TODO/tests: Run the wycheproof test vectors, once I figure out
- // how to work with JSON.
+ p1_str := string(hex.encode(p1[:], context.temp_allocator))
+ p2_str := string(hex.encode(p2[:], context.temp_allocator))
+ expect(
+ t,
+ p1_str == p2_str,
+ fmt.tprintf(
+ "Expected %s for %s * basepoint, but got %s instead",
+ p2_str,
+ v.scalar,
+ p1_str,
+ ),
+ )
+ }
+
+ // TODO/tests: Run the wycheproof test vectors, once I figure out
+ // how to work with JSON.
}
@(test)
@@ -340,13 +420,17 @@ test_rand_bytes :: proc(t: ^testing.T) {
mem.zero_explicit(raw_data(buf), len(buf))
crypto.rand_bytes(buf)
- if buf[0] != 0 && buf[len(buf)-1] != 0 {
+ if buf[0] != 0 && buf[len(buf) - 1] != 0 {
seems_ok = true
break
}
}
- expect(t, seems_ok, "Expected to randomize the head and tail of the buffer within a handful of attempts")
+ expect(
+ t,
+ seems_ok,
+ "Expected to randomize the head and tail of the buffer within a handful of attempts",
+ )
}
@(test)
@@ -359,29 +443,44 @@ bench_modern :: proc(t: ^testing.T) {
bench_x25519(t)
}
-_setup_sized_buf :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
+_setup_sized_buf :: proc(
+ options: ^time.Benchmark_Options,
+ allocator := context.allocator,
+) -> (
+ err: time.Benchmark_Error,
+) {
assert(options != nil)
options.input = make([]u8, options.bytes, allocator)
return nil if len(options.input) == options.bytes else .Allocation_Error
}
-_teardown_sized_buf :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
+_teardown_sized_buf :: proc(
+ options: ^time.Benchmark_Options,
+ allocator := context.allocator,
+) -> (
+ err: time.Benchmark_Error,
+) {
assert(options != nil)
delete(options.input)
return nil
}
-_benchmark_chacha20 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
+_benchmark_chacha20 :: proc(
+ options: ^time.Benchmark_Options,
+ allocator := context.allocator,
+) -> (
+ err: time.Benchmark_Error,
+) {
buf := options.input
- key := [chacha20.KEY_SIZE]byte{
+ key := [chacha20.KEY_SIZE]byte {
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
}
- nonce := [chacha20.NONCE_SIZE]byte{
+ nonce := [chacha20.NONCE_SIZE]byte {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
@@ -389,17 +488,22 @@ _benchmark_chacha20 :: proc(options: ^time.Benchmark_Options, allocator := conte
ctx: chacha20.Context = ---
chacha20.init(&ctx, key[:], nonce[:])
- for _ in 0..=options.rounds {
+ for _ in 0 ..= options.rounds {
chacha20.xor_bytes(&ctx, buf, buf)
}
- options.count = options.rounds
+ options.count = options.rounds
options.processed = options.rounds * options.bytes
return nil
}
-_benchmark_poly1305 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
+_benchmark_poly1305 :: proc(
+ options: ^time.Benchmark_Options,
+ allocator := context.allocator,
+) -> (
+ err: time.Benchmark_Error,
+) {
buf := options.input
- key := [poly1305.KEY_SIZE]byte{
+ key := [poly1305.KEY_SIZE]byte {
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
@@ -407,40 +511,46 @@ _benchmark_poly1305 :: proc(options: ^time.Benchmark_Options, allocator := conte
}
tag: [poly1305.TAG_SIZE]byte = ---
- for _ in 0..=options.rounds {
+ for _ in 0 ..= options.rounds {
poly1305.sum(tag[:], buf, key[:])
}
- options.count = options.rounds
+ options.count = options.rounds
options.processed = options.rounds * options.bytes
//options.hash = u128(h)
return nil
}
-_benchmark_chacha20poly1305 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
+_benchmark_chacha20poly1305 :: proc(
+ options: ^time.Benchmark_Options,
+ allocator := context.allocator,
+) -> (
+ err: time.Benchmark_Error,
+) {
buf := options.input
- key := [chacha20.KEY_SIZE]byte{
+ key := [chacha20.KEY_SIZE]byte {
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
}
- nonce := [chacha20.NONCE_SIZE]byte{
+ nonce := [chacha20.NONCE_SIZE]byte {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
tag: [chacha20poly1305.TAG_SIZE]byte = ---
- for _ in 0..=options.rounds {
- chacha20poly1305.encrypt(buf,tag[:], key[:], nonce[:], nil, buf)
+ for _ in 0 ..= options.rounds {
+ chacha20poly1305.encrypt(buf, tag[:], key[:], nonce[:], nil, buf)
}
- options.count = options.rounds
+ options.count = options.rounds
options.processed = options.rounds * options.bytes
return nil
}
benchmark_print :: proc(name: string, options: ^time.Benchmark_Options) {
- fmt.printf("\t[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
+ fmt.printf(
+ "\t[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
name,
options.rounds,
options.processed,
@@ -451,16 +561,16 @@ benchmark_print :: proc(name: string, options: ^time.Benchmark_Options) {
}
bench_chacha20 :: proc(t: ^testing.T) {
- name := "ChaCha20 64 bytes"
- options := &time.Benchmark_Options{
- rounds = 1_000,
- bytes = 64,
- setup = _setup_sized_buf,
- bench = _benchmark_chacha20,
+ name := "ChaCha20 64 bytes"
+ options := &time.Benchmark_Options {
+ rounds = 1_000,
+ bytes = 64,
+ setup = _setup_sized_buf,
+ bench = _benchmark_chacha20,
teardown = _teardown_sized_buf,
}
- err := time.benchmark(options, context.allocator)
+ err := time.benchmark(options, context.allocator)
expect(t, err == nil, name)
benchmark_print(name, options)
@@ -478,16 +588,16 @@ bench_chacha20 :: proc(t: ^testing.T) {
}
bench_poly1305 :: proc(t: ^testing.T) {
- name := "Poly1305 64 zero bytes"
- options := &time.Benchmark_Options{
- rounds = 1_000,
- bytes = 64,
- setup = _setup_sized_buf,
- bench = _benchmark_poly1305,
+ name := "Poly1305 64 zero bytes"
+ options := &time.Benchmark_Options {
+ rounds = 1_000,
+ bytes = 64,
+ setup = _setup_sized_buf,
+ bench = _benchmark_poly1305,
teardown = _teardown_sized_buf,
}
- err := time.benchmark(options, context.allocator)
+ err := time.benchmark(options, context.allocator)
expect(t, err == nil, name)
benchmark_print(name, options)
@@ -499,16 +609,16 @@ bench_poly1305 :: proc(t: ^testing.T) {
}
bench_chacha20poly1305 :: proc(t: ^testing.T) {
- name := "chacha20poly1305 64 bytes"
- options := &time.Benchmark_Options{
- rounds = 1_000,
- bytes = 64,
- setup = _setup_sized_buf,
- bench = _benchmark_chacha20poly1305,
+ name := "chacha20poly1305 64 bytes"
+ options := &time.Benchmark_Options {
+ rounds = 1_000,
+ bytes = 64,
+ setup = _setup_sized_buf,
+ bench = _benchmark_chacha20poly1305,
teardown = _teardown_sized_buf,
}
- err := time.benchmark(options, context.allocator)
+ err := time.benchmark(options, context.allocator)
expect(t, err == nil, name)
benchmark_print(name, options)
@@ -526,8 +636,11 @@ bench_chacha20poly1305 :: proc(t: ^testing.T) {
}
bench_x25519 :: proc(t: ^testing.T) {
- point := _decode_hex32("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
- scalar := _decode_hex32("cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe")
+ point_str := "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
+ scalar_str := "cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe"
+
+ point, _ := hex.decode(transmute([]byte)(point_str), context.temp_allocator)
+ scalar, _ := hex.decode(transmute([]byte)(scalar_str), context.temp_allocator)
out: [x25519.POINT_SIZE]byte = ---
iters :: 10000
@@ -537,5 +650,8 @@ bench_x25519 :: proc(t: ^testing.T) {
}
elapsed := time.since(start)
- log(t, fmt.tprintf("x25519.scalarmult: ~%f us/op", time.duration_microseconds(elapsed) / iters))
+ log(
+ t,
+ fmt.tprintf("x25519.scalarmult: ~%f us/op", time.duration_microseconds(elapsed) / iters),
+ )
}