xref: /freebsd/crypto/libecc/include/libecc/hash/sm3.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2021 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
6  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
7  *
8  *  This software is licensed under a dual BSD and GPL v2 license.
9  *  See LICENSE file at the root folder of the project.
10  */
11 #include <libecc/lib_ecc_config.h>
12 #ifdef WITH_HASH_SM3
13 
14 #ifndef __SM3_H__
15 #define __SM3_H__
16 
17 #include <libecc/words/words.h>
18 #include <libecc/utils/utils.h>
19 
20 #define SM3_STATE_SIZE    8 /* in 32 bits word */
21 #define SM3_BLOCK_SIZE   64
22 #define SM3_DIGEST_SIZE  32
23 #define SM3_DIGEST_SIZE_BITS  256
24 
25 /* Compute max hash digest and block sizes */
26 #ifndef MAX_DIGEST_SIZE
27 #define MAX_DIGEST_SIZE	0
28 #endif
29 #if (MAX_DIGEST_SIZE < SM3_DIGEST_SIZE)
30 #undef MAX_DIGEST_SIZE
31 #define MAX_DIGEST_SIZE SM3_DIGEST_SIZE
32 #endif
33 
34 #ifndef MAX_DIGEST_SIZE_BITS
35 #define MAX_DIGEST_SIZE_BITS    0
36 #endif
37 #if (MAX_DIGEST_SIZE_BITS < SM3_DIGEST_SIZE_BITS)
38 #undef MAX_DIGEST_SIZE_BITS
39 #define MAX_DIGEST_SIZE_BITS SM3_DIGEST_SIZE_BITS
40 #endif
41 
42 #ifndef MAX_BLOCK_SIZE
43 #define MAX_BLOCK_SIZE  0
44 #endif
45 #if (MAX_BLOCK_SIZE < SM3_BLOCK_SIZE)
46 #undef MAX_BLOCK_SIZE
47 #define MAX_BLOCK_SIZE SM3_BLOCK_SIZE
48 #endif
49 
50 #define SM3_HASH_MAGIC ((word_t)(0x2947510312849204ULL))
51 #define SM3_HASH_CHECK_INITIALIZED(A, ret, err) \
52         MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == SM3_HASH_MAGIC), ret, err)
53 
54 typedef struct {
55 	/* Number of bytes processed */
56 	u64 sm3_total;
57 	/* Internal state */
58 	u32 sm3_state[SM3_STATE_SIZE];
59 	/* Internal buffer to handle updates in a block */
60 	u8 sm3_buffer[SM3_BLOCK_SIZE];
61         /* Initialization magic value */
62         word_t magic;
63 } sm3_context;
64 
65 ATTRIBUTE_WARN_UNUSED_RET int sm3_init(sm3_context *ctx);
66 ATTRIBUTE_WARN_UNUSED_RET int sm3_update(sm3_context *ctx, const u8 *input, u32 ilen);
67 ATTRIBUTE_WARN_UNUSED_RET int sm3_final(sm3_context *ctx, u8 output[SM3_DIGEST_SIZE]);
68 ATTRIBUTE_WARN_UNUSED_RET int sm3_scattered(const u8 **inputs, const u32 *ilens,
69 		   u8 output[SM3_DIGEST_SIZE]);
70 ATTRIBUTE_WARN_UNUSED_RET int sm3(const u8 *input, u32 ilen, u8 output[SM3_DIGEST_SIZE]);
71 
72 #endif /* __SM3_H__ */
73 #endif /* WITH_HASH_SM3 */
74