Skip to content

Commit 06b1ffc

Browse files
committed
Replace test vectors from Wycheproof by vectors from Rooterberg
1 parent 5b16f87 commit 06b1ffc

File tree

7 files changed

+41
-79
lines changed

7 files changed

+41
-79
lines changed

src/test/main.zig

Lines changed: 41 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -926,101 +926,63 @@ const JsonTest = struct {
926926
msg: []const u8,
927927
ct: []const u8,
928928
tag: []const u8,
929-
result: []const u8,
930-
};
931-
const JsonTestGroup = struct {
932-
type: []const u8,
933-
tests: []const JsonTest,
929+
valid: bool,
934930
};
935931
const JsonTests = struct {
936-
testGroups: []JsonTestGroup,
937-
};
938-
const Result = enum {
939-
valid,
940-
invalid,
932+
testType: []const u8,
933+
algorithm: struct {
934+
primitive: []const u8,
935+
keySize: usize,
936+
ivSize: usize,
937+
tagSize: usize,
938+
},
939+
tests: []const JsonTest,
941940
};
942941

943942
const heap = std.heap;
944943
const zstd = std.compress.zstd;
945944

946-
test "aegis128l - wycheproof" {
945+
fn rooterberg(comptime file: []const u8, comptime func: anytype) !void {
947946
const alloc = std.testing.allocator;
948-
var fbs = std.io.fixedBufferStream(@embedFile("wycheproof/aegis128L_test.json.zst"));
947+
var fbs = std.io.fixedBufferStream(@embedFile(file));
949948
var window_buffer: [zstd.DecompressorOptions.default_window_buffer_len]u8 = undefined;
950949
var decompressor = zstd.decompressor(fbs.reader(), .{ .window_buffer = &window_buffer });
951950
const json = try decompressor.reader().readAllAlloc(alloc, 1000000);
952951
defer alloc.free(json);
953952
const parsed = try std.json.parseFromSlice(JsonTests, alloc, json, .{ .ignore_unknown_fields = true });
954953
defer parsed.deinit();
955-
for (parsed.value.testGroups) |test_group| {
956-
if (!std.mem.eql(u8, "AeadTest", test_group.type)) continue;
957-
for (test_group.tests) |t| {
958-
var arena = heap.ArenaAllocator.init(alloc);
959-
defer arena.deinit();
960-
var arena_alloc = arena.allocator();
961-
var key: [16]u8 = undefined;
962-
var nonce: [16]u8 = undefined;
963-
var tag: [16]u8 = undefined;
964-
const aad = try arena_alloc.alloc(u8, t.aad.len / 2);
965-
const ct = try arena_alloc.alloc(u8, t.ct.len / 2);
966-
const msg = try arena_alloc.alloc(u8, t.msg.len / 2);
967-
const expected_msg = try arena_alloc.alloc(u8, t.msg.len / 2);
968-
_ = try std.fmt.hexToBytes(&key, t.key);
969-
_ = try std.fmt.hexToBytes(&nonce, t.iv);
970-
_ = try std.fmt.hexToBytes(&tag, t.tag);
971-
_ = try std.fmt.hexToBytes(aad, t.aad);
972-
_ = try std.fmt.hexToBytes(expected_msg, t.msg);
973-
_ = try std.fmt.hexToBytes(ct, t.ct);
974-
const c_res = aegis.aegis128l_decrypt_detached(msg.ptr, ct.ptr, ct.len, &tag, tag.len, aad.ptr, aad.len, &nonce, &key);
975-
const res: Result = if (c_res == 0) res: {
976-
if (std.mem.eql(u8, msg, expected_msg)) break :res .valid;
977-
break :res .invalid;
978-
} else .invalid;
979-
if ((std.mem.eql(u8, "invalid", t.result) and res == .valid) or (std.mem.eql(u8, "valid", t.result) and res == .invalid)) {
980-
std.debug.print("Test failed: {}\n", .{t.tcId});
981-
try std.testing.expect(false);
982-
}
954+
const tests = parsed.value;
955+
try std.testing.expectEqualSlices(u8, "Aead", tests.testType);
956+
for (tests.tests) |t| {
957+
var arena = heap.ArenaAllocator.init(alloc);
958+
defer arena.deinit();
959+
var arena_alloc = arena.allocator();
960+
const key = try arena_alloc.alloc(u8, tests.algorithm.keySize / 8);
961+
const nonce = try arena_alloc.alloc(u8, tests.algorithm.ivSize / 8);
962+
const tag = try arena_alloc.alloc(u8, tests.algorithm.tagSize / 8);
963+
const aad = try arena_alloc.alloc(u8, t.aad.len / 2);
964+
const ct = try arena_alloc.alloc(u8, t.ct.len / 2);
965+
const msg = try arena_alloc.alloc(u8, @max(ct.len, t.msg.len / 2));
966+
const expected_msg = try arena_alloc.alloc(u8, t.msg.len / 2);
967+
_ = try std.fmt.hexToBytes(key, t.key);
968+
_ = try std.fmt.hexToBytes(nonce, t.iv);
969+
_ = try std.fmt.hexToBytes(tag, t.tag);
970+
_ = try std.fmt.hexToBytes(aad, t.aad);
971+
_ = try std.fmt.hexToBytes(expected_msg, t.msg);
972+
_ = try std.fmt.hexToBytes(ct, t.ct);
973+
const c_res = func(msg.ptr, ct.ptr, ct.len, tag.ptr, tag.len, aad.ptr, aad.len, nonce.ptr, key.ptr);
974+
const valid = (c_res == 0);
975+
if (valid == true) try std.testing.expectEqualSlices(u8, msg, expected_msg);
976+
if (t.valid != valid) {
977+
std.debug.print("Test failed: {}\n", .{t.tcId});
978+
try std.testing.expect(false);
983979
}
984980
}
985981
}
986982

987-
test "aegis256 - wycheproof" {
988-
const alloc = std.testing.allocator;
989-
var fbs = std.io.fixedBufferStream(@embedFile("wycheproof/aegis256_test.json.zst"));
990-
var window_buffer: [zstd.DecompressorOptions.default_window_buffer_len]u8 = undefined;
991-
var decompressor = zstd.decompressor(fbs.reader(), .{ .window_buffer = &window_buffer });
992-
const json = try decompressor.reader().readAllAlloc(alloc, 1000000);
993-
defer alloc.free(json);
994-
const parsed = try std.json.parseFromSlice(JsonTests, alloc, json, .{ .ignore_unknown_fields = true });
995-
defer parsed.deinit();
996-
for (parsed.value.testGroups) |test_group| {
997-
if (!std.mem.eql(u8, "AeadTest", test_group.type)) continue;
998-
for (test_group.tests) |t| {
999-
var arena = heap.ArenaAllocator.init(alloc);
1000-
defer arena.deinit();
1001-
var arena_alloc = arena.allocator();
1002-
var key: [32]u8 = undefined;
1003-
var nonce: [32]u8 = undefined;
1004-
var tag: [16]u8 = undefined;
1005-
const aad = try arena_alloc.alloc(u8, t.aad.len / 2);
1006-
const ct = try arena_alloc.alloc(u8, t.ct.len / 2);
1007-
const msg = try arena_alloc.alloc(u8, t.msg.len / 2);
1008-
const expected_msg = try arena_alloc.alloc(u8, t.msg.len / 2);
1009-
_ = try std.fmt.hexToBytes(&key, t.key);
1010-
_ = try std.fmt.hexToBytes(&nonce, t.iv);
1011-
_ = try std.fmt.hexToBytes(&tag, t.tag);
1012-
_ = try std.fmt.hexToBytes(aad, t.aad);
1013-
_ = try std.fmt.hexToBytes(expected_msg, t.msg);
1014-
_ = try std.fmt.hexToBytes(ct, t.ct);
1015-
const c_res = aegis.aegis256_decrypt_detached(msg.ptr, ct.ptr, ct.len, &tag, tag.len, aad.ptr, aad.len, &nonce, &key);
1016-
const res: Result = if (c_res == 0) res: {
1017-
if (std.mem.eql(u8, msg, expected_msg)) break :res .valid;
1018-
break :res .invalid;
1019-
} else .invalid;
1020-
if ((std.mem.eql(u8, "invalid", t.result) and res == .valid) or (std.mem.eql(u8, "valid", t.result) and res == .invalid)) {
1021-
std.debug.print("Test failed: {}\n", .{t.tcId});
1022-
try std.testing.expect(false);
1023-
}
1024-
}
1025-
}
983+
test "rooterberg test vectors" {
984+
try rooterberg("wycheproof/aegis128_l.json.zst", aegis.aegis128l_decrypt_detached);
985+
try rooterberg("wycheproof/aegis128_l_256.json.zst", aegis.aegis128l_decrypt_detached);
986+
try rooterberg("wycheproof/aegis256.json.zst", aegis.aegis256_decrypt_detached);
987+
try rooterberg("wycheproof/aegis256_256.json.zst", aegis.aegis256_decrypt_detached);
1026988
}
-79 KB
Binary file not shown.
5.48 KB
Binary file not shown.
6.81 KB
Binary file not shown.

src/test/wycheproof/aegis256.json.zst

6.18 KB
Binary file not shown.
7.48 KB
Binary file not shown.
-90.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)