Skip to contentSkip to conversation

KUnit tests for SHA-2 library functions

PATCH New Eric Biggers 1 participant 3 messages Jun 30, 2025

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

v3 cover KUnit tests for SHA-2 library functions

This series is also available at:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git sha2-kunit-v3

This series adds KUnit tests for the SHA-2 library functions.

Most of this code was originally part of my series
"SHA-512 library functions". I made a few updates in "SHA-256 library improvements". As per the request from Linus that the tests be kept last on the branch for a separate pull request, I've reordered the history to add the tests last. Thus this patch series.

Testing strategy:
- Each SHA variant gets its own KUnit test suite, but a header is used to share most of the test code among the SHA variants.
- Test against vectors generated by the Python hashlib and hmac modules.
- Test incremental computation.
- Test with a guard page to catch buffer overruns even in assembly code.
- Test various overlap and alignment cases.
- Compute hashes in task, softirq, and hardirq context in parallel, to verify that the functions work as expected in all contexts and that fallback code paths are exercised.
- Test that the finalization functions zeroize their context.
- Include benchmarks, guarded by a separate Kconfig option.

Eric Biggers (2):
lib/crypto: sha512: Add KUnit tests for SHA-384 and SHA-512 lib/crypto: sha256: Add KUnit tests for SHA-224 and SHA-256

 lib/crypto/Kconfig                    |   2 +
 lib/crypto/Makefile                   |   2 +
 lib/crypto/tests/Kconfig              |  25 +
 lib/crypto/tests/Makefile             |   6 +
 lib/crypto/tests/hash-test-template.h | 512 ++++++++++++++++++++
 lib/crypto/tests/sha224-testvecs.h    | 470 ++++++++++++++++++
 lib/crypto/tests/sha224_kunit.c       |  48 ++
 lib/crypto/tests/sha256-testvecs.h    | 470 ++++++++++++++++++
 lib/crypto/tests/sha256_kunit.c       |  48 ++
 lib/crypto/tests/sha384-testvecs.h    | 566 ++++++++++++++++++++++
 lib/crypto/tests/sha384_kunit.c       |  48 ++
 lib/crypto/tests/sha512-testvecs.h    | 662 ++++++++++++++++++++++++++
 lib/crypto/tests/sha512_kunit.c       |  48 ++
 scripts/crypto/gen-hash-testvecs.py   |  83 ++++
 14 files changed, 2990 insertions(+)
 create mode 100644 lib/crypto/tests/Kconfig
 create mode 100644 lib/crypto/tests/Makefile
 create mode 100644 lib/crypto/tests/hash-test-template.h
 create mode 100644 lib/crypto/tests/sha224-testvecs.h
 create mode 100644 lib/crypto/tests/sha224_kunit.c
 create mode 100644 lib/crypto/tests/sha256-testvecs.h
 create mode 100644 lib/crypto/tests/sha256_kunit.c
 create mode 100644 lib/crypto/tests/sha384-testvecs.h
 create mode 100644 lib/crypto/tests/sha384_kunit.c
 create mode 100644 lib/crypto/tests/sha512-testvecs.h
 create mode 100644 lib/crypto/tests/sha512_kunit.c
 create mode 100755 scripts/crypto/gen-hash-testvecs.py
Signature · 1 line
2.50.0
author maintainer
#1 lore raw

PATCH v3 1/2 lib/crypto: sha512: Add KUnit tests for SHA-384 and SHA-512

Add KUnit tests for the SHA-384 and SHA-512 library functions, including the corresponding HMAC support.

Testing strategy:
- Each SHA variant gets its own KUnit test suite, but a header is used to share most of the test code among the SHA variants.
- Test against vectors generated by the Python hashlib and hmac modules.
- Test incremental computation.
- Test with a guard page to catch buffer overruns even in assembly code.
- Test various overlap and alignment cases.
- Compute hashes in task, softirq, and hardirq context in parallel, to verify that the functions work as expected in all contexts and that fallback code paths are exercised.
- Test that the finalization functions zeroize their context.
- Include benchmarks, guarded by a separate Kconfig option.

---
 lib/crypto/Kconfig                    |   2 +
 lib/crypto/Makefile                   |   2 +
 lib/crypto/tests/Kconfig              |  16 +
 lib/crypto/tests/Makefile             |   4 +
 lib/crypto/tests/hash-test-template.h | 512 ++++++++++++++++++++
 lib/crypto/tests/sha384-testvecs.h    | 566 ++++++++++++++++++++++
 lib/crypto/tests/sha384_kunit.c       |  48 ++
 lib/crypto/tests/sha512-testvecs.h    | 662 ++++++++++++++++++++++++++
 lib/crypto/tests/sha512_kunit.c       |  48 ++
 scripts/crypto/gen-hash-testvecs.py   |  83 ++++
 10 files changed, 1943 insertions(+)
 create mode 100644 lib/crypto/tests/Kconfig
 create mode 100644 lib/crypto/tests/Makefile
 create mode 100644 lib/crypto/tests/hash-test-template.h
 create mode 100644 lib/crypto/tests/sha384-testvecs.h
 create mode 100644 lib/crypto/tests/sha384_kunit.c
 create mode 100644 lib/crypto/tests/sha512-testvecs.h
 create mode 100644 lib/crypto/tests/sha512_kunit.c
 create mode 100755 scripts/crypto/gen-hash-testvecs.py
lib/crypto/Kconfig +2 -0
index 3305c69085816..2580bc90c15e2 100644
@@ -1,9 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0
 
 menu "Crypto library routines"
 
+source "lib/crypto/tests/Kconfig"
+
 config CRYPTO_LIB_UTILS
 	tristate
 
 config CRYPTO_LIB_AES
 	tristate
lib/crypto/Makefile +2 -0
index a887bf103bf05..d92a7ba2ef0a0 100644
@@ -1,7 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 
+obj-y += tests/
+
 aflags-thumb2-$(CONFIG_THUMB2_KERNEL)  := -U__thumb2__ -D__thumb2__=1
 
 quiet_cmd_perlasm = PERLASM $@
       cmd_perlasm = $(PERL) $(<) > $(@)
 
lib/crypto/tests/Kconfig new file +16 -0
new file mode 100644
index 0000000000000..90be320c25bd2
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config CRYPTO_LIB_SHA512_KUNIT_TEST
+	tristate "KUnit tests for SHA-384 and SHA-512" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
+	select CRYPTO_LIB_SHA512
+	help
+	  KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions
+	  and their corresponding HMACs.
+
+config CRYPTO_LIB_BENCHMARK
+	bool "Include benchmarks in KUnit tests for cryptographic functions"
+	depends on CRYPTO_LIB_SHA512_KUNIT_TEST
+	help
+	  Include benchmarks in the KUnit tests for cryptographic functions.
lib/crypto/tests/Makefile new file +4 -0
new file mode 100644
index 0000000000000..3925dcb6513d8
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha512_kunit.o
lib/crypto/tests/hash-test-template.h new file +512 -0

512 lines not on this page · raw message

lib/crypto/tests/sha384-testvecs.h new file +566 -0

566 lines not on this page · raw message

lib/crypto/tests/sha384_kunit.c new file +48 -0

48 lines not on this page · raw message

lib/crypto/tests/sha512-testvecs.h new file +662 -0

662 lines not on this page · raw message

lib/crypto/tests/sha512_kunit.c new file +48 -0

48 lines not on this page · raw message

scripts/crypto/gen-hash-testvecs.py new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.50.0
author maintainer
#2 lore raw

PATCH v3 2/2 lib/crypto: sha256: Add KUnit tests for SHA-224 and SHA-256

In reply to Eric Biggers

Add KUnit tests for the SHA-224 and SHA-256 library functions, including the corresponding HMAC support. This uses the test template that was added by the previous commit.

---
 lib/crypto/tests/Kconfig           |  11 +-
 lib/crypto/tests/Makefile          |   2 +
 lib/crypto/tests/sha224-testvecs.h | 470 +++++++++++++++++++++++++++++
 lib/crypto/tests/sha224_kunit.c    |  48 +++
 lib/crypto/tests/sha256-testvecs.h | 470 +++++++++++++++++++++++++++++
 lib/crypto/tests/sha256_kunit.c    |  48 +++
 6 files changed, 1048 insertions(+), 1 deletion(-)
 create mode 100644 lib/crypto/tests/sha224-testvecs.h
 create mode 100644 lib/crypto/tests/sha224_kunit.c
 create mode 100644 lib/crypto/tests/sha256-testvecs.h
 create mode 100644 lib/crypto/tests/sha256_kunit.c
lib/crypto/tests/Kconfig +10 -1
index 90be320c25bd2..d9844725d3d07 100644
@@ -1,7 +1,16 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+config CRYPTO_LIB_SHA256_KUNIT_TEST
+	tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
+	select CRYPTO_LIB_SHA256
+	help
+	  KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions
+	  and their corresponding HMACs.
+
 config CRYPTO_LIB_SHA512_KUNIT_TEST
 	tristate "KUnit tests for SHA-384 and SHA-512" if !KUNIT_ALL_TESTS
 	depends on KUNIT
 	default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
 	select CRYPTO_LIB_SHA512
@@ -9,8 +18,8 @@ config CRYPTO_LIB_SHA512_KUNIT_TEST
 	  KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions
 	  and their corresponding HMACs.
 
 config CRYPTO_LIB_BENCHMARK
 	bool "Include benchmarks in KUnit tests for cryptographic functions"
-	depends on CRYPTO_LIB_SHA512_KUNIT_TEST
+	depends on CRYPTO_LIB_SHA256_KUNIT_TEST || CRYPTO_LIB_SHA512_KUNIT_TEST
 	help
 	  Include benchmarks in the KUnit tests for cryptographic functions.
lib/crypto/tests/Makefile +2 -0
index 3925dcb6513d8..95bb919aff6b4 100644
@@ -1,4 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha256_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha512_kunit.o
lib/crypto/tests/sha224-testvecs.h new file +470 -0

470 lines not on this page · raw message

lib/crypto/tests/sha224_kunit.c new file +48 -0

48 lines not on this page · raw message

lib/crypto/tests/sha256-testvecs.h new file +470 -0

470 lines not on this page · raw message

lib/crypto/tests/sha256_kunit.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.50.0