projects/linux·blob master · refs

89 lines · 2.1 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
4 * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
5 *
6 * Copyright (C) 2017 ARM Limited or its affiliates.
7 * Written by Gilad Ben-Yossef <[email protected]>
8 * Copyright (C) 2021 Tianjia Zhang <[email protected]>
9 * Copyright 2026 Google LLC
10 */
11
12#include <crypto/internal/hash.h>
13#include <crypto/sm3.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16
17#define SM3_CTX(desc) ((struct sm3_ctx *)shash_desc_ctx(desc))
18
19static int crypto_sm3_init(struct shash_desc *desc)
20{
21 sm3_init(SM3_CTX(desc));
22 return 0;
23}
24
25static int crypto_sm3_update(struct shash_desc *desc,
26 const u8 *data, unsigned int len)
27{
28 sm3_update(SM3_CTX(desc), data, len);
29 return 0;
30}
31
32static int crypto_sm3_final(struct shash_desc *desc, u8 *out)
33{
34 sm3_final(SM3_CTX(desc), out);
35 return 0;
36}
37
38static int crypto_sm3_digest(struct shash_desc *desc,
39 const u8 *data, unsigned int len, u8 *out)
40{
41 sm3(data, len, out);
42 return 0;
43}
44
45static int crypto_sm3_export_core(struct shash_desc *desc, void *out)
46{
47 memcpy(out, SM3_CTX(desc), sizeof(struct sm3_ctx));
48 return 0;
49}
50
51static int crypto_sm3_import_core(struct shash_desc *desc, const void *in)
52{
53 memcpy(SM3_CTX(desc), in, sizeof(struct sm3_ctx));
54 return 0;
55}
56
57static struct shash_alg sm3_alg = {
58 .base.cra_name = "sm3",
59 .base.cra_driver_name = "sm3-lib",
60 .base.cra_priority = 300,
61 .base.cra_blocksize = SM3_BLOCK_SIZE,
62 .base.cra_module = THIS_MODULE,
63 .digestsize = SM3_DIGEST_SIZE,
64 .init = crypto_sm3_init,
65 .update = crypto_sm3_update,
66 .final = crypto_sm3_final,
67 .digest = crypto_sm3_digest,
68 .export_core = crypto_sm3_export_core,
69 .import_core = crypto_sm3_import_core,
70 .descsize = sizeof(struct sm3_ctx),
71};
72
73static int __init crypto_sm3_mod_init(void)
74{
75 return crypto_register_shash(&sm3_alg);
76}
77module_init(crypto_sm3_mod_init);
78
79static void __exit crypto_sm3_mod_exit(void)
80{
81 crypto_unregister_shash(&sm3_alg);
82}
83module_exit(crypto_sm3_mod_exit);
84
85MODULE_LICENSE("GPL v2");
86MODULE_DESCRIPTION("Crypto API support for SM3");
87
88MODULE_ALIAS_CRYPTO("sm3");
89MODULE_ALIAS_CRYPTO("sm3-lib");