So I have this code:
BlueSOD::Encryption::DecryptionData BlueSOD::Encryption::EncryptionFactory::OpenEnvelopeDecrypt(DecryptionWork && work)
{
EVP_PKEY* privateKey = work.privateKey.get();
auto eKey = (unsigned char*)work.aes_info.key.c_str();
auto eIV = (unsigned char*)work.aes_info.iv.c_str();
EVP_CIPHER_CTX_ptr cipherCtxPtr{ AcquireCipherCtx() };
EVP_CIPHER_CTX* cipher = cipherCtxPtr.get();
int status;
status = EVP_OpenInit(cipher, m_Cipher, eKey, work.aes_info.key.size(), eIV, privateKey);
CheckForError(status);
auto decrypted = make_unique<unsigned char[]>(work.cipherText.size() + EVP_MAX_BLOCK_LENGTH);
auto cipherTemp = (unsigned char*)work.cipherText.c_str();
int amtDecrypted = 0;
int bufferLength = 0;
status = EVP_OpenUpdate(cipher, decrypted.get(), &amtDecrypted, cipherTemp, work.cipherText.size());
CheckForError(status);
bufferLength += amtDecrypted;
//error occurs in both versions of the following code
//status = EVP_OpenFinal(cipher, decrypted.get() + amtDecrypted, &amtDecrypted);
status = EVP_OpenFinal(cipher, decrypted.get(), &amtDecrypted);
CheckForError(status);
bufferLength += amtDecrypted;
DecryptionData data;
data.plainText = CreateSecureString(decrypted.get(), bufferLength);
return move(data);
}
And when I step through it in the debugger decrypted
holds the correct plain text before the call, yet EVP_OpenFinal
returns the error in the title. The code above it is what I initially had (and it returns the same error).
What would be the reason for EVP_OpenFinal
returning an error when decrypted
holds the correct plain text before the call?