blob: 5470dcfac0985cb20f3bcee5c3048cef63173008 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinef08ca832023-09-12 19:21:54 +02004 * \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"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020029#include "mbedtls/cipher_internal.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 Mezeie24dea82021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Dave Rodgman9f3f73d2023-09-20 14:29:45 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Hanno Becker4ccfc402018-11-09 16:10:57 +000058#if defined(MBEDTLS_USE_PSA_CRYPTO)
59#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000060#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000061#endif /* MBEDTLS_USE_PSA_CRYPTO */
62
Jack Lloydffdf2882019-03-07 17:00:32 -050063#if defined(MBEDTLS_NIST_KW_C)
64#include "mbedtls/nist_kw.h"
65#endif
66
Simon Butcher327398a2016-10-05 14:09:11 +010067#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069#define CIPHER_VALIDATE_RET(cond) \
70 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)
71#define CIPHER_VALIDATE(cond) \
72 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020074static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
137 if (def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200138 def->info->key_bitlen == (unsigned) key_bitlen &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 CIPHER_VALIDATE(ctx != NULL);
150 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151}
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200154{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200156 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200158
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000159#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (ctx->psa_enabled == 1) {
161 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000162 mbedtls_cipher_context_psa * const cipher_psa =
163 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000166 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000168 }
169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
171 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000172 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000175 return;
176 }
177#endif /* MBEDTLS_USE_PSA_CRYPTO */
178
Simon Butcher327398a2016-10-05 14:09:11 +0100179#if defined(MBEDTLS_CMAC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (ctx->cmac_ctx) {
181 mbedtls_platform_zeroize(ctx->cmac_ctx,
182 sizeof(mbedtls_cmac_context_t));
183 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100184 }
185#endif
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (ctx->cipher_ctx) {
188 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
189 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200192}
193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
195 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 CIPHER_VALIDATE_RET(ctx != NULL);
198 if (cipher_info == NULL) {
199 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
200 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
205 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
206 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207
208 ctx->cipher_info = cipher_info;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200211 /*
212 * Ignore possible errors caused by a cipher mode that doesn't use padding
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200216#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222}
223
Hanno Becker4ccfc402018-11-09 16:10:57 +0000224#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
226 const mbedtls_cipher_info_t *cipher_info,
227 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000228{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000229 psa_algorithm_t alg;
230 mbedtls_cipher_context_psa *cipher_psa;
231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 if (NULL == cipher_info || NULL == ctx) {
233 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
234 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000235
Hanno Becker4ee7e762018-11-17 22:00:38 +0000236 /* Check that the underlying cipher mode and cipher type are
237 * supported by the underlying PSA Crypto implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
239 if (alg == 0) {
240 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
241 }
242 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
243 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
244 }
Hanno Becker6118e432018-11-09 16:47:20 +0000245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
249 if (cipher_psa == NULL) {
250 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
251 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000252 cipher_psa->alg = alg;
253 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000254 ctx->cipher_info = cipher_info;
255 ctx->psa_enabled = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000257}
258#endif /* MBEDTLS_USE_PSA_CRYPTO */
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
261 const unsigned char *key,
262 int key_bitlen,
263 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 CIPHER_VALIDATE_RET(ctx != NULL);
266 CIPHER_VALIDATE_RET(key != NULL);
267 CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
268 operation == MBEDTLS_DECRYPT);
269 if (ctx->cipher_info == NULL) {
270 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
271 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000273#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000275 mbedtls_cipher_context_psa * const cipher_psa =
276 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000279
280 psa_status_t status;
281 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000283
284 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 if (key_bitlen % 8 != 0) {
286 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
287 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000288
289 /* Don't allow keys to be set multiple times. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
291 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
292 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000293
Andrzej Kurekc7509322019-01-08 09:36:01 -0500294 key_type = mbedtls_psa_translate_cipher_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 ctx->cipher_info->type);
296 if (key_type == 0) {
297 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
298 }
299 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000300
301 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500302 * (encrypt vs. decrypt): it is possible to setup a key for encryption
303 * and use it for AEAD decryption. Until tests relying on this
304 * are changed, allow any usage in PSA. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 psa_set_key_usage_flags(&attributes,
306 /* mbedtls_psa_translate_cipher_operation( operation ); */
307 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
308 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 status = psa_import_key(&attributes, key, key_bytelen,
311 &cipher_psa->slot);
312 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200313 case PSA_SUCCESS:
314 break;
315 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200317 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200319 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200321 }
322 /* Indicate that we own the key slot and need to
323 * destroy it in mbedtls_cipher_free(). */
324 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000325
326 ctx->key_bitlen = key_bitlen;
327 ctx->operation = operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000329 }
330#endif /* MBEDTLS_USE_PSA_CRYPTO */
331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
333 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
334 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200335 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200336
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200337 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338 ctx->operation = operation;
339
Paul Bakker343a8702011-06-09 14:27:58 +0000340 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100341 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100345 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
347 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
348 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000349 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 if (MBEDTLS_DECRYPT == operation) {
352 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
353 ctx->key_bitlen);
354 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357}
358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
360 const unsigned char *iv,
361 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200363 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 CIPHER_VALIDATE_RET(ctx != NULL);
366 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
367 if (ctx->cipher_info == NULL) {
368 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
369 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000370#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000372 /* While PSA Crypto has an API for multipart
373 * operations, we currently don't make it
374 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000376 }
377#endif /* MBEDTLS_USE_PSA_CRYPTO */
378
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200379 /* avoid buffer overflow in ctx->iv */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
381 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
382 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200385 actual_iv_size = iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 } else {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200387 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200388
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200389 /* avoid reading past the end of input buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 if (actual_iv_size > iv_len) {
391 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
392 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200393 }
394
Daniel Kingbd920622016-05-15 19:56:20 -0300395#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100397 /* Even though the actual_iv_size is overwritten with a correct value
398 * of 12 from the cipher info, return an error to indicate that
399 * the input iv_len is wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 if (iv_len != 12) {
401 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
402 }
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
405 iv,
406 0U)) { /* Initial counter value */
407 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300408 }
409 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100410#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
412 iv_len != 12) {
413 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
414 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100415#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300416#endif
417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if (actual_iv_size != 0) {
419 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300420 ctx->iv_size = actual_iv_size;
421 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200424}
425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200427{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 CIPHER_VALIDATE_RET(ctx != NULL);
429 if (ctx->cipher_info == NULL) {
430 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
431 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200432
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000433#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000435 /* We don't support resetting PSA-based
436 * cipher contexts, yet. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000438 }
439#endif /* MBEDTLS_USE_PSA_CRYPTO */
440
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 ctx->unprocessed_len = 0;
442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200444}
445
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200446#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
448 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 CIPHER_VALIDATE_RET(ctx != NULL);
451 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
452 if (ctx->cipher_info == NULL) {
453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
454 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200455
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000456#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000458 /* While PSA Crypto has an API for multipart
459 * operations, we currently don't make it
460 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000462 }
463#endif /* MBEDTLS_USE_PSA_CRYPTO */
464
Daniel King8fe47012016-05-17 20:33:28 -0300465#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
467 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
468 ctx->iv, ctx->iv_size, ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200469 }
Daniel King8fe47012016-05-17 20:33:28 -0300470#endif
471
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200472#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300474 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200475 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200478 ? MBEDTLS_CHACHAPOLY_ENCRYPT
479 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
482 ctx->iv,
483 mode);
484 if (result != 0) {
485 return result;
486 }
Daniel King8fe47012016-05-17 20:33:28 -0300487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
489 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300490 }
491#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200495#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
498 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500501 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 CIPHER_VALIDATE_RET(ctx != NULL);
504 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
505 CIPHER_VALIDATE_RET(output != NULL);
506 CIPHER_VALIDATE_RET(olen != NULL);
507 if (ctx->cipher_info == NULL) {
508 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
509 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000511#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000513 /* While PSA Crypto has an API for multipart
514 * operations, we currently don't make it
515 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000517 }
518#endif /* MBEDTLS_USE_PSA_CRYPTO */
519
Paul Bakker6c212762013-12-16 15:24:50 +0100520 *olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 block_size = mbedtls_cipher_get_block_size(ctx);
522 if (0 == block_size) {
523 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100524 }
Paul Bakker6c212762013-12-16 15:24:50 +0100525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
527 if (ilen != block_size) {
528 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
529 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200530
531 *olen = ilen;
532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
534 ctx->operation, input, output))) {
535 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200536 }
537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200543 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
545 output);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200546 }
547#endif
548
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200549#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200551 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
553 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200554 }
555#endif
556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if (input == output &&
558 (ctx->unprocessed_len != 0 || ilen % block_size)) {
559 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100560 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200564 size_t copy_len = 0;
565
Paul Bakker8123e9d2011-01-06 15:37:30 +0000566 /*
567 * If there is not enough data for a full block, cache it.
568 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
570 ilen <= block_size - ctx->unprocessed_len) ||
571 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
572 ilen < block_size - ctx->unprocessed_len) ||
573 (ctx->operation == MBEDTLS_ENCRYPT &&
574 ilen < block_size - ctx->unprocessed_len)) {
575 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
576 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577
578 ctx->unprocessed_len += ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 }
581
582 /*
583 * Process cached data first
584 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100586 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
589 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
592 ctx->operation, block_size, ctx->iv,
593 ctx->unprocessed_data, output))) {
594 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 }
596
Janos Follath98e28a72016-05-31 14:03:54 +0100597 *olen += block_size;
598 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599 ctx->unprocessed_len = 0;
600
601 input += copy_len;
602 ilen -= copy_len;
603 }
604
605 /*
606 * Cache final, incomplete block
607 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700609 /* Encryption: only cache partial blocks
610 * Decryption w/ padding: always keep at least one whole block
611 * Decryption w/o padding: only cache partial blocks
612 */
Janos Follath98e28a72016-05-31 14:03:54 +0100613 copy_len = ilen % block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700615 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100617 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700618 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
621 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622
623 ctx->unprocessed_len += copy_len;
624 ilen -= copy_len;
625 }
626
627 /*
628 * Process remaining full blocks
629 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 if (ilen) {
631 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
632 ctx->operation, ilen, ctx->iv, input,
633 output))) {
634 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200636
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 *olen += ilen;
638 }
639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
646 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
647 ctx->operation, ilen,
648 &ctx->unprocessed_len, ctx->iv,
649 input, output))) {
650 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000651 }
652
653 *olen = ilen;
654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000658
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100659#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
661 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
662 ilen, &ctx->unprocessed_len, ctx->iv,
663 input, output))) {
664 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100665 }
666
667 *olen = ilen;
668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100670 }
671#endif /* MBEDTLS_CIPHER_MODE_OFB */
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
675 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
676 ilen, &ctx->unprocessed_len, ctx->iv,
677 ctx->unprocessed_data, input, output))) {
678 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000679 }
680
681 *olen = ilen;
682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000686
Jaeden Ameroc6539902018-04-30 17:17:41 +0100687#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
689 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100690 /* We can only process an entire data unit at a time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100692 }
693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
695 ctx->operation, ilen, ctx->iv, input, output);
696 if (ret != 0) {
697 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100698 }
699
700 *olen = ilen;
701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100703 }
704#endif /* MBEDTLS_CIPHER_MODE_XTS */
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
708 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
709 ilen, input, output))) {
710 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200711 }
712
713 *olen = ilen;
714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720}
721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
723#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200724/*
725 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
726 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727static void add_pkcs_padding(unsigned char *output, size_t output_len,
728 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729{
Paul Bakker23986e52011-04-24 08:57:21 +0000730 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100731 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000734 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736}
737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738static int get_pkcs_padding(unsigned char *input, size_t input_len,
739 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100741 size_t i, pad_idx;
742 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 if (NULL == input || NULL == data_len) {
745 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
746 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
748 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749 *data_len = input_len - padding_len;
750
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100751 /* Avoid logical || since it results in a branch */
752 bad |= padding_len > input_len;
753 bad |= padding_len == 0;
754
755 /* The number of bytes checked must be independent of padding_len,
756 * so pick input_len, which is usually 8 or 16 (one block) */
757 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 for (i = 0; i < input_len; i++) {
Dave Rodgman9f3f73d2023-09-20 14:29:45 +0100759 size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
760 bad |= (input[i] ^ padding_len) & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 }
Dave Rodgman9f3f73d2023-09-20 14:29:45 +0100762 return (int) mbedtls_ct_uint_if(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200767/*
768 * One and zeros padding: fill with 80 00 ... 00
769 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770static void add_one_and_zeros_padding(unsigned char *output,
771 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200772{
773 size_t padding_len = output_len - data_len;
774 unsigned char i = 0;
775
776 output[data_len] = 0x80;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200778 output[data_len + i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200780}
781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
783 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200784{
Dave Rodgmandf254f62023-09-20 14:46:12 +0100785 unsigned int bad = 1;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 if (NULL == input || NULL == data_len) {
788 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
789 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200790
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100791 *data_len = 0;
Dave Rodgmandf254f62023-09-20 14:46:12 +0100792 size_t in_padding = ~0;
793
794 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
795 size_t is_nonzero = mbedtls_ct_uint_mask(input[i]);
796
797 size_t hit_first_nonzero = is_nonzero & in_padding;
798
799 *data_len = (*data_len & ~hit_first_nonzero) | ((size_t) i & hit_first_nonzero);
800
801 bad = mbedtls_ct_uint_if(hit_first_nonzero, !mbedtls_ct_size_bool_eq(input[i], 0x80), bad);
802
803 in_padding = in_padding & ~is_nonzero;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100804 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200805
Dave Rodgmandf254f62023-09-20 14:46:12 +0100806 return (int) mbedtls_ct_uint_if(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200811/*
812 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
813 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814static void add_zeros_and_len_padding(unsigned char *output,
815 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200816{
817 size_t padding_len = output_len - data_len;
818 unsigned char i = 0;
819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200821 output[data_len + i - 1] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200823 output[output_len - 1] = (unsigned char) padding_len;
824}
825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
827 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200828{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100829 size_t i, pad_idx;
830 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 if (NULL == input || NULL == data_len) {
833 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
834 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200835
836 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200837 *data_len = input_len - padding_len;
838
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100839 /* Avoid logical || since it results in a branch */
Dave Rodgman51773aa2023-09-20 14:51:21 +0100840 bad |= mbedtls_ct_size_mask_ge(padding_len, input_len + 1);
841 bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100842
843 /* The number of bytes checked must be independent of padding_len */
844 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman51773aa2023-09-20 14:51:21 +0100846 unsigned int mask = mbedtls_ct_size_mask_ge(i, pad_idx);
847 bad |= input[i] & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100849
Dave Rodgman51773aa2023-09-20 14:51:21 +0100850 return (int) mbedtls_ct_uint_if(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200855/*
856 * Zero padding: fill with 00 ... 00
857 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858static void add_zeros_padding(unsigned char *output,
859 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200860{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200861 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200864 output[i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200866}
867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868static int get_zeros_padding(unsigned char *input, size_t input_len,
869 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200870{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100871 size_t i;
872 unsigned char done = 0, prev_done;
873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874 if (NULL == input || NULL == data_len) {
875 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100876 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200877
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 *data_len = 0;
879 for (i = input_len; i > 0; i--) {
880 prev_done = done;
881 done |= (input[i-1] != 0);
Dave Rodgmanc1a17f52023-09-20 14:54:29 +0100882 size_t mask = mbedtls_ct_size_mask(done ^ prev_done);
883 *data_len |= i & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 }
885
886 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200889
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200890/*
891 * No padding: don't pad :)
892 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200894 * but a trivial get_padding function
895 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896static int get_no_padding(unsigned char *input, size_t input_len,
897 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200898{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899 if (NULL == input || NULL == data_len) {
900 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
901 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200902
903 *data_len = input_len;
904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
910 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000911{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 CIPHER_VALIDATE_RET(ctx != NULL);
913 CIPHER_VALIDATE_RET(output != NULL);
914 CIPHER_VALIDATE_RET(olen != NULL);
915 if (ctx->cipher_info == NULL) {
916 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
917 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000918
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000919#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000921 /* While PSA Crypto has an API for multipart
922 * operations, we currently don't make it
923 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000925 }
926#endif /* MBEDTLS_USE_PSA_CRYPTO */
927
Paul Bakker8123e9d2011-01-06 15:37:30 +0000928 *olen = 0;
929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100931 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
933 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100934 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
936 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000937 }
938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
940 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
941 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300942 }
943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
945 if (ctx->unprocessed_len != 0) {
946 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
947 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200950 }
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200954 int ret = 0;
955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200957 /* check for 'no padding' mode */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100958 if (NULL == ctx->add_padding) {
959 if (0 != ctx->unprocessed_len) {
960 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
961 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200964 }
965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
967 ctx->unprocessed_len);
968 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200969 /*
970 * For decrypt operations, expect a full block,
971 * or an empty block if no padding
972 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
974 return 0;
975 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 }
979
980 /* cipher block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
982 ctx->operation,
983 mbedtls_cipher_get_block_size(ctx),
984 ctx->iv,
985 ctx->unprocessed_data, output))) {
986 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987 }
988
989 /* Set output size for decryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100990 if (MBEDTLS_DECRYPT == ctx->operation) {
991 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
992 olen);
993 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000994
995 /* Set output size for encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 *olen = mbedtls_cipher_get_block_size(ctx);
997 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000998 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200999#else
1000 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001004}
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1008 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001009{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 CIPHER_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1013 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001014 }
1015
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001016#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001018 /* While PSA Crypto knows about CBC padding
1019 * schemes, we currently don't make them
1020 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021 if (mode != MBEDTLS_PADDING_NONE) {
1022 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1023 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001026 }
1027#endif /* MBEDTLS_USE_PSA_CRYPTO */
1028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031 case MBEDTLS_PADDING_PKCS7:
1032 ctx->add_padding = add_pkcs_padding;
1033 ctx->get_padding = get_pkcs_padding;
1034 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001037 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1038 ctx->add_padding = add_one_and_zeros_padding;
1039 ctx->get_padding = get_one_and_zeros_padding;
1040 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1044 ctx->add_padding = add_zeros_and_len_padding;
1045 ctx->get_padding = get_zeros_and_len_padding;
1046 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001049 case MBEDTLS_PADDING_ZEROS:
1050 ctx->add_padding = add_zeros_padding;
1051 ctx->get_padding = get_zeros_padding;
1052 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001053#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054 case MBEDTLS_PADDING_NONE:
1055 ctx->add_padding = NULL;
1056 ctx->get_padding = get_no_padding;
1057 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 default:
1060 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001061 }
1062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001066
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001067#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1069 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001070{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 CIPHER_VALIDATE_RET(ctx != NULL);
1072 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1073 if (ctx->cipher_info == NULL) {
1074 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1075 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 if (MBEDTLS_ENCRYPT != ctx->operation) {
1078 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1079 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001080
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001083 /* While PSA Crypto has an API for multipart
1084 * operations, we currently don't make it
1085 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001087 }
1088#endif /* MBEDTLS_USE_PSA_CRYPTO */
1089
Daniel King8fe47012016-05-17 20:33:28 -03001090#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1092 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1093 tag, tag_len);
Daniel King8fe47012016-05-17 20:33:28 -03001094 }
1095#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097#if defined(MBEDTLS_CHACHAPOLY_C)
1098 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1099 /* Don't allow truncated MAC for Poly1305 */
1100 if (tag_len != 16U) {
1101 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1102 }
1103
1104 return mbedtls_chachapoly_finish(
1105 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1106 }
1107#endif
1108
1109 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001110}
Paul Bakker9af723c2014-05-01 13:03:14 +02001111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1113 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001114{
Daniel King8fe47012016-05-17 20:33:28 -03001115 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 CIPHER_VALIDATE_RET(ctx != NULL);
1119 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1120 if (ctx->cipher_info == NULL) {
1121 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1122 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if (MBEDTLS_DECRYPT != ctx->operation) {
1125 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001126 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001127
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001130 /* While PSA Crypto has an API for multipart
1131 * operations, we currently don't make it
1132 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001134 }
1135#endif /* MBEDTLS_USE_PSA_CRYPTO */
1136
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001137 /* Status to return on a non-authenticated algorithm. It would make sense
1138 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1139 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1140 * unit tests assume 0. */
1141 ret = 0;
1142
Daniel King8fe47012016-05-17 20:33:28 -03001143#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1145 if (tag_len > sizeof(check_tag)) {
1146 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1147 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 if (0 != (ret = mbedtls_gcm_finish(
1150 (mbedtls_gcm_context *) ctx->cipher_ctx,
1151 check_tag, tag_len))) {
1152 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001153 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001154
1155 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001157 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001158 goto exit;
1159 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001160 }
Daniel King8fe47012016-05-17 20:33:28 -03001161#endif /* MBEDTLS_GCM_C */
1162
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001163#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001165 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if (tag_len != sizeof(check_tag)) {
1167 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1168 }
Daniel King8fe47012016-05-17 20:33:28 -03001169
Hanno Becker18597cd2018-11-09 16:36:33 +00001170 ret = mbedtls_chachapoly_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1172 if (ret != 0) {
1173 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001174 }
1175
1176 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001178 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001179 goto exit;
1180 }
Daniel King8fe47012016-05-17 20:33:28 -03001181 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001182#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001183
Gilles Peskinef9a05012021-12-13 16:57:47 +01001184exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 mbedtls_platform_zeroize(check_tag, tag_len);
1186 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001187}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001188#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001189
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001190/*
1191 * Packet-oriented wrapper for non-AEAD modes
1192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001193int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1194 const unsigned char *iv, size_t iv_len,
1195 const unsigned char *input, size_t ilen,
1196 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001197{
Janos Follath24eed8d2019-11-22 13:21:35 +00001198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001199 size_t finish_olen;
1200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 CIPHER_VALIDATE_RET(ctx != NULL);
1202 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1203 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1204 CIPHER_VALIDATE_RET(output != NULL);
1205 CIPHER_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001206
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001207#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001209 /* As in the non-PSA case, we don't check that
1210 * a key has been set. If not, the key slot will
1211 * still be in its default state of 0, which is
1212 * guaranteed to be invalid, hence the PSA-call
1213 * below will gracefully fail. */
1214 mbedtls_cipher_context_psa * const cipher_psa =
1215 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1216
1217 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001218 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001219 size_t part_len;
1220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001221 if (ctx->operation == MBEDTLS_DECRYPT) {
1222 status = psa_cipher_decrypt_setup(&cipher_op,
1223 cipher_psa->slot,
1224 cipher_psa->alg);
1225 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1226 status = psa_cipher_encrypt_setup(&cipher_op,
1227 cipher_psa->slot,
1228 cipher_psa->alg);
1229 } else {
1230 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001231 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001232
1233 /* In the following, we can immediately return on an error,
1234 * because the PSA Crypto API guarantees that cipher operations
1235 * are terminated by unsuccessful calls to psa_cipher_update(),
1236 * and by any call to psa_cipher_finish(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 if (status != PSA_SUCCESS) {
1238 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001239 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1242 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1243 if (status != PSA_SUCCESS) {
1244 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1245 }
1246 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 status = psa_cipher_update(&cipher_op,
1249 input, ilen,
1250 output, ilen, olen);
1251 if (status != PSA_SUCCESS) {
1252 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1253 }
1254
1255 status = psa_cipher_finish(&cipher_op,
1256 output + *olen, ilen - *olen,
1257 &part_len);
1258 if (status != PSA_SUCCESS) {
1259 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1260 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001261
1262 *olen += part_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001264 }
1265#endif /* MBEDTLS_USE_PSA_CRYPTO */
1266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1268 return ret;
1269 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1272 return ret;
1273 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1276 output, olen)) != 0) {
1277 return ret;
1278 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1281 &finish_olen)) != 0) {
1282 return ret;
1283 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001284
1285 *olen += finish_olen;
1286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001288}
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001291/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001292 * Packet-oriented encryption for AEAD modes: internal function shared by
1293 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001294 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1296 const unsigned char *iv, size_t iv_len,
1297 const unsigned char *ad, size_t ad_len,
1298 const unsigned char *input, size_t ilen,
1299 unsigned char *output, size_t *olen,
1300 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001301{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001302#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001304 /* As in the non-PSA case, we don't check that
1305 * a key has been set. If not, the key slot will
1306 * still be in its default state of 0, which is
1307 * guaranteed to be invalid, hence the PSA-call
1308 * below will gracefully fail. */
1309 mbedtls_cipher_context_psa * const cipher_psa =
1310 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1311
1312 psa_status_t status;
1313
1314 /* PSA Crypto API always writes the authentication tag
1315 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001316 if (output == NULL || tag != output + ilen) {
1317 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1318 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 status = psa_aead_encrypt(cipher_psa->slot,
1321 cipher_psa->alg,
1322 iv, iv_len,
1323 ad, ad_len,
1324 input, ilen,
1325 output, ilen + tag_len, olen);
1326 if (status != PSA_SUCCESS) {
1327 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1328 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001329
1330 *olen -= tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001332 }
1333#endif /* MBEDTLS_USE_PSA_CRYPTO */
1334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001337 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1339 ilen, iv, iv_len, ad, ad_len,
1340 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_GCM_C */
1343#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001345 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1347 iv, iv_len, ad, ad_len, input, output,
1348 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001351#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001353 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 if ((iv_len != ctx->cipher_info->iv_size) ||
1355 (tag_len != 16U)) {
1356 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001357 }
1358
1359 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1361 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001362 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001363#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001366}
1367
1368/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001369 * Packet-oriented encryption for AEAD modes: internal function shared by
1370 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1373 const unsigned char *iv, size_t iv_len,
1374 const unsigned char *ad, size_t ad_len,
1375 const unsigned char *input, size_t ilen,
1376 unsigned char *output, size_t *olen,
1377 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001378{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001379#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001380 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001381 /* As in the non-PSA case, we don't check that
1382 * a key has been set. If not, the key slot will
1383 * still be in its default state of 0, which is
1384 * guaranteed to be invalid, hence the PSA-call
1385 * below will gracefully fail. */
1386 mbedtls_cipher_context_psa * const cipher_psa =
1387 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1388
1389 psa_status_t status;
1390
1391 /* PSA Crypto API always writes the authentication tag
1392 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 if (input == NULL || tag != input + ilen) {
1394 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1395 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 status = psa_aead_decrypt(cipher_psa->slot,
1398 cipher_psa->alg,
1399 iv, iv_len,
1400 ad, ad_len,
1401 input, ilen + tag_len,
1402 output, ilen, olen);
1403 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1404 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1405 } else if (status != PSA_SUCCESS) {
1406 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1407 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001410 }
1411#endif /* MBEDTLS_USE_PSA_CRYPTO */
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001416
1417 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1419 iv, iv_len, ad, ad_len,
1420 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_GCM_C */
1429#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001432
1433 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1435 iv, iv_len, ad, ad_len,
1436 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001442 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001445#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001448
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001449 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 if ((iv_len != ctx->cipher_info->iv_size) ||
1451 (tag_len != 16U)) {
1452 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001453 }
1454
1455 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1457 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001460 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 }
Daniel King8fe47012016-05-17 20:33:28 -03001462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001464 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001465#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001467 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001468}
1469
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001470#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001471/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001472 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001473 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001474int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1475 const unsigned char *iv, size_t iv_len,
1476 const unsigned char *ad, size_t ad_len,
1477 const unsigned char *input, size_t ilen,
1478 unsigned char *output, size_t *olen,
1479 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001480{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 CIPHER_VALIDATE_RET(ctx != NULL);
1482 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1483 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1484 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1485 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1486 CIPHER_VALIDATE_RET(olen != NULL);
1487 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1490 input, ilen, output, olen,
1491 tag, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001492}
1493
1494/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001495 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001496 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1498 const unsigned char *iv, size_t iv_len,
1499 const unsigned char *ad, size_t ad_len,
1500 const unsigned char *input, size_t ilen,
1501 unsigned char *output, size_t *olen,
1502 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001503{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 CIPHER_VALIDATE_RET(ctx != NULL);
1505 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1506 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1507 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1508 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1509 CIPHER_VALIDATE_RET(olen != NULL);
1510 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1513 input, ilen, output, olen,
1514 tag, tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001515}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001516#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001518
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001519#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1520/*
1521 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1522 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001523int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1524 const unsigned char *iv, size_t iv_len,
1525 const unsigned char *ad, size_t ad_len,
1526 const unsigned char *input, size_t ilen,
1527 unsigned char *output, size_t output_len,
1528 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001529{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 CIPHER_VALIDATE_RET(ctx != NULL);
1531 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1532 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1533 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1534 CIPHER_VALIDATE_RET(output != NULL);
1535 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001536
1537#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001539#if defined(MBEDTLS_USE_PSA_CRYPTO)
1540 ctx->psa_enabled == 0 &&
1541#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1543 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1544 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1545 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001546
1547 /* There is no iv, tag or ad associated with KW and KWP,
1548 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1550 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1551 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001552
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001553 (void) iv;
1554 (void) ad;
1555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1557 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001558 }
1559#endif /* MBEDTLS_NIST_KW_C */
1560
1561#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1562 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 if (output_len < ilen + tag_len) {
1564 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1565 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1568 input, ilen, output, olen,
1569 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001570 *olen += tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001571 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001574#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1575}
1576
1577/*
1578 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1579 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1581 const unsigned char *iv, size_t iv_len,
1582 const unsigned char *ad, size_t ad_len,
1583 const unsigned char *input, size_t ilen,
1584 unsigned char *output, size_t output_len,
1585 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001586{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 CIPHER_VALIDATE_RET(ctx != NULL);
1588 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1589 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1590 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1591 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1592 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001593
1594#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001596#if defined(MBEDTLS_USE_PSA_CRYPTO)
1597 ctx->psa_enabled == 0 &&
1598#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1600 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1601 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1602 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001603
1604 /* There is no iv, tag or ad associated with KW and KWP,
1605 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1607 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1608 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001609
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001610 (void) iv;
1611 (void) ad;
1612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1614 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001615 }
1616#endif /* MBEDTLS_NIST_KW_C */
1617
1618#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1619 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 if (ilen < tag_len || output_len < ilen - tag_len) {
1621 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1622 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1625 input, ilen - tag_len, output, olen,
1626 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001627#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001629#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1630}
1631#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_CIPHER_C */