171 if (!
encrypt && data.Length != num)
180 if (
encrypt && data.Length == 0)
207 switch (padding.
Mode)
216 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO
bCRYPT_OAEP_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
217 bCRYPT_OAEP_PADDING_INFO.pszAlgId =
intPtr;
218 bCRYPT_OAEP_PADDING_INFO.pbLabel =
IntPtr.
Zero;
219 bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
240 if (!
encrypt && data.Length != num)
249 if (
encrypt && data.Length == 0)
276 switch (padding.
Mode)
285 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO
bCRYPT_OAEP_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
286 bCRYPT_OAEP_PADDING_INFO.pszAlgId =
intPtr;
287 bCRYPT_OAEP_PADDING_INFO.pbLabel =
IntPtr.
Zero;
288 bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
305 byte[]
array =
new byte[num];
307 global::Interop.NCrypt.ErrorCode
errorCode = global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS;
308 for (
int i = 0; i <= 1; i++)
311 if (
errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
316 if (
errorCode == global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL)
320 for (
int j = 0;
j <= 1;
j++)
323 if (
errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
331 throw errorCode.ToCryptographicException();
344 for (
int i = 0; i <= 1; i++)
350 case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
353 case global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL:
357 throw errorCode.ToCryptographicException();
358 case global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL:
362 throw global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL.ToCryptographicException();
367 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));
370 errorCode = global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL;
382 if (parameters.
D ==
null)
385 if (parameters.
P !=
null || parameters.
DP !=
null || parameters.
Q !=
null || parameters.
DQ !=
null || parameters.
InverseQ !=
null)
393 if (parameters.
P ==
null || parameters.
DP ==
null || parameters.
Q ==
null || parameters.
DQ ==
null || parameters.
InverseQ ==
null)
397 int num = (parameters.Modulus.Length + 1) / 2;
398 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)
403 int num2 =
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB) + parameters.
Exponent.Length + parameters.
Modulus.Length;
406 num2 += parameters.P.Length + parameters.
Q.Length;
411 global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*
ptr2 = (global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*)
ptr;
412 ptr2->Magic = (flag ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPUBLIC_MAGIC);
413 ptr2->BitLength = parameters.Modulus.Length * 8;
418 ptr2->cbPrime1 = parameters.
P.Length;
419 ptr2->cbPrime2 = parameters.
Q.Length;
421 int offset =
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB);
462 if (
response.GetAlgorithmGroup() !=
"RSA")
527 global::Interop.BCrypt.KeyBlobMagicNumber magic = (global::Interop.BCrypt.KeyBlobMagicNumber)
BitConverter.
ToInt32(
rsaBlob, 0);
529 if (
rsaBlob.Length <
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB))
531 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
535 global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*
ptr2 = (global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB*)
ptr;
536 int offset =
sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB);
555 if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAFULLPRIVATE_MAGIC)
560 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)
581 string name = hashAlgorithm.
Name;
582 if (
string.IsNullOrEmpty(name))
599 switch (padding.
Mode)
603 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO
bCRYPT_PKCS1_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
604 bCRYPT_PKCS1_PADDING_INFO.pszAlgId =
intPtr;
610 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO
bCRYPT_PSS_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
611 bCRYPT_PSS_PADDING_INFO.pszAlgId =
intPtr;
612 bCRYPT_PSS_PADDING_INFO.cbSalt =
hash.Length;
628 string name = hashAlgorithm.
Name;
629 if (
string.IsNullOrEmpty(name))
645 switch (padding.
Mode)
649 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO
bCRYPT_PKCS1_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
650 bCRYPT_PKCS1_PADDING_INFO.pszAlgId =
intPtr;
656 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO
bCRYPT_PSS_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
657 bCRYPT_PSS_PADDING_INFO.pszAlgId =
intPtr;
658 bCRYPT_PSS_PADDING_INFO.cbSalt =
hash.Length;
687 string name = hashAlgorithm.
Name;
688 if (
string.IsNullOrEmpty(name))
704 switch (padding.
Mode)
708 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO
bCRYPT_PKCS1_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO);
709 bCRYPT_PKCS1_PADDING_INFO.pszAlgId =
intPtr;
715 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO
bCRYPT_PSS_PADDING_INFO =
default(global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO);
716 bCRYPT_PSS_PADDING_INFO.pszAlgId =
intPtr;
717 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_ArgRSARequiresRSAKey
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 CngAlgorithmGroup Rsa
bool TryExportKeyBlob(string blobType, Span< byte > destination, out int bytesWritten)
byte[] Export(CngKeyBlobFormat format)
byte[] ExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount)
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
bool TryExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten)
SafeNCryptKeyHandle Handle
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)
override bool TryEncrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, out int bytesWritten)
override void Dispose(bool disposing)
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)
static readonly CngKeyBlobFormat s_rsaPublicBlob
override bool TryExportPkcs8PrivateKey(Span< byte > destination, out int bytesWritten)
byte[] ExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount)
static int GetHashSizeInBytes(HashAlgorithmName hashAlgorithm)
unsafe override void ImportParameters(RSAParameters parameters)
void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
void ProcessPkcs8Response(System.Security.Cryptography.CngPkcs8.Pkcs8Response response)
static readonly CngKeyBlobFormat s_rsaFullPrivateBlob
void AcceptImport(System.Security.Cryptography.CngPkcs8.Pkcs8Response response)
bool TryExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount, Span< byte > destination, out int bytesWritten)
void ForceSetKeySize(int newKeySize)
override RSAParameters ExportParameters(bool includePrivateParameters)
override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
override bool TryDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, out int bytesWritten)
override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
static readonly CngKeyBlobFormat s_rsaPrivateBlob
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
unsafe override bool VerifyHash(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
static unsafe void ExportParameters(ref RSAParameters rsaParams, byte[] rsaBlob, bool includePrivateParameters)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
override KeySizes[] LegalKeySizes
unsafe byte[] EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt)
override void ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
unsafe bool TryEncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, Span< byte > output, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt, out int bytesWritten)
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
unsafe override bool TrySignHash(ReadOnlySpan< byte > hash, Span< byte > destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
static readonly ConcurrentDictionary< HashAlgorithmName, int > s_hashSizes
override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding, bool encrypt)
byte[] ExportKeyBlob(bool includePrivateParameters)
static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
unsafe override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
override bool TryHashData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
unsafe bool TryEncryptOrDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
RSAEncryptionPaddingMode Mode
static RSAEncryptionPadding Pkcs1
HashAlgorithmName OaepHashAlgorithm
RSASignaturePaddingMode Mode
static void PadPkcs1Encryption(ReadOnlySpan< byte > source, Span< byte > destination)
static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
static int BytesRequiredForBitCount(int keySizeInBits)
CngKey GetOrGenerateKey(int keySize, CngAlgorithm algorithm)
static CngKey Duplicate(CngKey key)
static readonly IntPtr Zero
static HashAlgorithmName SHA512
static HashAlgorithmName SHA256
static HashAlgorithmName SHA384