core/crypto/aegis: Initial import

This commit is contained in:
Yawning Angel
2025-03-23 19:14:33 +09:00
parent 9fdcc4e39a
commit c2786a6dd5
11 changed files with 1441 additions and 86 deletions
@@ -8,6 +8,7 @@ import "core:strings"
import "core:testing"
import "core:time"
import "core:crypto/aegis"
import "core:crypto/aes"
import "core:crypto/chacha20"
import "core:crypto/chacha20poly1305"
@@ -164,6 +165,43 @@ benchmark_crypto :: proc(t: ^testing.T) {
testing.expect(t, err == nil, name)
benchmark_print(&str, name, options)
}
{
name := "AEGIS-256 64 bytes"
options := &time.Benchmark_Options {
rounds = 1_000,
bytes = 64,
setup = _setup_sized_buf,
bench = _benchmark_aegis_256,
teardown = _teardown_sized_buf,
}
key := [aegis.KEY_SIZE_256]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,
}
ctx: aegis.Context
aegis.init(&ctx, key[:])
context.user_ptr = &ctx
err := time.benchmark(options, context.allocator)
testing.expect(t, err == nil, name)
benchmark_print(&str, name, options)
name = "AEGIS-256 1024 bytes"
options.bytes = 1024
err = time.benchmark(options, context.allocator)
testing.expect(t, err == nil, name)
benchmark_print(&str, name, options)
name = "AEGIS-256 65536 bytes"
options.bytes = 65536
err = time.benchmark(options, context.allocator)
testing.expect(t, err == nil, name)
benchmark_print(&str, name, options)
}
{
iters :: 10000
@@ -423,6 +461,26 @@ _benchmark_aes256_gcm :: proc(
return nil
}
_benchmark_aegis_256 :: proc(
options: ^time.Benchmark_Options,
allocator := context.allocator,
) -> (
err: time.Benchmark_Error,
) {
buf := options.input
iv: [aegis.IV_SIZE_256]byte
tag: [aegis.TAG_SIZE_128]byte = ---
ctx := (^aegis.Context)(context.user_ptr)
for _ in 0 ..= options.rounds {
aegis.seal(ctx, buf, tag[:], iv[:], nil, buf)
}
options.count = options.rounds
options.processed = options.rounds * options.bytes
return nil
}
@(private)
benchmark_print :: proc(str: ^strings.Builder, name: string, options: ^time.Benchmark_Options, loc := #caller_location) {
fmt.sbprintfln(str, "[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
@@ -1,6 +1,8 @@
package test_core_crypto
import "base:runtime"
import "core:crypto/aes"
import "core:crypto/aegis"
import "core:crypto/aead"
import "core:encoding/hex"
import "core:testing"
@@ -17,6 +19,10 @@ test_aead :: proc(t: ^testing.T) {
for impl in supported_chacha_impls() {
append(&chacha_impls, impl)
}
aegis_impls := make([dynamic]aead.Implementation, context.temp_allocator)
for impl in supported_aegis_impls() {
append(&aegis_impls, impl)
}
impls := [aead.Algorithm][dynamic]aead.Implementation{
.Invalid = nil,
.AES_GCM_128 = aes_impls,
@@ -24,6 +30,10 @@ test_aead :: proc(t: ^testing.T) {
.AES_GCM_256 = aes_impls,
.CHACHA20POLY1305 = chacha_impls,
.XCHACHA20POLY1305 = chacha_impls,
.AEGIS_128L = aegis_impls,
.AEGIS_128L_256 = aegis_impls,
.AEGIS_256 = aegis_impls,
.AEGIS_256_256 = aegis_impls,
}
test_vectors := []struct{
@@ -224,6 +234,190 @@ test_aead :: proc(t: ^testing.T) {
"bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b4522f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff921f9664c97637da9768812f615c68b13b52e",
"c0875924c1c7987947deafd8780acf49",
},
// AEGIS-128L
// https://www.ietf.org/archive/id/draft-irtf-cfrg-aegis-aead-11.txt
{
.AEGIS_128L,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"",
"00000000000000000000000000000000",
"c1c0e58bd913006feba00f4b3cc3594e",
"abe0ece80c24868a226a35d16bdae37a",
},
{
.AEGIS_128L_256,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"",
"00000000000000000000000000000000",
"c1c0e58bd913006feba00f4b3cc3594e",
"25835bfbb21632176cf03840687cb968cace4617af1bd0f7d064c639a5c79ee4",
},
{
.AEGIS_128L,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"",
"",
"",
"c2b879a67def9d74e6c14f708bbcc9b4",
},
{
.AEGIS_128L_256,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"",
"",
"",
"1360dc9db8ae42455f6e5b6a9d488ea4f2184c4e12120249335c4ee84bafe25d",
},
{
.AEGIS_128L,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"79d94593d8c2119d7e8fd9b8fc77845c5c077a05b2528b6ac54b563aed8efe84",
"cc6f3372f6aa1bb82388d695c3962d9a",
},
{
.AEGIS_128L_256,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"79d94593d8c2119d7e8fd9b8fc77845c5c077a05b2528b6ac54b563aed8efe84",
"022cb796fe7e0ae1197525ff67e309484cfbab6528ddef89f17d74ef8ecd82b3",
},
{
.AEGIS_128L,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d",
"79d94593d8c2119d7e8fd9b8fc77",
"5c04b3dba849b2701effbe32c7f0fab7",
},
{
.AEGIS_128L_256,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d",
"79d94593d8c2119d7e8fd9b8fc77",
"86f1b80bfb463aba711d15405d094baf4a55a15dbfec81a76f35ed0b9c8b04ac",
},
{
.AEGIS_128L,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223242526272829",
"101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637",
"b31052ad1cca4e291abcf2df3502e6bdb1bfd6db36798be3607b1f94d34478aa7ede7f7a990fec10",
"7542a745733014f9474417b337399507",
},
{
.AEGIS_128L_256,
"10010000000000000000000000000000",
"10000200000000000000000000000000",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223242526272829",
"101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637",
"b31052ad1cca4e291abcf2df3502e6bdb1bfd6db36798be3607b1f94d34478aa7ede7f7a990fec10",
"b91e2947a33da8bee89b6794e647baf0fc835ff574aca3fc27c33be0db2aff98",
},
// AEGIS-256
// https://www.ietf.org/archive/id/draft-irtf-cfrg-aegis-aead-11.txt
{
.AEGIS_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"",
"00000000000000000000000000000000",
"754fc3d8c973246dcc6d741412a4b236",
"3fe91994768b332ed7f570a19ec5896e",
},
{
.AEGIS_256_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"",
"00000000000000000000000000000000",
"754fc3d8c973246dcc6d741412a4b236",
"1181a1d18091082bf0266f66297d167d2e68b845f61a3b0527d31fc7b7b89f13",
},
{
.AEGIS_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"",
"",
"",
"e3def978a0f054afd1e761d7553afba3",
},
{
.AEGIS_256_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"",
"",
"",
"6a348c930adbd654896e1666aad67de989ea75ebaa2b82fb588977b1ffec864a",
},
{
.AEGIS_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"f373079ed84b2709faee373584585d60accd191db310ef5d8b11833df9dec711",
"8d86f91ee606e9ff26a01b64ccbdd91d",
},
{
.AEGIS_256_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"f373079ed84b2709faee373584585d60accd191db310ef5d8b11833df9dec711",
"b7d28d0c3c0ebd409fd22b44160503073a547412da0854bfb9723020dab8da1a",
},
{
.AEGIS_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d",
"f373079ed84b2709faee37358458",
"c60b9c2d33ceb058f96e6dd03c215652",
},
{
.AEGIS_256_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"0001020304050607",
"000102030405060708090a0b0c0d",
"f373079ed84b2709faee37358458",
"8c1cc703c81281bee3f6d9966e14948b4a175b2efbdc31e61a98b4465235c2d9",
},
{
.AEGIS_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223242526272829",
"101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637",
"57754a7d09963e7c787583a2e7b859bb24fa1e04d49fd550b2511a358e3bca252a9b1b8b30cc4a67",
"ab8a7d53fd0e98d727accca94925e128",
},
{
.AEGIS_256_256,
"1001000000000000000000000000000000000000000000000000000000000000",
"1000020000000000000000000000000000000000000000000000000000000000",
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223242526272829",
"101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637",
"57754a7d09963e7c787583a2e7b859bb24fa1e04d49fd550b2511a358e3bca252a9b1b8b30cc4a67",
"a3aca270c006094d71c20e6910b5161c0826df233d08919a566ec2c05990f734",
},
}
for v, _ in test_vectors {
algo_name := aead.ALGORITHM_NAMES[v.algo]
@@ -337,3 +531,13 @@ test_aead :: proc(t: ^testing.T) {
}
}
}
supported_aegis_impls :: proc() -> [dynamic]aes.Implementation {
impls := make([dynamic]aes.Implementation, 0, 2, context.temp_allocator)
append(&impls, aes.Implementation.Portable)
if aegis.is_hardware_accelerated() {
append(&impls, aes.Implementation.Hardware)
}
return impls
}