Commit 3022c525ec

e4m2 <git@e4m2.com>
2023-07-14 16:02:01
std.crypto.sha3: Minor TurboSHAKE/Keccak fixes (#16408)
1 parent 094cd92
Changed files (2)
lib
lib/std/crypto/keccak_p.zig
@@ -56,7 +56,7 @@ pub fn KeccakF(comptime f: u11) type {
         /// Byte-swap the entire state if the architecture doesn't match the required endianness.
         pub fn endianSwap(self: *Self) void {
             for (&self.st) |*w| {
-                w.* = mem.littleTooNative(T, w.*);
+                w.* = mem.littleToNative(T, w.*);
             }
         }
 
lib/std/crypto/sha3.zig
@@ -21,14 +21,15 @@ pub const Shake256 = Shake(256);
 /// TurboSHAKE128 is a XOF (a secure hash function with a variable output length), with a 128 bit security level.
 /// It is based on the same permutation as SHA3 and SHAKE128, but which much higher performance.
 /// The delimiter is 0x1f by default, but can be changed for context-separation.
-pub fn TurboShake128(comptime delim: ?u8) type {
+/// For a protocol that uses both KangarooTwelve and TurboSHAKE128, it is recommended to avoid using 0x06, 0x07 or 0x0b for the delimiter.
+pub fn TurboShake128(comptime delim: ?u7) type {
     return TurboShake(128, delim);
 }
 
 /// TurboSHAKE256 is a XOF (a secure hash function with a variable output length), with a 256 bit security level.
 /// It is based on the same permutation as SHA3 and SHAKE256, but which much higher performance.
-/// The delimiter is 0x01 by default, but can be changed for context-separation.
-pub fn TurboShake256(comptime delim: ?u8) type {
+/// The delimiter is 0x1f by default, but can be changed for context-separation.
+pub fn TurboShake256(comptime delim: ?u7) type {
     return TurboShake(256, delim);
 }
 
@@ -94,9 +95,14 @@ pub fn Shake(comptime security_level: u11) type {
 }
 
 /// The TurboSHAKE extendable output hash function.
-/// https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/
-pub fn TurboShake(comptime security_level: u11, comptime delim: ?u8) type {
-    return ShakeLike(security_level, delim orelse 0x1f, 12);
+/// It is based on the same permutation as SHA3 and SHAKE, but which much higher performance.
+/// The delimiter is 0x1f by default, but can be changed for context-separation.
+/// https://eprint.iacr.org/2023/342
+pub fn TurboShake(comptime security_level: u11, comptime delim: ?u7) type {
+    comptime assert(security_level <= 256);
+    const d = delim orelse 0x1f;
+    comptime assert(d >= 0x01); // delimiter must be >= 1
+    return ShakeLike(security_level, d, 12);
 }
 
 fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: u5) type {