-
Notifications
You must be signed in to change notification settings - Fork 93
/
oqsprov_keys.c
1870 lines (1719 loc) · 74.1 KB
/
oqsprov_keys.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
// SPDX-License-Identifier: Apache-2.0 AND MIT
/*
* OQS OpenSSL 3 key handler.
*
* Code strongly inspired by OpenSSL crypto/ec key handler but relocated here
* to have code within provider.
*
*/
#include <assert.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <string.h>
#include "oqs_prov.h"
#ifdef NDEBUG
#define OQS_KEY_PRINTF(a)
#define OQS_KEY_PRINTF2(a, b)
#define OQS_KEY_PRINTF3(a, b, c)
#else
#define OQS_KEY_PRINTF(a) \
if (getenv("OQSKEY")) \
printf(a)
#define OQS_KEY_PRINTF2(a, b) \
if (getenv("OQSKEY")) \
printf(a, b)
#define OQS_KEY_PRINTF3(a, b, c) \
if (getenv("OQSKEY")) \
printf(a, b, c)
#endif // NDEBUG
typedef enum { KEY_OP_PUBLIC, KEY_OP_PRIVATE, KEY_OP_KEYGEN } oqsx_key_op_t;
/// NID/name table
typedef struct {
int nid;
char *tlsname;
char *oqsname;
int keytype;
int secbits;
} oqs_nid_name_t;
static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op);
///// OQS_TEMPLATE_FRAGMENT_OQSNAMES_START
#ifdef OQS_KEM_ENCODERS
#define NID_TABLE_LEN 109
#else
#define NID_TABLE_LEN 56
#endif
static oqs_nid_name_t nid_names[NID_TABLE_LEN] = {
#ifdef OQS_KEM_ENCODERS
{0, "frodo640aes", OQS_KEM_alg_frodokem_640_aes, KEY_TYPE_KEM, 128},
{0, "p256_frodo640aes", OQS_KEM_alg_frodokem_640_aes, KEY_TYPE_ECP_HYB_KEM,
128},
{0, "x25519_frodo640aes", OQS_KEM_alg_frodokem_640_aes,
KEY_TYPE_ECX_HYB_KEM, 128},
{0, "frodo640shake", OQS_KEM_alg_frodokem_640_shake, KEY_TYPE_KEM, 128},
{0, "p256_frodo640shake", OQS_KEM_alg_frodokem_640_shake,
KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_frodo640shake", OQS_KEM_alg_frodokem_640_shake,
KEY_TYPE_ECX_HYB_KEM, 128},
{0, "frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_KEM, 192},
{0, "p384_frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_ECP_HYB_KEM,
192},
{0, "x448_frodo976aes", OQS_KEM_alg_frodokem_976_aes, KEY_TYPE_ECX_HYB_KEM,
192},
{0, "frodo976shake", OQS_KEM_alg_frodokem_976_shake, KEY_TYPE_KEM, 192},
{0, "p384_frodo976shake", OQS_KEM_alg_frodokem_976_shake,
KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_frodo976shake", OQS_KEM_alg_frodokem_976_shake,
KEY_TYPE_ECX_HYB_KEM, 192},
{0, "frodo1344aes", OQS_KEM_alg_frodokem_1344_aes, KEY_TYPE_KEM, 256},
{0, "p521_frodo1344aes", OQS_KEM_alg_frodokem_1344_aes,
KEY_TYPE_ECP_HYB_KEM, 256},
{0, "frodo1344shake", OQS_KEM_alg_frodokem_1344_shake, KEY_TYPE_KEM, 256},
{0, "p521_frodo1344shake", OQS_KEM_alg_frodokem_1344_shake,
KEY_TYPE_ECP_HYB_KEM, 256},
{0, "kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_KEM, 128},
{0, "p256_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_kyber512", OQS_KEM_alg_kyber_512, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_KEM, 192},
{0, "p384_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "x25519_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "p256_kyber768", OQS_KEM_alg_kyber_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_KEM, 256},
{0, "p521_kyber1024", OQS_KEM_alg_kyber_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_KEM, 128},
{0, "p256_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_mlkem512", OQS_KEM_alg_ml_kem_512, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_KEM, 192},
{0, "p384_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "x25519_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "p256_mlkem768", OQS_KEM_alg_ml_kem_768, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_KEM, 256},
{0, "p521_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "p384_mlkem1024", OQS_KEM_alg_ml_kem_1024, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_KEM, 128},
{0, "p256_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_bikel1", OQS_KEM_alg_bike_l1, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_KEM, 192},
{0, "p384_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_bikel3", OQS_KEM_alg_bike_l3, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_KEM, 256},
{0, "p521_bikel5", OQS_KEM_alg_bike_l5, KEY_TYPE_ECP_HYB_KEM, 256},
{0, "hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_KEM, 128},
{0, "p256_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECP_HYB_KEM, 128},
{0, "x25519_hqc128", OQS_KEM_alg_hqc_128, KEY_TYPE_ECX_HYB_KEM, 128},
{0, "hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_KEM, 192},
{0, "p384_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECP_HYB_KEM, 192},
{0, "x448_hqc192", OQS_KEM_alg_hqc_192, KEY_TYPE_ECX_HYB_KEM, 192},
{0, "hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_KEM, 256},
{0, "p521_hqc256", OQS_KEM_alg_hqc_256, KEY_TYPE_ECP_HYB_KEM, 256},
#endif /* OQS_KEM_ENCODERS */
{0, "dilithium2", OQS_SIG_alg_dilithium_2, KEY_TYPE_SIG, 128},
{0, "p256_dilithium2", OQS_SIG_alg_dilithium_2, KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_dilithium2", OQS_SIG_alg_dilithium_2, KEY_TYPE_HYB_SIG, 128},
{0, "dilithium3", OQS_SIG_alg_dilithium_3, KEY_TYPE_SIG, 192},
{0, "p384_dilithium3", OQS_SIG_alg_dilithium_3, KEY_TYPE_HYB_SIG, 192},
{0, "dilithium5", OQS_SIG_alg_dilithium_5, KEY_TYPE_SIG, 256},
{0, "p521_dilithium5", OQS_SIG_alg_dilithium_5, KEY_TYPE_HYB_SIG, 256},
{0, "mldsa44", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_SIG, 128},
{0, "p256_mldsa44", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_mldsa44", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_HYB_SIG, 128},
{0, "mldsa44_pss2048", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_CMP_SIG, 112},
{0, "mldsa44_rsa2048", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_CMP_SIG, 112},
{0, "mldsa44_ed25519", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa44_p256", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa44_bp256", OQS_SIG_alg_ml_dsa_44, KEY_TYPE_CMP_SIG, 256},
{0, "mldsa65", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_SIG, 192},
{0, "p384_mldsa65", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_HYB_SIG, 192},
{0, "mldsa65_pss3072", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa65_rsa3072", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa65_p256", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa65_bp256", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_CMP_SIG, 256},
{0, "mldsa65_ed25519", OQS_SIG_alg_ml_dsa_65, KEY_TYPE_CMP_SIG, 128},
{0, "mldsa87", OQS_SIG_alg_ml_dsa_87, KEY_TYPE_SIG, 256},
{0, "p521_mldsa87", OQS_SIG_alg_ml_dsa_87, KEY_TYPE_HYB_SIG, 256},
{0, "mldsa87_p384", OQS_SIG_alg_ml_dsa_87, KEY_TYPE_CMP_SIG, 192},
{0, "mldsa87_bp384", OQS_SIG_alg_ml_dsa_87, KEY_TYPE_CMP_SIG, 384},
{0, "mldsa87_ed448", OQS_SIG_alg_ml_dsa_87, KEY_TYPE_CMP_SIG, 192},
{0, "falcon512", OQS_SIG_alg_falcon_512, KEY_TYPE_SIG, 128},
{0, "p256_falcon512", OQS_SIG_alg_falcon_512, KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_falcon512", OQS_SIG_alg_falcon_512, KEY_TYPE_HYB_SIG, 128},
{0, "falconpadded512", OQS_SIG_alg_falcon_padded_512, KEY_TYPE_SIG, 128},
{0, "p256_falconpadded512", OQS_SIG_alg_falcon_padded_512, KEY_TYPE_HYB_SIG,
128},
{0, "rsa3072_falconpadded512", OQS_SIG_alg_falcon_padded_512,
KEY_TYPE_HYB_SIG, 128},
{0, "falcon1024", OQS_SIG_alg_falcon_1024, KEY_TYPE_SIG, 256},
{0, "p521_falcon1024", OQS_SIG_alg_falcon_1024, KEY_TYPE_HYB_SIG, 256},
{0, "falconpadded1024", OQS_SIG_alg_falcon_padded_1024, KEY_TYPE_SIG, 256},
{0, "p521_falconpadded1024", OQS_SIG_alg_falcon_padded_1024,
KEY_TYPE_HYB_SIG, 256},
{0, "sphincssha2128fsimple", OQS_SIG_alg_sphincs_sha2_128f_simple,
KEY_TYPE_SIG, 128},
{0, "p256_sphincssha2128fsimple", OQS_SIG_alg_sphincs_sha2_128f_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_sphincssha2128fsimple", OQS_SIG_alg_sphincs_sha2_128f_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "sphincssha2128ssimple", OQS_SIG_alg_sphincs_sha2_128s_simple,
KEY_TYPE_SIG, 128},
{0, "p256_sphincssha2128ssimple", OQS_SIG_alg_sphincs_sha2_128s_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_sphincssha2128ssimple", OQS_SIG_alg_sphincs_sha2_128s_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "sphincssha2192fsimple", OQS_SIG_alg_sphincs_sha2_192f_simple,
KEY_TYPE_SIG, 192},
{0, "p384_sphincssha2192fsimple", OQS_SIG_alg_sphincs_sha2_192f_simple,
KEY_TYPE_HYB_SIG, 192},
{0, "sphincsshake128fsimple", OQS_SIG_alg_sphincs_shake_128f_simple,
KEY_TYPE_SIG, 128},
{0, "p256_sphincsshake128fsimple", OQS_SIG_alg_sphincs_shake_128f_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "rsa3072_sphincsshake128fsimple", OQS_SIG_alg_sphincs_shake_128f_simple,
KEY_TYPE_HYB_SIG, 128},
{0, "mayo1", OQS_SIG_alg_mayo_1, KEY_TYPE_SIG, 128},
{0, "p256_mayo1", OQS_SIG_alg_mayo_1, KEY_TYPE_HYB_SIG, 128},
{0, "mayo2", OQS_SIG_alg_mayo_2, KEY_TYPE_SIG, 128},
{0, "p256_mayo2", OQS_SIG_alg_mayo_2, KEY_TYPE_HYB_SIG, 128},
{0, "mayo3", OQS_SIG_alg_mayo_3, KEY_TYPE_SIG, 192},
{0, "p384_mayo3", OQS_SIG_alg_mayo_3, KEY_TYPE_HYB_SIG, 192},
{0, "mayo5", OQS_SIG_alg_mayo_5, KEY_TYPE_SIG, 256},
{0, "p521_mayo5", OQS_SIG_alg_mayo_5, KEY_TYPE_HYB_SIG, 256},
///// OQS_TEMPLATE_FRAGMENT_OQSNAMES_END
};
int oqs_set_nid(char *tlsname, int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (!strcmp(nid_names[i].tlsname, tlsname)) {
nid_names[i].nid = nid;
return 1;
}
}
return 0;
}
static int get_secbits(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].nid == nid)
return nid_names[i].secbits;
}
return 0;
}
static int get_keytype(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].nid == nid)
return nid_names[i].keytype;
}
return 0;
}
char *get_oqsname_fromtls(char *tlsname) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].keytype == KEY_TYPE_SIG) {
if (!strcmp(nid_names[i].oqsname, tlsname) ||
!strcmp(nid_names[i].tlsname, tlsname))
return nid_names[i].oqsname;
}
}
return 0; // classical
}
char *get_oqsname(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].nid == nid)
return nid_names[i].oqsname;
}
return 0;
}
char *get_cmpname(int nid, int index) {
int i, len;
char *name, *s;
if ((i = get_oqsalg_idx(nid)) == -1)
return NULL;
s = nid_names[i].tlsname;
len = strlen(s);
for (i = 0; i < len; i++) {
if (s[i] == '_') {
break;
}
}
switch (index) {
case 0:
name = OPENSSL_strndup(s, i);
break;
case 1:
i += 1;
name = OPENSSL_strndup(s + i, len - i);
break;
default:
name = NULL;
}
return name;
}
int get_oqsalg_idx(int nid) {
int i;
for (i = 0; i < NID_TABLE_LEN; i++) {
if (nid_names[i].nid == nid)
return i;
}
return -1;
}
/* Prepare composite data structures. RetVal 0 is error. */
static int oqsx_key_set_composites(OQSX_KEY *key) {
int ret = 1;
OQS_KEY_PRINTF2("Setting composites with evp_info %p\n", key->evp_info);
if (key->numkeys == 1) {
key->comp_privkey[0] = key->privkey;
key->comp_pubkey[0] = key->pubkey;
} else { // TBD: extend for more than 1 classic key or first OQS for
// composite:
if (key->keytype == KEY_TYPE_CMP_SIG) {
int i;
int privlen = 0;
int publen = 0;
for (i = 0; i < key->numkeys; i++) {
if (key->privkey) {
key->comp_privkey[i] = (char *)key->privkey + privlen;
privlen += key->privkeylen_cmp[i];
} else {
key->comp_privkey[i] = NULL;
}
if (key->pubkey) {
key->comp_pubkey[i] = (char *)key->pubkey + publen;
publen += key->pubkeylen_cmp[i];
} else {
key->comp_pubkey[i] = NULL;
}
}
} else {
uint32_t classic_pubkey_len = 0;
uint32_t classic_privkey_len = 0;
if (key->privkey) {
key->comp_privkey[0] = (char *)key->privkey + SIZE_OF_UINT32;
DECODE_UINT32(classic_privkey_len, key->privkey);
if (classic_privkey_len > key->evp_info->length_private_key) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err;
}
key->comp_privkey[1] =
(char *)key->privkey + classic_privkey_len + SIZE_OF_UINT32;
} else {
key->comp_privkey[0] = NULL;
key->comp_privkey[1] = NULL;
}
if (key->pubkey) {
key->comp_pubkey[0] = (char *)key->pubkey + SIZE_OF_UINT32;
DECODE_UINT32(classic_pubkey_len, key->pubkey);
if (classic_pubkey_len > key->evp_info->length_public_key) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err;
}
key->comp_pubkey[1] =
(char *)key->pubkey + classic_pubkey_len + SIZE_OF_UINT32;
} else {
key->comp_pubkey[0] = NULL;
key->comp_pubkey[1] = NULL;
}
}
}
err:
return ret;
}
PROV_OQS_CTX *oqsx_newprovctx(OSSL_LIB_CTX *libctx,
const OSSL_CORE_HANDLE *handle, BIO_METHOD *bm) {
PROV_OQS_CTX *ret = OPENSSL_zalloc(sizeof(PROV_OQS_CTX));
if (ret) {
ret->libctx = libctx;
ret->handle = handle;
ret->corebiometh = bm;
}
return ret;
}
void oqsx_freeprovctx(PROV_OQS_CTX *ctx) {
OSSL_LIB_CTX_free(ctx->libctx);
BIO_meth_free(ctx->corebiometh);
OPENSSL_free(ctx);
}
void oqsx_key_set0_libctx(OQSX_KEY *key, OSSL_LIB_CTX *libctx) {
key->libctx = libctx;
}
/* convenience function creating OQSX keys from nids (only for sigs) */
static OQSX_KEY *oqsx_key_new_from_nid(OSSL_LIB_CTX *libctx, const char *propq,
int nid) {
OQS_KEY_PRINTF2("Generating OQSX key for nid %d\n", nid);
char *tls_algname = (char *)OBJ_nid2sn(nid);
OQS_KEY_PRINTF2(" for tls_name %s\n", tls_algname);
if (!tls_algname) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_WRONG_PARAMETERS);
return NULL;
}
return oqsx_key_new(libctx, get_oqsname(nid), tls_algname, get_keytype(nid),
propq, get_secbits(nid), get_oqsalg_idx(nid));
}
/* Workaround for not functioning EC PARAM initialization
* TBD, check https://github.com/openssl/openssl/issues/16989
*/
EVP_PKEY *setECParams(EVP_PKEY *eck, int nid) {
const unsigned char p256params[] = {0x06, 0x08, 0x2a, 0x86, 0x48,
0xce, 0x3d, 0x03, 0x01, 0x07};
const unsigned char p384params[] = {0x06, 0x05, 0x2b, 0x81,
0x04, 0x00, 0x22};
const unsigned char p521params[] = {0x06, 0x05, 0x2b, 0x81,
0x04, 0x00, 0x23};
const unsigned char bp256params[] = {0x06, 0x09, 0x2b, 0x24, 0x03, 0x03,
0x02, 0x08, 0x01, 0x01, 0x07};
const unsigned char bp384params[] = {0x06, 0x09, 0x2b, 0x24, 0x03, 0x03,
0x02, 0x08, 0x01, 0x01, 0x0b};
const unsigned char *params;
switch (nid) {
case NID_X9_62_prime256v1:
params = p256params;
return d2i_KeyParams(EVP_PKEY_EC, &eck, ¶ms, sizeof(p256params));
case NID_secp384r1:
params = p384params;
return d2i_KeyParams(EVP_PKEY_EC, &eck, ¶ms, sizeof(p384params));
case NID_secp521r1:
params = p521params;
return d2i_KeyParams(EVP_PKEY_EC, &eck, ¶ms, sizeof(p521params));
case NID_brainpoolP256r1:
params = bp256params;
return d2i_KeyParams(EVP_PKEY_EC, &eck, ¶ms, sizeof(bp256params));
case NID_brainpoolP384r1:
params = bp384params;
return d2i_KeyParams(EVP_PKEY_EC, &eck, ¶ms, sizeof(bp384params));
default:
return NULL;
}
}
/* Key codes */
static const OQSX_EVP_INFO nids_sig[] = {
{EVP_PKEY_EC, NID_X9_62_prime256v1, 0, 65, 121, 32, 72}, // 128 bit
{EVP_PKEY_EC, NID_secp384r1, 0, 97, 167, 48, 104}, // 192 bit
{EVP_PKEY_EC, NID_secp521r1, 0, 133, 223, 66, 141}, // 256 bit
{EVP_PKEY_EC, NID_brainpoolP256r1, 0, 65, 122, 32, 72}, // 256 bit
{EVP_PKEY_EC, NID_brainpoolP384r1, 0, 97, 171, 48, 104}, // 384 bit
{EVP_PKEY_RSA, NID_rsaEncryption, 0, 398, 1770, 0, 384}, // 128 bit
{EVP_PKEY_RSA, NID_rsaEncryption, 0, 270, 1193, 0, 256}, // 112 bit
{EVP_PKEY_ED25519, NID_ED25519, 1, 32, 32, 32, 72}, // 128 bit
{EVP_PKEY_ED448, NID_ED448, 1, 57, 57, 57, 122}, // 192 bit
};
// These two array need to stay synced:
// note only leading 4 chars of alg name are checked
static const char *OQSX_ECP_NAMES[] = {"p256", "p384", "p521", 0};
static const OQSX_EVP_INFO nids_ecp[] = {
{EVP_PKEY_EC, NID_X9_62_prime256v1, 0, 65, 121, 32, 0}, // 128 bit
{EVP_PKEY_EC, NID_secp384r1, 0, 97, 167, 48, 0}, // 192 bit
{EVP_PKEY_EC, NID_secp521r1, 0, 133, 223, 66, 0} // 256 bit
};
// These two array need to stay synced:
// note only leading 4 chars of alg name are checked
static const char *OQSX_ECX_NAMES[] = {"x25519", "x448", 0};
static const OQSX_EVP_INFO nids_ecx[] = {
{EVP_PKEY_X25519, 0, 1, 32, 32, 32, 0}, // 128 bit
{EVP_PKEY_X448, 0, 1, 56, 56, 56, 0}, // 192 bit
{0, 0, 0, 0, 0, 0, 0} // 256 bit
};
static int oqsx_hybsig_init(int bit_security, OQSX_EVP_CTX *evp_ctx,
char *algname) {
int ret = 1;
int idx = (bit_security - 128) / 64;
ON_ERR_GOTO(idx < 0 || idx > 5, err_init);
if (!strncmp(algname, "rsa", 3) || !strncmp(algname, "pss", 3)) {
idx += 5;
if (bit_security == 112)
idx += 1;
} else if (algname[0] != 'p' && algname[0] != 'e') {
if (algname[0] == 'b') { // bp
if (algname[2] == '2') // bp256
idx += 1;
} else {
OQS_KEY_PRINTF2("OQS KEY: Incorrect hybrid name: %s\n", algname);
ret = 0;
goto err_init;
}
}
ON_ERR_GOTO(idx < 0 || idx > 6, err_init);
if (algname[0] == 'e') // ED25519 or ED448
{
evp_ctx->evp_info = &nids_sig[idx + 7];
evp_ctx->keyParam = EVP_PKEY_new();
ON_ERR_SET_GOTO(!evp_ctx->keyParam, ret, -1, err_init);
ret = EVP_PKEY_set_type(evp_ctx->keyParam, evp_ctx->evp_info->keytype);
ON_ERR_SET_GOTO(ret <= 0, ret, -1, err_init);
evp_ctx->ctx = EVP_PKEY_CTX_new(evp_ctx->keyParam, NULL);
ON_ERR_SET_GOTO(!evp_ctx->ctx, ret, -1, err_init);
} else {
evp_ctx->evp_info = &nids_sig[idx];
evp_ctx->ctx = EVP_PKEY_CTX_new_id(evp_ctx->evp_info->keytype, NULL);
ON_ERR_GOTO(!evp_ctx->ctx, err_init);
if (idx < 5) { // EC
ret = EVP_PKEY_paramgen_init(evp_ctx->ctx);
ON_ERR_GOTO(ret <= 0, err_init);
ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(
evp_ctx->ctx, evp_ctx->evp_info->nid);
ON_ERR_GOTO(ret <= 0, free_evp_ctx);
ret = EVP_PKEY_paramgen(evp_ctx->ctx, &evp_ctx->keyParam);
ON_ERR_GOTO(ret <= 0 || !evp_ctx->keyParam, free_evp_ctx);
}
}
// RSA bit length set only during keygen
goto err_init;
free_evp_ctx:
EVP_PKEY_CTX_free(evp_ctx->ctx);
evp_ctx->ctx = NULL;
err_init:
return ret;
}
static const int oqshybkem_init_ecp(char *tls_name, OQSX_EVP_CTX *evp_ctx) {
int ret = 1;
int idx = 0;
while (idx < OSSL_NELEM(OQSX_ECP_NAMES)) {
if (!strncmp(tls_name, OQSX_ECP_NAMES[idx], 4))
break;
idx++;
}
ON_ERR_GOTO(idx < 0 || idx > 2, err_init_ecp);
evp_ctx->evp_info = &nids_ecp[idx];
evp_ctx->ctx = EVP_PKEY_CTX_new_id(evp_ctx->evp_info->keytype, NULL);
ON_ERR_GOTO(!evp_ctx->ctx, err_init_ecp);
ret = EVP_PKEY_paramgen_init(evp_ctx->ctx);
ON_ERR_GOTO(ret <= 0, err_init_ecp);
ret = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(evp_ctx->ctx,
evp_ctx->evp_info->nid);
ON_ERR_GOTO(ret <= 0, err_init_ecp);
ret = EVP_PKEY_paramgen(evp_ctx->ctx, &evp_ctx->keyParam);
ON_ERR_GOTO(ret <= 0 || !evp_ctx->keyParam, err_init_ecp);
err_init_ecp:
return ret;
}
static const int oqshybkem_init_ecx(char *tls_name, OQSX_EVP_CTX *evp_ctx) {
int ret = 1;
int idx = 0;
while (idx < OSSL_NELEM(OQSX_ECX_NAMES)) {
if (!strncmp(tls_name, OQSX_ECX_NAMES[idx], 4))
break;
idx++;
}
ON_ERR_GOTO(idx < 0 || idx > 2, err_init_ecx);
evp_ctx->evp_info = &nids_ecx[idx];
evp_ctx->keyParam = EVP_PKEY_new();
ON_ERR_SET_GOTO(!evp_ctx->keyParam, ret, -1, err_init_ecx);
ret = EVP_PKEY_set_type(evp_ctx->keyParam, evp_ctx->evp_info->keytype);
ON_ERR_SET_GOTO(ret <= 0, ret, -1, err_init_ecx);
evp_ctx->ctx = EVP_PKEY_CTX_new(evp_ctx->keyParam, NULL);
ON_ERR_SET_GOTO(!evp_ctx->ctx, ret, -1, err_init_ecx);
err_init_ecx:
return ret;
}
/* Re-create OQSX_KEY from encoding(s): Same end-state as after ken-gen */
static OQSX_KEY *oqsx_key_op(const X509_ALGOR *palg, const unsigned char *p,
int plen, oqsx_key_op_t op, OSSL_LIB_CTX *libctx,
const char *propq) {
OQSX_KEY *key = NULL;
void **privkey, **pubkey;
int nid = NID_undef;
int ret = 0;
OQS_KEY_PRINTF2("OQSX KEY: key_op called with data of len %d\n", plen);
if (palg != NULL) {
int ptype;
/* Algorithm parameters must be absent */
X509_ALGOR_get0(NULL, &ptype, NULL, palg);
if (ptype != V_ASN1_UNDEF || !palg || !palg->algorithm) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
return 0;
}
nid = OBJ_obj2nid(palg->algorithm);
}
if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
return 0;
}
key = oqsx_key_new_from_nid(libctx, propq, nid);
if (key == NULL) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
return 0;
}
OQS_KEY_PRINTF2("OQSX KEY: Recreated OQSX key %s\n", key->tls_name);
if (op == KEY_OP_PUBLIC) {
if (key->pubkeylen != plen) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
if (oqsx_key_allocate_keymaterial(key, 0)) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
goto err_key_op;
}
memcpy(key->pubkey, p, plen);
} else {
uint32_t classical_privatekey_len = 0;
// for plain OQS keys, we expect OQS priv||OQS pub key
size_t actualprivkeylen = key->privkeylen;
// for hybrid keys, we expect classic priv key||OQS priv key||OQS
// pub key classic pub key must/can be re-created from classic
// private key
if (key->keytype == KEY_TYPE_CMP_SIG) {
uint32_t privlen = 0;
size_t publen = 0;
size_t previous_privlen = 0;
size_t previous_publen = 0;
size_t temp_pub_len, temp_priv_len;
char *temp_priv, *temp_pub;
int pqc_pub_enc = 0;
int i;
// check if key is the right size
for (i = 0; i < key->numkeys; i++) {
char *name;
if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) ==
NULL) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
privlen = key->privkeylen_cmp[i];
if (get_oqsname_fromtls(name) == 0) { // classical key
publen = 0;
} else { // PQC key
publen = key->pubkeylen_cmp[i]; // pubkey in
// PQC privkey
// is OPTIONAL
}
previous_privlen += privlen;
previous_publen += publen;
OPENSSL_free(name);
}
if (previous_privlen != plen) {
// is ok, PQC pubkey might be in privkey
pqc_pub_enc = 1;
if (previous_privlen + previous_publen != plen) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
if (oqsx_key_allocate_keymaterial(key, 0)) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
goto err_key_op;
}
}
if (oqsx_key_allocate_keymaterial(key, 1)) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
goto err_key_op;
}
temp_priv_len = previous_privlen;
temp_pub_len = previous_publen;
temp_priv = OPENSSL_secure_zalloc(temp_priv_len);
temp_pub = OPENSSL_secure_zalloc(temp_pub_len);
previous_privlen = 0;
previous_publen = 0;
for (i = 0; i < key->numkeys; i++) {
size_t classic_publen = 0;
char *name;
if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) ==
NULL) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
OPENSSL_secure_clear_free(temp_priv, temp_priv_len);
OPENSSL_secure_clear_free(temp_pub, temp_pub_len);
goto err_key_op;
}
if (get_oqsname_fromtls(name) == 0) { // classical key
publen = 0; // no pubkey encoded with privkey
// on classical keys. will
// recreate the pubkey later
if (key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype ==
EVP_PKEY_RSA) { // get the RSA real key size
if (previous_privlen + previous_publen + 4 > plen) {
OPENSSL_free(name);
OPENSSL_secure_clear_free(temp_priv, temp_priv_len);
OPENSSL_secure_clear_free(temp_pub, temp_pub_len);
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
unsigned char *enc_len =
(unsigned char *)OPENSSL_strndup(
(const char *)(p + previous_privlen +
previous_publen),
4);
OPENSSL_cleanse(enc_len, 2);
DECODE_UINT32(privlen, enc_len);
privlen += 4;
OPENSSL_free(enc_len);
if (privlen > key->privkeylen_cmp[i]) {
OPENSSL_free(name);
OPENSSL_secure_clear_free(temp_priv, temp_priv_len);
OPENSSL_secure_clear_free(temp_pub, temp_pub_len);
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
key->privkeylen_cmp[i] = privlen;
} else
privlen = key->privkeylen_cmp[i];
} else { // PQC key
privlen = key->privkeylen_cmp[i];
if (pqc_pub_enc)
publen = key->pubkeylen_cmp[i];
else
publen = 0;
}
if (previous_privlen + previous_publen + privlen > plen) {
OPENSSL_free(name);
OPENSSL_secure_clear_free(temp_priv, temp_priv_len);
OPENSSL_secure_clear_free(temp_pub, temp_pub_len);
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
memcpy(temp_priv + previous_privlen,
p + previous_privlen + previous_publen, privlen);
memcpy(temp_pub + previous_publen,
p + privlen + previous_privlen + previous_publen,
publen);
previous_privlen += privlen;
previous_publen += publen;
OPENSSL_free(name);
}
memcpy(key->privkey, temp_priv, previous_privlen);
memcpy(key->pubkey, temp_pub, previous_publen);
OPENSSL_secure_clear_free(temp_priv, temp_priv_len);
OPENSSL_secure_clear_free(temp_pub, temp_pub_len);
} else {
if (key->numkeys == 2) {
size_t expected_pq_privkey_len =
key->oqsx_provider_ctx.oqsx_qs_ctx.kem->length_secret_key;
#ifndef NOPUBKEY_IN_PRIVKEY
expected_pq_privkey_len +=
key->oqsx_provider_ctx.oqsx_qs_ctx.kem->length_public_key;
#endif
if (plen > (SIZE_OF_UINT32 + expected_pq_privkey_len)) {
size_t max_classical_privkey_len =
key->evp_info->length_private_key;
size_t space_for_classical_privkey =
plen - expected_pq_privkey_len - SIZE_OF_UINT32;
if (space_for_classical_privkey >
max_classical_privkey_len) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
DECODE_UINT32(classical_privatekey_len,
p); // actual classic key len
if (classical_privatekey_len !=
space_for_classical_privkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
} else {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
actualprivkeylen -= (key->evp_info->length_private_key -
classical_privatekey_len);
}
#ifdef NOPUBKEY_IN_PRIVKEY
if (actualprivkeylen != plen) {
OQS_KEY_PRINTF3("OQSX KEY: private key with "
"unexpected length %d vs %d\n",
plen, (int)(actualprivkeylen));
#else
if (actualprivkeylen + oqsx_key_get_oqs_public_key_len(key) !=
plen) {
OQS_KEY_PRINTF3("OQSX KEY: private key with unexpected length "
"%d vs %d\n",
plen,
(int)(actualprivkeylen +
oqsx_key_get_oqs_public_key_len(key)));
#endif
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
if (oqsx_key_allocate_keymaterial(key, 1)
#ifndef NOPUBKEY_IN_PRIVKEY
|| oqsx_key_allocate_keymaterial(key, 0)
#endif
) {
ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
goto err_key_op;
}
// first populate private key data
memcpy(key->privkey, p, actualprivkeylen);
#ifndef NOPUBKEY_IN_PRIVKEY
// only enough data to fill public OQS key component
if (oqsx_key_get_oqs_public_key_len(key) !=
plen - actualprivkeylen) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto err_key_op;
}
// populate OQS public key structure
if (key->numkeys == 2) {
unsigned char *pubkey = (unsigned char *)key->pubkey;
ENCODE_UINT32(pubkey, key->evp_info->length_public_key);
memcpy(pubkey + SIZE_OF_UINT32 +
key->evp_info->length_public_key,
p + actualprivkeylen, plen - actualprivkeylen);
} else
memcpy(key->pubkey, p + key->privkeylen,
plen - key->privkeylen);
#endif
}
}
if (!oqsx_key_set_composites(key) || !oqsx_key_recreate_classickey(key, op))
goto err_key_op;
return key;
err_key_op:
oqsx_key_free(key);
return NULL;
}
/* Recreate EVP data structure after import. RetVal 0 is error. */
static int oqsx_key_recreate_classickey(OQSX_KEY *key, oqsx_key_op_t op) {
if (key->keytype == KEY_TYPE_CMP_SIG) {
int i;
if (op == KEY_OP_PUBLIC) {
for (i = 0; i < key->numkeys; i++) {
char *name;
if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) ==
NULL) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
const unsigned char *enc_pubkey = key->comp_pubkey[i];
if (get_oqsname_fromtls(name) == 0) {
if (!key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->raw_key_support) {
EVP_PKEY *npk = EVP_PKEY_new();
if (key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype != EVP_PKEY_RSA) {
npk = setECParams(npk,
key->oqsx_provider_ctx
.oqsx_evp_ctx->evp_info->nid);
}
key->classical_pkey = d2i_PublicKey(
key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype,
&npk, &enc_pubkey, key->pubkeylen_cmp[i]);
} else
key->classical_pkey = EVP_PKEY_new_raw_public_key(
key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype,
NULL, enc_pubkey, key->pubkeylen_cmp[i]);
if (!key->classical_pkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
OPENSSL_free(name);
goto rec_err;
}
}
OPENSSL_free(name);
}
}
if (op == KEY_OP_PRIVATE) {
for (i = 0; i < key->numkeys; i++) {
char *name;
if ((name = get_cmpname(OBJ_sn2nid(key->tls_name), i)) ==
NULL) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
if (get_oqsname_fromtls(name) == 0) {
const unsigned char *enc_privkey = key->comp_privkey[i];
if (!key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->raw_key_support) {
EVP_PKEY *npk;
key->classical_pkey = d2i_PrivateKey(
key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype,
NULL, &enc_privkey, key->privkeylen_cmp[i]);
} else {
key->classical_pkey = EVP_PKEY_new_raw_private_key(
key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->keytype,
NULL, enc_privkey, key->privkeylen_cmp[i]);
}
if (!key->classical_pkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
OPENSSL_free(name);
goto rec_err;
}
if (!key->oqsx_provider_ctx.oqsx_evp_ctx->evp_info
->raw_key_support) {
unsigned char *comp_pubkey = key->comp_pubkey[i];
int pubkeylen =
i2d_PublicKey(key->classical_pkey, &comp_pubkey);
if (pubkeylen != key->oqsx_provider_ctx.oqsx_evp_ctx
->evp_info->length_public_key) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
OPENSSL_free(name);
goto rec_err;
}
} else {
size_t pubkeylen = key->pubkeylen_cmp[i];
int ret = EVP_PKEY_get_raw_public_key(
key->classical_pkey, key->comp_pubkey[i],
&pubkeylen);
if (ret <= 0) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
OPENSSL_free(name);
goto rec_err;
}
}
}
OPENSSL_free(name);
}
}
} else {
if (key->numkeys == 2) { // hybrid key
uint32_t classical_pubkey_len = 0;
uint32_t classical_privkey_len = 0;
if (!key->evp_info) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_EVPINFO_MISSING);
goto rec_err;
}
if (op == KEY_OP_PUBLIC) {
const unsigned char *enc_pubkey = key->comp_pubkey[0];
DECODE_UINT32(classical_pubkey_len, key->pubkey);
if (classical_pubkey_len > key->evp_info->length_public_key) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
if (key->evp_info->raw_key_support) {
key->classical_pkey = EVP_PKEY_new_raw_public_key(
key->evp_info->keytype, NULL, enc_pubkey,
classical_pubkey_len);
if (!key->classical_pkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
} else {
EVP_PKEY *npk = EVP_PKEY_new();
if (key->evp_info->keytype != EVP_PKEY_RSA) {
npk = setECParams(npk, key->evp_info->nid);
}
key->classical_pkey =
d2i_PublicKey(key->evp_info->keytype, &npk, &enc_pubkey,
classical_pubkey_len);
if (!key->classical_pkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
EVP_PKEY_free(npk);
goto rec_err;
}
}
}
if (op == KEY_OP_PRIVATE) {
DECODE_UINT32(classical_privkey_len, key->privkey);
if (classical_privkey_len > key->evp_info->length_private_key) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
const unsigned char *enc_privkey = key->comp_privkey[0];
unsigned char *enc_pubkey = key->comp_pubkey[0];
if (key->evp_info->raw_key_support) {
key->classical_pkey = EVP_PKEY_new_raw_private_key(
key->evp_info->keytype, NULL, enc_privkey,
classical_privkey_len);
if (!key->classical_pkey) {
ERR_raise(ERR_LIB_USER, OQSPROV_R_INVALID_ENCODING);
goto rec_err;
}
#ifndef NOPUBKEY_IN_PRIVKEY
// re-create classic public key part from
// private key:
size_t pubkeylen;
EVP_PKEY_get_raw_public_key(key->classical_pkey, NULL,
&pubkeylen);
if (pubkeylen != key->evp_info->length_public_key ||
EVP_PKEY_get_raw_public_key(
key->classical_pkey, enc_pubkey, &pubkeylen) != 1) {