All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS
Date: Sun, 15 Nov 2026 22:28:49 -0800	[thread overview]
Message-ID: <20261116062849.1071038-8-ebiggers@kernel.org> (raw)
In-Reply-To: <20261116062849.1071038-1-ebiggers@kernel.org>

Add a KUnit test suite for the AES-XTS library API.

It consists of:

- All the shared test cases from skcipher-test-template.h.  These
  include extensive consistency tests, a "Monte-Carlo test", and a
  benchmark.

- Tests against hardcoded AES-XTS test vectors from IEEE 1619-2007,
  including ciphertext stealing test vectors.

- A test for weak key rejection (keys whose two halves are identical).

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 lib/crypto/.kunitconfig          |   1 +
 lib/crypto/tests/Kconfig         |   9 +
 lib/crypto/tests/Makefile        |   1 +
 lib/crypto/tests/aes_xts_kunit.c | 365 +++++++++++++++++++++++++++++++
 4 files changed, 376 insertions(+)
 create mode 100644 lib/crypto/tests/aes_xts_kunit.c

diff --git a/lib/crypto/.kunitconfig b/lib/crypto/.kunitconfig
index 465375a1a14c..1be91698a654 100644
--- a/lib/crypto/.kunitconfig
+++ b/lib/crypto/.kunitconfig
@@ -8,6 +8,7 @@ CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_CTR_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_GCM_KUNIT_TEST=y
+CONFIG_CRYPTO_LIB_AES_XTS_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_CHACHA_KUNIT_TEST=y
diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig
index 4f7a07b0e3c0..98fcf053e937 100644
--- a/lib/crypto/tests/Kconfig
+++ b/lib/crypto/tests/Kconfig
@@ -54,6 +54,15 @@ config CRYPTO_LIB_AES_GCM_KUNIT_TEST
 	help
 	  KUnit tests for the AES-GCM authenticated encryption algorithm.
 
+config CRYPTO_LIB_AES_XTS_KUNIT_TEST
+	tristate "KUnit tests for AES-XTS" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_LIB_AES_XTS
+	default KUNIT_ALL_TESTS
+	select CRYPTO_LIB_BENCHMARK_VISIBLE
+	# This test uses BLAKE2s, but that's always built-in.
+	help
+	  KUnit tests for the AES-XTS encryption mode.
+
 config CRYPTO_LIB_BLAKE2B_KUNIT_TEST
 	tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_BLAKE2B
diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile
index 50e4a3f70ebf..048ceb1d595d 100644
--- a/lib/crypto/tests/Makefile
+++ b/lib/crypto/tests/Makefile
@@ -6,6 +6,7 @@ obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CTR_KUNIT_TEST) += aes_ctr_kunit.o aes_xctr_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST) += aes_ecb_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_GCM_KUNIT_TEST) += aes_gcm_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_AES_XTS_KUNIT_TEST) += aes_xts_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_CHACHA_KUNIT_TEST) += chacha_kunit.o
diff --git a/lib/crypto/tests/aes_xts_kunit.c b/lib/crypto/tests/aes_xts_kunit.c
new file mode 100644
index 000000000000..b45741567d2f
--- /dev/null
+++ b/lib/crypto/tests/aes_xts_kunit.c
@@ -0,0 +1,365 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * KUnit test suite for AES-XTS
+ *
+ * Copyright 2026 Google LLC
+ */
+#include <crypto/aes-xts.h>
+#include <crypto/blake2s.h>
+#include <linux/fips.h>
+#include "test-utils.h"
+
+static int aes_xts_preparekey_test(struct aes_xts_key *key, const u8 *in_key,
+				   size_t key_len)
+{
+	return aes_xts_preparekey(key, in_key, key_len, 0);
+}
+
+/* In FIPS mode, AES-192-XTS (48-byte keys) isn't allowed. */
+static bool aes_xts_is_key_len_allowed(size_t key_len)
+{
+	return !fips_enabled || key_len != 2 * AES_KEYSIZE_192;
+}
+
+static const size_t aes_xts_valid_key_lens[] = { 2 * AES_KEYSIZE_128,
+						 2 * AES_KEYSIZE_192,
+						 2 * AES_KEYSIZE_256 };
+#define SKCIPHER_VALID_KEY_LENS aes_xts_valid_key_lens
+#define SKCIPHER_MAX_KEY_LEN (2 * AES_MAX_KEY_SIZE)
+#define SKCIPHER_IV_LEN AES_BLOCK_SIZE /* the tweak */
+#define SKCIPHER_MIN_DATA_LEN AES_BLOCK_SIZE
+#define SKCIPHER_DATA_LEN_ALIGN 1
+
+#define SKCIPHER_KEY aes_xts_key
+#define SKCIPHER_PREPAREKEY aes_xts_preparekey_test
+#define SKCIPHER_ENCRYPT aes_xts_encrypt
+#define SKCIPHER_DECRYPT aes_xts_decrypt
+
+#define SKCIPHER_IS_KEY_LEN_ALLOWED aes_xts_is_key_len_allowed
+#define SKCIPHER_INCREMENTAL
+#define SKCIPHER_NONFINAL_DATA_LEN_ALIGN AES_BLOCK_SIZE
+
+/* This value was generated by gen-skcipher-testvecs.py. */
+static const u8 aes_xts_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = {
+	0x24, 0x93, 0x8f, 0x89, 0x72, 0x6f, 0xfb, 0x59, 0x4a, 0xea, 0x32,
+	0x74, 0x15, 0x6a, 0x8f, 0x9a, 0xba, 0xde, 0x9e, 0xc3, 0x5c, 0x58,
+	0x98, 0xa9, 0xb6, 0x9f, 0xeb, 0xe5, 0xfb, 0x3a, 0xf0, 0x87,
+};
+#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_xts_monte_carlo_checksum
+
+#include "skcipher-test-template.h"
+
+/* Byte sequence 0x00..0xff, used in IEEE 1619-2007 Vector 10 */
+#define IEEE1619_SEQ256                    \
+	"\x00\x01\x02\x03\x04\x05\x06\x07" \
+	"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
+	"\x10\x11\x12\x13\x14\x15\x16\x17" \
+	"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
+	"\x20\x21\x22\x23\x24\x25\x26\x27" \
+	"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \
+	"\x30\x31\x32\x33\x34\x35\x36\x37" \
+	"\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
+	"\x40\x41\x42\x43\x44\x45\x46\x47" \
+	"\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
+	"\x50\x51\x52\x53\x54\x55\x56\x57" \
+	"\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" \
+	"\x60\x61\x62\x63\x64\x65\x66\x67" \
+	"\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
+	"\x70\x71\x72\x73\x74\x75\x76\x77" \
+	"\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" \
+	"\x80\x81\x82\x83\x84\x85\x86\x87" \
+	"\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
+	"\x90\x91\x92\x93\x94\x95\x96\x97" \
+	"\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
+	"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7" \
+	"\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
+	"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7" \
+	"\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
+	"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7" \
+	"\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
+	"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7" \
+	"\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
+	"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7" \
+	"\xe8\xe9\xea\xeb\xec\xed\xee\xef" \
+	"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7" \
+	"\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
+
+/* Inputs shared by IEEE 1619-2007 Vectors 15-17 */
+#define IEEE1619_VEC15_KEY                 \
+	"\xff\xfe\xfd\xfc\xfb\xfa\xf9\xf8" \
+	"\xf7\xf6\xf5\xf4\xf3\xf2\xf1\xf0" \
+	"\xbf\xbe\xbd\xbc\xbb\xba\xb9\xb8" \
+	"\xb7\xb6\xb5\xb4\xb3\xb2\xb1\xb0"
+#define IEEE1619_VEC15_TWEAK               \
+	"\x9a\x78\x56\x34\x12\x00\x00\x00" \
+	"\x00\x00\x00\x00\x00\x00\x00\x00"
+#define IEEE1619_VEC15_PTEXT               \
+	"\x00\x01\x02\x03\x04\x05\x06\x07" \
+	"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
+	"\x10\x11\x12"
+
+/*
+ * AES-XTS test vectors from IEEE Std 1619-2007.  Vector 10 covers AES-256-XTS
+ * with a 512-byte data unit, and Vectors 15-17 cover ciphertext stealing.
+ */
+static const struct skcipher_testvec aes_xts_testvecs[] = {
+	{
+		.name = "IEEE 1619-2007 Vector 1",
+		.key = "\x00\x00\x00\x00\x00\x00\x00\x00"
+		       "\x00\x00\x00\x00\x00\x00\x00\x00"
+		       "\x00\x00\x00\x00\x00\x00\x00\x00"
+		       "\x00\x00\x00\x00\x00\x00\x00\x00",
+		.key_len = 32,
+		.iv = "\x00\x00\x00\x00\x00\x00\x00\x00"
+		      "\x00\x00\x00\x00\x00\x00\x00\x00",
+		.ptext = "\x00\x00\x00\x00\x00\x00\x00\x00"
+			 "\x00\x00\x00\x00\x00\x00\x00\x00"
+			 "\x00\x00\x00\x00\x00\x00\x00\x00"
+			 "\x00\x00\x00\x00\x00\x00\x00\x00",
+		.ctext = "\x91\x7c\xf6\x9e\xbd\x68\xb2\xec"
+			 "\x9b\x9f\xe9\xa3\xea\xdd\xa6\x92"
+			 "\xcd\x43\xd2\xf5\x95\x98\xed\x85"
+			 "\x8c\x02\xc2\x65\x2f\xbf\x92\x2e",
+		.data_len = 32,
+		.next_iv = "\x98\xa5\x2f\x51\xbf\x2b\xb2\xec"
+			   "\x20\x32\xe9\x67\x29\xd3\xac\xb8",
+	},
+	{
+		.name = "IEEE 1619-2007 Vector 2",
+		.key = "\x11\x11\x11\x11\x11\x11\x11\x11"
+		       "\x11\x11\x11\x11\x11\x11\x11\x11"
+		       "\x22\x22\x22\x22\x22\x22\x22\x22"
+		       "\x22\x22\x22\x22\x22\x22\x22\x22",
+		.key_len = 32,
+		.iv = "\x33\x33\x33\x33\x33\x00\x00\x00"
+		      "\x00\x00\x00\x00\x00\x00\x00\x00",
+		.ptext = "\x44\x44\x44\x44\x44\x44\x44\x44"
+			 "\x44\x44\x44\x44\x44\x44\x44\x44"
+			 "\x44\x44\x44\x44\x44\x44\x44\x44"
+			 "\x44\x44\x44\x44\x44\x44\x44\x44",
+		.ctext = "\xc4\x54\x18\x5e\x6a\x16\x93\x6e"
+			 "\x39\x33\x40\x38\xac\xef\x83\x8b"
+			 "\xfb\x18\x6f\xff\x74\x80\xad\xc4"
+			 "\x28\x93\x82\xec\xd6\xd3\x94\xf0",
+		.data_len = 32,
+		.next_iv = "\xf2\x01\xee\x34\x37\xfc\x49\xcf"
+			   "\xd6\x61\x05\x7d\x66\x55\x37\x9b",
+	},
+	{
+		.name = "IEEE 1619-2007 Vector 10",
+		.key = "\x27\x18\x28\x18\x28\x45\x90\x45"
+		       "\x23\x53\x60\x28\x74\x71\x35\x26"
+		       "\x62\x49\x77\x57\x24\x70\x93\x69"
+		       "\x99\x59\x57\x49\x66\x96\x76\x27"
+		       "\x31\x41\x59\x26\x53\x58\x97\x93"
+		       "\x23\x84\x62\x64\x33\x83\x27\x95"
+		       "\x02\x88\x41\x97\x16\x93\x99\x37"
+		       "\x51\x05\x82\x09\x74\x94\x45\x92",
+		.key_len = 64,
+		.iv = "\xff\x00\x00\x00\x00\x00\x00\x00"
+		      "\x00\x00\x00\x00\x00\x00\x00\x00",
+		.ptext = IEEE1619_SEQ256 IEEE1619_SEQ256,
+		.ctext = "\x1c\x3b\x3a\x10\x2f\x77\x03\x86"
+			 "\xe4\x83\x6c\x99\xe3\x70\xcf\x9b"
+			 "\xea\x00\x80\x3f\x5e\x48\x23\x57"
+			 "\xa4\xae\x12\xd4\x14\xa3\xe6\x3b"
+			 "\x5d\x31\xe2\x76\xf8\xfe\x4a\x8d"
+			 "\x66\xb3\x17\xf9\xac\x68\x3f\x44"
+			 "\x68\x0a\x86\xac\x35\xad\xfc\x33"
+			 "\x45\xbe\xfe\xcb\x4b\xb1\x88\xfd"
+			 "\x57\x76\x92\x6c\x49\xa3\x09\x5e"
+			 "\xb1\x08\xfd\x10\x98\xba\xec\x70"
+			 "\xaa\xa6\x69\x99\xa7\x2a\x82\xf2"
+			 "\x7d\x84\x8b\x21\xd4\xa7\x41\xb0"
+			 "\xc5\xcd\x4d\x5f\xff\x9d\xac\x89"
+			 "\xae\xba\x12\x29\x61\xd0\x3a\x75"
+			 "\x71\x23\xe9\x87\x0f\x8a\xcf\x10"
+			 "\x00\x02\x08\x87\x89\x14\x29\xca"
+			 "\x2a\x3e\x7a\x7d\x7d\xf7\xb1\x03"
+			 "\x55\x16\x5c\x8b\x9a\x6d\x0a\x7d"
+			 "\xe8\xb0\x62\xc4\x50\x0d\xc4\xcd"
+			 "\x12\x0c\x0f\x74\x18\xda\xe3\xd0"
+			 "\xb5\x78\x1c\x34\x80\x3f\xa7\x54"
+			 "\x21\xc7\x90\xdf\xe1\xde\x18\x34"
+			 "\xf2\x80\xd7\x66\x7b\x32\x7f\x6c"
+			 "\x8c\xd7\x55\x7e\x12\xac\x3a\x0f"
+			 "\x93\xec\x05\xc5\x2e\x04\x93\xef"
+			 "\x31\xa1\x2d\x3d\x92\x60\xf7\x9a"
+			 "\x28\x9d\x6a\x37\x9b\xc7\x0c\x50"
+			 "\x84\x14\x73\xd1\xa8\xcc\x81\xec"
+			 "\x58\x3e\x96\x45\xe0\x7b\x8d\x96"
+			 "\x70\x65\x5b\xa5\xbb\xcf\xec\xc6"
+			 "\xdc\x39\x66\x38\x0a\xd8\xfe\xcb"
+			 "\x17\xb6\xba\x02\x46\x9a\x02\x0a"
+			 "\x84\xe1\x8e\x8f\x84\x25\x20\x70"
+			 "\xc1\x3e\x9f\x1f\x28\x9b\xe5\x4f"
+			 "\xbc\x48\x14\x57\x77\x8f\x61\x60"
+			 "\x15\xe1\x32\x7a\x02\xb1\x40\xf1"
+			 "\x50\x5e\xb3\x09\x32\x6d\x68\x37"
+			 "\x8f\x83\x74\x59\x5c\x84\x9d\x84"
+			 "\xf4\xc3\x33\xec\x44\x23\x88\x51"
+			 "\x43\xcb\x47\xbd\x71\xc5\xed\xae"
+			 "\x9b\xe6\x9a\x2f\xfe\xce\xb1\xbe"
+			 "\xc9\xde\x24\x4f\xbe\x15\x99\x2b"
+			 "\x11\xb7\x7c\x04\x0f\x12\xbd\x8f"
+			 "\x6a\x97\x5a\x44\xa0\xf9\x0c\x29"
+			 "\xa9\xab\xc3\xd4\xd8\x93\x92\x72"
+			 "\x84\xc5\x87\x54\xcc\xe2\x94\x52"
+			 "\x9f\x86\x14\xdc\xd2\xab\xa9\x91"
+			 "\x92\x5f\xed\xc4\xae\x74\xff\xac"
+			 "\x6e\x33\x3b\x93\xeb\x4a\xff\x04"
+			 "\x79\xda\x9a\x41\x0e\x44\x50\xe0"
+			 "\xdd\x7a\xe4\xc6\xe2\x91\x09\x00"
+			 "\x57\x5d\xa4\x01\xfc\x07\x05\x9f"
+			 "\x64\x5e\x8b\x7e\x9b\xfd\xef\x33"
+			 "\x94\x30\x54\xff\x84\x01\x14\x93"
+			 "\xc2\x7b\x34\x29\xea\xed\xb4\xed"
+			 "\x53\x76\x44\x1a\x77\xed\x43\x85"
+			 "\x1a\xd7\x7f\x16\xf5\x41\xdf\xd2"
+			 "\x69\xd5\x0d\x6a\x5f\x14\xfb\x0a"
+			 "\xab\x1c\xbb\x4c\x15\x50\xbe\x97"
+			 "\xf7\xab\x40\x66\x19\x3c\x4c\xaa"
+			 "\x77\x3d\xad\x38\x01\x4b\xd2\x09"
+			 "\x2f\xa7\x55\xc8\x24\xbb\x5e\x54"
+			 "\xc4\xf3\x6f\xfd\xa9\xfc\xea\x70"
+			 "\xb9\xc6\xe6\x93\xe1\x48\xc1\x51",
+		.data_len = 512,
+		.next_iv = "\xd7\xcf\x75\x24\x5c\x26\x75\x77"
+			   "\xa9\x0c\xaa\xd6\xae\x98\x8e\x22",
+	},
+	{
+		.name = "IEEE 1619-2007 Vector 15",
+		.key = IEEE1619_VEC15_KEY,
+		.key_len = 32,
+		.iv = IEEE1619_VEC15_TWEAK,
+		.ptext = IEEE1619_VEC15_PTEXT,
+		.ctext = "\x6c\x16\x25\xdb\x46\x71\x52\x2d"
+			 "\x3d\x75\x99\x60\x1d\xe7\xca\x09"
+			 "\xed",
+		.data_len = 17,
+	},
+	{
+		.name = "IEEE 1619-2007 Vector 16",
+		.key = IEEE1619_VEC15_KEY,
+		.key_len = 32,
+		.iv = IEEE1619_VEC15_TWEAK,
+		.ptext = IEEE1619_VEC15_PTEXT,
+		.ctext = "\xd0\x69\x44\x4b\x7a\x7e\x0c\xab"
+			 "\x09\xe2\x44\x47\xd2\x4d\xeb\x1f"
+			 "\xed\xbf",
+		.data_len = 18,
+	},
+	{
+		.name = "IEEE 1619-2007 Vector 17",
+		.key = IEEE1619_VEC15_KEY,
+		.key_len = 32,
+		.iv = IEEE1619_VEC15_TWEAK,
+		.ptext = IEEE1619_VEC15_PTEXT,
+		.ctext = "\xe5\xdf\x13\x51\xc0\x54\x4b\xa1"
+			 "\x35\x0b\x33\x63\xcd\x8e\xf4\xbe"
+			 "\xed\xbf\x9d",
+		.data_len = 19,
+	},
+};
+
+static void test_aes_xts_test_vectors(struct kunit *test)
+{
+	for (size_t i = 0; i < ARRAY_SIZE(aes_xts_testvecs); i++) {
+		const struct skcipher_testvec *tv = &aes_xts_testvecs[i];
+		const size_t half = tv->key_len / 2;
+
+		/*
+		 * In FIPS mode, keys whose two halves are the same are
+		 * rejected, so skip any test vectors that use such keys.
+		 */
+		if (fips_enabled && memcmp(tv->key, &tv->key[half], half) == 0)
+			continue;
+		skcipher_run_testvecs(test, tv, 1);
+	}
+}
+
+/*
+ * Test that keys whose two halves are the same are rejected when
+ * XTS_FORBID_WEAK_KEYS is given (or in FIPS mode), and otherwise accepted, and
+ * that struct aes_xts_key is zeroized on failure.
+ */
+static void test_aes_xts_weak_keys(struct kunit *test)
+{
+	struct aes_xts_key *key = alloc_buf(test, sizeof(*key));
+	u8 *raw_key_buf_end = alloc_guarded_buf(test, SKCIPHER_MAX_KEY_LEN) +
+			      SKCIPHER_MAX_KEY_LEN;
+	int err;
+
+	for (size_t i = 0; i < ARRAY_SIZE(aes_xts_valid_key_lens); i++) {
+		const size_t key_len = aes_xts_valid_key_lens[i];
+		const size_t half = key_len / 2;
+		const bool allowed = aes_xts_is_key_len_allowed(key_len);
+		u8 *raw_key = raw_key_buf_end - key_len;
+
+		/* Generate a weak key in an exact-size guarded buffer. */
+		rand_bytes(raw_key, half);
+		memcpy(&raw_key[half], raw_key, half);
+
+		memset(key, 0xff, sizeof(*key));
+		err = aes_xts_preparekey(key, raw_key, key_len,
+					 XTS_FORBID_WEAK_KEYS);
+		KUNIT_ASSERT_EQ_MSG(
+			test, -EINVAL, err,
+			"Weak key wasn't rejected with XTS_FORBID_WEAK_KEYS; key_len=%zu",
+			key_len);
+		KUNIT_ASSERT_TRUE_MSG(
+			test, mem_is_zero(key, sizeof(*key)),
+			"Key struct wasn't zeroized on failure; key_len=%zu",
+			key_len);
+
+		memset(key, 0xff, sizeof(*key));
+		err = aes_xts_preparekey(key, raw_key, key_len, 0);
+		if (fips_enabled)
+			KUNIT_ASSERT_EQ_MSG(
+				test, -EINVAL, err,
+				"Weak key wasn't rejected in FIPS mode; key_len=%zu",
+				key_len);
+		else
+			KUNIT_ASSERT_EQ_MSG(
+				test, 0, err,
+				"Weak key wasn't accepted in non-FIPS mode; key_len=%zu",
+				key_len);
+		if (err)
+			KUNIT_ASSERT_TRUE(test, mem_is_zero(key, sizeof(*key)));
+
+		/*
+		 * Make the key non-weak by flipping a bit at each byte position
+		 * in turn, to verify that all bytes of the halves are compared.
+		 */
+		for (size_t j = 0; j < key_len; j++) {
+			raw_key[j] ^= 1;
+			memset(key, 0xff, sizeof(*key));
+			err = aes_xts_preparekey(key, raw_key, key_len,
+						 XTS_FORBID_WEAK_KEYS);
+			KUNIT_ASSERT_EQ_MSG(
+				test, allowed ? 0 : -EINVAL, err,
+				"Wrong result for non-weak key; key_len=%zu diff_byte=%zu",
+				key_len, j);
+			if (err)
+				KUNIT_ASSERT_TRUE(
+					test, mem_is_zero(key, sizeof(*key)));
+			raw_key[j] ^= 1;
+		}
+	}
+}
+
+static struct kunit_case aes_xts_test_cases[] = {
+	KUNIT_CASE(test_aes_xts_test_vectors),
+	KUNIT_CASE(test_aes_xts_weak_keys),
+	SKCIPHER_KUNIT_CASES,
+	{},
+};
+
+static struct kunit_suite aes_xts_test_suite = {
+	.name = "aes_xts",
+	.test_cases = aes_xts_test_cases,
+};
+kunit_test_suite(aes_xts_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for AES-XTS");
+MODULE_LICENSE("GPL");
-- 
2.56.1


      parent reply	other threads:[~2026-11-16  6:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers
2026-11-16  6:28 ` [PATCH 1/7] lib/crypto: tests: Add skcipher-test-template.h and gen-skcipher-testvecs.py Eric Biggers
2026-11-16  6:28 ` [PATCH 2/7] lib/crypto: tests: Add KUnit test suite for AES-ECB Eric Biggers
2026-11-16  6:28 ` [PATCH 3/7] lib/crypto: tests: Add KUnit test suite for AES-CBC Eric Biggers
2026-11-16  6:28 ` [PATCH 4/7] lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS Eric Biggers
2026-11-16  6:28 ` [PATCH 5/7] lib/crypto: tests: Add KUnit test suite for AES-CTR Eric Biggers
2026-11-16  6:28 ` [PATCH 6/7] lib/crypto: tests: Add KUnit test suite for AES-XCTR Eric Biggers
2026-11-16  6:28 ` Eric Biggers [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116062849.1071038-8-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ardb@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.