internal CryptoApiHashProvider(int providerType, int calgHash) { SafeProvHandle hProv; if (!Interop.Advapi32.CryptAcquireContext(out hProv, null, null, providerType, (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT)) { int hr = Marshal.GetHRForLastWin32Error(); throw new CryptographicException(hr); } SafeHashHandle hHash; if (!Interop.Advapi32.CryptCreateHash(hProv, calgHash, SafeKeyHandle.InvalidHandle, (int)Interop.Advapi32.CryptCreateHashFlags.None, out hHash)) { int hr = Marshal.GetHRForLastWin32Error(); throw new CryptographicException(hr); } int dwHashSize = 0; int cbHashSize = sizeof(int); if (!Interop.Advapi32.CryptGetHashParam(hHash, Interop.Advapi32.CryptHashProperty.HP_HASHSIZE, out dwHashSize, ref cbHashSize, 0)) { int hr = Marshal.GetHRForLastWin32Error(); throw new CryptographicException(hr); } if (dwHashSize < 0) { throw new PlatformNotSupportedException( SR.Format( SR.Cryptography_UnknownHashAlgorithm, providerType, calgHash)); } HashSizeInBytes = dwHashSize; _calgHash = calgHash; _hHash = hHash; _hProv = hProv; _hHash.SetParent(_hProv); }
private void SetKey() { SafeProvHandle hProv; if (!Interop.Advapi32.CryptAcquireContext(out hProv, null, null, _providerType, (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT)) { int hr = Interop.CPError.GetHRForLastWin32Error(); throw new CryptographicException(hr); } int keyHashCalg; int keyCalg; GetCalgsFromHMAC(_calgHash, out keyCalg, out keyHashCalg); SafeKeyHandle hMacKey; //if key Length longer than sha512 output, we can't derive key just from hash, so need to use simpleblob. if (_key.Length > 64) { ImportLongKeyToCSP(hProv, out hMacKey); } else { ImportShortKeyToCSP(hProv, keyCalg, keyHashCalg, out hMacKey); } //Create Hash with imported Key SafeHashHandle hMacHash; if (keyCalg == GostConstants.CALG_GENERIC_SECRET) { if (!Interop.Advapi32.CryptCreateHash(hProv, GostConstants.CALG_HMAC, hMacKey, Interop.Advapi32.CryptCreateHashFlags.None, out hMacHash)) { int hr = Interop.CPError.GetHRForLastWin32Error(); throw new CryptographicException(hr); } var hmacInfo = new Interop.Advapi32.HMAC_INFO(); hmacInfo.HashAlgid = _calgHash; if (!Interop.Advapi32.CryptSetHashParam(hMacHash, Interop.Advapi32.CryptHashProperty.HP_HMAC_INFO, hmacInfo.ToByteArray(), 0)) { int hr = Interop.CPError.GetHRForLastWin32Error(); throw new CryptographicException(hr); } } else { if (!Interop.Advapi32.CryptCreateHash(hProv, _calgHash, hMacKey, Interop.Advapi32.CryptCreateHashFlags.None, out hMacHash)) { int hr = Interop.CPError.GetHRForLastWin32Error(); throw new CryptographicException(hr); } } _hProv = hProv; _hKey = hMacKey; _hHash = hMacHash; _hHash.SetParent(_hProv); _hKey.SetParent(_hProv); }
public override bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten) { int hashSize = HashSizeInBytes; if (!Interop.Advapi32.CryptGetHashParam(_hHash, Interop.Advapi32.CryptHashProperty.HP_HASHVAL, destination, ref hashSize, 0)) { int hr = Marshal.GetHRForLastWin32Error(); throw new CryptographicException(hr); } bytesWritten = hashSize; //reinitialize _hHash.Dispose(); if (!Interop.Advapi32.CryptCreateHash(_hProv, _calgHash, SafeKeyHandle.InvalidHandle, (int)Interop.Advapi32.CryptCreateHashFlags.None, out _hHash)) { int hr = Marshal.GetHRForLastWin32Error(); throw new CryptographicException(hr); } _hHash.SetParent(_hProv); return true; }