Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix re-initialization of hash objects on CNG.
In .NET 6, we added "real" support for Initialize on hash objects.
However, the Reset call would create a new hash object without the CNG
"resuable" flag. This led to the HashProvider's "_reusable" field and
the actual reusability of the hash instance to disagree.
  • Loading branch information
vcsjones authored and github-actions committed Dec 4, 2021
commit 1d2c30d809d49a2b10e1c62e81eb527b769728a2
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ public override void Reset()
{
if (_reusable && !_running)
return;

DestroyHash();

BCryptCreateHashFlags flags = _reusable ?
BCryptCreateHashFlags.BCRYPT_HASH_REUSABLE_FLAG :
BCryptCreateHashFlags.None;

SafeBCryptHashHandle hHash;
NTSTATUS ntStatus = Interop.BCrypt.BCryptCreateHash(_hAlgorithm, out hHash, IntPtr.Zero, 0, _key, _key == null ? 0 : _key.Length, BCryptCreateHashFlags.None);
NTSTATUS ntStatus = Interop.BCrypt.BCryptCreateHash(_hAlgorithm, out hHash, IntPtr.Zero, 0, _key, _key == null ? 0 : _key.Length, flags);
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
throw Interop.BCrypt.CreateCryptographicException(ntStatus);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,30 @@ public void Initialize_TransformBlock_Unused()
}
}

[Fact]
public void Initialize_DoubleInitialize_Works()
{
byte[] hashInput = new byte[] { 1, 2, 3, 4, 5 };
byte[] expectedDigest;

using (HashAlgorithm hash = Create())
{
expectedDigest = hash.ComputeHash(hashInput);
}

using (HashAlgorithm hash = Create())
{
byte[] buffer = new byte[1024];
hash.TransformBlock(buffer, 0, buffer.Length, buffer, 0);
hash.Initialize();
hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
hash.Initialize();
hash.TransformFinalBlock (hashInput, 0, hashInput.Length);

Assert.Equal(expectedDigest, hash.Hash);
}
}

protected class DataRepeatingStream : Stream
{
private int _remaining;
Expand Down