aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/ecc_key
diff options
context:
space:
mode:
Diffstat (limited to 'src/pubkey/ecc_key')
-rw-r--r--src/pubkey/ecc_key/ecc_key.cpp117
-rw-r--r--src/pubkey/ecc_key/ecc_key.h50
2 files changed, 41 insertions, 126 deletions
diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp
index c14617cfe..8273256cf 100644
--- a/src/pubkey/ecc_key/ecc_key.cpp
+++ b/src/pubkey/ecc_key/ecc_key.cpp
@@ -2,7 +2,7 @@
* ECC Key implemenation
* (C) 2007 Manuel Hartl, FlexSecure GmbH
* Falko Strenzke, FlexSecure GmbH
-* 2008 Jack Lloyd
+* 2008-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -17,48 +17,15 @@
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");
- }
-
-const EC_Domain_Params& EC_PublicKey::domain_parameters() const
- {
- if(!mp_dom_pars.get())
- 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()
- {
- return mp_dom_pars.get();
- }
-
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();
+ public_point().check_invariants();
}
catch(Illegal_Point)
{
- throw Decoding_Error("decoded public point was found not to lie on curve");
+ throw Decoding_Error("Invalid public point; not on curve");
}
}
@@ -69,16 +36,13 @@ X509_Encoder* EC_PublicKey::x509_encoder() const
public:
AlgorithmIdentifier alg_id() const
{
- key->affirm_init();
-
return AlgorithmIdentifier(key->get_oid(),
- key->domain_parameters().DER_encode(key->m_param_enc));
+ key->domain().DER_encode(key->domain_format()));
}
MemoryVector<byte> key_bits() const
{
- key->affirm_init();
- return EC2OSP(*(key->mp_public_point), PointGFp::COMPRESSED);
+ return EC2OSP(key->public_point(), PointGFp::COMPRESSED);
}
EC_Key_Encoder(const EC_PublicKey* k): key(k) {}
@@ -96,15 +60,13 @@ X509_Decoder* EC_PublicKey::x509_decoder()
public:
void alg_id(const AlgorithmIdentifier& alg_id)
{
- key->mp_dom_pars.reset(new EC_Domain_Params(alg_id.parameters));
+ key->domain_params = EC_Domain_Params(alg_id.parameters);
}
void key_bits(const MemoryRegion<byte>& bits)
{
- key->mp_public_point.reset(
- new PointGFp(
- OS2ECP(bits, key->domain_parameters().get_curve())
- ));
+ key->public_key = PointGFp(
+ OS2ECP(bits, key->domain().get_curve()));
key->X509_load_hook();
}
@@ -124,33 +86,20 @@ void EC_PublicKey::set_parameter_encoding(EC_Domain_Params_Encoding form)
form != EC_DOMPAR_ENC_OID)
throw Invalid_Argument("Invalid encoding form for EC-key object specified");
- affirm_init();
-
- if((form == EC_DOMPAR_ENC_OID) && (mp_dom_pars->get_oid() == ""))
+ if((form == EC_DOMPAR_ENC_OID) && (domain_params.get_oid() == ""))
throw Invalid_Argument("Invalid encoding form OID specified for "
"EC-key object whose corresponding domain "
"parameters are without oid");
- m_param_enc = form;
- }
-
-/*
-* EC_PrivateKey
-*/
-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();
+ domain_encoding = form;
}
const BigInt& EC_PrivateKey::private_value() const
{
- if(m_private_value == 0)
+ if(private_key == 0)
throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized");
- return m_private_value;
+ return private_key;
}
/**
@@ -158,16 +107,20 @@ const BigInt& EC_PrivateKey::private_value() const
**/
void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng)
{
- if(mp_dom_pars.get() == 0)
- {
- throw Invalid_State("cannot generate private key when domain parameters are not set");
- }
+ if(!domain().initialized())
+ throw Invalid_State("Cannot generate new EC key, domain unset");
- m_private_value = BigInt::random_integer(rng, 1, mp_dom_pars->get_order());
+ private_key = BigInt::random_integer(rng, 1, domain().get_order());
+ public_key = domain().get_base_point() * private_key;
- mp_public_point = std::auto_ptr<PointGFp>( new PointGFp (mp_dom_pars->get_base_point()));
-
- *mp_public_point *= m_private_value;
+ try
+ {
+ public_key.check_invariants();
+ }
+ catch(Illegal_Point& e)
+ {
+ throw Invalid_State(algo_name() + " key generation failed");
+ }
}
/**
@@ -180,22 +133,17 @@ PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const
public:
AlgorithmIdentifier alg_id() const
{
- key->affirm_init();
-
return AlgorithmIdentifier(key->get_oid(),
- key->domain_parameters().DER_encode(EC_DOMPAR_ENC_EXPLICIT));
+ key->domain().DER_encode(EC_DOMPAR_ENC_EXPLICIT));
}
MemoryVector<byte> key_bits() const
{
- key->affirm_init();
- 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)
+ .encode(BigInt::encode_1363(key->private_key, key->private_key.bytes()),
+ OCTET_STRING)
.end_cons()
.get_contents();
}
@@ -218,7 +166,7 @@ PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
public:
void alg_id(const AlgorithmIdentifier& alg_id)
{
- key->mp_dom_pars.reset(new EC_Domain_Params(alg_id.parameters));
+ key->domain_params = EC_Domain_Params(alg_id.parameters);
}
void key_bits(const MemoryRegion<byte>& bits)
@@ -233,7 +181,7 @@ PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
.verify_end()
.end_cons();
- key->m_private_value = BigInt::decode(octstr_secret, octstr_secret.size());
+ key->private_key = BigInt::decode(octstr_secret, octstr_secret.size());
if(version != 1)
throw Decoding_Error("Wrong PKCS #1 key format version for EC key");
@@ -251,12 +199,7 @@ PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
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();
+ public_key = domain().get_base_point() * private_key;
}
}
diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h
index f61e4a2dc..073597bc9 100644
--- a/src/pubkey/ecc_key/ecc_key.h
+++ b/src/pubkey/ecc_key/ecc_key.h
@@ -2,7 +2,7 @@
* ECDSA
* (C) 2007 Falko Strenzke, FlexSecure GmbH
* Manuel Hartl, FlexSecure GmbH
-* (C) 2008 Jack Lloyd
+* (C) 2008-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -33,18 +33,12 @@ class BOTAN_DLL 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
*/
- const PointGFp& public_point() const;
+ const PointGFp& public_point() const { return public_key; }
/**
* Get the domain parameters of this key.
@@ -52,7 +46,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
*/
- const EC_Domain_Params& domain_parameters() const;
+ const EC_Domain_Params& domain() const { return domain_params; }
/**
* Set the domain parameter encoding to be used when encoding this key.
@@ -64,18 +58,7 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
* 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(EC_DOMPAR_ENC_EXPLICIT)
- {
- //assert(mp_dom_pars.get() == 0);
- //assert(mp_public_point.get() == 0);
- }
+ EC_Domain_Params_Encoding domain_format() const { return domain_encoding; }
/**
* Get an x509_encoder that can be used to encode this key.
@@ -90,19 +73,14 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_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;
-
+ EC_PublicKey() : domain_encoding(EC_DOMPAR_ENC_EXPLICIT) {}
virtual ~EC_PublicKey() {}
protected:
virtual void X509_load_hook();
- std::auto_ptr<EC_Domain_Params> mp_dom_pars;
- std::auto_ptr<PointGFp> mp_public_point;
- EC_Domain_Params_Encoding m_param_enc;
+ EC_Domain_Params domain_params;
+ PointGFp public_key;
+ EC_Domain_Params_Encoding domain_encoding;
};
/**
@@ -131,19 +109,13 @@ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Priv
*/
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() {}
protected:
virtual void PKCS8_load_hook(bool = false);
+
void generate_private_key(RandomNumberGenerator&);
- BigInt m_private_value;
+
+ BigInt private_key;
};
}