Files
linux/crypto/sm3.c
Eric Biggers ed065bd06e 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>
2026-03-23 17:50:59 -07:00

90 lines
2.1 KiB
C

// 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");