Linux cryptographic layer development
 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 6/7] lib/crypto: tests: Add KUnit test suite for AES-XCTR
Date: Sun, 15 Nov 2026 22:28:48 -0800	[thread overview]
Message-ID: <20261116062849.1071038-7-ebiggers@kernel.org> (raw)
In-Reply-To: <20261116062849.1071038-1-ebiggers@kernel.org>

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.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 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

diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig
index 0d13ce05f2de..4f7a07b0e3c0 100644
--- a/lib/crypto/tests/Kconfig
+++ b/lib/crypto/tests/Kconfig
@@ -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
diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile
index 1deb07c2558f..50e4a3f70ebf 100644
--- a/lib/crypto/tests/Makefile
+++ b/lib/crypto/tests/Makefile
@@ -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
diff --git a/lib/crypto/tests/aes_xctr_kunit.c b/lib/crypto/tests/aes_xctr_kunit.c
new file mode 100644
index 000000000000..86fe4356cb66
--- /dev/null
+++ b/lib/crypto/tests/aes_xctr_kunit.c
@@ -0,0 +1,149 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * KUnit test suite for AES-XCTR
+ *
+ * Copyright 2026 Google LLC
+ */
+#include <crypto/aes-ctr.h>
+#include <crypto/blake2s.h>
+#include <linux/unaligned.h>
+#include "test-utils.h"
+
+/*
+ * For the purpose of the tests, the IV state for AES-XCTR is the 64-bit little
+ * endian block counter followed by the 16-byte IV.  The IV is placed last so
+ * that when the IV state is in a guarded buffer, over-reads of the IV in
+ * aes_xctr() are detected.
+ */
+#define AES_XCTR_STATE_SIZE (sizeof(__le64) + AES_BLOCK_SIZE)
+
+static void aes_xctr_test(u8 *dst, const u8 *src, size_t len, u8 *state,
+			  aes_encrypt_arg key, bool cont)
+{
+	u64 ctr = get_unaligned_le64(state);
+
+	aes_xctr(dst, src, len, &ctr, &state[sizeof(__le64)], key);
+	put_unaligned_le64(ctr, state);
+}
+
+static const size_t aes_xctr_valid_key_lens[] = { AES_KEYSIZE_128,
+						  AES_KEYSIZE_192,
+						  AES_KEYSIZE_256 };
+#define SKCIPHER_VALID_KEY_LENS aes_xctr_valid_key_lens
+#define SKCIPHER_MAX_KEY_LEN AES_MAX_KEY_SIZE
+#define SKCIPHER_IV_LEN AES_XCTR_STATE_SIZE
+#define SKCIPHER_MIN_DATA_LEN 0
+#define SKCIPHER_DATA_LEN_ALIGN 1
+
+#define SKCIPHER_KEY aes_key
+#define SKCIPHER_PREPAREKEY aes_preparekey
+#define SKCIPHER_ENCRYPT aes_xctr_test
+#define SKCIPHER_DECRYPT aes_xctr_test
+
+#define SKCIPHER_ENCKEY aes_enckey
+#define SKCIPHER_PREPAREENCKEY aes_prepareenckey
+#define SKCIPHER_INCREMENTAL
+#define SKCIPHER_NONFINAL_DATA_LEN_ALIGN AES_BLOCK_SIZE
+#define SKCIPHER_NEXT_IV_ALWAYS_DEFINED
+
+/* This value was generated by gen-skcipher-testvecs.py. */
+static const u8 aes_xctr_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = {
+	0x40, 0xc0, 0x13, 0x02, 0xc6, 0x12, 0x5c, 0x86, 0xce, 0x85, 0x9d,
+	0xcb, 0x8f, 0x9b, 0xda, 0x17, 0x6f, 0x15, 0x73, 0x71, 0x0f, 0x6c,
+	0x68, 0x41, 0xb5, 0x7d, 0x29, 0xe9, 0xb1, 0xb5, 0xa4, 0xb5,
+};
+#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_xctr_monte_carlo_checksum
+
+#include "skcipher-test-template.h"
+
+/* Initial XCTR block counter of 1, in the format of the IV state */
+#define AES_XCTR_CTR1 "\x01\x00\x00\x00\x00\x00\x00\x00"
+
+/* XCTR block counter after 2 blocks (initial counter 1 + 2 = 3) */
+#define AES_XCTR_CTR3 "\x03\x00\x00\x00\x00\x00\x00\x00"
+
+/*
+ * AES-XCTR test vectors generated by the HCTR2 reference implementation
+ * (https://github.com/google/hctr2).
+ */
+static const struct skcipher_testvec aes_xctr_testvecs[] = {
+	{
+		.name = "HCTR2 reference AES-128-XCTR, 31 bytes",
+		.key = "\x44\x03\xbf\x4c\x30\xf0\xa7\xd6"
+		       "\xbd\x54\xbb\x66\x8e\xa6\x0e\x8a",
+		.key_len = 16,
+		.iv = AES_XCTR_CTR1 "\xe6\xf7\x26\xdf\x8c\x3c\xaa\x88"
+				    "\xce\xc1\xbd\x43\x3b\x09\x62\xad",
+		.ptext = "\x3c\xe3\x46\xb9\x8f\x9d\x3f\x8d"
+			 "\xef\xf2\x53\xab\x24\xe2\x29\x08"
+			 "\xf8\x7e\x1d\xa6\x6d\x86\x7d\x60"
+			 "\x97\x63\x93\x29\x71\x94\xb4",
+		.ctext = "\xd4\xa3\xc6\xb8\xc1\x6f\x70\x1a"
+			 "\x52\x0c\xed\x4c\xaf\x51\x56\x23"
+			 "\x48\x45\x07\x10\x34\xc5\xba\x71"
+			 "\xe5\xf8\x1e\xd8\xcb\xa6\xe7",
+		.data_len = 31,
+		.next_iv = AES_XCTR_CTR3 "\xe6\xf7\x26\xdf\x8c\x3c\xaa\x88"
+					 "\xce\xc1\xbd\x43\x3b\x09\x62\xad",
+	},
+	{
+		.name = "HCTR2 reference AES-192-XCTR, 17 bytes",
+		.key = "\xc8\xa0\x27\x67\x04\x3f\xed\xa5"
+		       "\xb4\x0c\x51\x91\x2d\x27\x77\x33"
+		       "\xa5\xfc\x2a\x9f\x78\xd8\x1c\x68",
+		.key_len = 24,
+		.iv = AES_XCTR_CTR1 "\x83\x99\x1a\xe2\x84\xca\xa9\x16"
+				    "\x8d\xc4\x2d\x1b\x67\xc8\x86\x21",
+		.ptext = "\xd6\x22\x85\xb8\x5d\x7e\x26\x2e"
+			 "\xbe\x04\x9d\x0c\x03\x91\x45\x4a"
+			 "\x36",
+		.ctext = "\x0f\x44\xa9\x62\x72\xec\x12\x26"
+			 "\x3a\xc6\x83\x26\x62\x5e\xb7\x13"
+			 "\x05",
+		.data_len = 17,
+		.next_iv = AES_XCTR_CTR3 "\x83\x99\x1a\xe2\x84\xca\xa9\x16"
+					 "\x8d\xc4\x2d\x1b\x67\xc8\x86\x21",
+	},
+	{
+		.name = "HCTR2 reference AES-256-XCTR, 31 bytes",
+		.key = "\x77\x5c\xc0\x73\x9a\x64\x97\x91"
+		       "\x2f\xee\xe0\x20\xc2\x04\x59\x2e"
+		       "\x97\xd2\xa7\x70\xb3\xb0\x21\x6b"
+		       "\x8f\xbf\xb8\x51\xa8\xea\x0f\x62",
+		.key_len = 32,
+		.iv = AES_XCTR_CTR1 "\x31\x8e\x1f\xcd\xfd\x23\xeb\x7f"
+				    "\x8a\x1f\x1b\x23\x53\x27\x44\xe5",
+		.ptext = "\xcd\xff\x8c\x9b\x94\x5a\x51\x3f"
+			 "\x40\x93\x56\x93\x66\x39\x63\x1f"
+			 "\xbf\xe6\xa4\xfa\xbe\x79\x93\x03"
+			 "\xf5\x66\x74\x16\xfc\xe4\xce",
+		.ctext = "\x8b\xd3\xc3\xce\x66\xf8\x66\x4c"
+			 "\xad\xd6\xf5\x0f\xd8\x99\x5a\x75"
+			 "\xa1\x3c\xab\x0b\x21\x36\x57\x72"
+			 "\x88\x29\xe9\xea\x4a\x8d\xe9",
+		.data_len = 31,
+		.next_iv = AES_XCTR_CTR3 "\x31\x8e\x1f\xcd\xfd\x23\xeb\x7f"
+					 "\x8a\x1f\x1b\x23\x53\x27\x44\xe5",
+	},
+};
+
+static void test_aes_xctr_test_vectors(struct kunit *test)
+{
+	skcipher_run_testvecs(test, aes_xctr_testvecs,
+			      ARRAY_SIZE(aes_xctr_testvecs));
+}
+
+static struct kunit_case aes_xctr_test_cases[] = {
+	KUNIT_CASE(test_aes_xctr_test_vectors),
+	SKCIPHER_KUNIT_CASES,
+	{},
+};
+
+static struct kunit_suite aes_xctr_test_suite = {
+	.name = "aes_xctr",
+	.test_cases = aes_xctr_test_cases,
+};
+kunit_test_suite(aes_xctr_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for AES-XCTR");
+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 ` Eric Biggers [this message]
2026-11-16  6:28 ` [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS Eric Biggers

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-7-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox