authorgravatar for git@e4m2.come4m2 <git@e4m2.com> 2023-07-14 16:02:01+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-14 14:02:01+00:00
log3022c525ec87c391842ec339916037e12b3a7b5c
tree5321df11e98d02a4f6d209678460e41161faa1d7
parent094cd92615f906116e68fe6a665122b58f66cd3b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.crypto.sha3: Minor TurboSHAKE/Keccak fixes (#16408)


2 files changed, 13 insertions(+), 7 deletions(-)

lib/std/crypto/keccak_p.zig+1-1
...@@ -56,7 +56,7 @@ pub fn KeccakF(comptime f: u11) type {...@@ -56,7 +56,7 @@ pub fn KeccakF(comptime f: u11) type {
56 /// Byte-swap the entire state if the architecture doesn't match the required endianness.56 /// Byte-swap the entire state if the architecture doesn't match the required endianness.
57 pub fn endianSwap(self: *Self) void {57 pub fn endianSwap(self: *Self) void {
58 for (&self.st) |*w| {58 for (&self.st) |*w| {
59 w.* = mem.littleTooNative(T, w.*);59 w.* = mem.littleToNative(T, w.*);
60 }60 }
61 }61 }
6262
lib/std/crypto/sha3.zig+12-6
...@@ -21,14 +21,15 @@ pub const Shake256 = Shake(256);...@@ -21,14 +21,15 @@ pub const Shake256 = Shake(256);
21/// TurboSHAKE128 is a XOF (a secure hash function with a variable output length), with a 128 bit security level.21/// TurboSHAKE128 is a XOF (a secure hash function with a variable output length), with a 128 bit security level.
22/// It is based on the same permutation as SHA3 and SHAKE128, but which much higher performance.22/// It is based on the same permutation as SHA3 and SHAKE128, but which much higher performance.
23/// The delimiter is 0x1f by default, but can be changed for context-separation.23/// The delimiter is 0x1f by default, but can be changed for context-separation.
24pub fn TurboShake128(comptime delim: ?u8) type {24/// For a protocol that uses both KangarooTwelve and TurboSHAKE128, it is recommended to avoid using 0x06, 0x07 or 0x0b for the delimiter.
25pub fn TurboShake128(comptime delim: ?u7) type {
25 return TurboShake(128, delim);26 return TurboShake(128, delim);
26}27}
2728
28/// TurboSHAKE256 is a XOF (a secure hash function with a variable output length), with a 256 bit security level.29/// TurboSHAKE256 is a XOF (a secure hash function with a variable output length), with a 256 bit security level.
29/// It is based on the same permutation as SHA3 and SHAKE256, but which much higher performance.30/// It is based on the same permutation as SHA3 and SHAKE256, but which much higher performance.
30/// The delimiter is 0x01 by default, but can be changed for context-separation.31/// The delimiter is 0x1f by default, but can be changed for context-separation.
31pub fn TurboShake256(comptime delim: ?u8) type {32pub fn TurboShake256(comptime delim: ?u7) type {
32 return TurboShake(256, delim);33 return TurboShake(256, delim);
33}34}
3435
...@@ -94,9 +95,14 @@ pub fn Shake(comptime security_level: u11) type {...@@ -94,9 +95,14 @@ pub fn Shake(comptime security_level: u11) type {
94}95}
9596
96/// The TurboSHAKE extendable output hash function.97/// The TurboSHAKE extendable output hash function.
97/// https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/98/// It is based on the same permutation as SHA3 and SHAKE, but which much higher performance.
98pub fn TurboShake(comptime security_level: u11, comptime delim: ?u8) type {99/// The delimiter is 0x1f by default, but can be changed for context-separation.
99 return ShakeLike(security_level, delim orelse 0x1f, 12);100/// https://eprint.iacr.org/2023/342
101pub fn TurboShake(comptime security_level: u11, comptime delim: ?u7) type {
102 comptime assert(security_level <= 256);
103 const d = delim orelse 0x1f;
104 comptime assert(d >= 0x01); // delimiter must be >= 1
105 return ShakeLike(security_level, d, 12);
100}106}
101107
102fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: u5) type {108fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: u5) type {