authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-30 17:22:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-30 17:22:42-07:00
log8703053296a10f971ef119369116e6b7ef2e3798
tree3df83f2d7394892cdaef306987282aa9bb2173c6
parent18bc68847107d5bd183df0023b5bb123139f5343

std.crypto: support for hashes without options


26 files changed, 211 insertions(+), 195 deletions(-)

lib/std/Build/Step/Compile.zig+1-2
......@@ -1821,8 +1821,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
18211821 const partially_quoted = try std.mem.join(arena, "\" \"", escaped_args.items);
18221822 const args = try std.mem.concat(arena, u8, &[_][]const u8{ "\"", partially_quoted, "\"" });
18231823
1824 var args_hash: [Sha256.digest_length]u8 = undefined;
1825 Sha256.hash(args, &args_hash, .{});
1824 const args_hash = Sha256.hash(args);
18261825 var args_hex_hash: [Sha256.digest_length * 2]u8 = undefined;
18271826 _ = try std.fmt.bufPrint(&args_hex_hash, "{x}", .{&args_hash});
18281827
lib/std/compress/xz/block.zig+1-2
......@@ -190,8 +190,7 @@ pub fn Decoder(comptime ReaderType: type) type {
190190 return error.WrongChecksum;
191191 },
192192 .sha256 => {
193 var hash_a: [Sha256.digest_length]u8 = undefined;
194 Sha256.hash(unpacked_bytes, &hash_a);
193 const hash_a = Sha256.hash(unpacked_bytes);
195194
196195 var hash_b: [Sha256.digest_length]u8 = undefined;
197196 try self.inner_reader.readNoEof(&hash_b);
lib/std/crypto.zig+1-1
......@@ -368,7 +368,7 @@ test "issue #4532: no index out of bounds" {
368368 var block = [_]u8{'#'} ** Hasher.block_length;
369369 var out1: [Hasher.digest_length]u8 = undefined;
370370 var out2: [Hasher.digest_length]u8 = undefined;
371 const h0 = Hasher.init(.{});
371 const h0 = Hasher.init();
372372 var h = h0;
373373 h.update(block[0..]);
374374 h.final(&out1);
lib/std/crypto/25519/ed25519.zig+8-8
......@@ -54,7 +54,7 @@ pub const Ed25519 = struct {
5454 // Return the clamped secret scalar and prefix for this secret key
5555 fn scalarAndPrefix(self: SecretKey) struct { scalar: CompressedScalar, prefix: [32]u8 } {
5656 var az: [Sha512.digest_length]u8 = undefined;
57 var h = Sha512.init(.{});
57 var h = Sha512.init();
5858 h.update(&self.seed());
5959 h.final(&az);
6060
......@@ -80,7 +80,7 @@ pub const Ed25519 = struct {
8080 var t: [64]u8 = undefined;
8181 t[0..32].* = r_bytes;
8282 t[32..].* = public_key.bytes;
83 var h = Sha512.init(.{});
83 var h = Sha512.init();
8484 h.update(&t);
8585
8686 return Signer{ .h = h, .scalar = scalar, .nonce = nonce, .r_bytes = r_bytes };
......@@ -128,7 +128,7 @@ pub const Ed25519 = struct {
128128 }
129129
130130 fn computeNonceAndSign(public_key: PublicKey, msg: []const u8, noise: ?[noise_length]u8, scalar: CompressedScalar, prefix: []const u8) (IdentityElementError || NonCanonicalError || KeyMismatchError || WeakPublicKeyError)!Signature {
131 var h = Sha512.init(.{});
131 var h = Sha512.init();
132132 if (noise) |*z| {
133133 h.update(z);
134134 }
......@@ -257,7 +257,7 @@ pub const Ed25519 = struct {
257257 /// from which the actual secret is derived.
258258 pub fn generateDeterministic(seed: [seed_length]u8) IdentityElementError!KeyPair {
259259 var az: [Sha512.digest_length]u8 = undefined;
260 var h = Sha512.init(.{});
260 var h = Sha512.init();
261261 h.update(&seed);
262262 h.final(&az);
263263 const pk_p = Curve.basePoint.clampedMul(az[0..32].*) catch return error.IdentityElement;
......@@ -335,7 +335,7 @@ pub const Ed25519 = struct {
335335 return error.KeyMismatch;
336336 }
337337 const scalar_and_prefix = key_pair.secret_key.scalarAndPrefix();
338 var h = Sha512.init(.{});
338 var h = Sha512.init();
339339 h.update(&scalar_and_prefix.prefix);
340340 var noise2: [noise_length]u8 = undefined;
341341 crypto.random.bytes(&noise2);
......@@ -382,7 +382,7 @@ pub const Ed25519 = struct {
382382
383383 var hram_batch: [count]Curve.scalar.CompressedScalar = undefined;
384384 for (signature_batch, 0..) |signature, i| {
385 var h = Sha512.init(.{});
385 var h = Sha512.init();
386386 h.update(&r_batch[i]);
387387 h.update(&signature.public_key.bytes);
388388 h.update(signature.msg);
......@@ -452,7 +452,7 @@ pub const Ed25519 = struct {
452452 /// Create an blind key pair from an existing key pair, a blinding seed and a context.
453453 pub fn init(key_pair: Ed25519.KeyPair, blind_seed: [blind_seed_length]u8, ctx: []const u8) (NonCanonicalError || IdentityElementError)!BlindKeyPair {
454454 var h: [Sha512.digest_length]u8 = undefined;
455 Sha512.hash(&key_pair.secret_key.seed(), &h, .{});
455 h = Sha512.hash(&key_pair.secret_key.seed());
456456 Curve.scalar.clamp(h[0..32]);
457457 const scalar = Curve.scalar.reduce(h[0..32].*);
458458
......@@ -494,7 +494,7 @@ pub const Ed25519 = struct {
494494 /// Compute a blind context from a blinding seed and a context.
495495 fn blindCtx(blind_seed: [blind_seed_length]u8, ctx: []const u8) [Sha512.digest_length]u8 {
496496 var blind_h: [Sha512.digest_length]u8 = undefined;
497 var hx = Sha512.init(.{});
497 var hx = Sha512.init();
498498 hx.update(&blind_seed);
499499 hx.update(&[1]u8{0});
500500 hx.update(ctx);
lib/std/crypto/25519/edwards25519.zig+3-3
......@@ -467,7 +467,7 @@ pub const Edwards25519 = struct {
467467 var xctx = ctx;
468468 var hctx: [H.digest_length]u8 = undefined;
469469 if (ctx.len > 0xff) {
470 var st = H.init(.{});
470 var st = H.init();
471471 st.update("H2C-OVERSIZE-DST-");
472472 st.update(ctx);
473473 st.final(&hctx);
......@@ -476,7 +476,7 @@ pub const Edwards25519 = struct {
476476 const empty_block = [_]u8{0} ** H.block_length;
477477 var t = [3]u8{ 0, n * h_l, 0 };
478478 var xctx_len_u8 = [1]u8{@as(u8, @intCast(xctx.len))};
479 var st = H.init(.{});
479 var st = H.init();
480480 st.update(empty_block[0..]);
481481 st.update(s);
482482 st.update(t[0..]);
......@@ -493,7 +493,7 @@ pub const Edwards25519 = struct {
493493 u[i + j] ^= u[i + j - H.digest_length];
494494 }
495495 t[2] += 1;
496 st = H.init(.{});
496 st = H.init();
497497 st.update(u[i..][0..H.digest_length]);
498498 st.update(t[2..3]);
499499 st.update(xctx);
lib/std/crypto/25519/x25519.zig+1-2
......@@ -55,8 +55,7 @@ pub const X25519 = struct {
5555 /// Create a key pair from an Ed25519 key pair
5656 pub fn fromEd25519(ed25519_key_pair: crypto.sign.Ed25519.KeyPair) (IdentityElementError || EncodingError)!KeyPair {
5757 const seed = ed25519_key_pair.secret_key.seed();
58 var az: [Sha512.digest_length]u8 = undefined;
59 Sha512.hash(&seed, &az, .{});
58 const az = Sha512.hash(&seed);
6059 var sk = az[0..32].*;
6160 Curve.scalar.clamp(&sk);
6261 const pk = try publicKeyFromEd25519(ed25519_key_pair.public_key);
lib/std/crypto/Certificate.zig+2-3
......@@ -1037,8 +1037,7 @@ pub const rsa = struct {
10371037 std.mem.copyForwards(u8, m_p[(8 + Hash.digest_length)..], salt);
10381038
10391039 // 13. Let H' = Hash(M'), an octet string of length hLen.
1040 var h_p: [Hash.digest_length]u8 = undefined;
1041 Hash.hash(m_p, &h_p);
1040 const h_p = Hash.hash(m_p);
10421041
10431042 // 14. If H = H', output "consistent". Otherwise, output
10441043 // "inconsistent".
......@@ -1054,7 +1053,7 @@ pub const rsa = struct {
10541053
10551054 while (idx < len) {
10561055 std.mem.writeInt(u32, hash[seed.len..][0..4], counter, .big);
1057 Hash.hash(&hash, out[idx..][0..Hash.digest_length]);
1056 out[idx..][0..Hash.digest_length].* = Hash.hash(&hash);
10581057 idx += Hash.digest_length;
10591058 counter += 1;
10601059 }
lib/std/crypto/Sha1.zig+3-3
......@@ -277,9 +277,9 @@ const RoundParam = struct {
277277const htest = @import("test.zig");
278278
279279test "sha1 single" {
280 try htest.assertEqualHashInterface(Sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "");
281 try htest.assertEqualHashInterface(Sha1, "a9993e364706816aba3e25717850c26c9cd0d89d", "abc");
282 try htest.assertEqualHashInterface(Sha1, "a49b2446a02c645bf419f995b67091253a04a259", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
280 try htest.assertEqualHash(Sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709", "");
281 try htest.assertEqualHash(Sha1, "a9993e364706816aba3e25717850c26c9cd0d89d", "abc");
282 try htest.assertEqualHash(Sha1, "a49b2446a02c645bf419f995b67091253a04a259", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
283283}
284284
285285test "sha1 streaming" {
lib/std/crypto/argon2.zig+5-5
......@@ -113,7 +113,7 @@ fn initHash(
113113 var h0: H0 = undefined;
114114 var parameters: [24]u8 = undefined;
115115 var tmp: [4]u8 = undefined;
116 var b2 = Blake2b512.init(.{});
116 var b2 = Blake2b512.init();
117117 mem.writeInt(u32, parameters[0..4], params.p, .little);
118118 mem.writeInt(u32, parameters[4..8], @as(u32, @intCast(dk_len)), .little);
119119 mem.writeInt(u32, parameters[8..12], params.m, .little);
......@@ -149,7 +149,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {
149149 var out_buf: [H.digest_length]u8 = undefined;
150150
151151 if (out.len <= H.digest_length) {
152 var h = H.init(.{ .expected_out_bits = out.len * 8 });
152 var h = H.initOptions(.{ .expected_out_bits = out.len * 8 });
153153 h.update(&outlen_bytes);
154154 h.update(in);
155155 h.final(&out_buf);
......@@ -157,7 +157,7 @@ fn blake2bLong(out: []u8, in: []const u8) void {
157157 return;
158158 }
159159
160 var h = H.init(.{});
160 var h = H.init();
161161 h.update(&outlen_bytes);
162162 h.update(in);
163163 h.final(&out_buf);
......@@ -168,12 +168,12 @@ fn blake2bLong(out: []u8, in: []const u8) void {
168168 var in_buf: [H.digest_length]u8 = undefined;
169169 while (out_slice.len > H.digest_length) {
170170 in_buf = out_buf;
171 H.hash(&in_buf, &out_buf, .{});
171 out_buf = H.hash(&in_buf);
172172 out_slice[0 .. H.digest_length / 2].* = out_buf[0 .. H.digest_length / 2].*;
173173 out_slice = out_slice[H.digest_length / 2 ..];
174174 }
175175 in_buf = out_buf;
176 H.hash(&in_buf, &out_buf, .{ .expected_out_bits = out_slice.len * 8 });
176 out_buf = H.hashOptions(&in_buf, .{ .expected_out_bits = out_slice.len * 8 });
177177 @memcpy(out_slice, out_buf[0..out_slice.len]);
178178}
179179
lib/std/crypto/bcrypt.zig+4-4
......@@ -499,7 +499,7 @@ const pbkdf_prf = struct {
499499
500500 pub fn init(key: []const u8) Self {
501501 var self: Self = undefined;
502 self.hasher = Sha512.init(.{});
502 self.hasher = Sha512.init();
503503 Sha512.hash(key, &self.sha2pass, .{});
504504 return self;
505505 }
......@@ -575,7 +575,7 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v
575575 return error.InvalidInput;
576576 }
577577 var sha2pass: [Sha512.digest_length]u8 = undefined;
578 Sha512.hash(pass, &sha2pass, .{});
578 sha2pass = Sha512.hash(pass);
579579 const stride = (key.len + tmp.len - 1) / tmp.len;
580580 var amt = (key.len + stride - 1) / stride;
581581 if (math.shr(usize, key.len, 32) >= amt) {
......@@ -587,14 +587,14 @@ pub fn opensshKdf(pass: []const u8, salt: []const u8, key: []u8, rounds: u32) !v
587587 var count_salt: [4]u8 = undefined;
588588 std.mem.writeInt(u32, count_salt[0..], count, .big);
589589 var sha2salt: [Sha512.digest_length]u8 = undefined;
590 var h = Sha512.init(.{});
590 var h = Sha512.init();
591591 h.update(salt);
592592 h.update(&count_salt);
593593 h.final(&sha2salt);
594594 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);
595595 tmp = tmp2;
596596 for (1..rounds) |_| {
597 Sha512.hash(&tmp2, &sha2salt, .{});
597 sha2salt = Sha512.hash(&tmp2);
598598 tmp2 = pbkdf_prf.hash(sha2pass, sha2salt);
599599 for (&tmp, tmp2) |*o, t| o.* ^= t;
600600 }
lib/std/crypto/blake2.zig+90-60
......@@ -40,7 +40,12 @@ pub fn Blake2s(comptime out_bits: usize) type {
4040 pub const key_length_min = 0;
4141 pub const key_length_max = 32;
4242 pub const key_length = 32; // recommended key length
43 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null, expected_out_bits: usize = out_bits };
43 pub const Options = struct {
44 key: ?[]const u8 = null,
45 salt: ?[8]u8 = null,
46 context: ?[8]u8 = null,
47 expected_out_bits: usize = out_bits,
48 };
4449
4550 const iv = [8]u32{
4651 0x6A09E667,
......@@ -72,7 +77,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
7277 buf: [64]u8,
7378 buf_len: u8,
7479
75 pub fn init(options: Options) Self {
80 pub fn initOptions(options: Options) Self {
7681 comptime debug.assert(8 <= out_bits and out_bits <= 256);
7782
7883 var d: Self = undefined;
......@@ -100,10 +105,20 @@ pub fn Blake2s(comptime out_bits: usize) type {
100105 return d;
101106 }
102107
103 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
104 var d = Self.init(options);
108 pub fn init() Self {
109 return initOptions(.{});
110 }
111
112 pub fn hash(b: []const u8) [digest_length]u8 {
113 return hashOptions(b, .{});
114 }
115
116 pub fn hashOptions(b: []const u8, options: Options) [digest_length]u8 {
117 var out: [digest_length]u8 = undefined;
118 var d = Self.initOptions(options);
105119 d.update(b);
106 d.final(out);
120 d.final(&out);
121 return out;
107122 }
108123
109124 pub fn update(d: *Self, b: []const u8) void {
......@@ -215,7 +230,7 @@ test "blake2s160 single" {
215230}
216231
217232test "blake2s160 streaming" {
218 var h = Blake2s160.init(.{});
233 var h = Blake2s160.init();
219234 var out: [20]u8 = undefined;
220235
221236 const h1 = "354c9c33f735962418bdacb9479873429c34916f";
......@@ -225,12 +240,12 @@ test "blake2s160 streaming" {
225240
226241 const h2 = "5ae3b99be29b01834c3b508521ede60438f8de17";
227242
228 h = Blake2s160.init(.{});
243 h = Blake2s160.init();
229244 h.update("abc");
230245 h.final(out[0..]);
231246 try htest.assertEqual(h2, out[0..]);
232247
233 h = Blake2s160.init(.{});
248 h = Blake2s160.init();
234249 h.update("a");
235250 h.update("b");
236251 h.update("c");
......@@ -239,26 +254,26 @@ test "blake2s160 streaming" {
239254
240255 const h3 = "b60c4dc60e2681e58fbc24e77f07e02c69e72ed0";
241256
242 h = Blake2s160.init(.{});
257 h = Blake2s160.init();
243258 h.update("a" ** 32);
244259 h.update("b" ** 32);
245260 h.final(out[0..]);
246261 try htest.assertEqual(h3, out[0..]);
247262
248 h = Blake2s160.init(.{});
263 h = Blake2s160.init();
249264 h.update("a" ** 32 ++ "b" ** 32);
250265 h.final(out[0..]);
251266 try htest.assertEqual(h3, out[0..]);
252267
253268 const h4 = "4667fd60791a7fe41f939bca646b4529e296bd68";
254269
255 h = Blake2s160.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
270 h = Blake2s160.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
256271 h.update("a" ** 32);
257272 h.update("b" ** 32);
258273 h.final(out[0..]);
259274 try htest.assertEqual(h4, out[0..]);
260275
261 h = Blake2s160.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
276 h = Blake2s160.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
262277 h.update("a" ** 32 ++ "b" ** 32);
263278 h.final(out[0..]);
264279 try htest.assertEqual(h4, out[0..]);
......@@ -275,7 +290,7 @@ test "comptime blake2s160" {
275290
276291 try htest.assertEqualHash(Blake2s160, h1, block[0..]);
277292
278 var h = Blake2s160.init(.{});
293 var h = Blake2s160.init();
279294 h.update(&block);
280295 h.final(out[0..]);
281296
......@@ -298,7 +313,7 @@ test "blake2s224 single" {
298313}
299314
300315test "blake2s224 streaming" {
301 var h = Blake2s224.init(.{});
316 var h = Blake2s224.init();
302317 var out: [28]u8 = undefined;
303318
304319 const h1 = "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4";
......@@ -308,12 +323,12 @@ test "blake2s224 streaming" {
308323
309324 const h2 = "0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55";
310325
311 h = Blake2s224.init(.{});
326 h = Blake2s224.init();
312327 h.update("abc");
313328 h.final(out[0..]);
314329 try htest.assertEqual(h2, out[0..]);
315330
316 h = Blake2s224.init(.{});
331 h = Blake2s224.init();
317332 h.update("a");
318333 h.update("b");
319334 h.update("c");
......@@ -322,26 +337,26 @@ test "blake2s224 streaming" {
322337
323338 const h3 = "557381a78facd2b298640f4e32113e58967d61420af1aa939d0cfe01";
324339
325 h = Blake2s224.init(.{});
340 h = Blake2s224.init();
326341 h.update("a" ** 32);
327342 h.update("b" ** 32);
328343 h.final(out[0..]);
329344 try htest.assertEqual(h3, out[0..]);
330345
331 h = Blake2s224.init(.{});
346 h = Blake2s224.init();
332347 h.update("a" ** 32 ++ "b" ** 32);
333348 h.final(out[0..]);
334349 try htest.assertEqual(h3, out[0..]);
335350
336351 const h4 = "a4d6a9d253441b80e5dfd60a04db169ffab77aec56a2855c402828c3";
337352
338 h = Blake2s224.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
353 h = Blake2s224.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
339354 h.update("a" ** 32);
340355 h.update("b" ** 32);
341356 h.final(out[0..]);
342357 try htest.assertEqual(h4, out[0..]);
343358
344 h = Blake2s224.init(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
359 h = Blake2s224.initOptions(.{ .context = [_]u8{0x69} ** 8, .salt = [_]u8{0x42} ** 8 });
345360 h.update("a" ** 32 ++ "b" ** 32);
346361 h.final(out[0..]);
347362 try htest.assertEqual(h4, out[0..]);
......@@ -357,7 +372,7 @@ test "comptime blake2s224" {
357372
358373 try htest.assertEqualHash(Blake2s224, h1, block[0..]);
359374
360 var h = Blake2s224.init(.{});
375 var h = Blake2s224.init();
361376 h.update(&block);
362377 h.final(out[0..]);
363378
......@@ -380,7 +395,7 @@ test "blake2s256 single" {
380395}
381396
382397test "blake2s256 streaming" {
383 var h = Blake2s256.init(.{});
398 var h = Blake2s256.init();
384399 var out: [32]u8 = undefined;
385400
386401 const h1 = "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9";
......@@ -390,12 +405,12 @@ test "blake2s256 streaming" {
390405
391406 const h2 = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982";
392407
393 h = Blake2s256.init(.{});
408 h = Blake2s256.init();
394409 h.update("abc");
395410 h.final(out[0..]);
396411 try htest.assertEqual(h2, out[0..]);
397412
398 h = Blake2s256.init(.{});
413 h = Blake2s256.init();
399414 h.update("a");
400415 h.update("b");
401416 h.update("c");
......@@ -404,13 +419,13 @@ test "blake2s256 streaming" {
404419
405420 const h3 = "8d8711dade07a6b92b9a3ea1f40bee9b2c53ff3edd2a273dec170b0163568977";
406421
407 h = Blake2s256.init(.{});
422 h = Blake2s256.init();
408423 h.update("a" ** 32);
409424 h.update("b" ** 32);
410425 h.final(out[0..]);
411426 try htest.assertEqual(h3, out[0..]);
412427
413 h = Blake2s256.init(.{});
428 h = Blake2s256.init();
414429 h.update("a" ** 32 ++ "b" ** 32);
415430 h.final(out[0..]);
416431 try htest.assertEqual(h3, out[0..]);
......@@ -422,16 +437,16 @@ test "blake2s256 keyed" {
422437 const h1 = "10f918da4d74fab3302e48a5d67d03804b1ec95372a62a0f33b7c9fa28ba1ae6";
423438 const key = "secret_key";
424439
425 Blake2s256.hash("a" ** 64 ++ "b" ** 64, &out, .{ .key = key });
440 out = Blake2s256.hashOptions("a" ** 64 ++ "b" ** 64, .{ .key = key });
426441 try htest.assertEqual(h1, out[0..]);
427442
428 var h = Blake2s256.init(.{ .key = key });
443 var h = Blake2s256.initOptions(.{ .key = key });
429444 h.update("a" ** 64 ++ "b" ** 64);
430445 h.final(out[0..]);
431446
432447 try htest.assertEqual(h1, out[0..]);
433448
434 h = Blake2s256.init(.{ .key = key });
449 h = Blake2s256.initOptions(.{ .key = key });
435450 h.update("a" ** 64);
436451 h.update("b" ** 64);
437452 h.final(out[0..]);
......@@ -449,7 +464,7 @@ test "comptime blake2s256" {
449464
450465 try htest.assertEqualHash(Blake2s256, h1, block[0..]);
451466
452 var h = Blake2s256.init(.{});
467 var h = Blake2s256.init();
453468 h.update(&block);
454469 h.final(out[0..]);
455470
......@@ -474,7 +489,12 @@ pub fn Blake2b(comptime out_bits: usize) type {
474489 pub const key_length_min = 0;
475490 pub const key_length_max = 64;
476491 pub const key_length = 32; // recommended key length
477 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null, expected_out_bits: usize = out_bits };
492 pub const Options = struct {
493 key: ?[]const u8 = null,
494 salt: ?[16]u8 = null,
495 context: ?[16]u8 = null,
496 expected_out_bits: usize = out_bits,
497 };
478498
479499 const iv = [8]u64{
480500 0x6a09e667f3bcc908,
......@@ -508,7 +528,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
508528 buf: [128]u8,
509529 buf_len: u8,
510530
511 pub fn init(options: Options) Self {
531 pub fn initOptions(options: Options) Self {
512532 comptime debug.assert(8 <= out_bits and out_bits <= 512);
513533
514534 var d: Self = undefined;
......@@ -536,10 +556,20 @@ pub fn Blake2b(comptime out_bits: usize) type {
536556 return d;
537557 }
538558
539 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
540 var d = Self.init(options);
559 pub fn init() Self {
560 return initOptions(.{});
561 }
562
563 pub fn hash(b: []const u8) [digest_length]u8 {
564 return hashOptions(b, .{});
565 }
566
567 pub fn hashOptions(b: []const u8, options: Options) [digest_length]u8 {
568 var out: [digest_length]u8 = undefined;
569 var d = Self.initOptions(options);
541570 d.update(b);
542 d.final(out);
571 d.final(&out);
572 return out;
543573 }
544574
545575 pub fn update(d: *Self, b: []const u8) void {
......@@ -639,7 +669,7 @@ test "blake2b160 single" {
639669}
640670
641671test "blake2b160 streaming" {
642 var h = Blake2b160.init(.{});
672 var h = Blake2b160.init();
643673 var out: [20]u8 = undefined;
644674
645675 const h1 = "3345524abf6bbe1809449224b5972c41790b6cf2";
......@@ -649,12 +679,12 @@ test "blake2b160 streaming" {
649679
650680 const h2 = "384264f676f39536840523f284921cdc68b6846b";
651681
652 h = Blake2b160.init(.{});
682 h = Blake2b160.init();
653683 h.update("abc");
654684 h.final(out[0..]);
655685 try htest.assertEqual(h2, out[0..]);
656686
657 h = Blake2b160.init(.{});
687 h = Blake2b160.init();
658688 h.update("a");
659689 h.update("b");
660690 h.update("c");
......@@ -663,18 +693,18 @@ test "blake2b160 streaming" {
663693
664694 const h3 = "43758f5de1740f651f1ae39de92260fe8bd5a11f";
665695
666 h = Blake2b160.init(.{});
696 h = Blake2b160.init();
667697 h.update("a" ** 64 ++ "b" ** 64);
668698 h.final(out[0..]);
669699 try htest.assertEqual(h3, out[0..]);
670700
671 h = Blake2b160.init(.{});
701 h = Blake2b160.init();
672702 h.update("a" ** 64);
673703 h.update("b" ** 64);
674704 h.final(out[0..]);
675705 try htest.assertEqual(h3, out[0..]);
676706
677 h = Blake2b160.init(.{});
707 h = Blake2b160.init();
678708 h.update("a" ** 64);
679709 h.update("b" ** 64);
680710 h.final(out[0..]);
......@@ -682,13 +712,13 @@ test "blake2b160 streaming" {
682712
683713 const h4 = "72328f8a8200663752fc302d372b5dd9b49dd8dc";
684714
685 h = Blake2b160.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
715 h = Blake2b160.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
686716 h.update("a" ** 64);
687717 h.update("b" ** 64);
688718 h.final(out[0..]);
689719 try htest.assertEqual(h4, out[0..]);
690720
691 h = Blake2b160.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
721 h = Blake2b160.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
692722 h.update("a" ** 64);
693723 h.update("b" ** 64);
694724 h.final(out[0..]);
......@@ -705,7 +735,7 @@ test "comptime blake2b160" {
705735
706736 try htest.assertEqualHash(Blake2b160, h1, block[0..]);
707737
708 var h = Blake2b160.init(.{});
738 var h = Blake2b160.init();
709739 h.update(&block);
710740 h.final(out[0..]);
711741
......@@ -728,7 +758,7 @@ test "blake2b384 single" {
728758}
729759
730760test "blake2b384 streaming" {
731 var h = Blake2b384.init(.{});
761 var h = Blake2b384.init();
732762 var out: [48]u8 = undefined;
733763
734764 const h1 = "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100";
......@@ -738,12 +768,12 @@ test "blake2b384 streaming" {
738768
739769 const h2 = "6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30f54ee6c66ba83be64b302d7cba6ce15bb556f4";
740770
741 h = Blake2b384.init(.{});
771 h = Blake2b384.init();
742772 h.update("abc");
743773 h.final(out[0..]);
744774 try htest.assertEqual(h2, out[0..]);
745775
746 h = Blake2b384.init(.{});
776 h = Blake2b384.init();
747777 h.update("a");
748778 h.update("b");
749779 h.update("c");
......@@ -752,18 +782,18 @@ test "blake2b384 streaming" {
752782
753783 const h3 = "b7283f0172fecbbd7eca32ce10d8a6c06b453cb3cf675b33eb4246f0da2bb94a6c0bdd6eec0b5fd71ec4fd51be80bf4c";
754784
755 h = Blake2b384.init(.{});
785 h = Blake2b384.init();
756786 h.update("a" ** 64 ++ "b" ** 64);
757787 h.final(out[0..]);
758788 try htest.assertEqual(h3, out[0..]);
759789
760 h = Blake2b384.init(.{});
790 h = Blake2b384.init();
761791 h.update("a" ** 64);
762792 h.update("b" ** 64);
763793 h.final(out[0..]);
764794 try htest.assertEqual(h3, out[0..]);
765795
766 h = Blake2b384.init(.{});
796 h = Blake2b384.init();
767797 h.update("a" ** 64);
768798 h.update("b" ** 64);
769799 h.final(out[0..]);
......@@ -771,13 +801,13 @@ test "blake2b384 streaming" {
771801
772802 const h4 = "934c48fcb197031c71f583d92f98703510805e72142e0b46f5752d1e971bc86c355d556035613ff7a4154b4de09dac5c";
773803
774 h = Blake2b384.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
804 h = Blake2b384.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
775805 h.update("a" ** 64);
776806 h.update("b" ** 64);
777807 h.final(out[0..]);
778808 try htest.assertEqual(h4, out[0..]);
779809
780 h = Blake2b384.init(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
810 h = Blake2b384.initOptions(.{ .context = [_]u8{0x69} ** 16, .salt = [_]u8{0x42} ** 16 });
781811 h.update("a" ** 64);
782812 h.update("b" ** 64);
783813 h.final(out[0..]);
......@@ -794,7 +824,7 @@ test "comptime blake2b384" {
794824
795825 try htest.assertEqualHash(Blake2b384, h1, block[0..]);
796826
797 var h = Blake2b384.init(.{});
827 var h = Blake2b384.init();
798828 h.update(&block);
799829 h.final(out[0..]);
800830
......@@ -817,7 +847,7 @@ test "blake2b512 single" {
817847}
818848
819849test "blake2b512 streaming" {
820 var h = Blake2b512.init(.{});
850 var h = Blake2b512.init();
821851 var out: [64]u8 = undefined;
822852
823853 const h1 = "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
......@@ -827,12 +857,12 @@ test "blake2b512 streaming" {
827857
828858 const h2 = "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
829859
830 h = Blake2b512.init(.{});
860 h = Blake2b512.init();
831861 h.update("abc");
832862 h.final(out[0..]);
833863 try htest.assertEqual(h2, out[0..]);
834864
835 h = Blake2b512.init(.{});
865 h = Blake2b512.init();
836866 h.update("a");
837867 h.update("b");
838868 h.update("c");
......@@ -841,12 +871,12 @@ test "blake2b512 streaming" {
841871
842872 const h3 = "049980af04d6a2cf16b4b49793c3ed7e40732073788806f2c989ebe9547bda0541d63abe298ec8955d08af48ae731f2e8a0bd6d201655a5473b4aa79d211b920";
843873
844 h = Blake2b512.init(.{});
874 h = Blake2b512.init();
845875 h.update("a" ** 64 ++ "b" ** 64);
846876 h.final(out[0..]);
847877 try htest.assertEqual(h3, out[0..]);
848878
849 h = Blake2b512.init(.{});
879 h = Blake2b512.init();
850880 h.update("a" ** 64);
851881 h.update("b" ** 64);
852882 h.final(out[0..]);
......@@ -859,7 +889,7 @@ test "blake2b512 keyed" {
859889 const h1 = "8a978060ccaf582f388f37454363071ac9a67e3a704585fd879fb8a419a447e389c7c6de790faa20a7a7dccf197de736bc5b40b98a930b36df5bee7555750c4d";
860890 const key = "secret_key";
861891
862 Blake2b512.hash("a" ** 64 ++ "b" ** 64, &out, .{ .key = key });
892 out = Blake2b512.hashOptions("a" ** 64 ++ "b" ** 64, .{ .key = key });
863893 try htest.assertEqual(h1, out[0..]);
864894
865895 var h = Blake2b512.init(.{ .key = key });
......@@ -886,7 +916,7 @@ test "comptime blake2b512" {
886916
887917 try htest.assertEqualHash(Blake2b512, h1, block[0..]);
888918
889 var h = Blake2b512.init(.{});
919 var h = Blake2b512.init();
890920 h.update(&block);
891921 h.final(out[0..]);
892922
lib/std/crypto/ecdsa.zig+1-1
......@@ -200,7 +200,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
200200
201201 fn init(secret_key: SecretKey, noise: ?[noise_length]u8) !Signer {
202202 return Signer{
203 .h = Hash.init(.{}),
203 .h = Hash.init(),
204204 .secret_key = secret_key,
205205 .noise = noise,
206206 };
lib/std/crypto/hash_composition.zig+6-3
......@@ -25,14 +25,17 @@ pub fn Composition(comptime H1: type, comptime H2: type) type {
2525 /// Options for both hashes.
2626 pub const Options = struct {
2727 /// Options for H1.
28 H1: H1.Options = .{},
28 H1: if (@hasDecl(H1, "Options")) H1.Options else struct {} = .{},
2929 /// Options for H2.
30 H2: H2.Options = .{},
30 H2: if (@hasDecl(H2, "Options")) H2.Options else struct {} = .{},
3131 };
3232
3333 /// Initialize the hash composition with the given options.
3434 pub fn init(options: Options) Self {
35 return Self{ .H1 = H1.init(options.H1), .H2 = H2.init(options.H2) };
35 return .{
36 .H1 = if (@hasDecl(H1, "Options")) H1.init(options.H1) else H1.init(),
37 .H2 = if (@hasDecl(H2, "Options")) H2.init(options.H2) else H2.init(),
38 };
3639 }
3740
3841 /// Compute H1(H2(b)).
lib/std/crypto/hmac.zig+1-1
......@@ -37,7 +37,7 @@ pub fn Hmac(comptime Hash: type) type {
3737
3838 // Normalize key length to block size of hash
3939 if (key.len > Hash.block_length) {
40 Hash.hash(key, scratch[0..mac_length]);
40 scratch[0..mac_length].* = Hash.hash(key);
4141 @memset(scratch[mac_length..Hash.block_length], 0);
4242 } else if (key.len < Hash.block_length) {
4343 @memcpy(scratch[0..key.len], key);
lib/std/crypto/md5.zig+8-14
......@@ -52,15 +52,9 @@ pub const Md5 = struct {
5252 };
5353 }
5454
55 pub fn hash(data: []const u8, out: *[digest_length]u8) void {
56 var d = Md5.init();
57 d.update(data);
58 d.final(out);
59 }
60
61 pub fn hashResult(data: []const u8) [digest_length]u8 {
55 pub fn hash(data: []const u8) [digest_length]u8 {
6256 var out: [digest_length]u8 = undefined;
63 var d = Md5.init(.{});
57 var d = Md5.init();
6458 d.update(data);
6559 d.final(&out);
6660 return out;
......@@ -249,22 +243,22 @@ test "single" {
249243}
250244
251245test "streaming" {
252 var h = Md5.init(.{});
246 var h = Md5.init();
253247 var out: [16]u8 = undefined;
254248
255249 h.final(out[0..]);
256250 try htest.assertEqual("d41d8cd98f00b204e9800998ecf8427e", out[0..]);
257251
258 h = Md5.init(.{});
252 h = Md5.init();
259253 h.update("abc");
260254 h.final(out[0..]);
261255 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
262256
263 h = Md5.init(.{});
257 h = Md5.init();
264258 h.update("a");
265259 h.update("b");
266260 h.update("c");
267 h.final(out[0..]);
261 h.final(&out);
268262
269263 try htest.assertEqual("900150983cd24fb0d6963f7d28e17f72", out[0..]);
270264}
......@@ -273,7 +267,7 @@ test "aligned final" {
273267 var block = [_]u8{0} ** Md5.block_length;
274268 var out: [Md5.digest_length]u8 = undefined;
275269
276 var h = Md5.init(.{});
270 var h = Md5.init();
277271 h.update(&block);
278 h.final(out[0..]);
272 h.final(&out);
279273}
lib/std/crypto/ml_kem.zig+9-9
......@@ -254,7 +254,7 @@ fn Kyber(comptime p: Params) type {
254254 @memcpy(&m, &seed);
255255 } else {
256256 // m = H(seed)
257 sha3.Sha3_256.hash(&seed, &m, .{});
257 m = sha3.Sha3_256.hash(&seed);
258258 }
259259 } else {
260260 crypto.random.bytes(&m);
......@@ -262,7 +262,7 @@ fn Kyber(comptime p: Params) type {
262262
263263 // (K', r) = G(m ‖ H(pk))
264264 var kr: [inner_plaintext_length + h_length]u8 = undefined;
265 var g = sha3.Sha3_512.init(.{});
265 var g = sha3.Sha3_512.init();
266266 g.update(&m);
267267 g.update(&pk.hpk);
268268 g.final(&kr);
......@@ -277,7 +277,7 @@ fn Kyber(comptime p: Params) type {
277277 };
278278 } else {
279279 // Compute H(c) and put in second slot of kr, which will be (K', H(c)).
280 sha3.Sha3_256.hash(&ct, kr[32..], .{});
280 kr[32..].* = sha3.Sha3_256.hash(&ct);
281281
282282 var ss: [shared_length]u8 = undefined;
283283 sha3.Shake256.hash(&kr, &ss, .{});
......@@ -297,7 +297,7 @@ fn Kyber(comptime p: Params) type {
297297 pub fn fromBytes(buf: *const [bytes_length]u8) errors.NonCanonicalError!PublicKey {
298298 var ret: PublicKey = undefined;
299299 ret.pk = try InnerPk.fromBytes(buf[0..InnerPk.bytes_length]);
300 sha3.Sha3_256.hash(buf, &ret.hpk, .{});
300 ret.hpk = sha3.Sha3_256.hash(buf);
301301 return ret;
302302 }
303303 };
......@@ -320,7 +320,7 @@ fn Kyber(comptime p: Params) type {
320320
321321 // (K'', r') = G(m' ‖ H(pk))
322322 var kr2: [64]u8 = undefined;
323 var g = sha3.Sha3_512.init(.{});
323 var g = sha3.Sha3_512.init();
324324 g.update(&m2);
325325 g.update(&sk.hpk);
326326 g.final(&kr2);
......@@ -329,7 +329,7 @@ fn Kyber(comptime p: Params) type {
329329 const ct2 = sk.pk.encrypt(&m2, kr2[32..64]);
330330
331331 // Compute H(ct) and put in the second slot of kr2 which will be (K'', H(ct)).
332 sha3.Sha3_256.hash(ct, kr2[32..], .{});
332 kr2[32..].* = sha3.Sha3_256.hash(ct);
333333
334334 // Replace K'' by z in the first slot of kr2 if ct ≠ ct'.
335335 cmov(32, kr2[0..32], sk.z, ctneq(ciphertext_length, ct.*, ct2));
......@@ -389,7 +389,7 @@ fn Kyber(comptime p: Params) type {
389389 ret.secret_key.z = seed[inner_seed_length..seed_length].*;
390390
391391 // Compute H(pk)
392 sha3.Sha3_256.hash(&ret.public_key.pk.toBytes(), &ret.secret_key.hpk, .{});
392 ret.secret_key.hpk = sha3.Sha3_256.hash(&ret.public_key.pk.toBytes());
393393 ret.public_key.hpk = ret.secret_key.hpk;
394394
395395 return ret;
......@@ -505,7 +505,7 @@ fn Kyber(comptime p: Params) type {
505505 // Derives inner PKE keypair from given seed.
506506 fn innerKeyFromSeed(seed: [inner_seed_length]u8, pk: *InnerPk, sk: *InnerSk) void {
507507 var expanded_seed: [64]u8 = undefined;
508 var h = sha3.Sha3_512.init(.{});
508 var h = sha3.Sha3_512.init();
509509 if (p.ml_kem) h.update(&[1]u8{p.k});
510510 h.update(&seed);
511511 h.final(&expanded_seed);
......@@ -1734,7 +1734,7 @@ test "NIST KAT test" {
17341734 for (&seed, 0..) |*s, i| {
17351735 s.* = @as(u8, @intCast(i));
17361736 }
1737 var f = sha2.Sha256.init(.{});
1737 var f = sha2.Sha256.init();
17381738 const fw = f.writer();
17391739 var g = NistDRBG.init(seed);
17401740 try std.fmt.format(fw, "# {s}\n\n", .{mode.name});
lib/std/crypto/salsa20.zig+1-1
......@@ -523,7 +523,7 @@ pub const SealedBox = struct {
523523 pub const KeyPair = Box.KeyPair;
524524
525525 fn createNonce(pk1: [public_length]u8, pk2: [public_length]u8) [Box.nonce_length]u8 {
526 var hasher = Blake2b(Box.nonce_length * 8).init(.{});
526 var hasher = Blake2b(Box.nonce_length * 8).init();
527527 hasher.update(&pk1);
528528 hasher.update(&pk2);
529529 var nonce: [Box.nonce_length]u8 = undefined;
lib/std/crypto/sha2.zig+30-30
......@@ -92,7 +92,6 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {
9292 const Self = @This();
9393 pub const block_length = 64;
9494 pub const digest_length = digest_bits / 8;
95 pub const Options = struct {};
9695
9796 s: [8]u32 align(16),
9897 // Streaming Cache
......@@ -100,15 +99,16 @@ fn Sha2x32(comptime iv: Iv32, digest_bits: comptime_int) type {
10099 buf_len: u8 = 0,
101100 total_len: u64 = 0,
102101
103 pub fn init(options: Options) Self {
104 _ = options;
102 pub fn init() Self {
105103 return Self{ .s = iv };
106104 }
107105
108 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
109 var d = Self.init(options);
106 pub fn hash(b: []const u8) [digest_length]u8 {
107 var out: [digest_length]u8 = undefined;
108 var d = Self.init();
110109 d.update(b);
111 d.final(out);
110 d.final(&out);
111 return out;
112112 }
113113
114114 pub fn update(d: *Self, b: []const u8) void {
......@@ -421,18 +421,18 @@ test Sha224 {
421421}
422422
423423test "sha224 streaming" {
424 var h = Sha224.init(.{});
424 var h = Sha224.init();
425425 var out: [28]u8 = undefined;
426426
427427 h.final(out[0..]);
428428 try htest.assertEqual("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", out[0..]);
429429
430 h = Sha224.init(.{});
430 h = Sha224.init();
431431 h.update("abc");
432432 h.final(out[0..]);
433433 try htest.assertEqual("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", out[0..]);
434434
435 h = Sha224.init(.{});
435 h = Sha224.init();
436436 h.update("a");
437437 h.update("b");
438438 h.update("c");
......@@ -453,18 +453,18 @@ test Sha256T192 {
453453}
454454
455455test "sha256 streaming" {
456 var h = Sha256.init(.{});
456 var h = Sha256.init();
457457 var out: [32]u8 = undefined;
458458
459459 h.final(out[0..]);
460460 try htest.assertEqual("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", out[0..]);
461461
462 h = Sha256.init(.{});
462 h = Sha256.init();
463463 h.update("abc");
464464 h.final(out[0..]);
465465 try htest.assertEqual("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", out[0..]);
466466
467 h = Sha256.init(.{});
467 h = Sha256.init();
468468 h.update("a");
469469 h.update("b");
470470 h.update("c");
......@@ -473,12 +473,12 @@ test "sha256 streaming" {
473473}
474474
475475test "sha256 aligned final" {
476 var block = [_]u8{0} ** Sha256.block_length;
476 var block: [Sha256.block_length]u8 = @splat(0);
477477 var out: [Sha256.digest_length]u8 = undefined;
478478
479 var h = Sha256.init(.{});
479 var h = Sha256.init();
480480 h.update(&block);
481 h.final(out[0..]);
481 h.final(&out);
482482}
483483
484484const Iv64 = [8]u64;
......@@ -487,7 +487,6 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {
487487 const Self = @This();
488488 pub const block_length = 128;
489489 pub const digest_length = std.math.divCeil(comptime_int, digest_bits, 8) catch unreachable;
490 pub const Options = struct {};
491490
492491 s: Iv64,
493492 // Streaming Cache
......@@ -495,15 +494,16 @@ fn Sha2x64(comptime iv: Iv64, digest_bits: comptime_int) type {
495494 buf_len: u8 = 0,
496495 total_len: u128 = 0,
497496
498 pub fn init(options: Options) Self {
499 _ = options;
500 return Self{ .s = iv };
497 pub fn init() Self {
498 return .{ .s = iv };
501499 }
502500
503 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
504 var d = Self.init(options);
501 pub fn hash(b: []const u8) [digest_length]u8 {
502 var out: [digest_length]u8 = undefined;
503 var d = Self.init();
505504 d.update(b);
506 d.final(out);
505 d.final(&out);
506 return out;
507507 }
508508
509509 pub fn update(d: *Self, b: []const u8) void {
......@@ -737,7 +737,7 @@ fn truncatedSha512Iv(digest_len: comptime_int) Iv64 {
737737
738738 var params: [@sizeOf(Iv64)]u8 = undefined;
739739 const algo_str = std.fmt.comptimePrint("SHA-512/{d}", .{digest_len});
740 GenHash.hash(algo_str, &params, .{});
740 params = GenHash.hash(algo_str);
741741
742742 return Iv64{
743743 std.mem.readInt(u64, params[0..8], .big),
......@@ -788,7 +788,7 @@ test Sha384 {
788788}
789789
790790test "sha384 streaming" {
791 var h = Sha384.init(.{});
791 var h = Sha384.init();
792792 var out: [48]u8 = undefined;
793793
794794 const h1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";
......@@ -797,12 +797,12 @@ test "sha384 streaming" {
797797
798798 const h2 = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
799799
800 h = Sha384.init(.{});
800 h = Sha384.init();
801801 h.update("abc");
802802 h.final(out[0..]);
803803 try htest.assertEqual(h2, out[0..]);
804804
805 h = Sha384.init(.{});
805 h = Sha384.init();
806806 h.update("a");
807807 h.update("b");
808808 h.update("c");
......@@ -822,7 +822,7 @@ test Sha512 {
822822}
823823
824824test "sha512 streaming" {
825 var h = Sha512.init(.{});
825 var h = Sha512.init();
826826 var out: [64]u8 = undefined;
827827
828828 const h1 = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
......@@ -831,12 +831,12 @@ test "sha512 streaming" {
831831
832832 const h2 = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
833833
834 h = Sha512.init(.{});
834 h = Sha512.init();
835835 h.update("abc");
836836 h.final(out[0..]);
837837 try htest.assertEqual(h2, out[0..]);
838838
839 h = Sha512.init(.{});
839 h = Sha512.init();
840840 h.update("a");
841841 h.update("b");
842842 h.update("c");
......@@ -848,7 +848,7 @@ test "sha512 aligned final" {
848848 var block = [_]u8{0} ** Sha512.block_length;
849849 var out: [Sha512.digest_length]u8 = undefined;
850850
851 var h = Sha512.init(.{});
851 var h = Sha512.init();
852852 h.update(&block);
853853 h.final(out[0..]);
854854}
lib/std/crypto/sha3.zig+30-20
......@@ -59,15 +59,25 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim
5959 pub const Options = struct { delim: u8 = default_delim };
6060
6161 /// Initialize a Keccak hash function.
62 pub fn init(options: Options) Self {
62 pub fn initOptions(options: Options) Self {
6363 return Self{ .st = .{ .delim = options.delim } };
6464 }
6565
66 pub fn init() Self {
67 return initOptions(.{});
68 }
69
70 pub fn hash(bytes: []const u8) [digest_length]u8 {
71 return hashOptions(bytes, .{});
72 }
73
6674 /// Hash a slice of bytes.
67 pub fn hash(bytes: []const u8, out: *[digest_length]u8, options: Options) void {
68 var st = Self.init(options);
75 pub fn hashOptions(bytes: []const u8, options: Options) [digest_length]u8 {
76 var out: [digest_length]u8 = undefined;
77 var st = Self.initOptions(options);
6978 st.update(bytes);
70 st.final(out);
79 st.final(&out);
80 return out;
7181 }
7282
7383 /// Absorb a slice of bytes into the state.
......@@ -547,18 +557,18 @@ test "sha3-224 single" {
547557}
548558
549559test "sha3-224 streaming" {
550 var h = Sha3_224.init(.{});
560 var h = Sha3_224.init();
551561 var out: [28]u8 = undefined;
552562
553563 h.final(out[0..]);
554564 try htest.assertEqual("6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", out[0..]);
555565
556 h = Sha3_224.init(.{});
566 h = Sha3_224.init();
557567 h.update("abc");
558568 h.final(out[0..]);
559569 try htest.assertEqual("e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", out[0..]);
560570
561 h = Sha3_224.init(.{});
571 h = Sha3_224.init();
562572 h.update("a");
563573 h.update("b");
564574 h.update("c");
......@@ -573,18 +583,18 @@ test "sha3-256 single" {
573583}
574584
575585test "sha3-256 streaming" {
576 var h = Sha3_256.init(.{});
586 var h = Sha3_256.init();
577587 var out: [32]u8 = undefined;
578588
579589 h.final(out[0..]);
580590 try htest.assertEqual("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", out[0..]);
581591
582 h = Sha3_256.init(.{});
592 h = Sha3_256.init();
583593 h.update("abc");
584594 h.final(out[0..]);
585595 try htest.assertEqual("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", out[0..]);
586596
587 h = Sha3_256.init(.{});
597 h = Sha3_256.init();
588598 h.update("a");
589599 h.update("b");
590600 h.update("c");
......@@ -596,7 +606,7 @@ test "sha3-256 aligned final" {
596606 var block = [_]u8{0} ** Sha3_256.block_length;
597607 var out: [Sha3_256.digest_length]u8 = undefined;
598608
599 var h = Sha3_256.init(.{});
609 var h = Sha3_256.init();
600610 h.update(&block);
601611 h.final(out[0..]);
602612}
......@@ -611,7 +621,7 @@ test "sha3-384 single" {
611621}
612622
613623test "sha3-384 streaming" {
614 var h = Sha3_384.init(.{});
624 var h = Sha3_384.init();
615625 var out: [48]u8 = undefined;
616626
617627 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
......@@ -619,12 +629,12 @@ test "sha3-384 streaming" {
619629 try htest.assertEqual(h1, out[0..]);
620630
621631 const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
622 h = Sha3_384.init(.{});
632 h = Sha3_384.init();
623633 h.update("abc");
624634 h.final(out[0..]);
625635 try htest.assertEqual(h2, out[0..]);
626636
627 h = Sha3_384.init(.{});
637 h = Sha3_384.init();
628638 h.update("a");
629639 h.update("b");
630640 h.update("c");
......@@ -642,7 +652,7 @@ test "sha3-512 single" {
642652}
643653
644654test "sha3-512 streaming" {
645 var h = Sha3_512.init(.{});
655 var h = Sha3_512.init();
646656 var out: [64]u8 = undefined;
647657
648658 const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";
......@@ -650,12 +660,12 @@ test "sha3-512 streaming" {
650660 try htest.assertEqual(h1, out[0..]);
651661
652662 const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
653 h = Sha3_512.init(.{});
663 h = Sha3_512.init();
654664 h.update("abc");
655665 h.final(out[0..]);
656666 try htest.assertEqual(h2, out[0..]);
657667
658 h = Sha3_512.init(.{});
668 h = Sha3_512.init();
659669 h.update("a");
660670 h.update("b");
661671 h.update("c");
......@@ -667,7 +677,7 @@ test "sha3-512 aligned final" {
667677 var block = [_]u8{0} ** Sha3_512.block_length;
668678 var out: [Sha3_512.digest_length]u8 = undefined;
669679
670 var h = Sha3_512.init(.{});
680 var h = Sha3_512.init();
671681 h.update(&block);
672682 h.final(out[0..]);
673683}
......@@ -730,10 +740,10 @@ test "SHA-3 with streaming" {
730740 var msg: [613]u8 = [613]u8{ 0x97, 0xd1, 0x2d, 0x1a, 0x16, 0x2d, 0x36, 0x4d, 0x20, 0x62, 0x19, 0x0b, 0x14, 0x93, 0xbb, 0xf8, 0x5b, 0xea, 0x04, 0xc2, 0x61, 0x8e, 0xd6, 0x08, 0x81, 0xa1, 0x1d, 0x73, 0x27, 0x48, 0xbf, 0xa4, 0xba, 0xb1, 0x9a, 0x48, 0x9c, 0xf9, 0x9b, 0xff, 0x34, 0x48, 0xa9, 0x75, 0xea, 0xc8, 0xa3, 0x48, 0x24, 0x9d, 0x75, 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, 0xb2, 0x51, 0x1a, 0xe9, 0x3e, 0x89, 0x9f, 0x0c, 0x79 };
731741 var out: [Sha3_256.digest_length]u8 = undefined;
732742
733 Sha3_256.hash(&msg, &out, .{});
743 out = Sha3_256.hash(&msg);
734744 try htest.assertEqual("5780048dfa381a1d01c747906e4a08711dd34fd712ecd7c6801dd2b38fd81a89", &out);
735745
736 var h = Sha3_256.init(.{});
746 var h = Sha3_256.init();
737747 h.update(msg[0..64]);
738748 h.update(msg[64..613]);
739749 h.final(&out);
lib/std/crypto/test.zig-10
......@@ -7,16 +7,6 @@ pub fn assertEqualHash(
77 comptime Hasher: type,
88 expected_hex: *const [Hasher.digest_length * 2:0]u8,
99 input: []const u8,
10) !void {
11 var h: [Hasher.digest_length]u8 = undefined;
12 Hasher.hash(input, &h, .{});
13 try assertEqual(expected_hex, &h);
14}
15
16pub fn assertEqualHashInterface(
17 comptime Hasher: type,
18 expected_hex: *const [Hasher.digest_length * 2:0]u8,
19 input: []const u8,
2010) !void {
2111 const digest = Hasher.hash(input);
2212 try assertEqual(expected_hex, &digest);
lib/std/crypto/tls.zig-6
......@@ -576,12 +576,6 @@ pub fn hkdfExpandLabel(
576576 return result;
577577}
578578
579pub fn emptyHash(comptime Hash: type) [Hash.digest_length]u8 {
580 var result: [Hash.digest_length]u8 = undefined;
581 Hash.hash(&.{}, &result);
582 return result;
583}
584
585579pub fn hmac(comptime Hmac: type, message: []const u8, key: [Hmac.key_length]u8) [Hmac.mac_length]u8 {
586580 var result: [Hmac.mac_length]u8 = undefined;
587581 Hmac.create(&result, message, &key);
lib/std/crypto/tls/Client.zig+1-1
......@@ -524,7 +524,7 @@ pub fn init(stream: anytype, options: Options) InitError(@TypeOf(stream))!Client
524524 const hello_hash = p.transcript_hash.peek();
525525 const zeroes = [1]u8{0} ** P.Hash.digest_length;
526526 const early_secret = P.Hkdf.extract(&[1]u8{0}, &zeroes);
527 const empty_hash = tls.emptyHash(P.Hash);
527 const empty_hash = P.Hash.hash(&.{});
528528 p.version = .{ .tls_1_3 = undefined };
529529 const pv = &p.version.tls_1_3;
530530 const hs_derived_secret = hkdfExpandLabel(P.Hkdf, early_secret, "derived", &empty_hash, P.Hash.digest_length);
src/Package.zig+1-2
......@@ -132,8 +132,7 @@ pub const Hash = struct {
132132 @memcpy(result.bytes[i..][0..sub_path.len], sub_path);
133133 return result;
134134 }
135 var bin_digest: [Algo.digest_length]u8 = undefined;
136 Algo.hash(sub_path, &bin_digest);
135 const bin_digest = Algo.hash(sub_path);
137136 _ = std.fmt.bufPrint(result.bytes[i..], "{x}", .{&bin_digest}) catch unreachable;
138137 return result;
139138 }
src/link/MachO/CodeSignature.zig+2-2
......@@ -307,7 +307,7 @@ pub fn writeAdhocSignature(
307307 var buf = std.ArrayList(u8).init(allocator);
308308 defer buf.deinit();
309309 try req.write(buf.writer());
310 Sha256.hash(buf.items, &hash);
310 hash = Sha256.hash(buf.items);
311311 self.code_directory.addSpecialHash(req.slotType(), hash);
312312
313313 try blobs.append(.{ .requirements = req });
......@@ -319,7 +319,7 @@ pub fn writeAdhocSignature(
319319 var buf = std.ArrayList(u8).init(allocator);
320320 defer buf.deinit();
321321 try ents.write(buf.writer());
322 Sha256.hash(buf.items, &hash);
322 hash = Sha256.hash(buf.items);
323323 self.code_directory.addSpecialHash(ents.slotType(), hash);
324324
325325 try blobs.append(.{ .entitlements = ents });
src/link/MachO/hasher.zig+1-1
......@@ -58,7 +58,7 @@ pub fn ParallelHasher(comptime Hasher: type) type {
5858 const tracy = trace(@src());
5959 defer tracy.end();
6060 err.* = file.preadAll(buffer, fstart);
61 Hasher.hash(buffer, out);
61 out.* = Hasher.hash(buffer);
6262 }
6363
6464 const Self = @This();
src/link/MachO/uuid.zig+1-1
......@@ -28,7 +28,7 @@ pub fn calcUuid(comp: *const Compilation, file: fs.File, file_size: u64, out: *[
2828 @memcpy(final_buffer[i * Md5.digest_length ..][0..Md5.digest_length], &hash);
2929 }
3030
31 Md5.hash(final_buffer, out);
31 out.* = Md5.hash(final_buffer);
3232 conform(out);
3333}
3434