authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2022-12-23 22:38:27+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-23 21:38:27+00:00
logc9e3524d0b12e11488519bb377e7dcf60047963a
tree5b6bfd9dca072cf3da4b9790499341d68f047d71
parent581d292381157464ae6f2c88c8a628314f005742
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

HKDF allow expansion up to, and including <hash size> * 255 bytes (#14051)

Fixes #14050

1 files changed, 3 insertions(+), 2 deletions(-)

lib/std/crypto/hkdf.zig+3-2
...@@ -22,7 +22,7 @@ pub fn Hkdf(comptime Hmac: type) type {...@@ -22,7 +22,7 @@ pub fn Hkdf(comptime Hmac: type) type {
2222
23 /// Derive a subkey from a master key `prk` and a subkey description `ctx`.23 /// Derive a subkey from a master key `prk` and a subkey description `ctx`.
24 pub fn expand(out: []u8, ctx: []const u8, prk: [Hmac.mac_length]u8) void {24 pub fn expand(out: []u8, ctx: []const u8, prk: [Hmac.mac_length]u8) void {
25 assert(out.len < Hmac.mac_length * 255); // output size is too large for the Hkdf construction25 assert(out.len <= Hmac.mac_length * 255); // output size is too large for the Hkdf construction
26 var i: usize = 0;26 var i: usize = 0;
27 var counter = [1]u8{1};27 var counter = [1]u8{1};
28 while (i + Hmac.mac_length <= out.len) : (i += Hmac.mac_length) {28 while (i + Hmac.mac_length <= out.len) : (i += Hmac.mac_length) {
...@@ -33,7 +33,8 @@ pub fn Hkdf(comptime Hmac: type) type {...@@ -33,7 +33,8 @@ pub fn Hkdf(comptime Hmac: type) type {
33 st.update(ctx);33 st.update(ctx);
34 st.update(&counter);34 st.update(&counter);
35 st.final(out[i..][0..Hmac.mac_length]);35 st.final(out[i..][0..Hmac.mac_length]);
36 counter[0] += 1;36 counter[0] +%= 1;
37 assert(counter[0] != 1);
37 }38 }
38 const left = out.len % Hmac.mac_length;39 const left = out.len % Hmac.mac_length;
39 if (left > 0) {40 if (left > 0) {