Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assert when using HashProviderCng concurrently #100989

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using BCryptCreateHashFlags = Interop.BCrypt.BCryptCreateHashFlags;
Expand Down Expand Up @@ -122,7 +123,9 @@ public sealed override void Dispose(bool disposing)
{
if (disposing)
{
DestroyHash();
// Not disposing of _hAlgorithm as we got this from a cache. So it's not ours to Dispose().
_hHash.Dispose();

if (_key != null)
{
byte[] key = _key;
Expand All @@ -134,13 +137,17 @@ public sealed override void Dispose(bool disposing)

public sealed override int HashSizeInBytes => _hashSize;

[MemberNotNull(nameof(_hHash))]
public override void Reset()
{
// Reset does not need to use ConcurrencyBlock. It either no-ops, or creates an entirely new handle, exchanges
// them, and disposes of the old handle. We don't need to block concurrency on the Dispose because SafeHandle
// does that.
if (_reusable && !_running)
{
Debug.Assert(_hHash is not null);
return;
}

BCryptCreateHashFlags flags = _reusable ?
BCryptCreateHashFlags.BCRYPT_HASH_REUSABLE_FLAG :
Expand All @@ -159,20 +166,8 @@ public override void Reset()
previousHash?.Dispose();
}

private void DestroyHash()
{
SafeBCryptHashHandle? hHash = _hHash;
if (hHash != null)
{
_hHash = null;
hHash.Dispose();
}

// Not disposing of _hAlgorithm as we got this from a cache. So it's not ours to Dispose().
}

private readonly SafeBCryptAlgorithmHandle _hAlgorithm;
private SafeBCryptHashHandle? _hHash;
private SafeBCryptHashHandle _hHash;
private byte[]? _key;
private readonly bool _reusable;

Expand Down
Loading