authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-09-30 18:36:31+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-01 02:04:30+02:00
logf1ad94437baaae40109f388a7d44d698c10a56d3
tree8267128a08b5cfb13dd2bb1dc06b4bf02c0a8160
parent58873ed3f90325cee5c442169d609d02c71fd05a

ghash & poly1305: use pointer to slices for keys and output


2 files changed, 6 insertions(+), 16 deletions(-)

lib/std/crypto/ghash.zig+3-8
......@@ -34,8 +34,7 @@ pub const Ghash = struct {
3434 leftover: usize = 0,
3535 buf: [block_size]u8 align(16) = undefined,
3636
37 pub fn init(key: []const u8) Ghash {
38 assert(key.len >= minimum_key_length);
37 pub fn init(key: *const [minimum_key_length]u8) Ghash {
3938 const h1 = mem.readIntBig(u64, key[0..8]);
4039 const h0 = mem.readIntBig(u64, key[8..16]);
4140 const h1r = @bitReverse(u64, h1);
......@@ -150,8 +149,7 @@ pub const Ghash = struct {
150149 }
151150 }
152151
153 pub fn final(st: *Ghash, out: []u8) void {
154 assert(out.len >= mac_length);
152 pub fn final(st: *Ghash, out: *[mac_length]u8) void {
155153 if (st.leftover > 0) {
156154 var i = st.leftover;
157155 while (i < block_size) : (i += 1) {
......@@ -165,10 +163,7 @@ pub const Ghash = struct {
165163 mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Ghash)]);
166164 }
167165
168 pub fn create(out: []u8, msg: []const u8, key: []const u8) void {
169 std.debug.assert(out.len >= mac_length);
170 std.debug.assert(key.len >= minimum_key_length);
171
166 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [minimum_key_length]u8) void {
172167 var st = Ghash.init(key);
173168 st.update(msg);
174169 st.final(out);
lib/std/crypto/poly1305.zig+3-8
......@@ -22,8 +22,7 @@ pub const Poly1305 = struct {
2222 // partial block buffer
2323 buf: [block_size]u8 align(16) = undefined,
2424
25 pub fn init(key: []const u8) Poly1305 {
26 std.debug.assert(key.len >= minimum_key_length);
25 pub fn init(key: *const [minimum_key_length]u8) Poly1305 {
2726 const t0 = mem.readIntLittle(u64, key[0..8]);
2827 const t1 = mem.readIntLittle(u64, key[8..16]);
2928 return Poly1305{
......@@ -115,8 +114,7 @@ pub const Poly1305 = struct {
115114 }
116115 }
117116
118 pub fn final(st: *Poly1305, out: []u8) void {
119 std.debug.assert(out.len >= mac_length);
117 pub fn final(st: *Poly1305, out: *[mac_length]u8) void {
120118 if (st.leftover > 0) {
121119 var i = st.leftover;
122120 st.buf[i] = 1;
......@@ -187,10 +185,7 @@ pub const Poly1305 = struct {
187185 std.mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Poly1305)]);
188186 }
189187
190 pub fn create(out: []u8, msg: []const u8, key: []const u8) void {
191 std.debug.assert(out.len >= mac_length);
192 std.debug.assert(key.len >= minimum_key_length);
193
188 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [minimum_key_length]u8) void {
194189 var st = Poly1305.init(key);
195190 st.update(msg);
196191 st.final(out);