]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2: add tpm2_sym_alg_*_string() and tpm2_sym_mode_*_string()
authorDan Streetman <ddstreet@ieee.org>
Mon, 6 Nov 2023 18:40:11 +0000 (13:40 -0500)
committerDan Streetman <ddstreet@ieee.org>
Tue, 7 Nov 2023 15:59:44 +0000 (10:59 -0500)
Add functions to convert between alg id and string name for symmetric
algorithms and symmetric encryption modes.

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

index 9b2609c9a458a887073525b6e47cfa698fba6e69..9fa66cf5bf350762787cfb514257328211556030 100644 (file)
@@ -6423,6 +6423,62 @@ int tpm2_asym_alg_from_string(const char *alg) {
         return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown asymmetric algorithm name '%s'", alg);
 }
 
+const char *tpm2_sym_alg_to_string(uint16_t alg) {
+        switch (alg) {
+#if HAVE_TPM2
+        case TPM2_ALG_AES:
+                return "aes";
+#endif
+        default:
+                log_debug("Unknown symmetric algorithm id 0x%" PRIx16, alg);
+                return NULL;
+        }
+}
+
+int tpm2_sym_alg_from_string(const char *alg) {
+#if HAVE_TPM2
+        if (strcaseeq_ptr(alg, "aes"))
+                return TPM2_ALG_AES;
+#endif
+        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown symmetric algorithm name '%s'", alg);
+}
+
+const char *tpm2_sym_mode_to_string(uint16_t mode) {
+        switch (mode) {
+#if HAVE_TPM2
+        case TPM2_ALG_CTR:
+                return "ctr";
+        case TPM2_ALG_OFB:
+                return "ofb";
+        case TPM2_ALG_CBC:
+                return "cbc";
+        case TPM2_ALG_CFB:
+                return "cfb";
+        case TPM2_ALG_ECB:
+                return "ecb";
+#endif
+        default:
+                log_debug("Unknown symmetric mode id 0x%" PRIx16, mode);
+                return NULL;
+        }
+}
+
+int tpm2_sym_mode_from_string(const char *mode) {
+#if HAVE_TPM2
+        if (strcaseeq_ptr(mode, "ctr"))
+                return TPM2_ALG_CTR;
+        if (strcaseeq_ptr(mode, "ofb"))
+                return TPM2_ALG_OFB;
+        if (strcaseeq_ptr(mode, "cbc"))
+                return TPM2_ALG_CBC;
+        if (strcaseeq_ptr(mode, "cfb"))
+                return TPM2_ALG_CFB;
+        if (strcaseeq_ptr(mode, "ecb"))
+                return TPM2_ALG_ECB;
+#endif
+        return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown symmetric mode name '%s'", mode);
+}
+
 Tpm2Support tpm2_support(void) {
         Tpm2Support support = TPM2_SUPPORT_NONE;
         int r;
index 3ee3b43792d2be0a3a7df7cd4e62f7e1a2482166..6dd4d85afac0a4da6333b5afe85196b278d40fda 100644 (file)
@@ -412,6 +412,12 @@ int tpm2_hash_alg_from_string(const char *alg) _pure_;
 const char *tpm2_asym_alg_to_string(uint16_t alg) _const_;
 int tpm2_asym_alg_from_string(const char *alg) _pure_;
 
+const char *tpm2_sym_alg_to_string(uint16_t alg) _const_;
+int tpm2_sym_alg_from_string(const char *alg) _pure_;
+
+const char *tpm2_sym_mode_to_string(uint16_t mode) _const_;
+int tpm2_sym_mode_from_string(const char *mode) _pure_;
+
 char *tpm2_pcr_mask_to_string(uint32_t mask);
 
 extern const uint16_t tpm2_hash_algorithms[];