Skip to content

Commit ed065bd

Browse files
author
Eric Biggers
committed
crypto: sm3 - Replace with wrapper around library
Reimplement the "sm3" crypto_shash on top of the SM3 library, closely mirroring the other hash algorithms (e.g. SHA-*). The result, after later commits migrate the architecture-optimized SM3 code into the library as well, is that crypto/sm3.c will be the single point of integration between crypto_shash and the actual SM3 implementations, simplifying the code. Note: to see the diff from crypto/sm3_generic.c to crypto/sm3.c, view this commit with 'git show -M10'. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260321040935.410034-7-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent d6781b8 commit ed065bd

5 files changed

Lines changed: 94 additions & 67 deletions

File tree

crypto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ obj-$(CONFIG_CRYPTO_SHA1) += sha1.o
8383
obj-$(CONFIG_CRYPTO_SHA256) += sha256.o
8484
obj-$(CONFIG_CRYPTO_SHA512) += sha512.o
8585
obj-$(CONFIG_CRYPTO_SHA3) += sha3.o
86-
obj-$(CONFIG_CRYPTO_SM3) += sm3_generic.o
86+
obj-$(CONFIG_CRYPTO_SM3) += sm3.o
8787
obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
8888
obj-$(CONFIG_CRYPTO_WP512) += wp512.o
8989
CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns) # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149

crypto/sm3.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
4+
* described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
5+
*
6+
* Copyright (C) 2017 ARM Limited or its affiliates.
7+
* Written by Gilad Ben-Yossef <gilad@benyossef.com>
8+
* Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
9+
* Copyright 2026 Google LLC
10+
*/
11+
12+
#include <crypto/internal/hash.h>
13+
#include <crypto/sm3.h>
14+
#include <linux/kernel.h>
15+
#include <linux/module.h>
16+
17+
#define SM3_CTX(desc) ((struct sm3_ctx *)shash_desc_ctx(desc))
18+
19+
static int crypto_sm3_init(struct shash_desc *desc)
20+
{
21+
sm3_init(SM3_CTX(desc));
22+
return 0;
23+
}
24+
25+
static int crypto_sm3_update(struct shash_desc *desc,
26+
const u8 *data, unsigned int len)
27+
{
28+
sm3_update(SM3_CTX(desc), data, len);
29+
return 0;
30+
}
31+
32+
static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
33+
{
34+
sm3_final(SM3_CTX(desc), out);
35+
return 0;
36+
}
37+
38+
static int crypto_sm3_digest(struct shash_desc *desc,
39+
const u8 *data, unsigned int len, u8 *out)
40+
{
41+
sm3(data, len, out);
42+
return 0;
43+
}
44+
45+
static int crypto_sm3_export_core(struct shash_desc *desc, void *out)
46+
{
47+
memcpy(out, SM3_CTX(desc), sizeof(struct sm3_ctx));
48+
return 0;
49+
}
50+
51+
static int crypto_sm3_import_core(struct shash_desc *desc, const void *in)
52+
{
53+
memcpy(SM3_CTX(desc), in, sizeof(struct sm3_ctx));
54+
return 0;
55+
}
56+
57+
static struct shash_alg sm3_alg = {
58+
.base.cra_name = "sm3",
59+
.base.cra_driver_name = "sm3-lib",
60+
.base.cra_priority = 300,
61+
.base.cra_blocksize = SM3_BLOCK_SIZE,
62+
.base.cra_module = THIS_MODULE,
63+
.digestsize = SM3_DIGEST_SIZE,
64+
.init = crypto_sm3_init,
65+
.update = crypto_sm3_update,
66+
.final = crypto_sm3_final,
67+
.digest = crypto_sm3_digest,
68+
.export_core = crypto_sm3_export_core,
69+
.import_core = crypto_sm3_import_core,
70+
.descsize = sizeof(struct sm3_ctx),
71+
};
72+
73+
static int __init crypto_sm3_mod_init(void)
74+
{
75+
return crypto_register_shash(&sm3_alg);
76+
}
77+
module_init(crypto_sm3_mod_init);
78+
79+
static void __exit crypto_sm3_mod_exit(void)
80+
{
81+
crypto_unregister_shash(&sm3_alg);
82+
}
83+
module_exit(crypto_sm3_mod_exit);
84+
85+
MODULE_LICENSE("GPL v2");
86+
MODULE_DESCRIPTION("Crypto API support for SM3");
87+
88+
MODULE_ALIAS_CRYPTO("sm3");
89+
MODULE_ALIAS_CRYPTO("sm3-lib");

crypto/sm3_generic.c

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

crypto/testmgr.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5079,6 +5079,7 @@ static const struct alg_test_desc alg_test_descs[] = {
50795079
}
50805080
}, {
50815081
.alg = "hmac(sm3)",
5082+
.generic_driver = "hmac(sm3-lib)",
50825083
.test = alg_test_hash,
50835084
.suite = {
50845085
.hash = __VECS(hmac_sm3_tv_template)
@@ -5446,6 +5447,7 @@ static const struct alg_test_desc alg_test_descs[] = {
54465447
}
54475448
}, {
54485449
.alg = "sm3",
5450+
.generic_driver = "sm3-lib",
54495451
.test = alg_test_hash,
54505452
.suite = {
54515453
.hash = __VECS(sm3_tv_template)

drivers/crypto/starfive/jh7110-hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ static int starfive_sha512_init_tfm(struct crypto_ahash *hash)
520520

521521
static int starfive_sm3_init_tfm(struct crypto_ahash *hash)
522522
{
523-
return starfive_hash_init_tfm(hash, "sm3-generic",
523+
return starfive_hash_init_tfm(hash, "sm3-lib",
524524
STARFIVE_HASH_SM3, 0);
525525
}
526526

@@ -550,7 +550,7 @@ static int starfive_hmac_sha512_init_tfm(struct crypto_ahash *hash)
550550

551551
static int starfive_hmac_sm3_init_tfm(struct crypto_ahash *hash)
552552
{
553-
return starfive_hash_init_tfm(hash, "hmac(sm3-generic)",
553+
return starfive_hash_init_tfm(hash, "hmac(sm3-lib)",
554554
STARFIVE_HASH_SM3, 1);
555555
}
556556

0 commit comments

Comments
 (0)