text
[RRG-proxmark3.git] / common / mbedtls / cipher.c
blob61941f56c5326f001d40491d836d7a5838049661
1 /**
2 * \file cipher.c
4 * \brief Generic cipher wrapper for mbed TLS
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 #include "common.h"
26 #if defined(MBEDTLS_CIPHER_C)
28 #include "mbedtls/cipher.h"
29 #include "mbedtls/cipher_internal.h"
30 #include "mbedtls/platform_util.h"
31 #include "mbedtls/error.h"
33 #include <stdlib.h>
34 #include <string.h>
36 #if defined(MBEDTLS_CHACHAPOLY_C)
37 #include "mbedtls/chachapoly.h"
38 #endif
40 #if defined(MBEDTLS_GCM_C)
41 #include "mbedtls/gcm.h"
42 #endif
44 #if defined(MBEDTLS_CCM_C)
45 #include "mbedtls/ccm.h"
46 #endif
48 #if defined(MBEDTLS_CHACHA20_C)
49 #include "mbedtls/chacha20.h"
50 #endif
52 #if defined(MBEDTLS_CMAC_C)
53 #include "mbedtls/cmac.h"
54 #endif
56 #if defined(MBEDTLS_USE_PSA_CRYPTO)
57 #include "psa/crypto.h"
58 #include "mbedtls/psa_util.h"
59 #endif /* MBEDTLS_USE_PSA_CRYPTO */
61 #if defined(MBEDTLS_NIST_KW_C)
62 #include "mbedtls/nist_kw.h"
63 #endif
65 #if defined(MBEDTLS_PLATFORM_C)
66 #include "mbedtls/platform.h"
67 #else
68 #define mbedtls_calloc calloc
69 #define mbedtls_free free
70 #endif
72 #define CIPHER_VALIDATE_RET( cond ) \
73 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
74 #define CIPHER_VALIDATE( cond ) \
75 MBEDTLS_INTERNAL_VALIDATE( cond )
77 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
78 /* Compare the contents of two buffers in constant time.
79 * Returns 0 if the contents are bitwise identical, otherwise returns
80 * a non-zero value.
81 * This is currently only used by GCM and ChaCha20+Poly1305.
83 static int mbedtls_constant_time_memcmp(const void *v1, const void *v2,
84 size_t len) {
85 const unsigned char *p1 = (const unsigned char *) v1;
86 const unsigned char *p2 = (const unsigned char *) v2;
87 size_t i;
88 unsigned char diff;
90 for (diff = 0, i = 0; i < len; i++)
91 diff |= p1[i] ^ p2[i];
93 return ((int)diff);
95 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
97 static int supported_init = 0;
99 const int *mbedtls_cipher_list(void) {
100 const mbedtls_cipher_definition_t *def;
101 int *type;
103 if (! supported_init) {
104 def = mbedtls_cipher_definitions;
105 type = mbedtls_cipher_supported;
107 while (def->type != 0)
108 *type++ = (*def++).type;
110 *type = 0;
112 supported_init = 1;
115 return (mbedtls_cipher_supported);
118 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
119 const mbedtls_cipher_type_t cipher_type) {
120 const mbedtls_cipher_definition_t *def;
122 for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
123 if (def->type == cipher_type)
124 return (def->info);
126 return (NULL);
129 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
130 const char *cipher_name) {
131 const mbedtls_cipher_definition_t *def;
133 if (NULL == cipher_name)
134 return (NULL);
136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
137 if (! strcmp(def->info->name, cipher_name))
138 return (def->info);
140 return (NULL);
143 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
144 const mbedtls_cipher_id_t cipher_id,
145 int key_bitlen,
146 const mbedtls_cipher_mode_t mode) {
147 const mbedtls_cipher_definition_t *def;
149 for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
150 if (def->info->base->cipher == cipher_id &&
151 def->info->key_bitlen == (unsigned) key_bitlen &&
152 def->info->mode == mode)
153 return (def->info);
155 return (NULL);
158 void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) {
159 CIPHER_VALIDATE(ctx != NULL);
160 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
163 void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) {
164 if (ctx == NULL)
165 return;
167 #if defined(MBEDTLS_USE_PSA_CRYPTO)
168 if (ctx->psa_enabled == 1) {
169 if (ctx->cipher_ctx != NULL) {
170 mbedtls_cipher_context_psa *const cipher_psa =
171 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
173 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
174 /* xxx_free() doesn't allow to return failures. */
175 (void) psa_destroy_key(cipher_psa->slot);
178 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
179 mbedtls_free(cipher_psa);
182 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
183 return;
185 #endif /* MBEDTLS_USE_PSA_CRYPTO */
187 #if defined(MBEDTLS_CMAC_C)
188 if (ctx->cmac_ctx) {
189 mbedtls_platform_zeroize(ctx->cmac_ctx,
190 sizeof(mbedtls_cmac_context_t));
191 mbedtls_free(ctx->cmac_ctx);
193 #endif
195 if (ctx->cipher_ctx)
196 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
198 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
201 int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
202 const mbedtls_cipher_info_t *cipher_info) {
203 CIPHER_VALIDATE_RET(ctx != NULL);
204 if (cipher_info == NULL)
205 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
207 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
209 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func()))
210 return (MBEDTLS_ERR_CIPHER_ALLOC_FAILED);
212 ctx->cipher_info = cipher_info;
214 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
216 * Ignore possible errors caused by a cipher mode that doesn't use padding
218 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
219 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
220 #else
221 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
222 #endif
223 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
225 return (0);
228 #if defined(MBEDTLS_USE_PSA_CRYPTO)
229 int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
230 const mbedtls_cipher_info_t *cipher_info,
231 size_t taglen) {
232 psa_algorithm_t alg;
233 mbedtls_cipher_context_psa *cipher_psa;
235 if (NULL == cipher_info || NULL == ctx)
236 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
238 /* Check that the underlying cipher mode and cipher type are
239 * supported by the underlying PSA Crypto implementation. */
240 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
241 if (alg == 0)
242 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
243 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0)
244 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
246 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
248 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
249 if (cipher_psa == NULL)
250 return (MBEDTLS_ERR_CIPHER_ALLOC_FAILED);
251 cipher_psa->alg = alg;
252 ctx->cipher_ctx = cipher_psa;
253 ctx->cipher_info = cipher_info;
254 ctx->psa_enabled = 1;
255 return (0);
257 #endif /* MBEDTLS_USE_PSA_CRYPTO */
259 int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
260 const unsigned char *key,
261 int key_bitlen,
262 const mbedtls_operation_t operation) {
263 CIPHER_VALIDATE_RET(ctx != NULL);
264 CIPHER_VALIDATE_RET(key != NULL);
265 CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
266 operation == MBEDTLS_DECRYPT);
267 if (ctx->cipher_info == NULL)
268 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
270 #if defined(MBEDTLS_USE_PSA_CRYPTO)
271 if (ctx->psa_enabled == 1) {
272 mbedtls_cipher_context_psa *const cipher_psa =
273 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
275 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
277 psa_status_t status;
278 psa_key_type_t key_type;
279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
281 /* PSA Crypto API only accepts byte-aligned keys. */
282 if (key_bitlen % 8 != 0)
283 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
285 /* Don't allow keys to be set multiple times. */
286 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET)
287 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
289 key_type = mbedtls_psa_translate_cipher_type(
290 ctx->cipher_info->type);
291 if (key_type == 0)
292 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
293 psa_set_key_type(&attributes, key_type);
295 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
296 * (encrypt vs. decrypt): it is possible to setup a key for encryption
297 * and use it for AEAD decryption. Until tests relying on this
298 * are changed, allow any usage in PSA. */
299 psa_set_key_usage_flags(&attributes,
300 /* mbedtls_psa_translate_cipher_operation( operation ); */
301 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
302 psa_set_key_algorithm(&attributes, cipher_psa->alg);
304 status = psa_import_key(&attributes, key, key_bytelen,
305 &cipher_psa->slot);
306 switch (status) {
307 case PSA_SUCCESS:
308 break;
309 case PSA_ERROR_INSUFFICIENT_MEMORY:
310 return (MBEDTLS_ERR_CIPHER_ALLOC_FAILED);
311 case PSA_ERROR_NOT_SUPPORTED:
312 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
313 default:
314 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
316 /* Indicate that we own the key slot and need to
317 * destroy it in mbedtls_cipher_free(). */
318 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
320 ctx->key_bitlen = key_bitlen;
321 ctx->operation = operation;
322 return (0);
324 #endif /* MBEDTLS_USE_PSA_CRYPTO */
326 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
327 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
328 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
331 ctx->key_bitlen = key_bitlen;
332 ctx->operation = operation;
335 * For OFB, CFB and CTR mode always use the encryption key schedule
337 if (MBEDTLS_ENCRYPT == operation ||
338 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
339 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
340 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
341 return (ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
342 ctx->key_bitlen));
345 if (MBEDTLS_DECRYPT == operation)
346 return (ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
347 ctx->key_bitlen));
349 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
352 int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
353 const unsigned char *iv,
354 size_t iv_len) {
355 size_t actual_iv_size;
357 CIPHER_VALIDATE_RET(ctx != NULL);
358 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
359 if (ctx->cipher_info == NULL)
360 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
361 #if defined(MBEDTLS_USE_PSA_CRYPTO)
362 if (ctx->psa_enabled == 1) {
363 /* While PSA Crypto has an API for multipart
364 * operations, we currently don't make it
365 * accessible through the cipher layer. */
366 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
368 #endif /* MBEDTLS_USE_PSA_CRYPTO */
370 /* avoid buffer overflow in ctx->iv */
371 if (iv_len > MBEDTLS_MAX_IV_LENGTH)
372 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
374 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0)
375 actual_iv_size = iv_len;
376 else {
377 actual_iv_size = ctx->cipher_info->iv_size;
379 /* avoid reading past the end of input buffer */
380 if (actual_iv_size > iv_len)
381 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
384 #if defined(MBEDTLS_CHACHA20_C)
385 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
386 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *)ctx->cipher_ctx,
388 0U)) { /* Initial counter value */
389 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
392 #endif
394 if (actual_iv_size != 0) {
395 memcpy(ctx->iv, iv, actual_iv_size);
396 ctx->iv_size = actual_iv_size;
399 return (0);
402 int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) {
403 CIPHER_VALIDATE_RET(ctx != NULL);
404 if (ctx->cipher_info == NULL)
405 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
407 #if defined(MBEDTLS_USE_PSA_CRYPTO)
408 if (ctx->psa_enabled == 1) {
409 /* We don't support resetting PSA-based
410 * cipher contexts, yet. */
411 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
413 #endif /* MBEDTLS_USE_PSA_CRYPTO */
415 ctx->unprocessed_len = 0;
417 return (0);
420 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
421 int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
422 const unsigned char *ad, size_t ad_len) {
423 CIPHER_VALIDATE_RET(ctx != NULL);
424 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
425 if (ctx->cipher_info == NULL)
426 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
428 #if defined(MBEDTLS_USE_PSA_CRYPTO)
429 if (ctx->psa_enabled == 1) {
430 /* While PSA Crypto has an API for multipart
431 * operations, we currently don't make it
432 * accessible through the cipher layer. */
433 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
435 #endif /* MBEDTLS_USE_PSA_CRYPTO */
437 #if defined(MBEDTLS_GCM_C)
438 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
439 return (mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
440 ctx->iv, ctx->iv_size, ad, ad_len));
442 #endif
444 #if defined(MBEDTLS_CHACHAPOLY_C)
445 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
446 int result;
447 mbedtls_chachapoly_mode_t mode;
449 mode = (ctx->operation == MBEDTLS_ENCRYPT)
450 ? MBEDTLS_CHACHAPOLY_ENCRYPT
451 : MBEDTLS_CHACHAPOLY_DECRYPT;
453 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
454 ctx->iv,
455 mode);
456 if (result != 0)
457 return (result);
459 return (mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
460 ad, ad_len));
462 #endif
464 return (0);
466 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
468 int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
469 size_t ilen, unsigned char *output, size_t *olen) {
470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
471 size_t block_size;
473 CIPHER_VALIDATE_RET(ctx != NULL);
474 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
475 CIPHER_VALIDATE_RET(output != NULL);
476 CIPHER_VALIDATE_RET(olen != NULL);
477 if (ctx->cipher_info == NULL)
478 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
480 #if defined(MBEDTLS_USE_PSA_CRYPTO)
481 if (ctx->psa_enabled == 1) {
482 /* While PSA Crypto has an API for multipart
483 * operations, we currently don't make it
484 * accessible through the cipher layer. */
485 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
487 #endif /* MBEDTLS_USE_PSA_CRYPTO */
489 *olen = 0;
490 block_size = mbedtls_cipher_get_block_size(ctx);
491 if (0 == block_size) {
492 return (MBEDTLS_ERR_CIPHER_INVALID_CONTEXT);
495 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
496 if (ilen != block_size)
497 return (MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
499 *olen = ilen;
501 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
502 ctx->operation, input, output))) {
503 return (ret);
506 return (0);
509 #if defined(MBEDTLS_GCM_C)
510 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
511 *olen = ilen;
512 return (mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
513 output));
515 #endif
517 #if defined(MBEDTLS_CHACHAPOLY_C)
518 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
519 *olen = ilen;
520 return (mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
521 ilen, input, output));
523 #endif
525 if (input == output &&
526 (ctx->unprocessed_len != 0 || ilen % block_size)) {
527 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
530 #if defined(MBEDTLS_CIPHER_MODE_CBC)
531 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
532 size_t copy_len = 0;
535 * If there is not enough data for a full block, cache it.
537 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
538 ilen <= block_size - ctx->unprocessed_len) ||
539 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
540 ilen < block_size - ctx->unprocessed_len) ||
541 (ctx->operation == MBEDTLS_ENCRYPT &&
542 ilen < block_size - ctx->unprocessed_len)) {
543 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
544 ilen);
546 ctx->unprocessed_len += ilen;
547 return (0);
551 * Process cached data first
553 if (0 != ctx->unprocessed_len) {
554 copy_len = block_size - ctx->unprocessed_len;
556 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
557 copy_len);
559 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
560 ctx->operation, block_size, ctx->iv,
561 ctx->unprocessed_data, output))) {
562 return (ret);
565 *olen += block_size;
566 output += block_size;
567 ctx->unprocessed_len = 0;
569 input += copy_len;
570 ilen -= copy_len;
574 * Cache final, incomplete block
576 if (0 != ilen) {
577 /* Encryption: only cache partial blocks
578 * Decryption w/ padding: always keep at least one whole block
579 * Decryption w/o padding: only cache partial blocks
581 copy_len = ilen % block_size;
582 if (copy_len == 0 &&
583 ctx->operation == MBEDTLS_DECRYPT &&
584 NULL != ctx->add_padding) {
585 copy_len = block_size;
588 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
589 copy_len);
591 ctx->unprocessed_len += copy_len;
592 ilen -= copy_len;
596 * Process remaining full blocks
598 if (ilen) {
599 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
600 ctx->operation, ilen, ctx->iv, input, output))) {
601 return (ret);
604 *olen += ilen;
607 return (0);
609 #endif /* MBEDTLS_CIPHER_MODE_CBC */
611 #if defined(MBEDTLS_CIPHER_MODE_CFB)
612 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
613 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
614 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
615 input, output))) {
616 return (ret);
619 *olen = ilen;
621 return (0);
623 #endif /* MBEDTLS_CIPHER_MODE_CFB */
625 #if defined(MBEDTLS_CIPHER_MODE_OFB)
626 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
627 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
628 ilen, &ctx->unprocessed_len, ctx->iv, input, output))) {
629 return (ret);
632 *olen = ilen;
634 return (0);
636 #endif /* MBEDTLS_CIPHER_MODE_OFB */
638 #if defined(MBEDTLS_CIPHER_MODE_CTR)
639 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
640 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
641 ilen, &ctx->unprocessed_len, ctx->iv,
642 ctx->unprocessed_data, input, output))) {
643 return (ret);
646 *olen = ilen;
648 return (0);
650 #endif /* MBEDTLS_CIPHER_MODE_CTR */
652 #if defined(MBEDTLS_CIPHER_MODE_XTS)
653 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
654 if (ctx->unprocessed_len > 0) {
655 /* We can only process an entire data unit at a time. */
656 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
659 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
660 ctx->operation, ilen, ctx->iv, input, output);
661 if (ret != 0) {
662 return (ret);
665 *olen = ilen;
667 return (0);
669 #endif /* MBEDTLS_CIPHER_MODE_XTS */
671 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
672 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
673 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
674 ilen, input, output))) {
675 return (ret);
678 *olen = ilen;
680 return (0);
682 #endif /* MBEDTLS_CIPHER_MODE_STREAM */
684 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
687 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
688 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
690 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
692 static void add_pkcs_padding(unsigned char *output, size_t output_len,
693 size_t data_len) {
694 size_t padding_len = output_len - data_len;
695 unsigned char i;
697 for (i = 0; i < padding_len; i++)
698 output[data_len + i] = (unsigned char) padding_len;
701 static int get_pkcs_padding(unsigned char *input, size_t input_len,
702 size_t *data_len) {
703 size_t i, pad_idx;
704 unsigned char padding_len, bad = 0;
706 if (NULL == input || NULL == data_len)
707 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
709 padding_len = input[input_len - 1];
710 *data_len = input_len - padding_len;
712 /* Avoid logical || since it results in a branch */
713 bad |= padding_len > input_len;
714 bad |= padding_len == 0;
716 /* The number of bytes checked must be independent of padding_len,
717 * so pick input_len, which is usually 8 or 16 (one block) */
718 pad_idx = input_len - padding_len;
719 for (i = 0; i < input_len; i++)
720 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
722 return (MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0));
724 #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
726 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
728 * One and zeros padding: fill with 80 00 ... 00
730 static void add_one_and_zeros_padding(unsigned char *output,
731 size_t output_len, size_t data_len) {
732 size_t padding_len = output_len - data_len;
733 unsigned char i = 0;
735 output[data_len] = 0x80;
736 for (i = 1; i < padding_len; i++)
737 output[data_len + i] = 0x00;
740 static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
741 size_t *data_len) {
742 size_t i;
743 unsigned char done = 0, prev_done, bad;
745 if (NULL == input || NULL == data_len)
746 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
748 bad = 0x80;
749 *data_len = 0;
750 for (i = input_len; i > 0; i--) {
751 prev_done = done;
752 done |= (input[i - 1] != 0);
753 *data_len |= (i - 1) * (done != prev_done);
754 bad ^= input[i - 1] * (done != prev_done);
757 return (MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0));
760 #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
762 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
764 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
766 static void add_zeros_and_len_padding(unsigned char *output,
767 size_t output_len, size_t data_len) {
768 size_t padding_len = output_len - data_len;
769 unsigned char i = 0;
771 for (i = 1; i < padding_len; i++)
772 output[data_len + i - 1] = 0x00;
773 output[output_len - 1] = (unsigned char) padding_len;
776 static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
777 size_t *data_len) {
778 size_t i, pad_idx;
779 unsigned char padding_len, bad = 0;
781 if (NULL == input || NULL == data_len)
782 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
784 padding_len = input[input_len - 1];
785 *data_len = input_len - padding_len;
787 /* Avoid logical || since it results in a branch */
788 bad |= padding_len > input_len;
789 bad |= padding_len == 0;
791 /* The number of bytes checked must be independent of padding_len */
792 pad_idx = input_len - padding_len;
793 for (i = 0; i < input_len - 1; i++)
794 bad |= input[i] * (i >= pad_idx);
796 return (MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0));
798 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
800 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
802 * Zero padding: fill with 00 ... 00
804 static void add_zeros_padding(unsigned char *output,
805 size_t output_len, size_t data_len) {
806 size_t i;
808 for (i = data_len; i < output_len; i++)
809 output[i] = 0x00;
812 static int get_zeros_padding(unsigned char *input, size_t input_len,
813 size_t *data_len) {
814 size_t i;
815 unsigned char done = 0, prev_done;
817 if (NULL == input || NULL == data_len)
818 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
820 *data_len = 0;
821 for (i = input_len; i > 0; i--) {
822 prev_done = done;
823 done |= (input[i - 1] != 0);
824 *data_len |= i * (done != prev_done);
827 return (0);
829 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
832 * No padding: don't pad :)
834 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
835 * but a trivial get_padding function
837 static int get_no_padding(unsigned char *input, size_t input_len,
838 size_t *data_len) {
839 if (NULL == input || NULL == data_len)
840 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
842 *data_len = input_len;
844 return (0);
846 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
848 int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
849 unsigned char *output, size_t *olen) {
850 CIPHER_VALIDATE_RET(ctx != NULL);
851 CIPHER_VALIDATE_RET(output != NULL);
852 CIPHER_VALIDATE_RET(olen != NULL);
853 if (ctx->cipher_info == NULL)
854 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
856 #if defined(MBEDTLS_USE_PSA_CRYPTO)
857 if (ctx->psa_enabled == 1) {
858 /* While PSA Crypto has an API for multipart
859 * operations, we currently don't make it
860 * accessible through the cipher layer. */
861 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
863 #endif /* MBEDTLS_USE_PSA_CRYPTO */
865 *olen = 0;
867 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
868 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
869 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
870 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
871 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
872 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
873 return (0);
876 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
877 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
878 return (0);
881 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
882 if (ctx->unprocessed_len != 0)
883 return (MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
885 return (0);
888 #if defined(MBEDTLS_CIPHER_MODE_CBC)
889 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
890 int ret = 0;
892 if (MBEDTLS_ENCRYPT == ctx->operation) {
893 /* check for 'no padding' mode */
894 if (NULL == ctx->add_padding) {
895 if (0 != ctx->unprocessed_len)
896 return (MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
898 return (0);
901 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
902 ctx->unprocessed_len);
903 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
905 * For decrypt operations, expect a full block,
906 * or an empty block if no padding
908 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len)
909 return (0);
911 return (MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
914 /* cipher block */
915 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
916 ctx->operation, mbedtls_cipher_get_block_size(ctx), ctx->iv,
917 ctx->unprocessed_data, output))) {
918 return (ret);
921 /* Set output size for decryption */
922 if (MBEDTLS_DECRYPT == ctx->operation)
923 return (ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
924 olen));
926 /* Set output size for encryption */
927 *olen = mbedtls_cipher_get_block_size(ctx);
928 return (0);
930 #else
931 ((void) output);
932 #endif /* MBEDTLS_CIPHER_MODE_CBC */
934 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
937 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
938 int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
939 mbedtls_cipher_padding_t mode) {
940 CIPHER_VALIDATE_RET(ctx != NULL);
942 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
943 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
946 #if defined(MBEDTLS_USE_PSA_CRYPTO)
947 if (ctx->psa_enabled == 1) {
948 /* While PSA Crypto knows about CBC padding
949 * schemes, we currently don't make them
950 * accessible through the cipher layer. */
951 if (mode != MBEDTLS_PADDING_NONE)
952 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
954 return (0);
956 #endif /* MBEDTLS_USE_PSA_CRYPTO */
958 switch (mode) {
959 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
960 case MBEDTLS_PADDING_PKCS7:
961 ctx->add_padding = add_pkcs_padding;
962 ctx->get_padding = get_pkcs_padding;
963 break;
964 #endif
965 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
966 case MBEDTLS_PADDING_ONE_AND_ZEROS:
967 ctx->add_padding = add_one_and_zeros_padding;
968 ctx->get_padding = get_one_and_zeros_padding;
969 break;
970 #endif
971 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
972 case MBEDTLS_PADDING_ZEROS_AND_LEN:
973 ctx->add_padding = add_zeros_and_len_padding;
974 ctx->get_padding = get_zeros_and_len_padding;
975 break;
976 #endif
977 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
978 case MBEDTLS_PADDING_ZEROS:
979 ctx->add_padding = add_zeros_padding;
980 ctx->get_padding = get_zeros_padding;
981 break;
982 #endif
983 case MBEDTLS_PADDING_NONE:
984 ctx->add_padding = NULL;
985 ctx->get_padding = get_no_padding;
986 break;
988 default:
989 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
992 return (0);
994 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
996 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
997 int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
998 unsigned char *tag, size_t tag_len) {
999 CIPHER_VALIDATE_RET(ctx != NULL);
1000 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1001 if (ctx->cipher_info == NULL)
1002 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1004 if (MBEDTLS_ENCRYPT != ctx->operation)
1005 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1007 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1008 if (ctx->psa_enabled == 1) {
1009 /* While PSA Crypto has an API for multipart
1010 * operations, we currently don't make it
1011 * accessible through the cipher layer. */
1012 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1014 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1016 #if defined(MBEDTLS_GCM_C)
1017 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode)
1018 return (mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1019 tag, tag_len));
1020 #endif
1022 #if defined(MBEDTLS_CHACHAPOLY_C)
1023 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1024 /* Don't allow truncated MAC for Poly1305 */
1025 if (tag_len != 16U)
1026 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1028 return (mbedtls_chachapoly_finish(
1029 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag));
1031 #endif
1033 return (0);
1036 int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1037 const unsigned char *tag, size_t tag_len) {
1038 unsigned char check_tag[16];
1039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1041 CIPHER_VALIDATE_RET(ctx != NULL);
1042 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1043 if (ctx->cipher_info == NULL)
1044 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1046 if (MBEDTLS_DECRYPT != ctx->operation) {
1047 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1050 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1051 if (ctx->psa_enabled == 1) {
1052 /* While PSA Crypto has an API for multipart
1053 * operations, we currently don't make it
1054 * accessible through the cipher layer. */
1055 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1057 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1059 #if defined(MBEDTLS_GCM_C)
1060 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1061 if (tag_len > sizeof(check_tag))
1062 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1064 if (0 != (ret = mbedtls_gcm_finish(
1065 (mbedtls_gcm_context *) ctx->cipher_ctx,
1066 check_tag, tag_len))) {
1067 return (ret);
1070 /* Check the tag in "constant-time" */
1071 if (mbedtls_constant_time_memcmp(tag, check_tag, tag_len) != 0)
1072 return (MBEDTLS_ERR_CIPHER_AUTH_FAILED);
1074 return (0);
1076 #endif /* MBEDTLS_GCM_C */
1078 #if defined(MBEDTLS_CHACHAPOLY_C)
1079 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1080 /* Don't allow truncated MAC for Poly1305 */
1081 if (tag_len != sizeof(check_tag))
1082 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1084 ret = mbedtls_chachapoly_finish(
1085 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1086 if (ret != 0) {
1087 return (ret);
1090 /* Check the tag in "constant-time" */
1091 if (mbedtls_constant_time_memcmp(tag, check_tag, tag_len) != 0)
1092 return (MBEDTLS_ERR_CIPHER_AUTH_FAILED);
1094 return (0);
1096 #endif /* MBEDTLS_CHACHAPOLY_C */
1098 return (0);
1100 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
1103 * Packet-oriented wrapper for non-AEAD modes
1105 int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1106 const unsigned char *iv, size_t iv_len,
1107 const unsigned char *input, size_t ilen,
1108 unsigned char *output, size_t *olen) {
1109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1110 size_t finish_olen;
1112 CIPHER_VALIDATE_RET(ctx != NULL);
1113 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1114 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1115 CIPHER_VALIDATE_RET(output != NULL);
1116 CIPHER_VALIDATE_RET(olen != NULL);
1118 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1119 if (ctx->psa_enabled == 1) {
1120 /* As in the non-PSA case, we don't check that
1121 * a key has been set. If not, the key slot will
1122 * still be in its default state of 0, which is
1123 * guaranteed to be invalid, hence the PSA-call
1124 * below will gracefully fail. */
1125 mbedtls_cipher_context_psa *const cipher_psa =
1126 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1128 psa_status_t status;
1129 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1130 size_t part_len;
1132 if (ctx->operation == MBEDTLS_DECRYPT) {
1133 status = psa_cipher_decrypt_setup(&cipher_op,
1134 cipher_psa->slot,
1135 cipher_psa->alg);
1136 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1137 status = psa_cipher_encrypt_setup(&cipher_op,
1138 cipher_psa->slot,
1139 cipher_psa->alg);
1140 } else
1141 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1143 /* In the following, we can immediately return on an error,
1144 * because the PSA Crypto API guarantees that cipher operations
1145 * are terminated by unsuccessful calls to psa_cipher_update(),
1146 * and by any call to psa_cipher_finish(). */
1147 if (status != PSA_SUCCESS)
1148 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1150 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1151 if (status != PSA_SUCCESS)
1152 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1154 status = psa_cipher_update(&cipher_op,
1155 input, ilen,
1156 output, ilen, olen);
1157 if (status != PSA_SUCCESS)
1158 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1160 status = psa_cipher_finish(&cipher_op,
1161 output + *olen, ilen - *olen,
1162 &part_len);
1163 if (status != PSA_SUCCESS)
1164 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1166 *olen += part_len;
1167 return (0);
1169 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1171 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0)
1172 return (ret);
1174 if ((ret = mbedtls_cipher_reset(ctx)) != 0)
1175 return (ret);
1177 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1178 output, olen)) != 0)
1179 return (ret);
1181 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1182 &finish_olen)) != 0)
1183 return (ret);
1185 *olen += finish_olen;
1187 return (0);
1190 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
1192 * Packet-oriented encryption for AEAD modes: internal function shared by
1193 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
1195 static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1196 const unsigned char *iv, size_t iv_len,
1197 const unsigned char *ad, size_t ad_len,
1198 const unsigned char *input, size_t ilen,
1199 unsigned char *output, size_t *olen,
1200 unsigned char *tag, size_t tag_len) {
1201 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1202 if (ctx->psa_enabled == 1) {
1203 /* As in the non-PSA case, we don't check that
1204 * a key has been set. If not, the key slot will
1205 * still be in its default state of 0, which is
1206 * guaranteed to be invalid, hence the PSA-call
1207 * below will gracefully fail. */
1208 mbedtls_cipher_context_psa *const cipher_psa =
1209 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1211 psa_status_t status;
1213 /* PSA Crypto API always writes the authentication tag
1214 * at the end of the encrypted message. */
1215 if (output == NULL || tag != output + ilen)
1216 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1218 status = psa_aead_encrypt(cipher_psa->slot,
1219 cipher_psa->alg,
1220 iv, iv_len,
1221 ad, ad_len,
1222 input, ilen,
1223 output, ilen + tag_len, olen);
1224 if (status != PSA_SUCCESS)
1225 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1227 *olen -= tag_len;
1228 return (0);
1230 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1232 #if defined(MBEDTLS_GCM_C)
1233 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1234 *olen = ilen;
1235 return (mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1236 ilen, iv, iv_len, ad, ad_len,
1237 input, output, tag_len, tag));
1239 #endif /* MBEDTLS_GCM_C */
1240 #if defined(MBEDTLS_CCM_C)
1241 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
1242 *olen = ilen;
1243 return (mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1244 iv, iv_len, ad, ad_len, input, output,
1245 tag, tag_len));
1247 #endif /* MBEDTLS_CCM_C */
1248 #if defined(MBEDTLS_CHACHAPOLY_C)
1249 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1250 /* ChachaPoly has fixed length nonce and MAC (tag) */
1251 if ((iv_len != ctx->cipher_info->iv_size) ||
1252 (tag_len != 16U)) {
1253 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1256 *olen = ilen;
1257 return (mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1258 ilen, iv, ad, ad_len, input, output, tag));
1260 #endif /* MBEDTLS_CHACHAPOLY_C */
1262 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1266 * Packet-oriented encryption for AEAD modes: internal function shared by
1267 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
1269 static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1270 const unsigned char *iv, size_t iv_len,
1271 const unsigned char *ad, size_t ad_len,
1272 const unsigned char *input, size_t ilen,
1273 unsigned char *output, size_t *olen,
1274 const unsigned char *tag, size_t tag_len) {
1275 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1276 if (ctx->psa_enabled == 1) {
1277 /* As in the non-PSA case, we don't check that
1278 * a key has been set. If not, the key slot will
1279 * still be in its default state of 0, which is
1280 * guaranteed to be invalid, hence the PSA-call
1281 * below will gracefully fail. */
1282 mbedtls_cipher_context_psa *const cipher_psa =
1283 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1285 psa_status_t status;
1287 /* PSA Crypto API always writes the authentication tag
1288 * at the end of the encrypted message. */
1289 if (input == NULL || tag != input + ilen)
1290 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1292 status = psa_aead_decrypt(cipher_psa->slot,
1293 cipher_psa->alg,
1294 iv, iv_len,
1295 ad, ad_len,
1296 input, ilen + tag_len,
1297 output, ilen, olen);
1298 if (status == PSA_ERROR_INVALID_SIGNATURE)
1299 return (MBEDTLS_ERR_CIPHER_AUTH_FAILED);
1300 else if (status != PSA_SUCCESS)
1301 return (MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED);
1303 return (0);
1305 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1307 #if defined(MBEDTLS_GCM_C)
1308 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1311 *olen = ilen;
1312 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1313 iv, iv_len, ad, ad_len,
1314 tag, tag_len, input, output);
1316 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED)
1317 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1319 return (ret);
1321 #endif /* MBEDTLS_GCM_C */
1322 #if defined(MBEDTLS_CCM_C)
1323 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
1324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1326 *olen = ilen;
1327 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1328 iv, iv_len, ad, ad_len,
1329 input, output, tag, tag_len);
1331 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED)
1332 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1334 return (ret);
1336 #endif /* MBEDTLS_CCM_C */
1337 #if defined(MBEDTLS_CHACHAPOLY_C)
1338 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1341 /* ChachaPoly has fixed length nonce and MAC (tag) */
1342 if ((iv_len != ctx->cipher_info->iv_size) ||
1343 (tag_len != 16U)) {
1344 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1347 *olen = ilen;
1348 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1349 iv, ad, ad_len, tag, input, output);
1351 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED)
1352 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1354 return (ret);
1356 #endif /* MBEDTLS_CHACHAPOLY_C */
1358 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1361 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
1363 * Packet-oriented encryption for AEAD modes: public legacy function.
1365 int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1366 const unsigned char *iv, size_t iv_len,
1367 const unsigned char *ad, size_t ad_len,
1368 const unsigned char *input, size_t ilen,
1369 unsigned char *output, size_t *olen,
1370 unsigned char *tag, size_t tag_len) {
1371 CIPHER_VALIDATE_RET(ctx != NULL);
1372 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1373 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1374 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1375 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1376 CIPHER_VALIDATE_RET(olen != NULL);
1377 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1379 return (mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1380 input, ilen, output, olen,
1381 tag, tag_len));
1385 * Packet-oriented decryption for AEAD modes: public legacy function.
1387 int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1388 const unsigned char *iv, size_t iv_len,
1389 const unsigned char *ad, size_t ad_len,
1390 const unsigned char *input, size_t ilen,
1391 unsigned char *output, size_t *olen,
1392 const unsigned char *tag, size_t tag_len) {
1393 CIPHER_VALIDATE_RET(ctx != NULL);
1394 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1395 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1396 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1397 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1398 CIPHER_VALIDATE_RET(olen != NULL);
1399 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1401 return (mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1402 input, ilen, output, olen,
1403 tag, tag_len));
1405 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
1406 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
1408 #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1410 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1412 int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1413 const unsigned char *iv, size_t iv_len,
1414 const unsigned char *ad, size_t ad_len,
1415 const unsigned char *input, size_t ilen,
1416 unsigned char *output, size_t output_len,
1417 size_t *olen, size_t tag_len) {
1418 CIPHER_VALIDATE_RET(ctx != NULL);
1419 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1420 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1421 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1422 CIPHER_VALIDATE_RET(output != NULL);
1423 CIPHER_VALIDATE_RET(olen != NULL);
1425 #if defined(MBEDTLS_NIST_KW_C)
1426 if (
1427 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1428 ctx->psa_enabled == 0 &&
1429 #endif
1430 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1431 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1432 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1433 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1435 /* There is no iv, tag or ad associated with KW and KWP,
1436 * so these length should be 0 as documented. */
1437 if (iv_len != 0 || tag_len != 0 || ad_len != 0)
1438 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1440 (void) iv;
1441 (void) ad;
1443 return (mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1444 output, olen, output_len));
1446 #endif /* MBEDTLS_NIST_KW_C */
1448 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
1449 /* AEAD case: check length before passing on to shared function */
1450 if (output_len < ilen + tag_len)
1451 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1453 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1454 input, ilen, output, olen,
1455 output + ilen, tag_len);
1456 *olen += tag_len;
1457 return (ret);
1458 #else
1459 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1460 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
1464 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1466 int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1467 const unsigned char *iv, size_t iv_len,
1468 const unsigned char *ad, size_t ad_len,
1469 const unsigned char *input, size_t ilen,
1470 unsigned char *output, size_t output_len,
1471 size_t *olen, size_t tag_len) {
1472 CIPHER_VALIDATE_RET(ctx != NULL);
1473 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1474 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1475 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1476 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1477 CIPHER_VALIDATE_RET(olen != NULL);
1479 #if defined(MBEDTLS_NIST_KW_C)
1480 if (
1481 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1482 ctx->psa_enabled == 0 &&
1483 #endif
1484 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1485 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1486 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1487 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1489 /* There is no iv, tag or ad associated with KW and KWP,
1490 * so these length should be 0 as documented. */
1491 if (iv_len != 0 || tag_len != 0 || ad_len != 0)
1492 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1494 (void) iv;
1495 (void) ad;
1497 return (mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1498 output, olen, output_len));
1500 #endif /* MBEDTLS_NIST_KW_C */
1502 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
1503 /* AEAD case: check length before passing on to shared function */
1504 if (ilen < tag_len || output_len < ilen - tag_len)
1505 return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
1507 return (mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1508 input, ilen - tag_len, output, olen,
1509 input + ilen - tag_len, tag_len));
1510 #else
1511 return (MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
1512 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
1514 #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1516 #endif /* MBEDTLS_CIPHER_C */