Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RSACng.cs
Go to the documentation of this file.
3using System.IO;
7
9
10public sealed class RSACng : RSA
11{
12 private CngAlgorithmCore _core = new CngAlgorithmCore("RSACng");
13
14 private static readonly CngKeyBlobFormat s_rsaFullPrivateBlob = new CngKeyBlobFormat("RSAFULLPRIVATEBLOB");
15
16 private static readonly CngKeyBlobFormat s_rsaPrivateBlob = new CngKeyBlobFormat("RSAPRIVATEBLOB");
17
18 private static readonly CngKeyBlobFormat s_rsaPublicBlob = new CngKeyBlobFormat("RSAPUBLICBLOB");
19
21 {
25 });
26
27 public CngKey Key
28 {
29 get
30 {
32 }
33 private set
34 {
35 if (value.AlgorithmGroup != CngAlgorithmGroup.Rsa)
36 {
38 }
40 ForceSetKeySize(value.KeySize);
41 }
42 }
43
44 public override KeySizes[] LegalKeySizes => new KeySizes[1]
45 {
46 new KeySizes(512, 16384, 64)
47 };
48
50 {
51 if (key == null)
52 {
53 throw new ArgumentNullException("key");
54 }
55 if (key.AlgorithmGroup != CngAlgorithmGroup.Rsa)
56 {
58 }
60 }
61
62 protected override void Dispose(bool disposing)
63 {
64 _core.Dispose();
65 }
66
67 private void ThrowIfDisposed()
68 {
70 }
71
72 private void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
73 {
76 cngKey.ExportPolicy |= CngExportPolicies.AllowPlaintextExport;
77 Key = cngKey;
78 }
79
84
89
91 {
92 return Key.TryExportKeyBlob("PKCS8_PRIVATEKEY", destination, out bytesWritten);
93 }
94
99
104
106 {
107 return Key.Handle;
108 }
109
110 public RSACng()
111 : this(2048)
112 {
113 }
114
115 public RSACng(int keySize)
116 {
118 }
119
120 protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
121 {
122 return Internal.Cryptography.CngCommon.HashData(data, offset, count, hashAlgorithm);
123 }
124
125 protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
126 {
128 }
129
130 protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
131 {
132 return Internal.Cryptography.CngCommon.HashData(data, hashAlgorithm);
133 }
134
135 private void ForceSetKeySize(int newKeySize)
136 {
138 }
139
140 public override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
141 {
142 return EncryptOrDecrypt(data, padding, encrypt: true);
143 }
144
145 public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
146 {
147 return EncryptOrDecrypt(data, padding, encrypt: false);
148 }
149
151 {
152 return TryEncryptOrDecrypt(data, destination, padding, encrypt: true, out bytesWritten);
153 }
154
156 {
157 return TryEncryptOrDecrypt(data, destination, padding, encrypt: false, out bytesWritten);
158 }
159
160 private unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding, bool encrypt)
161 {
162 if (data == null)
163 {
164 throw new ArgumentNullException("data");
165 }
166 if (padding == null)
167 {
168 throw new ArgumentNullException("padding");
169 }
171 if (!encrypt && data.Length != num)
172 {
174 }
175 if (encrypt && padding.Mode == RSAEncryptionPaddingMode.Pkcs1 && data.Length > num - 11)
176 {
178 }
180 if (encrypt && data.Length == 0)
181 {
183 Span<byte> span = new Span<byte>(array, 0, num);
184 try
185 {
186 if (padding == RSAEncryptionPadding.Pkcs1)
187 {
189 }
190 else
191 {
192 if (padding.Mode != RSAEncryptionPaddingMode.Oaep)
193 {
195 }
197 rsaPaddingProcessor.PadOaep(data, span);
198 }
199 return EncryptOrDecrypt(key, span, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_NO_PADDING_FLAG, null, encrypt);
200 }
201 finally
202 {
205 }
206 }
207 switch (padding.Mode)
208 {
209 case RSAEncryptionPaddingMode.Pkcs1:
210 return EncryptOrDecrypt(key, data, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt);
211 case RSAEncryptionPaddingMode.Oaep:
212 {
214 try
215 {
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;
220 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO2 = bCRYPT_OAEP_PADDING_INFO;
221 return EncryptOrDecrypt(key, data, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &bCRYPT_OAEP_PADDING_INFO2, encrypt);
222 }
223 finally
224 {
226 }
227 }
228 default:
230 }
231 }
232
234 {
235 if (padding == null)
236 {
237 throw new ArgumentNullException("padding");
238 }
240 if (!encrypt && data.Length != num)
241 {
243 }
244 if (encrypt && padding.Mode == RSAEncryptionPaddingMode.Pkcs1 && data.Length > num - 11)
245 {
247 }
249 if (encrypt && data.Length == 0)
250 {
252 Span<byte> span = new Span<byte>(array, 0, num);
253 try
254 {
255 if (padding == RSAEncryptionPadding.Pkcs1)
256 {
258 }
259 else
260 {
261 if (padding.Mode != RSAEncryptionPaddingMode.Oaep)
262 {
264 }
266 rsaPaddingProcessor.PadOaep(data, span);
267 }
268 return TryEncryptOrDecrypt(key, span, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_NO_PADDING_FLAG, null, encrypt, out bytesWritten);
269 }
270 finally
271 {
274 }
275 }
276 switch (padding.Mode)
277 {
278 case RSAEncryptionPaddingMode.Pkcs1:
279 return TryEncryptOrDecrypt(key, data, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt, out bytesWritten);
280 case RSAEncryptionPaddingMode.Oaep:
281 {
283 try
284 {
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;
289 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO2 = bCRYPT_OAEP_PADDING_INFO;
290 return TryEncryptOrDecrypt(key, data, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &bCRYPT_OAEP_PADDING_INFO2, encrypt, out bytesWritten);
291 }
292 finally
293 {
295 }
296 }
297 default:
299 }
300 }
301
302 private unsafe byte[] EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan<byte> input, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void* paddingInfo, bool encrypt)
303 {
304 int num = KeySize / 8;
305 byte[] array = new byte[num];
306 int bytesNeeded = 0;
307 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS;
308 for (int i = 0; i <= 1; i++)
309 {
311 if (errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
312 {
313 break;
314 }
315 }
316 if (errorCode == global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL)
317 {
319 array = new byte[bytesNeeded];
320 for (int j = 0; j <= 1; j++)
321 {
323 if (errorCode != global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
324 {
325 break;
326 }
327 }
328 }
329 if (errorCode != 0)
330 {
331 throw errorCode.ToCryptographicException();
332 }
333 if (bytesNeeded != array.Length)
334 {
335 byte[] array2 = array.AsSpan(0, bytesNeeded).ToArray();
337 array = array2;
338 }
339 return array;
340 }
341
342 private unsafe bool TryEncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan<byte> input, Span<byte> output, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void* paddingInfo, bool encrypt, out int bytesWritten)
343 {
344 for (int i = 0; i <= 1; i++)
345 {
346 int bytesNeeded;
347 global::Interop.NCrypt.ErrorCode errorCode = EncryptOrDecrypt(key, input, output, paddingMode, paddingInfo, encrypt, out bytesNeeded);
348 switch (errorCode)
349 {
350 case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
352 return true;
353 case global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL:
354 bytesWritten = 0;
355 return false;
356 default:
357 throw errorCode.ToCryptographicException();
358 case global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL:
359 break;
360 }
361 }
362 throw global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL.ToCryptographicException();
363 }
364
365 private unsafe static 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)
366 {
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));
368 if (errorCode == global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS && bytesNeeded > output.Length)
369 {
370 errorCode = global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL;
371 }
372 return errorCode;
373 }
374
375 public unsafe override void ImportParameters(RSAParameters parameters)
376 {
377 if (parameters.Exponent == null || parameters.Modulus == null)
378 {
380 }
381 bool flag;
382 if (parameters.D == null)
383 {
384 flag = false;
385 if (parameters.P != null || parameters.DP != null || parameters.Q != null || parameters.DQ != null || parameters.InverseQ != null)
386 {
388 }
389 }
390 else
391 {
392 flag = true;
393 if (parameters.P == null || parameters.DP == null || parameters.Q == null || parameters.DQ == null || parameters.InverseQ == null)
394 {
396 }
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)
399 {
401 }
402 }
403 int num2 = sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB) + parameters.Exponent.Length + parameters.Modulus.Length;
404 if (flag)
405 {
406 num2 += parameters.P.Length + parameters.Q.Length;
407 }
408 byte[] array = new byte[num2];
409 fixed (byte* ptr = &array[0])
410 {
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;
414 ptr2->cbPublicExp = parameters.Exponent.Length;
415 ptr2->cbModulus = parameters.Modulus.Length;
416 if (flag)
417 {
418 ptr2->cbPrime1 = parameters.P.Length;
419 ptr2->cbPrime2 = parameters.Q.Length;
420 }
421 int offset = sizeof(global::Interop.BCrypt.BCRYPT_RSAKEY_BLOB);
422 global::Interop.BCrypt.Emit(array, ref offset, parameters.Exponent);
423 global::Interop.BCrypt.Emit(array, ref offset, parameters.Modulus);
424 if (flag)
425 {
426 global::Interop.BCrypt.Emit(array, ref offset, parameters.P);
427 global::Interop.BCrypt.Emit(array, ref offset, parameters.Q);
428 }
429 }
430 ImportKeyBlob(array, flag);
431 }
432
441
450
459
461 {
462 if (response.GetAlgorithmGroup() != "RSA")
463 {
464 response.FreeKey();
466 }
468 }
469
478
492
502
516
524
526 {
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))
530 {
531 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
532 }
533 fixed (byte* ptr = &rsaBlob[0])
534 {
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);
537 rsaParams.Exponent = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPublicExp);
538 rsaParams.Modulus = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbModulus);
540 {
541 rsaParams.P = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPrime1);
542 rsaParams.Q = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPrime2);
543 rsaParams.DP = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPrime1);
544 rsaParams.DQ = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPrime2);
545 rsaParams.InverseQ = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbPrime1);
546 rsaParams.D = global::Interop.BCrypt.Consume(rsaBlob, ref offset, ptr2->cbModulus);
547 }
548 }
549 }
550
551 private static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
552 {
554 {
555 if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAPRIVATE_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_RSAFULLPRIVATE_MAGIC)
556 {
558 }
559 }
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)
561 {
563 }
564 }
565
566 private static int GetHashSizeInBytes(HashAlgorithmName hashAlgorithm)
567 {
568 return s_hashSizes.GetOrAdd(hashAlgorithm, delegate(HashAlgorithmName hashAlgorithm)
569 {
571 return hashProviderCng.HashSizeInBytes;
572 });
573 }
574
575 public unsafe override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
576 {
577 if (hash == null)
578 {
579 throw new ArgumentNullException("hash");
580 }
581 string name = hashAlgorithm.Name;
582 if (string.IsNullOrEmpty(name))
583 {
585 }
586 if (padding == null)
587 {
588 throw new ArgumentNullException("padding");
589 }
590 if (hash.Length != GetHashSizeInBytes(hashAlgorithm))
591 {
593 }
596 try
597 {
598 int estimatedSize = KeySize / 8;
599 switch (padding.Mode)
600 {
601 case RSASignaturePaddingMode.Pkcs1:
602 {
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;
605 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO bCRYPT_PKCS1_PADDING_INFO2 = bCRYPT_PKCS1_PADDING_INFO;
606 return keyHandle.SignHash(hash, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, &bCRYPT_PKCS1_PADDING_INFO2, estimatedSize);
607 }
609 {
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;
613 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO bCRYPT_PSS_PADDING_INFO2 = bCRYPT_PSS_PADDING_INFO;
614 return keyHandle.SignHash(hash, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PSS_FLAG, &bCRYPT_PSS_PADDING_INFO2, estimatedSize);
615 }
616 default:
618 }
619 }
620 finally
621 {
623 }
624 }
625
627 {
628 string name = hashAlgorithm.Name;
629 if (string.IsNullOrEmpty(name))
630 {
632 }
633 if (padding == null)
634 {
635 throw new ArgumentNullException("padding");
636 }
638 if (hash.Length != GetHashSizeInBytes(hashAlgorithm))
639 {
641 }
643 try
644 {
645 switch (padding.Mode)
646 {
647 case RSASignaturePaddingMode.Pkcs1:
648 {
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;
651 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO bCRYPT_PKCS1_PADDING_INFO2 = bCRYPT_PKCS1_PADDING_INFO;
652 return keyHandle.TrySignHash(hash, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, &bCRYPT_PKCS1_PADDING_INFO2, out bytesWritten);
653 }
655 {
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;
659 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO bCRYPT_PSS_PADDING_INFO2 = bCRYPT_PSS_PADDING_INFO;
660 return keyHandle.TrySignHash(hash, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PSS_FLAG, &bCRYPT_PSS_PADDING_INFO2, out bytesWritten);
661 }
662 default:
664 }
665 }
666 finally
667 {
669 }
670 }
671
672 public override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
673 {
674 if (hash == null)
675 {
676 throw new ArgumentNullException("hash");
677 }
678 if (signature == null)
679 {
680 throw new ArgumentNullException("signature");
681 }
682 return VerifyHash((ReadOnlySpan<byte>)hash, (ReadOnlySpan<byte>)signature, hashAlgorithm, padding);
683 }
684
686 {
687 string name = hashAlgorithm.Name;
688 if (string.IsNullOrEmpty(name))
689 {
691 }
692 if (padding == null)
693 {
694 throw new ArgumentNullException("padding");
695 }
697 if (hash.Length != GetHashSizeInBytes(hashAlgorithm))
698 {
699 return false;
700 }
702 try
703 {
704 switch (padding.Mode)
705 {
706 case RSASignaturePaddingMode.Pkcs1:
707 {
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;
710 global::Interop.BCrypt.BCRYPT_PKCS1_PADDING_INFO bCRYPT_PKCS1_PADDING_INFO2 = bCRYPT_PKCS1_PADDING_INFO;
711 return keyHandle.VerifyHash(hash, signature, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, &bCRYPT_PKCS1_PADDING_INFO2);
712 }
714 {
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;
718 global::Interop.BCrypt.BCRYPT_PSS_PADDING_INFO bCRYPT_PSS_PADDING_INFO2 = bCRYPT_PSS_PADDING_INFO;
719 return keyHandle.VerifyHash(hash, signature, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PSS_FLAG, &bCRYPT_PSS_PADDING_INFO2);
720 }
721 default:
723 }
724 }
725 finally
726 {
728 }
729 }
730}
static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Definition CngCommon.cs:10
static bool TryHashData(ReadOnlySpan< byte > source, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
Definition CngCommon.cs:17
static int ToInt32(byte[] value, int startIndex)
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static unsafe IntPtr StringToHGlobalUni(string? s)
Definition Marshal.cs:1310
static string Cryptography_ArgRSARequiresRSAKey
Definition SR.cs:38
static string Cryptography_NotValidPrivateKey
Definition SR.cs:120
static string Cryptography_RSA_DecryptWrongSize
Definition SR.cs:138
static string Cryptography_InvalidRsaParameters
Definition SR.cs:108
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_NotValidPublicOrPrivateKey
Definition SR.cs:122
static string Cryptography_UnsupportedPaddingMode
Definition SR.cs:160
static string Cryptography_SignHash_WrongSize
Definition SR.cs:142
static string Cryptography_Encryption_MessageTooLong
Definition SR.cs:52
static string Cryptography_HashAlgorithmNameNullOrEmpty
Definition SR.cs:60
Definition SR.cs:7
bool TryExportKeyBlob(string blobType, Span< byte > destination, out int bytesWritten)
Definition CngKey.cs:643
byte[] Export(CngKeyBlobFormat format)
Definition CngKey.cs:622
byte[] ExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount)
Definition CngKey.cs:665
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
Definition CngKey.cs:525
bool TryExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten)
Definition CngKey.cs:673
SafeNCryptKeyHandle Handle
Definition CngKey.cs:51
static bool IsPlatformScheme(PbeParameters pbeParameters)
Definition CngPkcs8.cs:44
static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:134
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
Definition CngPkcs8.cs:53
static Pkcs8Response ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:90
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition CngPkcs8.cs:68
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
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)
Definition RSACng.cs:150
override void Dispose(bool disposing)
Definition RSACng.cs:62
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)
Definition RSACng.cs:365
static readonly CngKeyBlobFormat s_rsaPublicBlob
Definition RSACng.cs:18
override bool TryExportPkcs8PrivateKey(Span< byte > destination, out int bytesWritten)
Definition RSACng.cs:90
byte[] ExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount)
Definition RSACng.cs:95
static int GetHashSizeInBytes(HashAlgorithmName hashAlgorithm)
Definition RSACng.cs:566
unsafe override void ImportParameters(RSAParameters parameters)
Definition RSACng.cs:375
void ImportKeyBlob(byte[] rsaBlob, bool includePrivate)
Definition RSACng.cs:72
void ProcessPkcs8Response(System.Security.Cryptography.CngPkcs8.Pkcs8Response response)
Definition RSACng.cs:460
static readonly CngKeyBlobFormat s_rsaFullPrivateBlob
Definition RSACng.cs:14
void AcceptImport(System.Security.Cryptography.CngPkcs8.Pkcs8Response response)
Definition RSACng.cs:80
bool TryExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount, Span< byte > destination, out int bytesWritten)
Definition RSACng.cs:100
void ForceSetKeySize(int newKeySize)
Definition RSACng.cs:135
override RSAParameters ExportParameters(bool includePrivateParameters)
Definition RSACng.cs:517
override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Definition RSACng.cs:672
override bool TryDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, out int bytesWritten)
Definition RSACng.cs:155
override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
Definition RSACng.cs:130
static readonly CngKeyBlobFormat s_rsaPrivateBlob
Definition RSACng.cs:16
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
Definition RSACng.cs:451
unsafe override bool VerifyHash(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Definition RSACng.cs:685
static unsafe void ExportParameters(ref RSAParameters rsaParams, byte[] rsaBlob, bool includePrivateParameters)
Definition RSACng.cs:525
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition RSACng.cs:493
override byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
Definition RSACng.cs:140
override KeySizes[] LegalKeySizes
Definition RSACng.cs:44
unsafe byte[] EncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt)
Definition RSACng.cs:302
override void ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition RSACng.cs:433
unsafe bool TryEncryptOrDecrypt(SafeNCryptKeyHandle key, ReadOnlySpan< byte > input, Span< byte > output, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *paddingInfo, bool encrypt, out int bytesWritten)
Definition RSACng.cs:342
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
Definition RSACng.cs:470
unsafe override bool TrySignHash(ReadOnlySpan< byte > hash, Span< byte > destination, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding, out int bytesWritten)
Definition RSACng.cs:626
static readonly ConcurrentDictionary< HashAlgorithmName, int > s_hashSizes
Definition RSACng.cs:20
override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
Definition RSACng.cs:145
unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding, bool encrypt)
Definition RSACng.cs:160
byte[] ExportKeyBlob(bool includePrivateParameters)
Definition RSACng.cs:85
static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
Definition RSACng.cs:551
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters)
Definition RSACng.cs:479
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
Definition RSACng.cs:442
unsafe override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
Definition RSACng.cs:575
override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Definition RSACng.cs:120
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition RSACng.cs:503
override bool TryHashData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
Definition RSACng.cs:125
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
Definition RSACng.cs:105
unsafe bool TryEncryptOrDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
Definition RSACng.cs:233
static void PadPkcs1Encryption(ReadOnlySpan< byte > source, Span< byte > destination)
static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
CngKey GetOrGenerateKey(int keySize, CngAlgorithm algorithm)
static readonly IntPtr Zero
Definition IntPtr.cs:18