blob: 0683677edaa47242d302130824272986fe96e588 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +02004 * \brief Generic cipher wrapper for Mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker8123e9d2011-01-06 15:37:30 +000010 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/cipher.h"
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
Valerio Settibbc46b42023-10-26 09:00:21 +0200254 if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
255 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
256 if (ctx->cipher_ctx == NULL) {
257 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
258 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
261 ctx->cipher_info = cipher_info;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264}
265
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200266#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100267int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
268 const mbedtls_cipher_info_t *cipher_info,
269 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000270{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000271 psa_algorithm_t alg;
272 mbedtls_cipher_context_psa *cipher_psa;
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (NULL == cipher_info || NULL == ctx) {
275 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
276 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000277
Hanno Becker4ee7e762018-11-17 22:00:38 +0000278 /* Check that the underlying cipher mode and cipher type are
279 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100280 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (alg == 0) {
282 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
283 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100284 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
286 }
Hanno Becker6118e432018-11-09 16:47:20 +0000287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
291 if (cipher_psa == NULL) {
292 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
293 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000294 cipher_psa->alg = alg;
295 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000296 ctx->cipher_info = cipher_info;
297 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000299}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200300#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
303 const unsigned char *key,
304 int key_bitlen,
305 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306{
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100308 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 }
310 if (ctx->cipher_info == NULL) {
311 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
312 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800313#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Yanray Wang4995e0c2023-11-07 17:50:52 +0800314 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
315 MBEDTLS_DECRYPT == operation) {
316 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
317 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800318#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200320#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000322 mbedtls_cipher_context_psa * const cipher_psa =
323 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000326
327 psa_status_t status;
328 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000330
331 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (key_bitlen % 8 != 0) {
333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
334 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000335
336 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
338 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
339 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000340
Andrzej Kurekc7509322019-01-08 09:36:01 -0500341 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100342 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if (key_type == 0) {
344 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
345 }
346 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000347
348 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500349 * (encrypt vs. decrypt): it is possible to setup a key for encryption
350 * and use it for AEAD decryption. Until tests relying on this
351 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
354 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 status = psa_import_key(&attributes, key, key_bytelen,
357 &cipher_psa->slot);
358 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200359 case PSA_SUCCESS:
360 break;
361 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200363 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200365 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200367 }
368 /* Indicate that we own the key slot and need to
369 * destroy it in mbedtls_cipher_free(). */
370 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000371
372 ctx->key_bitlen = key_bitlen;
373 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000375 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200376#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100379 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200381 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200382
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200383 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384 ctx->operation = operation;
385
Yanray Wangb67b4742023-10-31 17:10:32 +0800386#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker343a8702011-06-09 14:27:58 +0000387 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100388 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100391 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
392 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
393 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100394 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100395 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000396 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100399 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100400 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800402#else
403 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
404 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
405 ctx->key_bitlen);
406 }
407#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410}
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
413 const unsigned char *iv,
414 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200416 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ctx->cipher_info == NULL) {
419 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
420 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200421#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000423 /* While PSA Crypto has an API for multipart
424 * operations, we currently don't make it
425 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000427 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200428#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000429
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200430 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
432 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
433 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200436 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100438 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200439
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200440 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (actual_iv_size > iv_len) {
442 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
443 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200444 }
445
Daniel Kingbd920622016-05-15 19:56:20 -0300446#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100447 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100448 /* Even though the actual_iv_size is overwritten with a correct value
449 * of 12 from the cipher info, return an error to indicate that
450 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (iv_len != 12) {
452 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
453 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
456 iv,
457 0U)) { /* Initial counter value */
458 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300459 }
460 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100461#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100462 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 iv_len != 12) {
464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
465 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100466#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300467#endif
468
Gilles Peskine295fc132021-04-15 18:32:23 +0200469#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100470 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
472 ctx->operation,
473 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200474 }
475#endif
476
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200477#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100478 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200479 int set_lengths_result;
480 int ccm_star_mode;
481
482 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 (mbedtls_ccm_context *) ctx->cipher_ctx,
484 0, 0, 0);
485 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200486 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200490 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200492 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200494 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
498 ccm_star_mode,
499 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200500 }
501#endif
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (actual_iv_size != 0) {
504 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300505 ctx->iv_size = actual_iv_size;
506 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200509}
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200512{
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (ctx->cipher_info == NULL) {
514 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
515 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200516
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200517#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000519 /* We don't support resetting PSA-based
520 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000522 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200523#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000524
Paul Bakker8123e9d2011-01-06 15:37:30 +0000525 ctx->unprocessed_len = 0;
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200528}
529
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200530#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100531int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
532 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200533{
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (ctx->cipher_info == NULL) {
535 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
536 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200537
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200538#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000540 /* While PSA Crypto has an API for multipart
541 * operations, we currently don't make it
542 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000544 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200545#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000546
Daniel King8fe47012016-05-17 20:33:28 -0300547#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100548 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
550 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200551 }
Daniel King8fe47012016-05-17 20:33:28 -0300552#endif
553
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200554#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100555 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300556 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200557 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200560 ? MBEDTLS_CHACHAPOLY_ENCRYPT
561 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
564 ctx->iv,
565 mode);
566 if (result != 0) {
567 return result;
568 }
Daniel King8fe47012016-05-17 20:33:28 -0300569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
571 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300572 }
573#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200577#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
580 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581{
Janos Follath24eed8d2019-11-22 13:21:35 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500583 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (ctx->cipher_info == NULL) {
586 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
587 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200589#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000591 /* While PSA Crypto has an API for multipart
592 * operations, we currently don't make it
593 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000595 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200596#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000597
Paul Bakker6c212762013-12-16 15:24:50 +0100598 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 block_size = mbedtls_cipher_get_block_size(ctx);
600 if (0 == block_size) {
601 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100602 }
Paul Bakker6c212762013-12-16 15:24:50 +0100603
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100604 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (ilen != block_size) {
606 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
607 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200608
609 *olen = ilen;
610
Dave Rodgmande3de772023-06-24 12:51:06 +0100611 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100612 ctx->operation, input,
613 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615 }
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200618 }
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100621 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
623 input, ilen,
624 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200625 }
626#endif
627
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200628#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100629 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
631 input, ilen,
632 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200633 }
634#endif
635
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200636#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100637 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200638 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
640 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200641 }
642#endif
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (input == output &&
645 (ctx->unprocessed_len != 0 || ilen % block_size)) {
646 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100647 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100650 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200651 size_t copy_len = 0;
652
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 /*
654 * If there is not enough data for a full block, cache it.
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
657 ilen <= block_size - ctx->unprocessed_len) ||
658 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
659 ilen < block_size - ctx->unprocessed_len) ||
660 (ctx->operation == MBEDTLS_ENCRYPT &&
661 ilen < block_size - ctx->unprocessed_len)) {
662 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
663 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664
665 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
668
669 /*
670 * Process cached data first
671 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100673 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
676 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677
Dave Rodgmande3de772023-06-24 12:51:06 +0100678 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100679 ctx->operation,
680 block_size, ctx->iv,
681 ctx->
682 unprocessed_data,
683 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 }
686
Janos Follath98e28a72016-05-31 14:03:54 +0100687 *olen += block_size;
688 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000689 ctx->unprocessed_len = 0;
690
691 input += copy_len;
692 ilen -= copy_len;
693 }
694
695 /*
696 * Cache final, incomplete block
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700699 /* Encryption: only cache partial blocks
700 * Decryption w/ padding: always keep at least one whole block
701 * Decryption w/o padding: only cache partial blocks
702 */
Janos Follath98e28a72016-05-31 14:03:54 +0100703 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700705 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100707 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700708 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
711 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712
713 ctx->unprocessed_len += copy_len;
714 ilen -= copy_len;
715 }
716
717 /*
718 * Process remaining full blocks
719 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100721 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100722 ctx->operation,
723 ilen, ctx->iv,
724 input,
725 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000727 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200728
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 *olen += ilen;
730 }
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100737 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100738 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100739 ctx->operation, ilen,
740 &ctx->unprocessed_len,
741 ctx->iv,
742 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000744 }
745
746 *olen = ilen;
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000749 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000751
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100752#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100753 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100754 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100755 ilen,
756 &ctx->unprocessed_len,
757 ctx->iv,
758 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100760 }
761
762 *olen = ilen;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100765 }
766#endif /* MBEDTLS_CIPHER_MODE_OFB */
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100769 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100770 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100771 ilen,
772 &ctx->unprocessed_len,
773 ctx->iv,
774 ctx->unprocessed_data,
775 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000777 }
778
779 *olen = ilen;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000784
Jaeden Ameroc6539902018-04-30 17:17:41 +0100785#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100786 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100788 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100790 }
791
Dave Rodgmande3de772023-06-24 12:51:06 +0100792 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100793 ctx->operation,
794 ilen,
795 ctx->iv,
796 input,
797 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ret != 0) {
799 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100800 }
801
802 *olen = ilen;
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100805 }
806#endif /* MBEDTLS_CIPHER_MODE_XTS */
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100809 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100810 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100811 ilen, input,
812 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200814 }
815
816 *olen = ilen;
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000823}
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
826#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200827/*
828 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830static void add_pkcs_padding(unsigned char *output, size_t output_len,
831 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000832{
Paul Bakker23986e52011-04-24 08:57:21 +0000833 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100834 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000837 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000839}
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841static int get_pkcs_padding(unsigned char *input, size_t input_len,
842 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100844 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100845 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (NULL == input || NULL == data_len) {
848 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
849 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850
851 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000852 *data_len = input_len - padding_len;
853
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100854 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100855 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100856
857 /* The number of bytes checked must be independent of padding_len,
858 * so pick input_len, which is usually 8 or 16 (one block) */
859 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100861 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
862 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
863 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100865
Dave Rodgmand03f4832023-09-22 09:52:15 +0100866 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200871/*
872 * One and zeros padding: fill with 80 00 ... 00
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static void add_one_and_zeros_padding(unsigned char *output,
875 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200876{
877 size_t padding_len = output_len - data_len;
878 unsigned char i = 0;
879
880 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200882 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200884}
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
887 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200888{
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (NULL == input || NULL == data_len) {
890 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
891 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200892
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100893 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
894 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
895
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100896 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100897
Dave Rodgman437500c2023-09-19 21:36:43 +0100898 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100899 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
900
901 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
902
903 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
904
Dave Rodgmanfd965792023-09-19 21:51:50 +0100905 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100906
907 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100908 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200909
Dave Rodgmand03f4832023-09-22 09:52:15 +0100910 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200915/*
916 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918static void add_zeros_and_len_padding(unsigned char *output,
919 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200920{
921 size_t padding_len = output_len - data_len;
922 unsigned char i = 0;
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200925 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200927 output[output_len - 1] = (unsigned char) padding_len;
928}
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
931 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200932{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100933 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100934 unsigned char padding_len;
935 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (NULL == input || NULL == data_len) {
938 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
939 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200940
941 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200942 *data_len = input_len - padding_len;
943
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100944 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100945 bad = mbedtls_ct_uint_gt(padding_len, input_len);
946 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100947
948 /* The number of bytes checked must be independent of padding_len */
949 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100951 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100952 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100953 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100954 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100956
Dave Rodgmand03f4832023-09-22 09:52:15 +0100957 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200962/*
963 * Zero padding: fill with 00 ... 00
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965static void add_zeros_padding(unsigned char *output,
966 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200967{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100968 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200969}
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971static int get_zeros_padding(unsigned char *input, size_t input_len,
972 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200973{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100974 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100975 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (NULL == input || NULL == data_len) {
978 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100979 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 *data_len = 0;
982 for (i = input_len; i > 0; i--) {
983 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100984 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
985 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 }
987
988 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200989}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200991
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992/*
993 * No padding: don't pad :)
994 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200996 * but a trivial get_padding function
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static int get_no_padding(unsigned char *input, size_t input_len,
999 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001000{
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (NULL == input || NULL == data_len) {
1002 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1003 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001004
1005 *data_len = input_len;
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1012 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001013{
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (ctx->cipher_info == NULL) {
1015 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1016 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001017
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001018#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001020 /* While PSA Crypto has an API for multipart
1021 * operations, we currently don't make it
1022 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001024 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001025#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001026
Paul Bakker8123e9d2011-01-06 15:37:30 +00001027 *olen = 0;
1028
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001029#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1030 /* CBC mode requires padding so we make sure a call to
1031 * mbedtls_cipher_set_padding_mode has been done successfully. */
1032 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1033 if (ctx->get_padding == NULL) {
1034 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1035 }
1036 }
1037#endif
1038
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001039 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1042 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1043 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1044 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1045 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001047 }
1048
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001049 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1050 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001052 }
1053
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001054 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ctx->unprocessed_len != 0) {
1056 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1057 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001060 }
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001063 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001064 int ret = 0;
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001067 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (NULL == ctx->add_padding) {
1069 if (0 != ctx->unprocessed_len) {
1070 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1071 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001074 }
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1077 ctx->unprocessed_len);
1078 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001079 /*
1080 * For decrypt operations, expect a full block,
1081 * or an empty block if no padding
1082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1084 return 0;
1085 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001088 }
1089
1090 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001091 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001092 ctx->operation,
1093 mbedtls_cipher_get_block_size(
1094 ctx),
1095 ctx->iv,
1096 ctx->unprocessed_data,
1097 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001099 }
1100
1101 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (MBEDTLS_DECRYPT == ctx->operation) {
1103 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1104 olen);
1105 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106
1107 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 *olen = mbedtls_cipher_get_block_size(ctx);
1109 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001110 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001111#else
1112 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001116}
1117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1120 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001121{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001122 if (NULL == ctx->cipher_info ||
1123 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001125 }
1126
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001127#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001129 /* While PSA Crypto knows about CBC padding
1130 * schemes, we currently don't make them
1131 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (mode != MBEDTLS_PADDING_NONE) {
1133 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1134 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001137 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001138#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 case MBEDTLS_PADDING_PKCS7:
1143 ctx->add_padding = add_pkcs_padding;
1144 ctx->get_padding = get_pkcs_padding;
1145 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1149 ctx->add_padding = add_one_and_zeros_padding;
1150 ctx->get_padding = get_one_and_zeros_padding;
1151 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1155 ctx->add_padding = add_zeros_and_len_padding;
1156 ctx->get_padding = get_zeros_and_len_padding;
1157 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 case MBEDTLS_PADDING_ZEROS:
1161 ctx->add_padding = add_zeros_padding;
1162 ctx->get_padding = get_zeros_padding;
1163 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001164#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 case MBEDTLS_PADDING_NONE:
1166 ctx->add_padding = NULL;
1167 ctx->get_padding = get_no_padding;
1168 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 default:
1171 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001177
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001178#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001179int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1180 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001181{
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (ctx->cipher_info == NULL) {
1183 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1184 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (MBEDTLS_ENCRYPT != ctx->operation) {
1187 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1188 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001189
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001190#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001192 /* While PSA Crypto has an API for multipart
1193 * operations, we currently don't make it
1194 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001196 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001197#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001198
Daniel King8fe47012016-05-17 20:33:28 -03001199#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001200 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001201 size_t output_length;
1202 /* The code here doesn't yet support alternative implementations
1203 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1205 NULL, 0, &output_length,
1206 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001207 }
Daniel King8fe47012016-05-17 20:33:28 -03001208#endif
1209
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001210#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001211 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001212 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (tag_len != 16U) {
1214 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1215 }
Daniel King8fe47012016-05-17 20:33:28 -03001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return mbedtls_chachapoly_finish(
1218 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001219 }
1220#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001223}
Paul Bakker9af723c2014-05-01 13:03:14 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1226 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001227{
Daniel King8fe47012016-05-17 20:33:28 -03001228 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (ctx->cipher_info == NULL) {
1232 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1233 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (MBEDTLS_DECRYPT != ctx->operation) {
1236 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001237 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001238
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001239#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001241 /* While PSA Crypto has an API for multipart
1242 * operations, we currently don't make it
1243 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001245 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001246#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001247
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001248 /* Status to return on a non-authenticated algorithm. */
1249 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001250
Daniel King8fe47012016-05-17 20:33:28 -03001251#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001252 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001253 size_t output_length;
1254 /* The code here doesn't yet support alternative implementations
1255 * that can delay up to a block of output. */
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (tag_len > sizeof(check_tag)) {
1258 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1259 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (0 != (ret = mbedtls_gcm_finish(
1262 (mbedtls_gcm_context *) ctx->cipher_ctx,
1263 NULL, 0, &output_length,
1264 check_tag, tag_len))) {
1265 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001266 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001267
1268 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001270 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001271 goto exit;
1272 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001273 }
Daniel King8fe47012016-05-17 20:33:28 -03001274#endif /* MBEDTLS_GCM_C */
1275
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001276#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001277 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001278 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (tag_len != sizeof(check_tag)) {
1280 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1281 }
Daniel King8fe47012016-05-17 20:33:28 -03001282
Hanno Becker18597cd2018-11-09 16:36:33 +00001283 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1285 if (ret != 0) {
1286 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001287 }
1288
1289 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001291 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001292 goto exit;
1293 }
Daniel King8fe47012016-05-17 20:33:28 -03001294 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001295#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001296
Gilles Peskinecd742982021-12-13 16:57:47 +01001297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 mbedtls_platform_zeroize(check_tag, tag_len);
1299 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001300}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001301#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001302
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001303/*
1304 * Packet-oriented wrapper for non-AEAD modes
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1307 const unsigned char *iv, size_t iv_len,
1308 const unsigned char *input, size_t ilen,
1309 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001310{
Janos Follath24eed8d2019-11-22 13:21:35 +00001311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001312 size_t finish_olen;
1313
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001314#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001316 /* As in the non-PSA case, we don't check that
1317 * a key has been set. If not, the key slot will
1318 * still be in its default state of 0, which is
1319 * guaranteed to be invalid, hence the PSA-call
1320 * below will gracefully fail. */
1321 mbedtls_cipher_context_psa * const cipher_psa =
1322 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1323
1324 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001325 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001326 size_t part_len;
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (ctx->operation == MBEDTLS_DECRYPT) {
1329 status = psa_cipher_decrypt_setup(&cipher_op,
1330 cipher_psa->slot,
1331 cipher_psa->alg);
1332 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1333 status = psa_cipher_encrypt_setup(&cipher_op,
1334 cipher_psa->slot,
1335 cipher_psa->alg);
1336 } else {
1337 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001338 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001339
1340 /* In the following, we can immediately return on an error,
1341 * because the PSA Crypto API guarantees that cipher operations
1342 * are terminated by unsuccessful calls to psa_cipher_update(),
1343 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (status != PSA_SUCCESS) {
1345 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001346 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001347
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001348 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1350 if (status != PSA_SUCCESS) {
1351 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1352 }
1353 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 status = psa_cipher_update(&cipher_op,
1356 input, ilen,
1357 output, ilen, olen);
1358 if (status != PSA_SUCCESS) {
1359 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1360 }
1361
1362 status = psa_cipher_finish(&cipher_op,
1363 output + *olen, ilen - *olen,
1364 &part_len);
1365 if (status != PSA_SUCCESS) {
1366 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1367 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001368
1369 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001371 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001372#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1375 return ret;
1376 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1379 return ret;
1380 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1383 output, olen)) != 0) {
1384 return ret;
1385 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1388 &finish_olen)) != 0) {
1389 return ret;
1390 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001391
1392 *olen += finish_olen;
1393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001395}
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001398/*
TRodziewicz18efb732021-04-29 23:12:19 +02001399 * Packet-oriented encryption for AEAD modes: internal function used by
1400 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1403 const unsigned char *iv, size_t iv_len,
1404 const unsigned char *ad, size_t ad_len,
1405 const unsigned char *input, size_t ilen,
1406 unsigned char *output, size_t *olen,
1407 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001408{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001409#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001411 /* As in the non-PSA case, we don't check that
1412 * a key has been set. If not, the key slot will
1413 * still be in its default state of 0, which is
1414 * guaranteed to be invalid, hence the PSA-call
1415 * below will gracefully fail. */
1416 mbedtls_cipher_context_psa * const cipher_psa =
1417 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1418
1419 psa_status_t status;
1420
1421 /* PSA Crypto API always writes the authentication tag
1422 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (output == NULL || tag != output + ilen) {
1424 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1425 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 status = psa_aead_encrypt(cipher_psa->slot,
1428 cipher_psa->alg,
1429 iv, iv_len,
1430 ad, ad_len,
1431 input, ilen,
1432 output, ilen + tag_len, olen);
1433 if (status != PSA_SUCCESS) {
1434 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1435 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001436
1437 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001439 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001440#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001443 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001444 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1446 ilen, iv, iv_len, ad, ad_len,
1447 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001448 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_GCM_C */
1450#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001451 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001452 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1454 iv, iv_len, ad, ad_len, input, output,
1455 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001458#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001459 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001460 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001461 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 (tag_len != 16U)) {
1463 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001464 }
1465
1466 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1468 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001469 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001470#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001473}
1474
1475/*
TRodziewicz18efb732021-04-29 23:12:19 +02001476 * Packet-oriented encryption for AEAD modes: internal function used by
1477 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1480 const unsigned char *iv, size_t iv_len,
1481 const unsigned char *ad, size_t ad_len,
1482 const unsigned char *input, size_t ilen,
1483 unsigned char *output, size_t *olen,
1484 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001485{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001486#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001488 /* As in the non-PSA case, we don't check that
1489 * a key has been set. If not, the key slot will
1490 * still be in its default state of 0, which is
1491 * guaranteed to be invalid, hence the PSA-call
1492 * below will gracefully fail. */
1493 mbedtls_cipher_context_psa * const cipher_psa =
1494 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1495
1496 psa_status_t status;
1497
1498 /* PSA Crypto API always writes the authentication tag
1499 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (input == NULL || tag != input + ilen) {
1501 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1502 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 status = psa_aead_decrypt(cipher_psa->slot,
1505 cipher_psa->alg,
1506 iv, iv_len,
1507 ad, ad_len,
1508 input, ilen + tag_len,
1509 output, ilen, olen);
1510 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1511 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1512 } else if (status != PSA_SUCCESS) {
1513 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1514 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001517 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001518#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001521 if (MBEDTLS_MODE_GCM == ((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é-Gonnard4562ffe2014-05-13 12:19:29 +02001523
1524 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1526 iv, iv_len, ad, ad_len,
1527 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret == MBEDTLS_ERR_GCM_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é-Gonnard4562ffe2014-05-13 12:19:29 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_GCM_C */
1536#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001537 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001539
1540 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1542 iv, iv_len, ad, ad_len,
1543 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001552#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001553 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001555
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001556 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001557 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 (tag_len != 16U)) {
1559 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001560 }
1561
1562 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1564 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001567 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 }
Daniel King8fe47012016-05-17 20:33:28 -03001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001571 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001572#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001577
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001578#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1579/*
1580 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1581 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001582int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1583 const unsigned char *iv, size_t iv_len,
1584 const unsigned char *ad, size_t ad_len,
1585 const unsigned char *input, size_t ilen,
1586 unsigned char *output, size_t output_len,
1587 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001588{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001591#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001592 ctx->psa_enabled == 0 &&
1593#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001594 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1595 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1596 mbedtls_nist_kw_mode_t mode =
1597 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1598 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001599
1600 /* There is no iv, tag or ad associated with KW and KWP,
1601 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1603 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1604 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001605
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001606 (void) iv;
1607 (void) ad;
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1610 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001611 }
1612#endif /* MBEDTLS_NIST_KW_C */
1613
1614#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1615 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (output_len < ilen + tag_len) {
1617 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1618 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1621 input, ilen, output, olen,
1622 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001623 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001625#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001627#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1628}
1629
1630/*
1631 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001633int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1634 const unsigned char *iv, size_t iv_len,
1635 const unsigned char *ad, size_t ad_len,
1636 const unsigned char *input, size_t ilen,
1637 unsigned char *output, size_t output_len,
1638 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001639{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001640#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001642#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001643 ctx->psa_enabled == 0 &&
1644#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001645 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1646 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1647 mbedtls_nist_kw_mode_t mode =
1648 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1649 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001650
1651 /* There is no iv, tag or ad associated with KW and KWP,
1652 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1654 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1655 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001656
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001657 (void) iv;
1658 (void) ad;
1659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1661 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001662 }
1663#endif /* MBEDTLS_NIST_KW_C */
1664
1665#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1666 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (ilen < tag_len || output_len < ilen - tag_len) {
1668 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1669 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1672 input, ilen - tag_len, output, olen,
1673 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001674#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001676#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1677}
1678#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_CIPHER_C */