From 345b71793664d8d69b04be1d81c22409ddcf54bd Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 21 Sep 2026 12:11:08 -0700 Subject: [PATCH] std.crypto: add missing finalResult methods --- lib/std/crypto/aegis.zig | 6 ++++++ lib/std/crypto/ascon.zig | 6 ++++++ lib/std/crypto/bcrypt.zig | 6 ++++++ lib/std/crypto/blake2.zig | 12 ++++++++++++ lib/std/crypto/cbc_mac.zig | 6 ++++++ lib/std/crypto/cmac.zig | 6 ++++++ lib/std/crypto/ghash_polyval.zig | 6 ++++++ lib/std/crypto/hash_composition.zig | 6 ++++++ lib/std/crypto/hmac.zig | 6 ++++++ lib/std/crypto/md5.zig | 6 ++++++ lib/std/crypto/poly1305.zig | 6 ++++++ lib/std/crypto/sha3.zig | 6 ++++++ 12 files changed, 78 insertions(+) diff --git a/lib/std/crypto/aegis.zig b/lib/std/crypto/aegis.zig index 82fb96b34804f77f4679df8546a27508bcd63358..488e89438f4906572a4941d402a5f21f8d88df8c 100644 --- a/lib/std/crypto/aegis.zig +++ b/lib/std/crypto/aegis.zig @@ -794,6 +794,12 @@ fn AegisMac(comptime T: type) type { out.* = self.state.finalizeMac(T.tag_length * 8, self.msg_len); } + pub fn finalResult(d: *Mac) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } + /// Return an authentication tag for a message, a key and a nonce pub fn createWithNonce(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8, nonce: *const [nonce_length]u8) void { var ctx = Mac.initWithNonce(key, nonce); diff --git a/lib/std/crypto/ascon.zig b/lib/std/crypto/ascon.zig index 9d2ac52e83dc500062c93aa4af56cd9b50f11f03..b16094f902b0dec89027d9d7158263fc0ed94ba1 100644 --- a/lib/std/crypto/ascon.zig +++ b/lib/std/crypto/ascon.zig @@ -673,6 +673,12 @@ pub const AsconHash256 = struct { mem.writeInt(u64, out[i * 8 ..][0..8], h[i], .little); } } + + pub fn finalResult(d: *AsconHash256) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } }; /// Ascon-XOF128 as specified in NIST SP 800-232 Section 5 diff --git a/lib/std/crypto/bcrypt.zig b/lib/std/crypto/bcrypt.zig index 6fa5f984734492c259b303f54960afdf5fbc131c..2ebf9319e61b2aab7d97a44ae7992231355de24d 100644 --- a/lib/std/crypto/bcrypt.zig +++ b/lib/std/crypto/bcrypt.zig @@ -514,6 +514,12 @@ const pbkdf_prf = struct { out.* = hash(self.sha2pass, sha2salt); } + pub fn finalResult(d: *Self) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } + /// Matches OpenBSD function /// https://github.com/openbsd/src/blob/6df1256b7792691e66c2ed9d86a8c103069f9e34/lib/libutil/bcrypt_pbkdf.c#L98 pub fn hash(sha2pass: [Sha512.digest_length]u8, sha2salt: [Sha512.digest_length]u8) [32]u8 { diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index efa948810947ed2bc99d4178ebbf12bbf4c3a7db..588552c288d689d12a123e1eb34cf20c6a67a6d6 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -138,6 +138,12 @@ pub fn Blake2s(comptime out_bits: usize) type { out.* = @as(*[digest_length]u8, @ptrCast(&d.h)).*; } + pub fn finalResult(d: *Self) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } + fn round(d: *Self, b: *const [64]u8, last: bool) void { var m: [16]u32 = undefined; var v: [16]u32 = undefined; @@ -580,6 +586,12 @@ pub fn Blake2b(comptime out_bits: usize) type { out.* = @as(*[digest_length]u8, @ptrCast(&d.h)).*; } + pub fn finalResult(d: *Self) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } + fn round(d: *Self, b: *const [128]u8, last: bool) void { var m: [16]u64 = undefined; var v: [16]u64 = undefined; diff --git a/lib/std/crypto/cbc_mac.zig b/lib/std/crypto/cbc_mac.zig index d65c3a990b0c0eb06762c055a0c631096ee3dadf..db4ef5739d6385d9a961ce383e5e496eedc935cc 100644 --- a/lib/std/crypto/cbc_mac.zig +++ b/lib/std/crypto/cbc_mac.zig @@ -68,6 +68,12 @@ pub fn CbcMac(comptime BlockCipher: type) type { // Partial blocks are implicitly zero-padded: buf[pos..] contains zeros from initialization. self.cipher_ctx.encrypt(out, &self.buf); } + + pub fn finalResult(d: *Self) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } }; } diff --git a/lib/std/crypto/cmac.zig b/lib/std/crypto/cmac.zig index 4f3858b50ee36e40d93a8103c4dbfe6527bd64c2..9440b93631e0ca7bf46702cd9d60088cda657a81 100644 --- a/lib/std/crypto/cmac.zig +++ b/lib/std/crypto/cmac.zig @@ -73,6 +73,12 @@ pub fn Cmac(comptime BlockCipher: type) type { self.cipher_ctx.encrypt(out, &mac); } + pub fn finalResult(d: *Self) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } + fn double(l: Block) Block { const Int = @Int(.unsigned, block_length * 8); const l_ = mem.readInt(Int, &l, .big); diff --git a/lib/std/crypto/ghash_polyval.zig b/lib/std/crypto/ghash_polyval.zig index 2ae68da1d654ee0a1ff33390637c5240a3f26aae..666ef543cda11eb7d724ade688a995eb5c96ee42 100644 --- a/lib/std/crypto/ghash_polyval.zig +++ b/lib/std/crypto/ghash_polyval.zig @@ -405,6 +405,12 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { std.crypto.secureZero(Self, st[0..1]); } + pub fn finalResult(d: *Self) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } + /// Compute the GHASH of a message. pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void { var st = Self.init(key); diff --git a/lib/std/crypto/hash_composition.zig b/lib/std/crypto/hash_composition.zig index 1ffa3d4c47f5f501a859d8de519438ee5ae8287f..db52f4c617c74f34046e323ffe1cb01bde0c4cd9 100644 --- a/lib/std/crypto/hash_composition.zig +++ b/lib/std/crypto/hash_composition.zig @@ -54,6 +54,12 @@ pub fn Composition(comptime H1: type, comptime H2: type) type { d.H1.update(&H2_digest); d.H1.final(out); } + + pub fn finalResult(d: *Self) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } }; } diff --git a/lib/std/crypto/hmac.zig b/lib/std/crypto/hmac.zig index b87d65edb3c476ab4e591885da89fbb2b88ef35d..ca96467190feafe25a5c634d7c39b25da9731332 100644 --- a/lib/std/crypto/hmac.zig +++ b/lib/std/crypto/hmac.zig @@ -71,6 +71,12 @@ pub fn Hmac(comptime Hash: type) type { ohash.update(&scratch); ohash.final(out); } + + pub fn finalResult(d: *Self) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } }; } diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig index 17eccc658585c9675a683133362edb583a14763c..15c88c210337254053d94aa0b44b6ce53ec01186 100644 --- a/lib/std/crypto/md5.zig +++ b/lib/std/crypto/md5.zig @@ -124,6 +124,12 @@ pub const Md5 = struct { } } + pub fn finalResult(d: *Self) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } + fn round(d: *Self, b: *const [64]u8) void { var s: [16]u32 = undefined; diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index 3d6e4d89c33b785dfbeda0fc2fd03a12cf88be2b..db63827723b97bef74b5188c1b0e8dc793ddee9d 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -187,6 +187,12 @@ pub const Poly1305 = struct { std.crypto.secureZero(Poly1305, st[0..1]); } + pub fn finalResult(d: *Poly1305) [mac_length]u8 { + var result: [mac_length]u8 = undefined; + d.final(&result); + return result; + } + pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void { var st = Poly1305.init(key); st.update(msg); diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig index 6fe661981ab71fa49906f5998400aa5e1250d48c..f194f62f4b02f543b7f14f280d974f4368d5490a 100644 --- a/lib/std/crypto/sha3.zig +++ b/lib/std/crypto/sha3.zig @@ -86,6 +86,12 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime default_delim self.st.pad(); self.st.squeeze(out[0..]); } + + pub fn finalResult(d: *Self) [digest_length]u8 { + var result: [digest_length]u8 = undefined; + d.final(&result); + return result; + } }; } -- 2.54.0