@@ -397,7 +397,6 @@ static int we_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
397397 }
398398 }
399399 if (ret == 1 ) {
400-
401400 WOLFENGINE_MSG_VERBOSE (WE_LOG_CIPHER , "Encrypted %zu bytes "
402401 "(AES-GCM):" , len );
403402 WOLFENGINE_BUFFER (WE_LOG_CIPHER , out , (unsigned int )len );
@@ -427,6 +426,8 @@ static int we_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
427426 XMEMCPY (aes -> decryptBuf , in , len );
428427 aes -> decryptBufLen = len ;
429428 }
429+ /* No data decrypted, yet, so return 0. */
430+ ret = 0 ;
430431 }
431432 else
432433 #endif
@@ -453,10 +454,7 @@ static int we_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
453454 if (!aes -> enc ) {
454455 ret = we_aes_gcm_decrypt (aes , out , aes -> decryptBuf ,
455456 aes -> decryptBufLen );
456-
457- OPENSSL_free (aes -> decryptBuf );
458- aes -> decryptBuf = NULL ;
459- aes -> decryptBufLen = 0 ;
457+ aes -> tagLen = 0 ;
460458 }
461459 #endif
462460
@@ -470,6 +468,20 @@ static int we_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
470468 }
471469 }
472470
471+ #ifdef WE_AES_GCM_DECRYPT_ON_FINAL
472+ if (!aes -> enc ) {
473+ if (ret != -1 ) {
474+ /* Return the length of decryption now that we've actually
475+ * decrypted. */
476+ ret = aes -> decryptBufLen ;
477+ }
478+
479+ OPENSSL_free (aes -> decryptBuf );
480+ aes -> decryptBuf = NULL ;
481+ aes -> decryptBufLen = 0 ;
482+ }
483+ #endif
484+
473485 if (aes -> aad != NULL ) {
474486 OPENSSL_free (aes -> aad );
475487 aes -> aad = NULL ;
0 commit comments