1 /**
2 * PK Key Factory
3 * 
4 * Copyright:
5 * (C) 1999-2010 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.pubkey.pk_algs;
12 
13 import botan.constants;
14 static if (BOTAN_HAS_PUBLIC_KEY_CRYPTO):
15 
16 import botan.pubkey.pk_keys;
17 import botan.asn1.oids;
18 import botan.utils.types;
19 import botan.rng.rng;
20 
21 static if (BOTAN_HAS_RSA)                  import botan.pubkey.algo.rsa;
22 static if (BOTAN_HAS_DSA)                  import botan.pubkey.algo.dsa;
23 static if (BOTAN_HAS_DIFFIE_HELLMAN)       import botan.pubkey.algo.dh;
24 static if (BOTAN_HAS_ECDSA)                import botan.pubkey.algo.ecdsa;
25 static if (BOTAN_HAS_GOST_34_10_2001)      import botan.pubkey.algo.gost_3410;
26 static if (BOTAN_HAS_NYBERG_RUEPPEL)       import botan.pubkey.algo.nr;
27 static if (BOTAN_HAS_RW)                   import botan.pubkey.algo.rw;
28 static if (BOTAN_HAS_ELGAMAL)              import botan.pubkey.algo.elgamal;
29 static if (BOTAN_HAS_ECDH)                 import botan.pubkey.algo.ecdh;
30 static if (BOTAN_HAS_CURVE25519)           import botan.pubkey.algo.curve25519;
31 
32 PublicKey makePublicKey(in AlgorithmIdentifier alg_id, const ref SecureVector!ubyte key_bits)
33 {
34     const string alg_name = OIDS.lookup(alg_id.oid);
35     if (alg_name == "")
36         throw new DecodingError("Unknown algorithm OID: " ~ alg_id.oid.toString());
37     
38     static if (BOTAN_HAS_RSA) {
39         if (alg_name == "RSA")
40             return RSAPublicKey(alg_id, key_bits).release();
41     }
42     
43     static if (BOTAN_HAS_RW) {
44         if (alg_name == "RW")
45 			return RWPublicKey(alg_id, key_bits).release();
46     }
47     
48     static if (BOTAN_HAS_DSA) {
49         if (alg_name == "DSA")
50 			return DSAPublicKey(alg_id, key_bits).release();
51     }
52     
53     static if (BOTAN_HAS_DIFFIE_HELLMAN) {
54         if (alg_name == "DH")
55 			return DHPublicKey(alg_id, key_bits).release();
56     }
57     
58     static if (BOTAN_HAS_NYBERG_RUEPPEL) {
59         if (alg_name == "NR")
60 			return NRPublicKey(alg_id, key_bits).release();
61     }
62     
63     static if (BOTAN_HAS_ELGAMAL) {
64         if (alg_name == "ElGamal")
65 			return ElGamalPublicKey(alg_id, key_bits).release();
66     }
67     
68     static if (BOTAN_HAS_ECDSA) {
69         if (alg_name == "ECDSA")
70 			return ECDSAPublicKey(alg_id, key_bits).release();
71     }
72     
73     static if (BOTAN_HAS_GOST_34_10_2001) {
74         if (alg_name == "GOST-34.10")
75 			return GOST3410PublicKey(alg_id, key_bits).release();
76     }
77     
78     static if (BOTAN_HAS_ECDH) {
79         if (alg_name == "ECDH")
80 			return ECDHPublicKey(alg_id, key_bits).release();
81     }
82 
83 	static if (BOTAN_HAS_CURVE25519) {
84 		if (alg_name == "Curve25519")
85 			return Curve25519PublicKey(alg_id, key_bits).release();
86 	}
87     
88     return null;
89 }
90 
91 // TODO: use Embed!'s release() and make ~this delete the inner object if set
92 PrivateKey makePrivateKey(const ref AlgorithmIdentifier alg_id,
93                           const ref SecureVector!ubyte key_bits,
94                           RandomNumberGenerator rng)
95 {
96     const string alg_name = OIDS.lookup(alg_id.oid);
97     if (alg_name == "")
98         throw new DecodingError("Unknown algorithm OID: " ~ alg_id.oid.toString());
99     
100     static if (BOTAN_HAS_RSA) {
101         if (alg_name == "RSA")
102 			return RSAPrivateKey(alg_id, key_bits, rng).release();
103     }
104     
105     static if (BOTAN_HAS_RW) {
106         if (alg_name == "RW")
107 			return RWPrivateKey(alg_id, key_bits, rng).release();
108     }
109     
110     static if (BOTAN_HAS_DSA) {
111         if (alg_name == "DSA")
112 			return DSAPrivateKey(alg_id, key_bits, rng).release();
113     }
114     
115     static if (BOTAN_HAS_DIFFIE_HELLMAN) {
116         if (alg_name == "DH")
117 			return DHPrivateKey(alg_id, key_bits, rng).release();
118     }
119     
120     static if (BOTAN_HAS_NYBERG_RUEPPEL) {
121         if (alg_name == "NR")
122 			return NRPrivateKey(alg_id, key_bits, rng).release();
123     }
124     
125     static if (BOTAN_HAS_ELGAMAL) {
126         if (alg_name == "ElGamal")
127 			return ElGamalPrivateKey(alg_id, key_bits, rng).release();
128     }
129     
130     static if (BOTAN_HAS_ECDSA) {
131         if (alg_name == "ECDSA")
132 			return ECDSAPrivateKey(alg_id, key_bits).release();
133     }
134     
135     static if (BOTAN_HAS_GOST_34_10_2001) {
136         if (alg_name == "GOST-34.10")
137 			return GOST3410PrivateKey(alg_id, key_bits).release();
138     }
139     
140     static if (BOTAN_HAS_ECDH) {
141         if (alg_name == "ECDH")
142 			return ECDHPrivateKey(alg_id, key_bits).release();
143     }
144 	
145 	static if (BOTAN_HAS_CURVE25519) {
146 		if (alg_name == "Curve25519")
147 			return Curve25519PrivateKey(alg_id, key_bits, rng).release();
148 	}
149     
150     return null;
151 }