Browse Source

preparation for openssl 1.1

pull/700/head
orignal 8 years ago
parent
commit
9ba9bd4415
  1. 20
      Crypto.h
  2. 36
      Signature.h

20
Crypto.h

@ -7,6 +7,7 @@
#include <openssl/dh.h> #include <openssl/dh.h>
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/dsa.h> #include <openssl/dsa.h>
#include <openssl/rsa.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/rand.h> #include <openssl/rand.h>
@ -282,10 +283,23 @@ namespace crypto
// take care about openssl version // take care about openssl version
#include <openssl/opensslv.h> #include <openssl/opensslv.h>
#if (OPENSSL_VERSION_NUMBER < 0x010100000) || defined(LIBRESSL_VERSION_NUMBER) // 1.1.0 or LibreSSL #if !(OPENSSL_VERSION_NUMBER >= 0x010100000) // < 1.1.0 or non-OpenSSL
// define getters and setters introduced in 1.1.0 // define getters and setters introduced in 1.1.0
inline int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) { d->p = p; d->q = q; d->g = g; return 1; } inline int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
inline int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) { d->pub_key = pub_key; d->priv_key = priv_key; return 1; } { d->p = p; d->q = q; d->g = g; return 1; }
inline int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
{ d->pub_key = pub_key; d->priv_key = priv_key; return 1; }
inline void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
{ *pub_key = d->pub_key; *priv_key = d->priv_key; }
inline int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
{ sig->r = r; sig->s = s; return 1; }
inline void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
{ *pr = sig->r; *ps = sig->s; }
inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
{ r->n = n; r->e = e; r->d = d; return 1; }
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; }
#endif #endif

36
Signature.h

@ -43,7 +43,7 @@ namespace crypto
DSAVerifier (const uint8_t * signingKey) DSAVerifier (const uint8_t * signingKey)
{ {
m_PublicKey = CreateDSA (); m_PublicKey = CreateDSA ();
m_PublicKey->pub_key = BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL); DSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL), NULL);
} }
~DSAVerifier () ~DSAVerifier ()
@ -58,8 +58,7 @@ namespace crypto
SHA1 (buf, len, digest); SHA1 (buf, len, digest);
// signature // signature
DSA_SIG * sig = DSA_SIG_new(); DSA_SIG * sig = DSA_SIG_new();
sig->r = BN_bin2bn (signature, DSA_SIGNATURE_LENGTH/2, NULL); DSA_SIG_set0 (sig, BN_bin2bn (signature, DSA_SIGNATURE_LENGTH/2, NULL), BN_bin2bn (signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2, NULL));
sig->s = BN_bin2bn (signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2, NULL);
// DSA verification // DSA verification
int ret = DSA_do_verify (digest, 20, sig, m_PublicKey); int ret = DSA_do_verify (digest, 20, sig, m_PublicKey);
DSA_SIG_free(sig); DSA_SIG_free(sig);
@ -81,7 +80,7 @@ namespace crypto
DSASigner (const uint8_t * signingPrivateKey) DSASigner (const uint8_t * signingPrivateKey)
{ {
m_PrivateKey = CreateDSA (); m_PrivateKey = CreateDSA ();
m_PrivateKey->priv_key = BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL); DSA_set0_key (m_PrivateKey, NULL, BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL));
} }
~DSASigner () ~DSASigner ()
@ -94,8 +93,10 @@ namespace crypto
uint8_t digest[20]; uint8_t digest[20];
SHA1 (buf, len, digest); SHA1 (buf, len, digest);
DSA_SIG * sig = DSA_do_sign (digest, 20, m_PrivateKey); DSA_SIG * sig = DSA_do_sign (digest, 20, m_PrivateKey);
bn2buf (sig->r, signature, DSA_SIGNATURE_LENGTH/2); const BIGNUM * r, * s;
bn2buf (sig->s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2); DSA_SIG_get0 (sig, &r, &s);
bn2buf (r, signature, DSA_SIGNATURE_LENGTH/2);
bn2buf (s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2);
DSA_SIG_free(sig); DSA_SIG_free(sig);
} }
@ -108,10 +109,11 @@ namespace crypto
{ {
DSA * dsa = CreateDSA (); DSA * dsa = CreateDSA ();
DSA_generate_key (dsa); DSA_generate_key (dsa);
bn2buf (dsa->priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH); const BIGNUM * pub_key, * priv_key;
bn2buf (dsa->pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH); DSA_get0_key(dsa, &pub_key, &priv_key);
bn2buf (priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH);
bn2buf (pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH);
DSA_free (dsa); DSA_free (dsa);
} }
struct SHA256Hash struct SHA256Hash
@ -270,8 +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 (GetRSAE ()); RSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, keyLen, NULL) /* n */ , BN_dup (GetRSAE ()) /* d */, NULL);
m_PublicKey->n = BN_bin2bn (signingKey, keyLen, NULL);
} }
~RSAVerifier () ~RSAVerifier ()
@ -304,9 +305,8 @@ 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 (GetRSAE ()); RSA_set0_key (m_PrivateKey, BN_bin2bn (signingPrivateKey, keyLen, NULL), /* n */
m_PrivateKey->n = BN_bin2bn (signingPrivateKey, keyLen, NULL); BN_dup (GetRSAE ()) /* e */, BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL) /* d */);
m_PrivateKey->d = BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL);
} }
~RSASigner () ~RSASigner ()
@ -332,9 +332,11 @@ namespace crypto
RSA * rsa = RSA_new (); RSA * rsa = RSA_new ();
BIGNUM * e = BN_dup (GetRSAE ()); // make it non-const BIGNUM * e = BN_dup (GetRSAE ()); // make it non-const
RSA_generate_key_ex (rsa, publicKeyLen*8, e, NULL); RSA_generate_key_ex (rsa, publicKeyLen*8, e, NULL);
bn2buf (rsa->n, signingPrivateKey, publicKeyLen); const BIGNUM * n, * d, * e1;
bn2buf (rsa->d, signingPrivateKey + publicKeyLen, publicKeyLen); RSA_get0_key (rsa, &n, &e1, &d);
bn2buf (rsa->n, signingPublicKey, publicKeyLen); bn2buf (n, signingPrivateKey, publicKeyLen);
bn2buf (d, signingPrivateKey + publicKeyLen, publicKeyLen);
bn2buf (n, signingPublicKey, publicKeyLen);
BN_free (e); // this e is not assigned to rsa->e BN_free (e); // this e is not assigned to rsa->e
RSA_free (rsa); RSA_free (rsa);
} }

Loading…
Cancel
Save