Skip to contentSkip to conversation

lib/crypto: KUnit tests for unauthenticated AES modes

PATCH Applied Eric Biggers 1 participant 9 messages Nov 16 – Nov 21

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

cover lib/crypto: KUnit tests for unauthenticated AES modes

This series applies to libcrypto-next. It can also be retrieved from:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git aes-kunit-v1

This series adds comprehensive KUnit test suites for the remaining AES modes that got new library APIs in v7.3: AES-ECB, AES-CBC, AES-CBC-CTS, AES-CTR, AES-XCTR, and AES-XTS.

Most of the test code is placed in a common header file, like it is in many of the existing tests.

Eric Biggers (7):
lib/crypto: tests: Add skcipher-test-template.h and
gen-skcipher-testvecs.py
lib/crypto: tests: Add KUnit test suite for AES-ECB lib/crypto: tests: Add KUnit test suite for AES-CBC lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS lib/crypto: tests: Add KUnit test suite for AES-CTR lib/crypto: tests: Add KUnit test suite for AES-XCTR lib/crypto: tests: Add KUnit test suite for AES-XTS

 lib/crypto/.kunitconfig                   |    4 +
 lib/crypto/tests/Kconfig                  |   36 +
 lib/crypto/tests/Makefile                 |    4 +
 lib/crypto/tests/aead-test-template.h     |   67 +-
 lib/crypto/tests/aes_cbc_cts_kunit.c      |  239 ++++
 lib/crypto/tests/aes_cbc_kunit.c          |  137 +++
 lib/crypto/tests/aes_ctr_kunit.c          |  196 ++++
 lib/crypto/tests/aes_ecb_kunit.c          |  227 ++++
 lib/crypto/tests/aes_xctr_kunit.c         |  149 +++
 lib/crypto/tests/aes_xts_kunit.c          |  365 ++++++
 lib/crypto/tests/skcipher-test-template.h | 1241 +++++++++++++++++++++
 lib/crypto/tests/sp800-38a-data.h         |   36 +
 lib/crypto/tests/test-utils.h             |   25 +
 scripts/crypto/gen-skcipher-testvecs.py   |  167 +++
 14 files changed, 2846 insertions(+), 47 deletions(-)
 create mode 100644 lib/crypto/tests/aes_cbc_cts_kunit.c
 create mode 100644 lib/crypto/tests/aes_cbc_kunit.c
 create mode 100644 lib/crypto/tests/aes_ctr_kunit.c
 create mode 100644 lib/crypto/tests/aes_ecb_kunit.c
 create mode 100644 lib/crypto/tests/aes_xctr_kunit.c
 create mode 100644 lib/crypto/tests/aes_xts_kunit.c
 create mode 100644 lib/crypto/tests/skcipher-test-template.h
 create mode 100644 lib/crypto/tests/sp800-38a-data.h
 create mode 100755 scripts/crypto/gen-skcipher-testvecs.py

base-commit: 84abb955b1015477aa11a96e2f3ac1b938b77f48

Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/7 lib/crypto: tests: Add skcipher-test-template.h and gen-skcipher-testvecs.py

Add skcipher-test-template.h which contains shared test logic for length-preserving symmetric ciphers, analogous to the existing hash-test-template.h and aead-test-template.h. It generates the following KUnit test cases:

test_skcipher_all_key_lens
test_skcipher_monte_carlo
test_skcipher_incremental (when incremental operation is supported) test_skcipher_incremental_all_splits (ditto) test_skcipher_buffer_overruns test_skcipher_alignment_consistency test_skcipher_inplace
test_skcipher_enckey (when encryption-only key struct is supported) test_skcipher_interrupt_context
benchmark_skcipher (when CONFIG_CRYPTO_LIB_BENCHMARK=y)

The initial use cases for this will be AES-ECB, AES-CBC, AES-CBC-CTS, AES-CTR, AES-XCTR, and AES-XTS.

Move a couple helper functions from aead-test-template.h into test-utils.h so that skcipher-test-template.h can use them too.

Also add a Python script gen-skcipher-testvecs.py which generates the expected Monte-Carlo checksum for each mode using python-cryptography and reference implementations.

---
 lib/crypto/tests/aead-test-template.h     |   67 +-
 lib/crypto/tests/skcipher-test-template.h | 1241 +++++++++++++++++++++
 lib/crypto/tests/test-utils.h             |   25 +
 scripts/crypto/gen-skcipher-testvecs.py   |  167 +++
 4 files changed, 1453 insertions(+), 47 deletions(-)
 create mode 100644 lib/crypto/tests/skcipher-test-template.h
 create mode 100755 scripts/crypto/gen-skcipher-testvecs.py
lib/crypto/tests/aead-test-template.h +20 -47
index 894a927c6f39..7689567c86f9 100644
@@ -64,30 +64,6 @@ static struct AEAD_KEY *aead_alloc_random_key(struct kunit *test,
 	return key;
 }
 
-/*
- * Allocate a KUnit-managed slab buffer of length @len bytes and initialize it
- * with random data.
- */
-static u8 *aead_alloc_random_data(struct kunit *test, size_t len)
-{
-	u8 *buf = alloc_buf(test, len);
-
-	rand_bytes(buf, len);
-	return buf;
-}
-
-/*
- * Allocate a KUnit-managed guarded buffer of length @len bytes and initialize
- * it with random data.
- */
-static u8 *aead_alloc_random_data_guarded(struct kunit *test, size_t len)
-{
-	u8 *buf = alloc_guarded_buf(test, len);
-
-	rand_bytes(buf, len);
-	return buf;
-}
-
 /* Process the given associated data using a random incremental strategy. */
 static size_t aead_auth_incrementally(struct AEAD_CTX *ctx, const u8 *ad,
 				      size_t ad_len)
@@ -262,15 +238,12 @@ aead_alloc_basic_validation_test_ctx(struct kunit *test)
 	 * dst on authentication failure can actually fail.
 	 */
 	rand_bytes(ctx->pt, sizeof(ctx->pt));
-	ctx->raw_key_buf_end =
-		aead_alloc_random_data_guarded(test, AEAD_MAX_KEY_LEN) +
-		AEAD_MAX_KEY_LEN;
-	ctx->nonce_buf_end =
-		aead_alloc_random_data_guarded(test, AEAD_MAX_NONCE_LEN) +
-		AEAD_MAX_NONCE_LEN;
-	ctx->tag_buf_end =
-		aead_alloc_random_data_guarded(test, AEAD_MAX_TAG_LEN) +
-		AEAD_MAX_TAG_LEN;
+	ctx->raw_key_buf_end = alloc_guarded_rand_buf(test, AEAD_MAX_KEY_LEN) +
+			       AEAD_MAX_KEY_LEN;
+	ctx->nonce_buf_end = alloc_guarded_rand_buf(test, AEAD_MAX_NONCE_LEN) +
+			     AEAD_MAX_NONCE_LEN;
+	ctx->tag_buf_end = alloc_guarded_rand_buf(test, AEAD_MAX_TAG_LEN) +
+			   AEAD_MAX_TAG_LEN;
 
 	/*
 	 * A pointer to this buffer is passed when passing a length that is
@@ -519,9 +492,9 @@ static void test_aead_incremental_updates(struct kunit *test)
 	size_t tag_len;
 	struct AEAD_KEY *key = aead_alloc_random_key(test, &tag_len);
 	struct AEAD_CTX *ctx = alloc_buf(test, sizeof(*ctx));
-	u8 *pt = aead_alloc_random_data(test, max_data_len);
-	u8 *ad = aead_alloc_random_data(test, max_ad_len);
-	u8 *nonce = aead_alloc_random_data(test, nonce_len);
+	u8 *pt = alloc_rand_buf(test, max_data_len);
+	u8 *ad = alloc_rand_buf(test, max_ad_len);
+	u8 *nonce = alloc_rand_buf(test, nonce_len);
 	u8 *ct = alloc_buf(test, max_data_len);
 	u8 *ct2 = alloc_buf(test, max_data_len);
 	u8 *decrypted = alloc_buf(test, max_data_len);
@@ -584,11 +557,11 @@ static void test_aead_data_buffer_overruns(struct kunit *test)
 	const size_t nonce_len = AEAD_MAX_NONCE_LEN;
 	size_t tag_len;
 	struct AEAD_KEY *key = aead_alloc_random_key(test, &tag_len);
-	const u8 *nonce = aead_alloc_random_data(test, nonce_len);
-	const u8 *pt_end = aead_alloc_random_data_guarded(test, max_data_len) +
-			   max_data_len;
+	const u8 *nonce = alloc_rand_buf(test, nonce_len);
+	const u8 *pt_end =
+		alloc_guarded_rand_buf(test, max_data_len) + max_data_len;
 	const u8 *ad_end =
-		aead_alloc_random_data_guarded(test, max_ad_len) + max_ad_len;
+		alloc_guarded_rand_buf(test, max_ad_len) + max_ad_len;
 	u8 *ct_end = alloc_guarded_buf(test, max_data_len) + max_data_len;
 	u8 *decrypted_end =
 		alloc_guarded_buf(test, max_data_len) + max_data_len;
@@ -725,10 +698,10 @@ static void test_aead_inplace(struct kunit *test)
 	const size_t nonce_len = AEAD_MAX_NONCE_LEN;
 	size_t tag_len;
 	struct AEAD_KEY *key = aead_alloc_random_key(test, &tag_len);
-	u8 *data = aead_alloc_random_data(test, max_data_len + tag_len);
+	u8 *data = alloc_rand_buf(test, max_data_len + tag_len);
 	u8 *data2 = alloc_buf(test, max_data_len + tag_len);
-	u8 *ad = aead_alloc_random_data(test, max_ad_len);
-	const u8 *nonce = aead_alloc_random_data(test, nonce_len);
+	u8 *ad = alloc_rand_buf(test, max_ad_len);
+	const u8 *nonce = alloc_rand_buf(test, nonce_len);
 
 	for (int i = 0; i < 100; i++) {
 		size_t data_len = rand_length(max_data_len);
@@ -970,10 +943,10 @@ static void benchmark_aead(struct kunit *test)
 	if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK))
 		kunit_skip(test, "not enabled");
 
-	raw_key = aead_alloc_random_data(test, key_len);
-	nonce = aead_alloc_random_data(test, nonce_len);
-	ad = aead_alloc_random_data(test, ad_len);
-	pt = aead_alloc_random_data(test, max_data_len);
+	raw_key = alloc_rand_buf(test, key_len);
+	nonce = alloc_rand_buf(test, nonce_len);
+	ad = alloc_rand_buf(test, ad_len);
+	pt = alloc_rand_buf(test, max_data_len);
 	ct = alloc_buf(test, max_data_len);
 	tag = alloc_buf(test, tag_len);
 
lib/crypto/tests/skcipher-test-template.h new file +1241 -0

1,241 lines not on this page · raw message

lib/crypto/tests/test-utils.h +25 -0
index 73ca21f9176e..c39b1dd93453 100644
@@ -73,6 +73,31 @@ static __maybe_unused void rand_bytes(u8 *out, size_t len)
 		out[i] = rand32();
 }
 
+/*
+ * Allocate a KUnit-managed slab buffer of length @size bytes and initialize it
+ * with random data.
+ */
+static __maybe_unused void *alloc_rand_buf(struct kunit *test, size_t size)
+{
+	void *buf = alloc_buf(test, size);
+
+	rand_bytes(buf, size);
+	return buf;
+}
+
+/*
+ * Allocate a KUnit-managed guarded buffer of length @size bytes (> 0) and
+ * initialize it with random data.
+ */
+static __maybe_unused void *alloc_guarded_rand_buf(struct kunit *test,
+						   size_t size)
+{
+	void *buf = alloc_guarded_buf(test, size);
+
+	rand_bytes(buf, size);
+	return buf;
+}
+
 static __maybe_unused void rand_bytes_seeded_from_len(u8 *out, size_t len)
 {
 	random_seed = len;
scripts/crypto/gen-skcipher-testvecs.py new file +167 -0

167 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/7 lib/crypto: tests: Add KUnit test suite for AES-ECB

In reply to Eric Biggers

Add a KUnit test suite for the AES-ECB 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-ECB test vectors from NIST SP 800-38A.

- A test comparing multi-block AES-ECB en/decryption against single-block aes_encrypt() and aes_decrypt(), which also provides test coverage for the single-block AES functions.

---
 lib/crypto/.kunitconfig           |   1 +
 lib/crypto/tests/Kconfig          |   9 ++
 lib/crypto/tests/Makefile         |   1 +
 lib/crypto/tests/aes_ecb_kunit.c  | 227 ++++++++++++++++++++++++++++++
 lib/crypto/tests/sp800-38a-data.h |  36 +++++
 5 files changed, 274 insertions(+)
 create mode 100644 lib/crypto/tests/aes_ecb_kunit.c
 create mode 100644 lib/crypto/tests/sp800-38a-data.h
lib/crypto/.kunitconfig +1 -0
index 6d83badf4292..e323e955ceff 100644
@@ -4,6 +4,7 @@ CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y
 
 CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST=y
+CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_GCM_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y
lib/crypto/tests/Kconfig +9 -0
index 5a86a02d25b7..54af41f3c07d 100644
@@ -18,6 +18,15 @@ config CRYPTO_LIB_AES_CCM_KUNIT_TEST
 	help
 	  KUnit tests for the AES-CCM authenticated encryption algorithm.
 
+config CRYPTO_LIB_AES_ECB_KUNIT_TEST
+	tristate "KUnit tests for AES-ECB" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_LIB_AES_ECB
+	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-ECB encryption mode.
+
 config CRYPTO_LIB_AES_GCM_KUNIT_TEST
 	tristate "KUnit tests for AES-GCM" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_AES_GCM
lib/crypto/tests/Makefile +1 -0
index 40c9624eaaa8..1a5be11ce378 100644
@@ -2,6 +2,7 @@
 
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_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_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o
lib/crypto/tests/aes_ecb_kunit.c new file +227 -0

227 lines not on this page · raw message

lib/crypto/tests/sp800-38a-data.h new file +36 -0
new file mode 100644
index 000000000000..73517f1fef91
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Shared data from the AES test vectors in NIST SP 800-38A Appendix F
+ *
+ * Copyright 2026 Google LLC
+ */
+#ifndef LIB_CRYPTO_TESTS_SP800_38A_DATA_H
+#define LIB_CRYPTO_TESTS_SP800_38A_DATA_H
+
+#define SP800_38A_KEY128                   \
+	"\x2b\x7e\x15\x16\x28\xae\xd2\xa6" \
+	"\xab\xf7\x15\x88\x09\xcf\x4f\x3c"
+
+#define SP800_38A_KEY192                   \
+	"\x8e\x73\xb0\xf7\xda\x0e\x64\x52" \
+	"\xc8\x10\xf3\x2b\x80\x90\x79\xe5" \
+	"\x62\xf8\xea\xd2\x52\x2c\x6b\x7b"
+
+#define SP800_38A_KEY256                   \
+	"\x60\x3d\xeb\x10\x15\xca\x71\xbe" \
+	"\x2b\x73\xae\xf0\x85\x7d\x77\x81" \
+	"\x1f\x35\x2c\x07\x3b\x61\x08\xd7" \
+	"\x2d\x98\x10\xa3\x09\x14\xdf\xf4"
+
+/* The plaintext, which is 64 bytes long */
+#define SP800_38A_PTEXT                    \
+	"\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96" \
+	"\xe9\x3d\x7e\x11\x73\x93\x17\x2a" \
+	"\xae\x2d\x8a\x57\x1e\x03\xac\x9c" \
+	"\x9e\xb7\x6f\xac\x45\xaf\x8e\x51" \
+	"\x30\xc8\x1c\x46\xa3\x5c\xe4\x11" \
+	"\xe5\xfb\xc1\x19\x1a\x0a\x52\xef" \
+	"\xf6\x9f\x24\x45\xdf\x4f\x9b\x17" \
+	"\xad\x2b\x41\x7b\xe6\x6c\x37\x10"
+
+#endif /* LIB_CRYPTO_TESTS_SP800_38A_DATA_H */
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 3/7 lib/crypto: tests: Add KUnit test suite for AES-CBC

In reply to Eric Biggers

Add a KUnit test suite for the AES-CBC 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-CBC test vectors from NIST SP 800-38A.

---
 lib/crypto/.kunitconfig          |   1 +
 lib/crypto/tests/Kconfig         |   9 ++
 lib/crypto/tests/Makefile        |   1 +
 lib/crypto/tests/aes_cbc_kunit.c | 137 +++++++++++++++++++++++++++++++
 4 files changed, 148 insertions(+)
 create mode 100644 lib/crypto/tests/aes_cbc_kunit.c
lib/crypto/.kunitconfig +1 -0
index e323e955ceff..c3ac80ccb695 100644
@@ -2,6 +2,7 @@ CONFIG_KUNIT=y
 
 CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y
 
+CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST=y
lib/crypto/tests/Kconfig +9 -0
index 54af41f3c07d..7076ea606ce8 100644
@@ -1,5 +1,14 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
+config CRYPTO_LIB_AES_CBC_KUNIT_TEST
+	tristate "KUnit tests for AES-CBC" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_LIB_AES_CBC
+	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-CBC encryption mode.
+
 config CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST
 	tristate "KUnit tests for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_AES_CBC_MACS
lib/crypto/tests/Makefile +1 -0
index 1a5be11ce378..e7b7efee663a 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
+obj-$(CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST) += aes_cbc_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST) += aes_ecb_kunit.o
lib/crypto/tests/aes_cbc_kunit.c new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH 4/7 lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS

In reply to Eric Biggers

Add a KUnit test suite for the AES-CBC-CTS 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-CBC-CTS test vectors from RFC 3962.

- A test comparing AES-CBC-CTS against a reference implementation built on top of aes_cbc_encrypt().

---
 lib/crypto/tests/Kconfig             |   4 +-
 lib/crypto/tests/Makefile            |   2 +-
 lib/crypto/tests/aes_cbc_cts_kunit.c | 239 +++++++++++++++++++++++++++
 3 files changed, 242 insertions(+), 3 deletions(-)
 create mode 100644 lib/crypto/tests/aes_cbc_cts_kunit.c
lib/crypto/tests/Kconfig +2 -2
index 7076ea606ce8..b0b0ae84a02e 100644
@@ -1,13 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 config CRYPTO_LIB_AES_CBC_KUNIT_TEST
-	tristate "KUnit tests for AES-CBC" if !KUNIT_ALL_TESTS
+	tristate "KUnit tests for AES-CBC and AES-CBC-CTS" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_AES_CBC
 	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-CBC encryption mode.
+	  KUnit tests for the AES-CBC and AES-CBC-CTS encryption modes.
 
 config CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST
 	tristate "KUnit tests for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC" if !KUNIT_ALL_TESTS
lib/crypto/tests/Makefile +1 -1
index e7b7efee663a..fa22a4138a68 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
-obj-$(CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST) += aes_cbc_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST) += aes_cbc_kunit.o aes_cbc_cts_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_ECB_KUNIT_TEST) += aes_ecb_kunit.o
lib/crypto/tests/aes_cbc_cts_kunit.c new file +239 -0

239 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH 5/7 lib/crypto: tests: Add KUnit test suite for AES-CTR

In reply to Eric Biggers

Add a KUnit test suite for the AES-CTR 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-CTR test vectors from NIST SP 800-38A.

- A test for counter overflows across 8-bit, 32-bit, 64-bit, and 128-bit boundaries, comparing against a reference implementation built on top of aes_encrypt().

---
 lib/crypto/.kunitconfig          |   1 +
 lib/crypto/tests/Kconfig         |   9 ++
 lib/crypto/tests/Makefile        |   1 +
 lib/crypto/tests/aes_ctr_kunit.c | 196 +++++++++++++++++++++++++++++++
 4 files changed, 207 insertions(+)
 create mode 100644 lib/crypto/tests/aes_ctr_kunit.c
lib/crypto/.kunitconfig +1 -0
index c3ac80ccb695..465375a1a14c 100644
@@ -5,6 +5,7 @@ CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y
 CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST=y
 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_BLAKE2B_KUNIT_TEST=y
lib/crypto/tests/Kconfig +9 -0
index b0b0ae84a02e..0d13ce05f2de 100644
@@ -27,6 +27,15 @@ config CRYPTO_LIB_AES_CCM_KUNIT_TEST
 	help
 	  KUnit tests for the AES-CCM authenticated encryption algorithm.
 
+config CRYPTO_LIB_AES_CTR_KUNIT_TEST
+	tristate "KUnit tests for AES-CTR" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_LIB_AES_CTR
+	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-CTR stream cipher.
+
 config CRYPTO_LIB_AES_ECB_KUNIT_TEST
 	tristate "KUnit tests for AES-ECB" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_AES_ECB
lib/crypto/tests/Makefile +1 -0
index fa22a4138a68..1deb07c2558f 100644
@@ -3,6 +3,7 @@
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST) += aes_cbc_kunit.o aes_cbc_cts_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_AES_CTR_KUNIT_TEST) += aes_ctr_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_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
lib/crypto/tests/aes_ctr_kunit.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#6 lore raw

PATCH 6/7 lib/crypto: tests: Add KUnit test suite for AES-XCTR

In reply to Eric Biggers

Add a KUnit test suite for the AES-XCTR 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-XCTR test vectors from the HCTR2 reference implementation.

---
 lib/crypto/tests/Kconfig          |   4 +-
 lib/crypto/tests/Makefile         |   2 +-
 lib/crypto/tests/aes_xctr_kunit.c | 149 ++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+), 3 deletions(-)
 create mode 100644 lib/crypto/tests/aes_xctr_kunit.c
lib/crypto/tests/Kconfig +2 -2
index 0d13ce05f2de..4f7a07b0e3c0 100644
@@ -28,13 +28,13 @@ config CRYPTO_LIB_AES_CCM_KUNIT_TEST
 	  KUnit tests for the AES-CCM authenticated encryption algorithm.
 
 config CRYPTO_LIB_AES_CTR_KUNIT_TEST
-	tristate "KUnit tests for AES-CTR" if !KUNIT_ALL_TESTS
+	tristate "KUnit tests for AES-CTR and AES-XCTR" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_AES_CTR
 	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-CTR stream cipher.
+	  KUnit tests for the AES-CTR and AES-XCTR stream ciphers.
 
 config CRYPTO_LIB_AES_ECB_KUNIT_TEST
 	tristate "KUnit tests for AES-ECB" if !KUNIT_ALL_TESTS
lib/crypto/tests/Makefile +1 -1
index 1deb07c2558f..50e4a3f70ebf 100644
@@ -3,7 +3,7 @@
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_KUNIT_TEST) += aes_cbc_kunit.o aes_cbc_cts_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_AES_CCM_KUNIT_TEST) += aes_ccm_kunit.o
-obj-$(CONFIG_CRYPTO_LIB_AES_CTR_KUNIT_TEST) += aes_ctr_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_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
lib/crypto/tests/aes_xctr_kunit.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer
#7 lore raw

PATCH 7/7 lib/crypto: tests: Add KUnit test suite for AES-XTS

In reply to Eric Biggers

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).

---
 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
lib/crypto/.kunitconfig +1 -0
index 465375a1a14c..1be91698a654 100644
@@ -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
lib/crypto/tests/Kconfig +9 -0
index 4f7a07b0e3c0..98fcf053e937 100644
@@ -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
lib/crypto/tests/Makefile +1 -0
index 50e4a3f70ebf..048ceb1d595d 100644
@@ -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
lib/crypto/tests/aes_xts_kunit.c new file +365 -0

365 lines not on this page · raw message

Signature · 1 line
2.56.1
author maintainer Applied
#8 lore raw
In reply to Eric Biggers
Quoted 37 lines · Eric Biggers

This series applies to libcrypto-next. It can also be retrieved from:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git aes-kunit-v1

This series adds comprehensive KUnit test suites for the remaining AES modes that got new library APIs in v7.3: AES-ECB, AES-CBC, AES-CBC-CTS, AES-CTR, AES-XCTR, and AES-XTS.

Most of the test code is placed in a common header file, like it is in many of the existing tests.

Eric Biggers (7):
lib/crypto: tests: Add skcipher-test-template.h and
gen-skcipher-testvecs.py
lib/crypto: tests: Add KUnit test suite for AES-ECB lib/crypto: tests: Add KUnit test suite for AES-CBC lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS lib/crypto: tests: Add KUnit test suite for AES-CTR lib/crypto: tests: Add KUnit test suite for AES-XCTR lib/crypto: tests: Add KUnit test suite for AES-XTS

 lib/crypto/.kunitconfig                   |    4 +
 lib/crypto/tests/Kconfig                  |   36 +
 lib/crypto/tests/Makefile                 |    4 +
 lib/crypto/tests/aead-test-template.h     |   67 +-
 lib/crypto/tests/aes_cbc_cts_kunit.c      |  239 ++++
 lib/crypto/tests/aes_cbc_kunit.c          |  137 +++
 lib/crypto/tests/aes_ctr_kunit.c          |  196 ++++
 lib/crypto/tests/aes_ecb_kunit.c          |  227 ++++
 lib/crypto/tests/aes_xctr_kunit.c         |  149 +++
 lib/crypto/tests/aes_xts_kunit.c          |  365 ++++++
 lib/crypto/tests/skcipher-test-template.h | 1241 +++++++++++++++++++++
 lib/crypto/tests/sp800-38a-data.h         |   36 +
 lib/crypto/tests/test-utils.h             |   25 +
 scripts/crypto/gen-skcipher-testvecs.py   |  167 +++
 14 files changed, 2846 insertions(+), 47 deletions(-)
 create mode 100644 lib/crypto/tests/aes_cbc_cts_kunit.c
Quoted 8 lines
 create mode 100644 lib/crypto/tests/aes_cbc_kunit.c
 create mode 100644 lib/crypto/tests/aes_ctr_kunit.c
 create mode 100644 lib/crypto/tests/aes_ecb_kunit.c
 create mode 100644 lib/crypto/tests/aes_xctr_kunit.c
 create mode 100644 lib/crypto/tests/aes_xts_kunit.c
 create mode 100644 lib/crypto/tests/skcipher-test-template.h
 create mode 100644 lib/crypto/tests/sp800-38a-data.h
 create mode 100755 scripts/crypto/gen-skcipher-testvecs.py

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next

- Eric