• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #ifndef GMSSL_SM3_H
12 #define GMSSL_SM3_H
13 
14 #include <string.h>
15 #include <stdint.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*
22 SM3 Public API
23 
24 	SM3_DIGEST_SIZE
25 	SM3_HMAC_SIZE
26 
27 	SM3_CTX
28 	sm3_init
29 	sm3_update
30 	sm3_finish
31 
32 	SM3_HMAC_CTX
33 	sm3_hmac_init
34 	sm3_hmac_update
35 	sm3_hmac_finish
36 
37 	sm3_digest
38 	sm3_hmac
39 */
40 
41 #define SM3_IS_BIG_ENDIAN	1
42 
43 #define SM3_DIGEST_SIZE		32
44 #define SM3_BLOCK_SIZE		64
45 #define SM3_STATE_WORDS		8
46 #define SM3_HMAC_SIZE		(SM3_DIGEST_SIZE)
47 
48 
49 typedef struct {
50 	uint32_t digest[SM3_STATE_WORDS];
51 	uint64_t nblocks;
52 	uint8_t block[SM3_BLOCK_SIZE];
53 	size_t num;
54 } SM3_CTX;
55 
56 void sm3_init(SM3_CTX *ctx);
57 void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen);
58 void sm3_finish(SM3_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
59 void sm3_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SM3_DIGEST_SIZE]);
60 
61 
62 typedef struct {
63 	SM3_CTX sm3_ctx;
64 	unsigned char key[SM3_BLOCK_SIZE];
65 } SM3_HMAC_CTX;
66 
67 void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t keylen);
68 void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t datalen);
69 void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]);
70 void sm3_hmac(const uint8_t *key, size_t keylen,
71 	const uint8_t *data, size_t datalen,
72 	uint8_t mac[SM3_HMAC_SIZE]);
73 
74 
75 typedef struct {
76 	SM3_CTX sm3_ctx;
77 	size_t outlen;
78 } SM3_KDF_CTX;
79 
80 void sm3_kdf_init(SM3_KDF_CTX *ctx, size_t outlen);
81 void sm3_kdf_update(SM3_KDF_CTX *ctx, const uint8_t *data, size_t datalen);
82 void sm3_kdf_finish(SM3_KDF_CTX *ctx, uint8_t *out);
83 
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 #endif
89