| ... | @@ -823,6 +823,44 @@ fn AegisMac(comptime T: type) type { | ... | @@ -823,6 +823,44 @@ fn AegisMac(comptime T: type) type { |
| 823 | const htest = @import("test.zig"); | 823 | const htest = @import("test.zig"); |
| 824 | const testing = std.testing; | 824 | const testing = std.testing; |
| 825 | | 825 | |
| | 826 | fn hashStateOutputs(comptime State: type, key: anytype, nonce: anytype) [32]u8 { |
| | 827 | var msg: [State.rate]u8 = undefined; |
| | 828 | for (&msg, 0..) |*x, i| x.* = @truncate(i *% 31 +% 5); |
| | 829 | var out: [State.rate]u8 = undefined; |
| | 830 | var h: crypto.hash.sha2.Sha256 = .init(.{}); |
| | 831 | |
| | 832 | var st = State.init(key, nonce); |
| | 833 | st.absorb(&msg); |
| | 834 | st.enc(&out, &msg); |
| | 835 | h.update(&out); |
| | 836 | st.dec(&out, &msg); |
| | 837 | h.update(&out); |
| | 838 | st.decLast(out[3..], msg[3..]); |
| | 839 | h.update(out[3..]); |
| | 840 | inline for (.{ 128, 256 }) |tag_bits| { |
| | 841 | var st2 = st; |
| | 842 | h.update(&st2.finalize(tag_bits, 33, 12345)); |
| | 843 | st2 = st; |
| | 844 | h.update(&st2.finalizeMac(tag_bits, 54321)); |
| | 845 | } |
| | 846 | return h.finalResult(); |
| | 847 | } |
| | 848 | |
| | 849 | test "Aegis bitsliced states" { |
| | 850 | const key = "0123456789abcdef0123456789ABCDEF"; |
| | 851 | const nonce = "nonce0123456789Nnonce0123456789N"; |
| | 852 | inline for (.{ 1, 2, 4 }) |degree| { |
| | 853 | try testing.expectEqual( |
| | 854 | hashStateOutputs(State128X(degree), key[0..16].*, nonce[0..16].*), |
| | 855 | hashStateOutputs(bitsliced.State128X(degree), key[0..16].*, nonce[0..16].*), |
| | 856 | ); |
| | 857 | try testing.expectEqual( |
| | 858 | hashStateOutputs(State256X(degree), key.*, nonce.*), |
| | 859 | hashStateOutputs(bitsliced.State256X(degree), key.*, nonce.*), |
| | 860 | ); |
| | 861 | } |
| | 862 | } |
| | 863 | |
| 826 | test "Aegis128L test vector 1" { | 864 | test "Aegis128L test vector 1" { |
| 827 | const key: [Aegis128L.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ @as([14]u8, @splat(0x00)); | 865 | const key: [Aegis128L.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ @as([14]u8, @splat(0x00)); |
| 828 | const nonce: [Aegis128L.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ @as([13]u8, @splat(0x00)); | 866 | const nonce: [Aegis128L.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ @as([13]u8, @splat(0x00)); |