core/crypto/chacha20poly1305: Support AEAD_XChaCha20_Poly1305

IETF-draft flavor (32-bit counter) though this makes no practical
difference.
This commit is contained in:
Yawning Angel
2024-08-10 18:32:37 +09:00
parent 8efc98ce90
commit 14ceb0b19d
2 changed files with 92 additions and 10 deletions
+62
View File
@@ -45,6 +45,7 @@ test_chacha20 :: proc(t: ^testing.T) {
for impl in impls {
test_chacha20_stream(t, impl)
test_chacha20poly1305(t, impl)
test_xchacha20poly1305(t, impl)
}
}
@@ -283,6 +284,67 @@ test_chacha20poly1305 :: proc(t: ^testing.T, impl: chacha20.Implementation) {
testing.expectf(t, !ok, "chacha20poly1305/%v: Expected false for open(tag, bad_aad, ciphertext)", impl)
}
test_xchacha20poly1305 :: proc(t: ^testing.T, impl: chacha20.Implementation) {
// Test case taken from:
// - https://datatracker.ietf.org/doc/html/draft-arciszewski-xchacha-03
key_str := "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
iv_str := "404142434445464748494a4b4c4d4e4f5051525354555657"
aad_str := "50515253c0c1c2c3c4c5c6c7"
plaintext_str := "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e"
ciphertext_str := "bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b4522f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff921f9664c97637da9768812f615c68b13b52e"
tag_str := "c0875924c1c7987947deafd8780acf49"
key, _ := hex.decode(transmute([]byte)(key_str), context.temp_allocator)
iv, _ := hex.decode(transmute([]byte)(iv_str), context.temp_allocator)
aad, _ := hex.decode(transmute([]byte)(aad_str), context.temp_allocator)
plaintext, _ := hex.decode(transmute([]byte)(plaintext_str), context.temp_allocator)
ciphertext, _ := hex.decode(transmute([]byte)(ciphertext_str), context.temp_allocator)
tag, _ := hex.decode(transmute([]byte)(tag_str), context.temp_allocator)
tag_ := make([]byte, len(tag), context.temp_allocator)
dst := make([]byte, len(ciphertext), context.temp_allocator)
ctx: chacha20poly1305.Context
chacha20poly1305.init_xchacha(&ctx, key, impl)
chacha20poly1305.seal(&ctx, dst, tag_, iv, aad, plaintext)
dst_str := string(hex.encode(dst, context.temp_allocator))
tag_str_ := string(hex.encode(tag_, context.temp_allocator))
testing.expectf(
t,
dst_str == ciphertext_str && tag_str_ == tag_str,
"xchacha20poly1305/%v: Expected: (%s, %s) for seal(%s, %s, %s, %s), but got (%s, %s) instead",
impl,
ciphertext_str,
tag_str,
key_str,
iv_str,
aad_str,
plaintext_str,
dst_str,
tag_str_,
)
ok := chacha20poly1305.open(&ctx, dst, iv, aad, ciphertext, tag)
dst_str = string(hex.encode(dst, context.temp_allocator))
testing.expectf(
t,
ok && dst_str == plaintext_str,
"xchacha20poly1305/%v: Expected: (%s, true) for open(%s, %s, %s, %s, %s), but got (%s, %v) instead",
impl,
plaintext_str,
key_str,
iv_str,
aad_str,
ciphertext_str,
tag_str,
dst_str,
ok,
)
}
@(test)
test_rand_bytes :: proc(t: ^testing.T) {
if !crypto.HAS_RAND_BYTES {