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 18 #define SM3_IVA 0x7380166f 19 #define SM3_IVB 0x4914b2b9 20 #define SM3_IVC 0x172442d7 21 #define SM3_IVD 0xda8a0600 22 #define SM3_IVE 0xa96f30bc 23 #define SM3_IVF 0x163138aa 24 #define SM3_IVG 0xe38dee4d 25 #define SM3_IVH 0xb0fb0e4e 26 27 /* State for the SM3 compression function */ 28 struct sm3_block_state { 29 u32 h[SM3_DIGEST_SIZE / 4]; 30 }; 31 32 /** 33 * struct sm3_ctx - Context for hashing a message with SM3 34 * @state: the compression function state 35 * @bytecount: number of bytes processed so far 36 * @buf: partial block buffer; bytecount % SM3_BLOCK_SIZE bytes are valid 37 */ 38 struct sm3_ctx { 39 struct sm3_block_state state; 40 u64 bytecount; 41 u8 buf[SM3_BLOCK_SIZE] __aligned(__alignof__(__be64)); 42 }; 43 44 /** 45 * sm3_init() - Initialize an SM3 context for a new message 46 * @ctx: the context to initialize 47 * 48 * If you don't need incremental computation, consider sm3() instead. 49 * 50 * Context: Any context. 51 */ 52 void sm3_init(struct sm3_ctx *ctx); 53 54 /** 55 * sm3_update() - Update an SM3 context with message data 56 * @ctx: the context to update; must have been initialized 57 * @data: the message data 58 * @len: the data length in bytes 59 * 60 * This can be called any number of times. 61 * 62 * Context: Any context. 63 */ 64 void sm3_update(struct sm3_ctx *ctx, const u8 *data, size_t len); 65 66 /** 67 * sm3_final() - Finish computing an SM3 message digest 68 * @ctx: the context to finalize; must have been initialized 69 * @out: (output) the resulting SM3 message digest 70 * 71 * After finishing, this zeroizes @ctx. So the caller does not need to do it. 72 * 73 * Context: Any context. 74 */ 75 void sm3_final(struct sm3_ctx *ctx, u8 out[at_least SM3_DIGEST_SIZE]); 76 77 /** 78 * sm3() - Compute SM3 message digest in one shot 79 * @data: the message data 80 * @len: the data length in bytes 81 * @out: (output) the resulting SM3 message digest 82 * 83 * Context: Any context. 84 */ 85 void sm3(const u8 *data, size_t len, u8 out[at_least SM3_DIGEST_SIZE]); 86 87 #endif /* _CRYPTO_SM3_H */ 88