Skip to content

Commit 4dcf6ca

Browse files
author
Eric Biggers
committed
lib/crypto: tests: Add KUnit tests for SHA-224 and SHA-256
Add KUnit test suites for the SHA-224 and SHA-256 library functions, including the corresponding HMAC support. The core test logic is in the previously-added hash-test-template.h. This commit just adds the actual KUnit suites, and it adds the generated test vectors to the tree so that gen-hash-testvecs.py won't have to be run at build time. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250709200112.258500-3-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 950a812 commit 4dcf6ca

8 files changed

Lines changed: 590 additions & 0 deletions

File tree

lib/crypto/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ config CRYPTO_LIB_SHA512_ARCH
194194
config CRYPTO_LIB_SM3
195195
tristate
196196

197+
source "lib/crypto/tests/Kconfig"
198+
197199
if !KMSAN # avoid false positives from assembly
198200
if ARM
199201
source "lib/crypto/arm/Kconfig"

lib/crypto/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ quiet_cmd_perlasm = PERLASM $@
88
quiet_cmd_perlasm_with_args = PERLASM $@
99
cmd_perlasm_with_args = $(PERL) $(<) void $(@)
1010

11+
obj-$(CONFIG_KUNIT) += tests/
12+
1113
obj-$(CONFIG_CRYPTO_HASH_INFO) += hash_info.o
1214

1315
obj-$(CONFIG_CRYPTO_LIB_UTILS) += libcryptoutils.o

lib/crypto/tests/Kconfig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
# Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are
4+
# included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256).
5+
config CRYPTO_LIB_SHA256_KUNIT_TEST
6+
tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS
7+
depends on KUNIT
8+
default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
9+
select CRYPTO_LIB_BENCHMARK_VISIBLE
10+
select CRYPTO_LIB_SHA256
11+
help
12+
KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions
13+
and their corresponding HMACs.
14+
15+
config CRYPTO_LIB_BENCHMARK_VISIBLE
16+
bool
17+
18+
config CRYPTO_LIB_BENCHMARK
19+
bool "Include benchmarks in KUnit tests for cryptographic functions"
20+
depends on CRYPTO_LIB_BENCHMARK_VISIBLE
21+
help
22+
Include benchmarks in the KUnit tests for cryptographic functions.
23+
The benchmark results are printed to the kernel log when the
24+
corresponding KUnit test suite runs.
25+
26+
This is useful for evaluating the performance of the cryptographic
27+
functions. However, it will increase the runtime of the KUnit tests.
28+
29+
If you're only interested in correctness testing, leave this disabled.

lib/crypto/tests/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o

lib/crypto/tests/sha224-testvecs.h

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha224 */
3+
4+
static const struct {
5+
size_t data_len;
6+
u8 digest[SHA224_DIGEST_SIZE];
7+
} hash_testvecs[] = {
8+
{
9+
.data_len = 0,
10+
.digest = {
11+
0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9,
12+
0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
13+
0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a,
14+
0xc5, 0xb3, 0xe4, 0x2f,
15+
},
16+
},
17+
{
18+
.data_len = 1,
19+
.digest = {
20+
0xe3, 0x4d, 0x79, 0x17, 0x75, 0x35, 0xdc, 0xd2,
21+
0x27, 0xc9, 0x9d, 0x0b, 0x90, 0x0f, 0x21, 0x5d,
22+
0x95, 0xfb, 0x9c, 0x6d, 0xa8, 0xec, 0x19, 0x15,
23+
0x12, 0xef, 0xf5, 0x0f,
24+
},
25+
},
26+
{
27+
.data_len = 2,
28+
.digest = {
29+
0x81, 0xc7, 0x60, 0x0d, 0x6d, 0x13, 0x75, 0x70,
30+
0x4b, 0xc0, 0xab, 0xea, 0x04, 0xe3, 0x78, 0x7e,
31+
0x73, 0xb9, 0x0f, 0xb6, 0xae, 0x90, 0xf3, 0x94,
32+
0xb2, 0x56, 0xda, 0xc8,
33+
},
34+
},
35+
{
36+
.data_len = 3,
37+
.digest = {
38+
0x24, 0xf0, 0x8c, 0x6e, 0x9d, 0xd6, 0x06, 0x80,
39+
0x0a, 0x03, 0xee, 0x9b, 0x33, 0xec, 0x83, 0x42,
40+
0x2c, 0x8b, 0xe7, 0xc7, 0xc6, 0x04, 0xfb, 0xc6,
41+
0xa3, 0x3a, 0x4d, 0xc9,
42+
},
43+
},
44+
{
45+
.data_len = 16,
46+
.digest = {
47+
0x1c, 0x08, 0xa8, 0x55, 0x8f, 0xc6, 0x0a, 0xea,
48+
0x2f, 0x1b, 0x54, 0xff, 0x8d, 0xd2, 0xa3, 0xc7,
49+
0x42, 0xc2, 0x93, 0x3d, 0x73, 0x18, 0x84, 0xba,
50+
0x75, 0x49, 0x34, 0xfd,
51+
},
52+
},
53+
{
54+
.data_len = 32,
55+
.digest = {
56+
0x45, 0xdd, 0xb5, 0xf0, 0x3c, 0xda, 0xe6, 0xd4,
57+
0x6c, 0x86, 0x91, 0x29, 0x11, 0x2f, 0x88, 0x7d,
58+
0xd8, 0x3c, 0xa3, 0xd6, 0xdd, 0x1e, 0xac, 0x98,
59+
0xff, 0xf0, 0x14, 0x69,
60+
},
61+
},
62+
{
63+
.data_len = 48,
64+
.digest = {
65+
0x0b, 0xfb, 0x71, 0x4c, 0x06, 0x7a, 0xd5, 0x89,
66+
0x76, 0x0a, 0x43, 0x8b, 0x2b, 0x47, 0x12, 0x56,
67+
0xa7, 0x64, 0x33, 0x1d, 0xd3, 0x44, 0x17, 0x95,
68+
0x23, 0xe7, 0x53, 0x01,
69+
},
70+
},
71+
{
72+
.data_len = 49,
73+
.digest = {
74+
0xc4, 0xae, 0x9c, 0x33, 0xd5, 0x1d, 0xf4, 0xa7,
75+
0xfd, 0xb7, 0xd4, 0x6b, 0xc3, 0xeb, 0xa8, 0xbf,
76+
0xfb, 0x07, 0x89, 0x4b, 0x07, 0x15, 0x22, 0xec,
77+
0xe1, 0x45, 0x84, 0xba,
78+
},
79+
},
80+
{
81+
.data_len = 63,
82+
.digest = {
83+
0xad, 0x01, 0x34, 0x2a, 0xe2, 0x3b, 0x58, 0x06,
84+
0x9f, 0x20, 0xc8, 0xfb, 0xf3, 0x20, 0x82, 0xa6,
85+
0x9f, 0xee, 0x7a, 0xbe, 0xdf, 0xf3, 0x5d, 0x57,
86+
0x9b, 0xce, 0x79, 0x96,
87+
},
88+
},
89+
{
90+
.data_len = 64,
91+
.digest = {
92+
0xa7, 0xa6, 0x47, 0xf7, 0xed, 0x2a, 0xe5, 0xe3,
93+
0xc0, 0x1e, 0x7b, 0x40, 0xe4, 0xf7, 0x40, 0x65,
94+
0x42, 0xc1, 0x6f, 0x7d, 0x8d, 0x0d, 0x17, 0x4f,
95+
0xd3, 0xbc, 0x0d, 0x85,
96+
},
97+
},
98+
{
99+
.data_len = 65,
100+
.digest = {
101+
0xc4, 0x9c, 0xb5, 0x6a, 0x01, 0x2d, 0x10, 0xa9,
102+
0x5f, 0xa4, 0x5a, 0xe1, 0xba, 0x40, 0x12, 0x09,
103+
0x7b, 0xea, 0xdb, 0xa6, 0x7b, 0xcb, 0x56, 0xf0,
104+
0xfd, 0x5b, 0xe2, 0xe7,
105+
},
106+
},
107+
{
108+
.data_len = 127,
109+
.digest = {
110+
0x14, 0xda, 0x0e, 0x01, 0xca, 0x78, 0x7d, 0x2d,
111+
0x85, 0xa3, 0xca, 0x0e, 0x80, 0xf9, 0x95, 0x10,
112+
0xa1, 0x7b, 0xa5, 0xaa, 0xfc, 0x95, 0x05, 0x08,
113+
0x53, 0xda, 0x52, 0xee,
114+
},
115+
},
116+
{
117+
.data_len = 128,
118+
.digest = {
119+
0xa5, 0x24, 0xc4, 0x54, 0xe1, 0x50, 0xab, 0xee,
120+
0x22, 0xc1, 0xa7, 0x27, 0x15, 0x2c, 0x6f, 0xf7,
121+
0x4c, 0x31, 0xe5, 0x15, 0x25, 0x4e, 0x71, 0xc6,
122+
0x7e, 0xa0, 0x11, 0x5d,
123+
},
124+
},
125+
{
126+
.data_len = 129,
127+
.digest = {
128+
0x73, 0xd0, 0x8c, 0xce, 0xed, 0xed, 0x9f, 0xaa,
129+
0x21, 0xaf, 0xa2, 0x08, 0x80, 0x16, 0x15, 0x59,
130+
0x3f, 0x1d, 0x7f, 0x0a, 0x79, 0x3d, 0x7b, 0x58,
131+
0xf8, 0xc8, 0x5c, 0x27,
132+
},
133+
},
134+
{
135+
.data_len = 256,
136+
.digest = {
137+
0x31, 0xa7, 0xa1, 0xca, 0x49, 0x72, 0x75, 0xcc,
138+
0x6e, 0x02, 0x9e, 0xad, 0xea, 0x86, 0x5c, 0x91,
139+
0x02, 0xe4, 0xc9, 0xf9, 0xd3, 0x9e, 0x74, 0x50,
140+
0xd8, 0x43, 0x6b, 0x85,
141+
},
142+
},
143+
{
144+
.data_len = 511,
145+
.digest = {
146+
0x40, 0x60, 0x8b, 0xb0, 0x03, 0xa9, 0x75, 0xab,
147+
0x2d, 0x5b, 0x20, 0x9a, 0x05, 0x72, 0xb7, 0xa8,
148+
0xce, 0xf2, 0x4f, 0x66, 0x62, 0xe3, 0x7e, 0x24,
149+
0xd6, 0xe2, 0xea, 0xfa,
150+
},
151+
},
152+
{
153+
.data_len = 513,
154+
.digest = {
155+
0x4f, 0x5f, 0x9f, 0x1e, 0xb3, 0x66, 0x81, 0xdb,
156+
0x41, 0x5d, 0x65, 0x97, 0x00, 0x8d, 0xdc, 0x62,
157+
0x03, 0xb0, 0x4d, 0x6b, 0x5c, 0x7f, 0x1e, 0xa0,
158+
0xfe, 0xfc, 0x0e, 0xb8,
159+
},
160+
},
161+
{
162+
.data_len = 1000,
163+
.digest = {
164+
0x08, 0xa8, 0xa1, 0xc0, 0xd8, 0xf9, 0xb4, 0xaa,
165+
0x53, 0x22, 0xa1, 0x73, 0x0b, 0x45, 0xa0, 0x20,
166+
0x72, 0xf3, 0xa9, 0xbc, 0x51, 0xd0, 0x20, 0x79,
167+
0x69, 0x97, 0xf7, 0xe3,
168+
},
169+
},
170+
{
171+
.data_len = 3333,
172+
.digest = {
173+
0xe8, 0x60, 0x5f, 0xb9, 0x12, 0xe1, 0x6b, 0x24,
174+
0xc5, 0xe8, 0x43, 0xa9, 0x5c, 0x3f, 0x65, 0xed,
175+
0xbe, 0xfd, 0x77, 0xf5, 0x47, 0xf2, 0x75, 0x21,
176+
0xc2, 0x8f, 0x54, 0x8f,
177+
},
178+
},
179+
{
180+
.data_len = 4096,
181+
.digest = {
182+
0xc7, 0xdf, 0x50, 0x16, 0x10, 0x01, 0xb7, 0xdf,
183+
0x34, 0x1d, 0x18, 0xa2, 0xd5, 0xad, 0x1f, 0x50,
184+
0xf7, 0xa8, 0x9a, 0x72, 0xfb, 0xfd, 0xd9, 0x1c,
185+
0x57, 0xac, 0x08, 0x97,
186+
},
187+
},
188+
{
189+
.data_len = 4128,
190+
.digest = {
191+
0xdf, 0x16, 0x76, 0x7f, 0xc0, 0x16, 0x84, 0x63,
192+
0xac, 0xcf, 0xd0, 0x78, 0x1e, 0x96, 0x67, 0xc5,
193+
0x3c, 0x06, 0xe9, 0xdb, 0x6e, 0x7d, 0xd0, 0x07,
194+
0xaa, 0xb1, 0x56, 0xc9,
195+
},
196+
},
197+
{
198+
.data_len = 4160,
199+
.digest = {
200+
0x49, 0xec, 0x5c, 0x18, 0xd7, 0x5b, 0xda, 0xed,
201+
0x5b, 0x59, 0xde, 0x09, 0x34, 0xb2, 0x49, 0x43,
202+
0x62, 0x6a, 0x0a, 0x63, 0x6a, 0x51, 0x08, 0x37,
203+
0x8c, 0xb6, 0x29, 0x84,
204+
},
205+
},
206+
{
207+
.data_len = 4224,
208+
.digest = {
209+
0x3d, 0xc2, 0xc8, 0x43, 0xcf, 0xb7, 0x33, 0x14,
210+
0x04, 0x93, 0xed, 0xe2, 0xcd, 0x8a, 0x69, 0x5c,
211+
0x5a, 0xd5, 0x9b, 0x52, 0xdf, 0x48, 0xa7, 0xaa,
212+
0x28, 0x2b, 0x5d, 0x27,
213+
},
214+
},
215+
{
216+
.data_len = 16384,
217+
.digest = {
218+
0xa7, 0xaf, 0xda, 0x92, 0xe2, 0xe7, 0x61, 0xdc,
219+
0xa1, 0x32, 0x53, 0x2a, 0x3f, 0x41, 0x5c, 0x7e,
220+
0xc9, 0x89, 0xda, 0x1c, 0xf7, 0x8d, 0x00, 0xbd,
221+
0x21, 0x73, 0xb1, 0x69,
222+
},
223+
},
224+
};
225+
226+
static const u8 hash_testvec_consolidated[SHA224_DIGEST_SIZE] = {
227+
0x9e, 0xb8, 0x82, 0xab, 0x83, 0x37, 0xe4, 0x63,
228+
0x84, 0xee, 0x21, 0x15, 0xc2, 0xbb, 0xa3, 0x17,
229+
0x8f, 0xc4, 0x99, 0x33, 0xa0, 0x2c, 0x9f, 0xec,
230+
0xca, 0xd0, 0xf3, 0x73,
231+
};
232+
233+
static const u8 hmac_testvec_consolidated[SHA224_DIGEST_SIZE] = {
234+
0x66, 0x34, 0x79, 0x92, 0x47, 0x0e, 0xcd, 0x70,
235+
0xb0, 0x8b, 0x91, 0xcb, 0x94, 0x2f, 0x67, 0x65,
236+
0x2f, 0xc9, 0xd2, 0x91, 0x32, 0xaf, 0xf7, 0x5f,
237+
0xb6, 0x01, 0x5b, 0xf2,
238+
};

lib/crypto/tests/sha224_kunit.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright 2025 Google LLC
4+
*/
5+
#include <crypto/sha2.h>
6+
#include "sha224-testvecs.h"
7+
8+
#define HASH sha224
9+
#define HASH_CTX sha224_ctx
10+
#define HASH_SIZE SHA224_DIGEST_SIZE
11+
#define HASH_INIT sha224_init
12+
#define HASH_UPDATE sha224_update
13+
#define HASH_FINAL sha224_final
14+
#define HMAC_KEY hmac_sha224_key
15+
#define HMAC_CTX hmac_sha224_ctx
16+
#define HMAC_PREPAREKEY hmac_sha224_preparekey
17+
#define HMAC_INIT hmac_sha224_init
18+
#define HMAC_UPDATE hmac_sha224_update
19+
#define HMAC_FINAL hmac_sha224_final
20+
#define HMAC hmac_sha224
21+
#define HMAC_USINGRAWKEY hmac_sha224_usingrawkey
22+
#include "hash-test-template.h"
23+
24+
static struct kunit_case hash_test_cases[] = {
25+
HASH_KUNIT_CASES,
26+
KUNIT_CASE(benchmark_hash),
27+
{},
28+
};
29+
30+
static struct kunit_suite hash_test_suite = {
31+
.name = "sha224",
32+
.test_cases = hash_test_cases,
33+
.suite_init = hash_suite_init,
34+
.suite_exit = hash_suite_exit,
35+
};
36+
kunit_test_suite(hash_test_suite);
37+
38+
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-224 and HMAC-SHA224");
39+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)