Skip to content

Commit 66b1306

Browse files
author
Eric Biggers
committed
lib/crypto: tests: Add KUnit tests for SHA-1 and HMAC-SHA1
Add a KUnit test suite for the SHA-1 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 suite, 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. Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250712232329.818226-16-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 6dd4d9f commit 66b1306

4 files changed

Lines changed: 262 additions & 0 deletions

File tree

lib/crypto/tests/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ config CRYPTO_LIB_POLY1305_KUNIT_TEST
99
help
1010
KUnit tests for the Poly1305 library functions.
1111

12+
config CRYPTO_LIB_SHA1_KUNIT_TEST
13+
tristate "KUnit tests for SHA-1" if !KUNIT_ALL_TESTS
14+
depends on KUNIT
15+
default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
16+
select CRYPTO_LIB_BENCHMARK_VISIBLE
17+
select CRYPTO_LIB_SHA1
18+
help
19+
KUnit tests for the SHA-1 cryptographic hash function and its
20+
corresponding HMAC.
21+
1222
# Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are
1323
# included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256).
1424
config CRYPTO_LIB_SHA256_KUNIT_TEST

lib/crypto/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0-or-later
22

33
obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o
4+
obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o
45
obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o
56
obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o

lib/crypto/tests/sha1-testvecs.h

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

lib/crypto/tests/sha1_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/sha1.h>
6+
#include "sha1-testvecs.h"
7+
8+
#define HASH sha1
9+
#define HASH_CTX sha1_ctx
10+
#define HASH_SIZE SHA1_DIGEST_SIZE
11+
#define HASH_INIT sha1_init
12+
#define HASH_UPDATE sha1_update
13+
#define HASH_FINAL sha1_final
14+
#define HMAC_KEY hmac_sha1_key
15+
#define HMAC_CTX hmac_sha1_ctx
16+
#define HMAC_PREPAREKEY hmac_sha1_preparekey
17+
#define HMAC_INIT hmac_sha1_init
18+
#define HMAC_UPDATE hmac_sha1_update
19+
#define HMAC_FINAL hmac_sha1_final
20+
#define HMAC hmac_sha1
21+
#define HMAC_USINGRAWKEY hmac_sha1_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 = "sha1",
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-1 and HMAC-SHA1");
39+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)