예제 #1
0
 public static partial bool CryptDecrypt(
     SafeCapiKeyHandle hKey,
     SafeHashHandle hHash,
     bool Final,
     int dwFlags,
     byte[] pbData,
     ref int pdwDataLen);
예제 #2
0
        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));
                }
            }
        }
예제 #3
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);
            }
예제 #4
0
 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);
예제 #6
0
 public static extern bool CryptSignHash(
     SafeHashHandle hHash,
     KeySpec dwKeySpec,
     string szDescription,
     CryptSignAndVerifyHashFlags dwFlags,
     [Out] byte[] pbSignature,
     [In, Out] ref int pdwSigLen);
예제 #7
0
 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);
 }
예제 #9
0
 public static partial bool CryptDecrypt(
     SafeCapiKeyHandle hKey,
     SafeHashHandle hHash,
     [MarshalAs(UnmanagedType.Bool)] bool Final,
     int dwFlags,
     byte[] pbData,
     ref int pdwDataLen);
예제 #10
0
 public static partial bool CryptEncrypt(
     SafeKeyHandle hKey,
     SafeHashHandle hHash,
     bool Final,
     int dwFlags,
     byte[]?pbData,
     ref int pdwDataLen,
     int dwBufLen);
예제 #11
0
            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);
            }
예제 #12
0
        /// <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();
                }
            }
예제 #14
0
        [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);
        }
예제 #15
0
            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);
예제 #17
0
 private static extern bool _CryptDeriveKey(SafeProvHandle safeProvHandle, int algId, SafeHashHandle phHash, int dwFlags, out SafeKeyHandle phKey);
예제 #18
0
 public static extern bool CryptHashData(SafeHashHandle hHash, byte[] pbData, int dwDataLen, int dwFlags);
예제 #19
0
 internal static extern bool CryptDeriveKey(
     SafeProvHandle hProv,
     int Algid,
     SafeHashHandle hBaseData,
     int dwFlags,
     out SafeKeyHandle phKey);
예제 #20
0
 internal static partial bool CryptCreateHash(
     SafeProvHandle hProv,
     int Algid,
     SafeCapiKeyHandle hKey,
     CryptCreateHashFlags dwFlags,
     out SafeHashHandle phHash);
예제 #21
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 public static extern bool CryptGetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, out int pbData, [In, Out] ref int pdwDataLen, int dwFlags);
예제 #22
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 public static extern bool CryptSignHash(SafeHashHandle hHash, KeySpec dwKeySpec, String sDescription, CryptSignAndVerifyHashFlags dwFlags, [Out] byte[] pbSignature, [In, Out] ref int pdwSigLen);
예제 #23
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
            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;
            }
예제 #24
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 public static extern bool CryptEncrypt(SafeKeyHandle safeKeyHandle, SafeHashHandle safeHashHandle,
                                         bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen,
                                         int dwBufLen);
예제 #25
0
 public static extern bool CryptGetHashParam(
     SafeHashHandle hHash,
     CryptHashProperty dwParam,
     IntPtr pbData,
     [In, Out] ref int pdwDataLen,
     int dwFlags);
예제 #26
0
 public static partial bool CryptGetHashParam(
     SafeHashHandle hHash,
     CryptHashProperty dwParam,
     out int pbData,
     ref int pdwDataLen,
     int dwFlags);
예제 #27
0
 public static extern NTSTATUS BCryptCreateHash(
     SafeAlgorithmHandle hAlgorithm,
     out SafeHashHandle phHash,
     byte[] pbHashObject,
     int cbHashObject,
     byte[] pbSecret,
     int cbSecret,
     BCryptCreateHashFlags dwFlags);
예제 #28
0
 public static extern NTSTATUS BCryptHashData(
     SafeHashHandle hHash,
     byte[] pbInput,
     int cbInput,
     BCryptHashDataFlags dwFlags = BCryptHashDataFlags.None);
 public static extern bool CryptHashData(SafeHashHandle hHash, byte[] pbData, int dwDataLen, int dwFlags);
예제 #30
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 private static extern bool _CryptCreateHash(SafeProvHandle hProv, int algId, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash);
예제 #31
0
파일: Cng.cs 프로젝트: nuskarthik/corefx
 public static extern unsafe NTSTATUS BCryptHashData(SafeHashHandle hHash, byte* pbInput, int cbInput, int dwFlags);
예제 #32
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 public static extern bool CryptSetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, byte[] buffer, int dwFlags);
예제 #33
0
파일: Cng.cs 프로젝트: nuskarthik/corefx
 public static extern NTSTATUS BCryptFinishHash(SafeHashHandle hHash, [Out] byte[] pbOutput, int cbOutput, int dwFlags);
예제 #34
0
파일: CapiHelper.cs 프로젝트: SGuyGe/corefx
 public static extern bool CryptVerifySignature(SafeHashHandle hHash, byte[] pbSignature, int dwSigLen, SafeKeyHandle hPubKey, String sDescription, CryptSignAndVerifyHashFlags dwFlags);
예제 #35
0
 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);
 }
예제 #37
0
 public static extern bool CryptSetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, byte[] buffer, int dwFlags);
예제 #38
0
파일: Cng.cs 프로젝트: nuskarthik/corefx
 public static extern NTSTATUS BCryptCreateHash(SafeAlgorithmHandle hAlgorithm, out SafeHashHandle phHash, IntPtr pbHashObject, int cbHashObject, [In, Out] byte[] pbSecret, int cbSecret, int dwFlags);
예제 #39
0
 [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);
 }