From d9a9ab764d542fe29e177ff600eff108269d0a57 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sat, 27 Nov 2021 11:48:54 +0300 Subject: [PATCH] OpenSSL API hacks --- src/base58.cpp | 4 +- src/bignum.h | 179 ++++++++++++++++++++++++++++++------------------------ src/crypter.cpp | 24 ++++---- src/key.cpp | 63 +++++++++++++------ 4 files changed, 156 insertions(+), 114 deletions(-) diff --git a/src/base58.cpp b/src/base58.cpp index dc813ab..f81ebf0 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -48,7 +48,7 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) CBigNum rem; while (bn > bn0) { - if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) + if (!BN_div(dv.get(), rem.get(), bn.get(), bn58.get(), pctx)) throw bignum_error("EncodeBase58 : BN_div failed"); bn = dv; unsigned int c = rem.getuint32(); @@ -95,7 +95,7 @@ bool DecodeBase58(const char* psz, std::vector& vchRet) break; } bnChar.setuint32((uint32_t)(p1 - pszBase58)); - if (!BN_mul(&bn, &bn, &bn58, pctx)) + if (!BN_mul(bn.get(), bn.get(), bn58.get(), pctx)) throw bignum_error("DecodeBase58 : BN_mul failed"); bn += bnChar; } diff --git a/src/bignum.h b/src/bignum.h index 4bda99d..f1ec8d4 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -47,50 +47,52 @@ public: /** C++ wrapper for BIGNUM (OpenSSL bignum) */ -class CBigNum : public BIGNUM +class CBigNum { +private: + BIGNUM* bn; public: CBigNum() { - BN_init(this); + bn = BN_new(); } CBigNum(const CBigNum& b) { - BN_init(this); - if (!BN_copy(this, &b)) + bn = BN_new(); + if (!BN_copy(bn, b.bn)) { - BN_clear_free(this); + BN_clear_free(bn); throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); } } CBigNum& operator=(const CBigNum& b) { - if (!BN_copy(this, &b)) + if (!BN_copy(bn, b.bn)) throw bignum_error("CBigNum::operator= : BN_copy failed"); return (*this); } ~CBigNum() { - BN_clear_free(this); + BN_clear_free(bn); } - CBigNum(int8_t n) { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); } - CBigNum(int16_t n) { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); } - CBigNum(int32_t n) { BN_init(this); if (n >= 0) setuint32(n); else setint64(n); } - CBigNum(int64_t n) { BN_init(this); if (n >= 0) setuint64(n); else setint64(n); } + CBigNum(int8_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int16_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int32_t n) { bn = BN_new(); if (n >= 0) setuint32(n); else setint64(n); } + CBigNum(int64_t n) { bn = BN_new(); if (n >= 0) setuint64(n); else setint64(n); } - CBigNum(uint8_t n) { BN_init(this); setuint32(n); } - CBigNum(uint16_t n) { BN_init(this); setuint32(n); } - CBigNum(uint32_t n) { BN_init(this); setuint32(n); } - CBigNum(uint64_t n) { BN_init(this); setuint64(n); } + CBigNum(uint8_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint16_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint32_t n) { bn = BN_new(); setuint32(n); } + CBigNum(uint64_t n) { bn = BN_new(); setuint64(n); } - explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } + explicit CBigNum(uint256 n) { bn = BN_new(); setuint256(n); } explicit CBigNum(const std::vector& vch) { - BN_init(this); + bn = BN_new(); setvch(vch); } @@ -101,7 +103,7 @@ public: */ static CBigNum randBignum(const CBigNum& range) { CBigNum ret; - if(!BN_rand_range(&ret, &range)){ + if(!BN_rand_range(ret.bn, range.bn)){ throw bignum_error("CBigNum:rand element : BN_rand_range failed"); } return ret; @@ -113,7 +115,7 @@ public: */ static CBigNum RandKBitBigum(const uint32_t k){ CBigNum ret; - if(!BN_rand(&ret, k, -1, 0)){ + if(!BN_rand(ret.bn, k, -1, 0)){ throw bignum_error("CBigNum:rand element : BN_rand failed"); } return ret; @@ -124,25 +126,25 @@ public: * @return the size */ int bitSize() const{ - return BN_num_bits(this); + return BN_num_bits(bn); } void setuint32(uint32_t n) { - if (!BN_set_word(this, n)) + if (!BN_set_word(bn, n)) throw bignum_error("CBigNum conversion from uint32_t : BN_set_word failed"); } uint32_t getuint32() const { - return BN_get_word(this); + return BN_get_word(bn); } int32_t getint32() const { - uint64_t n = BN_get_word(this); - if (!BN_is_negative(this)) + uint64_t n = BN_get_word(bn); + if (!BN_is_negative(bn)) return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::max() : (int32_t)n); else return (n > (uint64_t)std::numeric_limits::max() ? std::numeric_limits::min() : -(int32_t)n); @@ -188,16 +190,16 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize) & 0xff; - BN_mpi2bn(pch, (int)(p - pch), this); + BN_mpi2bn(pch, (int)(p - pch), bn); } uint64_t getuint64() { - size_t nSize = BN_bn2mpi(this, NULL); + size_t nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); if (vch.size() > 4) vch[4] &= 0x7f; uint64_t n = 0; @@ -214,7 +216,7 @@ public: // Use BN_set_word if word size is sufficient for uint64_t if (check(sizeof(n) <= sizeof(BN_ULONG))) { - if (!BN_set_word(this, (BN_ULONG)n)) + if (!BN_set_word(bn, (BN_ULONG)n)) throw bignum_error("CBigNum conversion from uint64_t : BN_set_word failed"); return; } @@ -241,7 +243,7 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize) & 0xff; - BN_mpi2bn(pch, (int)(p - pch), this); + BN_mpi2bn(pch, (int)(p - pch), bn); } void setuint160(uint160 n) @@ -269,16 +271,16 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize >> 0) & 0xff; - BN_mpi2bn(pch, (int) (p - pch), this); + BN_mpi2bn(pch, (int) (p - pch), bn); } uint160 getuint160() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); if (vch.size() > 4) vch[4] &= 0x7f; uint160 n = 0; @@ -312,16 +314,16 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize >> 0) & 0xff; - BN_mpi2bn(pch, (int) (p - pch), this); + BN_mpi2bn(pch, (int) (p - pch), bn); } uint256 getuint256() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize < 4) return 0; std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); if (vch.size() > 4) vch[4] &= 0x7f; uint256 n = 0; @@ -332,16 +334,16 @@ public: void setBytes(const std::vector& vchBytes) { - BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), this); + BN_bin2bn(&vchBytes[0], (int) vchBytes.size(), bn); } std::vector getBytes() const { - int nBytes = BN_num_bytes(this); + int nBytes = BN_num_bytes(bn); std::vector vchBytes(nBytes); - int n = BN_bn2bin(this, &vchBytes[0]); + int n = BN_bn2bin(bn, &vchBytes[0]); if (n != nBytes) { throw bignum_error("CBigNum::getBytes : BN_bn2bin failed"); } @@ -361,16 +363,16 @@ public: vch2[3] = (nSize >> 0) & 0xff; // swap data to big endian reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4); - BN_mpi2bn(&vch2[0], (int) vch2.size(), this); + BN_mpi2bn(&vch2[0], (int) vch2.size(), bn); } std::vector getvch() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize <= 4) return std::vector(); std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); vch.erase(vch.begin(), vch.begin() + 4); reverse(vch.begin(), vch.end()); return vch; @@ -384,16 +386,16 @@ public: if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff; if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff; if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff; - BN_mpi2bn(&vch[0], (int) vch.size(), this); + BN_mpi2bn(&vch[0], (int) vch.size(), bn); return *this; } uint32_t GetCompact() const { - uint32_t nSize = BN_bn2mpi(this, NULL); + uint32_t nSize = BN_bn2mpi(bn, NULL); std::vector vch(nSize); nSize -= 4; - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); uint32_t nCompact = nSize << 24; if (nSize >= 1) nCompact |= (vch[4] << 16); if (nSize >= 2) nCompact |= (vch[5] << 8); @@ -438,20 +440,20 @@ public: CBigNum bn0 = 0; std::string str; CBigNum bn = *this; - BN_set_negative(&bn, false); + BN_set_negative(bn.bn, false); CBigNum dv; CBigNum rem; - if (BN_cmp(&bn, &bn0) == 0) + if (BN_cmp(bn.bn, bn0.bn) == 0) return "0"; - while (BN_cmp(&bn, &bn0) > 0) + while (BN_cmp(bn.bn, bn0.bn) > 0) { - if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) + if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx)) throw bignum_error("CBigNum::ToString() : BN_div failed"); bn = dv; uint32_t c = rem.getuint32(); str += "0123456789abcdef"[c]; } - if (BN_is_negative(this)) + if (BN_is_negative(bn.bn)) str += "-"; reverse(str.begin(), str.end()); return str; @@ -462,6 +464,10 @@ public: return ToString(16); } + BIGNUM* get() const { + return bn; + } + unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const { return ::GetSerializeSize(getvch(), nType, nVersion); @@ -498,7 +504,7 @@ public: CBigNum pow(const CBigNum& e) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_exp(&ret, this, &e, pctx)) + if (!BN_exp(ret.bn, bn, e.bn, pctx)) throw bignum_error("CBigNum::pow : BN_exp failed"); return ret; } @@ -511,7 +517,7 @@ public: CBigNum mul_mod(const CBigNum& b, const CBigNum& m) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_mod_mul(&ret, this, &b, &m, pctx)) + if (!BN_mod_mul(ret.bn, bn, b.bn, m.bn, pctx)) throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed"); return ret; @@ -529,10 +535,10 @@ public: // g^-x = (g^-1)^x CBigNum inv = this->inverse(m); CBigNum posE = e * -1; - if (!BN_mod_exp(&ret, &inv, &posE, &m, pctx)) + if (!BN_mod_exp(ret.bn, inv.bn, posE.bn, m.bn, pctx)) throw bignum_error("CBigNum::pow_mod: BN_mod_exp failed on negative exponent"); }else - if (!BN_mod_exp(&ret, this, &e, &m, pctx)) + if (!BN_mod_exp(ret.bn, bn, e.bn, m.bn, pctx)) throw bignum_error("CBigNum::pow_mod : BN_mod_exp failed"); return ret; @@ -547,7 +553,7 @@ public: CBigNum inverse(const CBigNum& m) const { CAutoBN_CTX pctx; CBigNum ret; - if (!BN_mod_inverse(&ret, this, &m, pctx)) + if (!BN_mod_inverse(ret.bn, bn, m.bn, pctx)) throw bignum_error("CBigNum::inverse*= :BN_mod_inverse"); return ret; } @@ -560,7 +566,7 @@ public: */ static CBigNum generatePrime(const unsigned int numBits, bool safe = false) { CBigNum ret; - if(!BN_generate_prime_ex(&ret, numBits, (safe == true), NULL, NULL, NULL)) + if(!BN_generate_prime_ex(ret.bn, numBits, (safe == true), NULL, NULL, NULL)) throw bignum_error("CBigNum::generatePrime*= :BN_generate_prime_ex"); return ret; } @@ -573,7 +579,7 @@ public: CBigNum gcd( const CBigNum& b) const{ CAutoBN_CTX pctx; CBigNum ret; - if (!BN_gcd(&ret, this, &b, pctx)) + if (!BN_gcd(ret.bn, bn, b.bn, pctx)) throw bignum_error("CBigNum::gcd*= :BN_gcd"); return ret; } @@ -586,7 +592,7 @@ public: */ bool isPrime(const int checks=BN_prime_checks) const { CAutoBN_CTX pctx; - int ret = BN_is_prime(this, checks, NULL, pctx, NULL); + int ret = BN_is_prime(bn, checks, NULL, pctx, NULL); if(ret < 0){ throw bignum_error("CBigNum::isPrime :BN_is_prime"); } @@ -594,18 +600,18 @@ public: } bool isOne() const { - return BN_is_one(this); + return BN_is_one(bn); } bool operator!() const { - return BN_is_zero(this); + return BN_is_zero(bn); } CBigNum& operator+=(const CBigNum& b) { - if (!BN_add(this, this, &b)) + if (!BN_add(bn, bn, b.bn)) throw bignum_error("CBigNum::operator+= : BN_add failed"); return *this; } @@ -619,7 +625,7 @@ public: CBigNum& operator*=(const CBigNum& b) { CAutoBN_CTX pctx; - if (!BN_mul(this, this, &b, pctx)) + if (!BN_mul(bn, bn, b.bn, pctx)) throw bignum_error("CBigNum::operator*= : BN_mul failed"); return *this; } @@ -638,7 +644,7 @@ public: CBigNum& operator<<=(unsigned int shift) { - if (!BN_lshift(this, this, shift)) + if (!BN_lshift(bn, bn, shift)) throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); return *this; } @@ -649,13 +655,13 @@ public: // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL CBigNum a = 1; a <<= shift; - if (BN_cmp(&a, this) > 0) + if (BN_cmp(a.bn, bn) > 0) { *this = 0; return *this; } - if (!BN_rshift(this, this, shift)) + if (!BN_rshift(bn, bn, shift)) throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); return *this; } @@ -664,7 +670,7 @@ public: CBigNum& operator++() { // prefix operator - if (!BN_add(this, this, BN_value_one())) + if (!BN_add(bn, bn, BN_value_one())) throw bignum_error("CBigNum::operator++ : BN_add failed"); return *this; } @@ -681,7 +687,7 @@ public: { // prefix operator CBigNum r; - if (!BN_sub(&r, this, BN_value_one())) + if (!BN_sub(r.bn, bn, BN_value_one())) throw bignum_error("CBigNum::operator-- : BN_sub failed"); *this = r; return *this; @@ -695,12 +701,25 @@ public: return ret; } - friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b); friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); friend inline const CBigNum operator*(const CBigNum& a, const CBigNum& b); friend inline bool operator<(const CBigNum& a, const CBigNum& b); + friend inline const CBigNum operator+(const CBigNum& a, const CBigNum& b); + friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); + friend inline const CBigNum operator*(const CBigNum& a); + + friend inline const CBigNum operator-(const CBigNum& a); + friend inline const CBigNum operator<<(const CBigNum& a, unsigned int shift); + + friend inline bool operator==(const CBigNum& a, const CBigNum& b); + friend inline bool operator!=(const CBigNum& a, const CBigNum& b); + friend inline bool operator<=(const CBigNum& a, const CBigNum& b); + friend inline bool operator>=(const CBigNum& a, const CBigNum& b); + friend inline bool operator<(const CBigNum& a, const CBigNum& b); + friend inline bool operator>(const CBigNum& a, const CBigNum& b); + friend inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b); }; @@ -708,7 +727,7 @@ public: inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_add(&r, &a, &b)) + if (!BN_add(r.bn, a.bn, b.bn)) throw bignum_error("CBigNum::operator+ : BN_add failed"); return r; } @@ -716,7 +735,7 @@ inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_sub(&r, &a, &b)) + if (!BN_sub(r.bn, a.bn, b.bn)) throw bignum_error("CBigNum::operator- : BN_sub failed"); return r; } @@ -724,7 +743,7 @@ inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a) { CBigNum r(a); - BN_set_negative(&r, !BN_is_negative(&r)); + BN_set_negative(r.bn, !BN_is_negative(r.bn)); return r; } @@ -732,7 +751,7 @@ inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_mul(&r, &a, &b, pctx)) + if (!BN_mul(r.bn, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator* : BN_mul failed"); return r; } @@ -741,7 +760,7 @@ inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_div(&r, NULL, &a, &b, pctx)) + if (!BN_div(r.bn, NULL, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator/ : BN_div failed"); return r; } @@ -750,7 +769,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_nnmod(&r, &a, &b, pctx)) + if (!BN_nnmod(r.bn, a.bn, b.bn, pctx)) throw bignum_error("CBigNum::operator% : BN_div failed"); return r; } @@ -758,7 +777,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) { CBigNum r; - if (!BN_lshift(&r, &a, shift)) + if (!BN_lshift(r.bn, a.bn, shift)) throw bignum_error("CBigNum:operator<< : BN_lshift failed"); return r; } @@ -770,12 +789,12 @@ inline const CBigNum operator>>(const CBigNum& a, unsigned int shift) return r; } -inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); } -inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); } -inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); } -inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); } -inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); } -inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); } +inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) == 0); } +inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) != 0); } +inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) <= 0); } +inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) >= 0); } +inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) < 0); } +inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.bn, b.bn) > 0); } inline std::ostream& operator<<(std::ostream &strm, const CBigNum &b) { return strm << b.ToString(10); } diff --git a/src/crypter.cpp b/src/crypter.cpp index 2a6f36d..0ae13f4 100644 --- a/src/crypter.cpp +++ b/src/crypter.cpp @@ -59,15 +59,15 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector (nCLen); - EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX *ctx; bool fOk = true; - EVP_CIPHER_CTX_init(&ctx); - if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; - if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0; - if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; - EVP_CIPHER_CTX_cleanup(&ctx); + ctx = EVP_CIPHER_CTX_new(); + if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; + if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0; + if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; + EVP_CIPHER_CTX_free(ctx); if (!fOk) return false; @@ -86,15 +86,15 @@ bool CCrypter::Decrypt(const std::vector& vchCiphertext, CKeyingM vchPlaintext = CKeyingMaterial(nPLen); - EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX *ctx; bool fOk = true; - EVP_CIPHER_CTX_init(&ctx); - if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; - if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0; - if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0; - EVP_CIPHER_CTX_cleanup(&ctx); + ctx = EVP_CIPHER_CTX_new(); + if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; + if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0; + if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0; + EVP_CIPHER_CTX_free(ctx); if (!fOk) return false; diff --git a/src/key.cpp b/src/key.cpp index 1ed4e25..1f42533 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -80,7 +80,12 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch x = BN_CTX_get(ctx); if (!BN_copy(x, order)) { ret=-1; goto err; } if (!BN_mul_word(x, i)) { ret=-1; goto err; } - if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; } + + // Get internal R and S pointers + const BIGNUM *ecsig_r, *ecsig_s; + ECDSA_SIG_get0(ecsig, &ecsig_r, &ecsig_s); + + if (!BN_add(x, x, ecsig_r)) { ret=-1; goto err; } field = BN_CTX_get(ctx); if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; } if (BN_cmp(x, field) >= 0) { ret=0; goto err; } @@ -98,12 +103,12 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; } if (8*msglen > n) BN_rshift(e, e, 8-(n & 7)); zero = BN_CTX_get(ctx); - if (!BN_zero(zero)) { ret=-1; goto err; } + BN_zero(zero); if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; } rr = BN_CTX_get(ctx); - if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; } + if (!BN_mod_inverse(rr, ecsig_r, order, ctx)) { ret=-1; goto err; } sor = BN_CTX_get(ctx); - if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; } + if (!BN_mod_mul(sor, ecsig_s, rr, order, ctx)) { ret=-1; goto err; } eor = BN_CTX_get(ctx); if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; } if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; } @@ -375,12 +380,20 @@ bool CKey::Sign(uint256 hash, std::vector& vchSig) return false; const EC_GROUP *group = EC_KEY_get0_group(pkey); CBigNum order, halforder; - EC_GROUP_get_order(group, &order, NULL); - BN_rshift1(&halforder, &order); + EC_GROUP_get_order(group, order.get(), NULL); + BN_rshift1(halforder.get(), order.get()); + + // Get internal R and S pointers + const BIGNUM *current_s = ECDSA_SIG_get0_s(sig); + // enforce low S values, by negating the value (modulo the order) if above order/2. - if (BN_cmp(sig->s, &halforder) > 0) { - BN_sub(sig->s, &order, sig->s); + if (BN_cmp(current_s, halforder.get()) > 0) { + BIGNUM *updated_s = BN_new(); + BN_copy(updated_s, current_s); + BN_sub(updated_s, order.get(), updated_s); + ECDSA_SIG_set0(sig, NULL, updated_s); } + unsigned int nSize = ECDSA_size(pkey); vchSig.resize(nSize); // Make sure it is big enough unsigned char *pos = &vchSig[0]; @@ -407,16 +420,24 @@ bool CKey::SignCompact(uint256 hash, std::vector& vchSig) return false; const EC_GROUP *group = EC_KEY_get0_group(pkey); CBigNum order, halforder; - EC_GROUP_get_order(group, &order, NULL); - BN_rshift1(&halforder, &order); + EC_GROUP_get_order(group, order.get(), NULL); + BN_rshift1(halforder.get(), order.get()); + + // Get internal R and S pointers + const BIGNUM *current_s = ECDSA_SIG_get0_s(sig); + // enforce low S values, by negating the value (modulo the order) if above order/2. - if (BN_cmp(sig->s, &halforder) > 0) { - BN_sub(sig->s, &order, sig->s); + if (BN_cmp(current_s, halforder.get()) > 0) { + BIGNUM *updated_s = BN_new(); + BN_copy(updated_s, current_s); + BN_sub(updated_s, order.get(), updated_s); + ECDSA_SIG_set0(sig, NULL, updated_s); } + vchSig.clear(); vchSig.resize(65,0); - int nBitsR = BN_num_bits(sig->r); - int nBitsS = BN_num_bits(sig->s); + int nBitsR = BN_num_bits(ECDSA_SIG_get0_r(sig)); + int nBitsS = BN_num_bits(ECDSA_SIG_get0_s(sig)); bool fCompressedPubKey = IsCompressed(); if (nBitsR <= 256 && nBitsS <= 256) { @@ -441,8 +462,8 @@ bool CKey::SignCompact(uint256 hash, std::vector& vchSig) } vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0); - BN_bn2bin(sig->r,&vchSig[33-(nBitsR+7)/8]); - BN_bn2bin(sig->s,&vchSig[65-(nBitsS+7)/8]); + BN_bn2bin(ECDSA_SIG_get0_r(sig),&vchSig[33-(nBitsR+7)/8]); + BN_bn2bin(ECDSA_SIG_get0_s(sig),&vchSig[65-(nBitsS+7)/8]); fOk = true; } ECDSA_SIG_free(sig); @@ -461,8 +482,10 @@ bool CPubKey::SetCompactSignature(uint256 hash, const std::vector if (nV<27 || nV>=35) return false; ECDSA_SIG *sig = ECDSA_SIG_new(); - BN_bin2bn(&vchSig[1],32,sig->r); - BN_bin2bn(&vchSig[33],32,sig->s); + BIGNUM *sig_r, *sig_s; + BN_bin2bn(&vchSig[1],32,sig_r); + BN_bin2bn(&vchSig[33],32,sig_s); + ECDSA_SIG_set0(sig, sig_r, sig_s); bool fSuccessful = false; EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1); if (nV >= 31) @@ -631,7 +654,7 @@ bool CPoint::getBytes(std::vector &vchBytes) // ECC multiplication by specified multiplier bool CPoint::ECMUL(const CBigNum &bnMultiplier) { - if (!EC_POINT_mul(group, point, NULL, point, &bnMultiplier, NULL)) { + if (!EC_POINT_mul(group, point, NULL, point, bnMultiplier.get(), NULL)) { printf("CPoint::ECMUL() : EC_POINT_mul failed"); return false; } @@ -642,7 +665,7 @@ bool CPoint::ECMUL(const CBigNum &bnMultiplier) // Calculate G*m + q bool CPoint::ECMULGEN(const CBigNum &bnMultiplier, const CPoint &qPoint) { - if (!EC_POINT_mul(group, point, &bnMultiplier, qPoint.point, BN_value_one(), NULL)) { + if (!EC_POINT_mul(group, point, bnMultiplier.get(), qPoint.point, BN_value_one(), NULL)) { printf("CPoint::ECMULGEN() : EC_POINT_mul failed."); return false; } -- 1.7.1