From 4708884d8ceef86e5619ff5657ea47ed5635c0e9 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Sat, 11 May 2024 18:34:41 +0200 Subject: [PATCH] Allow "capi:" cipher format for benchmark command. Note, currently AEAD modes are not supported. --- lib/crypto_backend/crypto_cipher_kernel.c | 10 +++++++--- src/cryptsetup.c | 17 +++++++++++++---- tests/compat-test | 7 +++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/crypto_backend/crypto_cipher_kernel.c b/lib/crypto_backend/crypto_cipher_kernel.c index 327aed2c..39047105 100644 --- a/lib/crypto_backend/crypto_cipher_kernel.c +++ b/lib/crypto_backend/crypto_cipher_kernel.c @@ -86,9 +86,13 @@ int crypt_cipher_init_kernel(struct crypt_cipher_kernel *ctx, const char *name, if (!strcmp(name, "cipher_null")) key_length = 0; - r = snprintf((char *)sa.salg_name, sizeof(sa.salg_name), "%s(%s)", mode, name); - if (r < 0 || (size_t)r >= sizeof(sa.salg_name)) - return -EINVAL; + if (!strncmp(name, "capi:", 5)) + strncpy((char *)sa.salg_name, &name[5], sizeof(sa.salg_name) - 1); + else { + r = snprintf((char *)sa.salg_name, sizeof(sa.salg_name), "%s(%s)", mode, name); + if (r < 0 || (size_t)r >= sizeof(sa.salg_name)) + return -EINVAL; + } return _crypt_cipher_init(ctx, key, key_length, 0, &sa); } diff --git a/src/cryptsetup.c b/src/cryptsetup.c index b7e2ca76..3c8226c3 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -1206,7 +1206,7 @@ static int action_benchmark(void) char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN]; double enc_mbr = 0, dec_mbr = 0; int key_size = (ARG_UINT32(OPT_KEY_SIZE_ID) ?: DEFAULT_PLAIN_KEYBITS) / 8; - int skipped = 0, width; + int skipped = 0, width, mode_len; char *c; int i, r; @@ -1226,13 +1226,22 @@ static int action_benchmark(void) r = benchmark_cipher_loop(cipher, cipher_mode, key_size, &enc_mbr, &dec_mbr); if (!r) { - width = strlen(cipher) + strlen(cipher_mode) + 1; + if (!strncmp(cipher, "capi:", 5)) + mode_len = 0; + else + mode_len = strlen(cipher_mode); + width = strlen(cipher) + mode_len + 1; if (width < 11) width = 11; + /* TRANSLATORS: The string is header of a table and must be exactly (right side) aligned. */ log_std(_("#%*s Algorithm | Key | Encryption | Decryption\n"), width - 11, ""); - log_std("%*s-%s %9db %10.1f MiB/s %10.1f MiB/s\n", width - (int)strlen(cipher_mode) - 1, - cipher, cipher_mode, key_size*8, enc_mbr, dec_mbr); + if (mode_len) + log_std("%*s-%s %9db %10.1f MiB/s %10.1f MiB/s\n", width - mode_len - 1, + cipher, cipher_mode, key_size*8, enc_mbr, dec_mbr); + else + log_std("%*s %9db %10.1f MiB/s %10.1f MiB/s\n", width, + cipher, key_size*8, enc_mbr, dec_mbr); } else if (r < 0) log_err(_("Cipher %s (with %i bits key) is not available."), ARG_STR(OPT_CIPHER_ID), key_size * 8); } else { diff --git a/tests/compat-test b/tests/compat-test index 2dd39d55..909b0a59 100755 --- a/tests/compat-test +++ b/tests/compat-test @@ -310,6 +310,13 @@ echo "[10] uuid" echo $PWD1 | $CRYPTSETUP -q luksFormat --type luks1 $FAST_PBKDF_OPT --uuid $TEST_UUID $IMG || fail $CRYPTSETUP -q luksUUID $IMG | grep -q $TEST_UUID || fail +echo "[11] benchmark" +$CRYPTSETUP benchmark -c aes-xts --key-size 128 >/dev/null 2>&1 && fail +$CRYPTSETUP benchmark -c aes-xts >/dev/null || fail +$CRYPTSETUP benchmark -c aes-xts-plain64 >/dev/null || fail +$CRYPTSETUP benchmark -c capi:xts\(aes\) >/dev/null || fail +$CRYPTSETUP benchmark -c capi:xts\(aes\)-plain64 >/dev/null || fail + [ $(id -u) != 0 ] && skip "WARNING: You must be root to run this test, test skipped." [ -z "$LOOPDEV" ] && skip "WARNING: Cannot find free loop device, test skipped." [ ! -x "$DIFFER" ] && skip "Cannot find $DIFFER, test skipped."