authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-04-04 21:32:23+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-04-04 21:32:23+12:00
logf68c2e0a14c7d9997db0305981ce3e5998d88a36
treea7ddbf75a1711594e64c4f94ec57e58c526ecf9d
parent3d8541121b5a54b269d601d12426415f383a7007

Fix off-by-one error in all crypto functions


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

std/crypto/blake2.zig+20-2
......@@ -84,7 +84,7 @@ fn Blake2s(comptime out_len: usize) type { return struct {
8484 }
8585
8686 // Full middle blocks.
87 while (off + 64 < b.len) : (off += 64) {
87 while (off + 64 <= b.len) : (off += 64) {
8888 d.t += 64;
8989 d.round(b[off..off + 64], false);
9090 }
......@@ -229,6 +229,15 @@ test "blake2s256 streaming" {
229229 htest.assertEqual(h2, out[0..]);
230230}
231231
232test "blake2s256 aligned final" {
233 var block = []u8 {0} ** Blake2s256.block_size;
234 var out: [Blake2s256.digest_size]u8 = undefined;
235
236 var h = Blake2s256.init();
237 h.update(block);
238 h.final(out[0..]);
239}
240
232241
233242/////////////////////
234243// Blake2b
......@@ -305,7 +314,7 @@ fn Blake2b(comptime out_len: usize) type { return struct {
305314 }
306315
307316 // Full middle blocks.
308 while (off + 128 < b.len) : (off += 128) {
317 while (off + 128 <= b.len) : (off += 128) {
309318 d.t += 128;
310319 d.round(b[off..off + 128], false);
311320 }
......@@ -447,3 +456,12 @@ test "blake2b512 streaming" {
447456 h.final(out[0..]);
448457 htest.assertEqual(h2, out[0..]);
449458}
459
460test "blake2b512 aligned final" {
461 var block = []u8 {0} ** Blake2b512.block_size;
462 var out: [Blake2b512.digest_size]u8 = undefined;
463
464 var h = Blake2b512.init();
465 h.update(block);
466 h.final(out[0..]);
467}
std/crypto/md5.zig+10-1
......@@ -59,7 +59,7 @@ pub const Md5 = struct {
5959 }
6060
6161 // Full middle blocks.
62 while (off + 64 < b.len) : (off += 64) {
62 while (off + 64 <= b.len) : (off += 64) {
6363 d.round(b[off..off + 64]);
6464 }
6565
......@@ -253,3 +253,12 @@ test "md5 streaming" {
253253
254254 htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
255255}
256
257test "md5 aligned final" {
258 var block = []u8 {0} ** Md5.block_size;
259 var out: [Md5.digest_size]u8 = undefined;
260
261 var h = Md5.init();
262 h.update(block);
263 h.final(out[0..]);
264}
std/crypto/sha1.zig+10-1
......@@ -60,7 +60,7 @@ pub const Sha1 = struct {
6060 }
6161
6262 // Full middle blocks.
63 while (off + 64 < b.len) : (off += 64) {
63 while (off + 64 <= b.len) : (off += 64) {
6464 d.round(b[off..off + 64]);
6565 }
6666
......@@ -284,3 +284,12 @@ test "sha1 streaming" {
284284 h.final(out[0..]);
285285 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);
286286}
287
288test "sha1 aligned final" {
289 var block = []u8 {0} ** Sha1.block_size;
290 var out: [Sha1.digest_size]u8 = undefined;
291
292 var h = Sha1.init();
293 h.update(block);
294 h.final(out[0..]);
295}
std/crypto/sha2.zig+20-2
......@@ -105,7 +105,7 @@ fn Sha2_32(comptime params: Sha2Params32) type { return struct {
105105 }
106106
107107 // Full middle blocks.
108 while (off + 64 < b.len) : (off += 64) {
108 while (off + 64 <= b.len) : (off += 64) {
109109 d.round(b[off..off + 64]);
110110 }
111111
......@@ -319,6 +319,15 @@ test "sha256 streaming" {
319319 htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);
320320}
321321
322test "sha256 aligned final" {
323 var block = []u8 {0} ** Sha256.block_size;
324 var out: [Sha256.digest_size]u8 = undefined;
325
326 var h = Sha256.init();
327 h.update(block);
328 h.final(out[0..]);
329}
330
322331
323332/////////////////////
324333// Sha384 + Sha512
......@@ -420,7 +429,7 @@ fn Sha2_64(comptime params: Sha2Params64) type { return struct {
420429 }
421430
422431 // Full middle blocks.
423 while (off + 128 < b.len) : (off += 128) {
432 while (off + 128 <= b.len) : (off += 128) {
424433 d.round(b[off..off + 128]);
425434 }
426435
......@@ -669,3 +678,12 @@ test "sha512 streaming" {
669678 h.final(out[0..]);
670679 htest.assertEqual(h2, out[0..]);
671680}
681
682test "sha512 aligned final" {
683 var block = []u8 {0} ** Sha512.block_size;
684 var out: [Sha512.digest_size]u8 = undefined;
685
686 var h = Sha512.init();
687 h.update(block);
688 h.final(out[0..]);
689}
std/crypto/sha3.zig+18
......@@ -217,6 +217,15 @@ test "sha3-256 streaming" {
217217 htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
218218}
219219
220test "sha3-256 aligned final" {
221 var block = []u8 {0} ** Sha3_256.block_size;
222 var out: [Sha3_256.digest_size]u8 = undefined;
223
224 var h = Sha3_256.init();
225 h.update(block);
226 h.final(out[0..]);
227}
228
220229test "sha3-384 single" {
221230 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
222231 htest.assertEqualHash(Sha3_384, h1 , "");
......@@ -278,3 +287,12 @@ test "sha3-512 streaming" {
278287 h.final(out[0..]);
279288 htest.assertEqual(h2, out[0..]);
280289}
290
291test "sha3-512 aligned final" {
292 var block = []u8 {0} ** Sha3_512.block_size;
293 var out: [Sha3_512.digest_size]u8 = undefined;
294
295 var h = Sha3_512.init();
296 h.update(block);
297 h.final(out[0..]);
298}