Botan  1.11.30
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_GOST_34_10_2001)
32  #include <botan/gost_3410.h>
33 #endif
34 
35 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
36  #include <botan/nr.h>
37 #endif
38 
39 #if defined(BOTAN_HAS_RW)
40  #include <botan/rw.h>
41 #endif
42 
43 #if defined(BOTAN_HAS_ELGAMAL)
44  #include <botan/elgamal.h>
45 #endif
46 
47 #if defined(BOTAN_HAS_ECDH)
48  #include <botan/ecdh.h>
49 #endif
50 
51 #if defined(BOTAN_HAS_CURVE_25519)
52  #include <botan/curve25519.h>
53 #endif
54 
55 #if defined(BOTAN_HAS_MCELIECE)
56  #include <botan/mceliece.h>
57 #endif
58 
59 namespace Botan {
60 
62  const secure_vector<byte>& key_bits)
63  {
64  const std::string alg_name = OIDS::lookup(alg_id.oid);
65  if(alg_name == "")
66  throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
67 
68 #if defined(BOTAN_HAS_RSA)
69  if(alg_name == "RSA")
70  return new RSA_PublicKey(alg_id, key_bits);
71 #endif
72 
73 #if defined(BOTAN_HAS_RW)
74  if(alg_name == "RW")
75  return new RW_PublicKey(alg_id, key_bits);
76 #endif
77 
78 #if defined(BOTAN_HAS_DSA)
79  if(alg_name == "DSA")
80  return new DSA_PublicKey(alg_id, key_bits);
81 #endif
82 
83 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
84  if(alg_name == "DH")
85  return new DH_PublicKey(alg_id, key_bits);
86 #endif
87 
88 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
89  if(alg_name == "NR")
90  return new NR_PublicKey(alg_id, key_bits);
91 #endif
92 
93 #if defined(BOTAN_HAS_ELGAMAL)
94  if(alg_name == "ElGamal")
95  return new ElGamal_PublicKey(alg_id, key_bits);
96 #endif
97 
98 #if defined(BOTAN_HAS_ECDSA)
99  if(alg_name == "ECDSA")
100  return new ECDSA_PublicKey(alg_id, key_bits);
101 #endif
102 
103 #if defined(BOTAN_HAS_ECGDSA)
104  if(alg_name == "ECGDSA")
105  return new ECGDSA_PublicKey(alg_id, key_bits);
106 #endif
107 
108 #if defined(BOTAN_HAS_GOST_34_10_2001)
109  if(alg_name == "GOST-34.10")
110  return new GOST_3410_PublicKey(alg_id, key_bits);
111 #endif
112 
113 #if defined(BOTAN_HAS_ECDH)
114  if(alg_name == "ECDH")
115  return new ECDH_PublicKey(alg_id, key_bits);
116 #endif
117 
118 #if defined(BOTAN_HAS_CURVE_25519)
119  if(alg_name == "Curve25519")
120  return new Curve25519_PublicKey(alg_id, key_bits);
121 #endif
122 
123 #if defined(BOTAN_HAS_MCELIECE)
124  if(alg_name == "McEliece")
125  return new McEliece_PublicKey(unlock(key_bits));
126 #endif
127 
128  throw Decoding_Error("Unhandled PK algorithm " + alg_name);
129  }
130 
132  const secure_vector<byte>& key_bits,
134  {
135  const std::string alg_name = OIDS::lookup(alg_id.oid);
136  if(alg_name == "")
137  throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
138 
139 #if defined(BOTAN_HAS_RSA)
140  if(alg_name == "RSA")
141  return new RSA_PrivateKey(alg_id, key_bits, rng);
142 #endif
143 
144 #if defined(BOTAN_HAS_RW)
145  if(alg_name == "RW")
146  return new RW_PrivateKey(alg_id, key_bits, rng);
147 #endif
148 
149 #if defined(BOTAN_HAS_DSA)
150  if(alg_name == "DSA")
151  return new DSA_PrivateKey(alg_id, key_bits, rng);
152 #endif
153 
154 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
155  if(alg_name == "DH")
156  return new DH_PrivateKey(alg_id, key_bits, rng);
157 #endif
158 
159 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
160  if(alg_name == "NR")
161  return new NR_PrivateKey(alg_id, key_bits, rng);
162 #endif
163 
164 #if defined(BOTAN_HAS_ELGAMAL)
165  if(alg_name == "ElGamal")
166  return new ElGamal_PrivateKey(alg_id, key_bits, rng);
167 #endif
168 
169 #if defined(BOTAN_HAS_ECDSA)
170  if(alg_name == "ECDSA")
171  return new ECDSA_PrivateKey(alg_id, key_bits);
172 #endif
173 
174 #if defined(BOTAN_HAS_ECGDSA)
175  if(alg_name == "ECGDSA")
176  return new ECGDSA_PrivateKey(alg_id, key_bits);
177 #endif
178 
179 #if defined(BOTAN_HAS_GOST_34_10_2001)
180  if(alg_name == "GOST-34.10")
181  return new GOST_3410_PrivateKey(alg_id, key_bits);
182 #endif
183 
184 #if defined(BOTAN_HAS_ECDH)
185  if(alg_name == "ECDH")
186  return new ECDH_PrivateKey(alg_id, key_bits);
187 #endif
188 
189 #if defined(BOTAN_HAS_CURVE_25519)
190  if(alg_name == "Curve25519")
191  return new Curve25519_PrivateKey(alg_id, key_bits, rng);
192 #endif
193 
194 #if defined(BOTAN_HAS_MCELIECE)
195  if(alg_name == "McEliece")
196  return new McEliece_PrivateKey(key_bits);
197 #endif
198 
199  throw Decoding_Error("Unhandled PK algorithm " + alg_name);
200  }
201 
202 }
Private_Key * make_private_key(const AlgorithmIdentifier &alg_id, const secure_vector< byte > &key_bits, RandomNumberGenerator &rng)
Definition: pk_algs.cpp:131
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const secure_vector< byte > &key_bits)
Definition: pk_algs.cpp:61
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:96
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:100
std::string as_string() const
Definition: asn1_oid.cpp:50