blob: e5598de42e80199d0bbbd61d2326f57e9e204b3d [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 */
Dave Rodgmane0ad9a42023-09-20 19:23:58 +0100752 bad |= ~mbedtls_ct_size_mask_ge(input_len, padding_len);
753 bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100754
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
Dave Rodgman1d523682023-09-20 16:26:49 +0100801 bad = mbedtls_ct_uint_if((unsigned int) hit_first_nonzero,
802 !mbedtls_ct_size_bool_eq(input[i], 0x80), bad);
Dave Rodgmandf254f62023-09-20 14:46:12 +0100803
804 in_padding = in_padding & ~is_nonzero;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100805 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200806
Dave Rodgmandf254f62023-09-20 14:46:12 +0100807 return (int) mbedtls_ct_uint_if(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200812/*
813 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
814 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815static void add_zeros_and_len_padding(unsigned char *output,
816 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200817{
818 size_t padding_len = output_len - data_len;
819 unsigned char i = 0;
820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200822 output[data_len + i - 1] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100823 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200824 output[output_len - 1] = (unsigned char) padding_len;
825}
826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
828 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200829{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100830 size_t i, pad_idx;
831 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 if (NULL == input || NULL == data_len) {
834 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
835 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200836
837 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200838 *data_len = input_len - padding_len;
839
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100840 /* Avoid logical || since it results in a branch */
Dave Rodgman51773aa2023-09-20 14:51:21 +0100841 bad |= mbedtls_ct_size_mask_ge(padding_len, input_len + 1);
842 bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100843
844 /* The number of bytes checked must be independent of padding_len */
845 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman1d523682023-09-20 16:26:49 +0100847 size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
Dave Rodgman51773aa2023-09-20 14:51:21 +0100848 bad |= input[i] & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100850
Dave Rodgman51773aa2023-09-20 14:51:21 +0100851 return (int) mbedtls_ct_uint_if(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200856/*
857 * Zero padding: fill with 00 ... 00
858 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859static void add_zeros_padding(unsigned char *output,
860 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200861{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200862 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200865 output[i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100866 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200867}
868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869static int get_zeros_padding(unsigned char *input, size_t input_len,
870 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200871{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100872 size_t i;
873 unsigned char done = 0, prev_done;
874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (NULL == input || NULL == data_len) {
876 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100877 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 *data_len = 0;
880 for (i = input_len; i > 0; i--) {
881 prev_done = done;
Dave Rodgmane0ad9a42023-09-20 19:23:58 +0100882 done |= !mbedtls_ct_size_bool_eq(input[i-1], 0);
Dave Rodgmanc1a17f52023-09-20 14:54:29 +0100883 size_t mask = mbedtls_ct_size_mask(done ^ prev_done);
884 *data_len |= i & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885 }
886
887 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200890
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200891/*
892 * No padding: don't pad :)
893 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895 * but a trivial get_padding function
896 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897static int get_no_padding(unsigned char *input, size_t input_len,
898 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200899{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100900 if (NULL == input || NULL == data_len) {
901 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
902 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200903
904 *data_len = input_len;
905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
911 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000912{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 CIPHER_VALIDATE_RET(ctx != NULL);
914 CIPHER_VALIDATE_RET(output != NULL);
915 CIPHER_VALIDATE_RET(olen != NULL);
916 if (ctx->cipher_info == NULL) {
917 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
918 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000919
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000920#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000922 /* While PSA Crypto has an API for multipart
923 * operations, we currently don't make it
924 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000926 }
927#endif /* MBEDTLS_USE_PSA_CRYPTO */
928
Paul Bakker8123e9d2011-01-06 15:37:30 +0000929 *olen = 0;
930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100931 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100932 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
934 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100935 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100936 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
937 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000938 }
939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
941 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
942 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300943 }
944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
946 if (ctx->unprocessed_len != 0) {
947 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
948 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200951 }
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200955 int ret = 0;
956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200958 /* check for 'no padding' mode */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 if (NULL == ctx->add_padding) {
960 if (0 != ctx->unprocessed_len) {
961 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
962 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200965 }
966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
968 ctx->unprocessed_len);
969 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200970 /*
971 * For decrypt operations, expect a full block,
972 * or an empty block if no padding
973 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
975 return 0;
976 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000979 }
980
981 /* cipher block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
983 ctx->operation,
984 mbedtls_cipher_get_block_size(ctx),
985 ctx->iv,
986 ctx->unprocessed_data, output))) {
987 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000988 }
989
990 /* Set output size for decryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 if (MBEDTLS_DECRYPT == ctx->operation) {
992 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
993 olen);
994 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000995
996 /* Set output size for encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997 *olen = mbedtls_cipher_get_block_size(ctx);
998 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000999 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001000#else
1001 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005}
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001008int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1009 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001010{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 CIPHER_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1014 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001015 }
1016
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001017#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001019 /* While PSA Crypto knows about CBC padding
1020 * schemes, we currently don't make them
1021 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 if (mode != MBEDTLS_PADDING_NONE) {
1023 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1024 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001027 }
1028#endif /* MBEDTLS_USE_PSA_CRYPTO */
1029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 case MBEDTLS_PADDING_PKCS7:
1033 ctx->add_padding = add_pkcs_padding;
1034 ctx->get_padding = get_pkcs_padding;
1035 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001038 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1039 ctx->add_padding = add_one_and_zeros_padding;
1040 ctx->get_padding = get_one_and_zeros_padding;
1041 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1045 ctx->add_padding = add_zeros_and_len_padding;
1046 ctx->get_padding = get_zeros_and_len_padding;
1047 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 case MBEDTLS_PADDING_ZEROS:
1051 ctx->add_padding = add_zeros_padding;
1052 ctx->get_padding = get_zeros_padding;
1053 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001054#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 case MBEDTLS_PADDING_NONE:
1056 ctx->add_padding = NULL;
1057 ctx->get_padding = get_no_padding;
1058 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 default:
1061 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001062 }
1063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001067
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001068#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1070 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001071{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072 CIPHER_VALIDATE_RET(ctx != NULL);
1073 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1074 if (ctx->cipher_info == NULL) {
1075 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1076 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 if (MBEDTLS_ENCRYPT != ctx->operation) {
1079 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1080 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001081
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001084 /* While PSA Crypto has an API for multipart
1085 * operations, we currently don't make it
1086 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001087 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001088 }
1089#endif /* MBEDTLS_USE_PSA_CRYPTO */
1090
Daniel King8fe47012016-05-17 20:33:28 -03001091#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1093 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1094 tag, tag_len);
Daniel King8fe47012016-05-17 20:33:28 -03001095 }
1096#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098#if defined(MBEDTLS_CHACHAPOLY_C)
1099 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1100 /* Don't allow truncated MAC for Poly1305 */
1101 if (tag_len != 16U) {
1102 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1103 }
1104
1105 return mbedtls_chachapoly_finish(
1106 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1107 }
1108#endif
1109
1110 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111}
Paul Bakker9af723c2014-05-01 13:03:14 +02001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1114 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001115{
Daniel King8fe47012016-05-17 20:33:28 -03001116 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 CIPHER_VALIDATE_RET(ctx != NULL);
1120 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1121 if (ctx->cipher_info == NULL) {
1122 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1123 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 if (MBEDTLS_DECRYPT != ctx->operation) {
1126 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001127 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001128
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001130 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001131 /* While PSA Crypto has an API for multipart
1132 * operations, we currently don't make it
1133 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001134 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001135 }
1136#endif /* MBEDTLS_USE_PSA_CRYPTO */
1137
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001138 /* Status to return on a non-authenticated algorithm. It would make sense
1139 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1140 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1141 * unit tests assume 0. */
1142 ret = 0;
1143
Daniel King8fe47012016-05-17 20:33:28 -03001144#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1146 if (tag_len > sizeof(check_tag)) {
1147 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1148 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 if (0 != (ret = mbedtls_gcm_finish(
1151 (mbedtls_gcm_context *) ctx->cipher_ctx,
1152 check_tag, tag_len))) {
1153 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001154 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001155
1156 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001158 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001159 goto exit;
1160 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001161 }
Daniel King8fe47012016-05-17 20:33:28 -03001162#endif /* MBEDTLS_GCM_C */
1163
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001164#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001166 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 if (tag_len != sizeof(check_tag)) {
1168 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1169 }
Daniel King8fe47012016-05-17 20:33:28 -03001170
Hanno Becker18597cd2018-11-09 16:36:33 +00001171 ret = mbedtls_chachapoly_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001172 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1173 if (ret != 0) {
1174 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001175 }
1176
1177 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001179 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001180 goto exit;
1181 }
Daniel King8fe47012016-05-17 20:33:28 -03001182 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001183#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001184
Gilles Peskinef9a05012021-12-13 16:57:47 +01001185exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 mbedtls_platform_zeroize(check_tag, tag_len);
1187 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001189#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001190
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001191/*
1192 * Packet-oriented wrapper for non-AEAD modes
1193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1195 const unsigned char *iv, size_t iv_len,
1196 const unsigned char *input, size_t ilen,
1197 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001198{
Janos Follath24eed8d2019-11-22 13:21:35 +00001199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001200 size_t finish_olen;
1201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001202 CIPHER_VALIDATE_RET(ctx != NULL);
1203 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1204 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1205 CIPHER_VALIDATE_RET(output != NULL);
1206 CIPHER_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001207
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001208#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001209 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001210 /* As in the non-PSA case, we don't check that
1211 * a key has been set. If not, the key slot will
1212 * still be in its default state of 0, which is
1213 * guaranteed to be invalid, hence the PSA-call
1214 * below will gracefully fail. */
1215 mbedtls_cipher_context_psa * const cipher_psa =
1216 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1217
1218 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001219 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001220 size_t part_len;
1221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 if (ctx->operation == MBEDTLS_DECRYPT) {
1223 status = psa_cipher_decrypt_setup(&cipher_op,
1224 cipher_psa->slot,
1225 cipher_psa->alg);
1226 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1227 status = psa_cipher_encrypt_setup(&cipher_op,
1228 cipher_psa->slot,
1229 cipher_psa->alg);
1230 } else {
1231 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001232 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001233
1234 /* In the following, we can immediately return on an error,
1235 * because the PSA Crypto API guarantees that cipher operations
1236 * are terminated by unsuccessful calls to psa_cipher_update(),
1237 * and by any call to psa_cipher_finish(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if (status != PSA_SUCCESS) {
1239 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001240 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1243 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1244 if (status != PSA_SUCCESS) {
1245 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1246 }
1247 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249 status = psa_cipher_update(&cipher_op,
1250 input, ilen,
1251 output, ilen, olen);
1252 if (status != PSA_SUCCESS) {
1253 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1254 }
1255
1256 status = psa_cipher_finish(&cipher_op,
1257 output + *olen, ilen - *olen,
1258 &part_len);
1259 if (status != PSA_SUCCESS) {
1260 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1261 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001262
1263 *olen += part_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001265 }
1266#endif /* MBEDTLS_USE_PSA_CRYPTO */
1267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1269 return ret;
1270 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001272 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1273 return ret;
1274 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1277 output, olen)) != 0) {
1278 return ret;
1279 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001281 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1282 &finish_olen)) != 0) {
1283 return ret;
1284 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001285
1286 *olen += finish_olen;
1287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001289}
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001292/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001293 * Packet-oriented encryption for AEAD modes: internal function shared by
1294 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001295 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1297 const unsigned char *iv, size_t iv_len,
1298 const unsigned char *ad, size_t ad_len,
1299 const unsigned char *input, size_t ilen,
1300 unsigned char *output, size_t *olen,
1301 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001302{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001304 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001305 /* As in the non-PSA case, we don't check that
1306 * a key has been set. If not, the key slot will
1307 * still be in its default state of 0, which is
1308 * guaranteed to be invalid, hence the PSA-call
1309 * below will gracefully fail. */
1310 mbedtls_cipher_context_psa * const cipher_psa =
1311 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1312
1313 psa_status_t status;
1314
1315 /* PSA Crypto API always writes the authentication tag
1316 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 if (output == NULL || tag != output + ilen) {
1318 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1319 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 status = psa_aead_encrypt(cipher_psa->slot,
1322 cipher_psa->alg,
1323 iv, iv_len,
1324 ad, ad_len,
1325 input, ilen,
1326 output, ilen + tag_len, olen);
1327 if (status != PSA_SUCCESS) {
1328 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1329 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001330
1331 *olen -= tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001333 }
1334#endif /* MBEDTLS_USE_PSA_CRYPTO */
1335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001337 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001338 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1340 ilen, iv, iv_len, ad, ad_len,
1341 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_GCM_C */
1344#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001346 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1348 iv, iv_len, ad, ad_len, input, output,
1349 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001352#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001354 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001355 if ((iv_len != ctx->cipher_info->iv_size) ||
1356 (tag_len != 16U)) {
1357 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001358 }
1359
1360 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1362 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001363 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001364#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001367}
1368
1369/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001370 * Packet-oriented encryption for AEAD modes: internal function shared by
1371 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001372 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1374 const unsigned char *iv, size_t iv_len,
1375 const unsigned char *ad, size_t ad_len,
1376 const unsigned char *input, size_t ilen,
1377 unsigned char *output, size_t *olen,
1378 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001379{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001380#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001382 /* As in the non-PSA case, we don't check that
1383 * a key has been set. If not, the key slot will
1384 * still be in its default state of 0, which is
1385 * guaranteed to be invalid, hence the PSA-call
1386 * below will gracefully fail. */
1387 mbedtls_cipher_context_psa * const cipher_psa =
1388 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1389
1390 psa_status_t status;
1391
1392 /* PSA Crypto API always writes the authentication tag
1393 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001394 if (input == NULL || tag != input + ilen) {
1395 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1396 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 status = psa_aead_decrypt(cipher_psa->slot,
1399 cipher_psa->alg,
1400 iv, iv_len,
1401 ad, ad_len,
1402 input, ilen + tag_len,
1403 output, ilen, olen);
1404 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1405 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1406 } else if (status != PSA_SUCCESS) {
1407 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1408 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001411 }
1412#endif /* MBEDTLS_USE_PSA_CRYPTO */
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001417
1418 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1420 iv, iv_len, ad, ad_len,
1421 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001428 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_GCM_C */
1430#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001433
1434 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1436 iv, iv_len, ad, ad_len,
1437 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001446#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001447 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001449
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001450 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if ((iv_len != ctx->cipher_info->iv_size) ||
1452 (tag_len != 16U)) {
1453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001454 }
1455
1456 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1458 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001461 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 }
Daniel King8fe47012016-05-17 20:33:28 -03001463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001466#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001469}
1470
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001471#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001472/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001473 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001474 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1476 const unsigned char *iv, size_t iv_len,
1477 const unsigned char *ad, size_t ad_len,
1478 const unsigned char *input, size_t ilen,
1479 unsigned char *output, size_t *olen,
1480 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001481{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 CIPHER_VALIDATE_RET(ctx != NULL);
1483 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1484 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1485 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1486 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1487 CIPHER_VALIDATE_RET(olen != NULL);
1488 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1491 input, ilen, output, olen,
1492 tag, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001493}
1494
1495/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001496 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001497 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001498int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1499 const unsigned char *iv, size_t iv_len,
1500 const unsigned char *ad, size_t ad_len,
1501 const unsigned char *input, size_t ilen,
1502 unsigned char *output, size_t *olen,
1503 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001504{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 CIPHER_VALIDATE_RET(ctx != NULL);
1506 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1507 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1508 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1509 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1510 CIPHER_VALIDATE_RET(olen != NULL);
1511 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1514 input, ilen, output, olen,
1515 tag, tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001517#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001519
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001520#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1521/*
1522 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1523 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001524int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1525 const unsigned char *iv, size_t iv_len,
1526 const unsigned char *ad, size_t ad_len,
1527 const unsigned char *input, size_t ilen,
1528 unsigned char *output, size_t output_len,
1529 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001530{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 CIPHER_VALIDATE_RET(ctx != NULL);
1532 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1533 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1534 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1535 CIPHER_VALIDATE_RET(output != NULL);
1536 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001537
1538#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001540#if defined(MBEDTLS_USE_PSA_CRYPTO)
1541 ctx->psa_enabled == 0 &&
1542#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1544 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1545 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1546 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001547
1548 /* There is no iv, tag or ad associated with KW and KWP,
1549 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1551 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1552 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001553
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001554 (void) iv;
1555 (void) ad;
1556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1558 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559 }
1560#endif /* MBEDTLS_NIST_KW_C */
1561
1562#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1563 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001564 if (output_len < ilen + tag_len) {
1565 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1566 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1569 input, ilen, output, olen,
1570 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001571 *olen += tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001572 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001574 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001575#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1576}
1577
1578/*
1579 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1580 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001581int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1582 const unsigned char *iv, size_t iv_len,
1583 const unsigned char *ad, size_t ad_len,
1584 const unsigned char *input, size_t ilen,
1585 unsigned char *output, size_t output_len,
1586 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001587{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 CIPHER_VALIDATE_RET(ctx != NULL);
1589 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1590 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1591 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1592 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1593 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001594
1595#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001597#if defined(MBEDTLS_USE_PSA_CRYPTO)
1598 ctx->psa_enabled == 0 &&
1599#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1601 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1602 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1603 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001604
1605 /* There is no iv, tag or ad associated with KW and KWP,
1606 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1608 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1609 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001610
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001611 (void) iv;
1612 (void) ad;
1613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1615 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616 }
1617#endif /* MBEDTLS_NIST_KW_C */
1618
1619#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1620 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 if (ilen < tag_len || output_len < ilen - tag_len) {
1622 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1623 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001625 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1626 input, ilen - tag_len, output, olen,
1627 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001628#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001629 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001630#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1631}
1632#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#endif /* MBEDTLS_CIPHER_C */