xref: /linux/include/crypto/sm3.h (revision d6781b8ba33ae9f6ab2e88c1158e989a24847c4b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * SM3 hash algorithm
4  *
5  * Copyright (C) 2017 ARM Limited or its affiliates.
6  * Copyright (C) 2017 Gilad Ben-Yossef <gilad@benyossef.com>
7  * Copyright (C) 2021 Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
8  */
9 
10 #ifndef _CRYPTO_SM3_H
11 #define _CRYPTO_SM3_H
12 
13 #include <linux/types.h>
14 
15 #define SM3_DIGEST_SIZE	32
16 #define SM3_BLOCK_SIZE	64
17 #define SM3_STATE_SIZE	40
18 
19 #define SM3_IVA		0x7380166f
20 #define SM3_IVB		0x4914b2b9
21 #define SM3_IVC		0x172442d7
22 #define SM3_IVD		0xda8a0600
23 #define SM3_IVE		0xa96f30bc
24 #define SM3_IVF		0x163138aa
25 #define SM3_IVG		0xe38dee4d
26 #define SM3_IVH		0xb0fb0e4e
27 
28 struct sm3_state {
29 	u32 state[SM3_DIGEST_SIZE / 4];
30 	u64 count;
31 	u8 buffer[SM3_BLOCK_SIZE];
32 };
33 
34 void sm3_block_generic(struct sm3_state *sctx, u8 const *data, int blocks);
35 
36 /* State for the SM3 compression function */
37 struct sm3_block_state {
38 	u32 h[SM3_DIGEST_SIZE / 4];
39 };
40 
41 /**
42  * struct sm3_ctx - Context for hashing a message with SM3
43  * @state: the compression function state
44  * @bytecount: number of bytes processed so far
45  * @buf: partial block buffer; bytecount % SM3_BLOCK_SIZE bytes are valid
46  */
47 struct sm3_ctx {
48 	struct sm3_block_state state;
49 	u64 bytecount;
50 	u8 buf[SM3_BLOCK_SIZE] __aligned(__alignof__(__be64));
51 };
52 
53 /**
54  * sm3_init() - Initialize an SM3 context for a new message
55  * @ctx: the context to initialize
56  *
57  * If you don't need incremental computation, consider sm3() instead.
58  *
59  * Context: Any context.
60  */
61 void sm3_init(struct sm3_ctx *ctx);
62 
63 /**
64  * sm3_update() - Update an SM3 context with message data
65  * @ctx: the context to update; must have been initialized
66  * @data: the message data
67  * @len: the data length in bytes
68  *
69  * This can be called any number of times.
70  *
71  * Context: Any context.
72  */
73 void sm3_update(struct sm3_ctx *ctx, const u8 *data, size_t len);
74 
75 /**
76  * sm3_final() - Finish computing an SM3 message digest
77  * @ctx: the context to finalize; must have been initialized
78  * @out: (output) the resulting SM3 message digest
79  *
80  * After finishing, this zeroizes @ctx.  So the caller does not need to do it.
81  *
82  * Context: Any context.
83  */
84 void sm3_final(struct sm3_ctx *ctx, u8 out[at_least SM3_DIGEST_SIZE]);
85 
86 /**
87  * sm3() - Compute SM3 message digest in one shot
88  * @data: the message data
89  * @len: the data length in bytes
90  * @out: (output) the resulting SM3 message digest
91  *
92  * Context: Any context.
93  */
94 void sm3(const u8 *data, size_t len, u8 out[at_least SM3_DIGEST_SIZE]);
95 
96 #endif /* _CRYPTO_SM3_H */
97