Skip to content

Commit

Permalink
HmacSha512.MinKeySize value should be 32 not 64 bytes
Browse files Browse the repository at this point in the history
Resolves #82
  • Loading branch information
ektrah committed Sep 8, 2024
1 parent a46b0ea commit ab6484d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Cryptography/HmacSha256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace NSec.Cryptography
//
public sealed class HmacSha256 : MacAlgorithm
{
public static readonly int MinKeySize = crypto_hash_sha256_BYTES;
public static readonly int MinKeySize = crypto_auth_hmacsha256_KEYBYTES;
public static readonly int MaxKeySize = crypto_hash_sha256_BYTES;
public static readonly int MinMacSize = 16;
public static readonly int MaxMacSize = crypto_auth_hmacsha256_BYTES;
Expand Down Expand Up @@ -100,7 +100,7 @@ internal override void InitializeCore(
SecureMemoryHandle keyHandle,
out IncrementalMacState state)
{
Debug.Assert(keyHandle.Size == crypto_hash_sha256_BYTES);
Debug.Assert(keyHandle.Size <= crypto_hash_sha256_BYTES);

int error = crypto_auth_hmacsha256_init(
ref state.hmacsha256,
Expand Down Expand Up @@ -157,7 +157,7 @@ private protected override void MacCore(
ReadOnlySpan<byte> data,
Span<byte> mac)
{
Debug.Assert(keyHandle.Size == crypto_hash_sha256_BYTES);
Debug.Assert(keyHandle.Size <= crypto_hash_sha256_BYTES);
Debug.Assert(mac.Length <= crypto_auth_hmacsha256_BYTES);

Span<byte> temp = stackalloc byte[crypto_auth_hmacsha256_BYTES];
Expand Down
6 changes: 3 additions & 3 deletions src/Cryptography/HmacSha512.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace NSec.Cryptography
//
public sealed class HmacSha512 : MacAlgorithm
{
public static readonly int MinKeySize = crypto_hash_sha512_BYTES;
public static readonly int MinKeySize = crypto_auth_hmacsha512_KEYBYTES;
public static readonly int MaxKeySize = crypto_hash_sha512_BYTES;
public static readonly int MinMacSize = 16;
public static readonly int MaxMacSize = crypto_auth_hmacsha512_BYTES;
Expand Down Expand Up @@ -103,7 +103,7 @@ internal override void InitializeCore(
SecureMemoryHandle keyHandle,
out IncrementalMacState state)
{
Debug.Assert(keyHandle.Size == crypto_hash_sha512_BYTES);
Debug.Assert(keyHandle.Size <= crypto_hash_sha512_BYTES);

int error = crypto_auth_hmacsha512_init(
ref state.hmacsha512,
Expand Down Expand Up @@ -160,7 +160,7 @@ private protected override void MacCore(
ReadOnlySpan<byte> data,
Span<byte> mac)
{
Debug.Assert(keyHandle.Size == crypto_hash_sha512_BYTES);
Debug.Assert(keyHandle.Size <= crypto_hash_sha512_BYTES);
Debug.Assert(mac.Length <= crypto_auth_hmacsha512_BYTES);

Span<byte> temp = stackalloc byte[crypto_auth_hmacsha512_BYTES];
Expand Down
2 changes: 1 addition & 1 deletion tests/Algorithms/HmacSha512Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void Properties()
{
var a = new HmacSha512();

Assert.Equal(64, HmacSha512.MinKeySize);
Assert.Equal(32, HmacSha512.MinKeySize);
Assert.Equal(64, HmacSha512.MaxKeySize);
Assert.Equal(16, HmacSha512.MinMacSize);
Assert.Equal(64, HmacSha512.MaxMacSize);
Expand Down
1 change: 1 addition & 0 deletions tests/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal static class Registry
MacAlgorithm.HmacSha256_128,
MacAlgorithm.HmacSha512,
MacAlgorithm.HmacSha512_256,
new HmacSha512(32, 32),
};

public static readonly TheoryData<StreamCipherAlgorithm> StreamCipherAlgorithms = new()
Expand Down

0 comments on commit ab6484d

Please sign in to comment.