aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/ecc_key
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-11 23:44:16 +0000
committerlloyd <[email protected]>2008-10-11 23:44:16 +0000
commit053dfa09e95039022e3c4249655cbe5fe12db9c5 (patch)
tree887f5570708fca65b2d16fa850d7f14e5387aa21 /src/pubkey/ecc_key
parent1c45e7840fd7ec7d3d6bbacbb615a4809a84a0a1 (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/ecc_key')
-rw-r--r--src/pubkey/ecc_key/ecc_key.cpp157
-rw-r--r--src/pubkey/ecc_key/ecc_key.h29
2 files changed, 94 insertions, 92 deletions
diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp
index 2307f0e03..ee179a27c 100644
--- a/src/pubkey/ecc_key/ecc_key.cpp
+++ b/src/pubkey/ecc_key/ecc_key.cpp
@@ -1,9 +1,8 @@
/*************************************************
* ECC Key implemenation *
-* (C) 2007 Manuel Hartl / FlexSecure GmbH *
-* *
-* Falko Strenzke *
+* (C) 2007 Manuel Hartl, FlexSecure GmbH *
+* Falko Strenzke, FlexSecure GmbH *
+* 2008 Jack Lloyd *
*************************************************/
#include <botan/ecc_key.h>
@@ -22,27 +21,31 @@ namespace Botan {
*************************************************/
void EC_PublicKey::affirm_init() const // virtual
{
- if ((mp_dom_pars.get() == 0) || (mp_public_point.get() == 0))
- {
+ 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
+
+const EC_Domain_Params& EC_PublicKey::domain_parameters() const
{
if(!mp_dom_pars.get())
- {
- throw Invalid_State("EC_PublicKey::get_domain_parameters(): ec domain parameters are not yet set");
- }
+ throw Invalid_State("EC_PublicKey::domain_parameters(): ec domain parameters are not yet set");
+
return *mp_dom_pars;
}
+
+const PointGFp& EC_PublicKey::public_point() const
+ {
+ if(!mp_public_point.get())
+ throw Invalid_State("EC_PublicKey::public_point(): public point not set");
+
+ return *mp_public_point;
+ }
+
bool EC_PublicKey::domain_parameters_set()
{
- if (mp_dom_pars.get())
- {
- return true;
- }
- return false;
+ return mp_dom_pars.get();
}
+
void EC_PublicKey::X509_load_hook()
{
try
@@ -51,13 +54,12 @@ void EC_PublicKey::X509_load_hook()
affirm_init();
mp_public_point->check_invariants();
}
- catch ( Illegal_Point exc )
+ catch(Illegal_Point exc)
{
- throw Decoding_Error ( "decoded public point was found not to lie on curve" );
+ 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
@@ -66,20 +68,20 @@ X509_Encoder* EC_PublicKey::x509_encoder() const
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 );
+
+ SecureVector<byte> params =
+ encode_der_ec_dompar(key->domain_parameters(), 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 );
-
+ return EC2OSP(*(key->mp_public_point), PointGFp::COMPRESSED);
}
- EC_Key_Encoder ( const EC_PublicKey* k ) : key ( k )
- {}
+ EC_Key_Encoder(const EC_PublicKey* k): key(k) {}
private:
const EC_PublicKey* key;
};
@@ -92,19 +94,18 @@ X509_Decoder* EC_PublicKey::x509_decoder()
class EC_Key_Decoder : public X509_Decoder
{
public:
- void alg_id ( const AlgorithmIdentifier& alg_id )
+ void alg_id(const AlgorithmIdentifier& alg_id)
{
- key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) );
+ key->mp_dom_pars.reset(new EC_Domain_Params(decode_ber_ec_dompar(alg_id.parameters)));
}
- void key_bits ( const MemoryRegion<byte>& bits )
+ void key_bits(const MemoryRegion<byte>& bits)
{
- key->mp_public_point.reset ( new PointGFp ( OS2ECP ( bits, key->mp_dom_pars->get_curve() ) ) );
+ key->mp_public_point.reset(new PointGFp(OS2ECP(bits, key->domain_parameters().get_curve())));
key->X509_load_hook();
}
- EC_Key_Decoder ( EC_PublicKey* k ) : key ( k )
- {}
+ EC_Key_Decoder(EC_PublicKey* k): key(k) {}
private:
EC_PublicKey* key;
};
@@ -112,17 +113,16 @@ X509_Decoder* EC_PublicKey::x509_decoder()
return new EC_Key_Decoder(this);
}
-void EC_PublicKey::set_parameter_encoding ( EC_dompar_enc type )
+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" );
- }
+ 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" );
- }
+
+ if((type == ENC_OID) && (mp_dom_pars->get_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;
}
@@ -131,11 +131,18 @@ void EC_PublicKey::set_parameter_encoding ( EC_dompar_enc type )
********************************/
void EC_PrivateKey::affirm_init() const // virtual
{
+ if(m_private_value == 0)
+ throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized");
+
EC_PublicKey::affirm_init();
- if (m_private_value == 0)
- {
+ }
+
+const BigInt& EC_PrivateKey::private_value() const
+ {
+ if(m_private_value == 0)
throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized");
- }
+
+ return m_private_value;
}
/**
@@ -143,12 +150,13 @@ void EC_PrivateKey::affirm_init() const // virtual
**/
void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng)
{
- if (mp_dom_pars.get() == 0)
+ if(mp_dom_pars.get() == 0)
{
throw Invalid_State("cannot generate private key when domain parameters are not set");
}
+
BigInt tmp_private_value(0);
- tmp_private_value = BigInt::random_integer(rng, 1, mp_dom_pars->get_order() );
+ tmp_private_value = BigInt::random_integer(rng, 1, mp_dom_pars->get_order());
mp_public_point = std::auto_ptr<PointGFp>( new PointGFp (mp_dom_pars->get_base_point()));
mp_public_point->mult_this_secure(tmp_private_value, mp_dom_pars->get_order(), mp_dom_pars->get_order()-1);
@@ -167,26 +175,27 @@ PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const
AlgorithmIdentifier alg_id() const
{
key->affirm_init();
- SecureVector<byte> params = encode_der_ec_dompar ( * ( key->mp_dom_pars ), ENC_EXPLICIT );
- return AlgorithmIdentifier ( key->get_oid(),
- params );
+
+ SecureVector<byte> params = encode_der_ec_dompar(key->domain_parameters(), ENC_EXPLICIT);
+
+ return AlgorithmIdentifier(key->get_oid(), params);
}
MemoryVector<byte> key_bits() const
{
key->affirm_init();
- SecureVector<byte> octstr_secret = BigInt::encode_1363 ( key->m_private_value, key->m_private_value.bytes() );
+ SecureVector<byte> octstr_secret =
+ BigInt::encode_1363(key->m_private_value, key->m_private_value.bytes());
return DER_Encoder()
- .start_cons ( SEQUENCE )
- .encode ( BigInt ( 1 ) )
- .encode ( octstr_secret, OCTET_STRING )
+ .start_cons(SEQUENCE)
+ .encode(BigInt(1))
+ .encode(octstr_secret, OCTET_STRING)
.end_cons()
.get_contents();
}
- EC_Key_Encoder ( const EC_PrivateKey* k ) : key ( k )
- {}
+ EC_Key_Encoder(const EC_PrivateKey* k): key(k) {}
private:
const EC_PrivateKey* key;
};
@@ -202,29 +211,32 @@ PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
class EC_Key_Decoder : public PKCS8_Decoder
{
public:
- void alg_id ( const AlgorithmIdentifier& alg_id )
+ void alg_id(const AlgorithmIdentifier& alg_id)
{
- key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) );
+ key->mp_dom_pars.reset(new EC_Domain_Params(decode_ber_ec_dompar(alg_id.parameters)));
}
- void key_bits ( const MemoryRegion<byte>& bits )
+ 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 )
+
+ 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->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 )
- {}
+ EC_Key_Decoder(EC_PrivateKey* k): key(k) {}
private:
EC_PrivateKey* key;
};
@@ -232,17 +244,14 @@ PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
return new EC_Key_Decoder(this);
}
-
-void EC_PrivateKey::PKCS8_load_hook ( bool )
+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 )
- {
+ 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();
+ mp_public_point.reset(new PointGFp(m_private_value * mp_dom_pars->get_base_point()));
+ mp_public_point->check_invariants();
}
}
diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h
index aada1da1b..b2ca7dbcf 100644
--- a/src/pubkey/ecc_key/ecc_key.h
+++ b/src/pubkey/ecc_key/ecc_key.h
@@ -5,8 +5,8 @@
* (C) 2008 Jack Lloyd *
*************************************************/
-#ifndef BOTAN_ECC_KEY_H__
-#define BOTAN_ECC_KEY_H__
+#ifndef BOTAN_ECC_PUBLIC_KEY_BASE_H__
+#define BOTAN_ECC_PUBLIC_KEY_BASE_H__
#include <botan/bigint.h>
#include <botan/curve_gfp.h>
@@ -43,14 +43,7 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
* domain parameters of this point are not set
* @result the public point of this key
*/
- inline 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;
- }
+ const PointGFp& public_point() const;
/**
* Get the domain parameters of this key.
@@ -58,7 +51,7 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
* domain parameters of this point are not set
* @result the domain parameters of this key
*/
- EC_Domain_Params const get_domain_parameters() const;
+ const EC_Domain_Params& domain_parameters() const;
/**
* Set the domain parameter encoding to be used when encoding this key.
@@ -125,29 +118,29 @@ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Priv
* @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;
- }
+ const BigInt& private_value() 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;
- virtual ~EC_PrivateKey()
- {}
+ virtual void affirm_init() const;
+
+ virtual ~EC_PrivateKey() {}
protected:
virtual void PKCS8_load_hook(bool = false);
void generate_private_key(RandomNumberGenerator&);