From ff7a21eb948a8650ca97e34db1c05e5b0125747d Mon Sep 17 00:00:00 2001 From: Ondrej Kozina Date: Thu, 17 Apr 2025 10:49:57 +0200 Subject: [PATCH] Refactor get_adjusted_key_size. --- src/cryptsetup.c | 3 ++- src/utils_luks.c | 7 +++---- src/utils_luks.h | 3 ++- src/utils_reencrypt.c | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/cryptsetup.c b/src/cryptsetup.c index cc675a25..a6f70f67 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -1683,7 +1683,8 @@ int luksFormat(struct crypt_device **r_cd, char **r_password, size_t *r_password goto out; } - keysize = get_adjusted_key_size(cipher_mode, DEFAULT_LUKS1_KEYBITS, integrity_keysize); + keysize = get_adjusted_key_size(cipher_mode, ARG_UINT32(OPT_KEY_SIZE_ID), + DEFAULT_LUKS1_KEYBITS, integrity_keysize); if (ARG_SET(OPT_HW_OPAL_ONLY_ID)) keysize = opal_params.user_key_size; diff --git a/src/utils_luks.c b/src/utils_luks.c index 5bee9109..a0d964f6 100644 --- a/src/utils_luks.c +++ b/src/utils_luks.c @@ -139,12 +139,11 @@ int set_tries_tty(bool keyring) return (tools_is_stdin(ARG_STR(OPT_KEY_FILE_ID)) && isatty(STDIN_FILENO)) ? ARG_UINT32(OPT_TRIES_ID) : 1; } -int get_adjusted_key_size(const char *cipher_mode, uint32_t default_size_bits, int integrity_keysize) +int get_adjusted_key_size(const char *cipher_mode, uint32_t keysize_bits, + uint32_t default_size_bits, int integrity_keysize) { - uint32_t keysize_bits = ARG_UINT32(OPT_KEY_SIZE_ID); - #if ENABLE_LUKS_ADJUST_XTS_KEYSIZE - if (!ARG_SET(OPT_KEY_SIZE_ID) && !strncmp(cipher_mode, "xts-", 4)) { + if (!keysize_bits && !strncmp(cipher_mode, "xts-", 4)) { if (default_size_bits == 128) keysize_bits = 256; else if (default_size_bits == 256) diff --git a/src/utils_luks.h b/src/utils_luks.h index 30ced4ee..37fb289c 100644 --- a/src/utils_luks.h +++ b/src/utils_luks.h @@ -27,7 +27,8 @@ int set_pbkdf_params(struct crypt_device *cd, const char *dev_type); int set_tries_tty(bool keyring); -int get_adjusted_key_size(const char *cipher_mode, uint32_t default_size_bits, int integrity_keysize); +int get_adjusted_key_size(const char *cipher_mode, uint32_t keysize_bits, + uint32_t default_size_bits, int integrity_keysize); int luksFormat(struct crypt_device **r_cd, char **r_password, size_t *r_passwordLen); diff --git a/src/utils_reencrypt.c b/src/utils_reencrypt.c index d608682a..d598f55e 100644 --- a/src/utils_reencrypt.c +++ b/src/utils_reencrypt.c @@ -1146,7 +1146,8 @@ static int reencrypt_luks2_init(struct crypt_device *cd, const char *data_device /* key size */ if (ARG_SET(OPT_KEY_SIZE_ID) || new_cipher) - key_size = get_adjusted_key_size(mode, DEFAULT_LUKS1_KEYBITS, 0); + key_size = get_adjusted_key_size(mode, ARG_UINT32(OPT_KEY_SIZE_ID), + DEFAULT_LUKS1_KEYBITS, 0); else key_size = crypt_get_volume_key_size(cd);