• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13073795340

31 Jan 2025 01:32PM UTC coverage: 91.224% (-0.009%) from 91.233%
13073795340

push

github

web-flow
Merge pull request #4617 from randombit/jack/fix-build-configs

Fix build/test errors caught by test_all_configs.py

94150 of 103208 relevant lines covered (91.22%)

11427437.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/src/lib/pubkey/pk_algs.cpp
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

122
namespace Botan {
123

124
std::unique_ptr<Public_Key> load_public_key(const AlgorithmIdentifier& alg_id,
21,579✔
125
                                            [[maybe_unused]] std::span<const uint8_t> key_bits) {
126
   const std::string oid_str = alg_id.oid().to_formatted_string();
21,579✔
127
   const std::vector<std::string> alg_info = split_on(oid_str, '/');
21,579✔
128
   std::string_view alg_name = alg_info[0];
21,579✔
129

130
#if defined(BOTAN_HAS_RSA)
131
   if(alg_name == "RSA") {
21,579✔
132
      return std::make_unique<RSA_PublicKey>(alg_id, key_bits);
30,515✔
133
   }
134
#endif
135

136
#if defined(BOTAN_HAS_X25519)
137
   if(alg_name == "X25519" || alg_name == "Curve25519") {
6,028✔
138
      return std::make_unique<X25519_PublicKey>(alg_id, key_bits);
78✔
139
   }
140
#endif
141

142
#if defined(BOTAN_HAS_X448)
143
   if(alg_name == "X448") {
5,877✔
144
      return std::make_unique<X448_PublicKey>(alg_id, key_bits);
4✔
145
   }
146
#endif
147

148
#if defined(BOTAN_HAS_MCELIECE)
149
   if(alg_name == "McEliece") {
5,875✔
150
      return std::make_unique<McEliece_PublicKey>(key_bits);
2✔
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-")) {
5,875✔
156
      return std::make_unique<FrodoKEM_PublicKey>(alg_id, key_bits);
632✔
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-")) {
9,221✔
162
      return std::make_unique<Kyber_PublicKey>(alg_id, key_bits);
324✔
163
   }
164
#endif
165

166
#if defined(BOTAN_HAS_ML_KEM)
167
   if(alg_name.starts_with("ML-KEM-")) {
5,396✔
168
      return std::make_unique<ML_KEM_PublicKey>(alg_id, key_bits);
197✔
169
   }
170
#endif
171

172
#if defined(BOTAN_HAS_ECDSA)
173
   if(alg_name == "ECDSA") {
5,297✔
174
      return std::make_unique<ECDSA_PublicKey>(alg_id, key_bits);
6,881✔
175
   }
176
#endif
177

178
#if defined(BOTAN_HAS_ECDH)
179
   if(alg_name == "ECDH") {
1,708✔
180
      return std::make_unique<ECDH_PublicKey>(alg_id, key_bits);
33✔
181
   }
182
#endif
183

184
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
185
   if(alg_name == "DH") {
1,691✔
186
      return std::make_unique<DH_PublicKey>(alg_id, key_bits);
11✔
187
   }
188
#endif
189

190
#if defined(BOTAN_HAS_DSA)
191
   if(alg_name == "DSA") {
1,683✔
192
      return std::make_unique<DSA_PublicKey>(alg_id, key_bits);
355✔
193
   }
194
#endif
195

196
#if defined(BOTAN_HAS_ELGAMAL)
197
   if(alg_name == "ElGamal") {
1,449✔
198
      return std::make_unique<ElGamal_PublicKey>(alg_id, key_bits);
8✔
199
   }
200
#endif
201

202
#if defined(BOTAN_HAS_ECGDSA)
203
   if(alg_name == "ECGDSA") {
1,445✔
204
      return std::make_unique<ECGDSA_PublicKey>(alg_id, key_bits);
146✔
205
   }
206
#endif
207

208
#if defined(BOTAN_HAS_ECKCDSA)
209
   if(alg_name == "ECKCDSA") {
1,372✔
210
      return std::make_unique<ECKCDSA_PublicKey>(alg_id, key_bits);
144✔
211
   }
212
#endif
213

214
#if defined(BOTAN_HAS_ED25519)
215
   if(alg_name == "Ed25519") {
1,300✔
216
      return std::make_unique<Ed25519_PublicKey>(alg_id, key_bits);
1,528✔
217
   }
218
#endif
219

220
#if defined(BOTAN_HAS_ED448)
221
   if(alg_name == "Ed448") {
536✔
222
      return std::make_unique<Ed448_PublicKey>(alg_id, key_bits);
146✔
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") {
595✔
228
      return std::make_unique<GOST_3410_PublicKey>(alg_id, key_bits);
132✔
229
   }
230
#endif
231

232
#if defined(BOTAN_HAS_SM2)
233
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
465✔
234
      return std::make_unique<SM2_PublicKey>(alg_id, key_bits);
30✔
235
   }
236
#endif
237

238
#if defined(BOTAN_HAS_XMSS_RFC8391)
239
   if(alg_name == "XMSS") {
382✔
240
      return std::make_unique<XMSS_PublicKey>(key_bits);
88✔
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-")) {
340✔
246
      return std::make_unique<Dilithium_PublicKey>(alg_id, key_bits);
168✔
247
   }
248
#endif
249

250
#if defined(BOTAN_HAS_ML_DSA)
251
   if(alg_name.starts_with("ML-DSA-")) {
254✔
252
      return std::make_unique<ML_DSA_PublicKey>(alg_id, key_bits);
120✔
253
   }
254
#endif
255

256
#if defined(BOTAN_HAS_HSS_LMS)
257
   if(alg_name == "HSS-LMS") {
194✔
258
      return std::make_unique<HSS_LMS_PublicKey>(key_bits);
106✔
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-")) {
142✔
264
      return std::make_unique<SphincsPlus_PublicKey>(alg_id, key_bits);
48✔
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-")) {
117✔
270
      return std::make_unique<SLH_DSA_PublicKey>(alg_id, key_bits);
86✔
271
   }
272
#endif
273

274
#if defined(BOTAN_HAS_CLASSICMCELIECE)
275
   if(alg_name.starts_with("ClassicMcEliece")) {
74✔
276
      return std::make_unique<Classic_McEliece_PublicKey>(alg_id, key_bits);
40✔
277
   }
278
#endif
279

280
   throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
108✔
281
}
21,579✔
282

283
std::unique_ptr<Private_Key> load_private_key(const AlgorithmIdentifier& alg_id,
4,964✔
284
                                              [[maybe_unused]] std::span<const uint8_t> key_bits) {
285
   const std::string oid_str = alg_id.oid().to_formatted_string();
4,964✔
286
   const std::vector<std::string> alg_info = split_on(oid_str, '/');
4,964✔
287
   std::string_view alg_name = alg_info[0];
4,964✔
288

289
#if defined(BOTAN_HAS_RSA)
290
   if(alg_name == "RSA") {
4,964✔
291
      return std::make_unique<RSA_PrivateKey>(alg_id, key_bits);
1,023✔
292
   }
293
#endif
294

295
#if defined(BOTAN_HAS_X25519)
296
   if(alg_name == "X25519" || alg_name == "Curve25519") {
3,987✔
297
      return std::make_unique<X25519_PrivateKey>(alg_id, key_bits);
54✔
298
   }
299
#endif
300

301
#if defined(BOTAN_HAS_X448)
302
   if(alg_name == "X448") {
3,913✔
303
      return std::make_unique<X448_PrivateKey>(alg_id, key_bits);
12✔
304
   }
305
#endif
306

307
#if defined(BOTAN_HAS_ECDSA)
308
   if(alg_name == "ECDSA") {
3,907✔
309
      return std::make_unique<ECDSA_PrivateKey>(alg_id, key_bits);
4,078✔
310
   }
311
#endif
312

313
#if defined(BOTAN_HAS_ECDH)
314
   if(alg_name == "ECDH") {
1,569✔
315
      return std::make_unique<ECDH_PrivateKey>(alg_id, key_bits);
210✔
316
   }
317
#endif
318

319
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
320
   if(alg_name == "DH") {
1,447✔
321
      return std::make_unique<DH_PrivateKey>(alg_id, key_bits);
67✔
322
   }
323
#endif
324

325
#if defined(BOTAN_HAS_DSA)
326
   if(alg_name == "DSA") {
1,407✔
327
      return std::make_unique<DSA_PrivateKey>(alg_id, key_bits);
373✔
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-")) {
1,095✔
333
      return std::make_unique<FrodoKEM_PrivateKey>(alg_id, key_bits);
348✔
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-")) {
761✔
339
      return std::make_unique<Kyber_PrivateKey>(alg_id, key_bits);
211✔
340
   }
341
#endif
342

343
#if defined(BOTAN_HAS_ML_KEM)
344
   if(alg_name.starts_with("ML-KEM-")) {
532✔
345
      return std::make_unique<ML_KEM_PrivateKey>(alg_id, key_bits);
93✔
346
   }
347
#endif
348

349
#if defined(BOTAN_HAS_MCELIECE)
350
   if(alg_name == "McEliece") {
439✔
351
      return std::make_unique<McEliece_PrivateKey>(key_bits);
4✔
352
   }
353
#endif
354

355
#if defined(BOTAN_HAS_ECGDSA)
356
   if(alg_name == "ECGDSA") {
435✔
357
      return std::make_unique<ECGDSA_PrivateKey>(alg_id, key_bits);
36✔
358
   }
359
#endif
360

361
#if defined(BOTAN_HAS_ECKCDSA)
362
   if(alg_name == "ECKCDSA") {
417✔
363
      return std::make_unique<ECKCDSA_PrivateKey>(alg_id, key_bits);
36✔
364
   }
365
#endif
366

367
#if defined(BOTAN_HAS_ED25519)
368
   if(alg_name == "Ed25519") {
399✔
369
      return std::make_unique<Ed25519_PrivateKey>(alg_id, key_bits);
17✔
370
   }
371
#endif
372

373
#if defined(BOTAN_HAS_ED448)
374
   if(alg_name == "Ed448") {
389✔
375
      return std::make_unique<Ed448_PrivateKey>(alg_id, key_bits);
36✔
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") {
416✔
381
      return std::make_unique<GOST_3410_PrivateKey>(alg_id, key_bits);
24✔
382
   }
383
#endif
384

385
#if defined(BOTAN_HAS_SM2)
386
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
393✔
387
      return std::make_unique<SM2_PrivateKey>(alg_id, key_bits);
40✔
388
   }
389
#endif
390

391
#if defined(BOTAN_HAS_ELGAMAL)
392
   if(alg_name == "ElGamal") {
339✔
393
      return std::make_unique<ElGamal_PrivateKey>(alg_id, key_bits);
28✔
394
   }
395
#endif
396

397
#if defined(BOTAN_HAS_XMSS_RFC8391)
398
   if(alg_name == "XMSS") {
325✔
399
      return std::make_unique<XMSS_PrivateKey>(key_bits);
19✔
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-")) {
316✔
405
      return std::make_unique<Dilithium_PrivateKey>(alg_id, key_bits);
55✔
406
   }
407
#endif
408

409
#if defined(BOTAN_HAS_ML_DSA)
410
   if(alg_name.starts_with("ML-DSA-")) {
251✔
411
      return std::make_unique<ML_DSA_PrivateKey>(alg_id, key_bits);
24✔
412
   }
413
#endif
414

415
#if defined(BOTAN_HAS_HSS_LMS)
416
   if(alg_name == "HSS-LMS-Private-Key") {
227✔
417
      return std::make_unique<HSS_LMS_PrivateKey>(key_bits);
6✔
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-")) {
221✔
423
      return std::make_unique<SphincsPlus_PrivateKey>(alg_id, key_bits);
72✔
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-")) {
149✔
429
      return std::make_unique<SLH_DSA_PrivateKey>(alg_id, key_bits);
72✔
430
   }
431
#endif
432

433
#if defined(BOTAN_HAS_CLASSICMCELIECE)
434
   if(alg_name.starts_with("ClassicMcEliece")) {
77✔
435
      return std::make_unique<Classic_McEliece_PrivateKey>(alg_id, key_bits);
28✔
436
   }
437
#endif
438

439
   throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
98✔
440
}
4,964✔
441

442
std::unique_ptr<Private_Key> create_ec_private_key(std::string_view alg_name,
191✔
443
                                                   const EC_Group& ec_group,
444
                                                   RandomNumberGenerator& rng) {
445
   // Potentially unused if all EC algorithms are disabled
446
   BOTAN_UNUSED(alg_name, ec_group, rng);
191✔
447

448
#if defined(BOTAN_HAS_ECDSA)
449
   if(alg_name == "ECDSA") {
191✔
450
      return std::make_unique<ECDSA_PrivateKey>(rng, ec_group);
144✔
451
   }
452
#endif
453

454
#if defined(BOTAN_HAS_ECDH)
455
   if(alg_name == "ECDH") {
119✔
456
      return std::make_unique<ECDH_PrivateKey>(rng, ec_group);
74✔
457
   }
458
#endif
459

460
#if defined(BOTAN_HAS_ECKCDSA)
461
   if(alg_name == "ECKCDSA") {
82✔
462
      return std::make_unique<ECKCDSA_PrivateKey>(rng, ec_group);
62✔
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") {
63✔
468
      return std::make_unique<GOST_3410_PrivateKey>(rng, ec_group);
24✔
469
   }
470
#endif
471

472
#if defined(BOTAN_HAS_SM2)
473
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
48✔
474
      return std::make_unique<SM2_PrivateKey>(rng, ec_group);
16✔
475
   }
476
#endif
477

478
#if defined(BOTAN_HAS_ECGDSA)
479
   if(alg_name == "ECGDSA") {
31✔
480
      return std::make_unique<ECGDSA_PrivateKey>(rng, ec_group);
62✔
481
   }
482
#endif
483

484
   return nullptr;
×
485
}
486

487
std::unique_ptr<Private_Key> create_private_key(std::string_view alg_name,
1,209✔
488
                                                RandomNumberGenerator& rng,
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") {
1,470✔
497
      return std::make_unique<X25519_PrivateKey>(rng);
72✔
498
   }
499
#endif
500

501
#if defined(BOTAN_HAS_X448)
502
   if(alg_name == "X448") {
1,173✔
503
      return std::make_unique<X448_PrivateKey>(rng);
10✔
504
   }
505
#endif
506

507
#if defined(BOTAN_HAS_RSA)
508
   if(alg_name == "RSA") {
1,168✔
509
      const size_t modulus_bits = params.empty() ? 3072 : to_u32bit(params);
51✔
510
      return std::make_unique<RSA_PrivateKey>(rng, modulus_bits);
51✔
511
   }
512
#endif
513

514
#if defined(BOTAN_HAS_MCELIECE)
515
   if(alg_name == "McEliece") {
1,117✔
516
      const auto [n, t] = [&]() -> std::pair<size_t, size_t> {
2✔
517
         if(params.empty()) {
2✔
518
            return {2960, 57};
×
519
         }
520

521
         const auto mce_params = split_on(params, ',');
2✔
522

523
         if(mce_params.size() != 2) {
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]);
2✔
528
         const size_t mce_t = to_u32bit(mce_params[1]);
2✔
529
         return {mce_n, mce_t};
2✔
530
      }();
4✔
531

532
      return std::make_unique<McEliece_PrivateKey>(rng, n, t);
2✔
533
   }
534
#endif
535
#if defined(BOTAN_HAS_CLASSICMCELIECE)
536
   if(alg_name == "ClassicMcEliece") {
1,115✔
537
      auto cmce_params_set = params.empty() ? Classic_McEliece_Parameter_Set::ClassicMcEliece_6960119f
39✔
538
                                            : Classic_McEliece_Parameter_Set::from_string(params);
39✔
539
      return std::make_unique<Classic_McEliece_PrivateKey>(rng, cmce_params_set);
39✔
540
   }
541
#endif
542

543
#if defined(BOTAN_HAS_FRODOKEM)
544
   if(alg_name == "FrodoKEM") {
1,076✔
545
      const auto mode = params.empty() ? FrodoKEMMode::FrodoKEM976_SHAKE : FrodoKEMMode(params);
322✔
546
      return std::make_unique<FrodoKEM_PrivateKey>(rng, mode);
322✔
547
   }
548
#endif
549

550
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
551
   if(alg_name == "Kyber") {
754✔
552
      const auto mode = [&]() -> KyberMode {
555✔
553
         if(params.empty()) {
185✔
554
            return KyberMode::Kyber1024_R3;
1✔
555
         }
556
         return KyberMode(params);
184✔
557
      }();
185✔
558

559
      return std::make_unique<Kyber_PrivateKey>(rng, mode);
185✔
560
   }
561
#endif
562

563
#if defined(BOTAN_HAS_ML_KEM)
564
   if(alg_name == "ML-KEM") {
569✔
565
      const auto mode = [&]() -> ML_KEM_Mode {
537✔
566
         if(params.empty()) {
179✔
567
            return ML_KEM_Mode::ML_KEM_768;
1✔
568
         }
569
         return ML_KEM_Mode(params);
178✔
570
      }();
179✔
571

572
      return std::make_unique<ML_KEM_PrivateKey>(rng, mode);
179✔
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-")) {
420✔
578
      const auto mode = [&]() -> DilithiumMode {
30✔
579
         if(params.empty()) {
30✔
580
            return DilithiumMode::Dilithium6x5;
581
         }
582
         return DilithiumMode(params);
22✔
583
      }();
30✔
584

585
      return std::make_unique<Dilithium_PrivateKey>(rng, mode);
30✔
586
   }
587
#endif
588

589
#if defined(BOTAN_HAS_ML_DSA)
590
   if(alg_name == "ML-DSA") {
360✔
591
      const auto mode = [&]() -> ML_DSA_Mode {
15✔
592
         if(params.empty()) {
15✔
593
            return ML_DSA_Mode::ML_DSA_6x5;
594
         }
595
         return ML_DSA_Mode(params);
7✔
596
      }();
15✔
597

598
      return std::make_unique<ML_DSA_PrivateKey>(rng, mode);
15✔
599
   }
600
#endif
601

602
#if defined(BOTAN_HAS_HSS_LMS)
603
   if(alg_name == "HSS-LMS") {
345✔
604
      const auto hss_params = [&]() -> std::string {
45✔
605
         if(params.empty()) {
15✔
606
            return "SHA-256,HW(10,1)";
×
607
         } else {
608
            return std::string(params);
15✔
609
         }
610
      }();
15✔
611
      return std::make_unique<HSS_LMS_PrivateKey>(rng, hss_params);
15✔
612
   }
15✔
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") {
330✔
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") {
330✔
625
      auto slh_dsa_params = SLH_DSA_Parameters::create(params);
52✔
626

627
      return std::make_unique<SLH_DSA_PrivateKey>(rng, slh_dsa_params);
52✔
628
   }
629
#endif
630

631
#if defined(BOTAN_HAS_XMSS_RFC8391)
632
   if(alg_name == "XMSS") {
278✔
633
      const auto xmss_oid = [&]() -> XMSS_Parameters::xmss_algorithm_t {
24✔
634
         if(params.empty()) {
8✔
635
            return XMSS_Parameters::XMSS_SHA2_10_512;
636
         }
637
         return XMSS_Parameters(params).oid();
14✔
638
      }();
8✔
639

640
      return std::make_unique<XMSS_PrivateKey>(xmss_oid, rng);
8✔
641
   }
642
#endif
643

644
#if defined(BOTAN_HAS_ED25519)
645
   if(alg_name == "Ed25519") {
270✔
646
      return std::make_unique<Ed25519_PrivateKey>(rng);
44✔
647
   }
648
#endif
649

650
#if defined(BOTAN_HAS_ED448)
651
   if(alg_name == "Ed448") {
248✔
652
      return std::make_unique<Ed448_PrivateKey>(rng);
26✔
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" ||
443✔
660
      alg_name == "SM2_Sig" || alg_name == "SM2_Enc" || alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" ||
93✔
661
      alg_name == "GOST-34.10-2012-512") {
44✔
662
      const std::string group_id = [&]() -> std::string {
573✔
663
         if(!params.empty()) {
191✔
664
            return std::string(params);
182✔
665
         }
666
         if(alg_name == "SM2" || alg_name == "SM2_Enc" || alg_name == "SM2_Sig") {
9✔
667
            return "sm2p256v1";
×
668
         }
669
         if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256") {
9✔
670
            return "gost_256A";
×
671
         }
672
         if(alg_name == "GOST-34.10-2012-512") {
9✔
673
            return "gost_512A";
×
674
         }
675
         if(alg_name == "ECGDSA") {
9✔
676
            return "brainpool256r1";
×
677
         }
678
         return "secp256r1";
9✔
679
      }();
191✔
680

681
      if(EC_Group::supports_named_group(group_id)) {
191✔
682
         auto ec_group = EC_Group::from_name(group_id);
191✔
683
         return create_ec_private_key(alg_name, ec_group, rng);
191✔
684
      } else {
191✔
685
         return {};
×
686
      }
687
   }
191✔
688
#endif
689

690
   // DL crypto
691
#if defined(BOTAN_HAS_DL_GROUP)
692
   if(alg_name == "DH" || alg_name == "DSA" || alg_name == "ElGamal") {
88✔
693
      const std::string group_id = [&]() -> std::string {
132✔
694
         if(!params.empty()) {
44✔
695
            return std::string(params);
34✔
696
         }
697
         if(alg_name == "DSA") {
10✔
698
            return "dsa/botan/2048";
8✔
699
         }
700
         return "modp/ietf/2048";
2✔
701
      }();
44✔
702

703
      auto modp_group = DL_Group::from_name(group_id);
44✔
704

705
   #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
706
      if(alg_name == "DH") {
44✔
707
         return std::make_unique<DH_PrivateKey>(rng, modp_group);
30✔
708
      }
709
   #endif
710

711
   #if defined(BOTAN_HAS_DSA)
712
      if(alg_name == "DSA") {
29✔
713
         return std::make_unique<DSA_PrivateKey>(rng, modp_group);
38✔
714
      }
715
   #endif
716

717
   #if defined(BOTAN_HAS_ELGAMAL)
718
      if(alg_name == "ElGamal") {
10✔
719
         return std::make_unique<ElGamal_PrivateKey>(rng, modp_group);
20✔
720
      }
721
   #endif
722
   }
44✔
723
#endif
724

725
   BOTAN_UNUSED(alg_name, rng, params, provider);
×
726

727
   return std::unique_ptr<Private_Key>();
×
728
}
729

730
std::vector<std::string> probe_provider_private_key(std::string_view alg_name,
112✔
731
                                                    const std::vector<std::string>& possible) {
732
   std::vector<std::string> providers;
112✔
733

734
   for(auto&& prov : possible) {
560✔
735
      if(prov == "base") {
448✔
736
         providers.push_back(prov);
112✔
737
      }
738
   }
739

740
   BOTAN_UNUSED(alg_name);
112✔
741

742
   return providers;
112✔
743
}
×
744
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc