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 {...@@ -77,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
77 buf_len: u8,77 buf_len: u8,
7878
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);
8181
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 }
130130
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 }
147147
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;
153151
...@@ -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,
423421
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);
426424
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 }
475473
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 }
492490
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;
498494
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 }
4040
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 }
4545
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 }
5050
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 length26 pub const key_length = 32; // recommended key length
2727
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,
3230
33 // HMAC(k, m) = H(o_key_pad || H(i_key_pad || msg)) where || is concatenation31 // 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 }
3937
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;
4242
43 // Normalize key length to block size of hash43 // 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 }
5353
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 }
5757
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 }
6161
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 }
6666
...@@ -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 }
7070
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;
7373 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}