Botan  1.11.15
src/lib/pubkey/pk_algs.cpp
Go to the documentation of this file.
00001 /*
00002 * PK Key
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/pk_algs.h>
00009 #include <botan/oids.h>
00010 
00011 #if defined(BOTAN_HAS_RSA)
00012   #include <botan/rsa.h>
00013 #endif
00014 
00015 #if defined(BOTAN_HAS_DSA)
00016   #include <botan/dsa.h>
00017 #endif
00018 
00019 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
00020   #include <botan/dh.h>
00021 #endif
00022 
00023 #if defined(BOTAN_HAS_ECDSA)
00024   #include <botan/ecdsa.h>
00025 #endif
00026 
00027 #if defined(BOTAN_HAS_GOST_34_10_2001)
00028   #include <botan/gost_3410.h>
00029 #endif
00030 
00031 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
00032   #include <botan/nr.h>
00033 #endif
00034 
00035 #if defined(BOTAN_HAS_RW)
00036   #include <botan/rw.h>
00037 #endif
00038 
00039 #if defined(BOTAN_HAS_ELGAMAL)
00040   #include <botan/elgamal.h>
00041 #endif
00042 
00043 #if defined(BOTAN_HAS_ECDH)
00044   #include <botan/ecdh.h>
00045 #endif
00046 
00047 #if defined(BOTAN_HAS_CURVE_25519)
00048   #include <botan/curve25519.h>
00049 #endif
00050 
00051 namespace Botan {
00052 
00053 Public_Key* make_public_key(const AlgorithmIdentifier& alg_id,
00054                             const secure_vector<byte>& key_bits)
00055    {
00056    const std::string alg_name = OIDS::lookup(alg_id.oid);
00057    if(alg_name == "")
00058       throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
00059 
00060 #if defined(BOTAN_HAS_RSA)
00061    if(alg_name == "RSA")
00062       return new RSA_PublicKey(alg_id, key_bits);
00063 #endif
00064 
00065 #if defined(BOTAN_HAS_RW)
00066    if(alg_name == "RW")
00067       return new RW_PublicKey(alg_id, key_bits);
00068 #endif
00069 
00070 #if defined(BOTAN_HAS_DSA)
00071    if(alg_name == "DSA")
00072       return new DSA_PublicKey(alg_id, key_bits);
00073 #endif
00074 
00075 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
00076    if(alg_name == "DH")
00077       return new DH_PublicKey(alg_id, key_bits);
00078 #endif
00079 
00080 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
00081    if(alg_name == "NR")
00082       return new NR_PublicKey(alg_id, key_bits);
00083 #endif
00084 
00085 #if defined(BOTAN_HAS_ELGAMAL)
00086    if(alg_name == "ElGamal")
00087       return new ElGamal_PublicKey(alg_id, key_bits);
00088 #endif
00089 
00090 #if defined(BOTAN_HAS_ECDSA)
00091    if(alg_name == "ECDSA")
00092       return new ECDSA_PublicKey(alg_id, key_bits);
00093 #endif
00094 
00095 #if defined(BOTAN_HAS_GOST_34_10_2001)
00096    if(alg_name == "GOST-34.10")
00097       return new GOST_3410_PublicKey(alg_id, key_bits);
00098 #endif
00099 
00100 #if defined(BOTAN_HAS_ECDH)
00101    if(alg_name == "ECDH")
00102       return new ECDH_PublicKey(alg_id, key_bits);
00103 #endif
00104 
00105 #if defined(BOTAN_HAS_CURVE_25519)
00106    if(alg_name == "Curve25519")
00107       return new Curve25519_PublicKey(alg_id, key_bits);
00108 #endif
00109 
00110    throw Decoding_Error("Unhandled PK algorithm " + alg_name);
00111    }
00112 
00113 Private_Key* make_private_key(const AlgorithmIdentifier& alg_id,
00114                               const secure_vector<byte>& key_bits,
00115                               RandomNumberGenerator& rng)
00116    {
00117    const std::string alg_name = OIDS::lookup(alg_id.oid);
00118    if(alg_name == "")
00119       throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
00120 
00121 #if defined(BOTAN_HAS_RSA)
00122    if(alg_name == "RSA")
00123       return new RSA_PrivateKey(alg_id, key_bits, rng);
00124 #endif
00125 
00126 #if defined(BOTAN_HAS_RW)
00127    if(alg_name == "RW")
00128       return new RW_PrivateKey(alg_id, key_bits, rng);
00129 #endif
00130 
00131 #if defined(BOTAN_HAS_DSA)
00132    if(alg_name == "DSA")
00133       return new DSA_PrivateKey(alg_id, key_bits, rng);
00134 #endif
00135 
00136 #if defined(BOTAN_HAS_DIFFIE_HELLMAN)
00137    if(alg_name == "DH")
00138       return new DH_PrivateKey(alg_id, key_bits, rng);
00139 #endif
00140 
00141 #if defined(BOTAN_HAS_NYBERG_RUEPPEL)
00142    if(alg_name == "NR")
00143       return new NR_PrivateKey(alg_id, key_bits, rng);
00144 #endif
00145 
00146 #if defined(BOTAN_HAS_ELGAMAL)
00147    if(alg_name == "ElGamal")
00148       return new ElGamal_PrivateKey(alg_id, key_bits, rng);
00149 #endif
00150 
00151 #if defined(BOTAN_HAS_ECDSA)
00152    if(alg_name == "ECDSA")
00153       return new ECDSA_PrivateKey(alg_id, key_bits);
00154 #endif
00155 
00156 #if defined(BOTAN_HAS_GOST_34_10_2001)
00157    if(alg_name == "GOST-34.10")
00158       return new GOST_3410_PrivateKey(alg_id, key_bits);
00159 #endif
00160 
00161 #if defined(BOTAN_HAS_ECDH)
00162    if(alg_name == "ECDH")
00163       return new ECDH_PrivateKey(alg_id, key_bits);
00164 #endif
00165 
00166 #if defined(BOTAN_HAS_CURVE_25519)
00167    if(alg_name == "Curve25519")
00168       return new Curve25519_PrivateKey(alg_id, key_bits, rng);
00169 #endif
00170 
00171    throw Decoding_Error("Unhandled PK algorithm " + alg_name);
00172    }
00173 
00174 }