| author | |
| committer | |
| log | 446597bd3c935be632287a8ad6cfd72892d674e6 |
| tree | d80af39c7fd2036b2d832407cdb39c86e87cdf5b |
| parent | f92a5d79440402233bd0215e2bb2aeeb4f333931 |
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" { | ... | @@ -112,11 +112,11 @@ test "issue #4532: no index out of bounds" { |
| 112 | var block = [_]u8{'#'} ** Hasher.block_length; | 112 | var block = [_]u8{'#'} ** Hasher.block_length; |
| 113 | var out1: [Hasher.digest_length]u8 = undefined; | 113 | var out1: [Hasher.digest_length]u8 = undefined; |
| 114 | var out2: [Hasher.digest_length]u8 = undefined; | 114 | var out2: [Hasher.digest_length]u8 = undefined; |
| 115 | 115 | const h0 = Hasher.init(); | |
| 116 | var h = Hasher.init(); | 116 | var h = h0; |
| 117 | h.update(block[0..]); | 117 | h.update(block[0..]); |
| 118 | h.final(out1[0..]); | 118 | h.final(out1[0..]); |
| 119 | h.reset(); | 119 | h = h0; |
| 120 | h.update(block[0..1]); | 120 | h.update(block[0..1]); |
| 121 | h.update(block[1..]); | 121 | h.update(block[1..]); |
| 122 | h.final(out2[0..]); | 122 | h.final(out2[0..]); |
lib/std/crypto/blake2.zig+30-44| ... | @@ -71,8 +71,6 @@ pub fn Blake2s(comptime out_len: usize) type { | ... | @@ -71,8 +71,6 @@ pub fn Blake2s(comptime out_len: usize) type { |
| 71 | buf: [64]u8, | 71 | buf: [64]u8, |
| 72 | buf_len: u8, | 72 | buf_len: u8, |
| 73 | 73 | ||
| 74 | key: []const u8, | ||
| 75 | |||
| 76 | pub fn init() Self { | 74 | pub fn init() Self { |
| 77 | return comptime init_keyed(""); | 75 | return comptime init_keyed(""); |
| 78 | } | 76 | } |
| ... | @@ -80,25 +78,20 @@ pub fn Blake2s(comptime out_len: usize) type { | ... | @@ -80,25 +78,20 @@ pub fn Blake2s(comptime out_len: usize) type { |
| 80 | pub fn init_keyed(key: []const u8) Self { | 78 | pub fn init_keyed(key: []const u8) Self { |
| 81 | debug.assert(8 <= out_len and out_len <= 512); | 79 | debug.assert(8 <= out_len and out_len <= 512); |
| 82 | 80 | ||
| 83 | var s: Self = undefined; | 81 | var d: Self = undefined; |
| 84 | s.key = key; | ||
| 85 | s.reset(); | ||
| 86 | return s; | ||
| 87 | } | ||
| 88 | |||
| 89 | pub fn reset(d: *Self) void { | ||
| 90 | mem.copy(u32, d.h[0..], iv[0..]); | 82 | mem.copy(u32, d.h[0..], iv[0..]); |
| 91 | 83 | ||
| 92 | // default parameters | 84 | // 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); |
| 94 | d.t = 0; | 86 | d.t = 0; |
| 95 | d.buf_len = 0; | 87 | d.buf_len = 0; |
| 96 | 88 | ||
| 97 | if (d.key.len > 0) { | 89 | if (key.len > 0) { |
| 98 | mem.set(u8, d.buf[d.key.len..], 0); | 90 | mem.set(u8, d.buf[key.len..], 0); |
| 99 | d.update(d.key); | 91 | d.update(key); |
| 100 | d.buf_len = 64; | 92 | d.buf_len = 64; |
| 101 | } | 93 | } |
| 94 | return d; | ||
| 102 | } | 95 | } |
| 103 | 96 | ||
| 104 | pub fn hash(b: []const u8, out: []u8) void { | 97 | pub fn hash(b: []const u8, out: []u8) void { |
| ... | @@ -225,12 +218,12 @@ test "blake2s224 streaming" { | ... | @@ -225,12 +218,12 @@ test "blake2s224 streaming" { |
| 225 | 218 | ||
| 226 | const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55"; | 219 | const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55"; |
| 227 | 220 | ||
| 228 | h.reset(); | 221 | h = Blake2s224.init(); |
| 229 | h.update("abc"); | 222 | h.update("abc"); |
| 230 | h.final(out[0..]); | 223 | h.final(out[0..]); |
| 231 | htest.assertEqual(h2, out[0..]); | 224 | htest.assertEqual(h2, out[0..]); |
| 232 | 225 | ||
| 233 | h.reset(); | 226 | h = Blake2s224.init(); |
| 234 | h.update("a"); | 227 | h.update("a"); |
| 235 | h.update("b"); | 228 | h.update("b"); |
| 236 | h.update("c"); | 229 | h.update("c"); |
| ... | @@ -239,13 +232,13 @@ test "blake2s224 streaming" { | ... | @@ -239,13 +232,13 @@ test "blake2s224 streaming" { |
| 239 | 232 | ||
| 240 | const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01"; | 233 | const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01"; |
| 241 | 234 | ||
| 242 | h.reset(); | 235 | h = Blake2s224.init(); |
| 243 | h.update("a" ** 32); | 236 | h.update("a" ** 32); |
| 244 | h.update("b" ** 32); | 237 | h.update("b" ** 32); |
| 245 | h.final(out[0..]); | 238 | h.final(out[0..]); |
| 246 | htest.assertEqual(h3, out[0..]); | 239 | htest.assertEqual(h3, out[0..]); |
| 247 | 240 | ||
| 248 | h.reset(); | 241 | h = Blake2s224.init(); |
| 249 | h.update("a" ** 32 ++ "b" ** 32); | 242 | h.update("a" ** 32 ++ "b" ** 32); |
| 250 | h.final(out[0..]); | 243 | h.final(out[0..]); |
| 251 | htest.assertEqual(h3, out[0..]); | 244 | htest.assertEqual(h3, out[0..]); |
| ... | @@ -294,12 +287,12 @@ test "blake2s256 streaming" { | ... | @@ -294,12 +287,12 @@ test "blake2s256 streaming" { |
| 294 | 287 | ||
| 295 | const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"; | 288 | const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"; |
| 296 | 289 | ||
| 297 | h.reset(); | 290 | h = Blake2s256.init(); |
| 298 | h.update("abc"); | 291 | h.update("abc"); |
| 299 | h.final(out[0..]); | 292 | h.final(out[0..]); |
| 300 | htest.assertEqual(h2, out[0..]); | 293 | htest.assertEqual(h2, out[0..]); |
| 301 | 294 | ||
| 302 | h.reset(); | 295 | h = Blake2s256.init(); |
| 303 | h.update("a"); | 296 | h.update("a"); |
| 304 | h.update("b"); | 297 | h.update("b"); |
| 305 | h.update("c"); | 298 | h.update("c"); |
| ... | @@ -308,13 +301,13 @@ test "blake2s256 streaming" { | ... | @@ -308,13 +301,13 @@ test "blake2s256 streaming" { |
| 308 | 301 | ||
| 309 | const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977"; | 302 | const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977"; |
| 310 | 303 | ||
| 311 | h.reset(); | 304 | h = Blake2s256.init(); |
| 312 | h.update("a" ** 32); | 305 | h.update("a" ** 32); |
| 313 | h.update("b" ** 32); | 306 | h.update("b" ** 32); |
| 314 | h.final(out[0..]); | 307 | h.final(out[0..]); |
| 315 | htest.assertEqual(h3, out[0..]); | 308 | htest.assertEqual(h3, out[0..]); |
| 316 | 309 | ||
| 317 | h.reset(); | 310 | h = Blake2s256.init(); |
| 318 | h.update("a" ** 32 ++ "b" ** 32); | 311 | h.update("a" ** 32 ++ "b" ** 32); |
| 319 | h.final(out[0..]); | 312 | h.final(out[0..]); |
| 320 | htest.assertEqual(h3, out[0..]); | 313 | htest.assertEqual(h3, out[0..]); |
| ... | @@ -335,7 +328,7 @@ test "blake2s256 keyed" { | ... | @@ -335,7 +328,7 @@ test "blake2s256 keyed" { |
| 335 | 328 | ||
| 336 | htest.assertEqual(h1, out[0..]); | 329 | htest.assertEqual(h1, out[0..]); |
| 337 | 330 | ||
| 338 | h.reset(); | 331 | h = Blake2s256.init_keyed(key); |
| 339 | h.update("a" ** 64); | 332 | h.update("a" ** 64); |
| 340 | h.update("b" ** 64); | 333 | h.update("b" ** 64); |
| 341 | h.final(out[0..]); | 334 | h.final(out[0..]); |
| ... | @@ -406,8 +399,6 @@ pub fn Blake2b(comptime out_len: usize) type { | ... | @@ -406,8 +399,6 @@ pub fn Blake2b(comptime out_len: usize) type { |
| 406 | buf: [128]u8, | 399 | buf: [128]u8, |
| 407 | buf_len: u8, | 400 | buf_len: u8, |
| 408 | 401 | ||
| 409 | key: []const u8, | ||
| 410 | |||
| 411 | pub fn init() Self { | 402 | pub fn init() Self { |
| 412 | return init_keyed(""); | 403 | return init_keyed(""); |
| 413 | } | 404 | } |
| ... | @@ -415,25 +406,20 @@ pub fn Blake2b(comptime out_len: usize) type { | ... | @@ -415,25 +406,20 @@ pub fn Blake2b(comptime out_len: usize) type { |
| 415 | pub fn init_keyed(key: []const u8) Self { | 406 | pub fn init_keyed(key: []const u8) Self { |
| 416 | debug.assert(8 <= out_len and out_len <= 512); | 407 | debug.assert(8 <= out_len and out_len <= 512); |
| 417 | 408 | ||
| 418 | var s: Self = undefined; | 409 | var d: Self = undefined; |
| 419 | s.key = key; | ||
| 420 | s.reset(); | ||
| 421 | return s; | ||
| 422 | } | ||
| 423 | |||
| 424 | pub fn reset(d: *Self) void { | ||
| 425 | mem.copy(u64, d.h[0..], iv[0..]); | 410 | mem.copy(u64, d.h[0..], iv[0..]); |
| 426 | 411 | ||
| 427 | // default parameters | 412 | // 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); |
| 429 | d.t = 0; | 414 | d.t = 0; |
| 430 | d.buf_len = 0; | 415 | d.buf_len = 0; |
| 431 | 416 | ||
| 432 | if (d.key.len > 0) { | 417 | if (key.len > 0) { |
| 433 | mem.set(u8, d.buf[d.key.len..], 0); | 418 | mem.set(u8, d.buf[key.len..], 0); |
| 434 | d.update(d.key); | 419 | d.update(key); |
| 435 | d.buf_len = 128; | 420 | d.buf_len = 128; |
| 436 | } | 421 | } |
| 422 | return d; | ||
| 437 | } | 423 | } |
| 438 | 424 | ||
| 439 | pub fn hash(b: []const u8, out: []u8) void { | 425 | pub fn hash(b: []const u8, out: []u8) void { |
| ... | @@ -558,12 +544,12 @@ test "blake2b384 streaming" { | ... | @@ -558,12 +544,12 @@ test "blake2b384 streaming" { |
| 558 | 544 | ||
| 559 | const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4"; | 545 | const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4"; |
| 560 | 546 | ||
| 561 | h.reset(); | 547 | h = Blake2b384.init(); |
| 562 | h.update("abc"); | 548 | h.update("abc"); |
| 563 | h.final(out[0..]); | 549 | h.final(out[0..]); |
| 564 | htest.assertEqual(h2, out[0..]); | 550 | htest.assertEqual(h2, out[0..]); |
| 565 | 551 | ||
| 566 | h.reset(); | 552 | h = Blake2b384.init(); |
| 567 | h.update("a"); | 553 | h.update("a"); |
| 568 | h.update("b"); | 554 | h.update("b"); |
| 569 | h.update("c"); | 555 | h.update("c"); |
| ... | @@ -572,12 +558,12 @@ test "blake2b384 streaming" { | ... | @@ -572,12 +558,12 @@ test "blake2b384 streaming" { |
| 572 | 558 | ||
| 573 | const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c"; | 559 | const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c"; |
| 574 | 560 | ||
| 575 | h.reset(); | 561 | h = Blake2b384.init(); |
| 576 | h.update("a" ** 64 ++ "b" ** 64); | 562 | h.update("a" ** 64 ++ "b" ** 64); |
| 577 | h.final(out[0..]); | 563 | h.final(out[0..]); |
| 578 | htest.assertEqual(h3, out[0..]); | 564 | htest.assertEqual(h3, out[0..]); |
| 579 | 565 | ||
| 580 | h.reset(); | 566 | h = Blake2b384.init(); |
| 581 | h.update("a" ** 64); | 567 | h.update("a" ** 64); |
| 582 | h.update("b" ** 64); | 568 | h.update("b" ** 64); |
| 583 | h.final(out[0..]); | 569 | h.final(out[0..]); |
| ... | @@ -627,12 +613,12 @@ test "blake2b512 streaming" { | ... | @@ -627,12 +613,12 @@ test "blake2b512 streaming" { |
| 627 | 613 | ||
| 628 | const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"; | 614 | const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"; |
| 629 | 615 | ||
| 630 | h.reset(); | 616 | h = Blake2b512.init(); |
| 631 | h.update("abc"); | 617 | h.update("abc"); |
| 632 | h.final(out[0..]); | 618 | h.final(out[0..]); |
| 633 | htest.assertEqual(h2, out[0..]); | 619 | htest.assertEqual(h2, out[0..]); |
| 634 | 620 | ||
| 635 | h.reset(); | 621 | h = Blake2b512.init(); |
| 636 | h.update("a"); | 622 | h.update("a"); |
| 637 | h.update("b"); | 623 | h.update("b"); |
| 638 | h.update("c"); | 624 | h.update("c"); |
| ... | @@ -641,12 +627,12 @@ test "blake2b512 streaming" { | ... | @@ -641,12 +627,12 @@ test "blake2b512 streaming" { |
| 641 | 627 | ||
| 642 | const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920"; | 628 | const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920"; |
| 643 | 629 | ||
| 644 | h.reset(); | 630 | h = Blake2b512.init(); |
| 645 | h.update("a" ** 64 ++ "b" ** 64); | 631 | h.update("a" ** 64 ++ "b" ** 64); |
| 646 | h.final(out[0..]); | 632 | h.final(out[0..]); |
| 647 | htest.assertEqual(h3, out[0..]); | 633 | htest.assertEqual(h3, out[0..]); |
| 648 | 634 | ||
| 649 | h.reset(); | 635 | h = Blake2b512.init(); |
| 650 | h.update("a" ** 64); | 636 | h.update("a" ** 64); |
| 651 | h.update("b" ** 64); | 637 | h.update("b" ** 64); |
| 652 | h.final(out[0..]); | 638 | h.final(out[0..]); |
| ... | @@ -668,7 +654,7 @@ test "blake2b512 keyed" { | ... | @@ -668,7 +654,7 @@ test "blake2b512 keyed" { |
| 668 | 654 | ||
| 669 | htest.assertEqual(h1, out[0..]); | 655 | htest.assertEqual(h1, out[0..]); |
| 670 | 656 | ||
| 671 | h.reset(); | 657 | h = Blake2b512.init_keyed(key); |
| 672 | h.update("a" ** 64); | 658 | h.update("a" ** 64); |
| 673 | h.update("b" ** 64); | 659 | h.update("b" ** 64); |
| 674 | h.final(out[0..]); | 660 | h.final(out[0..]); |
lib/std/crypto/blake3.zig+6-7| ... | @@ -326,12 +326,6 @@ pub const Blake3 = struct { | ... | @@ -326,12 +326,6 @@ pub const Blake3 = struct { |
| 326 | hasher.final(out); | 326 | hasher.final(out); |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 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 | |||
| 335 | fn push_cv(self: *Blake3, cv: [8]u32) void { | 329 | fn push_cv(self: *Blake3, cv: [8]u32) void { |
| 336 | self.cv_stack[self.cv_stack_len] = cv; | 330 | self.cv_stack[self.cv_stack_len] = cv; |
| 337 | self.cv_stack_len += 1; | 331 | self.cv_stack_len += 1; |
| ... | @@ -566,6 +560,9 @@ const reference_test = ReferenceTest{ | ... | @@ -566,6 +560,9 @@ const reference_test = ReferenceTest{ |
| 566 | }; | 560 | }; |
| 567 | 561 | ||
| 568 | fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void { | 562 | fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void { |
| 563 | // Save initial state | ||
| 564 | const initial_state = hasher.*; | ||
| 565 | |||
| 569 | // Setup input pattern | 566 | // Setup input pattern |
| 570 | var input_pattern: [251]u8 = undefined; | 567 | var input_pattern: [251]u8 = undefined; |
| 571 | for (input_pattern) |*e, i| e.* = @truncate(u8, i); | 568 | 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 { | ... | @@ -581,12 +578,14 @@ fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void { |
| 581 | // Read final hash value | 578 | // Read final hash value |
| 582 | var actual_bytes: [expected_hex.len / 2]u8 = undefined; | 579 | var actual_bytes: [expected_hex.len / 2]u8 = undefined; |
| 583 | hasher.final(actual_bytes[0..]); | 580 | hasher.final(actual_bytes[0..]); |
| 584 | hasher.reset(); | ||
| 585 | 581 | ||
| 586 | // Compare to expected value | 582 | // Compare to expected value |
| 587 | var expected_bytes: [expected_hex.len / 2]u8 = undefined; | 583 | var expected_bytes: [expected_hex.len / 2]u8 = undefined; |
| 588 | fmt.hexToBytes(expected_bytes[0..], expected_hex[0..]) catch unreachable; | 584 | fmt.hexToBytes(expected_bytes[0..], expected_hex[0..]) catch unreachable; |
| 589 | testing.expectEqual(actual_bytes, expected_bytes); | 585 | testing.expectEqual(actual_bytes, expected_bytes); |
| 586 | |||
| 587 | // Restore initial state | ||
| 588 | hasher.* = initial_state; | ||
| 590 | } | 589 | } |
| 591 | 590 | ||
| 592 | test "BLAKE3 reference test cases" { | 591 | test "BLAKE3 reference test cases" { |
lib/std/crypto/gimli.zig-4| ... | @@ -120,10 +120,6 @@ pub const Hash = struct { | ... | @@ -120,10 +120,6 @@ pub const Hash = struct { |
| 120 | }; | 120 | }; |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | pub fn reset(self: *Self) void { | ||
| 124 | self.* = init(); | ||
| 125 | } | ||
| 126 | |||
| 127 | /// Also known as 'absorb' | 123 | /// Also known as 'absorb' |
| 128 | pub fn update(self: *Self, data: []const u8) void { | 124 | pub fn update(self: *Self, data: []const u8) void { |
| 129 | const buf = self.state.toSlice(); | 125 | const buf = self.state.toSlice(); |
lib/std/crypto/hmac.zig+4-4| ... | @@ -75,10 +75,10 @@ pub fn Hmac(comptime Hash: type) type { | ... | @@ -75,10 +75,10 @@ pub fn Hmac(comptime Hash: type) type { |
| 75 | debug.assert(Hash.block_length >= out.len and out.len >= mac_length); | 75 | debug.assert(Hash.block_length >= out.len and out.len >= mac_length); |
| 76 | 76 | ||
| 77 | ctx.hash.final(ctx.scratch[0..mac_length]); | 77 | ctx.hash.final(ctx.scratch[0..mac_length]); |
| 78 | ctx.hash.reset(); | 78 | var ohash = Hash.init(); |
| 79 | ctx.hash.update(ctx.o_key_pad[0..]); | 79 | ohash.update(ctx.o_key_pad[0..]); |
| 80 | ctx.hash.update(ctx.scratch[0..mac_length]); | 80 | ohash.update(ctx.scratch[0..mac_length]); |
| 81 | ctx.hash.final(out[0..mac_length]); | 81 | ohash.final(out[0..mac_length]); |
| 82 | } | 82 | } |
| 83 | }; | 83 | }; |
| 84 | } | 84 | } |
lib/std/crypto/md5.zig+2-6| ... | @@ -60,10 +60,6 @@ pub const Md5 = struct { | ... | @@ -60,10 +60,6 @@ pub const Md5 = struct { |
| 60 | }; | 60 | }; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | pub fn reset(self: *Self) void { | ||
| 64 | self.* = init(); | ||
| 65 | } | ||
| 66 | |||
| 67 | pub fn hash(b: []const u8, out: []u8) void { | 63 | pub fn hash(b: []const u8, out: []u8) void { |
| 68 | var d = Md5.init(); | 64 | var d = Md5.init(); |
| 69 | d.update(b); | 65 | d.update(b); |
| ... | @@ -267,12 +263,12 @@ test "md5 streaming" { | ... | @@ -267,12 +263,12 @@ test "md5 streaming" { |
| 267 | h.final(out[0..]); | 263 | h.final(out[0..]); |
| 268 | htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]); | 264 | htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]); |
| 269 | 265 | ||
| 270 | h.reset(); | 266 | h = Md5.init(); |
| 271 | h.update("abc"); | 267 | h.update("abc"); |
| 272 | h.final(out[0..]); | 268 | h.final(out[0..]); |
| 273 | htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]); | 269 | htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]); |
| 274 | 270 | ||
| 275 | h.reset(); | 271 | h = Md5.init(); |
| 276 | h.update("a"); | 272 | h.update("a"); |
| 277 | h.update("b"); | 273 | h.update("b"); |
| 278 | h.update("c"); | 274 | h.update("c"); |
lib/std/crypto/sha1.zig+5-12| ... | @@ -39,9 +39,9 @@ pub const Sha1 = struct { | ... | @@ -39,9 +39,9 @@ pub const Sha1 = struct { |
| 39 | 39 | ||
| 40 | s: [5]u32, | 40 | s: [5]u32, |
| 41 | // Streaming Cache | 41 | // Streaming Cache |
| 42 | buf: [64]u8, | 42 | buf: [64]u8 = undefined, |
| 43 | buf_len: u8, | 43 | buf_len: u8 = 0, |
| 44 | total_len: u64, | 44 | total_len: u64 = 0, |
| 45 | 45 | ||
| 46 | pub fn init() Self { | 46 | pub fn init() Self { |
| 47 | return Self{ | 47 | return Self{ |
| ... | @@ -52,16 +52,9 @@ pub const Sha1 = struct { | ... | @@ -52,16 +52,9 @@ pub const Sha1 = struct { |
| 52 | 0x10325476, | 52 | 0x10325476, |
| 53 | 0xC3D2E1F0, | 53 | 0xC3D2E1F0, |
| 54 | }, | 54 | }, |
| 55 | .buf = undefined, | ||
| 56 | .buf_len = 0, | ||
| 57 | .total_len = 0, | ||
| 58 | }; | 55 | }; |
| 59 | } | 56 | } |
| 60 | 57 | ||
| 61 | pub fn reset(self: *Self) void { | ||
| 62 | self.* = init(); | ||
| 63 | } | ||
| 64 | |||
| 65 | pub fn hash(b: []const u8, out: []u8) void { | 58 | pub fn hash(b: []const u8, out: []u8) void { |
| 66 | var d = Sha1.init(); | 59 | var d = Sha1.init(); |
| 67 | d.update(b); | 60 | d.update(b); |
| ... | @@ -289,12 +282,12 @@ test "sha1 streaming" { | ... | @@ -289,12 +282,12 @@ test "sha1 streaming" { |
| 289 | h.final(out[0..]); | 282 | h.final(out[0..]); |
| 290 | htest.assertEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709", out[0..]); | 283 | htest.assertEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709", out[0..]); |
| 291 | 284 | ||
| 292 | h.reset(); | 285 | h = Sha1.init(); |
| 293 | h.update("abc"); | 286 | h.update("abc"); |
| 294 | h.final(out[0..]); | 287 | h.final(out[0..]); |
| 295 | htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]); | 288 | htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]); |
| 296 | 289 | ||
| 297 | h.reset(); | 290 | h = Sha1.init(); |
| 298 | h.update("a"); | 291 | h.update("a"); |
| 299 | h.update("b"); | 292 | h.update("b"); |
| 300 | h.update("c"); | 293 | h.update("c"); |
lib/std/crypto/sha2.zig+26-37| ... | @@ -91,9 +91,9 @@ fn Sha2_32(comptime params: Sha2Params32) type { | ... | @@ -91,9 +91,9 @@ fn Sha2_32(comptime params: Sha2Params32) type { |
| 91 | 91 | ||
| 92 | s: [8]u32, | 92 | s: [8]u32, |
| 93 | // Streaming Cache | 93 | // Streaming Cache |
| 94 | buf: [64]u8, | 94 | buf: [64]u8 = undefined, |
| 95 | buf_len: u8, | 95 | buf_len: u8 = 0, |
| 96 | total_len: u64, | 96 | total_len: u64 = 0, |
| 97 | 97 | ||
| 98 | pub fn init() Self { | 98 | pub fn init() Self { |
| 99 | return Self{ | 99 | return Self{ |
| ... | @@ -107,16 +107,9 @@ fn Sha2_32(comptime params: Sha2Params32) type { | ... | @@ -107,16 +107,9 @@ fn Sha2_32(comptime params: Sha2Params32) type { |
| 107 | params.iv6, | 107 | params.iv6, |
| 108 | params.iv7, | 108 | params.iv7, |
| 109 | }, | 109 | }, |
| 110 | .buf = undefined, | ||
| 111 | .buf_len = 0, | ||
| 112 | .total_len = 0, | ||
| 113 | }; | 110 | }; |
| 114 | } | 111 | } |
| 115 | 112 | ||
| 116 | pub fn reset(self: *Self) void { | ||
| 117 | self.* = init(); | ||
| 118 | } | ||
| 119 | |||
| 120 | pub fn hash(b: []const u8, out: []u8) void { | 113 | pub fn hash(b: []const u8, out: []u8) void { |
| 121 | var d = Self.init(); | 114 | var d = Self.init(); |
| 122 | d.update(b); | 115 | d.update(b); |
| ... | @@ -309,12 +302,12 @@ test "sha224 streaming" { | ... | @@ -309,12 +302,12 @@ test "sha224 streaming" { |
| 309 | h.final(out[0..]); | 302 | h.final(out[0..]); |
| 310 | htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]); | 303 | htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]); |
| 311 | 304 | ||
| 312 | h.reset(); | 305 | h = Sha224.init(); |
| 313 | h.update("abc"); | 306 | h.update("abc"); |
| 314 | h.final(out[0..]); | 307 | h.final(out[0..]); |
| 315 | htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]); | 308 | htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]); |
| 316 | 309 | ||
| 317 | h.reset(); | 310 | h = Sha224.init(); |
| 318 | h.update("a"); | 311 | h.update("a"); |
| 319 | h.update("b"); | 312 | h.update("b"); |
| 320 | h.update("c"); | 313 | h.update("c"); |
| ... | @@ -335,12 +328,12 @@ test "sha256 streaming" { | ... | @@ -335,12 +328,12 @@ test "sha256 streaming" { |
| 335 | h.final(out[0..]); | 328 | h.final(out[0..]); |
| 336 | htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]); | 329 | htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]); |
| 337 | 330 | ||
| 338 | h.reset(); | 331 | h = Sha256.init(); |
| 339 | h.update("abc"); | 332 | h.update("abc"); |
| 340 | h.final(out[0..]); | 333 | h.final(out[0..]); |
| 341 | htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]); | 334 | htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]); |
| 342 | 335 | ||
| 343 | h.reset(); | 336 | h = Sha256.init(); |
| 344 | h.update("a"); | 337 | h.update("a"); |
| 345 | h.update("b"); | 338 | h.update("b"); |
| 346 | h.update("c"); | 339 | h.update("c"); |
| ... | @@ -468,27 +461,23 @@ fn Sha2_64(comptime params: Sha2Params64) type { | ... | @@ -468,27 +461,23 @@ fn Sha2_64(comptime params: Sha2Params64) type { |
| 468 | 461 | ||
| 469 | s: [8]u64, | 462 | s: [8]u64, |
| 470 | // Streaming Cache | 463 | // Streaming Cache |
| 471 | buf: [128]u8, | 464 | buf: [128]u8 = undefined, |
| 472 | buf_len: u8, | 465 | buf_len: u8 = 0, |
| 473 | total_len: u128, | 466 | total_len: u128 = 0, |
| 474 | 467 | ||
| 475 | pub fn init() Self { | 468 | pub fn init() Self { |
| 476 | var d: Self = undefined; | 469 | return Self{ |
| 477 | d.reset(); | 470 | .s = [_]u64{ |
| 478 | return d; | 471 | params.iv0, |
| 479 | } | 472 | params.iv1, |
| 480 | 473 | params.iv2, | |
| 481 | pub fn reset(d: *Self) void { | 474 | params.iv3, |
| 482 | d.s[0] = params.iv0; | 475 | params.iv4, |
| 483 | d.s[1] = params.iv1; | 476 | params.iv5, |
| 484 | d.s[2] = params.iv2; | 477 | params.iv6, |
| 485 | d.s[3] = params.iv3; | 478 | params.iv7, |
| 486 | d.s[4] = params.iv4; | 479 | }, |
| 487 | d.s[5] = params.iv5; | 480 | }; |
| 488 | d.s[6] = params.iv6; | ||
| 489 | d.s[7] = params.iv7; | ||
| 490 | d.buf_len = 0; | ||
| 491 | d.total_len = 0; | ||
| 492 | } | 481 | } |
| 493 | 482 | ||
| 494 | pub fn hash(b: []const u8, out: []u8) void { | 483 | pub fn hash(b: []const u8, out: []u8) void { |
| ... | @@ -713,12 +702,12 @@ test "sha384 streaming" { | ... | @@ -713,12 +702,12 @@ test "sha384 streaming" { |
| 713 | 702 | ||
| 714 | const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"; | 703 | const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"; |
| 715 | 704 | ||
| 716 | h.reset(); | 705 | h = Sha384.init(); |
| 717 | h.update("abc"); | 706 | h.update("abc"); |
| 718 | h.final(out[0..]); | 707 | h.final(out[0..]); |
| 719 | htest.assertEqual(h2, out[0..]); | 708 | htest.assertEqual(h2, out[0..]); |
| 720 | 709 | ||
| 721 | h.reset(); | 710 | h = Sha384.init(); |
| 722 | h.update("a"); | 711 | h.update("a"); |
| 723 | h.update("b"); | 712 | h.update("b"); |
| 724 | h.update("c"); | 713 | h.update("c"); |
| ... | @@ -747,12 +736,12 @@ test "sha512 streaming" { | ... | @@ -747,12 +736,12 @@ test "sha512 streaming" { |
| 747 | 736 | ||
| 748 | const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; | 737 | const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; |
| 749 | 738 | ||
| 750 | h.reset(); | 739 | h = Sha512.init(); |
| 751 | h.update("abc"); | 740 | h.update("abc"); |
| 752 | h.final(out[0..]); | 741 | h.final(out[0..]); |
| 753 | htest.assertEqual(h2, out[0..]); | 742 | htest.assertEqual(h2, out[0..]); |
| 754 | 743 | ||
| 755 | h.reset(); | 744 | h = Sha512.init(); |
| 756 | h.update("a"); | 745 | h.update("a"); |
| 757 | h.update("b"); | 746 | h.update("b"); |
| 758 | h.update("c"); | 747 | h.update("c"); |
lib/std/crypto/sha3.zig+9-17| ... | @@ -26,15 +26,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { | ... | @@ -26,15 +26,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type { |
| 26 | rate: usize, | 26 | rate: usize, |
| 27 | 27 | ||
| 28 | pub fn init() Self { | 28 | pub fn init() Self { |
| 29 | return comptime Self{ | 29 | return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) }; |
| 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(); | ||
| 38 | } | 30 | } |
| 39 | 31 | ||
| 40 | pub fn hash(b: []const u8, out: []u8) void { | 32 | pub fn hash(b: []const u8, out: []u8) void { |
| ... | @@ -189,12 +181,12 @@ test "sha3-224 streaming" { | ... | @@ -189,12 +181,12 @@ test "sha3-224 streaming" { |
| 189 | h.final(out[0..]); | 181 | h.final(out[0..]); |
| 190 | htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]); | 182 | htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]); |
| 191 | 183 | ||
| 192 | h.reset(); | 184 | h = Sha3_224.init(); |
| 193 | h.update("abc"); | 185 | h.update("abc"); |
| 194 | h.final(out[0..]); | 186 | h.final(out[0..]); |
| 195 | htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]); | 187 | htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]); |
| 196 | 188 | ||
| 197 | h.reset(); | 189 | h = Sha3_224.init(); |
| 198 | h.update("a"); | 190 | h.update("a"); |
| 199 | h.update("b"); | 191 | h.update("b"); |
| 200 | h.update("c"); | 192 | h.update("c"); |
| ... | @@ -215,12 +207,12 @@ test "sha3-256 streaming" { | ... | @@ -215,12 +207,12 @@ test "sha3-256 streaming" { |
| 215 | h.final(out[0..]); | 207 | h.final(out[0..]); |
| 216 | htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]); | 208 | htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]); |
| 217 | 209 | ||
| 218 | h.reset(); | 210 | h = Sha3_256.init(); |
| 219 | h.update("abc"); | 211 | h.update("abc"); |
| 220 | h.final(out[0..]); | 212 | h.final(out[0..]); |
| 221 | htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]); | 213 | htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]); |
| 222 | 214 | ||
| 223 | h.reset(); | 215 | h = Sha3_256.init(); |
| 224 | h.update("a"); | 216 | h.update("a"); |
| 225 | h.update("b"); | 217 | h.update("b"); |
| 226 | h.update("c"); | 218 | h.update("c"); |
| ... | @@ -255,12 +247,12 @@ test "sha3-384 streaming" { | ... | @@ -255,12 +247,12 @@ test "sha3-384 streaming" { |
| 255 | htest.assertEqual(h1, out[0..]); | 247 | htest.assertEqual(h1, out[0..]); |
| 256 | 248 | ||
| 257 | const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25"; | 249 | const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25"; |
| 258 | h.reset(); | 250 | h = Sha3_384.init(); |
| 259 | h.update("abc"); | 251 | h.update("abc"); |
| 260 | h.final(out[0..]); | 252 | h.final(out[0..]); |
| 261 | htest.assertEqual(h2, out[0..]); | 253 | htest.assertEqual(h2, out[0..]); |
| 262 | 254 | ||
| 263 | h.reset(); | 255 | h = Sha3_384.init(); |
| 264 | h.update("a"); | 256 | h.update("a"); |
| 265 | h.update("b"); | 257 | h.update("b"); |
| 266 | h.update("c"); | 258 | h.update("c"); |
| ... | @@ -286,12 +278,12 @@ test "sha3-512 streaming" { | ... | @@ -286,12 +278,12 @@ test "sha3-512 streaming" { |
| 286 | htest.assertEqual(h1, out[0..]); | 278 | htest.assertEqual(h1, out[0..]); |
| 287 | 279 | ||
| 288 | const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"; | 280 | const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0"; |
| 289 | h.reset(); | 281 | h = Sha3_512.init(); |
| 290 | h.update("abc"); | 282 | h.update("abc"); |
| 291 | h.final(out[0..]); | 283 | h.final(out[0..]); |
| 292 | htest.assertEqual(h2, out[0..]); | 284 | htest.assertEqual(h2, out[0..]); |
| 293 | 285 | ||
| 294 | h.reset(); | 286 | h = Sha3_512.init(); |
| 295 | h.update("a"); | 287 | h.update("a"); |
| 296 | h.update("b"); | 288 | h.update("b"); |
| 297 | h.update("c"); | 289 | h.update("c"); |