address EVP_CIPHER_CTX opaque type compilation problem with newer openssl versions.

should fix #406
This commit is contained in:
Miguel Freitas 2017-06-21 11:38:01 -03:00
parent 7eabdcaab0
commit 6b157f10c0
3 changed files with 54 additions and 37 deletions

View File

@ -8,7 +8,7 @@
// These need to be macros, as version.cpp's and bitcoin-qt.rc's voodoo requires it
#define CLIENT_VERSION_MAJOR 0
#define CLIENT_VERSION_MINOR 9
#define CLIENT_VERSION_REVISION 38
#define CLIENT_VERSION_REVISION 39
#define CLIENT_VERSION_BUILD 0
// Set to true for release, false for prerelease or test build

View File

@ -56,15 +56,17 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
vchCiphertext = std::vector<unsigned char> (nCLen);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
bool fOk = true;
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_CIPHER_CTX_init(ctx);
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0])+nCLen, &nFLen);
EVP_CIPHER_CTX_cleanup(ctx);
EVP_CIPHER_CTX_free(ctx);
if (!fOk) return false;
@ -83,15 +85,17 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
vchPlaintext = CKeyingMaterial(nPLen);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
bool fOk = true;
EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_CIPHER_CTX_init(ctx);
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0])+nPLen, &nFLen);
EVP_CIPHER_CTX_cleanup(ctx);
EVP_CIPHER_CTX_free(ctx);
if (!fOk) return false;

View File

@ -379,20 +379,22 @@ public:
memset(iv, 0, EVP_MAX_IV_LENGTH);
// Setup the cipher context, the body length, and store a pointer to the body buffer location.
EVP_CIPHER_CTX cipher;
EVP_CIPHER_CTX_init(&cipher);
EVP_CIPHER_CTX *cipher;
cipher = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(cipher);
unsigned char *body = reinterpret_cast<unsigned char *>(&cryptex.body[0]);
int body_length = cryptex.body.size();
// Initialize the cipher with the envelope key.
if (EVP_EncryptInit_ex(&cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 ||
EVP_CIPHER_CTX_set_padding(&cipher, 0) != 1 ||
EVP_EncryptUpdate(&cipher, body, &body_length, reinterpret_cast<const unsigned char *>(&vchText[0]), length - (length % block_length)) != 1) {
if (EVP_EncryptInit_ex(cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 ||
EVP_CIPHER_CTX_set_padding(cipher, 0) != 1 ||
EVP_EncryptUpdate(cipher, body, &body_length, reinterpret_cast<const unsigned char *>(&vchText[0]), length - (length % block_length)) != 1) {
#ifdef DEBUG_ECIES
printf("An error occurred while trying to secure the data using the chosen symmetric cipher.\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
// Check whether all of the data was encrypted. If they don't match up, we either have a partial block remaining, or an error occurred.
@ -402,7 +404,8 @@ public:
#ifdef DEBUG_ECIES
printf("Unable to secure the data using the chosen symmetric cipher.\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
@ -416,16 +419,18 @@ public:
#ifdef DEBUG_ECIES
printf("The symmetric cipher overflowed!\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
// Pass the final partially filled data block into the cipher as a complete block. The padding will be removed during the decryption process.
else if (EVP_EncryptUpdate(&cipher, body, &body_length, block, block_length) != 1) {
else if (EVP_EncryptUpdate(cipher, body, &body_length, block, block_length) != 1) {
#ifdef DEBUG_ECIES
printf("Unable to secure the data using the chosen symmetric cipher\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
}
@ -437,19 +442,22 @@ public:
#ifdef DEBUG_ECIES
printf("The symmetric cipher overflowed!\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
else if (EVP_EncryptFinal_ex(&cipher, body, &body_length) != 1) {
else if (EVP_EncryptFinal_ex(cipher, body, &body_length) != 1) {
#ifdef DEBUG_ECIES
printf("Unable to secure the data using the chosen symmetric cipher.\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
// Generate an authenticated hash which can be used to validate the data during decryption.
HMAC_CTX hmac;
@ -611,17 +619,19 @@ public:
memset(output, 0, output_length + 1);
// Setup the cipher context, the body length, and store a pointer to the body buffer location.
EVP_CIPHER_CTX cipher;
EVP_CIPHER_CTX_init(&cipher);
EVP_CIPHER_CTX *cipher;
cipher = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(cipher);
// Decrypt the data using the chosen symmetric cipher.
if (EVP_DecryptInit_ex(&cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 ||
EVP_CIPHER_CTX_set_padding(&cipher, 0) != 1 ||
EVP_DecryptUpdate(&cipher, block, &output_length, reinterpret_cast<const unsigned char *>(cryptex.body.data()), cryptex.body.size()) != 1) {
if (EVP_DecryptInit_ex(cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 ||
EVP_CIPHER_CTX_set_padding(cipher, 0) != 1 ||
EVP_DecryptUpdate(cipher, block, &output_length, reinterpret_cast<const unsigned char *>(cryptex.body.data()), cryptex.body.size()) != 1) {
#ifdef DEBUG_ECIES
printf("Unable to decrypt the data using the chosen symmetric cipher.\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
@ -630,19 +640,22 @@ public:
#ifdef DEBUG_ECIES
printf("The symmetric cipher failed to properly decrypt the correct amount of data!\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
if (EVP_DecryptFinal_ex(&cipher, block, &output_length) != 1) {
if (EVP_DecryptFinal_ex(cipher, block, &output_length) != 1) {
#ifdef DEBUG_ECIES
printf("Unable to decrypt the data using the chosen symmetric cipher.\n");
#endif
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
return false;
}
EVP_CIPHER_CTX_cleanup(&cipher);
EVP_CIPHER_CTX_cleanup(cipher);
EVP_CIPHER_CTX_free(cipher);
vchText.resize(cryptex.orig);
return true;