public static partial bool CryptDecrypt( SafeCapiKeyHandle hKey, SafeHashHandle hHash, bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen);
public static bool CryptGetHashParam( SafeHashHandle safeHashHandle, CryptHashProperty dwParam, Span <byte> pbData, [In, Out] ref int pdwDataLen, int dwFlags) { if (pbData.IsEmpty) { return(CryptGetHashParam(safeHashHandle, dwParam, IntPtr.Zero, ref pdwDataLen, 0)); } if (pdwDataLen > pbData.Length) { throw new IndexOutOfRangeException(); } unsafe { fixed(byte *bytePtr = &MemoryMarshal.GetReference(pbData)) { return(CryptGetHashParam(safeHashHandle, dwParam, (IntPtr)bytePtr, ref pdwDataLen, 0)); } } }
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); }
public static partial bool CryptSignHash( SafeHashHandle hHash, KeySpec dwKeySpec, string?szDescription, CryptSignAndVerifyHashFlags dwFlags, byte[]?pbSignature, ref int pdwSigLen);
public static extern bool CryptDecrypt( SafeKeyHandle hKey, SafeHashHandle hHash, bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen);
public static extern bool CryptSignHash( SafeHashHandle hHash, KeySpec dwKeySpec, string szDescription, CryptSignAndVerifyHashFlags dwFlags, [Out] byte[] pbSignature, [In, Out] ref int pdwSigLen);
public static partial bool CryptVerifySignature( SafeHashHandle hHash, byte[] pbSignature, int dwSigLen, SafeCapiKeyHandle hPubKey, string?szDescription, CryptSignAndVerifyHashFlags dwFlags);
[System.Security.SecuritySafeCritical] // auto-generated public override void Initialize() { if (_safeHashHandle != null && !_safeHashHandle.IsClosed) _safeHashHandle.Dispose(); // _CreateHash will check for failures and throw the appropriate exception _safeHashHandle = Utils.CreateHash(Utils.StaticProvHandle, Constants.CALG_SHA1); }
public static partial bool CryptDecrypt( SafeCapiKeyHandle hKey, SafeHashHandle hHash, [MarshalAs(UnmanagedType.Bool)] bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen);
public static partial bool CryptEncrypt( SafeKeyHandle hKey, SafeHashHandle hHash, bool Final, int dwFlags, byte[]?pbData, ref int pdwDataLen, int dwBufLen);
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); }
/// <summary> /// Retrieves the hash or Message Authentication Code (MAC) value for the data accumulated from prior calls to <see cref="BCryptHashData(SafeHashHandle, byte[], int, BCryptHashDataFlags)"/>. /// </summary> /// <param name="hHash"> /// The handle of the hash or MAC object to use to compute the hash or MAC. This handle is obtained by calling the <see cref="BCryptCreateHash(SafeAlgorithmHandle, byte[], byte[], BCryptCreateHashFlags)"/> function. After this function has been called, the hash handle passed to this function cannot be used again except in a call to <see cref="BCryptDestroyHash"/>. /// </param> /// <param name="flags">A set of flags that modify the behavior of this function.</param> /// <returns>The hash or MAC value.</returns> public static byte[] BCryptFinishHash( SafeHashHandle hHash, BCryptFinishHashFlags flags = BCryptFinishHashFlags.None) { int hashLength = BCryptGetProperty <int>(hHash, PropertyNames.BCRYPT_HASH_LENGTH); byte[] result = new byte[hashLength]; BCryptFinishHash(hHash, result, result.Length, flags).ThrowOnError(); return(result); }
private void DestroyHash() { SafeHashHandle hHash = _hHash; _hHash = null; if (hHash != null) { hHash.Dispose(); } }
[System.Security.SecuritySafeCritical] // auto-generated public override void Initialize() { if (_safeHashHandle != null && !_safeHashHandle.IsClosed) { _safeHashHandle.Dispose(); } // _CreateHash will check for failures and throw the appropriate exception _safeHashHandle = Utils.CreateHash(Utils.StaticProvHandle, Constants.CALG_SHA1); }
public static bool CryptDeriveKey( SafeProvHandle hProv, int algId, SafeHashHandle phHash, int dwFlags, out SafeKeyHandle phKey) { bool response = _CryptDeriveKey(hProv, algId, phHash, dwFlags, out phKey); phKey.SetParent(hProv); return response; }
internal static extern bool CryptCreateHash( SafeProvHandle hProv, int Algid, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash);
private static extern bool _CryptDeriveKey(SafeProvHandle safeProvHandle, int algId, SafeHashHandle phHash, int dwFlags, out SafeKeyHandle phKey);
public static extern bool CryptHashData(SafeHashHandle hHash, byte[] pbData, int dwDataLen, int dwFlags);
internal static extern bool CryptDeriveKey( SafeProvHandle hProv, int Algid, SafeHashHandle hBaseData, int dwFlags, out SafeKeyHandle phKey);
internal static partial bool CryptCreateHash( SafeProvHandle hProv, int Algid, SafeCapiKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash);
public static extern bool CryptGetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, out int pbData, [In, Out] ref int pdwDataLen, int dwFlags);
public static extern bool CryptSignHash(SafeHashHandle hHash, KeySpec dwKeySpec, String sDescription, CryptSignAndVerifyHashFlags dwFlags, [Out] byte[] pbSignature, [In, Out] ref int pdwSigLen);
public static bool CryptCreateHash( SafeProvHandle hProv, int algId, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash) { bool response = _CryptCreateHash(hProv, algId, hKey, dwFlags, out phHash); phHash.SetParent(hProv); return response; }
public static extern bool CryptEncrypt(SafeKeyHandle safeKeyHandle, SafeHashHandle safeHashHandle, bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen, int dwBufLen);
public static extern bool CryptGetHashParam( SafeHashHandle hHash, CryptHashProperty dwParam, IntPtr pbData, [In, Out] ref int pdwDataLen, int dwFlags);
public static partial bool CryptGetHashParam( SafeHashHandle hHash, CryptHashProperty dwParam, out int pbData, ref int pdwDataLen, int dwFlags);
public static extern NTSTATUS BCryptCreateHash( SafeAlgorithmHandle hAlgorithm, out SafeHashHandle phHash, byte[] pbHashObject, int cbHashObject, byte[] pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
public static extern NTSTATUS BCryptHashData( SafeHashHandle hHash, byte[] pbInput, int cbInput, BCryptHashDataFlags dwFlags = BCryptHashDataFlags.None);
private static extern bool _CryptCreateHash(SafeProvHandle hProv, int algId, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash);
public static extern unsafe NTSTATUS BCryptHashData(SafeHashHandle hHash, byte* pbInput, int cbInput, int dwFlags);
public static extern bool CryptSetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, byte[] buffer, int dwFlags);
public static extern NTSTATUS BCryptFinishHash(SafeHashHandle hHash, [Out] byte[] pbOutput, int cbOutput, int dwFlags);
public static extern bool CryptVerifySignature(SafeHashHandle hHash, byte[] pbSignature, int dwSigLen, SafeKeyHandle hPubKey, String sDescription, CryptSignAndVerifyHashFlags dwFlags);
public static extern NTSTATUS BCryptFinishHash( SafeHashHandle hHash, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pbOutput, int cbOutput, BCryptFinishHashFlags dwFlags = BCryptFinishHashFlags.None);
[System.Security.SecuritySafeCritical] // auto-generated public SHA1CryptoServiceProvider() { // _CreateHash will check for failures and throw the appropriate exception _safeHashHandle = Utils.CreateHash(Utils.StaticProvHandle, Constants.CALG_SHA1); }
public static extern NTSTATUS BCryptCreateHash(SafeAlgorithmHandle hAlgorithm, out SafeHashHandle phHash, IntPtr pbHashObject, int cbHashObject, [In, Out] byte[] pbSecret, int cbSecret, int dwFlags);