Skip to content
0.2.0-dev Download ZIP
1126 lines ·37.6 KB Raw
1/**
2 * \file cipher.c
3 *
4 * \brief Generic cipher wrapper for Mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 */
11
12#include "tf_psa_crypto_common.h"
13
14#if defined(MBEDTLS_CIPHER_C)
15
16#include "mbedtls/private/cipher.h"
17#include "cipher_invasive.h"
18#include "cipher_wrap.h"
19#include "mbedtls/platform_util.h"
20#include "mbedtls/private/error_common.h"
21#include "mbedtls/constant_time.h"
22#include "constant_time_internal.h"
23
24#include <stdlib.h>
25#include <string.h>
26
27#if defined(MBEDTLS_CHACHAPOLY_C)
28#include "mbedtls/private/chachapoly.h"
29#endif
30
31#if defined(MBEDTLS_GCM_C)
32#include "mbedtls/private/gcm.h"
33#endif
34
35#if defined(MBEDTLS_CCM_C)
36#include "mbedtls/private/ccm.h"
37#endif
38
39#if defined(MBEDTLS_CHACHA20_C)
40#include "mbedtls/private/chacha20.h"
41#endif
42
43#if defined(MBEDTLS_CMAC_C)
44#include "mbedtls/private/cmac.h"
45#endif
46
47#include "mbedtls/platform.h"
48
49static int supported_init = 0;
50
51static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
52 const mbedtls_cipher_info_t *info)
53{
54 return mbedtls_cipher_base_lookup_table[info->base_idx];
55}
56
57const int *mbedtls_cipher_list(void)
58{
59 const mbedtls_cipher_definition_t *def;
60 int *type;
61
62 if (!supported_init) {
63 def = mbedtls_cipher_definitions;
64 type = mbedtls_cipher_supported;
65
66 while (def->type != 0) {
67 *type++ = (*def++).type;
68 }
69
70 *type = 0;
71
72 supported_init = 1;
73 }
74
75 return mbedtls_cipher_supported;
76}
77
78const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
79 const mbedtls_cipher_type_t cipher_type)
80{
81 const mbedtls_cipher_definition_t *def;
82
83 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
84 if (def->type == cipher_type) {
85 return def->info;
86 }
87 }
88
89 return NULL;
90}
91
92const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
93 const char *cipher_name)
94{
95 const mbedtls_cipher_definition_t *def;
96
97 if (NULL == cipher_name) {
98 return NULL;
99 }
100
101 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
102 if (!strcmp(def->info->name, cipher_name)) {
103 return def->info;
104 }
105 }
106
107 return NULL;
108}
109
110const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
111 const mbedtls_cipher_id_t cipher_id,
112 int key_bitlen,
113 const mbedtls_cipher_mode_t mode)
114{
115 const mbedtls_cipher_definition_t *def;
116
117 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
118 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
119 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
120 def->info->mode == mode) {
121 return def->info;
122 }
123 }
124
125 return NULL;
126}
127
128void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
129{
130 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
131}
132
133void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
134{
135 if (ctx == NULL) {
136 return;
137 }
138
139#if defined(MBEDTLS_CMAC_C)
140 if (ctx->cmac_ctx) {
141 mbedtls_zeroize_and_free(ctx->cmac_ctx,
142 sizeof(mbedtls_cmac_context_t));
143 }
144#endif
145
146 if (ctx->cipher_ctx) {
147 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
148 }
149
150 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
151}
152
153int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
154 const mbedtls_cipher_info_t *cipher_info)
155{
156 if (cipher_info == NULL) {
157 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
158 }
159
160 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
161
162 if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
163 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
164 if (ctx->cipher_ctx == NULL) {
165 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
166 }
167 }
168
169 ctx->cipher_info = cipher_info;
170
171 return 0;
172}
173
174int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
175 const unsigned char *key,
176 int key_bitlen,
177 const mbedtls_operation_t operation)
178{
179 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
180 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
181 }
182 if (ctx->cipher_info == NULL) {
183 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
184 }
185#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
186 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
187 MBEDTLS_DECRYPT == operation) {
188 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
189 }
190#endif
191
192 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
193 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
194 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
195 }
196
197 ctx->key_bitlen = key_bitlen;
198 ctx->operation = operation;
199
200#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
201 /*
202 * For OFB, CFB and CTR mode always use the encryption key schedule
203 */
204 if (MBEDTLS_ENCRYPT == operation ||
205 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
206 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
207 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
208 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
209 ctx->key_bitlen);
210 }
211
212 if (MBEDTLS_DECRYPT == operation) {
213 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
214 ctx->key_bitlen);
215 }
216#else
217 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
218 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
219 ctx->key_bitlen);
220 }
221#endif
222
223 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
224}
225
226int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
227 const unsigned char *iv,
228 size_t iv_len)
229{
230 size_t actual_iv_size;
231
232 if (ctx->cipher_info == NULL) {
233 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
234 }
235
236 /* avoid buffer overflow in ctx->iv */
237 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
238 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
239 }
240
241 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
242 actual_iv_size = iv_len;
243 } else {
244 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
245
246 /* avoid reading past the end of input buffer */
247 if (actual_iv_size > iv_len) {
248 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
249 }
250 }
251
252#if defined(MBEDTLS_CHACHA20_C)
253 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
254 /* Even though the actual_iv_size is overwritten with a correct value
255 * of 12 from the cipher info, return an error to indicate that
256 * the input iv_len is wrong. */
257 if (iv_len != 12) {
258 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
259 }
260
261 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
262 iv,
263 0U)) { /* Initial counter value */
264 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
265 }
266 }
267#if defined(MBEDTLS_CHACHAPOLY_C)
268 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
269 iv_len != 12) {
270 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
271 }
272#endif
273#endif
274
275#if defined(MBEDTLS_GCM_C)
276 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
277 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
278 ctx->operation,
279 iv, iv_len);
280 }
281#endif
282
283#if defined(MBEDTLS_CCM_C)
284 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
285 int set_lengths_result;
286 int ccm_star_mode;
287
288 set_lengths_result = mbedtls_ccm_set_lengths(
289 (mbedtls_ccm_context *) ctx->cipher_ctx,
290 0, 0, 0);
291 if (set_lengths_result != 0) {
292 return set_lengths_result;
293 }
294
295 if (ctx->operation == MBEDTLS_DECRYPT) {
296 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
297 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
298 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
299 } else {
300 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
301 }
302
303 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
304 ccm_star_mode,
305 iv, iv_len);
306 }
307#endif
308
309 if (actual_iv_size != 0) {
310 memcpy(ctx->iv, iv, actual_iv_size);
311 ctx->iv_size = actual_iv_size;
312 }
313
314 return 0;
315}
316
317int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
318{
319 if (ctx->cipher_info == NULL) {
320 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
321 }
322
323 ctx->unprocessed_len = 0;
324
325 return 0;
326}
327
328#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
329int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
330 const unsigned char *ad, size_t ad_len)
331{
332 if (ctx->cipher_info == NULL) {
333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
334 }
335
336#if defined(MBEDTLS_GCM_C)
337 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
338 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
339 ad, ad_len);
340 }
341#endif
342
343#if defined(MBEDTLS_CHACHAPOLY_C)
344 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
345 int result;
346 mbedtls_chachapoly_mode_t mode;
347
348 mode = (ctx->operation == MBEDTLS_ENCRYPT)
349 ? MBEDTLS_CHACHAPOLY_ENCRYPT
350 : MBEDTLS_CHACHAPOLY_DECRYPT;
351
352 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
353 ctx->iv,
354 mode);
355 if (result != 0) {
356 return result;
357 }
358
359 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
360 ad, ad_len);
361 }
362#endif
363
364 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
365}
366#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
367
368int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
369 size_t ilen, unsigned char *output, size_t *olen)
370{
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
372 size_t block_size;
373
374 if (ctx->cipher_info == NULL) {
375 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
376 }
377
378 *olen = 0;
379 block_size = mbedtls_cipher_get_block_size(ctx);
380 if (0 == block_size) {
381 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
382 }
383
384 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
385 if (ilen != block_size) {
386 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
387 }
388
389 *olen = ilen;
390
391 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
392 ctx->operation, input,
393 output))) {
394 return ret;
395 }
396
397 return 0;
398 }
399
400#if defined(MBEDTLS_GCM_C)
401 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
402 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
403 input, ilen,
404 output, ilen, olen);
405 }
406#endif
407
408#if defined(MBEDTLS_CCM_C)
409 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
410 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
411 input, ilen,
412 output, ilen, olen);
413 }
414#endif
415
416#if defined(MBEDTLS_CHACHAPOLY_C)
417 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
418 *olen = ilen;
419 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
420 ilen, input, output);
421 }
422#endif
423
424 if (input == output &&
425 (ctx->unprocessed_len != 0 || ilen % block_size)) {
426 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
427 }
428
429#if defined(MBEDTLS_CIPHER_MODE_CBC)
430 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
431 size_t copy_len = 0;
432
433 /*
434 * If there is not enough data for a full block, cache it.
435 */
436 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
437 ilen <= block_size - ctx->unprocessed_len) ||
438 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
439 ilen < block_size - ctx->unprocessed_len) ||
440 (ctx->operation == MBEDTLS_ENCRYPT &&
441 ilen < block_size - ctx->unprocessed_len)) {
442 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
443 ilen);
444
445 ctx->unprocessed_len += ilen;
446 return 0;
447 }
448
449 /*
450 * Process cached data first
451 */
452 if (0 != ctx->unprocessed_len) {
453 copy_len = block_size - ctx->unprocessed_len;
454
455 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
456 copy_len);
457
458 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
459 ctx->operation,
460 block_size, ctx->iv,
461 ctx->
462 unprocessed_data,
463 output))) {
464 return ret;
465 }
466
467 *olen += block_size;
468 output += block_size;
469 ctx->unprocessed_len = 0;
470
471 input += copy_len;
472 ilen -= copy_len;
473 }
474
475 /*
476 * Cache final, incomplete block
477 */
478 if (0 != ilen) {
479 /* Encryption: only cache partial blocks
480 * Decryption w/ padding: always keep at least one whole block
481 * Decryption w/o padding: only cache partial blocks
482 */
483 copy_len = ilen % block_size;
484 if (copy_len == 0 &&
485 ctx->operation == MBEDTLS_DECRYPT &&
486 NULL != ctx->add_padding) {
487 copy_len = block_size;
488 }
489
490 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
491 copy_len);
492
493 ctx->unprocessed_len += copy_len;
494 ilen -= copy_len;
495 }
496
497 /*
498 * Process remaining full blocks
499 */
500 if (ilen) {
501 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
502 ctx->operation,
503 ilen, ctx->iv,
504 input,
505 output))) {
506 return ret;
507 }
508
509 *olen += ilen;
510 }
511
512 return 0;
513 }
514#endif /* MBEDTLS_CIPHER_MODE_CBC */
515
516#if defined(MBEDTLS_CIPHER_MODE_CFB)
517 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
518 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
519 ctx->operation, ilen,
520 &ctx->unprocessed_len,
521 ctx->iv,
522 input, output))) {
523 return ret;
524 }
525
526 *olen = ilen;
527
528 return 0;
529 }
530#endif /* MBEDTLS_CIPHER_MODE_CFB */
531
532#if defined(MBEDTLS_CIPHER_MODE_OFB)
533 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
534 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
535 ilen,
536 &ctx->unprocessed_len,
537 ctx->iv,
538 input, output))) {
539 return ret;
540 }
541
542 *olen = ilen;
543
544 return 0;
545 }
546#endif /* MBEDTLS_CIPHER_MODE_OFB */
547
548#if defined(MBEDTLS_CIPHER_MODE_CTR)
549 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
550 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
551 ilen,
552 &ctx->unprocessed_len,
553 ctx->iv,
554 ctx->unprocessed_data,
555 input, output))) {
556 return ret;
557 }
558
559 *olen = ilen;
560
561 return 0;
562 }
563#endif /* MBEDTLS_CIPHER_MODE_CTR */
564
565#if defined(MBEDTLS_CIPHER_MODE_XTS)
566 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
567 if (ctx->unprocessed_len > 0) {
568 /* We can only process an entire data unit at a time. */
569 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
570 }
571
572 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
573 ctx->operation,
574 ilen,
575 ctx->iv,
576 input,
577 output);
578 if (ret != 0) {
579 return ret;
580 }
581
582 *olen = ilen;
583
584 return 0;
585 }
586#endif /* MBEDTLS_CIPHER_MODE_XTS */
587
588#if defined(MBEDTLS_CIPHER_MODE_STREAM)
589 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
590 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
591 ilen, input,
592 output))) {
593 return ret;
594 }
595
596 *olen = ilen;
597
598 return 0;
599 }
600#endif /* MBEDTLS_CIPHER_MODE_STREAM */
601
602 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
603}
604
605#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
606#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
607/*
608 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
609 */
610static void add_pkcs_padding(unsigned char *output, size_t output_len,
611 size_t data_len)
612{
613 size_t padding_len = output_len - data_len;
614 unsigned char i;
615
616 for (i = 0; i < padding_len; i++) {
617 output[data_len + i] = (unsigned char) padding_len;
618 }
619}
620
621/*
622 * Get the length of the PKCS7 padding.
623 *
624 * Note: input_len must be the block size of the cipher.
625 */
626MBEDTLS_STATIC_TESTABLE int mbedtls_get_pkcs_padding(unsigned char *input,
627 size_t input_len,
628 size_t *data_len,
629 size_t *invalid_padding)
630{
631 size_t i, pad_idx;
632 unsigned char padding_len;
633
634 if (NULL == input || NULL == data_len) {
635 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
636 }
637
638 padding_len = input[input_len - 1];
639
640 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
641 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
642
643 /* The number of bytes checked must be independent of padding_len,
644 * so pick input_len, which is usually 8 or 16 (one block) */
645 pad_idx = input_len - padding_len;
646 for (i = 0; i < input_len; i++) {
647 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
648 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
649 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
650 }
651
652 /* If the padding is invalid, set the output length to 0 */
653 *data_len = mbedtls_ct_if(bad, 0, input_len - padding_len);
654
655 *invalid_padding = mbedtls_ct_size_if_else_0(bad, SIZE_MAX);
656 return 0;
657}
658#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
659
660/*
661 * No padding: don't pad :)
662 *
663 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
664 * but a trivial get_padding function
665 */
666static int get_no_padding(unsigned char *input, size_t input_len,
667 size_t *data_len, size_t *invalid_padding)
668{
669 if (NULL == input || NULL == data_len) {
670 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
671 }
672
673 *data_len = input_len;
674 *invalid_padding = 0;
675 return 0;
676}
677#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
678
679int mbedtls_cipher_finish_padded(mbedtls_cipher_context_t *ctx,
680 unsigned char *output, size_t *olen,
681 size_t *invalid_padding)
682{
683 if (ctx->cipher_info == NULL) {
684 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
685 }
686
687 *olen = 0;
688 *invalid_padding = 0;
689
690#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
691 /* CBC mode requires padding so we make sure a call to
692 * mbedtls_cipher_set_padding_mode has been done successfully. */
693 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
694 if (ctx->get_padding == NULL) {
695 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
696 }
697 }
698#endif
699
700 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
701 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
702 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
703 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
704 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
705 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
706 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
707 return 0;
708 }
709
710 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
711 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
712 return 0;
713 }
714
715 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
716 if (ctx->unprocessed_len != 0) {
717 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
718 }
719
720 return 0;
721 }
722
723#if defined(MBEDTLS_CIPHER_MODE_CBC)
724 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
725 int ret = 0;
726
727 if (MBEDTLS_ENCRYPT == ctx->operation) {
728 /* check for 'no padding' mode */
729 if (NULL == ctx->add_padding) {
730 if (0 != ctx->unprocessed_len) {
731 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
732 }
733
734 return 0;
735 }
736
737 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
738 ctx->unprocessed_len);
739 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
740 /*
741 * For decrypt operations, expect a full block,
742 * or an empty block if no padding
743 */
744 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
745 return 0;
746 }
747
748 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
749 }
750
751 /* cipher block */
752 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
753 ctx->operation,
754 mbedtls_cipher_get_block_size(
755 ctx),
756 ctx->iv,
757 ctx->unprocessed_data,
758 output))) {
759 return ret;
760 }
761
762 /* Set output size for decryption */
763 if (MBEDTLS_DECRYPT == ctx->operation) {
764 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
765 olen, invalid_padding);
766 }
767
768 /* Set output size for encryption */
769 *olen = mbedtls_cipher_get_block_size(ctx);
770 return 0;
771 }
772#else
773 ((void) output);
774#endif /* MBEDTLS_CIPHER_MODE_CBC */
775
776 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
777}
778
779int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
780 unsigned char *output, size_t *olen)
781{
782 size_t invalid_padding = 0;
783 int ret = mbedtls_cipher_finish_padded(ctx, output, olen,
784 &invalid_padding);
785 if (ret == 0) {
786 ret = mbedtls_ct_error_if_else_0(invalid_padding,
787 MBEDTLS_ERR_CIPHER_INVALID_PADDING);
788 }
789 return ret;
790}
791
792#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
793int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
794 mbedtls_cipher_padding_t mode)
795{
796 if (NULL == ctx->cipher_info ||
797 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
798 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
799 }
800
801 switch (mode) {
802
803#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
804 case MBEDTLS_PADDING_PKCS7:
805 ctx->add_padding = add_pkcs_padding;
806 ctx->get_padding = mbedtls_get_pkcs_padding;
807 break;
808#endif
809 case MBEDTLS_PADDING_NONE:
810 ctx->add_padding = NULL;
811 ctx->get_padding = get_no_padding;
812 break;
813
814 default:
815 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
816 }
817
818 return 0;
819}
820#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
821
822#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
823int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
824 unsigned char *tag, size_t tag_len)
825{
826 if (ctx->cipher_info == NULL) {
827 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
828 }
829
830 if (MBEDTLS_ENCRYPT != ctx->operation) {
831 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
832 }
833
834#if defined(MBEDTLS_GCM_C)
835 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
836 size_t output_length;
837 /* The code here doesn't yet support alternative implementations
838 * that can delay up to a block of output. */
839 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
840 NULL, 0, &output_length,
841 tag, tag_len);
842 }
843#endif
844
845#if defined(MBEDTLS_CHACHAPOLY_C)
846 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
847 /* Don't allow truncated MAC for Poly1305 */
848 if (tag_len != 16U) {
849 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
850 }
851
852 return mbedtls_chachapoly_finish(
853 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
854 }
855#endif
856
857 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
858}
859
860int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
861 const unsigned char *tag, size_t tag_len)
862{
863 unsigned char check_tag[16];
864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
865
866 if (ctx->cipher_info == NULL) {
867 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
868 }
869
870 if (MBEDTLS_DECRYPT != ctx->operation) {
871 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
872 }
873
874 /* Status to return on a non-authenticated algorithm. */
875 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
876
877#if defined(MBEDTLS_GCM_C)
878 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
879 size_t output_length;
880 /* The code here doesn't yet support alternative implementations
881 * that can delay up to a block of output. */
882
883 if (tag_len > sizeof(check_tag)) {
884 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
885 }
886
887 if (0 != (ret = mbedtls_gcm_finish(
888 (mbedtls_gcm_context *) ctx->cipher_ctx,
889 NULL, 0, &output_length,
890 check_tag, tag_len))) {
891 return ret;
892 }
893
894 /* Check the tag in "constant-time" */
895 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
896 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
897 goto exit;
898 }
899 }
900#endif /* MBEDTLS_GCM_C */
901
902#if defined(MBEDTLS_CHACHAPOLY_C)
903 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
904 /* Don't allow truncated MAC for Poly1305 */
905 if (tag_len != sizeof(check_tag)) {
906 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
907 }
908
909 ret = mbedtls_chachapoly_finish(
910 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
911 if (ret != 0) {
912 return ret;
913 }
914
915 /* Check the tag in "constant-time" */
916 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
917 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
918 goto exit;
919 }
920 }
921#endif /* MBEDTLS_CHACHAPOLY_C */
922
923exit:
924 mbedtls_platform_zeroize(check_tag, tag_len);
925 return ret;
926}
927#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
928
929/*
930 * Packet-oriented wrapper for non-AEAD modes
931 */
932int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
933 const unsigned char *iv, size_t iv_len,
934 const unsigned char *input, size_t ilen,
935 unsigned char *output, size_t *olen)
936{
937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
938 size_t finish_olen;
939
940 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
941 return ret;
942 }
943
944 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
945 return ret;
946 }
947
948 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
949 output, olen)) != 0) {
950 return ret;
951 }
952
953 size_t invalid_padding = 0;
954 if ((ret = mbedtls_cipher_finish_padded(ctx, output + *olen,
955 &finish_olen,
956 &invalid_padding)) != 0) {
957 return ret;
958 }
959 *olen += finish_olen;
960
961 ret = mbedtls_ct_error_if_else_0(invalid_padding,
962 MBEDTLS_ERR_CIPHER_INVALID_PADDING);
963 return ret;
964}
965
966#if defined(MBEDTLS_CIPHER_MODE_AEAD)
967/*
968 * Packet-oriented encryption for AEAD modes: internal function used by
969 * mbedtls_cipher_auth_encrypt_ext().
970 */
971static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
972 const unsigned char *iv, size_t iv_len,
973 const unsigned char *ad, size_t ad_len,
974 const unsigned char *input, size_t ilen,
975 unsigned char *output, size_t *olen,
976 unsigned char *tag, size_t tag_len)
977{
978
979#if defined(MBEDTLS_GCM_C)
980 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
981 *olen = ilen;
982 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
983 ilen, iv, iv_len, ad, ad_len,
984 input, output, tag_len, tag);
985 }
986#endif /* MBEDTLS_GCM_C */
987#if defined(MBEDTLS_CCM_C)
988 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
989 *olen = ilen;
990 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
991 iv, iv_len, ad, ad_len, input, output,
992 tag, tag_len);
993 }
994#endif /* MBEDTLS_CCM_C */
995#if defined(MBEDTLS_CHACHAPOLY_C)
996 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
997 /* ChachaPoly has fixed length nonce and MAC (tag) */
998 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
999 (tag_len != 16U)) {
1000 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1001 }
1002
1003 *olen = ilen;
1004 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1005 ilen, iv, ad, ad_len, input, output, tag);
1006 }
1007#endif /* MBEDTLS_CHACHAPOLY_C */
1008
1009 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1010}
1011
1012/*
1013 * Packet-oriented encryption for AEAD modes: internal function used by
1014 * mbedtls_cipher_auth_encrypt_ext().
1015 */
1016static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1017 const unsigned char *iv, size_t iv_len,
1018 const unsigned char *ad, size_t ad_len,
1019 const unsigned char *input, size_t ilen,
1020 unsigned char *output, size_t *olen,
1021 const unsigned char *tag, size_t tag_len)
1022{
1023
1024#if defined(MBEDTLS_GCM_C)
1025 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1027
1028 *olen = ilen;
1029 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1030 iv, iv_len, ad, ad_len,
1031 tag, tag_len, input, output);
1032
1033 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
1034 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1035 }
1036
1037 return ret;
1038 }
1039#endif /* MBEDTLS_GCM_C */
1040#if defined(MBEDTLS_CCM_C)
1041 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1043
1044 *olen = ilen;
1045 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1046 iv, iv_len, ad, ad_len,
1047 input, output, tag, tag_len);
1048
1049 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
1050 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1051 }
1052
1053 return ret;
1054 }
1055#endif /* MBEDTLS_CCM_C */
1056#if defined(MBEDTLS_CHACHAPOLY_C)
1057 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
1058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1059
1060 /* ChachaPoly has fixed length nonce and MAC (tag) */
1061 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
1062 (tag_len != 16U)) {
1063 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1064 }
1065
1066 *olen = ilen;
1067 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1068 iv, ad, ad_len, tag, input, output);
1069
1070 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
1071 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1072 }
1073
1074 return ret;
1075 }
1076#endif /* MBEDTLS_CHACHAPOLY_C */
1077
1078 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1079}
1080#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1081
1082#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1083/*
1084 * Packet-oriented encryption for AEAD: public function.
1085 */
1086int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1087 const unsigned char *iv, size_t iv_len,
1088 const unsigned char *ad, size_t ad_len,
1089 const unsigned char *input, size_t ilen,
1090 unsigned char *output, size_t output_len,
1091 size_t *olen, size_t tag_len)
1092{
1093 /* AEAD case: check length before passing on to shared function */
1094 if (output_len < ilen + tag_len) {
1095 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1096 }
1097
1098 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1099 input, ilen, output, olen,
1100 output + ilen, tag_len);
1101 *olen += tag_len;
1102 return ret;
1103}
1104
1105/*
1106 * Packet-oriented decryption for AEAD: public function.
1107 */
1108int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1109 const unsigned char *iv, size_t iv_len,
1110 const unsigned char *ad, size_t ad_len,
1111 const unsigned char *input, size_t ilen,
1112 unsigned char *output, size_t output_len,
1113 size_t *olen, size_t tag_len)
1114{
1115 /* AEAD case: check length before passing on to shared function */
1116 if (ilen < tag_len || output_len < ilen - tag_len) {
1117 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1118 }
1119
1120 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1121 input, ilen - tag_len, output, olen,
1122 input + ilen - tag_len, tag_len);
1123}
1124#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1125
1126#endif /* MBEDTLS_CIPHER_C */