aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-03-04 03:49:35 +0000
committerlloyd <[email protected]>2010-03-04 03:49:35 +0000
commitfe9aa5acece6c004f2c1c1aa4b23d7c44207672f (patch)
tree56bf2fc8378d965138e29074c23f645ed5d1a947
parentde89566f633d5ed807ca57a59cc1071f79fdded3 (diff)
Add similar decoding constructors to the private keys
-rw-r--r--checks/ecdh.cpp3
-rw-r--r--checks/pk_bench.cpp3
-rw-r--r--src/pubkey/dh/dh.h12
-rw-r--r--src/pubkey/dl_algo/dl_algo.cpp10
-rw-r--r--src/pubkey/dl_algo/dl_algo.h5
-rw-r--r--src/pubkey/dsa/dsa.h21
-rw-r--r--src/pubkey/ecc_key/ecc_key.cpp23
-rw-r--r--src/pubkey/ecc_key/ecc_key.h11
-rw-r--r--src/pubkey/ecdh/ecdh.h8
-rw-r--r--src/pubkey/ecdsa/ecdsa.h9
-rw-r--r--src/pubkey/elgamal/elgamal.h9
-rw-r--r--src/pubkey/gost_3410/gost_3410.h9
-rw-r--r--src/pubkey/if_algo/if_algo.cpp22
-rw-r--r--src/pubkey/if_algo/if_algo.h10
-rw-r--r--src/pubkey/nr/nr.h8
-rw-r--r--src/pubkey/pk_algs.cpp67
-rw-r--r--src/pubkey/rsa/rsa.h12
-rw-r--r--src/pubkey/rw/rw.h10
18 files changed, 167 insertions, 85 deletions
diff --git a/checks/ecdh.cpp b/checks/ecdh.cpp
index cb3246ace..d8f8e2fad 100644
--- a/checks/ecdh.cpp
+++ b/checks/ecdh.cpp
@@ -176,8 +176,7 @@ void test_ecdh_cp_ctor_as_op(RandomNumberGenerator& rng)
// alices key (a key constructed by domain parameters IS an ephimeral key!)
Botan::ECDH_PrivateKey private_a(rng, dom_pars);
Botan::ECDH_PrivateKey private_a2(private_a);
- Botan::ECDH_PrivateKey private_a3;
- private_a3 = private_a2;
+ Botan::ECDH_PrivateKey private_a3 = private_a2;
Botan::ECDH_PublicKey public_a = private_a; // Bob gets this
Botan::ECDH_PublicKey public_a2(public_a);
diff --git a/checks/pk_bench.cpp b/checks/pk_bench.cpp
index 322e643cc..4053316dc 100644
--- a/checks/pk_bench.cpp
+++ b/checks/pk_bench.cpp
@@ -479,7 +479,7 @@ void benchmark_dsa_nr(RandomNumberGenerator& rng,
"dsa/botan/3072",
NULL };
- const std::string algo_name = PRIV_KEY_TYPE().algo_name();
+ std::string algo_name;
for(size_t j = 0; domains[j]; j++)
{
@@ -499,6 +499,7 @@ void benchmark_dsa_nr(RandomNumberGenerator& rng,
keygen_timer.start();
PRIV_KEY_TYPE key(rng, group);
+ algo_name = key.algo_name();
keygen_timer.stop();
std::auto_ptr<PK_Signer> sig(get_pk_signer(key, padding));
diff --git a/src/pubkey/dh/dh.h b/src/pubkey/dh/dh.h
index 7b81008a3..638553db4 100644
--- a/src/pubkey/dh/dh.h
+++ b/src/pubkey/dh/dh.h
@@ -57,11 +57,13 @@ class BOTAN_DLL DH_PrivateKey : public DH_PublicKey,
MemoryVector<byte> public_value() const;
- /**
- * Construct an uninitialized key. Use this constructor if you wish
- * to decode an encoded key into the new instance.
- */
- DH_PrivateKey() {}
+ DH_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
+ {
+ PKCS8_load_hook(rng);
+ }
/**
* Construct a private key with predetermined value.
diff --git a/src/pubkey/dl_algo/dl_algo.cpp b/src/pubkey/dl_algo/dl_algo.cpp
index 7ec6877e1..185e62cef 100644
--- a/src/pubkey/dl_algo/dl_algo.cpp
+++ b/src/pubkey/dl_algo/dl_algo.cpp
@@ -38,6 +38,16 @@ MemoryVector<byte> DL_Scheme_PrivateKey::pkcs8_private_key() const
return DER_Encoder().encode(x).get_contents();
}
+DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ DL_Group::Format format)
+ {
+ DataSource_Memory source(alg_id.parameters);
+ group.BER_decode(source, format);
+
+ BER_Decoder(key_bits).decode(x);
+ }
+
/*
* Return the PKCS #8 private key decoder
*/
diff --git a/src/pubkey/dl_algo/dl_algo.h b/src/pubkey/dl_algo/dl_algo.h
index 66e6eafd0..59690a33a 100644
--- a/src/pubkey/dl_algo/dl_algo.h
+++ b/src/pubkey/dl_algo/dl_algo.h
@@ -91,6 +91,10 @@ class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey,
MemoryVector<byte> pkcs8_private_key() const;
+ DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ DL_Group::Format group_format);
+
/**
* Get an PKCS#8 decoder for this key.
* @param rng the rng to use
@@ -99,6 +103,7 @@ class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey,
*/
PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator& rng);
protected:
+ DL_Scheme_PrivateKey() {}
BigInt x;
private:
virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false) {}
diff --git a/src/pubkey/dsa/dsa.h b/src/pubkey/dsa/dsa.h
index 302715222..c263d4ec8 100644
--- a/src/pubkey/dsa/dsa.h
+++ b/src/pubkey/dsa/dsa.h
@@ -35,8 +35,8 @@ class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key,
{ X509_load_hook(); }
DSA_PublicKey(const DL_Group& group, const BigInt& y);
- DSA_PublicKey() {}
protected:
+ DSA_PublicKey() {}
DSA_Core core;
private:
void X509_load_hook();
@@ -50,14 +50,23 @@ class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey,
public virtual DL_Scheme_PrivateKey
{
public:
- SecureVector<byte> sign(const byte[], u32bit,
+ SecureVector<byte> sign(const byte hash[], u32bit hash_len,
RandomNumberGenerator& rng) const;
- bool check_key(RandomNumberGenerator& rng, bool) const;
+ bool check_key(RandomNumberGenerator& rng, bool strong) const;
+
+ DSA_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
+ {
+ PKCS8_load_hook(rng);
+ }
+
+ DSA_PrivateKey(RandomNumberGenerator& rng,
+ const DL_Group& group,
+ const BigInt& private_key = 0);
- DSA_PrivateKey() {}
- DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&,
- const BigInt& = 0);
private:
void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false);
};
diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp
index f80e2bb15..4c91672d3 100644
--- a/src/pubkey/ecc_key/ecc_key.cpp
+++ b/src/pubkey/ecc_key/ecc_key.cpp
@@ -139,6 +139,29 @@ MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const
.get_contents();
}
+EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits)
+ {
+ domain_params = EC_Domain_Params(alg_id.parameters);
+
+ u32bit version;
+ SecureVector<byte> octstr_secret;
+
+ BER_Decoder(key_bits)
+ .start_cons(SEQUENCE)
+ .decode(version)
+ .decode(octstr_secret, OCTET_STRING)
+ .verify_end()
+ .end_cons();
+
+ if(version != 1)
+ throw Decoding_Error("Wrong key format version for EC key");
+
+ private_key = BigInt::decode(octstr_secret, octstr_secret.size());
+
+ public_key = domain().get_base_point() * private_key;
+ }
+
/**
* Return the PKCS #8 public key decoder
*/
diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h
index 1b8ac3ff5..283813236 100644
--- a/src/pubkey/ecc_key/ecc_key.h
+++ b/src/pubkey/ecc_key/ecc_key.h
@@ -32,8 +32,6 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
{
public:
- EC_PublicKey() : domain_encoding(EC_DOMPAR_ENC_EXPLICIT) {}
-
EC_PublicKey(const EC_Domain_Params& dom_par,
const PointGFp& pub_point);
@@ -82,6 +80,8 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key
EC_Domain_Params_Encoding domain_format() const
{ return domain_encoding; }
protected:
+ EC_PublicKey() : domain_encoding(EC_DOMPAR_ENC_EXPLICIT) {}
+
virtual void X509_load_hook();
EC_Domain_Params domain_params;
@@ -96,14 +96,15 @@ 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);
+ EC_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits);
+
virtual ~EC_PrivateKey() {}
MemoryVector<byte> pkcs8_private_key() const;
@@ -121,6 +122,8 @@ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey,
*/
const BigInt& private_value() const;
protected:
+ EC_PrivateKey() {}
+
virtual void PKCS8_load_hook(bool = false);
BigInt private_key;
diff --git a/src/pubkey/ecdh/ecdh.h b/src/pubkey/ecdh/ecdh.h
index 10cf75de0..8185e25f2 100644
--- a/src/pubkey/ecdh/ecdh.h
+++ b/src/pubkey/ecdh/ecdh.h
@@ -61,11 +61,9 @@ class BOTAN_DLL ECDH_PrivateKey : public ECDH_PublicKey,
{
public:
- /**
- * Default constructor. Use this one if you want to later fill
- * this object with data from an encoded key.
- */
- ECDH_PrivateKey() {}
+ ECDH_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits) :
+ EC_PrivateKey(alg_id, key_bits) {}
/**
* Generate a new private key
diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h
index 4beb531b0..53d17e304 100644
--- a/src/pubkey/ecdsa/ecdsa.h
+++ b/src/pubkey/ecdsa/ecdsa.h
@@ -75,11 +75,10 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey,
public PK_Signing_Key
{
public:
- /**
- * Default constructor. Use this one if you want to later fill
- * this object with data from an encoded key.
- */
- ECDSA_PrivateKey() {}
+
+ ECDSA_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits) :
+ EC_PrivateKey(alg_id, key_bits) {}
/**
* Generate a new private key
diff --git a/src/pubkey/elgamal/elgamal.h b/src/pubkey/elgamal/elgamal.h
index 305587d93..92adf108d 100644
--- a/src/pubkey/elgamal/elgamal.h
+++ b/src/pubkey/elgamal/elgamal.h
@@ -52,7 +52,14 @@ class BOTAN_DLL ElGamal_PrivateKey : public ElGamal_PublicKey,
bool check_key(RandomNumberGenerator& rng, bool) const;
- ElGamal_PrivateKey() {}
+ ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42)
+ {
+ PKCS8_load_hook(rng);
+ }
+
ElGamal_PrivateKey(RandomNumberGenerator&, const DL_Group&,
const BigInt& = 0);
private:
diff --git a/src/pubkey/gost_3410/gost_3410.h b/src/pubkey/gost_3410/gost_3410.h
index 7dda6ccae..eb80943b1 100644
--- a/src/pubkey/gost_3410/gost_3410.h
+++ b/src/pubkey/gost_3410/gost_3410.h
@@ -80,11 +80,10 @@ class BOTAN_DLL GOST_3410_PrivateKey : public GOST_3410_PublicKey,
public PK_Signing_Key
{
public:
- /**
- * Default constructor. Use this one if you want to later fill
- * this object with data from an encoded key.
- */
- GOST_3410_PrivateKey() {}
+
+ GOST_3410_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits) :
+ EC_PrivateKey(alg_id, key_bits) {}
/**
* Generate a new private key
diff --git a/src/pubkey/if_algo/if_algo.cpp b/src/pubkey/if_algo/if_algo.cpp
index 682870663..e31f6011f 100644
--- a/src/pubkey/if_algo/if_algo.cpp
+++ b/src/pubkey/if_algo/if_algo.cpp
@@ -56,6 +56,28 @@ MemoryVector<byte> IF_Scheme_PrivateKey::pkcs8_private_key() const
.get_contents();
}
+IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(const AlgorithmIdentifier&,
+ const MemoryRegion<byte>& key_bits)
+ {
+ u32bit version;
+
+ BER_Decoder(key_bits)
+ .start_cons(SEQUENCE)
+ .decode(version)
+ .decode(n)
+ .decode(e)
+ .decode(d)
+ .decode(p)
+ .decode(q)
+ .decode(d1)
+ .decode(d2)
+ .decode(c)
+ .end_cons();
+
+ if(version != 0)
+ throw Decoding_Error("Unknown PKCS #1 key format version");
+ }
+
/*
* Return the PKCS #8 public key decoder
*/
diff --git a/src/pubkey/if_algo/if_algo.h b/src/pubkey/if_algo/if_algo.h
index 958735c4d..c16627e73 100644
--- a/src/pubkey/if_algo/if_algo.h
+++ b/src/pubkey/if_algo/if_algo.h
@@ -21,6 +21,9 @@ namespace Botan {
class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key
{
public:
+ IF_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits);
+
bool check_key(RandomNumberGenerator& rng, bool) const;
AlgorithmIdentifier algorithm_identifier() const;
@@ -41,8 +44,6 @@ class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key
u32bit max_input_bits() const { return (n.bits() - 1); }
- IF_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
- const MemoryRegion<byte>& key_bits);
protected:
IF_Scheme_PublicKey() {}
@@ -59,6 +60,9 @@ class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
public virtual Private_Key
{
public:
+ IF_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits);
+
bool check_key(RandomNumberGenerator& rng, bool) const;
/**
@@ -83,6 +87,8 @@ class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&);
protected:
+ IF_Scheme_PrivateKey() {}
+
virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
BigInt d, p, q, d1, d2, c;
};
diff --git a/src/pubkey/nr/nr.h b/src/pubkey/nr/nr.h
index 64b50fc7f..df4d9ec48 100644
--- a/src/pubkey/nr/nr.h
+++ b/src/pubkey/nr/nr.h
@@ -55,7 +55,13 @@ class BOTAN_DLL NR_PrivateKey : public NR_PublicKey,
bool check_key(RandomNumberGenerator& rng, bool) const;
- NR_PrivateKey() {}
+ NR_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57)
+ {
+ PKCS8_load_hook(rng);
+ }
NR_PrivateKey(RandomNumberGenerator&, const DL_Group&,
const BigInt& = 0);
diff --git a/src/pubkey/pk_algs.cpp b/src/pubkey/pk_algs.cpp
index d65913a06..132e9f35c 100644
--- a/src/pubkey/pk_algs.cpp
+++ b/src/pubkey/pk_algs.cpp
@@ -94,70 +94,55 @@ BOTAN_DLL Public_Key* make_public_key(const AlgorithmIdentifier& alg_id,
return 0;
}
-namespace {
-
-Private_Key* get_private_key(const std::string& alg_name)
+BOTAN_DLL Private_Key* make_private_key(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng)
{
+ const std::string alg_name = OIDS::lookup(alg_id.oid);
+ if(alg_name == "")
+ throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
+
#if defined(BOTAN_HAS_RSA)
- if(alg_name == "RSA") return new RSA_PrivateKey;
+ if(alg_name == "RSA")
+ return new RSA_PrivateKey(alg_id, key_bits, rng);
+#endif
+
+#if defined(BOTAN_HAS_RW)
+ if(alg_name == "RW")
+ return new RW_PrivateKey(alg_id, key_bits, rng);
#endif
#if defined(BOTAN_HAS_DSA)
- if(alg_name == "DSA") return new DSA_PrivateKey;
+ if(alg_name == "DSA")
+ return new DSA_PrivateKey(alg_id, key_bits, rng);
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
- if(alg_name == "DH") return new DH_PrivateKey;
+ if(alg_name == "DH")
+ return new DH_PrivateKey(alg_id, key_bits, rng);
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
- if(alg_name == "NR") return new NR_PrivateKey;
-#endif
-
-#if defined(BOTAN_HAS_RW)
- if(alg_name == "RW") return new RW_PrivateKey;
+ if(alg_name == "NR")
+ return new NR_PrivateKey(alg_id, key_bits, rng);
#endif
#if defined(BOTAN_HAS_ELG)
- if(alg_name == "ELG") return new ElGamal_PrivateKey;
+ if(alg_name == "ELG")
+ return new ElGamal_PrivateKey(alg_id, key_bits, rng);
#endif
#if defined(BOTAN_HAS_ECDSA)
- if(alg_name == "ECDSA") return new ECDSA_PrivateKey;
+ if(alg_name == "ECDSA")
+ return new ECDSA_PrivateKey(alg_id, key_bits);
#endif
#if defined(BOTAN_HAS_GOST_34_10_2001)
- if(alg_name == "GOST-34.10") return new GOST_3410_PrivateKey;
+ if(alg_name == "GOST-34.10")
+ return new GOST_3410_PrivateKey(alg_id, key_bits);
#endif
return 0;
}
}
-
-BOTAN_DLL Private_Key* make_private_key(const AlgorithmIdentifier& alg_id,
- const MemoryRegion<byte>& key_bits,
- RandomNumberGenerator& rng)
- {
- const std::string alg_name = OIDS::lookup(alg_id.oid);
- if(alg_name == "")
- throw Decoding_Error("Unknown algorithm OID: " + alg_id.oid.as_string());
-
- std::auto_ptr<Private_Key> key(get_private_key(alg_name));
-
- if(!key.get())
- throw PKCS8_Exception("Unknown PK algorithm/OID: " + alg_name + ", " +
- alg_id.oid.as_string());
-
- std::auto_ptr<PKCS8_Decoder> decoder(key->pkcs8_decoder(rng));
-
- if(!decoder.get())
- throw Decoding_Error("Key does not support PKCS #8 decoding");
-
- decoder->alg_id(alg_id);
- decoder->key_bits(key_bits);
-
- return key.release();
- }
-
-}
diff --git a/src/pubkey/rsa/rsa.h b/src/pubkey/rsa/rsa.h
index c24dc7d0c..33a049efe 100644
--- a/src/pubkey/rsa/rsa.h
+++ b/src/pubkey/rsa/rsa.h
@@ -58,11 +58,13 @@ class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey,
bool check_key(RandomNumberGenerator& rng, bool) const;
- /**
- * Default constructor, does not set any internal values. Use this
- * constructor if you wish to decode a DER or PEM encoded key.
- */
- RSA_PrivateKey() {}
+ RSA_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ IF_Scheme_PrivateKey(alg_id, key_bits)
+ {
+ PKCS8_load_hook(rng);
+ }
/**
* Construct a private key from the specified parameters.
diff --git a/src/pubkey/rw/rw.h b/src/pubkey/rw/rw.h
index ecc29ed6c..dff9a17dc 100644
--- a/src/pubkey/rw/rw.h
+++ b/src/pubkey/rw/rw.h
@@ -46,9 +46,15 @@ class BOTAN_DLL RW_PrivateKey : public RW_PublicKey,
bool check_key(RandomNumberGenerator& rng, bool) const;
- RW_PrivateKey() {}
+ RW_PrivateKey(const AlgorithmIdentifier& alg_id,
+ const MemoryRegion<byte>& key_bits,
+ RandomNumberGenerator& rng) :
+ IF_Scheme_PrivateKey(alg_id, key_bits)
+ {
+ PKCS8_load_hook(rng);
+ }
- RW_PrivateKey(RandomNumberGenerator&,
+ RW_PrivateKey(RandomNumberGenerator& rng,
const BigInt&, const BigInt&, const BigInt&,
const BigInt& = 0, const BigInt& = 0);