| | 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 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using System.Runtime.InteropServices; |
| | 8 | | using System.Security.Cryptography; |
| | 9 | | using Microsoft.Win32.SafeHandles; |
| | 10 | |
|
| | 11 | | namespace 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( |
| 0 | 98 | | var size = 6 * 4 + rsaParams.Exponent.Length + rsaParams.Modulus.Length; |
| 0 | 99 | | var data = new byte[size]; |
| 0 | 100 | | var stream = new MemoryStream( data ); |
| 0 | 101 | | var writer = new BinaryWriter( stream ); |
| 0 | 102 | | writer.Write( (int)0x31415352 ); |
| 0 | 103 | | writer.Write( (int)rsaParams.Modulus.Length * 8 ); |
| 0 | 104 | | writer.Write( (int)rsaParams.Exponent.Length ); |
| 0 | 105 | | writer.Write( (int)rsaParams.Modulus.Length ); |
| 0 | 106 | | writer.Write( (int)0 ); |
| 0 | 107 | | writer.Write( (int)0 ); |
| 0 | 108 | | writer.Write( rsaParams.Exponent ); |
| 0 | 109 | | writer.Write( rsaParams.Modulus ); |
| 0 | 110 | | 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( |
| 0 | 116 | | var size = 6 * 4 + rsaParams.Exponent.Length + rsaParams.Modulus.Length + rsaParams.P.Length + rsaParams.Q.L |
| 0 | 117 | | var data = new byte[size]; |
| 0 | 118 | | var stream = new MemoryStream( data ); |
| 0 | 119 | | var writer = new BinaryWriter( stream ); |
| 0 | 120 | | writer.Write( BCRYPT_RSAPRIVATE_MAGIC ); |
| 0 | 121 | | writer.Write( rsaParams.Modulus.Length * 8 ); // BitLength |
| 0 | 122 | | writer.Write( rsaParams.Exponent.Length ); // cbPublicExp |
| 0 | 123 | | writer.Write( rsaParams.Modulus.Length ); // cbModulus |
| 0 | 124 | | writer.Write( rsaParams.P.Length ); // cbPrime1 |
| 0 | 125 | | writer.Write( rsaParams.Q.Length ); // cbPrime2 |
| 0 | 126 | | writer.Write( rsaParams.Exponent ); |
| 0 | 127 | | writer.Write( rsaParams.Modulus ); |
| 0 | 128 | | writer.Write( rsaParams.P ); |
| 0 | 129 | | writer.Write( rsaParams.Q ); |
| 0 | 130 | | return data; |
| | 131 | | } |
| | 132 | | } |
| | 133 | | } |