Browse Source

support openssl 1.1 for DH

pull/701/head
orignal 8 years ago
parent
commit
232d42881b
  1. 28
      Crypto.cpp
  2. 11
      Crypto.h

28
Crypto.cpp

@ -227,10 +227,8 @@ namespace crypto
DHKeys::DHKeys (): m_IsUpdated (true) DHKeys::DHKeys (): m_IsUpdated (true)
{ {
m_DH = DH_new (); m_DH = DH_new ();
m_DH->p = BN_dup (elgp); DH_set0_pqg (m_DH, BN_dup (elgp), NULL, BN_dup (elgg));
m_DH->g = BN_dup (elgg); DH_set0_key (m_DH, NULL, NULL);
m_DH->priv_key = NULL;
m_DH->pub_key = NULL;
} }
DHKeys::~DHKeys () DHKeys::~DHKeys ()
@ -240,27 +238,31 @@ namespace crypto
void DHKeys::GenerateKeys (uint8_t * priv, uint8_t * pub) void DHKeys::GenerateKeys (uint8_t * priv, uint8_t * pub)
{ {
if (m_DH->priv_key) { BN_free (m_DH->priv_key); m_DH->priv_key = NULL; }; BIGNUM * priv_key = NULL, * pub_key = NULL;
if (m_DH->pub_key) { BN_free (m_DH->pub_key); m_DH->pub_key = NULL; };
#if !defined(__x86_64__) // use short exponent for non x64 #if !defined(__x86_64__) // use short exponent for non x64
m_DH->priv_key = BN_new (); priv_key = BN_new ();
BN_rand (m_DH->priv_key, ELGAMAL_SHORT_EXPONENT_NUM_BITS, 0, 1); BN_rand (priv_key, ELGAMAL_SHORT_EXPONENT_NUM_BITS, 0, 1);
#endif #endif
if (g_ElggTable) if (g_ElggTable)
{ {
#if defined(__x86_64__) #if defined(__x86_64__)
m_DH->priv_key = BN_new (); priv_key = BN_new ();
BN_rand (m_DH->priv_key, ELGAMAL_FULL_EXPONENT_NUM_BITS, 0, 1); BN_rand (priv_key, ELGAMAL_FULL_EXPONENT_NUM_BITS, 0, 1);
#endif #endif
auto ctx = BN_CTX_new (); auto ctx = BN_CTX_new ();
m_DH->pub_key = ElggPow (m_DH->priv_key, g_ElggTable, ctx); pub_key = ElggPow (priv_key, g_ElggTable, ctx);
DH_set0_key (m_DH, pub_key, priv_key);
BN_CTX_free (ctx); BN_CTX_free (ctx);
} }
else else
{
DH_set0_key (m_DH, NULL, priv_key);
DH_generate_key (m_DH); DH_generate_key (m_DH);
DH_get0_key (m_DH, (const BIGNUM **)&pub_key, (const BIGNUM **)&priv_key);
}
if (priv) bn2buf (m_DH->priv_key, priv, 256); if (priv) bn2buf (priv_key, priv, 256);
if (pub) bn2buf (m_DH->pub_key, pub, 256); if (pub) bn2buf (pub_key, pub, 256);
m_IsUpdated = true; m_IsUpdated = true;
} }

11
Crypto.h

@ -301,6 +301,17 @@ inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
{ *n = r->n; *e = r->e; *d = r->d; } { *n = r->n; *e = r->e; *d = r->d; }
inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
{ dh->p = p; dh->q = q; dh->g = g; return 1; }
inline int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
{
if (dh->pub_key) BN_free (dh->pub_key);
if (dh->priv_key) BN_free (dh->priv_key);
dh->pub_key = pub_key; dh->priv_key = priv_key; return 1;
}
inline void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
{ *pub_key = dh->pub_key; *priv_key = dh->priv_key; }
#endif #endif
} }

Loading…
Cancel
Save