authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-26 13:23:16+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-29 15:18:37-04:00
log26793453a7cf2016ae6fa7ad3d649dca150b53f5
treecb174c663085e6908335350622e39de1bd4f181d
parent32e65c3f96b5418acf4d444714facbba98790a91

std/crypto/blake2b: allow the initial output length to be set

BLAKE2 includes the expected output length in the initial state. This length is actually distinct from the actual output length used at finalization. BLAKE2b-256/128 is thus not the same as BLAKE2b-128. This behavior can be a little bit surprising, and has been "fixed" in BLAKE3. In order to support this, we may want to provide an option to set the length used for domain separation. In Zig, there is another reason to allow this: we assume that the output length is defined at comptime. But BLAKE2 doesn't have a fixed output length. For an output length that is not known at comptime, we can't take the full block size and truncate it due to the reason above. What we can do now is set that length as an option to get the correct initial state, and truncate the output if necessary.

1 files changed, 4 insertions(+), 4 deletions(-)

lib/std/crypto/blake2.zig+4-4
...@@ -44,7 +44,7 @@ pub fn Blake2s(comptime out_bits: usize) type {...@@ -44,7 +44,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
44 pub const key_length_min = 0;44 pub const key_length_min = 0;
45 pub const key_length_max = 32;45 pub const key_length_max = 32;
46 pub const key_length = 32; // recommended key length46 pub const key_length = 32; // recommended key length
47 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null };47 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null, expected_out_bits: usize = out_bits };
4848
49 const iv = [8]u32{49 const iv = [8]u32{
50 0x6A09E667,50 0x6A09E667,
...@@ -84,7 +84,7 @@ pub fn Blake2s(comptime out_bits: usize) type {...@@ -84,7 +84,7 @@ pub fn Blake2s(comptime out_bits: usize) type {
8484
85 const key_len = if (options.key) |key| key.len else 0;85 const key_len = if (options.key) |key| key.len else 0;
86 // default parameters86 // default parameters
87 d.h[0] ^= 0x01010000 ^ @truncate(u32, key_len << 8) ^ @intCast(u32, out_bits >> 3);87 d.h[0] ^= 0x01010000 ^ @truncate(u32, key_len << 8) ^ @intCast(u32, options.expected_out_bits >> 3);
88 d.t = 0;88 d.t = 0;
89 d.buf_len = 0;89 d.buf_len = 0;
9090
...@@ -385,7 +385,7 @@ pub fn Blake2b(comptime out_bits: usize) type {...@@ -385,7 +385,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
385 pub const key_length_min = 0;385 pub const key_length_min = 0;
386 pub const key_length_max = 64;386 pub const key_length_max = 64;
387 pub const key_length = 32; // recommended key length387 pub const key_length = 32; // recommended key length
388 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null };388 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null, expected_out_bits: usize = out_bits };
389389
390 const iv = [8]u64{390 const iv = [8]u64{
391 0x6a09e667f3bcc908,391 0x6a09e667f3bcc908,
...@@ -427,7 +427,7 @@ pub fn Blake2b(comptime out_bits: usize) type {...@@ -427,7 +427,7 @@ pub fn Blake2b(comptime out_bits: usize) type {
427427
428 const key_len = if (options.key) |key| key.len else 0;428 const key_len = if (options.key) |key| key.len else 0;
429 // default parameters429 // default parameters
430 d.h[0] ^= 0x01010000 ^ (key_len << 8) ^ (out_bits >> 3);430 d.h[0] ^= 0x01010000 ^ (key_len << 8) ^ (options.expected_out_bits >> 3);
431 d.t = 0;431 d.t = 0;
432 d.buf_len = 0;432 d.buf_len = 0;
433433