-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathML_DSA.cs
More file actions
2982 lines (2489 loc) · 80.9 KB
/
ML_DSA.cs
File metadata and controls
2982 lines (2489 loc) · 80.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using Waher.Security.SHA3;
namespace Waher.Security.PQC
{
/// <summary>
/// Implements the ML-DSA algorithm for post-quantum cryptography, as defined in
/// NIST FIPS 204: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf
/// </summary>
public class ML_DSA : ML_Common
{
/// <summary>
/// Model parameters for a required RBG strength 128 (cryptographic security strength),
/// as defined in §4.
/// </summary>
public static readonly ML_DSA ML_DSA_44 = new ML_DSA(39, 128, 1 << 17, (q - 1) / 88, 4, 4, 2, 80, 2560, 1312, 2420);
/// <summary>
/// Model parameters for a required RBG strength 192 (cryptographic security strength),
/// as defined in §4.
/// </summary>
public static readonly ML_DSA ML_DSA_65 = new ML_DSA(49, 192, 1 << 19, (q - 1) / 32, 6, 5, 4, 55, 4032, 1952, 3309);
/// <summary>
/// Model parameters for a required RBG strength 256 (cryptographic security strength),
/// as defined in §4.
/// </summary>
public static readonly ML_DSA ML_DSA_87 = new ML_DSA(60, 256, 1 << 19, (q - 1) / 32, 8, 7, 2, 75, 4896, 2592, 4627);
/// <summary>
/// Gets a model by name, as defined in §8.
/// </summary>
/// <param name="Name">Name of model.</param>
/// <returns>Reference to model.</returns>
/// <exception cref="ArgumentException">Model name not recognized.</exception>
public static ML_DSA GetModel(string Name)
{
switch (Name.ToUpper())
{
case "ML-DSA-44":
return ML_DSA_44;
case "ML-DSA-65":
return ML_DSA_65;
case "ML-DSA-87":
return ML_DSA_87;
default:
throw new ArgumentException("Unknown model name: " + Name, nameof(Name));
}
}
private const int n = 256;
private const int q = 8380417;
private const int ζ = 1753; // 512th root of unity in ℤ𝑞
private const int d = 13; // #dropped bits from t
private const uint dBitMask = 0x1fff; // & dBitMask corresponds to mod 2^d.
private const short twoDHalf = 1 << (d - 1); // 2^(d-1)
private const short twoD = 1 << d; // 2^d
private readonly int τ; // # of ±1’s in polynomial c
private readonly int λ; // collision strength of c
private readonly uint γ1; // coefficient range of y
private readonly uint γ2; // low-order rounding range
private readonly byte k; // Rows of matrix A
private readonly byte l; // Columns of matrix A
private readonly byte η; // private key range
private readonly int β; // τ*η
private readonly int ω; // max # of 1’s in the hint h
private readonly int twoγ2; // 2*γ2
private readonly int privateKeySize;
private readonly int publicKeySize;
private readonly int signatureSize;
/// <summary>
/// Implements the ML-DSA algorithm for post-quantum cryptography, as defined in
/// NIST FIPS 204: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf
/// </summary>
/// <param name="τ"># of ±1’s in polynomial c</param>
/// <param name="λ">collision strength of c</param>
/// <param name="γ1">coefficient range of y</param>
/// <param name="γ2">low-order rounding range</param>
/// <param name="k">Rows of matrix A</param>
/// <param name="l">Columns of matrix A</param>
/// <param name="η">private key range</param>
/// <param name="ω">max # of 1’s in the hint h</param>
/// <param name="PrivateKeySize">Size of private key in bytes</param>
/// <param name="PublicKeySize">Size of public key in bytes</param>
/// <param name="SignatureSize">Size of signature in bytes</param>
public ML_DSA(int τ, int λ, uint γ1, uint γ2, byte k, byte l, byte η, int ω,
int PrivateKeySize, int PublicKeySize, int SignatureSize)
{
if (η != 2 && η != 4)
throw new ArgumentException("η must be 2 or 4.", nameof(η));
this.τ = τ;
this.λ = λ;
this.γ1 = γ1;
this.γ2 = γ2;
this.k = k;
this.l = l;
this.η = η;
this.β = τ * η;
this.ω = ω;
this.twoγ2 = (int)(this.γ2 << 1);
this.privateKeySize = PrivateKeySize;
this.publicKeySize = PublicKeySize;
this.signatureSize = SignatureSize;
}
/// <summary>
/// Length of the public key.
/// </summary>
public override int PublicKeyLength => this.publicKeySize;
/// <summary>
/// Length of the private key.
/// </summary>
public override int PrivateKeyLength => this.privateKeySize;
/// <summary>
/// Generates a public and private key.
/// (Algorithm 1 ML-DSA.KeyGen() in §5.1)
/// </summary>
/// <returns>Public Key pk (320k+32 bytes) and Private Key sk
/// (160*((l+k)*bitlen(2η)+dk) bytes).</returns>
public ML_DSA_Keys KeyGen()
{
return this.KeyGen(false);
}
/// <summary>
/// Generates a public and private key.
/// (Algorithm 1 ML-DSA.KeyGen() in §5.1)
/// </summary>
/// <param name="ReturnSeed">If seed should be returned.</param>
/// <returns>Public Key pk (320k+32 bytes) and Private Key sk
/// (160*((l+k)*bitlen(2η)+dk) bytes).</returns>
public ML_DSA_Keys KeyGen(bool ReturnSeed)
{
byte[] ξ = CreateSeed();
ML_DSA_Keys Result = this.KeyGen_Internal(ξ, ReturnSeed);
if (!ReturnSeed)
Clear(ξ);
return Result;
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message,
out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, out int RejectionCount)
{
return this.Sign(Keys, Message, (byte[])null, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, byte[] Context,
out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, byte[] Context,
out int RejectionCount)
{
byte[] ξ = CreateSeed();
byte[] Result = this.Sign(Keys, Message, Context, ξ, out RejectionCount);
Clear(ξ);
return Result;
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, byte[] Context, byte[] Seed,
out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
Seed, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA algorithm.
/// (Algorithm 2 ML-DSA.Sign() in §5.2)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, byte[] Context, byte[] Seed,
out int RejectionCount)
{
int MessageLen = Message.Length;
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
throw new ArgumentException("Context must be 255 bytes or less.", nameof(Context));
if (Seed is null)
Seed = new byte[32];
else if (Seed.Length != 32)
throw new ArgumentException("Seed must be 32 bytes long.", nameof(Seed));
byte[] Bin = new byte[2 + ContextLen + MessageLen];
Bin[0] = 0;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Message, 0, Bin, 2 + ContextLen, MessageLen);
return this.Sign_Internal(Keys, Bin, false, Seed, out RejectionCount);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA algorithm.
/// (Algorithm 3 ML-DSA.Verify() in §5.3)
/// </summary>
/// <param name="PublicKey">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(byte[] PublicKey, byte[] Message, byte[] Signature)
{
return this.Verify(ML_DSA_Keys.FromPublicKey(PublicKey), Message, Signature);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA algorithm.
/// (Algorithm 3 ML-DSA.Verify() in §5.3)
/// </summary>
/// <param name="Keys">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(ML_DSA_Keys Keys, byte[] Message, byte[] Signature)
{
return this.Verify(Keys, Message, Signature, null);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA algorithm.
/// (Algorithm 3 ML-DSA.Verify() in §5.3)
/// </summary>
/// <param name="PublicKey">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(byte[] PublicKey, byte[] Message, byte[] Signature, byte[] Context)
{
return this.Verify(ML_DSA_Keys.FromPublicKey(PublicKey), Message, Signature,
Context);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA algorithm.
/// (Algorithm 3 ML-DSA.Verify() in §5.3)
/// </summary>
/// <param name="Keys">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(ML_DSA_Keys Keys, byte[] Message, byte[] Signature, byte[] Context)
{
int MessageLen = Message.Length;
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
return false;
byte[] Bin = new byte[2 + ContextLen + MessageLen];
Bin[0] = 0;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Message, 0, Bin, 2 + ContextLen, MessageLen);
return this.Verify_Internal(Keys, Bin, false, Signature);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, string HashAlgorithm,
out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message,
HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, string HashAlgorithm,
out int RejectionCount)
{
return this.Sign(Keys, Message, null, HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, byte[] Context,
string HashAlgorithm, out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, byte[] Context,
string HashAlgorithm, out int RejectionCount)
{
byte[] ξ = CreateSeed();
byte[] Result = this.Sign(Keys, Message, Context, ξ, HashAlgorithm,
out RejectionCount);
Clear(ξ);
return Result;
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, byte[] Message, byte[] Context, byte[] Seed,
string HashAlgorithm, out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
Seed, HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, byte[] Message, byte[] Context, byte[] Seed,
string HashAlgorithm, out int RejectionCount)
{
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
throw new ArgumentException("Context must be 255 bytes or less.", nameof(Context));
if (Seed is null)
Seed = new byte[32];
else if (Seed.Length != 32)
throw new ArgumentException("Seed must be 32 bytes long.", nameof(Seed));
if (!TryGetHashFunction(HashAlgorithm, out HashFunctionArray H, out byte[] Oid))
throw new ArgumentException("Unsupported hash algorithm: " + HashAlgorithm, nameof(HashAlgorithm));
Message = H(Message);
int MessageLen = Message.Length;
int OidLen = Oid.Length;
int Pos;
byte[] Bin = new byte[2 + ContextLen + OidLen + MessageLen];
Bin[0] = 1;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Oid, 0, Bin, Pos = 2 + ContextLen, OidLen);
Pos += OidLen;
Buffer.BlockCopy(Message, 0, Bin, Pos, MessageLen);
return this.Sign_Internal(Keys, Bin, false, Seed, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, Stream Message, byte[] Context,
string HashAlgorithm, out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, Stream Message, byte[] Context,
string HashAlgorithm, out int RejectionCount)
{
byte[] ξ = CreateSeed();
byte[] Result = this.Sign(Keys, Message, Context, ξ, HashAlgorithm,
out RejectionCount);
Clear(ξ);
return Result;
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="PrivateKey">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(byte[] PrivateKey, Stream Message, byte[] Context, byte[] Seed,
string HashAlgorithm, out int RejectionCount)
{
return this.Sign(ML_DSA_Keys.FromPrivateKey(PrivateKey), Message, Context,
Seed, HashAlgorithm, out RejectionCount);
}
/// <summary>
/// Signs a message using the ML-DSA pre-hash algorithm.
/// (Algorithm 4 HashML-DSA.Sign() in §5.4.1)
/// </summary>
/// <param name="Keys">Private key</param>
/// <param name="Message">Message to sign</param>
/// <param name="Context">Context, optionally empty.</param>
/// <param name="Seed">Optional 32-byte randomness.</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <param name="RejectionCount">Number of rejected signatures created before
/// an approved signature was calculated.</param>
/// <returns>Digital signature.</returns>
public byte[] Sign(ML_DSA_Keys Keys, Stream Message, byte[] Context, byte[] Seed,
string HashAlgorithm, out int RejectionCount)
{
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
throw new ArgumentException("Context must be 255 bytes or less.", nameof(Context));
if (Seed is null)
Seed = new byte[32];
else if (Seed.Length != 32)
throw new ArgumentException("Seed must be 32 bytes long.", nameof(Seed));
if (!TryGetHashFunction(HashAlgorithm, out HashFunctionStream H, out byte[] Oid))
throw new ArgumentException("Unsupported hash algorithm: " + HashAlgorithm, nameof(HashAlgorithm));
Message.Position = 0;
byte[] Message2 = H(Message);
int Message2Len = Message2.Length;
int OidLen = Oid.Length;
int Pos;
byte[] Bin = new byte[2 + ContextLen + OidLen + Message2Len];
Bin[0] = 1;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Oid, 0, Bin, Pos = 2 + ContextLen, OidLen);
Pos += OidLen;
Buffer.BlockCopy(Message2, 0, Bin, Pos, Message2Len);
return this.Sign_Internal(Keys, Bin, false, Seed, out RejectionCount);
}
private static bool TryGetHashFunction(string Name, out HashFunctionArray H, out byte[] Oid)
{
switch (Name.ToUpper())
{
case "SHA256":
case "SHA-256":
case "SHA2-256":
Oid = oidSha256;
H = Hashes.ComputeSHA256Hash;
return true;
case "SHA384":
case "SHA-384":
case "SHA2-384":
Oid = oidSha384;
H = Hashes.ComputeSHA384Hash;
return true;
case "SHA512":
case "SHA-512":
case "SHA2-512":
Oid = oidSha512;
H = Hashes.ComputeSHA512Hash;
return true;
case "SHA3-224":
Oid = oidSha3_224;
H = new SHA3_224().ComputeVariable;
return true;
case "SHA3-256":
Oid = oidSha3_256;
H = new SHA3_256().ComputeVariable;
return true;
case "SHA3-384":
Oid = oidSha3_384;
H = new SHA3_384().ComputeVariable;
return true;
case "SHA3-512":
Oid = oidSha3_512;
H = new SHA3_512().ComputeVariable;
return true;
case "SHAKE128":
case "SHAKE-128":
Oid = oidShake128;
H = new SHAKE128(256).ComputeVariable;
return true;
case "SHAKE256":
case "SHAKE-256":
Oid = oidShake256;
H = new SHAKE256(256).ComputeVariable;
return true;
default:
Oid = null;
H = null;
return false;
}
}
private static bool TryGetHashFunction(string Name, out HashFunctionStream H, out byte[] Oid)
{
switch (Name.ToUpper())
{
case "SHA256":
case "SHA-256":
case "SHA2-256":
Oid = oidSha256;
H = Hashes.ComputeSHA256Hash;
return true;
case "SHA384":
case "SHA-384":
case "SHA2-384":
Oid = oidSha384;
H = Hashes.ComputeSHA384Hash;
return true;
case "SHA512":
case "SHA-512":
case "SHA2-512":
Oid = oidSha512;
H = Hashes.ComputeSHA512Hash;
return true;
case "SHA3-224":
Oid = oidSha3_224;
H = new SHA3_224().ComputeVariable;
return true;
case "SHA3-256":
Oid = oidSha3_256;
H = new SHA3_256().ComputeVariable;
return true;
case "SHA3-384":
Oid = oidSha3_384;
H = new SHA3_384().ComputeVariable;
return true;
case "SHA3-512":
Oid = oidSha3_512;
H = new SHA3_512().ComputeVariable;
return true;
case "SHAKE128":
case "SHAKE-128":
Oid = oidShake128;
H = new SHAKE128(256).ComputeVariable;
return true;
case "SHAKE256":
case "SHAKE-256":
Oid = oidShake256;
H = new SHAKE256(256).ComputeVariable;
return true;
default:
Oid = null;
H = null;
return false;
}
}
private static readonly byte[] oidSha256 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
};
private static readonly byte[] oidSha384 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
};
private static readonly byte[] oidSha512 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
};
private static readonly byte[] oidSha3_224 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x07
};
private static readonly byte[] oidSha3_256 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08
};
private static readonly byte[] oidSha3_384 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09
};
private static readonly byte[] oidSha3_512 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0a
};
private static readonly byte[] oidShake128 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0b
};
private static readonly byte[] oidShake256 = new byte[]
{
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0c
};
/// <summary>
/// Verifies a digital signature using the ML-DSA pre-hash algorithm.
/// (Algorithm 5 HashML-DSA.Verify() in §5.4.1)
/// </summary>
/// <param name="PublicKey">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(byte[] PublicKey, byte[] Message, byte[] Signature, byte[] Context,
string HashAlgorithm)
{
return this.Verify(ML_DSA_Keys.FromPublicKey(PublicKey), Message, Signature,
Context, HashAlgorithm);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA pre-hash algorithm.
/// (Algorithm 5 HashML-DSA.Verify() in §5.4.1)
/// </summary>
/// <param name="Keys">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(ML_DSA_Keys Keys, byte[] Message, byte[] Signature, byte[] Context,
string HashAlgorithm)
{
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
return false;
if (!TryGetHashFunction(HashAlgorithm, out HashFunctionArray H, out byte[] Oid))
throw new ArgumentException("Unsupported hash algorithm: " + HashAlgorithm, nameof(HashAlgorithm));
Message = H(Message);
int MessageLen = Message.Length;
int OidLen = Oid.Length;
int Pos;
byte[] Bin = new byte[2 + ContextLen + OidLen + MessageLen];
Bin[0] = 1;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Oid, 0, Bin, Pos = 2 + ContextLen, OidLen);
Pos += OidLen;
Buffer.BlockCopy(Message, 0, Bin, Pos, MessageLen);
return this.Verify_Internal(Keys, Bin, false, Signature);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA pre-hash algorithm.
/// (Algorithm 5 HashML-DSA.Verify() in §5.4.1)
/// </summary>
/// <param name="PublicKey">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(byte[] PublicKey, Stream Message, byte[] Signature, byte[] Context,
string HashAlgorithm)
{
return this.Verify(ML_DSA_Keys.FromPublicKey(PublicKey), Message, Signature,
Context, HashAlgorithm);
}
/// <summary>
/// Verifies a digital signature using the ML-DSA pre-hash algorithm.
/// (Algorithm 5 HashML-DSA.Verify() in §5.4.1)
/// </summary>
/// <param name="Keys">Public Key</param>
/// <param name="Message">Message</param>
/// <param name="Signature">Signature</param>
/// <param name="Context">Context</param>
/// <param name="HashAlgorithm">Name of hash algorithm.</param>
/// <returns>If the signature is valid.</returns>
public bool Verify(ML_DSA_Keys Keys, Stream Message, byte[] Signature, byte[] Context,
string HashAlgorithm)
{
int ContextLen;
if (Context is null)
{
Context = Array.Empty<byte>();
ContextLen = 0;
}
else if ((ContextLen = Context.Length) > 255)
return false;
if (!TryGetHashFunction(HashAlgorithm, out HashFunctionStream H, out byte[] Oid))
throw new ArgumentException("Unsupported hash algorithm: " + HashAlgorithm, nameof(HashAlgorithm));
Message.Position = 0;
byte[] Message2 = H(Message);
int Message2Len = Message2.Length;
int OidLen = Oid.Length;
int Pos;
byte[] Bin = new byte[2 + ContextLen + OidLen + Message2Len];
Bin[0] = 1;
Bin[1] = (byte)ContextLen;
if (ContextLen > 0)
Buffer.BlockCopy(Context, 0, Bin, 2, ContextLen);
Buffer.BlockCopy(Oid, 0, Bin, Pos = 2 + ContextLen, OidLen);
Pos += OidLen;
Buffer.BlockCopy(Message2, 0, Bin, Pos, Message2Len);
return this.Verify_Internal(Keys, Bin, false, Signature);
}
/// <summary>
/// Generates a public and private key.
/// (Algorithm 6 ML-DSA.KeyGen_Internal() in §6.1)
/// </summary>
/// <param name="ξ">Seed</param>
/// <returns>Public Key pk (320k+32 bytes) and Private Key sk
/// (160*((l+k)*bitlen(2η)+dk) bytes).</returns>
public ML_DSA_Keys KeyGen_Internal(byte[] ξ)
{
return this.KeyGen_Internal(ξ, false);
}
/// <summary>
/// Generates a public and private key.
/// (Algorithm 6 ML-DSA.KeyGen_Internal() in §6.1)
/// </summary>
/// <param name="ξ">Seed</param>
/// <param name="ReturnSeed">If seed should be returned.</param>
/// <returns>Public Key pk (320k+32 bytes) and Private Key sk
/// (160*((l+k)*bitlen(2η)+dk) bytes).</returns>
public ML_DSA_Keys KeyGen_Internal(byte[] ξ, bool ReturnSeed)
{
if (ξ.Length != 32)
throw new ArgumentException("Seed must be 32 bytes long.", nameof(ξ));
byte[] Bin = new byte[34];
Buffer.BlockCopy(ξ, 0, Bin, 0, 32);
Bin[32] = this.k;
Bin[33] = this.l;
byte[] Bin2 = H(Bin, 128);
Clear(Bin);
byte[] ρ = new byte[32];
Buffer.BlockCopy(Bin2, 0, ρ, 0, 32);
byte[] K = new byte[32];
Buffer.BlockCopy(Bin2, 96, K, 0, 32);
uint[,][] Â = this.ExpandÂ(ρ);
byte[] B = new byte[66]; // ρ´ || 2 index bytes
Buffer.BlockCopy(Bin2, 32, B, 0, 64);
short[][] s1 = this.ExpandS(B, this.l);
short[][] s2 = this.ExpandS(B, this.k);
Clear(B);
uint[][] NTTs1 = NTT(s1);
uint[][] t = new uint[this.k][];
uint[] f;
int i, j;
for (i = 0; i < this.k; i++)
{
t[i] = f = new uint[n];
for (j = 0; j < this.l; j++)
MultiplyNTTsAndAdd(Â[i, j], NTTs1[j], f);
}
InverseNTT(t);
AddTo(t, s2);
// Power2Round, decomposes t into two arrays t1, t0, where t=t1*2^d+t0 mod q
// (Algorithm 35, §7.4)
short[][] t0 = new short[this.k][];
short[][] t1 = new short[this.k][];
short[] f0, f1;
short r0, r1;
uint r;
for (i = 0; i < this.k; i++)
{
f = t[i];
t0[i] = f0 = new short[n];
t1[i] = f1 = new short[n];