Skip to content

Commit

Permalink
Fix HMAC usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Dec 13, 2021
1 parent b861f56 commit 6fef1f2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion source/sha3d.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public struct KECCAK(uint digestSize, uint shake = 0)

@safe @nogc pure nothrow:

enum blockSize = (1600 - digestSize * 2); /// Digest size in bits

// ...0: Reserved
// 01: SHA-3
// ...11: RawSHAKE
// 1111: SHAKE
private enum delim = shake ? 0x1f : 0x06; /// Delimiter suffix when finishing
private enum rate = (1600 - digestSize * 2) / 8; /// Sponge rate in bytes
private enum rate = blockSize / 8; /// Sponge rate in bytes
private enum stateSize = 200;
private enum stateSt64Size = stateSize / 8;
private enum stateStzSize = stateSize / size_t.sizeof;
Expand Down Expand Up @@ -297,6 +299,8 @@ private:
}
}

// Unittest based on https://www.di-mgt.com.au/sha_testvectors.html

/// Test against empty datasets
@safe unittest
{
Expand Down Expand Up @@ -596,6 +600,21 @@ public alias SHAKE256Digest = WrapperDigest!SHAKE256;
hexString!("46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f"));
}

/// Testing with HMAC
@system unittest
{
import std.ascii : LetterCase;
import std.string : representation;
import std.digest.hmac : hmac;

auto secret = "secret".representation;
assert("The quick brown fox jumps over the lazy dog"
.representation
.hmac!SHA3_256(secret)
.toHexString!(LetterCase.lower) ==
"93379fab68fae6d0fde0c816ea8a49fbd3c80f136c6af08bc61df5268d01b4d8");
}

/// Testing out various SHAKE XOFs.
@system unittest
{
Expand Down

0 comments on commit 6fef1f2

Please sign in to comment.