xref: /linux/include/crypto/sm3.h (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
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 #include <linux/string.h>
15 
16 #define SM3_DIGEST_SIZE	32
17 #define SM3_BLOCK_SIZE	64
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 /* State for the SM3 compression function */
29 struct sm3_block_state {
30 	u32 h[SM3_DIGEST_SIZE / 4];
31 };
32 
33 /**
34  * struct sm3_ctx - Context for hashing a message with SM3
35  * @state: the compression function state
36  * @bytecount: number of bytes processed so far
37  * @buf: partial block buffer; bytecount % SM3_BLOCK_SIZE bytes are valid
38  */
39 struct sm3_ctx {
40 	struct sm3_block_state state;
41 	u64 bytecount;
42 	u8 buf[SM3_BLOCK_SIZE] __aligned(__alignof__(__be64));
43 };
44 
45 /**
46  * sm3_zeroize_ctx() - Zeroize an sm3_ctx structure
47  * @ctx: The sm3_ctx to zeroize
48  */
49 static inline void sm3_zeroize_ctx(struct sm3_ctx *ctx)
50 {
51 	memzero_explicit(ctx, sizeof(*ctx));
52 }
53 
54 /**
55  * sm3_init() - Initialize an SM3 context for a new message
56  * @ctx: the context to initialize
57  *
58  * If you don't need incremental computation, consider sm3() instead.
59  *
60  * Context: Any context.
61  */
62 void sm3_init(struct sm3_ctx *ctx);
63 
64 /**
65  * sm3_update() - Update an SM3 context with message data
66  * @ctx: the context to update; must have been initialized
67  * @data: the message data
68  * @len: the data length in bytes
69  *
70  * This can be called any number of times.
71  *
72  * Context: Any context.
73  */
74 void sm3_update(struct sm3_ctx *ctx, const u8 *data, size_t len);
75 
76 /**
77  * sm3_final() - Finish computing an SM3 message digest
78  * @ctx: the context to finalize; must have been initialized
79  * @out: (output) the resulting SM3 message digest
80  *
81  * After finishing, this zeroizes @ctx.  So the caller does not need to do it.
82  *
83  * Context: Any context.
84  */
85 void sm3_final(struct sm3_ctx *ctx, u8 out[at_least SM3_DIGEST_SIZE]);
86 
87 /**
88  * sm3() - Compute SM3 message digest in one shot
89  * @data: the message data
90  * @len: the data length in bytes
91  * @out: (output) the resulting SM3 message digest
92  *
93  * Context: Any context.
94  */
95 void sm3(const u8 *data, size_t len, u8 out[at_least SM3_DIGEST_SIZE]);
96 
97 #endif /* _CRYPTO_SM3_H */
98