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" {...@@ -57,3 +57,34 @@ test "crypto" {
57 _ = @import("crypto/sha3.zig");57 _ = @import("crypto/sha3.zig");
58 _ = @import("crypto/x25519.zig");58 _ = @import("crypto/x25519.zig");
59}59}
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 {...@@ -94,7 +94,7 @@ fn Blake2s(comptime out_len: usize) type {
94 var off: usize = 0;94 var off: usize = 0;
9595
96 // Partial buffer exists from previous update. Copy into buffer then hash.96 // 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) {
98 off += 64 - d.buf_len;98 off += 64 - d.buf_len;
99 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);99 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
100 d.t += 64;100 d.t += 64;
...@@ -331,7 +331,7 @@ fn Blake2b(comptime out_len: usize) type {...@@ -331,7 +331,7 @@ fn Blake2b(comptime out_len: usize) type {
331 var off: usize = 0;331 var off: usize = 0;
332332
333 // Partial buffer exists from previous update. Copy into buffer then hash.333 // 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) {
335 off += 128 - d.buf_len;335 off += 128 - d.buf_len;
336 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);336 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
337 d.t += 128;337 d.t += 128;
lib/std/crypto/md5.zig+1-1
...@@ -63,7 +63,7 @@ pub const Md5 = struct {...@@ -63,7 +63,7 @@ pub const Md5 = struct {
63 var off: usize = 0;63 var off: usize = 0;
6464
65 // Partial buffer exists from previous update. Copy into buffer then hash.65 // 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) {
67 off += 64 - d.buf_len;67 off += 64 - d.buf_len;
68 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);68 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 {...@@ -61,7 +61,7 @@ pub const Sha1 = struct {
61 var off: usize = 0;61 var off: usize = 0;
6262
63 // Partial buffer exists from previous update. Copy into buffer then hash.63 // 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) {
65 off += 64 - d.buf_len;65 off += 64 - d.buf_len;
66 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);66 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 {...@@ -116,7 +116,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
116 var off: usize = 0;116 var off: usize = 0;
117117
118 // Partial buffer exists from previous update. Copy into buffer then hash.118 // 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) {
120 off += 64 - d.buf_len;120 off += 64 - d.buf_len;
121 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);121 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
122122
...@@ -458,7 +458,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -458,7 +458,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
458 var off: usize = 0;458 var off: usize = 0;
459459
460 // Partial buffer exists from previous update. Copy into buffer then hash.460 // 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) {
462 off += 128 - d.buf_len;462 off += 128 - d.buf_len;
463 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);463 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
464464