Example #1
0
 internal static extern ErrorCode BCryptExportKey([In] SafeBCryptKeyHandle hKey,
                                                  [In] IntPtr hExportKey,
                                                  [In][MarshalAs(UnmanagedType.LPWStr)] string pszBlobType,
                                                  [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbOutput,
                                                  [In] int cbOutput,
                                                  [In] ref int pcbResult,
                                                  [In] int dwFlags);
Example #2
0
 internal static extern ErrorCode BCryptImportKey(
     SafeBCryptAlgorithmHandle hAlgorithm,
     IntPtr hImportKey,
     string pszBlobType,
     out SafeBCryptKeyHandle hKey,
     IntPtr pbKeyObject,
     int cbKeyObject,
     byte[] pbInput,
     int cbInput,
     int dwFlags);
Example #3
0
 public static extern unsafe ErrorCode BCryptDecrypt(
     SafeBCryptKeyHandle hKey,
     byte *pbInput,
     int cbInput,
     IntPtr paddingInfo,
     [In, Out] byte[] pbIV,
     int cbIV,
     byte *pbOutput,
     int cbOutput,
     out int cbResult,
     int dwFlags);
Example #4
0
        public static int BCryptDecrypt(
            SafeBCryptKeyHandle hKey,
            byte[] input,
            int inputOffset,
            int inputCount,
            byte[] iv,
            byte[] output,
            int outputOffset,
            int outputCount)
        {
            Debug.Assert(input != null);
            Debug.Assert(inputOffset >= 0);
            Debug.Assert(inputCount >= 0);
            Debug.Assert(inputCount <= input.Length - inputOffset);
            Debug.Assert(output != null);
            Debug.Assert(outputOffset >= 0);
            Debug.Assert(outputCount >= 0);
            Debug.Assert(outputCount <= output.Length - outputOffset);

            unsafe
            {
                fixed(byte *pbInput = input)
                {
                    fixed(byte *pbOutput = output)
                    {
                        int       cbResult;
                        ErrorCode error = UnsafeNativeMethods.BCryptDecrypt(
                            hKey,
                            pbInput inputOffset,
                            inputCount,
                            IntPtr.Zero,
                            iv,
                            iv == null ? 0 : iv.Length,
                            pbOutput outputOffset,
                            outputCount,
                            out cbResult,
                            0);

                        if (error != ErrorCode.Success)
                        {
                            throw new CryptographicException((int)error);
                        }

                        return(cbResult);
                    }
                }
            }
        }
Example #5
0
        internal static SafeBCryptKeyHandle ImportAsymmetricPublicKey(X509Native.CERT_PUBLIC_KEY_INFO certPublicKeyInfo, int dwFlag)
        {
            SafeBCryptKeyHandle keyHandle = null;
            int error = UnsafeNativeMethods.CryptImportPublicKeyInfoEx2(
                X509Native.X509_ASN_ENCODING,
                ref certPublicKeyInfo,
                dwFlag,
                IntPtr.Zero,
                out keyHandle);

            if (error == 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(keyHandle);
        }
Example #6
0
        internal static byte[] ExportBCryptKey(SafeBCryptKeyHandle hKey, string blobType)
        {
            byte[] keyBlob = null;
            int    length  = 0;

            ErrorCode error = UnsafeNativeMethods.BCryptExportKey(hKey, IntPtr.Zero, blobType, null, 0, ref length, 0);

            if (error != ErrorCode.BufferToSmall && error != ErrorCode.Success)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            keyBlob = new byte[length];
            error   = UnsafeNativeMethods.BCryptExportKey(hKey, IntPtr.Zero, blobType, keyBlob, length, ref length, 0);
            if (error != ErrorCode.Success)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(keyBlob);
        }
Example #7
0
 internal static extern int CryptImportPublicKeyInfoEx2([In] uint dwCertEncodingType,
                                                        [In] ref X509Native.CERT_PUBLIC_KEY_INFO pInfo,
                                                        [In] int dwFlags,
                                                        [In] IntPtr pvAuxInfo,
                                                        [Out] out SafeBCryptKeyHandle phKey);
Example #8
0
 internal static extern ErrorCode BCryptGetProperty(SafeBCryptKeyHandle hObject,
                                                    string pszProperty,
                                                    [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
                                                    int cbOutput,
                                                    [Out] out int pcbResult,
                                                    int flags);