blob: e9ad2ba96a3f234447bd983ec9c8b87ba2e47182 [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 Rodgmane3c05852023-11-03 12:21:36 +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"
Chris Jonesdaacb592021-03-09 17:03:29 +000017#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050018#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000019#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020020#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010021#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000022
Rich Evans00ab4702015-02-06 13:43:58 +000023#include <stdlib.h>
24#include <string.h>
25
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020026#if defined(MBEDTLS_CHACHAPOLY_C)
27#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030028#endif
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020036#endif
37
Daniel Kingbd920622016-05-15 19:56:20 -030038#if defined(MBEDTLS_CHACHA20_C)
39#include "mbedtls/chacha20.h"
40#endif
41
Simon Butcher327398a2016-10-05 14:09:11 +010042#if defined(MBEDTLS_CMAC_C)
43#include "mbedtls/cmac.h"
44#endif
45
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020046#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000047#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020048#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000049
Jack Lloydffdf2882019-03-07 17:00:32 -050050#if defined(MBEDTLS_NIST_KW_C)
51#include "mbedtls/nist_kw.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010055
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020056static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000057
Dave Rodgman3b46b772023-06-24 13:25:06 +010058static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
59 const mbedtls_cipher_info_t *info)
60{
Dave Rodgmande3de772023-06-24 12:51:06 +010061 return mbedtls_cipher_base_lookup_table[info->base_idx];
62}
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067 int *type;
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 def = mbedtls_cipher_definitions;
71 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020074 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020076
77 *type = 0;
78
79 supported_init = 1;
80 }
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000083}
84
Hanno Becker18597cd2018-11-09 16:36:33 +000085const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010086 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
91 if (def->type == cipher_type) {
92 return def->info;
93 }
94 }
Paul Bakker343a8702011-06-09 14:27:58 +000095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000097}
98
Hanno Becker18597cd2018-11-09 16:36:33 +000099const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if (NULL == cipher_name) {
105 return NULL;
106 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
109 if (!strcmp(def->info->name, cipher_name)) {
110 return def->info;
111 }
112 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115}
116
Hanno Becker18597cd2018-11-09 16:36:33 +0000117const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
118 const mbedtls_cipher_id_t cipher_id,
119 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100125 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100126 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 def->info->mode == mode) {
128 return def->info;
129 }
130 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133}
134
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200135#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
136static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
137 mbedtls_cipher_type_t cipher)
138{
139 switch (cipher) {
140 case MBEDTLS_CIPHER_AES_128_CCM:
141 case MBEDTLS_CIPHER_AES_192_CCM:
142 case MBEDTLS_CIPHER_AES_256_CCM:
143 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
144 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
145 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
146 case MBEDTLS_CIPHER_AES_128_GCM:
147 case MBEDTLS_CIPHER_AES_192_GCM:
148 case MBEDTLS_CIPHER_AES_256_GCM:
149 case MBEDTLS_CIPHER_AES_128_CBC:
150 case MBEDTLS_CIPHER_AES_192_CBC:
151 case MBEDTLS_CIPHER_AES_256_CBC:
152 case MBEDTLS_CIPHER_AES_128_ECB:
153 case MBEDTLS_CIPHER_AES_192_ECB:
154 case MBEDTLS_CIPHER_AES_256_ECB:
155 return PSA_KEY_TYPE_AES;
156
157 /* ARIA not yet supported in PSA. */
158 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
159 case MBEDTLS_CIPHER_ARIA_192_CCM:
160 case MBEDTLS_CIPHER_ARIA_256_CCM:
161 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
162 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
163 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
164 case MBEDTLS_CIPHER_ARIA_128_GCM:
165 case MBEDTLS_CIPHER_ARIA_192_GCM:
166 case MBEDTLS_CIPHER_ARIA_256_GCM:
167 case MBEDTLS_CIPHER_ARIA_128_CBC:
168 case MBEDTLS_CIPHER_ARIA_192_CBC:
169 case MBEDTLS_CIPHER_ARIA_256_CBC:
170 return( PSA_KEY_TYPE_ARIA ); */
171
172 default:
173 return 0;
174 }
175}
176
177static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
178 mbedtls_cipher_mode_t mode, size_t taglen)
179{
180 switch (mode) {
181 case MBEDTLS_MODE_ECB:
182 return PSA_ALG_ECB_NO_PADDING;
183 case MBEDTLS_MODE_GCM:
184 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
185 case MBEDTLS_MODE_CCM:
186 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
187 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
188 return PSA_ALG_CCM_STAR_NO_TAG;
189 case MBEDTLS_MODE_CBC:
190 if (taglen == 0) {
191 return PSA_ALG_CBC_NO_PADDING;
192 } else {
193 return 0;
194 }
195 default:
196 return 0;
197 }
198}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200199#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200202{
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200209 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200211
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200212#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 if (ctx->psa_enabled == 1) {
214 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000215 mbedtls_cipher_context_psa * const cipher_psa =
216 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000219 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000221 }
222
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100223 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000224 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000227 return;
228 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200229#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000230
Simon Butcher327398a2016-10-05 14:09:11 +0100231#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100233 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100235 }
236#endif
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100239 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200243}
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
246 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247{
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (cipher_info == NULL) {
249 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
250 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253
Dave Rodgmande3de772023-06-24 12:51:06 +0100254 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
256 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000257
258 ctx->cipher_info = cipher_info;
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261}
262
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200263#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100264int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
265 const mbedtls_cipher_info_t *cipher_info,
266 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000267{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000268 psa_algorithm_t alg;
269 mbedtls_cipher_context_psa *cipher_psa;
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (NULL == cipher_info || NULL == ctx) {
272 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
273 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000274
Hanno Becker4ee7e762018-11-17 22:00:38 +0000275 /* Check that the underlying cipher mode and cipher type are
276 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100277 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (alg == 0) {
279 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
280 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100281 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
283 }
Hanno Becker6118e432018-11-09 16:47:20 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
288 if (cipher_psa == NULL) {
289 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
290 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000291 cipher_psa->alg = alg;
292 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000293 ctx->cipher_info = cipher_info;
294 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000296}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200297#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
300 const unsigned char *key,
301 int key_bitlen,
302 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303{
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100305 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 }
307 if (ctx->cipher_info == NULL) {
308 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
309 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200311#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000313 mbedtls_cipher_context_psa * const cipher_psa =
314 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000317
318 psa_status_t status;
319 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000321
322 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (key_bitlen % 8 != 0) {
324 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
325 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000326
327 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
329 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
330 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000331
Andrzej Kurekc7509322019-01-08 09:36:01 -0500332 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100333 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (key_type == 0) {
335 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
336 }
337 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000338
339 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500340 * (encrypt vs. decrypt): it is possible to setup a key for encryption
341 * and use it for AEAD decryption. Until tests relying on this
342 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
345 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 status = psa_import_key(&attributes, key, key_bytelen,
348 &cipher_psa->slot);
349 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200350 case PSA_SUCCESS:
351 break;
352 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200354 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200356 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200358 }
359 /* Indicate that we own the key slot and need to
360 * destroy it in mbedtls_cipher_free(). */
361 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000362
363 ctx->key_bitlen = key_bitlen;
364 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000366 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200367#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100370 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200372 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200373
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200374 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000375 ctx->operation = operation;
376
Paul Bakker343a8702011-06-09 14:27:58 +0000377 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100378 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000379 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100381 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
382 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
383 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100384 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100385 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000386 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100389 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100390 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394}
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
397 const unsigned char *iv,
398 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200400 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (ctx->cipher_info == NULL) {
403 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
404 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200405#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000407 /* While PSA Crypto has an API for multipart
408 * operations, we currently don't make it
409 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000411 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200412#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000413
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200414 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
416 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
417 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200420 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100422 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200423
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200424 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (actual_iv_size > iv_len) {
426 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
427 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200428 }
429
Daniel Kingbd920622016-05-15 19:56:20 -0300430#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100431 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100432 /* Even though the actual_iv_size is overwritten with a correct value
433 * of 12 from the cipher info, return an error to indicate that
434 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (iv_len != 12) {
436 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
437 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
440 iv,
441 0U)) { /* Initial counter value */
442 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300443 }
444 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100445#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100446 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 iv_len != 12) {
448 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
449 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100450#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300451#endif
452
Gilles Peskine295fc132021-04-15 18:32:23 +0200453#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100454 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
456 ctx->operation,
457 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200458 }
459#endif
460
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200461#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100462 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200463 int set_lengths_result;
464 int ccm_star_mode;
465
466 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 (mbedtls_ccm_context *) ctx->cipher_ctx,
468 0, 0, 0);
469 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200470 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200474 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200476 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200478 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
482 ccm_star_mode,
483 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200484 }
485#endif
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (actual_iv_size != 0) {
488 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300489 ctx->iv_size = actual_iv_size;
490 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200493}
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200496{
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (ctx->cipher_info == NULL) {
498 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
499 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200500
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200501#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000503 /* We don't support resetting PSA-based
504 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000506 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200507#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000508
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509 ctx->unprocessed_len = 0;
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200512}
513
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200514#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
516 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200517{
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (ctx->cipher_info == NULL) {
519 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
520 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200521
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200522#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000524 /* While PSA Crypto has an API for multipart
525 * operations, we currently don't make it
526 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000528 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200529#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000530
Daniel King8fe47012016-05-17 20:33:28 -0300531#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100532 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
534 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200535 }
Daniel King8fe47012016-05-17 20:33:28 -0300536#endif
537
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200538#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100539 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300540 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200541 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200544 ? MBEDTLS_CHACHAPOLY_ENCRYPT
545 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
548 ctx->iv,
549 mode);
550 if (result != 0) {
551 return result;
552 }
Daniel King8fe47012016-05-17 20:33:28 -0300553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
555 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300556 }
557#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200561#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
564 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565{
Janos Follath24eed8d2019-11-22 13:21:35 +0000566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (ctx->cipher_info == NULL) {
570 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
571 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200573#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000575 /* While PSA Crypto has an API for multipart
576 * operations, we currently don't make it
577 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000579 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200580#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000581
Paul Bakker6c212762013-12-16 15:24:50 +0100582 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 block_size = mbedtls_cipher_get_block_size(ctx);
584 if (0 == block_size) {
585 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100586 }
Paul Bakker6c212762013-12-16 15:24:50 +0100587
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100588 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ilen != block_size) {
590 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
591 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200592
593 *olen = ilen;
594
Dave Rodgmande3de772023-06-24 12:51:06 +0100595 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100596 ctx->operation, input,
597 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200602 }
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100605 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
607 input, ilen,
608 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200609 }
610#endif
611
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200612#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100613 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
615 input, ilen,
616 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200617 }
618#endif
619
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200620#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100621 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200622 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
624 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200625 }
626#endif
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (input == output &&
629 (ctx->unprocessed_len != 0 || ilen % block_size)) {
630 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100631 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100634 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200635 size_t copy_len = 0;
636
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 /*
638 * If there is not enough data for a full block, cache it.
639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
641 ilen <= block_size - ctx->unprocessed_len) ||
642 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
643 ilen < block_size - ctx->unprocessed_len) ||
644 (ctx->operation == MBEDTLS_ENCRYPT &&
645 ilen < block_size - ctx->unprocessed_len)) {
646 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
647 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
649 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 }
652
653 /*
654 * Process cached data first
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100657 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
660 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661
Dave Rodgmande3de772023-06-24 12:51:06 +0100662 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100663 ctx->operation,
664 block_size, ctx->iv,
665 ctx->
666 unprocessed_data,
667 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 }
670
Janos Follath98e28a72016-05-31 14:03:54 +0100671 *olen += block_size;
672 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 ctx->unprocessed_len = 0;
674
675 input += copy_len;
676 ilen -= copy_len;
677 }
678
679 /*
680 * Cache final, incomplete block
681 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700683 /* Encryption: only cache partial blocks
684 * Decryption w/ padding: always keep at least one whole block
685 * Decryption w/o padding: only cache partial blocks
686 */
Janos Follath98e28a72016-05-31 14:03:54 +0100687 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700689 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100691 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700692 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
695 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696
697 ctx->unprocessed_len += copy_len;
698 ilen -= copy_len;
699 }
700
701 /*
702 * Process remaining full blocks
703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100705 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100706 ctx->operation,
707 ilen, ctx->iv,
708 input,
709 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200712
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713 *olen += ilen;
714 }
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100721 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100722 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100723 ctx->operation, ilen,
724 &ctx->unprocessed_len,
725 ctx->iv,
726 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000728 }
729
730 *olen = ilen;
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000735
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100736#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100737 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100738 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100739 ilen,
740 &ctx->unprocessed_len,
741 ctx->iv,
742 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100744 }
745
746 *olen = ilen;
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100749 }
750#endif /* MBEDTLS_CIPHER_MODE_OFB */
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100753 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100754 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100755 ilen,
756 &ctx->unprocessed_len,
757 ctx->iv,
758 ctx->unprocessed_data,
759 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000761 }
762
763 *olen = ilen;
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000768
Jaeden Ameroc6539902018-04-30 17:17:41 +0100769#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100770 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100772 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100774 }
775
Dave Rodgmande3de772023-06-24 12:51:06 +0100776 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100777 ctx->operation,
778 ilen,
779 ctx->iv,
780 input,
781 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ret != 0) {
783 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100784 }
785
786 *olen = ilen;
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100789 }
790#endif /* MBEDTLS_CIPHER_MODE_XTS */
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100793 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100794 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100795 ilen, input,
796 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200798 }
799
800 *olen = ilen;
801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000807}
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
810#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200811/*
812 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
813 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814static void add_pkcs_padding(unsigned char *output, size_t output_len,
815 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000816{
Paul Bakker23986e52011-04-24 08:57:21 +0000817 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100818 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000821 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000823}
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825static int get_pkcs_padding(unsigned char *input, size_t input_len,
826 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100828 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100829 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (NULL == input || NULL == data_len) {
832 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
833 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834
835 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000836 *data_len = input_len - padding_len;
837
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100838 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100839 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100840
841 /* The number of bytes checked must be independent of padding_len,
842 * so pick input_len, which is usually 8 or 16 (one block) */
843 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100845 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
846 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
847 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100849
Dave Rodgmand03f4832023-09-22 09:52:15 +0100850 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200855/*
856 * One and zeros padding: fill with 80 00 ... 00
857 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static void add_one_and_zeros_padding(unsigned char *output,
859 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200860{
861 size_t padding_len = output_len - data_len;
862 unsigned char i = 0;
863
864 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200866 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200868}
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
871 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200872{
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (NULL == input || NULL == data_len) {
874 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
875 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200876
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100877 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
878 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
879
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100880 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100881
Dave Rodgman437500c2023-09-19 21:36:43 +0100882 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100883 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
884
885 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
886
887 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
888
Dave Rodgmanfd965792023-09-19 21:51:50 +0100889 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100890
891 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100892 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200893
Dave Rodgmand03f4832023-09-22 09:52:15 +0100894 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200899/*
900 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902static void add_zeros_and_len_padding(unsigned char *output,
903 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200904{
905 size_t padding_len = output_len - data_len;
906 unsigned char i = 0;
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200909 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200911 output[output_len - 1] = (unsigned char) padding_len;
912}
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
915 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200916{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100917 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100918 unsigned char padding_len;
919 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (NULL == input || NULL == data_len) {
922 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
923 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200924
925 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200926 *data_len = input_len - padding_len;
927
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100928 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100929 bad = mbedtls_ct_uint_gt(padding_len, input_len);
930 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100931
932 /* The number of bytes checked must be independent of padding_len */
933 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100935 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100936 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100937 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100938 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100940
Dave Rodgmand03f4832023-09-22 09:52:15 +0100941 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200946/*
947 * Zero padding: fill with 00 ... 00
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949static void add_zeros_padding(unsigned char *output,
950 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200951{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100952 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200953}
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955static int get_zeros_padding(unsigned char *input, size_t input_len,
956 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200957{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100958 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100959 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (NULL == input || NULL == data_len) {
962 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100963 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 *data_len = 0;
966 for (i = input_len; i > 0; i--) {
967 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100968 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
969 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 }
971
972 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200975
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200976/*
977 * No padding: don't pad :)
978 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200980 * but a trivial get_padding function
981 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982static int get_no_padding(unsigned char *input, size_t input_len,
983 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200984{
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (NULL == input || NULL == data_len) {
986 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
987 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988
989 *data_len = input_len;
990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
996 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (ctx->cipher_info == NULL) {
999 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1000 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001001
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001002#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001004 /* While PSA Crypto has an API for multipart
1005 * operations, we currently don't make it
1006 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001008 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001009#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001010
Paul Bakker8123e9d2011-01-06 15:37:30 +00001011 *olen = 0;
1012
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001013#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1014 /* CBC mode requires padding so we make sure a call to
1015 * mbedtls_cipher_set_padding_mode has been done successfully. */
1016 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1017 if (ctx->get_padding == NULL) {
1018 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1019 }
1020 }
1021#endif
1022
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001023 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1024 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1025 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1026 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1027 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1028 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1029 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001031 }
1032
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001033 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1034 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001036 }
1037
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001038 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (ctx->unprocessed_len != 0) {
1040 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1041 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001044 }
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001047 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001048 int ret = 0;
1049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001051 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (NULL == ctx->add_padding) {
1053 if (0 != ctx->unprocessed_len) {
1054 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1055 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001058 }
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1061 ctx->unprocessed_len);
1062 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001063 /*
1064 * For decrypt operations, expect a full block,
1065 * or an empty block if no padding
1066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1068 return 0;
1069 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001072 }
1073
1074 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001075 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001076 ctx->operation,
1077 mbedtls_cipher_get_block_size(
1078 ctx),
1079 ctx->iv,
1080 ctx->unprocessed_data,
1081 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001083 }
1084
1085 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (MBEDTLS_DECRYPT == ctx->operation) {
1087 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1088 olen);
1089 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001090
1091 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 *olen = mbedtls_cipher_get_block_size(ctx);
1093 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001094 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001095#else
1096 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100}
1101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001103int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1104 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001105{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001106 if (NULL == ctx->cipher_info ||
1107 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001109 }
1110
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001111#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001113 /* While PSA Crypto knows about CBC padding
1114 * schemes, we currently don't make them
1115 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (mode != MBEDTLS_PADDING_NONE) {
1117 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1118 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001121 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001122#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 case MBEDTLS_PADDING_PKCS7:
1127 ctx->add_padding = add_pkcs_padding;
1128 ctx->get_padding = get_pkcs_padding;
1129 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1133 ctx->add_padding = add_one_and_zeros_padding;
1134 ctx->get_padding = get_one_and_zeros_padding;
1135 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1139 ctx->add_padding = add_zeros_and_len_padding;
1140 ctx->get_padding = get_zeros_and_len_padding;
1141 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 case MBEDTLS_PADDING_ZEROS:
1145 ctx->add_padding = add_zeros_padding;
1146 ctx->get_padding = get_zeros_padding;
1147 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001148#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 case MBEDTLS_PADDING_NONE:
1150 ctx->add_padding = NULL;
1151 ctx->get_padding = get_no_padding;
1152 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 default:
1155 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001161
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001162#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1164 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001165{
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (ctx->cipher_info == NULL) {
1167 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1168 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (MBEDTLS_ENCRYPT != ctx->operation) {
1171 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1172 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001173
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001174#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001176 /* While PSA Crypto has an API for multipart
1177 * operations, we currently don't make it
1178 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001180 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001181#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001182
Daniel King8fe47012016-05-17 20:33:28 -03001183#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001184 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001185 size_t output_length;
1186 /* The code here doesn't yet support alternative implementations
1187 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1189 NULL, 0, &output_length,
1190 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001191 }
Daniel King8fe47012016-05-17 20:33:28 -03001192#endif
1193
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001194#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001195 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001196 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 if (tag_len != 16U) {
1198 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1199 }
Daniel King8fe47012016-05-17 20:33:28 -03001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 return mbedtls_chachapoly_finish(
1202 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001203 }
1204#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001207}
Paul Bakker9af723c2014-05-01 13:03:14 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1210 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001211{
Daniel King8fe47012016-05-17 20:33:28 -03001212 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (ctx->cipher_info == NULL) {
1216 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1217 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (MBEDTLS_DECRYPT != ctx->operation) {
1220 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001221 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001222
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001223#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001225 /* While PSA Crypto has an API for multipart
1226 * operations, we currently don't make it
1227 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001229 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001230#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001231
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001232 /* Status to return on a non-authenticated algorithm. */
1233 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001234
Daniel King8fe47012016-05-17 20:33:28 -03001235#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001236 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001237 size_t output_length;
1238 /* The code here doesn't yet support alternative implementations
1239 * that can delay up to a block of output. */
1240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (tag_len > sizeof(check_tag)) {
1242 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1243 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (0 != (ret = mbedtls_gcm_finish(
1246 (mbedtls_gcm_context *) ctx->cipher_ctx,
1247 NULL, 0, &output_length,
1248 check_tag, tag_len))) {
1249 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001250 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001251
1252 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001254 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001255 goto exit;
1256 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001257 }
Daniel King8fe47012016-05-17 20:33:28 -03001258#endif /* MBEDTLS_GCM_C */
1259
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001260#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001261 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001262 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (tag_len != sizeof(check_tag)) {
1264 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1265 }
Daniel King8fe47012016-05-17 20:33:28 -03001266
Hanno Becker18597cd2018-11-09 16:36:33 +00001267 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1269 if (ret != 0) {
1270 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001271 }
1272
1273 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001275 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001276 goto exit;
1277 }
Daniel King8fe47012016-05-17 20:33:28 -03001278 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001279#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001280
Gilles Peskinecd742982021-12-13 16:57:47 +01001281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_platform_zeroize(check_tag, tag_len);
1283 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001284}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001285#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001286
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001287/*
1288 * Packet-oriented wrapper for non-AEAD modes
1289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1291 const unsigned char *iv, size_t iv_len,
1292 const unsigned char *input, size_t ilen,
1293 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001294{
Janos Follath24eed8d2019-11-22 13:21:35 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001296 size_t finish_olen;
1297
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001298#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001300 /* As in the non-PSA case, we don't check that
1301 * a key has been set. If not, the key slot will
1302 * still be in its default state of 0, which is
1303 * guaranteed to be invalid, hence the PSA-call
1304 * below will gracefully fail. */
1305 mbedtls_cipher_context_psa * const cipher_psa =
1306 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1307
1308 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001309 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001310 size_t part_len;
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ctx->operation == MBEDTLS_DECRYPT) {
1313 status = psa_cipher_decrypt_setup(&cipher_op,
1314 cipher_psa->slot,
1315 cipher_psa->alg);
1316 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1317 status = psa_cipher_encrypt_setup(&cipher_op,
1318 cipher_psa->slot,
1319 cipher_psa->alg);
1320 } else {
1321 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001322 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001323
1324 /* In the following, we can immediately return on an error,
1325 * because the PSA Crypto API guarantees that cipher operations
1326 * are terminated by unsuccessful calls to psa_cipher_update(),
1327 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (status != PSA_SUCCESS) {
1329 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001330 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001331
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001332 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1334 if (status != PSA_SUCCESS) {
1335 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1336 }
1337 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 status = psa_cipher_update(&cipher_op,
1340 input, ilen,
1341 output, ilen, olen);
1342 if (status != PSA_SUCCESS) {
1343 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1344 }
1345
1346 status = psa_cipher_finish(&cipher_op,
1347 output + *olen, ilen - *olen,
1348 &part_len);
1349 if (status != PSA_SUCCESS) {
1350 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1351 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001352
1353 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001355 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001356#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1359 return ret;
1360 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1363 return ret;
1364 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1367 output, olen)) != 0) {
1368 return ret;
1369 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1372 &finish_olen)) != 0) {
1373 return ret;
1374 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001375
1376 *olen += finish_olen;
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001379}
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001382/*
TRodziewicz18efb732021-04-29 23:12:19 +02001383 * Packet-oriented encryption for AEAD modes: internal function used by
1384 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001385 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1387 const unsigned char *iv, size_t iv_len,
1388 const unsigned char *ad, size_t ad_len,
1389 const unsigned char *input, size_t ilen,
1390 unsigned char *output, size_t *olen,
1391 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001392{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001393#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001395 /* As in the non-PSA case, we don't check that
1396 * a key has been set. If not, the key slot will
1397 * still be in its default state of 0, which is
1398 * guaranteed to be invalid, hence the PSA-call
1399 * below will gracefully fail. */
1400 mbedtls_cipher_context_psa * const cipher_psa =
1401 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1402
1403 psa_status_t status;
1404
1405 /* PSA Crypto API always writes the authentication tag
1406 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (output == NULL || tag != output + ilen) {
1408 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1409 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 status = psa_aead_encrypt(cipher_psa->slot,
1412 cipher_psa->alg,
1413 iv, iv_len,
1414 ad, ad_len,
1415 input, ilen,
1416 output, ilen + tag_len, olen);
1417 if (status != PSA_SUCCESS) {
1418 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1419 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001420
1421 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001423 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001424#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001427 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001428 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1430 ilen, iv, iv_len, ad, ad_len,
1431 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_GCM_C */
1434#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001435 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001436 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1438 iv, iv_len, ad, ad_len, input, output,
1439 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001442#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001443 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001444 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001445 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 (tag_len != 16U)) {
1447 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001448 }
1449
1450 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1452 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001453 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001454#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001457}
1458
1459/*
TRodziewicz18efb732021-04-29 23:12:19 +02001460 * Packet-oriented encryption for AEAD modes: internal function used by
1461 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1464 const unsigned char *iv, size_t iv_len,
1465 const unsigned char *ad, size_t ad_len,
1466 const unsigned char *input, size_t ilen,
1467 unsigned char *output, size_t *olen,
1468 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001469{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001470#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001472 /* As in the non-PSA case, we don't check that
1473 * a key has been set. If not, the key slot will
1474 * still be in its default state of 0, which is
1475 * guaranteed to be invalid, hence the PSA-call
1476 * below will gracefully fail. */
1477 mbedtls_cipher_context_psa * const cipher_psa =
1478 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1479
1480 psa_status_t status;
1481
1482 /* PSA Crypto API always writes the authentication tag
1483 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (input == NULL || tag != input + ilen) {
1485 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1486 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 status = psa_aead_decrypt(cipher_psa->slot,
1489 cipher_psa->alg,
1490 iv, iv_len,
1491 ad, ad_len,
1492 input, ilen + tag_len,
1493 output, ilen, olen);
1494 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1495 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1496 } else if (status != PSA_SUCCESS) {
1497 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1498 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001501 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001502#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001505 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001507
1508 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1510 iv, iv_len, ad, ad_len,
1511 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001518 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_GCM_C */
1520#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001521 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001523
1524 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1526 iv, iv_len, ad, ad_len,
1527 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001536#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001537 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001539
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001540 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001541 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 (tag_len != 16U)) {
1543 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001544 }
1545
1546 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1548 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001551 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 }
Daniel King8fe47012016-05-17 20:33:28 -03001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001555 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001556#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001561
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001562#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1563/*
1564 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1567 const unsigned char *iv, size_t iv_len,
1568 const unsigned char *ad, size_t ad_len,
1569 const unsigned char *input, size_t ilen,
1570 unsigned char *output, size_t output_len,
1571 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001575#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001576 ctx->psa_enabled == 0 &&
1577#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001578 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1579 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1580 mbedtls_nist_kw_mode_t mode =
1581 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1582 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001583
1584 /* There is no iv, tag or ad associated with KW and KWP,
1585 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1587 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1588 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001590 (void) iv;
1591 (void) ad;
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1594 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001595 }
1596#endif /* MBEDTLS_NIST_KW_C */
1597
1598#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1599 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (output_len < ilen + tag_len) {
1601 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1602 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1605 input, ilen, output, olen,
1606 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001607 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001609#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001611#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1612}
1613
1614/*
1615 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1618 const unsigned char *iv, size_t iv_len,
1619 const unsigned char *ad, size_t ad_len,
1620 const unsigned char *input, size_t ilen,
1621 unsigned char *output, size_t output_len,
1622 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001623{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001624#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001626#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001627 ctx->psa_enabled == 0 &&
1628#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001629 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1630 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1631 mbedtls_nist_kw_mode_t mode =
1632 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1633 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001634
1635 /* There is no iv, tag or ad associated with KW and KWP,
1636 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1638 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1639 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001640
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001641 (void) iv;
1642 (void) ad;
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1645 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001646 }
1647#endif /* MBEDTLS_NIST_KW_C */
1648
1649#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1650 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (ilen < tag_len || output_len < ilen - tag_len) {
1652 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1653 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1656 input, ilen - tag_len, output, olen,
1657 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001658#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001660#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1661}
1662#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_CIPHER_C */