Blame SOURCES/0004-tpm2_identity_util-move-create_name-into-utility-lib.patch

28a59a
From 6a3100ad060934228a1bec06ae43b41f5ea8a51b Mon Sep 17 00:00:00 2001
28a59a
From: Trammell hudson <hudson@trmm.net>
28a59a
Date: Fri, 26 Mar 2021 17:23:07 +0000
28a59a
Subject: [PATCH 03/17] tpm2_identity_util: move create_name() into utility
28a59a
 library
28a59a
28a59a
Signed-off-by: Trammell Hudson <hudson@trmm.net>
28a59a
---
28a59a
 lib/tpm2_identity_util.c | 40 ++++++++++++++++++++++++++++++++++++++
28a59a
 lib/tpm2_identity_util.h | 10 ++++++++++
28a59a
 tools/tpm2_import.c      | 42 +---------------------------------------
28a59a
 3 files changed, 51 insertions(+), 41 deletions(-)
28a59a
28a59a
diff --git a/lib/tpm2_identity_util.c b/lib/tpm2_identity_util.c
28a59a
index a3b0e387..e11137ab 100644
28a59a
--- a/lib/tpm2_identity_util.c
28a59a
+++ b/lib/tpm2_identity_util.c
28a59a
@@ -423,3 +423,43 @@ void tpm2_identity_util_calculate_outer_integrity(TPMI_ALG_HASH parent_name_alg,
28a59a
             encrypted_duplicate_sensitive->size, pubname->name, pubname->size,
28a59a
             protection_hmac_key->buffer, outer_hmac);
28a59a
 }
28a59a
+
28a59a
+bool tpm2_identity_create_name(TPM2B_PUBLIC *public, TPM2B_NAME *pubname) {
28a59a
+
28a59a
+    /*
28a59a
+     * A TPM2B_NAME is the name of the algorithm, followed by the hash.
28a59a
+     * Calculate the name by:
28a59a
+     * 1. Marshaling the name algorithm
28a59a
+     * 2. Marshaling the TPMT_PUBLIC past the name algorithm from step 1.
28a59a
+     * 3. Hash the TPMT_PUBLIC portion in marshaled data.
28a59a
+     */
28a59a
+
28a59a
+    TPMI_ALG_HASH name_alg = public->publicArea.nameAlg;
28a59a
+
28a59a
+    // Step 1 - set beginning of name to hash alg
28a59a
+    size_t hash_offset = 0;
28a59a
+    Tss2_MU_UINT16_Marshal(name_alg, pubname->name, pubname->size,
28a59a
+            &hash_offset);
28a59a
+
28a59a
+    // Step 2 - marshal TPMTP
28a59a
+    TPMT_PUBLIC marshaled_tpmt;
28a59a
+    size_t tpmt_marshalled_size = 0;
28a59a
+    Tss2_MU_TPMT_PUBLIC_Marshal(&public->publicArea,
28a59a
+            (uint8_t *) &marshaled_tpmt, sizeof(public->publicArea),
28a59a
+            &tpmt_marshalled_size);
28a59a
+
28a59a
+    // Step 3 - Hash the data into name just past the alg type.
28a59a
+    digester d = tpm2_openssl_halg_to_digester(name_alg);
28a59a
+    if (!d) {
28a59a
+        return false;
28a59a
+    }
28a59a
+
28a59a
+    d((const unsigned char *) &marshaled_tpmt, tpmt_marshalled_size,
28a59a
+            pubname->name + hash_offset);
28a59a
+
28a59a
+    //Set the name size, UINT16 followed by HASH
28a59a
+    UINT16 hash_size = tpm2_alg_util_get_hash_size(name_alg);
28a59a
+    pubname->size = hash_size + hash_offset;
28a59a
+
28a59a
+    return true;
28a59a
+}
28a59a
diff --git a/lib/tpm2_identity_util.h b/lib/tpm2_identity_util.h
28a59a
index 0ac55793..61e10376 100644
28a59a
--- a/lib/tpm2_identity_util.h
28a59a
+++ b/lib/tpm2_identity_util.h
28a59a
@@ -102,4 +102,14 @@ void tpm2_identity_util_calculate_outer_integrity(TPMI_ALG_HASH parent_name_alg,
28a59a
         TPM2B_MAX_BUFFER *encrypted_duplicate_sensitive,
28a59a
         TPM2B_DIGEST *outer_hmac);
28a59a
 
28a59a
+/**
28a59a
+ * Computes the name of a TPM key.
28a59a
+ *
28a59a
+ * @param public
28a59a
+ *  Public key structure
28a59a
+ * @param pubname
28a59a
+ *  The name structure to populate.
28a59a
+ */
28a59a
+bool tpm2_identity_create_name(TPM2B_PUBLIC *public, TPM2B_NAME *pubname);
28a59a
+
28a59a
 #endif /* LIB_TPM2_IDENTITY_UTIL_H_ */
28a59a
diff --git a/tools/tpm2_import.c b/tools/tpm2_import.c
28a59a
index eb8dd9a7..a5d1b4e6 100644
28a59a
--- a/tools/tpm2_import.c
28a59a
+++ b/tools/tpm2_import.c
28a59a
@@ -74,46 +74,6 @@ static tool_rc readpublic(ESYS_CONTEXT *ectx, ESYS_TR handle,
28a59a
     return tpm2_readpublic(ectx, handle, public, NULL, NULL);
28a59a
 }
28a59a
 
28a59a
-static bool create_name(TPM2B_PUBLIC *public, TPM2B_NAME *pubname) {
28a59a
-
28a59a
-    /*
28a59a
-     * A TPM2B_NAME is the name of the algorithm, followed by the hash.
28a59a
-     * Calculate the name by:
28a59a
-     * 1. Marshaling the name algorithm
28a59a
-     * 2. Marshaling the TPMT_PUBLIC past the name algorithm from step 1.
28a59a
-     * 3. Hash the TPMT_PUBLIC portion in marshaled data.
28a59a
-     */
28a59a
-
28a59a
-    TPMI_ALG_HASH name_alg = public->publicArea.nameAlg;
28a59a
-
28a59a
-    // Step 1 - set beginning of name to hash alg
28a59a
-    size_t hash_offset = 0;
28a59a
-    Tss2_MU_UINT16_Marshal(name_alg, pubname->name, pubname->size,
28a59a
-            &hash_offset);
28a59a
-
28a59a
-    // Step 2 - marshal TPMTP
28a59a
-    TPMT_PUBLIC marshaled_tpmt;
28a59a
-    size_t tpmt_marshalled_size = 0;
28a59a
-    Tss2_MU_TPMT_PUBLIC_Marshal(&public->publicArea,
28a59a
-            (uint8_t *) &marshaled_tpmt, sizeof(public->publicArea),
28a59a
-            &tpmt_marshalled_size);
28a59a
-
28a59a
-    // Step 3 - Hash the data into name just past the alg type.
28a59a
-    digester d = tpm2_openssl_halg_to_digester(name_alg);
28a59a
-    if (!d) {
28a59a
-        return false;
28a59a
-    }
28a59a
-
28a59a
-    d((const unsigned char *) &marshaled_tpmt, tpmt_marshalled_size,
28a59a
-            pubname->name + 2);
28a59a
-
28a59a
-    //Set the name size, UINT16 followed by HASH
28a59a
-    UINT16 hash_size = tpm2_alg_util_get_hash_size(name_alg);
28a59a
-    pubname->size = hash_size + 2;
28a59a
-
28a59a
-    return true;
28a59a
-}
28a59a
-
28a59a
 static void create_import_key_private_data(TPM2B_PRIVATE *private,
28a59a
         TPMI_ALG_HASH parent_name_alg,
28a59a
         TPM2B_MAX_BUFFER *encrypted_duplicate_sensitive,
28a59a
@@ -155,7 +115,7 @@ static tool_rc key_import(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *parent_pub,
28a59a
      * Calculate the object name.
28a59a
      */
28a59a
     TPM2B_NAME pubname = TPM2B_TYPE_INIT(TPM2B_NAME, name);
28a59a
-    bool res = create_name(pubkey, &pubname);
28a59a
+    bool res = tpm2_identity_create_name(pubkey, &pubname);
28a59a
     if (!res) {
28a59a
         return false;
28a59a
     }
28a59a
-- 
28a59a
2.31.1
28a59a