Skip to contentSkip to conversation

lib/crypto: tests: Migrate ChaCha20Poly1305 self-test to KUnit

PATCH Applied Eric Biggers 2 participants 3 messages Mar 27 – Mar 30

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH 1/1 lib/crypto: tests: Migrate ChaCha20Poly1305 self-test to KUnit

Move the ChaCha20Poly1305 test from an ad-hoc self-test to a KUnit test.

Keep the same test logic for now, just translated to KUnit.

Moving to KUnit has multiple benefits, such as:

- Consistency with the rest of the lib/crypto/ tests.

- Kernel developers familiar with KUnit, which is used kernel-wide, can quickly understand the test and how to enable and run it.

- The test will be automatically run by anyone using lib/crypto/.kunitconfig or KUnit's all_tests.config.

- Results are reported using the standard KUnit mechanism.

- It eliminates one of the few remaining back-references to crypto/ from lib/crypto/, specifically a reference to CONFIG_CRYPTO_SELFTESTS.

---
 include/crypto/chacha20poly1305.h             |    2 -
 lib/crypto/.kunitconfig                       |    1 +
 lib/crypto/Makefile                           |    1 -
 lib/crypto/chacha20poly1305.c                 |   14 -
 lib/crypto/tests/Kconfig                      |   10 +
 lib/crypto/tests/Makefile                     |    1 +
 .../chacha20poly1305_kunit.c}                 | 1493 +++++++++--------
 7 files changed, 760 insertions(+), 762 deletions(-)
 rename lib/crypto/{chacha20poly1305-selftest.c => tests/chacha20poly1305_kunit.c} (91%)
include/crypto/chacha20poly1305.h +0 -2
index 0f71b037702d..0f6d99170aaf 100644
@@ -44,8 +44,6 @@ bool chacha20poly1305_encrypt_sg_inplace(struct scatterlist *src, size_t src_len
 bool chacha20poly1305_decrypt_sg_inplace(struct scatterlist *src, size_t src_len,
 					 const u8 *ad, const size_t ad_len,
 					 const u64 nonce,
 					 const u8 key[at_least CHACHA20POLY1305_KEY_SIZE]);
 
-bool chacha20poly1305_selftest(void);
-
 #endif /* __CHACHA20POLY1305_H */
lib/crypto/.kunitconfig +1 -0
index f8a3c6e6367c..3efc854a2c08 100644
@@ -3,10 +3,11 @@ CONFIG_KUNIT=y
 CONFIG_CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT=y
 
 CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST=y
+CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_GHASH_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST=y
 CONFIG_CRYPTO_LIB_NH_KUNIT_TEST=y
lib/crypto/Makefile +0 -1
index ec1747f51d07..200f8bc6db52 100644
@@ -120,11 +120,10 @@ endif # CONFIG_CRYPTO_LIB_CHACHA_ARCH
 
 ################################################################################
 
 obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305)	+= libchacha20poly1305.o
 libchacha20poly1305-y				+= chacha20poly1305.o
-libchacha20poly1305-$(CONFIG_CRYPTO_SELFTESTS)	+= chacha20poly1305-selftest.o
 
 ################################################################################
 
 obj-$(CONFIG_CRYPTO_LIB_CURVE25519) += libcurve25519.o
 libcurve25519-y := curve25519.o
lib/crypto/chacha20poly1305.c +0 -14
index 212ce33562af..ea42a28f4ff7 100644
@@ -354,22 +354,8 @@ bool chacha20poly1305_decrypt_sg_inplace(struct scatterlist *src, size_t src_len
 						 src_len - POLY1305_DIGEST_SIZE,
 						 ad, ad_len, nonce, key, 0);
 }
 EXPORT_SYMBOL(chacha20poly1305_decrypt_sg_inplace);
 
-static int __init chacha20poly1305_init(void)
-{
-	if (IS_ENABLED(CONFIG_CRYPTO_SELFTESTS) &&
-	    WARN_ON(!chacha20poly1305_selftest()))
-		return -ENODEV;
-	return 0;
-}
-
-static void __exit chacha20poly1305_exit(void)
-{
-}
-
-module_init(chacha20poly1305_init);
-module_exit(chacha20poly1305_exit);
 MODULE_LICENSE("GPL v2");
 MODULE_DESCRIPTION("ChaCha20Poly1305 AEAD construction");
 MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>");
lib/crypto/tests/Kconfig +10 -0
index 7a5ad109aefc..9409c1a935c3 100644
@@ -25,10 +25,19 @@ config CRYPTO_LIB_BLAKE2S_KUNIT_TEST
 	# No need to depend on CRYPTO_LIB_BLAKE2S here, as that option doesn't
 	# exist; the BLAKE2s code is always built-in for the /dev/random driver.
 	help
 	  KUnit tests for the BLAKE2s cryptographic hash function.
 
+config CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST
+	tristate "KUnit tests for ChaCha20Poly1305" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_LIB_CHACHA20POLY1305
+	default KUNIT_ALL_TESTS
+	select CRYPTO_LIB_BENCHMARK_VISIBLE
+	help
+	  KUnit tests for the ChaCha20Poly1305 authenticated encryption
+	  algorithm.
+
 config CRYPTO_LIB_CURVE25519_KUNIT_TEST
 	tristate "KUnit tests for Curve25519" if !KUNIT_ALL_TESTS
 	depends on KUNIT && CRYPTO_LIB_CURVE25519
 	default KUNIT_ALL_TESTS
 	select CRYPTO_LIB_BENCHMARK_VISIBLE
@@ -135,10 +144,11 @@ config CRYPTO_LIB_SM3_KUNIT_TEST
 config CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT
 	tristate "Enable all crypto library code for KUnit tests"
 	depends on KUNIT
 	select CRYPTO_LIB_AES_CBC_MACS
 	select CRYPTO_LIB_BLAKE2B
+	select CRYPTO_LIB_CHACHA20POLY1305
 	select CRYPTO_LIB_CURVE25519
 	select CRYPTO_LIB_GF128HASH
 	select CRYPTO_LIB_MD5
 	select CRYPTO_LIB_MLDSA
 	select CRYPTO_LIB_NH
lib/crypto/tests/Makefile +1 -0
index ad1cbb88132f..a739413500b6 100644
@@ -1,10 +1,11 @@
 # 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_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
 obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_GHASH_KUNIT_TEST) += ghash_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST) += mldsa_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_NH_KUNIT_TEST) += nh_kunit.o
lib/crypto/tests/chacha20poly1305_kunit.c renamed from lib/crypto/chacha20poly1305-selftest.c +748 -745

2,907 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

base-commit: 7ac21b4032e5b9b8a6a312b6f1d54f4ba24d1c16

Signature · 1 line
2.53.0

Part of the diff is not shown here. View the raw message.

maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 30 lines · Eric Biggers

Move the ChaCha20Poly1305 test from an ad-hoc self-test to a KUnit test.

Keep the same test logic for now, just translated to KUnit.

Moving to KUnit has multiple benefits, such as:

- Consistency with the rest of the lib/crypto/ tests.

- Kernel developers familiar with KUnit, which is used kernel-wide, can quickly understand the test and how to enable and run it.

- The test will be automatically run by anyone using lib/crypto/.kunitconfig or KUnit's all_tests.config.

- Results are reported using the standard KUnit mechanism.

- It eliminates one of the few remaining back-references to crypto/ from lib/crypto/, specifically a reference to CONFIG_CRYPTO_SELFTESTS.

---
 include/crypto/chacha20poly1305.h             |    2 -
 lib/crypto/.kunitconfig                       |    1 +
 lib/crypto/Makefile                           |    1 -
 lib/crypto/chacha20poly1305.c                 |   14 -
 lib/crypto/tests/Kconfig                      |   10 +
 lib/crypto/tests/Makefile                     |    1 +
 .../chacha20poly1305_kunit.c}                 | 1493 +++++++++--------
 7 files changed, 760 insertions(+), 762 deletions(-)
 rename lib/crypto/{chacha20poly1305-selftest.c => 
Quoted 2 lines

tests/chacha20poly1305_kunit.c} (91%)

author maintainer Applied
#2 lore raw
In reply to Eric Biggers
Quoted 20 lines · Eric Biggers

Move the ChaCha20Poly1305 test from an ad-hoc self-test to a KUnit test.

Keep the same test logic for now, just translated to KUnit.

Moving to KUnit has multiple benefits, such as:

- Consistency with the rest of the lib/crypto/ tests.

- Kernel developers familiar with KUnit, which is used kernel-wide, can quickly understand the test and how to enable and run it.

- The test will be automatically run by anyone using lib/crypto/.kunitconfig or KUnit's all_tests.config.

- Results are reported using the standard KUnit mechanism.

- It eliminates one of the few remaining back-references to crypto/ from lib/crypto/, specifically a reference to CONFIG_CRYPTO_SELFTESTS.

Quoted 10 lines
---
 include/crypto/chacha20poly1305.h             |    2 -
 lib/crypto/.kunitconfig                       |    1 +
 lib/crypto/Makefile                           |    1 -
 lib/crypto/chacha20poly1305.c                 |   14 -
 lib/crypto/tests/Kconfig                      |   10 +
 lib/crypto/tests/Makefile                     |    1 +
 .../chacha20poly1305_kunit.c}                 | 1493 +++++++++--------
 7 files changed, 760 insertions(+), 762 deletions(-)
 rename lib/crypto/{chacha20poly1305-selftest.c => tests/chacha20poly1305_kunit.c} (91%)

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next

- Eric