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 {...@@ -84,7 +84,7 @@ fn Blake2s(comptime out_len: usize) type { return struct {
84 }84 }
8585
86 // Full middle blocks.86 // Full middle blocks.
87 while (off + 64 < b.len) : (off += 64) {87 while (off + 64 <= b.len) : (off += 64) {
88 d.t += 64;88 d.t += 64;
89 d.round(b[off..off + 64], false);89 d.round(b[off..off + 64], false);
90 }90 }
...@@ -229,6 +229,15 @@ test "blake2s256 streaming" {...@@ -229,6 +229,15 @@ test "blake2s256 streaming" {
229 htest.assertEqual(h2, out[0..]);229 htest.assertEqual(h2, out[0..]);
230}230}
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
233/////////////////////242/////////////////////
234// Blake2b243// Blake2b
...@@ -305,7 +314,7 @@ fn Blake2b(comptime out_len: usize) type { return struct {...@@ -305,7 +314,7 @@ fn Blake2b(comptime out_len: usize) type { return struct {
305 }314 }
306315
307 // Full middle blocks.316 // Full middle blocks.
308 while (off + 128 < b.len) : (off += 128) {317 while (off + 128 <= b.len) : (off += 128) {
309 d.t += 128;318 d.t += 128;
310 d.round(b[off..off + 128], false);319 d.round(b[off..off + 128], false);
311 }320 }
...@@ -447,3 +456,12 @@ test "blake2b512 streaming" {...@@ -447,3 +456,12 @@ test "blake2b512 streaming" {
447 h.final(out[0..]);456 h.final(out[0..]);
448 htest.assertEqual(h2, out[0..]);457 htest.assertEqual(h2, out[0..]);
449}458}
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 {...@@ -59,7 +59,7 @@ pub const Md5 = struct {
59 }59 }
6060
61 // Full middle blocks.61 // Full middle blocks.
62 while (off + 64 < b.len) : (off += 64) {62 while (off + 64 <= b.len) : (off += 64) {
63 d.round(b[off..off + 64]);63 d.round(b[off..off + 64]);
64 }64 }
6565
...@@ -253,3 +253,12 @@ test "md5 streaming" {...@@ -253,3 +253,12 @@ test "md5 streaming" {
253253
254 htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);254 htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
255}255}
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 {...@@ -60,7 +60,7 @@ pub const Sha1 = struct {
60 }60 }
6161
62 // Full middle blocks.62 // Full middle blocks.
63 while (off + 64 < b.len) : (off += 64) {63 while (off + 64 <= b.len) : (off += 64) {
64 d.round(b[off..off + 64]);64 d.round(b[off..off + 64]);
65 }65 }
6666
...@@ -284,3 +284,12 @@ test "sha1 streaming" {...@@ -284,3 +284,12 @@ test "sha1 streaming" {
284 h.final(out[0..]);284 h.final(out[0..]);
285 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);285 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);
286}286}
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 {...@@ -105,7 +105,7 @@ fn Sha2_32(comptime params: Sha2Params32) type { return struct {
105 }105 }
106106
107 // Full middle blocks.107 // Full middle blocks.
108 while (off + 64 < b.len) : (off += 64) {108 while (off + 64 <= b.len) : (off += 64) {
109 d.round(b[off..off + 64]);109 d.round(b[off..off + 64]);
110 }110 }
111111
...@@ -319,6 +319,15 @@ test "sha256 streaming" {...@@ -319,6 +319,15 @@ test "sha256 streaming" {
319 htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);319 htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);
320}320}
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
323/////////////////////332/////////////////////
324// Sha384 + Sha512333// Sha384 + Sha512
...@@ -420,7 +429,7 @@ fn Sha2_64(comptime params: Sha2Params64) type { return struct {...@@ -420,7 +429,7 @@ fn Sha2_64(comptime params: Sha2Params64) type { return struct {
420 }429 }
421430
422 // Full middle blocks.431 // Full middle blocks.
423 while (off + 128 < b.len) : (off += 128) {432 while (off + 128 <= b.len) : (off += 128) {
424 d.round(b[off..off + 128]);433 d.round(b[off..off + 128]);
425 }434 }
426435
...@@ -669,3 +678,12 @@ test "sha512 streaming" {...@@ -669,3 +678,12 @@ test "sha512 streaming" {
669 h.final(out[0..]);678 h.final(out[0..]);
670 htest.assertEqual(h2, out[0..]);679 htest.assertEqual(h2, out[0..]);
671}680}
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" {...@@ -217,6 +217,15 @@ test "sha3-256 streaming" {
217 htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);217 htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
218}218}
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
220test "sha3-384 single" {229test "sha3-384 single" {
221 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";230 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
222 htest.assertEqualHash(Sha3_384, h1 , "");231 htest.assertEqualHash(Sha3_384, h1 , "");
...@@ -278,3 +287,12 @@ test "sha3-512 streaming" {...@@ -278,3 +287,12 @@ test "sha3-512 streaming" {
278 h.final(out[0..]);287 h.final(out[0..]);
279 htest.assertEqual(h2, out[0..]);288 htest.assertEqual(h2, out[0..]);
280}289}
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}