Blame SOURCES/0010-openssl-Convert-deprecated-SHA256-384-digesters-to-E.patch

05e1a9
From 220b7e0afa943693a4fdafd9a5b8d9e38ea4e785 Mon Sep 17 00:00:00 2001
05e1a9
From: Petr Gotthard <petr.gotthard@centrum.cz>
05e1a9
Date: Sat, 7 Aug 2021 15:54:51 +0200
05e1a9
Subject: [PATCH 09/17] openssl: Convert deprecated SHA256|384 digesters to
05e1a9
 EVP_Digest
05e1a9
05e1a9
The EVP_Digest function is available since OpenSSL 1.1.0
05e1a9
05e1a9
Signed-off-by: Petr Gotthard <petr.gotthard@centrum.cz>
05e1a9
---
05e1a9
 lib/tpm2_identity_util.c | 34 ++++++++++++++++++++++++----------
05e1a9
 lib/tpm2_kdfe.c          | 13 +++++++++++--
05e1a9
 lib/tpm2_openssl.c       | 17 -----------------
05e1a9
 lib/tpm2_openssl.h       | 28 ----------------------------
05e1a9
 lib/tpm2_util.c          | 15 +++++++++++----
05e1a9
 5 files changed, 46 insertions(+), 61 deletions(-)
05e1a9
05e1a9
diff --git a/lib/tpm2_identity_util.c b/lib/tpm2_identity_util.c
05e1a9
index a268295f..e0c3f404 100644
05e1a9
--- a/lib/tpm2_identity_util.c
05e1a9
+++ b/lib/tpm2_identity_util.c
05e1a9
@@ -391,11 +391,20 @@ bool tpm2_identity_util_calculate_inner_integrity(TPMI_ALG_HASH name_alg,
05e1a9
     Tss2_MU_UINT16_Marshal(hash_size, marshalled_sensitive_and_name_digest,
05e1a9
             sizeof(uint16_t), &digest_size_info);
05e1a9
 
05e1a9
-    digester d = tpm2_openssl_halg_to_digester(name_alg);
05e1a9
-    d(buffer_marshalled_sensitiveArea,
05e1a9
-            marshalled_sensitive_size_info + marshalled_sensitive_size
05e1a9
-                    + pubname->size,
05e1a9
-            marshalled_sensitive_and_name_digest + digest_size_info);
05e1a9
+    const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(name_alg);
05e1a9
+    if (!md) {
05e1a9
+        LOG_ERR("Algorithm not supported: %x", name_alg);
05e1a9
+        return false;
05e1a9
+    }
05e1a9
+    int rc = EVP_Digest(buffer_marshalled_sensitiveArea,
05e1a9
+                        marshalled_sensitive_size_info + marshalled_sensitive_size
05e1a9
+                            + pubname->size,
05e1a9
+                        marshalled_sensitive_and_name_digest + digest_size_info,
05e1a9
+                        NULL, md, NULL);
05e1a9
+    if (!rc) {
05e1a9
+        LOG_ERR("Hash calculation failed");
05e1a9
+        return false;
05e1a9
+    }
05e1a9
 
05e1a9
     //Inner integrity
05e1a9
     encrypted_inner_integrity->size = marshalled_sensitive_size_info
05e1a9
@@ -452,16 +461,21 @@ bool tpm2_identity_create_name(TPM2B_PUBLIC *public, TPM2B_NAME *pubname) {
05e1a9
             &tpmt_marshalled_size);
05e1a9
 
05e1a9
     // Step 3 - Hash the data into name just past the alg type.
05e1a9
-    digester d = tpm2_openssl_halg_to_digester(name_alg);
05e1a9
-    if (!d) {
05e1a9
+    const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(name_alg);
05e1a9
+    if (!md) {
05e1a9
+        LOG_ERR("Algorithm not supported: %x", name_alg);
05e1a9
         return false;
05e1a9
     }
05e1a9
 
05e1a9
-    d((const unsigned char *) &marshaled_tpmt, tpmt_marshalled_size,
05e1a9
-            pubname->name + hash_offset);
05e1a9
+    unsigned int hash_size;
05e1a9
+    int rc = EVP_Digest(&marshaled_tpmt, tpmt_marshalled_size,
05e1a9
+                        pubname->name + hash_offset, &hash_size, md, NULL);
05e1a9
+    if (!rc) {
05e1a9
+        LOG_ERR("Hash calculation failed");
05e1a9
+        return false;
05e1a9
+    }
05e1a9
 
05e1a9
     //Set the name size, UINT16 followed by HASH
05e1a9
-    UINT16 hash_size = tpm2_alg_util_get_hash_size(name_alg);
05e1a9
     pubname->size = hash_size + hash_offset;
05e1a9
 
05e1a9
     return true;
05e1a9
diff --git a/lib/tpm2_kdfe.c b/lib/tpm2_kdfe.c
05e1a9
index e8aeb04c..aa4d3e0b 100644
05e1a9
--- a/lib/tpm2_kdfe.c
05e1a9
+++ b/lib/tpm2_kdfe.c
05e1a9
@@ -42,13 +42,22 @@ TSS2_RC tpm2_kdfe(
05e1a9
     tpm2_util_concat_buffer(&hash_input, (TPM2B *) party_u);
05e1a9
     tpm2_util_concat_buffer(&hash_input, (TPM2B *) party_v);
05e1a9
 
05e1a9
-    digester d = tpm2_openssl_halg_to_digester(hash_alg);
05e1a9
+    const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(hash_alg);
05e1a9
+    if (!md) {
05e1a9
+        LOG_ERR("Algorithm not supported: %x", hash_alg);
05e1a9
+        return TPM2_RC_HASH;
05e1a9
+    }
05e1a9
 
05e1a9
     for (done = 0, counter = 1; done < bytes; done += hash_size, counter++) {
05e1a9
         counter_be = tpm2_util_hton_32(counter);
05e1a9
         memcpy(hash_input.buffer, &counter_be, 4);
05e1a9
 
05e1a9
-        d(hash_input.buffer, hash_input.size, result_key->buffer + done);
05e1a9
+        int rc = EVP_Digest(hash_input.buffer, hash_input.size,
05e1a9
+                            result_key->buffer + done, NULL, md, NULL);
05e1a9
+        if (!rc) {
05e1a9
+            LOG_ERR("Hash calculation failed");
05e1a9
+            return TPM2_RC_MEMORY;
05e1a9
+        }
05e1a9
     }
05e1a9
     // truncate the result to the desired size
05e1a9
     result_key->size = bytes;
05e1a9
diff --git a/lib/tpm2_openssl.c b/lib/tpm2_openssl.c
05e1a9
index 1752525e..cdce92f8 100644
05e1a9
--- a/lib/tpm2_openssl.c
05e1a9
+++ b/lib/tpm2_openssl.c
05e1a9
@@ -368,23 +368,6 @@ out:
05e1a9
     return result;
05e1a9
 }
05e1a9
 
05e1a9
-digester tpm2_openssl_halg_to_digester(TPMI_ALG_HASH halg) {
05e1a9
-
05e1a9
-    switch (halg) {
05e1a9
-    case TPM2_ALG_SHA1:
05e1a9
-        return SHA1;
05e1a9
-    case TPM2_ALG_SHA256:
05e1a9
-        return SHA256;
05e1a9
-    case TPM2_ALG_SHA384:
05e1a9
-        return SHA384;
05e1a9
-    case TPM2_ALG_SHA512:
05e1a9
-        return SHA512;
05e1a9
-        /* no default */
05e1a9
-    }
05e1a9
-
05e1a9
-    return NULL;
05e1a9
-}
05e1a9
-
05e1a9
 /*
05e1a9
  * Per man openssl(1), handle the following --passin formats:
05e1a9
  *     pass:password
05e1a9
diff --git a/lib/tpm2_openssl.h b/lib/tpm2_openssl.h
05e1a9
index 642e4635..78cb826a 100644
05e1a9
--- a/lib/tpm2_openssl.h
05e1a9
+++ b/lib/tpm2_openssl.h
05e1a9
@@ -28,23 +28,6 @@
05e1a9
         EC_POINT_get_affine_coordinates_GFp(group, tpm_pub_key, bn_x, bn_y, dmy)
05e1a9
 #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */
05e1a9
 
05e1a9
-/**
05e1a9
- * Function prototype for a hashing routine.
05e1a9
- *
05e1a9
- * This is a wrapper around OSSL SHA256|384 and etc digesters.
05e1a9
- *
05e1a9
- * @param d
05e1a9
- *  The data to digest.
05e1a9
- * @param n
05e1a9
- *  The length of the data to digest.
05e1a9
- * @param md
05e1a9
- *  The output message digest.
05e1a9
- * @return
05e1a9
- * A pointer to the digest or NULL on error.
05e1a9
- */
05e1a9
-typedef unsigned char *(*digester)(const unsigned char *d, size_t n,
05e1a9
-        unsigned char *md);
05e1a9
-
05e1a9
 static inline const char *tpm2_openssl_get_err(void) {
05e1a9
     return ERR_error_string(ERR_get_error(), NULL);
05e1a9
 }
05e1a9
@@ -147,17 +130,6 @@ bool tpm2_openssl_hash_pcr_banks_le(TPMI_ALG_HASH hashAlg,
05e1a9
 bool tpm2_openssl_pcr_extend(TPMI_ALG_HASH halg, BYTE *pcr,
05e1a9
         const BYTE *data, UINT16 length);
05e1a9
 
05e1a9
-/**
05e1a9
- * Returns a function pointer capable of performing the
05e1a9
- * given digest from a TPMI_HASH_ALG.
05e1a9
- *
05e1a9
- * @param halg
05e1a9
- *  The hashing algorithm to use.
05e1a9
- * @return
05e1a9
- *  NULL on failure or a valid digester on success.
05e1a9
- */
05e1a9
-digester tpm2_openssl_halg_to_digester(TPMI_ALG_HASH halg);
05e1a9
-
05e1a9
 typedef enum tpm2_openssl_load_rc tpm2_openssl_load_rc;
05e1a9
 enum tpm2_openssl_load_rc {
05e1a9
     lprc_error = 0, /* an error has occurred */
05e1a9
diff --git a/lib/tpm2_util.c b/lib/tpm2_util.c
05e1a9
index 4125a4b9..d2c654db 100644
05e1a9
--- a/lib/tpm2_util.c
05e1a9
+++ b/lib/tpm2_util.c
05e1a9
@@ -579,13 +579,20 @@ bool tpm2_util_calc_unique(TPMI_ALG_HASH name_alg,
05e1a9
     memcpy(buf.buffer, seed->buffer, seed->size);
05e1a9
     memcpy(&buf.buffer[seed->size], key->buffer, key->size);
05e1a9
 
05e1a9
-    digester d = tpm2_openssl_halg_to_digester(name_alg);
05e1a9
-    if (!d) {
05e1a9
+    const EVP_MD *md = tpm2_openssl_halg_from_tpmhalg(name_alg);
05e1a9
+    if (!md) {
05e1a9
+        LOG_ERR("Algorithm not supported: %x", name_alg);
05e1a9
         return false;
05e1a9
     }
05e1a9
 
05e1a9
-    unique_data->size = tpm2_alg_util_get_hash_size(name_alg);
05e1a9
-    d(buf.buffer, buf.size, unique_data->buffer);
05e1a9
+    unsigned int hash_size;
05e1a9
+    int rc = EVP_Digest(buf.buffer, buf.size, unique_data->buffer, &hash_size,
05e1a9
+                        md, NULL);
05e1a9
+    if (!rc) {
05e1a9
+        LOG_ERR("Hash calculation failed");
05e1a9
+        return false;
05e1a9
+    }
05e1a9
+    unique_data->size = hash_size;
05e1a9
 
05e1a9
     return true;
05e1a9
 }
05e1a9
-- 
05e1a9
2.31.1
05e1a9