| ... | @@ -18,6 +18,20 @@ pub const Keccak_512 = @compileError("Deprecated: use `Keccak512` instead"); | ... | @@ -18,6 +18,20 @@ pub const Keccak_512 = @compileError("Deprecated: use `Keccak512` instead"); |
| 18 | pub const Shake128 = Shake(128); | 18 | pub const Shake128 = Shake(128); |
| 19 | pub const Shake256 = Shake(256); | 19 | pub const Shake256 = Shake(256); |
| 20 | | 20 | |
| | 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. |
| | 23 | /// The delimiter is 0x01 by default, but can be changed for context-separation. |
| | 24 | pub fn TurboShake128(comptime delim: ?u8) type { |
| | 25 | return TurboShake(128, delim); |
| | 26 | } |
| | 27 | |
| | 28 | /// 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 | /// The delimiter is 0x01 by default, but can be changed for context-separation. |
| | 31 | pub fn TurboShake256(comptime delim: ?u8) type { |
| | 32 | return TurboShake(256, delim); |
| | 33 | } |
| | 34 | |
| 21 | /// A generic Keccak hash function. | 35 | /// A generic Keccak hash function. |
| 22 | pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime delim: u8, comptime rounds: u5) type { | 36 | pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime delim: u8, comptime rounds: u5) type { |
| 23 | comptime assert(output_bits > 0 and output_bits * 2 < f and output_bits % 8 == 0); // invalid output length | 37 | comptime assert(output_bits > 0 and output_bits * 2 < f and output_bits % 8 == 0); // invalid output length |
| ... | @@ -76,9 +90,18 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime delim: u8, co | ... | @@ -76,9 +90,18 @@ pub fn Keccak(comptime f: u11, comptime output_bits: u11, comptime delim: u8, co |
| 76 | | 90 | |
| 77 | /// The SHAKE extendable output hash function. | 91 | /// The SHAKE extendable output hash function. |
| 78 | pub fn Shake(comptime security_level: u11) type { | 92 | pub fn Shake(comptime security_level: u11) type { |
| | 93 | return ShakeLike(security_level, 0x1f, 24); |
| | 94 | } |
| | 95 | |
| | 96 | /// The TurboSHAKE extendable output hash function. |
| | 97 | /// https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/ |
| | 98 | pub fn TurboShake(comptime security_level: u11, comptime delim: ?u8) type { |
| | 99 | return ShakeLike(security_level, delim orelse 0x01, 12); |
| | 100 | } |
| | 101 | |
| | 102 | fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: u5) type { |
| 79 | const f = 1600; | 103 | const f = 1600; |
| 80 | const rounds = 24; | 104 | const State = KeccakState(f, security_level * 2, delim, rounds); |
| 81 | const State = KeccakState(f, security_level * 2, 0x1f, rounds); | | |
| 82 | | 105 | |
| 83 | return struct { | 106 | return struct { |
| 84 | const Self = @This(); | 107 | const Self = @This(); |
| ... | @@ -348,3 +371,9 @@ test "SHAKE-256 single" { | ... | @@ -348,3 +371,9 @@ test "SHAKE-256 single" { |
| 348 | Shake256.hash("hello123", &out, .{}); | 371 | Shake256.hash("hello123", &out, .{}); |
| 349 | try htest.assertEqual("ade612ba265f92de4a37", &out); | 372 | try htest.assertEqual("ade612ba265f92de4a37", &out); |
| 350 | } | 373 | } |
| | 374 | |
| | 375 | test "TurboSHAKE-128" { |
| | 376 | var out: [32]u8 = undefined; |
| | 377 | TurboShake(128, 0x06).hash("\xff", &out, .{}); |
| | 378 | try htest.assertEqual("8ec9c66465ed0d4a6c35d13506718d687a25cb05c74cca1e42501abd83874a67", &out); |
| | 379 | } |