Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ECDsa.cs
Go to the documentation of this file.
4using System.IO;
7
9
10[UnsupportedOSPlatform("browser")]
11public abstract class ECDsa : AsymmetricAlgorithm
12{
13 private static readonly string[] s_validOids = new string[1] { "1.2.840.10045.2.1" };
14
15 public override string? KeyExchangeAlgorithm => null;
16
17 public override string SignatureAlgorithm => "ECDsa";
18
19 [RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
20 public new static ECDsa? Create(string algorithm)
21 {
22 if (algorithm == null)
23 {
24 throw new ArgumentNullException("algorithm");
25 }
26 return CryptoConfig.CreateFromName(algorithm) as ECDsa;
27 }
28
29 public virtual ECParameters ExportParameters(bool includePrivateParameters)
30 {
32 }
33
34 public virtual ECParameters ExportExplicitParameters(bool includePrivateParameters)
35 {
37 }
38
39 public virtual void ImportParameters(ECParameters parameters)
40 {
42 }
43
44 public virtual void GenerateKey(ECCurve curve)
45 {
47 }
48
49 public virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm)
50 {
51 if (data == null)
52 {
53 throw new ArgumentNullException("data");
54 }
55 return SignData(data, 0, data.Length, hashAlgorithm);
56 }
57
58 public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
59 {
60 if (data == null)
61 {
62 throw new ArgumentNullException("data");
63 }
64 if (offset < 0 || offset > data.Length)
65 {
66 throw new ArgumentOutOfRangeException("offset");
67 }
68 if (count < 0 || count > data.Length - offset)
69 {
70 throw new ArgumentOutOfRangeException("count");
71 }
72 if (string.IsNullOrEmpty(hashAlgorithm.Name))
73 {
75 }
76 byte[] hash = HashData(data, offset, count, hashAlgorithm);
77 return SignHash(hash);
78 }
79
80 public byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
81 {
82 if (data == null)
83 {
84 throw new ArgumentNullException("data");
85 }
86 if (offset < 0 || offset > data.Length)
87 {
88 throw new ArgumentOutOfRangeException("offset");
89 }
90 if (count < 0 || count > data.Length - offset)
91 {
92 throw new ArgumentOutOfRangeException("count");
93 }
94 if (string.IsNullOrEmpty(hashAlgorithm.Name))
95 {
97 }
98 if (!signatureFormat.IsKnownValue())
99 {
101 }
102 return SignDataCore(new ReadOnlySpan<byte>(data, offset, count), hashAlgorithm, signatureFormat);
103 }
104
105 protected virtual byte[] SignDataCore(ReadOnlySpan<byte> data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
106 {
107 Span<byte> destination = stackalloc byte[256];
108 int maxSignatureSize = GetMaxSignatureSize(signatureFormat);
109 byte[] array = null;
110 bool flag = false;
111 int bytesWritten = 0;
112 if (maxSignatureSize > destination.Length)
113 {
114 array = ArrayPool<byte>.Shared.Rent(maxSignatureSize);
116 }
117 try
118 {
119 if (!TrySignDataCore(data, destination, hashAlgorithm, signatureFormat, out bytesWritten))
120 {
121 throw new CryptographicException();
122 }
123 byte[] result = destination.Slice(0, bytesWritten).ToArray();
124 flag = true;
125 return result;
126 }
127 finally
128 {
129 if (array != null)
130 {
131 CryptographicOperations.ZeroMemory(array.AsSpan(0, bytesWritten));
132 if (flag)
133 {
135 }
136 }
137 }
138 }
139
140 public byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
141 {
142 if (data == null)
143 {
144 throw new ArgumentNullException("data");
145 }
146 if (string.IsNullOrEmpty(hashAlgorithm.Name))
147 {
149 }
150 if (!signatureFormat.IsKnownValue())
151 {
153 }
154 return SignDataCore(data, hashAlgorithm, signatureFormat);
155 }
156
157 public byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
158 {
159 if (data == null)
160 {
161 throw new ArgumentNullException("data");
162 }
163 if (string.IsNullOrEmpty(hashAlgorithm.Name))
164 {
166 }
167 if (!signatureFormat.IsKnownValue())
168 {
170 }
171 return SignDataCore(data, hashAlgorithm, signatureFormat);
172 }
173
174 protected virtual byte[] SignDataCore(Stream data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
175 {
176 byte[] array = HashData(data, hashAlgorithm);
177 return SignHashCore(array, signatureFormat);
178 }
179
180 public byte[] SignHash(byte[] hash, DSASignatureFormat signatureFormat)
181 {
182 if (hash == null)
183 {
184 throw new ArgumentNullException("hash");
185 }
186 if (!signatureFormat.IsKnownValue())
187 {
189 }
190 return SignHashCore(hash, signatureFormat);
191 }
192
193 protected virtual byte[] SignHashCore(ReadOnlySpan<byte> hash, DSASignatureFormat signatureFormat)
194 {
195 Span<byte> destination = stackalloc byte[256];
196 int maxSignatureSize = GetMaxSignatureSize(signatureFormat);
197 byte[] array = null;
198 bool flag = false;
199 int bytesWritten = 0;
200 if (maxSignatureSize > destination.Length)
201 {
202 array = ArrayPool<byte>.Shared.Rent(maxSignatureSize);
204 }
205 try
206 {
207 if (!TrySignHashCore(hash, destination, signatureFormat, out bytesWritten))
208 {
209 throw new CryptographicException();
210 }
211 byte[] result = destination.Slice(0, bytesWritten).ToArray();
212 flag = true;
213 return result;
214 }
215 finally
216 {
217 if (array != null)
218 {
219 CryptographicOperations.ZeroMemory(array.AsSpan(0, bytesWritten));
220 if (flag)
221 {
223 }
224 }
225 }
226 }
227
228 public virtual bool TrySignData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
229 {
230 if (string.IsNullOrEmpty(hashAlgorithm.Name))
231 {
233 }
234 Span<byte> tmp = stackalloc byte[128];
235 ReadOnlySpan<byte> hash = HashSpanToTmp(data, hashAlgorithm, tmp);
236 return TrySignHash(hash, destination, out bytesWritten);
237 }
238
239 public bool TrySignData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat, out int bytesWritten)
240 {
241 if (string.IsNullOrEmpty(hashAlgorithm.Name))
242 {
244 }
245 if (!signatureFormat.IsKnownValue())
246 {
248 }
249 return TrySignDataCore(data, destination, hashAlgorithm, signatureFormat, out bytesWritten);
250 }
251
252 protected virtual bool TrySignDataCore(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat, out int bytesWritten)
253 {
254 Span<byte> tmp = stackalloc byte[128];
255 ReadOnlySpan<byte> hash = HashSpanToTmp(data, hashAlgorithm, tmp);
256 return TrySignHashCore(hash, destination, signatureFormat, out bytesWritten);
257 }
258
259 public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm)
260 {
261 if (data == null)
262 {
263 throw new ArgumentNullException("data");
264 }
265 if (string.IsNullOrEmpty(hashAlgorithm.Name))
266 {
268 }
269 byte[] hash = HashData(data, hashAlgorithm);
270 return SignHash(hash);
271 }
272
273 public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm)
274 {
275 if (data == null)
276 {
277 throw new ArgumentNullException("data");
278 }
279 return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
280 }
281
282 public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm)
283 {
284 if (data == null)
285 {
286 throw new ArgumentNullException("data");
287 }
288 if (offset < 0 || offset > data.Length)
289 {
290 throw new ArgumentOutOfRangeException("offset");
291 }
292 if (count < 0 || count > data.Length - offset)
293 {
294 throw new ArgumentOutOfRangeException("count");
295 }
296 if (signature == null)
297 {
298 throw new ArgumentNullException("signature");
299 }
300 if (string.IsNullOrEmpty(hashAlgorithm.Name))
301 {
303 }
304 byte[] hash = HashData(data, offset, count, hashAlgorithm);
305 return VerifyHash(hash, signature);
306 }
307
308 public bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
309 {
310 if (data == null)
311 {
312 throw new ArgumentNullException("data");
313 }
314 if (offset < 0 || offset > data.Length)
315 {
316 throw new ArgumentOutOfRangeException("offset");
317 }
318 if (count < 0 || count > data.Length - offset)
319 {
320 throw new ArgumentOutOfRangeException("count");
321 }
322 if (signature == null)
323 {
324 throw new ArgumentNullException("signature");
325 }
326 if (string.IsNullOrEmpty(hashAlgorithm.Name))
327 {
329 }
330 if (!signatureFormat.IsKnownValue())
331 {
333 }
334 return VerifyDataCore(new ReadOnlySpan<byte>(data, offset, count), signature, hashAlgorithm, signatureFormat);
335 }
336
337 public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
338 {
339 if (data == null)
340 {
341 throw new ArgumentNullException("data");
342 }
343 if (signature == null)
344 {
345 throw new ArgumentNullException("signature");
346 }
347 if (string.IsNullOrEmpty(hashAlgorithm.Name))
348 {
350 }
351 if (!signatureFormat.IsKnownValue())
352 {
354 }
355 return VerifyDataCore(data, signature, hashAlgorithm, signatureFormat);
356 }
357
358 public virtual bool VerifyData(ReadOnlySpan<byte> data, ReadOnlySpan<byte> signature, HashAlgorithmName hashAlgorithm)
359 {
360 if (string.IsNullOrEmpty(hashAlgorithm.Name))
361 {
363 }
364 Span<byte> tmp = stackalloc byte[128];
365 ReadOnlySpan<byte> hash = HashSpanToTmp(data, hashAlgorithm, tmp);
366 return VerifyHash(hash, signature);
367 }
368
369 public bool VerifyData(ReadOnlySpan<byte> data, ReadOnlySpan<byte> signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
370 {
371 if (string.IsNullOrEmpty(hashAlgorithm.Name))
372 {
374 }
375 if (!signatureFormat.IsKnownValue())
376 {
378 }
379 return VerifyDataCore(data, signature, hashAlgorithm, signatureFormat);
380 }
381
382 protected virtual bool VerifyDataCore(ReadOnlySpan<byte> data, ReadOnlySpan<byte> signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
383 {
384 Span<byte> span = stackalloc byte[64];
385 span = ((!TryHashData(data, span, hashAlgorithm, out var bytesWritten)) ? ((Span<byte>)HashData(data.ToArray(), 0, data.Length, hashAlgorithm)) : span.Slice(0, bytesWritten));
386 return VerifyHashCore(span, signature, signatureFormat);
387 }
388
389 public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm)
390 {
391 if (data == null)
392 {
393 throw new ArgumentNullException("data");
394 }
395 if (signature == null)
396 {
397 throw new ArgumentNullException("signature");
398 }
399 if (string.IsNullOrEmpty(hashAlgorithm.Name))
400 {
402 }
403 byte[] hash = HashData(data, hashAlgorithm);
404 return VerifyHash(hash, signature);
405 }
406
407 public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
408 {
409 if (data == null)
410 {
411 throw new ArgumentNullException("data");
412 }
413 if (signature == null)
414 {
415 throw new ArgumentNullException("signature");
416 }
417 if (string.IsNullOrEmpty(hashAlgorithm.Name))
418 {
420 }
421 if (!signatureFormat.IsKnownValue())
422 {
424 }
425 return VerifyDataCore(data, signature, hashAlgorithm, signatureFormat);
426 }
427
428 protected virtual bool VerifyDataCore(Stream data, ReadOnlySpan<byte> signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
429 {
430 byte[] array = HashData(data, hashAlgorithm);
431 return VerifyHashCore(array, signature, signatureFormat);
432 }
433
434 public abstract byte[] SignHash(byte[] hash);
435
436 public abstract bool VerifyHash(byte[] hash, byte[] signature);
437
438 protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
439 {
441 }
442
443 protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
444 {
446 }
447
448 protected virtual bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
449 {
450 byte[] array = ArrayPool<byte>.Shared.Rent(data.Length);
451 bool flag = false;
452 try
453 {
454 data.CopyTo(array);
455 byte[] array2 = HashData(array, 0, data.Length, hashAlgorithm);
456 flag = true;
457 if (array2.Length <= destination.Length)
458 {
460 bytesWritten = array2.Length;
461 return true;
462 }
463 bytesWritten = 0;
464 return false;
465 }
466 finally
467 {
468 Array.Clear(array, 0, data.Length);
469 if (flag)
470 {
472 }
473 }
474 }
475
476 public virtual bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination, out int bytesWritten)
477 {
478 return TrySignHashCore(hash, destination, DSASignatureFormat.IeeeP1363FixedFieldConcatenation, out bytesWritten);
479 }
480
481 public bool TrySignHash(ReadOnlySpan<byte> hash, Span<byte> destination, DSASignatureFormat signatureFormat, out int bytesWritten)
482 {
483 if (!signatureFormat.IsKnownValue())
484 {
486 }
487 return TrySignHashCore(hash, destination, signatureFormat, out bytesWritten);
488 }
489
490 protected virtual bool TrySignHashCore(ReadOnlySpan<byte> hash, Span<byte> destination, DSASignatureFormat signatureFormat, out int bytesWritten)
491 {
492 byte[] signature = SignHash(hash.ToArray());
493 byte[] array = AsymmetricAlgorithmHelpers.ConvertFromIeeeP1363Signature(signature, signatureFormat);
495 }
496
497 public virtual bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature)
498 {
499 return VerifyHashCore(hash, signature, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
500 }
501
502 public bool VerifyHash(byte[] hash, byte[] signature, DSASignatureFormat signatureFormat)
503 {
504 if (hash == null)
505 {
506 throw new ArgumentNullException("hash");
507 }
508 if (signature == null)
509 {
510 throw new ArgumentNullException("signature");
511 }
512 if (!signatureFormat.IsKnownValue())
513 {
515 }
516 return VerifyHashCore(hash, signature, signatureFormat);
517 }
518
519 public bool VerifyHash(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, DSASignatureFormat signatureFormat)
520 {
521 if (!signatureFormat.IsKnownValue())
522 {
524 }
525 return VerifyHashCore(hash, signature, signatureFormat);
526 }
527
528 protected virtual bool VerifyHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, DSASignatureFormat signatureFormat)
529 {
530 byte[] array = this.ConvertSignatureToIeeeP1363(signatureFormat, signature);
531 if (array == null)
532 {
533 return false;
534 }
535 return VerifyHash(hash.ToArray(), array);
536 }
537
539 {
540 if (TryHashData(data, tmp, hashAlgorithm, out var bytesWritten))
541 {
542 return tmp.Slice(0, bytesWritten);
543 }
544 return HashSpanToArray(data, hashAlgorithm);
545 }
546
547 private byte[] HashSpanToArray(ReadOnlySpan<byte> data, HashAlgorithmName hashAlgorithm)
548 {
549 byte[] array = ArrayPool<byte>.Shared.Rent(data.Length);
550 bool flag = false;
551 try
552 {
553 data.CopyTo(array);
554 byte[] result = HashData(array, 0, data.Length, hashAlgorithm);
555 flag = true;
556 return result;
557 }
558 finally
559 {
560 Array.Clear(array, 0, data.Length);
561 if (flag)
562 {
564 }
565 }
566 }
567
568 public unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
569 {
570 if (pbeParameters == null)
571 {
572 throw new ArgumentNullException("pbeParameters");
573 }
575 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
576 fixed (byte* ptr = ecParameters.D)
577 {
578 try
579 {
580 AsnWriter pkcs8Writer = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
581 AsnWriter asnWriter = KeyFormatHelper.WriteEncryptedPkcs8(passwordBytes, pkcs8Writer, pbeParameters);
582 return asnWriter.TryEncode(destination, out bytesWritten);
583 }
584 finally
585 {
587 }
588 }
589 }
590
591 public unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
592 {
593 if (pbeParameters == null)
594 {
595 throw new ArgumentNullException("pbeParameters");
596 }
598 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
599 fixed (byte* ptr = ecParameters.D)
600 {
601 try
602 {
603 AsnWriter pkcs8Writer = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
604 AsnWriter asnWriter = KeyFormatHelper.WriteEncryptedPkcs8(password, pkcs8Writer, pbeParameters);
605 return asnWriter.TryEncode(destination, out bytesWritten);
606 }
607 finally
608 {
610 }
611 }
612 }
613
614 public unsafe override bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritten)
615 {
616 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
617 fixed (byte* ptr = ecParameters.D)
618 {
619 try
620 {
621 AsnWriter asnWriter = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
622 return asnWriter.TryEncode(destination, out bytesWritten);
623 }
624 finally
625 {
627 }
628 }
629 }
630
631 public override bool TryExportSubjectPublicKeyInfo(Span<byte> destination, out int bytesWritten)
632 {
633 ECParameters ecParameters = ExportParameters(includePrivateParameters: false);
635 return asnWriter.TryEncode(destination, out bytesWritten);
636 }
637
638 public unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, ReadOnlySpan<byte> source, out int bytesRead)
639 {
640 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, passwordBytes, (KeyFormatHelper.KeyReader<ECParameters>)EccKeyFormatHelper.FromECPrivateKey, out int bytesRead2, out ECParameters ret);
641 fixed (byte* ptr = ret.D)
642 {
643 try
644 {
645 ImportParameters(ret);
646 bytesRead = bytesRead2;
647 }
648 finally
649 {
651 }
652 }
653 }
654
655 public unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, ReadOnlySpan<byte> source, out int bytesRead)
656 {
657 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, password, (KeyFormatHelper.KeyReader<ECParameters>)EccKeyFormatHelper.FromECPrivateKey, out int bytesRead2, out ECParameters ret);
658 fixed (byte* ptr = ret.D)
659 {
660 try
661 {
662 ImportParameters(ret);
663 bytesRead = bytesRead2;
664 }
665 finally
666 {
668 }
669 }
670 }
671
672 public unsafe override void ImportPkcs8PrivateKey(ReadOnlySpan<byte> source, out int bytesRead)
673 {
675 fixed (byte* ptr = ret.D)
676 {
677 try
678 {
679 ImportParameters(ret);
680 bytesRead = bytesRead2;
681 }
682 finally
683 {
685 }
686 }
687 }
688
689 public override void ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source, out int bytesRead)
690 {
692 ImportParameters(ret);
693 bytesRead = bytesRead2;
694 }
695
696 public unsafe virtual void ImportECPrivateKey(ReadOnlySpan<byte> source, out int bytesRead)
697 {
698 int bytesRead2;
699 ECParameters parameters = EccKeyFormatHelper.FromECPrivateKey(source, out bytesRead2);
700 fixed (byte* ptr = parameters.D)
701 {
702 try
703 {
704 ImportParameters(parameters);
705 bytesRead = bytesRead2;
706 }
707 finally
708 {
710 }
711 }
712 }
713
714 public unsafe virtual byte[] ExportECPrivateKey()
715 {
716 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
717 fixed (byte* ptr = ecParameters.D)
718 {
719 try
720 {
721 AsnWriter asnWriter = EccKeyFormatHelper.WriteECPrivateKey(in ecParameters);
722 return asnWriter.Encode();
723 }
724 finally
725 {
727 }
728 }
729 }
730
731 public unsafe virtual bool TryExportECPrivateKey(Span<byte> destination, out int bytesWritten)
732 {
733 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
734 fixed (byte* ptr = ecParameters.D)
735 {
736 try
737 {
738 AsnWriter asnWriter = EccKeyFormatHelper.WriteECPrivateKey(in ecParameters);
739 return asnWriter.TryEncode(destination, out bytesWritten);
740 }
741 finally
742 {
744 }
745 }
746 }
747
748 public int GetMaxSignatureSize(DSASignatureFormat signatureFormat)
749 {
750 int keySize = KeySize;
751 if (keySize == 0)
752 {
753 ExportParameters(includePrivateParameters: false);
754 keySize = KeySize;
755 if (keySize == 0)
756 {
758 }
759 }
760 return signatureFormat switch
761 {
762 DSASignatureFormat.IeeeP1363FixedFieldConcatenation => AsymmetricAlgorithmHelpers.BitsToBytes(keySize) * 2,
763 DSASignatureFormat.Rfc3279DerSequence => AsymmetricAlgorithmHelpers.GetMaxDerSignatureSize(keySize),
764 _ => throw new ArgumentOutOfRangeException("signatureFormat"),
765 };
766 }
767
769 {
771 {
772 if (label.SequenceEqual("PRIVATE KEY"))
773 {
774 return ImportPkcs8PrivateKey;
775 }
776 if (label.SequenceEqual("PUBLIC KEY"))
777 {
778 return ImportSubjectPublicKeyInfo;
779 }
780 return label.SequenceEqual("EC PRIVATE KEY") ? new PemKeyImportHelpers.ImportKeyAction(ImportECPrivateKey) : null;
781 });
782 }
783
785 {
786 PemKeyImportHelpers.ImportEncryptedPem(input, password, ImportEncryptedPkcs8PrivateKey);
787 }
788
790 {
791 PemKeyImportHelpers.ImportEncryptedPem(input, passwordBytes, ImportEncryptedPkcs8PrivateKey);
792 }
793
794 public override void FromXmlString(string xmlString)
795 {
797 }
798
799 public override string ToXmlString(bool includePrivateParameters)
800 {
802 }
803
804 public new static ECDsa Create()
805 {
806 return new ECDsaImplementation.ECDsaCng();
807 }
808
809 public static ECDsa Create(ECCurve curve)
810 {
811 return new ECDsaImplementation.ECDsaCng(curve);
812 }
813
814 public static ECDsa Create(ECParameters parameters)
815 {
816 ECDsa eCDsa = new ECDsaImplementation.ECDsaCng();
817 eCDsa.ImportParameters(parameters);
818 return eCDsa;
819 }
820}
static byte[] ConvertFromIeeeP1363Signature(byte[] signature, DSASignatureFormat targetFormat)
static bool TryCopyToDestination(this ReadOnlySpan< byte > source, Span< byte > destination, out int bytesWritten)
Definition Helpers.cs:62
static void ImportPem(ReadOnlySpan< char > input, FindImportActionFunc callback)
delegate void ImportKeyAction(ReadOnlySpan< byte > source, out int bytesRead)
static unsafe void Clear(Array array)
Definition Array.cs:755
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
bool TryEncode(Span< byte > destination, out int bytesWritten)
Definition AsnWriter.cs:173
int Encode(Span< byte > destination)
Definition AsnWriter.cs:195
static string Cryptography_ECXmlSerializationFormatRequired
Definition SR.cs:54
static string Cryptography_InvalidKeySize
Definition SR.cs:92
static string Cryptography_HashAlgorithmNameNullOrEmpty
Definition SR.cs:60
static string NotSupported_SubclassOverride
Definition SR.cs:1714
Definition SR.cs:7
static ? object CreateFromName(string name, params object?[]? args)
static Exception CreateUnknownValueException(DSASignatureFormat signatureFormat)
override? string KeyExchangeAlgorithm
Definition ECDsa.cs:15
virtual unsafe byte[] ExportECPrivateKey()
Definition ECDsa.cs:714
virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:259
override void ImportFromEncryptedPem(ReadOnlySpan< char > input, ReadOnlySpan< char > password)
Definition ECDsa.cs:784
unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:638
virtual void GenerateKey(ECCurve curve)
Definition ECDsa.cs:44
virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:282
virtual bool TrySignHashCore(ReadOnlySpan< byte > hash, Span< byte > destination, DSASignatureFormat signatureFormat, out int bytesWritten)
Definition ECDsa.cs:490
virtual bool TrySignData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
Definition ECDsa.cs:228
byte[] SignHash(byte[] hash, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:180
virtual bool TrySignHash(ReadOnlySpan< byte > hash, Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:476
override void ImportFromEncryptedPem(ReadOnlySpan< char > input, ReadOnlySpan< byte > passwordBytes)
Definition ECDsa.cs:789
virtual bool TrySignDataCore(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat, out int bytesWritten)
Definition ECDsa.cs:252
override void FromXmlString(string xmlString)
Definition ECDsa.cs:794
byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:140
virtual unsafe bool TryExportECPrivateKey(Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:731
ReadOnlySpan< byte > HashSpanToTmp(ReadOnlySpan< byte > data, HashAlgorithmName hashAlgorithm, Span< byte > tmp)
Definition ECDsa.cs:538
virtual byte[] SignHashCore(ReadOnlySpan< byte > hash, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:193
bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:337
static readonly string[] s_validOids
Definition ECDsa.cs:13
static ECDsa Create(ECParameters parameters)
Definition ECDsa.cs:814
bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:273
override void ImportFromPem(ReadOnlySpan< char > input)
Definition ECDsa.cs:768
static ECDsa Create(ECCurve curve)
Definition ECDsa.cs:809
virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:443
override bool TryExportSubjectPublicKeyInfo(Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:631
virtual unsafe void ImportECPrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:696
virtual byte[] SignDataCore(Stream data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:174
unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:568
virtual void ImportParameters(ECParameters parameters)
Definition ECDsa.cs:39
virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:49
bool VerifyHash(byte[] hash, byte[] signature)
byte[] HashSpanToArray(ReadOnlySpan< byte > data, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:547
static new? ECDsa Create(string algorithm)
Definition ECDsa.cs:20
byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:157
virtual bool VerifyHash(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature)
Definition ECDsa.cs:497
virtual ECParameters ExportExplicitParameters(bool includePrivateParameters)
Definition ECDsa.cs:34
virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:438
bool TrySignData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat, out int bytesWritten)
Definition ECDsa.cs:239
unsafe override bool TryExportPkcs8PrivateKey(Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:614
unsafe override void ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:672
override string SignatureAlgorithm
Definition ECDsa.cs:17
virtual bool VerifyData(ReadOnlySpan< byte > data, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:358
byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:80
virtual bool TryHashData(ReadOnlySpan< byte > data, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
Definition ECDsa.cs:448
bool TrySignHash(ReadOnlySpan< byte > hash, Span< byte > destination, DSASignatureFormat signatureFormat, out int bytesWritten)
Definition ECDsa.cs:481
bool VerifyData(ReadOnlySpan< byte > data, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:369
virtual ECParameters ExportParameters(bool includePrivateParameters)
Definition ECDsa.cs:29
bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:308
virtual bool VerifyDataCore(ReadOnlySpan< byte > data, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:382
virtual byte[] SignDataCore(ReadOnlySpan< byte > data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:105
bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:389
unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:655
virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Definition ECDsa.cs:58
virtual bool VerifyDataCore(Stream data, ReadOnlySpan< byte > signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:428
virtual bool VerifyHashCore(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:528
bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:407
int GetMaxSignatureSize(DSASignatureFormat signatureFormat)
Definition ECDsa.cs:748
static new ECDsa Create()
Definition ECDsa.cs:804
unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition ECDsa.cs:591
override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:689
bool VerifyHash(byte[] hash, byte[] signature, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:502
override string ToXmlString(bool includePrivateParameters)
Definition ECDsa.cs:799
bool VerifyHash(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, DSASignatureFormat signatureFormat)
Definition ECDsa.cs:519
static void FromECPublicKey(ReadOnlyMemory< byte > key, in AlgorithmIdentifierAsn algId, out ECParameters ret)
static AsnWriter WriteSubjectPublicKeyInfo(ECParameters ecParameters)
static AsnWriter WriteECPrivateKey(in ECParameters ecParameters)
static unsafe ECParameters FromECPrivateKey(ReadOnlySpan< byte > key, out int bytesRead)
static AsnWriter WritePkcs8PrivateKey(ECParameters ecParameters, AttributeAsn[] attributes=null)
static ReadOnlyMemory< byte > ReadSubjectPublicKeyInfo(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static ReadOnlyMemory< byte > ReadPkcs8(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static AsnWriter WriteEncryptedPkcs8(ReadOnlySpan< char > password, AsnWriter pkcs8Writer, PbeParameters pbeParameters)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)
void CopyTo(Span< T > destination)
Span< T > Slice(int start)
Definition Span.cs:271