authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-11-03 02:01:48+01:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-11-03 02:01:48+01:00
logd764636d21f9aafa1e9a33f625156a829cc5d747
treeeb3957e35a041dcd5f299a1d4df703fa318612d2
parentb30a765b95cf666409c1e5324718a7c8c60af4af

Another big-endian fix for Gimli

We read and write bytes directly from the state, but in the init function, we potentially endian-swap them. Initialize bytes in native format since we will be reading them in native format as well later. Also use the public interface in the "permute" test rather than an internal interface. The state itself is not meant to be accessed directly, even in tests.

1 files changed, 5 insertions(+), 27 deletions(-)

lib/std/crypto/gimli.zig+5-27
...@@ -33,7 +33,7 @@ pub const State = struct {...@@ -33,7 +33,7 @@ pub const State = struct {
33 var data: [BLOCKBYTES / 4]u32 = undefined;33 var data: [BLOCKBYTES / 4]u32 = undefined;
34 var i: usize = 0;34 var i: usize = 0;
35 while (i < State.BLOCKBYTES) : (i += 4) {35 while (i < State.BLOCKBYTES) : (i += 4) {
36 data[i / 4] = mem.readIntLittle(u32, initial_state[i..][0..4]);36 data[i / 4] = mem.readIntNative(u32, initial_state[i..][0..4]);
37 }37 }
38 return Self{ .data = data };38 return Self{ .data = data };
39 }39 }
...@@ -184,27 +184,11 @@ pub const State = struct {...@@ -184,27 +184,11 @@ pub const State = struct {
184184
185test "permute" {185test "permute" {
186 // test vector from gimli-20170627186 // test vector from gimli-20170627
187 var state = State{187 var input: [48]u8 = undefined;
188 .data = blk: {188 try std.fmt.hexToBytes(input[0..], "00000000ba79379e7af36e3c466da6da24e7dd781a6115172edb4cb566558453c8cfbbf15a4af38f22c52a2e264062cc");
189 var input: [12]u32 = undefined;189 var state = State.init(input);
190 var i = @as(u32, 0);
191 while (i < 12) : (i += 1) {
192 input[i] = i * i * i + i *% 0x9e3779b9;
193 }
194 testing.expectEqualSlices(u32, &input, &[_]u32{
195 0x00000000, 0x9e3779ba, 0x3c6ef37a, 0xdaa66d46,
196 0x78dde724, 0x1715611a, 0xb54cdb2e, 0x53845566,
197 0xf1bbcfc8, 0x8ff34a5a, 0x2e2ac522, 0xcc624026,
198 });
199 break :blk input;
200 },
201 };
202 state.permute();190 state.permute();
203 testing.expectEqualSlices(u32, &state.data, &[_]u32{191 htest.assertEqual("5ac811ba19d1ba9180e80c38682c4cd2eaffce3e1c927a27bda0734fd89c5adaf073b684f72fe53449ef2b9ed6b81bf4", state.toSliceConst());
204 0xba11c85a, 0x91bad119, 0x380ce880, 0xd24c2c68,
205 0x3eceffea, 0x277a921c, 0x4f73a0bd, 0xda5a9cd8,
206 0x84b673f0, 0x34e52ff7, 0x9e2bef49, 0xf41bb8d6,
207 });
208}192}
209193
210pub const Hash = struct {194pub const Hash = struct {
...@@ -269,9 +253,6 @@ pub fn hash(out: *[Hash.digest_length]u8, in: []const u8, options: Hash.Options)...@@ -269,9 +253,6 @@ pub fn hash(out: *[Hash.digest_length]u8, in: []const u8, options: Hash.Options)
269}253}
270254
271test "hash" {255test "hash" {
272 // https://github.com/ziglang/zig/issues/5127
273 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
274
275 // a test vector (30) from NIST KAT submission.256 // a test vector (30) from NIST KAT submission.
276 var msg: [58 / 2]u8 = undefined;257 var msg: [58 / 2]u8 = undefined;
277 try std.fmt.hexToBytes(&msg, "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C");258 try std.fmt.hexToBytes(&msg, "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C");
...@@ -423,9 +404,6 @@ pub const Aead = struct {...@@ -423,9 +404,6 @@ pub const Aead = struct {
423};404};
424405
425test "cipher" {406test "cipher" {
426 // https://github.com/ziglang/zig/issues/5127
427 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
428
429 var key: [32]u8 = undefined;407 var key: [32]u8 = undefined;
430 try std.fmt.hexToBytes(&key, "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");408 try std.fmt.hexToBytes(&key, "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
431 var nonce: [16]u8 = undefined;409 var nonce: [16]u8 = undefined;