All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/crypto: tests: Test some longer lengths in AEAD tests
@ 2026-12-06  6:58 Eric Biggers
  2026-12-06  7:08 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Biggers @ 2026-12-06  6:58 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers

Make test_aead_monte_carlo() test some en/decrypted and associated data
lengths up to 65536 bytes.  Previously, it just tested en/decrypted data
lengths up to 1024 bytes and associated data lengths up to 292 bytes.

This is needed to properly test arch-optimized implementations that
process data in a non-preemptible context which they yield occasionally,
such as the x86 AES-GCM code.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 lib/crypto/tests/aead-test-template.h | 21 +++++++++++++++------
 lib/crypto/tests/aes_ccm_kunit.c      |  6 +++---
 lib/crypto/tests/aes_gcm_kunit.c      |  6 +++---
 scripts/crypto/gen-aead-testvecs.py   | 20 ++++++++++++++++++--
 4 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/lib/crypto/tests/aead-test-template.h b/lib/crypto/tests/aead-test-template.h
index 7689567c86f9b..a841a51bc3426 100644
--- a/lib/crypto/tests/aead-test-template.h
+++ b/lib/crypto/tests/aead-test-template.h
@@ -738,13 +738,21 @@ static void test_aead_inplace(struct kunit *test)
 
 /*
  * Monte-Carlo test for AEAD algorithms.  This deterministically generates
- * random AEAD inputs, encrypts them, verifies decryption, and computes and
- * verifies the checksum of all computed (ciphertext, tag) pairs.
+ * random AEAD inputs for every en/decrypted data length up to 1024 bytes and
+ * every associated data length up to 292 bytes as well as some longer lengths
+ * up to 64 KiB for both, encrypts them, verifies decryption, and computes and
+ * verifies the checksum of all computed (ciphertext, tag) pairs.  The expected
+ * checksum was computed using an independent implementation of the algorithm
+ * (see scripts/crypto/gen-aead-testvecs.py).
  */
 static void test_aead_monte_carlo(struct kunit *test)
 {
-	const size_t max_data_len = 1024;
-	const size_t max_ad_len = 293;
+	static const size_t extra_lens[] = {
+		4096,  4111,  8192,  16384, 16400, 32752, 32767,
+		32768, 32769, 32784, 49152, 65520, 65535, 65536,
+	};
+	const size_t max_data_len = 65536;
+	const size_t max_ad_len = 65536;
 	u8 raw_key[AEAD_MAX_KEY_LEN];
 	u8 nonce[AEAD_MAX_NONCE_LEN];
 	u8 tag[AEAD_MAX_TAG_LEN];
@@ -759,8 +767,9 @@ static void test_aead_monte_carlo(struct kunit *test)
 
 	blake2s_init(&checksum_ctx, BLAKE2S_HASH_SIZE);
 
-	for (size_t data_len = 0; data_len <= max_data_len; data_len++) {
-		size_t ad_len = data_len % max_ad_len;
+	for (size_t i = 0; i < 1025 + ARRAY_SIZE(extra_lens); i++) {
+		size_t data_len = i <= 1024 ? i : extra_lens[i - 1025];
+		size_t ad_len = data_len <= 1024 ? data_len % 293 : data_len;
 		size_t key_len =
 			AEAD_VALID_KEY_LENS[data_len %
 					    ARRAY_SIZE(AEAD_VALID_KEY_LENS)];
diff --git a/lib/crypto/tests/aes_ccm_kunit.c b/lib/crypto/tests/aes_ccm_kunit.c
index e8f2b34460173..497c95d051d31 100644
--- a/lib/crypto/tests/aes_ccm_kunit.c
+++ b/lib/crypto/tests/aes_ccm_kunit.c
@@ -251,9 +251,9 @@ static const size_t aes_ccm_valid_tag_lens[] = { 4, 6, 8, 10, 12, 14, 16 };
 
 /* This value was generated by gen-aead-testvecs.py. */
 static const u8 aes_ccm_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = {
-	0x70, 0x1c, 0xde, 0xa4, 0xe2, 0x03, 0x50, 0xb2, 0xf5, 0x9e, 0x61,
-	0x66, 0xe4, 0xe5, 0x13, 0x1a, 0x00, 0x95, 0x34, 0x03, 0xb7, 0x61,
-	0x2c, 0xdb, 0xc3, 0x15, 0x36, 0x84, 0x93, 0x7f, 0xb4, 0x5b,
+	0x71, 0xed, 0x72, 0xa2, 0x5a, 0xdf, 0xd3, 0xe6, 0x79, 0x17, 0x82,
+	0xf7, 0x83, 0x97, 0x22, 0x4a, 0xe2, 0x11, 0xf9, 0xff, 0x9c, 0x0a,
+	0x56, 0xc2, 0x8e, 0x12, 0xfb, 0x41, 0xbc, 0xff, 0xe1, 0x53,
 };
 #define AEAD_MONTE_CARLO_CHECKSUM aes_ccm_monte_carlo_checksum
 
diff --git a/lib/crypto/tests/aes_gcm_kunit.c b/lib/crypto/tests/aes_gcm_kunit.c
index 6959d86b316a7..6e73327833d9a 100644
--- a/lib/crypto/tests/aes_gcm_kunit.c
+++ b/lib/crypto/tests/aes_gcm_kunit.c
@@ -448,9 +448,9 @@ static const size_t aes_gcm_valid_tag_lens[] = { 4, 8, 12, 13, 14, 15, 16 };
 
 /* This value was generated by gen-aead-testvecs.py. */
 static const u8 aes_gcm_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = {
-	0x6d, 0xd0, 0x6e, 0x6b, 0xde, 0x3e, 0x92, 0x9f, 0xae, 0x1f, 0xf1,
-	0x84, 0x99, 0x5a, 0x9e, 0x7b, 0xfe, 0x20, 0x9e, 0x22, 0x7c, 0x5f,
-	0x15, 0xb3, 0x59, 0x89, 0xd0, 0xb7, 0x74, 0x5d, 0xd2, 0xa5,
+	0x03, 0xd7, 0xff, 0xd2, 0x7d, 0x77, 0x15, 0xdc, 0x4a, 0x72, 0x85,
+	0xbf, 0x03, 0x42, 0xa9, 0x06, 0x63, 0x98, 0x76, 0x52, 0xf1, 0x03,
+	0x92, 0x8f, 0x09, 0xf5, 0xd4, 0x01, 0xdd, 0x72, 0x66, 0x73,
 };
 #define AEAD_MONTE_CARLO_CHECKSUM aes_gcm_monte_carlo_checksum
 
diff --git a/scripts/crypto/gen-aead-testvecs.py b/scripts/crypto/gen-aead-testvecs.py
index d77d646f75b54..73728fb21feaa 100755
--- a/scripts/crypto/gen-aead-testvecs.py
+++ b/scripts/crypto/gen-aead-testvecs.py
@@ -28,8 +28,24 @@ def rand_bytes(length):
 # pairs concatenated to each other; and print the checksum as a C struct.
 def gen_monte_carlo_checksum(alg):
     blake2s = hashlib.blake2s()
-    for data_len in range(1025):
-        ad_len = data_len % 293
+    extra_lens = [
+        4096,
+        4111,
+        8192,
+        16384,
+        16400,
+        32752,
+        32767,
+        32768,
+        32769,
+        32784,
+        49152,
+        65520,
+        65535,
+        65536,
+    ]
+    for data_len in list(range(1025)) + extra_lens:
+        ad_len = data_len % 293 if data_len <= 1024 else data_len
         pt = rand_bytes(data_len)
         ad = rand_bytes(ad_len)
         if alg == "aes-ccm":

base-commit: 84a878a7122a3d341e8ec2675cf9ad2d65217c6e
-- 
2.98.0.rc0.2.g460126620f


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] lib/crypto: tests: Test some longer lengths in AEAD tests
  2026-12-06  6:58 [PATCH] lib/crypto: tests: Test some longer lengths in AEAD tests Eric Biggers
@ 2026-12-06  7:08 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06  7:08 UTC (permalink / raw)
  To: Eric Biggers, linux-crypto
  Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] lib/crypto: order-4 kmalloc() in test_aead_monte_carlo() exceeds
  costly order
--

commit 55dddd51268c30bf76d6b3b8102ea572f5972e8b
Author: Eric Biggers <ebiggers@kernel.org>

lib/crypto: tests: Test some longer lengths in AEAD tests

This commit extends test_aead_monte_carlo() and gen-aead-testvecs.py to test
additional data and associated data lengths up to 65536 bytes, and updates
the expected AES-CCM and AES-GCM Monte-Carlo checksums.

> diff --git a/lib/crypto/tests/aead-test-template.h b/lib/crypto/tests/aead-test-template.h
> index 7689567c86f9b..a841a51bc3426 100644
> --- a/lib/crypto/tests/aead-test-template.h
> +++ b/lib/crypto/tests/aead-test-template.h
> @@ -738,13 +738,21 @@ static void test_aead_inplace(struct kunit *test)

[ ... ]

>  static void test_aead_monte_carlo(struct kunit *test)
>  {
> -	const size_t max_data_len = 1024;
> -	const size_t max_ad_len = 293;
> +	static const size_t extra_lens[] = {
> +		4096,  4111,  8192,  16384, 16400, 32752, 32767,
> +		32768, 32769, 32784, 49152, 65520, 65535, 65536,
> +	};
> +	const size_t max_data_len = 65536;
> +	const size_t max_ad_len = 65536;

[Severity: Low]
Can raising max_data_len and max_ad_len to 65536 cause spurious failures in
test_aead_monte_carlo() under memory fragmentation?

pt, ct, decrypted, and ad are allocated via alloc_buf(), which calls
kunit_kmalloc(..., GFP_KERNEL) and asserts non-NULL.  On 4 KiB page kernels,
each 65536-byte allocation is an order-4 physically contiguous request above
PAGE_ALLOC_COSTLY_ORDER (3).

Because plain GFP_KERNEL omits __GFP_RETRY_MAYFAIL, loading the AES-CCM or
AES-GCM KUnit modules on a fragmented system can fail any of these four
allocations.  Using alloc_guarded_buf() or a kvmalloc()-backed helper would
avoid order-4 allocations.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206065847.452749-1-ebiggers@kernel.org?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06  7:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  6:58 [PATCH] lib/crypto: tests: Test some longer lengths in AEAD tests Eric Biggers
2026-12-06  7:08 ` sashiko-bot

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.