170            if (!
encrypt && data.Length != num)
 
  179            if (
encrypt && data.Length == 0)
 
  206            switch (padding.
Mode)
 
  215                    global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO 
bCRYPT_OAEP_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
 
  216                    bCRYPT_OAEP_PADDING_INFO.pszAlgId = 
intPtr;
 
  217                    bCRYPT_OAEP_PADDING_INFO.pbLabel = 
IntPtr.
Zero;
 
  218                    bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
 
 
  239            if (!
encrypt && data.Length != num)
 
  248            if (
encrypt && data.Length == 0)
 
  275            switch (padding.
Mode)
 
  284                    global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO 
bCRYPT_OAEP_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
 
  285                    bCRYPT_OAEP_PADDING_INFO.pszAlgId = 
intPtr;
 
  286                    bCRYPT_OAEP_PADDING_INFO.pbLabel = 
IntPtr.
Zero;
 
  287                    bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
 
 
  304            byte[] 
array = 
new byte[num];
 
  306            global::Interop.NCrypt.ErrorCode 
errorCode = global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS;
 
  307            for (
int i = 0; i <= 1; i++)
 
  310                if (
errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
 
  315            if (
errorCode == global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL)
 
  319                for (
int j = 0; 
j <= 1; 
j++)
 
  322                    if (
errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
 
  330                throw errorCode.ToCryptographicException();
 
 
  343            for (
int i = 0; i <= 1; i++)
 
  349                case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
 
  352                case global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL:
 
  356                    throw errorCode.ToCryptographicException();
 
  357                case global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL:
 
  361            throw global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL.ToCryptographicException();
 
 
  366            global::Interop.NCrypt.ErrorCode 
errorCode = (
encrypt ? global::Interop.NCrypt.NCryptEncrypt(
key, 
input, 
input.Length, 
paddingInfo, 
output, 
output.Length, 
out bytesNeeded, 
paddingMode) : global::Interop.NCrypt.NCryptDecrypt(
key, 
input, 
input.Length, 
paddingInfo, 
output, 
output.Length, 
out bytesNeeded, 
paddingMode));
 
  369                errorCode = global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL;
 
 
  381            if (parameters.
D == 
null)
 
  384                if (parameters.
P != 
null || parameters.
DP != 
null || parameters.
Q != 
null || parameters.
DQ != 
null || parameters.
InverseQ != 
null)
 
  392                if (parameters.
P == 
null || parameters.
DP == 
null || parameters.
Q == 
null || parameters.
DQ == 
null || parameters.
InverseQ == 
null)
 
  396                int num = (parameters.Modulus.Length + 1) / 2;
 
  397                if (parameters.
D.Length != parameters.
Modulus.Length || parameters.
P.Length != num || parameters.
Q.Length != num || parameters.
DP.Length != num || parameters.
DQ.Length != num || parameters.
InverseQ.Length != num)
 
  402            int num2 = 
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB) + parameters.
Exponent.Length + parameters.
Modulus.Length;
 
  405                num2 += parameters.P.Length + parameters.
Q.Length;
 
  410                global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB* 
ptr2 = (global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*)
ptr;
 
  411                ptr2->Magic = (flag ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPUBLIC_MAGIC);
 
  412                ptr2->BitLength = parameters.Modulus.Length * 8;
 
  417                    ptr2->cbPrime1 = parameters.
P.Length;
 
  418                    ptr2->cbPrime2 = parameters.
Q.Length;
 
  420                int offset = 
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB);
 
 
  461            if (
response.GetAlgorithmGroup() != 
"RSA")
 
 
  526            global::Interop.BCrypt.KeyBlobMagicNumber magic = (global::Interop.BCrypt.KeyBlobMagicNumber)
BitConverter.
ToInt32(
rsaBlob, 0);
 
  528            if (
rsaBlob.Length < 
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB))
 
  530                throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
 
  534                global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB* 
ptr2 = (global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*)
ptr;
 
  535                int offset = 
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB);
 
 
  554                if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAFULLPRIVATE_MAGIC)
 
  559            else if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPUBLIC_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAFULLPRIVATE_MAGIC)
 
 
  580            string name = hashAlgorithm.
Name;
 
  581            if (
string.IsNullOrEmpty(name))
 
  598                switch (padding.
Mode)
 
  602                    global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO 
bCRYPT_PKCS1_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
 
  603                    bCRYPT_PKCS1_PADDING_INFO.pszAlgId = 
intPtr;
 
  609                    global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO 
bCRYPT_PSS_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
 
  610                    bCRYPT_PSS_PADDING_INFO.pszAlgId = 
intPtr;
 
  611                    bCRYPT_PSS_PADDING_INFO.cbSalt = 
hash.Length;
 
 
  627            string name = hashAlgorithm.
Name;
 
  628            if (
string.IsNullOrEmpty(name))
 
  644                switch (padding.
Mode)
 
  648                    global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO 
bCRYPT_PKCS1_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
 
  649                    bCRYPT_PKCS1_PADDING_INFO.pszAlgId = 
intPtr;
 
  655                    global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO 
bCRYPT_PSS_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
 
  656                    bCRYPT_PSS_PADDING_INFO.pszAlgId = 
intPtr;
 
  657                    bCRYPT_PSS_PADDING_INFO.cbSalt = 
hash.Length;
 
 
  686            string name = hashAlgorithm.
Name;
 
  687            if (
string.IsNullOrEmpty(name))
 
  703                switch (padding.
Mode)
 
  707                    global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO 
bCRYPT_PKCS1_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
 
  708                    bCRYPT_PKCS1_PADDING_INFO.pszAlgId = 
intPtr;
 
  714                    global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO 
bCRYPT_PSS_PADDING_INFO = 
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
 
  715                    bCRYPT_PSS_PADDING_INFO.pszAlgId = 
intPtr;
 
  716                    bCRYPT_PSS_PADDING_INFO.cbSalt = 
hash.Length;
 
 
 
 
static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
static bool TryHashData(ReadOnlySpan< byte > source, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
static int ToInt32(byte[] value, int startIndex)
static void FreeHGlobal(IntPtr hglobal)
static unsafe IntPtr StringToHGlobalUni(string? s)
static string Cryptography_NotValidPrivateKey
static string Cryptography_RSA_DecryptWrongSize
static string Cryptography_InvalidRsaParameters
static string Format(string resourceFormat, object p1)
static string Cryptography_NotValidPublicOrPrivateKey
static string Cryptography_UnsupportedPaddingMode
static string Cryptography_SignHash_WrongSize
static string Cryptography_Encryption_MessageTooLong
static string Cryptography_HashAlgorithmNameNullOrEmpty
static bool TryExportPkcs8KeyBlob(SafeNCryptKeyHandle keyHandle, ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten)
static byte[] ExportPkcs8KeyBlob(SafeNCryptKeyHandle keyHandle, ReadOnlySpan< char > password, int kdfCount)
static unsafe SafeNCryptKeyHandle ImportKeyBlob(string blobType, ReadOnlySpan< byte > keyBlob, bool encrypted=false, ReadOnlySpan< char > password=default(ReadOnlySpan< char >))
static byte[] ExportKeyBlob(SafeNCryptKeyHandle keyHandle, string blobType)
static int GetKeyLength(SafeNCryptKeyHandle keyHandle)
static SafeNCryptKeyHandle GenerateNewExportableKey(string algorithm, int keySize)
static bool IsPlatformScheme(PbeParameters pbeParameters)
static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
static Pkcs8Response ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
static void Return(byte[] array, int clearSize=-1)
static byte[] Rent(int minimumLength)
static void ZeroMemory(Span< byte > buffer)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)
RSAEncryptionPaddingMode Mode
static RSAEncryptionPadding Pkcs1
HashAlgorithmName OaepHashAlgorithm
byte[] ExportKeyBlob(bool includePrivateParameters)
override bool TryDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, out int bytesWritten)
SafeNCryptKeyHandle _keyHandle
override bool TryHashData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
bool TryExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount, Span< byte > destination, out int bytesWritten)
void AcceptImport(CngPkcs8.Pkcs8Response response)
void ProcessPkcs8Response(CngPkcs8.Pkcs8Response response)
unsafe override bool TrySignHash(ReadOnlySpan< byte > hash, Span< byte > destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
unsafe bool TryEncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, Span< byte > output, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt, out int bytesWritten)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding, bool encrypt)
override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
override void ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
static int GetHashSizeInBytes(HashAlgorithmName hashAlgorithm)
byte[] ExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount)
override bool TryEncrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, out int bytesWritten)
static unsafe void ExportParameters(ref RSAParameters rsaParams, byte[] rsaBlob, bool includePrivateParameters)
static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
static unsafe global::Interop.NCrypt.ErrorCode EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, Span< byte > output, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt, out int bytesNeeded)
override KeySizes[] LegalKeySizes
unsafe override bool VerifyHash(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
unsafe override void ImportParameters(RSAParameters parameters)
override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
unsafe bool TryEncryptOrDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
void ForceSetKeySize(int newKeySize)
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters)
override RSAParameters ExportParameters(bool includePrivateParameters)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
void SetKeyHandle(SafeNCryptKeyHandle keyHandle)
static readonly ConcurrentDictionary< HashAlgorithmName, int > s_hashSizes
override void Dispose(bool disposing)
unsafe override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
unsafe byte[] EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt)
RSASignaturePaddingMode Mode
static void PadPkcs1Encryption(ReadOnlySpan< byte > source, Span< byte > destination)
static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
static int BytesRequiredForBitCount(int keySizeInBits)
static readonly IntPtr Zero
static HashAlgorithmName SHA512
static HashAlgorithmName SHA256
static HashAlgorithmName SHA384