Esempio n. 1
0
        internal static byte[] GetHashParameter(SafeCapiHashHandle hashHandle, CapiNative.HashParameter parameter)
        {
            Contract.Requires(hashHandle != null);
            Contract.Requires(CapiNative.HashParameter.AlgorithmId <= parameter && parameter <= CapiNative.HashParameter.HashSize);

            //
            // Determine the maximum size of the parameter and retrieve it
            //

            int parameterSize = 0;

            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, null, ref parameterSize, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            Debug.Assert(0 < parameterSize, "Invalid parameter size returned");
            byte[] parameterValue = new byte[parameterSize];
            if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, parameterValue, ref parameterSize, 0))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            // CAPI may have asked for a larger buffer than it used, so only copy the used bytes
            if (parameterSize != parameterValue.Length)
            {
                byte[] realValue = new byte[parameterSize];
                Buffer.BlockCopy(parameterValue, 0, realValue, 0, parameterSize);
                parameterValue = realValue;
            }

            return(parameterValue);
        }
Esempio n. 2
0
        /// <summary>
        ///     Reset the hash algorithm to begin hashing a new set of data
        /// </summary>
        public SafeCapiHashHandle Initialize()
        {
            Contract.Ensures(m_hashHandle != null && !m_hashHandle.IsInvalid && !m_hashHandle.IsClosed);
            Contract.Assert(m_cspHandle != null);

            // Try to create a new hash algorithm to use
            SafeCapiHashHandle?newHashAlgorithm = null;

#if !NET_COREAPP_50
            RuntimeHelpers.PrepareConstrainedRegions();
#endif
            try
            {
                if (!CapiNative.UnsafeNativeMethods.CryptCreateHash(m_cspHandle,
                                                                    m_algorithmId,
                                                                    SafeCapiKeyHandle.InvalidHandle,
                                                                    0,
                                                                    out newHashAlgorithm))
                {
                    // BadAlgorithmId means that this CSP does not support the specified algorithm, which means
                    // that we're on a platform that does not support the given hash function.
                    int error = Marshal.GetLastWin32Error();
                    if (error == (int)CapiNative.ErrorCode.BadAlgorithmId)
                    {
                        throw new PlatformNotSupportedException(SR.Cryptography_PlatformNotSupported);
                    }
                    else
                    {
                        throw new CryptographicException(error);
                    }
                }
            }
            finally
            {
                if (newHashAlgorithm != null && !newHashAlgorithm.IsInvalid)
                {
                    newHashAlgorithm.SetParentCsp(m_cspHandle);
                }
            }

            // If we created a new algorithm, dispose of the old one and use the new one
            Contract.Assert(newHashAlgorithm != null, "newHashAlgorithm != null");
            if (m_hashHandle != null)
            {
                m_hashHandle.Dispose();
            }

            m_hashHandle = newHashAlgorithm;
            return(m_hashHandle);
        }
Esempio n. 3
0
        /*
         * SafeCritical - we're not exposing out anything that we want to prevent untrusted code from getting at
         */
        public CapiHashAlgorithm(
            string provider,
            CapiNative.ProviderType providerType,
            CapiNative.AlgorithmId algorithm)
        {
            Contract.Requires(!string.IsNullOrEmpty(provider));
            Contract.Requires((CapiNative.AlgorithmClass)((uint)algorithm & (uint)CapiNative.AlgorithmClass.Hash) == CapiNative.AlgorithmClass.Hash);

            m_algorithmId = algorithm;
            m_cspHandle   = CapiNative.AcquireCsp(null,
                                                  provider,
                                                  providerType,
                                                  CapiNative.CryptAcquireContextFlags.VerifyContext,
                                                  true);
            m_hashHandle = Initialize();
        }
Esempio n. 4
0
 public static extern unsafe bool CryptHashData(SafeCapiHashHandle hHash,
                                                byte *pbData,
                                                int dwDataLen,
                                                int dwFlags);
Esempio n. 5
0
 public static extern bool CryptCreateHash(SafeCspHandle hProv,
                                           AlgorithmId Algid,
                                           SafeCapiKeyHandle hKey,
                                           int dwFlags,
                                           [Out] out SafeCapiHashHandle phHash);
Esempio n. 6
0
 public static extern bool CryptGetHashParam(SafeCapiHashHandle hHash,
                                             HashParameter dwParam,
                                             [Out, MarshalAs(UnmanagedType.LPArray)] byte[]?pbData,
                                             [In, Out] ref int pdwDataLen,
                                             int dwFlags);