Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
0004  * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
0005  *
0006  * Copyright (C) 2017 ARM Limited or its affiliates.
0007  * Written by Gilad Ben-Yossef <gilad@benyossef.com>
0008  * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
0009  */
0010 
0011 #include <crypto/internal/hash.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/mm.h>
0015 #include <linux/types.h>
0016 #include <crypto/sm3.h>
0017 #include <crypto/sm3_base.h>
0018 #include <linux/bitops.h>
0019 #include <asm/byteorder.h>
0020 #include <asm/unaligned.h>
0021 
0022 const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
0023     0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
0024     0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
0025     0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
0026     0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
0027 };
0028 EXPORT_SYMBOL_GPL(sm3_zero_message_hash);
0029 
0030 static int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
0031               unsigned int len)
0032 {
0033     sm3_update(shash_desc_ctx(desc), data, len);
0034     return 0;
0035 }
0036 
0037 static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
0038 {
0039     sm3_final(shash_desc_ctx(desc), out);
0040     return 0;
0041 }
0042 
0043 static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
0044             unsigned int len, u8 *hash)
0045 {
0046     struct sm3_state *sctx = shash_desc_ctx(desc);
0047 
0048     if (len)
0049         sm3_update(sctx, data, len);
0050     sm3_final(sctx, hash);
0051     return 0;
0052 }
0053 
0054 static struct shash_alg sm3_alg = {
0055     .digestsize =   SM3_DIGEST_SIZE,
0056     .init       =   sm3_base_init,
0057     .update     =   crypto_sm3_update,
0058     .final      =   crypto_sm3_final,
0059     .finup      =   crypto_sm3_finup,
0060     .descsize   =   sizeof(struct sm3_state),
0061     .base       =   {
0062         .cra_name    =  "sm3",
0063         .cra_driver_name =  "sm3-generic",
0064         .cra_priority   =   100,
0065         .cra_blocksize   =  SM3_BLOCK_SIZE,
0066         .cra_module  =  THIS_MODULE,
0067     }
0068 };
0069 
0070 static int __init sm3_generic_mod_init(void)
0071 {
0072     return crypto_register_shash(&sm3_alg);
0073 }
0074 
0075 static void __exit sm3_generic_mod_fini(void)
0076 {
0077     crypto_unregister_shash(&sm3_alg);
0078 }
0079 
0080 subsys_initcall(sm3_generic_mod_init);
0081 module_exit(sm3_generic_mod_fini);
0082 
0083 MODULE_LICENSE("GPL v2");
0084 MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
0085 
0086 MODULE_ALIAS_CRYPTO("sm3");
0087 MODULE_ALIAS_CRYPTO("sm3-generic");