* [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes
@ 2026-11-16 6:28 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
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw)
To: linux-crypto
Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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
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
--
2.56.1
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 1/7] lib/crypto: tests: Add skcipher-test-template.h and gen-skcipher-testvecs.py 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers @ 2026-11-16 6:28 ` Eric Biggers 2026-11-16 6:28 ` [PATCH 2/7] lib/crypto: tests: Add KUnit test suite for AES-ECB Eric Biggers ` (6 subsequent siblings) 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers 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. Signed-off-by: Eric Biggers <ebiggers@kernel.org> --- 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 diff --git a/lib/crypto/tests/aead-test-template.h b/lib/crypto/tests/aead-test-template.h index 894a927c6f39..7689567c86f9 100644 --- a/lib/crypto/tests/aead-test-template.h +++ b/lib/crypto/tests/aead-test-template.h @@ -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); diff --git a/lib/crypto/tests/skcipher-test-template.h b/lib/crypto/tests/skcipher-test-template.h new file mode 100644 index 000000000000..e68be28b003b --- /dev/null +++ b/lib/crypto/tests/skcipher-test-template.h @@ -0,0 +1,1241 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Shared KUnit test cases for length-preserving symmetric ciphers, including a + * benchmark + * + * Copyright 2026 Google LLC + */ + +/* + * This file implements KUnit test cases shared by the different KUnit test + * suites for length-preserving symmetric ciphers. + * + * Test suites including this file must #define the following: + * + * Data structs: + * + * - SKCIPHER_KEY: name of key struct + * + * Constants: + * + * - SKCIPHER_VALID_KEY_LENS: array of all valid key lengths in bytes + * + * - SKCIPHER_MAX_KEY_LEN: max key length in bytes (assumed to fit on stack) + * + * - SKCIPHER_IV_LEN: length of the initialization vector. If + * SKCIPHER_INCREMENTAL is defined, the initialization vector buffer is also + * assumed to contain the carried-forward state. + * + * - SKCIPHER_MIN_DATA_LEN: minimum valid data length in bytes. Must be a + * (possibly zero) multiple of SKCIPHER_DATA_LEN_ALIGN, and a (possibly zero) + * multiple of SKCIPHER_NONFINAL_DATA_LEN_ALIGN if that is defined. + * + * - SKCIPHER_DATA_LEN_ALIGN: valid data lengths are multiples of this. + * Must be a power of 2. + * + * - SKCIPHER_MONTE_CARLO_CHECKSUM: checksum of a deterministically generated + * series of ciphertexts (see test_skcipher_monte_carlo()) + * + * Functions: + * + * - SKCIPHER_PREPAREKEY(key, raw_key, key_len): key preparation, returning 0 or + * -EINVAL. + * + * - SKCIPHER_ENCRYPT(dst, src, len, iv, key, cont) and + * SKCIPHER_DECRYPT(dst, src, len, iv, key, cont): en/decryption functions. + * 'iv' is the IV state, which is updated in place. 'cont' is false on the + * first call for a message and true on any subsequent calls. + * + * Optionally, test suites may #define the following: + * + * - SKCIPHER_ENCKEY: encryption-only key struct. If defined, + * SKCIPHER_ENCRYPT must accept either SKCIPHER_KEY or SKCIPHER_ENCKEY. + * + * - SKCIPHER_PREPAREENCKEY(key, raw_key, key_len): + * function to prepare a struct SKCIPHER_ENCKEY. + * + * - SKCIPHER_IS_KEY_LEN_ALLOWED(key_len): returns false if a key length in + * SKCIPHER_VALID_KEY_LENS is currently disallowed (e.g. due to FIPS mode). + * + * - SKCIPHER_INCREMENTAL: if defined, en/decryption may be split across + * multiple calls. The length of each non-final call must be a multiple of + * SKCIPHER_NONFINAL_DATA_LEN_ALIGN and at least SKCIPHER_MIN_DATA_LEN, and + * the updated IV state must be passed to the next call. In this case, if the + * total length is a multiple of SKCIPHER_NONFINAL_DATA_LEN_ALIGN (or if + * SKCIPHER_NEXT_IV_ALWAYS_DEFINED is also defined), the final IV state is + * expected to be the same regardless of how the message was split. + * + * - SKCIPHER_NONFINAL_DATA_LEN_ALIGN: non-final calls require a data length + * that is a multiple of this. Must be a power of 2 and a multiple of + * SKCIPHER_DATA_LEN_ALIGN. + * + * - SKCIPHER_NEXT_IV_ALWAYS_DEFINED: if defined alongside SKCIPHER_INCREMENTAL, + * the final IV state is well-defined for all valid data lengths, not just + * multiples of SKCIPHER_NONFINAL_DATA_LEN_ALIGN. + * + * After including this file, include SKCIPHER_KUNIT_CASES in the kunit_case + * array. This file also provides struct skcipher_testvec and + * skcipher_run_testvecs() for running known-answer test vectors. + */ + +#include <crypto/blake2s.h> +#include <kunit/run-in-irq-context.h> +#include <kunit/test.h> +#include <linux/preempt.h> +#include <linux/timekeeping.h> +#include "test-utils.h" + +#ifndef SKCIPHER_IS_KEY_LEN_ALLOWED +#define SKCIPHER_IS_KEY_LEN_ALLOWED(key_len) ((void)(key_len), true) +#endif + +#define SKCIPHER_IS_POWER_OF_2(x) ((x) != 0 && ((x) & ((x) - 1)) == 0) + +static_assert(SKCIPHER_IS_POWER_OF_2(SKCIPHER_DATA_LEN_ALIGN)); +static_assert(SKCIPHER_MIN_DATA_LEN % SKCIPHER_DATA_LEN_ALIGN == 0); +#ifdef SKCIPHER_INCREMENTAL +static_assert(SKCIPHER_IS_POWER_OF_2(SKCIPHER_NONFINAL_DATA_LEN_ALIGN)); +static_assert(SKCIPHER_NONFINAL_DATA_LEN_ALIGN % SKCIPHER_DATA_LEN_ALIGN == 0); +static_assert(SKCIPHER_MIN_DATA_LEN % SKCIPHER_NONFINAL_DATA_LEN_ALIGN == 0); +#endif + +/* Size to use for IV buffers. Avoids zero-length arrays. */ +#define SKCIPHER_IV_BUF_SIZE (SKCIPHER_IV_LEN > 0 ? SKCIPHER_IV_LEN : 1) + +struct skcipher_testvec { + const char *name; + const char *key; + size_t key_len; + const char *iv; /* Unused if SKCIPHER_IV_LEN == 0 */ + const char *ptext; + const char *ctext; + size_t data_len; + /* Optional: the expected IV state after en/decrypting the message */ + const char *next_iv; +}; + +static void skcipher_crypt(u8 *dst, const u8 *src, size_t len, u8 *iv, + const struct SKCIPHER_KEY *key, bool cont, bool enc) +{ + if (enc) + SKCIPHER_ENCRYPT(dst, src, len, iv, key, cont); + else + SKCIPHER_DECRYPT(dst, src, len, iv, key, cont); +} + +static void skcipher_copy_iv(u8 *dst, const u8 *src) +{ + if (SKCIPHER_IV_LEN) + memcpy(dst, src, SKCIPHER_IV_LEN); +} + +/* + * Return true if the IV state is defined after en/decrypting a message of + * length @data_len. + */ +static bool skcipher_is_next_iv_defined(size_t data_len) +{ +#if defined(SKCIPHER_INCREMENTAL) && defined(SKCIPHER_NEXT_IV_ALWAYS_DEFINED) + return SKCIPHER_IV_LEN != 0; +#elif defined(SKCIPHER_INCREMENTAL) + return SKCIPHER_IV_LEN != 0 && + data_len % SKCIPHER_NONFINAL_DATA_LEN_ALIGN == 0; +#else + return false; +#endif +} + +/* Return true if data_len is a valid data length. */ +static bool skcipher_is_data_len_valid(size_t data_len) +{ + return data_len >= SKCIPHER_MIN_DATA_LEN && + data_len % SKCIPHER_DATA_LEN_ALIGN == 0; +} + +/* + * Generate a random valid data length that is <= @max_len. @max_len must be at + * least SKCIPHER_MIN_DATA_LEN. + */ +static size_t skcipher_rand_data_len(size_t max_len) +{ + size_t len = round_down(rand_length(max_len), SKCIPHER_DATA_LEN_ALIGN); + + return max(len, SKCIPHER_MIN_DATA_LEN); +} + +/* Return true if key_len is a valid key length and is currently allowed. */ +static bool skcipher_is_key_len_expected_valid(size_t key_len) +{ + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) { + if (SKCIPHER_VALID_KEY_LENS[i] == key_len) + return SKCIPHER_IS_KEY_LEN_ALLOWED(key_len); + } + return false; +} + +/* Select a random key length that is valid and currently allowed. */ +static size_t skcipher_rand_key_len(void) +{ + size_t key_len; + + do { + key_len = SKCIPHER_VALID_KEY_LENS + [rand32() % ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS)]; + } while (!SKCIPHER_IS_KEY_LEN_ALLOWED(key_len)); + return key_len; +} + +struct skcipher_test_keys { + struct SKCIPHER_KEY keys[ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS)]; + size_t num_keys; +}; + +/* + * Allocate and prepare one struct SKCIPHER_KEY for each currently-allowed key + * length in SKCIPHER_VALID_KEY_LENS, using random key bytes. + */ +static struct skcipher_test_keys *skcipher_alloc_test_keys(struct kunit *test) +{ + struct skcipher_test_keys *keys = alloc_buf(test, sizeof(*keys)); + u8 raw_key[SKCIPHER_MAX_KEY_LEN]; + int err; + + keys->num_keys = 0; + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) { + size_t key_len = SKCIPHER_VALID_KEY_LENS[i]; + + KUNIT_ASSERT_LE(test, key_len, SKCIPHER_MAX_KEY_LEN); + if (!SKCIPHER_IS_KEY_LEN_ALLOWED(key_len)) + continue; + rand_bytes(raw_key, key_len); + err = SKCIPHER_PREPAREKEY(&keys->keys[keys->num_keys++], + raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + } + KUNIT_ASSERT_GT(test, keys->num_keys, 0); + return keys; +} + +static const struct SKCIPHER_KEY * +skcipher_pick_key(const struct skcipher_test_keys *keys, size_t idx) +{ + return &keys->keys[idx % keys->num_keys]; +} + +/* + * Allocate a KUnit-managed IV state buffer that is immediately followed by an + * unmapped page. If SKCIPHER_IV_LEN == 0, the returned pointer points to the + * unmapped page itself, so any access to the IV state will fault. + */ +static u8 *skcipher_alloc_guarded_iv(struct kunit *test) +{ + return alloc_guarded_buf(test, SKCIPHER_IV_BUF_SIZE) + + SKCIPHER_IV_BUF_SIZE - SKCIPHER_IV_LEN; +} + +/* + * Run the given test vectors. This is called by each test suite, which defines + * its own test vectors after including this file. + */ +static __maybe_unused void +skcipher_run_testvecs(struct kunit *test, const struct skcipher_testvec *tvs, + size_t num_tvs) +{ + const size_t max_data_len = 512; + struct SKCIPHER_KEY *key = alloc_buf(test, sizeof(*key)); + u8 *out = alloc_buf(test, max_data_len); + u8 iv[SKCIPHER_IV_BUF_SIZE]; + int err; + + for (size_t i = 0; i < num_tvs; i++) { + const struct skcipher_testvec *tv = &tvs[i]; + + if (!SKCIPHER_IS_KEY_LEN_ALLOWED(tv->key_len)) + continue; + + KUNIT_ASSERT_LE(test, tv->data_len, max_data_len); + KUNIT_ASSERT_TRUE(test, + skcipher_is_data_len_valid(tv->data_len)); + if (tv->next_iv) + KUNIT_ASSERT_TRUE(test, skcipher_is_next_iv_defined( + tv->data_len)); + + err = SKCIPHER_PREPAREKEY(key, tv->key, tv->key_len); + KUNIT_ASSERT_EQ_MSG(test, 0, err, + "Failed to prepare key for %s", tv->name); + + skcipher_copy_iv(iv, tv->iv); + SKCIPHER_ENCRYPT(out, tv->ptext, tv->data_len, iv, key, false); + KUNIT_ASSERT_MEMEQ_MSG(test, tv->ctext, out, tv->data_len, + "Wrong ciphertext for %s", tv->name); + if (tv->next_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, tv->next_iv, iv, SKCIPHER_IV_LEN, + "Wrong next IV after encryption for %s", + tv->name); + + skcipher_copy_iv(iv, tv->iv); + SKCIPHER_DECRYPT(out, tv->ctext, tv->data_len, iv, key, false); + KUNIT_ASSERT_MEMEQ_MSG(test, tv->ptext, out, tv->data_len, + "Wrong plaintext for %s", tv->name); + if (tv->next_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, tv->next_iv, iv, SKCIPHER_IV_LEN, + "Wrong next IV after decryption for %s", + tv->name); + } +} + +/* + * Given an expected-valid key_len, verify that preparing a key with that length + * succeeds and that encryption and decryption round-trip with it. Use guarded + * buffers for the key struct, the raw key, and the IV state to detect any + * buffer overruns. + */ +static void skcipher_verify_valid_key_len(struct kunit *test, + struct SKCIPHER_KEY *key, + const u8 *raw_key_buf_end, + size_t key_len, u8 *iv, + const u8 *orig_iv) +{ + const u8 *raw_key = raw_key_buf_end - key_len; + u8 pt[64], ct[64], decrypted[64]; + int err; + + static_assert(sizeof(pt) >= SKCIPHER_MIN_DATA_LEN); + static_assert(sizeof(pt) % SKCIPHER_DATA_LEN_ALIGN == 0); + rand_bytes(pt, sizeof(pt)); + + err = SKCIPHER_PREPAREKEY(key, raw_key, key_len); + KUNIT_ASSERT_EQ_MSG(test, 0, err, "key_len=%zu wasn't accepted", + key_len); + + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_ENCRYPT(ct, pt, sizeof(pt), iv, key, false); + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_DECRYPT(decrypted, ct, sizeof(pt), iv, key, false); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, decrypted, sizeof(pt), + "Decryption didn't invert encryption with key_len=%zu", + key_len); +} + +/* Verify that the given expected-invalid key_len is actually rejected. */ +static void skcipher_verify_invalid_key_len(struct kunit *test, + struct SKCIPHER_KEY *key, + const u8 *unmapped_ptr, + size_t key_len) +{ + int err; + + /* + * The preparekey function should reject the key_len without + * dereferencing the raw key pointer, and it should leave the key struct + * zeroed. (It may either not write to it at all or zeroize it.) + */ + memset(key, 0, sizeof(*key)); + err = SKCIPHER_PREPAREKEY(key, unmapped_ptr, key_len); + KUNIT_ASSERT_EQ_MSG(test, -EINVAL, err, + "key_len=%zu wasn't rejected with -EINVAL", + key_len); + KUNIT_ASSERT_TRUE_MSG( + test, mem_is_zero(key, sizeof(*key)), + "Key struct wasn't left zeroed on invalid key_len=%zu", + key_len); +} + +/* + * Test that every valid key length is accepted and works, and test that invalid + * key lengths are rejected. + */ +static void test_skcipher_all_key_lens(struct kunit *test) +{ + struct SKCIPHER_KEY *key = alloc_guarded_buf(test, sizeof(*key)); + /* + * raw_key_buf_end points to the unmapped guard page immediately + * following the guarded buffer. Passing it when testing invalid key + * lengths ensures that any dereference of the raw key pointer faults. + */ + const u8 *raw_key_buf_end = + alloc_guarded_rand_buf(test, SKCIPHER_MAX_KEY_LEN) + + SKCIPHER_MAX_KEY_LEN; + u8 *iv = skcipher_alloc_guarded_iv(test); + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + + rand_bytes(orig_iv, sizeof(orig_iv)); + + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) + KUNIT_ASSERT_LE(test, SKCIPHER_VALID_KEY_LENS[i], + SKCIPHER_MAX_KEY_LEN); + + for (size_t key_len = 0; key_len <= SKCIPHER_MAX_KEY_LEN; key_len++) { + if (skcipher_is_key_len_expected_valid(key_len)) + skcipher_verify_valid_key_len(test, key, + raw_key_buf_end, key_len, + iv, orig_iv); + else + skcipher_verify_invalid_key_len( + test, key, raw_key_buf_end, key_len); + } + skcipher_verify_invalid_key_len(test, key, raw_key_buf_end, + SKCIPHER_MAX_KEY_LEN + 1); + skcipher_verify_invalid_key_len(test, key, raw_key_buf_end, + SKCIPHER_MAX_KEY_LEN * 2); + skcipher_verify_invalid_key_len(test, key, raw_key_buf_end, U32_MAX); + skcipher_verify_invalid_key_len(test, key, raw_key_buf_end, SIZE_MAX); +#ifdef CONFIG_64BIT + /* Check for 64-to-32-bit truncation of key_len. */ + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) + skcipher_verify_invalid_key_len( + test, key, raw_key_buf_end, + ((size_t)U32_MAX + 1) + SKCIPHER_VALID_KEY_LENS[i]); +#endif +} + +/* + * Monte-Carlo test. This deterministically generates random inputs for every + * valid data length up to 1024 bytes, encrypts them, verifies decryption, and + * computes and verifies the checksum of all the computed ciphertexts. The + * expected checksum was computed using an independent implementation of the + * algorithm (see scripts/crypto/gen-skcipher-testvecs.py), so this test + * provides known-answer test coverage for many inputs at once. + */ +static void test_skcipher_monte_carlo(struct kunit *test) +{ + const size_t max_data_len = 1024; + u8 raw_key[SKCIPHER_MAX_KEY_LEN]; + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + u8 *pt = alloc_buf(test, max_data_len); + u8 *ct = alloc_buf(test, max_data_len); + u8 *decrypted = alloc_buf(test, max_data_len); + struct SKCIPHER_KEY *key = alloc_buf(test, sizeof(*key)); + struct blake2s_ctx checksum_ctx; + u8 actual_checksum[BLAKE2S_HASH_SIZE]; + int err; + + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) { + /* + * The expected checksum covers all the valid key lengths, so + * this test isn't applicable if any of them is disallowed. + */ + if (!SKCIPHER_IS_KEY_LEN_ALLOWED(SKCIPHER_VALID_KEY_LENS[i])) + kunit_skip( + test, + "not applicable, since key_len=%zu is disallowed (FIPS mode)", + SKCIPHER_VALID_KEY_LENS[i]); + } + + blake2s_init(&checksum_ctx, BLAKE2S_HASH_SIZE); + + for (size_t data_len = 0; data_len <= max_data_len; data_len++) { + size_t key_len; + + if (!skcipher_is_data_len_valid(data_len)) + continue; + + key_len = SKCIPHER_VALID_KEY_LENS + [(data_len / SKCIPHER_DATA_LEN_ALIGN) % + ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS)]; + + random_seed = data_len; + rand_bytes(raw_key, key_len); + rand_bytes(orig_iv, SKCIPHER_IV_LEN); + rand_bytes(pt, data_len); + + err = SKCIPHER_PREPAREKEY(key, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_ENCRYPT(ct, pt, data_len, iv, key, false); + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_DECRYPT(decrypted, ct, data_len, iv, key, false); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, decrypted, data_len, + "Decryption didn't invert encryption; data_len=%zu", + data_len); + + blake2s_update(&checksum_ctx, ct, data_len); + } + + blake2s_final(&checksum_ctx, actual_checksum); + KUNIT_EXPECT_MEMEQ_MSG(test, actual_checksum, + SKCIPHER_MONTE_CARLO_CHECKSUM, BLAKE2S_HASH_SIZE, + "Monte-Carlo checksum mismatch"); +} + +#ifdef SKCIPHER_INCREMENTAL +/* + * En/decrypt a message using a random incremental strategy. The length of each + * part will be at least SKCIPHER_MIN_DATA_LEN (assuming @len >= + * SKCIPHER_MIN_DATA_LEN), and if it's a non-final part the length will also be + * a multiple of SKCIPHER_NONFINAL_DATA_LEN_ALIGN. Return the number of parts. + */ +static size_t skcipher_crypt_incrementally(u8 *dst, const u8 *src, size_t len, + u8 *iv, + const struct SKCIPHER_KEY *key, + bool enc) +{ + size_t num_parts = 0; + size_t pos = 0; + + while (rand_bool() && len - pos >= 2 * SKCIPHER_MIN_DATA_LEN) { + size_t part_len = round_down( + rand_length(len - pos - SKCIPHER_MIN_DATA_LEN), + SKCIPHER_NONFINAL_DATA_LEN_ALIGN); + + part_len = max(part_len, SKCIPHER_MIN_DATA_LEN); + skcipher_crypt(&dst[pos], &src[pos], part_len, iv, key, + num_parts != 0, enc); + pos += part_len; + num_parts++; + } + if (pos < len || rand_bool()) { + skcipher_crypt(&dst[pos], &src[pos], len - pos, iv, key, + num_parts != 0, enc); + num_parts++; + } + return num_parts; +} + +/* + * Test that one-shot encryption and decryption are consistent with incremental + * encryption and decryption using random split points. + */ +static void test_skcipher_incremental(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 *ct = alloc_buf(test, max_data_len); + u8 *ct2 = alloc_buf(test, max_data_len); + u8 *decrypted = alloc_buf(test, max_data_len); + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv2[SKCIPHER_IV_BUF_SIZE]; + + for (int i = 0; i < 500; i++) { + const struct SKCIPHER_KEY *key = skcipher_pick_key(keys, i); + size_t data_len = skcipher_rand_data_len(max_data_len); + bool check_iv = skcipher_is_next_iv_defined(data_len); + size_t num_parts; + + rand_bytes(orig_iv, sizeof(orig_iv)); + + /* Encrypt one-shot, then incrementally. */ + memset(ct, 0, data_len); + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_ENCRYPT(ct, pt, data_len, iv, key, false); + memset(ct2, 0, data_len); + skcipher_copy_iv(iv2, orig_iv); + num_parts = skcipher_crypt_incrementally(ct2, pt, data_len, iv2, + key, true); + KUNIT_ASSERT_MEMEQ_MSG( + test, ct, ct2, data_len, + "One-shot and incremental encryption differ; data_len=%zu num_parts=%zu", + data_len, num_parts); + if (check_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, iv, iv2, SKCIPHER_IV_LEN, + "One-shot and incremental encryption gave different next IV; data_len=%zu num_parts=%zu", + data_len, num_parts); + + /* Decrypt one-shot, then incrementally. */ + memset(decrypted, 0, data_len); + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_DECRYPT(decrypted, ct, data_len, iv, key, false); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, decrypted, data_len, + "Decryption didn't invert encryption; data_len=%zu", + data_len); + memset(decrypted, 0, data_len); + skcipher_copy_iv(iv2, orig_iv); + num_parts = skcipher_crypt_incrementally( + decrypted, ct, data_len, iv2, key, false); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, decrypted, data_len, + "Incremental decryption gave wrong plaintext; data_len=%zu num_parts=%zu", + data_len, num_parts); + if (check_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, iv, iv2, SKCIPHER_IV_LEN, + "One-shot and incremental decryption gave different next IV; data_len=%zu num_parts=%zu", + data_len, num_parts); + } +} + +static void skcipher_check_all_splits(struct kunit *test, + const struct SKCIPHER_KEY *key, + const u8 *pt, u8 *ct, u8 *out, + size_t max_data_len) +{ + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + u8 next_iv_enc[SKCIPHER_IV_BUF_SIZE]; + u8 next_iv_dec[SKCIPHER_IV_BUF_SIZE]; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + + rand_bytes(orig_iv, sizeof(orig_iv)); + + for (size_t data_len = 0; data_len <= max_data_len; data_len++) { + bool check_iv = skcipher_is_next_iv_defined(data_len); + + if (!skcipher_is_data_len_valid(data_len)) + continue; + + /* Compute the expected results using one-shot en/decryption. */ + skcipher_copy_iv(next_iv_enc, orig_iv); + SKCIPHER_ENCRYPT(ct, pt, data_len, next_iv_enc, key, false); + skcipher_copy_iv(next_iv_dec, orig_iv); + SKCIPHER_DECRYPT(out, ct, data_len, next_iv_dec, key, false); + KUNIT_ASSERT_MEMEQ(test, pt, out, data_len); + + for (size_t split = SKCIPHER_MIN_DATA_LEN; + split + SKCIPHER_MIN_DATA_LEN <= data_len; + split += SKCIPHER_NONFINAL_DATA_LEN_ALIGN) { + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_ENCRYPT(out, pt, split, iv, key, false); + SKCIPHER_ENCRYPT(&out[split], &pt[split], + data_len - split, iv, key, true); + KUNIT_ASSERT_MEMEQ_MSG( + test, ct, out, data_len, + "Wrong ciphertext with data_len=%zu split=%zu", + data_len, split); + if (check_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, next_iv_enc, iv, SKCIPHER_IV_LEN, + "Wrong next IV after encryption with data_len=%zu split=%zu", + data_len, split); + + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_DECRYPT(out, ct, split, iv, key, false); + SKCIPHER_DECRYPT(&out[split], &ct[split], + data_len - split, iv, key, true); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, out, data_len, + "Wrong plaintext with data_len=%zu split=%zu", + data_len, split); + if (check_iv) + KUNIT_ASSERT_MEMEQ_MSG( + test, next_iv_dec, iv, SKCIPHER_IV_LEN, + "Wrong next IV after decryption with data_len=%zu split=%zu", + data_len, split); + } + } +} + +/* + * For every valid data length up to a small maximum, try every possible way of + * splitting the message into two parts, and verify that the result matches + * one-shot en/decryption. This deterministically covers the edge cases in the + * handling of the end of the message, e.g. partial blocks and ciphertext + * stealing, in combination with continuing a message. + */ +static void test_skcipher_incremental_all_splits(struct kunit *test) +{ + const size_t max_data_len = 96; + const struct skcipher_test_keys *keys = skcipher_alloc_test_keys(test); + const u8 *pt = alloc_rand_buf(test, max_data_len); + u8 *ct = alloc_buf(test, max_data_len); + u8 *out = alloc_buf(test, max_data_len); + + for (size_t i = 0; i < keys->num_keys; i++) + skcipher_check_all_splits(test, &keys->keys[i], pt, ct, out, + max_data_len); +} +#endif /* SKCIPHER_INCREMENTAL */ + +#define SKCIPHER_POISON 0xa5 +#define SKCIPHER_POISON_LEN 64 + +static void skcipher_check_buffer_overruns(struct kunit *test, + const struct SKCIPHER_KEY *key, + size_t data_len, const u8 *pt_orig, + u8 *src_end, u8 *dst_end, u8 *ct, + u8 *iv) +{ + /* Set up exact-size guarded buffers. */ + u8 *src = src_end - data_len; + u8 *dst = dst_end - data_len; + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + + rand_bytes(orig_iv, sizeof(orig_iv)); + + for (int enc = 1; enc >= 0; enc--) { + const u8 *expected_src = enc ? pt_orig : ct; + + memcpy(src, expected_src, data_len); + memset(dst - SKCIPHER_POISON_LEN, SKCIPHER_POISON, + SKCIPHER_POISON_LEN + data_len); + memset(iv - SKCIPHER_POISON_LEN, SKCIPHER_POISON, + SKCIPHER_POISON_LEN); + skcipher_copy_iv(iv, orig_iv); + + skcipher_crypt(dst, src, data_len, iv, key, false, enc); + + KUNIT_ASSERT_MEMEQ_MSG(test, src, expected_src, data_len, + "Source modified; data_len=%zu enc=%d", + data_len, enc); + KUNIT_ASSERT_NULL_MSG( + test, + memchr_inv(dst - SKCIPHER_POISON_LEN, SKCIPHER_POISON, + SKCIPHER_POISON_LEN), + "Write before dst; data_len=%zu enc=%d", data_len, enc); + KUNIT_ASSERT_NULL_MSG( + test, + memchr_inv(iv - SKCIPHER_POISON_LEN, SKCIPHER_POISON, + SKCIPHER_POISON_LEN), + "Write before IV state; data_len=%zu enc=%d", data_len, + enc); + if (data_len == 0 && SKCIPHER_IV_LEN != 0) + KUNIT_ASSERT_MEMEQ_MSG( + test, iv, orig_iv, SKCIPHER_IV_LEN, + "IV state modified with data_len=0; enc=%d", + enc); + if (enc) + memcpy(ct, dst, data_len); + else + KUNIT_ASSERT_MEMEQ_MSG( + test, dst, pt_orig, data_len, + "Decryption didn't invert encryption; data_len=%zu", + data_len); + } +} + +/* + * Test using guarded buffers for the source, destination, and IV state. This + * detects out-of-bounds accesses past the end of the buffers, even in assembly + * code. Also check for writes before the beginning of the destination and IV + * state buffers, check that the source buffer isn't modified, and check that + * the IV state isn't modified when the data length is 0. + * + * Note: test_skcipher_all_key_lens() covers overrun of the raw key and key + * struct. + */ +static void test_skcipher_buffer_overruns(struct kunit *test) +{ + const size_t max_data_len = 4096; + const size_t buf_len = max_data_len + SKCIPHER_POISON_LEN; + const size_t iv_buf_len = SKCIPHER_POISON_LEN + SKCIPHER_IV_LEN; + const struct skcipher_test_keys *keys = skcipher_alloc_test_keys(test); + const u8 *pt_orig = alloc_rand_buf(test, max_data_len); + u8 *src_end = alloc_guarded_buf(test, max_data_len) + max_data_len; + u8 *dst_end = alloc_guarded_buf(test, buf_len) + buf_len; + u8 *ct = alloc_buf(test, max_data_len); + /* + * The IV state is immediately followed by an unmapped page and is + * preceded by SKCIPHER_POISON_LEN bytes of poison. If + * SKCIPHER_IV_LEN == 0, 'iv' points to the unmapped page itself. + */ + u8 *iv = alloc_guarded_buf(test, iv_buf_len) + iv_buf_len - + SKCIPHER_IV_LEN; + + /* Deterministically test all valid short lengths with every key. */ + for (size_t i = 0; i < keys->num_keys; i++) { + for (size_t data_len = SKCIPHER_MIN_DATA_LEN; data_len <= 256; + data_len += SKCIPHER_DATA_LEN_ALIGN) + skcipher_check_buffer_overruns(test, &keys->keys[i], + data_len, pt_orig, + src_end, dst_end, ct, + iv); + } + + /* Also test random lengths up to max_data_len. */ + for (int i = 0; i < 200; i++) + skcipher_check_buffer_overruns( + test, skcipher_pick_key(keys, i), + skcipher_rand_data_len(max_data_len), pt_orig, src_end, + dst_end, ct, iv); +} + +/* + * Test that encryption and decryption produce the same results regardless of + * how the buffers and key structs are aligned in memory. + */ +static void test_skcipher_alignment_consistency(struct kunit *test) +{ + const size_t max_data_len = 4096; + const size_t max_offset = 128; + const size_t key_align = __alignof__(struct SKCIPHER_KEY); + const size_t key_buf_len = + sizeof(struct SKCIPHER_KEY) + max_offset + key_align - 1; + u8 *key1_buf = alloc_buf(test, key_buf_len); + u8 *key2_buf = alloc_buf(test, key_buf_len); + u8 *raw_key1_buf = alloc_buf(test, SKCIPHER_MAX_KEY_LEN + max_offset); + u8 *raw_key2_buf = alloc_buf(test, SKCIPHER_MAX_KEY_LEN + max_offset); + u8 *iv1_buf = alloc_buf(test, SKCIPHER_IV_BUF_SIZE + max_offset); + u8 *iv2_buf = alloc_buf(test, SKCIPHER_IV_BUF_SIZE + max_offset); + u8 *pt1_buf = alloc_buf(test, max_data_len); + u8 *pt2_buf = alloc_buf(test, max_data_len); + u8 *ct1_buf = alloc_buf(test, max_data_len); + u8 *ct2_buf = alloc_buf(test, max_data_len); + u8 *pt_orig = alloc_buf(test, max_data_len); + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + int err; + + for (int i = 0; i < 100; i++) { + /* Generate lengths. */ + const size_t data_len = skcipher_rand_data_len(max_data_len); + const size_t key_len = skcipher_rand_key_len(); + const bool check_iv = skcipher_is_next_iv_defined(data_len); + + /* Generate two sets of alignments. */ + struct SKCIPHER_KEY *key1 = (void *)PTR_ALIGN( + key1_buf + rand_offset(max_offset), key_align); + struct SKCIPHER_KEY *key2 = (void *)PTR_ALIGN( + key2_buf + rand_offset(max_offset), key_align); + u8 *raw_key1 = raw_key1_buf + rand_offset(max_offset); + u8 *raw_key2 = raw_key2_buf + rand_offset(max_offset); + u8 *iv1 = iv1_buf + rand_offset(max_offset); + u8 *iv2 = iv2_buf + rand_offset(max_offset); + u8 *pt1 = pt1_buf + rand_offset(max_data_len - data_len); + u8 *pt2 = pt2_buf + rand_offset(max_data_len - data_len); + u8 *ct1 = ct1_buf + rand_offset(max_data_len - data_len); + u8 *ct2 = ct2_buf + rand_offset(max_data_len - data_len); + + /* Generate inputs and copy them to both sets of buffers. */ + rand_bytes(raw_key1, key_len); + rand_bytes(orig_iv, sizeof(orig_iv)); + rand_bytes(pt1, data_len); + memcpy(raw_key2, raw_key1, key_len); + memcpy(pt2, pt1, data_len); + + /* Verify encryption consistency. */ + + err = SKCIPHER_PREPAREKEY(key1, raw_key1, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + skcipher_copy_iv(iv1, orig_iv); + SKCIPHER_ENCRYPT(ct1, pt1, data_len, iv1, key1, false); + + err = SKCIPHER_PREPAREKEY(key2, raw_key2, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + skcipher_copy_iv(iv2, orig_iv); + SKCIPHER_ENCRYPT(ct2, pt2, data_len, iv2, key2, false); + + KUNIT_ASSERT_MEMEQ(test, ct1, ct2, data_len); + if (check_iv) + KUNIT_ASSERT_MEMEQ(test, iv1, iv2, SKCIPHER_IV_LEN); + + /* Verify decryption consistency. */ + + /* + * Decryption overwrites pt1 and pt2, so save the original + * plaintext for the round-trip check, and clear pt1 and pt2 so + * that a decryption that doesn't write to dst gets detected. + */ + memcpy(pt_orig, pt1, data_len); + memset(pt1, 0, data_len); + memset(pt2, 0, data_len); + + skcipher_copy_iv(iv1, orig_iv); + SKCIPHER_DECRYPT(pt1, ct1, data_len, iv1, key1, false); + + skcipher_copy_iv(iv2, orig_iv); + SKCIPHER_DECRYPT(pt2, ct2, data_len, iv2, key2, false); + + KUNIT_ASSERT_MEMEQ_MSG( + test, pt_orig, pt1, data_len, + "Decryption didn't invert encryption; data_len=%zu", + data_len); + KUNIT_ASSERT_MEMEQ(test, pt1, pt2, data_len); + if (check_iv) + KUNIT_ASSERT_MEMEQ(test, iv1, iv2, SKCIPHER_IV_LEN); + } +} + +static void skcipher_check_inplace(struct kunit *test, + const struct SKCIPHER_KEY *key, + size_t data_len, u8 *buf1, u8 *buf2, + size_t max_offset) +{ + const bool check_iv = skcipher_is_next_iv_defined(data_len); + const size_t offset = rand_offset(max_offset); + u8 *data = buf1 + offset; + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv2[SKCIPHER_IV_BUF_SIZE]; + + rand_bytes(orig_iv, sizeof(orig_iv)); + + for (int enc = 1; enc >= 0; enc--) { + /* En/decrypt out-of-place. */ + skcipher_copy_iv(iv, orig_iv); + skcipher_crypt(buf2, data, data_len, iv, key, false, enc); + + /* En/decrypt in-place. */ + skcipher_copy_iv(iv2, orig_iv); + skcipher_crypt(data, data, data_len, iv2, key, false, enc); + + /* Compare the results. */ + KUNIT_ASSERT_MEMEQ_MSG( + test, buf2, data, data_len, + "In-place and out-of-place results differ; data_len=%zu offset=%zu enc=%d", + data_len, offset, enc); + if (check_iv) + KUNIT_ASSERT_MEMEQ(test, iv, iv2, SKCIPHER_IV_LEN); + } +} + +/* Test that in-place en/decryption gives the same results as out-of-place. */ +static void test_skcipher_inplace(struct kunit *test) +{ + const size_t max_data_len = 4096; + const size_t max_offset = 128; + const struct skcipher_test_keys *keys = skcipher_alloc_test_keys(test); + u8 *buf1 = alloc_rand_buf(test, max_data_len + max_offset); + u8 *buf2 = alloc_buf(test, max_data_len); + + /* Deterministically test all valid short lengths with every key. */ + for (size_t i = 0; i < keys->num_keys; i++) { + for (size_t data_len = SKCIPHER_MIN_DATA_LEN; data_len <= 256; + data_len += SKCIPHER_DATA_LEN_ALIGN) + skcipher_check_inplace(test, &keys->keys[i], data_len, + buf1, buf2, max_offset); + } + + /* Also test random lengths up to max_data_len. */ + for (int i = 0; i < 100; i++) + skcipher_check_inplace(test, skcipher_pick_key(keys, i), + skcipher_rand_data_len(max_data_len), + buf1, buf2, max_offset); +} + +#ifdef SKCIPHER_ENCKEY +/* + * Verify that the given expected-invalid key_len is rejected by + * SKCIPHER_PREPAREENCKEY(). + */ +static void skcipher_verify_invalid_enckey_len(struct kunit *test, + struct SKCIPHER_ENCKEY *enckey, + const u8 *unmapped_ptr, + size_t key_len) +{ + int err; + + memset(enckey, 0, sizeof(*enckey)); + err = SKCIPHER_PREPAREENCKEY(enckey, unmapped_ptr, key_len); + KUNIT_ASSERT_EQ_MSG(test, -EINVAL, err, + "key_len=%zu wasn't rejected with -EINVAL", + key_len); + KUNIT_ASSERT_TRUE_MSG( + test, mem_is_zero(enckey, sizeof(*enckey)), + "Key struct wasn't left zeroed on invalid key_len=%zu", + key_len); +} + +/* + * Test that encryption using a key prepared for encryption only + * (SKCIPHER_ENCKEY) gives the same results as encryption using SKCIPHER_KEY. + * Also test that SKCIPHER_PREPAREENCKEY verifies the key length properly. + */ +static void test_skcipher_enckey(struct kunit *test) +{ + const size_t max_data_len = 4096; + struct SKCIPHER_KEY *key = alloc_buf(test, sizeof(*key)); + struct SKCIPHER_ENCKEY *enckey = + alloc_guarded_buf(test, sizeof(*enckey)); + const u8 *pt = alloc_rand_buf(test, max_data_len); + u8 *ct = alloc_buf(test, max_data_len); + u8 *ct2 = alloc_buf(test, max_data_len); + u8 *raw_key_buf_end = alloc_guarded_buf(test, SKCIPHER_MAX_KEY_LEN) + + SKCIPHER_MAX_KEY_LEN; + u8 orig_iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + u8 iv2[SKCIPHER_IV_BUF_SIZE]; + int err; + + for (size_t key_len = 0; key_len <= SKCIPHER_MAX_KEY_LEN; key_len++) { + if (!skcipher_is_key_len_expected_valid(key_len)) + skcipher_verify_invalid_enckey_len( + test, enckey, raw_key_buf_end, key_len); + } + skcipher_verify_invalid_enckey_len(test, enckey, raw_key_buf_end, + SKCIPHER_MAX_KEY_LEN + 1); + skcipher_verify_invalid_enckey_len(test, enckey, raw_key_buf_end, + SKCIPHER_MAX_KEY_LEN * 2); + skcipher_verify_invalid_enckey_len(test, enckey, raw_key_buf_end, + U32_MAX); + skcipher_verify_invalid_enckey_len(test, enckey, raw_key_buf_end, + SIZE_MAX); +#ifdef CONFIG_64BIT + /* Check for 64-to-32-bit truncation of key_len. */ + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) + skcipher_verify_invalid_enckey_len( + test, enckey, raw_key_buf_end, + ((size_t)U32_MAX + 1) + SKCIPHER_VALID_KEY_LENS[i]); +#endif + + for (int i = 0; i < 100; i++) { + const size_t data_len = skcipher_rand_data_len(max_data_len); + const size_t key_len = skcipher_rand_key_len(); + u8 *raw_key = raw_key_buf_end - key_len; + + rand_bytes(raw_key, key_len); + rand_bytes(orig_iv, sizeof(orig_iv)); + err = SKCIPHER_PREPAREKEY(key, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + err = SKCIPHER_PREPAREENCKEY(enckey, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + + skcipher_copy_iv(iv, orig_iv); + SKCIPHER_ENCRYPT(ct, pt, data_len, iv, key, false); + skcipher_copy_iv(iv2, orig_iv); + SKCIPHER_ENCRYPT(ct2, pt, data_len, iv2, enckey, false); + KUNIT_ASSERT_MEMEQ_MSG( + test, ct, ct2, data_len, + "Encryption with enckey differs; data_len=%zu key_len=%zu", + data_len, key_len); + if (skcipher_is_next_iv_defined(data_len)) + KUNIT_ASSERT_MEMEQ(test, iv, iv2, SKCIPHER_IV_LEN); + } +} +#endif /* SKCIPHER_ENCKEY */ + +/* + * The data length used by the interrupt context test. When the algorithm + * allows it, this isn't a multiple of the block size, so that the fallback code + * for handling partial blocks (or ciphertext stealing) gets tested too. + */ +#define IRQ_TEST_DATA_LEN round_down(263, SKCIPHER_DATA_LEN_ALIGN) +static_assert(IRQ_TEST_DATA_LEN >= SKCIPHER_MIN_DATA_LEN); +static_assert(IRQ_TEST_DATA_LEN > 0); +#define IRQ_TEST_NUM_BUFFERS 3 /* matches max concurrency level */ + +struct skcipher_irq_test_slot { + /* Fields written only at test case initialization time */ + u8 raw_key[SKCIPHER_MAX_KEY_LEN]; + size_t key_len; + u8 iv[SKCIPHER_IV_BUF_SIZE]; + u8 next_iv_enc[SKCIPHER_IV_BUF_SIZE]; + u8 next_iv_dec[SKCIPHER_IV_BUF_SIZE]; + u8 pt[IRQ_TEST_DATA_LEN]; + u8 ct[IRQ_TEST_DATA_LEN]; + + /* Fields written throughout the test case */ + struct SKCIPHER_KEY key; + u8 scratch_iv[SKCIPHER_IV_BUF_SIZE]; + u8 scratch_buf[IRQ_TEST_DATA_LEN]; + int phase; + atomic_t in_use; +}; + +struct skcipher_irq_test_state { + struct skcipher_irq_test_slot slots[IRQ_TEST_NUM_BUFFERS]; +}; + +static bool skcipher_irq_test_func(void *state_) +{ + struct skcipher_irq_test_state *state = state_; + struct skcipher_irq_test_slot *slot; + bool ok = true; + + /* + * Find a free slot. This should always succeed, since the number of + * slots is equal to the max concurrency level of kunit_run_irq_test(). + */ + for (slot = &state->slots[0]; + slot < &state->slots[ARRAY_SIZE(state->slots)]; slot++) { + if (atomic_cmpxchg(&slot->in_use, 0, 1) == 0) + break; + } + if (WARN_ON_ONCE(slot == &state->slots[ARRAY_SIZE(state->slots)])) + return false; + /* + * This execution context now has exclusive access to 'slot'. + * Next, execute the next operation that the slot is set to perform. + */ + + if (slot->phase == 0) { + /* Phase 0: Prepare slot's key in current context. */ + ok = SKCIPHER_PREPAREKEY(&slot->key, slot->raw_key, + slot->key_len) == 0; + } else if (slot->phase == 1) { + /* + * Phase 1: Encrypt plaintext using key that may have been + * prepared in a different context. + */ + skcipher_copy_iv(slot->scratch_iv, slot->iv); + SKCIPHER_ENCRYPT(slot->scratch_buf, slot->pt, IRQ_TEST_DATA_LEN, + slot->scratch_iv, &slot->key, false); + ok = memcmp(slot->scratch_buf, slot->ct, IRQ_TEST_DATA_LEN) == + 0; + if (skcipher_is_next_iv_defined(IRQ_TEST_DATA_LEN)) + ok = ok && memcmp(slot->scratch_iv, slot->next_iv_enc, + SKCIPHER_IV_LEN) == 0; + } else { + /* + * Phase 2: Decrypt ciphertext using key that may have been + * prepared in a different context. + */ + skcipher_copy_iv(slot->scratch_iv, slot->iv); + SKCIPHER_DECRYPT(slot->scratch_buf, slot->ct, IRQ_TEST_DATA_LEN, + slot->scratch_iv, &slot->key, false); + ok = memcmp(slot->scratch_buf, slot->pt, IRQ_TEST_DATA_LEN) == + 0; + if (skcipher_is_next_iv_defined(IRQ_TEST_DATA_LEN)) + ok = ok && memcmp(slot->scratch_iv, slot->next_iv_dec, + SKCIPHER_IV_LEN) == 0; + } + slot->phase = (slot->phase + 1) % 3; + atomic_set_release(&slot->in_use, 0); + return ok; +} + +/* + * Test that encryption and decryption produce the correct results in task, + * softirq, and hardirq contexts running concurrently -- including with keys + * prepared in other contexts. This is needed to cover fallback code paths that + * execute in contexts where FPU or vector registers cannot be used. + */ +static void test_skcipher_interrupt_context(struct kunit *test) +{ + struct skcipher_irq_test_state *state = alloc_buf(test, sizeof(*state)); + size_t allowed_key_lens[ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS)]; + size_t num_allowed_key_lens = 0; + + memset(state, 0, sizeof(*state)); + + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) { + size_t key_len = SKCIPHER_VALID_KEY_LENS[i]; + + if (SKCIPHER_IS_KEY_LEN_ALLOWED(key_len)) + allowed_key_lens[num_allowed_key_lens++] = key_len; + } + KUNIT_ASSERT_GT(test, num_allowed_key_lens, 0); + + /* + * For each slot, generate a key, an IV state, and a plaintext. Then + * generate the corresponding ciphertext and expected final IV states. + */ + for (int i = 0; i < IRQ_TEST_NUM_BUFFERS; i++) { + struct skcipher_irq_test_slot *slot = &state->slots[i]; + int err; + + slot->key_len = allowed_key_lens[i % num_allowed_key_lens]; + rand_bytes(slot->raw_key, slot->key_len); + rand_bytes(slot->iv, sizeof(slot->iv)); + rand_bytes(slot->pt, sizeof(slot->pt)); + err = SKCIPHER_PREPAREKEY(&slot->key, slot->raw_key, + slot->key_len); + KUNIT_ASSERT_EQ(test, 0, err); + skcipher_copy_iv(slot->next_iv_enc, slot->iv); + SKCIPHER_ENCRYPT(slot->ct, slot->pt, IRQ_TEST_DATA_LEN, + slot->next_iv_enc, &slot->key, false); + skcipher_copy_iv(slot->next_iv_dec, slot->iv); + SKCIPHER_DECRYPT(slot->scratch_buf, slot->ct, IRQ_TEST_DATA_LEN, + slot->next_iv_dec, &slot->key, false); + KUNIT_ASSERT_MEMEQ(test, slot->pt, slot->scratch_buf, + IRQ_TEST_DATA_LEN); + } + + kunit_run_irq_test(test, skcipher_irq_test_func, 100000, state); +} + +/* Benchmark encryption and decryption on various data lengths. */ +static void benchmark_skcipher(struct kunit *test) +{ + static const size_t data_lens_to_test[] = { + 16, 64, 128, 256, 512, 1024, 1420, 4096, 16384, + }; + const bool enc_is_dec = (void *)SKCIPHER_ENCRYPT == + (void *)SKCIPHER_DECRYPT; + const size_t max_data_len = 16384; + size_t key_len = 0; + u8 iv[SKCIPHER_IV_BUF_SIZE] = {}; + const u8 *raw_key; + struct SKCIPHER_KEY *key; + u8 *data; + int err; + + if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK)) + kunit_skip(test, "not enabled"); + + for (size_t i = 0; i < ARRAY_SIZE(SKCIPHER_VALID_KEY_LENS); i++) { + if (SKCIPHER_IS_KEY_LEN_ALLOWED(SKCIPHER_VALID_KEY_LENS[i])) + key_len = max(key_len, SKCIPHER_VALID_KEY_LENS[i]); + } + KUNIT_ASSERT_GT(test, key_len, 0); + + raw_key = alloc_rand_buf(test, key_len); + data = alloc_rand_buf(test, max_data_len); + key = alloc_buf(test, sizeof(*key)); + err = SKCIPHER_PREPAREKEY(key, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + + /* Warm-up */ + for (size_t i = 0; i < 10000000; i += max_data_len) { + SKCIPHER_ENCRYPT(data, data, max_data_len, iv, key, false); + if (!enc_is_dec) + SKCIPHER_DECRYPT(data, data, max_data_len, iv, key, + false); + } + + for (size_t i = 0; i < ARRAY_SIZE(data_lens_to_test); i++) { + size_t data_len = round_down(data_lens_to_test[i], + SKCIPHER_DATA_LEN_ALIGN); + size_t num_iters = 10000000 / (data_len + 128); + u64 t_enc, t_dec = 0; + + if (data_len < SKCIPHER_MIN_DATA_LEN) + continue; + + KUNIT_ASSERT_LE(test, data_len, max_data_len); + KUNIT_ASSERT_TRUE(test, skcipher_is_data_len_valid(data_len)); + + preempt_disable(); + + t_enc = ktime_get_ns(); + for (size_t j = 0; j < num_iters; j++) + SKCIPHER_ENCRYPT(data, data, data_len, iv, key, false); + t_enc = ktime_get_ns() - t_enc; + + if (!enc_is_dec) { + t_dec = ktime_get_ns(); + for (size_t j = 0; j < num_iters; j++) + SKCIPHER_DECRYPT(data, data, data_len, iv, key, + false); + t_dec = ktime_get_ns() - t_dec; + } + + preempt_enable(); + + if (enc_is_dec) + kunit_info(test, "data_len=%zu: %llu MB/s", data_len, + div64_u64((u64)data_len * num_iters * 1000, + t_enc ?: 1)); + else + kunit_info(test, + "data_len=%zu: enc %llu MB/s, dec %llu MB/s", + data_len, + div64_u64((u64)data_len * num_iters * 1000, + t_enc ?: 1), + div64_u64((u64)data_len * num_iters * 1000, + t_dec ?: 1)); + } +} + +/* clang-format off */ +#ifdef SKCIPHER_INCREMENTAL +#define SKCIPHER_INCREMENTAL_KUNIT_CASES \ + KUNIT_CASE(test_skcipher_incremental), \ + KUNIT_CASE(test_skcipher_incremental_all_splits), +#else +#define SKCIPHER_INCREMENTAL_KUNIT_CASES +#endif + +#ifdef SKCIPHER_ENCKEY +#define SKCIPHER_ENCKEY_KUNIT_CASES KUNIT_CASE(test_skcipher_enckey), +#else +#define SKCIPHER_ENCKEY_KUNIT_CASES +#endif + +#define SKCIPHER_KUNIT_CASES \ + KUNIT_CASE(test_skcipher_all_key_lens), \ + KUNIT_CASE(test_skcipher_monte_carlo), \ + SKCIPHER_INCREMENTAL_KUNIT_CASES \ + KUNIT_CASE(test_skcipher_buffer_overruns), \ + KUNIT_CASE(test_skcipher_alignment_consistency), \ + KUNIT_CASE(test_skcipher_inplace), \ + SKCIPHER_ENCKEY_KUNIT_CASES \ + KUNIT_CASE(test_skcipher_interrupt_context), \ + KUNIT_CASE(benchmark_skcipher) +/* clang-format on */ diff --git a/lib/crypto/tests/test-utils.h b/lib/crypto/tests/test-utils.h index 73ca21f9176e..c39b1dd93453 100644 --- a/lib/crypto/tests/test-utils.h +++ b/lib/crypto/tests/test-utils.h @@ -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; diff --git a/scripts/crypto/gen-skcipher-testvecs.py b/scripts/crypto/gen-skcipher-testvecs.py new file mode 100755 index 000000000000..8e2d8c6be59b --- /dev/null +++ b/scripts/crypto/gen-skcipher-testvecs.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Script that generates known-good data used in the KUnit tests for +# length-preserving symmetric ciphers. +# +# Requires that python-cryptography be installed. +# +# Copyright 2026 Google LLC + +import hashlib +import sys +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + +AES_BLOCK_SIZE = 16 + + +# Linear congruential generator matching rand32() in +# lib/crypto/tests/test-utils.h +class Rng: + def __init__(self, seed): + self.seed = seed + + def rand_bytes(self, length): + out = [] + for _ in range(length): + self.seed = (self.seed * 25214903917 + 11) % 2**48 + out.append((self.seed >> 16) % 256) + return bytes(out) + + +def xor(a, b): + return bytes(x ^ y for x, y in zip(a, b)) + + +def aes_encrypt_with_mode(key, mode, data): + encryptor = Cipher(algorithms.AES(key), mode).encryptor() + return encryptor.update(data) + encryptor.finalize() + + +def aes_ecb_encrypt(key, ptext): + return aes_encrypt_with_mode(key, modes.ECB(), ptext) + + +def aes_cbc_encrypt(key, iv, ptext): + return aes_encrypt_with_mode(key, modes.CBC(iv), ptext) + + +# AES-CBC-CTS, CS3 variant (i.e., the last two blocks are always swapped) +def aes_cbc_cts_encrypt(key, iv, ptext): + data_len = len(ptext) + assert data_len >= AES_BLOCK_SIZE + padded_len = -(-data_len // AES_BLOCK_SIZE) * AES_BLOCK_SIZE + ctext = aes_cbc_encrypt(key, iv, ptext.ljust(padded_len, b"\0")) + if padded_len == AES_BLOCK_SIZE: + return ctext + pn_len = data_len - (padded_len - AES_BLOCK_SIZE) + return ( + ctext[: padded_len - 2 * AES_BLOCK_SIZE] + + ctext[padded_len - AES_BLOCK_SIZE :] + + ctext[padded_len - 2 * AES_BLOCK_SIZE :][:pn_len] + ) + + +# AES-CTR with a 128-bit big endian counter +def aes_ctr_crypt(key, ctr, data): + return aes_encrypt_with_mode(key, modes.CTR(ctr), data) + + +# AES-XCTR. For consistency with the kernel tests, 'state' is the 64-bit little +# endian block counter followed by the 16-byte IV. +def aes_xctr_crypt(key, state, data): + ctr = int.from_bytes(state[:8], byteorder="little") + iv = state[8:] + out = b"" + for i in range(0, len(data), AES_BLOCK_SIZE): + ctr_block = ctr.to_bytes(8, byteorder="little") + bytes(8) + keystream = aes_ecb_encrypt(key, xor(iv, ctr_block)) + out += xor(data[i : i + AES_BLOCK_SIZE], keystream) + ctr = (ctr + 1) % 2**64 + return out + + +# AES-XTS, implemented manually since python-cryptography doesn't support +# AES-192-XTS. Includes ciphertext stealing. +def aes_xts_encrypt_manual(key, tweak, ptext): + assert len(ptext) >= AES_BLOCK_SIZE + key1, key2 = key[: len(key) // 2], key[len(key) // 2 :] + t = int.from_bytes(aes_ecb_encrypt(key2, tweak), byteorder="little") + num_full_blocks = len(ptext) // AES_BLOCK_SIZE + partial_len = len(ptext) % AES_BLOCK_SIZE + + def encrypt_block(block, t): + t_bytes = t.to_bytes(AES_BLOCK_SIZE, byteorder="little") + return xor(aes_ecb_encrypt(key1, xor(block, t_bytes)), t_bytes) + + def mul_x(t): + t <<= 1 + if t >> 128: + t = (t & (2**128 - 1)) ^ 0x87 + return t + + out = b"" + for i in range(num_full_blocks): + out += encrypt_block(ptext[i * AES_BLOCK_SIZE :][:AES_BLOCK_SIZE], t) + t = mul_x(t) + if partial_len: + cc = out[-AES_BLOCK_SIZE:] + pp = ptext[num_full_blocks * AES_BLOCK_SIZE :] + cc[partial_len:] + out = out[:-AES_BLOCK_SIZE] + encrypt_block(pp, t) + cc[:partial_len] + return out + + +def aes_xts_encrypt(key, tweak, ptext): + ctext = aes_xts_encrypt_manual(key, tweak, ptext) + # Cross-check against python-cryptography where possible. It supports + # neither AES-192-XTS nor keys whose two halves are the same. + if len(key) != 48 and key[: len(key) // 2] != key[len(key) // 2 :]: + assert ctext == aes_encrypt_with_mode(key, modes.XTS(tweak), ptext) + return ctext + + +# For each mode: (encryption function, valid key lengths, IV state size, +# minimum data length, data length alignment). These must match the values +# used by the corresponding test in lib/crypto/tests/. +MODES = { + "aes-ecb": (lambda k, iv, p: aes_ecb_encrypt(k, p), [16, 24, 32], 0, 0, 16), + "aes-cbc": (aes_cbc_encrypt, [16, 24, 32], 16, 0, 16), + "aes-cbc-cts": (aes_cbc_cts_encrypt, [16, 24, 32], 16, 16, 1), + "aes-ctr": (aes_ctr_crypt, [16, 24, 32], 16, 0, 1), + "aes-xctr": (aes_xctr_crypt, [16, 24, 32], 24, 0, 1), + "aes-xts": (aes_xts_encrypt, [32, 48, 64], 16, 16, 1), +} + + +# Deterministically generate many different inputs using exactly the same +# method that test_skcipher_monte_carlo() uses; encrypt them using an +# independent implementation of the algorithm; compute the checksum of all the +# resulting ciphertexts concatenated to each other; and print the checksum as a +# C array. +def gen_monte_carlo_checksum(alg): + encrypt, key_lens, iv_size, min_data_len, data_len_align = MODES[alg] + blake2s = hashlib.blake2s() + for data_len in range(1025): + if data_len < min_data_len or data_len % data_len_align != 0: + continue + rng = Rng(data_len) + key_len = key_lens[(data_len // data_len_align) % len(key_lens)] + key = rng.rand_bytes(key_len) + iv = rng.rand_bytes(iv_size) + ptext = rng.rand_bytes(data_len) + blake2s.update(encrypt(key, iv, ptext)) + + name = f"{alg.replace('-', '_')}_monte_carlo_checksum" + value = blake2s.digest() + print(f"static const u8 {name}[BLAKE2S_HASH_SIZE] = {{") + for i in range(0, len(value), 11): + line = "\t" + "".join(f"0x{b:02x}, " for b in value[i : i + 11]) + print(f"{line.rstrip()}") + print("};") + + +if __name__ == "__main__": + if len(sys.argv) != 2 or sys.argv[1] not in MODES: + sys.stderr.write(f"Usage: gen-skcipher-testvecs.py [{'|'.join(MODES)}]\n") + sys.exit(1) + gen_monte_carlo_checksum(sys.argv[1]) -- 2.56.1 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/7] lib/crypto: tests: Add KUnit test suite for AES-ECB 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 ` Eric Biggers 2026-11-16 6:28 ` [PATCH 3/7] lib/crypto: tests: Add KUnit test suite for AES-CBC Eric Biggers ` (5 subsequent siblings) 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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. 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_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 diff --git a/lib/crypto/.kunitconfig b/lib/crypto/.kunitconfig index 6d83badf4292..e323e955ceff 100644 --- a/lib/crypto/.kunitconfig +++ b/lib/crypto/.kunitconfig @@ -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 diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig index 5a86a02d25b7..54af41f3c07d 100644 --- a/lib/crypto/tests/Kconfig +++ b/lib/crypto/tests/Kconfig @@ -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 diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile index 40c9624eaaa8..1a5be11ce378 100644 --- a/lib/crypto/tests/Makefile +++ b/lib/crypto/tests/Makefile @@ -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 diff --git a/lib/crypto/tests/aes_ecb_kunit.c b/lib/crypto/tests/aes_ecb_kunit.c new file mode 100644 index 000000000000..b73e2c7f725a --- /dev/null +++ b/lib/crypto/tests/aes_ecb_kunit.c @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * KUnit test suite for AES-ECB + * + * Copyright 2026 Google LLC + */ +#include <crypto/aes-ecb.h> +#include <crypto/blake2s.h> +#include "sp800-38a-data.h" +#include "test-utils.h" + +static void aes_ecb_encrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv, + aes_encrypt_arg key, bool cont) +{ + aes_ecb_encrypt(dst, src, len, key); +} + +static void aes_ecb_decrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv, + const struct aes_key *key, bool cont) +{ + aes_ecb_decrypt(dst, src, len, key); +} + +static const size_t aes_ecb_valid_key_lens[] = { AES_KEYSIZE_128, + AES_KEYSIZE_192, + AES_KEYSIZE_256 }; +#define SKCIPHER_VALID_KEY_LENS aes_ecb_valid_key_lens +#define SKCIPHER_MAX_KEY_LEN AES_MAX_KEY_SIZE +#define SKCIPHER_IV_LEN 0 +#define SKCIPHER_MIN_DATA_LEN 0 +#define SKCIPHER_DATA_LEN_ALIGN AES_BLOCK_SIZE + +#define SKCIPHER_KEY aes_key +#define SKCIPHER_PREPAREKEY aes_preparekey +#define SKCIPHER_ENCRYPT aes_ecb_encrypt_test +#define SKCIPHER_DECRYPT aes_ecb_decrypt_test + +#define SKCIPHER_ENCKEY aes_enckey +#define SKCIPHER_PREPAREENCKEY aes_prepareenckey +#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_ecb_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = { + 0xf7, 0x96, 0x84, 0x87, 0xc7, 0xcb, 0x57, 0xb5, 0xb3, 0x5a, 0xbe, + 0x57, 0xf6, 0x0e, 0x90, 0x3a, 0x8e, 0x64, 0xd8, 0x7f, 0xbc, 0x3e, + 0x8f, 0x6f, 0x57, 0x32, 0xc2, 0x56, 0x73, 0x44, 0xf1, 0x9a, +}; +#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_ecb_monte_carlo_checksum + +#include "skcipher-test-template.h" + +/* AES-ECB test vectors from NIST SP 800-38A */ +static const struct skcipher_testvec aes_ecb_testvecs[] = { + { + .name = "SP 800-38A F.1.1 (ECB-AES128)", + .key = SP800_38A_KEY128, + .key_len = 16, + .ptext = SP800_38A_PTEXT, + .ctext = "\x3a\xd7\x7b\xb4\x0d\x7a\x36\x60" + "\xa8\x9e\xca\xf3\x24\x66\xef\x97" + "\xf5\xd3\xd5\x85\x03\xb9\x69\x9d" + "\xe7\x85\x89\x5a\x96\xfd\xba\xaf" + "\x43\xb1\xcd\x7f\x59\x8e\xce\x23" + "\x88\x1b\x00\xe3\xed\x03\x06\x88" + "\x7b\x0c\x78\x5e\x27\xe8\xad\x3f" + "\x82\x23\x20\x71\x04\x72\x5d\xd4", + .data_len = 64, + }, + { + .name = "SP 800-38A F.1.3 (ECB-AES192)", + .key = SP800_38A_KEY192, + .key_len = 24, + .ptext = SP800_38A_PTEXT, + .ctext = "\xbd\x33\x4f\x1d\x6e\x45\xf2\x5f" + "\xf7\x12\xa2\x14\x57\x1f\xa5\xcc" + "\x97\x41\x04\x84\x6d\x0a\xd3\xad" + "\x77\x34\xec\xb3\xec\xee\x4e\xef" + "\xef\x7a\xfd\x22\x70\xe2\xe6\x0a" + "\xdc\xe0\xba\x2f\xac\xe6\x44\x4e" + "\x9a\x4b\x41\xba\x73\x8d\x6c\x72" + "\xfb\x16\x69\x16\x03\xc1\x8e\x0e", + .data_len = 64, + }, + { + .name = "SP 800-38A F.1.5 (ECB-AES256)", + .key = SP800_38A_KEY256, + .key_len = 32, + .ptext = SP800_38A_PTEXT, + .ctext = "\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c" + "\x06\x4b\x5a\x7e\x3d\xb1\x81\xf8" + "\x59\x1c\xcb\x10\xd4\x10\xed\x26" + "\xdc\x5b\xa7\x4a\x31\x36\x28\x70" + "\xb6\xed\x21\xb9\x9c\xa6\xf4\xf9" + "\xf1\x53\xe7\xb1\xbe\xaf\xed\x1d" + "\x23\x30\x4b\x7a\x39\xf9\xf3\xff" + "\x06\x7d\x8d\x8f\x9e\x24\xec\xc7", + .data_len = 64, + }, +}; + +static void test_aes_ecb_test_vectors(struct kunit *test) +{ + skcipher_run_testvecs(test, aes_ecb_testvecs, + ARRAY_SIZE(aes_ecb_testvecs)); +} + +/* + * Test that AES-ECB en/decryption of multiple blocks gives the same results as + * en/decrypting each block individually using aes_encrypt() and aes_decrypt(). + * Also test aes_encrypt() with struct aes_enckey, and test both functions with + * unaligned buffers, variously-aligned key structs, in-place operation, and + * guarded single-block buffers and key structs. + */ +static void test_aes_ecb_vs_block_cipher(struct kunit *test) +{ + const size_t max_data_len = 1024; + const size_t max_offset = 128; + const size_t key_align = __alignof__(struct aes_key); + const size_t enckey_align = __alignof__(struct aes_enckey); + u8 raw_key[AES_MAX_KEY_SIZE]; + u8 *key_buf = alloc_buf(test, sizeof(struct aes_key) + max_offset + + key_align - 1); + u8 *enckey_buf = alloc_buf(test, sizeof(struct aes_enckey) + + max_offset + enckey_align - 1); + struct aes_key *guarded_key = + alloc_guarded_buf(test, sizeof(*guarded_key)); + struct aes_enckey *guarded_enckey = + alloc_guarded_buf(test, sizeof(*guarded_enckey)); + const u8 *pt = alloc_rand_buf(test, max_data_len); + u8 *ct = alloc_buf(test, max_data_len); + u8 *buf1 = alloc_buf(test, max_data_len + max_offset); + u8 *buf2 = alloc_buf(test, max_data_len + max_offset); + u8 *guarded_in = alloc_guarded_buf(test, AES_BLOCK_SIZE); + u8 *guarded_out = alloc_guarded_buf(test, AES_BLOCK_SIZE); + int err; + + for (int i = 0; i < 100; i++) { + const size_t key_len = skcipher_rand_key_len(); + const size_t data_len = skcipher_rand_data_len(max_data_len); + struct aes_key *key = (void *)PTR_ALIGN( + key_buf + rand_offset(max_offset), key_align); + struct aes_enckey *enckey = (void *)PTR_ALIGN( + enckey_buf + rand_offset(max_offset), enckey_align); + u8 *p1 = &buf1[rand_offset(max_offset)]; + u8 *p2 = &buf2[rand_offset(max_offset)]; + + rand_bytes(raw_key, key_len); + err = aes_preparekey(key, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + err = aes_prepareenckey(enckey, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + + aes_ecb_encrypt(ct, pt, data_len, key); + + /* Out-of-place aes_encrypt() and aes_decrypt() (unaligned) */ + memcpy(p1, pt, data_len); + for (size_t j = 0; j < data_len; j += AES_BLOCK_SIZE) + aes_encrypt(key, &p2[j], &p1[j]); + KUNIT_ASSERT_MEMEQ_MSG( + test, ct, p2, data_len, + "AES-ECB encryption differs from aes_encrypt(); data_len=%zu", + data_len); + + memset(p1, 0, data_len); + for (size_t j = 0; j < data_len; j += AES_BLOCK_SIZE) + aes_decrypt(key, &p1[j], &p2[j]); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, p1, data_len, + "aes_decrypt() didn't invert encryption; data_len=%zu", + data_len); + + /* In-place aes_encrypt() (with aes_enckey) and aes_decrypt() */ + for (size_t j = 0; j < data_len; j += AES_BLOCK_SIZE) + aes_encrypt(enckey, &p1[j], &p1[j]); + KUNIT_ASSERT_MEMEQ_MSG( + test, ct, p1, data_len, + "In-place aes_encrypt() with aes_enckey differs; data_len=%zu", + data_len); + for (size_t j = 0; j < data_len; j += AES_BLOCK_SIZE) + aes_decrypt(key, &p1[j], &p1[j]); + KUNIT_ASSERT_MEMEQ_MSG( + test, pt, p1, data_len, + "In-place aes_decrypt() didn't invert encryption; data_len=%zu", + data_len); + + /* Guarded single-block buffers and guarded key structs */ + if (data_len >= AES_BLOCK_SIZE) { + err = aes_preparekey(guarded_key, raw_key, key_len); + KUNIT_ASSERT_EQ(test, 0, err); + err = aes_prepareenckey(guarded_enckey, raw_key, + key_len); + KUNIT_ASSERT_EQ(test, 0, err); + + memcpy(guarded_in, pt, AES_BLOCK_SIZE); + memset(guarded_out, 0, AES_BLOCK_SIZE); + aes_encrypt(guarded_key, guarded_out, guarded_in); + KUNIT_ASSERT_MEMEQ(test, ct, guarded_out, + AES_BLOCK_SIZE); + memset(guarded_out, 0, AES_BLOCK_SIZE); + aes_encrypt(guarded_enckey, guarded_out, guarded_in); + KUNIT_ASSERT_MEMEQ(test, ct, guarded_out, + AES_BLOCK_SIZE); + memcpy(guarded_in, ct, AES_BLOCK_SIZE); + memset(guarded_out, 0, AES_BLOCK_SIZE); + aes_decrypt(guarded_key, guarded_out, guarded_in); + KUNIT_ASSERT_MEMEQ(test, pt, guarded_out, + AES_BLOCK_SIZE); + } + } +} + +static struct kunit_case aes_ecb_test_cases[] = { + KUNIT_CASE(test_aes_ecb_test_vectors), + KUNIT_CASE(test_aes_ecb_vs_block_cipher), + SKCIPHER_KUNIT_CASES, + {}, +}; + +static struct kunit_suite aes_ecb_test_suite = { + .name = "aes_ecb", + .test_cases = aes_ecb_test_cases, +}; +kunit_test_suite(aes_ecb_test_suite); + +MODULE_DESCRIPTION("KUnit tests and benchmark for AES-ECB"); +MODULE_LICENSE("GPL"); diff --git a/lib/crypto/tests/sp800-38a-data.h b/lib/crypto/tests/sp800-38a-data.h new file mode 100644 index 000000000000..73517f1fef91 --- /dev/null +++ b/lib/crypto/tests/sp800-38a-data.h @@ -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 */ -- 2.56.1 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/7] lib/crypto: tests: Add KUnit test suite for AES-CBC 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 ` Eric Biggers 2026-11-16 6:28 ` [PATCH 4/7] lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS Eric Biggers ` (4 subsequent siblings) 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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. 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_cbc_kunit.c | 137 +++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 lib/crypto/tests/aes_cbc_kunit.c diff --git a/lib/crypto/.kunitconfig b/lib/crypto/.kunitconfig index e323e955ceff..c3ac80ccb695 100644 --- a/lib/crypto/.kunitconfig +++ b/lib/crypto/.kunitconfig @@ -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 diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig index 54af41f3c07d..7076ea606ce8 100644 --- a/lib/crypto/tests/Kconfig +++ b/lib/crypto/tests/Kconfig @@ -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 diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile index 1a5be11ce378..e7b7efee663a 100644 --- a/lib/crypto/tests/Makefile +++ b/lib/crypto/tests/Makefile @@ -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 diff --git a/lib/crypto/tests/aes_cbc_kunit.c b/lib/crypto/tests/aes_cbc_kunit.c new file mode 100644 index 000000000000..775a67b48359 --- /dev/null +++ b/lib/crypto/tests/aes_cbc_kunit.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * KUnit test suite for AES-CBC + * + * Copyright 2026 Google LLC + */ +#include <crypto/aes-cbc.h> +#include <crypto/blake2s.h> +#include "sp800-38a-data.h" +#include "test-utils.h" + +static void aes_cbc_encrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv, + aes_encrypt_arg key, bool cont) +{ + aes_cbc_encrypt(dst, src, len, iv, key); +} + +static void aes_cbc_decrypt_test(u8 *dst, const u8 *src, size_t len, u8 *iv, + const struct aes_key *key, bool cont) +{ + aes_cbc_decrypt(dst, src, len, iv, key); +} + +static const size_t aes_cbc_valid_key_lens[] = { AES_KEYSIZE_128, + AES_KEYSIZE_192, + AES_KEYSIZE_256 }; +#define SKCIPHER_VALID_KEY_LENS aes_cbc_valid_key_lens +#define SKCIPHER_MAX_KEY_LEN AES_MAX_KEY_SIZE +#define SKCIPHER_IV_LEN AES_BLOCK_SIZE +#define SKCIPHER_MIN_DATA_LEN 0 +#define SKCIPHER_DATA_LEN_ALIGN AES_BLOCK_SIZE + +#define SKCIPHER_KEY aes_key +#define SKCIPHER_PREPAREKEY aes_preparekey +#define SKCIPHER_ENCRYPT aes_cbc_encrypt_test +#define SKCIPHER_DECRYPT aes_cbc_decrypt_test + +#define SKCIPHER_ENCKEY aes_enckey +#define SKCIPHER_PREPAREENCKEY aes_prepareenckey +#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_cbc_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = { + 0x87, 0xfd, 0xb4, 0x92, 0xde, 0x3e, 0x69, 0xac, 0xe2, 0x78, 0xdf, + 0x23, 0xbe, 0x6f, 0x2f, 0xcc, 0x32, 0x98, 0x10, 0x89, 0x63, 0x64, + 0x1c, 0xff, 0x76, 0xe5, 0x4f, 0x31, 0x90, 0xf3, 0x2c, 0x7a, +}; +#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_cbc_monte_carlo_checksum + +#include "skcipher-test-template.h" + +#define SP800_38A_CBC_IV \ + "\x00\x01\x02\x03\x04\x05\x06\x07" \ + "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + +/* AES-CBC test vectors from NIST SP 800-38A */ +static const struct skcipher_testvec aes_cbc_testvecs[] = { + { + .name = "SP 800-38A F.2.1 (CBC-AES128)", + .key = SP800_38A_KEY128, + .key_len = 16, + .iv = SP800_38A_CBC_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\x76\x49\xab\xac\x81\x19\xb2\x46" + "\xce\xe9\x8e\x9b\x12\xe9\x19\x7d" + "\x50\x86\xcb\x9b\x50\x72\x19\xee" + "\x95\xdb\x11\x3a\x91\x76\x78\xb2" + "\x73\xbe\xd6\xb8\xe3\xc1\x74\x3b" + "\x71\x16\xe6\x9e\x22\x22\x95\x16" + "\x3f\xf1\xca\xa1\x68\x1f\xac\x09" + "\x12\x0e\xca\x30\x75\x86\xe1\xa7", + .data_len = 64, + /* The last ciphertext block */ + .next_iv = "\x3f\xf1\xca\xa1\x68\x1f\xac\x09" + "\x12\x0e\xca\x30\x75\x86\xe1\xa7", + }, + { + .name = "SP 800-38A F.2.3 (CBC-AES192)", + .key = SP800_38A_KEY192, + .key_len = 24, + .iv = SP800_38A_CBC_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\x4f\x02\x1d\xb2\x43\xbc\x63\x3d" + "\x71\x78\x18\x3a\x9f\xa0\x71\xe8" + "\xb4\xd9\xad\xa9\xad\x7d\xed\xf4" + "\xe5\xe7\x38\x76\x3f\x69\x14\x5a" + "\x57\x1b\x24\x20\x12\xfb\x7a\xe0" + "\x7f\xa9\xba\xac\x3d\xf1\x02\xe0" + "\x08\xb0\xe2\x79\x88\x59\x88\x81" + "\xd9\x20\xa9\xe6\x4f\x56\x15\xcd", + .data_len = 64, + /* The last ciphertext block */ + .next_iv = "\x08\xb0\xe2\x79\x88\x59\x88\x81" + "\xd9\x20\xa9\xe6\x4f\x56\x15\xcd", + }, + { + .name = "SP 800-38A F.2.5 (CBC-AES256)", + .key = SP800_38A_KEY256, + .key_len = 32, + .iv = SP800_38A_CBC_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\xf5\x8c\x4c\x04\xd6\xe5\xf1\xba" + "\x77\x9e\xab\xfb\x5f\x7b\xfb\xd6" + "\x9c\xfc\x4e\x96\x7e\xdb\x80\x8d" + "\x67\x9f\x77\x7b\xc6\x70\x2c\x7d" + "\x39\xf2\x33\x69\xa9\xd9\xba\xcf" + "\xa5\x30\xe2\x63\x04\x23\x14\x61" + "\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc" + "\xda\x6c\x19\x07\x8c\x6a\x9d\x1b", + .data_len = 64, + /* The last ciphertext block */ + .next_iv = "\xb2\xeb\x05\xe2\xc3\x9b\xe9\xfc" + "\xda\x6c\x19\x07\x8c\x6a\x9d\x1b", + }, +}; + +static void test_aes_cbc_test_vectors(struct kunit *test) +{ + skcipher_run_testvecs(test, aes_cbc_testvecs, + ARRAY_SIZE(aes_cbc_testvecs)); +} + +static struct kunit_case aes_cbc_test_cases[] = { + KUNIT_CASE(test_aes_cbc_test_vectors), + SKCIPHER_KUNIT_CASES, + {}, +}; + +static struct kunit_suite aes_cbc_test_suite = { + .name = "aes_cbc", + .test_cases = aes_cbc_test_cases, +}; +kunit_test_suite(aes_cbc_test_suite); + +MODULE_DESCRIPTION("KUnit tests and benchmark for AES-CBC"); +MODULE_LICENSE("GPL"); -- 2.56.1 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 4/7] lib/crypto: tests: Add KUnit test suite for AES-CBC-CTS 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers ` (2 preceding siblings ...) 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 2026-11-16 6:28 ` [PATCH 5/7] lib/crypto: tests: Add KUnit test suite for AES-CTR Eric Biggers ` (3 subsequent siblings) 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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(). 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 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5/7] lib/crypto: tests: Add KUnit test suite for AES-CTR 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers ` (3 preceding siblings ...) 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 ` Eric Biggers 2026-11-16 6:28 ` [PATCH 6/7] lib/crypto: tests: Add KUnit test suite for AES-XCTR Eric Biggers ` (2 subsequent siblings) 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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(). 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_ctr_kunit.c | 196 +++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 lib/crypto/tests/aes_ctr_kunit.c diff --git a/lib/crypto/.kunitconfig b/lib/crypto/.kunitconfig index c3ac80ccb695..465375a1a14c 100644 --- a/lib/crypto/.kunitconfig +++ b/lib/crypto/.kunitconfig @@ -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 diff --git a/lib/crypto/tests/Kconfig b/lib/crypto/tests/Kconfig index b0b0ae84a02e..0d13ce05f2de 100644 --- a/lib/crypto/tests/Kconfig +++ b/lib/crypto/tests/Kconfig @@ -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 diff --git a/lib/crypto/tests/Makefile b/lib/crypto/tests/Makefile index fa22a4138a68..1deb07c2558f 100644 --- a/lib/crypto/tests/Makefile +++ b/lib/crypto/tests/Makefile @@ -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 diff --git a/lib/crypto/tests/aes_ctr_kunit.c b/lib/crypto/tests/aes_ctr_kunit.c new file mode 100644 index 000000000000..344fc9e9571d --- /dev/null +++ b/lib/crypto/tests/aes_ctr_kunit.c @@ -0,0 +1,196 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * KUnit test suite for AES-CTR + * + * Copyright 2026 Google LLC + */ +#include <crypto/aes-ctr.h> +#include <crypto/blake2s.h> +#include "sp800-38a-data.h" +#include "test-utils.h" + +static void aes_ctr_test(u8 *dst, const u8 *src, size_t len, u8 *ctr, + aes_encrypt_arg key, bool cont) +{ + aes_ctr(dst, src, len, ctr, key); +} + +static const size_t aes_ctr_valid_key_lens[] = { AES_KEYSIZE_128, + AES_KEYSIZE_192, + AES_KEYSIZE_256 }; +#define SKCIPHER_VALID_KEY_LENS aes_ctr_valid_key_lens +#define SKCIPHER_MAX_KEY_LEN AES_MAX_KEY_SIZE +#define SKCIPHER_IV_LEN AES_BLOCK_SIZE /* the 128-bit big endian counter */ +#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_ctr_test +#define SKCIPHER_DECRYPT aes_ctr_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_ctr_monte_carlo_checksum[BLAKE2S_HASH_SIZE] = { + 0x1e, 0x14, 0xcd, 0x03, 0x44, 0x7d, 0x85, 0xa3, 0x4b, 0xdc, 0x97, + 0xd4, 0xb8, 0x0d, 0x06, 0xd8, 0x53, 0x66, 0x75, 0xde, 0x62, 0xd5, + 0x4e, 0x01, 0xc8, 0xf0, 0x1b, 0xb6, 0xbd, 0xc6, 0xd8, 0x9e, +}; +#define SKCIPHER_MONTE_CARLO_CHECKSUM aes_ctr_monte_carlo_checksum + +#include "skcipher-test-template.h" + +#define SP800_38A_CTR_IV \ + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7" \ + "\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" + +/* The initial counter plus 4, i.e. the counter after 64 bytes */ +#define SP800_38A_CTR_NEXT_IV \ + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7" \ + "\xf8\xf9\xfa\xfb\xfc\xfd\xff\x03" + +/* AES-CTR test vectors from NIST SP 800-38A */ +static const struct skcipher_testvec aes_ctr_testvecs[] = { + { + .name = "SP 800-38A F.5.1 (CTR-AES128)", + .key = SP800_38A_KEY128, + .key_len = 16, + .iv = SP800_38A_CTR_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\x87\x4d\x61\x91\xb6\x20\xe3\x26" + "\x1b\xef\x68\x64\x99\x0d\xb6\xce" + "\x98\x06\xf6\x6b\x79\x70\xfd\xff" + "\x86\x17\x18\x7b\xb9\xff\xfd\xff" + "\x5a\xe4\xdf\x3e\xdb\xd5\xd3\x5e" + "\x5b\x4f\x09\x02\x0d\xb0\x3e\xab" + "\x1e\x03\x1d\xda\x2f\xbe\x03\xd1" + "\x79\x21\x70\xa0\xf3\x00\x9c\xee", + .data_len = 64, + .next_iv = SP800_38A_CTR_NEXT_IV, + }, + { + .name = "SP 800-38A F.5.3 (CTR-AES192)", + .key = SP800_38A_KEY192, + .key_len = 24, + .iv = SP800_38A_CTR_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\x1a\xbc\x93\x24\x17\x52\x1c\xa2" + "\x4f\x2b\x04\x59\xfe\x7e\x6e\x0b" + "\x09\x03\x39\xec\x0a\xa6\xfa\xef" + "\xd5\xcc\xc2\xc6\xf4\xce\x8e\x94" + "\x1e\x36\xb2\x6b\xd1\xeb\xc6\x70" + "\xd1\xbd\x1d\x66\x56\x20\xab\xf7" + "\x4f\x78\xa7\xf6\xd2\x98\x09\x58" + "\x5a\x97\xda\xec\x58\xc6\xb0\x50", + .data_len = 64, + .next_iv = SP800_38A_CTR_NEXT_IV, + }, + { + .name = "SP 800-38A F.5.5 (CTR-AES256)", + .key = SP800_38A_KEY256, + .key_len = 32, + .iv = SP800_38A_CTR_IV, + .ptext = SP800_38A_PTEXT, + .ctext = "\x60\x1e\xc3\x13\x77\x57\x89\xa5" + "\xb7\xa7\xf5\x04\xbb\xf3\xd2\x28" + "\xf4\x43\xe3\xca\x4d\x62\xb5\x9a" + "\xca\x84\xe9\x90\xca\xca\xf5\xc5" + "\x2b\x09\x30\xda\xa2\x3d\xe9\x4c" + "\xe8\x70\x17\xba\x2d\x84\x98\x8d" + "\xdf\xc9\xc5\x8d\xb6\x7a\xad\xa6" + "\x13\xc2\xdd\x08\x45\x79\x41\xa6", + .data_len = 64, + .next_iv = SP800_38A_CTR_NEXT_IV, + }, +}; + +static void test_aes_ctr_test_vectors(struct kunit *test) +{ + skcipher_run_testvecs(test, aes_ctr_testvecs, + ARRAY_SIZE(aes_ctr_testvecs)); +} + +/* Reference implementation of AES-CTR, built on top of aes_encrypt() */ +static void aes_ctr_ref(u8 *dst, const u8 *src, size_t len, + u8 ctr[AES_BLOCK_SIZE], const struct aes_key *key) +{ + u8 keystream[AES_BLOCK_SIZE]; + + for (size_t i = 0; i < len; i += AES_BLOCK_SIZE) { + aes_encrypt(key, keystream, ctr); + for (size_t j = i; j < min(len, i + AES_BLOCK_SIZE); j++) + dst[j] = src[j] ^ keystream[j - i]; + /* Increment the 128-bit big endian counter. */ + for (int k = AES_BLOCK_SIZE - 1; k >= 0 && ++ctr[k] == 0; k--) + ; + } +} + +/* + * Test that AES-CTR correctly handles the counter crossing an 8-bit, 32-bit, + * 64-bit, or 128-bit boundary, which some optimized implementations handle + * specially. The counter starts 1 to 64 blocks before the boundary. Compare + * against a reference implementation, and also verify that the counter is + * advanced by the number of blocks (including any partial block) each time. + */ +static void test_aes_ctr_counter_overflow(struct kunit *test) +{ + static const int carry_bits[] = { 8, 32, 64, 128 }; + const size_t max_data_len = 2048; + const struct skcipher_test_keys *keys = skcipher_alloc_test_keys(test); + const u8 *pt = alloc_rand_buf(test, max_data_len); + u8 *ct = alloc_buf(test, max_data_len); + u8 *ref_ct = alloc_buf(test, max_data_len); + u8 orig_ctr[AES_BLOCK_SIZE]; + u8 ctr[AES_BLOCK_SIZE]; + u8 ref_ctr[AES_BLOCK_SIZE]; + + for (int i = 0; i < 400; i++) { + const struct aes_key *key = + skcipher_pick_key(keys, i / ARRAY_SIZE(carry_bits)); + const int bits = carry_bits[i % ARRAY_SIZE(carry_bits)]; + const size_t data_len = rand_length(max_data_len); + size_t num_parts; + + /* Set the counter's low 'bits' bits to 2^bits - (1..64). */ + rand_bytes(orig_ctr, AES_BLOCK_SIZE); + memset(&orig_ctr[AES_BLOCK_SIZE - bits / 8], 0xff, bits / 8); + orig_ctr[AES_BLOCK_SIZE - 1] -= rand32() % 64; + + memcpy(ref_ctr, orig_ctr, AES_BLOCK_SIZE); + aes_ctr_ref(ref_ct, pt, data_len, ref_ctr, key); + + memcpy(ctr, orig_ctr, AES_BLOCK_SIZE); + num_parts = skcipher_crypt_incrementally(ct, pt, data_len, ctr, + key, true); + KUNIT_ASSERT_MEMEQ_MSG( + test, ref_ct, ct, data_len, + "Wrong ciphertext; data_len=%zu carry_bits=%d num_parts=%zu", + data_len, bits, num_parts); + KUNIT_ASSERT_MEMEQ_MSG( + test, ref_ctr, ctr, AES_BLOCK_SIZE, + "Wrong next counter; data_len=%zu carry_bits=%d num_parts=%zu", + data_len, bits, num_parts); + } +} + +static struct kunit_case aes_ctr_test_cases[] = { + KUNIT_CASE(test_aes_ctr_test_vectors), + KUNIT_CASE(test_aes_ctr_counter_overflow), + SKCIPHER_KUNIT_CASES, + {}, +}; + +static struct kunit_suite aes_ctr_test_suite = { + .name = "aes_ctr", + .test_cases = aes_ctr_test_cases, +}; +kunit_test_suite(aes_ctr_test_suite); + +MODULE_DESCRIPTION("KUnit tests and benchmark for AES-CTR"); +MODULE_LICENSE("GPL"); -- 2.56.1 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 6/7] lib/crypto: tests: Add KUnit test suite for AES-XCTR 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers ` (4 preceding siblings ...) 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 2026-11-16 6:28 ` [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS Eric Biggers 2026-11-21 19:53 ` [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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. 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 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers ` (5 preceding siblings ...) 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 2026-11-21 19:53 ` [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-16 6:28 UTC (permalink / raw) To: linux-crypto Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, 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). 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 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes 2026-11-16 6:28 [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers ` (6 preceding siblings ...) 2026-11-16 6:28 ` [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS Eric Biggers @ 2026-11-21 19:53 ` Eric Biggers 7 siblings, 0 replies; 9+ messages in thread From: Eric Biggers @ 2026-11-21 19:53 UTC (permalink / raw) To: linux-crypto; +Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu On Sun, Nov 15, 2026 at 10:28:42PM -0800, Eric Biggers wrote: > 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 Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next - Eric ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-21 19:53 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 ` [PATCH 7/7] lib/crypto: tests: Add KUnit test suite for AES-XTS Eric Biggers 2026-11-21 19:53 ` [PATCH 0/7] lib/crypto: KUnit tests for unauthenticated AES modes Eric Biggers
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.