Skip to content

Commit

Permalink
Merge pull request #62403 from dotnet/backport/pr-62397-to-release/6.0
Browse files Browse the repository at this point in the history
[release/6.0] Fix re-initialization of hash objects on CNG.
  • Loading branch information
safern committed Dec 15, 2021
2 parents 3b394a2 + 71af4f5 commit de46fa4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit de46fa4

Please sign in to comment.