diff options
author | lloyd <[email protected]> | 2010-03-02 02:53:51 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-02 02:53:51 +0000 |
commit | 1fac460e94edf5c4dda296588a87476195f08c24 (patch) | |
tree | bd505c2e05ea3a0ffc3cf026322fdc29b3f3479d /src | |
parent | 9c2131c935f052f3c783e3fd35e13f2908d02a98 (diff) |
Remove ECDSA_Op and its attendant bits
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/def_engine/def_pk_ops.cpp | 16 | ||||
-rw-r--r-- | src/engine/def_engine/default_engine.h | 6 | ||||
-rw-r--r-- | src/engine/engine.h | 12 | ||||
-rw-r--r-- | src/libstate/pk_engine.cpp | 21 | ||||
-rw-r--r-- | src/libstate/pk_engine.h | 11 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.cpp | 75 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.h | 9 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_core.cpp | 58 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_core.h | 46 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_op.cpp | 74 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_op.h | 64 |
11 files changed, 45 insertions, 347 deletions
diff --git a/src/engine/def_engine/def_pk_ops.cpp b/src/engine/def_engine/def_pk_ops.cpp index 3ac608819..177040805 100644 --- a/src/engine/def_engine/def_pk_ops.cpp +++ b/src/engine/def_engine/def_pk_ops.cpp @@ -27,10 +27,6 @@ #include <botan/dh_op.h> #endif -#if defined(BOTAN_HAS_ECDSA) - #include <botan/ecdsa_op.h> -#endif - #if defined(BOTAN_HAS_ECKAEG) #include <botan/eckaeg_op.h> #endif @@ -94,18 +90,6 @@ DH_Operation* Default_Engine::dh_op(const DL_Group& group, } #endif -#if defined(BOTAN_HAS_ECDSA) -/* -* Acquire a ECDSA op -*/ -ECDSA_Operation* Default_Engine::ecdsa_op(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key) const - { - return new Default_ECDSA_Op(dom_pars, priv_key, pub_key); - } -#endif - #if defined(BOTAN_HAS_ECKAEG) /* * Acquire a ECKAEG op diff --git a/src/engine/def_engine/default_engine.h b/src/engine/def_engine/default_engine.h index 6846d3497..5fe4c373b 100644 --- a/src/engine/def_engine/default_engine.h +++ b/src/engine/def_engine/default_engine.h @@ -44,12 +44,6 @@ class Default_Engine : public Engine DH_Operation* dh_op(const DL_Group&, const BigInt&) const; #endif -#if defined(BOTAN_HAS_ECDSA) - virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&, - const BigInt&, - const PointGFp&) const; -#endif - #if defined(BOTAN_HAS_ECKAEG) virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&, const BigInt&, diff --git a/src/engine/engine.h b/src/engine/engine.h index 66a159e38..8c1b9dd5f 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -39,11 +39,6 @@ #include <botan/elg_op.h> #endif -#if defined(BOTAN_HAS_ECDSA) - #include <botan/ecdsa_op.h> - #include <botan/ec_dompar.h> -#endif - #if defined(BOTAN_HAS_ECKAEG) #include <botan/eckaeg_op.h> #include <botan/ec_dompar.h> @@ -120,13 +115,6 @@ class BOTAN_DLL Engine { return 0; } #endif -#if defined(BOTAN_HAS_ECDSA) - virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&, - const BigInt&, - const PointGFp&) const - { return 0; } -#endif - #if defined(BOTAN_HAS_ECKAEG) virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&, const BigInt&, diff --git a/src/libstate/pk_engine.cpp b/src/libstate/pk_engine.cpp index d6f4f7015..38fe17d73 100644 --- a/src/libstate/pk_engine.cpp +++ b/src/libstate/pk_engine.cpp @@ -110,27 +110,6 @@ DH_Operation* dh_op(const DL_Group& group, const BigInt& x) } #endif -#if defined(BOTAN_HAS_ECDSA) -/* -* Acquire an ECDSA op -*/ -ECDSA_Operation* ecdsa_op(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key) - { - Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); - - while(const Engine* engine = i.next()) - { - ECDSA_Operation* op = engine->ecdsa_op(dom_pars, priv_key, pub_key); - if(op) - return op; - } - - throw Lookup_Error("Engine_Core::ecdsa_op: Unable to find a working engine"); - } -#endif - #if defined(BOTAN_HAS_ECKAEG) /* * Acquire a ECKAEG op diff --git a/src/libstate/pk_engine.h b/src/libstate/pk_engine.h index 256a47c20..6c45c3fca 100644 --- a/src/libstate/pk_engine.h +++ b/src/libstate/pk_engine.h @@ -31,11 +31,6 @@ #include <botan/elg_op.h> #endif -#if defined(BOTAN_HAS_ECDSA) - #include <botan/ecdsa_op.h> - #include <botan/ec_dompar.h> -#endif - #if defined(BOTAN_HAS_ECKAEG) #include <botan/eckaeg_op.h> #include <botan/ec_dompar.h> @@ -76,12 +71,6 @@ ELG_Operation* elg_op(const DL_Group&, const BigInt&, const BigInt&); DH_Operation* dh_op(const DL_Group&, const BigInt&); #endif -#if defined(BOTAN_HAS_ECDSA) -ECDSA_Operation* ecdsa_op(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key); -#endif - #if defined(BOTAN_HAS_ECKAEG) ECKAEG_Operation* eckaeg_op(const EC_Domain_Params& dom_pars, const BigInt& priv_key, diff --git a/src/pubkey/ecdsa/ecdsa.cpp b/src/pubkey/ecdsa/ecdsa.cpp index 7afbf96af..f5ded5aa6 100644 --- a/src/pubkey/ecdsa/ecdsa.cpp +++ b/src/pubkey/ecdsa/ecdsa.cpp @@ -9,17 +9,21 @@ #include <botan/ecdsa.h> -#include <assert.h> - namespace Botan { +ECDSA_PublicKey::ECDSA_PublicKey(const EC_Domain_Params& dom_par, + const PointGFp& pub_point) + { + domain_encoding = EC_DOMPAR_ENC_EXPLICIT; + domain_params = dom_par; + public_key = pub_point; + } + ECDSA_PrivateKey::ECDSA_PrivateKey(RandomNumberGenerator& rng, const EC_Domain_Params& dom_pars) { domain_params = dom_pars; generate_private_key(rng); - - ecdsa_core = ECDSA_Core(domain(), private_value(), public_point()); } ECDSA_PrivateKey::ECDSA_PrivateKey(const EC_Domain_Params& dom_pars, @@ -38,36 +42,34 @@ ECDSA_PrivateKey::ECDSA_PrivateKey(const EC_Domain_Params& dom_pars, { throw Invalid_State("ECDSA key generation failed"); } - - ecdsa_core = ECDSA_Core(domain(), private_value(), public_point()); } bool ECDSA_PublicKey::verify(const byte msg[], u32bit msg_len, const byte sig[], u32bit sig_len) const { - return ecdsa_core.verify(msg, msg_len, sig, sig_len); - } + const BigInt& n = domain().get_order(); -ECDSA_PublicKey::ECDSA_PublicKey(const EC_Domain_Params& dom_par, - const PointGFp& pub_point) - { - domain_encoding = EC_DOMPAR_ENC_EXPLICIT; - domain_params = dom_par; - public_key = pub_point; + if(n == 0) + throw Invalid_State("ECDSA_PublicKey::verify: Not initialized"); - ecdsa_core = ECDSA_Core(domain(), 0, public_point()); - } + if(sig_len != n.bytes()*2) + return false; -void ECDSA_PublicKey::X509_load_hook() - { - EC_PublicKey::X509_load_hook(); - ecdsa_core = ECDSA_Core(domain(), 0, public_point()); - } + BigInt e(msg, msg_len); -void ECDSA_PrivateKey::PKCS8_load_hook(bool generated) - { - EC_PrivateKey::PKCS8_load_hook(generated); - ecdsa_core = ECDSA_Core(domain(), private_value(), public_point()); + BigInt r(sig, sig_len / 2); + BigInt s(sig + sig_len / 2, sig_len / 2); + + if(r < 0 || r >= n || s < 0 || s >= n) + return false; + + BigInt w = inverse_mod(s, n); + + PointGFp R = w * (e * domain().get_base_point() + r*public_point()); + if(R.is_zero()) + return false; + + return (R.get_affine_x() % n == r); } SecureVector<byte> ECDSA_PrivateKey::sign(const byte msg[], @@ -76,17 +78,30 @@ SecureVector<byte> ECDSA_PrivateKey::sign(const byte msg[], { const BigInt& n = domain().get_order(); - if(n == 0) - throw Invalid_State("ECDSA_PrivateKey: Not initialized"); - - assert(n.bits() >= 1); + if(n == 0 || private_value() == 0) + throw Invalid_State("ECDSA_PrivateKey::sign: Not initialized"); BigInt k; do k.randomize(rng, n.bits()-1); while(k >= n); - return ecdsa_core.sign(msg, msg_len, k); + BigInt e(msg, msg_len); + + PointGFp k_times_P = domain().get_base_point() * k; + BigInt r = k_times_P.get_affine_x() % n; + + if(r == 0) + throw Internal_Error("Default_ECDSA_Op::sign: r was zero"); + + BigInt k_inv = inverse_mod(k, n); + + BigInt s = (((r * private_value()) + e) * k_inv) % n; + + SecureVector<byte> output(2*n.bytes()); + r.binary_encode(output + (output.size() / 2 - r.bytes())); + s.binary_encode(output + (output.size() - s.bytes())); + return output; } } diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h index bc767d8ad..447bc3758 100644 --- a/src/pubkey/ecdsa/ecdsa.h +++ b/src/pubkey/ecdsa/ecdsa.h @@ -11,7 +11,6 @@ #define BOTAN_ECDSA_KEY_H__ #include <botan/ecc_key.h> -#include <botan/ecdsa_core.h> namespace Botan { @@ -64,11 +63,6 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, */ ECDSA_PublicKey(const EC_Domain_Params& dom_par, const PointGFp& public_point); // sets core - - protected: - void X509_load_hook(); - - ECDSA_Core ecdsa_core; }; /** @@ -108,9 +102,6 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, SecureVector<byte> sign(const byte message[], u32bit mess_len, RandomNumberGenerator& rng) const; - - private: - void PKCS8_load_hook(bool = false); }; } diff --git a/src/pubkey/ecdsa/ecdsa_core.cpp b/src/pubkey/ecdsa/ecdsa_core.cpp deleted file mode 100644 index d661963f2..000000000 --- a/src/pubkey/ecdsa/ecdsa_core.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* -* ECDSA Core -* (C) 1999-2010 Jack Lloyd -* (C) 2007 FlexSecure GmbH -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/ecdsa_core.h> -#include <botan/internal/pk_engine.h> - -namespace Botan { - -/* -* ECDSA Operation -*/ -bool ECDSA_Core::verify(const byte signature[], u32bit sig_len, - const byte message[], u32bit mess_len) const - { - if(op == 0) - throw Invalid_State("ECDSA_Core: uninitialized"); - - return op->verify(signature, sig_len, message, mess_len); - } - -SecureVector<byte> ECDSA_Core::sign(const byte message[], - u32bit mess_len, - const BigInt& k) const - { - if(op == 0) - throw Invalid_State("ECDSA_Core: uninitialized"); - - return op->sign(message, mess_len, k); - } - -ECDSA_Core& ECDSA_Core::operator=(const ECDSA_Core& core) - { - delete op; - if(core.op) - op = core.op->clone(); - return (*this); - } - -ECDSA_Core::ECDSA_Core(const ECDSA_Core& core) - { - op = 0; - if(core.op) - op = core.op->clone(); - } - -ECDSA_Core::ECDSA_Core(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key) - { - op = Engine_Core::ecdsa_op(dom_pars, priv_key, pub_key); - } - -} diff --git a/src/pubkey/ecdsa/ecdsa_core.h b/src/pubkey/ecdsa/ecdsa_core.h deleted file mode 100644 index c6583a86f..000000000 --- a/src/pubkey/ecdsa/ecdsa_core.h +++ /dev/null @@ -1,46 +0,0 @@ -/* -* ECDSA Core -* (C) 1999-2007 Jack Lloyd -* (C) 2007 FlexSecure GmbH -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_ECDSA_CORE_H__ -#define BOTAN_ECDSA_CORE_H__ - -#include <botan/ecdsa_op.h> -#include <botan/ec_dompar.h> - -namespace Botan { - -/* -* ECDSA Core -*/ -class BOTAN_DLL ECDSA_Core - { - public: - bool verify(const byte signature[], u32bit sig_len, - const byte message[], u32bit mess_len) const; - - SecureVector<byte> sign(const byte message[], u32bit mess_len, - const BigInt& k) const; - - ECDSA_Core& operator=(const ECDSA_Core&); - - ECDSA_Core() { op = 0; } - - ECDSA_Core(const ECDSA_Core&); - - ECDSA_Core(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key); - - ~ECDSA_Core() { delete op; } - private: - ECDSA_Operation* op; - }; - -} - -#endif diff --git a/src/pubkey/ecdsa/ecdsa_op.cpp b/src/pubkey/ecdsa/ecdsa_op.cpp deleted file mode 100644 index dd92ac5c0..000000000 --- a/src/pubkey/ecdsa/ecdsa_op.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* -* ECDSA Operation -* (C) 2007 FlexSecure GmbH -* 2008-2010 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/ecdsa_op.h> -#include <botan/numthry.h> - -namespace Botan { - -Default_ECDSA_Op::Default_ECDSA_Op(const EC_Domain_Params& domain, - const BigInt& priv, - const PointGFp& pub) : - dom_pars(domain), mod_n(dom_pars.get_order()), - pub_key(pub), priv_key(priv) - { - } - -bool Default_ECDSA_Op::verify(const byte msg[], u32bit msg_len, - const byte sig[], u32bit sig_len) const - { - const BigInt& n = dom_pars.get_order(); - - if(sig_len != n.bytes()*2) - return false; - - BigInt e(msg, msg_len); - - BigInt r(sig, sig_len / 2); - BigInt s(sig + sig_len / 2, sig_len / 2); - - if(r < 0 || r >= n || s < 0 || s >= n) - return false; - - BigInt w = inverse_mod(s, n); - - PointGFp R = w * (e * dom_pars.get_base_point() + r*pub_key); - if(R.is_zero()) - return false; - - return (mod_n.reduce(R.get_affine_x()) == r); - } - -SecureVector<byte> Default_ECDSA_Op::sign(const byte msg[], u32bit msg_len, - const BigInt& k) const - { - if(priv_key == 0) - throw Internal_Error("Default_ECDSA_Op::sign(): no private key"); - - const BigInt& n = dom_pars.get_order(); - - BigInt e(msg, msg_len); - - PointGFp k_times_P = dom_pars.get_base_point() * k; - BigInt r = mod_n.reduce(k_times_P.get_affine_x()); - - if(r == 0) - throw Internal_Error("Default_ECDSA_Op::sign: r was zero"); - - BigInt k_inv = inverse_mod(k, n); - - BigInt s = mod_n.reduce(mod_n.multiply(r, priv_key) + e); - s = mod_n.multiply(s, k_inv); - - SecureVector<byte> output(2*n.bytes()); - r.binary_encode(output + (output.size() / 2 - r.bytes())); - s.binary_encode(output + (output.size() - s.bytes())); - return output; - } - -} diff --git a/src/pubkey/ecdsa/ecdsa_op.h b/src/pubkey/ecdsa/ecdsa_op.h deleted file mode 100644 index 3a492ccf4..000000000 --- a/src/pubkey/ecdsa/ecdsa_op.h +++ /dev/null @@ -1,64 +0,0 @@ -/* -* ECDSA Operations -* (C) 1999-2008 Jack Lloyd -* (C) 2007 FlexSecure GmbH -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_ECDSA_OPERATIONS_H__ -#define BOTAN_ECDSA_OPERATIONS_H__ - -#include <botan/ec_dompar.h> -#include <botan/reducer.h> - -namespace Botan { - -/* -* ECDSA Operation -*/ -class BOTAN_DLL ECDSA_Operation - { - public: - virtual bool verify(const byte msg[], u32bit msg_len, - const byte sig[], u32bit sig_len) const = 0; - - virtual SecureVector<byte> sign(const byte msg[], u32bit msg_len, - const BigInt& k) const = 0; - - virtual ECDSA_Operation* clone() const = 0; - - virtual ~ECDSA_Operation() {} - }; - -/* -* Default ECDSA operation -*/ -class BOTAN_DLL Default_ECDSA_Op : public ECDSA_Operation - { - public: - bool verify(const byte sig[], u32bit sig_len, - const byte msg[], u32bit msg_len) const; - - SecureVector<byte> sign(const byte msg[], u32bit msg_len, - const BigInt& k) const; - - ECDSA_Operation* clone() const - { - return new Default_ECDSA_Op(*this); - } - - Default_ECDSA_Op(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key); - private: - EC_Domain_Params dom_pars; - Modular_Reducer mod_n; - - PointGFp pub_key; - BigInt priv_key; - }; - -} - -#endif |