]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2: add tpm2_pcr_values_has_(any|all)_values() functions
authorDan Streetman <ddstreet@ieee.org>
Mon, 21 Aug 2023 23:18:40 +0000 (19:18 -0400)
committerDan Streetman <ddstreet@ieee.org>
Thu, 24 Aug 2023 16:35:58 +0000 (12:35 -0400)
src/cryptenroll/cryptenroll-tpm2.c
src/shared/tpm2-util.c
src/shared/tpm2-util.h

index d7f81ede3f4c3879125401cb76edb105e81b1ff1..38920fb94e5174886c5d13dbfd047498895e7274 100644 (file)
@@ -211,12 +211,7 @@ int enroll_tpm2(struct crypt_device *cd,
         if (r < 0)
                 return r;
 
-        bool pcr_value_specified = false;
-        for (size_t i = 0; i < n_hash_pcr_values; i++)
-                if (hash_pcr_values[i].value.size > 0) {
-                        pcr_value_specified = true;
-                        break;
-                }
+        bool pcr_value_specified = tpm2_pcr_values_has_any_values(hash_pcr_values, n_hash_pcr_values);
 
         r = tpm2_pcr_read_missing_values(tpm2_context, hash_pcr_values, n_hash_pcr_values);
         if (r < 0)
index 0da85b3ff2983c7d052cc17b6a50ce0dede0b54c..83070ab8243e57c4f60ed6fc49f262c5ed741c5d 100644 (file)
@@ -1590,6 +1590,28 @@ bool tpm2_pcr_values_valid(const Tpm2PCRValue *pcr_values, size_t n_pcr_values)
         return true;
 }
 
+/* Returns true if any of the provided PCR values has an actual hash value included, false otherwise. */
+bool tpm2_pcr_values_has_any_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values) {
+        assert(pcr_values || n_pcr_values == 0);
+
+        FOREACH_ARRAY(v, pcr_values, n_pcr_values)
+                if (v->value.size > 0)
+                        return true;
+
+        return false;
+}
+
+/* Returns true if all of the provided PCR values has an actual hash value included, false otherwise. */
+bool tpm2_pcr_values_has_all_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values) {
+        assert(pcr_values || n_pcr_values == 0);
+
+        FOREACH_ARRAY(v, pcr_values, n_pcr_values)
+                if (v->value.size == 0)
+                        return false;
+
+        return true;
+}
+
 static int cmp_pcr_values(const Tpm2PCRValue *a, const Tpm2PCRValue *b) {
         assert(a);
         assert(b);
index e62a3c30e12fcacf355b0ce2e10e42d1212c5b8d..0fd376a2e6dbc9ac0a46b8b958624ac2ecdc4ad4 100644 (file)
@@ -83,6 +83,8 @@ typedef struct {
         }
 
 bool tpm2_pcr_value_valid(const Tpm2PCRValue *pcr_value);
+bool tpm2_pcr_values_has_any_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values);
+bool tpm2_pcr_values_has_all_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values);
 int tpm2_pcr_value_from_string(const char *arg, Tpm2PCRValue *ret_pcr_value);
 char *tpm2_pcr_value_to_string(const Tpm2PCRValue *pcr_value);