| author | |
| committer | |
| log | 7c2bde1f07f5e672bb2320c517568dab9edab652 |
| tree | 80413ac9ae3b3496d1fa1bb7dd874ab6d8d05814 |
| parent | e2caf5752791cb81cd1110617d646e87f360e11b |
3 files changed, 29 insertions(+), 34 deletions(-)
lib/std/crypto/blake2.zig+6-10| ... | @@ -77,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type { | ... | @@ -77,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type { |
| 77 | buf_len: u8, | 77 | buf_len: u8, |
| 78 | 78 | ||
| 79 | pub fn init(options: Options) Self { | 79 | pub fn init(options: Options) Self { |
| 80 | debug.assert(8 <= out_bits and out_bits <= 256); | 80 | comptime debug.assert(8 <= out_bits and out_bits <= 256); |
| 81 | 81 | ||
| 82 | var d: Self = undefined; | 82 | var d: Self = undefined; |
| 83 | mem.copy(u32, d.h[0..], iv[0..]); | 83 | mem.copy(u32, d.h[0..], iv[0..]); |
| ... | @@ -125,7 +125,7 @@ pub fn Blake2s(comptime out_bits: usize) type { | ... | @@ -125,7 +125,7 @@ pub fn Blake2s(comptime out_bits: usize) type { |
| 125 | // Full middle blocks. | 125 | // Full middle blocks. |
| 126 | while (off + 64 < b.len) : (off += 64) { | 126 | while (off + 64 < b.len) : (off += 64) { |
| 127 | d.t += 64; | 127 | d.t += 64; |
| 128 | d.round(b[off .. off + 64], false); | 128 | d.round(b[off..][0..64], false); |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | // Copy any remainder for next pass. | 131 | // Copy any remainder for next pass. |
| ... | @@ -145,9 +145,7 @@ pub fn Blake2s(comptime out_bits: usize) type { | ... | @@ -145,9 +145,7 @@ pub fn Blake2s(comptime out_bits: usize) type { |
| 145 | } | 145 | } |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | fn round(d: *Self, b: []const u8, last: bool) void { | 148 | fn round(d: *Self, b: *const [64]u8, last: bool) void { |
| 149 | debug.assert(b.len == 64); | ||
| 150 | |||
| 151 | var m: [16]u32 = undefined; | 149 | var m: [16]u32 = undefined; |
| 152 | var v: [16]u32 = undefined; | 150 | var v: [16]u32 = undefined; |
| 153 | 151 | ||
| ... | @@ -422,7 +420,7 @@ pub fn Blake2b(comptime out_bits: usize) type { | ... | @@ -422,7 +420,7 @@ pub fn Blake2b(comptime out_bits: usize) type { |
| 422 | buf_len: u8, | 420 | buf_len: u8, |
| 423 | 421 | ||
| 424 | pub fn init(options: Options) Self { | 422 | pub fn init(options: Options) Self { |
| 425 | debug.assert(8 <= out_bits and out_bits <= 512); | 423 | comptime debug.assert(8 <= out_bits and out_bits <= 512); |
| 426 | 424 | ||
| 427 | var d: Self = undefined; | 425 | var d: Self = undefined; |
| 428 | mem.copy(u64, d.h[0..], iv[0..]); | 426 | mem.copy(u64, d.h[0..], iv[0..]); |
| ... | @@ -470,7 +468,7 @@ pub fn Blake2b(comptime out_bits: usize) type { | ... | @@ -470,7 +468,7 @@ pub fn Blake2b(comptime out_bits: usize) type { |
| 470 | // Full middle blocks. | 468 | // Full middle blocks. |
| 471 | while (off + 128 < b.len) : (off += 128) { | 469 | while (off + 128 < b.len) : (off += 128) { |
| 472 | d.t += 128; | 470 | d.t += 128; |
| 473 | d.round(b[off .. off + 128], false); | 471 | d.round(b[off..][0..128], false); |
| 474 | } | 472 | } |
| 475 | 473 | ||
| 476 | // Copy any remainder for next pass. | 474 | // Copy any remainder for next pass. |
| ... | @@ -490,9 +488,7 @@ pub fn Blake2b(comptime out_bits: usize) type { | ... | @@ -490,9 +488,7 @@ pub fn Blake2b(comptime out_bits: usize) type { |
| 490 | } | 488 | } |
| 491 | } | 489 | } |
| 492 | 490 | ||
| 493 | fn round(d: *Self, b: []const u8, last: bool) void { | 491 | fn round(d: *Self, b: *const [128]u8, last: bool) void { |
| 494 | debug.assert(b.len == 128); | ||
| 495 | |||
| 496 | var m: [16]u64 = undefined; | 492 | var m: [16]u64 = undefined; |
| 497 | var v: [16]u64 = undefined; | 493 | var v: [16]u64 = undefined; |
| 498 | 494 |
lib/std/crypto/gimli.zig+4-4| ... | @@ -39,13 +39,13 @@ pub const State = struct { | ... | @@ -39,13 +39,13 @@ pub const State = struct { |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | /// TODO follow the span() convention instead of having this and `toSliceConst` | 41 | /// TODO follow the span() convention instead of having this and `toSliceConst` |
| 42 | pub fn toSlice(self: *Self) []u8 { | 42 | pub fn toSlice(self: *Self) *[BLOCKBYTES]u8 { |
| 43 | return mem.sliceAsBytes(self.data[0..]); | 43 | return mem.asBytes(&self.data); |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | /// TODO follow the span() convention instead of having this and `toSlice` | 46 | /// TODO follow the span() convention instead of having this and `toSlice` |
| 47 | pub fn toSliceConst(self: *Self) []const u8 { | 47 | pub fn toSliceConst(self: *const Self) *const [BLOCKBYTES]u8 { |
| 48 | return mem.sliceAsBytes(self.data[0..]); | 48 | return mem.asBytes(&self.data); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | fn permute_unrolled(self: *Self) void { | 51 | fn permute_unrolled(self: *Self) void { |
lib/std/crypto/hmac.zig+19-20| ... | @@ -26,41 +26,41 @@ pub fn Hmac(comptime Hash: type) type { | ... | @@ -26,41 +26,41 @@ pub fn Hmac(comptime Hash: type) type { |
| 26 | pub const key_length = 32; // recommended key length | 26 | pub const key_length = 32; // recommended key length |
| 27 | 27 | ||
| 28 | o_key_pad: [Hash.block_length]u8, | 28 | o_key_pad: [Hash.block_length]u8, |
| 29 | i_key_pad: [Hash.block_length]u8, | ||
| 30 | scratch: [Hash.block_length]u8, | ||
| 31 | hash: Hash, | 29 | hash: Hash, |
| 32 | 30 | ||
| 33 | // HMAC(k, m) = H(o_key_pad || H(i_key_pad || msg)) where || is concatenation | 31 | // HMAC(k, m) = H(o_key_pad || H(i_key_pad || msg)) where || is concatenation |
| 34 | pub fn create(out: []u8, msg: []const u8, key: []const u8) void { | 32 | pub fn create(out: *[mac_length]u8, msg: []const u8, key: []const u8) void { |
| 35 | var ctx = Self.init(key); | 33 | var ctx = Self.init(key); |
| 36 | ctx.update(msg); | 34 | ctx.update(msg); |
| 37 | ctx.final(out[0..]); | 35 | ctx.final(out); |
| 38 | } | 36 | } |
| 39 | 37 | ||
| 40 | pub fn init(key: []const u8) Self { | 38 | pub fn init(key: []const u8) Self { |
| 41 | var ctx: Self = undefined; | 39 | var ctx: Self = undefined; |
| 40 | var scratch: [Hash.block_length]u8 = undefined; | ||
| 41 | var i_key_pad: [Hash.block_length]u8 = undefined; | ||
| 42 | 42 | ||
| 43 | // Normalize key length to block size of hash | 43 | // Normalize key length to block size of hash |
| 44 | if (key.len > Hash.block_length) { | 44 | if (key.len > Hash.block_length) { |
| 45 | Hash.hash(key, ctx.scratch[0..mac_length], .{}); | 45 | Hash.hash(key, scratch[0..mac_length], .{}); |
| 46 | mem.set(u8, ctx.scratch[mac_length..Hash.block_length], 0); | 46 | mem.set(u8, scratch[mac_length..Hash.block_length], 0); |
| 47 | } else if (key.len < Hash.block_length) { | 47 | } else if (key.len < Hash.block_length) { |
| 48 | mem.copy(u8, ctx.scratch[0..key.len], key); | 48 | mem.copy(u8, scratch[0..key.len], key); |
| 49 | mem.set(u8, ctx.scratch[key.len..Hash.block_length], 0); | 49 | mem.set(u8, scratch[key.len..Hash.block_length], 0); |
| 50 | } else { | 50 | } else { |
| 51 | mem.copy(u8, ctx.scratch[0..], key); | 51 | mem.copy(u8, scratch[0..], key); |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | for (ctx.o_key_pad) |*b, i| { | 54 | for (ctx.o_key_pad) |*b, i| { |
| 55 | b.* = ctx.scratch[i] ^ 0x5c; | 55 | b.* = scratch[i] ^ 0x5c; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | for (ctx.i_key_pad) |*b, i| { | 58 | for (i_key_pad) |*b, i| { |
| 59 | b.* = ctx.scratch[i] ^ 0x36; | 59 | b.* = scratch[i] ^ 0x36; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | ctx.hash = Hash.init(.{}); | 62 | ctx.hash = Hash.init(.{}); |
| 63 | ctx.hash.update(ctx.i_key_pad[0..]); | 63 | ctx.hash.update(&i_key_pad); |
| 64 | return ctx; | 64 | return ctx; |
| 65 | } | 65 | } |
| 66 | 66 | ||
| ... | @@ -68,14 +68,13 @@ pub fn Hmac(comptime Hash: type) type { | ... | @@ -68,14 +68,13 @@ pub fn Hmac(comptime Hash: type) type { |
| 68 | ctx.hash.update(msg); | 68 | ctx.hash.update(msg); |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | pub fn final(ctx: *Self, out: []u8) void { | 71 | pub fn final(ctx: *Self, out: *[mac_length]u8) void { |
| 72 | debug.assert(Hash.block_length >= out.len and out.len >= mac_length); | 72 | var scratch: [mac_length]u8 = undefined; |
| 73 | 73 | ctx.hash.final(&scratch); | |
| 74 | ctx.hash.final(ctx.scratch[0..mac_length]); | ||
| 75 | var ohash = Hash.init(.{}); | 74 | var ohash = Hash.init(.{}); |
| 76 | ohash.update(ctx.o_key_pad[0..]); | 75 | ohash.update(&ctx.o_key_pad); |
| 77 | ohash.update(ctx.scratch[0..mac_length]); | 76 | ohash.update(&scratch); |
| 78 | ohash.final(out[0..mac_length]); | 77 | ohash.final(out); |
| 79 | } | 78 | } |
| 80 | }; | 79 | }; |
| 81 | } | 80 | } |