| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "mz.h" |
| 12 |
#include "mz_os.h" |
| 13 |
#include "mz_crypt.h" |
| 14 |
|
| 15 |
#include <windows.h> |
| 16 |
|
| 17 |
#if _WIN32_WINNT <= _WIN32_WINNT_WINXP |
| 18 |
# include <wincrypt.h> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
int32_t mz_crypt_rand(uint8_t *buf, int32_t size) { |
| 23 |
HCRYPTPROV provider; |
| 24 |
int32_t result = 0; |
| 25 |
|
| 26 |
result = CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); |
| 27 |
if (result) { |
| 28 |
result = CryptGenRandom(provider, size, buf); |
| 29 |
CryptReleaseContext(provider, 0); |
| 30 |
if (result) |
| 31 |
return size; |
| 32 |
} |
| 33 |
|
| 34 |
return mz_os_rand(buf, size); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
typedef struct mz_crypt_sha_s { |
| 40 |
union { |
| 41 |
struct { |
| 42 |
HCRYPTPROV provider; |
| 43 |
HCRYPTHASH hash; |
| 44 |
}; |
| 45 |
}; |
| 46 |
int32_t error; |
| 47 |
uint16_t algorithm; |
| 48 |
} mz_crypt_sha; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static void mz_crypt_sha_free(void *handle) { |
| 53 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 54 |
if (sha->hash) |
| 55 |
CryptDestroyHash(sha->hash); |
| 56 |
sha->hash = 0; |
| 57 |
if (sha->provider) |
| 58 |
CryptReleaseContext(sha->provider, 0); |
| 59 |
sha->provider = 0; |
| 60 |
} |
| 61 |
|
| 62 |
void mz_crypt_sha_reset(void *handle) { |
| 63 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 64 |
mz_crypt_sha_free(handle); |
| 65 |
sha->error = 0; |
| 66 |
} |
| 67 |
|
| 68 |
int32_t mz_crypt_sha_begin(void *handle) { |
| 69 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 70 |
ALG_ID alg_id = 0; |
| 71 |
int32_t result = 0; |
| 72 |
int32_t err = MZ_OK; |
| 73 |
|
| 74 |
if (!sha) |
| 75 |
return MZ_PARAM_ERROR; |
| 76 |
|
| 77 |
if (sha->algorithm == MZ_HASH_SHA224) |
| 78 |
return MZ_SUPPORT_ERROR; |
| 79 |
|
| 80 |
switch (sha->algorithm) { |
| 81 |
case MZ_HASH_SHA1: |
| 82 |
alg_id = CALG_SHA1; |
| 83 |
break; |
| 84 |
# if NTDDI_VERSION > NTDDI_WINXPSP2 |
| 85 |
case MZ_HASH_SHA256: |
| 86 |
alg_id = CALG_SHA_256; |
| 87 |
break; |
| 88 |
case MZ_HASH_SHA384: |
| 89 |
alg_id = CALG_SHA_384; |
| 90 |
break; |
| 91 |
case MZ_HASH_SHA512: |
| 92 |
alg_id = CALG_SHA_512; |
| 93 |
break; |
| 94 |
# else |
| 95 |
case MZ_HASH_SHA256: |
| 96 |
case MZ_HASH_SHA384: |
| 97 |
case MZ_HASH_SHA512: |
| 98 |
return MZ_SUPPORT_ERROR; |
| 99 |
# endif |
| 100 |
default: |
| 101 |
return MZ_PARAM_ERROR; |
| 102 |
} |
| 103 |
|
| 104 |
result = CryptAcquireContext(&sha->provider, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); |
| 105 |
if (!result) { |
| 106 |
sha->error = GetLastError(); |
| 107 |
err = MZ_CRYPT_ERROR; |
| 108 |
} |
| 109 |
|
| 110 |
if (result) { |
| 111 |
result = CryptCreateHash(sha->provider, alg_id, 0, 0, &sha->hash); |
| 112 |
if (!result) { |
| 113 |
sha->error = GetLastError(); |
| 114 |
err = MZ_HASH_ERROR; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return err; |
| 119 |
} |
| 120 |
|
| 121 |
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) { |
| 122 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 123 |
int32_t result = 0; |
| 124 |
|
| 125 |
if (!sha || !buf || size < 0) |
| 126 |
return MZ_PARAM_ERROR; |
| 127 |
|
| 128 |
if (sha->algorithm == MZ_HASH_SHA224) |
| 129 |
return MZ_SUPPORT_ERROR; |
| 130 |
|
| 131 |
if (sha->hash == 0) |
| 132 |
return MZ_PARAM_ERROR; |
| 133 |
|
| 134 |
result = CryptHashData(sha->hash, buf, size, 0); |
| 135 |
if (!result) { |
| 136 |
sha->error = GetLastError(); |
| 137 |
return MZ_HASH_ERROR; |
| 138 |
} |
| 139 |
return size; |
| 140 |
} |
| 141 |
|
| 142 |
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) { |
| 143 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 144 |
int32_t result = 0; |
| 145 |
int32_t expected_size = 0; |
| 146 |
|
| 147 |
if (!sha || !digest) |
| 148 |
return MZ_PARAM_ERROR; |
| 149 |
|
| 150 |
if (sha->algorithm == MZ_HASH_SHA224) |
| 151 |
return MZ_SUPPORT_ERROR; |
| 152 |
|
| 153 |
if (sha->hash == 0) |
| 154 |
return MZ_PARAM_ERROR; |
| 155 |
|
| 156 |
result = CryptGetHashParam(sha->hash, HP_HASHVAL, NULL, (DWORD *)&expected_size, 0); |
| 157 |
if (expected_size > digest_size) |
| 158 |
return MZ_BUF_ERROR; |
| 159 |
if (!result) |
| 160 |
return MZ_HASH_ERROR; |
| 161 |
result = CryptGetHashParam(sha->hash, HP_HASHVAL, digest, (DWORD *)&digest_size, 0); |
| 162 |
if (!result) { |
| 163 |
sha->error = GetLastError(); |
| 164 |
return MZ_HASH_ERROR; |
| 165 |
} |
| 166 |
return MZ_OK; |
| 167 |
} |
| 168 |
|
| 169 |
int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) { |
| 170 |
mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 171 |
if (algorithm != MZ_HASH_SHA1) |
| 172 |
return MZ_SUPPORT_ERROR; |
| 173 |
sha->algorithm = algorithm; |
| 174 |
return MZ_OK; |
| 175 |
} |
| 176 |
|
| 177 |
void *mz_crypt_sha_create(void) { |
| 178 |
mz_crypt_sha *sha = (mz_crypt_sha *)calloc(1, sizeof(mz_crypt_sha)); |
| 179 |
if (sha) |
| 180 |
sha->algorithm = MZ_HASH_SHA256; |
| 181 |
return sha; |
| 182 |
} |
| 183 |
|
| 184 |
void mz_crypt_sha_delete(void **handle) { |
| 185 |
mz_crypt_sha *sha = NULL; |
| 186 |
if (!handle) |
| 187 |
return; |
| 188 |
sha = (mz_crypt_sha *)*handle; |
| 189 |
if (sha) { |
| 190 |
mz_crypt_sha_free(*handle); |
| 191 |
free(sha); |
| 192 |
} |
| 193 |
*handle = NULL; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
typedef struct mz_crypt_aes_s { |
| 199 |
HCRYPTPROV provider; |
| 200 |
HCRYPTKEY key; |
| 201 |
int32_t mode; |
| 202 |
int32_t error; |
| 203 |
} mz_crypt_aes; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static void mz_crypt_aes_free(void *handle) { |
| 208 |
mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 209 |
if (aes->key) |
| 210 |
CryptDestroyKey(aes->key); |
| 211 |
aes->key = 0; |
| 212 |
if (aes->provider) |
| 213 |
CryptReleaseContext(aes->provider, 0); |
| 214 |
aes->provider = 0; |
| 215 |
} |
| 216 |
|
| 217 |
void mz_crypt_aes_reset(void *handle) { |
| 218 |
mz_crypt_aes_free(handle); |
| 219 |
} |
| 220 |
|
| 221 |
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
| 222 |
mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 223 |
int32_t result = 0; |
| 224 |
|
| 225 |
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || (aad && aad_size > 0)) |
| 226 |
return MZ_PARAM_ERROR; |
| 227 |
result = CryptEncrypt(aes->key, 0, 0, 0, buf, (DWORD *)&size, size); |
| 228 |
if (!result) { |
| 229 |
aes->error = GetLastError(); |
| 230 |
return MZ_CRYPT_ERROR; |
| 231 |
} |
| 232 |
return size; |
| 233 |
} |
| 234 |
|
| 235 |
int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) { |
| 236 |
return MZ_SUPPORT_ERROR; |
| 237 |
} |
| 238 |
|
| 239 |
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
| 240 |
mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 241 |
int32_t result = 0; |
| 242 |
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || (aad && aad_size > 0)) |
| 243 |
return MZ_PARAM_ERROR; |
| 244 |
result = CryptDecrypt(aes->key, 0, 0, 0, buf, (DWORD *)&size); |
| 245 |
if (!result) { |
| 246 |
aes->error = GetLastError(); |
| 247 |
return MZ_CRYPT_ERROR; |
| 248 |
} |
| 249 |
return size; |
| 250 |
} |
| 251 |
|
| 252 |
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) { |
| 253 |
return MZ_SUPPORT_ERROR; |
| 254 |
} |
| 255 |
|
| 256 |
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 257 |
int32_t iv_length) { |
| 258 |
mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 259 |
HCRYPTHASH hash = 0; |
| 260 |
ALG_ID alg_id = 0; |
| 261 |
typedef struct key_blob_header_s { |
| 262 |
BLOBHEADER hdr; |
| 263 |
uint32_t key_length; |
| 264 |
} key_blob_header_s; |
| 265 |
key_blob_header_s *key_blob_s = NULL; |
| 266 |
uint32_t mode; |
| 267 |
uint8_t *key_blob = NULL; |
| 268 |
int32_t key_blob_size = 0; |
| 269 |
int32_t result = 0; |
| 270 |
int32_t err = MZ_OK; |
| 271 |
|
| 272 |
if (!aes || !key || !key_length) |
| 273 |
return MZ_PARAM_ERROR; |
| 274 |
if (iv && iv_length < MZ_AES_BLOCK_SIZE) |
| 275 |
return MZ_PARAM_ERROR; |
| 276 |
|
| 277 |
mz_crypt_aes_reset(handle); |
| 278 |
|
| 279 |
if (aes->mode == MZ_AES_MODE_CBC) |
| 280 |
mode = CRYPT_MODE_CBC; |
| 281 |
else if (aes->mode == MZ_AES_MODE_ECB) |
| 282 |
mode = CRYPT_MODE_ECB; |
| 283 |
else if (aes->mode == MZ_AES_MODE_GCM) |
| 284 |
return MZ_SUPPORT_ERROR; |
| 285 |
else |
| 286 |
return MZ_PARAM_ERROR; |
| 287 |
|
| 288 |
if (key_length == 16) |
| 289 |
alg_id = CALG_AES_128; |
| 290 |
else if (key_length == 24) |
| 291 |
alg_id = CALG_AES_192; |
| 292 |
else if (key_length == 32) |
| 293 |
alg_id = CALG_AES_256; |
| 294 |
else |
| 295 |
return MZ_PARAM_ERROR; |
| 296 |
|
| 297 |
result = CryptAcquireContext(&aes->provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, |
| 298 |
CRYPT_VERIFYCONTEXT | CRYPT_SILENT); |
| 299 |
if (result) { |
| 300 |
key_blob_size = sizeof(key_blob_header_s) + key_length; |
| 301 |
key_blob = (uint8_t *)malloc(key_blob_size); |
| 302 |
if (key_blob) { |
| 303 |
key_blob_s = (key_blob_header_s *)key_blob; |
| 304 |
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB; |
| 305 |
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION; |
| 306 |
key_blob_s->hdr.aiKeyAlg = alg_id; |
| 307 |
key_blob_s->hdr.reserved = 0; |
| 308 |
key_blob_s->key_length = key_length; |
| 309 |
|
| 310 |
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length); |
| 311 |
|
| 312 |
result = CryptImportKey(aes->provider, key_blob, key_blob_size, 0, 0, &aes->key); |
| 313 |
|
| 314 |
SecureZeroMemory(key_blob, key_blob_size); |
| 315 |
free(key_blob); |
| 316 |
} else { |
| 317 |
err = MZ_MEM_ERROR; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (result && err == MZ_OK) |
| 322 |
result = CryptSetKeyParam(aes->key, KP_MODE, (const uint8_t *)&mode, 0); |
| 323 |
|
| 324 |
if (!result && err == MZ_OK) { |
| 325 |
aes->error = GetLastError(); |
| 326 |
err = MZ_CRYPT_ERROR; |
| 327 |
} |
| 328 |
|
| 329 |
if (result && err == MZ_OK && iv) { |
| 330 |
if (aes->mode == MZ_AES_MODE_ECB) |
| 331 |
return MZ_PARAM_ERROR; |
| 332 |
|
| 333 |
result = CryptSetKeyParam(aes->key, KP_IV, iv, 0); |
| 334 |
|
| 335 |
if (!result) { |
| 336 |
aes->error = GetLastError(); |
| 337 |
return MZ_CRYPT_ERROR; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
if (hash) |
| 342 |
CryptDestroyHash(hash); |
| 343 |
|
| 344 |
return err; |
| 345 |
} |
| 346 |
|
| 347 |
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 348 |
int32_t iv_length) { |
| 349 |
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length); |
| 350 |
} |
| 351 |
|
| 352 |
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 353 |
int32_t iv_length) { |
| 354 |
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length); |
| 355 |
} |
| 356 |
|
| 357 |
void mz_crypt_aes_set_mode(void *handle, int32_t mode) { |
| 358 |
mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 359 |
aes->mode = mode; |
| 360 |
} |
| 361 |
|
| 362 |
void *mz_crypt_aes_create(void) { |
| 363 |
mz_crypt_aes *aes = (mz_crypt_aes *)calloc(1, sizeof(mz_crypt_aes)); |
| 364 |
return aes; |
| 365 |
} |
| 366 |
|
| 367 |
void mz_crypt_aes_delete(void **handle) { |
| 368 |
mz_crypt_aes *aes = NULL; |
| 369 |
if (!handle) |
| 370 |
return; |
| 371 |
aes = (mz_crypt_aes *)*handle; |
| 372 |
if (aes) { |
| 373 |
mz_crypt_aes_free(*handle); |
| 374 |
free(aes); |
| 375 |
} |
| 376 |
*handle = NULL; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
typedef struct mz_crypt_hmac_s { |
| 382 |
HCRYPTPROV provider; |
| 383 |
HCRYPTHASH hash; |
| 384 |
HCRYPTKEY key; |
| 385 |
HMAC_INFO info; |
| 386 |
int32_t mode; |
| 387 |
int32_t error; |
| 388 |
uint16_t algorithm; |
| 389 |
} mz_crypt_hmac; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static void mz_crypt_hmac_free(void *handle) { |
| 394 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 395 |
if (hmac->key) |
| 396 |
CryptDestroyKey(hmac->key); |
| 397 |
hmac->key = 0; |
| 398 |
if (hmac->hash) |
| 399 |
CryptDestroyHash(hmac->hash); |
| 400 |
hmac->hash = 0; |
| 401 |
if (hmac->provider) |
| 402 |
CryptReleaseContext(hmac->provider, 0); |
| 403 |
hmac->provider = 0; |
| 404 |
memset(&hmac->info, 0, sizeof(hmac->info)); |
| 405 |
} |
| 406 |
|
| 407 |
void mz_crypt_hmac_reset(void *handle) { |
| 408 |
mz_crypt_hmac_free(handle); |
| 409 |
} |
| 410 |
|
| 411 |
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) { |
| 412 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 413 |
ALG_ID alg_id = 0; |
| 414 |
typedef struct key_blob_header_s { |
| 415 |
BLOBHEADER hdr; |
| 416 |
uint32_t key_length; |
| 417 |
} key_blob_header_s; |
| 418 |
key_blob_header_s *key_blob_s = NULL; |
| 419 |
uint8_t *key_blob = NULL; |
| 420 |
int32_t key_blob_size = 0; |
| 421 |
int32_t pad_key_length = key_length; |
| 422 |
int32_t result = 0; |
| 423 |
int32_t err = MZ_OK; |
| 424 |
|
| 425 |
if (!hmac || !key) |
| 426 |
return MZ_PARAM_ERROR; |
| 427 |
|
| 428 |
mz_crypt_hmac_reset(handle); |
| 429 |
|
| 430 |
if (hmac->algorithm == MZ_HASH_SHA1) |
| 431 |
alg_id = CALG_SHA1; |
| 432 |
else |
| 433 |
# ifdef CALG_SHA_256 |
| 434 |
alg_id = CALG_SHA_256; |
| 435 |
# else |
| 436 |
return MZ_SUPPORT_ERROR; |
| 437 |
# endif |
| 438 |
|
| 439 |
hmac->info.HashAlgid = alg_id; |
| 440 |
|
| 441 |
result = |
| 442 |
CryptAcquireContext(&hmac->provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT); |
| 443 |
|
| 444 |
if (!result) { |
| 445 |
hmac->error = GetLastError(); |
| 446 |
err = MZ_CRYPT_ERROR; |
| 447 |
} else { |
| 448 |
|
| 449 |
if (pad_key_length == 1) |
| 450 |
pad_key_length += 1; |
| 451 |
key_blob_size = sizeof(key_blob_header_s) + pad_key_length; |
| 452 |
key_blob = (uint8_t *)malloc(key_blob_size); |
| 453 |
} |
| 454 |
|
| 455 |
if (key_blob) { |
| 456 |
memset(key_blob, 0, key_blob_size); |
| 457 |
key_blob_s = (key_blob_header_s *)key_blob; |
| 458 |
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB; |
| 459 |
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION; |
| 460 |
key_blob_s->hdr.aiKeyAlg = CALG_RC2; |
| 461 |
key_blob_s->hdr.reserved = 0; |
| 462 |
key_blob_s->key_length = pad_key_length; |
| 463 |
|
| 464 |
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length); |
| 465 |
|
| 466 |
result = CryptImportKey(hmac->provider, key_blob, key_blob_size, 0, CRYPT_IPSEC_HMAC_KEY, &hmac->key); |
| 467 |
if (result) |
| 468 |
result = CryptCreateHash(hmac->provider, CALG_HMAC, hmac->key, 0, &hmac->hash); |
| 469 |
if (result) |
| 470 |
result = CryptSetHashParam(hmac->hash, HP_HMAC_INFO, (uint8_t *)&hmac->info, 0); |
| 471 |
|
| 472 |
SecureZeroMemory(key_blob, key_blob_size); |
| 473 |
free(key_blob); |
| 474 |
} else if (err == MZ_OK) { |
| 475 |
err = MZ_MEM_ERROR; |
| 476 |
} |
| 477 |
|
| 478 |
if (!result) { |
| 479 |
hmac->error = GetLastError(); |
| 480 |
err = MZ_CRYPT_ERROR; |
| 481 |
} |
| 482 |
|
| 483 |
if (err != MZ_OK) |
| 484 |
mz_crypt_hmac_free(handle); |
| 485 |
|
| 486 |
return err; |
| 487 |
} |
| 488 |
|
| 489 |
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) { |
| 490 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 491 |
int32_t result = 0; |
| 492 |
|
| 493 |
if (!hmac || !buf || !hmac->hash) |
| 494 |
return MZ_PARAM_ERROR; |
| 495 |
|
| 496 |
result = CryptHashData(hmac->hash, buf, size, 0); |
| 497 |
if (!result) { |
| 498 |
hmac->error = GetLastError(); |
| 499 |
return MZ_HASH_ERROR; |
| 500 |
} |
| 501 |
return MZ_OK; |
| 502 |
} |
| 503 |
|
| 504 |
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size) { |
| 505 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 506 |
int32_t result = 0; |
| 507 |
int32_t expected_size = 0; |
| 508 |
|
| 509 |
if (!hmac || !digest || !hmac->hash) |
| 510 |
return MZ_PARAM_ERROR; |
| 511 |
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, NULL, (DWORD *)&expected_size, 0); |
| 512 |
if (expected_size > digest_size) |
| 513 |
return MZ_BUF_ERROR; |
| 514 |
if (!result) |
| 515 |
return MZ_HASH_ERROR; |
| 516 |
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, digest, (DWORD *)&digest_size, 0); |
| 517 |
if (!result) { |
| 518 |
hmac->error = GetLastError(); |
| 519 |
return MZ_HASH_ERROR; |
| 520 |
} |
| 521 |
return MZ_OK; |
| 522 |
} |
| 523 |
|
| 524 |
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm) { |
| 525 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 526 |
hmac->algorithm = algorithm; |
| 527 |
} |
| 528 |
|
| 529 |
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle) { |
| 530 |
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle; |
| 531 |
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle; |
| 532 |
int32_t result = 0; |
| 533 |
int32_t err = MZ_OK; |
| 534 |
|
| 535 |
if (target->hash) { |
| 536 |
CryptDestroyHash(target->hash); |
| 537 |
target->hash = 0; |
| 538 |
} |
| 539 |
|
| 540 |
result = CryptDuplicateHash(source->hash, NULL, 0, &target->hash); |
| 541 |
|
| 542 |
if (!result) { |
| 543 |
target->error = GetLastError(); |
| 544 |
err = MZ_HASH_ERROR; |
| 545 |
} |
| 546 |
return err; |
| 547 |
} |
| 548 |
|
| 549 |
void *mz_crypt_hmac_create(void) { |
| 550 |
mz_crypt_hmac *hmac = (mz_crypt_hmac *)calloc(1, sizeof(mz_crypt_hmac)); |
| 551 |
if (hmac) |
| 552 |
hmac->algorithm = MZ_HASH_SHA256; |
| 553 |
return hmac; |
| 554 |
} |
| 555 |
|
| 556 |
void mz_crypt_hmac_delete(void **handle) { |
| 557 |
mz_crypt_hmac *hmac = NULL; |
| 558 |
if (!handle) |
| 559 |
return; |
| 560 |
hmac = (mz_crypt_hmac *)*handle; |
| 561 |
if (hmac) { |
| 562 |
mz_crypt_hmac_free(*handle); |
| 563 |
free(hmac); |
| 564 |
} |
| 565 |
*handle = NULL; |
| 566 |
} |
| 567 |
|
| 568 |
#endif |