Skip to content

Commit

Permalink
Add public constants for HMAC and hash sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Jan 13, 2022
1 parent c912a15 commit 79e40e3
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ namespace System.Security.Cryptography
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class MD5CryptoServiceProvider : MD5
{
private const int HashSizeBits = 128;
private readonly IncrementalHash _incrementalHash;
private bool _running;

public MD5CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.MD5);
HashSizeValue = HashSizeBits;
HashSizeValue = HashSizeInBits;
}

public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeBits / 8];
Span<byte> destination = stackalloc byte[HashSizeInBytes];

if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace System.Security.Cryptography
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SHA1CryptoServiceProvider : SHA1
{
private const int HashSizeBits = 160;
private readonly IncrementalHash _incrementalHash;
private bool _running;

public SHA1CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
HashSizeValue = HashSizeBits;
HashSizeValue = HashSizeInBits;
}

public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeBits / 8];
Span<byte> destination = stackalloc byte[HashSizeInBytes];

if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace System.Security.Cryptography
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SHA256CryptoServiceProvider : SHA256
{
private const int HashSizeBits = 256;
private readonly IncrementalHash _incrementalHash;
private bool _running;

public SHA256CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
HashSizeValue = HashSizeBits;
HashSizeValue = HashSizeInBits;
}

public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeBits / 8];
Span<byte> destination = stackalloc byte[HashSizeInBytes];

if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace System.Security.Cryptography
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SHA384CryptoServiceProvider : SHA384
{
private const int HashSizeBits = 384;
private readonly IncrementalHash _incrementalHash;
private bool _running;

public SHA384CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA384);
HashSizeValue = HashSizeBits;
HashSizeValue = HashSizeInBits;
}

public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeBits / 8];
Span<byte> destination = stackalloc byte[HashSizeInBytes];

if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace System.Security.Cryptography
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SHA512CryptoServiceProvider : SHA512
{
private const int HashSizeBits = 512;
private readonly IncrementalHash _incrementalHash;
private bool _running;

public SHA512CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA512);
HashSizeValue = HashSizeBits;
HashSizeValue = HashSizeInBits;
}

public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeBits / 8];
Span<byte> destination = stackalloc byte[HashSizeInBytes];

if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private static string GetCrlFileName(SafeX509Handle cert, string crlUrl)
}

uint persistentHash = unchecked((uint)persistentHashLong);
Span<byte> hash = stackalloc byte[256 >> 3];
Span<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];

// Endianness isn't important, it just needs to be consistent.
// (Even if the same storage was used for two different endianness systems it'd stabilize at two files).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,9 @@ private static unsafe byte[] MacAndEncode(
ReadOnlyMemory<byte> encodedAuthSafe,
ReadOnlySpan<char> passwordSpan)
{
const int MacSize = 160 / 8; // HMAC-SHA1 is 160 bits.
Span<byte> macKey = stackalloc byte[MacSize];
Span<byte> macSalt = stackalloc byte[MacSize];
Span<byte> macSpan = stackalloc byte[MacSize];
Span<byte> macKey = stackalloc byte[HMACSHA1.HashSizeInBytes];
Span<byte> macSalt = stackalloc byte[HMACSHA1.HashSizeInBytes];
Span<byte> macSpan = stackalloc byte[HMACSHA1.HashSizeInBytes];
RandomNumberGenerator.Fill(macSalt);

Pkcs12Kdf.DeriveMacKey(
Expand All @@ -496,9 +495,9 @@ private static unsafe byte[] MacAndEncode(

int bytesWritten = HMACSHA1.HashData(macKey, encodedAuthSafe.Span, macSpan);

if (bytesWritten != MacSize)
if (bytesWritten != HMACSHA1.HashSizeInBytes)
{
Debug.Fail($"HMACSHA1.HashData wrote {bytesWritten} of {MacSize} bytes");
Debug.Fail($"HMACSHA1.HashData wrote {bytesWritten} of {HMACSHA1.HashSizeInBytes} bytes");
throw new CryptographicException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ public override void Initialize() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACMD5 : System.Security.Cryptography.HMAC
{
public const int HashSizeInBits = 128;
public const int HashSizeInBytes = 16;
public HMACMD5() { }
public HMACMD5(byte[] key) { }
public override byte[] Key { get { throw null; } set { } }
Expand All @@ -689,6 +691,8 @@ public override void Initialize() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA1 : System.Security.Cryptography.HMAC
{
public const int HashSizeInBits = 160;
public const int HashSizeInBytes = 20;
public HMACSHA1() { }
public HMACSHA1(byte[] key) { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
Expand All @@ -709,6 +713,8 @@ public override void Initialize() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA256 : System.Security.Cryptography.HMAC
{
public const int HashSizeInBits = 256;
public const int HashSizeInBytes = 32;
public HMACSHA256() { }
public HMACSHA256(byte[] key) { }
public override byte[] Key { get { throw null; } set { } }
Expand All @@ -726,6 +732,8 @@ public override void Initialize() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA384 : System.Security.Cryptography.HMAC
{
public const int HashSizeInBits = 384;
public const int HashSizeInBytes = 48;
public HMACSHA384() { }
public HMACSHA384(byte[] key) { }
public override byte[] Key { get { throw null; } set { } }
Expand All @@ -745,6 +753,8 @@ public override void Initialize() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA512 : System.Security.Cryptography.HMAC
{
public const int HashSizeInBits = 512;
public const int HashSizeInBytes = 64;
public HMACSHA512() { }
public HMACSHA512(byte[] key) { }
public override byte[] Key { get { throw null; } set { } }
Expand Down Expand Up @@ -817,6 +827,8 @@ protected MaskGenerationMethod() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public abstract partial class MD5 : System.Security.Cryptography.HashAlgorithm
{
public const int HashSizeInBits = 128;
public const int HashSizeInBytes = 16;
protected MD5() { }
public static new System.Security.Cryptography.MD5 Create() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
Expand Down Expand Up @@ -1178,6 +1190,8 @@ public enum RSASignaturePaddingMode
}
public abstract partial class SHA1 : System.Security.Cryptography.HashAlgorithm
{
public const int HashSizeInBits = 160;
public const int HashSizeInBytes = 20;
protected SHA1() { }
public static new System.Security.Cryptography.SHA1 Create() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
Expand All @@ -1201,6 +1215,8 @@ public sealed override void Initialize() { }
}
public abstract partial class SHA256 : System.Security.Cryptography.HashAlgorithm
{
public const int HashSizeInBits = 256;
public const int HashSizeInBytes = 32;
protected SHA256() { }
public static new System.Security.Cryptography.SHA256 Create() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
Expand All @@ -1224,6 +1240,8 @@ public sealed override void Initialize() { }
}
public abstract partial class SHA384 : System.Security.Cryptography.HashAlgorithm
{
public const int HashSizeInBits = 384;
public const int HashSizeInBytes = 48;
protected SHA384() { }
public static new System.Security.Cryptography.SHA384 Create() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
Expand All @@ -1247,6 +1265,8 @@ public sealed override void Initialize() { }
}
public abstract partial class SHA512 : System.Security.Cryptography.HashAlgorithm
{
public const int HashSizeInBits = 512;
public const int HashSizeInBytes = 64;
protected SHA512() { }
public static new System.Security.Cryptography.SHA512 Create() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ namespace System.Security.Cryptography
[UnsupportedOSPlatform("browser")]
public class HMACMD5 : HMAC
{
private const int HmacSizeBits = 128;
private const int HmacSizeBytes = HmacSizeBits / 8;
/// <summary>
/// The hash size produced by the HMAC MD5 algorithm, in bits.
/// </summary>
public const int HashSizeInBits = 128;

/// <summary>
/// The hash size produced by the HMAC MD5 algorithm, in bytes.
/// </summary>
public const int HashSizeInBytes = HashSizeInBits / 8;

public HMACMD5()
: this(RandomNumberGenerator.GetBytes(BlockSize))
Expand All @@ -37,7 +44,7 @@ public HMACMD5(byte[] key)
// we just want to be explicit in all HMAC extended classes
BlockSizeValue = BlockSize;
HashSizeValue = _hMacCommon.HashSizeInBits;
Debug.Assert(HashSizeValue == HmacSizeBits);
Debug.Assert(HashSizeValue == HashSizeInBits);
}

public override byte[] Key
Expand Down Expand Up @@ -99,7 +106,7 @@ public static byte[] HashData(byte[] key, byte[] source)
/// <returns>The HMAC of the data.</returns>
public static byte[] HashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source)
{
byte[] buffer = new byte[HmacSizeBytes];
byte[] buffer = new byte[HashSizeInBytes];

int written = HashData(key, source, buffer.AsSpan());
Debug.Assert(written == buffer.Length);
Expand Down Expand Up @@ -143,14 +150,14 @@ public static int HashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source, Sp
/// </returns>
public static bool TryHashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
{
if (destination.Length < HmacSizeBytes)
if (destination.Length < HashSizeInBytes)
{
bytesWritten = 0;
return false;
}

bytesWritten = HashProviderDispenser.OneShotHashProvider.MacData(HashAlgorithmNames.MD5, key, source, destination);
Debug.Assert(bytesWritten == HmacSizeBytes);
Debug.Assert(bytesWritten == HashSizeInBytes);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ namespace System.Security.Cryptography
[UnsupportedOSPlatform("browser")]
public class HMACSHA1 : HMAC
{
private const int HmacSizeBits = 160;
private const int HmacSizeBytes = HmacSizeBits / 8;
/// <summary>
/// The hash size produced by the HMAC SHA1 algorithm, in bits.
/// </summary>
public const int HashSizeInBits = 160;

/// <summary>
/// The hash size produced by the HMAC SHA1 algorithm, in bytes.
/// </summary>
public const int HashSizeInBytes = HashSizeInBits / 8;

public HMACSHA1()
: this(RandomNumberGenerator.GetBytes(BlockSize))
Expand All @@ -39,7 +46,7 @@ public HMACSHA1(byte[] key)
// we just want to be explicit in all HMAC extended classes
BlockSizeValue = BlockSize;
HashSizeValue = _hMacCommon.HashSizeInBits;
Debug.Assert(HashSizeValue == HmacSizeBits);
Debug.Assert(HashSizeValue == HashSizeInBits);
}

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -108,7 +115,7 @@ public static byte[] HashData(byte[] key, byte[] source)
/// <returns>The HMAC of the data.</returns>
public static byte[] HashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source)
{
byte[] buffer = new byte[HmacSizeBytes];
byte[] buffer = new byte[HashSizeInBytes];

int written = HashData(key, source, buffer.AsSpan());
Debug.Assert(written == buffer.Length);
Expand Down Expand Up @@ -152,14 +159,14 @@ public static int HashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source, Sp
/// </returns>
public static bool TryHashData(ReadOnlySpan<byte> key, ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
{
if (destination.Length < HmacSizeBytes)
if (destination.Length < HashSizeInBytes)
{
bytesWritten = 0;
return false;
}

bytesWritten = HashProviderDispenser.OneShotHashProvider.MacData(HashAlgorithmNames.SHA1, key, source, destination);
Debug.Assert(bytesWritten == HmacSizeBytes);
Debug.Assert(bytesWritten == HashSizeInBytes);

return true;
}
Expand Down
Loading

0 comments on commit 79e40e3

Please sign in to comment.