Debian Codegraph @

crt/aws-c-cal/source/darwin/commoncrypto_aes.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/cal/private/symmetric_cipher_priv.h>
7
8 #include <CommonCrypto/CommonCryptor.h>
9 #include <CommonCrypto/CommonHMAC.h>
10 #include <CommonCrypto/CommonSymmetricKeywrap.h>
11
12 #if !defined(AWS_APPSTORE_SAFE)
13 /* CommonCrypto does not offer public APIs for doing AES GCM.
14 * There are private APIs for doing it (CommonCryptoSPI.h), but App Store
15 * submissions that reference these private symbols will be rejected. */
16
17 # define SUPPORT_AES_GCM_VIA_SPI 1
18 # include "common_cryptor_spi.h"
19 #endif
20
21 struct cc_aes_cipher {
22 struct aws_symmetric_cipher cipher_base;
23 struct _CCCryptor *encryptor_handle;
24 struct _CCCryptor *decryptor_handle;
25 struct aws_byte_buf working_buffer;
26 };
27
28 static int s_encrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
29 /* allow for a padded block by making sure we have at least a block of padding reserved. */
30 size_t required_buffer_space = input.len + cipher->block_size - 1;
31
32 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
33 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
34 }
35
36 size_t available_write_space = out->capacity - out->len;
37 struct cc_aes_cipher *cc_cipher = cipher->impl;
38
39 size_t len_written = 0;
40 CCStatus status = CCCryptorUpdate(
41 cc_cipher->encryptor_handle, input.ptr, input.len, out->buffer + out->len, available_write_space, &len_written);
42
43 if (status != kCCSuccess) {
44 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
45 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
46 }
47
48 out->len += len_written;
49 return AWS_OP_SUCCESS;
50 }
51
52 static int s_decrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
53 /* allow for a padded block by making sure we have at least a block of padding reserved. */
54 size_t required_buffer_space = input.len + cipher->block_size - 1;
55
56 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
57 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
58 }
59
60 size_t available_write_space = out->capacity - out->len;
61 struct cc_aes_cipher *cc_cipher = cipher->impl;
62
63 size_t len_written = 0;
64 CCStatus status = CCCryptorUpdate(
65 cc_cipher->decryptor_handle, input.ptr, input.len, out->buffer + out->len, available_write_space, &len_written);
66
67 if (status != kCCSuccess) {
68 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
69 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
70 }
71
72 out->len += len_written;
73 return AWS_OP_SUCCESS;
74 }
75
76 static int s_finalize_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
77 /* in CBC mode, this will pad the final block from the previous encrypt call, or do nothing
78 * if we were already on a block boundary. In CTR mode this will do nothing. */
79 size_t required_buffer_space = cipher->block_size;
80 size_t len_written = 0;
81
82 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
83 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
84 }
85
86 size_t available_write_space = out->capacity - out->len;
87 struct cc_aes_cipher *cc_cipher = cipher->impl;
88
89 CCStatus status =
90 CCCryptorFinal(cc_cipher->encryptor_handle, out->buffer + out->len, available_write_space, &len_written);
91
92 if (status != kCCSuccess) {
93 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
94 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
95 }
96
97 out->len += len_written;
98 return AWS_OP_SUCCESS;
99 }
100
101 static int s_finalize_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
102 /* in CBC mode, this will pad the final block from the previous encrypt call, or do nothing
103 * if we were already on a block boundary. In CTR mode this will do nothing. */
104 size_t required_buffer_space = cipher->block_size;
105 size_t len_written = 0;
106
107 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
108 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
109 }
110
111 size_t available_write_space = out->capacity - out->len;
112 struct cc_aes_cipher *cc_cipher = cipher->impl;
113
114 CCStatus status =
115 CCCryptorFinal(cc_cipher->decryptor_handle, out->buffer + out->len, available_write_space, &len_written);
116
117 if (status != kCCSuccess) {
118 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
119 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
120 }
121
122 out->len += len_written;
123 return AWS_OP_SUCCESS;
124 }
125
126 static int s_initialize_cbc_cipher_materials(
127 struct cc_aes_cipher *cc_cipher,
128 const struct aws_byte_cursor *key,
129 const struct aws_byte_cursor *iv) {
130 if (!cc_cipher->cipher_base.key.len) {
131 if (key) {
132 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, *key);
133 } else {
134 aws_byte_buf_init(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, AWS_AES_256_KEY_BYTE_LEN);
135 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cc_cipher->cipher_base.key);
136 }
137 }
138
139 if (!cc_cipher->cipher_base.iv.len) {
140 if (iv) {
141 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, *iv);
142 } else {
143 aws_byte_buf_init(
144 &cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
145 aws_symmetric_cipher_generate_initialization_vector(
146 AWS_AES_256_CIPHER_BLOCK_SIZE, false, &cc_cipher->cipher_base.iv);
147 }
148 }
149
150 CCCryptorStatus status = CCCryptorCreateWithMode(
151 kCCEncrypt,
152 kCCModeCBC,
153 kCCAlgorithmAES,
154 ccPKCS7Padding,
155 cc_cipher->cipher_base.iv.buffer,
156 cc_cipher->cipher_base.key.buffer,
157 cc_cipher->cipher_base.key.len,
158 NULL,
159 0,
160 0,
161 0,
162 &cc_cipher->encryptor_handle);
163
164 status |= CCCryptorCreateWithMode(
165 kCCDecrypt,
166 kCCModeCBC,
167 kCCAlgorithmAES,
168 ccPKCS7Padding,
169 cc_cipher->cipher_base.iv.buffer,
170 cc_cipher->cipher_base.key.buffer,
171 cc_cipher->cipher_base.key.len,
172 NULL,
173 0,
174 0,
175 0,
176 &cc_cipher->decryptor_handle);
177
178 return status == kCCSuccess ? AWS_OP_SUCCESS : aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
179 }
180
181 static int s_reset(struct aws_symmetric_cipher *cipher) {
182 struct cc_aes_cipher *cc_cipher = cipher->impl;
183
184 if (cc_cipher->encryptor_handle) {
185 CCCryptorRelease(cc_cipher->encryptor_handle);
186 cc_cipher->encryptor_handle = NULL;
187 }
188
189 if (cc_cipher->decryptor_handle) {
190 CCCryptorRelease(cc_cipher->decryptor_handle);
191 cc_cipher->decryptor_handle = NULL;
192 }
193
194 aws_byte_buf_secure_zero(&cc_cipher->working_buffer);
195
196 return AWS_OP_SUCCESS;
197 }
198
199 static void s_destroy(struct aws_symmetric_cipher *cipher) {
200 aws_byte_buf_clean_up_secure(&cipher->key);
201 aws_byte_buf_clean_up_secure(&cipher->iv);
202 aws_byte_buf_clean_up_secure(&cipher->tag);
203 aws_byte_buf_clean_up_secure(&cipher->aad);
204
205 s_reset(cipher);
206
207 struct cc_aes_cipher *cc_cipher = cipher->impl;
208 aws_byte_buf_clean_up_secure(&cc_cipher->working_buffer);
209
210 aws_mem_release(cipher->allocator, cc_cipher);
211 }
212
213 static int s_cbc_reset(struct aws_symmetric_cipher *cipher) {
214 struct cc_aes_cipher *cc_cipher = cipher->impl;
215
216 int ret_val = s_reset(cipher);
217
218 if (ret_val == AWS_OP_SUCCESS) {
219 ret_val = s_initialize_cbc_cipher_materials(cc_cipher, NULL, NULL);
220 }
221
222 return ret_val;
223 }
224
225 static struct aws_symmetric_cipher_vtable s_aes_cbc_vtable = {
226 .finalize_decryption = s_finalize_decryption,
227 .finalize_encryption = s_finalize_encryption,
228 .decrypt = s_decrypt,
229 .encrypt = s_encrypt,
230 .provider = "CommonCrypto",
231 .alg_name = "AES-CBC 256",
232 .destroy = s_destroy,
233 .reset = s_cbc_reset,
234 };
235
236 struct aws_symmetric_cipher *aws_aes_cbc_256_new_impl(
237 struct aws_allocator *allocator,
238 const struct aws_byte_cursor *key,
239 const struct aws_byte_cursor *iv) {
240 struct cc_aes_cipher *cc_cipher = aws_mem_calloc(allocator, 1, sizeof(struct cc_aes_cipher));
241 cc_cipher->cipher_base.allocator = allocator;
242 cc_cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
243 cc_cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
244 cc_cipher->cipher_base.impl = cc_cipher;
245 cc_cipher->cipher_base.vtable = &s_aes_cbc_vtable;
246
247 if (s_initialize_cbc_cipher_materials(cc_cipher, key, iv) != AWS_OP_SUCCESS) {
248 s_destroy(&cc_cipher->cipher_base);
249 return NULL;
250 }
251
252 cc_cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
253 cc_cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
254
255 return &cc_cipher->cipher_base;
256 }
257
258 static int s_initialize_ctr_cipher_materials(
259 struct cc_aes_cipher *cc_cipher,
260 const struct aws_byte_cursor *key,
261 const struct aws_byte_cursor *iv) {
262 if (!cc_cipher->cipher_base.key.len) {
263 if (key) {
264 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, *key);
265 } else {
266 aws_byte_buf_init(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, AWS_AES_256_KEY_BYTE_LEN);
267 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cc_cipher->cipher_base.key);
268 }
269 }
270
271 if (!cc_cipher->cipher_base.iv.len) {
272 if (iv) {
273 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, *iv);
274 } else {
275 aws_byte_buf_init(
276 &cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
277 aws_symmetric_cipher_generate_initialization_vector(
278 AWS_AES_256_CIPHER_BLOCK_SIZE, true, &cc_cipher->cipher_base.iv);
279 }
280 }
281
282 CCCryptorStatus status = CCCryptorCreateWithMode(
283 kCCEncrypt,
284 kCCModeCTR,
285 kCCAlgorithmAES,
286 ccNoPadding,
287 cc_cipher->cipher_base.iv.buffer,
288 cc_cipher->cipher_base.key.buffer,
289 cc_cipher->cipher_base.key.len,
290 NULL,
291 0,
292 0,
293 kCCModeOptionCTR_BE,
294 &cc_cipher->encryptor_handle);
295
296 status |= CCCryptorCreateWithMode(
297 kCCDecrypt,
298 kCCModeCTR,
299 kCCAlgorithmAES,
300 ccNoPadding,
301 cc_cipher->cipher_base.iv.buffer,
302 cc_cipher->cipher_base.key.buffer,
303 cc_cipher->cipher_base.key.len,
304 NULL,
305 0,
306 0,
307 kCCModeOptionCTR_BE,
308 &cc_cipher->decryptor_handle);
309
310 return status == kCCSuccess ? AWS_OP_SUCCESS : aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
311 }
312
313 static int s_ctr_reset(struct aws_symmetric_cipher *cipher) {
314 struct cc_aes_cipher *cc_cipher = cipher->impl;
315
316 int ret_val = s_reset(cipher);
317
318 if (ret_val == AWS_OP_SUCCESS) {
319 ret_val = s_initialize_ctr_cipher_materials(cc_cipher, NULL, NULL);
320 }
321
322 return ret_val;
323 }
324
325 static struct aws_symmetric_cipher_vtable s_aes_ctr_vtable = {
326 .finalize_decryption = s_finalize_decryption,
327 .finalize_encryption = s_finalize_encryption,
328 .decrypt = s_decrypt,
329 .encrypt = s_encrypt,
330 .provider = "CommonCrypto",
331 .alg_name = "AES-CTR 256",
332 .destroy = s_destroy,
333 .reset = s_ctr_reset,
334 };
335
336 struct aws_symmetric_cipher *aws_aes_ctr_256_new_impl(
337 struct aws_allocator *allocator,
338 const struct aws_byte_cursor *key,
339 const struct aws_byte_cursor *iv) {
340 struct cc_aes_cipher *cc_cipher = aws_mem_calloc(allocator, 1, sizeof(struct cc_aes_cipher));
341 cc_cipher->cipher_base.allocator = allocator;
342 cc_cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
343 cc_cipher->cipher_base.impl = cc_cipher;
344 cc_cipher->cipher_base.vtable = &s_aes_ctr_vtable;
345
346 if (s_initialize_ctr_cipher_materials(cc_cipher, key, iv) != AWS_OP_SUCCESS) {
347 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
348 s_destroy(&cc_cipher->cipher_base);
349 return NULL;
350 }
351
352 cc_cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
353 cc_cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
354
355 return &cc_cipher->cipher_base;
356 }
357
358 static int s_gcm_decrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
359 if (cipher->tag.buffer == NULL) {
360 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
361 }
362
363 return s_decrypt(cipher, input, out);
364 }
365
366 #ifdef SUPPORT_AES_GCM_VIA_SPI
367
368 static int s_finalize_gcm_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
369 (void)out;
370
371 /* user specification takes precedence. If its wrong its wrong */
372 if (!cipher->tag.len) {
373 aws_byte_buf_init(&cipher->tag, cipher->allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
374 }
375
376 struct cc_aes_cipher *cc_cipher = cipher->impl;
377
378 size_t tag_length = AWS_AES_256_CIPHER_BLOCK_SIZE;
379 CCStatus status = CCCryptorGCMFinalize(cc_cipher->encryptor_handle, cipher->tag.buffer, tag_length);
380 if (status != kCCSuccess) {
381 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
382 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
383 }
384
385 cipher->tag.len = tag_length;
386 return AWS_OP_SUCCESS;
387 }
388
389 static int s_finalize_gcm_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
390 (void)out;
391
392 struct cc_aes_cipher *cc_cipher = cipher->impl;
393
394 size_t tag_length = AWS_AES_256_CIPHER_BLOCK_SIZE;
395 CCStatus status = CCCryptorGCMFinalize(cc_cipher->decryptor_handle, cipher->tag.buffer, tag_length);
396 if (status != kCCSuccess) {
397 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
398 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
399 }
400
401 return AWS_OP_SUCCESS;
402 }
403
404 static int s_initialize_gcm_cipher_materials(
405 struct cc_aes_cipher *cc_cipher,
406 const struct aws_byte_cursor *key,
407 const struct aws_byte_cursor *iv,
408 const struct aws_byte_cursor *aad) {
409 if (!cc_cipher->cipher_base.key.len) {
410 if (key) {
411 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, *key);
412 } else {
413 aws_byte_buf_init(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, AWS_AES_256_KEY_BYTE_LEN);
414 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cc_cipher->cipher_base.key);
415 }
416 }
417
418 if (!cc_cipher->cipher_base.iv.len) {
419 if (iv) {
420 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, *iv);
421 } else {
422 /* GCM IVs are kind of a hidden implementation detail. 4 are reserved by the system for long running stream
423 * blocks. */
424 /* This is because there's a GMAC attached to the cipher (that's what tag is for). For that to work, it has
425 * to control the actual counter */
426 aws_byte_buf_init(
427 &cc_cipher->cipher_base.iv, cc_cipher->cipher_base.allocator, AWS_AES_256_CIPHER_BLOCK_SIZE - 4);
428 aws_symmetric_cipher_generate_initialization_vector(
429 AWS_AES_256_CIPHER_BLOCK_SIZE - 4, false, &cc_cipher->cipher_base.iv);
430 }
431 }
432
433 if (aad && aad->len) {
434 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.aad, cc_cipher->cipher_base.allocator, *aad);
435 }
436
437 CCCryptorStatus status = CCCryptorCreateWithMode(
438 kCCEncrypt,
439 kCCModeGCM,
440 kCCAlgorithmAES,
441 ccNoPadding,
442 NULL,
443 cc_cipher->cipher_base.key.buffer,
444 cc_cipher->cipher_base.key.len,
445 NULL,
446 0,
447 0,
448 kCCModeOptionCTR_BE,
449 &cc_cipher->encryptor_handle);
450
451 if (status != kCCSuccess) {
452 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
453 }
454 status =
455 CCCryptorGCMSetIV(cc_cipher->encryptor_handle, cc_cipher->cipher_base.iv.buffer, cc_cipher->cipher_base.iv.len);
456
457 if (status != kCCSuccess) {
458 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
459 }
460
461 if (cc_cipher->cipher_base.aad.len) {
462 status = CCCryptorGCMAddAAD(
463 cc_cipher->encryptor_handle, cc_cipher->cipher_base.aad.buffer, cc_cipher->cipher_base.aad.len);
464
465 if (status != kCCSuccess) {
466 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
467 }
468 }
469
470 status = CCCryptorCreateWithMode(
471 kCCDecrypt,
472 kCCModeGCM,
473 kCCAlgorithmAES,
474 ccNoPadding,
475 NULL,
476 cc_cipher->cipher_base.key.buffer,
477 cc_cipher->cipher_base.key.len,
478 NULL,
479 0,
480 0,
481 kCCModeOptionCTR_BE,
482 &cc_cipher->decryptor_handle);
483
484 if (status != kCCSuccess) {
485 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
486 }
487 status =
488 CCCryptorGCMSetIV(cc_cipher->decryptor_handle, cc_cipher->cipher_base.iv.buffer, cc_cipher->cipher_base.iv.len);
489
490 if (status != kCCSuccess) {
491 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
492 }
493
494 if (cc_cipher->cipher_base.aad.len) {
495 status = CCCryptorGCMAddAAD(
496 cc_cipher->decryptor_handle, cc_cipher->cipher_base.aad.buffer, cc_cipher->cipher_base.aad.len);
497 }
498
499 if (status != kCCSuccess) {
500 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
501 }
502
503 return AWS_OP_SUCCESS;
504 }
505
506 static int s_gcm_reset(struct aws_symmetric_cipher *cipher) {
507 struct cc_aes_cipher *cc_cipher = cipher->impl;
508
509 int ret_val = s_reset(cipher);
510 aws_byte_buf_clean_up_secure(&cc_cipher->cipher_base.tag);
511
512 if (ret_val == AWS_OP_SUCCESS) {
513 ret_val = s_initialize_gcm_cipher_materials(cc_cipher, NULL, NULL, NULL);
514 }
515
516 return ret_val;
517 }
518
519 static struct aws_symmetric_cipher_vtable s_aes_gcm_vtable = {
520 .finalize_decryption = s_finalize_gcm_decryption,
521 .finalize_encryption = s_finalize_gcm_encryption,
522 .decrypt = s_gcm_decrypt,
523 .encrypt = s_encrypt,
524 .provider = "CommonCrypto",
525 .alg_name = "AES-GCM 256",
526 .destroy = s_destroy,
527 .reset = s_gcm_reset,
528 };
529
530 struct aws_symmetric_cipher *aws_aes_gcm_256_new_impl(
531 struct aws_allocator *allocator,
532 const struct aws_byte_cursor *key,
533 const struct aws_byte_cursor *iv,
534 const struct aws_byte_cursor *aad) {
535 struct cc_aes_cipher *cc_cipher = aws_mem_calloc(allocator, 1, sizeof(struct cc_aes_cipher));
536 cc_cipher->cipher_base.allocator = allocator;
537 cc_cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
538 cc_cipher->cipher_base.impl = cc_cipher;
539 cc_cipher->cipher_base.vtable = &s_aes_gcm_vtable;
540
541 if (s_initialize_gcm_cipher_materials(cc_cipher, key, iv, aad) != AWS_OP_SUCCESS) {
542 s_destroy(&cc_cipher->cipher_base);
543 return NULL;
544 }
545
546 cc_cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
547 cc_cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
548
549 return &cc_cipher->cipher_base;
550 }
551
552 #else /* !SUPPORT_AES_GCM_VIA_SPI */
553
554 struct aws_symmetric_cipher *aws_aes_gcm_256_new_impl(
555 struct aws_allocator *allocator,
556 const struct aws_byte_cursor *key,
557 const struct aws_byte_cursor *iv,
558 const struct aws_byte_cursor *aad) {
559
560 (void)allocator;
561 (void)key;
562 (void)iv;
563 (void)aad;
564 aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
565 return NULL;
566 }
567
568 #endif /* SUPPORT_AES_GCM_VIA_SPI */
569
570 static int s_keywrap_encrypt_decrypt(
571 struct aws_symmetric_cipher *cipher,
572 struct aws_byte_cursor input,
573 struct aws_byte_buf *out) {
574 struct cc_aes_cipher *cc_cipher = cipher->impl;
575 return aws_byte_buf_append_dynamic(&cc_cipher->working_buffer, &input);
576 }
577
578 static int s_finalize_keywrap_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
579 struct cc_aes_cipher *cc_cipher = cipher->impl;
580
581 if (cc_cipher->working_buffer.len == 0) {
582 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
583 return aws_raise_error(AWS_ERROR_INVALID_STATE);
584 }
585
586 size_t output_buffer_len = cipher->block_size + cc_cipher->working_buffer.len;
587
588 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, output_buffer_len)) {
589 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
590 }
591
592 CCCryptorStatus status = CCSymmetricKeyWrap(
593 kCCWRAPAES,
594 CCrfc3394_iv,
595 CCrfc3394_ivLen,
596 cipher->key.buffer,
597 cipher->key.len,
598 cc_cipher->working_buffer.buffer,
599 cc_cipher->working_buffer.len,
600 out->buffer,
601 &output_buffer_len);
602
603 if (status != kCCSuccess) {
604 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
605 return aws_raise_error(AWS_ERROR_INVALID_STATE);
606 }
607
608 out->len += output_buffer_len;
609
610 return AWS_OP_SUCCESS;
611 }
612
613 static int s_finalize_keywrap_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
614 struct cc_aes_cipher *cc_cipher = cipher->impl;
615
616 if (cc_cipher->working_buffer.len == 0) {
617 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
618 return aws_raise_error(AWS_ERROR_INVALID_STATE);
619 }
620
621 size_t output_buffer_len = cipher->block_size + cc_cipher->working_buffer.len;
622
623 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, output_buffer_len)) {
624 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
625 }
626
627 CCCryptorStatus status = CCSymmetricKeyUnwrap(
628 kCCWRAPAES,
629 CCrfc3394_iv,
630 CCrfc3394_ivLen,
631 cipher->key.buffer,
632 cipher->key.len,
633 cc_cipher->working_buffer.buffer,
634 cc_cipher->working_buffer.len,
635 out->buffer,
636 &output_buffer_len);
637
638 if (status != kCCSuccess) {
639 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
640 return aws_raise_error(AWS_ERROR_INVALID_STATE);
641 }
642
643 out->len += output_buffer_len;
644
645 return AWS_OP_SUCCESS;
646 }
647
648 static struct aws_symmetric_cipher_vtable s_aes_keywrap_vtable = {
649 .finalize_decryption = s_finalize_keywrap_decryption,
650 .finalize_encryption = s_finalize_keywrap_encryption,
651 .decrypt = s_keywrap_encrypt_decrypt,
652 .encrypt = s_keywrap_encrypt_decrypt,
653 .provider = "CommonCrypto",
654 .alg_name = "AES-KEYWRAP 256",
655 .destroy = s_destroy,
656 .reset = s_reset,
657 };
658
659 struct aws_symmetric_cipher *aws_aes_keywrap_256_new_impl(
660 struct aws_allocator *allocator,
661 const struct aws_byte_cursor *key) {
662 struct cc_aes_cipher *cc_cipher = aws_mem_calloc(allocator, 1, sizeof(struct cc_aes_cipher));
663 cc_cipher->cipher_base.allocator = allocator;
664 cc_cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE / 2;
665 cc_cipher->cipher_base.impl = cc_cipher;
666 cc_cipher->cipher_base.vtable = &s_aes_keywrap_vtable;
667
668 if (key) {
669 aws_byte_buf_init_copy_from_cursor(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, *key);
670 } else {
671 aws_byte_buf_init(&cc_cipher->cipher_base.key, cc_cipher->cipher_base.allocator, AWS_AES_256_KEY_BYTE_LEN);
672 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cc_cipher->cipher_base.key);
673 }
674
675 aws_byte_buf_init(&cc_cipher->working_buffer, allocator, (AWS_AES_256_CIPHER_BLOCK_SIZE * 2) + 8);
676 cc_cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
677 cc_cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
678
679 return &cc_cipher->cipher_base;
680 }