diff options
author | lloyd <[email protected]> | 2008-06-10 16:23:59 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-06-10 16:23:59 +0000 |
commit | 2aef9fa5bc25984a838a51a93ac0e918d2d1bbac (patch) | |
tree | 9f0b9035c4549380de6c62a7bf941a9396b8f554 /src | |
parent | 7ab69d77956048fdc27f49a07724d6b21549b916 (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.
Diffstat (limited to 'src')
-rw-r--r-- | src/dsa.cpp | 5 | ||||
-rw-r--r-- | src/elgamal.cpp | 9 | ||||
-rw-r--r-- | src/nr.cpp | 5 | ||||
-rw-r--r-- | src/pubkey.cpp | 11 | ||||
-rw-r--r-- | src/rsa.cpp | 6 | ||||
-rw-r--r-- | src/rw.cpp | 3 |
6 files changed, 22 insertions, 17 deletions
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) |