From 8d35a8961052e49922a0eac228f8ddc476845bd5 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Mon, 6 Nov 2023 13:40:11 -0500 Subject: [PATCH] tpm2: add tpm2_sym_alg_*_string() and tpm2_sym_mode_*_string() Add functions to convert between alg id and string name for symmetric algorithms and symmetric encryption modes. (cherry picked from commit 2d784782bf700ae26dbeaa24e11ee8faebc29367) Related: RHEL-16182 --- src/shared/tpm2-util.c | 56 ++++++++++++++++++++++++++++++++++++++++++ src/shared/tpm2-util.h | 6 +++++ 2 files changed, 62 insertions(+) diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 4f1aafedf8..2e2a3f5fb0 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -4923,6 +4923,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; diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h index bf41bac76e..59c6a6fa92 100644 --- a/src/shared/tpm2-util.h +++ b/src/shared/tpm2-util.h @@ -309,6 +309,12 @@ int tpm2_hash_alg_from_string(const char *alg); const char *tpm2_asym_alg_to_string(uint16_t alg); int tpm2_asym_alg_from_string(const char *alg); +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); typedef struct {