From a4d586b24e65bfcdd8948bbf0eaf50eac8d367a8 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 9 Nov 2016 15:59:01 -0500 Subject: [PATCH] openssl 1.1 for ECDSA --- Crypto.h | 6 ++++++ Signature.h | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Crypto.h b/Crypto.h index efbc7165..7d9a38d7 100644 --- a/Crypto.h +++ b/Crypto.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -296,6 +297,11 @@ inline int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) inline void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) { *pr = sig->r; *ps = sig->s; } +inline int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) + { sig->r = r; sig->s = s; return 1; } +inline void ECDSA_SIG_get0(const ECDSA_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) diff --git a/Signature.h b/Signature.h index df9d8d33..7962bc2d 100644 --- a/Signature.h +++ b/Signature.h @@ -169,8 +169,9 @@ namespace crypto uint8_t digest[Hash::hashLen]; Hash::CalculateHash (buf, len, digest); ECDSA_SIG * sig = ECDSA_SIG_new(); - sig->r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); - sig->s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); + auto r = BN_bin2bn (signature, GetSignatureLen ()/2, NULL); + auto s = BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL); + ECDSA_SIG_set0(sig, r, s); // ECDSA verification int ret = ECDSA_do_verify (digest, Hash::hashLen, sig, m_PublicKey); ECDSA_SIG_free(sig); @@ -207,9 +208,11 @@ namespace crypto uint8_t digest[Hash::hashLen]; Hash::CalculateHash (buf, len, digest); ECDSA_SIG * sig = ECDSA_do_sign (digest, Hash::hashLen, m_PrivateKey); + const BIGNUM * r, * s; + ECDSA_SIG_get0 (sig, &r, &s); // signatureLen = keyLen - bn2buf (sig->r, signature, keyLen/2); - bn2buf (sig->s, signature + keyLen/2, keyLen/2); + bn2buf (r, signature, keyLen/2); + bn2buf (s, signature + keyLen/2, keyLen/2); ECDSA_SIG_free(sig); } @@ -271,7 +274,6 @@ namespace crypto RSAVerifier (const uint8_t * signingKey) { m_PublicKey = RSA_new (); - memset (m_PublicKey, 0, sizeof (RSA)); RSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, keyLen, NULL) /* n */ , BN_dup (GetRSAE ()) /* d */, NULL); } @@ -304,7 +306,6 @@ namespace crypto RSASigner (const uint8_t * signingPrivateKey) { m_PrivateKey = RSA_new (); - memset (m_PrivateKey, 0, sizeof (RSA)); RSA_set0_key (m_PrivateKey, BN_bin2bn (signingPrivateKey, keyLen, NULL), /* n */ BN_dup (GetRSAE ()) /* e */, BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL) /* d */); }