Skip to content

Commit 903c4f1

Browse files
committed
[Consensus] Allow for compilation with OpenSSL 1.1
Code is largely inspired from str4d/zcash@f3f600a Squashing commits... -Fix double declaration of operator< in bn.h -Ensured compatibility with openssl pre 1.1 -Fix a depreciation with OpenSSL 1.1 -Compatible with OpenSSL below 1.1 -Fix compilation openssl 1.0 when using gui -Fix typo for compilation -[Compilation] Acknowledge ability to compile against OpenSSL 1.1wq
1 parent 2cf3be6 commit 903c4f1

File tree

5 files changed

+173
-126
lines changed

5 files changed

+173
-126
lines changed

configure.ac

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,10 @@ else
793793
fi
794794

795795
AC_CHECK_LIB([crypto],[RAND_egd],[],[
796-
AC_ARG_WITH([libressl],
797-
[AS_HELP_STRING([--with-libressl],[Build with system LibreSSL (default is no; DANGEROUS; NOT SUPPORTED)])],
798-
[AC_MSG_WARN([Detected LibreSSL: This is NOT supported, and may break consensus compatibility!])],
799-
[AC_MSG_ERROR([Detected LibreSSL: This is NOT supported, and may break consensus compatibility!])]
796+
AC_ARG_WITH([unsupported-ssl],
797+
[AS_HELP_STRING([--with-unsupported-ssl],[Build with system SSL (default is no; DANGEROUS; NOT SUPPORTED; You should use OpenSSL 1.0)])],
798+
[AC_MSG_WARN([Detected unsupported SSL version: This is NOT supported, and may break consensus compatibility!])],
799+
[AC_MSG_ERROR([Detected unsupported SSL version: This is NOT supported, and may break consensus compatibility!])]
800800
)
801801
])
802802

src/crypter.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
5757
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
5858
vchCiphertext = std::vector<unsigned char>(nCLen);
5959

60-
EVP_CIPHER_CTX ctx;
61-
6260
bool fOk = true;
6361

64-
EVP_CIPHER_CTX_init(&ctx);
65-
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
66-
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
67-
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
68-
EVP_CIPHER_CTX_cleanup(&ctx);
62+
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
63+
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
64+
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
65+
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
66+
EVP_CIPHER_CTX_free(ctx);
6967

7068
if (!fOk) return false;
7169

@@ -84,15 +82,13 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
8482

8583
vchPlaintext = CKeyingMaterial(nPLen);
8684

87-
EVP_CIPHER_CTX ctx;
88-
8985
bool fOk = true;
9086

91-
EVP_CIPHER_CTX_init(&ctx);
92-
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
93-
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
94-
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
95-
EVP_CIPHER_CTX_cleanup(&ctx);
87+
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
88+
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
89+
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
90+
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
91+
EVP_CIPHER_CTX_free(ctx);
9692

9793
if (!fOk) return false;
9894

@@ -131,15 +127,15 @@ bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, con
131127
sCiphertext.resize(nCLen);
132128

133129
// Perform the encryption
134-
EVP_CIPHER_CTX ctx;
130+
EVP_CIPHER_CTX* ctx;
135131

136132
bool fOk = true;
137133

138-
EVP_CIPHER_CTX_init(&ctx);
139-
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
140-
if (fOk) fOk = EVP_EncryptUpdate(&ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
141-
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
142-
EVP_CIPHER_CTX_cleanup(&ctx);
134+
ctx = EVP_CIPHER_CTX_new();
135+
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
136+
if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
137+
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
138+
EVP_CIPHER_CTX_free(ctx);
143139

144140
if (!fOk) return false;
145141

@@ -172,15 +168,15 @@ bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, con
172168

173169
sPlaintext.resize(nPLen);
174170

175-
EVP_CIPHER_CTX ctx;
171+
EVP_CIPHER_CTX* ctx;
176172

177173
bool fOk = true;
178174

179-
EVP_CIPHER_CTX_init(&ctx);
180-
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
181-
if (fOk) fOk = EVP_DecryptUpdate(&ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
182-
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
183-
EVP_CIPHER_CTX_cleanup(&ctx);
175+
ctx = EVP_CIPHER_CTX_new();
176+
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
177+
if (fOk) fOk = EVP_DecryptUpdate(ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
178+
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
179+
EVP_CIPHER_CTX_free(ctx);
184180

185181
if (!fOk) return false;
186182

src/ecwrapper.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
3939
int n = 0;
4040
int i = recid / 2;
4141

42+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
43+
const BIGNUM *sig_r, *sig_s;
44+
ECDSA_SIG_get0(ecsig, &sig_r, &sig_s);
45+
#endif
46+
4247
const EC_GROUP* group = EC_KEY_get0_group(eckey);
4348
if ((ctx = BN_CTX_new()) == NULL) {
4449
ret = -1;
@@ -59,7 +64,11 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
5964
ret = -1;
6065
goto err;
6166
}
67+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
68+
if (!BN_add(x, x, sig_r)) {
69+
#else
6270
if (!BN_add(x, x, ecsig->r)) {
71+
#endif
6372
ret = -1;
6473
goto err;
6574
}
@@ -115,12 +124,20 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
115124
goto err;
116125
}
117126
rr = BN_CTX_get(ctx);
127+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
128+
if (!BN_mod_inverse(rr, sig_r, order, ctx)) {
129+
#else
118130
if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) {
131+
#endif
119132
ret = -1;
120133
goto err;
121134
}
122135
sor = BN_CTX_get(ctx);
136+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
137+
if (!BN_mod_mul(sor, sig_s, rr, order, ctx)) {
138+
#else
123139
if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) {
140+
#endif
124141
ret = -1;
125142
goto err;
126143
}
@@ -218,8 +235,20 @@ bool CECKey::Recover(const uint256& hash, const unsigned char* p64, int rec)
218235
if (rec < 0 || rec >= 3)
219236
return false;
220237
ECDSA_SIG* sig = ECDSA_SIG_new();
221-
BN_bin2bn(&p64[0], 32, sig->r);
238+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
239+
BIGNUM *sig_r = NULL;
240+
BIGNUM *sig_s = NULL;
241+
if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) ||
242+
!(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) ||
243+
!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
244+
BN_free(sig_r);
245+
BN_free(sig_s);
246+
return false;
247+
}
248+
#else
249+
BN_bin2bn(&p64[0], 32, sig->r);
222250
BN_bin2bn(&p64[32], 32, sig->s);
251+
#endif
223252
bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
224253
ECDSA_SIG_free(sig);
225254
return ret;

0 commit comments

Comments
 (0)