diff options
author | lloyd <[email protected]> | 2008-10-08 02:32:56 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-08 02:32:56 +0000 |
commit | 89fed41be3c9a77aff495a636d40bf67ac503fa9 (patch) | |
tree | 7ce90033aead61eec1f6b70eb79c47cdc2a43fe4 /src/pubkey/ecdsa | |
parent | dbec5c3bbbd53f1a208707300752e59213cf53c6 (diff) |
Split ecdsa module into ecc_key, ecdsa, eckaeg
Add actual implementations (from InSiTo) for ECDSA_Operation and
ECKAEG_Operation.
Diffstat (limited to 'src/pubkey/ecdsa')
-rw-r--r-- | src/pubkey/ecdsa/ecc_core.cpp | 92 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecc_key.cpp | 141 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecc_key.h | 160 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecc_op.h | 44 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.cpp | 99 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.h | 1 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_core.cpp | 53 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_core.h (renamed from src/pubkey/ecdsa/ecc_core.h) | 37 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_op.cpp | 136 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_op.h | 62 | ||||
-rw-r--r-- | src/pubkey/ecdsa/eckaeg.cpp | 135 | ||||
-rw-r--r-- | src/pubkey/ecdsa/eckaeg.h | 121 | ||||
-rw-r--r-- | src/pubkey/ecdsa/info.txt | 14 |
13 files changed, 292 insertions, 803 deletions
diff --git a/src/pubkey/ecdsa/ecc_core.cpp b/src/pubkey/ecdsa/ecc_core.cpp deleted file mode 100644 index f644e231c..000000000 --- a/src/pubkey/ecdsa/ecc_core.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/************************************************* -* ECDSA/ECKAEG Core Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/ecc_core.h> -#include <botan/numthry.h> -#include <botan/engine.h> -#include <botan/parsing.h> -#include <algorithm> - -namespace Botan { - -/************************************************* -* ECKAEG_Core Constructor * -*************************************************/ -ECKAEG_Core::ECKAEG_Core(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - const PointGFp& pub_key) - { - op = Engine_Core::eckaeg_op(dom_pars, priv_key, pub_key); - } - -/************************************************* -* ECKAEG_Core Copy Constructor * -*************************************************/ -ECKAEG_Core::ECKAEG_Core(const ECKAEG_Core& core) - { - op = 0; - if(core.op) - op = core.op->clone(); - blinder = core.blinder; - } - -/************************************************* -* ECKAEG_Core Assignment Operator * -*************************************************/ -ECKAEG_Core& ECKAEG_Core::operator=(const ECKAEG_Core& core) - { - delete op; - if(core.op) - op = core.op->clone(); - blinder = core.blinder; - return (*this); - } - -/************************************************* -* ECKAEG Operation * -*************************************************/ -SecureVector<byte> ECKAEG_Core::agree(const PointGFp& otherKey) const - { - //assert(op.get()); - return op->agree(otherKey); - } - -/************************************************* -* ECDSA Operation * -*************************************************/ -bool ECDSA_Core::verify(const byte signature[], u32bit sig_len, - const byte message[], u32bit mess_len) const - { - //assert(op.get()); - return op->verify(signature, sig_len, message, mess_len); - } - -SecureVector<byte> ECDSA_Core::sign(const byte message[], u32bit mess_len) const - { - //assert(op.get()); - return op->sign(message, mess_len); - } - -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(EC_Domain_Params const& dom_pars, const BigInt& priv_key, PointGFp const& pub_key) - { - op = Engine_Core::ecdsa_op(dom_pars, priv_key, pub_key); - } - -} diff --git a/src/pubkey/ecdsa/ecc_key.cpp b/src/pubkey/ecdsa/ecc_key.cpp deleted file mode 100644 index 36226b47b..000000000 --- a/src/pubkey/ecdsa/ecc_key.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/************************************************* -* ECC Key implemenation * -* (C) 2007 Manuel Hartl / FlexSecure GmbH * -* * -* Falko Strenzke * -* [email protected] * -*************************************************/ - -#include <botan/ecc_key.h> -#include <botan/x509_key.h> -#include <botan/numthry.h> -#include <botan/util.h> -#include <botan/der_enc.h> -#include <botan/ber_dec.h> -#include <botan/secmem.h> -#include <botan/point_gfp.h> - -namespace Botan { - -/************************************************* -* EC_PublicKey * -*************************************************/ -void EC_PublicKey::affirm_init() const // virtual - { - if ((mp_dom_pars.get() == 0) || (mp_public_point.get() == 0)) - { - throw Invalid_State("cannot use uninitialized EC_Key"); - } - } -EC_Domain_Params const EC_PublicKey::get_domain_parameters() const - { - if(!mp_dom_pars.get()) - { - throw Invalid_State("EC_PublicKey::get_domain_parameters(): ec domain parameters are not yet set"); - } - return *mp_dom_pars; - } -bool EC_PublicKey::domain_parameters_set() - { - if (mp_dom_pars.get()) - { - return true; - } - return false; - } -void EC_PublicKey::X509_load_hook() - { - try - { - // the base point is checked to be on curve already when decoding it - affirm_init(); - mp_public_point->check_invariants(); - } - catch ( Illegal_Point exc ) - { - throw Decoding_Error ( "decoded public point was found not to lie on curve" ); - } - } - - -X509_Encoder* EC_PublicKey::x509_encoder() const - { - class EC_Key_Encoder : public X509_Encoder - { - public: - AlgorithmIdentifier alg_id() const - { - key->affirm_init(); - SecureVector<byte> params = encode_der_ec_dompar ( * ( key->mp_dom_pars ), key->m_param_enc ); - return AlgorithmIdentifier ( key->get_oid(), - params ); - } - - MemoryVector<byte> key_bits() const - { - key->affirm_init(); - return EC2OSP ( * ( key->mp_public_point ), PointGFp::COMPRESSED ); - - } - - EC_Key_Encoder ( const EC_PublicKey* k ) : key ( k ) - {} - private: - const EC_PublicKey* key; - }; - - return new EC_Key_Encoder(this); - } - -X509_Decoder* EC_PublicKey::x509_decoder() - { - class EC_Key_Decoder : public X509_Decoder - { - public: - void alg_id ( const AlgorithmIdentifier& alg_id ) - { - key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) ); - } - - void key_bits ( const MemoryRegion<byte>& bits ) - { - key->mp_public_point.reset ( new PointGFp ( OS2ECP ( bits, key->mp_dom_pars->get_curve() ) ) ); - key->X509_load_hook(); - } - - EC_Key_Decoder ( EC_PublicKey* k ) : key ( k ) - {} - private: - EC_PublicKey* key; - }; - - return new EC_Key_Decoder(this); - } - -void EC_PublicKey::set_parameter_encoding ( EC_dompar_enc type ) - { - if ( ( type != ENC_EXPLICIT ) && ( type != ENC_IMPLICITCA ) && ( type != ENC_OID ) ) - { - throw Invalid_Argument ( "invalid encoding type for EC-key object specified" ); - } - affirm_init(); - if ( ( mp_dom_pars->get_oid() == "" ) && ( type == ENC_OID ) ) - { - throw Invalid_Argument ( "invalid encoding type ENC_OID specified for EC-key object whose corresponding domain parameters are without oid" ); - } - m_param_enc = type; - } - -/******************************** -* EC_PrivateKey * -********************************/ -void EC_PrivateKey::affirm_init() const // virtual - { - EC_PublicKey::affirm_init(); - if (m_private_value == 0) - { - throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized"); - } - } - -} diff --git a/src/pubkey/ecdsa/ecc_key.h b/src/pubkey/ecdsa/ecc_key.h deleted file mode 100644 index 1beba96c5..000000000 --- a/src/pubkey/ecdsa/ecc_key.h +++ /dev/null @@ -1,160 +0,0 @@ -/************************************************* -* ECDSA Header File * -* (C) 2007 Falko Strenzke, FlexSecure GmbH * -* Manuel hartl, FlexSecure GmbH * -* (C) 2008 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_ECC_KEY_H__ -#define BOTAN_ECC_KEY_H__ - -#include <botan/bigint.h> -#include <botan/curve_gfp.h> -#include <botan/pk_keys.h> -#include <botan/ec_dompar.h> -#include <botan/ecc_core.h> -#include <botan/x509_key.h> -#include <botan/pkcs8.h> - -namespace Botan { - -/** -* This class represents abstract EC Public Keys. When encoding a key -* via an encoder that can be accessed via the corresponding member -* functions, the key will decide upon its internally stored encoding -* information whether to encode itself with or without domain -* parameters, or using the domain parameter oid. Furthermore, a public -* key without domain parameters can be decoded. In that case, it -* cannot be used for verification until its domain parameters are set -* by calling the corresponding member function. -*/ -class EC_PublicKey : public virtual Public_Key - { - public: - - /** - * Tells whether this key knows his own domain parameters. - * @result true if the domain parameters are set, false otherwise - */ - bool domain_parameters_set(); - - /** - * Get the public point of this key. - * @throw Invalid_State is thrown if the - * domain parameters of this point are not set - * @result the public point of this key - */ - inline Botan::PointGFp get_public_point() const - { - if (!mp_public_point.get()) - { - throw Invalid_State("EC_PublicKey::get_public_point(): public point not set because ec domain parameters are not yet set"); - } - return *mp_public_point; - } - - /** - * Get the domain parameters of this key. - * @throw Invalid_State is thrown if the - * domain parameters of this point are not set - * @result the domain parameters of this key - */ - EC_Domain_Params const get_domain_parameters() const; - - /** - * Set the domain parameter encoding to be used when encoding this key. - * @param enc the encoding to use - */ - void set_parameter_encoding(EC_dompar_enc enc); - - /** - * Get the domain parameter encoding to be used when encoding this key. - * @result the encoding to use - */ - inline int get_parameter_encoding() const - { - return m_param_enc; - } - - //ctors - EC_PublicKey() - : m_param_enc(ENC_EXPLICIT) - { - //assert(mp_dom_pars.get() == 0); - //assert(mp_public_point.get() == 0); - } - - /** - * Get an x509_encoder that can be used to encode this key. - * @result an x509_encoder for this key - */ - X509_Encoder* x509_encoder() const; - - /** - * Get an x509_decoder that can be used to decode a stored key into - * this key. - * @result an x509_decoder for this key - */ - X509_Decoder* x509_decoder(); - - /** - * Make sure that the public point and domain parameters of this key are set. - * @throw Invalid_State if either of the two data members is not set - */ - virtual void affirm_init() const; - - virtual ~EC_PublicKey() {} - protected: - virtual void X509_load_hook(); - - SecureVector<byte> m_enc_public_point; // stores the public point - - std::auto_ptr<EC_Domain_Params> mp_dom_pars; - std::auto_ptr<Botan::PointGFp> mp_public_point; - EC_dompar_enc m_param_enc; - }; - -/** -* This abstract class represents general EC Private Keys -*/ -class EC_PrivateKey : public virtual EC_PublicKey, public virtual Private_Key - { - public: - - /** - * Get an PKCS#8 encoder that can be used to encoded this key. - * @result an PKCS#8 encoder for this key - */ - PKCS8_Encoder* pkcs8_encoder() const; - /** - * Get an PKCS#8 decoder that can be used to decoded a stored key into - * this key. - * @result an PKCS#8 decoder for this key - */ - PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&); - /** - * Get the private key value of this key object. - * @result the private key value of this key object - */ - inline BigInt const get_value() const - { - return m_private_value; - } - /** - * Make sure that the public key parts of this object are set - * (calls EC_PublicKey::affirm_init()) as well as the private key - * value. - * @throw Invalid_State if the above conditions are not satisfied - */ - virtual void affirm_init() const; - virtual ~EC_PrivateKey() - {} - protected: - virtual void PKCS8_load_hook(bool = false); - void generate_private_key(RandomNumberGenerator&); - BigInt m_private_value; - }; - -} - -#endif diff --git a/src/pubkey/ecdsa/ecc_op.h b/src/pubkey/ecdsa/ecc_op.h deleted file mode 100644 index 319e02da9..000000000 --- a/src/pubkey/ecdsa/ecc_op.h +++ /dev/null @@ -1,44 +0,0 @@ -/************************************************* -* ECDSA/ECKAEG Operations Header File * -* (C) 1999-2008 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_ECDSA_OPERATIONS_H__ -#define BOTAN_ECDSA_OPERATIONS_H__ - -#include <botan/bigint.h> -#include <botan/point_gfp.h> - -namespace Botan { - -/************************************************* -* ECDSA Operation * -*************************************************/ -class BOTAN_DLL ECDSA_Operation - { - public: - virtual bool verify(const byte sig[], u32bit sig_len, - const byte msg[], u32bit msg_len) const = 0; - - virtual SecureVector<byte> sign(const byte message[], - u32bit mess_len) const = 0; - - virtual ECDSA_Operation* clone() const = 0; - - virtual ~ECDSA_Operation() {} - }; - -/************************************************* -* ECKAEG Operation * -*************************************************/ -class BOTAN_DLL ECKAEG_Operation - { - public: - virtual SecureVector<byte> agree(const PointGFp&) const = 0; - virtual ECKAEG_Operation* clone() const = 0; - virtual ~ECKAEG_Operation() {} - }; - -} - -#endif diff --git a/src/pubkey/ecdsa/ecdsa.cpp b/src/pubkey/ecdsa/ecdsa.cpp index ae098cdc0..5cc226203 100644 --- a/src/pubkey/ecdsa/ecdsa.cpp +++ b/src/pubkey/ecdsa/ecdsa.cpp @@ -90,60 +90,6 @@ PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const return new EC_Key_Encoder(this); } -/** -* Return the PKCS #8 public key decoder -*/ -PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&) - { - class EC_Key_Decoder : public PKCS8_Decoder - { - public: - void alg_id ( const AlgorithmIdentifier& alg_id ) - { - key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) ); - } - - void key_bits ( const MemoryRegion<byte>& bits ) - { - u32bit version; - SecureVector<byte> octstr_secret; - BER_Decoder ( bits ) - .start_cons ( SEQUENCE ) - .decode ( version ) - .decode ( octstr_secret, OCTET_STRING ) - .verify_end() - .end_cons(); - key->m_private_value = BigInt::decode ( octstr_secret, octstr_secret.size() ); - if ( version != 1 ) - throw Decoding_Error ( "Wrong PKCS #1 key format version for EC key" ); - key->PKCS8_load_hook(); - } - - EC_Key_Decoder ( EC_PrivateKey* k ) : key ( k ) - {} - private: - EC_PrivateKey* key; - }; - - return new EC_Key_Decoder(this); - } - - -void EC_PrivateKey::PKCS8_load_hook ( bool ) - { - // we cannot use affirm_init() here because mp_public_point might still be null - if (mp_dom_pars.get() == 0 ) - { - throw Invalid_State("attempt to set public point for an uninitialized key"); - } - mp_public_point.reset ( new PointGFp ( m_private_value * mp_dom_pars->get_base_point() ) ); - mp_public_point->check_invariants(); - - } - - - - /************************************************* * ECDSA_PublicKey * *************************************************/ @@ -183,6 +129,7 @@ void ECDSA_PublicKey::set_domain_parameters(EC_Domain_Params const& dom_pars) { throw Invalid_State("EC_PublicKey::set_domain_parameters(): point does not lie on provided curve"); } + std::auto_ptr<EC_Domain_Params> p_tmp_pars(new EC_Domain_Params(dom_pars)); ECDSA_Core tmp_ecdsa_core( *p_tmp_pars, BigInt ( 0 ), tmp_pp ); mp_public_point.reset(new PointGFp(tmp_pp)); @@ -195,28 +142,31 @@ void ECDSA_PublicKey::set_all_values ( ECDSA_PublicKey const& other ) m_param_enc = other.m_param_enc; m_ecdsa_core = other.m_ecdsa_core; m_enc_public_point = other.m_enc_public_point; - if ( other.mp_dom_pars.get() ) - { + if(other.mp_dom_pars.get()) mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) ); - } - if ( other.mp_public_point.get() ) - { + + if(other.mp_public_point.get()) mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) ); - } } -ECDSA_PublicKey::ECDSA_PublicKey ( ECDSA_PublicKey const& other ) + +ECDSA_PublicKey::ECDSA_PublicKey(const ECDSA_PublicKey& other) : Public_Key(), EC_PublicKey(), PK_Verifying_wo_MR_Key() { set_all_values ( other ); } -ECDSA_PublicKey const& ECDSA_PublicKey::operator= ( ECDSA_PublicKey const& rhs ) + +const ECDSA_PublicKey& ECDSA_PublicKey::operator=(const ECDSA_PublicKey& rhs) { set_all_values ( rhs ); return *this; } -bool ECDSA_PublicKey::verify ( const byte message[], u32bit mess_len, const byte signature [], u32bit sig_len ) const + +bool ECDSA_PublicKey::verify(const byte message[], + u32bit mess_len, + const byte signature[], + u32bit sig_len) const { affirm_init(); ECDSA_Signature sig; @@ -227,19 +177,23 @@ bool ECDSA_PublicKey::verify ( const byte message[], u32bit mess_len, const byte SecureVector<byte> sv_plain_sig = sig.get_concatenation(); return m_ecdsa_core.verify ( sv_plain_sig, sv_plain_sig.size(), message, mess_len ); } -ECDSA_PublicKey::ECDSA_PublicKey ( EC_Domain_Params const& dom_par, PointGFp const& public_point ) + +ECDSA_PublicKey::ECDSA_PublicKey(const EC_Domain_Params& dom_par, + const PointGFp& public_point) { - mp_dom_pars = std::auto_ptr<EC_Domain_Params> ( new EC_Domain_Params ( dom_par ) ); - mp_public_point = std::auto_ptr<PointGFp> ( new PointGFp ( public_point ) ); + mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_par)); + mp_public_point = std::auto_ptr<PointGFp>(new PointGFp(public_point)); m_param_enc = ENC_EXPLICIT; - m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point ); + m_ecdsa_core = ECDSA_Core(*mp_dom_pars, BigInt(0), *mp_public_point); } + void ECDSA_PublicKey::X509_load_hook() { EC_PublicKey::X509_load_hook(); EC_PublicKey::affirm_init(); m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point ); } + u32bit ECDSA_PublicKey::max_input_bits() const { if(!mp_dom_pars.get()) @@ -249,11 +203,9 @@ u32bit ECDSA_PublicKey::max_input_bits() const return mp_dom_pars->get_order().bits(); } - /************************* * ECDSA_PrivateKey * *************************/ - void ECDSA_PrivateKey::affirm_init() const // virtual { EC_PrivateKey::affirm_init(); @@ -293,16 +245,19 @@ ECDSA_PrivateKey::ECDSA_PrivateKey(ECDSA_PrivateKey const& other) { set_all_values(other); } -ECDSA_PrivateKey const& ECDSA_PrivateKey::operator= (ECDSA_PrivateKey const& rhs) + +const ECDSA_PrivateKey& ECDSA_PrivateKey::operator=(const ECDSA_PrivateKey& rhs) { set_all_values(rhs); return *this; } -SecureVector<byte> ECDSA_PrivateKey::sign ( const byte message [], u32bit mess_len, RandomNumberGenerator&) const +SecureVector<byte> ECDSA_PrivateKey::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const { affirm_init(); - SecureVector<byte> sv_sig = m_ecdsa_core.sign ( message, mess_len ); + SecureVector<byte> sv_sig = m_ecdsa_core.sign(message, mess_len, rng); //code which der encodes the signature returned ECDSA_Signature sig = decode_concatenation( sv_sig ); std::auto_ptr<ECDSA_Signature_Encoder> enc(sig.x509_encoder()); diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h index 68d69b24a..4b7e7b5b3 100644 --- a/src/pubkey/ecdsa/ecdsa.h +++ b/src/pubkey/ecdsa/ecdsa.h @@ -9,6 +9,7 @@ #define BOTAN_ECDSA_KEY_H__ #include <botan/ecc_key.h> +#include <botan/ecdsa_core.h> namespace Botan { diff --git a/src/pubkey/ecdsa/ecdsa_core.cpp b/src/pubkey/ecdsa/ecdsa_core.cpp new file mode 100644 index 000000000..2a5ea29cb --- /dev/null +++ b/src/pubkey/ecdsa/ecdsa_core.cpp @@ -0,0 +1,53 @@ +/************************************************* +* ECDSA Core Source File * +* (C) 1999-2007 Jack Lloyd * +* (C) 2007 FlexSecure GmbH * +*************************************************/ + +#include <botan/ecdsa_core.h> +#include <botan/numthry.h> +#include <botan/engine.h> +#include <botan/parsing.h> +#include <algorithm> + +namespace Botan { + +/************************************************* +* ECDSA Operation * +*************************************************/ +bool ECDSA_Core::verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const + { + //assert(op.get()); + return op->verify(signature, sig_len, message, mess_len); + } + +SecureVector<byte> ECDSA_Core::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const + { + //assert(op.get()); + return op->sign(message, mess_len, rng); + } + +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(EC_Domain_Params const& dom_pars, const BigInt& priv_key, PointGFp const& pub_key) + { + op = Engine_Core::ecdsa_op(dom_pars, priv_key, pub_key); + } + +} diff --git a/src/pubkey/ecdsa/ecc_core.h b/src/pubkey/ecdsa/ecdsa_core.h index 46be43b8a..30668d858 100644 --- a/src/pubkey/ecdsa/ecc_core.h +++ b/src/pubkey/ecdsa/ecdsa_core.h @@ -1,13 +1,13 @@ /************************************************* -* ECC Core Header File * +* ECDSA Core Header File * * (C) 1999-2007 Jack Lloyd * * (C) 2007 FlexSecure GmbH * *************************************************/ -#ifndef BOTAN_ECC_CORE_H__ -#define BOTAN_ECC_CORE_H__ +#ifndef BOTAN_ECDSA_CORE_H__ +#define BOTAN_ECDSA_CORE_H__ -#include <botan/ecc_op.h> +#include <botan/ecdsa_op.h> #include <botan/blinding.h> #include <botan/ec_dompar.h> @@ -16,13 +16,14 @@ namespace Botan { /************************************************* * ECDSA Core * *************************************************/ -class 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; + SecureVector<byte> sign(const byte message[], u32bit mess_len, + RandomNumberGenerator& rng) const; ECDSA_Core& operator=(const ECDSA_Core&); @@ -39,30 +40,6 @@ class ECDSA_Core ECDSA_Operation* op; }; -/************************************************* -* ECKAEG Core * -*************************************************/ -class ECKAEG_Core - { - public: - SecureVector<byte> agree(const PointGFp&) const; - - ECKAEG_Core& operator=(const ECKAEG_Core&); - - ECKAEG_Core() { op = 0; } - - ECKAEG_Core(const ECKAEG_Core&); - - ECKAEG_Core(const EC_Domain_Params& dom_pars, - const BigInt& priv_key, - PointGFp const& pub_key); - - ~ECKAEG_Core() { delete op; } - private: - ECKAEG_Operation* op; - Blinder blinder; - }; - } #endif diff --git a/src/pubkey/ecdsa/ecdsa_op.cpp b/src/pubkey/ecdsa/ecdsa_op.cpp new file mode 100644 index 000000000..58f00cb4b --- /dev/null +++ b/src/pubkey/ecdsa/ecdsa_op.cpp @@ -0,0 +1,136 @@ +/************************************************* +* ECDSA Operation * +* (C) 2007 FlexSecure GmbH * +* 2008 Jack Lloyd * +*************************************************/ + +#include <botan/eng_def.h> + +namespace Botan { + +bool Default_ECDSA_Op::verify(const byte signature[], u32bit sig_len, + const byte message[], u32bit mess_len) const + { + if(sig_len % 2 != 0) + throw Invalid_Argument("Erroneous length of signature"); + + //NOTE: it is not checked whether the public point is set + if(m_dom_pars.get_curve().get_p() == 0) + throw Internal_Error("domain parameters not set"); + + BigInt e(message, mess_len); + + u32bit rs_len = sig_len/2; + SecureVector<byte> sv_r; + SecureVector<byte> sv_s; + sv_r.set(signature, rs_len); + sv_s.set(signature+rs_len, rs_len); + BigInt r = BigInt::decode ( sv_r, sv_r.size()); + BigInt s = BigInt::decode (sv_s, sv_s.size()); + + if(r < 0 || r >= m_dom_pars.get_order()) + throw Invalid_Argument("r in ECDSA signature has an illegal value"); + + if(s < 0 || s >= m_dom_pars.get_order()) + throw Invalid_Argument("s in ECDSA signature has an illegal value"); + + BigInt w = inverse_mod(s, m_dom_pars.get_order()); + + PointGFp R = w*(e*m_dom_pars.get_base_point() + r*m_pub_key); + if(R.is_zero()) + return false; + + BigInt x = R.get_affine_x().get_value(); + bool result = (x % m_dom_pars.get_order() == r); + return result; + } + +SecureVector<byte> Default_ECDSA_Op::sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator& rng) const + { + if(m_priv_key == 0) + throw Internal_Error("Default_ECDSA_Op::sign(): no private key"); + + if(m_dom_pars.get_curve().get_p() == 0) + throw Internal_Error("Default_ECDSA_Op::sign(): domain parameters not set"); + + BigInt e(message, mess_len); + + // generate k + BigInt k; + BigInt r(0); + const BigInt n(m_dom_pars.get_order()); + while(r == 0) + { + k = BigInt::random_integer(rng, 1, n); + + PointGFp k_times_P(m_dom_pars.get_base_point()); + k_times_P.mult_this_secure(k, n, n-1); + k_times_P.check_invariants(); + r = k_times_P.get_affine_x().get_value() % n; + } + BigInt k_inv = inverse_mod(k, n); + + // use randomization against attacks on s: + // a = k_inv * (r*(d + x) + e) mod n + // b = k_inv * r * x mod n + // s = a - b mod n + // where x is a random integer + +#ifdef CMS_RAND + BigInt x = Botan::random_integer(0, n); + BigInt s = m_priv_key + x; // obscure the secret from the beginning + // all following operations thus are randomized + s *= r; + s += e; + s *= k_inv; + s %= n; + + BigInt b = x; // again, start with the random number + b *= r; + b *= k_inv; + b %= n; + s -= b; // s = a - b + if(s <= 0) // s %= n + { + s += n; + } +#else // CMS_RAND + // no countermeasure here + BigInt s(r); + s *= m_priv_key; + s += e; + s *= k_inv; + s %= n; + +#endif // CMS_RAND + + SecureVector<byte> sv_r = BigInt::encode_1363 ( r, m_dom_pars.get_order().bytes() ); + SecureVector<byte> sv_s = BigInt::encode_1363 ( s, m_dom_pars.get_order().bytes() ); + + SecureVector<byte> result(sv_r); + result.append(sv_s); + return result; + } + +Default_ECDSA_Op::Default_ECDSA_Op(const EC_Domain_Params& dom_pars, const BigInt& priv_key, const PointGFp& pub_key) + : m_dom_pars(dom_pars), + m_pub_key(pub_key), + m_priv_key(priv_key) + { + + } + +/************************************************* +* 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); + } + +} + diff --git a/src/pubkey/ecdsa/ecdsa_op.h b/src/pubkey/ecdsa/ecdsa_op.h new file mode 100644 index 000000000..235824b89 --- /dev/null +++ b/src/pubkey/ecdsa/ecdsa_op.h @@ -0,0 +1,62 @@ +/************************************************* +* ECDSA Operations Header File * +* (C) 1999-2008 Jack Lloyd * +* (C) 2007 FlexSecure GmbH * +*************************************************/ + +#ifndef BOTAN_ECDSA_OPERATIONS_H__ +#define BOTAN_ECDSA_OPERATIONS_H__ + +#include <botan/ec_dompar.h> +#include <botan/rng.h> + +namespace Botan { + +/************************************************* +* ECDSA Operation * +*************************************************/ +class BOTAN_DLL ECDSA_Operation + { + public: + virtual bool verify(const byte sig[], u32bit sig_len, + const byte msg[], u32bit msg_len) const = 0; + + virtual SecureVector<byte> sign(const byte message[], + u32bit mess_len, + RandomNumberGenerator&) const = 0; + + virtual ECDSA_Operation* clone() const = 0; + + virtual ~ECDSA_Operation() {} + }; + + +/************************************************* +* Default ECDSA operation * +*************************************************/ +class Default_ECDSA_Op : public ECDSA_Operation + { + 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, + RandomNumberGenerator& rng) 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 m_dom_pars; + PointGFp m_pub_key; + BigInt m_priv_key; + }; + +} + +#endif diff --git a/src/pubkey/ecdsa/eckaeg.cpp b/src/pubkey/ecdsa/eckaeg.cpp deleted file mode 100644 index dcd30499a..000000000 --- a/src/pubkey/ecdsa/eckaeg.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/************************************************* -* ECKAEG implemenation * -* (C) 2007 Manuel Hartl, FlexSecure GmbH * -* 2007 Falko Strenzke, FlexSecure GmbH * -* 2008 Jack Lloyd * -*************************************************/ - -#include <botan/eckaeg.h> -#include <botan/numthry.h> -#include <botan/util.h> -#include <botan/der_enc.h> -#include <botan/ber_dec.h> -#include <botan/secmem.h> -#include <botan/point_gfp.h> - -namespace Botan { - -/********************************* -* ECKAEG_PublicKey * -*********************************/ - -void ECKAEG_PublicKey::affirm_init() const // virtual - { - EC_PublicKey::affirm_init(); - } - -void ECKAEG_PublicKey::set_all_values ( ECKAEG_PublicKey const& other ) - { - m_param_enc = other.m_param_enc; - m_eckaeg_core = other.m_eckaeg_core; - m_enc_public_point = other.m_enc_public_point; - if ( other.mp_dom_pars.get() ) - { - mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) ); - } - if ( other.mp_public_point.get() ) - { - mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) ); - } - } -ECKAEG_PublicKey::ECKAEG_PublicKey ( ECKAEG_PublicKey const& other ) - : Public_Key(), - EC_PublicKey() - { - set_all_values ( other ); - } -ECKAEG_PublicKey const& ECKAEG_PublicKey::operator= ( ECKAEG_PublicKey const& rhs ) - { - set_all_values ( rhs ); - return *this; - } - -void ECKAEG_PublicKey::X509_load_hook() - { - EC_PublicKey::X509_load_hook(); - EC_PublicKey::affirm_init(); - m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point ); - } -ECKAEG_PublicKey::ECKAEG_PublicKey ( EC_Domain_Params const& dom_par, PointGFp const& public_point ) - { - - mp_dom_pars = std::auto_ptr<EC_Domain_Params> ( new EC_Domain_Params ( dom_par ) ); - mp_public_point = std::auto_ptr<PointGFp> ( new PointGFp ( public_point ) ); - if(mp_public_point->get_curve() != mp_dom_pars->get_curve()) - { - throw Invalid_Argument("ECKAEG_PublicKey(): curve of arg. point and curve of arg. domain parameters are different"); - } - EC_PublicKey::affirm_init(); - m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point ); - } - - -/********************************* -* ECKAEG_PrivateKey * -*********************************/ -void ECKAEG_PrivateKey::affirm_init() const // virtual - { - EC_PrivateKey::affirm_init(); - } -void ECKAEG_PrivateKey::PKCS8_load_hook ( bool generated ) - { - EC_PrivateKey::PKCS8_load_hook ( generated ); - EC_PrivateKey::affirm_init(); - m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, m_private_value, *mp_public_point ); - } -void ECKAEG_PrivateKey::set_all_values ( ECKAEG_PrivateKey const& other ) - { - m_private_value = other.m_private_value; - m_param_enc = other.m_param_enc; - m_eckaeg_core = other.m_eckaeg_core; - m_enc_public_point = other.m_enc_public_point; - if ( other.mp_dom_pars.get() ) - { - mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) ); - } - if ( other.mp_public_point.get() ) - { - mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) ); - } - } - -ECKAEG_PrivateKey::ECKAEG_PrivateKey(ECKAEG_PrivateKey const& other) - : Public_Key(), - EC_PublicKey(), - Private_Key(), - ECKAEG_PublicKey(), - EC_PrivateKey(), - PK_Key_Agreement_Key() - - { - set_all_values(other); - } -ECKAEG_PrivateKey const& ECKAEG_PrivateKey::operator= (ECKAEG_PrivateKey const& rhs) - { - set_all_values(rhs); - return *this; - } - -/** -* Derive a key -*/ -SecureVector<byte> ECKAEG_PrivateKey::derive_key(const Public_Key& key) const - { - affirm_init(); - - const EC_PublicKey * p_ec_pk = dynamic_cast<const EC_PublicKey*>(&key); - if(!p_ec_pk) - { - throw Invalid_Argument("ECKAEG_PrivateKey::derive_key(): argument must be an EC_PublicKey"); - } - p_ec_pk->affirm_init(); - return m_eckaeg_core.agree ( p_ec_pk->get_public_point() ); - } - -} diff --git a/src/pubkey/ecdsa/eckaeg.h b/src/pubkey/ecdsa/eckaeg.h deleted file mode 100644 index 7ab286424..000000000 --- a/src/pubkey/ecdsa/eckaeg.h +++ /dev/null @@ -1,121 +0,0 @@ -/************************************************* -* ECKAEG Header File * -* (C) 2007 Falko Strenzke, FlexSecure GmbH * -* Manuel hartl, FlexSecure GmbH * -* (C) 2008 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_ECKAEG_KEY_H__ -#define BOTAN_ECKAEG_KEY_H__ - -#include <botan/ecc_key.h> - -namespace Botan { - -/** -* This class represents ECKAEG Public Keys. -*/ -class ECKAEG_PublicKey : public virtual EC_PublicKey - { - public: - /** - * Default constructor. Use this one if you want to later fill this object with data - * from an encoded key. - */ - ECKAEG_PublicKey() - {}; - /** - * Construct a public key from a given public point. - * @param dom_par the domain parameters associated with this key - * @param public_point the public point defining this key - */ - ECKAEG_PublicKey(EC_Domain_Params const& dom_par, Botan::PointGFp const& public_point); - - /** - * Get this keys algorithm name. - * @result this keys algorithm name - */ - std::string algo_name() const - { - return "ECKAEG"; - } - /** - * Get the maximum number of bits allowed to be fed to this key. - * This is the bitlength of the order of the base point. - * - * @result the maximum number of input bits - */ - u32bit max_input_bits() const - { - if (!mp_dom_pars.get()) - { - throw Invalid_State("ECKAEG_PublicKey::max_input_bits(): domain parameters not set"); - } - return mp_dom_pars->get_order().bits(); - } - ECKAEG_PublicKey(ECKAEG_PublicKey const& other); - ECKAEG_PublicKey const& operator= (ECKAEG_PublicKey const& rhs); - - - /** - * Make sure that the public point and domain parameters of this key are set. - * @throw Invalid_State if either of the two data members is not set - */ - virtual void affirm_init() const; - protected: - void X509_load_hook(); - virtual void set_all_values ( ECKAEG_PublicKey const& other ); - - ECKAEG_Core m_eckaeg_core; - }; - -/** -* This class represents ECKAEG Private Keys. -*/ -class ECKAEG_PrivateKey : public ECKAEG_PublicKey, public EC_PrivateKey, public PK_Key_Agreement_Key - { - public: - /** - * Generate a new private key - * @param the domain parameters to used for this key - */ - ECKAEG_PrivateKey(RandomNumberGenerator& rng, - EC_Domain_Params const& dom_pars) - { - mp_dom_pars = std::auto_ptr<EC_Domain_Params>(new EC_Domain_Params(dom_pars)); - generate_private_key(rng); - mp_public_point->check_invariants(); - m_eckaeg_core = ECKAEG_Core(*mp_dom_pars, m_private_value, *mp_public_point); - } - /** - * Default constructor. Use this one if you want to later fill this object with data - * from an encoded key. - */ - ECKAEG_PrivateKey() - {} - ECKAEG_PrivateKey(ECKAEG_PrivateKey const& other); - ECKAEG_PrivateKey const& operator= (ECKAEG_PrivateKey const& rhs); - - void PKCS8_load_hook(bool = false); - - /** - * Derive a shared key with the other partys public key. - * @param pub_key the other partys public key - */ - SecureVector<byte> derive_key(const Public_Key& pub_key) const; - - /** - * Make sure that the public key parts of this object are set - * (calls EC_PublicKey::affirm_init()) as well as the private key - * value. - * @throw Invalid_State if the above conditions are not satisfied - */ - virtual void affirm_init() const; - - protected: - virtual void set_all_values ( ECKAEG_PrivateKey const& other ); - }; - -} - -#endif diff --git a/src/pubkey/ecdsa/info.txt b/src/pubkey/ecdsa/info.txt index 5769abd03..6e692bd5f 100644 --- a/src/pubkey/ecdsa/info.txt +++ b/src/pubkey/ecdsa/info.txt @@ -1,4 +1,4 @@ -realname "ECDSA/ECKAEG" +realname "ECDSA" define ECDSA @@ -8,21 +8,19 @@ load_on auto asn1 bigint ec_dompar +ecc_key numbertheory gfpmath pubkey </requires> <add> -ecc_core.cpp -ecc_core.h -ecc_key.cpp -ecc_key.h -ecc_op.h ecdsa.cpp ecdsa.h +ecdsa_core.cpp +ecdsa_core.h +ecdsa_op.cpp +ecdsa_op.h ecdsa_sig.cpp ecdsa_sig.h -eckaeg.cpp -eckaeg.h </add> |