authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-20 22:36:57+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-20 23:02:10+02:00
log446597bd3c935be632287a8ad6cfd72892d674e6
treed80af39c7fd2036b2d832407cdb39c86e87cdf5b
parentf92a5d79440402233bd0215e2bb2aeeb4f333931

Remove the reset() function from hash functions

Justification: - reset() is unnecessary; states that have to be reused can be copied - reset() is error-prone. Copying a previous state prevents forgetting struct members. - reset() forces implementation to store sensitive data (key, initial state) in memory even when they are not needed. - reset() is confusing as it has a different meaning elsewhere in Zig.

9 files changed, 85 insertions(+), 134 deletions(-)

lib/std/crypto.zig+3-3
......@@ -112,11 +112,11 @@ test "issue #4532: no index out of bounds" {
112112 var block = [_]u8{'#'} ** Hasher.block_length;
113113 var out1: [Hasher.digest_length]u8 = undefined;
114114 var out2: [Hasher.digest_length]u8 = undefined;
115
116 var h = Hasher.init();
115 const h0 = Hasher.init();
116 var h = h0;
117117 h.update(block[0..]);
118118 h.final(out1[0..]);
119 h.reset();
119 h = h0;
120120 h.update(block[0..1]);
121121 h.update(block[1..]);
122122 h.final(out2[0..]);
lib/std/crypto/blake2.zig+30-44
......@@ -71,8 +71,6 @@ pub fn Blake2s(comptime out_len: usize) type {
7171 buf: [64]u8,
7272 buf_len: u8,
7373
74 key: []const u8,
75
7674 pub fn init() Self {
7775 return comptime init_keyed("");
7876 }
......@@ -80,25 +78,20 @@ pub fn Blake2s(comptime out_len: usize) type {
8078 pub fn init_keyed(key: []const u8) Self {
8179 debug.assert(8 <= out_len and out_len <= 512);
8280
83 var s: Self = undefined;
84 s.key = key;
85 s.reset();
86 return s;
87 }
88
89 pub fn reset(d: *Self) void {
81 var d: Self = undefined;
9082 mem.copy(u32, d.h[0..], iv[0..]);
9183
9284 // default parameters
93 d.h[0] ^= 0x01010000 ^ @truncate(u32, d.key.len << 8) ^ @intCast(u32, out_len >> 3);
85 d.h[0] ^= 0x01010000 ^ @truncate(u32, key.len << 8) ^ @intCast(u32, out_len >> 3);
9486 d.t = 0;
9587 d.buf_len = 0;
9688
97 if (d.key.len > 0) {
98 mem.set(u8, d.buf[d.key.len..], 0);
99 d.update(d.key);
89 if (key.len > 0) {
90 mem.set(u8, d.buf[key.len..], 0);
91 d.update(key);
10092 d.buf_len = 64;
10193 }
94 return d;
10295 }
10396
10497 pub fn hash(b: []const u8, out: []u8) void {
......@@ -225,12 +218,12 @@ test "blake2s224 streaming" {
225218
226219 const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55";
227220
228 h.reset();
221 h = Blake2s224.init();
229222 h.update("abc");
230223 h.final(out[0..]);
231224 htest.assertEqual(h2, out[0..]);
232225
233 h.reset();
226 h = Blake2s224.init();
234227 h.update("a");
235228 h.update("b");
236229 h.update("c");
......@@ -239,13 +232,13 @@ test "blake2s224 streaming" {
239232
240233 const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01";
241234
242 h.reset();
235 h = Blake2s224.init();
243236 h.update("a" ** 32);
244237 h.update("b" ** 32);
245238 h.final(out[0..]);
246239 htest.assertEqual(h3, out[0..]);
247240
248 h.reset();
241 h = Blake2s224.init();
249242 h.update("a" ** 32 ++ "b" ** 32);
250243 h.final(out[0..]);
251244 htest.assertEqual(h3, out[0..]);
......@@ -294,12 +287,12 @@ test "blake2s256 streaming" {
294287
295288 const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982";
296289
297 h.reset();
290 h = Blake2s256.init();
298291 h.update("abc");
299292 h.final(out[0..]);
300293 htest.assertEqual(h2, out[0..]);
301294
302 h.reset();
295 h = Blake2s256.init();
303296 h.update("a");
304297 h.update("b");
305298 h.update("c");
......@@ -308,13 +301,13 @@ test "blake2s256 streaming" {
308301
309302 const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977";
310303
311 h.reset();
304 h = Blake2s256.init();
312305 h.update("a" ** 32);
313306 h.update("b" ** 32);
314307 h.final(out[0..]);
315308 htest.assertEqual(h3, out[0..]);
316309
317 h.reset();
310 h = Blake2s256.init();
318311 h.update("a" ** 32 ++ "b" ** 32);
319312 h.final(out[0..]);
320313 htest.assertEqual(h3, out[0..]);
......@@ -335,7 +328,7 @@ test "blake2s256 keyed" {
335328
336329 htest.assertEqual(h1, out[0..]);
337330
338 h.reset();
331 h = Blake2s256.init_keyed(key);
339332 h.update("a" ** 64);
340333 h.update("b" ** 64);
341334 h.final(out[0..]);
......@@ -406,8 +399,6 @@ pub fn Blake2b(comptime out_len: usize) type {
406399 buf: [128]u8,
407400 buf_len: u8,
408401
409 key: []const u8,
410
411402 pub fn init() Self {
412403 return init_keyed("");
413404 }
......@@ -415,25 +406,20 @@ pub fn Blake2b(comptime out_len: usize) type {
415406 pub fn init_keyed(key: []const u8) Self {
416407 debug.assert(8 <= out_len and out_len <= 512);
417408
418 var s: Self = undefined;
419 s.key = key;
420 s.reset();
421 return s;
422 }
423
424 pub fn reset(d: *Self) void {
409 var d: Self = undefined;
425410 mem.copy(u64, d.h[0..], iv[0..]);
426411
427412 // default parameters
428 d.h[0] ^= 0x01010000 ^ (d.key.len << 8) ^ (out_len >> 3);
413 d.h[0] ^= 0x01010000 ^ (key.len << 8) ^ (out_len >> 3);
429414 d.t = 0;
430415 d.buf_len = 0;
431416
432 if (d.key.len > 0) {
433 mem.set(u8, d.buf[d.key.len..], 0);
434 d.update(d.key);
417 if (key.len > 0) {
418 mem.set(u8, d.buf[key.len..], 0);
419 d.update(key);
435420 d.buf_len = 128;
436421 }
422 return d;
437423 }
438424
439425 pub fn hash(b: []const u8, out: []u8) void {
......@@ -558,12 +544,12 @@ test "blake2b384 streaming" {
558544
559545 const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4";
560546
561 h.reset();
547 h = Blake2b384.init();
562548 h.update("abc");
563549 h.final(out[0..]);
564550 htest.assertEqual(h2, out[0..]);
565551
566 h.reset();
552 h = Blake2b384.init();
567553 h.update("a");
568554 h.update("b");
569555 h.update("c");
......@@ -572,12 +558,12 @@ test "blake2b384 streaming" {
572558
573559 const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c";
574560
575 h.reset();
561 h = Blake2b384.init();
576562 h.update("a" ** 64 ++ "b" ** 64);
577563 h.final(out[0..]);
578564 htest.assertEqual(h3, out[0..]);
579565
580 h.reset();
566 h = Blake2b384.init();
581567 h.update("a" ** 64);
582568 h.update("b" ** 64);
583569 h.final(out[0..]);
......@@ -627,12 +613,12 @@ test "blake2b512 streaming" {
627613
628614 const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
629615
630 h.reset();
616 h = Blake2b512.init();
631617 h.update("abc");
632618 h.final(out[0..]);
633619 htest.assertEqual(h2, out[0..]);
634620
635 h.reset();
621 h = Blake2b512.init();
636622 h.update("a");
637623 h.update("b");
638624 h.update("c");
......@@ -641,12 +627,12 @@ test "blake2b512 streaming" {
641627
642628 const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920";
643629
644 h.reset();
630 h = Blake2b512.init();
645631 h.update("a" ** 64 ++ "b" ** 64);
646632 h.final(out[0..]);
647633 htest.assertEqual(h3, out[0..]);
648634
649 h.reset();
635 h = Blake2b512.init();
650636 h.update("a" ** 64);
651637 h.update("b" ** 64);
652638 h.final(out[0..]);
......@@ -668,7 +654,7 @@ test "blake2b512 keyed" {
668654
669655 htest.assertEqual(h1, out[0..]);
670656
671 h.reset();
657 h = Blake2b512.init_keyed(key);
672658 h.update("a" ** 64);
673659 h.update("b" ** 64);
674660 h.final(out[0..]);
lib/std/crypto/blake3.zig+6-7
......@@ -326,12 +326,6 @@ pub const Blake3 = struct {
326326 hasher.final(out);
327327 }
328328
329 /// Reset the `Blake3` to its initial state.
330 pub fn reset(self: *Blake3) void {
331 self.chunk_state = ChunkState.init(self.key, 0, self.flags);
332 self.cv_stack_len = 0;
333 }
334
335329 fn push_cv(self: *Blake3, cv: [8]u32) void {
336330 self.cv_stack[self.cv_stack_len] = cv;
337331 self.cv_stack_len += 1;
......@@ -566,6 +560,9 @@ const reference_test = ReferenceTest{
566560};
567561
568562fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void {
563 // Save initial state
564 const initial_state = hasher.*;
565
569566 // Setup input pattern
570567 var input_pattern: [251]u8 = undefined;
571568 for (input_pattern) |*e, i| e.* = @truncate(u8, i);
......@@ -581,12 +578,14 @@ fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void {
581578 // Read final hash value
582579 var actual_bytes: [expected_hex.len / 2]u8 = undefined;
583580 hasher.final(actual_bytes[0..]);
584 hasher.reset();
585581
586582 // Compare to expected value
587583 var expected_bytes: [expected_hex.len / 2]u8 = undefined;
588584 fmt.hexToBytes(expected_bytes[0..], expected_hex[0..]) catch unreachable;
589585 testing.expectEqual(actual_bytes, expected_bytes);
586
587 // Restore initial state
588 hasher.* = initial_state;
590589}
591590
592591test "BLAKE3 reference test cases" {
lib/std/crypto/gimli.zig-4
......@@ -120,10 +120,6 @@ pub const Hash = struct {
120120 };
121121 }
122122
123 pub fn reset(self: *Self) void {
124 self.* = init();
125 }
126
127123 /// Also known as 'absorb'
128124 pub fn update(self: *Self, data: []const u8) void {
129125 const buf = self.state.toSlice();
lib/std/crypto/hmac.zig+4-4
......@@ -75,10 +75,10 @@ pub fn Hmac(comptime Hash: type) type {
7575 debug.assert(Hash.block_length >= out.len and out.len >= mac_length);
7676
7777 ctx.hash.final(ctx.scratch[0..mac_length]);
78 ctx.hash.reset();
79 ctx.hash.update(ctx.o_key_pad[0..]);
80 ctx.hash.update(ctx.scratch[0..mac_length]);
81 ctx.hash.final(out[0..mac_length]);
78 var ohash = Hash.init();
79 ohash.update(ctx.o_key_pad[0..]);
80 ohash.update(ctx.scratch[0..mac_length]);
81 ohash.final(out[0..mac_length]);
8282 }
8383 };
8484}
lib/std/crypto/md5.zig+2-6
......@@ -60,10 +60,6 @@ pub const Md5 = struct {
6060 };
6161 }
6262
63 pub fn reset(self: *Self) void {
64 self.* = init();
65 }
66
6763 pub fn hash(b: []const u8, out: []u8) void {
6864 var d = Md5.init();
6965 d.update(b);
......@@ -267,12 +263,12 @@ test "md5 streaming" {
267263 h.final(out[0..]);
268264 htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]);
269265
270 h.reset();
266 h = Md5.init();
271267 h.update("abc");
272268 h.final(out[0..]);
273269 htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
274270
275 h.reset();
271 h = Md5.init();
276272 h.update("a");
277273 h.update("b");
278274 h.update("c");
lib/std/crypto/sha1.zig+5-12
......@@ -39,9 +39,9 @@ pub const Sha1 = struct {
3939
4040 s: [5]u32,
4141 // Streaming Cache
42 buf: [64]u8,
43 buf_len: u8,
44 total_len: u64,
42 buf: [64]u8 = undefined,
43 buf_len: u8 = 0,
44 total_len: u64 = 0,
4545
4646 pub fn init() Self {
4747 return Self{
......@@ -52,16 +52,9 @@ pub const Sha1 = struct {
5252 0x10325476,
5353 0xC3D2E1F0,
5454 },
55 .buf = undefined,
56 .buf_len = 0,
57 .total_len = 0,
5855 };
5956 }
6057
61 pub fn reset(self: *Self) void {
62 self.* = init();
63 }
64
6558 pub fn hash(b: []const u8, out: []u8) void {
6659 var d = Sha1.init();
6760 d.update(b);
......@@ -289,12 +282,12 @@ test "sha1 streaming" {
289282 h.final(out[0..]);
290283 htest.assertEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709", out[0..]);
291284
292 h.reset();
285 h = Sha1.init();
293286 h.update("abc");
294287 h.final(out[0..]);
295288 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);
296289
297 h.reset();
290 h = Sha1.init();
298291 h.update("a");
299292 h.update("b");
300293 h.update("c");
lib/std/crypto/sha2.zig+26-37
......@@ -91,9 +91,9 @@ fn Sha2_32(comptime params: Sha2Params32) type {
9191
9292 s: [8]u32,
9393 // Streaming Cache
94 buf: [64]u8,
95 buf_len: u8,
96 total_len: u64,
94 buf: [64]u8 = undefined,
95 buf_len: u8 = 0,
96 total_len: u64 = 0,
9797
9898 pub fn init() Self {
9999 return Self{
......@@ -107,16 +107,9 @@ fn Sha2_32(comptime params: Sha2Params32) type {
107107 params.iv6,
108108 params.iv7,
109109 },
110 .buf = undefined,
111 .buf_len = 0,
112 .total_len = 0,
113110 };
114111 }
115112
116 pub fn reset(self: *Self) void {
117 self.* = init();
118 }
119
120113 pub fn hash(b: []const u8, out: []u8) void {
121114 var d = Self.init();
122115 d.update(b);
......@@ -309,12 +302,12 @@ test "sha224 streaming" {
309302 h.final(out[0..]);
310303 htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]);
311304
312 h.reset();
305 h = Sha224.init();
313306 h.update("abc");
314307 h.final(out[0..]);
315308 htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]);
316309
317 h.reset();
310 h = Sha224.init();
318311 h.update("a");
319312 h.update("b");
320313 h.update("c");
......@@ -335,12 +328,12 @@ test "sha256 streaming" {
335328 h.final(out[0..]);
336329 htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]);
337330
338 h.reset();
331 h = Sha256.init();
339332 h.update("abc");
340333 h.final(out[0..]);
341334 htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);
342335
343 h.reset();
336 h = Sha256.init();
344337 h.update("a");
345338 h.update("b");
346339 h.update("c");
......@@ -468,27 +461,23 @@ fn Sha2_64(comptime params: Sha2Params64) type {
468461
469462 s: [8]u64,
470463 // Streaming Cache
471 buf: [128]u8,
472 buf_len: u8,
473 total_len: u128,
464 buf: [128]u8 = undefined,
465 buf_len: u8 = 0,
466 total_len: u128 = 0,
474467
475468 pub fn init() Self {
476 var d: Self = undefined;
477 d.reset();
478 return d;
479 }
480
481 pub fn reset(d: *Self) void {
482 d.s[0] = params.iv0;
483 d.s[1] = params.iv1;
484 d.s[2] = params.iv2;
485 d.s[3] = params.iv3;
486 d.s[4] = params.iv4;
487 d.s[5] = params.iv5;
488 d.s[6] = params.iv6;
489 d.s[7] = params.iv7;
490 d.buf_len = 0;
491 d.total_len = 0;
469 return Self{
470 .s = [_]u64{
471 params.iv0,
472 params.iv1,
473 params.iv2,
474 params.iv3,
475 params.iv4,
476 params.iv5,
477 params.iv6,
478 params.iv7,
479 },
480 };
492481 }
493482
494483 pub fn hash(b: []const u8, out: []u8) void {
......@@ -713,12 +702,12 @@ test "sha384 streaming" {
713702
714703 const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
715704
716 h.reset();
705 h = Sha384.init();
717706 h.update("abc");
718707 h.final(out[0..]);
719708 htest.assertEqual(h2, out[0..]);
720709
721 h.reset();
710 h = Sha384.init();
722711 h.update("a");
723712 h.update("b");
724713 h.update("c");
......@@ -747,12 +736,12 @@ test "sha512 streaming" {
747736
748737 const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
749738
750 h.reset();
739 h = Sha512.init();
751740 h.update("abc");
752741 h.final(out[0..]);
753742 htest.assertEqual(h2, out[0..]);
754743
755 h.reset();
744 h = Sha512.init();
756745 h.update("a");
757746 h.update("b");
758747 h.update("c");
lib/std/crypto/sha3.zig+9-17
......@@ -26,15 +26,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
2626 rate: usize,
2727
2828 pub fn init() Self {
29 return comptime Self{
30 .s = [_]u8{0} ** 200,
31 .offset = 0,
32 .rate = 200 - (bits / 4),
33 };
34 }
35
36 pub fn reset(self: *Self) void {
37 self.* = init();
29 return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) };
3830 }
3931
4032 pub fn hash(b: []const u8, out: []u8) void {
......@@ -189,12 +181,12 @@ test "sha3-224 streaming" {
189181 h.final(out[0..]);
190182 htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]);
191183
192 h.reset();
184 h = Sha3_224.init();
193185 h.update("abc");
194186 h.final(out[0..]);
195187 htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]);
196188
197 h.reset();
189 h = Sha3_224.init();
198190 h.update("a");
199191 h.update("b");
200192 h.update("c");
......@@ -215,12 +207,12 @@ test "sha3-256 streaming" {
215207 h.final(out[0..]);
216208 htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]);
217209
218 h.reset();
210 h = Sha3_256.init();
219211 h.update("abc");
220212 h.final(out[0..]);
221213 htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
222214
223 h.reset();
215 h = Sha3_256.init();
224216 h.update("a");
225217 h.update("b");
226218 h.update("c");
......@@ -255,12 +247,12 @@ test "sha3-384 streaming" {
255247 htest.assertEqual(h1, out[0..]);
256248
257249 const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
258 h.reset();
250 h = Sha3_384.init();
259251 h.update("abc");
260252 h.final(out[0..]);
261253 htest.assertEqual(h2, out[0..]);
262254
263 h.reset();
255 h = Sha3_384.init();
264256 h.update("a");
265257 h.update("b");
266258 h.update("c");
......@@ -286,12 +278,12 @@ test "sha3-512 streaming" {
286278 htest.assertEqual(h1, out[0..]);
287279
288280 const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
289 h.reset();
281 h = Sha3_512.init();
290282 h.update("abc");
291283 h.final(out[0..]);
292284 htest.assertEqual(h2, out[0..]);
293285
294 h.reset();
286 h = Sha3_512.init();
295287 h.update("a");
296288 h.update("b");
297289 h.update("c");