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

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