mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-28 18:30:06 +00:00
core/crypto/kmac: Initial import
This commit is contained in:
@@ -4,6 +4,7 @@ import "core:encoding/hex"
|
||||
import "core:fmt"
|
||||
import "core:testing"
|
||||
|
||||
import "core:crypto/kmac"
|
||||
import "core:crypto/shake"
|
||||
import "core:crypto/tuplehash"
|
||||
|
||||
@@ -14,6 +15,7 @@ test_sha3_variants :: proc(t: ^testing.T) {
|
||||
test_shake(t)
|
||||
test_cshake(t)
|
||||
test_tuplehash(t)
|
||||
test_kmac(t)
|
||||
}
|
||||
|
||||
@(test)
|
||||
@@ -339,3 +341,99 @@ test_tuplehash :: proc(t: ^testing.T) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_kmac :: proc(t:^testing.T) {
|
||||
log(t, "Testing KMAC")
|
||||
|
||||
test_vectors := []struct {
|
||||
sec_strength: int,
|
||||
key: string,
|
||||
domainsep: string,
|
||||
msg: string,
|
||||
output: string,
|
||||
} {
|
||||
// KMAC128
|
||||
// - https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/KMAC_samples.pdf
|
||||
{
|
||||
128,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"",
|
||||
"00010203",
|
||||
"e5780b0d3ea6f7d3a429c5706aa43a00fadbd7d49628839e3187243f456ee14e",
|
||||
},
|
||||
{
|
||||
128,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"My Tagged Application",
|
||||
"00010203",
|
||||
"3b1fba963cd8b0b59e8c1a6d71888b7143651af8ba0a7070c0979e2811324aa5",
|
||||
},
|
||||
{
|
||||
128,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"My Tagged Application",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7",
|
||||
"1f5b4e6cca02209e0dcb5ca635b89a15e271ecc760071dfd805faa38f9729230",
|
||||
},
|
||||
|
||||
// KMAC256
|
||||
// - https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/KMAC_samples.pdf
|
||||
{
|
||||
256,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"My Tagged Application",
|
||||
"00010203",
|
||||
"20c570c31346f703c9ac36c61c03cb64c3970d0cfc787e9b79599d273a68d2f7f69d4cc3de9d104a351689f27cf6f5951f0103f33f4f24871024d9c27773a8dd",
|
||||
},
|
||||
{
|
||||
256,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7",
|
||||
"75358cf39e41494e949707927cee0af20a3ff553904c86b08f21cc414bcfd691589d27cf5e15369cbbff8b9a4c2eb17800855d0235ff635da82533ec6b759b69",
|
||||
},
|
||||
{
|
||||
256,
|
||||
"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f",
|
||||
"My Tagged Application",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7",
|
||||
"b58618f71f92e1d56c1b8c55ddd7cd188b97b4ca4d99831eb2699a837da2e4d970fbacfde50033aea585f1a2708510c32d07880801bd182898fe476876fc8965",
|
||||
},
|
||||
}
|
||||
|
||||
for v in test_vectors {
|
||||
dst := make([]byte, len(v.output) / 2, context.temp_allocator)
|
||||
|
||||
key, _ := hex.decode(transmute([]byte)(v.key))
|
||||
domainsep := transmute([]byte)(v.domainsep)
|
||||
|
||||
ctx: kmac.Context
|
||||
switch v.sec_strength {
|
||||
case 128:
|
||||
kmac.init_128(&ctx, key, domainsep)
|
||||
case 256:
|
||||
kmac.init_256(&ctx, key, domainsep)
|
||||
}
|
||||
|
||||
data, _ := hex.decode(transmute([]byte)(v.msg))
|
||||
kmac.update(&ctx, data)
|
||||
kmac.final(&ctx, dst)
|
||||
|
||||
dst_str := string(hex.encode(dst, context.temp_allocator))
|
||||
|
||||
expect(
|
||||
t,
|
||||
dst_str == v.output,
|
||||
fmt.tprintf(
|
||||
"KMAC%d: Expected: %s for input of (%s, %s, %s), but got %s instead",
|
||||
v.sec_strength,
|
||||
v.output,
|
||||
v.key,
|
||||
v.domainsep,
|
||||
v.msg,
|
||||
dst_str,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user