lib/crypto: KUnit tests for AES-CCM and AES-GCM
Subsystems (2)
Eric Biggers
maintainerv2 cover lib/crypto: KUnit tests for AES-CCM and AES-GCM
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 gcm-ccm-kunit-v2
This series adds comprehensive KUnit test suites for the AES-CCM and AES-GCM library APIs.
This is a follow-up to the series
"[PATCH v2 00/13] Library APIs for AES encryption modes"
(https://lore.kernel.org/linux-crypto/[email protected]/)
Changed in v2:
- Fixed crash in test_aes_ccm_data_len_too_large() on 32-bit kernels.
Eric Biggers (5):
lib/crypto: tests: Create test-utils.h lib/crypto: tests: Use per-test-case buffers in hash tests lib/crypto: tests: Add aead-test-template.h lib/crypto: tests: Add KUnit test suite for AES-CCM lib/crypto: tests: Add KUnit test suite for AES-GCM
lib/crypto/.kunitconfig | 2 + lib/crypto/tests/Kconfig | 18 + lib/crypto/tests/Makefile | 2 + lib/crypto/tests/aead-test-template.h | 1039 +++++++++++++++++++++ lib/crypto/tests/aes_cbc_macs_kunit.c | 12 +- lib/crypto/tests/aes_ccm_kunit.c | 356 +++++++ lib/crypto/tests/aes_gcm_kunit.c | 472 ++++++++++ lib/crypto/tests/blake2b_kunit.c | 33 +- lib/crypto/tests/blake2s_kunit.c | 33 +- lib/crypto/tests/chacha20poly1305_kunit.c | 9 +- lib/crypto/tests/ghash_kunit.c | 37 +- lib/crypto/tests/hash-test-template.h | 249 ++--- lib/crypto/tests/md5_kunit.c | 2 - lib/crypto/tests/mldsa_kunit.c | 19 +- lib/crypto/tests/nh_kunit.c | 5 +- lib/crypto/tests/poly1305_kunit.c | 21 +- lib/crypto/tests/polyval_kunit.c | 38 +- lib/crypto/tests/sha1_kunit.c | 2 - lib/crypto/tests/sha224_kunit.c | 2 - lib/crypto/tests/sha256_kunit.c | 48 +- lib/crypto/tests/sha384_kunit.c | 2 - lib/crypto/tests/sha3_kunit.c | 30 +- lib/crypto/tests/sha512_kunit.c | 2 - lib/crypto/tests/sm3_kunit.c | 2 - lib/crypto/tests/test-utils.h | 111 +++ scripts/crypto/gen-aead-testvecs.py | 69 ++ 26 files changed, 2275 insertions(+), 340 deletions(-) create mode 100644 lib/crypto/tests/aead-test-template.h create mode 100644 lib/crypto/tests/aes_ccm_kunit.c create mode 100644 lib/crypto/tests/aes_gcm_kunit.c create mode 100644 lib/crypto/tests/test-utils.h create mode 100755 scripts/crypto/gen-aead-testvecs.py
base-commit: 6d22ec26295c1412d765e3d687e46224fc332928
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 1/5 lib/crypto: tests: Create test-utils.h
Move hash-test-template's test RNG code and the buffer allocation code from various test suites into a header test-utils.h. This allows them to be shared by other test suites.
--- lib/crypto/tests/chacha20poly1305_kunit.c | 9 +- lib/crypto/tests/hash-test-template.h | 58 +---------- lib/crypto/tests/mldsa_kunit.c | 19 ++-- lib/crypto/tests/nh_kunit.c | 5 +- lib/crypto/tests/sha256_kunit.c | 21 +--- lib/crypto/tests/test-utils.h | 111 ++++++++++++++++++++++ 6 files changed, 124 insertions(+), 99 deletions(-) create mode 100644 lib/crypto/tests/test-utils.h
lib/crypto/tests/chacha20poly1305_kunit.c +3 -6
@@ -11,7 +11,7 @@#include <linux/init.h> #include <linux/mm.h> #include <linux/kernel.h> -#include <linux/slab.h> +#include "test-utils.h" struct chacha20poly1305_testvec {const u8 *input, *output, *assoc, *nonce, *key;
@@ -8890,11 +8890,8 @@ static void test_chacha20poly1305(struct kunit *test)bool ret;struct scatterlist sg_src[3 ]; - computed_output =kunit_kmalloc (test,MAXIMUM_TEST_BUFFER_LEN , -GFP_KERNEL ); - input =kunit_kmalloc (test,MAXIMUM_TEST_BUFFER_LEN ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_NULL (test, computed_output); -KUNIT_ASSERT_NOT_NULL (test, input); + computed_output =alloc_buf (test,MAXIMUM_TEST_BUFFER_LEN ); + input =alloc_buf (test,MAXIMUM_TEST_BUFFER_LEN );for (i =0 ; i <ARRAY_SIZE (chacha20poly1305_enc_vectors); ++i) {memset (computed_output,0 ,MAXIMUM_TEST_BUFFER_LEN );
lib/crypto/tests/hash-test-template.h +1 -57
70 lines not on this page · raw message
lib/crypto/tests/mldsa_kunit.c +6 -13
@@ -8,6 +8,7 @@#include <kunit/test.h> #include <linux/random.h> #include <linux/unaligned.h> +#include "test-utils.h" #define Q 8380417/* The prime q = 2^23 - 2^13 + 1 */
@@ -60,14 +61,6 @@ static void do_mldsa_and_assert_success(struct kunit *test,KUNIT_ASSERT_EQ (test, err,0 ); } -static u8 *kunit_kmemdup_or_fail (struct kunit *test,const u8 *src,size_t len) -{ -u8 *dst =kunit_kmalloc (test, len,GFP_KERNEL ); - -KUNIT_ASSERT_NOT_NULL (test, dst); -return memcpy (dst, src, len); -} -/* * Test that changing coefficients in a valid signature's z vector results in * the following behavior from mldsa_verify():
@@ -83,7 +76,7 @@ static u8 *kunit_kmemdup_or_fail(struct kunit *test, const u8 *src, size_t len)static void test_mldsa_z_range (struct kunit *test,const struct mldsa_testvector *tv) { -u8 *sig =kunit_kmemdup_or_fail (test, tv->sig, tv->sig_len); +u8 *sig =memdup_buf (test, tv->sig, tv->sig_len);const int lambda = params[tv->alg].lambda;const s32 gamma1 = params[tv->alg].gamma1;const int beta = params[tv->alg].beta;
@@ -146,7 +139,7 @@ static void test_mldsa_bad_hints(struct kunit *test, {const int omega = params[tv->alg].omega;const int k = params[tv->alg].k; -u8 *sig =kunit_kmemdup_or_fail (test, tv->sig, tv->sig_len); +u8 *sig =memdup_buf (test, tv->sig, tv->sig_len);/* Pointer to the encoded hint vector in the signature */ u8 *hintvec = &sig[tv->sig_len - omega - k];u8 h;
@@ -202,9 +195,9 @@ static void test_mldsa_mutation(struct kunit *test,const int msg_len = tv->msg_len;const int pk_len = tv->pk_len;const int num_iter =200 ; -u8 *sig =kunit_kmemdup_or_fail (test, tv->sig, sig_len); -u8 *msg =kunit_kmemdup_or_fail (test, tv->msg, msg_len); -u8 *pk =kunit_kmemdup_or_fail (test, tv->pk, pk_len); +u8 *sig =memdup_buf (test, tv->sig, sig_len); +u8 *msg =memdup_buf (test, tv->msg, msg_len); +u8 *pk =memdup_buf (test, tv->pk, pk_len);/* Initially the signature is valid. */ do_mldsa_and_assert_success (test, tv);
lib/crypto/tests/nh_kunit.c +2 -3
@@ -5,14 +5,13 @@#include <crypto/nh.h> #include <kunit/test.h> #include "nh-testvecs.h" +#include "test-utils.h" static void test_nh (struct kunit *test) { -u32 *key =kunit_kmalloc (test,NH_KEY_BYTES ,GFP_KERNEL ); +u32 *key =memdup_buf (test, nh_test_key,NH_KEY_BYTES );__le64 hash[NH_NUM_PASSES ]; -KUNIT_ASSERT_NOT_NULL (test, key); -memcpy (key, nh_test_key,NH_KEY_BYTES );le32_to_cpu_array (key,NH_KEY_WORDS );nh (key, nh_test_msg,16 , hash);
lib/crypto/tests/sha256_kunit.c +1 -20
@@ -4,6 +4,7 @@*/ #include <crypto/sha2.h> #include "sha256-testvecs.h" +#include "test-utils.h" /* Generate the HASH_KUNIT_CASES using hash-test-template.h. */ #define HASH sha256
@@ -22,26 +23,6 @@#define HMAC_USINGRAWKEY hmac_sha256_usingrawkey#include "hash-test-template.h" -static void free_guarded_buf (void *buf) -{ -vfree (buf); -} - -/* -* Allocate a KUnit-managed buffer that has length @len bytes immediately -* followed by an unmapped page, and assert that the allocation succeeds. -*/ -static void *alloc_guarded_buf (struct kunit *test,size_t len) -{ -size_t full_len =round_up (len,PAGE_SIZE ); -void *buf =vmalloc (full_len); - -KUNIT_ASSERT_NOT_NULL (test, buf); -KUNIT_ASSERT_EQ (test,0 , -kunit_add_action_or_reset (test, free_guarded_buf, buf)); -return buf + full_len - len; -} -/* * Test for sha256_finup_2x(). Specifically, choose various data lengths and * salt lengths, and for each one, verify that sha256_finup_2x() produces the
lib/crypto/tests/test-utils.h new file +111 -0
111 lines not on this page · raw message
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 2/5 lib/crypto: tests: Use per-test-case buffers in hash tests
In reply to Eric BiggersMake the test cases more self-contained by replacing the test suite scoped guarded buffer ('test_buf') with buffers allocated by each test case using the helper functions in test-utils.h.
--- lib/crypto/tests/aes_cbc_macs_kunit.c | 12 +- lib/crypto/tests/blake2b_kunit.c | 33 ++--- lib/crypto/tests/blake2s_kunit.c | 33 ++--- lib/crypto/tests/ghash_kunit.c | 37 +++-- lib/crypto/tests/hash-test-template.h | 191 +++++++++++++------------- lib/crypto/tests/md5_kunit.c | 2 - lib/crypto/tests/poly1305_kunit.c | 21 ++- lib/crypto/tests/polyval_kunit.c | 38 +++-- lib/crypto/tests/sha1_kunit.c | 2 - lib/crypto/tests/sha224_kunit.c | 2 - lib/crypto/tests/sha256_kunit.c | 27 ++-- lib/crypto/tests/sha384_kunit.c | 2 - lib/crypto/tests/sha3_kunit.c | 30 ++-- lib/crypto/tests/sha512_kunit.c | 2 - lib/crypto/tests/sm3_kunit.c | 2 - 15 files changed, 193 insertions(+), 241 deletions(-)
lib/crypto/tests/aes_cbc_macs_kunit.c +1 -11
@@ -34,18 +34,9 @@ static void aes_cmac_withtestkey(const u8 *data, size_t data_len,static int aes_cbc_macs_suite_init (struct kunit_suite *suite) {u8 raw_key[AES_KEYSIZE_256 ]; -int err;rand_bytes_seeded_from_len (raw_key,sizeof (raw_key)); - err =aes_cmac_preparekey (&test_key, raw_key,sizeof (raw_key)); -if (err) -return err; -return hash_suite_init (suite); -} - -static void aes_cbc_macs_suite_exit (struct kunit_suite *suite) -{ -hash_suite_exit (suite); +return aes_cmac_preparekey (&test_key, raw_key,sizeof (raw_key)); }/* Verify compatibility of the AES-CMAC implementation with RFC 4493. */
@@ -218,7 +209,6 @@ static struct kunit_suite aes_cbc_macs_test_suite = { .name = "aes_cbc_macs", .test_cases = aes_cbc_macs_test_cases, .suite_init = aes_cbc_macs_suite_init, - .suite_exit = aes_cbc_macs_suite_exit, };kunit_test_suite (aes_cbc_macs_test_suite);
lib/crypto/tests/blake2b_kunit.c +17 -16
@@ -41,9 +41,9 @@ static void blake2b_init_default(struct blake2b_ctx *ctx)static void test_blake2b_all_key_and_hash_lens (struct kunit *test) {const size_t data_len =100 ; -u8 *data = &test_buf[0 ]; -u8 *key = data + data_len; -u8 *hash = key +BLAKE2B_KEY_SIZE ; +u8 *data =alloc_buf (test, data_len); +u8 *key =alloc_buf (test,BLAKE2B_KEY_SIZE ); +u8 *hash =alloc_buf (test,BLAKE2B_HASH_SIZE );struct blake2b_ctx main_ctx;u8 main_hash[BLAKE2B_HASH_SIZE ];
@@ -68,11 +68,13 @@ static void test_blake2b_all_key_and_hash_lens(struct kunit *test)static void test_blake2b_with_guarded_key_buf (struct kunit *test) {const size_t data_len =100 ; +u8 *data =alloc_buf (test, data_len); +u8 *guarded_key_buf =alloc_guarded_buf (test,BLAKE2B_KEY_SIZE ); -rand_bytes (test_buf, data_len); +rand_bytes (data, data_len);for (int key_len =0 ; key_len <=BLAKE2B_KEY_SIZE ; key_len++) {u8 key[BLAKE2B_KEY_SIZE ]; -u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len]; +u8 *guarded_key = &guarded_key_buf[BLAKE2B_KEY_SIZE - key_len];u8 hash1[BLAKE2B_HASH_SIZE ];u8 hash2[BLAKE2B_HASH_SIZE ];struct blake2b_ctx ctx;
@@ -80,14 +82,13 @@ static void test_blake2b_with_guarded_key_buf(struct kunit *test)rand_bytes (key, key_len);memcpy (guarded_key, key, key_len); -blake2b (key, key_len, test_buf, data_len, - hash1,BLAKE2B_HASH_SIZE ); -blake2b (guarded_key, key_len, test_buf, data_len, - hash2,BLAKE2B_HASH_SIZE ); +blake2b (key, key_len, data, data_len, hash1,BLAKE2B_HASH_SIZE ); +blake2b (guarded_key, key_len, data, data_len, hash2, +BLAKE2B_HASH_SIZE );KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2B_HASH_SIZE );blake2b_init_key (&ctx,BLAKE2B_HASH_SIZE , guarded_key, key_len); -blake2b_update (&ctx, test_buf, data_len); +blake2b_update (&ctx, data, data_len);blake2b_final (&ctx, hash2);KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2B_HASH_SIZE ); }
@@ -100,14 +101,16 @@ static void test_blake2b_with_guarded_key_buf(struct kunit *test)static void test_blake2b_with_guarded_out_buf (struct kunit *test) {const size_t data_len =100 ; +u8 *data =alloc_buf (test, data_len); +u8 *out_buf =alloc_guarded_buf (test,BLAKE2B_HASH_SIZE ); -rand_bytes (test_buf, data_len); +rand_bytes (data, data_len);for (int out_len =1 ; out_len <=BLAKE2B_HASH_SIZE ; out_len++) {u8 hash[BLAKE2B_HASH_SIZE ]; -u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; +u8 *guarded_hash = &out_buf[BLAKE2B_HASH_SIZE - out_len]; -blake2b (NULL ,0 , test_buf, data_len, hash, out_len); -blake2b (NULL ,0 , test_buf, data_len, guarded_hash, out_len); +blake2b (NULL ,0 , data, data_len, hash, out_len); +blake2b (NULL ,0 , data, data_len, guarded_hash, out_len);KUNIT_ASSERT_MEMEQ (test, hash, guarded_hash, out_len); } }
@@ -124,8 +127,6 @@ static struct kunit_case blake2b_test_cases[] = {static struct kunit_suite blake2b_test_suite = { .name ="blake2b" , .test_cases = blake2b_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (blake2b_test_suite);
lib/crypto/tests/blake2s_kunit.c +17 -16
@@ -41,9 +41,9 @@ static void blake2s_init_default(struct blake2s_ctx *ctx)static void test_blake2s_all_key_and_hash_lens (struct kunit *test) {const size_t data_len =100 ; -u8 *data = &test_buf[0 ]; -u8 *key = data + data_len; -u8 *hash = key +BLAKE2S_KEY_SIZE ; +u8 *data =alloc_buf (test, data_len); +u8 *key =alloc_buf (test,BLAKE2S_KEY_SIZE ); +u8 *hash =alloc_buf (test,BLAKE2S_HASH_SIZE );struct blake2s_ctx main_ctx;u8 main_hash[BLAKE2S_HASH_SIZE ];
@@ -68,11 +68,13 @@ static void test_blake2s_all_key_and_hash_lens(struct kunit *test)static void test_blake2s_with_guarded_key_buf (struct kunit *test) {const size_t data_len =100 ; +u8 *data =alloc_buf (test, data_len); +u8 *guarded_key_buf =alloc_guarded_buf (test,BLAKE2S_KEY_SIZE ); -rand_bytes (test_buf, data_len); +rand_bytes (data, data_len);for (int key_len =0 ; key_len <=BLAKE2S_KEY_SIZE ; key_len++) {u8 key[BLAKE2S_KEY_SIZE ]; -u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len]; +u8 *guarded_key = &guarded_key_buf[BLAKE2S_KEY_SIZE - key_len];u8 hash1[BLAKE2S_HASH_SIZE ];u8 hash2[BLAKE2S_HASH_SIZE ];struct blake2s_ctx ctx;
@@ -80,14 +82,13 @@ static void test_blake2s_with_guarded_key_buf(struct kunit *test)rand_bytes (key, key_len);memcpy (guarded_key, key, key_len); -blake2s (key, key_len, test_buf, data_len, - hash1,BLAKE2S_HASH_SIZE ); -blake2s (guarded_key, key_len, test_buf, data_len, - hash2,BLAKE2S_HASH_SIZE ); +blake2s (key, key_len, data, data_len, hash1,BLAKE2S_HASH_SIZE ); +blake2s (guarded_key, key_len, data, data_len, hash2, +BLAKE2S_HASH_SIZE );KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2S_HASH_SIZE );blake2s_init_key (&ctx,BLAKE2S_HASH_SIZE , guarded_key, key_len); -blake2s_update (&ctx, test_buf, data_len); +blake2s_update (&ctx, data, data_len);blake2s_final (&ctx, hash2);KUNIT_ASSERT_MEMEQ (test, hash1, hash2,BLAKE2S_HASH_SIZE ); }
@@ -100,14 +101,16 @@ static void test_blake2s_with_guarded_key_buf(struct kunit *test)static void test_blake2s_with_guarded_out_buf (struct kunit *test) {const size_t data_len =100 ; +u8 *data =alloc_buf (test, data_len); +u8 *out_buf =alloc_guarded_buf (test,BLAKE2S_HASH_SIZE ); -rand_bytes (test_buf, data_len); +rand_bytes (data, data_len);for (int out_len =1 ; out_len <=BLAKE2S_HASH_SIZE ; out_len++) {u8 hash[BLAKE2S_HASH_SIZE ]; -u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; +u8 *guarded_hash = &out_buf[BLAKE2S_HASH_SIZE - out_len]; -blake2s (NULL ,0 , test_buf, data_len, hash, out_len); -blake2s (NULL ,0 , test_buf, data_len, guarded_hash, out_len); +blake2s (NULL ,0 , data, data_len, hash, out_len); +blake2s (NULL ,0 , data, data_len, guarded_hash, out_len);KUNIT_ASSERT_MEMEQ (test, hash, guarded_hash, out_len); } }
@@ -124,8 +127,6 @@ static struct kunit_case blake2s_test_cases[] = {static struct kunit_suite blake2s_test_suite = { .name ="blake2s" , .test_cases = blake2s_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (blake2s_test_suite);
lib/crypto/tests/ghash_kunit.c +16 -21
@@ -39,17 +39,18 @@ static void ghash_withtestkey(const u8 *data, size_t len,*/ static void test_ghash_allones_key_and_message (struct kunit *test) { +const size_t max_len =4096 ; +u8 *data =alloc_buf (test, max_len);struct ghash_key key;struct ghash_ctx hashofhashes_ctx;u8 hash[GHASH_BLOCK_SIZE ]; -static_assert (TEST_BUF_LEN >=4096 ); -memset (test_buf,0xff ,4096 ); +memset (data,0xff , max_len); -ghash_preparekey (&key, test_buf); +ghash_preparekey (&key, data);ghash_init (&hashofhashes_ctx, &key); -for (size_t len =0 ; len <=4096 ; len +=16 ) { -ghash (&key, test_buf, len, hash); +for (size_t len =0 ; len <= max_len; len +=16 ) { +ghash (&key, data, len, hash);ghash_update (&hashofhashes_ctx, hash,sizeof (hash)); }ghash_final (&hashofhashes_ctx, hash);
@@ -68,7 +69,7 @@ static void check_key_consistency(struct kunit *test,const struct ghash_key *key1, const struct ghash_key *key2) { - u8 *data = test_buf; + u8 *data = alloc_buf(test, MAX_LEN_FOR_KEY_CHECK);u8 hash1[GHASH_BLOCK_SIZE ];u8 hash2[GHASH_BLOCK_SIZE ];
@@ -88,10 +89,10 @@ static void check_key_consistency(struct kunit *test,static void test_ghash_with_guarded_key (struct kunit *test) {u8 raw_key[GHASH_BLOCK_SIZE ]; -u8 *guarded_raw_key = &test_buf[TEST_BUF_LEN -sizeof (raw_key)]; +u8 *guarded_raw_key =alloc_guarded_buf (test,sizeof (raw_key));struct ghash_key key1, key2;struct ghash_key *guarded_key = - (struct ghash_key *)&test_buf[TEST_BUF_LEN -sizeof (key1)]; +alloc_guarded_buf (test,sizeof (*guarded_key));/* Prepare with regular buffers. */ rand_bytes (raw_key,sizeof (raw_key));
@@ -116,14 +117,14 @@ static void test_ghash_with_minimally_aligned_key(struct kunit *test) {u8 raw_key[GHASH_BLOCK_SIZE ];struct ghash_key key; +const size_t align = __alignof__(struct ghash_key ); +u8 *key_buf =alloc_buf (test,sizeof (struct ghash_key ) +3 * align);struct ghash_key *minaligned_key = - (struct ghash_key *)&test_buf[MAX_LEN_FOR_KEY_CHECK + - __alignof__(struct ghash_key )]; + (struct ghash_key *)(PTR_ALIGN (key_buf,2 * align) + align); -KUNIT_ASSERT_TRUE (test,IS_ALIGNED ((uintptr_t )minaligned_key, - __alignof__(struct ghash_key ))); -KUNIT_ASSERT_TRUE (test, !IS_ALIGNED ((uintptr_t )minaligned_key, -2 * __alignof__(struct ghash_key ))); +KUNIT_ASSERT_TRUE (test,IS_ALIGNED ((uintptr_t )minaligned_key, align)); +KUNIT_ASSERT_TRUE (test, + !IS_ALIGNED ((uintptr_t )minaligned_key,2 * align));rand_bytes (raw_key,sizeof (raw_key));ghash_preparekey (&key, raw_key);
@@ -164,12 +165,7 @@ static int ghash_suite_init(struct kunit_suite *suite)rand_bytes_seeded_from_len (raw_key,sizeof (raw_key));ghash_preparekey (&test_key, raw_key); -return hash_suite_init (suite); -} - -static void ghash_suite_exit (struct kunit_suite *suite) -{ -hash_suite_exit (suite); +return 0 ; }static struct kunit_case ghash_test_cases[] = {
@@ -186,7 +182,6 @@ static struct kunit_suite ghash_test_suite = { .name = "ghash", .test_cases = ghash_test_cases, .suite_init = ghash_suite_init, - .suite_exit = ghash_suite_exit, };kunit_test_suite (ghash_test_suite);
lib/crypto/tests/hash-test-template.h +95 -96
416 lines not on this page · raw message
lib/crypto/tests/md5_kunit.c +0 -2
@@ -30,8 +30,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="md5" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/poly1305_kunit.c +8 -13
@@ -46,12 +46,7 @@ static void poly1305_withtestkey(const u8 *data, size_t len,static int poly1305_suite_init (struct kunit_suite *suite) {rand_bytes_seeded_from_len (test_key,POLY1305_KEY_SIZE ); -return hash_suite_init (suite); -} - -static void poly1305_suite_exit (struct kunit_suite *suite) -{ -hash_suite_exit (suite); +return 0 ; }/*
@@ -81,19 +76,20 @@ static void poly1305_suite_exit(struct kunit_suite *suite)*/ static void test_poly1305_allones_keys_and_message (struct kunit *test) { +const size_t max_len =4096 ; +u8 *data =alloc_buf (test, max_len);struct poly1305_desc_ctx mac_ctx, macofmacs_ctx;u8 mac[POLY1305_DIGEST_SIZE ]; -static_assert (TEST_BUF_LEN >=4096 ); -memset (test_buf,0xff ,4096 ); +memset (data,0xff , max_len); -poly1305_init (&mac_ctx, test_buf); -poly1305_init (&macofmacs_ctx, test_buf); +poly1305_init (&mac_ctx, data); +poly1305_init (&macofmacs_ctx, data);for (int i =0 ; i <32 ; i++) { -for (size_t len =0 ; len <=4096 ; len +=16 ) { +for (size_t len =0 ; len <= max_len; len +=16 ) {struct poly1305_desc_ctx tmp_ctx; -poly1305_update (&mac_ctx, test_buf, len); +poly1305_update (&mac_ctx, data, len); tmp_ctx = mac_ctx;poly1305_final (&tmp_ctx, mac); poly1305_update(&macofmacs_ctx, mac,
@@ -157,7 +153,6 @@ static struct kunit_suite poly1305_test_suite = { .name = "poly1305", .test_cases = poly1305_test_cases, .suite_init = poly1305_suite_init, - .suite_exit = poly1305_suite_exit, };kunit_test_suite (poly1305_test_suite);
lib/crypto/tests/polyval_kunit.c +16 -22
@@ -66,17 +66,18 @@ static void test_polyval_rfc8452_testvec(struct kunit *test)*/ static void test_polyval_allones_key_and_message (struct kunit *test) { +const size_t max_len =4096 ; +u8 *data =alloc_buf (test, max_len);struct polyval_key key;struct polyval_ctx hashofhashes_ctx;u8 hash[POLYVAL_BLOCK_SIZE ]; -static_assert (TEST_BUF_LEN >=4096 ); -memset (test_buf,0xff ,4096 ); +memset (data,0xff , max_len); -polyval_preparekey (&key, test_buf); +polyval_preparekey (&key, data);polyval_init (&hashofhashes_ctx, &key); -for (size_t len =0 ; len <=4096 ; len +=16 ) { -polyval (&key, test_buf, len, hash); +for (size_t len =0 ; len <= max_len; len +=16 ) { +polyval (&key, data, len, hash);polyval_update (&hashofhashes_ctx, hash,sizeof (hash)); }polyval_final (&hashofhashes_ctx, hash);
@@ -95,7 +96,7 @@ static void check_key_consistency(struct kunit *test,const struct polyval_key *key1, const struct polyval_key *key2) { - u8 *data = test_buf; + u8 *data = alloc_buf(test, MAX_LEN_FOR_KEY_CHECK);u8 hash1[POLYVAL_BLOCK_SIZE ];u8 hash2[POLYVAL_BLOCK_SIZE ];
@@ -115,10 +116,10 @@ static void check_key_consistency(struct kunit *test,static void test_polyval_with_guarded_key (struct kunit *test) {u8 raw_key[POLYVAL_BLOCK_SIZE ]; -u8 *guarded_raw_key = &test_buf[TEST_BUF_LEN -sizeof (raw_key)]; +u8 *guarded_raw_key =alloc_guarded_buf (test,sizeof (raw_key));struct polyval_key key1, key2;struct polyval_key *guarded_key = - (struct polyval_key *)&test_buf[TEST_BUF_LEN -sizeof (key1)]; +alloc_guarded_buf (test,sizeof (*guarded_key));/* Prepare with regular buffers. */ rand_bytes (raw_key,sizeof (raw_key));
@@ -137,21 +138,20 @@ static void test_polyval_with_guarded_key(struct kunit *test)/* * Test that polyval_key only needs to be aligned to * __alignof__(struct polyval_key), i.e. 8 bytes. The assembly code may prefer -* 16-byte or higher alignment, but it musn't require it. +* 16-byte or higher alignment, but it mustn't require it. */ static void test_polyval_with_minimally_aligned_key (struct kunit *test) {u8 raw_key[POLYVAL_BLOCK_SIZE ];struct polyval_key key; +const size_t align = __alignof__(struct polyval_key ); +u8 *key_buf =alloc_buf (test,sizeof (struct polyval_key ) +3 * align);struct polyval_key *minaligned_key = - (struct polyval_key *)&test_buf[MAX_LEN_FOR_KEY_CHECK + - __alignof__(struct polyval_key )]; + (struct polyval_key *)(PTR_ALIGN (key_buf,2 * align) + align); -KUNIT_ASSERT_TRUE (test,IS_ALIGNED ((uintptr_t )minaligned_key, - __alignof__(struct polyval_key ))); +KUNIT_ASSERT_TRUE (test,IS_ALIGNED ((uintptr_t )minaligned_key, align));KUNIT_ASSERT_TRUE (test, - !IS_ALIGNED ((uintptr_t )minaligned_key, -2 * __alignof__(struct polyval_key ))); + !IS_ALIGNED ((uintptr_t )minaligned_key,2 * align));rand_bytes (raw_key,sizeof (raw_key));polyval_preparekey (&key, raw_key);
@@ -192,12 +192,7 @@ static int polyval_suite_init(struct kunit_suite *suite)rand_bytes_seeded_from_len (raw_key,sizeof (raw_key));polyval_preparekey (&test_key, raw_key); -return hash_suite_init (suite); -} - -static void polyval_suite_exit (struct kunit_suite *suite) -{ -hash_suite_exit (suite); +return 0 ; }static struct kunit_case polyval_test_cases[] = {
@@ -215,7 +210,6 @@ static struct kunit_suite polyval_test_suite = { .name = "polyval", .test_cases = polyval_test_cases, .suite_init = polyval_suite_init, - .suite_exit = polyval_suite_exit, };kunit_test_suite (polyval_test_suite);
lib/crypto/tests/sha1_kunit.c +0 -2
@@ -30,8 +30,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="sha1" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/sha224_kunit.c +0 -2
@@ -30,8 +30,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="sha224" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/sha256_kunit.c +14 -13
@@ -86,19 +86,20 @@ static void test_sha256_finup_2x(struct kunit *test)static void test_sha256_finup_2x_defaultctx (struct kunit *test) {const size_t data_len =128 ; +u8 *data =alloc_buf (test,2 * data_len);struct sha256_ctx ctx;u8 hash1_a[SHA256_DIGEST_SIZE ];u8 hash2_a[SHA256_DIGEST_SIZE ];u8 hash1_b[SHA256_DIGEST_SIZE ];u8 hash2_b[SHA256_DIGEST_SIZE ]; -rand_bytes (test_buf,2 * data_len); +rand_bytes (data,2 * data_len);sha256_init (&ctx); -sha256_finup_2x (&ctx, test_buf, &test_buf[data_len], data_len, hash1_a, +sha256_finup_2x (&ctx, data, &data[data_len], data_len, hash1_a, hash2_a); -sha256_finup_2x (NULL , test_buf, &test_buf[data_len], data_len, hash1_b, +sha256_finup_2x (NULL , data, &data[data_len], data_len, hash1_b, hash2_b);KUNIT_ASSERT_MEMEQ (test, hash1_a, hash1_b,SHA256_DIGEST_SIZE );
@@ -112,18 +113,19 @@ static void test_sha256_finup_2x_defaultctx(struct kunit *test)static void test_sha256_finup_2x_hugelen (struct kunit *test) {const size_t data_len =4 *SHA256_BLOCK_SIZE ; +u8 *data =alloc_buf (test, data_len);struct sha256_ctx ctx = {};u8 expected_hash[SHA256_DIGEST_SIZE ];u8 hash[SHA256_DIGEST_SIZE ]; -rand_bytes (test_buf, data_len); +rand_bytes (data, data_len);for (size_t align =0 ; align <SHA256_BLOCK_SIZE ; align++) {sha256_init (&ctx); ctx.ctx.bytecount =0x123456789abcd00 + align; -sha256_finup_2x (&ctx, test_buf, test_buf, data_len, hash, hash); +sha256_finup_2x (&ctx, data, data, data_len, hash, hash); -sha256_update (&ctx, test_buf, data_len); +sha256_update (&ctx, data, data_len);sha256_final (&ctx, expected_hash); KUNIT_ASSERT_MEMEQ(test, hash, expected_hash,
@@ -142,6 +144,7 @@ static void benchmark_sha256_finup_2x(struct kunit *test)static const size_t salt_lens_to_test[] = {0 ,32 ,64 };const size_t data_len =4096 ;const size_t num_iters =4096 ; +u8 *data =alloc_buf (test, data_len *2 );struct sha256_ctx ctx;u8 hash1[SHA256_DIGEST_SIZE ];u8 hash2[SHA256_DIGEST_SIZE ];
@@ -151,12 +154,12 @@ static void benchmark_sha256_finup_2x(struct kunit *test)if (!sha256_finup_2x_is_optimized ())kunit_skip (test,"not relevant" ); -rand_bytes (test_buf, data_len *2 ); +rand_bytes (data, data_len *2 );/* Warm-up */ for (size_t i =0 ; i < num_iters; i++) -sha256_finup_2x (NULL , &test_buf[0 ], &test_buf[data_len], - data_len, hash1, hash2); +sha256_finup_2x (NULL , &data[0 ], &data[data_len], data_len, + hash1, hash2);for (size_t i =0 ; i <ARRAY_SIZE (salt_lens_to_test); i++) {size_t salt_len = salt_lens_to_test[i];
@@ -167,12 +170,12 @@ static void benchmark_sha256_finup_2x(struct kunit *test)* not measured; we're just interested in sha256_finup_2x(). */ sha256_init (&ctx); -sha256_update (&ctx, test_buf, salt_len); +sha256_update (&ctx, data, salt_len);preempt_disable (); t0 =ktime_get_ns ();for (size_t j =0 ; j < num_iters; j++) -sha256_finup_2x (&ctx, &test_buf[0 ], &test_buf[data_len], +sha256_finup_2x (&ctx, &data[0 ], &data[data_len], data_len, hash1, hash2); t1 =ktime_get_ns ();preempt_enable ();
@@ -196,8 +199,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="sha256" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/sha384_kunit.c +0 -2
@@ -30,8 +30,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="sha384" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/sha3_kunit.c +9 -21
@@ -273,12 +273,10 @@ static void test_shake_all_lens_up_to_4096(struct kunit *test) {struct sha3_ctx main_ctx;const size_t max_len =4096 ; -u8 *const in = test_buf; -u8 *const out = &test_buf[TEST_BUF_LEN - max_len]; +u8 *const in =alloc_buf (test, max_len); +u8 *const out =alloc_buf (test, max_len);u8 main_hash[SHA3_256_DIGEST_SIZE ]; -KUNIT_ASSERT_LE (test,2 * max_len,TEST_BUF_LEN ); -rand_bytes_seeded_from_len (in, max_len);for (int alg =0 ; alg <2 ; alg++) {sha3_256_init (&main_ctx);
@@ -309,12 +307,8 @@ static void test_shake_all_lens_up_to_4096(struct kunit *test)static void test_shake_multiple_squeezes (struct kunit *test) {const size_t max_len =512 ; -u8 *ref_out; - -KUNIT_ASSERT_GE (test,TEST_BUF_LEN ,2 * max_len); - - ref_out =kunit_kzalloc (test, max_len,GFP_KERNEL ); -KUNIT_ASSERT_NOT_NULL (test, ref_out); +u8 *buf =alloc_buf (test, max_len); +u8 *ref_out =alloc_buf (test, max_len);for (int i =0 ; i <2000 ; i++) {const int alg =rand32 () %2 ;
@@ -322,8 +316,8 @@ static void test_shake_multiple_squeezes(struct kunit *test)const size_t out_len =rand_length (max_len);const size_t in_offs =rand_offset (max_len - in_len);const size_t out_offs =rand_offset (max_len - out_len); -u8 *const in = &test_buf[in_offs]; -u8 *const out = &test_buf[out_offs]; +u8 *const in = &buf[in_offs]; +u8 *const out = &buf[out_offs];struct shake_ctx ctx;size_t remaining_len, j, num_parts;
@@ -368,16 +362,12 @@ static void test_shake_multiple_squeezes(struct kunit *test)static void test_shake_with_guarded_bufs (struct kunit *test) {const size_t max_len =512 ; -u8 *reg_buf; - -KUNIT_ASSERT_GE (test,TEST_BUF_LEN , max_len); - - reg_buf =kunit_kzalloc (test, max_len,GFP_KERNEL ); -KUNIT_ASSERT_NOT_NULL (test, reg_buf); +u8 *buf =alloc_guarded_buf (test, max_len); +u8 *reg_buf =alloc_buf (test, max_len);for (int alg =0 ; alg <2 ; alg++) {for (size_t len =0 ; len <= max_len; len++) { -u8 *guarded_buf = &test_buf[TEST_BUF_LEN - len]; +u8 *guarded_buf = &buf[max_len - len];rand_bytes (reg_buf, len);memcpy (guarded_buf, reg_buf, len);
@@ -413,8 +403,6 @@ static struct kunit_case sha3_test_cases[] = {static struct kunit_suite sha3_test_suite = { .name ="sha3" , .test_cases = sha3_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (sha3_test_suite);
lib/crypto/tests/sha512_kunit.c +0 -2
@@ -30,8 +30,6 @@ static struct kunit_case hash_test_cases[] = {static struct kunit_suite hash_test_suite = { .name ="sha512" , .test_cases = hash_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (hash_test_suite);
lib/crypto/tests/sm3_kunit.c +0 -2
@@ -22,8 +22,6 @@ static struct kunit_case sm3_test_cases[] = {static struct kunit_suite sm3_test_suite = { .name ="sm3" , .test_cases = sm3_test_cases, - .suite_init = hash_suite_init, - .suite_exit = hash_suite_exit, };kunit_test_suite (sm3_test_suite);
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 3/5 lib/crypto: tests: Add aead-test-template.h
In reply to Eric BiggersAdd aead-test-template.h which contains shared test logic for AEAD algorithms. It's similar to the existing hash-test-template.h, which contains shared test logic for hash algorithms.
The initial use cases for this will be AES-CCM and AES-GCM.
It may be used by the existing ChaCha20Poly1305 test suite in the future as well, though the ChaCha20Poly1305 API currently lacks some of the functionality that the "template" expects to be present.
--- lib/crypto/tests/aead-test-template.h | 1039 +++++++++++++++++++++++++ 1 file changed, 1039 insertions(+) create mode 100644 lib/crypto/tests/aead-test-template.h
lib/crypto/tests/aead-test-template.h new file +1039 -0
@@ -0,0 +1,1039 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Shared KUnit test cases for AEAD algorithms, including a benchmark +* +* Copyright 2026 Google LLC +*/ + +/* +* This file implements KUnit test cases shared by the different KUnit test +* suites for Authenticated Encryption with Associated Data (AEAD) algorithms. +* +* Test suites including this file must #define the following: +* +* Data structs: +* - AEAD_KEY: name of key struct +* - AEAD_CTX: name of context for incremental computation +* +* Constants: +* - AEAD_VALID_KEY_LENS: array of all valid key lengths in bytes +* - AEAD_VALID_NONCE_LENS: array of all valid nonce lengths in bytes +* - AEAD_VALID_TAG_LENS: array of all valid authtag lengths in bytes +* - AEAD_MAX_KEY_LEN: max key length in bytes (assumed to fit on stack) +* - AEAD_MAX_NONCE_LEN: max nonce length in bytes (assumed to fit on stack) +* - AEAD_MAX_TAG_LEN: max authtag length in bytes (assumed to fit on stack) +* - AEAD_MONTE_CARLO_CHECKSUM: checksum of a deterministically generated series +* of (ciphertext, authtag) pairs (see test_aead_monte_carlo()) +* +* Functions: +* - AEAD_PREPAREKEY: key preparation +* - AEAD_ENCRYPT and AEAD_DECRYPT: one-shot encryption and decryption +* - AEAD_INIT, AEAD_AUTH_UPDATE, AEAD_ENCRYPT_UPDATE, AEAD_ENCRYPT_FINAL, +* AEAD_DECRYPT_UPDATE, AEAD_DECRYPT_FINAL: functions for incremental +* encryption and decryption +* +* Function prototypes and their behavior must match the AES-CCM API. +*/ + +#include <crypto/blake2s.h> +#include <kunit/run-in-irq-context.h> +#include <kunit/test.h> +#include <linux/ktime.h> +#include <linux/preempt.h> +#include "test-utils.h" + +/* +* Allocate a KUnit-managed struct AEAD_KEY and prepare it with a random key, +* using a random key length and random authentication tag length. +*/ +static struct AEAD_KEY *aead_alloc_random_key (struct kunit *test, +size_t *tag_len_ret) +{ +size_t key_len = +AEAD_VALID_KEY_LENS [rand32 () %ARRAY_SIZE (AEAD_VALID_KEY_LENS )]; +size_t tag_len = +AEAD_VALID_TAG_LENS [rand32 () %ARRAY_SIZE (AEAD_VALID_TAG_LENS )]; +u8 raw_key[AEAD_MAX_KEY_LEN ]; +struct AEAD_KEY *key =alloc_buf (test,sizeof (*key)); +int err; + +rand_bytes (raw_key, key_len); + err =AEAD_PREPAREKEY (key, raw_key, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + *tag_len_ret = tag_len; +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) +{ +size_t num_parts =0 ; +size_t pos =0 ; + +while (rand_bool ()) { +size_t part_len =rand_length (ad_len - pos); + +AEAD_AUTH_UPDATE (ctx, &ad[pos], part_len); + pos += part_len; + num_parts++; + } +if (pos < ad_len ||rand_bool ()) { +AEAD_AUTH_UPDATE (ctx, &ad[pos], ad_len - pos); + num_parts++; + } +return num_parts; +} + +/* Process the given en/decrypted data using a random incremental strategy. */ +static size_t aead_crypt_incrementally (struct AEAD_CTX *ctx,u8 *dst, +const u8 *src,size_t data_len,bool enc) +{ +size_t num_parts =0 ; +size_t pos =0 ; + +while (rand_bool ()) { +size_t part_len =rand_length (data_len - pos); + +if (enc) +AEAD_ENCRYPT_UPDATE (ctx, &dst[pos], &src[pos], + part_len); +else +AEAD_DECRYPT_UPDATE (ctx, &dst[pos], &src[pos], + part_len); + pos += part_len; + num_parts++; + } +if (pos < data_len ||rand_bool ()) { +if (enc) +AEAD_ENCRYPT_UPDATE (ctx, &dst[pos], &src[pos], + data_len - pos); +else +AEAD_DECRYPT_UPDATE (ctx, &dst[pos], &src[pos], + data_len - pos); + num_parts++; + } +return num_parts; +} + +struct aead_incremental_info { +size_t num_data_parts; +size_t num_ad_parts; +}; + +static const char *aead_incr_info_str (struct kunit *test, +const struct aead_incremental_info *info) +{ +const size_t max_str_len =64 ; +char *str =alloc_buf (test, max_str_len); + +snprintf (str, max_str_len,"num_data_parts=%zu num_ad_parts=%zu" , + info->num_data_parts, info->num_ad_parts); +return str; +} + +/* +* Encrypt data using a random incremental strategy. +* Return information about the incremental strategy used. +*/ +static struct aead_incremental_info +aead_encrypt_incrementally (struct kunit *test,struct AEAD_CTX *ctx,u8 *dst, +const u8 *src,size_t data_len,u8 *tag, +const u8 *ad,size_t ad_len,const u8 *nonce, +size_t nonce_len,const struct AEAD_KEY *key) +{ +struct aead_incremental_info info; +int err; + + err =AEAD_INIT (ctx, data_len, ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + info.num_ad_parts =aead_auth_incrementally (ctx, ad, ad_len); + info.num_data_parts =aead_crypt_incrementally (ctx, dst, src, data_len, +/* enc= */ true); +AEAD_ENCRYPT_FINAL (ctx, tag); +KUNIT_ASSERT_TRUE_MSG (test,mem_is_zero (ctx,sizeof (*ctx)), +"encrypt_final didn't zeroize context" ); +return info; +} + +/* +* Decrypt authentic data using a random incremental strategy. +* Return information about the incremental strategy used. +*/ +static struct aead_incremental_info +aead_decrypt_incrementally (struct kunit *test,struct AEAD_CTX *ctx,u8 *dst, +const u8 *src,size_t data_len,const u8 *tag, +const u8 *ad,size_t ad_len,const u8 *nonce, +size_t nonce_len,const struct AEAD_KEY *key) +{ +struct aead_incremental_info info; +int err; + + err =AEAD_INIT (ctx, data_len, ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + info.num_ad_parts =aead_auth_incrementally (ctx, ad, ad_len); + info.num_data_parts =aead_crypt_incrementally (ctx, dst, src, data_len, +/* enc= */ false); + err =AEAD_DECRYPT_FINAL (ctx, tag); +KUNIT_ASSERT_EQ (test,0 , err); +KUNIT_ASSERT_TRUE_MSG (test,mem_is_zero (ctx,sizeof (*ctx)), +"decrypt_final didn't zeroize context" ); +return info; +} + +/* Return true if key_len is declared to be a valid key length. */ +static bool aead_is_key_len_expected_valid (size_t key_len) +{ +for (size_t i =0 ; i <ARRAY_SIZE (AEAD_VALID_KEY_LENS ); i++) { +if (AEAD_VALID_KEY_LENS [i] == key_len) +return true; + } +return false; +} + +/* Return true if nonce_len is declared to be a valid nonce length. */ +static bool aead_is_nonce_len_expected_valid (size_t nonce_len) +{ +for (size_t i =0 ; i <ARRAY_SIZE (AEAD_VALID_NONCE_LENS ); i++) { +if (AEAD_VALID_NONCE_LENS [i] == nonce_len) +return true; + } +return false; +} + +/* Return true if tag_len is declared to be a valid tag length. */ +static bool aead_is_tag_len_expected_valid (size_t tag_len) +{ +for (size_t i =0 ; i <ARRAY_SIZE (AEAD_VALID_TAG_LENS ); i++) { +if (AEAD_VALID_TAG_LENS [i] == tag_len) +return true; + } +return false; +} + +struct aead_basic_validation_test_ctx { +struct AEAD_KEY key; +struct AEAD_CTX ctx; +u8 *raw_key_buf_end; +u8 *nonce_buf_end; +u8 *tag_buf_end; +u8 pt[64 ];/* plaintext */ +u8 ct[64 ];/* ciphertext */ +u8 decrypted[64 ]; +u8 ad[16 ];/* associated data */ +u8 *unused_buf; +size_t data_len; +size_t ad_len; +}; + +static struct aead_basic_validation_test_ctx * +aead_alloc_basic_validation_test_ctx (struct kunit *test) +{ +struct aead_basic_validation_test_ctx *ctx = +alloc_buf (test,sizeof (*ctx)); + +memset (ctx,0 ,sizeof (*ctx)); + 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 ; + +/* +* A pointer to this buffer is passed when passing a length that is +* expected to be invalid. It should never actually be accessed. +*/ + ctx->unused_buf = +alloc_buf (test,max3 (AEAD_MAX_KEY_LEN ,AEAD_MAX_NONCE_LEN , +AEAD_MAX_TAG_LEN )); + + ctx->data_len =sizeof (ctx->pt); + ctx->ad_len =sizeof (ctx->ad); +return ctx; +} + +/* +* Given an expected-valid key_len, nonce_len, and tag_len, verify round-trip +* encryption and decryption with them. Use guarded buffers for each of the raw +* key, nonce, and tag to detect any buffer overruns in them. Also, verify that +* every byte of the tag is actually checked. +*/ +static void aead_do_basic_checks (struct kunit *test, +struct aead_basic_validation_test_ctx *ctx, +size_t key_len,size_t nonce_len, +size_t tag_len) +{ +/* Set up exact-size guarded buffers for (raw_key, nonce, tag). */ +const u8 *raw_key = ctx->raw_key_buf_end - key_len; +const u8 *nonce = ctx->nonce_buf_end - nonce_len; +u8 *tag = ctx->tag_buf_end - tag_len; +int err; + +/* Key preparation should succeed. */ + err =AEAD_PREPAREKEY (&ctx->key, raw_key, key_len, tag_len); +KUNIT_ASSERT_EQ_MSG (test,0 , err, +"key_len=%zu, tag_len=%zu wasn't accepted" , key_len, + tag_len); + +/* Encryption should succeed. */ + err =AEAD_ENCRYPT (ctx->ct, ctx->pt, ctx->data_len, tag, ctx->ad, + ctx->ad_len, nonce, nonce_len, &ctx->key); +KUNIT_ASSERT_EQ_MSG ( + test,0 , err, +"Encryption failed with key_len=%zu, nonce_len=%zu, tag_len=%zu" , + key_len, nonce_len, tag_len); + +/* Decryption should succeed and give the original data. */ + err =AEAD_DECRYPT (ctx->decrypted, ctx->ct, ctx->data_len, tag, ctx->ad, + ctx->ad_len, nonce, nonce_len, &ctx->key); +KUNIT_ASSERT_EQ_MSG ( + test,0 , err, +"Decryption failed with key_len=%zu, nonce_len=%zu, tag_len=%zu" , + key_len, nonce_len, tag_len); +KUNIT_ASSERT_MEMEQ_MSG ( + test, ctx->pt, ctx->decrypted, ctx->data_len, +"Decryption gave wrong output with key_len=%zu, nonce_len=%zu, tag_len=%zu" , + key_len, nonce_len, tag_len); + +/* +* Every byte of the tag should actually be checked. +* And on authentication failure, the dst buffer should be cleared. +*/ +for (size_t i =0 ; i < tag_len; i++) { +memset (ctx->decrypted,0xff , ctx->data_len); + tag[i] ^=1 ; + err =AEAD_DECRYPT (ctx->decrypted, ctx->ct, ctx->data_len, tag, + ctx->ad, ctx->ad_len, nonce, nonce_len, + &ctx->key); +KUNIT_ASSERT_EQ_MSG ( + test, -EBADMSG , err, +"Decryption with bad auth tag with key_len=%zu, nonce_len=%zu, tag_len=%zu didn't fail with -EBADMSG" , + key_len, nonce_len, tag_len); +KUNIT_ASSERT_TRUE_MSG ( + test,mem_is_zero (ctx->decrypted, ctx->data_len), +"dst wasn't cleared on authentication failure" ); + tag[i] ^=1 ; + } +} + +/* Verify that the given expected-invalid key_len is actually rejected. */ +static void +aead_verify_invalid_key_len (struct kunit *test, +struct aead_basic_validation_test_ctx *ctx, +size_t key_len) +{ +int err; + +/* +* The preparekey function should reject the key_len. It should do so +* before writing to the key struct. +*/ +memset (&ctx->key,0 ,sizeof (ctx->key)); + err =AEAD_PREPAREKEY (&ctx->key, ctx->unused_buf, key_len, +AEAD_MAX_TAG_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 (&ctx->key,sizeof (ctx->key)), +"Key struct was written to before length validation" ); +} + +/* +* Test that every valid key length is accepted and basic checks pass with it, +* and test that invalid key lengths are rejected. +*/ +static void test_aead_all_key_lens (struct kunit *test) +{ +struct aead_basic_validation_test_ctx *ctx = +aead_alloc_basic_validation_test_ctx (test); + +for (size_t key_len =0 ; key_len <=AEAD_MAX_KEY_LEN ; key_len++) { +if (aead_is_key_len_expected_valid (key_len)) +aead_do_basic_checks (test, ctx, key_len, +AEAD_MAX_NONCE_LEN , +AEAD_MAX_TAG_LEN ); +else +aead_verify_invalid_key_len (test, ctx, key_len); + } +aead_verify_invalid_key_len (test, ctx,AEAD_MAX_KEY_LEN +1 ); +aead_verify_invalid_key_len (test, ctx,AEAD_MAX_KEY_LEN *2 ); +aead_verify_invalid_key_len (test, ctx,U32_MAX ); +aead_verify_invalid_key_len (test, ctx,SIZE_MAX ); +} + +/* Verify that the given expected-invalid nonce_len is actually rejected. */ +static void +aead_verify_invalid_nonce_len (struct kunit *test, +struct aead_basic_validation_test_ctx *ctx, +size_t nonce_len) +{ +static const u8 raw_key[AEAD_MAX_KEY_LEN ]; +int err; + +/* Key preparation should succeed, as nonce_len isn't given yet. */ + err =AEAD_PREPAREKEY (&ctx->key, raw_key,sizeof (raw_key), +AEAD_MAX_TAG_LEN ); +KUNIT_ASSERT_EQ (test,0 , err); + +/* The init function should reject the nonce_len. */ +memset (&ctx->ctx,0 ,sizeof (ctx->ctx)); + err =AEAD_INIT (&ctx->ctx, ctx->data_len, ctx->ad_len, ctx->unused_buf, + nonce_len, &ctx->key); +KUNIT_ASSERT_EQ_MSG (test, -EINVAL , err, +"nonce_len=%zu wasn't rejected with -EINVAL (init)" , + nonce_len); +KUNIT_ASSERT_TRUE_MSG ( + test,mem_is_zero (&ctx->ctx,sizeof (ctx->ctx)), +"Context struct was written to before length validation" ); + +/* The encrypt function should reject the nonce_len. */ + err =AEAD_ENCRYPT (ctx->ct, ctx->pt, ctx->data_len, ctx->unused_buf, + ctx->ad, ctx->ad_len, ctx->unused_buf, nonce_len, + &ctx->key); +KUNIT_ASSERT_EQ_MSG ( + test, -EINVAL , err, +"nonce_len=%zu wasn't rejected with -EINVAL (encrypt)" , + nonce_len); + +/* The decrypt function should reject the nonce_len. */ + err =AEAD_DECRYPT (ctx->pt, ctx->ct, ctx->data_len, ctx->unused_buf, + ctx->ad, ctx->ad_len, ctx->unused_buf, nonce_len, + &ctx->key); +KUNIT_ASSERT_EQ_MSG ( + test, -EINVAL , err, +"nonce_len=%zu wasn't rejected with -EINVAL (decrypt)" , + nonce_len); +} + +/* +* Test that every valid nonce length is accepted and basic checks pass with it, +* and test that invalid nonce lengths are rejected. +*/ +static void test_aead_all_nonce_lens (struct kunit *test) +{ +struct aead_basic_validation_test_ctx *ctx = +aead_alloc_basic_validation_test_ctx (test); + +for (size_t nonce_len =0 ; nonce_len <=AEAD_MAX_NONCE_LEN ; + nonce_len++) { +if (aead_is_nonce_len_expected_valid (nonce_len)) +aead_do_basic_checks (test, ctx,AEAD_MAX_KEY_LEN , + nonce_len,AEAD_MAX_TAG_LEN ); +else +aead_verify_invalid_nonce_len (test, ctx, nonce_len); + } +aead_verify_invalid_nonce_len (test, ctx,AEAD_MAX_NONCE_LEN +1 ); +aead_verify_invalid_nonce_len (test, ctx,AEAD_MAX_NONCE_LEN *2 ); +aead_verify_invalid_nonce_len (test, ctx,U32_MAX ); +aead_verify_invalid_nonce_len (test, ctx,SIZE_MAX ); +} + +/* Verify that the given expected-invalid tag_len is actually rejected. */ +static void +aead_verify_invalid_tag_len (struct kunit *test, +struct aead_basic_validation_test_ctx *ctx, +size_t tag_len) +{ +static const u8 raw_key[AEAD_MAX_KEY_LEN ]; +int err; + +/* +* The preparekey function should reject the tag_len. It should do so +* before writing to the key struct. +*/ +memset (&ctx->key,0 ,sizeof (ctx->key)); + err =AEAD_PREPAREKEY (&ctx->key, raw_key,sizeof (raw_key), tag_len); +KUNIT_ASSERT_EQ_MSG (test, -EINVAL , err, +"tag_len=%zu wasn't rejected with -EINVAL" , + tag_len); +KUNIT_ASSERT_TRUE_MSG ( + test,mem_is_zero (&ctx->key,sizeof (ctx->key)), +"Key struct was written to before length validation" ); +} + +/* +* Test that every valid authentication tag length is accepted and basic checks +* pass with it, and test that invalid authentication tag lengths are rejected. +*/ +static void test_aead_all_tag_lens (struct kunit *test) +{ +struct aead_basic_validation_test_ctx *ctx = +aead_alloc_basic_validation_test_ctx (test); + +for (size_t tag_len =0 ; tag_len <=AEAD_MAX_TAG_LEN ; tag_len++) { +if (aead_is_tag_len_expected_valid (tag_len)) +aead_do_basic_checks (test, ctx,AEAD_MAX_KEY_LEN , +AEAD_MAX_NONCE_LEN , tag_len); +else +aead_verify_invalid_tag_len (test, ctx, tag_len); + } +aead_verify_invalid_tag_len (test, ctx,AEAD_MAX_TAG_LEN +1 ); +aead_verify_invalid_tag_len (test, ctx,AEAD_MAX_TAG_LEN *2 ); +aead_verify_invalid_tag_len (test, ctx,U32_MAX ); +aead_verify_invalid_tag_len (test, ctx,SIZE_MAX ); +} + +/* +* Test that one-shot encryption and decryption are consistent with each other +* and with incremental encryption and decryption. +*/ +static void test_aead_incremental_updates (struct kunit *test) +{ +const size_t max_data_len =1024 ; +const size_t max_ad_len =512 ; +const size_t nonce_len =AEAD_MAX_NONCE_LEN ; +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 *ct =alloc_buf (test, max_data_len); +u8 *ct2 =alloc_buf (test, max_data_len); +u8 *decrypted =alloc_buf (test, max_data_len); +u8 *tag =alloc_buf (test, tag_len); +u8 *tag2 =alloc_buf (test, tag_len); +int err; + +for (int i =0 ; i <500 ; i++) { +/* Select the lengths to test. */ +const size_t data_len =rand_length (max_data_len); +const size_t ad_len =rand_length (max_ad_len); +struct aead_incremental_info incr_info; + +/* Try one-shot encryption and decryption. */ + err =AEAD_ENCRYPT (ct, pt, data_len, tag, ad, ad_len, nonce, + nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_DECRYPT (decrypted, ct, data_len, tag, ad, ad_len, + nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); +KUNIT_ASSERT_MEMEQ_MSG ( + test, pt, decrypted, data_len, +"Decryption didn't invert encryption; data_len=%zu, ad_len=%zu" , + data_len, ad_len); + +/* Try incremental encryption and decryption. */ + incr_info =aead_encrypt_incrementally (test, ctx, ct2, pt, + data_len, tag2, ad, + ad_len, nonce, nonce_len, + key); +KUNIT_ASSERT_MEMEQ_MSG ( + test, ct, ct2, data_len, +"One-shot and incremental encryption gave different ciphertexts; data_len=%zu ad_len=%zu %s" , + data_len, ad_len,aead_incr_info_str (test, &incr_info)); +KUNIT_ASSERT_MEMEQ_MSG ( + test, tag, tag2, tag_len, +"One-shot and incremental encryption gave different auth tags; data_len=%zu ad_len=%zu %s" , + data_len, ad_len,aead_incr_info_str (test, &incr_info)); + incr_info =aead_decrypt_incrementally (test, ctx, decrypted, + ct2, data_len, tag2, ad, + ad_len, nonce, nonce_len, + key); +KUNIT_ASSERT_MEMEQ_MSG ( + test, pt, decrypted, data_len, +"One-shot and incremental decryption gave different plaintexts; data_len=%zu ad_len=%zu %s" , + data_len, ad_len,aead_incr_info_str (test, &incr_info)); + } +} + +/* +* Test using guarded buffers for the plaintext, ciphertext, and associated +* data. This detects out-of-bounds accesses, even in assembly code. +* +* Note: other test cases cover overrun of raw_key, nonce, and tag. +*/ +static void test_aead_data_buffer_overruns (struct kunit *test) +{ +const size_t max_data_len =1024 ; +const size_t max_ad_len =512 ; +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 *ad_end = +aead_alloc_random_data_guarded (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; +u8 *tag =alloc_buf (test, tag_len); + +for (int i =0 ; i <200 ; i++) { +/* Select the lengths to test. */ +const size_t data_len =rand_length (max_data_len); +const size_t ad_len =rand_length (max_ad_len); +/* Set up exact-size guarded buffers. */ +const u8 *pt = pt_end - data_len; +const u8 *ad = ad_end - ad_len; +u8 *ct = ct_end - data_len; +u8 *decrypted = decrypted_end - data_len; +int err; + +/* Encrypt and decrypt. */ + err =AEAD_ENCRYPT (ct, pt, data_len, tag, ad, ad_len, nonce, + nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_DECRYPT (decrypted, ct, data_len, tag, ad, ad_len, + nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); +KUNIT_ASSERT_MEMEQ_MSG ( + test, pt, decrypted, data_len, +"Decryption didn't invert encryption; data_len=%zu, ad_len=%zu" , + data_len, ad_len); + } +} + +/* +* Test that encryption and decryption produce the same results regardless of +* how the buffers are aligned in memory. +*/ +static void test_aead_alignment_consistency (struct kunit *test) +{ +const size_t max_data_len =4096 ; +const size_t max_ad_len =4096 ; +const size_t max_offset =128 ; +const size_t nonce_len =AEAD_MAX_NONCE_LEN ; +const size_t key_len =AEAD_MAX_KEY_LEN ; +const size_t tag_len =AEAD_MAX_TAG_LEN ; +u8 *raw_key1_buf =alloc_buf (test, key_len + max_offset); +u8 *raw_key2_buf =alloc_buf (test, key_len + max_offset); +u8 *nonce1_buf =alloc_buf (test, nonce_len + max_offset); +u8 *nonce2_buf =alloc_buf (test, nonce_len + 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 *ad1_buf =alloc_buf (test, max_ad_len); +u8 *ad2_buf =alloc_buf (test, max_ad_len); +u8 *tag1_buf =alloc_buf (test, tag_len + max_offset); +u8 *tag2_buf =alloc_buf (test, tag_len + max_offset); +struct AEAD_KEY *key =alloc_buf (test,sizeof (*key)); +int err; + +for (int i =0 ; i <100 ; i++) { +/* Generate lengths. */ +size_t data_len =rand_length (max_data_len); +size_t ad_len =rand_length (max_ad_len); + +/* Generate two sets of alignments. */ +u8 *raw_key1 = raw_key1_buf +rand_offset (max_offset); +u8 *raw_key2 = raw_key2_buf +rand_offset (max_offset); +u8 *nonce1 = nonce1_buf +rand_offset (max_offset); +u8 *nonce2 = nonce2_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); +u8 *ad1 = ad1_buf +rand_offset (max_ad_len - ad_len); +u8 *ad2 = ad2_buf +rand_offset (max_ad_len - ad_len); +u8 *tag1 = tag1_buf +rand_offset (max_offset); +u8 *tag2 = tag2_buf +rand_offset (max_offset); + +/* +* Generate inputs in the first set of buffers using the first +* set of alignments. +*/ +rand_bytes (raw_key1, key_len); +rand_bytes (nonce1, nonce_len); +rand_bytes (pt1, data_len); +rand_bytes (ad1, ad_len); + +/* +* Copy the inputs to the second set of buffers using the second +* set of alignments. +*/ +memcpy (raw_key2, raw_key1, key_len); +memcpy (nonce2, nonce1, nonce_len); +memcpy (pt2, pt1, data_len); +memcpy (ad2, ad1, ad_len); + +/* Verify encryption consistency. */ + + err =AEAD_PREPAREKEY (key, raw_key1, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_ENCRYPT (ct1, pt1, data_len, tag1, ad1, ad_len, + nonce1, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + + err =AEAD_PREPAREKEY (key, raw_key2, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_ENCRYPT (ct2, pt2, data_len, tag2, ad2, ad_len, + nonce2, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +KUNIT_ASSERT_MEMEQ (test, ct1, ct2, data_len); +KUNIT_ASSERT_MEMEQ (test, tag1, tag2, tag_len); + +/* Verify decryption consistency. */ + + err =AEAD_PREPAREKEY (key, raw_key1, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_DECRYPT (pt1, ct1, data_len, tag1, ad1, ad_len, + nonce1, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + + err =AEAD_PREPAREKEY (key, raw_key2, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_DECRYPT (pt2, ct2, data_len, tag2, ad2, ad_len, + nonce2, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +KUNIT_ASSERT_MEMEQ (test, pt1, pt2, data_len); + } +} + +static void test_aead_inplace (struct kunit *test) +{ +const size_t max_data_len =1024 ; +const size_t max_ad_len =512 ; +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 *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); + +for (int i =0 ; i <100 ; i++) { +size_t data_len =rand_length (max_data_len); +size_t ad_len =rand_length (max_ad_len); +int err; + +/* Encrypt out-of-place. */ + err =AEAD_ENCRYPT (data2, data, data_len, data2 + data_len, ad, + ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +/* Encrypt in-place. */ + err =AEAD_ENCRYPT (data, data, data_len, data + data_len, ad, + ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +/* Compare the results. */ +KUNIT_ASSERT_MEMEQ (test, data2, data, data_len + tag_len); + +/* Decrypt out-of-place. */ + err =AEAD_DECRYPT (data2, data, data_len, data + data_len, ad, + ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +/* Decrypt in-place. */ + err =AEAD_DECRYPT (data, data, data_len, data + data_len, ad, + ad_len, nonce, nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + +/* Compare the results. */ +KUNIT_ASSERT_MEMEQ (test, data2, data, data_len); + } +} + +/* +* Monte-Carlo test for AEAD algorithms. This deterministically generates +* random AEAD inputs, encrypts them, verifies decryption, and computes and +* verifies the checksum of all computed (ciphertext, tag) pairs. +*/ +static void test_aead_monte_carlo (struct kunit *test) +{ +const size_t max_data_len =1024 ; +const size_t max_ad_len =293 ; +u8 raw_key[AEAD_MAX_KEY_LEN ]; +u8 nonce[AEAD_MAX_NONCE_LEN ]; +u8 tag[AEAD_MAX_TAG_LEN ]; +u8 *pt =alloc_buf (test, max_data_len); +u8 *ct =alloc_buf (test, max_data_len); +u8 *decrypted =alloc_buf (test, max_data_len); +u8 *ad =alloc_buf (test, max_ad_len); +struct AEAD_KEY *key =alloc_buf (test,sizeof (*key)); +struct blake2s_ctx checksum_ctx; +u8 actual_checksum[BLAKE2S_HASH_SIZE ]; +int err; + +blake2s_init (&checksum_ctx,BLAKE2S_HASH_SIZE ); + +for (size_t data_len =0 ; data_len <= max_data_len; data_len++) { +size_t ad_len = data_len % max_ad_len; +size_t key_len = +AEAD_VALID_KEY_LENS [data_len % +ARRAY_SIZE (AEAD_VALID_KEY_LENS )]; +size_t nonce_len = +AEAD_VALID_NONCE_LENS [data_len % +ARRAY_SIZE (AEAD_VALID_NONCE_LENS )]; +size_t tag_len = +AEAD_VALID_TAG_LENS [data_len % +ARRAY_SIZE (AEAD_VALID_TAG_LENS )]; + +rand_bytes_seeded_from_len (pt, data_len); +rand_bytes_seeded_from_len (ad, ad_len); +rand_bytes_seeded_from_len (raw_key, key_len); +rand_bytes_seeded_from_len (nonce, nonce_len); + + err =AEAD_PREPAREKEY (key, raw_key, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + + err =AEAD_ENCRYPT (ct, pt, data_len, tag, ad, ad_len, nonce, + nonce_len, key); +KUNIT_ASSERT_EQ_MSG ( + test,0 , err, +"Encryption failed with data_len=%zu, ad_len=%zu" , + data_len, ad_len); + err =AEAD_DECRYPT (decrypted, ct, data_len, tag, ad, ad_len, + nonce, nonce_len, key); +KUNIT_ASSERT_EQ_MSG ( + test,0 , err, +"Decryption failed with data_len=%zu, ad_len=%zu" , + data_len, ad_len); +KUNIT_ASSERT_MEMEQ_MSG ( + test, pt, decrypted, data_len, +"Decryption didn't invert encryption; data_len=%zu, ad_len=%zu" , + data_len, ad_len); + +blake2s_update (&checksum_ctx, ct, data_len); +blake2s_update (&checksum_ctx, tag, tag_len); + } + +blake2s_final (&checksum_ctx, actual_checksum); +KUNIT_EXPECT_MEMEQ_MSG (test, actual_checksum,AEAD_MONTE_CARLO_CHECKSUM , +BLAKE2S_HASH_SIZE , +"Monte-Carlo checksum mismatch" ); +} + +#define IRQ_TEST_DATA_LEN 256 +#define IRQ_TEST_NUM_BUFFERS 3/* matches max concurrency level */ + +struct aead_irq_test_slot { +/* Fields written only at test case initialization time */ +u8 raw_key[AEAD_MAX_KEY_LEN ]; +u8 nonce[AEAD_MAX_NONCE_LEN ]; +u8 pt[IRQ_TEST_DATA_LEN ]; +u8 ct[IRQ_TEST_DATA_LEN +AEAD_MAX_TAG_LEN ]; +u8 ad[IRQ_TEST_DATA_LEN ]; + +/* Fields written throughout the test case */ +struct AEAD_KEY key; +u8 scratch_buf[IRQ_TEST_DATA_LEN +AEAD_MAX_TAG_LEN ]; +int phase; +atomic_t in_use; +}; + +struct aead_irq_test_state { +struct aead_irq_test_slot slots[IRQ_TEST_NUM_BUFFERS ]; +}; + +static bool aead_irq_test_func (void *state_) +{ +struct aead_irq_test_state *state = state_; +struct aead_irq_test_slot *slot; +size_t data_len; +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. +*/ + + data_len =sizeof (slot->pt); +if (slot->phase ==0 ) { +/* Phase 0: Prepare slot's key in current context. */ + ok = ok &&AEAD_PREPAREKEY (&slot->key, slot->raw_key, +sizeof (slot->raw_key), +AEAD_MAX_TAG_LEN ) ==0 ; + }else if (slot->phase ==1 ) { +/* +* Phase 1: Encrypt plaintext using key that may have been +* prepared in a different context. +*/ + ok = ok &&AEAD_ENCRYPT (slot->scratch_buf, slot->pt, data_len, + &slot->scratch_buf[data_len], slot->ad, +sizeof (slot->ad), slot->nonce, +sizeof (slot->nonce), &slot->key) ==0 ; +/* Verify the ciphertext (with concatenated auth tag) matches */ + ok = ok && +memcmp (slot->scratch_buf, slot->ct,sizeof (slot->ct)) ==0 ; + }else { +/* +* Phase 2: Decrypt ciphertext using key that may have been +* prepared in a different context. +*/ + ok = ok &&AEAD_DECRYPT (slot->scratch_buf, slot->ct, data_len, + &slot->ct[data_len], slot->ad, +sizeof (slot->ad), slot->nonce, +sizeof (slot->nonce), &slot->key) ==0 ; +/* Verify the plaintext matches. */ + ok = ok &&memcmp (slot->scratch_buf, slot->pt, data_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_aead_interrupt_context (struct kunit *test) +{ +struct aead_irq_test_state *state =alloc_buf (test,sizeof (*state)); + +memset (state,0 ,sizeof (*state)); + +/* +* For each slot, generate a set of AEAD inputs: a key, a nonce, a +* plaintext, and some associated data. Then generate the corresponding +* ciphertext with concatenated auth tag. +*/ +for (int i =0 ; i <IRQ_TEST_NUM_BUFFERS ; i++) { +struct aead_irq_test_slot *slot = &state->slots[i]; +int err; + +rand_bytes (slot->raw_key,sizeof (slot->raw_key)); +rand_bytes (slot->nonce,sizeof (slot->nonce)); +rand_bytes (slot->pt,sizeof (slot->pt)); +rand_bytes (slot->ad,sizeof (slot->ad)); + err =AEAD_PREPAREKEY (&slot->key, slot->raw_key, +sizeof (slot->raw_key),AEAD_MAX_TAG_LEN ); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_ENCRYPT (slot->ct, slot->pt,sizeof (slot->pt), + &slot->ct[sizeof (slot->pt)], slot->ad, +sizeof (slot->ad), slot->nonce, +sizeof (slot->nonce), &slot->key); +KUNIT_ASSERT_EQ (test,0 , err); + } + +kunit_run_irq_test (test, aead_irq_test_func,100000 , state); +} + +/* Benchmark AEAD encryption and decryption on various data lengths. */ +static void benchmark_aead (struct kunit *test) +{ +static const size_t data_lens_to_test[] = { +16 ,64 ,128 ,256 ,512 ,1024 ,1420 ,4096 ,16384 , + }; +const size_t max_data_len =16384 ; +const size_t ad_len =16 ; +const size_t key_len =AEAD_MAX_KEY_LEN ; +const size_t nonce_len =AEAD_MAX_NONCE_LEN ; +const size_t tag_len =AEAD_MAX_TAG_LEN ; +const u8 *raw_key, *nonce, *ad; +u8 *pt, *ct, *tag; +struct AEAD_KEY *key; +int err; + +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); + ct =alloc_buf (test, max_data_len); + tag =alloc_buf (test, tag_len); + + key =alloc_buf (test,sizeof (*key)); + err =AEAD_PREPAREKEY (key, raw_key, key_len, tag_len); +KUNIT_ASSERT_EQ (test,0 , err); + +/* Warm-up */ +for (size_t i =0 ; i <10000000 ; i += max_data_len) { + err =AEAD_ENCRYPT (ct, pt, max_data_len, tag, ad, ad_len, nonce, + nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + err =AEAD_DECRYPT (pt, ct, max_data_len, tag, ad, ad_len, nonce, + nonce_len, key); +KUNIT_ASSERT_EQ (test,0 , err); + } + +for (size_t i =0 ; i <ARRAY_SIZE (data_lens_to_test); i++) { +size_t data_len = data_lens_to_test[i]; +size_t num_iters =10000000 / (data_len +128 ); +u64 t_enc, t_dec; +bool ok = true; + +KUNIT_ASSERT_LE (test, data_len, max_data_len); + +preempt_disable (); + + t_enc =ktime_get_ns (); +for (size_t j =0 ; j < num_iters; j++) { + err =AEAD_ENCRYPT (ct, pt, data_len, tag, ad, ad_len, + nonce, nonce_len, key); + ok &= (err ==0 ); + } + t_enc =ktime_get_ns () - t_enc; + + t_dec =ktime_get_ns (); +for (size_t j =0 ; j < num_iters; j++) { + err =AEAD_DECRYPT (pt, ct, data_len, tag, ad, ad_len, + nonce, nonce_len, key); + ok &= (err ==0 ); + } + t_dec =ktime_get_ns () - t_dec; + +preempt_enable (); + +KUNIT_ASSERT_TRUE_MSG (test, ok,"data_len=%zu" , data_len); + +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 */ +#define AEAD_KUNIT_CASES \ + KUNIT_CASE(test_aead_all_key_lens), \ + KUNIT_CASE(test_aead_all_nonce_lens), \ + KUNIT_CASE(test_aead_all_tag_lens), \ + KUNIT_CASE(test_aead_incremental_updates), \ + KUNIT_CASE(test_aead_data_buffer_overruns), \ + KUNIT_CASE(test_aead_alignment_consistency), \ + KUNIT_CASE(test_aead_inplace), \ + KUNIT_CASE(test_aead_monte_carlo), \ + KUNIT_CASE(test_aead_interrupt_context), \ + KUNIT_CASE(benchmark_aead)
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 4/5 lib/crypto: tests: Add KUnit test suite for AES-CCM
In reply to Eric BiggersAdd a KUnit test suite for the AES-CCM library API.
It consists of:
- All the shared test cases from aead-test-template.h. These include extensive consistency tests, a "Monte-Carlo test", and a benchmark.
- Tests against hardcoded AES-CCM test vectors from external sources.
- Tests for CCM-specific message length validation.
To generate the expected aes_ccm_monte_carlo_checksum[] value, add a script gen-aead-testvecs.py which computes it using python-cryptography.
--- lib/crypto/.kunitconfig | 1 + lib/crypto/tests/Kconfig | 9 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/aes_ccm_kunit.c | 356 ++++++++++++++++++++++++++++ scripts/crypto/gen-aead-testvecs.py | 60 +++++ 5 files changed, 427 insertions(+) create mode 100644 lib/crypto/tests/aes_ccm_kunit.c create mode 100755 scripts/crypto/gen-aead-testvecs.py
lib/crypto/.kunitconfig +1 -0
@@ -3,6 +3,7 @@ CONFIG_KUNIT=y 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_BLAKE2B_KUNIT_TEST=y CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST=y
lib/crypto/tests/Kconfig +9 -0
@@ -9,6 +9,15 @@ config CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST KUnit tests for the AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC message authentication codes. +config CRYPTO_LIB_AES_CCM_KUNIT_TEST + tristate "KUnit tests for AES-CCM" if !KUNIT_ALL_TESTS + depends on KUNIT && CRYPTO_LIB_AES_CCM + 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-CCM authenticated encryption algorithm. + config CRYPTO_LIB_BLAKE2B_KUNIT_TEST tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_BLAKE2B
lib/crypto/tests/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later 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_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST) += chacha20poly1305_kunit.o
lib/crypto/tests/aes_ccm_kunit.c new file +356 -0
356 lines not on this page · raw message
scripts/crypto/gen-aead-testvecs.py new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.55.0
Eric Biggers
maintainerPATCH v2 5/5 lib/crypto: tests: Add KUnit test suite for AES-GCM
In reply to Eric BiggersAdd a KUnit test suite for the AES-GCM library API.
It consists of:
- All the shared test cases from aead-test-template.h. These include extensive consistency tests, a "Monte-Carlo test", and a benchmark.
- Tests against hardcoded AES-GCM test vectors from external sources.
--- lib/crypto/.kunitconfig | 1 + lib/crypto/tests/Kconfig | 9 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/aes_gcm_kunit.c | 472 ++++++++++++++++++++++++++++ scripts/crypto/gen-aead-testvecs.py | 13 +- 5 files changed, 494 insertions(+), 2 deletions(-) create mode 100644 lib/crypto/tests/aes_gcm_kunit.c
lib/crypto/.kunitconfig +1 -0
@@ -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_GCM_KUNIT_TEST=y CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST=y CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST=y
lib/crypto/tests/Kconfig +9 -0
@@ -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_GCM_KUNIT_TEST + tristate "KUnit tests for AES-GCM" if !KUNIT_ALL_TESTS + depends on KUNIT && CRYPTO_LIB_AES_GCM + 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-GCM authenticated encryption algorithm. + config CRYPTO_LIB_BLAKE2B_KUNIT_TEST tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS depends on KUNIT && CRYPTO_LIB_BLAKE2B
lib/crypto/tests/Makefile +1 -0
@@ -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_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 obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST) += chacha20poly1305_kunit.o
lib/crypto/tests/aes_gcm_kunit.c new file +472 -0
472 lines not on this page · raw message
scripts/crypto/gen-aead-testvecs.py +11 -2
@@ -41,6 +41,15 @@ def gen_monte_carlo_checksum(alg): key, tag_length=tag_len ) ct_and_tag = ccm.encrypt(nonce, pt, ad) + elif alg == "aes-gcm": + key_len = [16, 24, 32][data_len % 3] + key = rand_bytes(key_len) + nonce = rand_bytes(12) + tag_len = [4, 8, 12, 13, 14, 15, 16][data_len % 7] + gcm = cryptography.hazmat.primitives.ciphers.aead.AESGCM(key) + # python-cryptography supports only 16-byte GCM tags. However, in + # GCM, shorter tags are simply truncated. Do that below. + ct_and_tag = gcm.encrypt(nonce, pt, ad)[: data_len + tag_len] blake2s.update(ct_and_tag)
@@ -53,8 +62,8 @@ def gen_monte_carlo_checksum(alg): print("};") -if len(sys.argv) != 2 or sys.argv[1] not in ("aes-ccm"): - sys.stderr.write("Usage: gen-aead-testvecs.py [aes-ccm]\n") +if len(sys.argv) != 2 or sys.argv[1] not in ("aes-ccm", "aes-gcm"): + sys.stderr.write("Usage: gen-aead-testvecs.py [aes-ccm|aes-gcm]\n") sys.exit(1) gen_monte_carlo_checksum(sys.argv[1])
Signature · 1 line
2.55.0
Quoted 16 lines · Eric Biggers
This series applies to libcrypto-next. It can also be retrieved from:
git fetch
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git
gcm-ccm-kunit-v2This series adds comprehensive KUnit test suites for the AES-CCM and AES-GCM library APIs.
This is a follow-up to the series
"[PATCH v2 00/13] Library APIs for AES encryption modes"
(https://lore.kernel.org/linux-crypto/[email protected]/)Changed in v2:
- Fixed crash intest_aes_ccm_data_len_too_large()on 32-bit kernels.
Quoted 7 lines
Eric Biggers (5):
lib/crypto: tests: Createtest-utils.hlib/crypto: tests: Use per-test-case buffers in hash tests lib/crypto: tests: Addaead-test-template.hlib/crypto: tests: Add KUnit test suite for AES-CCM lib/crypto: tests: Add KUnit test suite for AES-GCM
Quoted 14 lines · Eric Biggers
This series applies to libcrypto-next. It can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git gcm-ccm-kunit-v2
This series adds comprehensive KUnit test suites for the AES-CCM and AES-GCM library APIs.
This is a follow-up to the series
"[PATCH v2 00/13] Library APIs for AES encryption modes"
(https://lore.kernel.org/linux-crypto/[email protected]/)Changed in v2:
- Fixed crash intest_aes_ccm_data_len_too_large()on 32-bit kernels.
Quoted 6 lines
Eric Biggers (5):
lib/crypto: tests: Createtest-utils.hlib/crypto: tests: Use per-test-case buffers in hash tests lib/crypto: tests: Addaead-test-template.hlib/crypto: tests: Add KUnit test suite for AES-CCM lib/crypto: tests: Add KUnit test suite for AES-GCM
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next
- Eric