| 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 | static void mz_crypt_init(void) { |
| 30 | static int32_t openssl_initialized = 0; |
| 31 | 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 | OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN, NULL); |
| 42 | #endif |
| 43 | |
| 44 | openssl_initialized = 1; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | int32_t mz_crypt_rand(uint8_t *buf, int32_t size) { |
| 49 | if (!RAND_bytes(buf, size)) |
| 50 | return MZ_CRYPT_ERROR; |
| 51 | |
| 52 | return size; |
| 53 | } |
| 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 | static void mz_crypt_sha_free(void *handle) { |
| 80 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 81 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 82 | if (sha->ctx) |
| 83 | EVP_MD_CTX_free(sha->ctx); |
| 84 | sha->ctx = NULL; |
| 85 | #else |
| 86 | MZ_UNUSED(handle); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void mz_crypt_sha_reset(void *handle) { |
| 91 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 92 | |
| 93 | mz_crypt_init(); |
| 94 | mz_crypt_sha_free(handle); |
| 95 | |
| 96 | sha->error = 0; |
| 97 | sha->initialized = 0; |
| 98 | } |
| 99 | |
| 100 | int32_t mz_crypt_sha_begin(void *handle) { |
| 101 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 102 | int32_t result = 0; |
| 103 | |
| 104 | if (!sha) |
| 105 | return MZ_PARAM_ERROR; |
| 106 | |
| 107 | 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 | const EVP_MD *md = NULL; |
| 129 | switch (sha->algorithm) { |
| 130 | case MZ_HASH_SHA1: |
| 131 | md = EVP_sha1(); |
| 132 | break; |
| 133 | case MZ_HASH_SHA224: |
| 134 | md = EVP_sha224(); |
| 135 | break; |
| 136 | case MZ_HASH_SHA256: |
| 137 | md = EVP_sha256(); |
| 138 | break; |
| 139 | case MZ_HASH_SHA384: |
| 140 | md = EVP_sha384(); |
| 141 | break; |
| 142 | case MZ_HASH_SHA512: |
| 143 | md = EVP_sha512(); |
| 144 | break; |
| 145 | } |
| 146 | if (!md) |
| 147 | return MZ_PARAM_ERROR; |
| 148 | |
| 149 | sha->ctx = EVP_MD_CTX_new(); |
| 150 | if (!sha->ctx) |
| 151 | return MZ_MEM_ERROR; |
| 152 | result = EVP_DigestInit_ex(sha->ctx, md, NULL); |
| 153 | #endif |
| 154 | |
| 155 | if (!result) { |
| 156 | sha->error = ERR_get_error(); |
| 157 | return MZ_HASH_ERROR; |
| 158 | } |
| 159 | |
| 160 | sha->initialized = 1; |
| 161 | return MZ_OK; |
| 162 | } |
| 163 | |
| 164 | int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) { |
| 165 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 166 | int32_t result = 0; |
| 167 | |
| 168 | if (!sha || !buf || !sha->initialized) |
| 169 | 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 | result = EVP_DigestUpdate(sha->ctx, buf, size); |
| 191 | #endif |
| 192 | |
| 193 | if (!result) { |
| 194 | sha->error = ERR_get_error(); |
| 195 | return MZ_HASH_ERROR; |
| 196 | } |
| 197 | |
| 198 | return size; |
| 199 | } |
| 200 | |
| 201 | int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) { |
| 202 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 203 | int32_t result = 0; |
| 204 | |
| 205 | if (!sha || !digest || !sha->initialized) |
| 206 | return MZ_PARAM_ERROR; |
| 207 | if (digest_size < mz_crypt_sha_digest_size[sha->algorithm - MZ_HASH_SHA1]) |
| 208 | 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 | result = EVP_DigestFinal_ex(sha->ctx, digest, NULL); |
| 230 | #endif |
| 231 | |
| 232 | if (!result) { |
| 233 | sha->error = ERR_get_error(); |
| 234 | return MZ_HASH_ERROR; |
| 235 | } |
| 236 | |
| 237 | return MZ_OK; |
| 238 | } |
| 239 | |
| 240 | int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) { |
| 241 | mz_crypt_sha *sha = (mz_crypt_sha *)handle; |
| 242 | if (algorithm < MZ_HASH_SHA1 || algorithm > MZ_HASH_SHA512) |
| 243 | return MZ_PARAM_ERROR; |
| 244 | sha->algorithm = algorithm; |
| 245 | return MZ_OK; |
| 246 | } |
| 247 | |
| 248 | void *mz_crypt_sha_create(void) { |
| 249 | mz_crypt_sha *sha = (mz_crypt_sha *)calloc(1, sizeof(mz_crypt_sha)); |
| 250 | if (sha) |
| 251 | sha->algorithm = MZ_HASH_SHA256; |
| 252 | return sha; |
| 253 | } |
| 254 | |
| 255 | void mz_crypt_sha_delete(void **handle) { |
| 256 | mz_crypt_sha *sha = NULL; |
| 257 | if (!handle) |
| 258 | return; |
| 259 | sha = (mz_crypt_sha *)*handle; |
| 260 | if (sha) { |
| 261 | mz_crypt_sha_free(*handle); |
| 262 | free(sha); |
| 263 | } |
| 264 | *handle = NULL; |
| 265 | } |
| 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 | static void mz_crypt_aes_free(void *handle) { |
| 278 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 279 | if (aes->ctx) |
| 280 | EVP_CIPHER_CTX_free(aes->ctx); |
| 281 | aes->ctx = NULL; |
| 282 | } |
| 283 | |
| 284 | void mz_crypt_aes_reset(void *handle) { |
| 285 | mz_crypt_init(); |
| 286 | mz_crypt_aes_free(handle); |
| 287 | } |
| 288 | |
| 289 | int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
| 290 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 291 | |
| 292 | if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx) |
| 293 | return MZ_PARAM_ERROR; |
| 294 | if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0) |
| 295 | return MZ_PARAM_ERROR; |
| 296 | |
| 297 | if (aad && aad_size > 0) { |
| 298 | int32_t how_many = 0; |
| 299 | if (!EVP_EncryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size)) |
| 300 | return MZ_CRYPT_ERROR; |
| 301 | } |
| 302 | |
| 303 | if (!EVP_EncryptUpdate(aes->ctx, buf, &size, buf, size)) |
| 304 | return MZ_CRYPT_ERROR; |
| 305 | |
| 306 | return size; |
| 307 | } |
| 308 | |
| 309 | int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) { |
| 310 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 311 | int result = 0; |
| 312 | int out_len = 0; |
| 313 | |
| 314 | if (!aes || !tag || !tag_size || !aes->ctx || aes->mode != MZ_AES_MODE_GCM) |
| 315 | return MZ_PARAM_ERROR; |
| 316 | |
| 317 | if (buf && size) { |
| 318 | if (!EVP_EncryptUpdate(aes->ctx, buf, &size, buf, size)) |
| 319 | return MZ_CRYPT_ERROR; |
| 320 | } |
| 321 | |
| 322 | /* Must call EncryptFinal for tag to be calculated */ |
| 323 | result = EVP_EncryptFinal_ex(aes->ctx, NULL, &out_len); |
| 324 | |
| 325 | if (result) |
| 326 | result = EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_GET_TAG, tag_size, tag); |
| 327 | |
| 328 | if (!result) { |
| 329 | aes->error = ERR_get_error(); |
| 330 | return MZ_CRYPT_ERROR; |
| 331 | } |
| 332 | |
| 333 | return size; |
| 334 | } |
| 335 | |
| 336 | int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) { |
| 337 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 338 | |
| 339 | if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx) |
| 340 | return MZ_PARAM_ERROR; |
| 341 | if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0) |
| 342 | return MZ_PARAM_ERROR; |
| 343 | |
| 344 | if (aad && aad_size > 0) { |
| 345 | int32_t how_many = 0; |
| 346 | if (!EVP_DecryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size)) |
| 347 | return MZ_CRYPT_ERROR; |
| 348 | } |
| 349 | |
| 350 | if (!EVP_DecryptUpdate(aes->ctx, buf, &size, buf, size)) |
| 351 | return MZ_CRYPT_ERROR; |
| 352 | |
| 353 | return size; |
| 354 | } |
| 355 | |
| 356 | int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) { |
| 357 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 358 | int out_len = 0; |
| 359 | |
| 360 | if (!aes || !tag || !tag_length || !aes->ctx || aes->mode != MZ_AES_MODE_GCM) |
| 361 | return MZ_PARAM_ERROR; |
| 362 | |
| 363 | if (buf && size) { |
| 364 | if (!EVP_DecryptUpdate(aes->ctx, buf, &size, buf, size)) |
| 365 | return MZ_CRYPT_ERROR; |
| 366 | } |
| 367 | |
| 368 | /* Set expected tag */ |
| 369 | if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG, tag_length, (void *)tag)) { |
| 370 | aes->error = ERR_get_error(); |
| 371 | return MZ_CRYPT_ERROR; |
| 372 | } |
| 373 | |
| 374 | /* Must call DecryptFinal for tag verification */ |
| 375 | if (!EVP_DecryptFinal_ex(aes->ctx, NULL, &out_len)) { |
| 376 | aes->error = ERR_get_error(); |
| 377 | return MZ_CRYPT_ERROR; |
| 378 | } |
| 379 | |
| 380 | return size; |
| 381 | } |
| 382 | |
| 383 | static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 384 | int32_t iv_length, int32_t encrypt) { |
| 385 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 386 | const EVP_CIPHER *type = NULL; |
| 387 | |
| 388 | switch (aes->mode) { |
| 389 | case MZ_AES_MODE_CBC: |
| 390 | if (key_length == 16) |
| 391 | type = EVP_aes_128_cbc(); |
| 392 | else if (key_length == 24) |
| 393 | type = EVP_aes_192_cbc(); |
| 394 | else if (key_length == 32) |
| 395 | type = EVP_aes_256_cbc(); |
| 396 | break; |
| 397 | case MZ_AES_MODE_ECB: |
| 398 | if (key_length == 16) |
| 399 | type = EVP_aes_128_ecb(); |
| 400 | else if (key_length == 24) |
| 401 | type = EVP_aes_192_ecb(); |
| 402 | else if (key_length == 32) |
| 403 | type = EVP_aes_256_ecb(); |
| 404 | break; |
| 405 | case MZ_AES_MODE_GCM: |
| 406 | if (key_length == 16) |
| 407 | type = EVP_aes_128_gcm(); |
| 408 | else if (key_length == 24) |
| 409 | type = EVP_aes_192_gcm(); |
| 410 | else if (key_length == 32) |
| 411 | type = EVP_aes_256_gcm(); |
| 412 | break; |
| 413 | } |
| 414 | if (!type) |
| 415 | return MZ_PARAM_ERROR; |
| 416 | |
| 417 | aes->ctx = EVP_CIPHER_CTX_new(); |
| 418 | if (!aes->ctx) |
| 419 | return MZ_MEM_ERROR; |
| 420 | |
| 421 | if (!EVP_CipherInit_ex(aes->ctx, type, NULL, key, iv, encrypt)) { |
| 422 | aes->error = ERR_get_error(); |
| 423 | return MZ_HASH_ERROR; |
| 424 | } |
| 425 | |
| 426 | EVP_CIPHER_CTX_set_padding(aes->ctx, aes->mode == MZ_AES_MODE_GCM); |
| 427 | |
| 428 | return MZ_OK; |
| 429 | } |
| 430 | |
| 431 | int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 432 | int32_t iv_length) { |
| 433 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 434 | |
| 435 | if (!aes || !key || !key_length) |
| 436 | return MZ_PARAM_ERROR; |
| 437 | if (key_length != 16 && key_length != 24 && key_length != 32) |
| 438 | return MZ_PARAM_ERROR; |
| 439 | if (iv && iv_length != MZ_AES_BLOCK_SIZE) |
| 440 | return MZ_PARAM_ERROR; |
| 441 | |
| 442 | mz_crypt_aes_reset(handle); |
| 443 | |
| 444 | return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, 1); |
| 445 | } |
| 446 | |
| 447 | int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv, |
| 448 | int32_t iv_length) { |
| 449 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 450 | |
| 451 | if (!aes || !key || !key_length) |
| 452 | return MZ_PARAM_ERROR; |
| 453 | if (key_length != 16 && key_length != 24 && key_length != 32) |
| 454 | return MZ_PARAM_ERROR; |
| 455 | if (iv && iv_length > MZ_AES_BLOCK_SIZE) |
| 456 | return MZ_PARAM_ERROR; |
| 457 | |
| 458 | mz_crypt_aes_reset(handle); |
| 459 | |
| 460 | return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, 0); |
| 461 | } |
| 462 | |
| 463 | void mz_crypt_aes_set_mode(void *handle, int32_t mode) { |
| 464 | mz_crypt_aes *aes = (mz_crypt_aes *)handle; |
| 465 | aes->mode = mode; |
| 466 | } |
| 467 | |
| 468 | void *mz_crypt_aes_create(void) { |
| 469 | mz_crypt_aes *aes = (mz_crypt_aes *)calloc(1, sizeof(mz_crypt_aes)); |
| 470 | return aes; |
| 471 | } |
| 472 | |
| 473 | void mz_crypt_aes_delete(void **handle) { |
| 474 | mz_crypt_aes *aes = NULL; |
| 475 | if (!handle) |
| 476 | return; |
| 477 | aes = (mz_crypt_aes *)*handle; |
| 478 | if (aes) { |
| 479 | mz_crypt_aes_free(*handle); |
| 480 | free(aes); |
| 481 | } |
| 482 | *handle = NULL; |
| 483 | } |
| 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 | static void mz_crypt_hmac_free(void *handle) { |
| 521 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 522 | |
| 523 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 524 | 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 | hmac->ctx = NULL; |
| 534 | } |
| 535 | |
| 536 | void mz_crypt_hmac_reset(void *handle) { |
| 537 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 538 | |
| 539 | mz_crypt_init(); |
| 540 | mz_crypt_hmac_free(handle); |
| 541 | |
| 542 | hmac->error = 0; |
| 543 | } |
| 544 | |
| 545 | int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) { |
| 546 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 547 | int32_t result = 0; |
| 548 | |
| 549 | if (!hmac || !key) |
| 550 | return MZ_PARAM_ERROR; |
| 551 | |
| 552 | mz_crypt_hmac_reset(handle); |
| 553 | |
| 554 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 555 | const EVP_MD *evp_md = NULL; |
| 556 | |
| 557 | if (hmac->algorithm == MZ_HASH_SHA1) |
| 558 | evp_md = EVP_sha1(); |
| 559 | else |
| 560 | evp_md = EVP_sha256(); |
| 561 | |
| 562 | hmac->ctx = HMAC_CTX_new(); |
| 563 | if (!hmac->ctx) |
| 564 | return MZ_MEM_ERROR; |
| 565 | |
| 566 | 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 | if (!result) { |
| 589 | hmac->error = ERR_get_error(); |
| 590 | return MZ_HASH_ERROR; |
| 591 | } |
| 592 | |
| 593 | return MZ_OK; |
| 594 | } |
| 595 | |
| 596 | int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) { |
| 597 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 598 | int32_t result = 0; |
| 599 | |
| 600 | if (!hmac || !buf) |
| 601 | return MZ_PARAM_ERROR; |
| 602 | |
| 603 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 604 | result = HMAC_Update(hmac->ctx, buf, size); |
| 605 | #else |
| 606 | result = EVP_MAC_update(hmac->ctx, buf, size); |
| 607 | #endif |
| 608 | if (!result) { |
| 609 | hmac->error = ERR_get_error(); |
| 610 | return MZ_HASH_ERROR; |
| 611 | } |
| 612 | |
| 613 | return MZ_OK; |
| 614 | } |
| 615 | |
| 616 | int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size) { |
| 617 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 618 | int32_t result = 0; |
| 619 | |
| 620 | if (!hmac || !digest) |
| 621 | return MZ_PARAM_ERROR; |
| 622 | |
| 623 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 624 | if (hmac->algorithm == MZ_HASH_SHA1) { |
| 625 | if (digest_size < MZ_HASH_SHA1_SIZE) |
| 626 | return MZ_BUF_ERROR; |
| 627 | |
| 628 | result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size); |
| 629 | } else { |
| 630 | if (digest_size < MZ_HASH_SHA256_SIZE) |
| 631 | return MZ_BUF_ERROR; |
| 632 | result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size); |
| 633 | } |
| 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 | if (!result) { |
| 642 | hmac->error = ERR_get_error(); |
| 643 | return MZ_HASH_ERROR; |
| 644 | } |
| 645 | |
| 646 | return MZ_OK; |
| 647 | } |
| 648 | |
| 649 | void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm) { |
| 650 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle; |
| 651 | hmac->algorithm = algorithm; |
| 652 | } |
| 653 | |
| 654 | int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle) { |
| 655 | mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle; |
| 656 | mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle; |
| 657 | |
| 658 | if (!source || !target) |
| 659 | return MZ_PARAM_ERROR; |
| 660 | |
| 661 | mz_crypt_hmac_reset(target_handle); |
| 662 | |
| 663 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 664 | if (!target->ctx) |
| 665 | target->ctx = HMAC_CTX_new(); |
| 666 | |
| 667 | if (!HMAC_CTX_copy(target->ctx, source->ctx)) { |
| 668 | target->error = ERR_get_error(); |
| 669 | return MZ_HASH_ERROR; |
| 670 | } |
| 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 | return MZ_OK; |
| 679 | } |
| 680 | |
| 681 | void *mz_crypt_hmac_create(void) { |
| 682 | mz_crypt_hmac *hmac = (mz_crypt_hmac *)calloc(1, sizeof(mz_crypt_hmac)); |
| 683 | if (hmac) |
| 684 | hmac->algorithm = MZ_HASH_SHA256; |
| 685 | return hmac; |
| 686 | } |
| 687 | |
| 688 | void mz_crypt_hmac_delete(void **handle) { |
| 689 | mz_crypt_hmac *hmac = NULL; |
| 690 | if (!handle) |
| 691 | return; |
| 692 | hmac = (mz_crypt_hmac *)*handle; |
| 693 | if (hmac) { |
| 694 | mz_crypt_hmac_free(*handle); |
| 695 | free(hmac); |
| 696 | } |
| 697 | *handle = NULL; |
| 698 | } |