forked from openssl/openssl
-
Notifications
You must be signed in to change notification settings - Fork 126
/
oqs_meth.c
1865 lines (1698 loc) · 62.6 KB
/
oqs_meth.c
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
/* OQS authentication methods.
*
* This file mimics ecx_meth.c. Compare the oqs* with the ecx* functions
* to understand the code.
*
* TODO:
* - Improve error reporting. Define OQS specific error codes, using util/mkerr.pl?
* (or perhaps re-use EC_* values?
* - Add tests
* - FIXMEOQS: which RSA NID should I use in hybrid sig? NID_rsaencryption is used by "req" when using "rsa"?
*/
/* OQS note:
In addition to post-quantum (PQ) signatures; we also support classical/PQ hybrids. In that case, a classical and a PQ signature
are generated on the same data, and the resulting signatures are concatenated; the classical and PQ keys are also concatenated
when serialized. The signed data is first hashed using the SHA-2 hash function matching the security level of the OQS scheme
(SHA256 for L1, SHA384 for L2/L3, SHA512 for L4/L5) before being signed by the classical algorithm (which can't support
arbitrarily long messages), and is passed directly to the OQS signature API. The hybrid scheme is identified as a new combo
scheme with a unique NID. Currently, ECDSA-p256 and RSA3072 hybrids are supported with L1 OQS schemes, and ECDSA-p384 hybrids
are supported with L3 schemes. The public and private keys are also concatenated when serialized. Encoding of artefacts (keys
and signatures) are as follow:
- classical_artefact_length: 4 bytes encoding the size of the classical artefact
- classical_artefact: the classical artefact of length classical_artefact_length
- oqs_artefact: the post-quantum artefact, of length determined by the OQS signature context
*/
#include <stdio.h>
#include "internal/cryptlib.h"
#include <openssl/x509.h>
#include <openssl/rsa.h>
#include "crypto/asn1.h"
#include "crypto/evp.h"
#include <openssl/cms.h>
#include <oqs/oqs.h>
#define SIZE_OF_UINT32 4
#define ENCODE_UINT32(pbuf, i) (pbuf)[0] = (unsigned char)((i>>24) & 0xff); \
(pbuf)[1] = (unsigned char)((i>>16) & 0xff); \
(pbuf)[2] = (unsigned char)((i>> 8) & 0xff); \
(pbuf)[3] = (unsigned char)((i ) & 0xff)
#define DECODE_UINT32(i, pbuf) i = ((uint32_t) (pbuf)[0]) << 24; \
i |= ((uint32_t) (pbuf)[1]) << 16; \
i |= ((uint32_t) (pbuf)[2]) << 8; \
i |= ((uint32_t) (pbuf)[3])
/*
* OQS context
*/
typedef struct
{
/* OpenSSL NID */
int nid;
/* OQS signature context */
OQS_SIG *s;
/* OQS public key */
uint8_t *pubkey;
/* OQS private key */
uint8_t *privkey;
/* Classical key pair for hybrid schemes; either a private or public key depending on context */
EVP_PKEY *classical_pkey;
/* Security bits for the scheme */
int security_bits;
/* digest engine for CMS: */
EVP_MD_CTX * digest;
} OQS_KEY;
/*
* OQS key type
*/
typedef enum {
KEY_TYPE_PUBLIC,
KEY_TYPE_PRIVATE,
} oqs_key_type_t;
int oqssl_sig_nids_list[] = {
///// OQS_TEMPLATE_FRAGMENT_LIST_KNOWN_NIDS_START
NID_dilithium2,
NID_p256_dilithium2,
NID_rsa3072_dilithium2,
NID_dilithium3,
NID_p384_dilithium3,
NID_dilithium5,
NID_p521_dilithium5,
NID_dilithium2_aes,
NID_p256_dilithium2_aes,
NID_rsa3072_dilithium2_aes,
NID_dilithium3_aes,
NID_p384_dilithium3_aes,
NID_dilithium5_aes,
NID_p521_dilithium5_aes,
NID_falcon512,
NID_p256_falcon512,
NID_rsa3072_falcon512,
NID_falcon1024,
NID_p521_falcon1024,
NID_picnicl1full,
NID_p256_picnicl1full,
NID_rsa3072_picnicl1full,
NID_picnic3l1,
NID_p256_picnic3l1,
NID_rsa3072_picnic3l1,
NID_rainbowIclassic,
NID_p256_rainbowIclassic,
NID_rsa3072_rainbowIclassic,
NID_rainbowVclassic,
NID_p521_rainbowVclassic,
NID_sphincsharaka128frobust,
NID_p256_sphincsharaka128frobust,
NID_rsa3072_sphincsharaka128frobust,
NID_sphincssha256128frobust,
NID_p256_sphincssha256128frobust,
NID_rsa3072_sphincssha256128frobust,
NID_sphincsshake256128frobust,
NID_p256_sphincsshake256128frobust,
NID_rsa3072_sphincsshake256128frobust,
/////// OQS_TEMPLATE_FRAGMENT_LIST_KNOWN_NIDS_END
};
int oqssl_kem_nids_list[] = {
///// OQS_TEMPLATE_FRAGMENT_LIST_KNOWN_KEM_NIDS_START
NID_frodo640aes,
NID_frodo640shake,
NID_frodo976aes,
NID_frodo976shake,
NID_frodo1344aes,
NID_frodo1344shake,
NID_kyber512,
NID_kyber768,
NID_kyber1024,
NID_ntru_hps2048509,
NID_ntru_hps2048677,
NID_ntru_hps4096821,
NID_ntru_hrss701,
NID_lightsaber,
NID_saber,
NID_firesaber,
NID_sidhp434,
NID_sidhp503,
NID_sidhp610,
NID_sidhp751,
NID_sikep434,
NID_sikep503,
NID_sikep610,
NID_sikep751,
NID_bikel1,
NID_bikel3,
NID_kyber90s512,
NID_kyber90s768,
NID_kyber90s1024,
NID_hqc128,
NID_hqc192,
NID_hqc256,
NID_ntrulpr653,
NID_ntrulpr761,
NID_ntrulpr857,
NID_sntrup653,
NID_sntrup761,
NID_sntrup857,
/////// OQS_TEMPLATE_FRAGMENT_LIST_KNOWN_KEM_NIDS_END
};
static int* sig_nid_list = NULL;
static int* kem_nid_list = NULL;
int* get_oqssl_sig_nids() {
if (!sig_nid_list) {
sig_nid_list = OPENSSL_malloc(sizeof(oqssl_sig_nids_list));
memcpy(sig_nid_list, oqssl_sig_nids_list, sizeof(oqssl_sig_nids_list));
}
return sig_nid_list;
}
int* get_oqssl_kem_nids() {
if (!kem_nid_list) {
kem_nid_list = OPENSSL_malloc(sizeof(oqssl_kem_nids_list));
memcpy(kem_nid_list, oqssl_kem_nids_list, sizeof(oqssl_kem_nids_list));
}
return kem_nid_list;
}
/*
* Maps OpenSSL NIDs to OQS IDs
*/
char* get_oqs_alg_name(int openssl_nid)
{
switch (openssl_nid)
{
///// OQS_TEMPLATE_FRAGMENT_ASSIGN_SIG_ALG_START
case NID_dilithium2:
case NID_p256_dilithium2:
case NID_rsa3072_dilithium2:
return OQS_SIG_alg_dilithium_2;
case NID_dilithium3:
case NID_p384_dilithium3:
return OQS_SIG_alg_dilithium_3;
case NID_dilithium5:
case NID_p521_dilithium5:
return OQS_SIG_alg_dilithium_5;
case NID_dilithium2_aes:
case NID_p256_dilithium2_aes:
case NID_rsa3072_dilithium2_aes:
return OQS_SIG_alg_dilithium_2_aes;
case NID_dilithium3_aes:
case NID_p384_dilithium3_aes:
return OQS_SIG_alg_dilithium_3_aes;
case NID_dilithium5_aes:
case NID_p521_dilithium5_aes:
return OQS_SIG_alg_dilithium_5_aes;
case NID_falcon512:
case NID_p256_falcon512:
case NID_rsa3072_falcon512:
return OQS_SIG_alg_falcon_512;
case NID_falcon1024:
case NID_p521_falcon1024:
return OQS_SIG_alg_falcon_1024;
case NID_picnicl1full:
case NID_p256_picnicl1full:
case NID_rsa3072_picnicl1full:
return OQS_SIG_alg_picnic_L1_full;
case NID_picnic3l1:
case NID_p256_picnic3l1:
case NID_rsa3072_picnic3l1:
return OQS_SIG_alg_picnic3_L1;
case NID_rainbowIclassic:
case NID_p256_rainbowIclassic:
case NID_rsa3072_rainbowIclassic:
return OQS_SIG_alg_rainbow_I_classic;
case NID_rainbowVclassic:
case NID_p521_rainbowVclassic:
return OQS_SIG_alg_rainbow_V_classic;
case NID_sphincsharaka128frobust:
case NID_p256_sphincsharaka128frobust:
case NID_rsa3072_sphincsharaka128frobust:
return OQS_SIG_alg_sphincs_haraka_128f_robust;
case NID_sphincssha256128frobust:
case NID_p256_sphincssha256128frobust:
case NID_rsa3072_sphincssha256128frobust:
return OQS_SIG_alg_sphincs_sha256_128f_robust;
case NID_sphincsshake256128frobust:
case NID_p256_sphincsshake256128frobust:
case NID_rsa3072_sphincsshake256128frobust:
return OQS_SIG_alg_sphincs_shake256_128f_robust;
case NID_frodo640aes:
case NID_p256_frodo640aes:
return OQS_KEM_alg_frodokem_640_aes;
case NID_frodo640shake:
case NID_p256_frodo640shake:
return OQS_KEM_alg_frodokem_640_shake;
case NID_frodo976aes:
case NID_p384_frodo976aes:
return OQS_KEM_alg_frodokem_976_aes;
case NID_frodo976shake:
case NID_p384_frodo976shake:
return OQS_KEM_alg_frodokem_976_shake;
case NID_frodo1344aes:
case NID_p521_frodo1344aes:
return OQS_KEM_alg_frodokem_1344_aes;
case NID_frodo1344shake:
case NID_p521_frodo1344shake:
return OQS_KEM_alg_frodokem_1344_shake;
case NID_kyber512:
case NID_p256_kyber512:
return OQS_KEM_alg_kyber_512;
case NID_kyber768:
case NID_p384_kyber768:
return OQS_KEM_alg_kyber_768;
case NID_kyber1024:
case NID_p521_kyber1024:
return OQS_KEM_alg_kyber_1024;
case NID_ntru_hps2048509:
case NID_p256_ntru_hps2048509:
return OQS_KEM_alg_ntru_hps2048509;
case NID_ntru_hps2048677:
case NID_p384_ntru_hps2048677:
return OQS_KEM_alg_ntru_hps2048677;
case NID_ntru_hps4096821:
case NID_p521_ntru_hps4096821:
return OQS_KEM_alg_ntru_hps4096821;
case NID_ntru_hrss701:
case NID_p384_ntru_hrss701:
return OQS_KEM_alg_ntru_hrss701;
case NID_lightsaber:
case NID_p256_lightsaber:
return OQS_KEM_alg_saber_lightsaber;
case NID_saber:
case NID_p384_saber:
return OQS_KEM_alg_saber_saber;
case NID_firesaber:
case NID_p521_firesaber:
return OQS_KEM_alg_saber_firesaber;
case NID_sidhp434:
case NID_p256_sidhp434:
return OQS_KEM_alg_sidh_p434;
case NID_sidhp503:
case NID_p256_sidhp503:
return OQS_KEM_alg_sidh_p503;
case NID_sidhp610:
case NID_p384_sidhp610:
return OQS_KEM_alg_sidh_p610;
case NID_sidhp751:
case NID_p521_sidhp751:
return OQS_KEM_alg_sidh_p751;
case NID_sikep434:
case NID_p256_sikep434:
return OQS_KEM_alg_sike_p434;
case NID_sikep503:
case NID_p256_sikep503:
return OQS_KEM_alg_sike_p503;
case NID_sikep610:
case NID_p384_sikep610:
return OQS_KEM_alg_sike_p610;
case NID_sikep751:
case NID_p521_sikep751:
return OQS_KEM_alg_sike_p751;
case NID_bikel1:
case NID_p256_bikel1:
return OQS_KEM_alg_bike_l1;
case NID_bikel3:
case NID_p384_bikel3:
return OQS_KEM_alg_bike_l3;
case NID_kyber90s512:
case NID_p256_kyber90s512:
return OQS_KEM_alg_kyber_512_90s;
case NID_kyber90s768:
case NID_p384_kyber90s768:
return OQS_KEM_alg_kyber_768_90s;
case NID_kyber90s1024:
case NID_p521_kyber90s1024:
return OQS_KEM_alg_kyber_1024_90s;
case NID_hqc128:
case NID_p256_hqc128:
return OQS_KEM_alg_hqc_128;
case NID_hqc192:
case NID_p384_hqc192:
return OQS_KEM_alg_hqc_192;
case NID_hqc256:
case NID_p521_hqc256:
return OQS_KEM_alg_hqc_256;
case NID_ntrulpr653:
case NID_p256_ntrulpr653:
return OQS_KEM_alg_ntruprime_ntrulpr653;
case NID_ntrulpr761:
case NID_p384_ntrulpr761:
return OQS_KEM_alg_ntruprime_ntrulpr761;
case NID_ntrulpr857:
case NID_p384_ntrulpr857:
return OQS_KEM_alg_ntruprime_ntrulpr857;
case NID_sntrup653:
case NID_p256_sntrup653:
return OQS_KEM_alg_ntruprime_sntrup653;
case NID_sntrup761:
case NID_p384_sntrup761:
return OQS_KEM_alg_ntruprime_sntrup761;
case NID_sntrup857:
case NID_p384_sntrup857:
return OQS_KEM_alg_ntruprime_sntrup857;
///// OQS_TEMPLATE_FRAGMENT_ASSIGN_SIG_ALG_END
default:
return NULL;
}
}
static int is_oqs_hybrid_alg(int openssl_nid)
{
switch (openssl_nid)
{
///// OQS_TEMPLATE_FRAGMENT_LIST_HYBRID_NIDS_START
case NID_p256_dilithium2:
case NID_rsa3072_dilithium2:
case NID_p384_dilithium3:
case NID_p521_dilithium5:
case NID_p256_dilithium2_aes:
case NID_rsa3072_dilithium2_aes:
case NID_p384_dilithium3_aes:
case NID_p521_dilithium5_aes:
case NID_p256_falcon512:
case NID_rsa3072_falcon512:
case NID_p521_falcon1024:
case NID_p256_picnicl1full:
case NID_rsa3072_picnicl1full:
case NID_p256_picnic3l1:
case NID_rsa3072_picnic3l1:
case NID_p256_rainbowIclassic:
case NID_rsa3072_rainbowIclassic:
case NID_p521_rainbowVclassic:
case NID_p256_sphincsharaka128frobust:
case NID_rsa3072_sphincsharaka128frobust:
case NID_p256_sphincssha256128frobust:
case NID_rsa3072_sphincssha256128frobust:
case NID_p256_sphincsshake256128frobust:
case NID_rsa3072_sphincsshake256128frobust:
///// OQS_TEMPLATE_FRAGMENT_LIST_HYBRID_NIDS_END
return 1;
default:
return 0;
}
}
static int get_classical_nid(int hybrid_id)
{
switch (hybrid_id)
{
///// OQS_TEMPLATE_FRAGMENT_ASSIGN_CLASSICAL_NIDS_START
case NID_rsa3072_dilithium2:
case NID_rsa3072_dilithium2_aes:
case NID_rsa3072_falcon512:
case NID_rsa3072_picnicl1full:
case NID_rsa3072_picnic3l1:
case NID_rsa3072_rainbowIclassic:
case NID_rsa3072_sphincsharaka128frobust:
case NID_rsa3072_sphincssha256128frobust:
case NID_rsa3072_sphincsshake256128frobust:
return NID_rsaEncryption;
case NID_p256_dilithium2:
case NID_p256_dilithium2_aes:
case NID_p256_falcon512:
case NID_p256_picnicl1full:
case NID_p256_picnic3l1:
case NID_p256_rainbowIclassic:
case NID_p256_sphincsharaka128frobust:
case NID_p256_sphincssha256128frobust:
case NID_p256_sphincsshake256128frobust:
return NID_X9_62_prime256v1;
case NID_p384_dilithium3:
case NID_p384_dilithium3_aes:
return NID_secp384r1;
case NID_p521_dilithium5:
case NID_p521_dilithium5_aes:
case NID_p521_falcon1024:
case NID_p521_rainbowVclassic:
return NID_secp521r1;///// OQS_TEMPLATE_FRAGMENT_ASSIGN_CLASSICAL_NIDS_END
default:
return 0;
}
}
static int get_oqs_nid(int hybrid_id)
{
switch (hybrid_id)
{
///// OQS_TEMPLATE_FRAGMENT_ASSIGN_OQS_NID_START
case NID_p256_dilithium2:
case NID_rsa3072_dilithium2:
return NID_dilithium2;
case NID_p384_dilithium3:
return NID_dilithium3;
case NID_p521_dilithium5:
return NID_dilithium5;
case NID_p256_dilithium2_aes:
case NID_rsa3072_dilithium2_aes:
return NID_dilithium2_aes;
case NID_p384_dilithium3_aes:
return NID_dilithium3_aes;
case NID_p521_dilithium5_aes:
return NID_dilithium5_aes;
case NID_p256_falcon512:
case NID_rsa3072_falcon512:
return NID_falcon512;
case NID_p521_falcon1024:
return NID_falcon1024;
case NID_p256_picnicl1full:
case NID_rsa3072_picnicl1full:
return NID_picnicl1full;
case NID_p256_picnic3l1:
case NID_rsa3072_picnic3l1:
return NID_picnic3l1;
case NID_p256_rainbowIclassic:
case NID_rsa3072_rainbowIclassic:
return NID_rainbowIclassic;
case NID_p521_rainbowVclassic:
return NID_rainbowVclassic;
case NID_p256_sphincsharaka128frobust:
case NID_rsa3072_sphincsharaka128frobust:
return NID_sphincsharaka128frobust;
case NID_p256_sphincssha256128frobust:
case NID_rsa3072_sphincssha256128frobust:
return NID_sphincssha256128frobust;
case NID_p256_sphincsshake256128frobust:
case NID_rsa3072_sphincsshake256128frobust:
return NID_sphincsshake256128frobust;
///// OQS_TEMPLATE_FRAGMENT_ASSIGN_OQS_NID_END
default:
return 0;
}
}
static int get_classical_key_len(oqs_key_type_t keytype, int classical_id) {
switch (classical_id)
{
case NID_rsaEncryption:
return (keytype == KEY_TYPE_PRIVATE) ? 1770 : 398;
case NID_X9_62_prime256v1:
return (keytype == KEY_TYPE_PRIVATE) ? 121 : 65;
case NID_secp384r1:
return (keytype == KEY_TYPE_PRIVATE) ? 167 : 97;
case NID_secp521r1:
return (keytype == KEY_TYPE_PRIVATE) ? 223 : 133;
default:
return 0;
}
}
static int get_classical_sig_len(int classical_id)
{
switch (classical_id)
{
case NID_rsaEncryption:
return 384;
case NID_X9_62_prime256v1:
return 72;
case NID_secp384r1:
return 104;
case NID_secp521r1:
return 141;
default:
return 0;
}
}
/*
* Returns options when running OQS KEM, e.g., in openssl speed
*/
const char *OQSKEM_options(void)
{
int offset;
// TODO: Revisit which OQS_COMPILE_FLAGS to show
#ifdef OQS_COMPILE_CFLAGS
const char* OQSKEMALGS = "OQS KEM build : ";
char* result = OPENSSL_zalloc(strlen(OQS_COMPILE_CFLAGS)+OQS_OPENSSL_KEM_algs_length*40); // OK, a bit pessimistic but this will be removed very soon...
memcpy(result, OQSKEMALGS, offset = strlen(OQSKEMALGS));
memcpy(result+offset, OQS_COMPILE_CFLAGS, strlen(OQS_COMPILE_CFLAGS));
offset += strlen(OQS_COMPILE_CFLAGS);
#else
const char* OQSKEMALGS = "";
char* result = OPENSSL_zalloc(OQS_OPENSSL_KEM_algs_length*40); // OK, a bit pessimistic but this will be removed very soon...
memcpy(result, OQSKEMALGS, offset = strlen(OQSKEMALGS));
#endif
result[offset++]='-';
int i;
for (i=0; i<OQS_OPENSSL_KEM_algs_length;i++) {
const char* name = OBJ_nid2sn(oqssl_kem_nids_list[i]);
if (OQS_KEM_alg_is_enabled(get_oqs_alg_name(oqssl_kem_nids_list[i]))) {
int l = strlen(name);
memcpy(result+offset, name, l);
if (i<OQS_OPENSSL_KEM_algs_length-1) {
result[offset+l]=',';
offset = offset+l+1;
}
}
}
return result;
}
/*
* Returns options when running OQS SIG, e.g., in openssl speed
*/
const char *OQSSIG_options(void)
{
int offset;
// TODO: Revisit which OQS_COMPILE_FLAGS to show
#ifdef OQS_COMPILE_CFLAGS
const char* OQSSIGALGS = "OQS SIG build : ";
char* result = OPENSSL_zalloc(strlen(OQS_COMPILE_CFLAGS)+OQS_OPENSSL_SIG_algs_length*40); // OK, a bit pessimistic but this will be removed very soon...
memcpy(result, OQSSIGALGS, offset = strlen(OQSSIGALGS));
memcpy(result+offset, OQS_COMPILE_CFLAGS, strlen(OQS_COMPILE_CFLAGS));
offset += strlen(OQS_COMPILE_CFLAGS);
#else
const char* OQSSIGALGS = "";
char* result = OPENSSL_zalloc(OQS_OPENSSL_SIG_algs_length*40); // OK, a bit pessimistic but this will be removed very soon...
memcpy(result, OQSSIGALGS, offset = strlen(OQSSIGALGS));
#endif
result[offset++]='-';
int i;
for (i=0; i<OQS_OPENSSL_SIG_algs_length;i++) {
const char* name = OBJ_nid2sn(oqssl_sig_nids_list[i]);
if (OQS_SIG_alg_is_enabled(get_oqs_alg_name(oqssl_sig_nids_list[i]))) {
int l = strlen(name);
memcpy(result+offset, name, l);
if (i<OQS_OPENSSL_SIG_algs_length-1) {
result[offset+l]=',';
offset = offset+l+1;
}
}
}
return result;
}
/*
* Returns the security level in bits for an OQS alg.
*/
static int get_oqs_security_bits(int openssl_nid)
{
switch (openssl_nid)
{
///// OQS_TEMPLATE_FRAGMENT_GET_SIG_SECURITY_BITS_START
case NID_dilithium2:
case NID_p256_dilithium2:
case NID_rsa3072_dilithium2:
return 128;
case NID_dilithium3:
case NID_p384_dilithium3:
return 192;
case NID_dilithium5:
case NID_p521_dilithium5:
return 256;
case NID_dilithium2_aes:
case NID_p256_dilithium2_aes:
case NID_rsa3072_dilithium2_aes:
return 128;
case NID_dilithium3_aes:
case NID_p384_dilithium3_aes:
return 192;
case NID_dilithium5_aes:
case NID_p521_dilithium5_aes:
return 256;
case NID_falcon512:
case NID_p256_falcon512:
case NID_rsa3072_falcon512:
return 128;
case NID_falcon1024:
case NID_p521_falcon1024:
return 256;
case NID_picnicl1full:
case NID_p256_picnicl1full:
case NID_rsa3072_picnicl1full:
return 128;
case NID_picnic3l1:
case NID_p256_picnic3l1:
case NID_rsa3072_picnic3l1:
return 128;
case NID_rainbowIclassic:
case NID_p256_rainbowIclassic:
case NID_rsa3072_rainbowIclassic:
return 128;
case NID_rainbowVclassic:
case NID_p521_rainbowVclassic:
return 256;
case NID_sphincsharaka128frobust:
case NID_p256_sphincsharaka128frobust:
case NID_rsa3072_sphincsharaka128frobust:
return 128;
case NID_sphincssha256128frobust:
case NID_p256_sphincssha256128frobust:
case NID_rsa3072_sphincssha256128frobust:
return 128;
case NID_sphincsshake256128frobust:
case NID_p256_sphincsshake256128frobust:
case NID_rsa3072_sphincsshake256128frobust:
return 128;
///// OQS_TEMPLATE_FRAGMENT_GET_SIG_SECURITY_BITS_END
default:
return 0;
}
}
static int is_EC_nid(int nid) {
return (nid == NID_X9_62_prime256v1 || nid == NID_secp384r1 || nid == NID_secp521r1);
}
static int decode_EC_key(oqs_key_type_t keytype, int nid, const unsigned char* encoded_key, int key_len, OQS_KEY* oqs_key) {
EC_GROUP *ecgroup = NULL;
EC_KEY *ec_key = NULL;
const unsigned char* p_encoded_key = encoded_key;
int rv = 0;
/* I can't figure out how to import the EC key with the high-level EVP API: the d2i_* functions complain
that the EC group is missing. If I set it manually (creating a group and using EC_KEY_set_group to set
it on a EC_KEY and assign it to a EVP_PKEY, the group gets erased by EVP_PKEY_set_type inside the d2i_*
functions. I therefore use lower-level functions for EC algs.
*/
if ((ecgroup = EC_GROUP_new_by_curve_name(nid)) == NULL) {
ECerr(0, ERR_R_FATAL);
goto end;
}
if ((ec_key = EC_KEY_new()) == NULL ||
!EC_KEY_set_group(ec_key, ecgroup)){
ECerr(0, ERR_R_FATAL);
goto end;
}
if (keytype == KEY_TYPE_PRIVATE) {
if (d2i_ECPrivateKey(&ec_key, &p_encoded_key, key_len) == NULL) {
ECerr(0, ERR_R_FATAL);
goto end;
}
} else {
if (o2i_ECPublicKey(&ec_key, &p_encoded_key, key_len) == NULL) {
ECerr(0, ERR_R_FATAL);
goto end;
}
}
if ((oqs_key->classical_pkey = EVP_PKEY_new()) == NULL ||
!EVP_PKEY_set_type(oqs_key->classical_pkey, NID_X9_62_id_ecPublicKey) ||
!EVP_PKEY_assign_EC_KEY(oqs_key->classical_pkey, ec_key)) {
ECerr(0, ERR_R_FATAL);
goto end;
}
rv = 1; /* success */
end:
if (rv == 0 && ecgroup) EC_GROUP_free(ecgroup);
if (rv == 0 && ec_key) EC_KEY_free(ec_key);
return rv;
}
/*
* Frees the OQS_KEY, including its keys.
*/
static void oqs_pkey_ctx_free(OQS_KEY* key) {
int privkey_len = 0;
if (key == NULL) {
return;
}
if (key->s) {
privkey_len = key->s->length_secret_key;
OQS_SIG_free(key->s);
}
if (key->privkey) {
OPENSSL_secure_clear_free(key->privkey, privkey_len);
}
if (key->pubkey) {
OPENSSL_free(key->pubkey);
}
if (key->classical_pkey) {
EVP_PKEY_free(key->classical_pkey);
}
OPENSSL_free(key);
}
/*
* Initializes a OQS_KEY, given an OpenSSL NID. This function only initializes
* the post-quantum key, not the classical one (for hybrid schemes)
*/
static int oqs_key_init(OQS_KEY **p_oqs_key, int nid, oqs_key_type_t keytype) {
OQS_KEY *oqs_key = NULL;
const char* oqs_alg_name = get_oqs_alg_name(nid);
oqs_key = OPENSSL_zalloc(sizeof(*oqs_key));
if (oqs_key == NULL) {
ECerr(0, ERR_R_MALLOC_FAILURE);
goto err;
}
oqs_key->nid = nid;
if (!OQS_SIG_alg_is_enabled(oqs_alg_name))
fprintf(stderr, "Warning: OQS algorithm '%s' not enabled.\n", oqs_alg_name);
oqs_key->s = OQS_SIG_new(oqs_alg_name);
if (oqs_key->s == NULL) {
/* TODO: Perhaps even check if the alg is available earlier in the stack. */
ECerr(EC_F_OQS_KEY_INIT, EC_R_NO_SUCH_OQS_ALGORITHM);
goto err;
}
oqs_key->pubkey = OPENSSL_malloc(oqs_key->s->length_public_key);
if (oqs_key->pubkey == NULL) {
ECerr(0, ERR_R_MALLOC_FAILURE);
goto err;
}
/* Optionally allocate the private key */
if (keytype == KEY_TYPE_PRIVATE) {
oqs_key->privkey = OPENSSL_secure_malloc(oqs_key->s->length_secret_key);
if (oqs_key->privkey == NULL) {
ECerr(EC_F_OQS_KEY_INIT, ERR_R_MALLOC_FAILURE);
goto err;
}
}
oqs_key->security_bits = get_oqs_security_bits(nid);
*p_oqs_key = oqs_key;
return 1;
err:
oqs_pkey_ctx_free(oqs_key);
return 0;
}
static int oqs_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
{
const OQS_KEY *oqs_key = (OQS_KEY*) pkey->pkey.ptr;
unsigned char *penc;
uint32_t pubkey_len = 0, max_classical_pubkey_len = 0, classical_pubkey_len = 0, index = 0;
if (!oqs_key || !oqs_key->s || !oqs_key->pubkey ) {
ECerr(EC_F_OQS_PUB_ENCODE, EC_R_KEY_NOT_SET);
return 0;
}
int is_hybrid = (oqs_key->classical_pkey != NULL);
/* determine the length of the key */
pubkey_len = oqs_key->s->length_public_key;
if (is_hybrid) {
max_classical_pubkey_len = get_classical_key_len(KEY_TYPE_PUBLIC, get_classical_nid(oqs_key->nid));
pubkey_len += (SIZE_OF_UINT32 + max_classical_pubkey_len);
}
penc = OPENSSL_malloc(pubkey_len);
if (penc == NULL) {
ECerr(EC_F_OQS_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
/* if hybrid, encode classical public key */
if (is_hybrid) {
unsigned char *classical_pubkey = penc + SIZE_OF_UINT32; /* i2d moves target pointer, so we copy into a temp var (leaving space for key len) */
uint32_t actual_classical_pubkey_len = i2d_PublicKey(oqs_key->classical_pkey, &classical_pubkey);
if (actual_classical_pubkey_len < 0 || actual_classical_pubkey_len > max_classical_pubkey_len) {
/* something went wrong, or we didn't allocate enough space */
OPENSSL_free(penc);
ECerr(EC_F_OQS_PUB_ENCODE, ERR_R_FATAL);
return 0;
}
ENCODE_UINT32(penc, actual_classical_pubkey_len);
classical_pubkey_len = SIZE_OF_UINT32 + actual_classical_pubkey_len;
index += classical_pubkey_len;
}
/* encode the pqc public key */
memcpy(penc + index, oqs_key->pubkey, oqs_key->s->length_public_key);
/* recalculate pub key len using actual classical key len */
pubkey_len = classical_pubkey_len + oqs_key->s->length_public_key;
if (!X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
V_ASN1_UNDEF, NULL, penc, pubkey_len)) {
OPENSSL_free(penc);
ECerr(EC_F_OQS_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
}
static int oqs_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
{
const unsigned char *p;
int pklen, max_pubkey_len;
X509_ALGOR *palg;
OQS_KEY *oqs_key = NULL;
int id = pkey->ameth->pkey_id;
int is_hybrid = is_oqs_hybrid_alg(id);
int index = 0;
if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) {
return 0;
}
if (p == NULL) {
/* pklen is checked below, after we instantiate the oqs_key to
learn the max len */
ECerr(EC_F_OQS_PUB_DECODE, ERR_R_FATAL);
return 0;
}
if (palg != NULL) {
int ptype;
/* Algorithm parameters must be absent */
X509_ALGOR_get0(NULL, &ptype, NULL, palg);
if (ptype != V_ASN1_UNDEF) {
ECerr(EC_F_OQS_PUB_DECODE, EC_R_PARAMETERS_MUST_BE_ABSENT);
return 0;
}
}
if (!oqs_key_init(&oqs_key, id, 0)) {
ECerr(EC_F_OQS_PUB_DECODE, EC_R_KEY_INIT_FAILED);
return 0;
}
max_pubkey_len = oqs_key->s->length_public_key;
if (is_hybrid) {
max_pubkey_len += (SIZE_OF_UINT32 + get_classical_key_len(KEY_TYPE_PUBLIC, get_classical_nid(id)));
}
if (pklen > max_pubkey_len) {
ECerr(EC_F_OQS_PUB_DECODE, EC_R_WRONG_LENGTH);
goto err;
}
/* if hybrid, decode classical public key */
if (is_hybrid) {
int classical_id = get_classical_nid(id);
int actual_classical_pubkey_len;
DECODE_UINT32(actual_classical_pubkey_len, p);
if (is_EC_nid(classical_id)) {
if (!decode_EC_key(KEY_TYPE_PUBLIC, classical_id, p + SIZE_OF_UINT32, actual_classical_pubkey_len, oqs_key)) {
ECerr(EC_F_OQS_PUB_DECODE, ERR_R_FATAL);
goto err;
}
} else {
const unsigned char* pubkey_temp = p + SIZE_OF_UINT32;
oqs_key->classical_pkey = d2i_PublicKey(classical_id, &oqs_key->classical_pkey, &pubkey_temp, actual_classical_pubkey_len);
if (oqs_key->classical_pkey == NULL) {
ECerr(EC_F_OQS_PUB_DECODE, ERR_R_FATAL);
goto err;
}
}
index += (SIZE_OF_UINT32 + actual_classical_pubkey_len);
}
/* decode PQC public key */
memcpy(oqs_key->pubkey, p + index, oqs_key->s->length_public_key);
EVP_PKEY_assign(pkey, id, oqs_key);
return 1;
err:
oqs_pkey_ctx_free(oqs_key);
return 0;
}
static int oqs_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
{
const OQS_KEY *akey = (OQS_KEY*) a->pkey.ptr;
const OQS_KEY *bkey = (OQS_KEY*) b->pkey.ptr;
if (akey == NULL || bkey == NULL)
return -2;
/* compare hybrid classical key if present */
if (akey->classical_pkey != NULL) {
if (bkey->classical_pkey == NULL) {
return 0; /* both should be hybrid or not */
}
if (!EVP_PKEY_cmp(akey->classical_pkey, bkey->classical_pkey)) {
return 0;
}
}
/* compare PQC key */
return CRYPTO_memcmp(akey->pubkey, bkey->pubkey, akey->s->length_public_key) == 0;
}
static int oqs_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p;
int plen, max_privkey_len;
ASN1_OCTET_STRING *oct = NULL;
const X509_ALGOR *palg;
OQS_KEY *oqs_key = NULL;
int id = pkey->ameth->pkey_id;
int is_hybrid = is_oqs_hybrid_alg(id);
int index = 0;
if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8))
return 0;
oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
if (oct == NULL) {
p = NULL;
plen = 0;
} else {
p = ASN1_STRING_get0_data(oct);
plen = ASN1_STRING_length(oct);
}
/* oct contains first the private key, then the public key */
if (palg != NULL) {
int ptype;
/* Algorithm parameters must be absent */
X509_ALGOR_get0(NULL, &ptype, NULL, palg);
if (ptype != V_ASN1_UNDEF) {
ECerr(EC_F_OQS_PRIV_DECODE, ERR_R_FATAL);
return 0;
}
}
if (!oqs_key_init(&oqs_key, id, 1)) {
ECerr(EC_F_OQS_PRIV_DECODE, EC_R_KEY_INIT_FAILED);
return 0;
}
max_privkey_len = oqs_key->s->length_secret_key + oqs_key->s->length_public_key;
if (is_hybrid) {
max_privkey_len += (SIZE_OF_UINT32 + get_classical_key_len(KEY_TYPE_PRIVATE, get_classical_nid(oqs_key->nid)));
}
if (plen > max_privkey_len) {
ECerr(EC_F_OQS_PRIV_DECODE, EC_R_KEY_LENGTH_WRONG);
goto err;
}
/* if hybrid, decode classical private key */
if (is_hybrid) {
int classical_id = get_classical_nid(id);
int actual_classical_privkey_len;
DECODE_UINT32(actual_classical_privkey_len, p);
if (is_EC_nid(classical_id)) {
if (!decode_EC_key(KEY_TYPE_PRIVATE, classical_id, p + SIZE_OF_UINT32, actual_classical_privkey_len, oqs_key)) {
ECerr(EC_F_OQS_PRIV_DECODE, ERR_R_FATAL);
goto err;
}
} else {
const unsigned char* privkey_temp = p + SIZE_OF_UINT32;
oqs_key->classical_pkey = d2i_PrivateKey(classical_id, &oqs_key->classical_pkey, &privkey_temp, actual_classical_privkey_len);
if (oqs_key->classical_pkey == NULL) {
ECerr(EC_F_OQS_PRIV_DECODE, ERR_R_FATAL);
goto err;