aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-06-10 16:23:59 +0000
committerlloyd <[email protected]>2008-06-10 16:23:59 +0000
commit2aef9fa5bc25984a838a51a93ac0e918d2d1bbac (patch)
tree9f0b9035c4549380de6c62a7bf941a9396b8f554
parent7ab69d77956048fdc27f49a07724d6b21549b916 (diff)
Pass RandomNumberGenerator references to public key operations that need
them (encrypt and sign), with the intent of slowly bubbling up the access points to the API level, at which point the application handles managing the RNG. This will allow removing the compiled-in global PRNG, and make testing much simpler.
-rw-r--r--include/dl_algo.h2
-rw-r--r--include/dsa.h3
-rw-r--r--include/elgamal.h3
-rw-r--r--include/nr.h3
-rw-r--r--include/pk_keys.h6
-rw-r--r--include/rsa.h8
-rw-r--r--include/rw.h3
-rw-r--r--src/dsa.cpp5
-rw-r--r--src/elgamal.cpp9
-rw-r--r--src/nr.cpp5
-rw-r--r--src/pubkey.cpp11
-rw-r--r--src/rsa.cpp6
-rw-r--r--src/rw.cpp3
13 files changed, 41 insertions, 26 deletions
diff --git a/include/dl_algo.h b/include/dl_algo.h
index 2bcd67cb9..aaf078bac 100644
--- a/include/dl_algo.h
+++ b/include/dl_algo.h
@@ -40,7 +40,7 @@ class BOTAN_DLL DL_Scheme_PublicKey : public virtual Public_Key
* DL Private Key *
*************************************************/
class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey,
- public virtual Private_Key
+ public virtual Private_Key
{
public:
bool check_key(RandomNumberGenerator& rng, bool) const;
diff --git a/include/dsa.h b/include/dsa.h
index a858374e6..67de8d137 100644
--- a/include/dsa.h
+++ b/include/dsa.h
@@ -43,7 +43,8 @@ class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey,
public virtual DL_Scheme_PrivateKey
{
public:
- SecureVector<byte> sign(const byte[], u32bit) const;
+ SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
bool check_key(RandomNumberGenerator& rng, bool) const;
diff --git a/include/elgamal.h b/include/elgamal.h
index 90fd8c77a..e5de3f965 100644
--- a/include/elgamal.h
+++ b/include/elgamal.h
@@ -21,7 +21,8 @@ class BOTAN_DLL ElGamal_PublicKey : public PK_Encrypting_Key,
std::string algo_name() const { return "ElGamal"; }
DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; }
- SecureVector<byte> encrypt(const byte[], u32bit) const;
+ SecureVector<byte> encrypt(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
u32bit max_input_bits() const;
ElGamal_PublicKey() {}
diff --git a/include/nr.h b/include/nr.h
index 0f5e1d677..c8eaaf32e 100644
--- a/include/nr.h
+++ b/include/nr.h
@@ -43,7 +43,8 @@ class BOTAN_DLL NR_PrivateKey : public NR_PublicKey,
public virtual DL_Scheme_PrivateKey
{
public:
- SecureVector<byte> sign(const byte[], u32bit) const;
+ SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
bool check_key(RandomNumberGenerator& rng, bool) const;
diff --git a/include/pk_keys.h b/include/pk_keys.h
index fed96d37b..c6f9ced3c 100644
--- a/include/pk_keys.h
+++ b/include/pk_keys.h
@@ -54,7 +54,8 @@ class BOTAN_DLL Private_Key : public virtual Public_Key
class BOTAN_DLL PK_Encrypting_Key : public virtual Public_Key
{
public:
- virtual SecureVector<byte> encrypt(const byte[], u32bit) const = 0;
+ virtual SecureVector<byte> encrypt(const byte[], u32bit,
+ RandomNumberGenerator&) const = 0;
virtual ~PK_Encrypting_Key() {}
};
@@ -74,7 +75,8 @@ class BOTAN_DLL PK_Decrypting_Key : public virtual Private_Key
class BOTAN_DLL PK_Signing_Key : public virtual Private_Key
{
public:
- virtual SecureVector<byte> sign(const byte[], u32bit) const = 0;
+ virtual SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator& rng) const = 0;
virtual ~PK_Signing_Key() {}
};
diff --git a/include/rsa.h b/include/rsa.h
index e9f92db51..7ca8068f9 100644
--- a/include/rsa.h
+++ b/include/rsa.h
@@ -20,7 +20,9 @@ class BOTAN_DLL RSA_PublicKey : public PK_Encrypting_Key,
public:
std::string algo_name() const { return "RSA"; }
- SecureVector<byte> encrypt(const byte[], u32bit) const;
+ SecureVector<byte> encrypt(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
+
SecureVector<byte> verify(const byte[], u32bit) const;
RSA_PublicKey() {}
@@ -38,8 +40,10 @@ class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey,
public IF_Scheme_PrivateKey
{
public:
+ SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator&) const;
+
SecureVector<byte> decrypt(const byte[], u32bit) const;
- SecureVector<byte> sign(const byte[], u32bit) const;
bool check_key(RandomNumberGenerator& rng, bool) const;
diff --git a/include/rw.h b/include/rw.h
index a809041d1..2cc2fb6a4 100644
--- a/include/rw.h
+++ b/include/rw.h
@@ -35,7 +35,8 @@ class BOTAN_DLL RW_PrivateKey : public RW_PublicKey,
public IF_Scheme_PrivateKey
{
public:
- SecureVector<byte> sign(const byte[], u32bit) const;
+ SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
bool check_key(RandomNumberGenerator& rng, bool) const;
diff --git a/src/dsa.cpp b/src/dsa.cpp
index 1d755e045..4438ce4d5 100644
--- a/src/dsa.cpp
+++ b/src/dsa.cpp
@@ -97,13 +97,14 @@ void DSA_PrivateKey::PKCS8_load_hook(bool generated)
/*************************************************
* DSA Signature Operation *
*************************************************/
-SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit length) const
+SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit length,
+ RandomNumberGenerator& rng) const
{
const BigInt& q = group_q();
BigInt k;
do
- k.randomize(global_state().prng_reference(), q.bits());
+ k.randomize(rng, q.bits());
while(k >= q);
return core.sign(in, length, k);
diff --git a/src/elgamal.cpp b/src/elgamal.cpp
index 4389e3457..02257af03 100644
--- a/src/elgamal.cpp
+++ b/src/elgamal.cpp
@@ -33,12 +33,11 @@ void ElGamal_PublicKey::X509_load_hook()
/*************************************************
* ElGamal Encryption Function *
*************************************************/
-SecureVector<byte> ElGamal_PublicKey::encrypt(const byte in[],
- u32bit length) const
+SecureVector<byte>
+ElGamal_PublicKey::encrypt(const byte in[], u32bit length,
+ RandomNumberGenerator& rng) const
{
- BigInt k(global_state().prng_reference(),
- 2 * dl_work_factor(group_p().bits()));
-
+ BigInt k(rng, 2 * dl_work_factor(group_p().bits()));
return core.encrypt(in, length, k);
}
diff --git a/src/nr.cpp b/src/nr.cpp
index 0acbd0bb0..5b7c28f72 100644
--- a/src/nr.cpp
+++ b/src/nr.cpp
@@ -96,13 +96,14 @@ void NR_PrivateKey::PKCS8_load_hook(bool generated)
/*************************************************
* Nyberg-Rueppel Signature Operation *
*************************************************/
-SecureVector<byte> NR_PrivateKey::sign(const byte in[], u32bit length) const
+SecureVector<byte> NR_PrivateKey::sign(const byte in[], u32bit length,
+ RandomNumberGenerator& rng) const
{
const BigInt& q = group_q();
BigInt k;
do
- k.randomize(global_state().prng_reference(), q.bits());
+ k.randomize(rng, q.bits());
while(k >= q);
return core.sign(in, length, k);
diff --git a/src/pubkey.cpp b/src/pubkey.cpp
index 0a4162711..d151878c4 100644
--- a/src/pubkey.cpp
+++ b/src/pubkey.cpp
@@ -62,18 +62,18 @@ PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& k,
SecureVector<byte> PK_Encryptor_MR_with_EME::enc(const byte msg[],
u32bit length) const
{
+ RandomNumberGenerator& rng = global_state().prng_reference();
+
SecureVector<byte> message;
if(encoder)
- message = encoder->encode(msg, length,
- key.max_input_bits(),
- global_state().prng_reference());
+ message = encoder->encode(msg, length, key.max_input_bits(), rng);
else
message.set(msg, length);
if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits())
throw Exception("PK_Encryptor_MR_with_EME: Input is too large");
- return key.encrypt(message, message.size());
+ return key.encrypt(message, message.size(), rng);
}
/*************************************************
@@ -187,7 +187,8 @@ SecureVector<byte> PK_Signer::signature()
{
SecureVector<byte> encoded = emsa->encoding_of(emsa->raw_data(),
key.max_input_bits());
- SecureVector<byte> plain_sig = key.sign(encoded, encoded.size());
+ SecureVector<byte> plain_sig = key.sign(encoded, encoded.size(),
+ global_state().prng_reference());
if(key.message_parts() == 1 || sig_format == IEEE_1363)
return plain_sig;
diff --git a/src/rsa.cpp b/src/rsa.cpp
index 574eca2da..d9bf9e22b 100644
--- a/src/rsa.cpp
+++ b/src/rsa.cpp
@@ -33,7 +33,8 @@ BigInt RSA_PublicKey::public_op(const BigInt& i) const
/*************************************************
* RSA Encryption Function *
*************************************************/
-SecureVector<byte> RSA_PublicKey::encrypt(const byte in[], u32bit len) const
+SecureVector<byte> RSA_PublicKey::encrypt(const byte in[], u32bit len,
+ RandomNumberGenerator&) const
{
BigInt i(in, len);
return BigInt::encode_1363(public_op(i), n.bytes());
@@ -117,7 +118,8 @@ SecureVector<byte> RSA_PrivateKey::decrypt(const byte in[], u32bit len) const
/*************************************************
* RSA Signature Operation *
*************************************************/
-SecureVector<byte> RSA_PrivateKey::sign(const byte in[], u32bit len) const
+SecureVector<byte> RSA_PrivateKey::sign(const byte in[], u32bit len,
+ RandomNumberGenerator&) const
{
return BigInt::encode_1363(private_op(in, len), n.bytes());
}
diff --git a/src/rw.cpp b/src/rw.cpp
index 4da0cdede..2574df442 100644
--- a/src/rw.cpp
+++ b/src/rw.cpp
@@ -95,7 +95,8 @@ RW_PrivateKey::RW_PrivateKey(const BigInt& prime1, const BigInt& prime2,
/*************************************************
* Rabin-Williams Signature Operation *
*************************************************/
-SecureVector<byte> RW_PrivateKey::sign(const byte in[], u32bit len) const
+SecureVector<byte> RW_PrivateKey::sign(const byte in[], u32bit len,
+ RandomNumberGenerator&) const
{
BigInt i(in, len);
if(i >= n || i % 16 != 12)