blob: 7dc09ee94567d05149148fa1553a1988cd5124d9 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
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
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
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.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073 int *type;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 def = mbedtls_cipher_definitions;
77 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020080 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020082
83 *type = 0;
84
85 supported_init = 1;
86 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000089}
90
Hanno Becker18597cd2018-11-09 16:36:33 +000091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010092 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
97 if (def->type == cipher_type) {
98 return def->info;
99 }
100 }
Paul Bakker343a8702011-06-09 14:27:58 +0000101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103}
104
Hanno Becker18597cd2018-11-09 16:36:33 +0000105const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (NULL == cipher_name) {
111 return NULL;
112 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
115 if (!strcmp(def->info->name, cipher_name)) {
116 return def->info;
117 }
118 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121}
122
Hanno Becker18597cd2018-11-09 16:36:33 +0000123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
124 const mbedtls_cipher_id_t cipher_id,
125 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
131 if (def->info->base->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100132 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 def->info->mode == mode) {
134 return def->info;
135 }
136 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200139}
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200142{
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200144}
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200149 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (ctx->psa_enabled == 1) {
154 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000155 mbedtls_cipher_context_psa * const cipher_psa =
156 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000159 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
164 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000165 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000168 return;
169 }
170#endif /* MBEDTLS_USE_PSA_CRYPTO */
171
Simon Butcher327398a2016-10-05 14:09:11 +0100172#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (ctx->cmac_ctx) {
174 mbedtls_platform_zeroize(ctx->cmac_ctx,
175 sizeof(mbedtls_cmac_context_t));
176 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100177 }
178#endif
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ctx->cipher_ctx) {
181 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
182 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185}
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
188 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189{
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (cipher_info == NULL) {
191 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
192 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
197 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
198 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
200 ctx->cipher_info = cipher_info;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200203 /*
204 * Ignore possible errors caused by a cipher mode that doesn't use padding
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200208#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214}
215
Hanno Becker4ccfc402018-11-09 16:10:57 +0000216#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200217#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100218int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
219 const mbedtls_cipher_info_t *cipher_info,
220 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000221{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000222 psa_algorithm_t alg;
223 mbedtls_cipher_context_psa *cipher_psa;
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (NULL == cipher_info || NULL == ctx) {
226 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
227 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000228
Hanno Becker4ee7e762018-11-17 22:00:38 +0000229 /* Check that the underlying cipher mode and cipher type are
230 * supported by the underlying PSA Crypto implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
232 if (alg == 0) {
233 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
234 }
235 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
236 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
237 }
Hanno Becker6118e432018-11-09 16:47:20 +0000238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
242 if (cipher_psa == NULL) {
243 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
244 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000245 cipher_psa->alg = alg;
246 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000247 ctx->cipher_info = cipher_info;
248 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000250}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200251#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000252#endif /* MBEDTLS_USE_PSA_CRYPTO */
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
255 const unsigned char *key,
256 int key_bitlen,
257 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100260 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
262 if (ctx->cipher_info == NULL) {
263 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
264 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000266#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000268 mbedtls_cipher_context_psa * const cipher_psa =
269 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000272
273 psa_status_t status;
274 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276
277 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (key_bitlen % 8 != 0) {
279 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
280 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000281
282 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
284 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
285 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000286
Andrzej Kurekc7509322019-01-08 09:36:01 -0500287 key_type = mbedtls_psa_translate_cipher_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 ctx->cipher_info->type);
289 if (key_type == 0) {
290 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
291 }
292 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000293
294 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500295 * (encrypt vs. decrypt): it is possible to setup a key for encryption
296 * and use it for AEAD decryption. Until tests relying on this
297 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 psa_set_key_usage_flags(&attributes,
299 /* mbedtls_psa_translate_cipher_operation( operation ); */
300 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
301 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 status = psa_import_key(&attributes, key, key_bytelen,
304 &cipher_psa->slot);
305 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200306 case PSA_SUCCESS:
307 break;
308 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200310 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200312 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200314 }
315 /* Indicate that we own the key slot and need to
316 * destroy it in mbedtls_cipher_free(). */
317 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000318
319 ctx->key_bitlen = key_bitlen;
320 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000322 }
323#endif /* MBEDTLS_USE_PSA_CRYPTO */
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100326 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200328 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200329
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200330 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 ctx->operation = operation;
332
Paul Bakker343a8702011-06-09 14:27:58 +0000333 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100334 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100338 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
340 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
341 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000342 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (MBEDTLS_DECRYPT == operation) {
345 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
346 ctx->key_bitlen);
347 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350}
351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
353 const unsigned char *iv,
354 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200356 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (ctx->cipher_info == NULL) {
359 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
360 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000361#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000363 /* While PSA Crypto has an API for multipart
364 * operations, we currently don't make it
365 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000367 }
368#endif /* MBEDTLS_USE_PSA_CRYPTO */
369
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200370 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
372 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
373 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200376 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 } else {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200378 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200379
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200380 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (actual_iv_size > iv_len) {
382 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
383 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200384 }
385
Daniel Kingbd920622016-05-15 19:56:20 -0300386#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100388 /* Even though the actual_iv_size is overwritten with a correct value
389 * of 12 from the cipher info, return an error to indicate that
390 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (iv_len != 12) {
392 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
393 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
396 iv,
397 0U)) { /* Initial counter value */
398 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300399 }
400 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100401#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
403 iv_len != 12) {
404 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
405 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100406#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300407#endif
408
Gilles Peskine295fc132021-04-15 18:32:23 +0200409#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
411 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
412 ctx->operation,
413 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200414 }
415#endif
416
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200417#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200419 int set_lengths_result;
420 int ccm_star_mode;
421
422 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 (mbedtls_ccm_context *) ctx->cipher_ctx,
424 0, 0, 0);
425 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200426 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200430 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200432 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200434 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
438 ccm_star_mode,
439 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200440 }
441#endif
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (actual_iv_size != 0) {
444 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300445 ctx->iv_size = actual_iv_size;
446 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200449}
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (ctx->cipher_info == NULL) {
454 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
455 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200456
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000459 /* We don't support resetting PSA-based
460 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000462 }
463#endif /* MBEDTLS_USE_PSA_CRYPTO */
464
Paul Bakker8123e9d2011-01-06 15:37:30 +0000465 ctx->unprocessed_len = 0;
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200468}
469
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200470#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100471int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
472 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (ctx->cipher_info == NULL) {
475 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
476 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200477
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000478#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000480 /* While PSA Crypto has an API for multipart
481 * operations, we currently don't make it
482 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000484 }
485#endif /* MBEDTLS_USE_PSA_CRYPTO */
486
Daniel King8fe47012016-05-17 20:33:28 -0300487#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
489 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
490 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200491 }
Daniel King8fe47012016-05-17 20:33:28 -0300492#endif
493
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300496 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200497 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200500 ? MBEDTLS_CHACHAPOLY_ENCRYPT
501 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
504 ctx->iv,
505 mode);
506 if (result != 0) {
507 return result;
508 }
Daniel King8fe47012016-05-17 20:33:28 -0300509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
511 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300512 }
513#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200517#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
520 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521{
Janos Follath24eed8d2019-11-22 13:21:35 +0000522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500523 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (ctx->cipher_info == NULL) {
526 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
527 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000531 /* While PSA Crypto has an API for multipart
532 * operations, we currently don't make it
533 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000535 }
536#endif /* MBEDTLS_USE_PSA_CRYPTO */
537
Paul Bakker6c212762013-12-16 15:24:50 +0100538 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 block_size = mbedtls_cipher_get_block_size(ctx);
540 if (0 == block_size) {
541 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100542 }
Paul Bakker6c212762013-12-16 15:24:50 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
545 if (ilen != block_size) {
546 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
547 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200548
549 *olen = ilen;
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
552 ctx->operation, input, output))) {
553 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200554 }
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200557 }
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
561 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
562 input, ilen,
563 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200564 }
565#endif
566
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200567#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
569 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
570 input, ilen,
571 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200572 }
573#endif
574
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200575#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200577 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
579 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200580 }
581#endif
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (input == output &&
584 (ctx->unprocessed_len != 0 || ilen % block_size)) {
585 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100586 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200590 size_t copy_len = 0;
591
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 /*
593 * If there is not enough data for a full block, cache it.
594 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
596 ilen <= block_size - ctx->unprocessed_len) ||
597 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
598 ilen < block_size - ctx->unprocessed_len) ||
599 (ctx->operation == MBEDTLS_ENCRYPT &&
600 ilen < block_size - ctx->unprocessed_len)) {
601 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
602 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
604 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 }
607
608 /*
609 * Process cached data first
610 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100612 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
615 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
618 ctx->operation, block_size, ctx->iv,
619 ctx->unprocessed_data, output))) {
620 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 }
622
Janos Follath98e28a72016-05-31 14:03:54 +0100623 *olen += block_size;
624 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 ctx->unprocessed_len = 0;
626
627 input += copy_len;
628 ilen -= copy_len;
629 }
630
631 /*
632 * Cache final, incomplete block
633 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700635 /* Encryption: only cache partial blocks
636 * Decryption w/ padding: always keep at least one whole block
637 * Decryption w/o padding: only cache partial blocks
638 */
Janos Follath98e28a72016-05-31 14:03:54 +0100639 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700641 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100643 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700644 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
647 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
649 ctx->unprocessed_len += copy_len;
650 ilen -= copy_len;
651 }
652
653 /*
654 * Process remaining full blocks
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (ilen) {
657 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
658 ctx->operation, ilen, ctx->iv, input,
659 output))) {
660 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200662
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 *olen += ilen;
664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
672 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
673 ctx->operation, ilen,
674 &ctx->unprocessed_len, ctx->iv,
675 input, output))) {
676 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000677 }
678
679 *olen = ilen;
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000684
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100685#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
687 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
688 ilen, &ctx->unprocessed_len, ctx->iv,
689 input, output))) {
690 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100691 }
692
693 *olen = ilen;
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100696 }
697#endif /* MBEDTLS_CIPHER_MODE_OFB */
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
701 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
702 ilen, &ctx->unprocessed_len, ctx->iv,
703 ctx->unprocessed_data, input, output))) {
704 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000705 }
706
707 *olen = ilen;
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000712
Jaeden Ameroc6539902018-04-30 17:17:41 +0100713#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
715 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100716 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
721 ctx->operation, ilen, ctx->iv, input, output);
722 if (ret != 0) {
723 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100724 }
725
726 *olen = ilen;
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100729 }
730#endif /* MBEDTLS_CIPHER_MODE_XTS */
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
734 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
735 ilen, input, output))) {
736 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200737 }
738
739 *olen = ilen;
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746}
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
749#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200750/*
751 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
752 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753static void add_pkcs_padding(unsigned char *output, size_t output_len,
754 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755{
Paul Bakker23986e52011-04-24 08:57:21 +0000756 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100757 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000760 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762}
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int get_pkcs_padding(unsigned char *input, size_t input_len,
765 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100767 size_t i, pad_idx;
768 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (NULL == input || NULL == data_len) {
771 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
772 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773
774 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775 *data_len = input_len - padding_len;
776
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100777 /* Avoid logical || since it results in a branch */
778 bad |= padding_len > input_len;
779 bad |= padding_len == 0;
780
781 /* The number of bytes checked must be independent of padding_len,
782 * so pick input_len, which is usually 8 or 16 (one block) */
783 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 for (i = 0; i < input_len; i++) {
785 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
786 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200793/*
794 * One and zeros padding: fill with 80 00 ... 00
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static void add_one_and_zeros_padding(unsigned char *output,
797 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200798{
799 size_t padding_len = output_len - data_len;
800 unsigned char i = 0;
801
802 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200806}
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
809 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100811 size_t i;
812 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (NULL == input || NULL == data_len) {
815 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
816 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200817
Micha Krausba8316f2017-12-23 23:40:08 +0100818 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100819 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100821 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 done |= (input[i - 1] != 0);
823 *data_len |= (i - 1) * (done != prev_done);
824 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100825 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200828
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200833/*
834 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
835 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static void add_zeros_and_len_padding(unsigned char *output,
837 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200838{
839 size_t padding_len = output_len - data_len;
840 unsigned char i = 0;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200843 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200845 output[output_len - 1] = (unsigned char) padding_len;
846}
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
849 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100851 size_t i, pad_idx;
852 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (NULL == input || NULL == data_len) {
855 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
856 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857
858 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200859 *data_len = input_len - padding_len;
860
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100861 /* Avoid logical || since it results in a branch */
862 bad |= padding_len > input_len;
863 bad |= padding_len == 0;
864
865 /* The number of bytes checked must be independent of padding_len */
866 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 for (i = 0; i < input_len - 1; i++) {
868 bad |= input[i] * (i >= pad_idx);
869 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200876/*
877 * Zero padding: fill with 00 ... 00
878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static void add_zeros_padding(unsigned char *output,
880 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200881{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200882 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887}
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int get_zeros_padding(unsigned char *input, size_t input_len,
890 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100892 size_t i;
893 unsigned char done = 0, prev_done;
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (NULL == input || NULL == data_len) {
896 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100897 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 *data_len = 0;
900 for (i = input_len; i > 0; i--) {
901 prev_done = done;
902 done |= (input[i-1] != 0);
903 *data_len |= i * (done != prev_done);
904 }
905
906 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200909
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200910/*
911 * No padding: don't pad :)
912 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200914 * but a trivial get_padding function
915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916static int get_no_padding(unsigned char *input, size_t input_len,
917 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200918{
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (NULL == input || NULL == data_len) {
920 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
921 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200922
923 *data_len = input_len;
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
930 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000931{
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (ctx->cipher_info == NULL) {
933 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
934 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000936#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000938 /* While PSA Crypto has an API for multipart
939 * operations, we currently don't make it
940 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000942 }
943#endif /* MBEDTLS_USE_PSA_CRYPTO */
944
Paul Bakker8123e9d2011-01-06 15:37:30 +0000945 *olen = 0;
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100948 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
950 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200951 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100952 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
954 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000955 }
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
958 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
959 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
963 if (ctx->unprocessed_len != 0) {
964 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
965 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200968 }
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200972 int ret = 0;
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (NULL == ctx->add_padding) {
977 if (0 != ctx->unprocessed_len) {
978 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
979 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200982 }
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
985 ctx->unprocessed_len);
986 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200987 /*
988 * For decrypt operations, expect a full block,
989 * or an empty block if no padding
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
992 return 0;
993 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 }
997
998 /* cipher block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
1000 ctx->operation,
1001 mbedtls_cipher_get_block_size(ctx),
1002 ctx->iv,
1003 ctx->unprocessed_data, output))) {
1004 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005 }
1006
1007 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (MBEDTLS_DECRYPT == ctx->operation) {
1009 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1010 olen);
1011 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001012
1013 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 *olen = mbedtls_cipher_get_block_size(ctx);
1015 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001017#else
1018 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001022}
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001025int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1026 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001027{
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1029 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001030 }
1031
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001032#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001034 /* While PSA Crypto knows about CBC padding
1035 * schemes, we currently don't make them
1036 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (mode != MBEDTLS_PADDING_NONE) {
1038 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1039 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001042 }
1043#endif /* MBEDTLS_USE_PSA_CRYPTO */
1044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 case MBEDTLS_PADDING_PKCS7:
1048 ctx->add_padding = add_pkcs_padding;
1049 ctx->get_padding = get_pkcs_padding;
1050 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1054 ctx->add_padding = add_one_and_zeros_padding;
1055 ctx->get_padding = get_one_and_zeros_padding;
1056 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1060 ctx->add_padding = add_zeros_and_len_padding;
1061 ctx->get_padding = get_zeros_and_len_padding;
1062 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 case MBEDTLS_PADDING_ZEROS:
1066 ctx->add_padding = add_zeros_padding;
1067 ctx->get_padding = get_zeros_padding;
1068 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001069#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 case MBEDTLS_PADDING_NONE:
1071 ctx->add_padding = NULL;
1072 ctx->get_padding = get_no_padding;
1073 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 default:
1076 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001077 }
1078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001082
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001083#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1085 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001086{
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ctx->cipher_info == NULL) {
1088 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1089 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (MBEDTLS_ENCRYPT != ctx->operation) {
1092 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1093 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001094
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001095#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001097 /* While PSA Crypto has an API for multipart
1098 * operations, we currently don't make it
1099 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001101 }
1102#endif /* MBEDTLS_USE_PSA_CRYPTO */
1103
Daniel King8fe47012016-05-17 20:33:28 -03001104#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001106 size_t output_length;
1107 /* The code here doesn't yet support alternative implementations
1108 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1110 NULL, 0, &output_length,
1111 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001112 }
Daniel King8fe47012016-05-17 20:33:28 -03001113#endif
1114
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001115#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001117 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (tag_len != 16U) {
1119 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1120 }
Daniel King8fe47012016-05-17 20:33:28 -03001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return mbedtls_chachapoly_finish(
1123 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001124 }
1125#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001128}
Paul Bakker9af723c2014-05-01 13:03:14 +02001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1131 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001132{
Daniel King8fe47012016-05-17 20:33:28 -03001133 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (ctx->cipher_info == NULL) {
1137 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1138 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (MBEDTLS_DECRYPT != ctx->operation) {
1141 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001142 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001143
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001146 /* While PSA Crypto has an API for multipart
1147 * operations, we currently don't make it
1148 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001150 }
1151#endif /* MBEDTLS_USE_PSA_CRYPTO */
1152
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001153 /* Status to return on a non-authenticated algorithm. */
1154 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001155
Daniel King8fe47012016-05-17 20:33:28 -03001156#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001158 size_t output_length;
1159 /* The code here doesn't yet support alternative implementations
1160 * that can delay up to a block of output. */
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (tag_len > sizeof(check_tag)) {
1163 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1164 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (0 != (ret = mbedtls_gcm_finish(
1167 (mbedtls_gcm_context *) ctx->cipher_ctx,
1168 NULL, 0, &output_length,
1169 check_tag, tag_len))) {
1170 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001171 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001172
1173 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001175 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001176 goto exit;
1177 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001178 }
Daniel King8fe47012016-05-17 20:33:28 -03001179#endif /* MBEDTLS_GCM_C */
1180
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001183 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (tag_len != sizeof(check_tag)) {
1185 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1186 }
Daniel King8fe47012016-05-17 20:33:28 -03001187
Hanno Becker18597cd2018-11-09 16:36:33 +00001188 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1190 if (ret != 0) {
1191 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001192 }
1193
1194 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001196 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001197 goto exit;
1198 }
Daniel King8fe47012016-05-17 20:33:28 -03001199 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001200#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001201
Gilles Peskinecd742982021-12-13 16:57:47 +01001202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_platform_zeroize(check_tag, tag_len);
1204 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001205}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001206#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001207
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001208/*
1209 * Packet-oriented wrapper for non-AEAD modes
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1212 const unsigned char *iv, size_t iv_len,
1213 const unsigned char *input, size_t ilen,
1214 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001215{
Janos Follath24eed8d2019-11-22 13:21:35 +00001216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001217 size_t finish_olen;
1218
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001221 /* As in the non-PSA case, we don't check that
1222 * a key has been set. If not, the key slot will
1223 * still be in its default state of 0, which is
1224 * guaranteed to be invalid, hence the PSA-call
1225 * below will gracefully fail. */
1226 mbedtls_cipher_context_psa * const cipher_psa =
1227 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1228
1229 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001230 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001231 size_t part_len;
1232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (ctx->operation == MBEDTLS_DECRYPT) {
1234 status = psa_cipher_decrypt_setup(&cipher_op,
1235 cipher_psa->slot,
1236 cipher_psa->alg);
1237 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1238 status = psa_cipher_encrypt_setup(&cipher_op,
1239 cipher_psa->slot,
1240 cipher_psa->alg);
1241 } else {
1242 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001243 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001244
1245 /* In the following, we can immediately return on an error,
1246 * because the PSA Crypto API guarantees that cipher operations
1247 * are terminated by unsuccessful calls to psa_cipher_update(),
1248 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (status != PSA_SUCCESS) {
1250 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001251 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1254 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1255 if (status != PSA_SUCCESS) {
1256 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1257 }
1258 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 status = psa_cipher_update(&cipher_op,
1261 input, ilen,
1262 output, ilen, olen);
1263 if (status != PSA_SUCCESS) {
1264 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1265 }
1266
1267 status = psa_cipher_finish(&cipher_op,
1268 output + *olen, ilen - *olen,
1269 &part_len);
1270 if (status != PSA_SUCCESS) {
1271 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1272 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001273
1274 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001276 }
1277#endif /* MBEDTLS_USE_PSA_CRYPTO */
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1280 return ret;
1281 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1284 return ret;
1285 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1288 output, olen)) != 0) {
1289 return ret;
1290 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1293 &finish_olen)) != 0) {
1294 return ret;
1295 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001296
1297 *olen += finish_olen;
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001300}
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001303/*
TRodziewicz18efb732021-04-29 23:12:19 +02001304 * Packet-oriented encryption for AEAD modes: internal function used by
1305 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1308 const unsigned char *iv, size_t iv_len,
1309 const unsigned char *ad, size_t ad_len,
1310 const unsigned char *input, size_t ilen,
1311 unsigned char *output, size_t *olen,
1312 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001313{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001314#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001316 /* As in the non-PSA case, we don't check that
1317 * a key has been set. If not, the key slot will
1318 * still be in its default state of 0, which is
1319 * guaranteed to be invalid, hence the PSA-call
1320 * below will gracefully fail. */
1321 mbedtls_cipher_context_psa * const cipher_psa =
1322 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1323
1324 psa_status_t status;
1325
1326 /* PSA Crypto API always writes the authentication tag
1327 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (output == NULL || tag != output + ilen) {
1329 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1330 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 status = psa_aead_encrypt(cipher_psa->slot,
1333 cipher_psa->alg,
1334 iv, iv_len,
1335 ad, ad_len,
1336 input, ilen,
1337 output, ilen + tag_len, olen);
1338 if (status != PSA_SUCCESS) {
1339 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1340 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001341
1342 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001344 }
1345#endif /* MBEDTLS_USE_PSA_CRYPTO */
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001349 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1351 ilen, iv, iv_len, ad, ad_len,
1352 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_GCM_C */
1355#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001357 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1359 iv, iv_len, ad, ad_len, input, output,
1360 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001363#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001365 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if ((iv_len != ctx->cipher_info->iv_size) ||
1367 (tag_len != 16U)) {
1368 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001369 }
1370
1371 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1373 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001374 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001375#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001378}
1379
1380/*
TRodziewicz18efb732021-04-29 23:12:19 +02001381 * Packet-oriented encryption for AEAD modes: internal function used by
1382 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1385 const unsigned char *iv, size_t iv_len,
1386 const unsigned char *ad, size_t ad_len,
1387 const unsigned char *input, size_t ilen,
1388 unsigned char *output, size_t *olen,
1389 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001390{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001391#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001393 /* As in the non-PSA case, we don't check that
1394 * a key has been set. If not, the key slot will
1395 * still be in its default state of 0, which is
1396 * guaranteed to be invalid, hence the PSA-call
1397 * below will gracefully fail. */
1398 mbedtls_cipher_context_psa * const cipher_psa =
1399 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1400
1401 psa_status_t status;
1402
1403 /* PSA Crypto API always writes the authentication tag
1404 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (input == NULL || tag != input + ilen) {
1406 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1407 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 status = psa_aead_decrypt(cipher_psa->slot,
1410 cipher_psa->alg,
1411 iv, iv_len,
1412 ad, ad_len,
1413 input, ilen + tag_len,
1414 output, ilen, olen);
1415 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1416 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1417 } else if (status != PSA_SUCCESS) {
1418 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1419 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001422 }
1423#endif /* MBEDTLS_USE_PSA_CRYPTO */
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001428
1429 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1431 iv, iv_len, ad, ad_len,
1432 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_GCM_C */
1441#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001444
1445 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1447 iv, iv_len, ad, ad_len,
1448 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001457#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001460
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001461 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if ((iv_len != ctx->cipher_info->iv_size) ||
1463 (tag_len != 16U)) {
1464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
1466
1467 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1469 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001472 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 }
Daniel King8fe47012016-05-17 20:33:28 -03001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001476 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001477#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001482
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001483#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1484/*
1485 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1488 const unsigned char *iv, size_t iv_len,
1489 const unsigned char *ad, size_t ad_len,
1490 const unsigned char *input, size_t ilen,
1491 unsigned char *output, size_t output_len,
1492 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001493{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001494#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001496#if defined(MBEDTLS_USE_PSA_CRYPTO)
1497 ctx->psa_enabled == 0 &&
1498#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1500 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1501 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1502 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001503
1504 /* There is no iv, tag or ad associated with KW and KWP,
1505 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1507 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1508 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001509
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001510 (void) iv;
1511 (void) ad;
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1514 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001515 }
1516#endif /* MBEDTLS_NIST_KW_C */
1517
1518#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1519 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (output_len < ilen + tag_len) {
1521 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1522 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1525 input, ilen, output, olen,
1526 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001527 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001529#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001531#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1532}
1533
1534/*
1535 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1538 const unsigned char *iv, size_t iv_len,
1539 const unsigned char *ad, size_t ad_len,
1540 const unsigned char *input, size_t ilen,
1541 unsigned char *output, size_t output_len,
1542 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001543{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001544#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001546#if defined(MBEDTLS_USE_PSA_CRYPTO)
1547 ctx->psa_enabled == 0 &&
1548#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1550 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1551 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1552 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001553
1554 /* There is no iv, tag or ad associated with KW and KWP,
1555 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1557 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1558 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001560 (void) iv;
1561 (void) ad;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1564 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001565 }
1566#endif /* MBEDTLS_NIST_KW_C */
1567
1568#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1569 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (ilen < tag_len || output_len < ilen - tag_len) {
1571 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1572 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1575 input, ilen - tag_len, output, olen,
1576 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001577#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1580}
1581#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_CIPHER_C */