diff options
author | lloyd <[email protected]> | 2010-03-05 17:21:40 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-05 17:21:40 +0000 |
commit | df8c46ffb5554d8804287b340e06f79fbafe8d1d (patch) | |
tree | d0f047eee908f3b85ce2d5e6b9b19a9e55ea3757 /src | |
parent | 8df87a70435cae25e30f7045f7799537857e13d4 (diff) |
Add ops for ElGamal encryption and decryption.
Note: blinding is not currently being used for RSA, RW, DH or ElGamal,
which used to have them. This should be added back before release.
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/def_engine/def_pk_ops.cpp | 45 | ||||
-rw-r--r-- | src/engine/def_engine/default_engine.h | 11 | ||||
-rw-r--r-- | src/engine/engine.h | 23 | ||||
-rw-r--r-- | src/libstate/pk_engine.cpp | 19 | ||||
-rw-r--r-- | src/libstate/pk_engine.h | 8 | ||||
-rw-r--r-- | src/pubkey/elgamal/elg_core.cpp | 93 | ||||
-rw-r--r-- | src/pubkey/elgamal/elg_core.h | 44 | ||||
-rw-r--r-- | src/pubkey/elgamal/elg_op.cpp | 56 | ||||
-rw-r--r-- | src/pubkey/elgamal/elg_op.h | 52 | ||||
-rw-r--r-- | src/pubkey/elgamal/elgamal.cpp | 82 | ||||
-rw-r--r-- | src/pubkey/elgamal/elgamal.h | 40 | ||||
-rw-r--r-- | src/pubkey/pk_keys.h | 7 | ||||
-rw-r--r-- | src/pubkey/pk_ops.h | 13 | ||||
-rw-r--r-- | src/pubkey/pubkey.cpp | 80 | ||||
-rw-r--r-- | src/pubkey/pubkey.h | 34 |
15 files changed, 208 insertions, 399 deletions
diff --git a/src/engine/def_engine/def_pk_ops.cpp b/src/engine/def_engine/def_pk_ops.cpp index 7dd7e0219..e99908aa5 100644 --- a/src/engine/def_engine/def_pk_ops.cpp +++ b/src/engine/def_engine/def_pk_ops.cpp @@ -13,7 +13,6 @@ #endif #if defined(BOTAN_HAS_RW) - #include <botan/if_op.h> #include <botan/rw.h> #endif @@ -27,7 +26,6 @@ #if defined(BOTAN_HAS_ELGAMAL) #include <botan/elgamal.h> - #include <botan/elg_op.h> #endif #if defined(BOTAN_HAS_GOST_3410_2001) @@ -48,6 +46,38 @@ namespace Botan { +PK_Ops::Encryption* +Default_Engine::get_encryption_op(const Public_Key& key) const + { +#if 0 && defined(BOTAN_HAS_RSA) + if(const RSA_PublicKey* s = dynamic_cast<const RSA_PublicKey*>(&key)) + return new RSA_Encryption_Operation(*s); +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + if(const ElGamal_PublicKey* s = dynamic_cast<const ElGamal_PublicKey*>(&key)) + return new ElGamal_Encryption_Operation(*s); +#endif + + return 0; + } + +PK_Ops::Decryption* +Default_Engine::get_decryption_op(const Private_Key& key) const + { +#if 0 && defined(BOTAN_HAS_RSA) + if(const RSA_PrivateKey* s = dynamic_cast<const RSA_PrivateKey*>(&key)) + return new RSA_Decryption_Operation(*s); +#endif + +#if defined(BOTAN_HAS_ELGAMAL) + if(const ElGamal_PrivateKey* s = dynamic_cast<const ElGamal_PrivateKey*>(&key)) + return new ElGamal_Decryption_Operation(*s); +#endif + + return 0; + } + PK_Ops::Key_Agreement* Default_Engine::get_key_agreement_op(const Private_Key& key) const { @@ -151,15 +181,4 @@ IF_Operation* Default_Engine::if_op(const BigInt& e, const BigInt& n, } #endif -#if defined(BOTAN_HAS_ELGAMAL) -/* -* Acquire an ElGamal op -*/ -ELG_Operation* Default_Engine::elg_op(const DL_Group& group, const BigInt& y, - const BigInt& x) const - { - return new Default_ELG_Op(group, y, x); - } -#endif - } diff --git a/src/engine/def_engine/default_engine.h b/src/engine/def_engine/default_engine.h index b77f5ce91..9edd25d2b 100644 --- a/src/engine/def_engine/default_engine.h +++ b/src/engine/def_engine/default_engine.h @@ -28,18 +28,17 @@ class Default_Engine : public Engine PK_Ops::Verification* get_verify_op(const Public_Key& key) const; + PK_Ops::Encryption* get_encryption_op(const Public_Key& key) const; + + PK_Ops::Decryption* get_decryption_op(const Private_Key& key) const; + #if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&) const; #endif -#if defined(BOTAN_HAS_ELGAMAL) - ELG_Operation* elg_op(const DL_Group&, const BigInt&, - const BigInt&) const; -#endif - - Modular_Exponentiator* mod_exp(const BigInt&, + Modular_Exponentiator* mod_exp(const BigInt& n, Power_Mod::Usage_Hints) const; virtual bool can_add_algorithms() { return true; } diff --git a/src/engine/engine.h b/src/engine/engine.h index c45718662..a3f227412 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -25,10 +25,6 @@ #include <botan/if_op.h> #endif -#if defined(BOTAN_HAS_ELGAMAL) - #include <botan/elg_op.h> -#endif - namespace Botan { class Algorithm_Factory; @@ -76,8 +72,7 @@ class BOTAN_DLL Engine return 0; } - virtual PK_Ops::Signature* - get_signature_op(const Private_Key&) const + virtual PK_Ops::Signature* get_signature_op(const Private_Key&) const { return 0; } @@ -87,18 +82,22 @@ class BOTAN_DLL Engine return 0; } + virtual PK_Ops::Encryption* get_encryption_op(const Public_Key&) const + { + return 0; + } + + virtual PK_Ops::Decryption* get_decryption_op(const Private_Key&) const + { + return 0; + } + #if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY) virtual IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&) const { return 0; } #endif - -#if defined(BOTAN_HAS_ELGAMAL) - virtual ELG_Operation* elg_op(const DL_Group&, const BigInt&, - const BigInt&) const - { return 0; } -#endif }; } diff --git a/src/libstate/pk_engine.cpp b/src/libstate/pk_engine.cpp index d4187adc1..5b7c3b4e1 100644 --- a/src/libstate/pk_engine.cpp +++ b/src/libstate/pk_engine.cpp @@ -34,25 +34,6 @@ IF_Operation* if_op(const BigInt& e, const BigInt& n, const BigInt& d, } #endif -#if defined(BOTAN_HAS_ELGAMAL) -/* -* Acquire an ElGamal op -*/ -ELG_Operation* elg_op(const DL_Group& group, const BigInt& y, const BigInt& x) - { - Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); - - while(const Engine* engine = i.next()) - { - ELG_Operation* op = engine->elg_op(group, y, x); - if(op) - return op; - } - - throw Lookup_Error("Engine_Core::elg_op: Unable to find a working engine"); - } -#endif - /* * Acquire a modular exponentiator */ diff --git a/src/libstate/pk_engine.h b/src/libstate/pk_engine.h index 3bc1a90f1..8f38b2928 100644 --- a/src/libstate/pk_engine.h +++ b/src/libstate/pk_engine.h @@ -15,10 +15,6 @@ #include <botan/if_op.h> #endif -#if defined(BOTAN_HAS_ELGAMAL) - #include <botan/elg_op.h> -#endif - namespace Botan { class Algorithm_Factory; @@ -38,10 +34,6 @@ IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, const BigInt&, const BigInt&); #endif -#if defined(BOTAN_HAS_ELGAMAL) -ELG_Operation* elg_op(const DL_Group&, const BigInt&, const BigInt&); -#endif - } } diff --git a/src/pubkey/elgamal/elg_core.cpp b/src/pubkey/elgamal/elg_core.cpp deleted file mode 100644 index 0005c4ffc..000000000 --- a/src/pubkey/elgamal/elg_core.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* -* ElGamal Core -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/elg_core.h> -#include <botan/numthry.h> -#include <botan/internal/pk_engine.h> -#include <botan/parsing.h> -#include <algorithm> - -namespace Botan { - -/* -* ELG_Core Constructor -*/ -ELG_Core::ELG_Core(const DL_Group& group, const BigInt& y) - { - op = Engine_Core::elg_op(group, y, 0); - p_bytes = 0; - } - -/* -* ELG_Core Constructor -*/ -ELG_Core::ELG_Core(RandomNumberGenerator& rng, - const DL_Group& group, const BigInt& y, const BigInt& x) - { - const u32bit BLINDING_BITS = BOTAN_PRIVATE_KEY_OP_BLINDING_BITS; - - op = Engine_Core::elg_op(group, y, x); - - const BigInt& p = group.get_p(); - p_bytes = p.bytes(); - - if(BLINDING_BITS) - { - BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS)); - blinder = Blinder(k, power_mod(k, x, p), p); - } - } - -/* -* ELG_Core Copy Constructor -*/ -ELG_Core::ELG_Core(const ELG_Core& core) - { - op = 0; - if(core.op) - op = core.op->clone(); - blinder = core.blinder; - p_bytes = core.p_bytes; - } - -/* -* ELG_Core Assignment Operator -*/ -ELG_Core& ELG_Core::operator=(const ELG_Core& core) - { - delete op; - if(core.op) - op = core.op->clone(); - blinder = core.blinder; - p_bytes = core.p_bytes; - return (*this); - } - -/* -* ElGamal Encrypt Operation -*/ -SecureVector<byte> ELG_Core::encrypt(const byte in[], u32bit length, - const BigInt& k) const - { - return op->encrypt(in, length, k); - } - -/* -* ElGamal Decrypt Operation -*/ -SecureVector<byte> ELG_Core::decrypt(const byte in[], u32bit length) const - { - if(length != 2*p_bytes) - throw Invalid_Argument("ELG_Core::decrypt: Invalid message"); - - BigInt a(in, p_bytes); - BigInt b(in + p_bytes, p_bytes); - - return BigInt::encode(blinder.unblind(op->decrypt(blinder.blind(a), b))); - } - -} diff --git a/src/pubkey/elgamal/elg_core.h b/src/pubkey/elgamal/elg_core.h deleted file mode 100644 index a7768a6ae..000000000 --- a/src/pubkey/elgamal/elg_core.h +++ /dev/null @@ -1,44 +0,0 @@ -/* -* ElGamal Core -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_ELGAMAL_CORE_H__ -#define BOTAN_ELGAMAL_CORE_H__ - -#include <botan/elg_op.h> -#include <botan/blinding.h> -#include <botan/dl_group.h> - -namespace Botan { - -/* -* ElGamal Core -*/ -class BOTAN_DLL ELG_Core - { - public: - SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; - SecureVector<byte> decrypt(const byte[], u32bit) const; - - ELG_Core& operator=(const ELG_Core&); - - ELG_Core() { op = 0; } - ELG_Core(const ELG_Core&); - - ELG_Core(const DL_Group&, const BigInt&); - ELG_Core(RandomNumberGenerator&, const DL_Group&, - const BigInt&, const BigInt&); - - ~ELG_Core() { delete op; } - private: - ELG_Operation* op; - Blinder blinder; - u32bit p_bytes; - }; - -} - -#endif diff --git a/src/pubkey/elgamal/elg_op.cpp b/src/pubkey/elgamal/elg_op.cpp deleted file mode 100644 index 1e476ab7a..000000000 --- a/src/pubkey/elgamal/elg_op.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* -* ElGamal Operations -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/elg_op.h> - -namespace Botan { - -/* -* Default_ELG_Op Constructor -*/ -Default_ELG_Op::Default_ELG_Op(const DL_Group& group, const BigInt& y, - const BigInt& x) : p(group.get_p()) - { - powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), p); - powermod_y_p = Fixed_Base_Power_Mod(y, p); - mod_p = Modular_Reducer(p); - - if(x != 0) - powermod_x_p = Fixed_Exponent_Power_Mod(x, p); - } - -/* -* Default ElGamal Encrypt Operation -*/ -SecureVector<byte> Default_ELG_Op::encrypt(const byte in[], u32bit length, - const BigInt& k) const - { - BigInt m(in, length); - if(m >= p) - throw Invalid_Argument("Default_ELG_Op::encrypt: Input is too large"); - - BigInt a = powermod_g_p(k); - BigInt b = mod_p.multiply(m, powermod_y_p(k)); - - SecureVector<byte> output(2*p.bytes()); - a.binary_encode(output + (p.bytes() - a.bytes())); - b.binary_encode(output + output.size() / 2 + (p.bytes() - b.bytes())); - return output; - } - -/* -* Default ElGamal Decrypt Operation -*/ -BigInt Default_ELG_Op::decrypt(const BigInt& a, const BigInt& b) const - { - if(a >= p || b >= p) - throw Invalid_Argument("Default_ELG_Op: Invalid message"); - - return mod_p.multiply(b, inverse_mod(powermod_x_p(a), p)); - } - -} diff --git a/src/pubkey/elgamal/elg_op.h b/src/pubkey/elgamal/elg_op.h deleted file mode 100644 index 39ed897f4..000000000 --- a/src/pubkey/elgamal/elg_op.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -* ElGamal Operations -* (C) 1999-2008 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_ELGAMAL_OPS_H__ -#define BOTAN_ELGAMAL_OPS_H__ - -#include <botan/pow_mod.h> -#include <botan/numthry.h> -#include <botan/reducer.h> -#include <botan/dl_group.h> - -namespace Botan { - -/* -* ElGamal Operation -*/ -class BOTAN_DLL ELG_Operation - { - public: - virtual SecureVector<byte> encrypt(const byte[], u32bit, - const BigInt&) const = 0; - virtual BigInt decrypt(const BigInt&, const BigInt&) const = 0; - virtual ELG_Operation* clone() const = 0; - virtual ~ELG_Operation() {} - }; - -/* -* Botan's Default ElGamal Operation -*/ -class BOTAN_DLL Default_ELG_Op : public ELG_Operation - { - public: - SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; - BigInt decrypt(const BigInt&, const BigInt&) const; - - ELG_Operation* clone() const { return new Default_ELG_Op(*this); } - - Default_ELG_Op(const DL_Group&, const BigInt&, const BigInt&); - private: - const BigInt p; - Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; - Fixed_Exponent_Power_Mod powermod_x_p; - Modular_Reducer mod_p; - }; - -} - -#endif diff --git a/src/pubkey/elgamal/elgamal.cpp b/src/pubkey/elgamal/elgamal.cpp index 6fe28a177..2abd769e5 100644 --- a/src/pubkey/elgamal/elgamal.cpp +++ b/src/pubkey/elgamal/elgamal.cpp @@ -20,18 +20,6 @@ ElGamal_PublicKey::ElGamal_PublicKey(const DL_Group& grp, const BigInt& y1) { group = grp; y = y1; - core = ELG_Core(group, y); - } - -/* -* ElGamal Encryption Function -*/ -SecureVector<byte> -ElGamal_PublicKey::encrypt(const byte in[], u32bit length, - RandomNumberGenerator& rng) const - { - BigInt k(rng, 2 * dl_work_factor(group_p().bits())); - return core.encrypt(in, length, k); } /* @@ -49,8 +37,6 @@ ElGamal_PrivateKey::ElGamal_PrivateKey(RandomNumberGenerator& rng, y = power_mod(group_g(), x, group_p()); - core = ELG_Core(rng, group, y, x); - if(x_arg == 0) gen_check(rng); else @@ -63,20 +49,10 @@ ElGamal_PrivateKey::ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id, DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42) { y = power_mod(group_g(), x, group_p()); - core = ELG_Core(rng, group, y, x); load_check(rng); } /* -* ElGamal Decryption Function -*/ -SecureVector<byte> ElGamal_PrivateKey::decrypt(const byte in[], - u32bit length) const - { - return core.decrypt(in, length); - } - -/* * Check Private ElGamal Parameters */ bool ElGamal_PrivateKey::check_key(RandomNumberGenerator& rng, @@ -103,4 +79,62 @@ bool ElGamal_PrivateKey::check_key(RandomNumberGenerator& rng, return true; } +ElGamal_Encryption_Operation::ElGamal_Encryption_Operation(const ElGamal_PublicKey& key) + { + const BigInt& p = key.group_p(); + + powermod_g_p = Fixed_Base_Power_Mod(key.group_g(), p); + powermod_y_p = Fixed_Base_Power_Mod(key.get_y(), p); + mod_p = Modular_Reducer(p); + } + +SecureVector<byte> +ElGamal_Encryption_Operation::encrypt(const byte msg[], u32bit msg_len, + RandomNumberGenerator& rng) const + { + const BigInt& p = mod_p.get_modulus(); + + BigInt m(msg, msg_len); + + if(m >= p) + throw Invalid_Argument("ElGamal encryption: Input is too large"); + + BigInt k(rng, 2 * dl_work_factor(p.bits())); + + BigInt a = powermod_g_p(k); + BigInt b = mod_p.multiply(m, powermod_y_p(k)); + + SecureVector<byte> output(2*p.bytes()); + a.binary_encode(output + (p.bytes() - a.bytes())); + b.binary_encode(output + output.size() / 2 + (p.bytes() - b.bytes())); + return output; + } + +ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key) + { + const BigInt& p = key.group_p(); + + powermod_x_p = Fixed_Exponent_Power_Mod(key.get_x(), p); + mod_p = Modular_Reducer(p); + } + +SecureVector<byte> +ElGamal_Decryption_Operation::decrypt(const byte msg[], u32bit msg_len) const + { + const BigInt& p = mod_p.get_modulus(); + + const u32bit p_bytes = p.bytes(); + + if(msg_len != 2 * p_bytes) + throw Invalid_Argument("ElGamal decryption: Invalid message"); + + BigInt a(msg, p_bytes); + BigInt b(msg + p_bytes, p_bytes); + + if(a >= p || b >= p) + throw Invalid_Argument("ElGamal decryption: Invalid message"); + + return BigInt::encode(mod_p.multiply(b, inverse_mod(powermod_x_p(a), p))); + } + } diff --git a/src/pubkey/elgamal/elgamal.h b/src/pubkey/elgamal/elgamal.h index 3ae8f3a6b..238f286e7 100644 --- a/src/pubkey/elgamal/elgamal.h +++ b/src/pubkey/elgamal/elgamal.h @@ -9,7 +9,9 @@ #define BOTAN_ELGAMAL_H__ #include <botan/dl_algo.h> -#include <botan/elg_core.h> +#include <botan/numthry.h> +#include <botan/reducer.h> +#include <botan/pk_ops.h> namespace Botan { @@ -25,18 +27,14 @@ class BOTAN_DLL ElGamal_PublicKey : public PK_Encrypting_Key, u32bit max_input_bits() const { return (group_p().bits() - 1); } - SecureVector<byte> encrypt(const byte msg[], u32bit msg_len, - RandomNumberGenerator& rng) const; - ElGamal_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) : DL_Scheme_PublicKey(alg_id, key_bits, DL_Group::ANSI_X9_42) - { core = ELG_Core(group, y); } + {} ElGamal_PublicKey(const DL_Group& group, const BigInt& y); protected: ElGamal_PublicKey() {} - ELG_Core core; }; /* @@ -47,8 +45,6 @@ class BOTAN_DLL ElGamal_PrivateKey : public ElGamal_PublicKey, public virtual DL_Scheme_PrivateKey { public: - SecureVector<byte> decrypt(const byte msg[], u32bit msg_len) const; - bool check_key(RandomNumberGenerator& rng, bool) const; ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id, @@ -60,6 +56,34 @@ class BOTAN_DLL ElGamal_PrivateKey : public ElGamal_PublicKey, const BigInt& priv_key = 0); }; +class BOTAN_DLL ElGamal_Encryption_Operation : public PK_Ops::Encryption + { + public: + u32bit max_input_bits() const { return mod_p.get_modulus().bits() - 1; } + + ElGamal_Encryption_Operation(const ElGamal_PublicKey& key); + + SecureVector<byte> encrypt(const byte msg[], u32bit msg_len, + RandomNumberGenerator& rng) const; + + private: + Fixed_Base_Power_Mod powermod_g_p, powermod_y_p; + Modular_Reducer mod_p; + }; + +class BOTAN_DLL ElGamal_Decryption_Operation : public PK_Ops::Decryption + { + public: + u32bit max_input_bits() const { return mod_p.get_modulus().bits() - 1; } + + ElGamal_Decryption_Operation(const ElGamal_PrivateKey& key); + + SecureVector<byte> decrypt(const byte msg[], u32bit msg_len) const; + private: + Fixed_Exponent_Power_Mod powermod_x_p; + Modular_Reducer mod_p; + }; + } #endif diff --git a/src/pubkey/pk_keys.h b/src/pubkey/pk_keys.h index f997a29e7..74e36c638 100644 --- a/src/pubkey/pk_keys.h +++ b/src/pubkey/pk_keys.h @@ -104,10 +104,6 @@ class BOTAN_DLL Private_Key : public virtual Public_Key */ class BOTAN_DLL PK_Encrypting_Key : public virtual Public_Key { - public: - virtual SecureVector<byte> encrypt(const byte[], u32bit, - RandomNumberGenerator&) const = 0; - virtual ~PK_Encrypting_Key() {} }; /** @@ -115,9 +111,6 @@ class BOTAN_DLL PK_Encrypting_Key : public virtual Public_Key */ class BOTAN_DLL PK_Decrypting_Key : public virtual Private_Key { - public: - virtual SecureVector<byte> decrypt(const byte[], u32bit) const = 0; - virtual ~PK_Decrypting_Key() {} }; /** diff --git a/src/pubkey/pk_ops.h b/src/pubkey/pk_ops.h index c749bc8ea..3f04b52dc 100644 --- a/src/pubkey/pk_ops.h +++ b/src/pubkey/pk_ops.h @@ -18,12 +18,25 @@ namespace PK_Ops { class BOTAN_DLL Encryption { public: + virtual u32bit max_input_bits() const = 0; + virtual SecureVector<byte> encrypt(const byte msg[], u32bit msg_len, RandomNumberGenerator& rng) const = 0; virtual ~Encryption() {} }; +class BOTAN_DLL Decryption + { + public: + virtual u32bit max_input_bits() const = 0; + + virtual SecureVector<byte> decrypt(const byte msg[], + u32bit msg_len) const = 0; + + virtual ~Decryption() {} + }; + class BOTAN_DLL Signature { public: diff --git a/src/pubkey/pubkey.cpp b/src/pubkey/pubkey.cpp index 64e080de3..7bff1a500 100644 --- a/src/pubkey/pubkey.cpp +++ b/src/pubkey/pubkey.cpp @@ -18,46 +18,24 @@ namespace Botan { /* -* Encrypt a message -*/ -SecureVector<byte> PK_Encryptor::encrypt(const byte in[], u32bit len, - RandomNumberGenerator& rng) const - { - return enc(in, len, rng); - } - -/* -* Encrypt a message -*/ -SecureVector<byte> PK_Encryptor::encrypt(const MemoryRegion<byte>& in, - RandomNumberGenerator& rng) const - { - return enc(in.begin(), in.size(), rng); - } - -/* -* Decrypt a message -*/ -SecureVector<byte> PK_Decryptor::decrypt(const byte in[], u32bit len) const - { - return dec(in, len); - } - -/* -* Decrypt a message -*/ -SecureVector<byte> PK_Decryptor::decrypt(const MemoryRegion<byte>& in) const - { - return dec(in.begin(), in.size()); - } - -/* * PK_Encryptor_MR_with_EME Constructor */ -PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& k, +PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const Public_Key& key, EME* eme_obj) : - key(k), encoder(eme_obj) + encoder(eme_obj) { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + op = engine->get_encryption_op(key); + if(op) + break; + } + + if(op == 0) + throw Lookup_Error("PK_Encryptor_MR_with_EME: No working engine for " + + key.algo_name()); } /* @@ -70,14 +48,14 @@ PK_Encryptor_MR_with_EME::enc(const byte msg[], { SecureVector<byte> message; if(encoder) - message = encoder->encode(msg, length, key.max_input_bits(), rng); + message = encoder->encode(msg, length, op->max_input_bits(), rng); else message.set(msg, length); - if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits()) + if(8*(message.size() - 1) + high_bit(message[0]) > op->max_input_bits()) throw Invalid_Argument("PK_Encryptor_MR_with_EME: Input is too large"); - return key.encrypt(message, message.size(), rng); + return op->encrypt(message, message.size(), rng); } /* @@ -86,18 +64,30 @@ PK_Encryptor_MR_with_EME::enc(const byte msg[], u32bit PK_Encryptor_MR_with_EME::maximum_input_size() const { if(!encoder) - return (key.max_input_bits() / 8); + return (op->max_input_bits() / 8); else - return encoder->maximum_input_size(key.max_input_bits()); + return encoder->maximum_input_size(op->max_input_bits()); } /* * PK_Decryptor_MR_with_EME Constructor */ -PK_Decryptor_MR_with_EME::PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& k, +PK_Decryptor_MR_with_EME::PK_Decryptor_MR_with_EME(const Private_Key& key, EME* eme_obj) : - key(k), encoder(eme_obj) + encoder(eme_obj) { + Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); + + while(const Engine* engine = i.next()) + { + op = engine->get_decryption_op(key); + if(op) + break; + } + + if(op == 0) + throw Lookup_Error("PK_Decryptor_MR_with_EME: No working engine for " + + key.algo_name()); } /* @@ -107,9 +97,9 @@ SecureVector<byte> PK_Decryptor_MR_with_EME::dec(const byte msg[], u32bit length) const { try { - SecureVector<byte> decrypted = key.decrypt(msg, length); + SecureVector<byte> decrypted = op->decrypt(msg, length); if(encoder) - return encoder->decode(decrypted, key.max_input_bits()); + return encoder->decode(decrypted, op->max_input_bits()); else return decrypted; } diff --git a/src/pubkey/pubkey.h b/src/pubkey/pubkey.h index f63da2b3e..127500dd6 100644 --- a/src/pubkey/pubkey.h +++ b/src/pubkey/pubkey.h @@ -38,7 +38,10 @@ class BOTAN_DLL PK_Encryptor * @return the encrypted message */ SecureVector<byte> encrypt(const byte in[], u32bit length, - RandomNumberGenerator& rng) const; + RandomNumberGenerator& rng) const + { + return enc(in, length, rng); + } /** * Encrypt a message. @@ -47,7 +50,10 @@ class BOTAN_DLL PK_Encryptor * @return the encrypted message */ SecureVector<byte> encrypt(const MemoryRegion<byte>& in, - RandomNumberGenerator& rng) const; + RandomNumberGenerator& rng) const + { + return enc(&in[0], in.size(), rng); + } /** * Return the maximum allowed message size in bytes. @@ -73,14 +79,20 @@ class BOTAN_DLL PK_Decryptor * @param length the length of the above byte array * @return the decrypted message */ - SecureVector<byte> decrypt(const byte in[], u32bit length) const; + SecureVector<byte> decrypt(const byte in[], u32bit length) const + { + return dec(in, length); + } /** * Decrypt a ciphertext. * @param in the ciphertext * @return the decrypted message */ - SecureVector<byte> decrypt(const MemoryRegion<byte>& in) const; + SecureVector<byte> decrypt(const MemoryRegion<byte>& in) const + { + return dec(&in[0], in.size()); + } virtual ~PK_Decryptor() {} private: @@ -364,10 +376,9 @@ class BOTAN_DLL PK_Encryptor_MR_with_EME : public PK_Encryptor * @param key the key to use inside the decryptor * @param eme the EME to use */ - PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& key, - EME* eme); + PK_Encryptor_MR_with_EME(const Public_Key& key, EME* eme = 0); - ~PK_Encryptor_MR_with_EME() { delete encoder; } + ~PK_Encryptor_MR_with_EME() { delete op; delete encoder; } private: PK_Encryptor_MR_with_EME(const PK_Encryptor_MR_with_EME&); PK_Encryptor_MR_with_EME& operator=(const PK_Encryptor_MR_with_EME&); @@ -375,7 +386,7 @@ class BOTAN_DLL PK_Encryptor_MR_with_EME : public PK_Encryptor SecureVector<byte> enc(const byte[], u32bit, RandomNumberGenerator& rng) const; - const PK_Encrypting_Key& key; + const PK_Ops::Encryption* op; const EME* encoder; }; @@ -390,17 +401,16 @@ class BOTAN_DLL PK_Decryptor_MR_with_EME : public PK_Decryptor * @param key the key to use inside the encryptor * @param eme the EME to use */ - PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& key, - EME* eme); + PK_Decryptor_MR_with_EME(const Private_Key& key, EME* eme = 0); - ~PK_Decryptor_MR_with_EME() { delete encoder; } + ~PK_Decryptor_MR_with_EME() { delete op; delete encoder; } private: PK_Decryptor_MR_with_EME(const PK_Decryptor_MR_with_EME&); PK_Decryptor_MR_with_EME& operator=(const PK_Decryptor_MR_with_EME&); SecureVector<byte> dec(const byte[], u32bit) const; - const PK_Decrypting_Key& key; + const PK_Ops::Decryption* op; const EME* encoder; }; |