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 4/7] lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS
Date: Sun, 15 Nov 2026 22:28:46 -0800	[thread overview]
Message-ID: <20261116062849.1071038-5-ebiggers@kernel.org> (raw)
In-Reply-To: <20261116062849.1071038-1-ebiggers@kernel.org>

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

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

diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig
index 7076ea606ce8..b0b0ae84a02e 100644
--- a/lib/crypto/tests/Kconfig
+++ b/lib/crypto/tests/Kconfig
@@ -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
diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile
index e7b7efee663a..fa22a4138a68 100644
--- a/lib/crypto/tests/Makefile
+++ b/lib/crypto/tests/Makefile
@@ -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
diff --git a/lib/crypto/tests/aes_cbc_cts_kunit.c b/lib/crypto/tests/aes_cbc_cts_kunit.c
new file mode 100644
index 000000000000..2890515ded0f
--- /dev/null
+++ b/lib/crypto/tests/aes_cbc_cts_kunit.c
@@ -0,0 +1,239 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * KUnit test suite for AES-CBC-CTS
+ *
+ * Copyright 2026 Google LLC
+ */
+#include <crypto/aes-cbc.h>
+#include <crypto/blake2s.h>
+#include "test-utils.h"
+
+static void aes_cbc_cts_encrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv,
+				     aes_encrypt_arg key, bool cont)
+{
+	aes_cbc_cts_encrypt(dst, src, len, iv, key);
+}
+
+static void aes_cbc_cts_decrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv,
+				     const struct aes_key *key, bool cont)
+{
+	aes_cbc_cts_decrypt(dst, src, len, iv, key);
+}
+
+static const size_t aes_cbc_cts_valid_key_lens[] = { AES_KEYSIZE_128,
+						     AES_KEYSIZE_192,
+						     AES_KEYSIZE_256 };
+#define SKCIPHER_VALID_KEY_LENS aes_cbc_cts_valid_key_lens
+#define SKCIPHER_MAX_KEY_LEN AES_MAX_KEY_SIZE
+#define SKCIPHER_IV_LEN AES_BLOCK_SIZE
+#define SKCIPHER_MIN_DATA_LEN AES_BLOCK_SIZE
+#define SKCIPHER_DATA_LEN_ALIGN 1
+
+#define SKCIPHER_KEY aes_key
+#define SKCIPHER_PREPAREKEY aes_preparekey
+#define SKCIPHER_ENCRYPT aes_cbc_cts_encrypt_test
+#define SKCIPHER_DECRYPT aes_cbc_cts_decrypt_test
+
+#define SKCIPHER_ENCKEY aes_enckey
+#define SKCIPHER_PREPAREENCKEY aes_prepareenckey
+
+/* This value was generated by gen-skcipher-testvecs.py. */
+static const u8 aes_cbc_cts_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = {
+	0x9a, 0xc5, 0x99, 0xaf, 0x25, 0x2d, 0x56, 0x52, 0xd6, 0x50, 0x14,
+	0x33, 0x61, 0x0b, 0xb6, 0x40, 0x5e, 0xc5, 0x9c, 0x8e, 0xfd, 0x6c,
+	0xae, 0xa1, 0x1d, 0x47, 0x43, 0xd4, 0x19, 0x01, 0xc9, 0x6b,
+};
+#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_cbc_cts_monte_carlo_checksum
+
+#include "skcipher-test-template.h"
+
+#define RFC3962_KEY "chicken teriyaki"
+#define RFC3962_IV "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
+#define RFC3962_PTEXT \
+	"I would like the General Gau's Chicken, please, and wonton soup."
+
+/*
+ * AES-CBC-CTS test vectors from RFC 3962 Appendix B.  The ciphertext stealing
+ * variant used in RFC 3962 is the same as CS3, i.e. the variant implemented by
+ * aes_cbc_cts_encrypt() and aes_cbc_cts_decrypt().
+ *
+ * RFC 3962 doesn't include a 16-byte test vector.  The one below was derived
+ * from the 32-byte one: when the length is exactly one block, CS3 is the same
+ * as plain CBC, so the ciphertext is just the first CBC ciphertext block.
+ */
+static const struct skcipher_testvec aes_cbc_cts_testvecs[] = {
+	{
+		.name = "Derived from RFC 3962 Appendix B, 16 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
+		.data_len = 16,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 17 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4"
+			 "\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
+			 "\x97",
+		.data_len = 17,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 31 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1"
+			 "\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
+			 "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5",
+		.data_len = 31,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 32 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\x39\x31\x25\x23\xa7\x86\x62\xd5"
+			 "\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
+			 "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
+		.data_len = 32,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 47 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
+			 "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c"
+			 "\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
+			 "\x39\x31\x25\x23\xa7\x86\x62\xd5"
+			 "\xbe\x7f\xcb\xcc\x98\xeb\xf5",
+		.data_len = 47,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 48 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
+			 "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0"
+			 "\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
+			 "\x39\x31\x25\x23\xa7\x86\x62\xd5"
+			 "\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
+		.data_len = 48,
+	},
+	{
+		.name = "RFC 3962 Appendix B, 64 bytes",
+		.key = RFC3962_KEY,
+		.key_len = 16,
+		.iv = RFC3962_IV,
+		.ptext = RFC3962_PTEXT,
+		.ctext = "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
+			 "\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
+			 "\x39\x31\x25\x23\xa7\x86\x62\xd5"
+			 "\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
+			 "\x48\x07\xef\xe8\x36\xee\x89\xa5"
+			 "\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
+			 "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0"
+			 "\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
+		.data_len = 64,
+	},
+};
+
+static void test_aes_cbc_cts_test_vectors(struct kunit *test)
+{
+	skcipher_run_testvecs(test, aes_cbc_cts_testvecs,
+			      ARRAY_SIZE(aes_cbc_cts_testvecs));
+}
+
+/*
+ * Test AES-CBC-CTS against a reference implementation built on top of AES-CBC,
+ * using random lengths up to 4096 bytes.  In the CS3 variant of CBC-CTS, the
+ * ciphertext is the AES-CBC encryption of the zero-padded plaintext, but with
+ * the last two blocks swapped and the (new) last block truncated to the length
+ * of the final plaintext block, which may be partial.  When the length is
+ * exactly one block, the ciphertext is the same as AES-CBC.
+ */
+static void test_aes_cbc_cts_vs_cbc(struct kunit *test)
+{
+	const size_t max_data_len = 4096;
+	const struct skcipher_test_keys *keys = skcipher_alloc_test_keys(test);
+	const u8 *pt = alloc_rand_buf(test, max_data_len);
+	u8 *padded_pt = alloc_buf(test, round_up(max_data_len, AES_BLOCK_SIZE));
+	u8 *ct = alloc_buf(test, max_data_len);
+	u8 *ref_ct = alloc_buf(test, round_up(max_data_len, AES_BLOCK_SIZE));
+	u8 *decrypted = alloc_buf(test, max_data_len);
+	u8 orig_iv[AES_BLOCK_SIZE];
+	u8 iv[AES_BLOCK_SIZE];
+
+	for (size_t i = 0; i < 200; i++) {
+		const struct aes_key *key = skcipher_pick_key(keys, i);
+		const size_t data_len =
+			i < keys->num_keys ?
+				AES_BLOCK_SIZE :
+				skcipher_rand_data_len(max_data_len);
+		const size_t padded_len = round_up(data_len, AES_BLOCK_SIZE);
+		const size_t pn_offset = padded_len - AES_BLOCK_SIZE;
+		const size_t pn_len = data_len - pn_offset;
+
+		rand_bytes(orig_iv, sizeof(orig_iv));
+
+		/* Compute the expected ciphertext using AES-CBC. */
+		memcpy(padded_pt, pt, data_len);
+		memset(&padded_pt[data_len], 0, padded_len - data_len);
+		memcpy(iv, orig_iv, sizeof(iv));
+		aes_cbc_encrypt(ref_ct, padded_pt, padded_len, iv, key);
+		if (padded_len > AES_BLOCK_SIZE) {
+			u8 tmp[AES_BLOCK_SIZE];
+
+			memcpy(tmp, &ref_ct[pn_offset - AES_BLOCK_SIZE],
+			       AES_BLOCK_SIZE);
+			memcpy(&ref_ct[pn_offset - AES_BLOCK_SIZE],
+			       &ref_ct[pn_offset], AES_BLOCK_SIZE);
+			memcpy(&ref_ct[pn_offset], tmp, pn_len);
+		}
+
+		memcpy(iv, orig_iv, sizeof(iv));
+		aes_cbc_cts_encrypt(ct, pt, data_len, iv, key);
+		KUNIT_ASSERT_MEMEQ_MSG(test, ref_ct, ct, data_len,
+				       "Wrong ciphertext; data_len=%zu",
+				       data_len);
+
+		memset(decrypted, 0, data_len);
+		memcpy(iv, orig_iv, sizeof(iv));
+		aes_cbc_cts_decrypt(decrypted, ct, data_len, iv, key);
+		KUNIT_ASSERT_MEMEQ_MSG(
+			test, pt, decrypted, data_len,
+			"Decryption didn't invert encryption; data_len=%zu",
+			data_len);
+	}
+}
+
+static struct kunit_case aes_cbc_cts_test_cases[] = {
+	KUNIT_CASE(test_aes_cbc_cts_test_vectors),
+	KUNIT_CASE(test_aes_cbc_cts_vs_cbc),
+	SKCIPHER_KUNIT_CASES,
+	{},
+};
+
+static struct kunit_suite aes_cbc_cts_test_suite = {
+	.name = "aes_cbc_cts",
+	.test_cases = aes_cbc_cts_test_cases,
+};
+kunit_test_suite(aes_cbc_cts_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests and benchmark for AES-CBC-CTS");
+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 ` Eric Biggers [this message]
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 ` [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-5-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.