internal static unsafe NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags) { fixed(byte *pbInputPtr = &pbInput.DangerousGetPinnableReference()) { return(BCryptHashData(hHash, pbInputPtr, cbInput, dwFlags)); } }
internal static unsafe NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags) { fixed(byte *pbOutputPtr = &pbOutput.DangerousGetPinnableReference()) { return(BCryptFinishHash(hHash, pbOutputPtr, cbOutput, dwFlags)); } }
private static extern ErrorCode BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, [Out] out SafeBCryptHashHandle phHash, [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbHashObject, int cbHashObject, IntPtr pbSecret, int cbSecret, BCryptAlgorithmFlags dwFlags);
// // - "hashAlgId" must be a name recognized by BCryptOpenAlgorithmProvider(). Examples: MD5, SHA1, SHA256. // // - "key" activates MAC hashing if present. If null, this HashProvider performs a regular old hash. // public HashProviderCng(string hashAlgId, byte[] key) { BCryptOpenAlgorithmProviderFlags dwFlags = BCryptOpenAlgorithmProviderFlags.None; if (key != null) { _key = key.CloneByteArray(); dwFlags |= BCryptOpenAlgorithmProviderFlags.BCRYPT_ALG_HANDLE_HMAC_FLAG; } _hAlgorithm = Interop.BCrypt.BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(hashAlgId, dwFlags); // Win7 won't set hHash, Win8+ will; and both will set _hHash. // So keep hHash trapped in this scope to prevent (mis-)use of it. { SafeBCryptHashHandle hHash = null; NTSTATUS ntStatus = Interop.BCrypt.BCryptCreateHash(_hAlgorithm, out hHash, IntPtr.Zero, 0, key, key == null ? 0 : key.Length, BCryptCreateHashFlags.BCRYPT_HASH_REUSABLE_FLAG); if (ntStatus == NTSTATUS.STATUS_INVALID_PARAMETER) { // If we got here, we're running on a downlevel OS (pre-Win8) that doesn't support reusable CNG hash objects. Fall back to creating a // new HASH object each time. ResetHashObject(); } else if (ntStatus != NTSTATUS.STATUS_SUCCESS) { throw Interop.BCrypt.CreateCryptographicException(ntStatus); } else { _hHash = hHash; //begin: gost if (hashAlgId != GostConstants.GOST3411_STRING) { //end: gost _reusable = true; //begin: gost } //end: gost } } unsafe { int cbSizeOfHashSize; int hashSize; NTSTATUS ntStatus = Interop.BCrypt.BCryptGetProperty(_hHash, Interop.BCrypt.BCryptPropertyStrings.BCRYPT_HASH_LENGTH, &hashSize, sizeof(int), out cbSizeOfHashSize, 0); if (ntStatus != NTSTATUS.STATUS_SUCCESS) { throw Interop.BCrypt.CreateCryptographicException(ntStatus); } _hashSize = hashSize; } return; }
internal static byte[] CalculateHash(byte[] inputBuffer) { byte[] hashResult; int status; SafeBCryptHashHandle hashHandle = null; status = MD5PInvokeHelper.BCryptCreateHash(MD5PInvokeHelper.MD5AlgorithmProvider, out hashHandle, IntPtr.Zero, 0, null, 0, 0); if (status != MD5PInvokeHelper.StatusSuccess) { DisposeMD5Handles(); throw new CryptographicException(status); } using (hashHandle) { int sizeOfHashSize; int hashSize; unsafe { status = MD5PInvokeHelper.BCryptGetProperty(hashHandle, MD5PInvokeHelper.BCryptHashLength, &hashSize, sizeof(int), out sizeOfHashSize, 0); if (status != MD5PInvokeHelper.StatusSuccess) { DisposeMD5Handles(); throw new CryptographicException(status); } } unsafe { fixed(byte *inputBytePointer = inputBuffer) { status = MD5PInvokeHelper.BCryptHashData(hashHandle, inputBytePointer, inputBuffer.Length, 0); if (status != MD5PInvokeHelper.StatusSuccess) { DisposeMD5Handles(); throw new CryptographicException(status); } } } hashResult = new byte[hashSize]; status = MD5PInvokeHelper.BCryptFinishHash(hashHandle, hashResult, hashResult.Length, 0); if (status != MD5PInvokeHelper.StatusSuccess) { DisposeMD5Handles(); throw new CryptographicException(status); } } // hashHandle has been disposed by the end of the "using" block. hashHandle = null; return(hashResult); }
private void DestroyHash() { SafeBCryptHashHandle hHash = _hHash; _hHash = null; if (hHash != null) { hHash.Dispose(); } // Not disposing of _hAlgorithm as we got this from a cache. So it's not ours to Dispose(). }
internal static SafeBCryptHashHandle BCryptDuplicateHash(SafeBCryptHashHandle hHash) { SafeBCryptHashHandle newHash; NTSTATUS status = BCryptDuplicateHash(hHash, out newHash, IntPtr.Zero, 0, 0); if (status != NTSTATUS.STATUS_SUCCESS) { newHash.Dispose(); throw CreateCryptographicException(status); } return(newHash); }
public override void Initialize() { Debug.Assert(m_algorithm != null, "m_algorithm != null"); base.Initialize(); // If we have a previously used hash handle, we can clean it up now if (m_hash != null) { m_hash.Dispose(); } m_hash = BCryptNative.CreateHash(m_algorithm, KeyValue); // We're allowed to reset the key at this point State = 0; }
public override int GetCurrentHash(Span <byte> destination) { Debug.Assert(destination.Length >= _hashSize); Debug.Assert(_hHash != null); using (SafeBCryptHashHandle tmpHash = Interop.BCrypt.BCryptDuplicateHash(_hHash)) { NTSTATUS ntStatus = Interop.BCrypt.BCryptFinishHash(tmpHash, destination, _hashSize, 0); if (ntStatus != NTSTATUS.STATUS_SUCCESS) { throw Interop.BCrypt.CreateCryptographicException(ntStatus); } return(_hashSize); } }
internal static partial NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags);
internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags) => BCryptFinishHash(hHash, ref pbOutput.DangerousGetPinnableReference(), cbOutput, dwFlags);
internal static extern NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, [In, Out] byte[] pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
internal static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, IntPtr pbOutput, int cbOutput, int dwFlags);
internal extern static NTSTATUS BCryptDuplicateHash(SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags);
internal static extern unsafe int BCryptHashData(SafeBCryptHashHandle hashHandle, byte *inputBuffer, int inputByteLength, int flags);
private static extern ErrorCode BCryptFinishHash(SafeBCryptHashHandle hHash, [MarshalAs(UnmanagedType.LPArray), Out] byte[] pbInput, int cbInput, int dwFlags);
internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags) => BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags);
private static partial NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags);
internal static NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ReadOnlySpan <byte> secret, int cbSecret, BCryptCreateHashFlags dwFlags) { return(BCryptCreateHash(hAlgorithm, out phHash, pbHashObject, cbHashObject, ref MemoryMarshal.GetReference(secret), cbSecret, dwFlags)); }
internal static partial NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags);
internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags) => BCryptHashData(hHash, ref pbInput.DangerousGetPinnableReference(), cbInput, dwFlags);
private static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags);
private static unsafe extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, byte *pbOutput, int cbOutput, int dwFlags);
internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags) => BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags);
internal static extern int BCryptCreateHash(SafeBCryptAlgorithmHandle algorithmHandle, out SafeBCryptHashHandle hashHandle, IntPtr hashObject, int hashObjectByteLength, [In, Out] byte[] secretBuffer, int secretByteLength, BCryptCreateHashFlags flags);
private static extern unsafe NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, byte *pbInput, int cbInput, int dwFlags);
internal static extern int BCryptFinishHash(SafeBCryptHashHandle hashHandle, [Out] byte[] outputBuffer, int outputByteLength, int flags);
private static partial NTSTATUS BCryptDuplicateHash( SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags);
private static partial NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
internal static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, [Out] byte[] pbOutput, int cbOutput, int dwFlags);