blob: f6d0fcef08762e508be0e95c6fcdb1607dd17030 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000059#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148{
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150}
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153{
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200155 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000158#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if (ctx->psa_enabled == 1) {
160 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000161 mbedtls_cipher_context_psa * const cipher_psa =
162 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000165 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000167 }
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
170 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000171 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000174 return;
175 }
176#endif /* MBEDTLS_USE_PSA_CRYPTO */
177
Simon Butcher327398a2016-10-05 14:09:11 +0100178#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (ctx->cmac_ctx) {
180 mbedtls_platform_zeroize(ctx->cmac_ctx,
181 sizeof(mbedtls_cmac_context_t));
182 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100183 }
184#endif
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100187 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191}
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
194 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (cipher_info == NULL) {
197 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
198 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201
Dave Rodgmande3de772023-06-24 12:51:06 +0100202 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
204 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205
206 ctx->cipher_info = cipher_info;
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200209 /*
210 * Ignore possible errors caused by a cipher mode that doesn't use padding
211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200214#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220}
221
Hanno Becker4ccfc402018-11-09 16:10:57 +0000222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200223#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
225 const mbedtls_cipher_info_t *cipher_info,
226 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000227{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000228 psa_algorithm_t alg;
229 mbedtls_cipher_context_psa *cipher_psa;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (NULL == cipher_info || NULL == ctx) {
232 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
233 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000234
Hanno Becker4ee7e762018-11-17 22:00:38 +0000235 /* Check that the underlying cipher mode and cipher type are
236 * supported by the underlying PSA Crypto implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
238 if (alg == 0) {
239 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
240 }
241 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
242 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
243 }
Hanno Becker6118e432018-11-09 16:47:20 +0000244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
248 if (cipher_psa == NULL) {
249 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
250 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000251 cipher_psa->alg = alg;
252 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000253 ctx->cipher_info = cipher_info;
254 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000256}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200257#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000258#endif /* MBEDTLS_USE_PSA_CRYPTO */
259
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100265 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100266 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 }
268 if (ctx->cipher_info == NULL) {
269 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
270 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000272#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000274 mbedtls_cipher_context_psa * const cipher_psa =
275 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000278
279 psa_status_t status;
280 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000282
283 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (key_bitlen % 8 != 0) {
285 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
286 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000287
288 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
290 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
291 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000292
Andrzej Kurekc7509322019-01-08 09:36:01 -0500293 key_type = mbedtls_psa_translate_cipher_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 ctx->cipher_info->type);
295 if (key_type == 0) {
296 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
297 }
298 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000299
300 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500301 * (encrypt vs. decrypt): it is possible to setup a key for encryption
302 * and use it for AEAD decryption. Until tests relying on this
303 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 psa_set_key_usage_flags(&attributes,
305 /* mbedtls_psa_translate_cipher_operation( operation ); */
306 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
307 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 status = psa_import_key(&attributes, key, key_bytelen,
310 &cipher_psa->slot);
311 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200312 case PSA_SUCCESS:
313 break;
314 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200316 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200318 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200320 }
321 /* Indicate that we own the key slot and need to
322 * destroy it in mbedtls_cipher_free(). */
323 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000324
325 ctx->key_bitlen = key_bitlen;
326 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000328 }
329#endif /* MBEDTLS_USE_PSA_CRYPTO */
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100332 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200334 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200335
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200336 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337 ctx->operation = operation;
338
Paul Bakker343a8702011-06-09 14:27:58 +0000339 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100340 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100344 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100346 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100347 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000348 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100351 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100352 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356}
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
359 const unsigned char *iv,
360 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200362 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (ctx->cipher_info == NULL) {
365 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
366 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000367#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000369 /* While PSA Crypto has an API for multipart
370 * operations, we currently don't make it
371 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000373 }
374#endif /* MBEDTLS_USE_PSA_CRYPTO */
375
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200376 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
378 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
379 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200382 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100384 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200385
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200386 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (actual_iv_size > iv_len) {
388 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
389 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200390 }
391
Daniel Kingbd920622016-05-15 19:56:20 -0300392#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100394 /* Even though the actual_iv_size is overwritten with a correct value
395 * of 12 from the cipher info, return an error to indicate that
396 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (iv_len != 12) {
398 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
399 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
402 iv,
403 0U)) { /* Initial counter value */
404 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300405 }
406 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100407#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
409 iv_len != 12) {
410 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
411 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100412#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300413#endif
414
Gilles Peskine295fc132021-04-15 18:32:23 +0200415#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
417 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
418 ctx->operation,
419 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200420 }
421#endif
422
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200423#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200425 int set_lengths_result;
426 int ccm_star_mode;
427
428 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 (mbedtls_ccm_context *) ctx->cipher_ctx,
430 0, 0, 0);
431 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200432 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200436 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200438 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200440 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
444 ccm_star_mode,
445 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200446 }
447#endif
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (actual_iv_size != 0) {
450 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300451 ctx->iv_size = actual_iv_size;
452 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200455}
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200458{
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (ctx->cipher_info == NULL) {
460 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
461 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200462
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000465 /* We don't support resetting PSA-based
466 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000468 }
469#endif /* MBEDTLS_USE_PSA_CRYPTO */
470
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471 ctx->unprocessed_len = 0;
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200474}
475
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200476#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100477int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
478 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200479{
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (ctx->cipher_info == NULL) {
481 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
482 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200483
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000486 /* While PSA Crypto has an API for multipart
487 * operations, we currently don't make it
488 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000490 }
491#endif /* MBEDTLS_USE_PSA_CRYPTO */
492
Daniel King8fe47012016-05-17 20:33:28 -0300493#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
495 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
496 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200497 }
Daniel King8fe47012016-05-17 20:33:28 -0300498#endif
499
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200500#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300502 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200503 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200506 ? MBEDTLS_CHACHAPOLY_ENCRYPT
507 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
510 ctx->iv,
511 mode);
512 if (result != 0) {
513 return result;
514 }
Daniel King8fe47012016-05-17 20:33:28 -0300515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
517 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300518 }
519#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200523#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
526 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527{
Janos Follath24eed8d2019-11-22 13:21:35 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500529 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ctx->cipher_info == NULL) {
532 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
533 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000535#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000537 /* While PSA Crypto has an API for multipart
538 * operations, we currently don't make it
539 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000541 }
542#endif /* MBEDTLS_USE_PSA_CRYPTO */
543
Paul Bakker6c212762013-12-16 15:24:50 +0100544 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 block_size = mbedtls_cipher_get_block_size(ctx);
546 if (0 == block_size) {
547 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100548 }
Paul Bakker6c212762013-12-16 15:24:50 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
551 if (ilen != block_size) {
552 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
553 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200554
555 *olen = ilen;
556
Dave Rodgmande3de772023-06-24 12:51:06 +0100557 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100558 ctx->operation, input,
559 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200561 }
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
568 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
569 input, ilen,
570 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200571 }
572#endif
573
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200574#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
576 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
577 input, ilen,
578 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200579 }
580#endif
581
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200582#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200584 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
586 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200587 }
588#endif
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (input == output &&
591 (ctx->unprocessed_len != 0 || ilen % block_size)) {
592 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100593 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200597 size_t copy_len = 0;
598
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599 /*
600 * If there is not enough data for a full block, cache it.
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
603 ilen <= block_size - ctx->unprocessed_len) ||
604 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
605 ilen < block_size - ctx->unprocessed_len) ||
606 (ctx->operation == MBEDTLS_ENCRYPT &&
607 ilen < block_size - ctx->unprocessed_len)) {
608 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
609 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
611 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613 }
614
615 /*
616 * Process cached data first
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100619 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
622 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623
Dave Rodgmande3de772023-06-24 12:51:06 +0100624 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100625 ctx->operation,
626 block_size, ctx->iv,
627 ctx->
628 unprocessed_data,
629 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 }
632
Janos Follath98e28a72016-05-31 14:03:54 +0100633 *olen += block_size;
634 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 ctx->unprocessed_len = 0;
636
637 input += copy_len;
638 ilen -= copy_len;
639 }
640
641 /*
642 * Cache final, incomplete block
643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700645 /* Encryption: only cache partial blocks
646 * Decryption w/ padding: always keep at least one whole block
647 * Decryption w/o padding: only cache partial blocks
648 */
Janos Follath98e28a72016-05-31 14:03:54 +0100649 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700651 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100653 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700654 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
657 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
659 ctx->unprocessed_len += copy_len;
660 ilen -= copy_len;
661 }
662
663 /*
664 * Process remaining full blocks
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100667 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100668 ctx->operation,
669 ilen, ctx->iv,
670 input,
671 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200674
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675 *olen += ilen;
676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100684 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100685 ctx->operation, ilen,
686 &ctx->unprocessed_len,
687 ctx->iv,
688 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000690 }
691
692 *olen = ilen;
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000695 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000697
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100698#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100700 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100701 ilen,
702 &ctx->unprocessed_len,
703 ctx->iv,
704 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100706 }
707
708 *olen = ilen;
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100711 }
712#endif /* MBEDTLS_CIPHER_MODE_OFB */
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100716 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100717 ilen,
718 &ctx->unprocessed_len,
719 ctx->iv,
720 ctx->unprocessed_data,
721 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000723 }
724
725 *olen = ilen;
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000730
Jaeden Ameroc6539902018-04-30 17:17:41 +0100731#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
733 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100734 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100736 }
737
Dave Rodgmande3de772023-06-24 12:51:06 +0100738 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100739 ctx->operation,
740 ilen,
741 ctx->iv,
742 input,
743 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (ret != 0) {
745 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100746 }
747
748 *olen = ilen;
749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100751 }
752#endif /* MBEDTLS_CIPHER_MODE_XTS */
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100756 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100757 ilen, input,
758 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200760 }
761
762 *olen = ilen;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200765 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769}
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
772#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200773/*
774 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776static void add_pkcs_padding(unsigned char *output, size_t output_len,
777 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778{
Paul Bakker23986e52011-04-24 08:57:21 +0000779 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100780 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000783 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785}
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787static int get_pkcs_padding(unsigned char *input, size_t input_len,
788 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000789{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100790 size_t i, pad_idx;
791 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (NULL == input || NULL == data_len) {
794 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
795 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796
797 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000798 *data_len = input_len - padding_len;
799
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100800 /* Avoid logical || since it results in a branch */
801 bad |= padding_len > input_len;
802 bad |= padding_len == 0;
803
804 /* The number of bytes checked must be independent of padding_len,
805 * so pick input_len, which is usually 8 or 16 (one block) */
806 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 for (i = 0; i < input_len; i++) {
808 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
809 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200816/*
817 * One and zeros padding: fill with 80 00 ... 00
818 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819static void add_one_and_zeros_padding(unsigned char *output,
820 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200821{
822 size_t padding_len = output_len - data_len;
823 unsigned char i = 0;
824
825 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200827 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200829}
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
832 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100834 size_t i;
835 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (NULL == input || NULL == data_len) {
838 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
839 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200840
Micha Krausba8316f2017-12-23 23:40:08 +0100841 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100842 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100844 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 done |= (input[i - 1] != 0);
846 *data_len |= (i - 1) * (done != prev_done);
847 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100848 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200851
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200856/*
857 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static void add_zeros_and_len_padding(unsigned char *output,
860 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200861{
862 size_t padding_len = output_len - data_len;
863 unsigned char i = 0;
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200866 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200868 output[output_len - 1] = (unsigned char) padding_len;
869}
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
872 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200873{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100874 size_t i, pad_idx;
875 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (NULL == input || NULL == data_len) {
878 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
879 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200880
881 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200882 *data_len = input_len - padding_len;
883
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100884 /* Avoid logical || since it results in a branch */
885 bad |= padding_len > input_len;
886 bad |= padding_len == 0;
887
888 /* The number of bytes checked must be independent of padding_len */
889 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 for (i = 0; i < input_len - 1; i++) {
891 bad |= input[i] * (i >= pad_idx);
892 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200899/*
900 * Zero padding: fill with 00 ... 00
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902static void add_zeros_padding(unsigned char *output,
903 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200904{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200905 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200908 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200910}
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912static int get_zeros_padding(unsigned char *input, size_t input_len,
913 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200914{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100915 size_t i;
916 unsigned char done = 0, prev_done;
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (NULL == input || NULL == data_len) {
919 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100920 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 *data_len = 0;
923 for (i = input_len; i > 0; i--) {
924 prev_done = done;
925 done |= (input[i-1] != 0);
926 *data_len |= i * (done != prev_done);
927 }
928
929 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200932
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200933/*
934 * No padding: don't pad :)
935 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200937 * but a trivial get_padding function
938 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939static int get_no_padding(unsigned char *input, size_t input_len,
940 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200941{
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (NULL == input || NULL == data_len) {
943 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
944 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200945
946 *data_len = input_len;
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
953 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000954{
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (ctx->cipher_info == NULL) {
956 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
957 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000958
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000959#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000961 /* While PSA Crypto has an API for multipart
962 * operations, we currently don't make it
963 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000965 }
966#endif /* MBEDTLS_USE_PSA_CRYPTO */
967
Paul Bakker8123e9d2011-01-06 15:37:30 +0000968 *olen = 0;
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100971 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
973 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200974 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100975 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
977 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000978 }
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
981 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
982 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
986 if (ctx->unprocessed_len != 0) {
987 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
988 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200991 }
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200995 int ret = 0;
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (NULL == ctx->add_padding) {
1000 if (0 != ctx->unprocessed_len) {
1001 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1002 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001005 }
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1008 ctx->unprocessed_len);
1009 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001010 /*
1011 * For decrypt operations, expect a full block,
1012 * or an empty block if no padding
1013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1015 return 0;
1016 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001019 }
1020
1021 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001022 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001023 ctx->operation,
1024 mbedtls_cipher_get_block_size(
1025 ctx),
1026 ctx->iv,
1027 ctx->unprocessed_data,
1028 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001030 }
1031
1032 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (MBEDTLS_DECRYPT == ctx->operation) {
1034 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1035 olen);
1036 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001037
1038 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 *olen = mbedtls_cipher_get_block_size(ctx);
1040 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001041 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001042#else
1043 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001047}
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001050int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1051 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001052{
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1054 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001055 }
1056
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001057#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001059 /* While PSA Crypto knows about CBC padding
1060 * schemes, we currently don't make them
1061 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (mode != MBEDTLS_PADDING_NONE) {
1063 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1064 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001067 }
1068#endif /* MBEDTLS_USE_PSA_CRYPTO */
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 case MBEDTLS_PADDING_PKCS7:
1073 ctx->add_padding = add_pkcs_padding;
1074 ctx->get_padding = get_pkcs_padding;
1075 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1079 ctx->add_padding = add_one_and_zeros_padding;
1080 ctx->get_padding = get_one_and_zeros_padding;
1081 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1085 ctx->add_padding = add_zeros_and_len_padding;
1086 ctx->get_padding = get_zeros_and_len_padding;
1087 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 case MBEDTLS_PADDING_ZEROS:
1091 ctx->add_padding = add_zeros_padding;
1092 ctx->get_padding = get_zeros_padding;
1093 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001094#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 case MBEDTLS_PADDING_NONE:
1096 ctx->add_padding = NULL;
1097 ctx->get_padding = get_no_padding;
1098 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 default:
1101 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001102 }
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001107
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001108#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1110 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111{
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (ctx->cipher_info == NULL) {
1113 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1114 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (MBEDTLS_ENCRYPT != ctx->operation) {
1117 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1118 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001119
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001120#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001122 /* While PSA Crypto has an API for multipart
1123 * operations, we currently don't make it
1124 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001126 }
1127#endif /* MBEDTLS_USE_PSA_CRYPTO */
1128
Daniel King8fe47012016-05-17 20:33:28 -03001129#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001131 size_t output_length;
1132 /* The code here doesn't yet support alternative implementations
1133 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1135 NULL, 0, &output_length,
1136 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001137 }
Daniel King8fe47012016-05-17 20:33:28 -03001138#endif
1139
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001140#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001142 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (tag_len != 16U) {
1144 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1145 }
Daniel King8fe47012016-05-17 20:33:28 -03001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return mbedtls_chachapoly_finish(
1148 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001149 }
1150#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001153}
Paul Bakker9af723c2014-05-01 13:03:14 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1156 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001157{
Daniel King8fe47012016-05-17 20:33:28 -03001158 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (ctx->cipher_info == NULL) {
1162 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1163 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (MBEDTLS_DECRYPT != ctx->operation) {
1166 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001167 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001168
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001171 /* While PSA Crypto has an API for multipart
1172 * operations, we currently don't make it
1173 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001175 }
1176#endif /* MBEDTLS_USE_PSA_CRYPTO */
1177
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001178 /* Status to return on a non-authenticated algorithm. */
1179 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001180
Daniel King8fe47012016-05-17 20:33:28 -03001181#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001183 size_t output_length;
1184 /* The code here doesn't yet support alternative implementations
1185 * that can delay up to a block of output. */
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (tag_len > sizeof(check_tag)) {
1188 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1189 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (0 != (ret = mbedtls_gcm_finish(
1192 (mbedtls_gcm_context *) ctx->cipher_ctx,
1193 NULL, 0, &output_length,
1194 check_tag, tag_len))) {
1195 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001196 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001197
1198 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001200 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001201 goto exit;
1202 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001203 }
Daniel King8fe47012016-05-17 20:33:28 -03001204#endif /* MBEDTLS_GCM_C */
1205
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001206#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001208 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (tag_len != sizeof(check_tag)) {
1210 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1211 }
Daniel King8fe47012016-05-17 20:33:28 -03001212
Hanno Becker18597cd2018-11-09 16:36:33 +00001213 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1215 if (ret != 0) {
1216 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001217 }
1218
1219 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001221 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001222 goto exit;
1223 }
Daniel King8fe47012016-05-17 20:33:28 -03001224 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001225#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001226
Gilles Peskinecd742982021-12-13 16:57:47 +01001227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_platform_zeroize(check_tag, tag_len);
1229 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001230}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001231#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001232
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001233/*
1234 * Packet-oriented wrapper for non-AEAD modes
1235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001236int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1237 const unsigned char *iv, size_t iv_len,
1238 const unsigned char *input, size_t ilen,
1239 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001240{
Janos Follath24eed8d2019-11-22 13:21:35 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001242 size_t finish_olen;
1243
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001244#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001246 /* As in the non-PSA case, we don't check that
1247 * a key has been set. If not, the key slot will
1248 * still be in its default state of 0, which is
1249 * guaranteed to be invalid, hence the PSA-call
1250 * below will gracefully fail. */
1251 mbedtls_cipher_context_psa * const cipher_psa =
1252 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1253
1254 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001255 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001256 size_t part_len;
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (ctx->operation == MBEDTLS_DECRYPT) {
1259 status = psa_cipher_decrypt_setup(&cipher_op,
1260 cipher_psa->slot,
1261 cipher_psa->alg);
1262 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1263 status = psa_cipher_encrypt_setup(&cipher_op,
1264 cipher_psa->slot,
1265 cipher_psa->alg);
1266 } else {
1267 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001268 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001269
1270 /* In the following, we can immediately return on an error,
1271 * because the PSA Crypto API guarantees that cipher operations
1272 * are terminated by unsuccessful calls to psa_cipher_update(),
1273 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (status != PSA_SUCCESS) {
1275 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001276 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1279 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1280 if (status != PSA_SUCCESS) {
1281 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1282 }
1283 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 status = psa_cipher_update(&cipher_op,
1286 input, ilen,
1287 output, ilen, olen);
1288 if (status != PSA_SUCCESS) {
1289 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1290 }
1291
1292 status = psa_cipher_finish(&cipher_op,
1293 output + *olen, ilen - *olen,
1294 &part_len);
1295 if (status != PSA_SUCCESS) {
1296 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1297 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001298
1299 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001301 }
1302#endif /* MBEDTLS_USE_PSA_CRYPTO */
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1305 return ret;
1306 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1309 return ret;
1310 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1313 output, olen)) != 0) {
1314 return ret;
1315 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1318 &finish_olen)) != 0) {
1319 return ret;
1320 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001321
1322 *olen += finish_olen;
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001325}
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001328/*
TRodziewicz18efb732021-04-29 23:12:19 +02001329 * Packet-oriented encryption for AEAD modes: internal function used by
1330 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1333 const unsigned char *iv, size_t iv_len,
1334 const unsigned char *ad, size_t ad_len,
1335 const unsigned char *input, size_t ilen,
1336 unsigned char *output, size_t *olen,
1337 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001338{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001339#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001341 /* As in the non-PSA case, we don't check that
1342 * a key has been set. If not, the key slot will
1343 * still be in its default state of 0, which is
1344 * guaranteed to be invalid, hence the PSA-call
1345 * below will gracefully fail. */
1346 mbedtls_cipher_context_psa * const cipher_psa =
1347 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1348
1349 psa_status_t status;
1350
1351 /* PSA Crypto API always writes the authentication tag
1352 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (output == NULL || tag != output + ilen) {
1354 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1355 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 status = psa_aead_encrypt(cipher_psa->slot,
1358 cipher_psa->alg,
1359 iv, iv_len,
1360 ad, ad_len,
1361 input, ilen,
1362 output, ilen + tag_len, olen);
1363 if (status != PSA_SUCCESS) {
1364 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1365 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001366
1367 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001369 }
1370#endif /* MBEDTLS_USE_PSA_CRYPTO */
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001374 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1376 ilen, iv, iv_len, ad, ad_len,
1377 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_GCM_C */
1380#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001382 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1384 iv, iv_len, ad, ad_len, input, output,
1385 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001386 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001388#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001390 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001391 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 (tag_len != 16U)) {
1393 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001394 }
1395
1396 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1398 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001399 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001400#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001403}
1404
1405/*
TRodziewicz18efb732021-04-29 23:12:19 +02001406 * Packet-oriented encryption for AEAD modes: internal function used by
1407 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1410 const unsigned char *iv, size_t iv_len,
1411 const unsigned char *ad, size_t ad_len,
1412 const unsigned char *input, size_t ilen,
1413 unsigned char *output, size_t *olen,
1414 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001415{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001416#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001418 /* As in the non-PSA case, we don't check that
1419 * a key has been set. If not, the key slot will
1420 * still be in its default state of 0, which is
1421 * guaranteed to be invalid, hence the PSA-call
1422 * below will gracefully fail. */
1423 mbedtls_cipher_context_psa * const cipher_psa =
1424 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1425
1426 psa_status_t status;
1427
1428 /* PSA Crypto API always writes the authentication tag
1429 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (input == NULL || tag != input + ilen) {
1431 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1432 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 status = psa_aead_decrypt(cipher_psa->slot,
1435 cipher_psa->alg,
1436 iv, iv_len,
1437 ad, ad_len,
1438 input, ilen + tag_len,
1439 output, ilen, olen);
1440 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1441 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1442 } else if (status != PSA_SUCCESS) {
1443 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1444 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001447 }
1448#endif /* MBEDTLS_USE_PSA_CRYPTO */
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001453
1454 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1456 iv, iv_len, ad, ad_len,
1457 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_GCM_C */
1466#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001469
1470 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1472 iv, iv_len, ad, ad_len,
1473 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001482#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001485
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001486 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001487 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 (tag_len != 16U)) {
1489 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001490 }
1491
1492 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1494 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001497 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 }
Daniel King8fe47012016-05-17 20:33:28 -03001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001501 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001502#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001507
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001508#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1509/*
1510 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1513 const unsigned char *iv, size_t iv_len,
1514 const unsigned char *ad, size_t ad_len,
1515 const unsigned char *input, size_t ilen,
1516 unsigned char *output, size_t output_len,
1517 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001518{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001519#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001521#if defined(MBEDTLS_USE_PSA_CRYPTO)
1522 ctx->psa_enabled == 0 &&
1523#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1525 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1526 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1527 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001528
1529 /* There is no iv, tag or ad associated with KW and KWP,
1530 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1532 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1533 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001534
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001535 (void) iv;
1536 (void) ad;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1539 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001540 }
1541#endif /* MBEDTLS_NIST_KW_C */
1542
1543#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1544 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (output_len < ilen + tag_len) {
1546 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1547 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1550 input, ilen, output, olen,
1551 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001552 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001554#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001556#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1557}
1558
1559/*
1560 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001562int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1563 const unsigned char *iv, size_t iv_len,
1564 const unsigned char *ad, size_t ad_len,
1565 const unsigned char *input, size_t ilen,
1566 unsigned char *output, size_t output_len,
1567 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001568{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001571#if defined(MBEDTLS_USE_PSA_CRYPTO)
1572 ctx->psa_enabled == 0 &&
1573#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1575 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1576 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1577 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001578
1579 /* There is no iv, tag or ad associated with KW and KWP,
1580 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1582 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1583 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001584
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001585 (void) iv;
1586 (void) ad;
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1589 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001590 }
1591#endif /* MBEDTLS_NIST_KW_C */
1592
1593#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1594 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (ilen < tag_len || output_len < ilen - tag_len) {
1596 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1597 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1600 input, ilen - tag_len, output, olen,
1601 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001604#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1605}
1606#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_CIPHER_C */