Blame SOURCES/0001-tpm2-CryptSym-fix-AES-output-IV.patch

ffd936
From 28d8e756676c5efc6979a0606d82d0223558fbff Mon Sep 17 00:00:00 2001
ffd936
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
ffd936
Date: Wed, 17 Mar 2021 16:29:00 +0400
ffd936
Subject: [PATCH] tpm2: CryptSym: fix AES output IV
ffd936
MIME-Version: 1.0
ffd936
Content-Type: text/plain; charset=UTF-8
ffd936
Content-Transfer-Encoding: 8bit
ffd936
ffd936
The TPM is supposed to provide the output IV in the ivInOut parameter in
ffd936
CryptSymmetricEncrypt. In the case of using the openssl routines, the
ffd936
output IV is missed, and the resulting output from the TPM is in the
ffd936
input IV.
ffd936
ffd936
OpenSSL unfortunately does not export EVP_CIPHER_CTX_iv() until
ffd936
tags/OpenSSL_1_1_0, so we have to fall back to the reference code for
ffd936
previous OpenSSL versions.
ffd936
ffd936
Signed-off-by: William Roberts <william.c.roberts@intel.com>
ffd936
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
ffd936
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ffd936
---
ffd936
 configure.ac                       |  1 +
ffd936
 src/tpm2/crypto/openssl/CryptSym.c | 19 +++++++++++++++++++
ffd936
 2 files changed, 20 insertions(+)
ffd936
ffd936
diff --git a/configure.ac b/configure.ac
ffd936
index 2895bc9..f113f17 100644
ffd936
--- a/configure.ac
ffd936
+++ b/configure.ac
ffd936
@@ -165,6 +165,7 @@ AS_IF([test "x$enable_use_openssl_functions" != "xno"], [
ffd936
 	AC_CHECK_LIB([crypto], [EVP_aes_128_cbc],, not_found=1)
ffd936
 	AC_CHECK_LIB([crypto], [EVP_des_ede3_cbc],, not_found=1)
ffd936
 	AC_CHECK_LIB([crypto], [DES_random_key],, not_found=1)
ffd936
+	AC_CHECK_LIB([crypto], [EVP_CIPHER_CTX_iv],, not_found=1)
ffd936
 	if test "x$not_found" = "x0"; then
ffd936
 		use_openssl_functions_symmetric=1
ffd936
 		use_openssl_functions_for="symmetric (AES, TDES) "
ffd936
diff --git a/src/tpm2/crypto/openssl/CryptSym.c b/src/tpm2/crypto/openssl/CryptSym.c
ffd936
index 7aa90da..856def6 100644
ffd936
--- a/src/tpm2/crypto/openssl/CryptSym.c
ffd936
+++ b/src/tpm2/crypto/openssl/CryptSym.c
ffd936
@@ -531,6 +531,7 @@ CryptSymmetricEncrypt(
ffd936
     BYTE                 keyToUse[MAX_SYM_KEY_BYTES];
ffd936
     UINT16               keyToUseLen = (UINT16)sizeof(keyToUse);
ffd936
     TPM_RC               retVal = TPM_RC_SUCCESS;
ffd936
+    int                  ivLen;
ffd936
 
ffd936
     pAssert(dOut != NULL && key != NULL && dIn != NULL);
ffd936
     if(dSize == 0)
ffd936
@@ -595,6 +596,14 @@ CryptSymmetricEncrypt(
ffd936
     if (EVP_EncryptFinal_ex(ctx, pOut + outlen1, &outlen2) != 1)
ffd936
         ERROR_RETURN(TPM_RC_FAILURE);
ffd936
 
ffd936
+    if (ivInOut) {
ffd936
+        ivLen = EVP_CIPHER_CTX_iv_length(ctx);
ffd936
+        if (ivLen < 0 || (size_t)ivLen > sizeof(ivInOut->t.buffer))
ffd936
+            ERROR_RETURN(TPM_RC_FAILURE);
ffd936
+
ffd936
+        ivInOut->t.size = ivLen;
ffd936
+        memcpy(ivInOut->t.buffer, EVP_CIPHER_CTX_iv(ctx), ivInOut->t.size);
ffd936
+    }
ffd936
  Exit:
ffd936
     if (retVal == TPM_RC_SUCCESS && pOut != dOut)
ffd936
         memcpy(dOut, pOut, outlen1 + outlen2);
ffd936
@@ -636,6 +645,7 @@ CryptSymmetricDecrypt(
ffd936
     BYTE                 keyToUse[MAX_SYM_KEY_BYTES];
ffd936
     UINT16               keyToUseLen = (UINT16)sizeof(keyToUse);
ffd936
     TPM_RC               retVal = TPM_RC_SUCCESS;
ffd936
+    int                  ivLen;
ffd936
 
ffd936
     // These are used but the compiler can't tell because they are initialized
ffd936
     // in case statements and it can't tell if they are always initialized
ffd936
@@ -707,6 +717,15 @@ CryptSymmetricDecrypt(
ffd936
 
ffd936
     pAssert((int)buffersize >= outlen1 + outlen2);
ffd936
 
ffd936
+    if (ivInOut) {
ffd936
+        ivLen = EVP_CIPHER_CTX_iv_length(ctx);
ffd936
+        if (ivLen < 0 || (size_t)ivLen > sizeof(ivInOut->t.buffer))
ffd936
+            ERROR_RETURN(TPM_RC_FAILURE);
ffd936
+
ffd936
+        ivInOut->t.size = ivLen;
ffd936
+        memcpy(ivInOut->t.buffer, EVP_CIPHER_CTX_iv(ctx), ivInOut->t.size);
ffd936
+    }
ffd936
+
ffd936
  Exit:
ffd936
     if (retVal == TPM_RC_SUCCESS) {
ffd936
         pAssert(dSize >= outlen1 + outlen2);
ffd936
-- 
ffd936
2.29.0
ffd936