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

randombit / botan / 5129806836

31 May 2023 07:54AM UTC coverage: 91.972% (-0.04%) from 92.012%
5129806836

Pull #3549

github

web-flow
Merge 830a653e4 into 1cbeffafb
Pull Request #3549: SPHINCS+

76805 of 83509 relevant lines covered (91.97%)

12126759.88 hits per line

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

93.82
/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/internal/fmt.h>
11
#include <botan/internal/parsing.h>
12

13
#if defined(BOTAN_HAS_RSA)
14
   #include <botan/rsa.h>
15
#endif
16

17
#if defined(BOTAN_HAS_DSA)
18
   #include <botan/dsa.h>
19
#endif
20

21
#if defined(BOTAN_HAS_DL_GROUP)
22
   #include <botan/dl_group.h>
23
#endif
24

25
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
26
   #include <botan/dh.h>
27
#endif
28

29
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
30
   #include <botan/ecc_key.h>
31
#endif
32

33
#if defined(BOTAN_HAS_ECDSA)
34
   #include <botan/ecdsa.h>
35
#endif
36

37
#if defined(BOTAN_HAS_ECGDSA)
38
   #include <botan/ecgdsa.h>
39
#endif
40

41
#if defined(BOTAN_HAS_ECKCDSA)
42
   #include <botan/eckcdsa.h>
43
#endif
44

45
#if defined(BOTAN_HAS_ED25519)
46
   #include <botan/ed25519.h>
47
#endif
48

49
#if defined(BOTAN_HAS_GOST_34_10_2001)
50
   #include <botan/gost_3410.h>
51
#endif
52

53
#if defined(BOTAN_HAS_ELGAMAL)
54
   #include <botan/elgamal.h>
55
#endif
56

57
#if defined(BOTAN_HAS_ECDH)
58
   #include <botan/ecdh.h>
59
#endif
60

61
#if defined(BOTAN_HAS_CURVE_25519)
62
   #include <botan/curve25519.h>
63
#endif
64

65
#if defined(BOTAN_HAS_MCELIECE)
66
   #include <botan/mceliece.h>
67
#endif
68

69
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
70
   #include <botan/kyber.h>
71
#endif
72

73
#if defined(BOTAN_HAS_XMSS_RFC8391)
74
   #include <botan/xmss.h>
75
#endif
76

77
#if defined(BOTAN_HAS_SM2)
78
   #include <botan/sm2.h>
79
#endif
80

81
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
82
   #include <botan/dilithium.h>
83
#endif
84

85
#if defined(BOTAN_HAS_SPHINCS_PLUS)
86
   #include <botan/sphincsplus.h>
87
#endif
88

89
namespace Botan {
90

91
std::unique_ptr<Public_Key> load_public_key(const AlgorithmIdentifier& alg_id,
15,301✔
92
                                            [[maybe_unused]] std::span<const uint8_t> key_bits) {
93
   const std::string oid_str = alg_id.oid().to_formatted_string();
15,301✔
94
   const std::vector<std::string> alg_info = split_on(oid_str, '/');
15,301✔
95
   std::string_view alg_name = alg_info[0];
15,301✔
96

97
#if defined(BOTAN_HAS_RSA)
98
   if(alg_name == "RSA") {
15,550✔
99
      return std::make_unique<RSA_PublicKey>(alg_id, key_bits);
22,221✔
100
   }
101
#endif
102

103
#if defined(BOTAN_HAS_CURVE_25519)
104
   if(alg_name == "Curve25519") {
3,765✔
105
      return std::make_unique<Curve25519_PublicKey>(alg_id, key_bits);
44✔
106
   }
107
#endif
108

109
#if defined(BOTAN_HAS_MCELIECE)
110
   if(alg_name == "McEliece") {
3,743✔
111
      return std::make_unique<McEliece_PublicKey>(key_bits);
2✔
112
   }
113
#endif
114

115
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
116
   if(alg_name == "Kyber" || alg_name.starts_with("Kyber-")) {
6,022✔
117
      return std::make_unique<Kyber_PublicKey>(alg_id, key_bits);
24✔
118
   }
119
#endif
120

121
#if defined(BOTAN_HAS_ECDSA)
122
   if(alg_name == "ECDSA") {
3,730✔
123
      return std::make_unique<ECDSA_PublicKey>(alg_id, key_bits);
4,331✔
124
   }
125
#endif
126

127
#if defined(BOTAN_HAS_ECDH)
128
   if(alg_name == "ECDH") {
1,487✔
129
      return std::make_unique<ECDH_PublicKey>(alg_id, key_bits);
33✔
130
   }
131
#endif
132

133
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
134
   if(alg_name == "DH") {
1,432✔
135
      return std::make_unique<DH_PublicKey>(alg_id, key_bits);
16✔
136
   }
137
#endif
138

139
#if defined(BOTAN_HAS_DSA)
140
   if(alg_name == "DSA") {
1,433✔
141
      return std::make_unique<DSA_PublicKey>(alg_id, key_bits);
355✔
142
   }
143
#endif
144

145
#if defined(BOTAN_HAS_ELGAMAL)
146
   if(alg_name == "ElGamal") {
2,018✔
147
      return std::make_unique<ElGamal_PublicKey>(alg_id, key_bits);
8✔
148
   }
149
#endif
150

151
#if defined(BOTAN_HAS_ECGDSA)
152
   if(alg_name == "ECGDSA") {
1,180✔
153
      return std::make_unique<ECGDSA_PublicKey>(alg_id, key_bits);
144✔
154
   }
155
#endif
156

157
#if defined(BOTAN_HAS_ECKCDSA)
158
   if(alg_name == "ECKCDSA") {
1,871✔
159
      return std::make_unique<ECKCDSA_PublicKey>(alg_id, key_bits);
142✔
160
   }
161
#endif
162

163
#if defined(BOTAN_HAS_ED25519)
164
   if(alg_name == "Ed25519") {
1,038✔
165
      return std::make_unique<Ed25519_PublicKey>(alg_id, key_bits);
1,524✔
166
   }
167
#endif
168

169
#if defined(BOTAN_HAS_GOST_34_10_2001)
170
   if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
353✔
171
      return std::make_unique<GOST_3410_PublicKey>(alg_id, key_bits);
130✔
172
   }
173
#endif
174

175
#if defined(BOTAN_HAS_SM2)
176
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
226✔
177
      return std::make_unique<SM2_PublicKey>(alg_id, key_bits);
26✔
178
   }
179
#endif
180

181
#if defined(BOTAN_HAS_XMSS_RFC8391)
182
   if(alg_name == "XMSS") {
197✔
183
      return std::make_unique<XMSS_PublicKey>(key_bits);
76✔
184
   }
185
#endif
186

187
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
188
   if(alg_name == "Dilithium" || alg_name.starts_with("Dilithium-")) {
162✔
189
      return std::make_unique<Dilithium_PublicKey>(alg_id, key_bits);
166✔
190
   }
191
#endif
192

193
   throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
152✔
194
}
15,455✔
195

196
std::unique_ptr<Private_Key> load_private_key(const AlgorithmIdentifier& alg_id,
3,356✔
197
                                              [[maybe_unused]] std::span<const uint8_t> key_bits) {
198
   const std::string oid_str = alg_id.oid().to_formatted_string();
3,356✔
199
   const std::vector<std::string> alg_info = split_on(oid_str, '/');
3,356✔
200
   std::string_view alg_name = alg_info[0];
3,356✔
201

202
#if defined(BOTAN_HAS_RSA)
203
   if(alg_name == "RSA") {
3,589✔
204
      return std::make_unique<RSA_PrivateKey>(alg_id, key_bits);
2,102✔
205
   }
206
#endif
207

208
#if defined(BOTAN_HAS_CURVE_25519)
209
   if(alg_name == "Curve25519") {
1,256✔
210
      return std::make_unique<Curve25519_PrivateKey>(alg_id, key_bits);
54✔
211
   }
212
#endif
213

214
#if defined(BOTAN_HAS_ECDSA)
215
   if(alg_name == "ECDSA") {
1,226✔
216
      return std::make_unique<ECDSA_PrivateKey>(alg_id, key_bits);
908✔
217
   }
218
#endif
219

220
#if defined(BOTAN_HAS_ECDH)
221
   if(alg_name == "ECDH") {
606✔
222
      return std::make_unique<ECDH_PrivateKey>(alg_id, key_bits);
158✔
223
   }
224
#endif
225

226
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
227
   if(alg_name == "DH") {
494✔
228
      return std::make_unique<DH_PrivateKey>(alg_id, key_bits);
24✔
229
   }
230
#endif
231

232
#if defined(BOTAN_HAS_DSA)
233
   if(alg_name == "DSA") {
497✔
234
      return std::make_unique<DSA_PrivateKey>(alg_id, key_bits);
278✔
235
   }
236
#endif
237

238
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
239
   if(alg_name == "Kyber" || alg_name.starts_with("Kyber-")) {
264✔
240
      return std::make_unique<Kyber_PrivateKey>(alg_id, key_bits);
36✔
241
   }
242
#endif
243

244
#if defined(BOTAN_HAS_MCELIECE)
245
   if(alg_name == "McEliece") {
228✔
246
      return std::make_unique<McEliece_PrivateKey>(key_bits);
4✔
247
   }
248
#endif
249

250
#if defined(BOTAN_HAS_ECGDSA)
251
   if(alg_name == "ECGDSA") {
224✔
252
      return std::make_unique<ECGDSA_PrivateKey>(alg_id, key_bits);
36✔
253
   }
254
#endif
255

256
#if defined(BOTAN_HAS_ECKCDSA)
257
   if(alg_name == "ECKCDSA") {
230✔
258
      return std::make_unique<ECKCDSA_PrivateKey>(alg_id, key_bits);
36✔
259
   }
260
#endif
261

262
#if defined(BOTAN_HAS_ED25519)
263
   if(alg_name == "Ed25519") {
202✔
264
      return std::make_unique<Ed25519_PrivateKey>(alg_id, key_bits);
17✔
265
   }
266
#endif
267

268
#if defined(BOTAN_HAS_GOST_34_10_2001)
269
   if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
194✔
270
      return std::make_unique<GOST_3410_PrivateKey>(alg_id, key_bits);
24✔
271
   }
272
#endif
273

274
#if defined(BOTAN_HAS_SM2)
275
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
215✔
276
      return std::make_unique<SM2_PrivateKey>(alg_id, key_bits);
40✔
277
   }
278
#endif
279

280
#if defined(BOTAN_HAS_ELGAMAL)
281
   if(alg_name == "ElGamal") {
146✔
282
      return std::make_unique<ElGamal_PrivateKey>(alg_id, key_bits);
28✔
283
   }
284
#endif
285

286
#if defined(BOTAN_HAS_XMSS_RFC8391)
287
   if(alg_name == "XMSS") {
132✔
288
      return std::make_unique<XMSS_PrivateKey>(key_bits);
16✔
289
   }
290
#endif
291

292
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
293
   if(alg_name == "Dilithium" || alg_name.starts_with("Dilithium-")) {
128✔
294
      return std::make_unique<Dilithium_PrivateKey>(alg_id, key_bits);
55✔
295
   }
296
#endif
297

298
   throw Decoding_Error(fmt("Unknown or unavailable public key algorithm '{}'", alg_name));
122✔
299
}
3,441✔
300

301
std::unique_ptr<Private_Key> create_ec_private_key(std::string_view alg_name,
126✔
302
                                                   const EC_Group& ec_group,
303
                                                   RandomNumberGenerator& rng) {
304
   // Potentially unused if all EC algorthms are disabled
305
   BOTAN_UNUSED(alg_name, ec_group, rng);
126✔
306

307
#if defined(BOTAN_HAS_ECDSA)
308
   if(alg_name == "ECDSA") {
126✔
309
      return std::make_unique<ECDSA_PrivateKey>(rng, ec_group);
84✔
310
   }
311
#endif
312

313
#if defined(BOTAN_HAS_ECDH)
314
   if(alg_name == "ECDH") {
84✔
315
      return std::make_unique<ECDH_PrivateKey>(rng, ec_group);
56✔
316
   }
317
#endif
318

319
#if defined(BOTAN_HAS_ECKCDSA)
320
   if(alg_name == "ECKCDSA") {
64✔
321
      return std::make_unique<ECKCDSA_PrivateKey>(rng, ec_group);
34✔
322
   }
323
#endif
324

325
#if defined(BOTAN_HAS_GOST_34_10_2001)
326
   if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") {
50✔
327
      return std::make_unique<GOST_3410_PrivateKey>(rng, ec_group);
22✔
328
   }
329
#endif
330

331
#if defined(BOTAN_HAS_SM2)
332
   if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") {
40✔
333
      return std::make_unique<SM2_PrivateKey>(rng, ec_group);
22✔
334
   }
335
#endif
336

337
#if defined(BOTAN_HAS_ECGDSA)
338
   if(alg_name == "ECGDSA") {
17✔
339
      return std::make_unique<ECGDSA_PrivateKey>(rng, ec_group);
34✔
340
   }
341
#endif
342

343
   return nullptr;
×
344
}
345

346
std::unique_ptr<Private_Key> create_private_key(std::string_view alg_name,
252✔
347
                                                RandomNumberGenerator& rng,
348
                                                std::string_view params,
349
                                                std::string_view provider) {
350
   /*
351
   * Default paramaters are chosen for work factor > 2**128 where possible
352
   */
353

354
#if defined(BOTAN_HAS_CURVE_25519)
355
   if(alg_name == "Curve25519") {
263✔
356
      return std::make_unique<Curve25519_PrivateKey>(rng);
6✔
357
   }
358
#endif
359

360
#if defined(BOTAN_HAS_RSA)
361
   if(alg_name == "RSA") {
265✔
362
      const size_t modulus_bits = params.empty() ? 3072 : to_u32bit(params);
43✔
363
      return std::make_unique<RSA_PrivateKey>(rng, modulus_bits);
43✔
364
   }
365
#endif
366

367
#if defined(BOTAN_HAS_MCELIECE)
368
   if(alg_name == "McEliece") {
206✔
369
      const auto [n, t] = [&]() -> std::pair<size_t, size_t> {
2✔
370
         if(params.empty()) {
2✔
371
            return {2960, 57};
×
372
         }
373

374
         const auto mce_params = split_on(params, ',');
2✔
375

376
         if(mce_params.size() != 2) {
2✔
377
            throw Invalid_Argument(fmt("create_private_key: invalid McEliece parameters '{}'", params));
×
378
         }
379

380
         const size_t mce_n = to_u32bit(mce_params[0]);
2✔
381
         const size_t mce_t = to_u32bit(mce_params[1]);
2✔
382
         return {mce_n, mce_t};
2✔
383
      }();
4✔
384

385
      return std::make_unique<McEliece_PrivateKey>(rng, n, t);
2✔
386
   }
387
#endif
388

389
#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
390
   if(alg_name == "Kyber") {
246✔
391
      const auto mode = [&]() -> KyberMode {
16✔
392
         if(params.empty()) {
8✔
393
            return KyberMode::Kyber1024;
1✔
394
         }
395
         return KyberMode(params);
7✔
396
      }();
8✔
397

398
      return std::make_unique<Kyber_PrivateKey>(rng, mode);
8✔
399
   }
400
#endif
401

402
#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
403
   if(alg_name == "Dilithium" || alg_name == "Dilithium-") {
225✔
404
      const auto mode = [&]() -> DilithiumMode {
18✔
405
         if(params.empty()) {
18✔
406
            return DilithiumMode::Dilithium6x5;
407
         }
408
         return DilithiumMode(params);
10✔
409
      }();
18✔
410

411
      return std::make_unique<Dilithium_PrivateKey>(rng, mode);
18✔
412
   }
413
#endif
414

415
#if defined(BOTAN_HAS_SPHINCS_PLUS)
416
   if(alg_name == "SPHINCS+" || alg_name == "SphincsPlus-") {
178✔
417
      auto sphincs_params = Sphincs_Parameters::create(params);
×
418

419
      return std::make_unique<SphincsPlus_PrivateKey>(rng, sphincs_params);
×
420
   }
421
#endif
422

423
#if defined(BOTAN_HAS_XMSS_RFC8391)
424
   if(alg_name == "XMSS") {
206✔
425
      const auto xmss_oid = [&]() -> XMSS_Parameters::xmss_algorithm_t {
8✔
426
         if(params.empty()) {
4✔
427
            return XMSS_Parameters::XMSS_SHA2_10_512;
428
         }
429
         return XMSS_Parameters(params).oid();
3✔
430
      }();
4✔
431

432
      return std::make_unique<XMSS_PrivateKey>(xmss_oid, rng);
4✔
433
   }
434
#endif
435

436
#if defined(BOTAN_HAS_ED25519)
437
   if(alg_name == "Ed25519") {
206✔
438
      return std::make_unique<Ed25519_PrivateKey>(rng);
20✔
439
   }
440
#endif
441

442
   // ECC crypto
443
#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
444

445
   if(alg_name == "ECDSA" || alg_name == "ECDH" || alg_name == "ECKCDSA" || alg_name == "ECGDSA" || alg_name == "SM2" ||
299✔
446
      alg_name == "SM2_Sig" || alg_name == "SM2_Enc" || alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" ||
91✔
447
      alg_name == "GOST-34.10-2012-512") {
38✔
448
      const std::string group_id = [&]() -> std::string {
252✔
449
         if(!params.empty()) {
126✔
450
            return std::string(params);
117✔
451
         }
452
         if(alg_name == "SM2" || alg_name == "SM2_Enc" || alg_name == "SM2_Sig") {
9✔
453
            return "sm2p256v1";
×
454
         }
455
         if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256") {
9✔
456
            return "gost_256A";
×
457
         }
458
         if(alg_name == "GOST-34.10-2012-512") {
9✔
459
            return "gost_512A";
×
460
         }
461
         if(alg_name == "ECGDSA") {
9✔
462
            return "brainpool256r1";
×
463
         }
464
         return "secp256r1";
9✔
465
      }();
126✔
466

467
      const EC_Group ec_group(group_id);
126✔
468
      return create_ec_private_key(alg_name, ec_group, rng);
126✔
469
   }
126✔
470
#endif
471

472
   // DL crypto
473
#if defined(BOTAN_HAS_DL_GROUP)
474
   if(alg_name == "DH" || alg_name == "DSA" || alg_name == "ElGamal") {
76✔
475
      const std::string group_id = [&]() -> std::string {
76✔
476
         if(!params.empty()) {
38✔
477
            return std::string(params);
28✔
478
         }
479
         if(alg_name == "DSA") {
10✔
480
            return "dsa/botan/2048";
8✔
481
         }
482
         return "modp/ietf/2048";
2✔
483
      }();
38✔
484

485
      DL_Group modp_group(group_id);
38✔
486

487
   #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
488
      if(alg_name == "DH") {
38✔
489
         return std::make_unique<DH_PrivateKey>(rng, modp_group);
36✔
490
      }
491
   #endif
492

493
   #if defined(BOTAN_HAS_DSA)
494
      if(alg_name == "DSA") {
20✔
495
         return std::make_unique<DSA_PrivateKey>(rng, modp_group);
26✔
496
      }
497
   #endif
498

499
   #if defined(BOTAN_HAS_ELGAMAL)
500
      if(alg_name == "ElGamal") {
7✔
501
         return std::make_unique<ElGamal_PrivateKey>(rng, modp_group);
14✔
502
      }
503
   #endif
504
   }
38✔
505
#endif
506

507
   BOTAN_UNUSED(alg_name, rng, params, provider);
×
508

509
   return std::unique_ptr<Private_Key>();
252✔
510
}
511

512
std::vector<std::string> probe_provider_private_key(std::string_view alg_name,
43✔
513
                                                    const std::vector<std::string>& possible) {
514
   std::vector<std::string> providers;
43✔
515

516
   for(auto&& prov : possible) {
215✔
517
      if(prov == "base") {
172✔
518
         providers.push_back(prov);
43✔
519
      }
520
   }
521

522
   BOTAN_UNUSED(alg_name);
43✔
523

524
   return providers;
43✔
525
}
×
526
}  // 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

© 2025 Coveralls, Inc