Commit f92a5d7944

Frank Denis <github@pureftpd.org>
2020-08-20 13:06:23
Repair crypto/benchmark; add BLAKE2b256
Some MACs have a 64-bit output
1 parent 3bed749
Changed files (4)
lib/std/crypto/benchmark.zig
@@ -56,19 +56,19 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64
 
 const macs = [_]Crypto{
     Crypto{ .ty = crypto.onetimeauth.Poly1305, .name = "poly1305" },
-    Crypto{ .ty = crypto.auth.HmacMd5, .name = "hmac-md5" },
-    Crypto{ .ty = crypto.auth.HmacSha1, .name = "hmac-sha1" },
-    Crypto{ .ty = crypto.auth.sha2.HmacSha256, .name = "hmac-sha256" },
-    Crypto{ .ty = crypto.auth.sha2.HmacSha512, .name = "hmac-sha512" },
+    Crypto{ .ty = crypto.auth.hmac.HmacMd5, .name = "hmac-md5" },
+    Crypto{ .ty = crypto.auth.hmac.HmacSha1, .name = "hmac-sha1" },
+    Crypto{ .ty = crypto.auth.hmac.sha2.HmacSha256, .name = "hmac-sha256" },
+    Crypto{ .ty = crypto.auth.hmac.sha2.HmacSha512, .name = "hmac-sha512" },
 };
 
 pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
-    std.debug.assert(32 >= Mac.mac_length and 32 >= Mac.minimum_key_length);
+    std.debug.assert(64 >= Mac.mac_length and 32 >= Mac.minimum_key_length);
 
     var in: [1 * MiB]u8 = undefined;
     prng.random.bytes(in[0..]);
 
-    var key: [32]u8 = undefined;
+    var key: [64]u8 = undefined;
     prng.random.bytes(key[0..]);
 
     var offset: usize = 0;
lib/std/crypto/blake2.zig
@@ -74,7 +74,7 @@ pub fn Blake2s(comptime out_len: usize) type {
         key: []const u8,
 
         pub fn init() Self {
-            return init_keyed("");
+            return comptime init_keyed("");
         }
 
         pub fn init_keyed(key: []const u8) Self {
@@ -364,6 +364,7 @@ test "comptime blake2s256" {
 /////////////////////
 // Blake2b
 
+pub const Blake2b256 = Blake2b(256);
 pub const Blake2b384 = Blake2b(384);
 pub const Blake2b512 = Blake2b(512);
 
lib/std/crypto/blake3.zig
@@ -298,7 +298,7 @@ pub const Blake3 = struct {
 
     /// Construct a new `Blake3` for the regular hash function.
     pub fn init() Blake3 {
-        return Blake3.init_internal(IV, 0);
+        return comptime Blake3.init_internal(IV, 0);
     }
 
     /// Construct a new `Blake3` for the keyed hash function.
lib/std/crypto/sha3.zig
@@ -26,11 +26,11 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
         rate: usize,
 
         pub fn init() Self {
-            var d: Self = undefined;
-            mem.set(u8, d.s[0..], 0);
-            d.offset = 0;
-            d.rate = 200 - (bits / 4);
-            return d;
+            return comptime Self{
+                .s = [_]u8{0} ** 200,
+                .offset = 0,
+                .rate = 200 - (bits / 4),
+            };
         }
 
         pub fn reset(self: *Self) void {