authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2023-03-14 22:40:02+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-03-14 21:40:02+00:00
loge17998b39655e8af5d2a1134fee7ca4850ad4389
treec65510685e12b5b98f824fc888c3797eae3dba7c
parentd6e48abde87400a8a4851c7ab8c918005d81d058
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Argon2: properly handle outputs > 64 bytes in blake2Long() (#14914)

Fixes #14912

1 files changed, 29 insertions(+), 30 deletions(-)

lib/std/crypto/argon2.zig+29-30
...@@ -138,40 +138,39 @@ fn initHash(...@@ -138,40 +138,39 @@ fn initHash(
138}138}
139139
140fn blake2bLong(out: []u8, in: []const u8) void {140fn blake2bLong(out: []u8, in: []const u8) void {
141 var b2 = Blake2b512.init(.{ .expected_out_bits = math.min(512, out.len * 8) });141 const H = Blake2b512;
142142 var outlen_bytes: [4]u8 = undefined;
143 var buffer: [Blake2b512.digest_length]u8 = undefined;143 mem.writeIntLittle(u32, &outlen_bytes, @intCast(u32, out.len));
144 mem.writeIntLittle(u32, buffer[0..4], @intCast(u32, out.len));144
145 b2.update(buffer[0..4]);145 var out_buf: [H.digest_length]u8 = undefined;
146 b2.update(in);146
147 b2.final(&buffer);147 if (out.len <= H.digest_length) {
148148 var h = H.init(.{ .expected_out_bits = out.len * 8 });
149 if (out.len <= Blake2b512.digest_length) {149 h.update(&outlen_bytes);
150 mem.copy(u8, out, buffer[0..out.len]);150 h.update(in);
151 h.final(&out_buf);
152 mem.copy(u8, out, out_buf[0..out.len]);
151 return;153 return;
152 }154 }
153155
154 b2 = Blake2b512.init(.{});156 var h = H.init(.{});
155 mem.copy(u8, out, buffer[0..32]);157 h.update(&outlen_bytes);
156 var out_slice = out[32..];158 h.update(in);
157 while (out_slice.len > Blake2b512.digest_length) : ({159 h.final(&out_buf);
158 out_slice = out_slice[32..];160 var out_slice = out;
159 b2 = Blake2b512.init(.{});161 mem.copy(u8, out_slice, out_buf[0 .. H.digest_length / 2]);
160 }) {162 out_slice = out_slice[H.digest_length / 2 ..];
161 b2.update(&buffer);163
162 b2.final(&buffer);164 var in_buf: [H.digest_length]u8 = undefined;
163 mem.copy(u8, out_slice, buffer[0..32]);165 while (out_slice.len > H.digest_length) {
164 }166 mem.copy(u8, &in_buf, &out_buf);
165167 H.hash(&in_buf, &out_buf, .{});
166 var r = Blake2b512.digest_length;168 mem.copy(u8, out_slice, out_buf[0 .. H.digest_length / 2]);
167 if (out.len % Blake2b512.digest_length > 0) {169 out_slice = out_slice[H.digest_length / 2 ..];
168 r = ((out.len + 31) / 32) - 2;
169 b2 = Blake2b512.init(.{ .expected_out_bits = r * 8 });
170 }170 }
171171 mem.copy(u8, &in_buf, &out_buf);
172 b2.update(&buffer);172 H.hash(&in_buf, &out_buf, .{ .expected_out_bits = out_slice.len * 8 });
173 b2.final(&buffer);173 mem.copy(u8, out_slice, out_buf[0..out_slice.len]);
174 mem.copy(u8, out_slice, buffer[0..r]);
175}174}
176175
177fn initBlocks(176fn initBlocks(