Botan 3.7.1
Crypto and TLS for C&
pk_algs.cpp
Go to the documentation of this file.
1/*
2* PK Key
3* (C) 1999-2010,2016 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/pk_algs.h>
9
10#include <botan/assert.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/parsing.h>
13
14#if defined(BOTAN_HAS_RSA)
15 #include <botan/rsa.h>
16#endif
17
18#if defined(BOTAN_HAS_CLASSICMCELIECE)
19 #include <botan/cmce.h>
20#endif
21
22#if defined(BOTAN_HAS_DSA)
23 #include <botan/dsa.h>
24#endif
25
26#if defined(BOTAN_HAS_DL_GROUP)
27 #include <botan/dl_group.h>
28#endif
29
30#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
31 #include <botan/dh.h>
32#endif
33
34#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
35 #include <botan/ecc_key.h>
36#endif
37
38#if defined(BOTAN_HAS_ECDSA)
39 #include <botan/ecdsa.h>
40#endif
41
42#if defined(BOTAN_HAS_ECGDSA)
43 #include <botan/ecgdsa.h>
44#endif
45
46#if defined(BOTAN_HAS_ECKCDSA)
47 #include <botan/eckcdsa.h>
48#endif
49
50#if defined(BOTAN_HAS_ED25519)
51 #include <botan/ed25519.h>
52#endif
53
54#if defined(BOTAN_HAS_ED448)
55 #include <botan/ed448.h>
56#endif
57
58#if defined(BOTAN_HAS_GOST_34_10_2001)
59 #include <botan/gost_3410.h>
60#endif
61
62#if defined(BOTAN_HAS_ELGAMAL)
63 #include <botan/elgamal.h>
64#endif
65
66#if defined(BOTAN_HAS_ECDH)
67 #include <botan/ecdh.h>
68#endif
69
70#if defined(BOTAN_HAS_X25519)
71 #include <botan/x25519.h>
72#endif
73
74#if defined(BOTAN_HAS_X448)
75 #include <botan/x448.h>
76#endif
77
78#if defined(BOTAN_HAS_MCELIECE)
79 #include <botan/mceliece.h>
80#endif
81
82#if defined(BOTAN_HAS_FRODOKEM)
83 #include <botan/frodokem.h>
84#endif
85
86#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
87 #include <botan/kyber.h>
88#endif
89
90#if defined(BOTAN_HAS_ML_KEM)
91 #include <botan/ml_kem.h>
92#endif
93
94#if defined(BOTAN_HAS_HSS_LMS)
95 #include <botan/hss_lms.h>
96#endif
97
98#if defined(BOTAN_HAS_XMSS_RFC8391)
99 #include <botan/xmss.h>
100#endif
101
102#if defined(BOTAN_HAS_SM2)
103 #include <botan/sm2.h>
104#endif
105
106#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
107 #include <botan/dilithium.h>
108#endif
109
110#if defined(BOTAN_HAS_ML_DSA)
111 #include <botan/ml_dsa.h>
112#endif
113
114#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
115 #include <botan/sphincsplus.h>
116#endif
117
118#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
119 #include <botan/slh_dsa.h>
120#endif
121
122namespace Botan {
123
124std::unique_ptr<Public_Key> load_public_key(const AlgorithmIdentifier& alg_id,
125 [[maybe_unused]] std::span<const uint8_t> key_bits) {
126 const std::string oid_str = alg_id.oid().to_formatted_string();
127 const std::vector<std::string> alg_info = split_on(oid_str, '/');
128 std::string_view alg_name = alg_info[0];
129
130#if defined(BOTAN_HAS_RSA)
131 if(alg_name == "RSA") {
132 return std::make_unique<RSA_PublicKey>(alg_id, key_bits);
133 }
134#endif
135
136#if defined(BOTAN_HAS_X25519)
137 if(alg_name == "X25519" || alg_name == "Curve25519") {
138 return std::make_unique<X25519_PublicKey>(alg_id, key_bits);
139 }
140#endif
141
142#if defined(BOTAN_HAS_X448)
143 if(alg_name == "X448") {
144 return std::make_unique<X448_PublicKey>(alg_id, key_bits);
145 }
146#endif
147
148#if defined(BOTAN_HAS_MCELIECE)
149 if(alg_name == "McEliece") {
150 return std::make_unique<McEliece_PublicKey>(key_bits);
151 }
152#endif
153
154#if defined(BOTAN_HAS_FRODOKEM)
155 if(alg_name == "FrodoKEM" || alg_name.starts_with("FrodoKEM-") || alg_name.starts_with("eFrodoKEM-")) {
156 return std::make_unique<FrodoKEM_PublicKey>(alg_id, key_bits);
157 }
158#endif
159
160#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
161 if(alg_name == "Kyber" || alg_name.starts_with("Kyber-")) {
162 return std::make_unique<Kyber_PublicKey>(alg_id, key_bits);
163 }
164#endif
165
166#if defined(BOTAN_HAS_ML_KEM)
167 if(alg_name.starts_with("ML-KEM-")) {
168 return std::make_unique<ML_KEM_PublicKey>(alg_id, key_bits);
169 }
170#endif
171
172#if defined(BOTAN_HAS_ECDSA)
173 if(alg_name == "ECDSA") {
174 return std::make_unique<ECDSA_PublicKey>(alg_id, key_bits);
175 }
176#endif
177
178#if defined(BOTAN_HAS_ECDH)
179 if(alg_name == "ECDH") {
180 return std::make_unique<ECDH_PublicKey>(alg_id, key_bits);
181 }
182#endif
183
184#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
185 if(alg_name == "DH") {
186 return std::make_unique<DH_PublicKey>(alg_id, key_bits);
187 }
188#endif
189
190#if defined(BOTAN_HAS_DSA)
191 if(alg_name == "DSA") {
192 return std::make_unique<DSA_PublicKey>(alg_id, key_bits);
193 }
194#endif
195
196#if defined(BOTAN_HAS_ELGAMAL)
197 if(alg_name == "ElGamal") {
198 return std::make_unique<ElGamal_PublicKey>(alg_id, key_bits);
199 }
200#endif
201
202#if defined(BOTAN_HAS_ECGDSA)
203 if(alg_name == "ECGDSA") {
204 return std::make_unique<ECGDSA_PublicKey>(alg_id, key_bits);
205 }
206#endif
207
208#if defined(BOTAN_HAS_ECKCDSA)
209 if(alg_name == "ECKCDSA") {
210 return std::make_unique<ECKCDSA_PublicKey>(alg_id, key_bits);
211 }
212#endif
213
214#if defined(BOTAN_HAS_ED25519)
215 if(alg_name == "Ed25519") {
216 return std::make_unique<Ed25519_PublicKey>(alg_id, key_bits);
217 }
218#endif
219
220#if defined(BOTAN_HAS_ED448)
221 if(alg_name == "Ed448") {
222 return std::make_unique<Ed448_PublicKey>(alg_id, key_bits);
223 }
224#endif
225
226#if defined(BOTAN_HAS_GOST_34_10_2001)
227 if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
228 return std::make_unique<GOST_3410_PublicKey>(alg_id, key_bits);
229 }
230#endif
231
232#if defined(BOTAN_HAS_SM2)
233 if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
234 return std::make_unique<SM2_PublicKey>(alg_id, key_bits);
235 }
236#endif
237
238#if defined(BOTAN_HAS_XMSS_RFC8391)
239 if(alg_name == "XMSS") {
240 return std::make_unique<XMSS_PublicKey>(key_bits);
241 }
242#endif
243
244#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
245 if(alg_name == "Dilithium" || alg_name.starts_with("Dilithium-")) {
246 return std::make_unique<Dilithium_PublicKey>(alg_id, key_bits);
247 }
248#endif
249
250#if defined(BOTAN_HAS_ML_DSA)
251 if(alg_name.starts_with("ML-DSA-")) {
252 return std::make_unique<ML_DSA_PublicKey>(alg_id, key_bits);
253 }
254#endif
255
256#if defined(BOTAN_HAS_HSS_LMS)
257 if(alg_name == "HSS-LMS") {
258 return std::make_unique<HSS_LMS_PublicKey>(key_bits);
259 }
260#endif
261
262#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
263 if(alg_name == "SPHINCS+" || alg_name.starts_with("SphincsPlus-")) {
264 return std::make_unique<SphincsPlus_PublicKey>(alg_id, key_bits);
265 }
266#endif
267
268#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
269 if(alg_name.starts_with("SLH-DSA-") || alg_name.starts_with("Hash-SLH-DSA-")) {
270 return std::make_unique<SLH_DSA_PublicKey>(alg_id, key_bits);
271 }
272#endif
273
274#if defined(BOTAN_HAS_CLASSICMCELIECE)
275 if(alg_name.starts_with("ClassicMcEliece")) {
276 return std::make_unique<Classic_McEliece_PublicKey>(alg_id, key_bits);
277 }
278#endif
279
280 throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
281}
282
283std::unique_ptr<Private_Key> load_private_key(const AlgorithmIdentifier& alg_id,
284 [[maybe_unused]] std::span<const uint8_t> key_bits) {
285 const std::string oid_str = alg_id.oid().to_formatted_string();
286 const std::vector<std::string> alg_info = split_on(oid_str, '/');
287 std::string_view alg_name = alg_info[0];
288
289#if defined(BOTAN_HAS_RSA)
290 if(alg_name == "RSA") {
291 return std::make_unique<RSA_PrivateKey>(alg_id, key_bits);
292 }
293#endif
294
295#if defined(BOTAN_HAS_X25519)
296 if(alg_name == "X25519" || alg_name == "Curve25519") {
297 return std::make_unique<X25519_PrivateKey>(alg_id, key_bits);
298 }
299#endif
300
301#if defined(BOTAN_HAS_X448)
302 if(alg_name == "X448") {
303 return std::make_unique<X448_PrivateKey>(alg_id, key_bits);
304 }
305#endif
306
307#if defined(BOTAN_HAS_ECDSA)
308 if(alg_name == "ECDSA") {
309 return std::make_unique<ECDSA_PrivateKey>(alg_id, key_bits);
310 }
311#endif
312
313#if defined(BOTAN_HAS_ECDH)
314 if(alg_name == "ECDH") {
315 return std::make_unique<ECDH_PrivateKey>(alg_id, key_bits);
316 }
317#endif
318
319#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
320 if(alg_name == "DH") {
321 return std::make_unique<DH_PrivateKey>(alg_id, key_bits);
322 }
323#endif
324
325#if defined(BOTAN_HAS_DSA)
326 if(alg_name == "DSA") {
327 return std::make_unique<DSA_PrivateKey>(alg_id, key_bits);
328 }
329#endif
330
331#if defined(BOTAN_HAS_FRODOKEM)
332 if(alg_name == "FrodoKEM" || alg_name.starts_with("FrodoKEM-") || alg_name.starts_with("eFrodoKEM-")) {
333 return std::make_unique<FrodoKEM_PrivateKey>(alg_id, key_bits);
334 }
335#endif
336
337#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
338 if(alg_name == "Kyber" || alg_name.starts_with("Kyber-")) {
339 return std::make_unique<Kyber_PrivateKey>(alg_id, key_bits);
340 }
341#endif
342
343#if defined(BOTAN_HAS_ML_KEM)
344 if(alg_name.starts_with("ML-KEM-")) {
345 return std::make_unique<ML_KEM_PrivateKey>(alg_id, key_bits);
346 }
347#endif
348
349#if defined(BOTAN_HAS_MCELIECE)
350 if(alg_name == "McEliece") {
351 return std::make_unique<McEliece_PrivateKey>(key_bits);
352 }
353#endif
354
355#if defined(BOTAN_HAS_ECGDSA)
356 if(alg_name == "ECGDSA") {
357 return std::make_unique<ECGDSA_PrivateKey>(alg_id, key_bits);
358 }
359#endif
360
361#if defined(BOTAN_HAS_ECKCDSA)
362 if(alg_name == "ECKCDSA") {
363 return std::make_unique<ECKCDSA_PrivateKey>(alg_id, key_bits);
364 }
365#endif
366
367#if defined(BOTAN_HAS_ED25519)
368 if(alg_name == "Ed25519") {
369 return std::make_unique<Ed25519_PrivateKey>(alg_id, key_bits);
370 }
371#endif
372
373#if defined(BOTAN_HAS_ED448)
374 if(alg_name == "Ed448") {
375 return std::make_unique<Ed448_PrivateKey>(alg_id, key_bits);
376 }
377#endif
378
379#if defined(BOTAN_HAS_GOST_34_10_2001)
380 if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
381 return std::make_unique<GOST_3410_PrivateKey>(alg_id, key_bits);
382 }
383#endif
384
385#if defined(BOTAN_HAS_SM2)
386 if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
387 return std::make_unique<SM2_PrivateKey>(alg_id, key_bits);
388 }
389#endif
390
391#if defined(BOTAN_HAS_ELGAMAL)
392 if(alg_name == "ElGamal") {
393 return std::make_unique<ElGamal_PrivateKey>(alg_id, key_bits);
394 }
395#endif
396
397#if defined(BOTAN_HAS_XMSS_RFC8391)
398 if(alg_name == "XMSS") {
399 return std::make_unique<XMSS_PrivateKey>(key_bits);
400 }
401#endif
402
403#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
404 if(alg_name == "Dilithium" || alg_name.starts_with("Dilithium-")) {
405 return std::make_unique<Dilithium_PrivateKey>(alg_id, key_bits);
406 }
407#endif
408
409#if defined(BOTAN_HAS_ML_DSA)
410 if(alg_name.starts_with("ML-DSA-")) {
411 return std::make_unique<ML_DSA_PrivateKey>(alg_id, key_bits);
412 }
413#endif
414
415#if defined(BOTAN_HAS_HSS_LMS)
416 if(alg_name == "HSS-LMS-Private-Key") {
417 return std::make_unique<HSS_LMS_PrivateKey>(key_bits);
418 }
419#endif
420
421#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
422 if(alg_name == "SPHINCS+" || alg_name.starts_with("SphincsPlus-")) {
423 return std::make_unique<SphincsPlus_PrivateKey>(alg_id, key_bits);
424 }
425#endif
426
427#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
428 if(alg_name.starts_with("SLH-DSA-") || alg_name.starts_with("Hash-SLH-DSA-")) {
429 return std::make_unique<SLH_DSA_PrivateKey>(alg_id, key_bits);
430 }
431#endif
432
433#if defined(BOTAN_HAS_CLASSICMCELIECE)
434 if(alg_name.starts_with("ClassicMcEliece")) {
435 return std::make_unique<Classic_McEliece_PrivateKey>(alg_id, key_bits);
436 }
437#endif
438
439 throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
440}
441
442std::unique_ptr<Private_Key> create_ec_private_key(std::string_view alg_name,
443 const EC_Group& ec_group,
445 // Potentially unused if all EC algorithms are disabled
446 BOTAN_UNUSED(alg_name, ec_group, rng);
447
448#if defined(BOTAN_HAS_ECDSA)
449 if(alg_name == "ECDSA") {
450 return std::make_unique<ECDSA_PrivateKey>(rng, ec_group);
451 }
452#endif
453
454#if defined(BOTAN_HAS_ECDH)
455 if(alg_name == "ECDH") {
456 return std::make_unique<ECDH_PrivateKey>(rng, ec_group);
457 }
458#endif
459
460#if defined(BOTAN_HAS_ECKCDSA)
461 if(alg_name == "ECKCDSA") {
462 return std::make_unique<ECKCDSA_PrivateKey>(rng, ec_group);
463 }
464#endif
465
466#if defined(BOTAN_HAS_GOST_34_10_2001)
467 if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
468 return std::make_unique<GOST_3410_PrivateKey>(rng, ec_group);
469 }
470#endif
471
472#if defined(BOTAN_HAS_SM2)
473 if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
474 return std::make_unique<SM2_PrivateKey>(rng, ec_group);
475 }
476#endif
477
478#if defined(BOTAN_HAS_ECGDSA)
479 if(alg_name == "ECGDSA") {
480 return std::make_unique<ECGDSA_PrivateKey>(rng, ec_group);
481 }
482#endif
483
484 return nullptr;
485}
486
487std::unique_ptr<Private_Key> create_private_key(std::string_view alg_name,
489 std::string_view params,
490 std::string_view provider) {
491 /*
492 * Default paramaters are chosen for work factor > 2**128 where possible
493 */
494
495#if defined(BOTAN_HAS_X25519)
496 if(alg_name == "X25519" || alg_name == "Curve25519") {
497 return std::make_unique<X25519_PrivateKey>(rng);
498 }
499#endif
500
501#if defined(BOTAN_HAS_X448)
502 if(alg_name == "X448") {
503 return std::make_unique<X448_PrivateKey>(rng);
504 }
505#endif
506
507#if defined(BOTAN_HAS_RSA)
508 if(alg_name == "RSA") {
509 const size_t modulus_bits = params.empty() ? 3072 : to_u32bit(params);
510 return std::make_unique<RSA_PrivateKey>(rng, modulus_bits);
511 }
512#endif
513
514#if defined(BOTAN_HAS_MCELIECE)
515 if(alg_name == "McEliece") {
516 const auto [n, t] = [&]() -> std::pair<size_t, size_t> {
517 if(params.empty()) {
518 return {2960, 57};
519 }
520
521 const auto mce_params = split_on(params, ',');
522
523 if(mce_params.size() != 2) {
524 throw Invalid_Argument(fmt("create_private_key: invalid McEliece parameters '{}'", params));
525 }
526
527 const size_t mce_n = to_u32bit(mce_params[0]);
528 const size_t mce_t = to_u32bit(mce_params[1]);
529 return {mce_n, mce_t};
530 }();
531
532 return std::make_unique<McEliece_PrivateKey>(rng, n, t);
533 }
534#endif
535#if defined(BOTAN_HAS_CLASSICMCELIECE)
536 if(alg_name == "ClassicMcEliece") {
537 auto cmce_params_set = params.empty() ? Classic_McEliece_Parameter_Set::ClassicMcEliece_6960119f
539 return std::make_unique<Classic_McEliece_PrivateKey>(rng, cmce_params_set);
540 }
541#endif
542
543#if defined(BOTAN_HAS_FRODOKEM)
544 if(alg_name == "FrodoKEM") {
545 const auto mode = params.empty() ? FrodoKEMMode::FrodoKEM976_SHAKE : FrodoKEMMode(params);
546 return std::make_unique<FrodoKEM_PrivateKey>(rng, mode);
547 }
548#endif
549
550#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
551 if(alg_name == "Kyber") {
552 const auto mode = [&]() -> KyberMode {
553 if(params.empty()) {
555 }
556 return KyberMode(params);
557 }();
558
559 return std::make_unique<Kyber_PrivateKey>(rng, mode);
560 }
561#endif
562
563#if defined(BOTAN_HAS_ML_KEM)
564 if(alg_name == "ML-KEM") {
565 const auto mode = [&]() -> ML_KEM_Mode {
566 if(params.empty()) {
568 }
569 return ML_KEM_Mode(params);
570 }();
571
572 return std::make_unique<ML_KEM_PrivateKey>(rng, mode);
573 }
574#endif
575
576#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
577 if(alg_name == "Dilithium" || alg_name.starts_with("Dilithium-")) {
578 const auto mode = [&]() -> DilithiumMode {
579 if(params.empty()) {
581 }
582 return DilithiumMode(params);
583 }();
584
585 return std::make_unique<Dilithium_PrivateKey>(rng, mode);
586 }
587#endif
588
589#if defined(BOTAN_HAS_ML_DSA)
590 if(alg_name == "ML-DSA") {
591 const auto mode = [&]() -> ML_DSA_Mode {
592 if(params.empty()) {
594 }
595 return ML_DSA_Mode(params);
596 }();
597
598 return std::make_unique<ML_DSA_PrivateKey>(rng, mode);
599 }
600#endif
601
602#if defined(BOTAN_HAS_HSS_LMS)
603 if(alg_name == "HSS-LMS") {
604 const auto hss_params = [&]() -> std::string {
605 if(params.empty()) {
606 return "SHA-256,HW(10,1)";
607 } else {
608 return std::string(params);
609 }
610 }();
611 return std::make_unique<HSS_LMS_PrivateKey>(rng, hss_params);
612 }
613#endif
614
615#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
616 if(alg_name == "SPHINCS+" || alg_name == "SphincsPlus") {
617 auto sphincs_params = Sphincs_Parameters::create(params);
618
619 return std::make_unique<SphincsPlus_PrivateKey>(rng, sphincs_params);
620 }
621#endif
622
623#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
624 if(alg_name == "SLH-DSA") {
625 auto slh_dsa_params = SLH_DSA_Parameters::create(params);
626
627 return std::make_unique<SLH_DSA_PrivateKey>(rng, slh_dsa_params);
628 }
629#endif
630
631#if defined(BOTAN_HAS_XMSS_RFC8391)
632 if(alg_name == "XMSS") {
633 const auto xmss_oid = [&]() -> XMSS_Parameters::xmss_algorithm_t {
634 if(params.empty()) {
636 }
637 return XMSS_Parameters(params).oid();
638 }();
639
640 return std::make_unique<XMSS_PrivateKey>(xmss_oid, rng);
641 }
642#endif
643
644#if defined(BOTAN_HAS_ED25519)
645 if(alg_name == "Ed25519") {
646 return std::make_unique<Ed25519_PrivateKey>(rng);
647 }
648#endif
649
650#if defined(BOTAN_HAS_ED448)
651 if(alg_name == "Ed448") {
652 return std::make_unique<Ed448_PrivateKey>(rng);
653 }
654#endif
655
656 // ECC crypto
657#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
658
659 if(alg_name == "ECDSA" || alg_name == "ECDH" || alg_name == "ECKCDSA" || alg_name == "ECGDSA" || alg_name == "SM2" ||
660 alg_name == "SM2_Sig" || alg_name == "SM2_Enc" || alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" ||
661 alg_name == "GOST-34.10-2012-512") {
662 const std::string group_id = [&]() -> std::string {
663 if(!params.empty()) {
664 return std::string(params);
665 }
666 if(alg_name == "SM2" || alg_name == "SM2_Enc" || alg_name == "SM2_Sig") {
667 return "sm2p256v1";
668 }
669 if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256") {
670 return "gost_256A";
671 }
672 if(alg_name == "GOST-34.10-2012-512") {
673 return "gost_512A";
674 }
675 if(alg_name == "ECGDSA") {
676 return "brainpool256r1";
677 }
678 return "secp256r1";
679 }();
680
681 if(EC_Group::supports_named_group(group_id)) {
682 auto ec_group = EC_Group::from_name(group_id);
683 return create_ec_private_key(alg_name, ec_group, rng);
684 } else {
685 return {};
686 }
687 }
688#endif
689
690 // DL crypto
691#if defined(BOTAN_HAS_DL_GROUP)
692 if(alg_name == "DH" || alg_name == "DSA" || alg_name == "ElGamal") {
693 const std::string group_id = [&]() -> std::string {
694 if(!params.empty()) {
695 return std::string(params);
696 }
697 if(alg_name == "DSA") {
698 return "dsa/botan/2048";
699 }
700 return "modp/ietf/2048";
701 }();
702
703 auto modp_group = DL_Group::from_name(group_id);
704
705 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
706 if(alg_name == "DH") {
707 return std::make_unique<DH_PrivateKey>(rng, modp_group);
708 }
709 #endif
710
711 #if defined(BOTAN_HAS_DSA)
712 if(alg_name == "DSA") {
713 return std::make_unique<DSA_PrivateKey>(rng, modp_group);
714 }
715 #endif
716
717 #if defined(BOTAN_HAS_ELGAMAL)
718 if(alg_name == "ElGamal") {
719 return std::make_unique<ElGamal_PrivateKey>(rng, modp_group);
720 }
721 #endif
722 }
723#endif
724
725 BOTAN_UNUSED(alg_name, rng, params, provider);
726
727 return std::unique_ptr<Private_Key>();
728}
729
730std::vector<std::string> probe_provider_private_key(std::string_view alg_name,
731 const std::vector<std::string>& possible) {
732 std::vector<std::string> providers;
733
734 for(auto&& prov : possible) {
735 if(prov == "base") {
736 providers.push_back(prov);
737 }
738 }
739
740 BOTAN_UNUSED(alg_name);
741
742 return providers;
743}
744} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
const OID & oid() const
Definition asn1_obj.h:472
static Classic_McEliece_Parameter_Set from_string(std::string_view param_name)
Get the parameter set for a given parameter set name.
static DL_Group from_name(std::string_view name)
Definition dl_group.cpp:212
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:354
static bool supports_named_group(std::string_view name)
Definition ec_named.cpp:476
std::string to_formatted_string() const
Definition asn1_oid.cpp:139
static Sphincs_Parameters create(Sphincs_Parameter_Set set, Sphincs_Hash_Type hash)
xmss_algorithm_t oid() const
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:32
DilithiumMode ML_DSA_Mode
Definition ml_dsa.h:21
std::unique_ptr< Private_Key > create_private_key(std::string_view alg_name, RandomNumberGenerator &rng, std::string_view params, std::string_view provider)
Definition pk_algs.cpp:487
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:111
std::vector< std::string > probe_provider_private_key(std::string_view alg_name, const std::vector< std::string > &possible)
Definition pk_algs.cpp:730
KyberMode ML_KEM_Mode
Definition ml_kem.h:21
std::unique_ptr< Private_Key > load_private_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition pk_algs.cpp:283
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition pk_algs.cpp:124
std::unique_ptr< Private_Key > create_ec_private_key(std::string_view alg_name, const EC_Group &ec_group, RandomNumberGenerator &rng)
Definition pk_algs.cpp:442