diff options
author | lloyd <[email protected]> | 2008-10-11 23:44:16 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-11 23:44:16 +0000 |
commit | 053dfa09e95039022e3c4249655cbe5fe12db9c5 (patch) | |
tree | 887f5570708fca65b2d16fa850d7f14e5387aa21 /src/pubkey/ecdsa | |
parent | 1c45e7840fd7ec7d3d6bbacbb615a4809a84a0a1 (diff) |
Move ECDSA_Signature into CVC module. It is not used by ECDSA directly now.
Change several ECC functions to return const references instead of const values.
Diffstat (limited to 'src/pubkey/ecdsa')
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.cpp | 50 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.h | 36 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_sig.cpp | 72 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa_sig.h | 99 | ||||
-rw-r--r-- | src/pubkey/ecdsa/info.txt | 2 |
5 files changed, 57 insertions, 202 deletions
diff --git a/src/pubkey/ecdsa/ecdsa.cpp b/src/pubkey/ecdsa/ecdsa.cpp index b3a63c304..9fed9fe86 100644 --- a/src/pubkey/ecdsa/ecdsa.cpp +++ b/src/pubkey/ecdsa/ecdsa.cpp @@ -6,7 +6,6 @@ *************************************************/ #include <botan/ecdsa.h> -#include <botan/ecdsa_sig.h> #include <botan/numthry.h> #include <botan/util.h> #include <botan/der_enc.h> @@ -113,13 +112,25 @@ bool ECDSA_PublicKey::verify(const byte message[], u32bit sig_len) const { affirm_init(); - ECDSA_Signature sig; - std::auto_ptr<ECDSA_Signature_Decoder> dec(sig.x509_decoder()); - SecureVector<byte> sv_sig; - sv_sig.set ( signature, sig_len ); - dec->signature_bits ( sv_sig ); - SecureVector<byte> sv_plain_sig = sig.get_concatenation(); - return m_ecdsa_core.verify ( sv_plain_sig, sv_plain_sig.size(), message, mess_len ); + + BigInt r, s; + + BER_Decoder(signature, sig_len) + .start_cons(SEQUENCE) + .decode(r) + .decode(s) + .end_cons() + .verify_end(); + + u32bit enc_len = std::max(r.bytes(), s.bytes()); + + SecureVector<byte> sv_plain_sig; + + sv_plain_sig.append(BigInt::encode_1363(r, enc_len)); + sv_plain_sig.append(BigInt::encode_1363(s, enc_len)); + + return m_ecdsa_core.verify(sv_plain_sig, sv_plain_sig.size(), + message, mess_len); } ECDSA_PublicKey::ECDSA_PublicKey(const EC_Domain_Params& dom_par, @@ -201,11 +212,26 @@ SecureVector<byte> ECDSA_PrivateKey::sign(const byte message[], RandomNumberGenerator& rng) const { affirm_init(); + 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()); - return enc->signature_bits(); + + if(sv_sig.size() % 2 != 0) + throw Invalid_Argument("Erroneous length of signature"); + + u32bit rs_len = sv_sig.size() / 2; + SecureVector<byte> sv_r, sv_s; + sv_r.set(sv_sig.begin(), rs_len); + sv_s.set(&sv_sig[rs_len], rs_len); + + BigInt r = BigInt::decode(sv_r, sv_r.size()); + BigInt s = BigInt::decode(sv_s, sv_s.size()); + + return DER_Encoder() + .start_cons(SEQUENCE) + .encode(r) + .encode(s) + .end_cons() + .get_contents(); } } diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h index e0f0c766e..4e9634f05 100644 --- a/src/pubkey/ecdsa/ecdsa.h +++ b/src/pubkey/ecdsa/ecdsa.h @@ -25,10 +25,7 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, * Get this keys algorithm name. * @result this keys algorithm name ("ECDSA") */ - std::string algo_name() const - { - return "ECDSA"; - } + std::string algo_name() const { return "ECDSA"; } /** * Get the maximum number of bits allowed to be fed to this key. @@ -49,8 +46,8 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, const byte signature [], u32bit sig_len) const; /** - * Default constructor. Use this one if you want to later fill this object with data - * from an encoded key. + * Default constructor. Use this one if you want to later fill + * this object with data from an encoded key. */ ECDSA_PublicKey() {} @@ -62,9 +59,9 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, ECDSA_PublicKey(const EC_Domain_Params& dom_par, const PointGFp& public_point); // sets core - ECDSA_PublicKey const& operator= (ECDSA_PublicKey const& rhs); + ECDSA_PublicKey const& operator=(const ECDSA_PublicKey& rhs); - ECDSA_PublicKey(ECDSA_PublicKey const& other); + ECDSA_PublicKey(const ECDSA_PublicKey& other); /** * Set the domain parameters of this key. This function has to be @@ -76,17 +73,17 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey, * or if this key already has domain parameters set * and these are differing from those given as the parameter */ - void set_domain_parameters(EC_Domain_Params const& dom_pars); + void set_domain_parameters(const EC_Domain_Params& dom_pars); /** - * Make sure that the public point and domain parameters of this key are set. + * Ensure 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(ECDSA_PublicKey const& other); + virtual void set_all_values(const ECDSA_PublicKey& other); ECDSA_Core m_ecdsa_core; }; @@ -100,9 +97,10 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, { public: //ctors + /** - * Default constructor. Use this one if you want to later fill this object with data - * from an encoded key. + * Default constructor. Use this one if you want to later fill + * this object with data from an encoded key. */ ECDSA_PrivateKey() {} @@ -113,8 +111,8 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, ECDSA_PrivateKey(RandomNumberGenerator& rng, const EC_Domain_Params& domain); - ECDSA_PrivateKey(ECDSA_PrivateKey const& other); - ECDSA_PrivateKey const& operator= (ECDSA_PrivateKey const& rhs); + ECDSA_PrivateKey(const ECDSA_PrivateKey& other); + ECDSA_PrivateKey const& operator=(const ECDSA_PrivateKey& rhs); /** * Sign a message with this key. @@ -122,7 +120,10 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, * @param mess_len the length of the message byte array * @result the signature */ - SecureVector<byte> sign(const byte message[], u32bit mess_len, RandomNumberGenerator& rng) const; + + SecureVector<byte> sign(const byte message[], u32bit mess_len, + RandomNumberGenerator& rng) const; + /** * Make sure that the public key parts of this object are set * (calls EC_PublicKey::affirm_init()) as well as the private key @@ -130,8 +131,9 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, * @throw Invalid_State if the above conditions are not satisfied */ virtual void affirm_init() const; + protected: - virtual void set_all_values ( ECDSA_PrivateKey const& other ); + virtual void set_all_values(const ECDSA_PrivateKey& other); private: void PKCS8_load_hook(bool = false); }; diff --git a/src/pubkey/ecdsa/ecdsa_sig.cpp b/src/pubkey/ecdsa/ecdsa_sig.cpp deleted file mode 100644 index abe1c631b..000000000 --- a/src/pubkey/ecdsa/ecdsa_sig.cpp +++ /dev/null @@ -1,72 +0,0 @@ - -#include <botan/ecdsa_sig.h> -#include <memory> - -namespace Botan { - -ECDSA_Signature::ECDSA_Signature(const BigInt& r, const BigInt& s) - : m_r(r), - m_s(s) - {} - -ECDSA_Signature::ECDSA_Signature(ECDSA_Signature const& other) - : m_r(other.m_r), - m_s(other.m_s) - {} - -ECDSA_Signature const& ECDSA_Signature::operator=(ECDSA_Signature const& other) - { - m_r = other.m_r; - m_s = other.m_s; - return *this; - } - -bool operator== ( ECDSA_Signature const& lhs, ECDSA_Signature const& rhs ) - { - return (lhs.get_r() == rhs.get_r() && lhs.get_s() == rhs.get_s()); - } - -ECDSA_Signature_Decoder* ECDSA_Signature::x509_decoder() - { - return new ECDSA_Signature_Decoder(this); - } - -ECDSA_Signature_Encoder* ECDSA_Signature::x509_encoder() const - { - return new ECDSA_Signature_Encoder(this); - } -SecureVector<byte> const ECDSA_Signature::get_concatenation() const - { - u32bit enc_len = m_r > m_s ? m_r.bytes() : m_s.bytes(); // use the larger - SecureVector<byte> sv_r = BigInt::encode_1363 ( m_r, enc_len ); - SecureVector<byte> sv_s = BigInt::encode_1363 ( m_s, enc_len ); - SecureVector<byte> result(sv_r); - result.append(sv_s); - return result; - } - -ECDSA_Signature const decode_seq(MemoryRegion<byte> const& seq) - { - ECDSA_Signature sig; - std::auto_ptr<ECDSA_Signature_Decoder> dec(sig.x509_decoder()); - dec->signature_bits(seq); - return sig; - } - -ECDSA_Signature const decode_concatenation(MemoryRegion<byte> const& concatenation) - { - if(concatenation.size() % 2 != 0) - { - throw Invalid_Argument("Erroneous length of signature"); - } - u32bit rs_len = concatenation.size()/2; - SecureVector<byte> sv_r; - SecureVector<byte> sv_s; - sv_r.set(concatenation.begin(), rs_len); - sv_s.set(&concatenation[rs_len], rs_len); - BigInt r = BigInt::decode ( sv_r, sv_r.size()); - BigInt s = BigInt::decode (sv_s, sv_s.size()); - return ECDSA_Signature(r, s); - } - -} diff --git a/src/pubkey/ecdsa/ecdsa_sig.h b/src/pubkey/ecdsa/ecdsa_sig.h deleted file mode 100644 index 73e2f8599..000000000 --- a/src/pubkey/ecdsa/ecdsa_sig.h +++ /dev/null @@ -1,99 +0,0 @@ -/************************************************* -* ECDSA Header File * -* (C) 2007 Falko Strenzke, FlexSecure GmbH * -* (C) 2008 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_ECDSA_SIGNATURE_H__ -#define BOTAN_ECDSA_SIGNATURE_H__ - -#include <botan/bigint.h> -#include <botan/der_enc.h> -#include <botan/ber_dec.h> - -namespace Botan { - -class BOTAN_DLL ECDSA_Signature_Decoder; -class BOTAN_DLL ECDSA_Signature_Encoder; - -class BOTAN_DLL ECDSA_Signature - { - friend class ECDSA_Signature_Decoder; - friend class ECDSA_Signature_Encoder; - public: - ECDSA_Signature(const BigInt& r, const BigInt& s); - ECDSA_Signature() - {} - ; - ECDSA_Signature(ECDSA_Signature const& other); - ECDSA_Signature const& operator=(ECDSA_Signature const& other); - - BigInt const get_r() const - { - return m_r; - } - BigInt const get_s() const - { - return m_s; - } - /** - * return the r||s - */ - SecureVector<byte> const get_concatenation() const; - - - ECDSA_Signature_Encoder* x509_encoder() const; - ECDSA_Signature_Decoder* x509_decoder(); - private: - BigInt m_r; - BigInt m_s; - }; - -bool operator== ( ECDSA_Signature const& lhs, ECDSA_Signature const& rhs ); -inline bool operator!= ( ECDSA_Signature const& lhs, ECDSA_Signature const& rhs ) - { - return !operator== ( lhs, rhs ); - } - -class BOTAN_DLL ECDSA_Signature_Decoder - { - public: - void signature_bits(const MemoryRegion<byte>& bits) - { - BER_Decoder(bits) - .start_cons(SEQUENCE) - .decode(m_signature->m_r) - .decode(m_signature->m_s) - .verify_end() - .end_cons(); - } - ECDSA_Signature_Decoder(ECDSA_Signature* signature) : m_signature(signature) - {} - private: - ECDSA_Signature* m_signature; - }; - -class BOTAN_DLL ECDSA_Signature_Encoder - { - public: - MemoryVector<byte> signature_bits() const - { - return DER_Encoder() - .start_cons(SEQUENCE) - .encode(m_signature->m_r) - .encode(m_signature->m_s) - .end_cons() - .get_contents(); - } - ECDSA_Signature_Encoder(const ECDSA_Signature* signature) : m_signature(signature) - {} - private: - const ECDSA_Signature* m_signature; - }; - -ECDSA_Signature const decode_seq(MemoryRegion<byte> const& seq); -ECDSA_Signature const decode_concatenation(MemoryRegion<byte> const& concatenation); - -} - -#endif diff --git a/src/pubkey/ecdsa/info.txt b/src/pubkey/ecdsa/info.txt index 6e692bd5f..48e88bda9 100644 --- a/src/pubkey/ecdsa/info.txt +++ b/src/pubkey/ecdsa/info.txt @@ -21,6 +21,4 @@ ecdsa_core.cpp ecdsa_core.h ecdsa_op.cpp ecdsa_op.h -ecdsa_sig.cpp -ecdsa_sig.h </add> |