Debian Codegraph @

mz_crypt_winvista.c

raw symbols SCIP
1 /* mz_crypt_winrt.c -- Crypto/hash functions for Windows Vista or later
2 part of the minizip-ng project
3
4 Copyright (C) 2010-2022 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_os.h"
13 #include "mz_crypt.h"
14
15 #include <windows.h>
16
17 #ifndef _WIN32_WINNT_VISTA
18 # define _WIN32_WINNT_VISTA 0x0600
19 #endif
20
21 #if _WIN32_WINNT >= _WIN32_WINNT_VISTA
22 # include <bcrypt.h>
23
24 /***************************************************************************/
25
26 # define NT_SUCCESS(status) ((status) >= 0)
27
28 /***************************************************************************/
29
30 int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
31 BCRYPT_ALG_HANDLE provider = NULL;
32 NTSTATUS status = 0;
33
34 status = BCryptOpenAlgorithmProvider(&provider, BCRYPT_RNG_ALGORITHM, NULL, 0);
35 if (NT_SUCCESS(status)) {
36 status = BCryptGenRandom(provider, buf, size, 0);
37 BCryptCloseAlgorithmProvider(provider, 0);
38 }
39 if (NT_SUCCESS(status))
40 return size;
41
42 return mz_os_rand(buf, size);
43 }
44
45 /***************************************************************************/
46
47 typedef struct mz_crypt_sha_s {
48 union {
49 struct {
50 BCRYPT_ALG_HANDLE provider;
51 BCRYPT_HASH_HANDLE hash;
52 uint8_t *buffer;
53 };
54 };
55 int32_t error;
56 uint16_t algorithm;
57 } mz_crypt_sha;
58
59 /***************************************************************************/
60
61 static void mz_crypt_sha_free(void *handle) {
62 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
63 if (sha->hash)
64 BCryptDestroyHash(sha->hash);
65 sha->hash = NULL;
66 if (sha->provider)
67 BCryptCloseAlgorithmProvider(sha->provider, 0);
68 sha->provider = NULL;
69 free(sha->buffer);
70 sha->buffer = NULL;
71 }
72
73 void mz_crypt_sha_reset(void *handle) {
74 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
75 mz_crypt_sha_free(handle);
76 sha->error = 0;
77 }
78
79 int32_t mz_crypt_sha_begin(void *handle) {
80 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
81 NTSTATUS status = 0;
82 const wchar_t *alg_id = BCRYPT_SHA256_ALGORITHM;
83 ULONG buffer_size = 0;
84 ULONG result_size = sizeof(buffer_size);
85 int32_t err = MZ_OK;
86
87 if (!sha)
88 return MZ_PARAM_ERROR;
89
90 if (sha->algorithm == MZ_HASH_SHA224)
91 return MZ_SUPPORT_ERROR;
92
93 switch (sha->algorithm) {
94 case MZ_HASH_SHA1:
95 alg_id = BCRYPT_SHA1_ALGORITHM;
96 break;
97 case MZ_HASH_SHA384:
98 alg_id = BCRYPT_SHA384_ALGORITHM;
99 break;
100 case MZ_HASH_SHA512:
101 alg_id = BCRYPT_SHA512_ALGORITHM;
102 break;
103 }
104
105 status = BCryptOpenAlgorithmProvider(&sha->provider, alg_id, NULL, 0);
106 if (NT_SUCCESS(status)) {
107 status =
108 BCryptGetProperty(sha->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size, &result_size, 0);
109 }
110 if (NT_SUCCESS(status)) {
111 sha->buffer = malloc(buffer_size);
112 if (!sha->buffer)
113 return MZ_MEM_ERROR;
114 status = BCryptCreateHash(sha->provider, &sha->hash, sha->buffer, buffer_size, NULL, 0, 0);
115 }
116 if (!NT_SUCCESS(status)) {
117 sha->error = status;
118 err = MZ_HASH_ERROR;
119 }
120
121 return err;
122 }
123
124 int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
125 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
126 NTSTATUS status = 0;
127
128 if (!sha || !buf || size < 0)
129 return MZ_PARAM_ERROR;
130
131 if (sha->algorithm == MZ_HASH_SHA224)
132 return MZ_SUPPORT_ERROR;
133
134 if (sha->hash == 0)
135 return MZ_PARAM_ERROR;
136
137 status = BCryptHashData(sha->hash, (uint8_t *)buf, size, 0);
138 if (!NT_SUCCESS(status)) {
139 sha->error = status;
140 return MZ_HASH_ERROR;
141 }
142 return size;
143 }
144
145 int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
146 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
147 NTSTATUS status = 0;
148 ULONG expected_size = 0;
149 ULONG result_size = sizeof(expected_size);
150
151 if (!sha || !digest)
152 return MZ_PARAM_ERROR;
153
154 if (sha->algorithm == MZ_HASH_SHA224)
155 return MZ_SUPPORT_ERROR;
156
157 if (sha->hash == 0)
158 return MZ_PARAM_ERROR;
159
160 status = BCryptGetProperty(sha->hash, BCRYPT_HASH_LENGTH, (PUCHAR)&expected_size, result_size, &result_size, 0);
161 if (!NT_SUCCESS(status))
162 return MZ_HASH_ERROR;
163 if ((int32_t)expected_size > digest_size)
164 return MZ_BUF_ERROR;
165 status = BCryptFinishHash(sha->hash, digest, expected_size, 0);
166 if (!NT_SUCCESS(status)) {
167 sha->error = status;
168 return MZ_HASH_ERROR;
169 }
170 return MZ_OK;
171 }
172
173 int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) {
174 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
175 if (algorithm < MZ_HASH_SHA1 || algorithm > MZ_HASH_SHA512)
176 return MZ_PARAM_ERROR;
177 sha->algorithm = algorithm;
178 return MZ_OK;
179 }
180
181 void *mz_crypt_sha_create(void) {
182 mz_crypt_sha *sha = (mz_crypt_sha *)calloc(1, sizeof(mz_crypt_sha));
183 if (sha)
184 sha->algorithm = MZ_HASH_SHA256;
185 return sha;
186 }
187
188 void mz_crypt_sha_delete(void **handle) {
189 mz_crypt_sha *sha = NULL;
190 if (!handle)
191 return;
192 sha = (mz_crypt_sha *)*handle;
193 if (sha) {
194 mz_crypt_sha_free(*handle);
195 free(sha);
196 }
197 *handle = NULL;
198 }
199
200 /***************************************************************************/
201
202 # define MZ_AES_MAX_TAG_SIZE (16)
203 # define MZ_AES_MAX_NONCE_SIZE (12)
204
205 typedef struct mz_crypt_aes_s {
206 BCRYPT_ALG_HANDLE provider;
207 BCRYPT_KEY_HANDLE key;
208 uint8_t *key_buffer;
209 int32_t mode;
210 int32_t error;
211 uint8_t *iv;
212 uint32_t iv_length;
213 uint8_t nonce[MZ_AES_MAX_NONCE_SIZE];
214 uint32_t nonce_length;
215 uint8_t mac[MZ_AES_MAX_TAG_SIZE];
216 uint8_t tag[MZ_AES_MAX_TAG_SIZE];
217 uint32_t tag_size;
218 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO *auth_info;
219 } mz_crypt_aes;
220
221 /***************************************************************************/
222
223 static void mz_crypt_aes_free(void *handle) {
224 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
225 if (aes->key)
226 BCryptDestroyKey(aes->key);
227 aes->key = NULL;
228 if (aes->provider)
229 BCryptCloseAlgorithmProvider(aes->provider, 0);
230 aes->provider = NULL;
231 free(aes->key_buffer);
232 aes->key_buffer = NULL;
233 free(aes->iv);
234 aes->iv = NULL;
235 free(aes->auth_info);
236 aes->auth_info = NULL;
237 }
238
239 void mz_crypt_aes_reset(void *handle) {
240 mz_crypt_aes_free(handle);
241 }
242
243 int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
244 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
245 ULONG output_size = 0;
246 NTSTATUS status = 0;
247
248 if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
249 return MZ_PARAM_ERROR;
250 if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
251 return MZ_PARAM_ERROR;
252 if (aes->mode == MZ_AES_MODE_GCM && !aes->auth_info)
253 return MZ_PARAM_ERROR;
254
255 if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
256 aes->auth_info->pbAuthData = (uint8_t *)aad;
257 aes->auth_info->cbAuthData = aad_size;
258 }
259
260 status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
261
262 if (aad && aes->auth_info) {
263 aes->auth_info->pbAuthData = NULL;
264 aes->auth_info->cbAuthData = 0;
265 }
266
267 if (!NT_SUCCESS(status)) {
268 aes->error = status;
269 return MZ_CRYPT_ERROR;
270 }
271 return size;
272 }
273
274 int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) {
275 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
276 NTSTATUS status = 0;
277 ULONG output_size = 0;
278
279 if (!aes || !tag || !tag_size || aes->mode != MZ_AES_MODE_GCM || !aes->auth_info)
280 return MZ_PARAM_ERROR;
281
282 aes->auth_info->pbTag = tag;
283 aes->auth_info->cbTag = tag_size;
284
285 aes->auth_info->dwFlags &= ~BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
286
287 status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
288
289 if (!NT_SUCCESS(status)) {
290 aes->error = status;
291 return MZ_CRYPT_ERROR;
292 }
293
294 return size;
295 }
296
297 int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
298 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
299 ULONG output_size = 0;
300 NTSTATUS status = 0;
301
302 if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
303 return MZ_PARAM_ERROR;
304 if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
305 return MZ_PARAM_ERROR;
306 if (aes->mode == MZ_AES_MODE_GCM && !aes->auth_info)
307 return MZ_PARAM_ERROR;
308
309 if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
310 aes->auth_info->pbAuthData = (uint8_t *)aad;
311 aes->auth_info->cbAuthData = aad_size;
312 }
313
314 status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
315
316 if (aad && aes->auth_info) {
317 aes->auth_info->pbAuthData = NULL;
318 aes->auth_info->cbAuthData = 0;
319 }
320
321 if (!NT_SUCCESS(status)) {
322 aes->error = status;
323 return MZ_CRYPT_ERROR;
324 }
325 return size;
326 }
327
328 int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
329 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
330 NTSTATUS status = 0;
331 ULONG output_size = 0;
332
333 if (!aes || !tag || !tag_length || aes->mode != MZ_AES_MODE_GCM || !aes->auth_info)
334 return MZ_PARAM_ERROR;
335
336 aes->auth_info->pbTag = (uint8_t *)tag;
337 aes->auth_info->cbTag = tag_length;
338
339 aes->auth_info->dwFlags &= ~BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
340
341 status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
342
343 if (!NT_SUCCESS(status)) {
344 aes->error = status;
345 return MZ_CRYPT_ERROR;
346 }
347
348 return size;
349 }
350
351 static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
352 int32_t iv_length) {
353 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
354 BCRYPT_KEY_DATA_BLOB_HEADER *key_blob = NULL;
355 int32_t key_blob_size = 0;
356 ULONG key_size = 0;
357 wchar_t *mode = NULL;
358 NTSTATUS status = 0;
359 int32_t err = MZ_OK;
360
361 if (!aes || !key || !key_length)
362 return MZ_PARAM_ERROR;
363 if (key_length != 16 && key_length != 24 && key_length != 32)
364 return MZ_PARAM_ERROR;
365 if (iv && iv_length > MZ_AES_BLOCK_SIZE)
366 return MZ_PARAM_ERROR;
367
368 if (aes->mode == MZ_AES_MODE_ECB)
369 mode = BCRYPT_CHAIN_MODE_ECB;
370 else if (aes->mode == MZ_AES_MODE_CBC)
371 mode = BCRYPT_CHAIN_MODE_CBC;
372 else if (aes->mode == MZ_AES_MODE_GCM)
373 mode = BCRYPT_CHAIN_MODE_GCM;
374 else
375 return MZ_PARAM_ERROR;
376
377 mz_crypt_aes_reset(handle);
378
379 if (iv && iv_length) {
380 if (aes->mode == MZ_AES_MODE_ECB)
381 return MZ_PARAM_ERROR;
382
383 aes->iv_length = MZ_AES_BLOCK_SIZE;
384 aes->iv = calloc(MZ_AES_BLOCK_SIZE, sizeof(uint8_t));
385 if (!aes->iv)
386 return MZ_MEM_ERROR;
387 memcpy(aes->iv, iv, iv_length);
388 }
389
390 status = BCryptOpenAlgorithmProvider(&aes->provider, BCRYPT_AES_ALGORITHM, NULL, 0);
391
392 if (NT_SUCCESS(status)) {
393 status = BCryptSetProperty(aes->provider, BCRYPT_CHAINING_MODE, (PBYTE)mode, sizeof(mode), 0);
394 }
395
396 if (NT_SUCCESS(status)) {
397 ULONG result_size;
398 status = BCryptGetProperty(aes->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&key_size, sizeof(key_size),
399 &result_size, 0);
400 }
401
402 if (NT_SUCCESS(status)) {
403 aes->key_buffer = malloc(key_size);
404 if (!aes->key_buffer)
405 return MZ_MEM_ERROR;
406 key_blob_size = sizeof(*key_blob) + key_length;
407 key_blob = malloc(key_blob_size);
408 if (key_blob) {
409 key_blob->dwMagic = BCRYPT_KEY_DATA_BLOB_MAGIC;
410 key_blob->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
411 key_blob->cbKeyData = key_length;
412
413 memcpy((uint8_t *)key_blob + sizeof(*key_blob), key, key_length);
414
415 status = BCryptImportKey(aes->provider, NULL, BCRYPT_KEY_DATA_BLOB, &aes->key, aes->key_buffer, key_size,
416 (PUCHAR)key_blob, key_blob_size, 0);
417 SecureZeroMemory(key_blob, key_blob_size);
418 free(key_blob);
419 }
420 }
421
422 if (aes->mode == MZ_AES_MODE_GCM) {
423 ULONG result_size;
424
425 aes->auth_info = malloc(sizeof(BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO));
426 if (!aes->auth_info)
427 return MZ_MEM_ERROR;
428
429 BCRYPT_INIT_AUTH_MODE_INFO(*aes->auth_info);
430 aes->auth_info->dwFlags = BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
431
432 if (NT_SUCCESS(status)) {
433 BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_lengths;
434
435 status = BCryptGetProperty(aes->provider, BCRYPT_AUTH_TAG_LENGTH, (PUCHAR)&tag_lengths, sizeof(tag_lengths),
436 &result_size, 0);
437
438 if (NT_SUCCESS(status)) {
439 aes->tag_size = tag_lengths.dwMaxLength;
440 if (aes->tag_size > sizeof(aes->tag))
441 return MZ_CRYPT_ERROR;
442
443 aes->auth_info->cbTag = aes->tag_size;
444 aes->auth_info->pbMacContext = aes->mac;
445 aes->auth_info->cbMacContext = aes->tag_size;
446 }
447 }
448
449 if (NT_SUCCESS(status)) {
450 ULONG block_length;
451
452 status = BCryptGetProperty(aes->provider, BCRYPT_BLOCK_LENGTH, (PUCHAR)&block_length, sizeof(block_length),
453 &result_size, 0);
454
455 if (NT_SUCCESS(status) && iv) {
456 if (aes->iv_length > block_length)
457 return MZ_CRYPT_ERROR;
458
459 aes->nonce_length = iv_length;
460 if (aes->nonce_length > MZ_AES_MAX_NONCE_SIZE)
461 aes->nonce_length = MZ_AES_MAX_NONCE_SIZE;
462 memcpy(aes->nonce, iv, aes->nonce_length);
463
464 aes->auth_info->pbNonce = aes->nonce;
465 aes->auth_info->cbNonce = aes->nonce_length;
466 }
467 }
468 }
469
470 if (!NT_SUCCESS(status)) {
471 aes->error = status;
472 err = MZ_CRYPT_ERROR;
473 }
474
475 return err;
476 }
477
478 int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
479 int32_t iv_length) {
480 return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
481 }
482
483 int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
484 int32_t iv_length) {
485 return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
486 }
487
488 void mz_crypt_aes_set_mode(void *handle, int32_t mode) {
489 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
490 aes->mode = mode;
491 }
492
493 void *mz_crypt_aes_create(void) {
494 mz_crypt_aes *aes = (mz_crypt_aes *)calloc(1, sizeof(mz_crypt_aes));
495 return aes;
496 }
497
498 void mz_crypt_aes_delete(void **handle) {
499 mz_crypt_aes *aes = NULL;
500 if (!handle)
501 return;
502 aes = (mz_crypt_aes *)*handle;
503 if (aes) {
504 mz_crypt_aes_free(*handle);
505 free(aes);
506 }
507 *handle = NULL;
508 }
509
510 /***************************************************************************/
511
512 typedef struct mz_crypt_hmac_s {
513 BCRYPT_ALG_HANDLE provider;
514 BCRYPT_KEY_HANDLE key;
515 BCRYPT_HASH_HANDLE hash;
516 uint8_t *buffer;
517 int32_t error;
518 uint16_t algorithm;
519 } mz_crypt_hmac;
520
521 /***************************************************************************/
522
523 static void mz_crypt_hmac_free(void *handle) {
524 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
525 if (hmac->hash)
526 BCryptDestroyHash(hmac->hash);
527 hmac->hash = NULL;
528 if (hmac->key)
529 BCryptDestroyKey(hmac->key);
530 hmac->key = NULL;
531 if (hmac->provider)
532 BCryptCloseAlgorithmProvider(hmac->provider, 0);
533 hmac->provider = NULL;
534 free(hmac->buffer);
535 hmac->buffer = NULL;
536 }
537
538 void mz_crypt_hmac_reset(void *handle) {
539 mz_crypt_hmac_free(handle);
540 }
541
542 int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) {
543 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
544 wchar_t *alg_id = BCRYPT_SHA256_ALGORITHM;
545 ULONG buffer_size = 0;
546 ULONG result_size = sizeof(buffer_size);
547 NTSTATUS status = 0;
548 int32_t err = MZ_OK;
549
550 if (!hmac || !key)
551 return MZ_PARAM_ERROR;
552
553 mz_crypt_hmac_reset(handle);
554
555 if (hmac->algorithm == MZ_HASH_SHA1)
556 alg_id = BCRYPT_SHA1_ALGORITHM;
557
558 status = BCryptOpenAlgorithmProvider(&hmac->provider, alg_id, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
559 if (NT_SUCCESS(status)) {
560 status =
561 BCryptGetProperty(hmac->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size, &result_size, 0);
562 }
563 if (NT_SUCCESS(status)) {
564 hmac->buffer = malloc(buffer_size);
565 if (!hmac->buffer)
566 return MZ_MEM_ERROR;
567 }
568 if (NT_SUCCESS(status))
569 status = BCryptCreateHash(hmac->provider, &hmac->hash, hmac->buffer, buffer_size, (PUCHAR)key, key_length, 0);
570
571 if (!NT_SUCCESS(status)) {
572 hmac->error = status;
573 err = MZ_CRYPT_ERROR;
574 }
575
576 if (err != MZ_OK)
577 mz_crypt_hmac_free(handle);
578
579 return err;
580 }
581
582 int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) {
583 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
584 NTSTATUS status = 0;
585
586 if (!hmac || !buf || !hmac->hash)
587 return MZ_PARAM_ERROR;
588
589 status = BCryptHashData(hmac->hash, (uint8_t *)buf, size, 0);
590 if (!NT_SUCCESS(status)) {
591 hmac->error = status;
592 return MZ_HASH_ERROR;
593 }
594 return MZ_OK;
595 }
596
597 int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size) {
598 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
599 NTSTATUS status = 0;
600 ULONG expected_size = 0;
601 ULONG result_size = sizeof(expected_size);
602
603 if (!hmac || !digest || !hmac->hash)
604 return MZ_PARAM_ERROR;
605 status = BCryptGetProperty(hmac->hash, BCRYPT_HASH_LENGTH, (PUCHAR)&expected_size, result_size, &result_size, 0);
606 if (!NT_SUCCESS(status))
607 return MZ_HASH_ERROR;
608 if ((int32_t)expected_size > digest_size)
609 return MZ_BUF_ERROR;
610 status = BCryptFinishHash(hmac->hash, digest, expected_size, 0);
611 if (!NT_SUCCESS(status)) {
612 hmac->error = status;
613 return MZ_HASH_ERROR;
614 }
615 return MZ_OK;
616 }
617
618 void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm) {
619 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
620 hmac->algorithm = algorithm;
621 }
622
623 int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle) {
624 mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
625 mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
626 NTSTATUS status = 0;
627 int32_t err = MZ_OK;
628
629 if (target->hash) {
630 BCryptDestroyHash(target->hash);
631 target->hash = NULL;
632 }
633
634 status = BCryptDuplicateHash(source->hash, &target->hash, NULL, 0, 0);
635
636 if (!NT_SUCCESS(status)) {
637 target->error = status;
638 err = MZ_HASH_ERROR;
639 }
640 return err;
641 }
642
643 void *mz_crypt_hmac_create(void) {
644 mz_crypt_hmac *hmac = (mz_crypt_hmac *)calloc(1, sizeof(mz_crypt_hmac));
645 if (hmac)
646 hmac->algorithm = MZ_HASH_SHA256;
647 return hmac;
648 }
649
650 void mz_crypt_hmac_delete(void **handle) {
651 mz_crypt_hmac *hmac = NULL;
652 if (!handle)
653 return;
654 hmac = (mz_crypt_hmac *)*handle;
655 if (hmac) {
656 mz_crypt_hmac_free(*handle);
657 free(hmac);
658 }
659 *handle = NULL;
660 }
661
662 #endif