blob: 7f4c121492d5dffd04356956add9ddc4cc391ba3 [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];
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500852 if (padding_len == 0 || padding_len > input_len) {
853 return MBEDTLS_ERR_CIPHER_INVALID_PADDING;
854 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000855 *data_len = input_len - padding_len;
856
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100857 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100858 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100859
860 /* The number of bytes checked must be independent of padding_len,
861 * so pick input_len, which is usually 8 or 16 (one block) */
862 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100864 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
865 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
866 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100868
Dave Rodgmand03f4832023-09-22 09:52:15 +0100869 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200874/*
875 * One and zeros padding: fill with 80 00 ... 00
876 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877static void add_one_and_zeros_padding(unsigned char *output,
878 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200879{
880 size_t padding_len = output_len - data_len;
881 unsigned char i = 0;
882
883 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200885 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200887}
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
890 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200891{
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (NULL == input || NULL == data_len) {
893 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
894 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200895
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100896 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
897 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
898
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100899 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100900
Dave Rodgman437500c2023-09-19 21:36:43 +0100901 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100902 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
903
904 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
905
906 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
907
Dave Rodgmanfd965792023-09-19 21:51:50 +0100908 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100909
910 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100911 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200912
Dave Rodgmand03f4832023-09-22 09:52:15 +0100913 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200918/*
919 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921static void add_zeros_and_len_padding(unsigned char *output,
922 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200923{
924 size_t padding_len = output_len - data_len;
925 unsigned char i = 0;
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200928 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200930 output[output_len - 1] = (unsigned char) padding_len;
931}
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
934 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200935{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100936 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100937 unsigned char padding_len;
938 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (NULL == input || NULL == data_len) {
941 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
942 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200943
944 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200945 *data_len = input_len - padding_len;
946
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100947 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100948 bad = mbedtls_ct_uint_gt(padding_len, input_len);
949 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100950
951 /* The number of bytes checked must be independent of padding_len */
952 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100954 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100955 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100956 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100957 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100959
Dave Rodgmand03f4832023-09-22 09:52:15 +0100960 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200965/*
966 * Zero padding: fill with 00 ... 00
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static void add_zeros_padding(unsigned char *output,
969 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200970{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100971 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200972}
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974static int get_zeros_padding(unsigned char *input, size_t input_len,
975 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200976{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100977 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100978 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (NULL == input || NULL == data_len) {
981 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100982 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 *data_len = 0;
985 for (i = input_len; i > 0; i--) {
986 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100987 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
988 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 }
990
991 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200994
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200995/*
996 * No padding: don't pad :)
997 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200999 * but a trivial get_padding function
1000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001static int get_no_padding(unsigned char *input, size_t input_len,
1002 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (NULL == input || NULL == data_len) {
1005 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1006 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001007
1008 *data_len = input_len;
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1015 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016{
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (ctx->cipher_info == NULL) {
1018 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1019 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001021#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001023 /* While PSA Crypto has an API for multipart
1024 * operations, we currently don't make it
1025 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001027 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001028#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001029
Paul Bakker8123e9d2011-01-06 15:37:30 +00001030 *olen = 0;
1031
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001032#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1033 /* CBC mode requires padding so we make sure a call to
1034 * mbedtls_cipher_set_padding_mode has been done successfully. */
1035 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1036 if (ctx->get_padding == NULL) {
1037 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1038 }
1039 }
1040#endif
1041
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001042 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1043 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1044 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1045 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1046 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1047 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1048 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001050 }
1051
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001052 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1053 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001055 }
1056
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001057 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ctx->unprocessed_len != 0) {
1059 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1060 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001063 }
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001066 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001067 int ret = 0;
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (NULL == ctx->add_padding) {
1072 if (0 != ctx->unprocessed_len) {
1073 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1074 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001077 }
1078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1080 ctx->unprocessed_len);
1081 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001082 /*
1083 * For decrypt operations, expect a full block,
1084 * or an empty block if no padding
1085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1087 return 0;
1088 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001091 }
1092
1093 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001094 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001095 ctx->operation,
1096 mbedtls_cipher_get_block_size(
1097 ctx),
1098 ctx->iv,
1099 ctx->unprocessed_data,
1100 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001102 }
1103
1104 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (MBEDTLS_DECRYPT == ctx->operation) {
1106 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1107 olen);
1108 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001109
1110 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 *olen = mbedtls_cipher_get_block_size(ctx);
1112 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001113 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001114#else
1115 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001119}
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1123 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001124{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001125 if (NULL == ctx->cipher_info ||
1126 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001128 }
1129
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001130#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001132 /* While PSA Crypto knows about CBC padding
1133 * schemes, we currently don't make them
1134 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (mode != MBEDTLS_PADDING_NONE) {
1136 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1137 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001140 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001141#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 case MBEDTLS_PADDING_PKCS7:
1146 ctx->add_padding = add_pkcs_padding;
1147 ctx->get_padding = get_pkcs_padding;
1148 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1152 ctx->add_padding = add_one_and_zeros_padding;
1153 ctx->get_padding = get_one_and_zeros_padding;
1154 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1158 ctx->add_padding = add_zeros_and_len_padding;
1159 ctx->get_padding = get_zeros_and_len_padding;
1160 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 case MBEDTLS_PADDING_ZEROS:
1164 ctx->add_padding = add_zeros_padding;
1165 ctx->get_padding = get_zeros_padding;
1166 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001167#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 case MBEDTLS_PADDING_NONE:
1169 ctx->add_padding = NULL;
1170 ctx->get_padding = get_no_padding;
1171 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 default:
1174 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001175 }
1176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001180
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1183 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001184{
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (ctx->cipher_info == NULL) {
1186 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1187 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (MBEDTLS_ENCRYPT != ctx->operation) {
1190 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1191 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001192
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001193#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001195 /* While PSA Crypto has an API for multipart
1196 * operations, we currently don't make it
1197 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001199 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001200#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001201
Daniel King8fe47012016-05-17 20:33:28 -03001202#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001203 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001204 size_t output_length;
1205 /* The code here doesn't yet support alternative implementations
1206 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1208 NULL, 0, &output_length,
1209 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001210 }
Daniel King8fe47012016-05-17 20:33:28 -03001211#endif
1212
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001213#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001214 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001215 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (tag_len != 16U) {
1217 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1218 }
Daniel King8fe47012016-05-17 20:33:28 -03001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 return mbedtls_chachapoly_finish(
1221 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001222 }
1223#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001226}
Paul Bakker9af723c2014-05-01 13:03:14 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1229 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001230{
Daniel King8fe47012016-05-17 20:33:28 -03001231 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ctx->cipher_info == NULL) {
1235 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1236 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (MBEDTLS_DECRYPT != ctx->operation) {
1239 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001240 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001241
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001242#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001244 /* While PSA Crypto has an API for multipart
1245 * operations, we currently don't make it
1246 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001248 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001249#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001250
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001251 /* Status to return on a non-authenticated algorithm. */
1252 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001253
Daniel King8fe47012016-05-17 20:33:28 -03001254#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001255 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001256 size_t output_length;
1257 /* The code here doesn't yet support alternative implementations
1258 * that can delay up to a block of output. */
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (tag_len > sizeof(check_tag)) {
1261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1262 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (0 != (ret = mbedtls_gcm_finish(
1265 (mbedtls_gcm_context *) ctx->cipher_ctx,
1266 NULL, 0, &output_length,
1267 check_tag, tag_len))) {
1268 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001269 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001270
1271 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001273 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001274 goto exit;
1275 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001276 }
Daniel King8fe47012016-05-17 20:33:28 -03001277#endif /* MBEDTLS_GCM_C */
1278
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001279#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001280 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001281 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (tag_len != sizeof(check_tag)) {
1283 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1284 }
Daniel King8fe47012016-05-17 20:33:28 -03001285
Hanno Becker18597cd2018-11-09 16:36:33 +00001286 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1288 if (ret != 0) {
1289 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001290 }
1291
1292 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001294 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001295 goto exit;
1296 }
Daniel King8fe47012016-05-17 20:33:28 -03001297 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001298#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001299
Gilles Peskinecd742982021-12-13 16:57:47 +01001300exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 mbedtls_platform_zeroize(check_tag, tag_len);
1302 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001303}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001304#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001305
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001306/*
1307 * Packet-oriented wrapper for non-AEAD modes
1308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1310 const unsigned char *iv, size_t iv_len,
1311 const unsigned char *input, size_t ilen,
1312 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001313{
Janos Follath24eed8d2019-11-22 13:21:35 +00001314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001315 size_t finish_olen;
1316
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001317#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001319 /* As in the non-PSA case, we don't check that
1320 * a key has been set. If not, the key slot will
1321 * still be in its default state of 0, which is
1322 * guaranteed to be invalid, hence the PSA-call
1323 * below will gracefully fail. */
1324 mbedtls_cipher_context_psa * const cipher_psa =
1325 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1326
1327 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001328 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001329 size_t part_len;
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (ctx->operation == MBEDTLS_DECRYPT) {
1332 status = psa_cipher_decrypt_setup(&cipher_op,
1333 cipher_psa->slot,
1334 cipher_psa->alg);
1335 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1336 status = psa_cipher_encrypt_setup(&cipher_op,
1337 cipher_psa->slot,
1338 cipher_psa->alg);
1339 } else {
1340 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001341 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001342
1343 /* In the following, we can immediately return on an error,
1344 * because the PSA Crypto API guarantees that cipher operations
1345 * are terminated by unsuccessful calls to psa_cipher_update(),
1346 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (status != PSA_SUCCESS) {
1348 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001349 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001350
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001351 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1353 if (status != PSA_SUCCESS) {
1354 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1355 }
1356 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 status = psa_cipher_update(&cipher_op,
1359 input, ilen,
1360 output, ilen, olen);
1361 if (status != PSA_SUCCESS) {
1362 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1363 }
1364
1365 status = psa_cipher_finish(&cipher_op,
1366 output + *olen, ilen - *olen,
1367 &part_len);
1368 if (status != PSA_SUCCESS) {
1369 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1370 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001371
1372 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001374 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001375#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1378 return ret;
1379 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1382 return ret;
1383 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1386 output, olen)) != 0) {
1387 return ret;
1388 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1391 &finish_olen)) != 0) {
1392 return ret;
1393 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001394
1395 *olen += finish_olen;
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001398}
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401/*
TRodziewicz18efb732021-04-29 23:12:19 +02001402 * Packet-oriented encryption for AEAD modes: internal function used by
1403 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1406 const unsigned char *iv, size_t iv_len,
1407 const unsigned char *ad, size_t ad_len,
1408 const unsigned char *input, size_t ilen,
1409 unsigned char *output, size_t *olen,
1410 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001411{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001412#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001414 /* As in the non-PSA case, we don't check that
1415 * a key has been set. If not, the key slot will
1416 * still be in its default state of 0, which is
1417 * guaranteed to be invalid, hence the PSA-call
1418 * below will gracefully fail. */
1419 mbedtls_cipher_context_psa * const cipher_psa =
1420 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1421
1422 psa_status_t status;
1423
1424 /* PSA Crypto API always writes the authentication tag
1425 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (output == NULL || tag != output + ilen) {
1427 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1428 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 status = psa_aead_encrypt(cipher_psa->slot,
1431 cipher_psa->alg,
1432 iv, iv_len,
1433 ad, ad_len,
1434 input, ilen,
1435 output, ilen + tag_len, olen);
1436 if (status != PSA_SUCCESS) {
1437 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1438 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001439
1440 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001442 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001443#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001446 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001447 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1449 ilen, iv, iv_len, ad, ad_len,
1450 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001451 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_GCM_C */
1453#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001454 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001455 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1457 iv, iv_len, ad, ad_len, input, output,
1458 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001461#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001462 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001463 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001464 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 (tag_len != 16U)) {
1466 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001467 }
1468
1469 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1471 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001472 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001473#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001476}
1477
1478/*
TRodziewicz18efb732021-04-29 23:12:19 +02001479 * Packet-oriented encryption for AEAD modes: internal function used by
1480 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1483 const unsigned char *iv, size_t iv_len,
1484 const unsigned char *ad, size_t ad_len,
1485 const unsigned char *input, size_t ilen,
1486 unsigned char *output, size_t *olen,
1487 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001488{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001489#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001491 /* As in the non-PSA case, we don't check that
1492 * a key has been set. If not, the key slot will
1493 * still be in its default state of 0, which is
1494 * guaranteed to be invalid, hence the PSA-call
1495 * below will gracefully fail. */
1496 mbedtls_cipher_context_psa * const cipher_psa =
1497 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1498
1499 psa_status_t status;
1500
1501 /* PSA Crypto API always writes the authentication tag
1502 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (input == NULL || tag != input + ilen) {
1504 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1505 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 status = psa_aead_decrypt(cipher_psa->slot,
1508 cipher_psa->alg,
1509 iv, iv_len,
1510 ad, ad_len,
1511 input, ilen + tag_len,
1512 output, ilen, olen);
1513 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1514 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1515 } else if (status != PSA_SUCCESS) {
1516 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1517 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001520 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001521#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001524 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001526
1527 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1529 iv, iv_len, ad, ad_len,
1530 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001537 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_GCM_C */
1539#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001540 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001542
1543 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1545 iv, iv_len, ad, ad_len,
1546 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001553 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001555#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001556 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001558
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001559 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001560 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 (tag_len != 16U)) {
1562 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001563 }
1564
1565 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1567 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001570 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 }
Daniel King8fe47012016-05-17 20:33:28 -03001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001574 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001575#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001580
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001581#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1582/*
1583 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001585int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1586 const unsigned char *iv, size_t iv_len,
1587 const unsigned char *ad, size_t ad_len,
1588 const unsigned char *input, size_t ilen,
1589 unsigned char *output, size_t output_len,
1590 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001591{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001592#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001594#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001595 ctx->psa_enabled == 0 &&
1596#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001597 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1598 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1599 mbedtls_nist_kw_mode_t mode =
1600 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1601 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602
1603 /* There is no iv, tag or ad associated with KW and KWP,
1604 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1606 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1607 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001608
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001609 (void) iv;
1610 (void) ad;
1611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1613 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001614 }
1615#endif /* MBEDTLS_NIST_KW_C */
1616
1617#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1618 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (output_len < ilen + tag_len) {
1620 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1621 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1624 input, ilen, output, olen,
1625 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001626 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001628#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001630#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1631}
1632
1633/*
1634 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1637 const unsigned char *iv, size_t iv_len,
1638 const unsigned char *ad, size_t ad_len,
1639 const unsigned char *input, size_t ilen,
1640 unsigned char *output, size_t output_len,
1641 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001642{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001643#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001645#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001646 ctx->psa_enabled == 0 &&
1647#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001648 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1649 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1650 mbedtls_nist_kw_mode_t mode =
1651 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1652 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001653
1654 /* There is no iv, tag or ad associated with KW and KWP,
1655 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1657 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1658 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001659
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001660 (void) iv;
1661 (void) ad;
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1664 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001665 }
1666#endif /* MBEDTLS_NIST_KW_C */
1667
1668#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1669 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (ilen < tag_len || output_len < ilen - tag_len) {
1671 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1672 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1675 input, ilen - tag_len, output, olen,
1676 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001677#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001679#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1680}
1681#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_CIPHER_C */