]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
crypto: sm3 - Replace with wrapper around library
authorEric Biggers <ebiggers@kernel.org>
Sat, 21 Mar 2026 04:09:29 +0000 (21:09 -0700)
committerEric Biggers <ebiggers@kernel.org>
Tue, 24 Mar 2026 00:50:59 +0000 (17:50 -0700)
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>
crypto/Makefile
crypto/sm3.c [new file with mode: 0644]
crypto/sm3_generic.c [deleted file]
crypto/testmgr.c
drivers/crypto/starfive/jh7110-hash.c

index 3fcbf0cd522d03da411a50da7854555aa1be3829..68646175f6aba2c7df8e68857446556a18b50ffd 100644 (file)
@@ -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 (file)
index 0000000..05111a9
--- /dev/null
@@ -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 (file)
index 0c606f5..0000000
+++ /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");
index dd01f86dd6febf512fe42d2160775f60f764c89f..60b6e4379aa6b50e416ece27d86b43d7e372d60f 100644 (file)
@@ -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)
index 54b7af4a7aee8239aa50c315361c3e332579ded9..742038a5201acddb8dc1809e01cded63ac231043 100644 (file)
@@ -520,7 +520,7 @@ static int starfive_sha512_init_tfm(struct crypto_ahash *hash)
 
 static int starfive_sm3_init_tfm(struct crypto_ahash *hash)
 {
-       return starfive_hash_init_tfm(hash, "sm3-generic",
+       return starfive_hash_init_tfm(hash, "sm3-lib",
                                      STARFIVE_HASH_SM3, 0);
 }
 
@@ -550,7 +550,7 @@ static int starfive_hmac_sha512_init_tfm(struct crypto_ahash *hash)
 
 static int starfive_hmac_sm3_init_tfm(struct crypto_ahash *hash)
 {
-       return starfive_hash_init_tfm(hash, "hmac(sm3-generic)",
+       return starfive_hash_init_tfm(hash, "hmac(sm3-lib)",
                                      STARFIVE_HASH_SM3, 1);
 }