1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24f0fc160SGilad Ben-Yossef /*
34f0fc160SGilad Ben-Yossef * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
44f0fc160SGilad Ben-Yossef * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
54f0fc160SGilad Ben-Yossef *
64f0fc160SGilad Ben-Yossef * Copyright (C) 2017 ARM Limited or its affiliates.
74f0fc160SGilad Ben-Yossef * Written by Gilad Ben-Yossef <gilad@benyossef.com>
8*b4784a45STianjia Zhang * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
94f0fc160SGilad Ben-Yossef */
104f0fc160SGilad Ben-Yossef
114f0fc160SGilad Ben-Yossef #include <crypto/internal/hash.h>
124f0fc160SGilad Ben-Yossef #include <linux/init.h>
134f0fc160SGilad Ben-Yossef #include <linux/module.h>
144f0fc160SGilad Ben-Yossef #include <linux/mm.h>
154f0fc160SGilad Ben-Yossef #include <linux/types.h>
164f0fc160SGilad Ben-Yossef #include <crypto/sm3.h>
174f0fc160SGilad Ben-Yossef #include <crypto/sm3_base.h>
184f0fc160SGilad Ben-Yossef #include <linux/bitops.h>
194f0fc160SGilad Ben-Yossef #include <asm/byteorder.h>
204f0fc160SGilad Ben-Yossef #include <asm/unaligned.h>
214f0fc160SGilad Ben-Yossef
224f0fc160SGilad Ben-Yossef const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
234f0fc160SGilad Ben-Yossef 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
244f0fc160SGilad Ben-Yossef 0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
254f0fc160SGilad Ben-Yossef 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
264f0fc160SGilad Ben-Yossef 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
274f0fc160SGilad Ben-Yossef };
284f0fc160SGilad Ben-Yossef EXPORT_SYMBOL_GPL(sm3_zero_message_hash);
294f0fc160SGilad Ben-Yossef
crypto_sm3_update(struct shash_desc * desc,const u8 * data,unsigned int len)30*b4784a45STianjia Zhang static int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
314f0fc160SGilad Ben-Yossef unsigned int len)
324f0fc160SGilad Ben-Yossef {
33*b4784a45STianjia Zhang sm3_update(shash_desc_ctx(desc), data, len);
34*b4784a45STianjia Zhang return 0;
354f0fc160SGilad Ben-Yossef }
364f0fc160SGilad Ben-Yossef
crypto_sm3_final(struct shash_desc * desc,u8 * out)37*b4784a45STianjia Zhang static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
384f0fc160SGilad Ben-Yossef {
39*b4784a45STianjia Zhang sm3_final(shash_desc_ctx(desc), out);
40*b4784a45STianjia Zhang return 0;
414f0fc160SGilad Ben-Yossef }
424f0fc160SGilad Ben-Yossef
crypto_sm3_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * hash)43*b4784a45STianjia Zhang static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
444f0fc160SGilad Ben-Yossef unsigned int len, u8 *hash)
454f0fc160SGilad Ben-Yossef {
46*b4784a45STianjia Zhang struct sm3_state *sctx = shash_desc_ctx(desc);
47*b4784a45STianjia Zhang
48*b4784a45STianjia Zhang if (len)
49*b4784a45STianjia Zhang sm3_update(sctx, data, len);
50*b4784a45STianjia Zhang sm3_final(sctx, hash);
51*b4784a45STianjia Zhang return 0;
524f0fc160SGilad Ben-Yossef }
534f0fc160SGilad Ben-Yossef
544f0fc160SGilad Ben-Yossef static struct shash_alg sm3_alg = {
554f0fc160SGilad Ben-Yossef .digestsize = SM3_DIGEST_SIZE,
564f0fc160SGilad Ben-Yossef .init = sm3_base_init,
574f0fc160SGilad Ben-Yossef .update = crypto_sm3_update,
58f4928287STianjia Zhang .final = crypto_sm3_final,
594f0fc160SGilad Ben-Yossef .finup = crypto_sm3_finup,
604f0fc160SGilad Ben-Yossef .descsize = sizeof(struct sm3_state),
614f0fc160SGilad Ben-Yossef .base = {
624f0fc160SGilad Ben-Yossef .cra_name = "sm3",
634f0fc160SGilad Ben-Yossef .cra_driver_name = "sm3-generic",
64*b4784a45STianjia Zhang .cra_priority = 100,
654f0fc160SGilad Ben-Yossef .cra_blocksize = SM3_BLOCK_SIZE,
664f0fc160SGilad Ben-Yossef .cra_module = THIS_MODULE,
674f0fc160SGilad Ben-Yossef }
684f0fc160SGilad Ben-Yossef };
694f0fc160SGilad Ben-Yossef
sm3_generic_mod_init(void)704f0fc160SGilad Ben-Yossef static int __init sm3_generic_mod_init(void)
714f0fc160SGilad Ben-Yossef {
724f0fc160SGilad Ben-Yossef return crypto_register_shash(&sm3_alg);
734f0fc160SGilad Ben-Yossef }
744f0fc160SGilad Ben-Yossef
sm3_generic_mod_fini(void)754f0fc160SGilad Ben-Yossef static void __exit sm3_generic_mod_fini(void)
764f0fc160SGilad Ben-Yossef {
774f0fc160SGilad Ben-Yossef crypto_unregister_shash(&sm3_alg);
784f0fc160SGilad Ben-Yossef }
794f0fc160SGilad Ben-Yossef
80c4741b23SEric Biggers subsys_initcall(sm3_generic_mod_init);
814f0fc160SGilad Ben-Yossef module_exit(sm3_generic_mod_fini);
824f0fc160SGilad Ben-Yossef
834f0fc160SGilad Ben-Yossef MODULE_LICENSE("GPL v2");
844f0fc160SGilad Ben-Yossef MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
854f0fc160SGilad Ben-Yossef
864f0fc160SGilad Ben-Yossef MODULE_ALIAS_CRYPTO("sm3");
874f0fc160SGilad Ben-Yossef MODULE_ALIAS_CRYPTO("sm3-generic");
88