Esempio n. 1
0
 internal static extern ErrorCode BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm,
                                                   [Out] out SafeBCryptHashHandle phHash,
                                                   IntPtr pbHashObject,
                                                   int cbHashObject,
                                                   IntPtr pbSecret,
                                                   int cbSecret,
                                                   int dwFlags);
Esempio n. 2
0
        internal LiteHash(string algorithm)
        {
            BCryptOpenAlgorithmProviderFlags algorithmFlags =
                BCryptOpenAlgorithmProviderFlags.None;

            // This is a shared handle, do not put this in a using.
            SafeBCryptAlgorithmHandle algorithmHandle = Interop.BCrypt.BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(
                algorithm,
                algorithmFlags,
                out _hashSizeInBytes);

            SafeBCryptHashHandle hashHandle;

            NTSTATUS ntStatus = Interop.BCrypt.BCryptCreateHash(
                algorithmHandle,
                out hashHandle,
                pbHashObject: IntPtr.Zero,
                cbHashObject: 0,
                secret: ReadOnlySpan <byte> .Empty,
                cbSecret: 0,
                BCryptCreateHashFlags.None);

            if (ntStatus != NTSTATUS.STATUS_SUCCESS)
            {
                hashHandle.Dispose();
                throw Interop.BCrypt.CreateCryptographicException(ntStatus);
            }

            _hashHandle = hashHandle;
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        public void Initialize()
        {
            SafeBCryptHashHandle phHash = null;
            IntPtr zero = IntPtr.Zero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                int cb = BCryptNative.GetInt32Property <SafeBCryptAlgorithmHandle>(this.m_algorithmHandle, "ObjectLength");
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    zero = Marshal.AllocCoTaskMem(cb);
                }
                BCryptNative.ErrorCode code = BCryptNative.UnsafeNativeMethods.BCryptCreateHash(this.m_algorithmHandle, out phHash, zero, cb, IntPtr.Zero, 0, 0);
                if (code != BCryptNative.ErrorCode.Success)
                {
                    throw new CryptographicException((int)code);
                }
                phHash.HashObject = zero;
            }
            finally
            {
                if ((zero != IntPtr.Zero) && ((phHash == null) || (phHash.HashObject == IntPtr.Zero)))
                {
                    Marshal.FreeCoTaskMem(zero);
                }
            }
            if (this.m_hashHandle != null)
            {
                this.m_hashHandle.Dispose();
            }
            this.m_hashHandle = phHash;
        }
Esempio n. 5
0
 internal static extern ErrorCode BCryptHashData(SafeBCryptHashHandle hHash,
                                                 [MarshalAs(UnmanagedType.LPArray), In] byte[] pbInput,
                                                 int cbInput,
                                                 int dwFlags);
Esempio n. 6
0
 internal static extern ErrorCode BCryptGetHashProperty(SafeBCryptHashHandle hObject,
                                                        string pszProperty,
                                                        [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
                                                        int cbOutput,
                                                        [In, Out] ref int pcbResult,
                                                        int flags);
 internal static extern BCryptNative.ErrorCode BCryptFinishHash(SafeBCryptHashHandle hHash, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput, int cbInput, int dwFlags);
        public void Initialize()
        {
            Contract.Ensures(m_hashHandle != null && !m_hashHandle.IsInvalid && !m_hashHandle.IsClosed);
            Contract.Assert(m_algorithmHandle != null);

            // Try to create a new hash algorithm to use
            SafeBCryptHashHandle newHashAlgorithm = null;
            IntPtr hashObjectBuffer = IntPtr.Zero;

            // Creating a BCRYPT_HASH_HANDLE requires providing a buffer to hold the hash object in, which
            // is tied to the lifetime of the hash handle. Wrap this in a CER so we can tie the lifetimes together
            // safely.
            try
            {
                int hashObjectSize = BCryptNative.GetInt32Property(m_algorithmHandle,
                                                                   BCryptNative.ObjectPropertyName.ObjectLength);
                Debug.Assert(hashObjectSize > 0, "hashObjectSize > 0");

                // Allocate in a CER because we could fail between the alloc and the assignment
                try { }
                finally
                {
                    hashObjectBuffer = Marshal.AllocCoTaskMem(hashObjectSize);
                }

                BCryptNative.ErrorCode error = BCryptNative.UnsafeNativeMethods.BCryptCreateHash(m_algorithmHandle,
                                                                                                 out newHashAlgorithm,
                                                                                                 hashObjectBuffer,
                                                                                                 hashObjectSize,
                                                                                                 IntPtr.Zero,
                                                                                                 0,
                                                                                                 0);

                if (error != BCryptNative.ErrorCode.Success)
                {
                    throw new CryptographicException((int)error);
                }
            }
            finally
            {
                // Make sure we've successfully transfered ownership of the hash object buffer to the safe handle
                if (hashObjectBuffer != IntPtr.Zero)
                {
                    // If we created the safe handle, it needs to own the buffer and free it in release
                    if (newHashAlgorithm != null)
                    {
                        newHashAlgorithm.HashObject = hashObjectBuffer;
                    }
                    else
                    {
                        Marshal.FreeCoTaskMem(hashObjectBuffer);
                    }
                }
            }

            // If we could create it, dispose of any old hash handle we had and replace it with the new one
            if (m_hashHandle != null)
            {
                m_hashHandle.Dispose();
            }
            m_hashHandle = newHashAlgorithm;
        }
Esempio n. 9
0
 internal static extern unsafe ErrorCode BCryptHashData(SafeBCryptHashHandle hHash,
                                                        byte *pbInput,
                                                        int cbInput,
                                                        int dwFlags);