Hi, This is an API question on using OpenSSL and AES GCM. I am successfully able to perform encryption / decryption and I am also able to detect errors in the mac value. The code I am using for this is below. I want to retrieve the mac value to my own buffer, in addition to just doing the internal comparison. I tried this by providing a local buffer in the function call 'DecryptFinal(..)', but nothing is written to the buffer. What am I doing wrong?
- Roar int decryptAes256Gcm(byte_t* pIv, int ivLen, byte_t* pKey, int keyLen, byte_t* pAAD, int AADLen, byte_t* pCipherText, int cipherTextLen, byte_t* pClearText, byte_t* pMAC, int MACLen) { int iResult = 0; EVP_CIPHER_CTX ctx; int bytesProcessed; EVP_CIPHER_CTX_init(&ctx); EVP_DecryptInit_ex(&ctx, EVP_aes_256_gcm(), NULL, pKey, pIv); EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, MACLen, pMAC); EVP_DecryptUpdate(&ctx, NULL, &bytesProcessed, pAAD, AADLen); EVP_DecryptUpdate(&ctx, pClearText, &bytesProcessed, pCipherText, cipherTextLen); if(NULL != pMAC) { byte_t localTag[GCM_BLOCK_SIZE]; int localTagLen = GCM_BLOCK_SIZE; if(1 != EVP_DecryptFinal_ex(&ctx, localTag, &bytesProcessed)) { goto CleanUp; } } else { if(!EVP_DecryptFinal_ex(&ctx, NULL, &bytesProcessed)) { goto CleanUp; } } iResult = 1; CleanUp: EVP_CIPHER_CTX_cleanup(&ctx); return iResult; }