blob: 36f87c3005067a57df8b4f12f9c9865329976b15 [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"
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"
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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068#define CIPHER_VALIDATE_RET(cond) \
69 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)
70#define CIPHER_VALIDATE(cond) \
71 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078 int *type;
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 def = mbedtls_cipher_definitions;
82 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 *type++ = (*def++).type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020087
88 *type = 0;
89
90 supported_init = 1;
91 }
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000094}
95
Hanno Becker18597cd2018-11-09 16:36:33 +000096const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
102 if (def->type == cipher_type) {
103 return def->info;
104 }
105 }
Paul Bakker343a8702011-06-09 14:27:58 +0000106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108}
109
Hanno Becker18597cd2018-11-09 16:36:33 +0000110const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 if (NULL == cipher_name) {
116 return NULL;
117 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
120 if (!strcmp(def->info->name, cipher_name)) {
121 return def->info;
122 }
123 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126}
127
Hanno Becker18597cd2018-11-09 16:36:33 +0000128const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
129 const mbedtls_cipher_id_t cipher_id,
130 int key_bitlen,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
136 if (def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200137 def->info->key_bitlen == (unsigned) key_bitlen &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 def->info->mode == mode) {
139 return def->info;
140 }
141 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144}
145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200147{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 CIPHER_VALIDATE(ctx != NULL);
149 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150}
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200153{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200155 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000158#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100166 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000167 }
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100186 if (ctx->cipher_ctx) {
187 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
188 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200191}
192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100196 CIPHER_VALIDATE_RET(ctx != NULL);
197 if (cipher_info == NULL) {
198 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
199 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
204 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
205 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206
207 ctx->cipher_info = cipher_info;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200210 /*
211 * Ignore possible errors caused by a cipher mode that doesn't use padding
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200215#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000221}
222
Hanno Becker4ccfc402018-11-09 16:10:57 +0000223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100245 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100255 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000256}
257#endif /* MBEDTLS_USE_PSA_CRYPTO */
258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
260 const unsigned char *key,
261 int key_bitlen,
262 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 CIPHER_VALIDATE_RET(ctx != NULL);
265 CIPHER_VALIDATE_RET(key != NULL);
266 CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
267 operation == MBEDTLS_DECRYPT);
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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100315 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200316 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200318 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return MBEDTLS_ERR_CIPHER_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 Peskine1b6c09a2023-01-11 14:52:35 +0100327 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000328 }
329#endif /* MBEDTLS_USE_PSA_CRYPTO */
330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
332 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
333 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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100345 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
346 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
347 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000348 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (MBEDTLS_DECRYPT == operation) {
351 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
352 ctx->key_bitlen);
353 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356}
357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100364 CIPHER_VALIDATE_RET(ctx != NULL);
365 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
366 if (ctx->cipher_info == NULL) {
367 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
368 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000369#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000371 /* While PSA Crypto has an API for multipart
372 * operations, we currently don't make it
373 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000375 }
376#endif /* MBEDTLS_USE_PSA_CRYPTO */
377
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200378 /* avoid buffer overflow in ctx->iv */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
380 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
381 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200384 actual_iv_size = iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 } else {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200386 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200387
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200388 /* avoid reading past the end of input buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 if (actual_iv_size > iv_len) {
390 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
391 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200392 }
393
Daniel Kingbd920622016-05-15 19:56:20 -0300394#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100396 /* Even though the actual_iv_size is overwritten with a correct value
397 * of 12 from the cipher info, return an error to indicate that
398 * the input iv_len is wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 if (iv_len != 12) {
400 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
401 }
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
404 iv,
405 0U)) { /* Initial counter value */
406 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300407 }
408 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100409#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
411 iv_len != 12) {
412 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
413 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100414#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300415#endif
416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 if (actual_iv_size != 0) {
418 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300419 ctx->iv_size = actual_iv_size;
420 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200423}
424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200426{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 CIPHER_VALIDATE_RET(ctx != NULL);
428 if (ctx->cipher_info == NULL) {
429 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
430 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200431
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000434 /* We don't support resetting PSA-based
435 * cipher contexts, yet. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000437 }
438#endif /* MBEDTLS_USE_PSA_CRYPTO */
439
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440 ctx->unprocessed_len = 0;
441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200443}
444
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200445#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
447 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200448{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 CIPHER_VALIDATE_RET(ctx != NULL);
450 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
451 if (ctx->cipher_info == NULL) {
452 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
453 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200454
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000455#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000457 /* While PSA Crypto has an API for multipart
458 * operations, we currently don't make it
459 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000461 }
462#endif /* MBEDTLS_USE_PSA_CRYPTO */
463
Daniel King8fe47012016-05-17 20:33:28 -0300464#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
466 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
467 ctx->iv, ctx->iv_size, ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200468 }
Daniel King8fe47012016-05-17 20:33:28 -0300469#endif
470
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200471#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300473 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200474 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200477 ? MBEDTLS_CHACHAPOLY_ENCRYPT
478 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
481 ctx->iv,
482 mode);
483 if (result != 0) {
484 return result;
485 }
Daniel King8fe47012016-05-17 20:33:28 -0300486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
488 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300489 }
490#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
497 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498{
Janos Follath24eed8d2019-11-22 13:21:35 +0000499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500500 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 CIPHER_VALIDATE_RET(ctx != NULL);
503 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
504 CIPHER_VALIDATE_RET(output != NULL);
505 CIPHER_VALIDATE_RET(olen != NULL);
506 if (ctx->cipher_info == NULL) {
507 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
508 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000510#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000512 /* While PSA Crypto has an API for multipart
513 * operations, we currently don't make it
514 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000516 }
517#endif /* MBEDTLS_USE_PSA_CRYPTO */
518
Paul Bakker6c212762013-12-16 15:24:50 +0100519 *olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 block_size = mbedtls_cipher_get_block_size(ctx);
521 if (0 == block_size) {
522 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100523 }
Paul Bakker6c212762013-12-16 15:24:50 +0100524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
526 if (ilen != block_size) {
527 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
528 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200529
530 *olen = ilen;
531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
533 ctx->operation, input, output))) {
534 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200535 }
536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200538 }
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200542 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
544 output);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200545 }
546#endif
547
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200548#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200550 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
552 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200553 }
554#endif
555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if (input == output &&
557 (ctx->unprocessed_len != 0 || ilen % block_size)) {
558 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100559 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200563 size_t copy_len = 0;
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565 /*
566 * If there is not enough data for a full block, cache it.
567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
569 ilen <= block_size - ctx->unprocessed_len) ||
570 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
571 ilen < block_size - ctx->unprocessed_len) ||
572 (ctx->operation == MBEDTLS_ENCRYPT &&
573 ilen < block_size - ctx->unprocessed_len)) {
574 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
575 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576
577 ctx->unprocessed_len += ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579 }
580
581 /*
582 * Process cached data first
583 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100585 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
588 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
591 ctx->operation, block_size, ctx->iv,
592 ctx->unprocessed_data, output))) {
593 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594 }
595
Janos Follath98e28a72016-05-31 14:03:54 +0100596 *olen += block_size;
597 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598 ctx->unprocessed_len = 0;
599
600 input += copy_len;
601 ilen -= copy_len;
602 }
603
604 /*
605 * Cache final, incomplete block
606 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700608 /* Encryption: only cache partial blocks
609 * Decryption w/ padding: always keep at least one whole block
610 * Decryption w/o padding: only cache partial blocks
611 */
Janos Follath98e28a72016-05-31 14:03:54 +0100612 copy_len = ilen % block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700614 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100616 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700617 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
620 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621
622 ctx->unprocessed_len += copy_len;
623 ilen -= copy_len;
624 }
625
626 /*
627 * Process remaining full blocks
628 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 if (ilen) {
630 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
631 ctx->operation, ilen, ctx->iv, input,
632 output))) {
633 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200635
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 *olen += ilen;
637 }
638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
645 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
646 ctx->operation, ilen,
647 &ctx->unprocessed_len, ctx->iv,
648 input, output))) {
649 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000650 }
651
652 *olen = ilen;
653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000657
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100658#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
660 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
661 ilen, &ctx->unprocessed_len, ctx->iv,
662 input, output))) {
663 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100664 }
665
666 *olen = ilen;
667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100669 }
670#endif /* MBEDTLS_CIPHER_MODE_OFB */
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
674 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
675 ilen, &ctx->unprocessed_len, ctx->iv,
676 ctx->unprocessed_data, input, output))) {
677 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000678 }
679
680 *olen = ilen;
681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000685
Jaeden Ameroc6539902018-04-30 17:17:41 +0100686#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
688 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100689 /* We can only process an entire data unit at a time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100691 }
692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
694 ctx->operation, ilen, ctx->iv, input, output);
695 if (ret != 0) {
696 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100697 }
698
699 *olen = ilen;
700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100702 }
703#endif /* MBEDTLS_CIPHER_MODE_XTS */
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
707 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
708 ilen, input, output))) {
709 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200710 }
711
712 *olen = ilen;
713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719}
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
722#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200723/*
724 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
725 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726static void add_pkcs_padding(unsigned char *output, size_t output_len,
727 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728{
Paul Bakker23986e52011-04-24 08:57:21 +0000729 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100730 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000733 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735}
736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100737static int get_pkcs_padding(unsigned char *input, size_t input_len,
738 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100740 size_t i, pad_idx;
741 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100743 if (NULL == input || NULL == data_len) {
744 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
745 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746
747 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748 *data_len = input_len - padding_len;
749
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100750 /* Avoid logical || since it results in a branch */
751 bad |= padding_len > input_len;
752 bad |= padding_len == 0;
753
754 /* The number of bytes checked must be independent of padding_len,
755 * so pick input_len, which is usually 8 or 16 (one block) */
756 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 for (i = 0; i < input_len; i++) {
758 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
759 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200766/*
767 * One and zeros padding: fill with 80 00 ... 00
768 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769static void add_one_and_zeros_padding(unsigned char *output,
770 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200771{
772 size_t padding_len = output_len - data_len;
773 unsigned char i = 0;
774
775 output[data_len] = 0x80;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200777 output[data_len + i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200779}
780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
782 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200783{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100784 size_t i;
785 unsigned char done = 0, prev_done, bad;
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
Micha Krausba8316f2017-12-23 23:40:08 +0100791 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100792 *data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100793 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100794 prev_done = done;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 done |= (input[i - 1] != 0);
796 *data_len |= (i - 1) * (done != prev_done);
797 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100798 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100800 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200801
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200806/*
807 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
808 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809static void add_zeros_and_len_padding(unsigned char *output,
810 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200811{
812 size_t padding_len = output_len - data_len;
813 unsigned char i = 0;
814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200816 output[data_len + i - 1] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200818 output[output_len - 1] = (unsigned char) padding_len;
819}
820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
822 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200823{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100824 size_t i, pad_idx;
825 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 if (NULL == input || NULL == data_len) {
828 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
829 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200830
831 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200832 *data_len = input_len - padding_len;
833
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100834 /* Avoid logical || since it results in a branch */
835 bad |= padding_len > input_len;
836 bad |= padding_len == 0;
837
838 /* The number of bytes checked must be independent of padding_len */
839 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840 for (i = 0; i < input_len - 1; i++) {
841 bad |= input[i] * (i >= pad_idx);
842 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100844 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200845}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200849/*
850 * Zero padding: fill with 00 ... 00
851 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852static void add_zeros_padding(unsigned char *output,
853 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200854{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200855 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200858 output[i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200860}
861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862static int get_zeros_padding(unsigned char *input, size_t input_len,
863 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200864{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100865 size_t i;
866 unsigned char done = 0, prev_done;
867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 if (NULL == input || NULL == data_len) {
869 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100870 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100872 *data_len = 0;
873 for (i = input_len; i > 0; i--) {
874 prev_done = done;
875 done |= (input[i-1] != 0);
876 *data_len |= i * (done != prev_done);
877 }
878
879 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200882
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200883/*
884 * No padding: don't pad :)
885 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200887 * but a trivial get_padding function
888 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100889static int get_no_padding(unsigned char *input, size_t input_len,
890 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200891{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 if (NULL == input || NULL == data_len) {
893 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
894 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895
896 *data_len = input_len;
897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
903 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000904{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905 CIPHER_VALIDATE_RET(ctx != NULL);
906 CIPHER_VALIDATE_RET(output != NULL);
907 CIPHER_VALIDATE_RET(olen != NULL);
908 if (ctx->cipher_info == NULL) {
909 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
910 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000911
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000912#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000914 /* While PSA Crypto has an API for multipart
915 * operations, we currently don't make it
916 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000918 }
919#endif /* MBEDTLS_USE_PSA_CRYPTO */
920
Paul Bakker8123e9d2011-01-06 15:37:30 +0000921 *olen = 0;
922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100924 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
926 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100927 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
929 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000930 }
931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
933 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
934 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300935 }
936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
938 if (ctx->unprocessed_len != 0) {
939 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
940 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200943 }
944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200947 int ret = 0;
948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200950 /* check for 'no padding' mode */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 if (NULL == ctx->add_padding) {
952 if (0 != ctx->unprocessed_len) {
953 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
954 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200957 }
958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
960 ctx->unprocessed_len);
961 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200962 /*
963 * For decrypt operations, expect a full block,
964 * or an empty block if no padding
965 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
967 return 0;
968 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200969
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000971 }
972
973 /* cipher block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
975 ctx->operation,
976 mbedtls_cipher_get_block_size(ctx),
977 ctx->iv,
978 ctx->unprocessed_data, output))) {
979 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000980 }
981
982 /* Set output size for decryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100983 if (MBEDTLS_DECRYPT == ctx->operation) {
984 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
985 olen);
986 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987
988 /* Set output size for encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 *olen = mbedtls_cipher_get_block_size(ctx);
990 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000991 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200992#else
993 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997}
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1001 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001002{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 CIPHER_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1006 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001007 }
1008
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001009#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001011 /* While PSA Crypto knows about CBC padding
1012 * schemes, we currently don't make them
1013 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 if (mode != MBEDTLS_PADDING_NONE) {
1015 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1016 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001019 }
1020#endif /* MBEDTLS_USE_PSA_CRYPTO */
1021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 case MBEDTLS_PADDING_PKCS7:
1025 ctx->add_padding = add_pkcs_padding;
1026 ctx->get_padding = get_pkcs_padding;
1027 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1031 ctx->add_padding = add_one_and_zeros_padding;
1032 ctx->get_padding = get_one_and_zeros_padding;
1033 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001036 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1037 ctx->add_padding = add_zeros_and_len_padding;
1038 ctx->get_padding = get_zeros_and_len_padding;
1039 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042 case MBEDTLS_PADDING_ZEROS:
1043 ctx->add_padding = add_zeros_padding;
1044 ctx->get_padding = get_zeros_padding;
1045 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001046#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 case MBEDTLS_PADDING_NONE:
1048 ctx->add_padding = NULL;
1049 ctx->get_padding = get_no_padding;
1050 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 default:
1053 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001054 }
1055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001059
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001060#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1062 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001063{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 CIPHER_VALIDATE_RET(ctx != NULL);
1065 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1066 if (ctx->cipher_info == NULL) {
1067 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1068 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 if (MBEDTLS_ENCRYPT != ctx->operation) {
1071 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1072 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001073
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001074#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001076 /* While PSA Crypto has an API for multipart
1077 * operations, we currently don't make it
1078 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001080 }
1081#endif /* MBEDTLS_USE_PSA_CRYPTO */
1082
Daniel King8fe47012016-05-17 20:33:28 -03001083#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1085 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1086 tag, tag_len);
Daniel King8fe47012016-05-17 20:33:28 -03001087 }
1088#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090#if defined(MBEDTLS_CHACHAPOLY_C)
1091 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1092 /* Don't allow truncated MAC for Poly1305 */
1093 if (tag_len != 16U) {
1094 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1095 }
1096
1097 return mbedtls_chachapoly_finish(
1098 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1099 }
1100#endif
1101
1102 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001103}
Paul Bakker9af723c2014-05-01 13:03:14 +02001104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001105int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1106 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001107{
Daniel King8fe47012016-05-17 20:33:28 -03001108 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 CIPHER_VALIDATE_RET(ctx != NULL);
1112 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1113 if (ctx->cipher_info == NULL) {
1114 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1115 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001117 if (MBEDTLS_DECRYPT != ctx->operation) {
1118 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001119 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001120
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001121#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001123 /* While PSA Crypto has an API for multipart
1124 * operations, we currently don't make it
1125 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001127 }
1128#endif /* MBEDTLS_USE_PSA_CRYPTO */
1129
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001130 /* Status to return on a non-authenticated algorithm. It would make sense
1131 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1132 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1133 * unit tests assume 0. */
1134 ret = 0;
1135
Daniel King8fe47012016-05-17 20:33:28 -03001136#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1138 if (tag_len > sizeof(check_tag)) {
1139 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1140 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 if (0 != (ret = mbedtls_gcm_finish(
1143 (mbedtls_gcm_context *) ctx->cipher_ctx,
1144 check_tag, tag_len))) {
1145 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001146 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001147
1148 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001150 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001151 goto exit;
1152 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001153 }
Daniel King8fe47012016-05-17 20:33:28 -03001154#endif /* MBEDTLS_GCM_C */
1155
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001156#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001158 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 if (tag_len != sizeof(check_tag)) {
1160 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1161 }
Daniel King8fe47012016-05-17 20:33:28 -03001162
Hanno Becker18597cd2018-11-09 16:36:33 +00001163 ret = mbedtls_chachapoly_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1165 if (ret != 0) {
1166 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001167 }
1168
1169 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001171 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001172 goto exit;
1173 }
Daniel King8fe47012016-05-17 20:33:28 -03001174 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001175#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001176
Gilles Peskinef9a05012021-12-13 16:57:47 +01001177exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 mbedtls_platform_zeroize(check_tag, tag_len);
1179 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001180}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001183/*
1184 * Packet-oriented wrapper for non-AEAD modes
1185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1187 const unsigned char *iv, size_t iv_len,
1188 const unsigned char *input, size_t ilen,
1189 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001190{
Janos Follath24eed8d2019-11-22 13:21:35 +00001191 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001192 size_t finish_olen;
1193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 CIPHER_VALIDATE_RET(ctx != NULL);
1195 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1196 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1197 CIPHER_VALIDATE_RET(output != NULL);
1198 CIPHER_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001199
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001200#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001202 /* As in the non-PSA case, we don't check that
1203 * a key has been set. If not, the key slot will
1204 * still be in its default state of 0, which is
1205 * guaranteed to be invalid, hence the PSA-call
1206 * below will gracefully fail. */
1207 mbedtls_cipher_context_psa * const cipher_psa =
1208 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1209
1210 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001211 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001212 size_t part_len;
1213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 if (ctx->operation == MBEDTLS_DECRYPT) {
1215 status = psa_cipher_decrypt_setup(&cipher_op,
1216 cipher_psa->slot,
1217 cipher_psa->alg);
1218 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1219 status = psa_cipher_encrypt_setup(&cipher_op,
1220 cipher_psa->slot,
1221 cipher_psa->alg);
1222 } else {
1223 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001224 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001225
1226 /* In the following, we can immediately return on an error,
1227 * because the PSA Crypto API guarantees that cipher operations
1228 * are terminated by unsuccessful calls to psa_cipher_update(),
1229 * and by any call to psa_cipher_finish(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 if (status != PSA_SUCCESS) {
1231 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001232 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1235 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1236 if (status != PSA_SUCCESS) {
1237 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1238 }
1239 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 status = psa_cipher_update(&cipher_op,
1242 input, ilen,
1243 output, ilen, olen);
1244 if (status != PSA_SUCCESS) {
1245 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1246 }
1247
1248 status = psa_cipher_finish(&cipher_op,
1249 output + *olen, ilen - *olen,
1250 &part_len);
1251 if (status != PSA_SUCCESS) {
1252 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1253 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001254
1255 *olen += part_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001257 }
1258#endif /* MBEDTLS_USE_PSA_CRYPTO */
1259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1261 return ret;
1262 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1265 return ret;
1266 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001268 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1269 output, olen)) != 0) {
1270 return ret;
1271 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1274 &finish_olen)) != 0) {
1275 return ret;
1276 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001277
1278 *olen += finish_olen;
1279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001281}
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001284/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001285 * Packet-oriented encryption for AEAD modes: internal function shared by
1286 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001287 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1289 const unsigned char *iv, size_t iv_len,
1290 const unsigned char *ad, size_t ad_len,
1291 const unsigned char *input, size_t ilen,
1292 unsigned char *output, size_t *olen,
1293 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001294{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001295#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001297 /* As in the non-PSA case, we don't check that
1298 * a key has been set. If not, the key slot will
1299 * still be in its default state of 0, which is
1300 * guaranteed to be invalid, hence the PSA-call
1301 * below will gracefully fail. */
1302 mbedtls_cipher_context_psa * const cipher_psa =
1303 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1304
1305 psa_status_t status;
1306
1307 /* PSA Crypto API always writes the authentication tag
1308 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 if (output == NULL || tag != output + ilen) {
1310 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1311 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 status = psa_aead_encrypt(cipher_psa->slot,
1314 cipher_psa->alg,
1315 iv, iv_len,
1316 ad, ad_len,
1317 input, ilen,
1318 output, ilen + tag_len, olen);
1319 if (status != PSA_SUCCESS) {
1320 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1321 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001322
1323 *olen -= tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001325 }
1326#endif /* MBEDTLS_USE_PSA_CRYPTO */
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001330 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1332 ilen, iv, iv_len, ad, ad_len,
1333 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_GCM_C */
1336#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001337 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001338 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1340 iv, iv_len, ad, ad_len, input, output,
1341 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001344#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001346 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 if ((iv_len != ctx->cipher_info->iv_size) ||
1348 (tag_len != 16U)) {
1349 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001350 }
1351
1352 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1354 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001355 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001356#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001359}
1360
1361/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001362 * Packet-oriented encryption for AEAD modes: internal function shared by
1363 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001364 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1366 const unsigned char *iv, size_t iv_len,
1367 const unsigned char *ad, size_t ad_len,
1368 const unsigned char *input, size_t ilen,
1369 unsigned char *output, size_t *olen,
1370 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001371{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001372#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001374 /* As in the non-PSA case, we don't check that
1375 * a key has been set. If not, the key slot will
1376 * still be in its default state of 0, which is
1377 * guaranteed to be invalid, hence the PSA-call
1378 * below will gracefully fail. */
1379 mbedtls_cipher_context_psa * const cipher_psa =
1380 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1381
1382 psa_status_t status;
1383
1384 /* PSA Crypto API always writes the authentication tag
1385 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386 if (input == NULL || tag != input + ilen) {
1387 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1388 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 status = psa_aead_decrypt(cipher_psa->slot,
1391 cipher_psa->alg,
1392 iv, iv_len,
1393 ad, ad_len,
1394 input, ilen + tag_len,
1395 output, ilen, olen);
1396 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1397 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1398 } else if (status != PSA_SUCCESS) {
1399 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1400 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001403 }
1404#endif /* MBEDTLS_USE_PSA_CRYPTO */
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001409
1410 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1412 iv, iv_len, ad, ad_len,
1413 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_GCM_C */
1422#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001425
1426 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1428 iv, iv_len, ad, ad_len,
1429 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001436 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001438#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001441
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001442 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 if ((iv_len != ctx->cipher_info->iv_size) ||
1444 (tag_len != 16U)) {
1445 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001446 }
1447
1448 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1450 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001453 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 }
Daniel King8fe47012016-05-17 20:33:28 -03001455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001457 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001458#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001461}
1462
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001463#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001464/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001465 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001466 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001467int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1468 const unsigned char *iv, size_t iv_len,
1469 const unsigned char *ad, size_t ad_len,
1470 const unsigned char *input, size_t ilen,
1471 unsigned char *output, size_t *olen,
1472 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001473{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001474 CIPHER_VALIDATE_RET(ctx != NULL);
1475 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1476 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1477 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1478 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1479 CIPHER_VALIDATE_RET(olen != NULL);
1480 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1483 input, ilen, output, olen,
1484 tag, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001485}
1486
1487/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001488 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001489 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1491 const unsigned char *iv, size_t iv_len,
1492 const unsigned char *ad, size_t ad_len,
1493 const unsigned char *input, size_t ilen,
1494 unsigned char *output, size_t *olen,
1495 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001496{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 CIPHER_VALIDATE_RET(ctx != NULL);
1498 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1499 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1500 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1501 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1502 CIPHER_VALIDATE_RET(olen != NULL);
1503 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1506 input, ilen, output, olen,
1507 tag, tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001508}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001509#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001511
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001512#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1513/*
1514 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1515 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1517 const unsigned char *iv, size_t iv_len,
1518 const unsigned char *ad, size_t ad_len,
1519 const unsigned char *input, size_t ilen,
1520 unsigned char *output, size_t output_len,
1521 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001522{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001523 CIPHER_VALIDATE_RET(ctx != NULL);
1524 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1525 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1526 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1527 CIPHER_VALIDATE_RET(output != NULL);
1528 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001529
1530#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001532#if defined(MBEDTLS_USE_PSA_CRYPTO)
1533 ctx->psa_enabled == 0 &&
1534#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001535 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1536 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1537 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1538 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001539
1540 /* There is no iv, tag or ad associated with KW and KWP,
1541 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1543 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1544 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001545
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001546 (void) iv;
1547 (void) ad;
1548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1550 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001551 }
1552#endif /* MBEDTLS_NIST_KW_C */
1553
1554#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1555 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 if (output_len < ilen + tag_len) {
1557 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1558 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1561 input, ilen, output, olen,
1562 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001563 *olen += tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001564 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001565#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001567#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1568}
1569
1570/*
1571 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1574 const unsigned char *iv, size_t iv_len,
1575 const unsigned char *ad, size_t ad_len,
1576 const unsigned char *input, size_t ilen,
1577 unsigned char *output, size_t output_len,
1578 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580 CIPHER_VALIDATE_RET(ctx != NULL);
1581 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1582 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1583 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1584 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1585 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001586
1587#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001589#if defined(MBEDTLS_USE_PSA_CRYPTO)
1590 ctx->psa_enabled == 0 &&
1591#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1593 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1594 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1595 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001596
1597 /* There is no iv, tag or ad associated with KW and KWP,
1598 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1600 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1601 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001603 (void) iv;
1604 (void) ad;
1605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1607 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001608 }
1609#endif /* MBEDTLS_NIST_KW_C */
1610
1611#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1612 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 if (ilen < tag_len || output_len < ilen - tag_len) {
1614 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1615 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1618 input, ilen - tag_len, output, olen,
1619 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001620#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001622#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1623}
1624#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#endif /* MBEDTLS_CIPHER_C */