summaryrefslogtreecommitdiffstats
path: root/crypto/sm3.c
blob: 05111a99b851c70637c06da27ca8c0d2548714e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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");