blob: 81e855d46575fa2f1d90d00908818fe1beaac55c [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
David Horstmann71159f42023-01-03 12:51:59 +000070const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073 int *type;
74
David Horstmann71159f42023-01-03 12:51:59 +000075 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 def = mbedtls_cipher_definitions;
77 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078
David Horstmann71159f42023-01-03 12:51:59 +000079 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020080 *type++ = (*def++).type;
David Horstmann71159f42023-01-03 12:51:59 +000081 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020082
83 *type = 0;
84
85 supported_init = 1;
86 }
87
David Horstmann71159f42023-01-03 12:51:59 +000088 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000089}
90
Hanno Becker18597cd2018-11-09 16:36:33 +000091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
David Horstmann71159f42023-01-03 12:51:59 +000092 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020095
David Horstmann71159f42023-01-03 12:51:59 +000096 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
97 if (def->type == cipher_type) {
98 return def->info;
99 }
100 }
Paul Bakker343a8702011-06-09 14:27:58 +0000101
David Horstmann71159f42023-01-03 12:51:59 +0000102 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103}
104
Hanno Becker18597cd2018-11-09 16:36:33 +0000105const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
David Horstmann71159f42023-01-03 12:51:59 +0000106 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200109
David Horstmann71159f42023-01-03 12:51:59 +0000110 if (NULL == cipher_name) {
111 return NULL;
112 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113
David Horstmann71159f42023-01-03 12:51:59 +0000114 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
115 if (!strcmp(def->info->name, cipher_name)) {
116 return def->info;
117 }
118 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000119
David Horstmann71159f42023-01-03 12:51:59 +0000120 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121}
122
Hanno Becker18597cd2018-11-09 16:36:33 +0000123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
124 const mbedtls_cipher_id_t cipher_id,
125 int key_bitlen,
David Horstmann71159f42023-01-03 12:51:59 +0000126 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200129
David Horstmann71159f42023-01-03 12:51:59 +0000130 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
131 if (def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200132 def->info->key_bitlen == (unsigned) key_bitlen &&
David Horstmann71159f42023-01-03 12:51:59 +0000133 def->info->mode == mode) {
134 return def->info;
135 }
136 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200137
David Horstmann71159f42023-01-03 12:51:59 +0000138 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200139}
140
David Horstmann71159f42023-01-03 12:51:59 +0000141void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200142{
David Horstmann71159f42023-01-03 12:51:59 +0000143 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200144}
145
David Horstmann71159f42023-01-03 12:51:59 +0000146void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200147{
David Horstmann71159f42023-01-03 12:51:59 +0000148 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200149 return;
David Horstmann71159f42023-01-03 12:51:59 +0000150 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000152#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000153 if (ctx->psa_enabled == 1) {
154 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000155 mbedtls_cipher_context_psa * const cipher_psa =
156 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
157
David Horstmann71159f42023-01-03 12:51:59 +0000158 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000159 /* xxx_free() doesn't allow to return failures. */
David Horstmann71159f42023-01-03 12:51:59 +0000160 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000161 }
162
David Horstmann71159f42023-01-03 12:51:59 +0000163 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
164 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000165 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000166
David Horstmann71159f42023-01-03 12:51:59 +0000167 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000168 return;
169 }
170#endif /* MBEDTLS_USE_PSA_CRYPTO */
171
Simon Butcher327398a2016-10-05 14:09:11 +0100172#if defined(MBEDTLS_CMAC_C)
David Horstmann71159f42023-01-03 12:51:59 +0000173 if (ctx->cmac_ctx) {
174 mbedtls_platform_zeroize(ctx->cmac_ctx,
175 sizeof(mbedtls_cmac_context_t));
176 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100177 }
178#endif
179
David Horstmann71159f42023-01-03 12:51:59 +0000180 if (ctx->cipher_ctx) {
181 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
182 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183
David Horstmann71159f42023-01-03 12:51:59 +0000184 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185}
186
David Horstmann71159f42023-01-03 12:51:59 +0000187int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
188 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189{
David Horstmann71159f42023-01-03 12:51:59 +0000190 if (cipher_info == NULL) {
191 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
192 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193
David Horstmann71159f42023-01-03 12:51:59 +0000194 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195
David Horstmann71159f42023-01-03 12:51:59 +0000196 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
197 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
198 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199
200 ctx->cipher_info = cipher_info;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200203 /*
204 * Ignore possible errors caused by a cipher mode that doesn't use padding
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
David Horstmann71159f42023-01-03 12:51:59 +0000207 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200208#else
David Horstmann71159f42023-01-03 12:51:59 +0000209 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200210#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200212
David Horstmann71159f42023-01-03 12:51:59 +0000213 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214}
215
Hanno Becker4ccfc402018-11-09 16:10:57 +0000216#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200217#if !defined(MBEDTLS_DEPRECATED_REMOVED)
David Horstmann71159f42023-01-03 12:51:59 +0000218int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
219 const mbedtls_cipher_info_t *cipher_info,
220 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000221{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000222 psa_algorithm_t alg;
223 mbedtls_cipher_context_psa *cipher_psa;
224
David Horstmann71159f42023-01-03 12:51:59 +0000225 if (NULL == cipher_info || NULL == ctx) {
226 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
227 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000228
Hanno Becker4ee7e762018-11-17 22:00:38 +0000229 /* Check that the underlying cipher mode and cipher type are
230 * supported by the underlying PSA Crypto implementation. */
David Horstmann71159f42023-01-03 12:51:59 +0000231 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
232 if (alg == 0) {
233 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
234 }
235 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
236 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
237 }
Hanno Becker6118e432018-11-09 16:47:20 +0000238
David Horstmann71159f42023-01-03 12:51:59 +0000239 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000240
David Horstmann71159f42023-01-03 12:51:59 +0000241 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
242 if (cipher_psa == NULL) {
243 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
244 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000245 cipher_psa->alg = alg;
246 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000247 ctx->cipher_info = cipher_info;
248 ctx->psa_enabled = 1;
David Horstmann71159f42023-01-03 12:51:59 +0000249 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000250}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200251#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000252#endif /* MBEDTLS_USE_PSA_CRYPTO */
253
David Horstmann71159f42023-01-03 12:51:59 +0000254int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
255 const unsigned char *key,
256 int key_bitlen,
257 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
David Horstmann71159f42023-01-03 12:51:59 +0000259 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100260 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
David Horstmann71159f42023-01-03 12:51:59 +0000261 }
262 if (ctx->cipher_info == NULL) {
263 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
264 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000266#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000267 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000268 mbedtls_cipher_context_psa * const cipher_psa =
269 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
270
David Horstmann71159f42023-01-03 12:51:59 +0000271 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000272
273 psa_status_t status;
274 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276
277 /* PSA Crypto API only accepts byte-aligned keys. */
David Horstmann71159f42023-01-03 12:51:59 +0000278 if (key_bitlen % 8 != 0) {
279 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
280 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000281
282 /* Don't allow keys to be set multiple times. */
David Horstmann71159f42023-01-03 12:51:59 +0000283 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
284 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
285 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000286
Andrzej Kurekc7509322019-01-08 09:36:01 -0500287 key_type = mbedtls_psa_translate_cipher_type(
David Horstmann71159f42023-01-03 12:51:59 +0000288 ctx->cipher_info->type);
289 if (key_type == 0) {
290 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
291 }
292 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000293
294 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500295 * (encrypt vs. decrypt): it is possible to setup a key for encryption
296 * and use it for AEAD decryption. Until tests relying on this
297 * are changed, allow any usage in PSA. */
David Horstmann71159f42023-01-03 12:51:59 +0000298 psa_set_key_usage_flags(&attributes,
299 /* mbedtls_psa_translate_cipher_operation( operation ); */
300 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
301 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000302
David Horstmann71159f42023-01-03 12:51:59 +0000303 status = psa_import_key(&attributes, key, key_bytelen,
304 &cipher_psa->slot);
305 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200306 case PSA_SUCCESS:
307 break;
308 case PSA_ERROR_INSUFFICIENT_MEMORY:
David Horstmann71159f42023-01-03 12:51:59 +0000309 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200310 case PSA_ERROR_NOT_SUPPORTED:
David Horstmann71159f42023-01-03 12:51:59 +0000311 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200312 default:
David Horstmann71159f42023-01-03 12:51:59 +0000313 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200314 }
315 /* Indicate that we own the key slot and need to
316 * destroy it in mbedtls_cipher_free(). */
317 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000318
319 ctx->key_bitlen = key_bitlen;
320 ctx->operation = operation;
David Horstmann71159f42023-01-03 12:51:59 +0000321 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000322 }
323#endif /* MBEDTLS_USE_PSA_CRYPTO */
324
David Horstmann71159f42023-01-03 12:51:59 +0000325 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
326 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
327 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200328 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200329
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200330 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 ctx->operation = operation;
332
Paul Bakker343a8702011-06-09 14:27:58 +0000333 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100334 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000335 */
David Horstmann71159f42023-01-03 12:51:59 +0000336 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100338 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
David Horstmann71159f42023-01-03 12:51:59 +0000339 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
340 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
341 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000342 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
David Horstmann71159f42023-01-03 12:51:59 +0000344 if (MBEDTLS_DECRYPT == operation) {
345 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
346 ctx->key_bitlen);
347 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
David Horstmann71159f42023-01-03 12:51:59 +0000349 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350}
351
David Horstmann71159f42023-01-03 12:51:59 +0000352int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
353 const unsigned char *iv,
354 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200356 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357
David Horstmann71159f42023-01-03 12:51:59 +0000358 if (ctx->cipher_info == NULL) {
359 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
360 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000361#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000362 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000363 /* While PSA Crypto has an API for multipart
364 * operations, we currently don't make it
365 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +0000366 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000367 }
368#endif /* MBEDTLS_USE_PSA_CRYPTO */
369
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200370 /* avoid buffer overflow in ctx->iv */
David Horstmann71159f42023-01-03 12:51:59 +0000371 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
372 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
373 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200374
David Horstmann71159f42023-01-03 12:51:59 +0000375 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200376 actual_iv_size = iv_len;
David Horstmann71159f42023-01-03 12:51:59 +0000377 } else {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200378 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200379
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200380 /* avoid reading past the end of input buffer */
David Horstmann71159f42023-01-03 12:51:59 +0000381 if (actual_iv_size > iv_len) {
382 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
383 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200384 }
385
Daniel Kingbd920622016-05-15 19:56:20 -0300386#if defined(MBEDTLS_CHACHA20_C)
David Horstmann71159f42023-01-03 12:51:59 +0000387 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100388 /* Even though the actual_iv_size is overwritten with a correct value
389 * of 12 from the cipher info, return an error to indicate that
390 * the input iv_len is wrong. */
David Horstmann71159f42023-01-03 12:51:59 +0000391 if (iv_len != 12) {
392 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
393 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100394
David Horstmann71159f42023-01-03 12:51:59 +0000395 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
396 iv,
397 0U)) { /* Initial counter value */
398 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300399 }
400 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100401#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +0000402 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
403 iv_len != 12) {
404 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
405 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100406#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300407#endif
408
Gilles Peskine295fc132021-04-15 18:32:23 +0200409#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +0000410 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
411 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
412 ctx->operation,
413 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200414 }
415#endif
416
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200417#if defined(MBEDTLS_CCM_C)
David Horstmann71159f42023-01-03 12:51:59 +0000418 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200419 int set_lengths_result;
420 int ccm_star_mode;
421
422 set_lengths_result = mbedtls_ccm_set_lengths(
David Horstmann71159f42023-01-03 12:51:59 +0000423 (mbedtls_ccm_context *) ctx->cipher_ctx,
424 0, 0, 0);
425 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200426 return set_lengths_result;
David Horstmann71159f42023-01-03 12:51:59 +0000427 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200428
David Horstmann71159f42023-01-03 12:51:59 +0000429 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200430 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
David Horstmann71159f42023-01-03 12:51:59 +0000431 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200432 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
David Horstmann71159f42023-01-03 12:51:59 +0000433 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200434 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
David Horstmann71159f42023-01-03 12:51:59 +0000435 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200436
David Horstmann71159f42023-01-03 12:51:59 +0000437 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
438 ccm_star_mode,
439 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200440 }
441#endif
442
David Horstmann71159f42023-01-03 12:51:59 +0000443 if (actual_iv_size != 0) {
444 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300445 ctx->iv_size = actual_iv_size;
446 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200447
David Horstmann71159f42023-01-03 12:51:59 +0000448 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200449}
450
David Horstmann71159f42023-01-03 12:51:59 +0000451int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200452{
David Horstmann71159f42023-01-03 12:51:59 +0000453 if (ctx->cipher_info == NULL) {
454 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
455 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200456
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000457#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000458 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000459 /* We don't support resetting PSA-based
460 * cipher contexts, yet. */
David Horstmann71159f42023-01-03 12:51:59 +0000461 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000462 }
463#endif /* MBEDTLS_USE_PSA_CRYPTO */
464
Paul Bakker8123e9d2011-01-06 15:37:30 +0000465 ctx->unprocessed_len = 0;
466
David Horstmann71159f42023-01-03 12:51:59 +0000467 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200468}
469
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200470#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +0000471int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
472 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200473{
David Horstmann71159f42023-01-03 12:51:59 +0000474 if (ctx->cipher_info == NULL) {
475 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
476 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200477
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000478#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000479 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000480 /* While PSA Crypto has an API for multipart
481 * operations, we currently don't make it
482 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +0000483 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000484 }
485#endif /* MBEDTLS_USE_PSA_CRYPTO */
486
Daniel King8fe47012016-05-17 20:33:28 -0300487#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +0000488 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
489 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
490 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200491 }
Daniel King8fe47012016-05-17 20:33:28 -0300492#endif
493
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +0000495 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300496 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200497 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300498
David Horstmann71159f42023-01-03 12:51:59 +0000499 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200500 ? MBEDTLS_CHACHAPOLY_ENCRYPT
501 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300502
David Horstmann71159f42023-01-03 12:51:59 +0000503 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
504 ctx->iv,
505 mode);
506 if (result != 0) {
507 return result;
508 }
Daniel King8fe47012016-05-17 20:33:28 -0300509
David Horstmann71159f42023-01-03 12:51:59 +0000510 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
511 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300512 }
513#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200514
David Horstmann71159f42023-01-03 12:51:59 +0000515 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200517#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518
David Horstmann71159f42023-01-03 12:51:59 +0000519int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
520 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521{
Janos Follath24eed8d2019-11-22 13:21:35 +0000522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500523 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524
David Horstmann71159f42023-01-03 12:51:59 +0000525 if (ctx->cipher_info == NULL) {
526 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
527 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000529#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000530 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000531 /* While PSA Crypto has an API for multipart
532 * operations, we currently don't make it
533 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +0000534 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000535 }
536#endif /* MBEDTLS_USE_PSA_CRYPTO */
537
Paul Bakker6c212762013-12-16 15:24:50 +0100538 *olen = 0;
David Horstmann71159f42023-01-03 12:51:59 +0000539 block_size = mbedtls_cipher_get_block_size(ctx);
540 if (0 == block_size) {
541 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100542 }
Paul Bakker6c212762013-12-16 15:24:50 +0100543
David Horstmann71159f42023-01-03 12:51:59 +0000544 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
545 if (ilen != block_size) {
546 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
547 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200548
549 *olen = ilen;
550
David Horstmann71159f42023-01-03 12:51:59 +0000551 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
552 ctx->operation, input, output))) {
553 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200554 }
555
David Horstmann71159f42023-01-03 12:51:59 +0000556 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200557 }
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +0000560 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
561 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
562 input, ilen,
563 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200564 }
565#endif
566
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200567#if defined(MBEDTLS_CCM_C)
David Horstmann71159f42023-01-03 12:51:59 +0000568 if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
569 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
570 input, ilen,
571 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200572 }
573#endif
574
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200575#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +0000576 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200577 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +0000578 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
579 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200580 }
581#endif
582
David Horstmann71159f42023-01-03 12:51:59 +0000583 if (input == output &&
584 (ctx->unprocessed_len != 0 || ilen % block_size)) {
585 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100586 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_CIPHER_MODE_CBC)
David Horstmann71159f42023-01-03 12:51:59 +0000589 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200590 size_t copy_len = 0;
591
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 /*
593 * If there is not enough data for a full block, cache it.
594 */
David Horstmann71159f42023-01-03 12:51:59 +0000595 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
596 ilen <= block_size - ctx->unprocessed_len) ||
597 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
598 ilen < block_size - ctx->unprocessed_len) ||
599 (ctx->operation == MBEDTLS_ENCRYPT &&
600 ilen < block_size - ctx->unprocessed_len)) {
601 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
602 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
604 ctx->unprocessed_len += ilen;
David Horstmann71159f42023-01-03 12:51:59 +0000605 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 }
607
608 /*
609 * Process cached data first
610 */
David Horstmann71159f42023-01-03 12:51:59 +0000611 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100612 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
David Horstmann71159f42023-01-03 12:51:59 +0000614 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
615 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616
David Horstmann71159f42023-01-03 12:51:59 +0000617 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
618 ctx->operation, block_size, ctx->iv,
619 ctx->unprocessed_data, output))) {
620 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 }
622
Janos Follath98e28a72016-05-31 14:03:54 +0100623 *olen += block_size;
624 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 ctx->unprocessed_len = 0;
626
627 input += copy_len;
628 ilen -= copy_len;
629 }
630
631 /*
632 * Cache final, incomplete block
633 */
David Horstmann71159f42023-01-03 12:51:59 +0000634 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700635 /* Encryption: only cache partial blocks
636 * Decryption w/ padding: always keep at least one whole block
637 * Decryption w/o padding: only cache partial blocks
638 */
Janos Follath98e28a72016-05-31 14:03:54 +0100639 copy_len = ilen % block_size;
David Horstmann71159f42023-01-03 12:51:59 +0000640 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700641 ctx->operation == MBEDTLS_DECRYPT &&
David Horstmann71159f42023-01-03 12:51:59 +0000642 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100643 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700644 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
David Horstmann71159f42023-01-03 12:51:59 +0000646 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
647 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
649 ctx->unprocessed_len += copy_len;
650 ilen -= copy_len;
651 }
652
653 /*
654 * Process remaining full blocks
655 */
David Horstmann71159f42023-01-03 12:51:59 +0000656 if (ilen) {
657 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
658 ctx->operation, ilen, ctx->iv, input,
659 output))) {
660 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200662
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 *olen += ilen;
664 }
665
David Horstmann71159f42023-01-03 12:51:59 +0000666 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_MODE_CFB)
David Horstmann71159f42023-01-03 12:51:59 +0000671 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
672 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
673 ctx->operation, ilen,
674 &ctx->unprocessed_len, ctx->iv,
675 input, output))) {
676 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000677 }
678
679 *olen = ilen;
680
David Horstmann71159f42023-01-03 12:51:59 +0000681 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000684
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100685#if defined(MBEDTLS_CIPHER_MODE_OFB)
David Horstmann71159f42023-01-03 12:51:59 +0000686 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
687 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
688 ilen, &ctx->unprocessed_len, ctx->iv,
689 input, output))) {
690 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100691 }
692
693 *olen = ilen;
694
David Horstmann71159f42023-01-03 12:51:59 +0000695 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100696 }
697#endif /* MBEDTLS_CIPHER_MODE_OFB */
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_CIPHER_MODE_CTR)
David Horstmann71159f42023-01-03 12:51:59 +0000700 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
701 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
702 ilen, &ctx->unprocessed_len, ctx->iv,
703 ctx->unprocessed_data, input, output))) {
704 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000705 }
706
707 *olen = ilen;
708
David Horstmann71159f42023-01-03 12:51:59 +0000709 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000710 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000712
Jaeden Ameroc6539902018-04-30 17:17:41 +0100713#if defined(MBEDTLS_CIPHER_MODE_XTS)
David Horstmann71159f42023-01-03 12:51:59 +0000714 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
715 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100716 /* We can only process an entire data unit at a time. */
David Horstmann71159f42023-01-03 12:51:59 +0000717 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100718 }
719
David Horstmann71159f42023-01-03 12:51:59 +0000720 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
721 ctx->operation, ilen, ctx->iv, input, output);
722 if (ret != 0) {
723 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100724 }
725
726 *olen = ilen;
727
David Horstmann71159f42023-01-03 12:51:59 +0000728 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100729 }
730#endif /* MBEDTLS_CIPHER_MODE_XTS */
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_CIPHER_MODE_STREAM)
David Horstmann71159f42023-01-03 12:51:59 +0000733 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
734 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
735 ilen, input, output))) {
736 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200737 }
738
739 *olen = ilen;
740
David Horstmann71159f42023-01-03 12:51:59 +0000741 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200744
David Horstmann71159f42023-01-03 12:51:59 +0000745 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746}
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
749#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200750/*
751 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
752 */
David Horstmann71159f42023-01-03 12:51:59 +0000753static void add_pkcs_padding(unsigned char *output, size_t output_len,
754 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755{
Paul Bakker23986e52011-04-24 08:57:21 +0000756 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100757 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758
David Horstmann71159f42023-01-03 12:51:59 +0000759 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000760 output[data_len + i] = (unsigned char) padding_len;
David Horstmann71159f42023-01-03 12:51:59 +0000761 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762}
763
David Horstmann71159f42023-01-03 12:51:59 +0000764static int get_pkcs_padding(unsigned char *input, size_t input_len,
765 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100767 size_t i, pad_idx;
768 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769
David Horstmann71159f42023-01-03 12:51:59 +0000770 if (NULL == input || NULL == data_len) {
771 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
772 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773
774 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775 *data_len = input_len - padding_len;
776
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100777 /* Avoid logical || since it results in a branch */
778 bad |= padding_len > input_len;
779 bad |= padding_len == 0;
780
781 /* The number of bytes checked must be independent of padding_len,
782 * so pick input_len, which is usually 8 or 16 (one block) */
783 pad_idx = input_len - padding_len;
David Horstmann71159f42023-01-03 12:51:59 +0000784 for (i = 0; i < input_len; i++) {
785 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
786 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100787
David Horstmann71159f42023-01-03 12:51:59 +0000788 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200793/*
794 * One and zeros padding: fill with 80 00 ... 00
795 */
David Horstmann71159f42023-01-03 12:51:59 +0000796static void add_one_and_zeros_padding(unsigned char *output,
797 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200798{
799 size_t padding_len = output_len - data_len;
800 unsigned char i = 0;
801
802 output[data_len] = 0x80;
David Horstmann71159f42023-01-03 12:51:59 +0000803 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804 output[data_len + i] = 0x00;
David Horstmann71159f42023-01-03 12:51:59 +0000805 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200806}
807
David Horstmann71159f42023-01-03 12:51:59 +0000808static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
809 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100811 size_t i;
812 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200813
David Horstmann71159f42023-01-03 12:51:59 +0000814 if (NULL == input || NULL == data_len) {
815 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
816 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200817
Micha Krausba8316f2017-12-23 23:40:08 +0100818 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100819 *data_len = 0;
David Horstmann71159f42023-01-03 12:51:59 +0000820 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100821 prev_done = done;
David Horstmann71159f42023-01-03 12:51:59 +0000822 done |= (input[i - 1] != 0);
823 *data_len |= (i - 1) * (done != prev_done);
824 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100825 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
David Horstmann71159f42023-01-03 12:51:59 +0000827 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200828
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200833/*
834 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
835 */
David Horstmann71159f42023-01-03 12:51:59 +0000836static void add_zeros_and_len_padding(unsigned char *output,
837 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200838{
839 size_t padding_len = output_len - data_len;
840 unsigned char i = 0;
841
David Horstmann71159f42023-01-03 12:51:59 +0000842 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200843 output[data_len + i - 1] = 0x00;
David Horstmann71159f42023-01-03 12:51:59 +0000844 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200845 output[output_len - 1] = (unsigned char) padding_len;
846}
847
David Horstmann71159f42023-01-03 12:51:59 +0000848static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
849 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100851 size_t i, pad_idx;
852 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200853
David Horstmann71159f42023-01-03 12:51:59 +0000854 if (NULL == input || NULL == data_len) {
855 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
856 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857
858 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200859 *data_len = input_len - padding_len;
860
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100861 /* Avoid logical || since it results in a branch */
862 bad |= padding_len > input_len;
863 bad |= padding_len == 0;
864
865 /* The number of bytes checked must be independent of padding_len */
866 pad_idx = input_len - padding_len;
David Horstmann71159f42023-01-03 12:51:59 +0000867 for (i = 0; i < input_len - 1; i++) {
868 bad |= input[i] * (i >= pad_idx);
869 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100870
David Horstmann71159f42023-01-03 12:51:59 +0000871 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200876/*
877 * Zero padding: fill with 00 ... 00
878 */
David Horstmann71159f42023-01-03 12:51:59 +0000879static void add_zeros_padding(unsigned char *output,
880 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200881{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200882 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200883
David Horstmann71159f42023-01-03 12:51:59 +0000884 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885 output[i] = 0x00;
David Horstmann71159f42023-01-03 12:51:59 +0000886 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887}
888
David Horstmann71159f42023-01-03 12:51:59 +0000889static int get_zeros_padding(unsigned char *input, size_t input_len,
890 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100892 size_t i;
893 unsigned char done = 0, prev_done;
894
David Horstmann71159f42023-01-03 12:51:59 +0000895 if (NULL == input || NULL == data_len) {
896 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100897 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200898
David Horstmann71159f42023-01-03 12:51:59 +0000899 *data_len = 0;
900 for (i = input_len; i > 0; i--) {
901 prev_done = done;
902 done |= (input[i-1] != 0);
903 *data_len |= i * (done != prev_done);
904 }
905
906 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200909
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200910/*
911 * No padding: don't pad :)
912 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200914 * but a trivial get_padding function
915 */
David Horstmann71159f42023-01-03 12:51:59 +0000916static int get_no_padding(unsigned char *input, size_t input_len,
917 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200918{
David Horstmann71159f42023-01-03 12:51:59 +0000919 if (NULL == input || NULL == data_len) {
920 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
921 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200922
923 *data_len = input_len;
924
David Horstmann71159f42023-01-03 12:51:59 +0000925 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200928
David Horstmann71159f42023-01-03 12:51:59 +0000929int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
930 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000931{
David Horstmann71159f42023-01-03 12:51:59 +0000932 if (ctx->cipher_info == NULL) {
933 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
934 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000936#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +0000937 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000938 /* While PSA Crypto has an API for multipart
939 * operations, we currently don't make it
940 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +0000941 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000942 }
943#endif /* MBEDTLS_USE_PSA_CRYPTO */
944
Paul Bakker8123e9d2011-01-06 15:37:30 +0000945 *olen = 0;
946
David Horstmann71159f42023-01-03 12:51:59 +0000947 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100948 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
950 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Mateusz Starzyk4cb97392021-10-27 10:42:31 +0200951 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100952 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
David Horstmann71159f42023-01-03 12:51:59 +0000953 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
954 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000955 }
956
David Horstmann71159f42023-01-03 12:51:59 +0000957 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
958 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
959 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300960 }
961
David Horstmann71159f42023-01-03 12:51:59 +0000962 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
963 if (ctx->unprocessed_len != 0) {
964 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
965 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200966
David Horstmann71159f42023-01-03 12:51:59 +0000967 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200968 }
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_CIPHER_MODE_CBC)
David Horstmann71159f42023-01-03 12:51:59 +0000971 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200972 int ret = 0;
973
David Horstmann71159f42023-01-03 12:51:59 +0000974 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975 /* check for 'no padding' mode */
David Horstmann71159f42023-01-03 12:51:59 +0000976 if (NULL == ctx->add_padding) {
977 if (0 != ctx->unprocessed_len) {
978 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
979 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200980
David Horstmann71159f42023-01-03 12:51:59 +0000981 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200982 }
983
David Horstmann71159f42023-01-03 12:51:59 +0000984 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
985 ctx->unprocessed_len);
986 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200987 /*
988 * For decrypt operations, expect a full block,
989 * or an empty block if no padding
990 */
David Horstmann71159f42023-01-03 12:51:59 +0000991 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
992 return 0;
993 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
David Horstmann71159f42023-01-03 12:51:59 +0000995 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 }
997
998 /* cipher block */
David Horstmann71159f42023-01-03 12:51:59 +0000999 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
1000 ctx->operation,
1001 mbedtls_cipher_get_block_size(ctx),
1002 ctx->iv,
1003 ctx->unprocessed_data, output))) {
1004 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005 }
1006
1007 /* Set output size for decryption */
David Horstmann71159f42023-01-03 12:51:59 +00001008 if (MBEDTLS_DECRYPT == ctx->operation) {
1009 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1010 olen);
1011 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001012
1013 /* Set output size for encryption */
David Horstmann71159f42023-01-03 12:51:59 +00001014 *olen = mbedtls_cipher_get_block_size(ctx);
1015 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001017#else
1018 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
David Horstmann71159f42023-01-03 12:51:59 +00001021 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001022}
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
David Horstmann71159f42023-01-03 12:51:59 +00001025int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1026 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001027{
David Horstmann71159f42023-01-03 12:51:59 +00001028 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1029 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001030 }
1031
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001032#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001033 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001034 /* While PSA Crypto knows about CBC padding
1035 * schemes, we currently don't make them
1036 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +00001037 if (mode != MBEDTLS_PADDING_NONE) {
1038 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1039 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001040
David Horstmann71159f42023-01-03 12:51:59 +00001041 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001042 }
1043#endif /* MBEDTLS_USE_PSA_CRYPTO */
1044
David Horstmann71159f42023-01-03 12:51:59 +00001045 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
David Horstmann71159f42023-01-03 12:51:59 +00001047 case MBEDTLS_PADDING_PKCS7:
1048 ctx->add_padding = add_pkcs_padding;
1049 ctx->get_padding = get_pkcs_padding;
1050 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
David Horstmann71159f42023-01-03 12:51:59 +00001053 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1054 ctx->add_padding = add_one_and_zeros_padding;
1055 ctx->get_padding = get_one_and_zeros_padding;
1056 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
David Horstmann71159f42023-01-03 12:51:59 +00001059 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1060 ctx->add_padding = add_zeros_and_len_padding;
1061 ctx->get_padding = get_zeros_and_len_padding;
1062 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
David Horstmann71159f42023-01-03 12:51:59 +00001065 case MBEDTLS_PADDING_ZEROS:
1066 ctx->add_padding = add_zeros_padding;
1067 ctx->get_padding = get_zeros_padding;
1068 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001069#endif
David Horstmann71159f42023-01-03 12:51:59 +00001070 case MBEDTLS_PADDING_NONE:
1071 ctx->add_padding = NULL;
1072 ctx->get_padding = get_no_padding;
1073 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001074
David Horstmann71159f42023-01-03 12:51:59 +00001075 default:
1076 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001077 }
1078
David Horstmann71159f42023-01-03 12:51:59 +00001079 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001082
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001083#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +00001084int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1085 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001086{
David Horstmann71159f42023-01-03 12:51:59 +00001087 if (ctx->cipher_info == NULL) {
1088 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1089 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001090
David Horstmann71159f42023-01-03 12:51:59 +00001091 if (MBEDTLS_ENCRYPT != ctx->operation) {
1092 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1093 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001094
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001095#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001096 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001097 /* While PSA Crypto has an API for multipart
1098 * operations, we currently don't make it
1099 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +00001100 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001101 }
1102#endif /* MBEDTLS_USE_PSA_CRYPTO */
1103
Daniel King8fe47012016-05-17 20:33:28 -03001104#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001105 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001106 size_t output_length;
1107 /* The code here doesn't yet support alternative implementations
1108 * that can delay up to a block of output. */
David Horstmann71159f42023-01-03 12:51:59 +00001109 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1110 NULL, 0, &output_length,
1111 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001112 }
Daniel King8fe47012016-05-17 20:33:28 -03001113#endif
1114
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001115#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +00001116 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001117 /* Don't allow truncated MAC for Poly1305 */
David Horstmann71159f42023-01-03 12:51:59 +00001118 if (tag_len != 16U) {
1119 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1120 }
Daniel King8fe47012016-05-17 20:33:28 -03001121
David Horstmann71159f42023-01-03 12:51:59 +00001122 return mbedtls_chachapoly_finish(
1123 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001124 }
1125#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001126
David Horstmann71159f42023-01-03 12:51:59 +00001127 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001128}
Paul Bakker9af723c2014-05-01 13:03:14 +02001129
David Horstmann71159f42023-01-03 12:51:59 +00001130int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1131 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001132{
Daniel King8fe47012016-05-17 20:33:28 -03001133 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001135
David Horstmann71159f42023-01-03 12:51:59 +00001136 if (ctx->cipher_info == NULL) {
1137 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1138 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001139
David Horstmann71159f42023-01-03 12:51:59 +00001140 if (MBEDTLS_DECRYPT != ctx->operation) {
1141 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001142 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001143
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001144#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001145 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001146 /* While PSA Crypto has an API for multipart
1147 * operations, we currently don't make it
1148 * accessible through the cipher layer. */
David Horstmann71159f42023-01-03 12:51:59 +00001149 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001150 }
1151#endif /* MBEDTLS_USE_PSA_CRYPTO */
1152
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001153 /* Status to return on a non-authenticated algorithm. */
1154 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001155
Daniel King8fe47012016-05-17 20:33:28 -03001156#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001157 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001158 size_t output_length;
1159 /* The code here doesn't yet support alternative implementations
1160 * that can delay up to a block of output. */
1161
David Horstmann71159f42023-01-03 12:51:59 +00001162 if (tag_len > sizeof(check_tag)) {
1163 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1164 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001165
David Horstmann71159f42023-01-03 12:51:59 +00001166 if (0 != (ret = mbedtls_gcm_finish(
1167 (mbedtls_gcm_context *) ctx->cipher_ctx,
1168 NULL, 0, &output_length,
1169 check_tag, tag_len))) {
1170 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001171 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001172
1173 /* Check the tag in "constant-time" */
David Horstmann71159f42023-01-03 12:51:59 +00001174 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001175 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001176 goto exit;
1177 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001178 }
Daniel King8fe47012016-05-17 20:33:28 -03001179#endif /* MBEDTLS_GCM_C */
1180
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +00001182 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001183 /* Don't allow truncated MAC for Poly1305 */
David Horstmann71159f42023-01-03 12:51:59 +00001184 if (tag_len != sizeof(check_tag)) {
1185 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1186 }
Daniel King8fe47012016-05-17 20:33:28 -03001187
Hanno Becker18597cd2018-11-09 16:36:33 +00001188 ret = mbedtls_chachapoly_finish(
David Horstmann71159f42023-01-03 12:51:59 +00001189 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1190 if (ret != 0) {
1191 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001192 }
1193
1194 /* Check the tag in "constant-time" */
David Horstmann71159f42023-01-03 12:51:59 +00001195 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001196 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001197 goto exit;
1198 }
Daniel King8fe47012016-05-17 20:33:28 -03001199 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001200#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001201
Gilles Peskinecd742982021-12-13 16:57:47 +01001202exit:
David Horstmann71159f42023-01-03 12:51:59 +00001203 mbedtls_platform_zeroize(check_tag, tag_len);
1204 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001205}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001206#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001207
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001208/*
1209 * Packet-oriented wrapper for non-AEAD modes
1210 */
David Horstmann71159f42023-01-03 12:51:59 +00001211int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1212 const unsigned char *iv, size_t iv_len,
1213 const unsigned char *input, size_t ilen,
1214 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001215{
Janos Follath24eed8d2019-11-22 13:21:35 +00001216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001217 size_t finish_olen;
1218
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001220 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001221 /* As in the non-PSA case, we don't check that
1222 * a key has been set. If not, the key slot will
1223 * still be in its default state of 0, which is
1224 * guaranteed to be invalid, hence the PSA-call
1225 * below will gracefully fail. */
1226 mbedtls_cipher_context_psa * const cipher_psa =
1227 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1228
1229 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001230 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001231 size_t part_len;
1232
David Horstmann71159f42023-01-03 12:51:59 +00001233 if (ctx->operation == MBEDTLS_DECRYPT) {
1234 status = psa_cipher_decrypt_setup(&cipher_op,
1235 cipher_psa->slot,
1236 cipher_psa->alg);
1237 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1238 status = psa_cipher_encrypt_setup(&cipher_op,
1239 cipher_psa->slot,
1240 cipher_psa->alg);
1241 } else {
1242 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001243 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001244
1245 /* In the following, we can immediately return on an error,
1246 * because the PSA Crypto API guarantees that cipher operations
1247 * are terminated by unsuccessful calls to psa_cipher_update(),
1248 * and by any call to psa_cipher_finish(). */
David Horstmann71159f42023-01-03 12:51:59 +00001249 if (status != PSA_SUCCESS) {
1250 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001251 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001252
David Horstmann71159f42023-01-03 12:51:59 +00001253 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1254 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1255 if (status != PSA_SUCCESS) {
1256 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1257 }
1258 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001259
David Horstmann71159f42023-01-03 12:51:59 +00001260 status = psa_cipher_update(&cipher_op,
1261 input, ilen,
1262 output, ilen, olen);
1263 if (status != PSA_SUCCESS) {
1264 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1265 }
1266
1267 status = psa_cipher_finish(&cipher_op,
1268 output + *olen, ilen - *olen,
1269 &part_len);
1270 if (status != PSA_SUCCESS) {
1271 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1272 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001273
1274 *olen += part_len;
David Horstmann71159f42023-01-03 12:51:59 +00001275 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001276 }
1277#endif /* MBEDTLS_USE_PSA_CRYPTO */
1278
David Horstmann71159f42023-01-03 12:51:59 +00001279 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1280 return ret;
1281 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001282
David Horstmann71159f42023-01-03 12:51:59 +00001283 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1284 return ret;
1285 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001286
David Horstmann71159f42023-01-03 12:51:59 +00001287 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1288 output, olen)) != 0) {
1289 return ret;
1290 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001291
David Horstmann71159f42023-01-03 12:51:59 +00001292 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1293 &finish_olen)) != 0) {
1294 return ret;
1295 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001296
1297 *olen += finish_olen;
1298
David Horstmann71159f42023-01-03 12:51:59 +00001299 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001300}
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001303/*
TRodziewicz18efb732021-04-29 23:12:19 +02001304 * Packet-oriented encryption for AEAD modes: internal function used by
1305 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001306 */
David Horstmann71159f42023-01-03 12:51:59 +00001307static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1308 const unsigned char *iv, size_t iv_len,
1309 const unsigned char *ad, size_t ad_len,
1310 const unsigned char *input, size_t ilen,
1311 unsigned char *output, size_t *olen,
1312 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001313{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001314#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001315 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001316 /* As in the non-PSA case, we don't check that
1317 * a key has been set. If not, the key slot will
1318 * still be in its default state of 0, which is
1319 * guaranteed to be invalid, hence the PSA-call
1320 * below will gracefully fail. */
1321 mbedtls_cipher_context_psa * const cipher_psa =
1322 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1323
1324 psa_status_t status;
1325
1326 /* PSA Crypto API always writes the authentication tag
1327 * at the end of the encrypted message. */
David Horstmann71159f42023-01-03 12:51:59 +00001328 if (output == NULL || tag != output + ilen) {
1329 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1330 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001331
David Horstmann71159f42023-01-03 12:51:59 +00001332 status = psa_aead_encrypt(cipher_psa->slot,
1333 cipher_psa->alg,
1334 iv, iv_len,
1335 ad, ad_len,
1336 input, ilen,
1337 output, ilen + tag_len, olen);
1338 if (status != PSA_SUCCESS) {
1339 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1340 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001341
1342 *olen -= tag_len;
David Horstmann71159f42023-01-03 12:51:59 +00001343 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001344 }
1345#endif /* MBEDTLS_USE_PSA_CRYPTO */
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001348 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001349 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001350 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1351 ilen, iv, iv_len, ad, ad_len,
1352 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_GCM_C */
1355#if defined(MBEDTLS_CCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001356 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001357 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001358 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1359 iv, iv_len, ad, ad_len, input, output,
1360 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001363#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +00001364 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001365 /* ChachaPoly has fixed length nonce and MAC (tag) */
David Horstmann71159f42023-01-03 12:51:59 +00001366 if ((iv_len != ctx->cipher_info->iv_size) ||
1367 (tag_len != 16U)) {
1368 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001369 }
1370
1371 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001372 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1373 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001374 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001375#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001376
David Horstmann71159f42023-01-03 12:51:59 +00001377 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001378}
1379
1380/*
TRodziewicz18efb732021-04-29 23:12:19 +02001381 * Packet-oriented encryption for AEAD modes: internal function used by
1382 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001383 */
David Horstmann71159f42023-01-03 12:51:59 +00001384static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1385 const unsigned char *iv, size_t iv_len,
1386 const unsigned char *ad, size_t ad_len,
1387 const unsigned char *input, size_t ilen,
1388 unsigned char *output, size_t *olen,
1389 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001390{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001391#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann71159f42023-01-03 12:51:59 +00001392 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001393 /* As in the non-PSA case, we don't check that
1394 * a key has been set. If not, the key slot will
1395 * still be in its default state of 0, which is
1396 * guaranteed to be invalid, hence the PSA-call
1397 * below will gracefully fail. */
1398 mbedtls_cipher_context_psa * const cipher_psa =
1399 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1400
1401 psa_status_t status;
1402
1403 /* PSA Crypto API always writes the authentication tag
1404 * at the end of the encrypted message. */
David Horstmann71159f42023-01-03 12:51:59 +00001405 if (input == NULL || tag != input + ilen) {
1406 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1407 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001408
David Horstmann71159f42023-01-03 12:51:59 +00001409 status = psa_aead_decrypt(cipher_psa->slot,
1410 cipher_psa->alg,
1411 iv, iv_len,
1412 ad, ad_len,
1413 input, ilen + tag_len,
1414 output, ilen, olen);
1415 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1416 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1417 } else if (status != PSA_SUCCESS) {
1418 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1419 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001420
David Horstmann71159f42023-01-03 12:51:59 +00001421 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001422 }
1423#endif /* MBEDTLS_USE_PSA_CRYPTO */
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_GCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001426 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001428
1429 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001430 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1431 iv, iv_len, ad, ad_len,
1432 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001433
David Horstmann71159f42023-01-03 12:51:59 +00001434 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
David Horstmann71159f42023-01-03 12:51:59 +00001436 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001437
David Horstmann71159f42023-01-03 12:51:59 +00001438 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_GCM_C */
1441#if defined(MBEDTLS_CCM_C)
David Horstmann71159f42023-01-03 12:51:59 +00001442 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001444
1445 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001446 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1447 iv, iv_len, ad, ad_len,
1448 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001449
David Horstmann71159f42023-01-03 12:51:59 +00001450 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
David Horstmann71159f42023-01-03 12:51:59 +00001452 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001453
David Horstmann71159f42023-01-03 12:51:59 +00001454 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001457#if defined(MBEDTLS_CHACHAPOLY_C)
David Horstmann71159f42023-01-03 12:51:59 +00001458 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001460
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001461 /* ChachaPoly has fixed length nonce and MAC (tag) */
David Horstmann71159f42023-01-03 12:51:59 +00001462 if ((iv_len != ctx->cipher_info->iv_size) ||
1463 (tag_len != 16U)) {
1464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
1466
1467 *olen = ilen;
David Horstmann71159f42023-01-03 12:51:59 +00001468 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1469 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001470
David Horstmann71159f42023-01-03 12:51:59 +00001471 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001472 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
David Horstmann71159f42023-01-03 12:51:59 +00001473 }
Daniel King8fe47012016-05-17 20:33:28 -03001474
David Horstmann71159f42023-01-03 12:51:59 +00001475 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001476 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001477#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001478
David Horstmann71159f42023-01-03 12:51:59 +00001479 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001482
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001483#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1484/*
1485 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1486 */
David Horstmann71159f42023-01-03 12:51:59 +00001487int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1488 const unsigned char *iv, size_t iv_len,
1489 const unsigned char *ad, size_t ad_len,
1490 const unsigned char *input, size_t ilen,
1491 unsigned char *output, size_t output_len,
1492 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001493{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001494#if defined(MBEDTLS_NIST_KW_C)
David Horstmann71159f42023-01-03 12:51:59 +00001495 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001496#if defined(MBEDTLS_USE_PSA_CRYPTO)
1497 ctx->psa_enabled == 0 &&
1498#endif
David Horstmann71159f42023-01-03 12:51:59 +00001499 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1500 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1501 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1502 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001503
1504 /* There is no iv, tag or ad associated with KW and KWP,
1505 * so these length should be 0 as documented. */
David Horstmann71159f42023-01-03 12:51:59 +00001506 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1507 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1508 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001509
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001510 (void) iv;
1511 (void) ad;
1512
David Horstmann71159f42023-01-03 12:51:59 +00001513 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1514 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001515 }
1516#endif /* MBEDTLS_NIST_KW_C */
1517
1518#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1519 /* AEAD case: check length before passing on to shared function */
David Horstmann71159f42023-01-03 12:51:59 +00001520 if (output_len < ilen + tag_len) {
1521 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1522 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001523
David Horstmann71159f42023-01-03 12:51:59 +00001524 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1525 input, ilen, output, olen,
1526 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001527 *olen += tag_len;
David Horstmann71159f42023-01-03 12:51:59 +00001528 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001529#else
David Horstmann71159f42023-01-03 12:51:59 +00001530 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001531#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1532}
1533
1534/*
1535 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1536 */
David Horstmann71159f42023-01-03 12:51:59 +00001537int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1538 const unsigned char *iv, size_t iv_len,
1539 const unsigned char *ad, size_t ad_len,
1540 const unsigned char *input, size_t ilen,
1541 unsigned char *output, size_t output_len,
1542 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001543{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001544#if defined(MBEDTLS_NIST_KW_C)
David Horstmann71159f42023-01-03 12:51:59 +00001545 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001546#if defined(MBEDTLS_USE_PSA_CRYPTO)
1547 ctx->psa_enabled == 0 &&
1548#endif
David Horstmann71159f42023-01-03 12:51:59 +00001549 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1550 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1551 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1552 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001553
1554 /* There is no iv, tag or ad associated with KW and KWP,
1555 * so these length should be 0 as documented. */
David Horstmann71159f42023-01-03 12:51:59 +00001556 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1557 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1558 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001560 (void) iv;
1561 (void) ad;
1562
David Horstmann71159f42023-01-03 12:51:59 +00001563 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1564 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001565 }
1566#endif /* MBEDTLS_NIST_KW_C */
1567
1568#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1569 /* AEAD case: check length before passing on to shared function */
David Horstmann71159f42023-01-03 12:51:59 +00001570 if (ilen < tag_len || output_len < ilen - tag_len) {
1571 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1572 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573
David Horstmann71159f42023-01-03 12:51:59 +00001574 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1575 input, ilen - tag_len, output, olen,
1576 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001577#else
David Horstmann71159f42023-01-03 12:51:59 +00001578 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1580}
1581#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_CIPHER_C */