blob: 3ed9aefa396f1423f5762d72b089049491023a17 [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
Dave Rodgmande3de772023-06-24 12:51:06 +010070static inline const mbedtls_cipher_base_t* mbedtls_cipher_get_base(const mbedtls_cipher_info_t *info) {
71 return mbedtls_cipher_base_lookup_table[info->base_idx];
72}
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020077 int *type;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 def = mbedtls_cipher_definitions;
81 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086
87 *type = 0;
88
89 supported_init = 1;
90 }
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000093}
94
Hanno Becker18597cd2018-11-09 16:36:33 +000095const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010096 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
101 if (def->type == cipher_type) {
102 return def->info;
103 }
104 }
Paul Bakker343a8702011-06-09 14:27:58 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107}
108
Hanno Becker18597cd2018-11-09 16:36:33 +0000109const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (NULL == cipher_name) {
115 return NULL;
116 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
119 if (!strcmp(def->info->name, cipher_name)) {
120 return def->info;
121 }
122 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125}
126
Hanno Becker18597cd2018-11-09 16:36:33 +0000127const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
128 const mbedtls_cipher_id_t cipher_id,
129 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100135 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100136 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 def->info->mode == mode) {
138 return def->info;
139 }
140 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143}
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200146{
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148}
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151{
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200155
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000156#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (ctx->psa_enabled == 1) {
158 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000159 mbedtls_cipher_context_psa * const cipher_psa =
160 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000163 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
168 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000169 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000172 return;
173 }
174#endif /* MBEDTLS_USE_PSA_CRYPTO */
175
Simon Butcher327398a2016-10-05 14:09:11 +0100176#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (ctx->cmac_ctx) {
178 mbedtls_platform_zeroize(ctx->cmac_ctx,
179 sizeof(mbedtls_cmac_context_t));
180 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100181 }
182#endif
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100185 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189}
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
192 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193{
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (cipher_info == NULL) {
195 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
196 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
Dave Rodgmande3de772023-06-24 12:51:06 +0100200 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
202 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
204 ctx->cipher_info = cipher_info;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200207 /*
208 * Ignore possible errors caused by a cipher mode that doesn't use padding
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200212#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218}
219
Hanno Becker4ccfc402018-11-09 16:10:57 +0000220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200221#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
223 const mbedtls_cipher_info_t *cipher_info,
224 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000225{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000226 psa_algorithm_t alg;
227 mbedtls_cipher_context_psa *cipher_psa;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (NULL == cipher_info || NULL == ctx) {
230 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
231 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000232
Hanno Becker4ee7e762018-11-17 22:00:38 +0000233 /* Check that the underlying cipher mode and cipher type are
234 * supported by the underlying PSA Crypto implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
236 if (alg == 0) {
237 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
238 }
239 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
240 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
241 }
Hanno Becker6118e432018-11-09 16:47:20 +0000242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
246 if (cipher_psa == NULL) {
247 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
248 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000249 cipher_psa->alg = alg;
250 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000251 ctx->cipher_info = cipher_info;
252 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000254}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200255#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000256#endif /* MBEDTLS_USE_PSA_CRYPTO */
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
259 const unsigned char *key,
260 int key_bitlen,
261 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262{
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100264 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 }
266 if (ctx->cipher_info == NULL) {
267 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
268 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000270#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000272 mbedtls_cipher_context_psa * const cipher_psa =
273 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276
277 psa_status_t status;
278 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000280
281 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (key_bitlen % 8 != 0) {
283 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
284 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000285
286 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
288 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
289 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000290
Andrzej Kurekc7509322019-01-08 09:36:01 -0500291 key_type = mbedtls_psa_translate_cipher_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 ctx->cipher_info->type);
293 if (key_type == 0) {
294 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
295 }
296 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000297
298 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500299 * (encrypt vs. decrypt): it is possible to setup a key for encryption
300 * and use it for AEAD decryption. Until tests relying on this
301 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 psa_set_key_usage_flags(&attributes,
303 /* mbedtls_psa_translate_cipher_operation( operation ); */
304 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
305 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_import_key(&attributes, key, key_bytelen,
308 &cipher_psa->slot);
309 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200310 case PSA_SUCCESS:
311 break;
312 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200314 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200316 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200318 }
319 /* Indicate that we own the key slot and need to
320 * destroy it in mbedtls_cipher_free(). */
321 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000322
323 ctx->key_bitlen = key_bitlen;
324 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000326 }
327#endif /* MBEDTLS_USE_PSA_CRYPTO */
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100330 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200332 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200333
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200334 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 ctx->operation = operation;
336
Paul Bakker343a8702011-06-09 14:27:58 +0000337 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100338 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100342 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100344 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000346 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100349 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 ctx->key_bitlen);
351 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
357 const unsigned char *iv,
358 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200360 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ctx->cipher_info == NULL) {
363 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
364 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000365#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000367 /* While PSA Crypto has an API for multipart
368 * operations, we currently don't make it
369 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000371 }
372#endif /* MBEDTLS_USE_PSA_CRYPTO */
373
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200374 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
376 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
377 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200380 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100382 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200383
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200384 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (actual_iv_size > iv_len) {
386 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
387 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200388 }
389
Daniel Kingbd920622016-05-15 19:56:20 -0300390#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100392 /* Even though the actual_iv_size is overwritten with a correct value
393 * of 12 from the cipher info, return an error to indicate that
394 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (iv_len != 12) {
396 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
397 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
400 iv,
401 0U)) { /* Initial counter value */
402 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300403 }
404 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100405#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
407 iv_len != 12) {
408 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
409 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100410#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300411#endif
412
Gilles Peskine295fc132021-04-15 18:32:23 +0200413#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
415 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
416 ctx->operation,
417 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200418 }
419#endif
420
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200421#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200423 int set_lengths_result;
424 int ccm_star_mode;
425
426 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 (mbedtls_ccm_context *) ctx->cipher_ctx,
428 0, 0, 0);
429 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200430 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200434 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200436 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200438 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
442 ccm_star_mode,
443 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200444 }
445#endif
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (actual_iv_size != 0) {
448 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300449 ctx->iv_size = actual_iv_size;
450 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200453}
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200456{
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ctx->cipher_info == NULL) {
458 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
459 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200460
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000461#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000463 /* We don't support resetting PSA-based
464 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000466 }
467#endif /* MBEDTLS_USE_PSA_CRYPTO */
468
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 ctx->unprocessed_len = 0;
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200472}
473
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200474#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100475int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
476 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200477{
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (ctx->cipher_info == NULL) {
479 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
480 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200481
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000482#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000484 /* While PSA Crypto has an API for multipart
485 * operations, we currently don't make it
486 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000488 }
489#endif /* MBEDTLS_USE_PSA_CRYPTO */
490
Daniel King8fe47012016-05-17 20:33:28 -0300491#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
493 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
494 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200495 }
Daniel King8fe47012016-05-17 20:33:28 -0300496#endif
497
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200498#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300500 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200501 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200504 ? MBEDTLS_CHACHAPOLY_ENCRYPT
505 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
508 ctx->iv,
509 mode);
510 if (result != 0) {
511 return result;
512 }
Daniel King8fe47012016-05-17 20:33:28 -0300513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
515 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300516 }
517#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200521#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
524 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525{
Janos Follath24eed8d2019-11-22 13:21:35 +0000526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500527 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (ctx->cipher_info == NULL) {
530 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
531 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000535 /* While PSA Crypto has an API for multipart
536 * operations, we currently don't make it
537 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000539 }
540#endif /* MBEDTLS_USE_PSA_CRYPTO */
541
Paul Bakker6c212762013-12-16 15:24:50 +0100542 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 block_size = mbedtls_cipher_get_block_size(ctx);
544 if (0 == block_size) {
545 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100546 }
Paul Bakker6c212762013-12-16 15:24:50 +0100547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
549 if (ilen != block_size) {
550 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
551 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200552
553 *olen = ilen;
554
Dave Rodgmande3de772023-06-24 12:51:06 +0100555 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 ctx->operation, input, output))) {
557 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200558 }
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200561 }
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
565 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
566 input, ilen,
567 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200568 }
569#endif
570
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200571#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
573 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
574 input, ilen,
575 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200576 }
577#endif
578
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200579#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200581 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
583 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200584 }
585#endif
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (input == output &&
588 (ctx->unprocessed_len != 0 || ilen % block_size)) {
589 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100590 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200594 size_t copy_len = 0;
595
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596 /*
597 * If there is not enough data for a full block, cache it.
598 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
600 ilen <= block_size - ctx->unprocessed_len) ||
601 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
602 ilen < block_size - ctx->unprocessed_len) ||
603 (ctx->operation == MBEDTLS_ENCRYPT &&
604 ilen < block_size - ctx->unprocessed_len)) {
605 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
606 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
608 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 }
611
612 /*
613 * Process cached data first
614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100616 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
619 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620
Dave Rodgmande3de772023-06-24 12:51:06 +0100621 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 ctx->operation, block_size, ctx->iv,
623 ctx->unprocessed_data, output))) {
624 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 }
626
Janos Follath98e28a72016-05-31 14:03:54 +0100627 *olen += block_size;
628 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629 ctx->unprocessed_len = 0;
630
631 input += copy_len;
632 ilen -= copy_len;
633 }
634
635 /*
636 * Cache final, incomplete block
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700639 /* Encryption: only cache partial blocks
640 * Decryption w/ padding: always keep at least one whole block
641 * Decryption w/o padding: only cache partial blocks
642 */
Janos Follath98e28a72016-05-31 14:03:54 +0100643 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700645 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100647 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700648 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
651 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
653 ctx->unprocessed_len += copy_len;
654 ilen -= copy_len;
655 }
656
657 /*
658 * Process remaining full blocks
659 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100661 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 ctx->operation, ilen, ctx->iv, input,
663 output))) {
664 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000665 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200666
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 *olen += ilen;
668 }
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100676 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 ctx->operation, ilen,
678 &ctx->unprocessed_len, ctx->iv,
679 input, output))) {
680 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000681 }
682
683 *olen = ilen;
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000688
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100689#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100691 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 ilen, &ctx->unprocessed_len, ctx->iv,
693 input, output))) {
694 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100695 }
696
697 *olen = ilen;
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100700 }
701#endif /* MBEDTLS_CIPHER_MODE_OFB */
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100705 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ilen, &ctx->unprocessed_len, ctx->iv,
707 ctx->unprocessed_data, input, output))) {
708 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000709 }
710
711 *olen = ilen;
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000714 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000716
Jaeden Ameroc6539902018-04-30 17:17:41 +0100717#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
719 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100720 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100722 }
723
Dave Rodgmande3de772023-06-24 12:51:06 +0100724 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 ctx->operation, ilen, ctx->iv, input, output);
726 if (ret != 0) {
727 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100728 }
729
730 *olen = ilen;
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100733 }
734#endif /* MBEDTLS_CIPHER_MODE_XTS */
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100738 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 ilen, input, output))) {
740 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200741 }
742
743 *olen = ilen;
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200746 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750}
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
753#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200754/*
755 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
756 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757static void add_pkcs_padding(unsigned char *output, size_t output_len,
758 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000759{
Paul Bakker23986e52011-04-24 08:57:21 +0000760 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100761 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000764 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766}
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int get_pkcs_padding(unsigned char *input, size_t input_len,
769 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100771 size_t i, pad_idx;
772 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (NULL == input || NULL == data_len) {
775 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
776 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000777
778 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000779 *data_len = input_len - padding_len;
780
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100781 /* Avoid logical || since it results in a branch */
782 bad |= padding_len > input_len;
783 bad |= padding_len == 0;
784
785 /* The number of bytes checked must be independent of padding_len,
786 * so pick input_len, which is usually 8 or 16 (one block) */
787 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 for (i = 0; i < input_len; i++) {
789 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
790 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200797/*
798 * One and zeros padding: fill with 80 00 ... 00
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800static void add_one_and_zeros_padding(unsigned char *output,
801 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200802{
803 size_t padding_len = output_len - data_len;
804 unsigned char i = 0;
805
806 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200808 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810}
811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
813 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200814{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100815 size_t i;
816 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (NULL == input || NULL == data_len) {
819 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
820 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200821
Micha Krausba8316f2017-12-23 23:40:08 +0100822 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100823 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100825 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 done |= (input[i - 1] != 0);
827 *data_len |= (i - 1) * (done != prev_done);
828 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100829 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200832
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200837/*
838 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840static void add_zeros_and_len_padding(unsigned char *output,
841 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200842{
843 size_t padding_len = output_len - data_len;
844 unsigned char i = 0;
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200847 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200849 output[output_len - 1] = (unsigned char) padding_len;
850}
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
853 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200854{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100855 size_t i, pad_idx;
856 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (NULL == input || NULL == data_len) {
859 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
860 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200861
862 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200863 *data_len = input_len - padding_len;
864
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100865 /* Avoid logical || since it results in a branch */
866 bad |= padding_len > input_len;
867 bad |= padding_len == 0;
868
869 /* The number of bytes checked must be independent of padding_len */
870 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 for (i = 0; i < input_len - 1; i++) {
872 bad |= input[i] * (i >= pad_idx);
873 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200880/*
881 * Zero padding: fill with 00 ... 00
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static void add_zeros_padding(unsigned char *output,
884 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200886 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200889 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891}
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893static int get_zeros_padding(unsigned char *input, size_t input_len,
894 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100896 size_t i;
897 unsigned char done = 0, prev_done;
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (NULL == input || NULL == data_len) {
900 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100901 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 *data_len = 0;
904 for (i = input_len; i > 0; i--) {
905 prev_done = done;
906 done |= (input[i-1] != 0);
907 *data_len |= i * (done != prev_done);
908 }
909
910 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200913
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200914/*
915 * No padding: don't pad :)
916 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200918 * but a trivial get_padding function
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920static int get_no_padding(unsigned char *input, size_t input_len,
921 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200922{
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (NULL == input || NULL == data_len) {
924 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
925 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200926
927 *data_len = input_len;
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
934 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935{
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (ctx->cipher_info == NULL) {
937 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
938 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000939
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000940#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000942 /* While PSA Crypto has an API for multipart
943 * operations, we currently don't make it
944 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000946 }
947#endif /* MBEDTLS_USE_PSA_CRYPTO */
948
Paul Bakker8123e9d2011-01-06 15:37:30 +0000949 *olen = 0;
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100952 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
954 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200955 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100956 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
958 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000959 }
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
962 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
963 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
967 if (ctx->unprocessed_len != 0) {
968 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
969 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200972 }
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200976 int ret = 0;
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200979 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (NULL == ctx->add_padding) {
981 if (0 != ctx->unprocessed_len) {
982 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
983 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200986 }
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
989 ctx->unprocessed_len);
990 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200991 /*
992 * For decrypt operations, expect a full block,
993 * or an empty block if no padding
994 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
996 return 0;
997 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001000 }
1001
1002 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001003 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 ctx->operation,
1005 mbedtls_cipher_get_block_size(ctx),
1006 ctx->iv,
1007 ctx->unprocessed_data, output))) {
1008 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001009 }
1010
1011 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (MBEDTLS_DECRYPT == ctx->operation) {
1013 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1014 olen);
1015 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016
1017 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 *olen = mbedtls_cipher_get_block_size(ctx);
1019 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001021#else
1022 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001026}
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001029int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1030 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1033 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001034 }
1035
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001036#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001038 /* While PSA Crypto knows about CBC padding
1039 * schemes, we currently don't make them
1040 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 if (mode != MBEDTLS_PADDING_NONE) {
1042 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1043 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001046 }
1047#endif /* MBEDTLS_USE_PSA_CRYPTO */
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 case MBEDTLS_PADDING_PKCS7:
1052 ctx->add_padding = add_pkcs_padding;
1053 ctx->get_padding = get_pkcs_padding;
1054 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1058 ctx->add_padding = add_one_and_zeros_padding;
1059 ctx->get_padding = get_one_and_zeros_padding;
1060 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1064 ctx->add_padding = add_zeros_and_len_padding;
1065 ctx->get_padding = get_zeros_and_len_padding;
1066 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 case MBEDTLS_PADDING_ZEROS:
1070 ctx->add_padding = add_zeros_padding;
1071 ctx->get_padding = get_zeros_padding;
1072 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001073#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 case MBEDTLS_PADDING_NONE:
1075 ctx->add_padding = NULL;
1076 ctx->get_padding = get_no_padding;
1077 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 default:
1080 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001081 }
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001086
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001087#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001088int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1089 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001090{
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ctx->cipher_info == NULL) {
1092 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1093 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (MBEDTLS_ENCRYPT != ctx->operation) {
1096 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1097 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001098
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001101 /* While PSA Crypto has an API for multipart
1102 * operations, we currently don't make it
1103 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001105 }
1106#endif /* MBEDTLS_USE_PSA_CRYPTO */
1107
Daniel King8fe47012016-05-17 20:33:28 -03001108#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001110 size_t output_length;
1111 /* The code here doesn't yet support alternative implementations
1112 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1114 NULL, 0, &output_length,
1115 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001116 }
Daniel King8fe47012016-05-17 20:33:28 -03001117#endif
1118
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001119#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001121 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (tag_len != 16U) {
1123 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1124 }
Daniel King8fe47012016-05-17 20:33:28 -03001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return mbedtls_chachapoly_finish(
1127 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001128 }
1129#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001132}
Paul Bakker9af723c2014-05-01 13:03:14 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1135 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001136{
Daniel King8fe47012016-05-17 20:33:28 -03001137 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (ctx->cipher_info == NULL) {
1141 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1142 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (MBEDTLS_DECRYPT != ctx->operation) {
1145 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001147
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001148#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001150 /* While PSA Crypto has an API for multipart
1151 * operations, we currently don't make it
1152 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001154 }
1155#endif /* MBEDTLS_USE_PSA_CRYPTO */
1156
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001157 /* Status to return on a non-authenticated algorithm. */
1158 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001159
Daniel King8fe47012016-05-17 20:33:28 -03001160#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001162 size_t output_length;
1163 /* The code here doesn't yet support alternative implementations
1164 * that can delay up to a block of output. */
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (tag_len > sizeof(check_tag)) {
1167 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1168 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (0 != (ret = mbedtls_gcm_finish(
1171 (mbedtls_gcm_context *) ctx->cipher_ctx,
1172 NULL, 0, &output_length,
1173 check_tag, tag_len))) {
1174 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001175 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001176
1177 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001179 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001180 goto exit;
1181 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001182 }
Daniel King8fe47012016-05-17 20:33:28 -03001183#endif /* MBEDTLS_GCM_C */
1184
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001185#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001187 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (tag_len != sizeof(check_tag)) {
1189 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1190 }
Daniel King8fe47012016-05-17 20:33:28 -03001191
Hanno Becker18597cd2018-11-09 16:36:33 +00001192 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1194 if (ret != 0) {
1195 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001196 }
1197
1198 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001200 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001201 goto exit;
1202 }
Daniel King8fe47012016-05-17 20:33:28 -03001203 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001204#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001205
Gilles Peskinecd742982021-12-13 16:57:47 +01001206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 mbedtls_platform_zeroize(check_tag, tag_len);
1208 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001209}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001210#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001211
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001212/*
1213 * Packet-oriented wrapper for non-AEAD modes
1214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1216 const unsigned char *iv, size_t iv_len,
1217 const unsigned char *input, size_t ilen,
1218 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001219{
Janos Follath24eed8d2019-11-22 13:21:35 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001221 size_t finish_olen;
1222
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001225 /* As in the non-PSA case, we don't check that
1226 * a key has been set. If not, the key slot will
1227 * still be in its default state of 0, which is
1228 * guaranteed to be invalid, hence the PSA-call
1229 * below will gracefully fail. */
1230 mbedtls_cipher_context_psa * const cipher_psa =
1231 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1232
1233 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001234 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001235 size_t part_len;
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (ctx->operation == MBEDTLS_DECRYPT) {
1238 status = psa_cipher_decrypt_setup(&cipher_op,
1239 cipher_psa->slot,
1240 cipher_psa->alg);
1241 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1242 status = psa_cipher_encrypt_setup(&cipher_op,
1243 cipher_psa->slot,
1244 cipher_psa->alg);
1245 } else {
1246 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001247 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001248
1249 /* In the following, we can immediately return on an error,
1250 * because the PSA Crypto API guarantees that cipher operations
1251 * are terminated by unsuccessful calls to psa_cipher_update(),
1252 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (status != PSA_SUCCESS) {
1254 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001255 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1258 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1259 if (status != PSA_SUCCESS) {
1260 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1261 }
1262 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_cipher_update(&cipher_op,
1265 input, ilen,
1266 output, ilen, olen);
1267 if (status != PSA_SUCCESS) {
1268 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1269 }
1270
1271 status = psa_cipher_finish(&cipher_op,
1272 output + *olen, ilen - *olen,
1273 &part_len);
1274 if (status != PSA_SUCCESS) {
1275 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1276 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001277
1278 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001280 }
1281#endif /* MBEDTLS_USE_PSA_CRYPTO */
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 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_reset(ctx)) != 0) {
1288 return ret;
1289 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1292 output, olen)) != 0) {
1293 return ret;
1294 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1297 &finish_olen)) != 0) {
1298 return ret;
1299 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001300
1301 *olen += finish_olen;
1302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001304}
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001307/*
TRodziewicz18efb732021-04-29 23:12:19 +02001308 * Packet-oriented encryption for AEAD modes: internal function used by
1309 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1312 const unsigned char *iv, size_t iv_len,
1313 const unsigned char *ad, size_t ad_len,
1314 const unsigned char *input, size_t ilen,
1315 unsigned char *output, size_t *olen,
1316 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001317{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001318#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001320 /* As in the non-PSA case, we don't check that
1321 * a key has been set. If not, the key slot will
1322 * still be in its default state of 0, which is
1323 * guaranteed to be invalid, hence the PSA-call
1324 * below will gracefully fail. */
1325 mbedtls_cipher_context_psa * const cipher_psa =
1326 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1327
1328 psa_status_t status;
1329
1330 /* PSA Crypto API always writes the authentication tag
1331 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (output == NULL || tag != output + ilen) {
1333 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1334 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 status = psa_aead_encrypt(cipher_psa->slot,
1337 cipher_psa->alg,
1338 iv, iv_len,
1339 ad, ad_len,
1340 input, ilen,
1341 output, ilen + tag_len, olen);
1342 if (status != PSA_SUCCESS) {
1343 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1344 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001345
1346 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001348 }
1349#endif /* MBEDTLS_USE_PSA_CRYPTO */
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1355 ilen, iv, iv_len, ad, ad_len,
1356 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_GCM_C */
1359#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001361 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1363 iv, iv_len, ad, ad_len, input, output,
1364 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001367#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001369 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001370 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 (tag_len != 16U)) {
1372 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001373 }
1374
1375 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1377 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001378 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001379#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001382}
1383
1384/*
TRodziewicz18efb732021-04-29 23:12:19 +02001385 * Packet-oriented encryption for AEAD modes: internal function used by
1386 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1389 const unsigned char *iv, size_t iv_len,
1390 const unsigned char *ad, size_t ad_len,
1391 const unsigned char *input, size_t ilen,
1392 unsigned char *output, size_t *olen,
1393 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001394{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001397 /* As in the non-PSA case, we don't check that
1398 * a key has been set. If not, the key slot will
1399 * still be in its default state of 0, which is
1400 * guaranteed to be invalid, hence the PSA-call
1401 * below will gracefully fail. */
1402 mbedtls_cipher_context_psa * const cipher_psa =
1403 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1404
1405 psa_status_t status;
1406
1407 /* PSA Crypto API always writes the authentication tag
1408 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (input == NULL || tag != input + ilen) {
1410 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1411 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 status = psa_aead_decrypt(cipher_psa->slot,
1414 cipher_psa->alg,
1415 iv, iv_len,
1416 ad, ad_len,
1417 input, ilen + tag_len,
1418 output, ilen, olen);
1419 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1420 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1421 } else if (status != PSA_SUCCESS) {
1422 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1423 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001426 }
1427#endif /* MBEDTLS_USE_PSA_CRYPTO */
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001432
1433 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1435 iv, iv_len, ad, ad_len,
1436 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_GCM_C */
1445#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001448
1449 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1451 iv, iv_len, ad, ad_len,
1452 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001461#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001464
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001465 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001466 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 (tag_len != 16U)) {
1468 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001469 }
1470
1471 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1473 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001476 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 }
Daniel King8fe47012016-05-17 20:33:28 -03001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001480 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001481#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001486
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001487#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1488/*
1489 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1492 const unsigned char *iv, size_t iv_len,
1493 const unsigned char *ad, size_t ad_len,
1494 const unsigned char *input, size_t ilen,
1495 unsigned char *output, size_t output_len,
1496 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001497{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001498#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001500#if defined(MBEDTLS_USE_PSA_CRYPTO)
1501 ctx->psa_enabled == 0 &&
1502#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1504 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1505 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1506 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001507
1508 /* There is no iv, tag or ad associated with KW and KWP,
1509 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1511 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1512 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001513
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001514 (void) iv;
1515 (void) ad;
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1518 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001519 }
1520#endif /* MBEDTLS_NIST_KW_C */
1521
1522#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1523 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (output_len < ilen + tag_len) {
1525 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1526 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1529 input, ilen, output, olen,
1530 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001531 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001533#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001535#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1536}
1537
1538/*
1539 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1542 const unsigned char *iv, size_t iv_len,
1543 const unsigned char *ad, size_t ad_len,
1544 const unsigned char *input, size_t ilen,
1545 unsigned char *output, size_t output_len,
1546 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001547{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001548#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001550#if defined(MBEDTLS_USE_PSA_CRYPTO)
1551 ctx->psa_enabled == 0 &&
1552#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1554 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1555 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1556 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001557
1558 /* There is no iv, tag or ad associated with KW and KWP,
1559 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1561 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1562 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001563
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001564 (void) iv;
1565 (void) ad;
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1568 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569 }
1570#endif /* MBEDTLS_NIST_KW_C */
1571
1572#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1573 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ilen < tag_len || output_len < ilen - tag_len) {
1575 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1576 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1579 input, ilen - tag_len, output, olen,
1580 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001581#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001583#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1584}
1585#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_CIPHER_C */