HashApi.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HashApi.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <sming_attr.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #ifdef ARCH_ESP8266
18 #define USE_ESP_CRYPTO
19 #include <esp_crypto.h>
20 #endif
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define CRYPTO_NAME(hash, name) crypto_##hash##_##name
27 #define CRYPTO_CTX(hash) CRYPTO_NAME(hash, context_t)
28 #define CRYPTO_FUNC_INIT(hash) void CRYPTO_NAME(hash, init)(CRYPTO_CTX(hash) * ctx)
29 #define CRYPTO_FUNC_UPDATE(hash) \
30  void CRYPTO_NAME(hash, update)(CRYPTO_CTX(hash) * ctx, const void* input, uint32_t length)
31 #define CRYPTO_FUNC_FINAL(hash) void CRYPTO_NAME(hash, final)(uint8_t * digest, CRYPTO_CTX(hash) * ctx)
32 #define CRYPTO_FUNC_GET_STATE(hash) uint64_t CRYPTO_NAME(hash, get_state)(CRYPTO_CTX(hash) * ctx, void* state)
33 #define CRYPTO_FUNC_SET_STATE(hash) \
34  void CRYPTO_NAME(hash, set_state)(CRYPTO_CTX(hash) * ctx, const void* state, uint64_t count)
35 
36 #define CRYPTO_FUNC_HMAC(hash) \
37  void CRYPTO_NAME(hash, hmac)(const uint8_t* msg, int msg_len, const uint8_t* key, int key_len, uint8_t* digest)
38 #define CRYPTO_FUNC_HMAC_V(hash) \
39  void CRYPTO_NAME(hash, hmac_v)(const uint8_t** msg, int* msg_len, int count, const uint8_t* key, int key_len, \
40  uint8_t* digest)
41 
42 /*
43  * MD5
44  */
45 
46 #define MD5_SIZE 16
47 #define MD5_STATESIZE 16
48 #define MD5_BLOCKSIZE 64
49 
50 #ifdef USE_ESP_CRYPTO
51 
52 #define crypto_md5_context_t ESP_MD5_CTX
53 
54 #define crypto_md5_init ESP_MD5_Init
55 #define crypto_md5_update ESP_MD5_Update
56 #define crypto_md5_final ESP_MD5_Final
57 
58 #else
59 
60 typedef struct {
61  uint32_t state[MD5_STATESIZE / 4];
62  uint32_t count;
63  uint8_t buffer[MD5_BLOCKSIZE];
65 
66 CRYPTO_FUNC_INIT(md5);
69 
70 #endif
71 
74 
75 #ifdef USE_ESP_CRYPTO
76 
77 static inline CRYPTO_FUNC_HMAC(md5)
78 {
79  ESP_hmac_md5_v(key, key_len, 1, &msg, &msg_len, digest);
80 }
81 
82 static inline CRYPTO_FUNC_HMAC_V(md5)
83 {
84  ESP_hmac_md5_v(key, key_len, count, msg, msg_len, digest);
85 }
86 
87 #else
88 
90 
91 static inline CRYPTO_FUNC_HMAC(md5)
92 {
93  crypto_md5_hmac_v(&msg, &msg_len, 1, key, key_len, digest);
94 }
95 
96 #endif
97 
98 /*
99  * SHA1
100  */
101 
102 #define SHA1_SIZE 20
103 #define SHA1_STATESIZE 20
104 #define SHA1_BLOCKSIZE 64
105 
106 #ifdef USE_ESP_CRYPTO
107 
108 #define crypto_sha1_context_t ESP_SHA1_CTX
109 #define crypto_sha1_init ESP_SHA1_Init
110 #define crypto_sha1_update ESP_SHA1_Update
111 #define crypto_sha1_final ESP_SHA1_Final
112 
113 #else
114 
115 typedef struct {
116  uint32_t state[SHA1_STATESIZE / 4];
117  uint32_t count;
118  uint8_t buffer[SHA1_BLOCKSIZE];
120 
124 
125 #endif
126 
129 
130 #ifdef USE_ESP_CRYPTO
131 
132 static inline CRYPTO_FUNC_HMAC(sha1)
133 {
134  ESP_hmac_sha1_v(key, key_len, 1, &msg, &msg_len, digest);
135 }
136 
137 static inline CRYPTO_FUNC_HMAC_V(sha1)
138 {
139  ESP_hmac_sha1_v(key, key_len, count, msg, msg_len, digest);
140 }
141 
142 #else
143 
145 
146 static inline CRYPTO_FUNC_HMAC(sha1)
147 {
148  crypto_sha1_hmac_v(&msg, &msg_len, 1, key, key_len, digest);
149 }
150 
151 #endif
152 
153 /*
154  * SHA224
155  */
156 
157 #define SHA224_SIZE 28
158 #define SHA224_STATESIZE 32
159 #define SHA224_BLOCKSIZE 64
160 
161 typedef struct {
162  uint32_t state[8];
163  uint32_t count;
164  uint8_t buffer[SHA224_BLOCKSIZE];
166 
167 CRYPTO_FUNC_INIT(sha224);
168 CRYPTO_FUNC_UPDATE(sha224);
169 CRYPTO_FUNC_FINAL(sha224);
170 CRYPTO_FUNC_GET_STATE(sha224);
171 CRYPTO_FUNC_SET_STATE(sha224);
172 
173 /*
174  * SHA256
175  */
176 
177 #define SHA256_SIZE 32
178 #define SHA256_STATESIZE 32
179 #define SHA256_BLOCKSIZE 64
180 
182 
183 CRYPTO_FUNC_INIT(sha256);
184 CRYPTO_FUNC_UPDATE(sha256);
185 CRYPTO_FUNC_FINAL(sha256);
186 CRYPTO_FUNC_GET_STATE(sha256);
187 CRYPTO_FUNC_SET_STATE(sha256);
188 
189 CRYPTO_FUNC_HMAC_V(sha256);
190 static inline CRYPTO_FUNC_HMAC(sha256)
191 {
192  crypto_sha256_hmac_v(&msg, &msg_len, 1, key, key_len, digest);
193 }
194 
195 /*
196  * SHA384
197  */
198 
199 #define SHA384_SIZE 48
200 #define SHA384_STATESIZE 64
201 #define SHA384_BLOCKSIZE 128
202 
203 typedef struct {
204  uint64_t state[8];
205  uint32_t count;
206  uint8_t buffer[SHA384_BLOCKSIZE];
208 
209 CRYPTO_FUNC_INIT(sha384);
210 CRYPTO_FUNC_UPDATE(sha384);
211 CRYPTO_FUNC_FINAL(sha384);
212 CRYPTO_FUNC_GET_STATE(sha384);
213 CRYPTO_FUNC_SET_STATE(sha384);
214 
215 CRYPTO_FUNC_HMAC_V(sha384);
216 
217 static inline CRYPTO_FUNC_HMAC(sha384)
218 {
219  crypto_sha384_hmac_v(&msg, &msg_len, 1, key, key_len, digest);
220 }
221 
222 /*
223  * SHA512
224  */
225 
226 #define SHA512_SIZE 64
227 #define SHA512_STATESIZE 64
228 #define SHA512_BLOCKSIZE 128
229 
231 
232 CRYPTO_FUNC_INIT(sha512);
233 CRYPTO_FUNC_UPDATE(sha512);
234 CRYPTO_FUNC_FINAL(sha512);
235 CRYPTO_FUNC_GET_STATE(sha512);
236 CRYPTO_FUNC_SET_STATE(sha512);
237 
238 CRYPTO_FUNC_HMAC_V(sha512);
239 
240 static inline CRYPTO_FUNC_HMAC(sha512)
241 {
242  crypto_sha512_hmac_v(&msg, &msg_len, 1, key, key_len, digest);
243 }
244 
245 #ifdef __cplusplus
246 }
247 #endif
#define MD5_STATESIZE
Definition: HashApi.h:47
#define CRYPTO_FUNC_UPDATE(hash)
Definition: HashApi.h:29
#define CRYPTO_FUNC_HMAC(hash)
Definition: HashApi.h:36
uint32_t count
Definition: HashApi.h:205
#define CRYPTO_FUNC_FINAL(hash)
Definition: HashApi.h:31
#define SHA384_BLOCKSIZE
Definition: HashApi.h:201
uint32_t count
Definition: HashApi.h:163
void sha1(unsigned char h[SHA1_SIZE], const void *_sha1_restrict p, size_t n)
Definition: HashApi.h:161
#define MD5_BLOCKSIZE
Definition: HashApi.h:48
#define SHA1_BLOCKSIZE
Definition: HashApi.h:104
#define SHA224_BLOCKSIZE
Definition: HashApi.h:159
uint32_t count
Bytes in message.
Definition: HashApi.h:62
crypto_sha384_context_t crypto_sha512_context_t
Definition: HashApi.h:230
#define SHA1_STATESIZE
Definition: HashApi.h:103
Definition: HashApi.h:115
Definition: HashApi.h:60
#define CRYPTO_FUNC_GET_STATE(hash)
Definition: HashApi.h:32
Definition: HashApi.h:203
uint32_t count
Message length in bytes.
Definition: HashApi.h:117
#define CRYPTO_FUNC_HMAC_V(hash)
Definition: HashApi.h:38
#define CRYPTO_FUNC_INIT(hash)
Definition: HashApi.h:28
#define CRYPTO_FUNC_SET_STATE(hash)
Definition: HashApi.h:33
crypto_sha224_context_t crypto_sha256_context_t
Definition: HashApi.h:181