summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@kernel.org>2026-03-20 21:09:29 -0700
committerEric Biggers <ebiggers@kernel.org>2026-03-23 17:50:59 -0700
commited065bd06ebe8d92d1647d230a14b9c035ad5b30 (patch)
tree64f64399e269afaf7251d4a3de973fba9a701ab7 /crypto
parentd6781b8ba33ae9f6ab2e88c1158e989a24847c4b (diff)
downloadlwn-ed065bd06ebe8d92d1647d230a14b9c035ad5b30.tar.gz
lwn-ed065bd06ebe8d92d1647d230a14b9c035ad5b30.zip
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>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/Makefile2
-rw-r--r--crypto/sm3.c89
-rw-r--r--crypto/sm3_generic.c64
-rw-r--r--crypto/testmgr.c2
4 files changed, 92 insertions, 65 deletions
diff --git a/crypto/Makefile b/crypto/Makefile
index 3fcbf0cd522d..68646175f6ab 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -83,7 +83,7 @@ obj-$(CONFIG_CRYPTO_SHA1) += sha1.o
obj-$(CONFIG_CRYPTO_SHA256) += sha256.o
obj-$(CONFIG_CRYPTO_SHA512) += sha512.o
obj-$(CONFIG_CRYPTO_SHA3) += sha3.o
-obj-$(CONFIG_CRYPTO_SM3) += sm3_generic.o
+obj-$(CONFIG_CRYPTO_SM3) += sm3.o
obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
obj-$(CONFIG_CRYPTO_WP512) += wp512.o
CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns) # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149
diff --git a/crypto/sm3.c b/crypto/sm3.c
new file mode 100644
index 000000000000..05111a99b851
--- /dev/null
+++ b/crypto/sm3.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
+ * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
+ *
+ * Copyright (C) 2017 ARM Limited or its affiliates.
+ * Written by Gilad Ben-Yossef <gilad@benyossef.com>
+ * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
+ * Copyright 2026 Google LLC
+ */
+
+#include <crypto/internal/hash.h>
+#include <crypto/sm3.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#define SM3_CTX(desc) ((struct sm3_ctx *)shash_desc_ctx(desc))
+
+static int crypto_sm3_init(struct shash_desc *desc)
+{
+ sm3_init(SM3_CTX(desc));
+ return 0;
+}
+
+static int crypto_sm3_update(struct shash_desc *desc,
+ const u8 *data, unsigned int len)
+{
+ sm3_update(SM3_CTX(desc), data, len);
+ return 0;
+}
+
+static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
+{
+ sm3_final(SM3_CTX(desc), out);
+ return 0;
+}
+
+static int crypto_sm3_digest(struct shash_desc *desc,
+ const u8 *data, unsigned int len, u8 *out)
+{
+ sm3(data, len, out);
+ return 0;
+}
+
+static int crypto_sm3_export_core(struct shash_desc *desc, void *out)
+{
+ memcpy(out, SM3_CTX(desc), sizeof(struct sm3_ctx));
+ return 0;
+}
+
+static int crypto_sm3_import_core(struct shash_desc *desc, const void *in)
+{
+ memcpy(SM3_CTX(desc), in, sizeof(struct sm3_ctx));
+ return 0;
+}
+
+static struct shash_alg sm3_alg = {
+ .base.cra_name = "sm3",
+ .base.cra_driver_name = "sm3-lib",
+ .base.cra_priority = 300,
+ .base.cra_blocksize = SM3_BLOCK_SIZE,
+ .base.cra_module = THIS_MODULE,
+ .digestsize = SM3_DIGEST_SIZE,
+ .init = crypto_sm3_init,
+ .update = crypto_sm3_update,
+ .final = crypto_sm3_final,
+ .digest = crypto_sm3_digest,
+ .export_core = crypto_sm3_export_core,
+ .import_core = crypto_sm3_import_core,
+ .descsize = sizeof(struct sm3_ctx),
+};
+
+static int __init crypto_sm3_mod_init(void)
+{
+ return crypto_register_shash(&sm3_alg);
+}
+module_init(crypto_sm3_mod_init);
+
+static void __exit crypto_sm3_mod_exit(void)
+{
+ crypto_unregister_shash(&sm3_alg);
+}
+module_exit(crypto_sm3_mod_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Crypto API support for SM3");
+
+MODULE_ALIAS_CRYPTO("sm3");
+MODULE_ALIAS_CRYPTO("sm3-lib");
diff --git a/crypto/sm3_generic.c b/crypto/sm3_generic.c
deleted file mode 100644
index 0c606f526347..000000000000
--- a/crypto/sm3_generic.c
+++ /dev/null
@@ -1,64 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
- * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
- *
- * Copyright (C) 2017 ARM Limited or its affiliates.
- * Written by Gilad Ben-Yossef <gilad@benyossef.com>
- * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
- */
-
-#include <crypto/internal/hash.h>
-#include <crypto/sm3.h>
-#include <crypto/sm3_base.h>
-#include <linux/kernel.h>
-#include <linux/module.h>
-
-static int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
- unsigned int len)
-{
- return sm3_base_do_update_blocks(desc, data, len, sm3_block_generic);
-}
-
-static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
- unsigned int len, u8 *hash)
-{
- sm3_base_do_finup(desc, data, len, sm3_block_generic);
- return sm3_base_finish(desc, hash);
-}
-
-static struct shash_alg sm3_alg = {
- .digestsize = SM3_DIGEST_SIZE,
- .init = sm3_base_init,
- .update = crypto_sm3_update,
- .finup = crypto_sm3_finup,
- .descsize = SM3_STATE_SIZE,
- .base = {
- .cra_name = "sm3",
- .cra_driver_name = "sm3-generic",
- .cra_priority = 100,
- .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY |
- CRYPTO_AHASH_ALG_FINUP_MAX,
- .cra_blocksize = SM3_BLOCK_SIZE,
- .cra_module = THIS_MODULE,
- }
-};
-
-static int __init sm3_generic_mod_init(void)
-{
- return crypto_register_shash(&sm3_alg);
-}
-
-static void __exit sm3_generic_mod_fini(void)
-{
- crypto_unregister_shash(&sm3_alg);
-}
-
-module_init(sm3_generic_mod_init);
-module_exit(sm3_generic_mod_fini);
-
-MODULE_LICENSE("GPL v2");
-MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
-
-MODULE_ALIAS_CRYPTO("sm3");
-MODULE_ALIAS_CRYPTO("sm3-generic");
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index dd01f86dd6fe..60b6e4379aa6 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -5079,6 +5079,7 @@ static const struct alg_test_desc alg_test_descs[] = {
}
}, {
.alg = "hmac(sm3)",
+ .generic_driver = "hmac(sm3-lib)",
.test = alg_test_hash,
.suite = {
.hash = __VECS(hmac_sm3_tv_template)
@@ -5446,6 +5447,7 @@ static const struct alg_test_desc alg_test_descs[] = {
}
}, {
.alg = "sm3",
+ .generic_driver = "sm3-lib",
.test = alg_test_hash,
.suite = {
.hash = __VECS(sm3_tv_template)