authorgravatar for jakwings@gmail.comJ.W <jakwings@gmail.com> 2020-02-24 01:45:37+08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-24 13:43:54-05:00
log5275b012020a7bb5de35120ca3e8f5d8d6ef2109
tree36d38a3fedca4a0edacec39259753254920739cd
parent907c5589ae20d1b2a73acf26c9d67139dfe3bfb9

hashing algorithms: fix logic and index out of bounds


5 files changed, 37 insertions(+), 6 deletions(-)

lib/std/crypto.zig+31
......@@ -57,3 +57,34 @@ test "crypto" {
5757 _ = @import("crypto/sha3.zig");
5858 _ = @import("crypto/x25519.zig");
5959}
60
61test "issue #4532: no index out of bounds" {
62 const types = [_]type{
63 Md5,
64 Sha1,
65 Sha224,
66 Sha256,
67 Sha384,
68 Sha512,
69 Blake2s224,
70 Blake2s256,
71 Blake2b384,
72 Blake2b512,
73 };
74
75 inline for (types) |Hasher| {
76 var block = [_]u8{'#'} ** Hasher.block_length;
77 var out1: [Hasher.digest_length]u8 = undefined;
78 var out2: [Hasher.digest_length]u8 = undefined;
79
80 var h = Hasher.init();
81 h.update(block[0..]);
82 h.final(out1[0..]);
83 h.reset();
84 h.update(block[0..1]);
85 h.update(block[1..]);
86 h.final(out2[0..]);
87
88 std.testing.expectEqual(out1, out2);
89 }
90}
lib/std/crypto/blake2.zig+2-2
......@@ -94,7 +94,7 @@ fn Blake2s(comptime out_len: usize) type {
9494 var off: usize = 0;
9595
9696 // Partial buffer exists from previous update. Copy into buffer then hash.
97 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
97 if (d.buf_len != 0 and d.buf_len + b.len >= 64) {
9898 off += 64 - d.buf_len;
9999 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
100100 d.t += 64;
......@@ -331,7 +331,7 @@ fn Blake2b(comptime out_len: usize) type {
331331 var off: usize = 0;
332332
333333 // Partial buffer exists from previous update. Copy into buffer then hash.
334 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
334 if (d.buf_len != 0 and d.buf_len + b.len >= 128) {
335335 off += 128 - d.buf_len;
336336 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
337337 d.t += 128;
lib/std/crypto/md5.zig+1-1
......@@ -63,7 +63,7 @@ pub const Md5 = struct {
6363 var off: usize = 0;
6464
6565 // Partial buffer exists from previous update. Copy into buffer then hash.
66 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
66 if (d.buf_len != 0 and d.buf_len + b.len >= 64) {
6767 off += 64 - d.buf_len;
6868 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
6969
lib/std/crypto/sha1.zig+1-1
......@@ -61,7 +61,7 @@ pub const Sha1 = struct {
6161 var off: usize = 0;
6262
6363 // Partial buffer exists from previous update. Copy into buffer then hash.
64 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
64 if (d.buf_len != 0 and d.buf_len + b.len >= 64) {
6565 off += 64 - d.buf_len;
6666 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
6767
lib/std/crypto/sha2.zig+2-2
......@@ -116,7 +116,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
116116 var off: usize = 0;
117117
118118 // Partial buffer exists from previous update. Copy into buffer then hash.
119 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
119 if (d.buf_len != 0 and d.buf_len + b.len >= 64) {
120120 off += 64 - d.buf_len;
121121 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
122122
......@@ -458,7 +458,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
458458 var off: usize = 0;
459459
460460 // Partial buffer exists from previous update. Copy into buffer then hash.
461 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
461 if (d.buf_len != 0 and d.buf_len + b.len >= 128) {
462462 off += 128 - d.buf_len;
463463 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
464464