-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathdilithium.c
More file actions
12685 lines (11800 loc) · 427 KB
/
dilithium.c
File metadata and controls
12685 lines (11800 loc) · 427 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
/* dilithium.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Based on ed448.c and Reworked for Dilithium by Anthony Hu.
* WolfSSL implementation by Sean Parkinson.
*/
/* Possible Dilithium/ML-DSA options:
*
* HAVE_DILITHIUM Default: OFF
* Enables the code in this file to be compiled.
*
* WOLFSSL_NO_ML_DSA_44 Default: OFF
* Does not compile in parameter set ML-DSA-44 and any code specific to that
* parameter set.
* WOLFSSL_NO_ML_DSA_65 Default: OFF
* Does not compile in parameter set ML-DSA-65 and any code specific to that
* parameter set.
* WOLFSSL_NO_ML_DSA_87 Default: OFF
* Does not compile in parameter set ML-DSA-87 and any code specific to that
* parameter set.
*
* WOLFSSL_DILITHIUM_NO_LARGE_CODE Default: OFF
* Compiles smaller, fast code with speed trade-off.
* WOLFSSL_DILITHIUM_SMALL Default: OFF
* Compiles to small code size with a speed trade-off.
* WOLFSSL_DILITHIUM_VERIFY_ONLY Default: OFF
* Compiles in only the verification and public key operations.
* WOLFSSL_DILITHIUM_VERIFY_SMALL_MEM Default: OFF
* Compiles verification implementation that uses smaller amounts of memory.
* WOLFSSL_DILITHIUM_VERIFY_NO_MALLOC Default: OFF
* Only works with WOLFSSL_DILITHIUM_VERIFY_SMALL_MEM.
* Don't allocate memory with XMALLOC. Memory is pinned against key.
* WOLFSSL_DILITHIUM_ASSIGN_KEY Default: OFF
* Key data is assigned into Dilithium key rather than copied.
* Life of key data passed in is tightly coupled to life of Dilithium key.
* Cannot be used when make key is enabled.
* WOLFSSL_DILITHIUM_DYNAMIC_KEYS Default: OFF
* Key buffers (public and private) are dynamically allocated on the heap
* instead of being static arrays in the key struct. Buffers are right-sized
* for the key's ML-DSA level and only allocated when needed (e.g. no private
* key buffer for verify-only keys). Reduces memory footprint significantly.
* Cannot be used with WOLFSSL_DILITHIUM_ASSIGN_KEY.
* WOLFSSL_DILITHIUM_SIGN_SMALL_MEM Default: OFF
* Compiles signature implementation that uses smaller amounts of memory but
* is considerably slower.
* WOLFSSL_DILITHIUM_SIGN_SMALL_MEM_PRECALC Default: OFF
* Compiles signature implementation that uses smaller amounts of memory but
* is considerably slower. Allocates vectors and decodes private key data
* into them upfront.
* WOLFSSL_DILITHIUM_SIGN_SMALL_MEM_PRECALC_A Default: OFF
* Compiles signature implementation that uses smaller amounts of memory but
* is slower. Allocates matrix A and calculates it upfront.
* WOLFSSL_DILITHIUM_MAKE_KEY_SMALL_MEM Default: OFF
* Compiles key generation implementation that uses smaller amounts of memory
* but is slower.
* WOLFSSL_DILITHIUM_SMALL_MEM_POLY64 Default: OFF
* Compiles the small memory implementations to use a 64-bit polynomial.
* Uses 2KB of memory but is slightly quicker (2.75-7%).
*
* WOLFSSL_DILITHIUM_ALIGNMENT Default: 8
* Use to indicate whether loading and storing of words needs to be aligned.
* Default is to use WOLFSSL_GENERAL_ALIGNMENT - should be 4 on some ARM CPUs.
* Set this value explicitly if specific Dilithium implementation alignment is
* needed.
*
* WOLFSSL_DILITHIUM_NO_ASN1 Default: OFF
* Disables any ASN.1 encoding or decoding code.
* WOLFSSL_DILITHIUM_REVERSE_HASH_OID Default: OFF
* Reverse the DER encoded hash oid when signing and verifying a pre-hashed
* message.
*
* WC_DILITHIUM_CACHE_MATRIX_A Default: OFF
* Enable caching of the A matrix on import.
* Less work is required in sign and verify operations.
* WC_DILITHIUM_CACHE_PRIV_VECTORS Default: OFF
* Enable caching of private key vectors on import.
* Enables WC_DILITHIUM_CACHE_MATRIX_A.
* Less work is required in sign operations.
* WC_DILITHIUM_CACHE_PUB_VECTORS Default: OFF
* Enable caching of public key vectors on import.
* Enables WC_DILITHIUM_CACHE_MATRIX_A.
* Less work is required in sign operations.
* WC_DILITHIUM_FIXED_ARRAY Default: OFF
* Make the matrix and vectors of cached data fixed arrays that have
* maximumal sizes for the configured parameters.
* Useful in low dynamic memory situations.
*
* WOLFSSL_DILITHIUM_SIGN_CHECK_Y Default: OFF
* Check vector y is in required range as an early check on valid vector z.
* Falsely reports invalid in approximately 1-2% of checks.
* All valid reports are true.
* Fast fail gives faster signing times on average.
* DO NOT enable this if implementation must be conformant to FIPS 204.
* WOLFSSL_DILITHIUM_SIGN_CHECK_W0 Default: OFF
* Check vector w0 is in required range as an early check on valid vector r0.
* Falsely reports invalid in approximately 3-5% of checks.
* All valid reports are true.
* Fast fail gives faster signing times on average.
* DO NOT enable this if implementation must be conformant to FIPS 204.
*
* DILITHIUM_MUL_SLOW Default: OFF
* Define when multiplying by Q / 44 is slower than masking.
* Only applies to ML-DSA-44.
* DILITHIUM_MUL_44_SLOW Default: OFF
* Define when multiplying by 44 is slower than by 11.
* Only applies to ML-DSA-44.
* DILITHIUM_MUL_11_SLOW Default: OFF
* Define when multiplying by 11 is slower than adding and shifting.
* Only applies to ML-DSA-44.
* DILITHIUM_MUL_QINV_SLOW Default: OFF
* Define when multiplying by QINV 0x3802001 is slower than add, subtract and
* shift equivalent.
* DILITHIUM_MUL_Q_SLOW Default: OFF
* Define when multiplying by Q 0x7fe001 is slower than add, subtract and
* shift equivalent.
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#if FIPS_VERSION3_GE(2,0,0)
/* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
#define FIPS_NO_WRAPPERS
#endif
#ifndef WOLFSSL_DILITHIUM_NO_ASN1
#include <wolfssl/wolfcrypt/asn.h>
#endif
#if defined(HAVE_DILITHIUM)
#include <wolfssl/wolfcrypt/dilithium.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/sha3.h>
#include <wolfssl/wolfcrypt/cpuid.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#if defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM_PRECALC) && \
!defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM)
#define WOLFSSL_DILITHIUM_SIGN_SMALL_MEM
#endif
#if defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM_PRECALC_A) && \
!defined(WOLFSSL_DILITHIUM_SIGN_SMALL_MEM)
#define WOLFSSL_DILITHIUM_SIGN_SMALL_MEM
#ifdef WOLFSSL_DILITHIUM_SIGN_SMALL_MEM_PRECALC
#error "PRECALC and PRECALC_A are equivalent to non small mem"
#endif
#endif
#if defined(USE_INTEL_SPEEDUP)
static cpuid_flags_t cpuid_flags = WC_CPUID_INITIALIZER;
#endif
#ifdef DEBUG_DILITHIUM
void print_polys(const char* name, const sword32* a, int d1, int d2);
void print_polys(const char* name, const sword32* a, int d1, int d2)
{
int i;
int j;
int k;
fprintf(stderr, "%s: %d %d\n", name, d1, d2);
for (i = 0; i < d1; i++) {
for (j = 0; j < d2; j++) {
for (k = 0; k < 256; k++) {
fprintf(stderr, "%9d,", a[(i*d2*256) + (j*256) + k]);
if ((k % 8) == 7) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
}
}
#endif
#ifdef DEBUG_DILITHIUM
void print_data(const char* name, const byte* d, int len);
void print_data(const char* name, const byte* d, int len)
{
int i;
fprintf(stderr, "%s\n", name);
for (i = 0; i < len; i++) {
fprintf(stderr, "0x%02x,", d[i]);
if ((i % 16) == 15) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
#endif
#if defined(WOLFSSL_NO_ML_DSA_44) && defined(WOLFSSL_NO_ML_DSA_65) && \
defined(WOLFSSL_NO_ML_DSA_87)
#error "No Dilithium parameters chosen"
#endif
#if defined(WOLFSSL_DILITHIUM_ASSIGN_KEY) && \
!defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
#error "Cannot use assign key when making keys"
#endif
#if defined(WOLFSSL_DILITHIUM_DYNAMIC_KEYS) && \
defined(WOLFSSL_DILITHIUM_ASSIGN_KEY)
#error "Cannot use both WOLFSSL_DILITHIUM_DYNAMIC_KEYS and WOLFSSL_DILITHIUM_ASSIGN_KEY"
#endif
/* Number of bytes from first block to use for sign. */
#define DILITHIUM_SIGN_BYTES 8
/* Length of seed in bytes when generating y. */
#define DILITHIUM_Y_SEED_SZ (DILITHIUM_PRIV_RAND_SEED_SZ + 2)
/* Length of seed in bytes used in generating matrix a. */
#define DILITHIUM_GEN_A_SEED_SZ (DILITHIUM_PUB_SEED_SZ + 2)
/* Length of seed in bytes used in generating vectors s1 and s2. */
#define DILITHIUM_GEN_S_SEED_SZ (DILITHIUM_PRIV_SEED_SZ + 2)
/* MAX: (256 * 8 / (17 + 1)) = 576, or ((256 * 8 / (19 + 1)) = 640
* but need blocks of 17 * 8 bytes: 5 * 17 * 8 = 680 */
#define DILITHIUM_MAX_V_BLOCKS 5
/* Maximum number of bytes to generate into v to make y. */
#define DILITHIUM_MAX_V (DILITHIUM_MAX_V_BLOCKS * 8 * 17)
/* 2 blocks, each block 136 bytes = 272 bytes.
* ETA 2: Min req is 128 but reject rate is 2 in 16 so we need 146.3 on average.
* ETA 4: Min req is 128 but reject rate is 7 in 16 so we need 227.6 on average.
*/
#define DILITHIUM_GEN_S_NBLOCKS 2
/* Number of bytes to a block of SHAKE-256 when generating s1 and s2. */
#define DILITHIUM_GEN_S_BLOCK_BYTES (WC_SHA3_256_COUNT * 8)
/* Number of bytes to generate with SHAKE-256 when generating s1 and s2. */
#define DILITHIUM_GEN_S_BYTES \
(DILITHIUM_GEN_S_NBLOCKS * DILITHIUM_GEN_S_BLOCK_BYTES)
/* Length of the hash OID to include in pre-hash message. */
#define DILITHIUM_HASH_OID_LEN 11
/* The ML-DSA parameters sets. */
static const wc_dilithium_params dilithium_params[] = {
#ifndef WOLFSSL_NO_ML_DSA_44
{ WC_ML_DSA_44, PARAMS_ML_DSA_44_K, PARAMS_ML_DSA_44_L,
PARAMS_ML_DSA_44_ETA, PARAMS_ML_DSA_44_ETA_BITS,
PARAMS_ML_DSA_44_TAU, PARAMS_ML_DSA_44_BETA, PARAMS_ML_DSA_44_OMEGA,
PARAMS_ML_DSA_44_LAMBDA,
PARAMS_ML_DSA_44_GAMMA1_BITS, PARAMS_ML_DSA_44_GAMMA2,
PARAMS_ML_DSA_44_W1_ENC_SZ, PARAMS_ML_DSA_44_A_SIZE,
PARAMS_ML_DSA_44_S1_SIZE, PARAMS_ML_DSA_44_S1_ENC_SIZE,
PARAMS_ML_DSA_44_S2_SIZE, PARAMS_ML_DSA_44_S2_ENC_SIZE,
PARAMS_ML_DSA_44_Z_ENC_SIZE,
PARAMS_ML_DSA_44_PK_SIZE, PARAMS_ML_DSA_44_SIG_SIZE },
#endif
#ifndef WOLFSSL_NO_ML_DSA_65
{ WC_ML_DSA_65, PARAMS_ML_DSA_65_K, PARAMS_ML_DSA_65_L,
PARAMS_ML_DSA_65_ETA, PARAMS_ML_DSA_65_ETA_BITS,
PARAMS_ML_DSA_65_TAU, PARAMS_ML_DSA_65_BETA, PARAMS_ML_DSA_65_OMEGA,
PARAMS_ML_DSA_65_LAMBDA,
PARAMS_ML_DSA_65_GAMMA1_BITS, PARAMS_ML_DSA_65_GAMMA2,
PARAMS_ML_DSA_65_W1_ENC_SZ, PARAMS_ML_DSA_65_A_SIZE,
PARAMS_ML_DSA_65_S1_SIZE, PARAMS_ML_DSA_65_S1_ENC_SIZE,
PARAMS_ML_DSA_65_S2_SIZE, PARAMS_ML_DSA_65_S2_ENC_SIZE,
PARAMS_ML_DSA_65_Z_ENC_SIZE,
PARAMS_ML_DSA_65_PK_SIZE, PARAMS_ML_DSA_65_SIG_SIZE },
#endif
#ifndef WOLFSSL_NO_ML_DSA_87
{ WC_ML_DSA_87, PARAMS_ML_DSA_87_K, PARAMS_ML_DSA_87_L,
PARAMS_ML_DSA_87_ETA, PARAMS_ML_DSA_87_ETA_BITS,
PARAMS_ML_DSA_87_TAU, PARAMS_ML_DSA_87_BETA, PARAMS_ML_DSA_87_OMEGA,
PARAMS_ML_DSA_87_LAMBDA,
PARAMS_ML_DSA_87_GAMMA1_BITS, PARAMS_ML_DSA_87_GAMMA2,
PARAMS_ML_DSA_87_W1_ENC_SZ, PARAMS_ML_DSA_87_A_SIZE,
PARAMS_ML_DSA_87_S1_SIZE, PARAMS_ML_DSA_87_S1_ENC_SIZE,
PARAMS_ML_DSA_87_S2_SIZE, PARAMS_ML_DSA_87_S2_ENC_SIZE,
PARAMS_ML_DSA_87_Z_ENC_SIZE,
PARAMS_ML_DSA_87_PK_SIZE, PARAMS_ML_DSA_87_SIG_SIZE },
#endif
#if defined(WOLFSSL_DILITHIUM_FIPS204_DRAFT)
#ifndef WOLFSSL_NO_ML_DSA_44
{ WC_ML_DSA_44_DRAFT, PARAMS_ML_DSA_44_K, PARAMS_ML_DSA_44_L,
PARAMS_ML_DSA_44_ETA, PARAMS_ML_DSA_44_ETA_BITS,
PARAMS_ML_DSA_44_TAU, PARAMS_ML_DSA_44_BETA, PARAMS_ML_DSA_44_OMEGA,
PARAMS_ML_DSA_44_LAMBDA,
PARAMS_ML_DSA_44_GAMMA1_BITS, PARAMS_ML_DSA_44_GAMMA2,
PARAMS_ML_DSA_44_W1_ENC_SZ, PARAMS_ML_DSA_44_A_SIZE,
PARAMS_ML_DSA_44_S1_SIZE, PARAMS_ML_DSA_44_S1_ENC_SIZE,
PARAMS_ML_DSA_44_S2_SIZE, PARAMS_ML_DSA_44_S2_ENC_SIZE,
PARAMS_ML_DSA_44_Z_ENC_SIZE,
PARAMS_ML_DSA_44_PK_SIZE, PARAMS_ML_DSA_44_SIG_SIZE },
#endif
#ifndef WOLFSSL_NO_ML_DSA_65
{ WC_ML_DSA_65_DRAFT, PARAMS_ML_DSA_65_K, PARAMS_ML_DSA_65_L,
PARAMS_ML_DSA_65_ETA, PARAMS_ML_DSA_65_ETA_BITS,
PARAMS_ML_DSA_65_TAU, PARAMS_ML_DSA_65_BETA, PARAMS_ML_DSA_65_OMEGA,
PARAMS_ML_DSA_65_LAMBDA,
PARAMS_ML_DSA_65_GAMMA1_BITS, PARAMS_ML_DSA_65_GAMMA2,
PARAMS_ML_DSA_65_W1_ENC_SZ, PARAMS_ML_DSA_65_A_SIZE,
PARAMS_ML_DSA_65_S1_SIZE, PARAMS_ML_DSA_65_S1_ENC_SIZE,
PARAMS_ML_DSA_65_S2_SIZE, PARAMS_ML_DSA_65_S2_ENC_SIZE,
PARAMS_ML_DSA_65_Z_ENC_SIZE,
PARAMS_ML_DSA_65_PK_SIZE, PARAMS_ML_DSA_65_SIG_SIZE },
#endif
#ifndef WOLFSSL_NO_ML_DSA_87
{ WC_ML_DSA_87_DRAFT, PARAMS_ML_DSA_87_K, PARAMS_ML_DSA_87_L,
PARAMS_ML_DSA_87_ETA, PARAMS_ML_DSA_87_ETA_BITS,
PARAMS_ML_DSA_87_TAU, PARAMS_ML_DSA_87_BETA, PARAMS_ML_DSA_87_OMEGA,
PARAMS_ML_DSA_87_LAMBDA,
PARAMS_ML_DSA_87_GAMMA1_BITS, PARAMS_ML_DSA_87_GAMMA2,
PARAMS_ML_DSA_87_W1_ENC_SZ, PARAMS_ML_DSA_87_A_SIZE,
PARAMS_ML_DSA_87_S1_SIZE, PARAMS_ML_DSA_87_S1_ENC_SIZE,
PARAMS_ML_DSA_87_S2_SIZE, PARAMS_ML_DSA_87_S2_ENC_SIZE,
PARAMS_ML_DSA_87_Z_ENC_SIZE,
PARAMS_ML_DSA_87_PK_SIZE, PARAMS_ML_DSA_87_SIG_SIZE },
#endif
#endif
};
/* Number of ML-DSA parameter sets compiled in. */
#define DILITHIUM_PARAMS_CNT \
((unsigned int)(sizeof(dilithium_params) / sizeof(wc_dilithium_params)))
/* Get the ML-DSA parameters that match the level.
*
* @param [in] level Level required.
* @param [out] params Parameter set.
* @return 0 on success.
* @return NOT_COMPILED_IN when parameters at level are not compiled in.
*/
static int dilithium_get_params(int level, const wc_dilithium_params** params)
{
unsigned int i;
int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
for (i = 0; i < DILITHIUM_PARAMS_CNT; i++) {
if (dilithium_params[i].level == level) {
*params = &dilithium_params[i];
ret = 0;
}
}
return ret;
}
#if defined(WOLFSSL_DILITHIUM_DYNAMIC_KEYS) && \
defined(WOLFSSL_DILITHIUM_PRIVATE_KEY)
/* Allocate the private key buffer for the current level if not already
* allocated. Buffer is sized via wc_dilithium_size(key) and the allocated size
* is stored in key->kSz for later use (ForceZero, free). On failure key->k may
* remain NULL; callers must not inspect it. */
static int dilithium_alloc_priv_buf(dilithium_key* key)
{
int ret = 0;
if (key->k == NULL) {
int secSz = wc_dilithium_size(key);
if (secSz < 0) {
/* Should not happen, as the level checks have already been
* performed, but defense-in-depth. */
ret = BAD_STATE_E;
}
else {
#ifdef USE_INTEL_SPEEDUP
secSz += 8;
#endif
key->k = (byte*)XMALLOC((word32)secSz, key->heap,
DYNAMIC_TYPE_DILITHIUM);
if (key->k == NULL) {
ret = MEMORY_E;
}
else {
key->kSz = (word32)secSz;
}
}
}
return ret;
}
#endif
#if defined(WOLFSSL_DILITHIUM_DYNAMIC_KEYS) && \
defined(WOLFSSL_DILITHIUM_PUBLIC_KEY)
/* Allocate the public key buffer for the current level if not already
* allocated. Buffer is sized via wc_dilithium_pub_size(key). On failure,
* key->p may remain NULL; callers must not inspect it. */
static int dilithium_alloc_pub_buf(dilithium_key* key)
{
int ret = 0;
if (key->p == NULL) {
int pubSz = wc_dilithium_pub_size(key);
if (pubSz < 0) {
/* Should not happen, as the level checks have already been
* performed, but defense-in-depth. */
ret = BAD_STATE_E;
}
else {
#ifdef USE_INTEL_SPEEDUP
pubSz += 8;
#endif
key->p = (byte*)XMALLOC((word32)pubSz, key->heap,
DYNAMIC_TYPE_DILITHIUM);
if (key->p == NULL) {
ret = MEMORY_E;
}
}
}
return ret;
}
#endif
/******************************************************************************
* Hash operations
******************************************************************************/
/* 256-bit hash using SHAKE-256.
*
* FIPS 204. 8.3: H(v,d) <- SHAKE256(v,d)
*
* @param [in, out] shake256 SHAKE-256 object.
* @param [in] data Buffer holding data to hash.
* @param [in] dataLen Length of data to hash in bytes.
* @param [out] hash Buffer to hold hash result.
* @param [in] hashLen Number of bytes of hash to return.
* @return 0 on success.
* @return Negative on error.
*/
static int dilithium_shake256(wc_Shake* shake256, const byte* data,
word32 dataLen, byte* hash, word32 hashLen)
{
int ret;
#ifdef USE_INTEL_SPEEDUP
word64* state = shake256->s;
word8 *state8 = (word8*)state;
if (dataLen >= WC_SHA3_256_COUNT * 8) {
XMEMCPY(state, data, WC_SHA3_256_COUNT * 8);
XMEMSET(state + WC_SHA3_256_COUNT, 0,
sizeof(shake256->s) - WC_SHA3_256_COUNT * 8);
dataLen -= WC_SHA3_256_COUNT * 8;
data += WC_SHA3_256_COUNT * 8;
#ifndef WC_SHA3_NO_ASM
if (IS_INTEL_AVX2(cpuid_flags) &&
(SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_avx2(state);
RESTORE_VECTOR_REGISTERS();
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_bmi2(state);
}
else
#endif
{
BlockSha3(state);
}
if (dataLen >= WC_SHA3_256_COUNT * 8) {
#ifndef WC_SHA3_NO_ASM
word32 n = dataLen / (WC_SHA3_256_COUNT * 8);
if (IS_INTEL_AVX2(cpuid_flags) &&
(SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_n_avx2(state, data, n, WC_SHA3_256_COUNT * 8);
RESTORE_VECTOR_REGISTERS();
n *= WC_SHA3_256_COUNT * 8;
dataLen -= n;
data += n;
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_n_bmi2(state, data, n, WC_SHA3_256_COUNT * 8);
n *= WC_SHA3_256_COUNT * 8;
dataLen -= n;
data += n;
}
else
#endif
{
while (dataLen >= WC_SHA3_256_COUNT * 8) {
xorbuf(state, data, WC_SHA3_256_COUNT * 8);
dataLen -= WC_SHA3_256_COUNT * 8;
data += WC_SHA3_256_COUNT * 8;
BlockSha3(state);
}
}
}
if (dataLen > 0) {
xorbuf(state, data, dataLen);
}
state8[dataLen] ^= 0x1f;
state8[WC_SHA3_256_COUNT * 8 - 1] ^= 0x80;
}
else {
XMEMCPY(state, data, dataLen);
state8[dataLen] = 0x1f;
XMEMSET(state8 + dataLen + 1, 0, sizeof(shake256->s) - (dataLen + 1));
state8[WC_SHA3_256_COUNT * 8 - 1] ^= 0x80;
}
#ifndef WC_SHA3_NO_ASM
if (IS_INTEL_AVX2(cpuid_flags) && (SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_avx2(state);
RESTORE_VECTOR_REGISTERS();
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_bmi2(state);
}
else
#endif
{
BlockSha3(state);
}
if (hash != (byte*)shake256->s) {
XMEMCPY(hash, shake256->s, hashLen);
}
ret = 0;
#else
/* Initialize SHAKE-256 operation. */
ret = wc_InitShake256(shake256, NULL, INVALID_DEVID);
if (ret == 0) {
/* Update with data. */
ret = wc_Shake256_Update(shake256, data, dataLen);
}
if (ret == 0) {
/* Compute hash of data. */
ret = wc_Shake256_Final(shake256, hash, hashLen);
}
#endif
return ret;
}
/* 256-bit hash using SHAKE-256.
*
* This is the domain-separated version of the hash.
* See FIPS 204. D.3.
*
* FIPS 204. 8.3: H(v,d) <- SHAKE256(v,d)
*
* @param [in, out] shake256 SHAKE-256 object.
* @param [in] data1 First block of data to hash.
* @param [in] data1Len Length of first block in bytes.
* @param [in] data2 Second block of data to hash.
* @param [in] data2Len Length of second block in bytes.
* @param [out] hash Buffer to hold hash result.
* @param [in] hashLen Number of bytes of hash to return.
* @return 0 on success.
* @return Negative on error.
*/
static int dilithium_hash256(wc_Shake* shake256, const byte* data1,
word32 data1Len, const byte* data2, word32 data2Len, byte* hash,
word32 hashLen)
{
int ret;
#ifdef USE_INTEL_SPEEDUP
word64* state = shake256->s;
word8 *state8 = (word8*)state;
if (data2Len > (WOLFSSL_MAX_32BIT - data1Len)) {
return BAD_FUNC_ARG;
}
if (data1Len + data2Len >= WC_SHA3_256_COUNT * 8) {
XMEMCPY(state8, data1, data1Len);
XMEMCPY(state8 + data1Len, data2, WC_SHA3_256_COUNT * 8 - data1Len);
XMEMSET(state + WC_SHA3_256_COUNT, 0,
sizeof(shake256->s) - WC_SHA3_256_COUNT * 8);
data2Len -= WC_SHA3_256_COUNT * 8 - data1Len;
data2 += WC_SHA3_256_COUNT * 8 - data1Len;
#ifndef WC_SHA3_NO_ASM
if (IS_INTEL_AVX2(cpuid_flags) &&
(SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_avx2(state);
RESTORE_VECTOR_REGISTERS();
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_bmi2(state);
}
else
#endif
{
BlockSha3(state);
}
if (data2Len >= WC_SHA3_256_COUNT * 8) {
#ifndef WC_SHA3_NO_ASM
word32 n = data2Len / (WC_SHA3_256_COUNT * 8);
if (IS_INTEL_AVX2(cpuid_flags) && (SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_n_avx2(state, data2, n, WC_SHA3_256_COUNT * 8);
RESTORE_VECTOR_REGISTERS();
n *= WC_SHA3_256_COUNT * 8;
data2Len -= n;
data2 += n;
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_n_bmi2(state, data2, n, WC_SHA3_256_COUNT * 8);
n *= WC_SHA3_256_COUNT * 8;
data2Len -= n;
data2 += n;
}
else
#endif
{
while (data2Len >= WC_SHA3_256_COUNT * 8) {
xorbuf(state, data2, WC_SHA3_256_COUNT * 8);
data2Len -= WC_SHA3_256_COUNT * 8;
data2 += WC_SHA3_256_COUNT * 8;
BlockSha3(state);
}
}
}
if (data2Len > 0) {
xorbuf(state, data2, data2Len);
}
state8[data2Len] ^= 0x1f;
state8[WC_SHA3_256_COUNT * 8 - 1] ^= 0x80;
}
else {
word32 dataLen = data1Len + data2Len;
XMEMCPY(state8, data1, data1Len);
XMEMCPY(state8 + data1Len, data2, data2Len);
state8[dataLen] = 0x1f;
XMEMSET(state8 + dataLen + 1, 0, sizeof(shake256->s) - (dataLen + 1));
state8[WC_SHA3_256_COUNT * 8 - 1] = 0x80;
}
#ifndef WC_SHA3_NO_ASM
if (IS_INTEL_AVX2(cpuid_flags) && (SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_avx2(state);
RESTORE_VECTOR_REGISTERS();
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_bmi2(state);
}
else
#endif
{
BlockSha3(state);
}
XMEMCPY(hash, shake256->s, hashLen);
ret = 0;
#else
/* Initialize SHAKE-256 operation. */
ret = wc_InitShake256(shake256, NULL, INVALID_DEVID);
if (ret == 0) {
/* Update with first data. */
ret = wc_Shake256_Update(shake256, data1, data1Len);
}
if (ret == 0) {
/* Update with second data. */
ret = wc_Shake256_Update(shake256, data2, data2Len);
}
if (ret == 0) {
/* Compute hash of data. */
ret = wc_Shake256_Final(shake256, hash, hashLen);
}
#endif
return ret;
}
#if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
/* 256-bit hash of context and message using SHAKE-256.
*
* FIPS 204. 5.2: Algorithm 2 ML-DSA.Sign(sk, M, ctx)
* ...
* 10: M' <- BytesToBits(IntegerToBytes(0, 1) || IntegerToBytes(|ctx|, 1) ||
* ctx) || M
* ...
*
* FIPS 204. 6.2: Algorithm 7 ML-DSA.Sign_internal(sk, M', rnd)
* ...
* 6: mu <- H(BytesToBits(tr)||M', 64))
* ...
*
* @param [in, out] shake256 SHAKE-256 object.
* @param [in] tr Public key hash.
* @param [in] trLen Length of public key hash in bytes.
* @param [in] preHash 0 when message was not hashed,
* 1 when message was hashed.
* @param [in] ctx Context of signature.
* @param [in] ctxLen Length of context of signature in bytes.
* @param [in] msg Message to sign.
* @param [in] msgLen Length of message to sign in bytes.
* @param [out] hash Buffer to hold hash result.
* @param [in] hashLen Number of bytes of hash to return.
* @return 0 on success.
* @return Negative on error.
*/
static int dilithium_hash256_ctx_msg(wc_Shake* shake256, const byte* tr,
byte trLen, byte preHash, const byte* ctx, byte ctxLen, const byte* msg,
word32 msgLen, byte* hash, word32 hashLen)
{
int ret;
byte prefix[2];
prefix[0] = preHash;
prefix[1] = ctxLen;
/* Initialize SHAKE-256 operation. */
ret = wc_InitShake256(shake256, NULL, INVALID_DEVID);
if (ret == 0) {
/* Update with public key hash. */
ret = wc_Shake256_Update(shake256, tr, trLen);
}
if (ret == 0) {
/* Update with context prefix - 0 | ctxLen. */
ret = wc_Shake256_Update(shake256, prefix, (word32)sizeof(prefix));
}
if (ret == 0) {
/* Update with context. */
ret = wc_Shake256_Update(shake256, ctx, ctxLen);
}
if (ret == 0) {
/* Update with message. */
ret = wc_Shake256_Update(shake256, msg, msgLen);
}
if (ret == 0) {
/* Compute hash of data. */
ret = wc_Shake256_Final(shake256, hash, hashLen);
}
return ret;
}
/* Get the OID for the digest hash.
*
* @param [in] hash Hash algorithm.
* @param [out] oidBuffer Buffer to hold OID.
* @param [out] oidLen Length of OID in buffer.
* @return 0 on success.
* @return BAD_FUNC_ARG if hash algorithm not known.
*/
static int dilithium_get_hash_oid(int hash, byte* oidBuffer, word32* oidLen)
{
int ret = 0;
const byte* oid;
#ifndef WOLFSSL_DILITHIUM_NO_ASN1
oid = OidFromId((word32)wc_HashGetOID((enum wc_HashType)hash), oidHashType,
oidLen);
if ((oid != NULL) && (*oidLen <= DILITHIUM_HASH_OID_LEN - 2)) {
#ifndef WOLFSSL_DILITHIUM_REVERSE_HASH_OID
oidBuffer[0] = 0x06; /* ObjectID */
oidBuffer[1] = (byte)*oidLen; /* ObjectID */
oidBuffer += 2;
XMEMCPY(oidBuffer, oid, *oidLen);
#else
int i;
for (i = (int)*oidLen - 1; i >= 0; i--) {
*(oidBuffer++) = oid[i];
}
*(oidBuffer++) = *oidLen; /* ObjectID */
* oidBuffer = 0x06; /* ObjectID */
#endif
*oidLen += 2;
}
else {
ret = BAD_FUNC_ARG;
}
#else
*oidLen = DILITHIUM_HASH_OID_LEN;
#ifndef NO_SHA256
if (hash == WC_HASH_TYPE_SHA256) {
static byte sha256Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
};
oid = sha256Oid;
}
else
#endif
#ifdef WOLFSSL_SHA384
if (hash == WC_HASH_TYPE_SHA384) {
static byte sha384Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
};
oid = sha384Oid;
}
else
#endif
#ifdef WOLFSSL_SHA512
if (hash == WC_HASH_TYPE_SHA512) {
static byte sha512Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
};
oid = sha512Oid;
}
else
#ifndef WOLFSSL_NOSHA512_224
if (hash == WC_HASH_TYPE_SHA512_224) {
static byte sha512_224Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05
};
oid = sha512_224Oid;
}
else
#endif
#ifndef WOLFSSL_NOSHA512_256
if (hash == WC_HASH_TYPE_SHA512_256) {
static byte sha512_256Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x06
};
oid = sha512_256Oid;
}
else
#endif
#endif
if (hash == WC_HASH_TYPE_SHAKE128) {
static byte shake128Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0B
};
oid = shake128Oid;
}
else if (hash == WC_HASH_TYPE_SHAKE256) {
static byte shake256Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0C
};
oid = shake256Oid;
}
else if (hash == WC_HASH_TYPE_SHA3_256) {
static byte sha3_256Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08
};
oid = sha3_256Oid;
}
else if (hash == WC_HASH_TYPE_SHA3_384) {
static byte sha3_384Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x09
};
oid = sha3_384Oid;
}
else if (hash == WC_HASH_TYPE_SHA3_512) {
static byte sha3_512Oid[DILITHIUM_HASH_OID_LEN] = {
0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0A
};
oid = sha3_512Oid;
}
else {
oid = NULL;
ret = BAD_FUNC_ARG;
}
if ((oid != NULL) && (*oidLen <= DILITHIUM_HASH_OID_LEN)) {
#ifndef WOLFSSL_DILITHIUM_REVERSE_HASH_OID
XMEMCPY(oidBuffer, oid, *oidLen);
#else
int i;
for (i = (int)*oidLen - 1; i >= 0; i--) {
*(oidBuffer++) = oid[i];
}
#endif
}
#endif
return ret;
}
#endif
#ifndef WOLFSSL_DILITHIUM_SMALL
/* 128-bit hash using SHAKE-128.
*
* FIPS 204. 8.3: H128(v,d) <- SHAKE128(v,d)
*
* @param [in, out] shake128 SHAKE-128 object.
* @param [in] in Block of data to hash.
* @param [in] inLen Length of data in bytes.
* @param [out] out Buffer to hold hash result.
* @param [in] outLen Number of hash blocks to return.
* @return 0 on success.
* @return Negative on error.
*/
static int dilithium_squeeze128(wc_Shake* shake128, const byte* in,
word32 inLen, byte* out, word32 outBlocks)
{
int ret;
/* Initialize SHAKE-128 operation. */
ret = wc_InitShake128(shake128, NULL, INVALID_DEVID);
if (ret == 0) {
/* Absorb data - update plus final. */
ret = wc_Shake128_Absorb(shake128, in, inLen);
}
if (ret == 0) {
/* Squeeze out hash data. */
ret = wc_Shake128_SqueezeBlocks(shake128, out, outBlocks);
}
return ret;
}
#endif /* WOLFSSL_DILITHIUM_SMALL */
#if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || \
(!defined(WOLFSSL_DILITHIUM_SMALL) && \
!defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY))
/* 256-bit hash using SHAKE-256.
*
* FIPS 204. 8.3: H(v,d) <- SHAKE256(v,d)
* Using SqueezeBlocks interface to get larger amounts of output.
*
* @param [in, out] shake256 SHAKE-256 object.
* @param [in] in Block of data to hash.
* @param [in] inLen Length of data in bytes.
* @param [out] out Buffer to hold hash result.
* @param [in] outLen Number of hash blocks to return.
* @return 0 on success.
* @return Negative on hash error.
*/
static int dilithium_squeeze256(wc_Shake* shake256, const byte* in,
word32 inLen, byte* out, word32 outBlocks)
{
int ret;
#ifdef USE_INTEL_SPEEDUP
word64* state = shake256->s;
word8* state8 = (word8*)state;
XMEMCPY(state, in, inLen);
state8[inLen] = 0x1f;
XMEMSET(state8 + inLen + 1, 0, sizeof(shake256->s) - (inLen + 1));
state8[WC_SHA3_256_COUNT * 8 - 1] ^= 0x80;
for (; outBlocks > 0; outBlocks--) {
#ifndef WC_SHA3_NO_ASM
if (IS_INTEL_AVX2(cpuid_flags) &&
(SAVE_VECTOR_REGISTERS2() == 0)) {
sha3_block_avx2(state);
RESTORE_VECTOR_REGISTERS();
}
else if (IS_INTEL_BMI2(cpuid_flags)) {
sha3_block_bmi2(state);
}
else
#endif
{
BlockSha3(state);
}
XMEMCPY(out, shake256->s, WC_SHA3_256_COUNT * 8);
out += WC_SHA3_256_COUNT * 8;
}
ret = 0;
#else
/* Initialize SHAKE-256 operation. */
ret = wc_InitShake256(shake256, NULL, INVALID_DEVID);
if (ret == 0) {
/* Absorb data - update plus final. */
ret = wc_Shake256_Absorb(shake256, in, inLen);
}
if (ret == 0) {
/* Squeeze out hash data. */
ret = wc_Shake256_SqueezeBlocks(shake256, out, outBlocks);
}
#endif
return ret;
}
#endif
/******************************************************************************
* Encode/Decode operations
******************************************************************************/
#ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
/* Encode vector of polynomials with range -ETA..ETA.
*
* FIPS 204. 8.2: Algorithm 18 skEncode(rho, K, tr, s1, s2, t0)
* ...
* 2: for i from 0 to l - 1 do
* 3: sk <- sk || BitPack(s1[i], eta, eta)
* 4: end for
* ...
* OR
* ...
* 5: for i from 0 to k - 1 do
* 6: sk <- sk || BitPack(s2[i], eta, eta)
* 7: end for
* ...