Debian Codegraph @

mz_crypt_apple.c

raw symbols SCIP
1 /* mz_crypt_apple.c -- Crypto/hash functions for Apple
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 <CoreFoundation/CoreFoundation.h>
15 #include <CommonCrypto/CommonCryptor.h>
16 #include <CommonCrypto/CommonDigest.h>
17 #include <CommonCrypto/CommonHMAC.h>
18 #include <Security/Security.h>
19 #include <Security/SecPolicy.h>
20
21 /***************************************************************************/
22
23 #ifndef MZ_TARGET_APPSTORE
24 # define MZ_TARGET_APPSTORE 1
25 #endif
26
27 /* Avoid use of private API for App Store as Apple does not allow it. Zip format doesn't need GCM. */
28 #if !MZ_TARGET_APPSTORE
29 enum {
30 kCCModeGCM = 11,
31 };
32
33 CCCryptorStatus CCCryptorGCMReset(CCCryptorRef cryptorRef);
34 CCCryptorStatus CCCryptorGCMAddIV(CCCryptorRef cryptorRef, const void *iv, size_t ivLen);
35 CCCryptorStatus CCCryptorGCMAddAAD(CCCryptorRef cryptorRef, const void *aData, size_t aDataLen);
36 CCCryptorStatus CCCryptorGCMEncrypt(CCCryptorRef cryptorRef, const void *dataIn, size_t dataInLength, void *dataOut);
37 CCCryptorStatus CCCryptorGCMDecrypt(CCCryptorRef cryptorRef, const void *dataIn, size_t dataInLength, void *dataOut);
38 CCCryptorStatus CCCryptorGCMFinal(CCCryptorRef cryptorRef, void *tagOut, size_t *tagLength);
39 #endif
40
41 /***************************************************************************/
42
43 int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
44 if (SecRandomCopyBytes(kSecRandomDefault, size, buf) != errSecSuccess)
45 return 0;
46 return size;
47 }
48
49 /***************************************************************************/
50
51 typedef struct mz_crypt_sha_s {
52 union {
53 CC_SHA1_CTX ctx1;
54 CC_SHA256_CTX ctx256;
55 CC_SHA512_CTX ctx512;
56 };
57 int32_t error;
58 int32_t initialized;
59 uint16_t algorithm;
60 } mz_crypt_sha;
61
62 /***************************************************************************/
63
64 static const uint8_t mz_crypt_sha_digest_size[] = {
65 MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE, MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE};
66
67 /***************************************************************************/
68
69 void mz_crypt_sha_reset(void *handle) {
70 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
71
72 sha->error = 0;
73 sha->initialized = 0;
74 }
75
76 int32_t mz_crypt_sha_begin(void *handle) {
77 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
78
79 if (!sha)
80 return MZ_PARAM_ERROR;
81
82 mz_crypt_sha_reset(handle);
83
84 switch (sha->algorithm) {
85 case MZ_HASH_SHA1:
86 sha->error = CC_SHA1_Init(&sha->ctx1);
87 break;
88 case MZ_HASH_SHA224:
89 sha->error = CC_SHA224_Init(&sha->ctx256);
90 break;
91 case MZ_HASH_SHA256:
92 sha->error = CC_SHA256_Init(&sha->ctx256);
93 break;
94 case MZ_HASH_SHA384:
95 sha->error = CC_SHA384_Init(&sha->ctx512);
96 break;
97 case MZ_HASH_SHA512:
98 sha->error = CC_SHA512_Init(&sha->ctx512);
99 break;
100 default:
101 return MZ_PARAM_ERROR;
102 }
103
104 if (!sha->error)
105 return MZ_HASH_ERROR;
106
107 sha->initialized = 1;
108 return MZ_OK;
109 }
110
111 int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
112 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
113
114 if (!sha || !buf || !sha->initialized)
115 return MZ_PARAM_ERROR;
116
117 switch (sha->algorithm) {
118 case MZ_HASH_SHA1:
119 sha->error = CC_SHA1_Update(&sha->ctx1, buf, size);
120 break;
121 case MZ_HASH_SHA224:
122 sha->error = CC_SHA224_Update(&sha->ctx256, buf, size);
123 break;
124 case MZ_HASH_SHA256:
125 sha->error = CC_SHA256_Update(&sha->ctx256, buf, size);
126 break;
127 case MZ_HASH_SHA384:
128 sha->error = CC_SHA384_Update(&sha->ctx512, buf, size);
129 break;
130 case MZ_HASH_SHA512:
131 sha->error = CC_SHA512_Update(&sha->ctx512, buf, size);
132 break;
133 }
134
135 if (!sha->error)
136 return MZ_HASH_ERROR;
137
138 return size;
139 }
140
141 int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
142 mz_crypt_sha *sha = (mz_crypt_sha *)handle;
143
144 if (!sha || !digest || !sha->initialized)
145 return MZ_PARAM_ERROR;
146 if (digest_size < mz_crypt_sha_digest_size[sha->algorithm - MZ_HASH_SHA1])
147 return MZ_PARAM_ERROR;
148
149 switch (sha->algorithm) {
150 case MZ_HASH_SHA1:
151 sha->error = CC_SHA1_Final(digest, &sha->ctx1);
152 break;
153 case MZ_HASH_SHA224:
154 sha->error = CC_SHA224_Final(digest, &sha->ctx256);
155 break;
156 case MZ_HASH_SHA256:
157 sha->error = CC_SHA256_Final(digest, &sha->ctx256);
158 break;
159 case MZ_HASH_SHA384:
160 sha->error = CC_SHA384_Final(digest, &sha->ctx512);
161 break;
162 case MZ_HASH_SHA512:
163 sha->error = CC_SHA512_Final(digest, &sha->ctx512);
164 break;
165 }
166
167 if (!sha->error)
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 memset(sha, 0, sizeof(mz_crypt_sha));
185 sha->algorithm = MZ_HASH_SHA256;
186 }
187 return sha;
188 }
189
190 void mz_crypt_sha_delete(void **handle) {
191 mz_crypt_sha *sha = NULL;
192 if (!handle)
193 return;
194 sha = (mz_crypt_sha *)*handle;
195 if (sha) {
196 mz_crypt_sha_reset(*handle);
197 free(sha);
198 }
199 *handle = NULL;
200 }
201
202 /***************************************************************************/
203
204 typedef struct mz_crypt_aes_s {
205 CCCryptorRef crypt;
206 int32_t mode;
207 int32_t error;
208 } mz_crypt_aes;
209
210 /***************************************************************************/
211
212 static void mz_crypt_aes_free(void *handle) {
213 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
214
215 if (aes->crypt)
216 CCCryptorRelease(aes->crypt);
217 aes->crypt = NULL;
218 }
219
220 void mz_crypt_aes_reset(void *handle) {
221 mz_crypt_aes_free(handle);
222 }
223
224 int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
225 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
226 size_t data_moved = 0;
227
228 if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->crypt)
229 return MZ_PARAM_ERROR;
230
231 if (aes->mode == MZ_AES_MODE_GCM) {
232 #if MZ_TARGET_APPSTORE
233 return MZ_SUPPORT_ERROR;
234 #else
235 if (aad && aad_size > 0) {
236 aes->error = CCCryptorGCMAddAAD(aes->crypt, aad, aad_size);
237 if (aes->error != kCCSuccess)
238 return MZ_CRYPT_ERROR;
239 }
240 aes->error = CCCryptorGCMEncrypt(aes->crypt, buf, size, buf);
241 #endif
242 } else {
243 if (aad && aad_size > 0)
244 return MZ_PARAM_ERROR;
245 aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
246 }
247
248 if (aes->error != kCCSuccess)
249 return MZ_CRYPT_ERROR;
250
251 return size;
252 }
253
254 int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) {
255 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
256 #if !MZ_TARGET_APPSTORE
257 size_t tag_outsize = tag_size;
258 #endif
259
260 if (!aes || !tag || !tag_size || !aes->crypt || aes->mode != MZ_AES_MODE_GCM)
261 return MZ_PARAM_ERROR;
262
263 #if MZ_TARGET_APPSTORE
264 return MZ_SUPPORT_ERROR;
265 #else
266 aes->error = CCCryptorGCMEncrypt(aes->crypt, buf, size, buf);
267 if (aes->error != kCCSuccess)
268 return MZ_CRYPT_ERROR;
269
270 aes->error = CCCryptorGCMFinal(aes->crypt, tag, &tag_outsize);
271
272 if (aes->error != kCCSuccess)
273 return MZ_CRYPT_ERROR;
274
275 return size;
276 #endif
277 }
278
279 int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
280 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
281 size_t data_moved = 0;
282
283 if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->crypt)
284 return MZ_PARAM_ERROR;
285
286 if (aes->mode == MZ_AES_MODE_GCM) {
287 #if MZ_TARGET_APPSTORE
288 return MZ_SUPPORT_ERROR;
289 #else
290 if (aad && aad_size > 0) {
291 aes->error = CCCryptorGCMAddAAD(aes->crypt, aad, aad_size);
292 if (aes->error != kCCSuccess)
293 return MZ_CRYPT_ERROR;
294 }
295 aes->error = CCCryptorGCMDecrypt(aes->crypt, buf, size, buf);
296 #endif
297 } else {
298 if (aad && aad_size > 0)
299 return MZ_PARAM_ERROR;
300 aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
301 }
302
303 if (aes->error != kCCSuccess)
304 return MZ_CRYPT_ERROR;
305
306 return size;
307 }
308
309 int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
310 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
311 #if !MZ_TARGET_APPSTORE
312 uint8_t tag_actual_buf[MZ_AES_BLOCK_SIZE];
313 size_t tag_actual_len = sizeof(tag_actual_buf);
314 uint8_t *tag_actual = tag_actual_buf;
315 int32_t c = tag_length;
316 int32_t is_ok = 0;
317 #endif
318
319 if (!aes || !tag || !tag_length || !aes->crypt || aes->mode != MZ_AES_MODE_GCM)
320 return MZ_PARAM_ERROR;
321
322 #if MZ_TARGET_APPSTORE
323 return MZ_SUPPORT_ERROR;
324 #else
325 aes->error = CCCryptorGCMDecrypt(aes->crypt, buf, size, buf);
326 if (aes->error != kCCSuccess)
327 return MZ_CRYPT_ERROR;
328
329 /* CCCryptorGCMFinal does not verify tag */
330 aes->error = CCCryptorGCMFinal(aes->crypt, tag_actual, &tag_actual_len);
331
332 if (aes->error != kCCSuccess)
333 return MZ_CRYPT_ERROR;
334 if (tag_length != (int32_t)tag_actual_len)
335 return MZ_CRYPT_ERROR;
336
337 /* Timing safe comparison */
338 for (; c > 0; c--)
339 is_ok |= *tag++ ^ *tag_actual++;
340
341 if (is_ok)
342 return MZ_CRYPT_ERROR;
343
344 return size;
345 #endif
346 }
347
348 static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
349 int32_t iv_length, CCOperation op) {
350 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
351 CCMode mode;
352
353 if (aes->mode == MZ_AES_MODE_CBC)
354 mode = kCCModeCBC;
355 else if (aes->mode == MZ_AES_MODE_ECB)
356 mode = kCCModeECB;
357 else if (aes->mode == MZ_AES_MODE_GCM)
358 #if !MZ_TARGET_APPSTORE
359 mode = kCCModeGCM;
360 #else
361 return MZ_SUPPORT_ERROR;
362 #endif
363 else
364 return MZ_PARAM_ERROR;
365
366 mz_crypt_aes_reset(handle);
367
368 aes->error = CCCryptorCreateWithMode(op, mode, kCCAlgorithmAES, ccNoPadding, iv, key, key_length, NULL, 0, 0, 0,
369 &aes->crypt);
370
371 if (aes->error != kCCSuccess)
372 return MZ_HASH_ERROR;
373
374 #if !MZ_TARGET_APPSTORE
375 if (aes->mode == MZ_AES_MODE_GCM) {
376 aes->error = CCCryptorGCMAddIV(aes->crypt, iv, iv_length);
377
378 if (aes->error != kCCSuccess)
379 return MZ_HASH_ERROR;
380 }
381 #endif
382
383 return MZ_OK;
384 }
385
386 int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
387 int32_t iv_length) {
388 return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, kCCEncrypt);
389 }
390
391 int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
392 int32_t iv_length) {
393 return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, kCCDecrypt);
394 }
395
396 void mz_crypt_aes_set_mode(void *handle, int32_t mode) {
397 mz_crypt_aes *aes = (mz_crypt_aes *)handle;
398 aes->mode = mode;
399 }
400
401 void *mz_crypt_aes_create(void) {
402 mz_crypt_aes *aes = (mz_crypt_aes *)calloc(1, sizeof(mz_crypt_aes));
403 return aes;
404 }
405
406 void mz_crypt_aes_delete(void **handle) {
407 mz_crypt_aes *aes = NULL;
408 if (!handle)
409 return;
410 aes = (mz_crypt_aes *)*handle;
411 if (aes) {
412 mz_crypt_aes_free(*handle);
413 free(aes);
414 }
415 *handle = NULL;
416 }
417
418 /***************************************************************************/
419
420 typedef struct mz_crypt_hmac_s {
421 CCHmacContext ctx;
422 int32_t initialized;
423 int32_t error;
424 uint16_t algorithm;
425 } mz_crypt_hmac;
426
427 /***************************************************************************/
428
429 static void mz_crypt_hmac_free(void *handle) {
430 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
431 memset(&hmac->ctx, 0, sizeof(hmac->ctx));
432 }
433
434 void mz_crypt_hmac_reset(void *handle) {
435 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
436 mz_crypt_hmac_free(handle);
437 hmac->error = 0;
438 }
439
440 int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) {
441 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
442 CCHmacAlgorithm algorithm = 0;
443
444 if (!hmac || !key)
445 return MZ_PARAM_ERROR;
446
447 mz_crypt_hmac_reset(handle);
448
449 if (hmac->algorithm == MZ_HASH_SHA1)
450 algorithm = kCCHmacAlgSHA1;
451 else if (hmac->algorithm == MZ_HASH_SHA256)
452 algorithm = kCCHmacAlgSHA256;
453 else
454 return MZ_PARAM_ERROR;
455
456 CCHmacInit(&hmac->ctx, algorithm, key, key_length);
457 return MZ_OK;
458 }
459
460 int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) {
461 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
462
463 if (!hmac || !buf)
464 return MZ_PARAM_ERROR;
465
466 CCHmacUpdate(&hmac->ctx, buf, size);
467 return MZ_OK;
468 }
469
470 int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size) {
471 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
472
473 if (!hmac || !digest)
474 return MZ_PARAM_ERROR;
475
476 if (hmac->algorithm == MZ_HASH_SHA1) {
477 if (digest_size < MZ_HASH_SHA1_SIZE)
478 return MZ_BUF_ERROR;
479 CCHmacFinal(&hmac->ctx, digest);
480 } else {
481 if (digest_size < MZ_HASH_SHA256_SIZE)
482 return MZ_BUF_ERROR;
483 CCHmacFinal(&hmac->ctx, digest);
484 }
485
486 return MZ_OK;
487 }
488
489 void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm) {
490 mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
491 hmac->algorithm = algorithm;
492 }
493
494 int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle) {
495 mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
496 mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
497
498 if (!source || !target)
499 return MZ_PARAM_ERROR;
500
501 memcpy(&target->ctx, &source->ctx, sizeof(CCHmacContext));
502 return MZ_OK;
503 }
504
505 void *mz_crypt_hmac_create(void) {
506 mz_crypt_hmac *hmac = (mz_crypt_hmac *)calloc(1, sizeof(mz_crypt_hmac));
507 if (hmac)
508 hmac->algorithm = MZ_HASH_SHA256;
509 return hmac;
510 }
511
512 void mz_crypt_hmac_delete(void **handle) {
513 mz_crypt_hmac *hmac = NULL;
514 if (!handle)
515 return;
516 hmac = (mz_crypt_hmac *)*handle;
517 if (hmac) {
518 mz_crypt_hmac_free(*handle);
519 free(hmac);
520 }
521 *handle = NULL;
522 }