< Summary

Class:Microsoft.Azure.KeyVault.Cryptography.Algorithms.NativeMethods
Assembly:Microsoft.Azure.KeyVault.Cryptography
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\NativeMethods.cs
Covered lines:0
Uncovered lines:28
Coverable lines:28
Total lines:133
Line coverage:0% (0 of 28)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
NewNCryptPublicBlob(...)-0%100%
NewNCryptPrivateBlob(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\NativeMethods.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using System;
 6using System.IO;
 7using System.Runtime.InteropServices;
 8using System.Security.Cryptography;
 9using Microsoft.Win32.SafeHandles;
 10
 11namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms
 12{
 13    internal static class NativeMethods
 14    {
 15        internal const int Success = 0x00000000;                              // ERROR_SUCCESS
 16        internal const int BadSignature = unchecked( (int)0x80090006 );        // NTE_BAD_SIGNATURE
 17        internal const int InvalidParameter = unchecked( (int)0x80090027 );    // NTE_INVALID_PARAMETER
 18
 19        internal const int BCRYPT_RSAPUBLIC_MAGIC      = 0x31415352;
 20        internal const int BCRYPT_RSAPRIVATE_MAGIC     = 0x32415352;
 21        internal const int BCRYPT_RSAFULLPRIVATE_MAGIC = 0x33415352;
 22
 23        internal const int BCRYPT_ECDSA_PUBLIC_P256_MAGIC = 0x31534345;
 24        internal const int BCRYPT_ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
 25        internal const int BCRYPT_ECDSA_PUBLIC_P384_MAGIC = 0x33534345;
 26        internal const int BCRYPT_ECDSA_PRIVATE_P384_MAGIC = 0x34534345;
 27        internal const int BCRYPT_ECDSA_PUBLIC_P521_MAGIC = 0x35534345;
 28        internal const int BCRYPT_ECDSA_PRIVATE_P521_MAGIC = 0x36534345;
 29        internal const int BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC = 0x50444345;
 30        internal const int BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC = 0x56444345;
 31
 32        internal const string BCRYPT_ECCFULLPUBLIC_BLOB = "ECCFULLPUBLICBLOB";
 33        internal const string BCRYPT_ECCFULLPRIVATE_BLOB = "ECCFULLPRIVATEBLOB";
 34        internal const string BCRYPT_ECC_PARAMETERS = "ECCParameters";
 35
 36        internal const int BCRYPT_ECC_PRIME_SHORT_WEIERSTRASS_CURVE = 0x1;
 37        internal const int BCRYPT_ECC_PRIME_TWISTED_EDWARDS_CURVE = 0x2;
 38        internal const int BCRYPT_ECC_PRIME_MONTGOMERY_CURVE = 0x3;
 39
 40        internal const int BCRYPT_NO_CURVE_GENERATION_ALG_ID = 0x0;
 41
 42        [StructLayout( LayoutKind.Sequential )]
 43        internal struct NCRYPT_PKCS1_PADDING_INFO
 44        {
 45            [MarshalAs( UnmanagedType.LPWStr )]
 46            public string pszAlgId;
 47        }
 48
 49        /// <summary>
 50        ///     Padding modes
 51        /// </summary>
 52        internal enum AsymmetricPaddingMode
 53        {
 54            None = 1,                       // BCRYPT_PAD_NONE
 55            Pkcs1 = 2,                      // BCRYPT_PAD_PKCS1
 56            Oaep = 4,                       // BCRYPT_PAD_OAEP
 57            Pss = 8                         // BCRYPT_PAD_PSS
 58        }
 59
 60        [DllImport( "ncrypt.dll" )]
 61        internal static extern int NCryptOpenStorageProvider( [Out] out SafeNCryptProviderHandle phProvider,
 62                                                            [MarshalAs( UnmanagedType.LPWStr )] string pszProviderName,
 63                                                            int dwFlags );
 64
 65        [DllImport( "ncrypt.dll" )]
 66        internal static extern int NCryptImportKey( SafeNCryptProviderHandle hProvider,
 67                                                            IntPtr hImportKey,
 68                                                            [MarshalAs( UnmanagedType.LPWStr )] string pszBlobType,
 69                                                            [In, MarshalAs( UnmanagedType.LPArray )] byte[] pParameterLi
 70                                                            out SafeNCryptKeyHandle phKey,
 71                                                            [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbData,
 72                                                            int cbData,
 73                                                            int dwFlags );
 74
 75        [DllImport( "ncrypt.dll" )]
 76        internal static extern int NCryptSignHash(
 77            SafeNCryptKeyHandle hKey,
 78            [In] ref NCRYPT_PKCS1_PADDING_INFO pPaddingInfo,
 79            [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbHashValue,
 80            int cbHashValue,
 81            [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbSignature,
 82            int cbSignature,
 83            [Out] out int pcbResult,
 84            AsymmetricPaddingMode dwFlags );
 85
 86        [DllImport( "ncrypt.dll" )]
 87        internal static extern int NCryptVerifySignature( SafeNCryptKeyHandle hKey,
 88                                                               [In] ref NCRYPT_PKCS1_PADDING_INFO pPaddingInfo,
 89                                                               [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbHashVal
 90                                                               int cbHashValue,
 91                                                               [In, MarshalAs( UnmanagedType.LPArray )] byte[] pbSignatu
 92                                                               int cbSignature,
 93                                                               AsymmetricPaddingMode dwFlags );
 94
 95        internal static byte[] NewNCryptPublicBlob( RSAParameters rsaParams )
 96        {
 97            // Builds a BCRYPT_RSAKEY_BLOB strucutre ( http://msdn.microsoft.com/en-us/library/windows/desktop/aa375531(
 098            var size = 6 * 4 + rsaParams.Exponent.Length + rsaParams.Modulus.Length;
 099            var data = new byte[size];
 0100            var stream = new MemoryStream( data );
 0101            var writer = new BinaryWriter( stream );
 0102            writer.Write( (int)0x31415352 );
 0103            writer.Write( (int)rsaParams.Modulus.Length * 8 );
 0104            writer.Write( (int)rsaParams.Exponent.Length );
 0105            writer.Write( (int)rsaParams.Modulus.Length );
 0106            writer.Write( (int)0 );
 0107            writer.Write( (int)0 );
 0108            writer.Write( rsaParams.Exponent );
 0109            writer.Write( rsaParams.Modulus );
 0110            return data;
 111        }
 112
 113        internal static byte[] NewNCryptPrivateBlob( RSAParameters rsaParams )
 114        {
 115            // Builds a BCRYPT_RSAKEY_BLOB strucutre ( http://msdn.microsoft.com/en-us/library/windows/desktop/aa375531(
 0116            var size = 6 * 4 + rsaParams.Exponent.Length + rsaParams.Modulus.Length + rsaParams.P.Length + rsaParams.Q.L
 0117            var data = new byte[size];
 0118            var stream = new MemoryStream( data );
 0119            var writer = new BinaryWriter( stream );
 0120            writer.Write( BCRYPT_RSAPRIVATE_MAGIC );
 0121            writer.Write( rsaParams.Modulus.Length * 8 ); // BitLength
 0122            writer.Write( rsaParams.Exponent.Length ); // cbPublicExp
 0123            writer.Write( rsaParams.Modulus.Length ); // cbModulus
 0124            writer.Write( rsaParams.P.Length ); // cbPrime1
 0125            writer.Write( rsaParams.Q.Length ); // cbPrime2
 0126            writer.Write( rsaParams.Exponent );
 0127            writer.Write( rsaParams.Modulus );
 0128            writer.Write( rsaParams.P );
 0129            writer.Write( rsaParams.Q );
 0130            return data;
 131        }
 132    }
 133}