blob: 90145a587938d52f2d3b1b72ef8bb2a3be305383 [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 */
840 bad |= padding_len > input_len;
841 bad |= padding_len == 0;
842
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++) {
846 bad |= input[i] * (i >= pad_idx);
847 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200854/*
855 * Zero padding: fill with 00 ... 00
856 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857static void add_zeros_padding(unsigned char *output,
858 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200859{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200860 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200863 output[i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200865}
866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867static int get_zeros_padding(unsigned char *input, size_t input_len,
868 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200869{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100870 size_t i;
871 unsigned char done = 0, prev_done;
872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 if (NULL == input || NULL == data_len) {
874 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100875 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100877 *data_len = 0;
878 for (i = input_len; i > 0; i--) {
879 prev_done = done;
880 done |= (input[i-1] != 0);
881 *data_len |= i * (done != prev_done);
882 }
883
884 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200888/*
889 * No padding: don't pad :)
890 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200892 * but a trivial get_padding function
893 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894static int get_no_padding(unsigned char *input, size_t input_len,
895 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200896{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 if (NULL == input || NULL == data_len) {
898 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
899 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200900
901 *data_len = input_len;
902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100907int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
908 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000909{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 CIPHER_VALIDATE_RET(ctx != NULL);
911 CIPHER_VALIDATE_RET(output != NULL);
912 CIPHER_VALIDATE_RET(olen != NULL);
913 if (ctx->cipher_info == NULL) {
914 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
915 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000916
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000917#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000919 /* While PSA Crypto has an API for multipart
920 * operations, we currently don't make it
921 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000923 }
924#endif /* MBEDTLS_USE_PSA_CRYPTO */
925
Paul Bakker8123e9d2011-01-06 15:37:30 +0000926 *olen = 0;
927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100929 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
931 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100932 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100933 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
934 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000935 }
936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
938 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
939 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300940 }
941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
943 if (ctx->unprocessed_len != 0) {
944 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
945 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200948 }
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200952 int ret = 0;
953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200955 /* check for 'no padding' mode */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 if (NULL == ctx->add_padding) {
957 if (0 != ctx->unprocessed_len) {
958 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
959 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200962 }
963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
965 ctx->unprocessed_len);
966 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200967 /*
968 * For decrypt operations, expect a full block,
969 * or an empty block if no padding
970 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
972 return 0;
973 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000976 }
977
978 /* cipher block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
980 ctx->operation,
981 mbedtls_cipher_get_block_size(ctx),
982 ctx->iv,
983 ctx->unprocessed_data, output))) {
984 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000985 }
986
987 /* Set output size for decryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 if (MBEDTLS_DECRYPT == ctx->operation) {
989 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
990 olen);
991 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000992
993 /* Set output size for encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 *olen = mbedtls_cipher_get_block_size(ctx);
995 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200997#else
998 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002}
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1006 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001007{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008 CIPHER_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1011 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001012 }
1013
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001014#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001016 /* While PSA Crypto knows about CBC padding
1017 * schemes, we currently don't make them
1018 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 if (mode != MBEDTLS_PADDING_NONE) {
1020 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1021 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001024 }
1025#endif /* MBEDTLS_USE_PSA_CRYPTO */
1026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 case MBEDTLS_PADDING_PKCS7:
1030 ctx->add_padding = add_pkcs_padding;
1031 ctx->get_padding = get_pkcs_padding;
1032 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1036 ctx->add_padding = add_one_and_zeros_padding;
1037 ctx->get_padding = get_one_and_zeros_padding;
1038 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1042 ctx->add_padding = add_zeros_and_len_padding;
1043 ctx->get_padding = get_zeros_and_len_padding;
1044 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 case MBEDTLS_PADDING_ZEROS:
1048 ctx->add_padding = add_zeros_padding;
1049 ctx->get_padding = get_zeros_padding;
1050 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001051#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 case MBEDTLS_PADDING_NONE:
1053 ctx->add_padding = NULL;
1054 ctx->get_padding = get_no_padding;
1055 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 default:
1058 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001059 }
1060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001064
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001065#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1067 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001068{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 CIPHER_VALIDATE_RET(ctx != NULL);
1070 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1071 if (ctx->cipher_info == NULL) {
1072 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1073 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 if (MBEDTLS_ENCRYPT != ctx->operation) {
1076 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1077 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001078
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001079#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001081 /* While PSA Crypto has an API for multipart
1082 * operations, we currently don't make it
1083 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001085 }
1086#endif /* MBEDTLS_USE_PSA_CRYPTO */
1087
Daniel King8fe47012016-05-17 20:33:28 -03001088#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1090 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1091 tag, tag_len);
Daniel King8fe47012016-05-17 20:33:28 -03001092 }
1093#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095#if defined(MBEDTLS_CHACHAPOLY_C)
1096 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1097 /* Don't allow truncated MAC for Poly1305 */
1098 if (tag_len != 16U) {
1099 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1100 }
1101
1102 return mbedtls_chachapoly_finish(
1103 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1104 }
1105#endif
1106
1107 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001108}
Paul Bakker9af723c2014-05-01 13:03:14 +02001109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1111 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001112{
Daniel King8fe47012016-05-17 20:33:28 -03001113 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 CIPHER_VALIDATE_RET(ctx != NULL);
1117 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1118 if (ctx->cipher_info == NULL) {
1119 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1120 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 if (MBEDTLS_DECRYPT != ctx->operation) {
1123 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001124 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001125
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001127 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001128 /* While PSA Crypto has an API for multipart
1129 * operations, we currently don't make it
1130 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001131 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001132 }
1133#endif /* MBEDTLS_USE_PSA_CRYPTO */
1134
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001135 /* Status to return on a non-authenticated algorithm. It would make sense
1136 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1137 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1138 * unit tests assume 0. */
1139 ret = 0;
1140
Daniel King8fe47012016-05-17 20:33:28 -03001141#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1143 if (tag_len > sizeof(check_tag)) {
1144 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1145 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001147 if (0 != (ret = mbedtls_gcm_finish(
1148 (mbedtls_gcm_context *) ctx->cipher_ctx,
1149 check_tag, tag_len))) {
1150 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001151 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152
1153 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001155 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001156 goto exit;
1157 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001158 }
Daniel King8fe47012016-05-17 20:33:28 -03001159#endif /* MBEDTLS_GCM_C */
1160
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001161#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001163 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 if (tag_len != sizeof(check_tag)) {
1165 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1166 }
Daniel King8fe47012016-05-17 20:33:28 -03001167
Hanno Becker18597cd2018-11-09 16:36:33 +00001168 ret = mbedtls_chachapoly_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1170 if (ret != 0) {
1171 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001172 }
1173
1174 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001175 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001176 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001177 goto exit;
1178 }
Daniel King8fe47012016-05-17 20:33:28 -03001179 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001180#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001181
Gilles Peskinef9a05012021-12-13 16:57:47 +01001182exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 mbedtls_platform_zeroize(check_tag, tag_len);
1184 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001186#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001187
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001188/*
1189 * Packet-oriented wrapper for non-AEAD modes
1190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1192 const unsigned char *iv, size_t iv_len,
1193 const unsigned char *input, size_t ilen,
1194 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001195{
Janos Follath24eed8d2019-11-22 13:21:35 +00001196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001197 size_t finish_olen;
1198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 CIPHER_VALIDATE_RET(ctx != NULL);
1200 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1201 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1202 CIPHER_VALIDATE_RET(output != NULL);
1203 CIPHER_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001204
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001207 /* As in the non-PSA case, we don't check that
1208 * a key has been set. If not, the key slot will
1209 * still be in its default state of 0, which is
1210 * guaranteed to be invalid, hence the PSA-call
1211 * below will gracefully fail. */
1212 mbedtls_cipher_context_psa * const cipher_psa =
1213 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1214
1215 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001216 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001217 size_t part_len;
1218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 if (ctx->operation == MBEDTLS_DECRYPT) {
1220 status = psa_cipher_decrypt_setup(&cipher_op,
1221 cipher_psa->slot,
1222 cipher_psa->alg);
1223 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1224 status = psa_cipher_encrypt_setup(&cipher_op,
1225 cipher_psa->slot,
1226 cipher_psa->alg);
1227 } else {
1228 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001229 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001230
1231 /* In the following, we can immediately return on an error,
1232 * because the PSA Crypto API guarantees that cipher operations
1233 * are terminated by unsuccessful calls to psa_cipher_update(),
1234 * and by any call to psa_cipher_finish(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 if (status != PSA_SUCCESS) {
1236 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001237 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1240 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1241 if (status != PSA_SUCCESS) {
1242 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1243 }
1244 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 status = psa_cipher_update(&cipher_op,
1247 input, ilen,
1248 output, ilen, olen);
1249 if (status != PSA_SUCCESS) {
1250 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1251 }
1252
1253 status = psa_cipher_finish(&cipher_op,
1254 output + *olen, ilen - *olen,
1255 &part_len);
1256 if (status != PSA_SUCCESS) {
1257 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1258 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001259
1260 *olen += part_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001262 }
1263#endif /* MBEDTLS_USE_PSA_CRYPTO */
1264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1266 return ret;
1267 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1270 return ret;
1271 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1274 output, olen)) != 0) {
1275 return ret;
1276 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1279 &finish_olen)) != 0) {
1280 return ret;
1281 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001282
1283 *olen += finish_olen;
1284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001286}
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001289/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001290 * Packet-oriented encryption for AEAD modes: internal function shared by
1291 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001292 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001293static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1294 const unsigned char *iv, size_t iv_len,
1295 const unsigned char *ad, size_t ad_len,
1296 const unsigned char *input, size_t ilen,
1297 unsigned char *output, size_t *olen,
1298 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001299{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001300#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001302 /* As in the non-PSA case, we don't check that
1303 * a key has been set. If not, the key slot will
1304 * still be in its default state of 0, which is
1305 * guaranteed to be invalid, hence the PSA-call
1306 * below will gracefully fail. */
1307 mbedtls_cipher_context_psa * const cipher_psa =
1308 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1309
1310 psa_status_t status;
1311
1312 /* PSA Crypto API always writes the authentication tag
1313 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 if (output == NULL || tag != output + ilen) {
1315 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1316 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001318 status = psa_aead_encrypt(cipher_psa->slot,
1319 cipher_psa->alg,
1320 iv, iv_len,
1321 ad, ad_len,
1322 input, ilen,
1323 output, ilen + tag_len, olen);
1324 if (status != PSA_SUCCESS) {
1325 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1326 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001327
1328 *olen -= tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001330 }
1331#endif /* MBEDTLS_USE_PSA_CRYPTO */
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001335 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1337 ilen, iv, iv_len, ad, ad_len,
1338 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_GCM_C */
1341#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001343 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1345 iv, iv_len, ad, ad_len, input, output,
1346 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001349#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001351 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 if ((iv_len != ctx->cipher_info->iv_size) ||
1353 (tag_len != 16U)) {
1354 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001355 }
1356
1357 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1359 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001360 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001361#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001363 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001364}
1365
1366/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001367 * Packet-oriented encryption for AEAD modes: internal function shared by
1368 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001369 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1371 const unsigned char *iv, size_t iv_len,
1372 const unsigned char *ad, size_t ad_len,
1373 const unsigned char *input, size_t ilen,
1374 unsigned char *output, size_t *olen,
1375 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001376{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001377#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001379 /* As in the non-PSA case, we don't check that
1380 * a key has been set. If not, the key slot will
1381 * still be in its default state of 0, which is
1382 * guaranteed to be invalid, hence the PSA-call
1383 * below will gracefully fail. */
1384 mbedtls_cipher_context_psa * const cipher_psa =
1385 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1386
1387 psa_status_t status;
1388
1389 /* PSA Crypto API always writes the authentication tag
1390 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 if (input == NULL || tag != input + ilen) {
1392 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1393 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 status = psa_aead_decrypt(cipher_psa->slot,
1396 cipher_psa->alg,
1397 iv, iv_len,
1398 ad, ad_len,
1399 input, ilen + tag_len,
1400 output, ilen, olen);
1401 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1402 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1403 } else if (status != PSA_SUCCESS) {
1404 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1405 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001408 }
1409#endif /* MBEDTLS_USE_PSA_CRYPTO */
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001414
1415 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1417 iv, iv_len, ad, ad_len,
1418 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001422 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001425 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_GCM_C */
1427#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001430
1431 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001432 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1433 iv, iv_len, ad, ad_len,
1434 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001436 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001443#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001446
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001447 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 if ((iv_len != ctx->cipher_info->iv_size) ||
1449 (tag_len != 16U)) {
1450 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001451 }
1452
1453 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1455 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001458 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 }
Daniel King8fe47012016-05-17 20:33:28 -03001460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001462 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001463#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001466}
1467
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001468#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001469/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001470 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001471 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1473 const unsigned char *iv, size_t iv_len,
1474 const unsigned char *ad, size_t ad_len,
1475 const unsigned char *input, size_t ilen,
1476 unsigned char *output, size_t *olen,
1477 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001478{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 CIPHER_VALIDATE_RET(ctx != NULL);
1480 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1481 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1482 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1483 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1484 CIPHER_VALIDATE_RET(olen != NULL);
1485 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001487 return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1488 input, ilen, output, olen,
1489 tag, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001490}
1491
1492/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001493 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001494 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001495int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1496 const unsigned char *iv, size_t iv_len,
1497 const unsigned char *ad, size_t ad_len,
1498 const unsigned char *input, size_t ilen,
1499 unsigned char *output, size_t *olen,
1500 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001501{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 CIPHER_VALIDATE_RET(ctx != NULL);
1503 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1504 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1505 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1506 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1507 CIPHER_VALIDATE_RET(olen != NULL);
1508 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001510 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1511 input, ilen, output, olen,
1512 tag, tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001513}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001514#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001517#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1518/*
1519 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1520 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1522 const unsigned char *iv, size_t iv_len,
1523 const unsigned char *ad, size_t ad_len,
1524 const unsigned char *input, size_t ilen,
1525 unsigned char *output, size_t output_len,
1526 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001527{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 CIPHER_VALIDATE_RET(ctx != NULL);
1529 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1530 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1531 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1532 CIPHER_VALIDATE_RET(output != NULL);
1533 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001534
1535#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001537#if defined(MBEDTLS_USE_PSA_CRYPTO)
1538 ctx->psa_enabled == 0 &&
1539#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001540 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1541 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1542 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1543 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001544
1545 /* There is no iv, tag or ad associated with KW and KWP,
1546 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1548 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1549 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001550
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001551 (void) iv;
1552 (void) ad;
1553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1555 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001556 }
1557#endif /* MBEDTLS_NIST_KW_C */
1558
1559#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1560 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 if (output_len < ilen + tag_len) {
1562 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1563 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1566 input, ilen, output, olen,
1567 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001568 *olen += tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001570#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001571 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1573}
1574
1575/*
1576 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1577 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1579 const unsigned char *iv, size_t iv_len,
1580 const unsigned char *ad, size_t ad_len,
1581 const unsigned char *input, size_t ilen,
1582 unsigned char *output, size_t output_len,
1583 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001584{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001585 CIPHER_VALIDATE_RET(ctx != NULL);
1586 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1587 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1588 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1589 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1590 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001591
1592#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001593 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001594#if defined(MBEDTLS_USE_PSA_CRYPTO)
1595 ctx->psa_enabled == 0 &&
1596#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1598 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1599 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1600 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001601
1602 /* There is no iv, tag or ad associated with KW and KWP,
1603 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1605 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1606 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001607
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001608 (void) iv;
1609 (void) ad;
1610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1612 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001613 }
1614#endif /* MBEDTLS_NIST_KW_C */
1615
1616#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1617 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618 if (ilen < tag_len || output_len < ilen - tag_len) {
1619 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1620 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1623 input, ilen - tag_len, output, olen,
1624 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001625#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001626 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001627#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1628}
1629#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_CIPHER_C */