diff options
author | lloyd <[email protected]> | 2008-09-28 20:41:59 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 20:41:59 +0000 |
commit | 062e7a3dc98af064792967cfac44f61b0e7b0d8a (patch) | |
tree | d8902fae073fd8268424ab804ae9d26b1fd3339d /src | |
parent | 25995ac07f9630b3e1da2c503054b9307b3fd788 (diff) |
Modularize the public key algorithms, though currently a great deal of
the underlying implementation goop remains in the core library instead of
being shunted off, due to various dependencies it has (most of which it
shouldn't).
Diffstat (limited to 'src')
-rw-r--r-- | src/pk/dh/dh.cpp (renamed from src/dh.cpp) | 0 | ||||
-rw-r--r-- | src/pk/dh/dh.h | 57 | ||||
-rw-r--r-- | src/pk/dsa/dsa.cpp (renamed from src/dsa.cpp) | 0 | ||||
-rw-r--r-- | src/pk/dsa/dsa.h | 60 | ||||
-rw-r--r-- | src/pk/elgamal/elgamal.cpp (renamed from src/elgamal.cpp) | 0 | ||||
-rw-r--r-- | src/pk/elgamal/elgamal.h | 57 | ||||
-rw-r--r-- | src/pk/nr/nr.cpp (renamed from src/nr.cpp) | 0 | ||||
-rw-r--r-- | src/pk/nr/nr.h | 61 | ||||
-rw-r--r-- | src/pk/rsa/rsa.cpp (renamed from src/rsa.cpp) | 0 | ||||
-rw-r--r-- | src/pk/rsa/rsa.h | 63 | ||||
-rw-r--r-- | src/pk/rw/rw.cpp (renamed from src/rw.cpp) | 0 | ||||
-rw-r--r-- | src/pk/rw/rw.h | 54 | ||||
-rw-r--r-- | src/pk_algs.cpp | 86 |
13 files changed, 422 insertions, 16 deletions
diff --git a/src/dh.cpp b/src/pk/dh/dh.cpp index 8d2059936..8d2059936 100644 --- a/src/dh.cpp +++ b/src/pk/dh/dh.cpp diff --git a/src/pk/dh/dh.h b/src/pk/dh/dh.h new file mode 100644 index 000000000..17a3fcae1 --- /dev/null +++ b/src/pk/dh/dh.h @@ -0,0 +1,57 @@ +/************************************************* +* Diffie-Hellman Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_DIFFIE_HELLMAN_H__ +#define BOTAN_DIFFIE_HELLMAN_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* Diffie-Hellman Public Key * +*************************************************/ +class BOTAN_DLL DH_PublicKey : public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "DH"; } + + MemoryVector<byte> public_value() const; + u32bit max_input_bits() const; + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + + DH_PublicKey() {} + DH_PublicKey(const DL_Group&, const BigInt&); + private: + void X509_load_hook(); + }; + +/************************************************* +* Diffie-Hellman Private Key * +*************************************************/ +class BOTAN_DLL DH_PrivateKey : public DH_PublicKey, + public PK_Key_Agreement_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> derive_key(const byte[], u32bit) const; + SecureVector<byte> derive_key(const DH_PublicKey&) const; + SecureVector<byte> derive_key(const BigInt&) const; + + MemoryVector<byte> public_value() const; + + DH_PrivateKey() {} + DH_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + DH_Core core; + }; + +} + +#endif diff --git a/src/dsa.cpp b/src/pk/dsa/dsa.cpp index a7eb8e789..a7eb8e789 100644 --- a/src/dsa.cpp +++ b/src/pk/dsa/dsa.cpp diff --git a/src/pk/dsa/dsa.h b/src/pk/dsa/dsa.h new file mode 100644 index 000000000..59776147b --- /dev/null +++ b/src/pk/dsa/dsa.h @@ -0,0 +1,60 @@ +/************************************************* +* DSA Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_DSA_H__ +#define BOTAN_DSA_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* DSA Public Key * +*************************************************/ +class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "DSA"; } + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + + bool verify(const byte[], u32bit, const byte[], u32bit) const; + u32bit max_input_bits() const; + + DSA_PublicKey() {} + DSA_PublicKey(const DL_Group&, const BigInt&); + protected: + DSA_Core core; + private: + void X509_load_hook(); + }; + +/************************************************* +* DSA Private Key * +*************************************************/ +class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + DSA_PrivateKey() {} + DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false); + }; + +} + +#endif diff --git a/src/elgamal.cpp b/src/pk/elgamal/elgamal.cpp index ea0d581b0..ea0d581b0 100644 --- a/src/elgamal.cpp +++ b/src/pk/elgamal/elgamal.cpp diff --git a/src/pk/elgamal/elgamal.h b/src/pk/elgamal/elgamal.h new file mode 100644 index 000000000..feeeb3953 --- /dev/null +++ b/src/pk/elgamal/elgamal.h @@ -0,0 +1,57 @@ +/************************************************* +* ElGamal Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_ELGAMAL_H__ +#define BOTAN_ELGAMAL_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* ElGamal Public Key * +*************************************************/ +class BOTAN_DLL ElGamal_PublicKey : public PK_Encrypting_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "ElGamal"; } + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + + SecureVector<byte> encrypt(const byte[], u32bit, + RandomNumberGenerator& rng) const; + u32bit max_input_bits() const; + + ElGamal_PublicKey() {} + ElGamal_PublicKey(const DL_Group&, const BigInt&); + protected: + ELG_Core core; + private: + void X509_load_hook(); + }; + +/************************************************* +* ElGamal Private Key * +*************************************************/ +class BOTAN_DLL ElGamal_PrivateKey : public ElGamal_PublicKey, + public PK_Decrypting_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> decrypt(const byte[], u32bit) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + ElGamal_PrivateKey() {} + ElGamal_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + }; + +} + +#endif diff --git a/src/nr.cpp b/src/pk/nr/nr.cpp index 02919d52e..02919d52e 100644 --- a/src/nr.cpp +++ b/src/pk/nr/nr.cpp diff --git a/src/pk/nr/nr.h b/src/pk/nr/nr.h new file mode 100644 index 000000000..0b68340d6 --- /dev/null +++ b/src/pk/nr/nr.h @@ -0,0 +1,61 @@ +/************************************************* +* Nyberg-Rueppel Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_NYBERG_RUEPPEL_H__ +#define BOTAN_NYBERG_RUEPPEL_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* Nyberg-Rueppel Public Key * +*************************************************/ +class BOTAN_DLL NR_PublicKey : public PK_Verifying_with_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + std::string algo_name() const { return "NR"; } + + SecureVector<byte> verify(const byte[], u32bit) const; + u32bit max_input_bits() const; + + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + + NR_PublicKey() {} + NR_PublicKey(const DL_Group&, const BigInt&); + protected: + NR_Core core; + private: + void X509_load_hook(); + }; + +/************************************************* +* Nyberg-Rueppel Private Key * +*************************************************/ +class BOTAN_DLL NR_PrivateKey : public NR_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + NR_PrivateKey() {} + + NR_PrivateKey(RandomNumberGenerator&, const DL_Group&, + const BigInt& = 0); + private: + void PKCS8_load_hook(RandomNumberGenerator&, bool = false); + }; + +} + +#endif diff --git a/src/rsa.cpp b/src/pk/rsa/rsa.cpp index 8faec9972..8faec9972 100644 --- a/src/rsa.cpp +++ b/src/pk/rsa/rsa.cpp diff --git a/src/pk/rsa/rsa.h b/src/pk/rsa/rsa.h new file mode 100644 index 000000000..445902a6f --- /dev/null +++ b/src/pk/rsa/rsa.h @@ -0,0 +1,63 @@ +/************************************************* +* RSA Header File * +* (C) 1999-2008 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_RSA_H__ +#define BOTAN_RSA_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/************************************************* +* RSA Public Key * +*************************************************/ +class BOTAN_DLL RSA_PublicKey : public PK_Encrypting_Key, + public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + std::string algo_name() const { return "RSA"; } + + SecureVector<byte> encrypt(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + SecureVector<byte> verify(const byte[], u32bit) const; + + RSA_PublicKey() {} + RSA_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + }; + +/************************************************* +* RSA Private Key * +*************************************************/ +class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey, + public PK_Decrypting_Key, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator&) const; + + SecureVector<byte> decrypt(const byte[], u32bit) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + RSA_PrivateKey() {} + + RSA_PrivateKey(RandomNumberGenerator&, + const BigInt& p, const BigInt& q, const BigInt& e, + const BigInt& d = 0, const BigInt& n = 0); + + RSA_PrivateKey(RandomNumberGenerator&, u32bit bits, u32bit = 65537); + private: + BigInt private_op(const byte[], u32bit) const; + }; + +} + +#endif diff --git a/src/rw.cpp b/src/pk/rw/rw.cpp index 39cbcdd6e..39cbcdd6e 100644 --- a/src/rw.cpp +++ b/src/pk/rw/rw.cpp diff --git a/src/pk/rw/rw.h b/src/pk/rw/rw.h new file mode 100644 index 000000000..d9f95eaa9 --- /dev/null +++ b/src/pk/rw/rw.h @@ -0,0 +1,54 @@ +/************************************************* +* Rabin-Williams Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_RW_H__ +#define BOTAN_RW_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/************************************************* +* Rabin-Williams Public Key * +*************************************************/ +class BOTAN_DLL RW_PublicKey : public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + std::string algo_name() const { return "RW"; } + + SecureVector<byte> verify(const byte[], u32bit) const; + + RW_PublicKey() {} + RW_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + }; + +/************************************************* +* Rabin-Williams Private Key * +*************************************************/ +class BOTAN_DLL RW_PrivateKey : public RW_PublicKey, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit, + RandomNumberGenerator& rng) const; + + bool check_key(RandomNumberGenerator& rng, bool) const; + + RW_PrivateKey() {} + + RW_PrivateKey(RandomNumberGenerator&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt& = 0, const BigInt& = 0); + + RW_PrivateKey(RandomNumberGenerator& rng, u32bit bits, u32bit = 2); + }; + +} + +#endif diff --git a/src/pk_algs.cpp b/src/pk_algs.cpp index 6a76b11c3..d400d5b74 100644 --- a/src/pk_algs.cpp +++ b/src/pk_algs.cpp @@ -4,12 +4,30 @@ *************************************************/ #include <botan/pk_algs.h> + +#ifdef BOTAN_HAS_RSA #include <botan/rsa.h> +#endif + +#ifdef BOTAN_HAS_DSA #include <botan/dsa.h> +#endif + +#ifdef BOTAN_HAS_DH #include <botan/dh.h> +#endif + +#ifdef BOTAN_HAS_NR #include <botan/nr.h> +#endif + +#ifdef BOTAN_HAS_RW #include <botan/rw.h> +#endif + +#ifdef BOTAN_HAS_ELGAMAL #include <botan/elgamal.h> +#endif namespace Botan { @@ -18,14 +36,32 @@ namespace Botan { *************************************************/ Public_Key* get_public_key(const std::string& alg_name) { - if(alg_name == "RSA") return new RSA_PublicKey; - else if(alg_name == "DSA") return new DSA_PublicKey; - else if(alg_name == "DH") return new DH_PublicKey; - else if(alg_name == "NR") return new NR_PublicKey; - else if(alg_name == "RW") return new RW_PublicKey; - else if(alg_name == "ELG") return new ElGamal_PublicKey; - else - return 0; +#if defined(BOTAN_HAS_RSA) + if(alg_name == "RSA") return new RSA_PublicKey; +#endif + +#if defined(BOTAN_HAS_DSA) + if(alg_name == "DSA") return new DSA_PublicKey; +#endif + +#if defined(BOTAN_HAS_DH) + if(alg_name == "DH") return new DH_PublicKey; +#endif + +#if defined(BOTAN_HAS_NR) + if(alg_name == "NR") return new NR_PublicKey; +#endif + +#if defined(BOTAN_HAS_RW) + if(alg_name == "RW") return new RW_PublicKey; +#endif + +#if defined(BOTAN_HAS_ELG) + if(alg_name == "ELG") return new ElGamal_PublicKey; +#endif + + + return 0; } /************************************************* @@ -33,14 +69,32 @@ Public_Key* get_public_key(const std::string& alg_name) *************************************************/ Private_Key* get_private_key(const std::string& alg_name) { - if(alg_name == "RSA") return new RSA_PrivateKey; - else if(alg_name == "DSA") return new DSA_PrivateKey; - else if(alg_name == "DH") return new DH_PrivateKey; - else if(alg_name == "NR") return new NR_PrivateKey; - else if(alg_name == "RW") return new RW_PrivateKey; - else if(alg_name == "ELG") return new ElGamal_PrivateKey; - else - return 0; +#if defined(BOTAN_HAS_RSA) + if(alg_name == "RSA") return new RSA_PrivateKey; +#endif + +#if defined(BOTAN_HAS_DSA) + if(alg_name == "DSA") return new DSA_PrivateKey; +#endif + +#if defined(BOTAN_HAS_DH) + if(alg_name == "DH") return new DH_PrivateKey; +#endif + +#if defined(BOTAN_HAS_NR) + if(alg_name == "NR") return new NR_PrivateKey; +#endif + +#if defined(BOTAN_HAS_RW) + if(alg_name == "RW") return new RW_PrivateKey; +#endif + +#if defined(BOTAN_HAS_ELG) + if(alg_name == "ELG") return new ElGamal_PrivateKey; +#endif + + + return 0; } } |