authorgravatar for ziga.zeljko@gmail.comŽiga Željko <ziga.zeljko@gmail.com> 2020-10-26 11:03:12+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-26 19:19:34-04:00
log7c2bde1f07f5e672bb2320c517568dab9edab652
tree80413ac9ae3b3496d1fa1bb7dd874ab6d8d05814
parente2caf5752791cb81cd1110617d646e87f360e11b

std/crypto: API cleanup


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 {
7777 buf_len: u8,
7878
7979 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);
8181
8282 var d: Self = undefined;
8383 mem.copy(u32, d.h[0..], iv[0..]);
......@@ -125,7 +125,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
125125 // Full middle blocks.
126126 while (off + 64 < b.len) : (off += 64) {
127127 d.t += 64;
128 d.round(b[off .. off + 64], false);
128 d.round(b[off..][0..64], false);
129129 }
130130
131131 // Copy any remainder for next pass.
......@@ -145,9 +145,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
145145 }
146146 }
147147
148 fn round(d: *Self, b: []const u8, last: bool) void {
149 debug.assert(b.len == 64);
150
148 fn round(d: *Self, b: *const [64]u8, last: bool) void {
151149 var m: [16]u32 = undefined;
152150 var v: [16]u32 = undefined;
153151
......@@ -422,7 +420,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
422420 buf_len: u8,
423421
424422 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);
426424
427425 var d: Self = undefined;
428426 mem.copy(u64, d.h[0..], iv[0..]);
......@@ -470,7 +468,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
470468 // Full middle blocks.
471469 while (off + 128 < b.len) : (off += 128) {
472470 d.t += 128;
473 d.round(b[off .. off + 128], false);
471 d.round(b[off..][0..128], false);
474472 }
475473
476474 // Copy any remainder for next pass.
......@@ -490,9 +488,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
490488 }
491489 }
492490
493 fn round(d: *Self, b: []const u8, last: bool) void {
494 debug.assert(b.len == 128);
495
491 fn round(d: *Self, b: *const [128]u8, last: bool) void {
496492 var m: [16]u64 = undefined;
497493 var v: [16]u64 = undefined;
498494
lib/std/crypto/gimli.zig+4-4
......@@ -39,13 +39,13 @@ pub const State = struct {
3939 }
4040
4141 /// TODO follow the span() convention instead of having this and `toSliceConst`
42 pub fn toSlice(self: *Self) []u8 {
43 return mem.sliceAsBytes(self.data[0..]);
42 pub fn toSlice(self: *Self) *[BLOCKBYTES]u8 {
43 return mem.asBytes(&self.data);
4444 }
4545
4646 /// TODO follow the span() convention instead of having this and `toSlice`
47 pub fn toSliceConst(self: *Self) []const u8 {
48 return mem.sliceAsBytes(self.data[0..]);
47 pub fn toSliceConst(self: *const Self) *const [BLOCKBYTES]u8 {
48 return mem.asBytes(&self.data);
4949 }
5050
5151 fn permute_unrolled(self: *Self) void {
lib/std/crypto/hmac.zig+19-20
......@@ -26,41 +26,41 @@ pub fn Hmac(comptime Hash: type) type {
2626 pub const key_length = 32; // recommended key length
2727
2828 o_key_pad: [Hash.block_length]u8,
29 i_key_pad: [Hash.block_length]u8,
30 scratch: [Hash.block_length]u8,
3129 hash: Hash,
3230
3331 // 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 {
3533 var ctx = Self.init(key);
3634 ctx.update(msg);
37 ctx.final(out[0..]);
35 ctx.final(out);
3836 }
3937
4038 pub fn init(key: []const u8) Self {
4139 var ctx: Self = undefined;
40 var scratch: [Hash.block_length]u8 = undefined;
41 var i_key_pad: [Hash.block_length]u8 = undefined;
4242
4343 // Normalize key length to block size of hash
4444 if (key.len > Hash.block_length) {
45 Hash.hash(key, ctx.scratch[0..mac_length], .{});
46 mem.set(u8, ctx.scratch[mac_length..Hash.block_length], 0);
45 Hash.hash(key, scratch[0..mac_length], .{});
46 mem.set(u8, scratch[mac_length..Hash.block_length], 0);
4747 } else if (key.len < Hash.block_length) {
48 mem.copy(u8, ctx.scratch[0..key.len], key);
49 mem.set(u8, ctx.scratch[key.len..Hash.block_length], 0);
48 mem.copy(u8, scratch[0..key.len], key);
49 mem.set(u8, scratch[key.len..Hash.block_length], 0);
5050 } else {
51 mem.copy(u8, ctx.scratch[0..], key);
51 mem.copy(u8, scratch[0..], key);
5252 }
5353
5454 for (ctx.o_key_pad) |*b, i| {
55 b.* = ctx.scratch[i] ^ 0x5c;
55 b.* = scratch[i] ^ 0x5c;
5656 }
5757
58 for (ctx.i_key_pad) |*b, i| {
59 b.* = ctx.scratch[i] ^ 0x36;
58 for (i_key_pad) |*b, i| {
59 b.* = scratch[i] ^ 0x36;
6060 }
6161
6262 ctx.hash = Hash.init(.{});
63 ctx.hash.update(ctx.i_key_pad[0..]);
63 ctx.hash.update(&i_key_pad);
6464 return ctx;
6565 }
6666
......@@ -68,14 +68,13 @@ pub fn Hmac(comptime Hash: type) type {
6868 ctx.hash.update(msg);
6969 }
7070
71 pub fn final(ctx: *Self, out: []u8) void {
72 debug.assert(Hash.block_length >= out.len and out.len >= mac_length);
73
74 ctx.hash.final(ctx.scratch[0..mac_length]);
71 pub fn final(ctx: *Self, out: *[mac_length]u8) void {
72 var scratch: [mac_length]u8 = undefined;
73 ctx.hash.final(&scratch);
7574 var ohash = Hash.init(.{});
76 ohash.update(ctx.o_key_pad[0..]);
77 ohash.update(ctx.scratch[0..mac_length]);
78 ohash.final(out[0..mac_length]);
75 ohash.update(&ctx.o_key_pad);
76 ohash.update(&scratch);
77 ohash.final(out);
7978 }
8079 };
8180}