コード例 #1
0
 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));
     }
 }
コード例 #2
0
 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));
     }
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: dreshetnyak/FileBadger
 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);
コード例 #4
0
        //
        //   - "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;
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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().
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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;
        }
コード例 #9
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);
            }
        }
コード例 #10
0
 internal static partial NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags);
コード例 #11
0
 internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags) =>
 BCryptFinishHash(hHash, ref pbOutput.DangerousGetPinnableReference(), cbOutput, dwFlags);
コード例 #12
0
 internal static extern NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, [In, Out] byte[] pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
コード例 #13
0
 internal static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, IntPtr pbOutput, int cbOutput, int dwFlags);
コード例 #14
0
 internal extern static NTSTATUS BCryptDuplicateHash(SafeBCryptHashHandle hHash, out SafeBCryptHashHandle phNewHash, IntPtr pbHashObject, int cbHashObject, int dwFlags);
コード例 #15
0
 internal static extern unsafe int BCryptHashData(SafeBCryptHashHandle hashHandle, byte *inputBuffer, int inputByteLength, int flags);
コード例 #16
0
ファイル: Program.cs プロジェクト: dreshetnyak/FileBadger
 private static extern ErrorCode BCryptFinishHash(SafeBCryptHashHandle hHash,
                                                  [MarshalAs(UnmanagedType.LPArray), Out] byte[] pbInput,
                                                  int cbInput,
                                                  int dwFlags);
コード例 #17
0
 internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags) =>
 BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags);
コード例 #18
0
 private static partial NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags);
コード例 #19
0
 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));
 }
コード例 #20
0
 internal static partial NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags);
コード例 #21
0
 internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan <byte> pbInput, int cbInput, int dwFlags) =>
 BCryptHashData(hHash, ref pbInput.DangerousGetPinnableReference(), cbInput, dwFlags);
コード例 #22
0
ファイル: BCryptHMAC.cs プロジェクト: scholtz/FastZep
        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;
        }
コード例 #23
0
 private static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags);
コード例 #24
0
 private static unsafe extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, byte *pbOutput, int cbOutput, int dwFlags);
コード例 #25
0
 internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span <byte> pbOutput, int cbOutput, int dwFlags) =>
 BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags);
コード例 #26
0
 internal static extern int BCryptCreateHash(SafeBCryptAlgorithmHandle algorithmHandle, out SafeBCryptHashHandle hashHandle, IntPtr hashObject, int hashObjectByteLength, [In, Out] byte[] secretBuffer, int secretByteLength, BCryptCreateHashFlags flags);
コード例 #27
0
 private static extern unsafe NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, byte *pbInput, int cbInput, int dwFlags);
コード例 #28
0
 internal static extern int BCryptFinishHash(SafeBCryptHashHandle hashHandle, [Out] byte[] outputBuffer, int outputByteLength, int flags);
コード例 #29
0
 private static partial NTSTATUS BCryptDuplicateHash(
     SafeBCryptHashHandle hHash,
     out SafeBCryptHashHandle phNewHash,
     IntPtr pbHashObject,
     int cbHashObject,
     int dwFlags);
コード例 #30
0
 private static partial NTSTATUS BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, out SafeBCryptHashHandle phHash, IntPtr pbHashObject, int cbHashObject, ref byte pbSecret, int cbSecret, BCryptCreateHashFlags dwFlags);
コード例 #31
0
 internal static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, [Out] byte[] pbOutput, int cbOutput, int dwFlags);