Search code examples
c++opensslblockchainlitecoin

error: no matching function for call to 'EVP_EncryptInit_ex'


I tried to compile litecoind for litecoin, but an error occurred.

The source code was taken from the latest version of litecoin.

Here is part of the code:

bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
{
    if (!fKeySet)
        return false;

    // plaintext will always be equal to or lesser than length of ciphertext
    int nLen = vchCiphertext.size();
    int nPLen = nLen, nFLen = 0;

    vchPlaintext = CKeyingMaterial(nPLen);

    EVP_CIPHER_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_free(ctx);


    if (!fOk) return false;

    vchPlaintext.resize(nPLen + nFLen);
    return true;
}

I searched for an answer, but did not find a suitable one for me.

As I understand it, this is related to the version of openssl.

There was a mistake before

error: field 'ctx' has incomplete type EVP_CIPHER_CTX.

Since I'm on a new openssl, I replaced it with EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); and the error disappeared but a new one appeared.


Solution

  • Since ctx is an EVP_CIPHER_CTX* pointer, which is what the EVP_Decrypt...() functions take, then you need to drop the & address-of operator when passing ctx to them, eg:

    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new()
    
    EVP_CIPHER_CTX_init(ctx);
    EVP_DecryptInit_ex(ctx, ...);
    EVP_DecryptUpdate(ctx, ...);
    EVP_DecryptFinal_ex(ctx, ...);
    
    EVP_CIPHER_CTX_free(ctx);