Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
fix up PRNG bits
Browse files Browse the repository at this point in the history
  • Loading branch information
majestrate committed Jan 20, 2016
1 parent d4e577b commit 502d187
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/core/crypto/CryptoPP_Impl.h
Expand Up @@ -128,7 +128,7 @@ class ECDSASigner : public Signer {
uint8_t* signature) const {
typename CryptoPP::ECDSA<CryptoPP::ECP, Hash>::Signer
signer(m_PrivateKey);
PRNG& rnd = GetPRNG();
PRNG rnd;
signer.SignMessage(rnd, buf, len, signature);
}

Expand All @@ -147,7 +147,7 @@ inline void CreateECDSARandomKeys(
privateKey;
typename CryptoPP::ECDSA<CryptoPP::ECP, Hash>::PublicKey
publicKey;
i2p::crypto::PRNG& rnd = i2p::crypto::GetPRNG();
PRNG rnd;
privateKey.Initialize(rnd, curve);
privateKey.MakePublicKey(publicKey);
privateKey.GetPrivateExponent().Encode(signingPrivateKey, keyLen / 2);
Expand Down Expand Up @@ -266,7 +266,7 @@ class RSASigner {
const uint8_t* buf,
size_t len,
uint8_t* signature) const {
PRNG& rnd = GetPRNG();
PRNG rnd;
typename CryptoPP::RSASS<CryptoPP::PKCS1v15, Hash>::Signer
signer(m_PrivateKey);
signer.SignMessage(rnd, buf, len, signature);
Expand Down
3 changes: 1 addition & 2 deletions src/core/crypto/CryptoPP_Rand.h
Expand Up @@ -38,8 +38,7 @@
namespace i2p {
namespace crypto {

typedef CryptoPP::RandomNumberGenerator PRNG;
PRNG& GetPRNG();
typedef CryptoPP::AutoSeededRandomPool PRNG;

} // namespace crypto
} // namespace i2p
Expand Down
4 changes: 2 additions & 2 deletions src/core/crypto/ElGamal.cpp
Expand Up @@ -78,7 +78,7 @@ void ElGamalEncryption::Encrypt(

ElGamalEncryption_Pimpl::ElGamalEncryption_Pimpl(
const uint8_t* key) {
PRNG & rnd = GetPRNG();
PRNG rnd;
CryptoPP::Integer y(key, 256),
k(rnd, CryptoPP::Integer::One(), elgp-1);
a = a_exp_b_mod_c(elgg, k, elgp);
Expand Down Expand Up @@ -133,7 +133,7 @@ void GenerateElGamalKeyPair(
RandBytes(priv, 256);
a_exp_b_mod_c(elgg, CryptoPP::Integer(priv, 256), elgp).Encode(pub, 256);
#else
PRNG & rnd = GetPRNG();
PRNG rnd;
CryptoPP::DH dh(elgp, elgg);
dh.GenerateKeyPair(rnd, priv, pub);
#endif
Expand Down
14 changes: 4 additions & 10 deletions src/core/crypto/Rand.cpp
Expand Up @@ -31,24 +31,18 @@
#include "Rand.h"

#include "CryptoPP_Rand.h"
#include <cryptopp/osrng.h>

// implementation of i2p::crypto::Rand* functions

namespace i2p {
namespace crypto {

static CryptoPP::AutoSeededRandomPool rnd;

PRNG & GetPRNG() {
return rnd;
}

void RandBytes(
void* dataptr,
uint8_t* dataptr,
size_t datalen) {
rnd.GenerateBlock((uint8_t *)dataptr, datalen);
PRNG r;
r.GenerateBlock(dataptr, datalen);
}

} // namespace crypto
} // namespace i2p
6 changes: 3 additions & 3 deletions src/core/crypto/Rand.h
Expand Up @@ -40,15 +40,15 @@ namespace crypto {
// @param dataptr buffer to store result
// @param datalen size of buffer
void RandBytes(
void* dataptr,
uint8_t* dataptr,
size_t datalen);

// generate random of type T
// @param T integer or floating point type
template<class T>
T Rand() {
T ret;
RandBytes(&ret, sizeof(ret));
//TODO(psi): alignment
RandBytes((uint8_t*)&ret, sizeof(ret));
return ret;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/crypto/Signature.cpp
Expand Up @@ -88,7 +88,7 @@ void DSASigner_Pimpl::Sign(
const uint8_t* buf,
size_t len,
uint8_t* signature) const {
i2p::crypto::PRNG& rnd = i2p::crypto::GetPRNG();
PRNG rnd;
CryptoPP::DSA::Signer signer(m_PrivateKey);
signer.SignMessage(rnd, buf, len, signature);
}
Expand Down Expand Up @@ -268,7 +268,7 @@ void CreateRSARandomKeys(
uint8_t* signingPrivateKey,
uint8_t* signingPublicKey) {
CryptoPP::RSA::PrivateKey privateKey;
PRNG & rnd = GetPRNG();
PRNG rnd;
privateKey.Initialize(
rnd,
publicKeyLen * 8,
Expand Down
3 changes: 1 addition & 2 deletions src/tests/unit_tests/Crypto.cpp
Expand Up @@ -218,8 +218,7 @@ BOOST_FIXTURE_TEST_CASE(AesCbcDecrypt, AesCbcFixture) {
struct EDDSAFixture {
EDDSAFixture()
: verifier(public_key),
signer(private_key),
dummy_rng() {}
signer(private_key) {}

uint8_t private_key[32] = {
0xe1, 0xec, 0xff, 0xa6, 0xcd, 0x4e, 0xc7, 0x09, 0x2f, 0x87,
Expand Down

0 comments on commit 502d187

Please sign in to comment.