/src/minizip-ng/mz_crypt_openssl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* mz_crypt_openssl.c -- Crypto/hash functions for OpenSSL |
2 | | part of the minizip-ng project |
3 | | |
4 | | Copyright (C) Nathan Moinvaziri |
5 | | https://github.com/zlib-ng/minizip-ng |
6 | | |
7 | | This program is distributed under the terms of the same license as zlib. |
8 | | See the accompanying LICENSE file for the full text of the license. |
9 | | */ |
10 | | |
11 | | #include "mz.h" |
12 | | #include "mz_crypt.h" |
13 | | |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/engine.h> |
16 | | #include <openssl/rand.h> |
17 | | #include <openssl/sha.h> |
18 | | #include <openssl/aes.h> |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/hmac.h> |
22 | | |
23 | | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
24 | | # include <openssl/core_names.h> |
25 | | #endif |
26 | | |
27 | | /***************************************************************************/ |
28 | | |
29 | 28.9M | static void mz_crypt_init(void) { |
30 | 28.9M | static int32_t openssl_initialized = 0; |
31 | 28.9M | if (!openssl_initialized) { |
32 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
33 | | OpenSSL_add_all_algorithms(); |
34 | | |
35 | | ERR_load_BIO_strings(); |
36 | | ERR_load_crypto_strings(); |
37 | | |
38 | | ENGINE_load_builtin_engines(); |
39 | | ENGINE_register_all_complete(); |
40 | | #else |
41 | 1 | OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN, NULL); |
42 | 1 | #endif |
43 | | |
44 | 1 | openssl_initialized = 1; |
45 | 1 | } |
46 | 28.9M | } |
47 | | |
48 | 186 | int32_t mz_crypt_rand(uint8_t *buf, int32_t size) { |
49 | 186 | if (!RAND_bytes(buf, size)) |
50 | 0 | return MZ_CRYPT_ERROR; |
51 | | |
52 | 186 | return size; |
53 | 186 | } |
54 | | |
55 | | /***************************************************************************/ |
56 | | |
57 | | typedef struct mz_crypt_sha_s { |
58 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
59 | | union { |
60 | | SHA512_CTX ctx512; |
61 | | SHA256_CTX ctx256; |
62 | | SHA_CTX ctx1; |
63 | | }; |
64 | | #else |
65 | | EVP_MD_CTX *ctx; |
66 | | #endif |
67 | | unsigned long error; |
68 | | int32_t initialized; |
69 | | uint16_t algorithm; |
70 | | } mz_crypt_sha; |
71 | | |
72 | | /***************************************************************************/ |
73 | | |
74 | | static const uint8_t mz_crypt_sha_digest_size[] = { |
75 | | MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE, MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE}; |
76 | | |
77 | | /***************************************************************************/ |
78 | | |
79 | 0 | static void mz_crypt_sha_free(void *handle) { |
80 | 0 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
81 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
82 | 0 | if (sha->ctx) |
83 | 0 | EVP_MD_CTX_free(sha->ctx); |
84 | 0 | sha->ctx = NULL; |
85 | | #else |
86 | | MZ_UNUSED(handle); |
87 | | #endif |
88 | 0 | } |
89 | | |
90 | 0 | void mz_crypt_sha_reset(void *handle) { |
91 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
92 | |
|
93 | 0 | mz_crypt_init(); |
94 | 0 | mz_crypt_sha_free(handle); |
95 | |
|
96 | 0 | sha->error = 0; |
97 | 0 | sha->initialized = 0; |
98 | 0 | } |
99 | | |
100 | 0 | int32_t mz_crypt_sha_begin(void *handle) { |
101 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
102 | 0 | int32_t result = 0; |
103 | |
|
104 | 0 | if (!sha) |
105 | 0 | return MZ_PARAM_ERROR; |
106 | | |
107 | 0 | mz_crypt_sha_reset(handle); |
108 | |
|
109 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
110 | | switch (sha->algorithm) { |
111 | | case MZ_HASH_SHA1: |
112 | | result = SHA1_Init(&sha->ctx1); |
113 | | break; |
114 | | case MZ_HASH_SHA224: |
115 | | result = SHA224_Init(&sha->ctx256); |
116 | | break; |
117 | | case MZ_HASH_SHA256: |
118 | | result = SHA256_Init(&sha->ctx256); |
119 | | break; |
120 | | case MZ_HASH_SHA384: |
121 | | result = SHA384_Init(&sha->ctx512); |
122 | | break; |
123 | | case MZ_HASH_SHA512: |
124 | | result = SHA512_Init(&sha->ctx512); |
125 | | break; |
126 | | } |
127 | | #else |
128 | 0 | const EVP_MD *md = NULL; |
129 | 0 | switch (sha->algorithm) { |
130 | 0 | case MZ_HASH_SHA1: |
131 | 0 | md = EVP_sha1(); |
132 | 0 | break; |
133 | 0 | case MZ_HASH_SHA224: |
134 | 0 | md = EVP_sha224(); |
135 | 0 | break; |
136 | 0 | case MZ_HASH_SHA256: |
137 | 0 | md = EVP_sha256(); |
138 | 0 | break; |
139 | 0 | case MZ_HASH_SHA384: |
140 | 0 | md = EVP_sha384(); |
141 | 0 | break; |
142 | 0 | case MZ_HASH_SHA512: |
143 | 0 | md = EVP_sha512(); |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | if (!md) |
147 | 0 | return MZ_PARAM_ERROR; |
148 | | |
149 | 0 | sha->ctx = EVP_MD_CTX_new(); |
150 | 0 | if (!sha->ctx) |
151 | 0 | return MZ_MEM_ERROR; |
152 | 0 | result = EVP_DigestInit_ex(sha->ctx, md, NULL); |
153 | 0 | #endif |
154 | |
|
155 | 0 | if (!result) { |
156 | 0 | sha->error = ERR_get_error(); |
157 | 0 | return MZ_HASH_ERROR; |
158 | 0 | } |
159 | | |
160 | 0 | sha->initialized = 1; |
161 | 0 | return MZ_OK; |
162 | 0 | } |
163 | | |
164 | 0 | int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) { |
165 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
166 | 0 | int32_t result = 0; |
167 | |
|
168 | 0 | if (!sha || !buf || !sha->initialized) |
169 | 0 | return MZ_PARAM_ERROR; |
170 | | |
171 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
172 | | switch (sha->algorithm) { |
173 | | case MZ_HASH_SHA1: |
174 | | result = SHA1_Update(&sha->ctx1, buf, size); |
175 | | break; |
176 | | case MZ_HASH_SHA224: |
177 | | result = SHA224_Update(&sha->ctx256, buf, size); |
178 | | break; |
179 | | case MZ_HASH_SHA256: |
180 | | result = SHA256_Update(&sha->ctx256, buf, size); |
181 | | break; |
182 | | case MZ_HASH_SHA384: |
183 | | result = SHA384_Update(&sha->ctx512, buf, size); |
184 | | break; |
185 | | case MZ_HASH_SHA512: |
186 | | result = SHA512_Update(&sha->ctx512, buf, size); |
187 | | break; |
188 | | } |
189 | | #else |
190 | 0 | result = EVP_DigestUpdate(sha->ctx, buf, size); |
191 | 0 | #endif |
192 | |
|
193 | 0 | if (!result) { |
194 | 0 | sha->error = ERR_get_error(); |
195 | 0 | return MZ_HASH_ERROR; |
196 | 0 | } |
197 | | |
198 | 0 | return size; |
199 | 0 | } |
200 | | |
201 | 0 | int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) { |
202 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
203 | 0 | int32_t result = 0; |
204 | |
|
205 | 0 | if (!sha || !digest || !sha->initialized) |
206 | 0 | return MZ_PARAM_ERROR; |
207 | 0 | if (digest_size < mz_crypt_sha_digest_size[sha->algorithm - MZ_HASH_SHA1]) |
208 | 0 | return MZ_PARAM_ERROR; |
209 | | |
210 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
211 | | switch (sha->algorithm) { |
212 | | case MZ_HASH_SHA1: |
213 | | result = SHA1_Final(digest, &sha->ctx1); |
214 | | break; |
215 | | case MZ_HASH_SHA224: |
216 | | result = SHA224_Final(digest, &sha->ctx256); |
217 | | break; |
218 | | case MZ_HASH_SHA256: |
219 | | result = SHA256_Final(digest, &sha->ctx256); |
220 | | break; |
221 | | case MZ_HASH_SHA384: |
222 | | result = SHA384_Final(digest, &sha->ctx512); |
223 | | break; |
224 | | case MZ_HASH_SHA512: |
225 | | result = SHA512_Final(digest, &sha->ctx512); |
226 | | break; |
227 | | } |
228 | | #else |
229 | 0 | result = EVP_DigestFinal_ex(sha->ctx, digest, NULL); |
230 | 0 | #endif |
231 | |
|
232 | 0 | if (!result) { |
233 | 0 | sha->error = ERR_get_error(); |
234 | 0 | return MZ_HASH_ERROR; |
235 | 0 | } |
236 | | |
237 | 0 | return MZ_OK; |
238 | 0 | } |
239 | | |
240 | 0 | int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) { |
241 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
242 | 0 | if (algorithm < MZ_HASH_SHA1 || algorithm > MZ_HASH_SHA512) |
243 | 0 | return MZ_PARAM_ERROR; |
244 | 0 | sha->algorithm = algorithm; |
245 | 0 | return MZ_OK; |
246 | 0 | } |
247 | | |
248 | 0 | void *mz_crypt_sha_create(void) { |
249 | 0 | mz_crypt_sha *sha = (mz_crypt_sha *)calloc(1, sizeof(mz_crypt_sha)); |
250 | 0 | if (sha) |
251 | 0 | sha->algorithm = MZ_HASH_SHA256; |
252 | 0 | return sha; |
253 | 0 | } |
254 | | |
255 | 0 | void mz_crypt_sha_delete(void **handle) { |
256 | 0 | mz_crypt_sha *sha = NULL; |
257 | 0 | if (!handle) |
258 | 0 | return; |
259 | 0 | sha = (mz_crypt_sha *)*handle; |
260 | 0 | if (sha) { |
261 | 0 | mz_crypt_sha_free(*handle); |
262 | 0 | free(sha); |
263 | 0 | } |
264 | 0 | *handle = NULL; |
265 | 0 | } |
266 | | |
267 | | /***************************************************************************/ |
268 | | |
269 | | typedef struct mz_crypt_aes_s { |
270 | | int32_t mode; |
271 | | unsigned long error; |
272 | | EVP_CIPHER_CTX *ctx; |
273 | | } mz_crypt_aes; |
274 | | |
275 | | /***************************************************************************/ |
276 | | |
277 | 40.0k | static void mz_crypt_aes_free(void *handle) { |
278 | 40.0k | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
279 | 40.0k | if (aes->ctx) |
280 | 13.3k | EVP_CIPHER_CTX_free(aes->ctx); |
281 | 40.0k | aes->ctx = NULL; |
282 | 40.0k | } |
283 | | |
284 | 26.6k | void mz_crypt_aes_reset(void *handle) { |
285 | 26.6k | mz_crypt_init(); |
286 | 26.6k | mz_crypt_aes_free(handle); |
287 | 26.6k | } |
288 | | |
289 | 438k | int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
290 | 438k | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
291 | | |
292 | 438k | if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx) |
293 | 0 | return MZ_PARAM_ERROR; |
294 | 438k | if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0) |
295 | 0 | return MZ_PARAM_ERROR; |
296 | | |
297 | 438k | if (aad && aad_size > 0) { |
298 | 0 | int32_t how_many = 0; |
299 | 0 | if (!EVP_EncryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size)) |
300 | 0 | return MZ_CRYPT_ERROR; |
301 | 0 | } |
302 | | |
303 | 438k | if (!EVP_EncryptUpdate(aes->ctx, buf, &size, buf, size)) |
304 | 0 | return MZ_CRYPT_ERROR; |
305 | | |
306 | 438k | return size; |
307 | 438k | } |
308 | | |
309 | 0 | int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) { |
310 | 0 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
311 | 0 | int result = 0; |
312 | 0 | int out_len = 0; |
313 | |
|
314 | 0 | if (!aes || !tag || !tag_size || !aes->ctx || aes->mode != MZ_AES_MODE_GCM) |
315 | 0 | return MZ_PARAM_ERROR; |
316 | | |
317 | 0 | if (buf && size) { |
318 | 0 | if (!EVP_EncryptUpdate(aes->ctx, buf, &size, buf, size)) |
319 | 0 | return MZ_CRYPT_ERROR; |
320 | 0 | } |
321 | | |
322 | | /* Must call EncryptFinal for tag to be calculated */ |
323 | 0 | result = EVP_EncryptFinal_ex(aes->ctx, NULL, &out_len); |
324 | |
|
325 | 0 | if (result) |
326 | 0 | result = EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_GET_TAG, tag_size, tag); |
327 | |
|
328 | 0 | if (!result) { |
329 | 0 | aes->error = ERR_get_error(); |
330 | 0 | return MZ_CRYPT_ERROR; |
331 | 0 | } |
332 | | |
333 | 0 | return size; |
334 | 0 | } |
335 | | |
336 | 0 | int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
337 | 0 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
338 | |
|
339 | 0 | if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx) |
340 | 0 | return MZ_PARAM_ERROR; |
341 | 0 | if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0) |
342 | 0 | return MZ_PARAM_ERROR; |
343 | | |
344 | 0 | if (aad && aad_size > 0) { |
345 | 0 | int32_t how_many = 0; |
346 | 0 | if (!EVP_DecryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size)) |
347 | 0 | return MZ_CRYPT_ERROR; |
348 | 0 | } |
349 | | |
350 | 0 | if (!EVP_DecryptUpdate(aes->ctx, buf, &size, buf, size)) |
351 | 0 | return MZ_CRYPT_ERROR; |
352 | | |
353 | 0 | return size; |
354 | 0 | } |
355 | | |
356 | 0 | int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) { |
357 | 0 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
358 | 0 | int out_len = 0; |
359 | |
|
360 | 0 | if (!aes || !tag || !tag_length || !aes->ctx || aes->mode != MZ_AES_MODE_GCM) |
361 | 0 | return MZ_PARAM_ERROR; |
362 | | |
363 | 0 | if (buf && size) { |
364 | 0 | if (!EVP_DecryptUpdate(aes->ctx, buf, &size, buf, size)) |
365 | 0 | return MZ_CRYPT_ERROR; |
366 | 0 | } |
367 | | |
368 | | /* Set expected tag */ |
369 | 0 | if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG, tag_length, (void *)tag)) { |
370 | 0 | aes->error = ERR_get_error(); |
371 | 0 | return MZ_CRYPT_ERROR; |
372 | 0 | } |
373 | | |
374 | | /* Must call DecryptFinal for tag verification */ |
375 | 0 | if (!EVP_DecryptFinal_ex(aes->ctx, NULL, &out_len)) { |
376 | 0 | aes->error = ERR_get_error(); |
377 | 0 | return MZ_CRYPT_ERROR; |
378 | 0 | } |
379 | | |
380 | 0 | return size; |
381 | 0 | } |
382 | | |
383 | | static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv, |
384 | 13.3k | int32_t iv_length, int32_t encrypt) { |
385 | 13.3k | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
386 | 13.3k | const EVP_CIPHER *type = NULL; |
387 | | |
388 | 13.3k | switch (aes->mode) { |
389 | 0 | case MZ_AES_MODE_CBC: |
390 | 0 | if (key_length == 16) |
391 | 0 | type = EVP_aes_128_cbc(); |
392 | 0 | else if (key_length == 24) |
393 | 0 | type = EVP_aes_192_cbc(); |
394 | 0 | else if (key_length == 32) |
395 | 0 | type = EVP_aes_256_cbc(); |
396 | 0 | break; |
397 | 13.3k | case MZ_AES_MODE_ECB: |
398 | 13.3k | if (key_length == 16) |
399 | 12.0k | type = EVP_aes_128_ecb(); |
400 | 1.30k | else if (key_length == 24) |
401 | 449 | type = EVP_aes_192_ecb(); |
402 | 858 | else if (key_length == 32) |
403 | 858 | type = EVP_aes_256_ecb(); |
404 | 13.3k | break; |
405 | 0 | case MZ_AES_MODE_GCM: |
406 | 0 | if (key_length == 16) |
407 | 0 | type = EVP_aes_128_gcm(); |
408 | 0 | else if (key_length == 24) |
409 | 0 | type = EVP_aes_192_gcm(); |
410 | 0 | else if (key_length == 32) |
411 | 0 | type = EVP_aes_256_gcm(); |
412 | 0 | break; |
413 | 13.3k | } |
414 | 13.3k | if (!type) |
415 | 0 | return MZ_PARAM_ERROR; |
416 | | |
417 | 13.3k | aes->ctx = EVP_CIPHER_CTX_new(); |
418 | 13.3k | if (!aes->ctx) |
419 | 0 | return MZ_MEM_ERROR; |
420 | | |
421 | 13.3k | if (!EVP_CipherInit_ex(aes->ctx, type, NULL, key, iv, encrypt)) { |
422 | 0 | aes->error = ERR_get_error(); |
423 | 0 | return MZ_HASH_ERROR; |
424 | 0 | } |
425 | | |
426 | 13.3k | EVP_CIPHER_CTX_set_padding(aes->ctx, aes->mode == MZ_AES_MODE_GCM); |
427 | | |
428 | 13.3k | return MZ_OK; |
429 | 13.3k | } |
430 | | |
431 | | int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
432 | 13.3k | int32_t iv_length) { |
433 | 13.3k | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
434 | | |
435 | 13.3k | if (!aes || !key || !key_length) |
436 | 0 | return MZ_PARAM_ERROR; |
437 | 13.3k | if (key_length != 16 && key_length != 24 && key_length != 32) |
438 | 0 | return MZ_PARAM_ERROR; |
439 | 13.3k | if (iv && iv_length != MZ_AES_BLOCK_SIZE) |
440 | 0 | return MZ_PARAM_ERROR; |
441 | | |
442 | 13.3k | mz_crypt_aes_reset(handle); |
443 | | |
444 | 13.3k | return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, 1); |
445 | 13.3k | } |
446 | | |
447 | | int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
448 | 0 | int32_t iv_length) { |
449 | 0 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
450 | |
|
451 | 0 | if (!aes || !key || !key_length) |
452 | 0 | return MZ_PARAM_ERROR; |
453 | 0 | if (key_length != 16 && key_length != 24 && key_length != 32) |
454 | 0 | return MZ_PARAM_ERROR; |
455 | 0 | if (iv && iv_length > MZ_AES_BLOCK_SIZE) |
456 | 0 | return MZ_PARAM_ERROR; |
457 | | |
458 | 0 | mz_crypt_aes_reset(handle); |
459 | |
|
460 | 0 | return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, 0); |
461 | 0 | } |
462 | | |
463 | 0 | void mz_crypt_aes_set_mode(void *handle, int32_t mode) { |
464 | 0 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
465 | 0 | aes->mode = mode; |
466 | 0 | } |
467 | | |
468 | 13.3k | void *mz_crypt_aes_create(void) { |
469 | 13.3k | mz_crypt_aes *aes = (mz_crypt_aes *)calloc(1, sizeof(mz_crypt_aes)); |
470 | 13.3k | return aes; |
471 | 13.3k | } |
472 | | |
473 | 13.3k | void mz_crypt_aes_delete(void **handle) { |
474 | 13.3k | mz_crypt_aes *aes = NULL; |
475 | 13.3k | if (!handle) |
476 | 0 | return; |
477 | 13.3k | aes = (mz_crypt_aes *)*handle; |
478 | 13.3k | if (aes) { |
479 | 13.3k | mz_crypt_aes_free(*handle); |
480 | 13.3k | free(aes); |
481 | 13.3k | } |
482 | 13.3k | *handle = NULL; |
483 | 13.3k | } |
484 | | |
485 | | /***************************************************************************/ |
486 | | |
487 | | typedef struct mz_crypt_hmac_s { |
488 | | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
489 | | HMAC_CTX *ctx; |
490 | | #else |
491 | | EVP_MAC *mac; |
492 | | EVP_MAC_CTX *ctx; |
493 | | #endif |
494 | | unsigned long error; |
495 | | int32_t initialized; |
496 | | uint16_t algorithm; |
497 | | } mz_crypt_hmac; |
498 | | |
499 | | /***************************************************************************/ |
500 | | |
501 | | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \ |
502 | | (defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x2070000fL)) |
503 | | static HMAC_CTX *HMAC_CTX_new(void) { |
504 | | HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX)); |
505 | | if (ctx) |
506 | | HMAC_CTX_init(ctx); |
507 | | return ctx; |
508 | | } |
509 | | |
510 | | static void HMAC_CTX_free(HMAC_CTX *ctx) { |
511 | | if (ctx) { |
512 | | HMAC_CTX_cleanup(ctx); |
513 | | OPENSSL_free(ctx); |
514 | | } |
515 | | } |
516 | | #endif |
517 | | |
518 | | /***************************************************************************/ |
519 | | |
520 | 28.9M | static void mz_crypt_hmac_free(void *handle) { |
521 | 28.9M | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
522 | | |
523 | 28.9M | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
524 | 28.9M | HMAC_CTX_free(hmac->ctx); |
525 | | #else |
526 | | if (hmac->ctx) |
527 | | EVP_MAC_CTX_free(hmac->ctx); |
528 | | if (hmac->mac) |
529 | | EVP_MAC_free(hmac->mac); |
530 | | hmac->mac = NULL; |
531 | | #endif |
532 | | |
533 | 28.9M | hmac->ctx = NULL; |
534 | 28.9M | } |
535 | | |
536 | 28.9M | void mz_crypt_hmac_reset(void *handle) { |
537 | 28.9M | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
538 | | |
539 | 28.9M | mz_crypt_init(); |
540 | 28.9M | mz_crypt_hmac_free(handle); |
541 | | |
542 | 28.9M | hmac->error = 0; |
543 | 28.9M | } |
544 | | |
545 | 39.9k | int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) { |
546 | 39.9k | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
547 | 39.9k | int32_t result = 0; |
548 | | |
549 | 39.9k | if (!hmac || !key) |
550 | 0 | return MZ_PARAM_ERROR; |
551 | | |
552 | 39.9k | mz_crypt_hmac_reset(handle); |
553 | | |
554 | 39.9k | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
555 | 39.9k | const EVP_MD *evp_md = NULL; |
556 | | |
557 | 39.9k | if (hmac->algorithm == MZ_HASH_SHA1) |
558 | 39.9k | evp_md = EVP_sha1(); |
559 | 0 | else |
560 | 0 | evp_md = EVP_sha256(); |
561 | | |
562 | 39.9k | hmac->ctx = HMAC_CTX_new(); |
563 | 39.9k | if (!hmac->ctx) |
564 | 0 | return MZ_MEM_ERROR; |
565 | | |
566 | 39.9k | result = HMAC_Init_ex(hmac->ctx, key, key_length, evp_md, NULL); |
567 | | #else |
568 | | char *digest_algorithm = NULL; |
569 | | OSSL_PARAM params[2]; |
570 | | |
571 | | if (hmac->algorithm == MZ_HASH_SHA1) |
572 | | digest_algorithm = "sha1"; |
573 | | else |
574 | | digest_algorithm = "sha256"; |
575 | | |
576 | | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_algorithm, 0); |
577 | | params[1] = OSSL_PARAM_construct_end(); |
578 | | |
579 | | hmac->mac = EVP_MAC_fetch(NULL, "HMAC", NULL); |
580 | | if (!hmac->mac) |
581 | | return MZ_MEM_ERROR; |
582 | | hmac->ctx = EVP_MAC_CTX_new(hmac->mac); |
583 | | if (!hmac->ctx) |
584 | | return MZ_MEM_ERROR; |
585 | | result = EVP_MAC_init(hmac->ctx, key, key_length, params); |
586 | | #endif |
587 | | |
588 | 39.9k | if (!result) { |
589 | 0 | hmac->error = ERR_get_error(); |
590 | 0 | return MZ_HASH_ERROR; |
591 | 0 | } |
592 | | |
593 | 39.9k | return MZ_OK; |
594 | 39.9k | } |
595 | | |
596 | 28.8M | int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) { |
597 | 28.8M | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
598 | 28.8M | int32_t result = 0; |
599 | | |
600 | 28.8M | if (!hmac || !buf) |
601 | 0 | return MZ_PARAM_ERROR; |
602 | | |
603 | 28.8M | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
604 | 28.8M | result = HMAC_Update(hmac->ctx, buf, size); |
605 | | #else |
606 | | result = EVP_MAC_update(hmac->ctx, buf, size); |
607 | | #endif |
608 | 28.8M | if (!result) { |
609 | 0 | hmac->error = ERR_get_error(); |
610 | 0 | return MZ_HASH_ERROR; |
611 | 0 | } |
612 | | |
613 | 28.8M | return MZ_OK; |
614 | 28.8M | } |
615 | | |
616 | 28.8M | int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size) { |
617 | 28.8M | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
618 | 28.8M | int32_t result = 0; |
619 | | |
620 | 28.8M | if (!hmac || !digest) |
621 | 0 | return MZ_PARAM_ERROR; |
622 | | |
623 | 28.8M | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
624 | 28.8M | if (hmac->algorithm == MZ_HASH_SHA1) { |
625 | 28.8M | if (digest_size < MZ_HASH_SHA1_SIZE) |
626 | 0 | return MZ_BUF_ERROR; |
627 | | |
628 | 28.8M | result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size); |
629 | 28.8M | } else { |
630 | 0 | if (digest_size < MZ_HASH_SHA256_SIZE) |
631 | 0 | return MZ_BUF_ERROR; |
632 | 0 | result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size); |
633 | 0 | } |
634 | | #else |
635 | | { |
636 | | size_t digest_outsize = digest_size; |
637 | | result = EVP_MAC_final(hmac->ctx, digest, &digest_outsize, digest_size); |
638 | | } |
639 | | #endif |
640 | | |
641 | 28.8M | if (!result) { |
642 | 0 | hmac->error = ERR_get_error(); |
643 | 0 | return MZ_HASH_ERROR; |
644 | 0 | } |
645 | | |
646 | 28.8M | return MZ_OK; |
647 | 28.8M | } |
648 | | |
649 | 53.3k | void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm) { |
650 | 53.3k | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
651 | 53.3k | hmac->algorithm = algorithm; |
652 | 53.3k | } |
653 | | |
654 | 28.8M | int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle) { |
655 | 28.8M | mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle; |
656 | 28.8M | mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle; |
657 | | |
658 | 28.8M | if (!source || !target) |
659 | 0 | return MZ_PARAM_ERROR; |
660 | | |
661 | 28.8M | mz_crypt_hmac_reset(target_handle); |
662 | | |
663 | 28.8M | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
664 | 28.8M | if (!target->ctx) |
665 | 28.8M | target->ctx = HMAC_CTX_new(); |
666 | | |
667 | 28.8M | if (!HMAC_CTX_copy(target->ctx, source->ctx)) { |
668 | 0 | target->error = ERR_get_error(); |
669 | 0 | return MZ_HASH_ERROR; |
670 | 0 | } |
671 | | #else |
672 | | if (!target->ctx) |
673 | | target->ctx = EVP_MAC_CTX_dup(source->ctx); |
674 | | if (!target->ctx) |
675 | | return MZ_MEM_ERROR; |
676 | | #endif |
677 | | |
678 | 28.8M | return MZ_OK; |
679 | 28.8M | } |
680 | | |
681 | 53.3k | void *mz_crypt_hmac_create(void) { |
682 | 53.3k | mz_crypt_hmac *hmac = (mz_crypt_hmac *)calloc(1, sizeof(mz_crypt_hmac)); |
683 | 53.3k | if (hmac) |
684 | 53.3k | hmac->algorithm = MZ_HASH_SHA256; |
685 | 53.3k | return hmac; |
686 | 53.3k | } |
687 | | |
688 | 53.3k | void mz_crypt_hmac_delete(void **handle) { |
689 | 53.3k | mz_crypt_hmac *hmac = NULL; |
690 | 53.3k | if (!handle) |
691 | 0 | return; |
692 | 53.3k | hmac = (mz_crypt_hmac *)*handle; |
693 | 53.3k | if (hmac) { |
694 | 53.3k | mz_crypt_hmac_free(*handle); |
695 | 53.3k | free(hmac); |
696 | 53.3k | } |
697 | 53.3k | *handle = NULL; |
698 | 53.3k | } |