Skip to content

Commit 8bc79ab

Browse files
author
Eric Biggers
committed
crypto: sha1 - Wrap library and add HMAC support
Like I did for crypto/sha512.c, rework crypto/sha1_generic.c (renamed to crypto/sha1.c) to simply wrap the normal library functions instead of accessing the low-level block function directly. Also add support for HMAC-SHA1, again just wrapping the library functions. Since the replacement crypto_shash algorithms are implemented using the (potentially arch-optimized) library functions, give them driver names ending with "-lib" rather than "-generic". Update crypto/testmgr.c and an odd driver to take this change in driver name into account. Note: to see the diff from crypto/sha1_generic.c to crypto/sha1.c, view this commit with 'git show -M10'. Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20250712232329.818226-6-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 4cbc844 commit 8bc79ab

6 files changed

Lines changed: 145 additions & 90 deletions

File tree

crypto/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,8 @@ config CRYPTO_SHA1
986986
select CRYPTO_HASH
987987
select CRYPTO_LIB_SHA1
988988
help
989-
SHA-1 secure hash algorithm (FIPS 180, ISO/IEC 10118-3)
989+
SHA-1 secure hash algorithm (FIPS 180, ISO/IEC 10118-3), including
990+
HMAC support.
990991

991992
config CRYPTO_SHA256
992993
tristate "SHA-224 and SHA-256"

crypto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o
7575
obj-$(CONFIG_CRYPTO_MD4) += md4.o
7676
obj-$(CONFIG_CRYPTO_MD5) += md5.o
7777
obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
78-
obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
78+
obj-$(CONFIG_CRYPTO_SHA1) += sha1.o
7979
obj-$(CONFIG_CRYPTO_SHA256) += sha256.o
8080
obj-$(CONFIG_CRYPTO_SHA512) += sha512.o
8181
obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o

crypto/sha1.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Crypto API support for SHA-1 and HMAC-SHA1
4+
*
5+
* Copyright (c) Alan Smithee.
6+
* Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7+
* Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
8+
* Copyright 2025 Google LLC
9+
*/
10+
#include <crypto/internal/hash.h>
11+
#include <crypto/sha1.h>
12+
#include <linux/kernel.h>
13+
#include <linux/module.h>
14+
15+
const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
16+
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
17+
0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
18+
0xaf, 0xd8, 0x07, 0x09
19+
};
20+
EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
21+
22+
#define SHA1_CTX(desc) ((struct sha1_ctx *)shash_desc_ctx(desc))
23+
24+
static int crypto_sha1_init(struct shash_desc *desc)
25+
{
26+
sha1_init(SHA1_CTX(desc));
27+
return 0;
28+
}
29+
30+
static int crypto_sha1_update(struct shash_desc *desc,
31+
const u8 *data, unsigned int len)
32+
{
33+
sha1_update(SHA1_CTX(desc), data, len);
34+
return 0;
35+
}
36+
37+
static int crypto_sha1_final(struct shash_desc *desc, u8 *out)
38+
{
39+
sha1_final(SHA1_CTX(desc), out);
40+
return 0;
41+
}
42+
43+
static int crypto_sha1_digest(struct shash_desc *desc,
44+
const u8 *data, unsigned int len, u8 *out)
45+
{
46+
sha1(data, len, out);
47+
return 0;
48+
}
49+
50+
#define HMAC_SHA1_KEY(tfm) ((struct hmac_sha1_key *)crypto_shash_ctx(tfm))
51+
#define HMAC_SHA1_CTX(desc) ((struct hmac_sha1_ctx *)shash_desc_ctx(desc))
52+
53+
static int crypto_hmac_sha1_setkey(struct crypto_shash *tfm,
54+
const u8 *raw_key, unsigned int keylen)
55+
{
56+
hmac_sha1_preparekey(HMAC_SHA1_KEY(tfm), raw_key, keylen);
57+
return 0;
58+
}
59+
60+
static int crypto_hmac_sha1_init(struct shash_desc *desc)
61+
{
62+
hmac_sha1_init(HMAC_SHA1_CTX(desc), HMAC_SHA1_KEY(desc->tfm));
63+
return 0;
64+
}
65+
66+
static int crypto_hmac_sha1_update(struct shash_desc *desc,
67+
const u8 *data, unsigned int len)
68+
{
69+
hmac_sha1_update(HMAC_SHA1_CTX(desc), data, len);
70+
return 0;
71+
}
72+
73+
static int crypto_hmac_sha1_final(struct shash_desc *desc, u8 *out)
74+
{
75+
hmac_sha1_final(HMAC_SHA1_CTX(desc), out);
76+
return 0;
77+
}
78+
79+
static int crypto_hmac_sha1_digest(struct shash_desc *desc,
80+
const u8 *data, unsigned int len, u8 *out)
81+
{
82+
hmac_sha1(HMAC_SHA1_KEY(desc->tfm), data, len, out);
83+
return 0;
84+
}
85+
86+
static struct shash_alg algs[] = {
87+
{
88+
.base.cra_name = "sha1",
89+
.base.cra_driver_name = "sha1-lib",
90+
.base.cra_priority = 300,
91+
.base.cra_blocksize = SHA1_BLOCK_SIZE,
92+
.base.cra_module = THIS_MODULE,
93+
.digestsize = SHA1_DIGEST_SIZE,
94+
.init = crypto_sha1_init,
95+
.update = crypto_sha1_update,
96+
.final = crypto_sha1_final,
97+
.digest = crypto_sha1_digest,
98+
.descsize = sizeof(struct sha1_ctx),
99+
},
100+
{
101+
.base.cra_name = "hmac(sha1)",
102+
.base.cra_driver_name = "hmac-sha1-lib",
103+
.base.cra_priority = 300,
104+
.base.cra_blocksize = SHA1_BLOCK_SIZE,
105+
.base.cra_ctxsize = sizeof(struct hmac_sha1_key),
106+
.base.cra_module = THIS_MODULE,
107+
.digestsize = SHA1_DIGEST_SIZE,
108+
.setkey = crypto_hmac_sha1_setkey,
109+
.init = crypto_hmac_sha1_init,
110+
.update = crypto_hmac_sha1_update,
111+
.final = crypto_hmac_sha1_final,
112+
.digest = crypto_hmac_sha1_digest,
113+
.descsize = sizeof(struct hmac_sha1_ctx),
114+
},
115+
};
116+
117+
static int __init crypto_sha1_mod_init(void)
118+
{
119+
return crypto_register_shashes(algs, ARRAY_SIZE(algs));
120+
}
121+
module_init(crypto_sha1_mod_init);
122+
123+
static void __exit crypto_sha1_mod_exit(void)
124+
{
125+
crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
126+
}
127+
module_exit(crypto_sha1_mod_exit);
128+
129+
MODULE_LICENSE("GPL");
130+
MODULE_DESCRIPTION("Crypto API support for SHA-1 and HMAC-SHA1");
131+
132+
MODULE_ALIAS_CRYPTO("sha1");
133+
MODULE_ALIAS_CRYPTO("sha1-lib");
134+
MODULE_ALIAS_CRYPTO("hmac(sha1)");
135+
MODULE_ALIAS_CRYPTO("hmac-sha1-lib");

crypto/sha1_generic.c

Lines changed: 0 additions & 87 deletions
This file was deleted.

crypto/testmgr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,19 +4237,22 @@ static const struct alg_test_desc alg_test_descs[] = {
42374237
}
42384238
}, {
42394239
.alg = "authenc(hmac(sha1),cbc(aes))",
4240+
.generic_driver = "authenc(hmac-sha1-lib,cbc(aes-generic))",
42404241
.test = alg_test_aead,
42414242
.fips_allowed = 1,
42424243
.suite = {
42434244
.aead = __VECS(hmac_sha1_aes_cbc_tv_temp)
42444245
}
42454246
}, {
42464247
.alg = "authenc(hmac(sha1),cbc(des))",
4248+
.generic_driver = "authenc(hmac-sha1-lib,cbc(des-generic))",
42474249
.test = alg_test_aead,
42484250
.suite = {
42494251
.aead = __VECS(hmac_sha1_des_cbc_tv_temp)
42504252
}
42514253
}, {
42524254
.alg = "authenc(hmac(sha1),cbc(des3_ede))",
4255+
.generic_driver = "authenc(hmac-sha1-lib,cbc(des3_ede-generic))",
42534256
.test = alg_test_aead,
42544257
.suite = {
42554258
.aead = __VECS(hmac_sha1_des3_ede_cbc_tv_temp)
@@ -4260,6 +4263,7 @@ static const struct alg_test_desc alg_test_descs[] = {
42604263
.fips_allowed = 1,
42614264
}, {
42624265
.alg = "authenc(hmac(sha1),ecb(cipher_null))",
4266+
.generic_driver = "authenc(hmac-sha1-lib,ecb-cipher_null)",
42634267
.test = alg_test_aead,
42644268
.suite = {
42654269
.aead = __VECS(hmac_sha1_ecb_cipher_null_tv_temp)
@@ -5122,6 +5126,7 @@ static const struct alg_test_desc alg_test_descs[] = {
51225126
}
51235127
}, {
51245128
.alg = "hmac(sha1)",
5129+
.generic_driver = "hmac-sha1-lib",
51255130
.test = alg_test_hash,
51265131
.fips_allowed = 1,
51275132
.suite = {
@@ -5462,6 +5467,7 @@ static const struct alg_test_desc alg_test_descs[] = {
54625467
}
54635468
}, {
54645469
.alg = "sha1",
5470+
.generic_driver = "sha1-lib",
54655471
.test = alg_test_hash,
54665472
.fips_allowed = 1,
54675473
.suite = {

drivers/crypto/img-hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static int img_hash_cra_md5_init(struct crypto_tfm *tfm)
705705

706706
static int img_hash_cra_sha1_init(struct crypto_tfm *tfm)
707707
{
708-
return img_hash_cra_init(tfm, "sha1-generic");
708+
return img_hash_cra_init(tfm, "sha1-lib");
709709
}
710710

711711
static int img_hash_cra_sha224_init(struct crypto_tfm *tfm)

0 commit comments

Comments
 (0)