projects/linux·blob master · refs

97 lines · 2.5 KiB

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 <[email protected]>
7 * Copyright (C) 2021 Tianjia Zhang <[email protected]>
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 */
29struct 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 */
39struct 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 */
49static 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 */
62void 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 */
74void 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 */
85void 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 */
95void sm3(const u8 *data, size_t len, u8 out[at_least SM3_DIGEST_SIZE]);
96
97#endif /* _CRYPTO_SM3_H */