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