From: Dan Streetman Date: Mon, 6 Nov 2023 18:40:11 +0000 (-0500) Subject: tpm2: add tpm2_sym_alg_*_string() and tpm2_sym_mode_*_string() X-Git-Tag: v255-rc2~84^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2d784782bf700ae26dbeaa24e11ee8faebc29367;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 9b2609c9a45..9fa66cf5bf3 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -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; diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h index 3ee3b43792d..6dd4d85afac 100644 --- a/src/shared/tpm2-util.h +++ b/src/shared/tpm2-util.h @@ -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[];