blob: f9d46213db98e29d27a6b871a9bdc6d1cdf08208 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +02004 * \brief Generic cipher wrapper for Mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker8123e9d2011-01-06 15:37:30 +000010 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/cipher.h"
David Horstmannd37e0c42025-01-16 16:24:35 +000017#include "cipher_invasive.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000018#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050019#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000020#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020021#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010022#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000023
Rich Evans00ab4702015-02-06 13:43:58 +000024#include <stdlib.h>
25#include <string.h>
26
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020027#if defined(MBEDTLS_CHACHAPOLY_C)
28#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030029#endif
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020037#endif
38
Daniel Kingbd920622016-05-15 19:56:20 -030039#if defined(MBEDTLS_CHACHA20_C)
40#include "mbedtls/chacha20.h"
41#endif
42
Simon Butcher327398a2016-10-05 14:09:11 +010043#if defined(MBEDTLS_CMAC_C)
44#include "mbedtls/cmac.h"
45#endif
46
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020047#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000048#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020049#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000050
Jack Lloydffdf2882019-03-07 17:00:32 -050051#if defined(MBEDTLS_NIST_KW_C)
52#include "mbedtls/nist_kw.h"
53#endif
54
Simon Butcher327398a2016-10-05 14:09:11 +010055#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010056
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020057static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000058
Dave Rodgman3b46b772023-06-24 13:25:06 +010059static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
60 const mbedtls_cipher_info_t *info)
61{
Dave Rodgmande3de772023-06-24 12:51:06 +010062 return mbedtls_cipher_base_lookup_table[info->base_idx];
63}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068 int *type;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 def = mbedtls_cipher_definitions;
72 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020075 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020077
78 *type = 0;
79
80 supported_init = 1;
81 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000084}
85
Hanno Becker18597cd2018-11-09 16:36:33 +000086const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010087 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
92 if (def->type == cipher_type) {
93 return def->info;
94 }
95 }
Paul Bakker343a8702011-06-09 14:27:58 +000096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000098}
99
Hanno Becker18597cd2018-11-09 16:36:33 +0000100const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (NULL == cipher_name) {
106 return NULL;
107 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
110 if (!strcmp(def->info->name, cipher_name)) {
111 return def->info;
112 }
113 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116}
117
Hanno Becker18597cd2018-11-09 16:36:33 +0000118const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
119 const mbedtls_cipher_id_t cipher_id,
120 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100126 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100127 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 def->info->mode == mode) {
129 return def->info;
130 }
131 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200134}
135
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200136#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
137static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
138 mbedtls_cipher_type_t cipher)
139{
140 switch (cipher) {
141 case MBEDTLS_CIPHER_AES_128_CCM:
142 case MBEDTLS_CIPHER_AES_192_CCM:
143 case MBEDTLS_CIPHER_AES_256_CCM:
144 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
145 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
146 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
147 case MBEDTLS_CIPHER_AES_128_GCM:
148 case MBEDTLS_CIPHER_AES_192_GCM:
149 case MBEDTLS_CIPHER_AES_256_GCM:
150 case MBEDTLS_CIPHER_AES_128_CBC:
151 case MBEDTLS_CIPHER_AES_192_CBC:
152 case MBEDTLS_CIPHER_AES_256_CBC:
153 case MBEDTLS_CIPHER_AES_128_ECB:
154 case MBEDTLS_CIPHER_AES_192_ECB:
155 case MBEDTLS_CIPHER_AES_256_ECB:
156 return PSA_KEY_TYPE_AES;
157
158 /* ARIA not yet supported in PSA. */
159 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
160 case MBEDTLS_CIPHER_ARIA_192_CCM:
161 case MBEDTLS_CIPHER_ARIA_256_CCM:
162 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
163 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
164 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
165 case MBEDTLS_CIPHER_ARIA_128_GCM:
166 case MBEDTLS_CIPHER_ARIA_192_GCM:
167 case MBEDTLS_CIPHER_ARIA_256_GCM:
168 case MBEDTLS_CIPHER_ARIA_128_CBC:
169 case MBEDTLS_CIPHER_ARIA_192_CBC:
170 case MBEDTLS_CIPHER_ARIA_256_CBC:
171 return( PSA_KEY_TYPE_ARIA ); */
172
173 default:
174 return 0;
175 }
176}
177
178static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
179 mbedtls_cipher_mode_t mode, size_t taglen)
180{
181 switch (mode) {
182 case MBEDTLS_MODE_ECB:
183 return PSA_ALG_ECB_NO_PADDING;
184 case MBEDTLS_MODE_GCM:
185 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
186 case MBEDTLS_MODE_CCM:
187 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
188 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
189 return PSA_ALG_CCM_STAR_NO_TAG;
190 case MBEDTLS_MODE_CBC:
191 if (taglen == 0) {
192 return PSA_ALG_CBC_NO_PADDING;
193 } else {
194 return 0;
195 }
196 default:
197 return 0;
198 }
199}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200200#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200203{
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200208{
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200210 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200212
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200213#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (ctx->psa_enabled == 1) {
215 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000216 mbedtls_cipher_context_psa * const cipher_psa =
217 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000220 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000222 }
223
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100224 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000225 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000228 return;
229 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200230#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000231
Simon Butcher327398a2016-10-05 14:09:11 +0100232#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100234 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100236 }
237#endif
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100240 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200244}
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
247 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248{
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (cipher_info == NULL) {
250 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
251 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254
Valerio Settibbc46b42023-10-26 09:00:21 +0200255 if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
256 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
257 if (ctx->cipher_ctx == NULL) {
258 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
259 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261
262 ctx->cipher_info = cipher_info;
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265}
266
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200267#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100268int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
269 const mbedtls_cipher_info_t *cipher_info,
270 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000271{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000272 psa_algorithm_t alg;
273 mbedtls_cipher_context_psa *cipher_psa;
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (NULL == cipher_info || NULL == ctx) {
276 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
277 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000278
Hanno Becker4ee7e762018-11-17 22:00:38 +0000279 /* Check that the underlying cipher mode and cipher type are
280 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100281 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (alg == 0) {
283 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
284 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100285 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
287 }
Hanno Becker6118e432018-11-09 16:47:20 +0000288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
292 if (cipher_psa == NULL) {
293 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
294 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000295 cipher_psa->alg = alg;
296 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000297 ctx->cipher_info = cipher_info;
298 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000300}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200301#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
304 const unsigned char *key,
305 int key_bitlen,
306 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307{
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100309 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 }
311 if (ctx->cipher_info == NULL) {
312 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
313 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800314#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Yanray Wang4995e0c2023-11-07 17:50:52 +0800315 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
316 MBEDTLS_DECRYPT == operation) {
317 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
318 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800319#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200321#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000323 mbedtls_cipher_context_psa * const cipher_psa =
324 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000327
328 psa_status_t status;
329 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200330 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000331
332 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (key_bitlen % 8 != 0) {
334 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
335 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000336
337 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
339 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
340 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000341
Andrzej Kurekc7509322019-01-08 09:36:01 -0500342 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100343 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (key_type == 0) {
345 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
346 }
347 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000348
349 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500350 * (encrypt vs. decrypt): it is possible to setup a key for encryption
351 * and use it for AEAD decryption. Until tests relying on this
352 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
355 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 status = psa_import_key(&attributes, key, key_bytelen,
358 &cipher_psa->slot);
359 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200360 case PSA_SUCCESS:
361 break;
362 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200364 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200366 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200368 }
369 /* Indicate that we own the key slot and need to
370 * destroy it in mbedtls_cipher_free(). */
371 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000372
373 ctx->key_bitlen = key_bitlen;
374 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000376 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200377#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100380 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200382 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200383
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200384 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 ctx->operation = operation;
386
Yanray Wangb67b4742023-10-31 17:10:32 +0800387#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker343a8702011-06-09 14:27:58 +0000388 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100389 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000390 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100392 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
393 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
394 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100395 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100396 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000397 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100400 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100401 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800403#else
404 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
405 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
406 ctx->key_bitlen);
407 }
408#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411}
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
414 const unsigned char *iv,
415 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200417 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (ctx->cipher_info == NULL) {
420 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
421 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200422#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000424 /* While PSA Crypto has an API for multipart
425 * operations, we currently don't make it
426 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000428 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200429#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000430
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200431 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
433 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
434 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200437 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100439 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200440
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200441 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (actual_iv_size > iv_len) {
443 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
444 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200445 }
446
Daniel Kingbd920622016-05-15 19:56:20 -0300447#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100448 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100449 /* Even though the actual_iv_size is overwritten with a correct value
450 * of 12 from the cipher info, return an error to indicate that
451 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (iv_len != 12) {
453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
454 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
457 iv,
458 0U)) { /* Initial counter value */
459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300460 }
461 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100462#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100463 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 iv_len != 12) {
465 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
466 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100467#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300468#endif
469
Gilles Peskine295fc132021-04-15 18:32:23 +0200470#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100471 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
473 ctx->operation,
474 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200475 }
476#endif
477
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200478#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100479 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200480 int set_lengths_result;
481 int ccm_star_mode;
482
483 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 (mbedtls_ccm_context *) ctx->cipher_ctx,
485 0, 0, 0);
486 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200487 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200491 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200493 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200495 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
499 ccm_star_mode,
500 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200501 }
502#endif
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (actual_iv_size != 0) {
505 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300506 ctx->iv_size = actual_iv_size;
507 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200510}
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200513{
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (ctx->cipher_info == NULL) {
515 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
516 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200517
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200518#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000520 /* We don't support resetting PSA-based
521 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000523 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200524#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000525
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526 ctx->unprocessed_len = 0;
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200529}
530
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200531#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100532int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
533 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200534{
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (ctx->cipher_info == NULL) {
536 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
537 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200538
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200539#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000541 /* While PSA Crypto has an API for multipart
542 * operations, we currently don't make it
543 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000545 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200546#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000547
Daniel King8fe47012016-05-17 20:33:28 -0300548#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100549 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
551 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200552 }
Daniel King8fe47012016-05-17 20:33:28 -0300553#endif
554
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200555#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100556 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300557 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200558 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200561 ? MBEDTLS_CHACHAPOLY_ENCRYPT
562 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
565 ctx->iv,
566 mode);
567 if (result != 0) {
568 return result;
569 }
Daniel King8fe47012016-05-17 20:33:28 -0300570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
572 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300573 }
574#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200578#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
581 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582{
Janos Follath24eed8d2019-11-22 13:21:35 +0000583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500584 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ctx->cipher_info == NULL) {
587 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
588 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200590#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000592 /* While PSA Crypto has an API for multipart
593 * operations, we currently don't make it
594 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000596 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200597#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000598
Paul Bakker6c212762013-12-16 15:24:50 +0100599 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 block_size = mbedtls_cipher_get_block_size(ctx);
601 if (0 == block_size) {
602 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100603 }
Paul Bakker6c212762013-12-16 15:24:50 +0100604
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100605 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (ilen != block_size) {
607 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
608 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200609
610 *olen = ilen;
611
Dave Rodgmande3de772023-06-24 12:51:06 +0100612 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100613 ctx->operation, input,
614 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200616 }
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200619 }
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100622 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
624 input, ilen,
625 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200626 }
627#endif
628
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200629#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100630 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
632 input, ilen,
633 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200634 }
635#endif
636
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200637#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100638 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200639 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
641 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200642 }
643#endif
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (input == output &&
646 (ctx->unprocessed_len != 0 || ilen % block_size)) {
647 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100648 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100651 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200652 size_t copy_len = 0;
653
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 /*
655 * If there is not enough data for a full block, cache it.
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
658 ilen <= block_size - ctx->unprocessed_len) ||
659 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
660 ilen < block_size - ctx->unprocessed_len) ||
661 (ctx->operation == MBEDTLS_ENCRYPT &&
662 ilen < block_size - ctx->unprocessed_len)) {
663 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
664 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000665
666 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 }
669
670 /*
671 * Process cached data first
672 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100674 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
677 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
Dave Rodgmande3de772023-06-24 12:51:06 +0100679 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100680 ctx->operation,
681 block_size, ctx->iv,
682 ctx->
683 unprocessed_data,
684 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686 }
687
Janos Follath98e28a72016-05-31 14:03:54 +0100688 *olen += block_size;
689 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000690 ctx->unprocessed_len = 0;
691
692 input += copy_len;
693 ilen -= copy_len;
694 }
695
696 /*
697 * Cache final, incomplete block
698 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700700 /* Encryption: only cache partial blocks
701 * Decryption w/ padding: always keep at least one whole block
702 * Decryption w/o padding: only cache partial blocks
703 */
Janos Follath98e28a72016-05-31 14:03:54 +0100704 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700706 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100708 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700709 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
712 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713
714 ctx->unprocessed_len += copy_len;
715 ilen -= copy_len;
716 }
717
718 /*
719 * Process remaining full blocks
720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100722 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100723 ctx->operation,
724 ilen, ctx->iv,
725 input,
726 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200729
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730 *olen += ilen;
731 }
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100738 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100739 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100740 ctx->operation, ilen,
741 &ctx->unprocessed_len,
742 ctx->iv,
743 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000745 }
746
747 *olen = ilen;
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000752
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100753#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100754 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100755 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100756 ilen,
757 &ctx->unprocessed_len,
758 ctx->iv,
759 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100761 }
762
763 *olen = ilen;
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100766 }
767#endif /* MBEDTLS_CIPHER_MODE_OFB */
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100770 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100771 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100772 ilen,
773 &ctx->unprocessed_len,
774 ctx->iv,
775 ctx->unprocessed_data,
776 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000778 }
779
780 *olen = ilen;
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000783 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000785
Jaeden Ameroc6539902018-04-30 17:17:41 +0100786#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100787 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100789 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100791 }
792
Dave Rodgmande3de772023-06-24 12:51:06 +0100793 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100794 ctx->operation,
795 ilen,
796 ctx->iv,
797 input,
798 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (ret != 0) {
800 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100801 }
802
803 *olen = ilen;
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100806 }
807#endif /* MBEDTLS_CIPHER_MODE_XTS */
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100810 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100811 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100812 ilen, input,
813 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200815 }
816
817 *olen = ilen;
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000824}
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
827#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200828/*
829 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831static void add_pkcs_padding(unsigned char *output, size_t output_len,
832 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000833{
Paul Bakker23986e52011-04-24 08:57:21 +0000834 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100835 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000838 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000840}
841
David Horstmann850e5b32025-03-28 17:33:45 +0000842/*
843 * Get the length of the PKCS7 padding.
844 *
845 * Note: input_len must be the block size of the cipher.
846 */
David Horstmannab7bb572025-03-05 18:05:04 +0000847MBEDTLS_STATIC_TESTABLE int mbedtls_get_pkcs_padding(unsigned char *input,
848 size_t input_len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200849 size_t *data_len,
850 size_t *invalid_padding)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100852 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100853 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (NULL == input || NULL == data_len) {
856 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
857 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858
859 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100861 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100862 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100863
864 /* The number of bytes checked must be independent of padding_len,
865 * so pick input_len, which is usually 8 or 16 (one block) */
866 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100868 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
869 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
870 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100872
David Horstmann652ea212025-01-20 17:44:00 +0000873 /* If the padding is invalid, set the output length to 0 */
874 *data_len = mbedtls_ct_if(bad, 0, input_len - padding_len);
875
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200876 *invalid_padding = mbedtls_ct_size_if_else_0(bad, SIZE_MAX);
877 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200882/*
883 * One and zeros padding: fill with 80 00 ... 00
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885static void add_one_and_zeros_padding(unsigned char *output,
886 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200887{
888 size_t padding_len = output_len - data_len;
889 unsigned char i = 0;
890
891 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200893 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200895}
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200898 size_t *data_len, size_t *invalid_padding)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200899{
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (NULL == input || NULL == data_len) {
901 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
902 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200903
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100904 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
905 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
906
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100907 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100908
Dave Rodgman437500c2023-09-19 21:36:43 +0100909 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100910 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
911
912 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
913
914 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
915
Dave Rodgmanfd965792023-09-19 21:51:50 +0100916 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100917
918 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100919 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200920
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200921 *invalid_padding = mbedtls_ct_size_if_else_0(bad, SIZE_MAX);
922 return 0;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200923}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200927/*
928 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
929 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930static void add_zeros_and_len_padding(unsigned char *output,
931 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200932{
933 size_t padding_len = output_len - data_len;
934 unsigned char i = 0;
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200937 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200939 output[output_len - 1] = (unsigned char) padding_len;
940}
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200943 size_t *data_len, size_t *invalid_padding)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200944{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100945 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100946 unsigned char padding_len;
947 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (NULL == input || NULL == data_len) {
950 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
951 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200952
953 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200954 *data_len = input_len - padding_len;
955
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100956 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100957 bad = mbedtls_ct_uint_gt(padding_len, input_len);
958 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100959
960 /* The number of bytes checked must be independent of padding_len */
961 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100963 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100964 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100965 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100966 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100968
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200969 *invalid_padding = mbedtls_ct_size_if_else_0(bad, SIZE_MAX);
970 return 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200975/*
976 * Zero padding: fill with 00 ... 00
977 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978static void add_zeros_padding(unsigned char *output,
979 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200980{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100981 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200982}
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984static int get_zeros_padding(unsigned char *input, size_t input_len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +0200985 size_t *data_len, size_t *invalid_padding)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200986{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100987 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100988 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (NULL == input || NULL == data_len) {
991 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100992 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 *data_len = 0;
995 for (i = input_len; i > 0; i--) {
996 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100997 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
998 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 }
1000
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001001 *invalid_padding = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001005
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001006/*
1007 * No padding: don't pad :)
1008 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001010 * but a trivial get_padding function
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int get_no_padding(unsigned char *input, size_t input_len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001013 size_t *data_len, size_t *invalid_padding)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001014{
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (NULL == input || NULL == data_len) {
1016 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1017 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001018
1019 *data_len = input_len;
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001020 *invalid_padding = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001024
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001025int mbedtls_cipher_finish_padded(mbedtls_cipher_context_t *ctx,
1026 unsigned char *output, size_t *olen,
1027 size_t *invalid_padding)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028{
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (ctx->cipher_info == NULL) {
1030 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1031 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001032
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001033#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001035 /* While PSA Crypto has an API for multipart
1036 * operations, we currently don't make it
1037 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001039 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001040#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001041
Paul Bakker8123e9d2011-01-06 15:37:30 +00001042 *olen = 0;
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001043 *invalid_padding = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001044
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001045#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1046 /* CBC mode requires padding so we make sure a call to
1047 * mbedtls_cipher_set_padding_mode has been done successfully. */
1048 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1049 if (ctx->get_padding == NULL) {
1050 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1051 }
1052 }
1053#endif
1054
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001055 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1056 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1057 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1058 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1059 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1060 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1061 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001063 }
1064
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001065 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1066 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001068 }
1069
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001070 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (ctx->unprocessed_len != 0) {
1072 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1073 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001076 }
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001079 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001080 int ret = 0;
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001083 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (NULL == ctx->add_padding) {
1085 if (0 != ctx->unprocessed_len) {
1086 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1087 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001090 }
1091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1093 ctx->unprocessed_len);
1094 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001095 /*
1096 * For decrypt operations, expect a full block,
1097 * or an empty block if no padding
1098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1100 return 0;
1101 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001104 }
1105
1106 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001107 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001108 ctx->operation,
1109 mbedtls_cipher_get_block_size(
1110 ctx),
1111 ctx->iv,
1112 ctx->unprocessed_data,
1113 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001115 }
1116
1117 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (MBEDTLS_DECRYPT == ctx->operation) {
1119 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001120 olen, invalid_padding);
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001122
1123 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 *olen = mbedtls_cipher_get_block_size(ctx);
1125 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001126 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001127#else
1128 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001132}
1133
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001134int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1135 unsigned char *output, size_t *olen)
Gilles Peskine155de2a2025-07-27 18:53:57 +02001136{
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001137 size_t invalid_padding = 0;
1138 int ret = mbedtls_cipher_finish_padded(ctx, output, olen,
1139 &invalid_padding);
1140 if (ret == 0) {
1141 ret = mbedtls_ct_error_if_else_0(invalid_padding,
1142 MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Gilles Peskine155de2a2025-07-27 18:53:57 +02001143 }
Gilles Peskine155de2a2025-07-27 18:53:57 +02001144 return ret;
1145}
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1149 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001150{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001151 if (NULL == ctx->cipher_info ||
1152 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001154 }
1155
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001156#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001158 /* While PSA Crypto knows about CBC padding
1159 * schemes, we currently don't make them
1160 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (mode != MBEDTLS_PADDING_NONE) {
1162 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1163 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001166 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001167#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 case MBEDTLS_PADDING_PKCS7:
1172 ctx->add_padding = add_pkcs_padding;
David Horstmann5a5440e2025-03-28 10:57:45 +00001173 ctx->get_padding = mbedtls_get_pkcs_padding;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1178 ctx->add_padding = add_one_and_zeros_padding;
1179 ctx->get_padding = get_one_and_zeros_padding;
1180 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1184 ctx->add_padding = add_zeros_and_len_padding;
1185 ctx->get_padding = get_zeros_and_len_padding;
1186 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 case MBEDTLS_PADDING_ZEROS:
1190 ctx->add_padding = add_zeros_padding;
1191 ctx->get_padding = get_zeros_padding;
1192 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001193#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 case MBEDTLS_PADDING_NONE:
1195 ctx->add_padding = NULL;
1196 ctx->get_padding = get_no_padding;
1197 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 default:
1200 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001201 }
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001206
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001207#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001208int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1209 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001210{
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (ctx->cipher_info == NULL) {
1212 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1213 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (MBEDTLS_ENCRYPT != ctx->operation) {
1216 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1217 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001218
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001219#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001221 /* While PSA Crypto has an API for multipart
1222 * operations, we currently don't make it
1223 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001225 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001226#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001227
Daniel King8fe47012016-05-17 20:33:28 -03001228#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001229 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001230 size_t output_length;
1231 /* The code here doesn't yet support alternative implementations
1232 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1234 NULL, 0, &output_length,
1235 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001236 }
Daniel King8fe47012016-05-17 20:33:28 -03001237#endif
1238
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001239#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001240 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001241 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (tag_len != 16U) {
1243 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1244 }
Daniel King8fe47012016-05-17 20:33:28 -03001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 return mbedtls_chachapoly_finish(
1247 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001248 }
1249#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001252}
Paul Bakker9af723c2014-05-01 13:03:14 +02001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1255 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001256{
Daniel King8fe47012016-05-17 20:33:28 -03001257 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (ctx->cipher_info == NULL) {
1261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1262 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (MBEDTLS_DECRYPT != ctx->operation) {
1265 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001266 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001267
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001268#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001270 /* While PSA Crypto has an API for multipart
1271 * operations, we currently don't make it
1272 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001274 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001275#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001276
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001277 /* Status to return on a non-authenticated algorithm. */
1278 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001279
Daniel King8fe47012016-05-17 20:33:28 -03001280#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001281 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001282 size_t output_length;
1283 /* The code here doesn't yet support alternative implementations
1284 * that can delay up to a block of output. */
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (tag_len > sizeof(check_tag)) {
1287 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1288 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (0 != (ret = mbedtls_gcm_finish(
1291 (mbedtls_gcm_context *) ctx->cipher_ctx,
1292 NULL, 0, &output_length,
1293 check_tag, tag_len))) {
1294 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001295 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001296
1297 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001299 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001300 goto exit;
1301 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001302 }
Daniel King8fe47012016-05-17 20:33:28 -03001303#endif /* MBEDTLS_GCM_C */
1304
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001305#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001306 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001307 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (tag_len != sizeof(check_tag)) {
1309 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1310 }
Daniel King8fe47012016-05-17 20:33:28 -03001311
Hanno Becker18597cd2018-11-09 16:36:33 +00001312 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1314 if (ret != 0) {
1315 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001316 }
1317
1318 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001320 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001321 goto exit;
1322 }
Daniel King8fe47012016-05-17 20:33:28 -03001323 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001324#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001325
Gilles Peskinecd742982021-12-13 16:57:47 +01001326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_platform_zeroize(check_tag, tag_len);
1328 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001329}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001330#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001331
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001332/*
1333 * Packet-oriented wrapper for non-AEAD modes
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1336 const unsigned char *iv, size_t iv_len,
1337 const unsigned char *input, size_t ilen,
1338 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001339{
Janos Follath24eed8d2019-11-22 13:21:35 +00001340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001341 size_t finish_olen;
1342
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001343#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001345 /* As in the non-PSA case, we don't check that
1346 * a key has been set. If not, the key slot will
1347 * still be in its default state of 0, which is
1348 * guaranteed to be invalid, hence the PSA-call
1349 * below will gracefully fail. */
1350 mbedtls_cipher_context_psa * const cipher_psa =
1351 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1352
1353 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001354 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001355 size_t part_len;
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (ctx->operation == MBEDTLS_DECRYPT) {
1358 status = psa_cipher_decrypt_setup(&cipher_op,
1359 cipher_psa->slot,
1360 cipher_psa->alg);
1361 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1362 status = psa_cipher_encrypt_setup(&cipher_op,
1363 cipher_psa->slot,
1364 cipher_psa->alg);
1365 } else {
1366 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001367 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001368
1369 /* In the following, we can immediately return on an error,
1370 * because the PSA Crypto API guarantees that cipher operations
1371 * are terminated by unsuccessful calls to psa_cipher_update(),
1372 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (status != PSA_SUCCESS) {
1374 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001375 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001376
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001377 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1379 if (status != PSA_SUCCESS) {
1380 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1381 }
1382 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 status = psa_cipher_update(&cipher_op,
1385 input, ilen,
1386 output, ilen, olen);
1387 if (status != PSA_SUCCESS) {
1388 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1389 }
1390
1391 status = psa_cipher_finish(&cipher_op,
1392 output + *olen, ilen - *olen,
1393 &part_len);
1394 if (status != PSA_SUCCESS) {
1395 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1396 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001397
1398 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001400 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001401#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1404 return ret;
1405 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1408 return ret;
1409 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1412 output, olen)) != 0) {
1413 return ret;
1414 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001415
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001416 size_t invalid_padding = 0;
1417 if ((ret = mbedtls_cipher_finish_padded(ctx, output + *olen,
1418 &finish_olen,
1419 &invalid_padding)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return ret;
1421 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001422 *olen += finish_olen;
1423
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001424 ret = mbedtls_ct_error_if_else_0(invalid_padding,
1425 MBEDTLS_ERR_CIPHER_INVALID_PADDING);
1426 return ret;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001427}
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001430/*
TRodziewicz18efb732021-04-29 23:12:19 +02001431 * Packet-oriented encryption for AEAD modes: internal function used by
1432 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001434static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1435 const unsigned char *iv, size_t iv_len,
1436 const unsigned char *ad, size_t ad_len,
1437 const unsigned char *input, size_t ilen,
1438 unsigned char *output, size_t *olen,
1439 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001440{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001441#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001443 /* As in the non-PSA case, we don't check that
1444 * a key has been set. If not, the key slot will
1445 * still be in its default state of 0, which is
1446 * guaranteed to be invalid, hence the PSA-call
1447 * below will gracefully fail. */
1448 mbedtls_cipher_context_psa * const cipher_psa =
1449 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1450
1451 psa_status_t status;
1452
1453 /* PSA Crypto API always writes the authentication tag
1454 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (output == NULL || tag != output + ilen) {
1456 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1457 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 status = psa_aead_encrypt(cipher_psa->slot,
1460 cipher_psa->alg,
1461 iv, iv_len,
1462 ad, ad_len,
1463 input, ilen,
1464 output, ilen + tag_len, olen);
1465 if (status != PSA_SUCCESS) {
1466 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1467 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001468
1469 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001471 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001472#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001475 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001476 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1478 ilen, iv, iv_len, ad, ad_len,
1479 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001480 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#endif /* MBEDTLS_GCM_C */
1482#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001483 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001484 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1486 iv, iv_len, ad, ad_len, input, output,
1487 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001490#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001491 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001492 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001493 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 (tag_len != 16U)) {
1495 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001496 }
1497
1498 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1500 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001501 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001502#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001505}
1506
1507/*
TRodziewicz18efb732021-04-29 23:12:19 +02001508 * Packet-oriented encryption for AEAD modes: internal function used by
1509 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1512 const unsigned char *iv, size_t iv_len,
1513 const unsigned char *ad, size_t ad_len,
1514 const unsigned char *input, size_t ilen,
1515 unsigned char *output, size_t *olen,
1516 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001517{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001518#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001520 /* As in the non-PSA case, we don't check that
1521 * a key has been set. If not, the key slot will
1522 * still be in its default state of 0, which is
1523 * guaranteed to be invalid, hence the PSA-call
1524 * below will gracefully fail. */
1525 mbedtls_cipher_context_psa * const cipher_psa =
1526 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1527
1528 psa_status_t status;
1529
1530 /* PSA Crypto API always writes the authentication tag
1531 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (input == NULL || tag != input + ilen) {
1533 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1534 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 status = psa_aead_decrypt(cipher_psa->slot,
1537 cipher_psa->alg,
1538 iv, iv_len,
1539 ad, ad_len,
1540 input, ilen + tag_len,
1541 output, ilen, olen);
1542 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1543 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1544 } else if (status != PSA_SUCCESS) {
1545 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1546 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001549 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001550#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001553 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001555
1556 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1558 iv, iv_len, ad, ad_len,
1559 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_GCM_C */
1568#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001569 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001571
1572 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1574 iv, iv_len, ad, ad_len,
1575 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001582 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001584#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001585 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001587
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001588 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001589 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 (tag_len != 16U)) {
1591 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001592 }
1593
1594 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1596 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001599 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 }
Daniel King8fe47012016-05-17 20:33:28 -03001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001603 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001604#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001609
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001610#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1611/*
1612 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1615 const unsigned char *iv, size_t iv_len,
1616 const unsigned char *ad, size_t ad_len,
1617 const unsigned char *input, size_t ilen,
1618 unsigned char *output, size_t output_len,
1619 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001620{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001621#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001623#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001624 ctx->psa_enabled == 0 &&
1625#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001626 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1627 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1628 mbedtls_nist_kw_mode_t mode =
1629 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1630 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001631
1632 /* There is no iv, tag or ad associated with KW and KWP,
1633 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1635 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1636 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001637
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001638 (void) iv;
1639 (void) ad;
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1642 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001643 }
1644#endif /* MBEDTLS_NIST_KW_C */
1645
1646#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1647 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (output_len < ilen + tag_len) {
1649 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1650 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1653 input, ilen, output, olen,
1654 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001655 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001657#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001659#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1660}
1661
1662/*
1663 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1666 const unsigned char *iv, size_t iv_len,
1667 const unsigned char *ad, size_t ad_len,
1668 const unsigned char *input, size_t ilen,
1669 unsigned char *output, size_t output_len,
1670 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001671{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001672#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001674#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001675 ctx->psa_enabled == 0 &&
1676#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001677 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1678 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1679 mbedtls_nist_kw_mode_t mode =
1680 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1681 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001682
1683 /* There is no iv, tag or ad associated with KW and KWP,
1684 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1686 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1687 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001688
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001689 (void) iv;
1690 (void) ad;
1691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1693 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001694 }
1695#endif /* MBEDTLS_NIST_KW_C */
1696
1697#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1698 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 if (ilen < tag_len || output_len < ilen - tag_len) {
1700 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1701 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1704 input, ilen - tag_len, output, olen,
1705 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001706#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001708#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1709}
1710#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_CIPHER_C */