aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-03-02 04:01:18 +0000
committerlloyd <[email protected]>2010-03-02 04:01:18 +0000
commitb822cc605b324bf449fb3e0a2185b83cd4a157e3 (patch)
treef59b57f65e30a0c025c0c4265e591a9f507ac0ff /src
parent510128ffd3789301305c7f1fe52a11b238a6a60b (diff)
Add some simple constructors to the EC_ base key types to simplify
the various implementations
Diffstat (limited to 'src')
-rw-r--r--src/pubkey/ecc_key/ecc_key.cpp38
-rw-r--r--src/pubkey/ecc_key/ecc_key.h23
-rw-r--r--src/pubkey/ecdh/ecdh.cpp17
-rw-r--r--src/pubkey/ecdh/ecdh.h30
-rw-r--r--src/pubkey/ecdsa/ecdsa.cpp33
-rw-r--r--src/pubkey/ecdsa/ecdsa.h10
-rw-r--r--src/pubkey/gost_3410/gost_3410.cpp25
-rw-r--r--src/pubkey/gost_3410/gost_3410.h8
8 files changed, 78 insertions, 106 deletions
diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp
index 8273256cf..10968fa7e 100644
--- a/src/pubkey/ecc_key/ecc_key.cpp
+++ b/src/pubkey/ecc_key/ecc_key.cpp
@@ -17,6 +17,24 @@
namespace Botan {
+EC_PublicKey::EC_PublicKey(const EC_Domain_Params& dom_par,
+ const PointGFp& pub_point) :
+ domain_params(dom_par), public_key(pub_point),
+ domain_encoding(EC_DOMPAR_ENC_EXPLICIT)
+ {
+ if(domain().get_curve() != public_point().get_curve())
+ throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
+
+ try
+ {
+ public_key.check_invariants();
+ }
+ catch(Illegal_Point)
+ {
+ throw Invalid_State("Public key failed invariant check");
+ }
+ }
+
void EC_PublicKey::X509_load_hook()
{
try
@@ -97,7 +115,7 @@ void EC_PublicKey::set_parameter_encoding(EC_Domain_Params_Encoding form)
const BigInt& EC_PrivateKey::private_value() const
{
if(private_key == 0)
- throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized");
+ throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
return private_key;
}
@@ -105,10 +123,20 @@ const BigInt& EC_PrivateKey::private_value() const
/**
* EC_PrivateKey generator
**/
-void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng)
+EC_PrivateKey::EC_PrivateKey(const EC_Domain_Params& dom_par,
+ const BigInt& priv_key) :
+ EC_PublicKey(dom_par, dom_par.get_base_point() * private_key),
+ private_key(priv_key)
+ {
+ }
+
+/**
+* EC_PrivateKey generator
+**/
+EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
+ const EC_Domain_Params& dom_par)
{
- if(!domain().initialized())
- throw Invalid_State("Cannot generate new EC key, domain unset");
+ domain_params = dom_par;
private_key = BigInt::random_integer(rng, 1, domain().get_order());
public_key = domain().get_base_point() * private_key;
@@ -119,7 +147,7 @@ void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng)
}
catch(Illegal_Point& e)
{
- throw Invalid_State(algo_name() + " key generation failed");
+ throw Internal_Error("ECC private key generation failed");
}
}
diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h
index 073597bc9..653f97cab 100644
--- a/src/pubkey/ecc_key/ecc_key.h
+++ b/src/pubkey/ecc_key/ecc_key.h
@@ -58,7 +58,8 @@ 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
*/
- EC_Domain_Params_Encoding domain_format() const { return domain_encoding; }
+ EC_Domain_Params_Encoding domain_format() const
+ { return domain_encoding; }
/**
* Get an x509_encoder that can be used to encode this key.
@@ -74,6 +75,10 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
X509_Decoder* x509_decoder();
EC_PublicKey() : domain_encoding(EC_DOMPAR_ENC_EXPLICIT) {}
+
+ EC_PublicKey(const EC_Domain_Params& dom_par,
+ const PointGFp& pub_point);
+
virtual ~EC_PublicKey() {}
protected:
virtual void X509_load_hook();
@@ -86,9 +91,19 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
/**
* This abstract class represents general EC Private Keys
*/
-class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Private_Key
+class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey,
+ public virtual Private_Key
{
public:
+ EC_PrivateKey() {}
+
+ EC_PrivateKey(const EC_Domain_Params& domain,
+ const BigInt& private_key);
+
+ EC_PrivateKey(RandomNumberGenerator& rng,
+ const EC_Domain_Params& domain);
+
+ virtual ~EC_PrivateKey() {}
/**
* Get an PKCS#8 encoder that can be used to encoded this key.
@@ -108,13 +123,9 @@ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Priv
* @result the private key value of this key object
*/
const BigInt& private_value() const;
-
- virtual ~EC_PrivateKey() {}
protected:
virtual void PKCS8_load_hook(bool = false);
- void generate_private_key(RandomNumberGenerator&);
-
BigInt private_key;
};
diff --git a/src/pubkey/ecdh/ecdh.cpp b/src/pubkey/ecdh/ecdh.cpp
index e27676d46..7577a8569 100644
--- a/src/pubkey/ecdh/ecdh.cpp
+++ b/src/pubkey/ecdh/ecdh.cpp
@@ -11,23 +11,6 @@
namespace Botan {
-ECDH_PublicKey::ECDH_PublicKey(const EC_Domain_Params& dom_par,
- const PointGFp& pub_point)
- {
- domain_params = dom_par;
- public_key = pub_point;
-
- if(domain().get_curve() != public_point().get_curve())
- throw Invalid_Argument("ECDH_PublicKey: curve mismatch in constructor");
- }
-
-ECDH_PrivateKey::ECDH_PrivateKey(RandomNumberGenerator& rng,
- const EC_Domain_Params& dom_pars)
- {
- domain_params = dom_pars;
- generate_private_key(rng);
- }
-
/**
* Derive a key
*/
diff --git a/src/pubkey/ecdh/ecdh.h b/src/pubkey/ecdh/ecdh.h
index 632083dcc..630237edf 100644
--- a/src/pubkey/ecdh/ecdh.h
+++ b/src/pubkey/ecdh/ecdh.h
@@ -22,12 +22,6 @@ class BOTAN_DLL ECDH_PublicKey : public virtual EC_PublicKey
public:
/**
- * Get this keys algorithm name.
- * @result this keys algorithm name
- */
- std::string algo_name() const { return "ECDH"; }
-
- /**
* Default constructor. Use this one if you want to later fill
* this object with data from an encoded key.
*/
@@ -39,7 +33,14 @@ class BOTAN_DLL ECDH_PublicKey : public virtual EC_PublicKey
* @param public_point the public point defining this key
*/
ECDH_PublicKey(const EC_Domain_Params& dom_par,
- const PointGFp& public_point);
+ const PointGFp& public_point) :
+ EC_PublicKey(dom_par, public_point) {}
+
+ /**
+ * Get this keys algorithm name.
+ * @result this keys algorithm name
+ */
+ std::string algo_name() const { return "ECDH"; }
/**
* Get the maximum number of bits allowed to be fed to this key.
@@ -60,18 +61,19 @@ class BOTAN_DLL ECDH_PrivateKey : public ECDH_PublicKey,
public:
/**
- * Generate a new private key
- * @param the domain parameters to used for this key
- */
- ECDH_PrivateKey(RandomNumberGenerator& rng,
- const EC_Domain_Params& dom_pars);
-
- /**
* Default constructor. Use this one if you want to later fill
* this object with data from an encoded key.
*/
ECDH_PrivateKey() {}
+ /**
+ * Generate a new private key
+ * @param the domain parameters to used for this key
+ */
+ ECDH_PrivateKey(RandomNumberGenerator& rng,
+ const EC_Domain_Params& domain) :
+ EC_PrivateKey(rng, domain) {}
+
MemoryVector<byte> public_value() const
{ return EC2OSP(public_point(), PointGFp::UNCOMPRESSED); }
diff --git a/src/pubkey/ecdsa/ecdsa.cpp b/src/pubkey/ecdsa/ecdsa.cpp
index f5ded5aa6..d245543f7 100644
--- a/src/pubkey/ecdsa/ecdsa.cpp
+++ b/src/pubkey/ecdsa/ecdsa.cpp
@@ -11,39 +11,6 @@
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_PrivateKey::ECDSA_PrivateKey(const EC_Domain_Params& dom_pars,
- const BigInt& x)
- {
- domain_params = dom_pars;
-
- private_key = x;
- public_key = domain().get_base_point() * x;
-
- try
- {
- public_key.check_invariants();
- }
- catch(Illegal_Point& e)
- {
- throw Invalid_State("ECDSA key generation failed");
- }
- }
-
bool ECDSA_PublicKey::verify(const byte msg[], u32bit msg_len,
const byte sig[], u32bit sig_len) const
{
diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h
index 447bc3758..e7f29b600 100644
--- a/src/pubkey/ecdsa/ecdsa.h
+++ b/src/pubkey/ecdsa/ecdsa.h
@@ -62,7 +62,9 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey,
* @param public_point the public point defining this key
*/
ECDSA_PublicKey(const EC_Domain_Params& dom_par,
- const PointGFp& public_point); // sets core
+ const PointGFp& public_point) :
+ EC_PublicKey(dom_par, public_point) {}
+
};
/**
@@ -84,14 +86,16 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey,
* @param the domain parameters to used for this key
*/
ECDSA_PrivateKey(RandomNumberGenerator& rng,
- const EC_Domain_Params& domain);
+ const EC_Domain_Params& domain) :
+ EC_PrivateKey(rng, domain) {}
/**
* Load a private key
* @param domain parameters
* @param x the private key
*/
- ECDSA_PrivateKey(const EC_Domain_Params& domain, const BigInt& x);
+ ECDSA_PrivateKey(const EC_Domain_Params& domain, const BigInt& x) :
+ EC_PrivateKey(domain, x) {}
/**
* Sign a message with this key.
diff --git a/src/pubkey/gost_3410/gost_3410.cpp b/src/pubkey/gost_3410/gost_3410.cpp
index 1c3faca7a..8dd72dfc1 100644
--- a/src/pubkey/gost_3410/gost_3410.cpp
+++ b/src/pubkey/gost_3410/gost_3410.cpp
@@ -16,31 +16,6 @@
namespace Botan {
-GOST_3410_PrivateKey::GOST_3410_PrivateKey(RandomNumberGenerator& rng,
- const EC_Domain_Params& dom_pars)
- {
- domain_params = dom_pars;
- generate_private_key(rng);
- }
-
-GOST_3410_PrivateKey::GOST_3410_PrivateKey(const EC_Domain_Params& dom_pars,
- const BigInt& x)
- {
- domain_params = dom_pars;
-
- private_key = x;
- public_key = domain().get_base_point() * private_key;
-
- try
- {
- public_key.check_invariants();
- }
- catch(Illegal_Point)
- {
- throw Invalid_State("GOST_3410 key generation failed");
- }
- }
-
X509_Encoder* GOST_3410_PublicKey::x509_encoder() const
{
class GOST_3410_Key_Encoder : public X509_Encoder
diff --git a/src/pubkey/gost_3410/gost_3410.h b/src/pubkey/gost_3410/gost_3410.h
index 1d3430753..8104cbb75 100644
--- a/src/pubkey/gost_3410/gost_3410.h
+++ b/src/pubkey/gost_3410/gost_3410.h
@@ -63,7 +63,7 @@ class BOTAN_DLL GOST_3410_PublicKey : public virtual EC_PublicKey,
* @param public_point the public point defining this key
*/
GOST_3410_PublicKey(const EC_Domain_Params& dom_par,
- const PointGFp& public_point); // sets core
+ const PointGFp& public_point);
/**
* Get an x509_encoder that can be used to encode this key.
@@ -98,14 +98,16 @@ class BOTAN_DLL GOST_3410_PrivateKey : public GOST_3410_PublicKey,
* @param the domain parameters to used for this key
*/
GOST_3410_PrivateKey(RandomNumberGenerator& rng,
- const EC_Domain_Params& domain);
+ const EC_Domain_Params& domain) :
+ EC_PrivateKey(rng, domain) {}
/**
* Load a private key
* @param domain parameters
* @param x the private key
*/
- GOST_3410_PrivateKey(const EC_Domain_Params& domain, const BigInt& x);
+ GOST_3410_PrivateKey(const EC_Domain_Params& domain, const BigInt& x) :
+ EC_PrivateKey(domain, x) {}
/**
* Sign a message with this key.