8#include <botan/pk_algs.h>
9#include <botan/parsing.h>
11#if defined(BOTAN_HAS_RSA)
12 #include <botan/rsa.h>
15#if defined(BOTAN_HAS_DSA)
16 #include <botan/dsa.h>
19#if defined(BOTAN_HAS_DL_GROUP)
20 #include <botan/dl_group.h>
23#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
27#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
28 #include <botan/ecc_key.h>
31#if defined(BOTAN_HAS_ECDSA)
32 #include <botan/ecdsa.h>
35#if defined(BOTAN_HAS_ECGDSA)
36 #include <botan/ecgdsa.h>
39#if defined(BOTAN_HAS_ECKCDSA)
40 #include <botan/eckcdsa.h>
43#if defined(BOTAN_HAS_ED25519)
44 #include <botan/ed25519.h>
47#if defined(BOTAN_HAS_GOST_34_10_2001)
48 #include <botan/gost_3410.h>
51#if defined(BOTAN_HAS_ELGAMAL)
52 #include <botan/elgamal.h>
55#if defined(BOTAN_HAS_ECDH)
56 #include <botan/ecdh.h>
59#if defined(BOTAN_HAS_CURVE_25519)
60 #include <botan/curve25519.h>
63#if defined(BOTAN_HAS_MCELIECE)
64 #include <botan/mceliece.h>
67#if defined(BOTAN_HAS_XMSS_RFC8391)
68 #include <botan/xmss.h>
71#if defined(BOTAN_HAS_SM2)
72 #include <botan/sm2.h>
77std::unique_ptr<Public_Key>
79 const std::vector<uint8_t>& key_bits)
82 const std::vector<std::string> alg_info =
split_on(oid_str,
'/');
83 const std::string alg_name = alg_info[0];
85#if defined(BOTAN_HAS_RSA)
87 return std::unique_ptr<Public_Key>(
new RSA_PublicKey(alg_id, key_bits));
90#if defined(BOTAN_HAS_CURVE_25519)
91 if(alg_name ==
"Curve25519")
95#if defined(BOTAN_HAS_MCELIECE)
96 if(alg_name ==
"McEliece")
100#if defined(BOTAN_HAS_ECDSA)
101 if(alg_name ==
"ECDSA")
102 return std::unique_ptr<Public_Key>(
new ECDSA_PublicKey(alg_id, key_bits));
105#if defined(BOTAN_HAS_ECDH)
106 if(alg_name ==
"ECDH")
107 return std::unique_ptr<Public_Key>(
new ECDH_PublicKey(alg_id, key_bits));
110#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
112 return std::unique_ptr<Public_Key>(
new DH_PublicKey(alg_id, key_bits));
115#if defined(BOTAN_HAS_DSA)
116 if(alg_name ==
"DSA")
117 return std::unique_ptr<Public_Key>(
new DSA_PublicKey(alg_id, key_bits));
120#if defined(BOTAN_HAS_ELGAMAL)
121 if(alg_name ==
"ElGamal")
125#if defined(BOTAN_HAS_ECGDSA)
126 if(alg_name ==
"ECGDSA")
130#if defined(BOTAN_HAS_ECKCDSA)
131 if(alg_name ==
"ECKCDSA")
135#if defined(BOTAN_HAS_ED25519)
136 if(alg_name ==
"Ed25519")
140#if defined(BOTAN_HAS_GOST_34_10_2001)
141 if(alg_name ==
"GOST-34.10" || alg_name ==
"GOST-34.10-2012-256" || alg_name ==
"GOST-34.10-2012-512")
145#if defined(BOTAN_HAS_SM2)
146 if(alg_name ==
"SM2" || alg_name ==
"SM2_Sig" || alg_name ==
"SM2_Enc")
147 return std::unique_ptr<Public_Key>(
new SM2_PublicKey(alg_id, key_bits));
150#if defined(BOTAN_HAS_XMSS_RFC8391)
151 if(alg_name ==
"XMSS")
155 throw Decoding_Error(
"Unknown or unavailable public key algorithm " + alg_name);
158std::unique_ptr<Private_Key>
164#if defined(BOTAN_HAS_RSA)
165 if(alg_name ==
"RSA")
166 return std::unique_ptr<Private_Key>(
new RSA_PrivateKey(alg_id, key_bits));
169#if defined(BOTAN_HAS_CURVE_25519)
170 if(alg_name ==
"Curve25519")
174#if defined(BOTAN_HAS_ECDSA)
175 if(alg_name ==
"ECDSA")
176 return std::unique_ptr<Private_Key>(
new ECDSA_PrivateKey(alg_id, key_bits));
179#if defined(BOTAN_HAS_ECDH)
180 if(alg_name ==
"ECDH")
181 return std::unique_ptr<Private_Key>(
new ECDH_PrivateKey(alg_id, key_bits));
184#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
186 return std::unique_ptr<Private_Key>(
new DH_PrivateKey(alg_id, key_bits));
189#if defined(BOTAN_HAS_DSA)
190 if(alg_name ==
"DSA")
191 return std::unique_ptr<Private_Key>(
new DSA_PrivateKey(alg_id, key_bits));
194#if defined(BOTAN_HAS_MCELIECE)
195 if(alg_name ==
"McEliece")
199#if defined(BOTAN_HAS_ECGDSA)
200 if(alg_name ==
"ECGDSA")
204#if defined(BOTAN_HAS_ECKCDSA)
205 if(alg_name ==
"ECKCDSA")
209#if defined(BOTAN_HAS_ED25519)
210 if(alg_name ==
"Ed25519")
214#if defined(BOTAN_HAS_GOST_34_10_2001)
215 if(alg_name ==
"GOST-34.10" || alg_name ==
"GOST-34.10-2012-256" || alg_name ==
"GOST-34.10-2012-512")
219#if defined(BOTAN_HAS_SM2)
220 if(alg_name ==
"SM2" || alg_name ==
"SM2_Sig" || alg_name ==
"SM2_Enc")
221 return std::unique_ptr<Private_Key>(
new SM2_PrivateKey(alg_id, key_bits));
224#if defined(BOTAN_HAS_ELGAMAL)
225 if(alg_name ==
"ElGamal")
229#if defined(BOTAN_HAS_XMSS_RFC8391)
230 if(alg_name ==
"XMSS")
234 throw Decoding_Error(
"Unknown or unavailable public key algorithm " + alg_name);
237#if defined(BOTAN_HAS_ECC_GROUP)
241std::string default_ec_group_for(
const std::string& alg_name)
243 if(alg_name ==
"SM2" || alg_name ==
"SM2_Enc" || alg_name ==
"SM2_Sig")
245 if(alg_name ==
"GOST-34.10" || alg_name ==
"GOST-34.10-2012-256")
247 if(alg_name ==
"GOST-34.10-2012-512")
249 if(alg_name ==
"ECGDSA")
250 return "brainpool256r1";
259std::unique_ptr<Private_Key>
262 const std::string& params,
263 const std::string& provider)
269#if defined(BOTAN_HAS_CURVE_25519)
270 if(alg_name ==
"Curve25519")
274#if defined(BOTAN_HAS_RSA)
275 if(alg_name ==
"RSA")
277 const size_t rsa_bits = (params.empty() ? 3072 :
to_u32bit(params));
278 return std::unique_ptr<Private_Key>(
new RSA_PrivateKey(rng, rsa_bits));
282#if defined(BOTAN_HAS_MCELIECE)
283 if(alg_name ==
"McEliece")
285 std::vector<std::string> mce_param =
288 if(mce_param.size() != 2)
289 throw Invalid_Argument(
"create_private_key bad McEliece parameters " + params);
298#if defined(BOTAN_HAS_XMSS_RFC8391)
299 if(alg_name ==
"XMSS")
301 return std::unique_ptr<Private_Key>(
306#if defined(BOTAN_HAS_ED25519)
307 if(alg_name ==
"Ed25519")
314#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
316 if(alg_name ==
"ECDSA" ||
317 alg_name ==
"ECDH" ||
318 alg_name ==
"ECKCDSA" ||
319 alg_name ==
"ECGDSA" ||
321 alg_name ==
"SM2_Sig" ||
322 alg_name ==
"SM2_Enc" ||
323 alg_name ==
"GOST-34.10" ||
324 alg_name ==
"GOST-34.10-2012-256" ||
325 alg_name ==
"GOST-34.10-2012-512")
327 const EC_Group ec_group(params.empty() ? default_ec_group_for(alg_name) : params);
329#if defined(BOTAN_HAS_ECDSA)
330 if(alg_name ==
"ECDSA")
334#if defined(BOTAN_HAS_ECDH)
335 if(alg_name ==
"ECDH")
336 return std::unique_ptr<Private_Key>(
new ECDH_PrivateKey(rng, ec_group));
339#if defined(BOTAN_HAS_ECKCDSA)
340 if(alg_name ==
"ECKCDSA")
344#if defined(BOTAN_HAS_GOST_34_10_2001)
345 if(alg_name ==
"GOST-34.10" || alg_name ==
"GOST-34.10-2012-256" || alg_name ==
"GOST-34.10-2012-512")
349#if defined(BOTAN_HAS_SM2)
350 if(alg_name ==
"SM2" || alg_name ==
"SM2_Sig" || alg_name ==
"SM2_Enc")
351 return std::unique_ptr<Private_Key>(
new SM2_PrivateKey(rng, ec_group));
354#if defined(BOTAN_HAS_ECGDSA)
355 if(alg_name ==
"ECGDSA")
362#if defined(BOTAN_HAS_DL_GROUP)
363 if(alg_name ==
"DH" || alg_name ==
"DSA" || alg_name ==
"ElGamal")
365 std::string default_group = (alg_name ==
"DSA") ?
"dsa/botan/2048" :
"modp/ietf/2048";
366 DL_Group modp_group(params.empty() ? default_group : params);
368#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
370 return std::unique_ptr<Private_Key>(
new DH_PrivateKey(rng, modp_group));
373#if defined(BOTAN_HAS_DSA)
374 if(alg_name ==
"DSA")
375 return std::unique_ptr<Private_Key>(
new DSA_PrivateKey(rng, modp_group));
378#if defined(BOTAN_HAS_ELGAMAL)
379 if(alg_name ==
"ElGamal")
387 return std::unique_ptr<Private_Key>();
390std::vector<std::string>
392 const std::vector<std::string> possible)
394 std::vector<std::string> providers;
395 for(
auto&& prov : possible)
399 providers.push_back(prov);
#define BOTAN_UNUSED(...)
const OID & get_oid() const
std::string to_formatted_string() const
xmss_algorithm_t oid() const
std::unique_ptr< Private_Key > load_private_key(const AlgorithmIdentifier &alg_id, const secure_vector< uint8_t > &key_bits)
std::vector< std::string > split_on(const std::string &str, char delim)
std::vector< std::string > probe_provider_private_key(const std::string &alg_name, const std::vector< std::string > possible)
std::unique_ptr< Private_Key > create_private_key(const std::string &alg_name, RandomNumberGenerator &rng, const std::string ¶ms, const std::string &provider)
uint32_t to_u32bit(const std::string &str)
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, const std::vector< uint8_t > &key_bits)
std::vector< T, secure_allocator< T > > secure_vector