]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2: add tpm2_hash_alg_to_size()
authorDan Streetman <ddstreet@ieee.org>
Tue, 11 Jul 2023 15:11:59 +0000 (11:11 -0400)
committerDan Streetman <ddstreet@ieee.org>
Fri, 4 Aug 2023 14:55:53 +0000 (10:55 -0400)
Add function to get the hash size for a hash algorithm

src/shared/tpm2-util.c
src/shared/tpm2-util.h

index a26bf35c70cbed6199c932d63689519e45018430..88cd7144b9760754744e06642606a3e3e8b71474 100644 (file)
@@ -4239,6 +4239,18 @@ int tpm2_parse_luks2_json(
         return 0;
 }
 
+int tpm2_hash_alg_to_size(uint16_t alg) {
+        if (alg == TPM2_ALG_SHA1)
+                return 20;
+        if (alg == TPM2_ALG_SHA256)
+                return 32;
+        if (alg == TPM2_ALG_SHA384)
+                return 48;
+        if (alg == TPM2_ALG_SHA512)
+                return 64;
+        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown hash algorithm id 0x%" PRIx16, alg);
+}
+
 const char *tpm2_hash_alg_to_string(uint16_t alg) {
         if (alg == TPM2_ALG_SHA1)
                 return "sha1";
index 12f05b43f9569c8be9e53010bdce520ac8b06e56..9ea8e5bda330e31d1e17aace45ed19191685de0e 100644 (file)
@@ -164,6 +164,8 @@ int tpm2_parse_luks2_json(JsonVariant *v, int *ret_keyslot, uint32_t *ret_hash_p
 #define TPM2_ALG_RSA 0x1
 #endif
 
+int tpm2_hash_alg_to_size(uint16_t alg);
+
 const char *tpm2_hash_alg_to_string(uint16_t alg);
 int tpm2_hash_alg_from_string(const char *alg);