Debian Codegraph @

crt/aws-c-cal/source/unix/openssl_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 #include <aws/cal/private/symmetric_cipher_priv.h>
6
7 #define OPENSSL_SUPPRESS_DEPRECATED
8 #include <openssl/evp.h>
9
10 struct openssl_aes_cipher {
11 struct aws_symmetric_cipher cipher_base;
12 EVP_CIPHER_CTX *encryptor_ctx;
13 EVP_CIPHER_CTX *decryptor_ctx;
14 struct aws_byte_buf working_buffer;
15 };
16
17 static struct aws_byte_cursor s_empty_plain_text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
18
19 static int s_encrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
20
21 /*
22 * Openssl 1.1.1 and its derivatives like aws-lc and boringssl do not handle
23 * the case of null input of 0 len gracefully in update (it succeeds, but
24 * finalize after it will fail). Openssl 3.0 fixed this. Other crypto implementations
25 * do not have similar issue.
26 * To workaround the issue, replace null cursor with empty cursor.
27 */
28 if (input.len == 0) {
29 input = s_empty_plain_text;
30 }
31
32 size_t required_buffer_space = input.len + cipher->block_size;
33
34 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
35 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
36 }
37
38 size_t available_write_space = out->capacity - out->len;
39 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
40
41 int len_written = (int)(available_write_space);
42 if (!EVP_EncryptUpdate(
43 openssl_cipher->encryptor_ctx, out->buffer + out->len, &len_written, input.ptr, (int)input.len)) {
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_finalize_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
53 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
54
55 size_t required_buffer_space = cipher->block_size;
56
57 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
58 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
59 }
60
61 int len_written = (int)(out->capacity - out->len);
62 if (!EVP_EncryptFinal_ex(openssl_cipher->encryptor_ctx, out->buffer + out->len, &len_written)) {
63 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
64 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
65 }
66
67 out->len += len_written;
68 return AWS_OP_SUCCESS;
69 }
70
71 static int s_decrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
72 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
73
74 size_t required_buffer_space = input.len + cipher->block_size;
75
76 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
77 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
78 }
79
80 size_t available_write_space = out->capacity - out->len;
81
82 int len_written = (int)available_write_space;
83 if (!EVP_DecryptUpdate(
84 openssl_cipher->decryptor_ctx, out->buffer + out->len, &len_written, input.ptr, (int)input.len)) {
85 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
86 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
87 }
88
89 out->len += len_written;
90 return AWS_OP_SUCCESS;
91 }
92
93 static int s_finalize_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
94 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
95
96 size_t required_buffer_space = cipher->block_size;
97
98 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
99 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
100 }
101
102 int len_written = (int)out->capacity - out->len;
103 if (!EVP_DecryptFinal_ex(openssl_cipher->decryptor_ctx, out->buffer + out->len, &len_written)) {
104 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
105 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
106 }
107
108 out->len += len_written;
109 return AWS_OP_SUCCESS;
110 }
111
112 static void s_destroy(struct aws_symmetric_cipher *cipher) {
113 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
114
115 if (openssl_cipher->encryptor_ctx) {
116 EVP_CIPHER_CTX_free(openssl_cipher->encryptor_ctx);
117 }
118
119 if (openssl_cipher->decryptor_ctx) {
120 EVP_CIPHER_CTX_free(openssl_cipher->decryptor_ctx);
121 }
122
123 aws_byte_buf_clean_up_secure(&cipher->key);
124 aws_byte_buf_clean_up_secure(&cipher->iv);
125
126 aws_byte_buf_clean_up_secure(&cipher->tag);
127
128 if (cipher->aad.buffer) {
129 aws_byte_buf_clean_up_secure(&cipher->aad);
130 }
131
132 aws_byte_buf_clean_up_secure(&openssl_cipher->working_buffer);
133
134 aws_mem_release(cipher->allocator, openssl_cipher);
135 }
136
137 static int s_clear_reusable_state(struct aws_symmetric_cipher *cipher) {
138 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
139
140 EVP_CIPHER_CTX_cleanup(openssl_cipher->encryptor_ctx);
141 EVP_CIPHER_CTX_cleanup(openssl_cipher->decryptor_ctx);
142 aws_byte_buf_secure_zero(&openssl_cipher->working_buffer);
143 cipher->state = AWS_SYMMETRIC_CIPHER_READY;
144 return AWS_OP_SUCCESS;
145 }
146
147 static int s_init_cbc_cipher_materials(struct aws_symmetric_cipher *cipher) {
148 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
149
150 if (!EVP_EncryptInit_ex(
151 openssl_cipher->encryptor_ctx,
152 EVP_aes_256_cbc(),
153 NULL,
154 openssl_cipher->cipher_base.key.buffer,
155 openssl_cipher->cipher_base.iv.buffer) ||
156 !EVP_DecryptInit_ex(
157 openssl_cipher->decryptor_ctx,
158 EVP_aes_256_cbc(),
159 NULL,
160 openssl_cipher->cipher_base.key.buffer,
161 openssl_cipher->cipher_base.iv.buffer)) {
162 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
163 }
164
165 return AWS_OP_SUCCESS;
166 }
167
168 static int s_reset_cbc_cipher_materials(struct aws_symmetric_cipher *cipher) {
169 int ret_val = s_clear_reusable_state(cipher);
170
171 if (ret_val == AWS_OP_SUCCESS) {
172 return s_init_cbc_cipher_materials(cipher);
173 }
174
175 return ret_val;
176 }
177
178 static struct aws_symmetric_cipher_vtable s_cbc_vtable = {
179 .alg_name = "AES-CBC 256",
180 .provider = "OpenSSL Compatible LibCrypto",
181 .destroy = s_destroy,
182 .reset = s_reset_cbc_cipher_materials,
183 .decrypt = s_decrypt,
184 .encrypt = s_encrypt,
185 .finalize_decryption = s_finalize_decryption,
186 .finalize_encryption = s_finalize_encryption,
187 };
188
189 struct aws_symmetric_cipher *aws_aes_cbc_256_new_impl(
190 struct aws_allocator *allocator,
191 const struct aws_byte_cursor *key,
192 const struct aws_byte_cursor *iv) {
193 struct openssl_aes_cipher *cipher = aws_mem_calloc(allocator, 1, sizeof(struct openssl_aes_cipher));
194
195 cipher->cipher_base.allocator = allocator;
196 cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
197 cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
198 cipher->cipher_base.vtable = &s_cbc_vtable;
199 cipher->cipher_base.impl = cipher;
200
201 if (key) {
202 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.key, allocator, *key);
203 } else {
204 aws_byte_buf_init(&cipher->cipher_base.key, allocator, AWS_AES_256_KEY_BYTE_LEN);
205 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cipher->cipher_base.key);
206 }
207
208 if (iv) {
209 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.iv, allocator, *iv);
210 } else {
211 aws_byte_buf_init(&cipher->cipher_base.iv, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
212 aws_symmetric_cipher_generate_initialization_vector(
213 AWS_AES_256_CIPHER_BLOCK_SIZE, false, &cipher->cipher_base.iv);
214 }
215
216 /* EVP_CIPHER_CTX_init() will be called inside EVP_CIPHER_CTX_new(). */
217 cipher->encryptor_ctx = EVP_CIPHER_CTX_new();
218 AWS_FATAL_ASSERT(cipher->encryptor_ctx && "Cipher initialization failed!");
219
220 /* EVP_CIPHER_CTX_init() will be called inside EVP_CIPHER_CTX_new(). */
221 cipher->decryptor_ctx = EVP_CIPHER_CTX_new();
222 AWS_FATAL_ASSERT(cipher->decryptor_ctx && "Cipher initialization failed!");
223
224 if (s_init_cbc_cipher_materials(&cipher->cipher_base) != AWS_OP_SUCCESS) {
225 goto error;
226 }
227
228 cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
229 return &cipher->cipher_base;
230
231 error:
232 s_destroy(&cipher->cipher_base);
233 return NULL;
234 }
235
236 static int s_init_ctr_cipher_materials(struct aws_symmetric_cipher *cipher) {
237 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
238
239 if (!(EVP_EncryptInit_ex(
240 openssl_cipher->encryptor_ctx,
241 EVP_aes_256_ctr(),
242 NULL,
243 openssl_cipher->cipher_base.key.buffer,
244 openssl_cipher->cipher_base.iv.buffer) &&
245 EVP_CIPHER_CTX_set_padding(openssl_cipher->encryptor_ctx, 0)) ||
246 !(EVP_DecryptInit_ex(
247 openssl_cipher->decryptor_ctx,
248 EVP_aes_256_ctr(),
249 NULL,
250 openssl_cipher->cipher_base.key.buffer,
251 openssl_cipher->cipher_base.iv.buffer) &&
252 EVP_CIPHER_CTX_set_padding(openssl_cipher->decryptor_ctx, 0))) {
253 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
254 }
255
256 return AWS_OP_SUCCESS;
257 }
258
259 static int s_reset_ctr_cipher_materials(struct aws_symmetric_cipher *cipher) {
260 int ret_val = s_clear_reusable_state(cipher);
261
262 if (ret_val == AWS_OP_SUCCESS) {
263 return s_init_ctr_cipher_materials(cipher);
264 }
265
266 return ret_val;
267 }
268
269 static struct aws_symmetric_cipher_vtable s_ctr_vtable = {
270 .alg_name = "AES-CTR 256",
271 .provider = "OpenSSL Compatible LibCrypto",
272 .destroy = s_destroy,
273 .reset = s_reset_ctr_cipher_materials,
274 .decrypt = s_decrypt,
275 .encrypt = s_encrypt,
276 .finalize_decryption = s_finalize_decryption,
277 .finalize_encryption = s_finalize_encryption,
278 };
279
280 struct aws_symmetric_cipher *aws_aes_ctr_256_new_impl(
281 struct aws_allocator *allocator,
282 const struct aws_byte_cursor *key,
283 const struct aws_byte_cursor *iv) {
284 struct openssl_aes_cipher *cipher = aws_mem_calloc(allocator, 1, sizeof(struct openssl_aes_cipher));
285
286 cipher->cipher_base.allocator = allocator;
287 cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
288 cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
289 cipher->cipher_base.vtable = &s_ctr_vtable;
290 cipher->cipher_base.impl = cipher;
291
292 if (key) {
293 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.key, allocator, *key);
294 } else {
295 aws_byte_buf_init(&cipher->cipher_base.key, allocator, AWS_AES_256_KEY_BYTE_LEN);
296 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cipher->cipher_base.key);
297 }
298
299 if (iv) {
300 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.iv, allocator, *iv);
301 } else {
302 aws_byte_buf_init(&cipher->cipher_base.iv, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
303 aws_symmetric_cipher_generate_initialization_vector(
304 AWS_AES_256_CIPHER_BLOCK_SIZE, true, &cipher->cipher_base.iv);
305 }
306
307 /* EVP_CIPHER_CTX_init() will be called inside EVP_CIPHER_CTX_new(). */
308 cipher->encryptor_ctx = EVP_CIPHER_CTX_new();
309 AWS_FATAL_ASSERT(cipher->encryptor_ctx && "Cipher initialization failed!");
310
311 /* EVP_CIPHER_CTX_init() will be called inside EVP_CIPHER_CTX_new(). */
312 cipher->decryptor_ctx = EVP_CIPHER_CTX_new();
313 AWS_FATAL_ASSERT(cipher->decryptor_ctx && "Cipher initialization failed!");
314
315 if (s_init_ctr_cipher_materials(&cipher->cipher_base) != AWS_OP_SUCCESS) {
316 goto error;
317 }
318
319 cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
320 return &cipher->cipher_base;
321
322 error:
323 s_destroy(&cipher->cipher_base);
324 return NULL;
325 }
326
327 static int s_gcm_decrypt(struct aws_symmetric_cipher *cipher, struct aws_byte_cursor input, struct aws_byte_buf *out) {
328 if (cipher->tag.buffer == NULL) {
329 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
330 }
331
332 return s_decrypt(cipher, input, out);
333 }
334
335 static int s_finalize_gcm_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
336 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
337
338 if (cipher->tag.buffer == NULL) {
339 aws_byte_buf_init(&cipher->tag, cipher->allocator, AWS_AES_256_CIPHER_BLOCK_SIZE);
340 }
341
342 int ret_val = s_finalize_encryption(cipher, out);
343
344 if (ret_val == AWS_OP_SUCCESS) {
345 if (!EVP_CIPHER_CTX_ctrl(
346 openssl_cipher->encryptor_ctx, EVP_CTRL_GCM_GET_TAG, (int)cipher->tag.capacity, cipher->tag.buffer)) {
347 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
348 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
349 }
350 cipher->tag.len = AWS_AES_256_CIPHER_BLOCK_SIZE;
351 }
352
353 return ret_val;
354 }
355
356 static int s_finalize_gcm_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
357 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
358
359 if (openssl_cipher->cipher_base.tag.len) {
360 if (!EVP_CIPHER_CTX_ctrl(
361 openssl_cipher->decryptor_ctx,
362 EVP_CTRL_GCM_SET_TAG,
363 (int)openssl_cipher->cipher_base.tag.len,
364 openssl_cipher->cipher_base.tag.buffer)) {
365 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
366 }
367 }
368
369 return s_finalize_decryption(cipher, out);
370 }
371
372 static int s_init_gcm_cipher_materials(struct aws_symmetric_cipher *cipher) {
373 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
374
375 if (!(EVP_EncryptInit_ex(openssl_cipher->encryptor_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) &&
376 EVP_EncryptInit_ex(
377 openssl_cipher->encryptor_ctx,
378 NULL,
379 NULL,
380 openssl_cipher->cipher_base.key.buffer,
381 openssl_cipher->cipher_base.iv.buffer) &&
382 EVP_CIPHER_CTX_set_padding(openssl_cipher->encryptor_ctx, 0)) ||
383 !(EVP_DecryptInit_ex(openssl_cipher->decryptor_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) &&
384 EVP_DecryptInit_ex(
385 openssl_cipher->decryptor_ctx,
386 NULL,
387 NULL,
388 openssl_cipher->cipher_base.key.buffer,
389 openssl_cipher->cipher_base.iv.buffer) &&
390 EVP_CIPHER_CTX_set_padding(openssl_cipher->decryptor_ctx, 0))) {
391 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
392 }
393
394 if (openssl_cipher->cipher_base.aad.len) {
395 int outLen = 0;
396 if (!EVP_EncryptUpdate(
397 openssl_cipher->encryptor_ctx,
398 NULL,
399 &outLen,
400 openssl_cipher->cipher_base.aad.buffer,
401 (int)openssl_cipher->cipher_base.aad.len) ||
402 !EVP_DecryptUpdate(
403 openssl_cipher->decryptor_ctx,
404 NULL,
405 &outLen,
406 openssl_cipher->cipher_base.aad.buffer,
407 (int)openssl_cipher->cipher_base.aad.len)) {
408 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
409 }
410 }
411
412 aws_byte_buf_clean_up_secure(&openssl_cipher->cipher_base.tag);
413
414 return AWS_OP_SUCCESS;
415 }
416
417 static int s_reset_gcm_cipher_materials(struct aws_symmetric_cipher *cipher) {
418 int ret_val = s_clear_reusable_state(cipher);
419
420 if (ret_val == AWS_OP_SUCCESS) {
421 return s_init_gcm_cipher_materials(cipher);
422 }
423
424 return ret_val;
425 }
426
427 static struct aws_symmetric_cipher_vtable s_gcm_vtable = {
428 .alg_name = "AES-GCM 256",
429 .provider = "OpenSSL Compatible LibCrypto",
430 .destroy = s_destroy,
431 .reset = s_reset_gcm_cipher_materials,
432 .decrypt = s_gcm_decrypt,
433 .encrypt = s_encrypt,
434 .finalize_decryption = s_finalize_gcm_decryption,
435 .finalize_encryption = s_finalize_gcm_encryption,
436 };
437
438 struct aws_symmetric_cipher *aws_aes_gcm_256_new_impl(
439 struct aws_allocator *allocator,
440 const struct aws_byte_cursor *key,
441 const struct aws_byte_cursor *iv,
442 const struct aws_byte_cursor *aad) {
443
444 struct openssl_aes_cipher *cipher = aws_mem_calloc(allocator, 1, sizeof(struct openssl_aes_cipher));
445 cipher->cipher_base.allocator = allocator;
446 cipher->cipher_base.block_size = AWS_AES_256_CIPHER_BLOCK_SIZE;
447 cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
448 cipher->cipher_base.vtable = &s_gcm_vtable;
449 cipher->cipher_base.impl = cipher;
450
451 /* Copy key into the cipher context. */
452 if (key) {
453 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.key, allocator, *key);
454 } else {
455 aws_byte_buf_init(&cipher->cipher_base.key, allocator, AWS_AES_256_KEY_BYTE_LEN);
456 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cipher->cipher_base.key);
457 }
458
459 /* Copy initialization vector into the cipher context. */
460 if (iv) {
461 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.iv, allocator, *iv);
462 } else {
463 aws_byte_buf_init(&cipher->cipher_base.iv, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE - 4);
464 aws_symmetric_cipher_generate_initialization_vector(
465 AWS_AES_256_CIPHER_BLOCK_SIZE - 4, false, &cipher->cipher_base.iv);
466 }
467
468 /* Initialize the cipher contexts. */
469 cipher->encryptor_ctx = EVP_CIPHER_CTX_new();
470 AWS_FATAL_ASSERT(cipher->encryptor_ctx && "Encryptor cipher initialization failed!");
471
472 cipher->decryptor_ctx = EVP_CIPHER_CTX_new();
473 AWS_FATAL_ASSERT(cipher->decryptor_ctx && "Decryptor cipher initialization failed!");
474
475 /* Set AAD if provided */
476 if (aad) {
477 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.aad, allocator, *aad);
478 }
479
480 /* Initialize the cipher contexts with the specified key and IV. */
481 if (s_init_gcm_cipher_materials(&cipher->cipher_base)) {
482 goto error;
483 }
484
485 cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
486 return &cipher->cipher_base;
487
488 error:
489 s_destroy(&cipher->cipher_base);
490 return NULL;
491 }
492
493 static int s_key_wrap_encrypt_decrypt(
494 struct aws_symmetric_cipher *cipher,
495 struct aws_byte_cursor input,
496 struct aws_byte_buf *out) {
497 (void)out;
498 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
499
500 return aws_byte_buf_append_dynamic(&openssl_cipher->working_buffer, &input);
501 }
502
503 static const size_t MIN_CEK_LENGTH_BYTES = 128 / 8;
504 static const unsigned char INTEGRITY_VALUE = 0xA6;
505 #define KEYWRAP_BLOCK_SIZE 8u
506
507 static int s_key_wrap_finalize_encryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
508 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
509
510 if (openssl_cipher->working_buffer.len < MIN_CEK_LENGTH_BYTES) {
511 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
512 return aws_raise_error(AWS_ERROR_INVALID_STATE);
513 }
514
515 /* the following is an in place implementation of
516 RFC 3394 using the alternate in-place implementation.
517 we use one in-place buffer instead of the copy at the end.
518 the one letter variable names are meant to directly reflect the variables in the RFC */
519 size_t required_buffer_space = openssl_cipher->working_buffer.len + cipher->block_size;
520 size_t starting_len_offset = out->len;
521
522 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
523 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
524 }
525
526 /* put the integrity check register in the first 8 bytes of the final buffer. */
527 aws_byte_buf_write_u8_n(out, INTEGRITY_VALUE, KEYWRAP_BLOCK_SIZE);
528 uint8_t *a = out->buffer + starting_len_offset;
529
530 struct aws_byte_cursor working_buf_cur = aws_byte_cursor_from_buf(&openssl_cipher->working_buffer);
531 aws_byte_buf_write_from_whole_cursor(out, working_buf_cur);
532
533 /* put the register buffer after the integrity check register */
534 uint8_t *r = out->buffer + starting_len_offset + KEYWRAP_BLOCK_SIZE;
535
536 int n = (int)(openssl_cipher->working_buffer.len / KEYWRAP_BLOCK_SIZE);
537
538 uint8_t b_buf[KEYWRAP_BLOCK_SIZE * 2] = {0};
539 struct aws_byte_buf b = aws_byte_buf_from_empty_array(b_buf, sizeof(b_buf));
540 int b_out_len = b.capacity;
541
542 uint8_t temp_buf[KEYWRAP_BLOCK_SIZE * 2] = {0};
543 struct aws_byte_buf temp_input = aws_byte_buf_from_empty_array(temp_buf, sizeof(temp_buf));
544
545 for (int j = 0; j <= 5; ++j) {
546 for (int i = 1; i <= n; ++i) {
547 /* concat A and R[i], A should be most significant and then R[i] should be least significant. */
548 memcpy(temp_input.buffer, a, KEYWRAP_BLOCK_SIZE);
549 memcpy(temp_input.buffer + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
550
551 /* encrypt the concatenated A and R[I] and store it in B */
552 if (!EVP_EncryptUpdate(
553 openssl_cipher->encryptor_ctx, b.buffer, &b_out_len, temp_input.buffer, (int)temp_input.capacity)) {
554 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
555 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
556 }
557
558 unsigned char t = (unsigned char)((n * j) + i);
559 /* put the 64 MSB ^ T into A */
560 memcpy(a, b.buffer, KEYWRAP_BLOCK_SIZE);
561 a[7] ^= t;
562
563 /* put the 64 LSB into R[i] */
564 memcpy(r, b.buffer + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
565 /* increment i -> R[i] */
566 r += KEYWRAP_BLOCK_SIZE;
567 }
568 /* reset R */
569 r = out->buffer + starting_len_offset + KEYWRAP_BLOCK_SIZE;
570 }
571
572 return AWS_OP_SUCCESS;
573 }
574
575 static int s_key_wrap_finalize_decryption(struct aws_symmetric_cipher *cipher, struct aws_byte_buf *out) {
576 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
577
578 if (openssl_cipher->working_buffer.len < MIN_CEK_LENGTH_BYTES + KEYWRAP_BLOCK_SIZE) {
579 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
580 return aws_raise_error(AWS_ERROR_INVALID_STATE);
581 }
582
583 /* the following is an in place implementation of
584 RFC 3394 using the alternate in-place implementation.
585 we use one in-place buffer instead of the copy at the end.
586 the one letter variable names are meant to directly reflect the variables in the RFC */
587 size_t required_buffer_space = openssl_cipher->working_buffer.len - KEYWRAP_BLOCK_SIZE;
588 size_t starting_len_offset = out->len;
589
590 if (aws_symmetric_cipher_try_ensure_sufficient_buffer_space(out, required_buffer_space)) {
591 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
592 }
593
594 memcpy(
595 out->buffer + starting_len_offset,
596 openssl_cipher->working_buffer.buffer + KEYWRAP_BLOCK_SIZE,
597 required_buffer_space);
598
599 /* integrity register should be the first 8 bytes of the final buffer. */
600 uint8_t *a = openssl_cipher->working_buffer.buffer;
601
602 /* in-place register is the plaintext. For decryption, start at the last array position (8 bytes before the end); */
603 uint8_t *r = out->buffer + starting_len_offset + required_buffer_space - KEYWRAP_BLOCK_SIZE;
604
605 int n = (int)(required_buffer_space / KEYWRAP_BLOCK_SIZE);
606
607 uint8_t b_buf[KEYWRAP_BLOCK_SIZE * 10] = {0};
608 struct aws_byte_buf b = aws_byte_buf_from_empty_array(b_buf, sizeof(b_buf));
609 int b_out_len = b.capacity;
610
611 uint8_t temp_buf[KEYWRAP_BLOCK_SIZE * 2] = {0};
612 struct aws_byte_buf temp_input = aws_byte_buf_from_empty_array(temp_buf, sizeof(temp_buf));
613
614 for (int j = 5; j >= 0; --j) {
615 for (int i = n; i >= 1; --i) {
616 /* concat A and T */
617 memcpy(temp_input.buffer, a, KEYWRAP_BLOCK_SIZE);
618 unsigned char t = (unsigned char)((n * j) + i);
619 temp_input.buffer[7] ^= t;
620 /* R[i] */
621 memcpy(temp_input.buffer + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
622
623 /* Decrypt the concatenated buffer */
624 if (!EVP_DecryptUpdate(
625 openssl_cipher->decryptor_ctx, b.buffer, &b_out_len, temp_input.buffer, (int)temp_input.capacity)) {
626 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
627 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
628 }
629
630 /* set A to 64 MSB of decrypted result */
631 memcpy(a, b.buffer, KEYWRAP_BLOCK_SIZE);
632 /* set the R[i] to the 64 LSB of decrypted result */
633 memcpy(r, b.buffer + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
634 /* decrement i -> R[i] */
635 r -= KEYWRAP_BLOCK_SIZE;
636 }
637 /* reset R */
638 r = out->buffer + starting_len_offset + required_buffer_space - KEYWRAP_BLOCK_SIZE;
639 }
640
641 /* here we perform the integrity check to make sure A == 0xA6A6A6A6A6A6A6A6 */
642 for (size_t i = 0; i < KEYWRAP_BLOCK_SIZE; ++i) {
643 if (a[i] != INTEGRITY_VALUE) {
644 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
645 return aws_raise_error(AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED);
646 }
647 }
648
649 out->len += required_buffer_space;
650 return AWS_OP_SUCCESS;
651 }
652
653 static int s_init_keywrap_cipher_materials(struct aws_symmetric_cipher *cipher) {
654 struct openssl_aes_cipher *openssl_cipher = cipher->impl;
655
656 if (!(EVP_EncryptInit_ex(openssl_cipher->encryptor_ctx, EVP_aes_256_ecb(), NULL, cipher->key.buffer, NULL) &&
657 EVP_CIPHER_CTX_set_padding(openssl_cipher->encryptor_ctx, 0)) ||
658 !(EVP_DecryptInit_ex(openssl_cipher->decryptor_ctx, EVP_aes_256_ecb(), NULL, cipher->key.buffer, NULL) &&
659 EVP_CIPHER_CTX_set_padding(openssl_cipher->decryptor_ctx, 0))) {
660 cipher->state = AWS_SYMMETRIC_CIPHER_ERROR;
661 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
662 }
663
664 return AWS_OP_SUCCESS;
665 }
666
667 static int s_reset_keywrap_cipher_materials(struct aws_symmetric_cipher *cipher) {
668 int ret_val = s_clear_reusable_state(cipher);
669
670 if (ret_val == AWS_OP_SUCCESS) {
671 return s_init_keywrap_cipher_materials(cipher);
672 }
673
674 return ret_val;
675 }
676
677 static struct aws_symmetric_cipher_vtable s_keywrap_vtable = {
678 .alg_name = "AES-KEYWRAP 256",
679 .provider = "OpenSSL Compatible LibCrypto",
680 .destroy = s_destroy,
681 .reset = s_reset_keywrap_cipher_materials,
682 .decrypt = s_key_wrap_encrypt_decrypt,
683 .encrypt = s_key_wrap_encrypt_decrypt,
684 .finalize_decryption = s_key_wrap_finalize_decryption,
685 .finalize_encryption = s_key_wrap_finalize_encryption,
686 };
687
688 struct aws_symmetric_cipher *aws_aes_keywrap_256_new_impl(
689 struct aws_allocator *allocator,
690 const struct aws_byte_cursor *key) {
691 struct openssl_aes_cipher *cipher = aws_mem_calloc(allocator, 1, sizeof(struct openssl_aes_cipher));
692 cipher->cipher_base.allocator = allocator;
693 cipher->cipher_base.block_size = KEYWRAP_BLOCK_SIZE;
694 cipher->cipher_base.key_length_bits = AWS_AES_256_KEY_BIT_LEN;
695 cipher->cipher_base.vtable = &s_keywrap_vtable;
696 cipher->cipher_base.impl = cipher;
697
698 /* Copy key into the cipher context. */
699 if (key) {
700 aws_byte_buf_init_copy_from_cursor(&cipher->cipher_base.key, allocator, *key);
701 } else {
702 aws_byte_buf_init(&cipher->cipher_base.key, allocator, AWS_AES_256_KEY_BYTE_LEN);
703 aws_symmetric_cipher_generate_key(AWS_AES_256_KEY_BYTE_LEN, &cipher->cipher_base.key);
704 }
705
706 aws_byte_buf_init(&cipher->working_buffer, allocator, KEYWRAP_BLOCK_SIZE);
707
708 /* Initialize the cipher contexts. */
709 cipher->encryptor_ctx = EVP_CIPHER_CTX_new();
710 AWS_FATAL_ASSERT(cipher->encryptor_ctx && "Encryptor cipher initialization failed!");
711
712 cipher->decryptor_ctx = EVP_CIPHER_CTX_new();
713 AWS_FATAL_ASSERT(cipher->decryptor_ctx && "Decryptor cipher initialization failed!");
714
715 /* Initialize the cipher contexts with the specified key and IV. */
716 if (s_init_keywrap_cipher_materials(&cipher->cipher_base)) {
717 goto error;
718 }
719
720 cipher->cipher_base.state = AWS_SYMMETRIC_CIPHER_READY;
721 return &cipher->cipher_base;
722
723 error:
724 s_destroy(&cipher->cipher_base);
725 return NULL;
726 }