Browse Source

call CryptoConstants from Crypto.cpp only

pull/308/head
orignal 9 years ago
parent
commit
a994bbc36b
  1. 65
      Crypto.cpp
  2. 52
      Crypto.h
  3. 4
      I2PControl.cpp
  4. 2
      Reseed.cpp
  5. 27
      Signature.h

65
Crypto.cpp

@ -66,7 +66,44 @@ namespace crypto
const int rsae_ = 65537; const int rsae_ = 65537;
const CryptoConstants& GetCryptoConstants () struct CryptoConstants
{
// DH/ElGamal
BIGNUM * elgp;
BIGNUM * elgg;
// DSA
BIGNUM * dsap;
BIGNUM * dsaq;
BIGNUM * dsag;
// RSA
BIGNUM * rsae;
CryptoConstants (const uint8_t * elgp_, int elgg_, const uint8_t * dsap_,
const uint8_t * dsaq_, const uint8_t * dsag_, int rsae_)
{
elgp = BN_new ();
BN_bin2bn (elgp_, 256, elgp);
elgg = BN_new ();
BN_set_word (elgg, elgg_);
dsap = BN_new ();
BN_bin2bn (dsap_, 128, dsap);
dsaq = BN_new ();
BN_bin2bn (dsaq_, 20, dsaq);
dsag = BN_new ();
BN_bin2bn (dsag_, 128, dsag);
rsae = BN_new ();
BN_set_word (rsae, rsae_);
}
~CryptoConstants ()
{
BN_free (elgp); BN_free (elgg); BN_free (dsap); BN_free (dsaq); BN_free (dsag); BN_free (rsae);
}
};
static const CryptoConstants& GetCryptoConstants ()
{ {
static CryptoConstants cryptoConstants (elgp_, elgg_, dsap_, dsaq_, dsag_, rsae_); static CryptoConstants cryptoConstants (elgp_, elgg_, dsap_, dsaq_, dsag_, rsae_);
return cryptoConstants; return cryptoConstants;
@ -81,6 +118,32 @@ namespace crypto
return true; return true;
} }
// RSA
#define rsae GetCryptoConstants ().rsae
const BIGNUM * GetRSAE ()
{
return rsae;
}
// DSA
#define dsap GetCryptoConstants ().dsap
#define dsaq GetCryptoConstants ().dsaq
#define dsag GetCryptoConstants ().dsag
DSA * CreateDSA ()
{
DSA * dsa = DSA_new ();
dsa->p = BN_dup (dsap);
dsa->q = BN_dup (dsaq);
dsa->g = BN_dup (dsag);
dsa->priv_key = NULL;
dsa->pub_key = NULL;
return dsa;
}
// DH/ElGamal
#define elgp GetCryptoConstants ().elgp
#define elgg GetCryptoConstants ().elgg
// DH // DH
DHKeys::DHKeys (): m_IsUpdated (true) DHKeys::DHKeys (): m_IsUpdated (true)

52
Crypto.h

@ -6,64 +6,20 @@
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/dh.h> #include <openssl/dh.h>
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/dsa.h>
#include "Base.h" #include "Base.h"
namespace i2p namespace i2p
{ {
namespace crypto namespace crypto
{ {
struct CryptoConstants bool bn2buf (const BIGNUM * bn, uint8_t * buf, size_t len);
{
// DH/ElGamal
BIGNUM * elgp;
BIGNUM * elgg;
// DSA
BIGNUM * dsap;
BIGNUM * dsaq;
BIGNUM * dsag;
// RSA
BIGNUM * rsae;
CryptoConstants (const uint8_t * elgp_, int elgg_, const uint8_t * dsap_,
const uint8_t * dsaq_, const uint8_t * dsag_, int rsae_)
{
elgp = BN_new ();
BN_bin2bn (elgp_, 256, elgp);
elgg = BN_new ();
BN_set_word (elgg, elgg_);
dsap = BN_new ();
BN_bin2bn (dsap_, 128, dsap);
dsaq = BN_new ();
BN_bin2bn (dsaq_, 20, dsaq);
dsag = BN_new ();
BN_bin2bn (dsag_, 128, dsag);
rsae = BN_new ();
BN_set_word (rsae, rsae_);
}
~CryptoConstants ()
{
BN_free (elgp); BN_free (elgg); BN_free (dsap); BN_free (dsaq); BN_free (dsag); BN_free (rsae);
}
};
const CryptoConstants& GetCryptoConstants ();
// DH/ElGamal
#define elgp GetCryptoConstants ().elgp
#define elgg GetCryptoConstants ().elgg
// DSA // DSA
#define dsap GetCryptoConstants ().dsap DSA * CreateDSA ();
#define dsaq GetCryptoConstants ().dsaq
#define dsag GetCryptoConstants ().dsag
// RSA // RSA
#define rsae GetCryptoConstants ().rsae const BIGNUM * GetRSAE ();
bool bn2buf (const BIGNUM * bn, uint8_t * buf, size_t len);
// DH // DH
class DHKeys class DHKeys

4
I2PControl.cpp

@ -525,7 +525,9 @@ namespace client
{ {
EVP_PKEY * pkey = EVP_PKEY_new (); EVP_PKEY * pkey = EVP_PKEY_new ();
RSA * rsa = RSA_new (); RSA * rsa = RSA_new ();
RSA_generate_key_ex (rsa, 4096, i2p::crypto::rsae, NULL); BIGNUM * e = BN_dup (i2p::crypto::GetRSAE ());
RSA_generate_key_ex (rsa, 4096, e, NULL);
BN_free (e);
if (rsa) if (rsa)
{ {
EVP_PKEY_assign_RSA (pkey, rsa); EVP_PKEY_assign_RSA (pkey, rsa);

2
Reseed.cpp

@ -156,7 +156,7 @@ namespace data
BIGNUM * s = BN_new (), * n = BN_new (); BIGNUM * s = BN_new (), * n = BN_new ();
BN_bin2bn (signature, signatureLength, s); BN_bin2bn (signature, signatureLength, s);
BN_bin2bn (it->second, i2p::crypto::RSASHA5124096_KEY_LENGTH, n); BN_bin2bn (it->second, i2p::crypto::RSASHA5124096_KEY_LENGTH, n);
BN_mod_exp (s, s, i2p::crypto::rsae, n, bnctx); // s = s^e mod n BN_mod_exp (s, s, i2p::crypto::GetRSAE (), n, bnctx); // s = s^e mod n
uint8_t * enSigBuf = new uint8_t[signatureLength]; uint8_t * enSigBuf = new uint8_t[signatureLength];
i2p::crypto::bn2buf (s, enSigBuf, signatureLength); i2p::crypto::bn2buf (s, enSigBuf, signatureLength);
// digest is right aligned // digest is right aligned

27
Signature.h

@ -44,11 +44,7 @@ namespace crypto
DSAVerifier (const uint8_t * signingKey) DSAVerifier (const uint8_t * signingKey)
{ {
m_PublicKey = DSA_new (); m_PublicKey = CreateDSA ();
m_PublicKey->p = BN_dup (dsap);
m_PublicKey->q = BN_dup (dsaq);
m_PublicKey->g = BN_dup (dsag);
m_PublicKey->priv_key = NULL;
m_PublicKey->pub_key = BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL); m_PublicKey->pub_key = BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL);
} }
@ -86,12 +82,8 @@ namespace crypto
DSASigner (const uint8_t * signingPrivateKey) DSASigner (const uint8_t * signingPrivateKey)
{ {
m_PrivateKey = DSA_new (); m_PrivateKey = CreateDSA ();
m_PrivateKey->p = BN_dup (dsap);
m_PrivateKey->q = BN_dup (dsaq);
m_PrivateKey->g = BN_dup (dsag);
m_PrivateKey->priv_key = BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL); m_PrivateKey->priv_key = BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL);
m_PrivateKey->pub_key = NULL;
} }
~DSASigner () ~DSASigner ()
@ -116,12 +108,7 @@ namespace crypto
inline void CreateDSARandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) inline void CreateDSARandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
{ {
DSA * dsa = DSA_new (); DSA * dsa = CreateDSA ();
dsa->p = BN_dup (dsap);
dsa->q = BN_dup (dsaq);
dsa->g = BN_dup (dsag);
dsa->priv_key = NULL;
dsa->pub_key = NULL;
DSA_generate_key (dsa); DSA_generate_key (dsa);
bn2buf (dsa->priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH); bn2buf (dsa->priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH);
bn2buf (dsa->pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH); bn2buf (dsa->pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH);
@ -285,7 +272,7 @@ namespace crypto
{ {
m_PublicKey = RSA_new (); m_PublicKey = RSA_new ();
memset (m_PublicKey, 0, sizeof (RSA)); memset (m_PublicKey, 0, sizeof (RSA));
m_PublicKey->e = BN_dup (rsae); m_PublicKey->e = BN_dup (GetRSAE ());
m_PublicKey->n = BN_bin2bn (signingKey, keyLen, NULL); m_PublicKey->n = BN_bin2bn (signingKey, keyLen, NULL);
} }
@ -319,7 +306,7 @@ namespace crypto
{ {
m_PrivateKey = RSA_new (); m_PrivateKey = RSA_new ();
memset (m_PrivateKey, 0, sizeof (RSA)); memset (m_PrivateKey, 0, sizeof (RSA));
m_PrivateKey->e = BN_dup (rsae); m_PrivateKey->e = BN_dup (GetRSAE ());
m_PrivateKey->n = BN_bin2bn (signingPrivateKey, keyLen, NULL); m_PrivateKey->n = BN_bin2bn (signingPrivateKey, keyLen, NULL);
m_PrivateKey->d = BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL); m_PrivateKey->d = BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL);
} }
@ -345,10 +332,12 @@ namespace crypto
inline void CreateRSARandomKeys (size_t publicKeyLen, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) inline void CreateRSARandomKeys (size_t publicKeyLen, uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
{ {
RSA * rsa = RSA_new (); RSA * rsa = RSA_new ();
RSA_generate_key_ex (rsa, publicKeyLen*8, rsae, NULL); BIGNUM * e = BN_dup (GetRSAE ()); // make it non-const
RSA_generate_key_ex (rsa, publicKeyLen*8, e, NULL);
bn2buf (rsa->n, signingPrivateKey, publicKeyLen); bn2buf (rsa->n, signingPrivateKey, publicKeyLen);
bn2buf (rsa->d, signingPrivateKey + publicKeyLen, publicKeyLen); bn2buf (rsa->d, signingPrivateKey + publicKeyLen, publicKeyLen);
bn2buf (rsa->n, signingPublicKey, publicKeyLen); bn2buf (rsa->n, signingPublicKey, publicKeyLen);
BN_free (e); // this e is not assigned to rsa->e
RSA_free (rsa); RSA_free (rsa);
} }

Loading…
Cancel
Save