Botan  1.11.32
pk_algs.cpp
Go to the documentation of this file.
1 /*
2 * PK Key
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/pk_algs.h>
9 #include <botan/oids.h>
10 
11 #if defined(BOTAN_HAS_RSA)
12  #include <botan/rsa.h>
13 #endif
14 
15 #if defined(BOTAN_HAS_DSA)
16  #include <botan/dsa.h>
17 #endif
18 
19 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
20  #include <botan/dh.h>
21 #endif
22 
23 #if defined(BOTAN_HAS_ECDSA)
24  #include <botan/ecdsa.h>
25 #endif
26 
27 #if defined(BOTAN_HAS_ECGDSA)
28  #include <botan/ecgdsa.h>
29 #endif
30 
31 #if defined(BOTAN_HAS_ECKCDSA)
32  #include <botan/eckcdsa.h>
33 #endif
34 
35 #if defined(BOTAN_HAS_GOST_34_10_2001)
36  #include <botan/gost_3410.h>
37 #endif
38 
39 #if defined(BOTAN_HAS_ELGAMAL)
40  #include <botan/elgamal.h>
41 #endif
42 
43 #if defined(BOTAN_HAS_ECDH)
44  #include <botan/ecdh.h>
45 #endif
46 
47 #if defined(BOTAN_HAS_CURVE_25519)
48  #include <botan/curve25519.h>
49 #endif
50 
51 #if defined(BOTAN_HAS_MCELIECE)
52  #include <botan/mceliece.h>
53 #endif
54 
55 namespace Botan {
56 
58  const secure_vector<byte>& key_bits)
59  {
60  const std::string alg_name = OIDS::lookup(alg_id.oid);
61  if(alg_name == "")
62  throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
63 
64 #if defined(BOTAN_HAS_RSA)
65  if(alg_name == "RSA")
66  return new RSA_PublicKey(alg_id, key_bits);
67 #endif
68 
69 #if defined(BOTAN_HAS_DSA)
70  if(alg_name == "DSA")
71  return new DSA_PublicKey(alg_id, key_bits);
72 #endif
73 
74 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
75  if(alg_name == "DH")
76  return new DH_PublicKey(alg_id, key_bits);
77 #endif
78 
79 #if defined(BOTAN_HAS_ELGAMAL)
80  if(alg_name == "ElGamal")
81  return new ElGamal_PublicKey(alg_id, key_bits);
82 #endif
83 
84 #if defined(BOTAN_HAS_ECDSA)
85  if(alg_name == "ECDSA")
86  return new ECDSA_PublicKey(alg_id, key_bits);
87 #endif
88 
89 #if defined(BOTAN_HAS_ECGDSA)
90  if(alg_name == "ECGDSA")
91  return new ECGDSA_PublicKey(alg_id, key_bits);
92 #endif
93 
94 #if defined(BOTAN_HAS_ECKCDSA)
95  if(alg_name == "ECKCDSA")
96  return new ECKCDSA_PublicKey(alg_id, key_bits);
97 #endif
98 
99 #if defined(BOTAN_HAS_GOST_34_10_2001)
100  if(alg_name == "GOST-34.10")
101  return new GOST_3410_PublicKey(alg_id, key_bits);
102 #endif
103 
104 #if defined(BOTAN_HAS_ECDH)
105  if(alg_name == "ECDH")
106  return new ECDH_PublicKey(alg_id, key_bits);
107 #endif
108 
109 #if defined(BOTAN_HAS_CURVE_25519)
110  if(alg_name == "Curve25519")
111  return new Curve25519_PublicKey(alg_id, key_bits);
112 #endif
113 
114 #if defined(BOTAN_HAS_MCELIECE)
115  if(alg_name == "McEliece")
116  return new McEliece_PublicKey(unlock(key_bits));
117 #endif
118 
119  throw Decoding_Error("Unhandled PK algorithm " + alg_name);
120  }
121 
123  const secure_vector<byte>& key_bits,
125  {
126  const std::string alg_name = OIDS::lookup(alg_id.oid);
127  if(alg_name == "")
128  throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
129 
130 #if defined(BOTAN_HAS_RSA)
131  if(alg_name == "RSA")
132  return new RSA_PrivateKey(alg_id, key_bits, rng);
133 #endif
134 
135 #if defined(BOTAN_HAS_DSA)
136  if(alg_name == "DSA")
137  return new DSA_PrivateKey(alg_id, key_bits, rng);
138 #endif
139 
140 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
141  if(alg_name == "DH")
142  return new DH_PrivateKey(alg_id, key_bits, rng);
143 #endif
144 
145 #if defined(BOTAN_HAS_ELGAMAL)
146  if(alg_name == "ElGamal")
147  return new ElGamal_PrivateKey(alg_id, key_bits, rng);
148 #endif
149 
150 #if defined(BOTAN_HAS_ECDSA)
151  if(alg_name == "ECDSA")
152  return new ECDSA_PrivateKey(alg_id, key_bits);
153 #endif
154 
155 #if defined(BOTAN_HAS_ECGDSA)
156  if(alg_name == "ECGDSA")
157  return new ECGDSA_PrivateKey(alg_id, key_bits);
158 #endif
159 
160 #if defined(BOTAN_HAS_ECKCDSA)
161  if(alg_name == "ECKCDSA")
162  return new ECKCDSA_PrivateKey(alg_id, key_bits);
163 #endif
164 
165 #if defined(BOTAN_HAS_GOST_34_10_2001)
166  if(alg_name == "GOST-34.10")
167  return new GOST_3410_PrivateKey(alg_id, key_bits);
168 #endif
169 
170 #if defined(BOTAN_HAS_ECDH)
171  if(alg_name == "ECDH")
172  return new ECDH_PrivateKey(alg_id, key_bits);
173 #endif
174 
175 #if defined(BOTAN_HAS_CURVE_25519)
176  if(alg_name == "Curve25519")
177  return new Curve25519_PrivateKey(alg_id, key_bits, rng);
178 #endif
179 
180 #if defined(BOTAN_HAS_MCELIECE)
181  if(alg_name == "McEliece")
182  return new McEliece_PrivateKey(key_bits);
183 #endif
184 
185  throw Decoding_Error("Unhandled PK algorithm " + alg_name);
186  }
187 
188 }
Private_Key * make_private_key(const AlgorithmIdentifier &alg_id, const secure_vector< byte > &key_bits, RandomNumberGenerator &rng)
Definition: pk_algs.cpp:122
std::string as_string() const
Definition: asn1_oid.cpp:50
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const secure_vector< byte > &key_bits)
Definition: pk_algs.cpp:57
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:104
std::string lookup(const OID &oid)
Definition: oids.cpp:155
Definition: alg_id.cpp:13
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:108