blob: de55efa78dd5949c52eafa8047082d18f091a361 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020060#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000061
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200147#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
148static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
149 mbedtls_cipher_type_t cipher)
150{
151 switch (cipher) {
152 case MBEDTLS_CIPHER_AES_128_CCM:
153 case MBEDTLS_CIPHER_AES_192_CCM:
154 case MBEDTLS_CIPHER_AES_256_CCM:
155 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
158 case MBEDTLS_CIPHER_AES_128_GCM:
159 case MBEDTLS_CIPHER_AES_192_GCM:
160 case MBEDTLS_CIPHER_AES_256_GCM:
161 case MBEDTLS_CIPHER_AES_128_CBC:
162 case MBEDTLS_CIPHER_AES_192_CBC:
163 case MBEDTLS_CIPHER_AES_256_CBC:
164 case MBEDTLS_CIPHER_AES_128_ECB:
165 case MBEDTLS_CIPHER_AES_192_ECB:
166 case MBEDTLS_CIPHER_AES_256_ECB:
167 return PSA_KEY_TYPE_AES;
168
169 /* ARIA not yet supported in PSA. */
170 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
171 case MBEDTLS_CIPHER_ARIA_192_CCM:
172 case MBEDTLS_CIPHER_ARIA_256_CCM:
173 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
176 case MBEDTLS_CIPHER_ARIA_128_GCM:
177 case MBEDTLS_CIPHER_ARIA_192_GCM:
178 case MBEDTLS_CIPHER_ARIA_256_GCM:
179 case MBEDTLS_CIPHER_ARIA_128_CBC:
180 case MBEDTLS_CIPHER_ARIA_192_CBC:
181 case MBEDTLS_CIPHER_ARIA_256_CBC:
182 return( PSA_KEY_TYPE_ARIA ); */
183
184 default:
185 return 0;
186 }
187}
188
189static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
190 mbedtls_cipher_mode_t mode, size_t taglen)
191{
192 switch (mode) {
193 case MBEDTLS_MODE_ECB:
194 return PSA_ALG_ECB_NO_PADDING;
195 case MBEDTLS_MODE_GCM:
196 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
197 case MBEDTLS_MODE_CCM:
198 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
199 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
200 return PSA_ALG_CCM_STAR_NO_TAG;
201 case MBEDTLS_MODE_CBC:
202 if (taglen == 0) {
203 return PSA_ALG_CBC_NO_PADDING;
204 } else {
205 return 0;
206 }
207 default:
208 return 0;
209 }
210}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200221 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (ctx->psa_enabled == 1) {
226 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000227 mbedtls_cipher_context_psa * const cipher_psa =
228 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000233 }
234
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100235 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100245 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100247 }
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100251 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
258 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (cipher_info == NULL) {
261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Dave Rodgmande3de772023-06-24 12:51:06 +0100266 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
268 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
270 ctx->cipher_info = cipher_info;
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273}
274
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200275#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
277 const mbedtls_cipher_info_t *cipher_info,
278 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000279{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000280 psa_algorithm_t alg;
281 mbedtls_cipher_context_psa *cipher_psa;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (NULL == cipher_info || NULL == ctx) {
284 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
285 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000286
Hanno Becker4ee7e762018-11-17 22:00:38 +0000287 /* Check that the underlying cipher mode and cipher type are
288 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100289 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (alg == 0) {
291 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
292 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100293 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
295 }
Hanno Becker6118e432018-11-09 16:47:20 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
300 if (cipher_psa == NULL) {
301 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
302 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000303 cipher_psa->alg = alg;
304 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000305 ctx->cipher_info = cipher_info;
306 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000308}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200309#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
312 const unsigned char *key,
313 int key_bitlen,
314 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315{
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100317 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
319 if (ctx->cipher_info == NULL) {
320 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
321 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800322#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
323 /* CBC, XTS, KW and KWP mode always need decryption, return an error to
324 * indicate those modes are not available under
325 * MBEDTLS_BLOCK_CIPHER_NO_DECRYPT. */
326 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
327 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
328 MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
329 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
330 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
331 }
332#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200334#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000336 mbedtls_cipher_context_psa * const cipher_psa =
337 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000340
341 psa_status_t status;
342 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000344
345 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (key_bitlen % 8 != 0) {
347 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
348 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000349
350 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
352 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
353 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000354
Andrzej Kurekc7509322019-01-08 09:36:01 -0500355 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100356 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (key_type == 0) {
358 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
359 }
360 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000361
362 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500363 * (encrypt vs. decrypt): it is possible to setup a key for encryption
364 * and use it for AEAD decryption. Until tests relying on this
365 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
368 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 status = psa_import_key(&attributes, key, key_bytelen,
371 &cipher_psa->slot);
372 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200373 case PSA_SUCCESS:
374 break;
375 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200377 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200379 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200381 }
382 /* Indicate that we own the key slot and need to
383 * destroy it in mbedtls_cipher_free(). */
384 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000385
386 ctx->key_bitlen = key_bitlen;
387 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000389 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200390#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100393 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200395 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200396
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200397 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398 ctx->operation = operation;
399
Yanray Wangb67b4742023-10-31 17:10:32 +0800400#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker343a8702011-06-09 14:27:58 +0000401 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100402 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100405 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
406 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
407 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100408 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100409 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000410 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100413 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100414 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 }
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800416#else
417 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
418 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
419 ctx->key_bitlen);
420 }
421#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424}
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
427 const unsigned char *iv,
428 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200430 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (ctx->cipher_info == NULL) {
433 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
434 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200435#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000437 /* While PSA Crypto has an API for multipart
438 * operations, we currently don't make it
439 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000441 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200442#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000443
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200444 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
446 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
447 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200450 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100452 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200453
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200454 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (actual_iv_size > iv_len) {
456 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
457 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200458 }
459
Daniel Kingbd920622016-05-15 19:56:20 -0300460#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100461 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100462 /* Even though the actual_iv_size is overwritten with a correct value
463 * of 12 from the cipher info, return an error to indicate that
464 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (iv_len != 12) {
466 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
467 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
470 iv,
471 0U)) { /* Initial counter value */
472 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300473 }
474 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100475#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100476 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 iv_len != 12) {
478 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
479 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100480#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300481#endif
482
Gilles Peskine295fc132021-04-15 18:32:23 +0200483#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100484 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
486 ctx->operation,
487 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200488 }
489#endif
490
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200491#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100492 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200493 int set_lengths_result;
494 int ccm_star_mode;
495
496 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 (mbedtls_ccm_context *) ctx->cipher_ctx,
498 0, 0, 0);
499 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200500 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200504 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200506 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200508 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
512 ccm_star_mode,
513 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200514 }
515#endif
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (actual_iv_size != 0) {
518 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300519 ctx->iv_size = actual_iv_size;
520 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200523}
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200526{
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (ctx->cipher_info == NULL) {
528 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
529 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200530
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200531#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000533 /* We don't support resetting PSA-based
534 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000536 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200537#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000538
Paul Bakker8123e9d2011-01-06 15:37:30 +0000539 ctx->unprocessed_len = 0;
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200542}
543
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200544#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100545int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
546 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200547{
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (ctx->cipher_info == NULL) {
549 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
550 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200551
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200552#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000554 /* While PSA Crypto has an API for multipart
555 * operations, we currently don't make it
556 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000558 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200559#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000560
Daniel King8fe47012016-05-17 20:33:28 -0300561#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100562 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
564 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200565 }
Daniel King8fe47012016-05-17 20:33:28 -0300566#endif
567
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200568#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100569 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300570 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200571 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200574 ? MBEDTLS_CHACHAPOLY_ENCRYPT
575 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
578 ctx->iv,
579 mode);
580 if (result != 0) {
581 return result;
582 }
Daniel King8fe47012016-05-17 20:33:28 -0300583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
585 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300586 }
587#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200591#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
594 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595{
Janos Follath24eed8d2019-11-22 13:21:35 +0000596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500597 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (ctx->cipher_info == NULL) {
600 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
601 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200603#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000605 /* While PSA Crypto has an API for multipart
606 * operations, we currently don't make it
607 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000609 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200610#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000611
Paul Bakker6c212762013-12-16 15:24:50 +0100612 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 block_size = mbedtls_cipher_get_block_size(ctx);
614 if (0 == block_size) {
615 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100616 }
Paul Bakker6c212762013-12-16 15:24:50 +0100617
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100618 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (ilen != block_size) {
620 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
621 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622
623 *olen = ilen;
624
Dave Rodgmande3de772023-06-24 12:51:06 +0100625 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100626 ctx->operation, input,
627 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200632 }
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100635 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
637 input, ilen,
638 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200639 }
640#endif
641
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200642#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100643 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
645 input, ilen,
646 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200647 }
648#endif
649
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200650#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100651 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200652 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
654 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200655 }
656#endif
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (input == output &&
659 (ctx->unprocessed_len != 0 || ilen % block_size)) {
660 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100661 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100664 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200665 size_t copy_len = 0;
666
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 /*
668 * If there is not enough data for a full block, cache it.
669 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
671 ilen <= block_size - ctx->unprocessed_len) ||
672 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
673 ilen < block_size - ctx->unprocessed_len) ||
674 (ctx->operation == MBEDTLS_ENCRYPT &&
675 ilen < block_size - ctx->unprocessed_len)) {
676 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
677 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
679 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 }
682
683 /*
684 * Process cached data first
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100687 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
690 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691
Dave Rodgmande3de772023-06-24 12:51:06 +0100692 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100693 ctx->operation,
694 block_size, ctx->iv,
695 ctx->
696 unprocessed_data,
697 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000699 }
700
Janos Follath98e28a72016-05-31 14:03:54 +0100701 *olen += block_size;
702 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703 ctx->unprocessed_len = 0;
704
705 input += copy_len;
706 ilen -= copy_len;
707 }
708
709 /*
710 * Cache final, incomplete block
711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700713 /* Encryption: only cache partial blocks
714 * Decryption w/ padding: always keep at least one whole block
715 * Decryption w/o padding: only cache partial blocks
716 */
Janos Follath98e28a72016-05-31 14:03:54 +0100717 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700719 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100721 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700722 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
725 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726
727 ctx->unprocessed_len += copy_len;
728 ilen -= copy_len;
729 }
730
731 /*
732 * Process remaining full blocks
733 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100735 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100736 ctx->operation,
737 ilen, ctx->iv,
738 input,
739 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200742
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743 *olen += ilen;
744 }
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100751 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100752 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100753 ctx->operation, ilen,
754 &ctx->unprocessed_len,
755 ctx->iv,
756 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000758 }
759
760 *olen = ilen;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000765
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100766#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100767 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100768 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100769 ilen,
770 &ctx->unprocessed_len,
771 ctx->iv,
772 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100774 }
775
776 *olen = ilen;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100779 }
780#endif /* MBEDTLS_CIPHER_MODE_OFB */
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100783 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100784 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100785 ilen,
786 &ctx->unprocessed_len,
787 ctx->iv,
788 ctx->unprocessed_data,
789 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000791 }
792
793 *olen = ilen;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000798
Jaeden Ameroc6539902018-04-30 17:17:41 +0100799#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100800 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100802 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100804 }
805
Dave Rodgmande3de772023-06-24 12:51:06 +0100806 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100807 ctx->operation,
808 ilen,
809 ctx->iv,
810 input,
811 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (ret != 0) {
813 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100814 }
815
816 *olen = ilen;
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100819 }
820#endif /* MBEDTLS_CIPHER_MODE_XTS */
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100823 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100824 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100825 ilen, input,
826 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200828 }
829
830 *olen = ilen;
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000837}
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
840#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200841/*
842 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
843 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844static void add_pkcs_padding(unsigned char *output, size_t output_len,
845 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846{
Paul Bakker23986e52011-04-24 08:57:21 +0000847 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100848 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000851 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000853}
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static int get_pkcs_padding(unsigned char *input, size_t input_len,
856 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000857{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100858 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100859 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (NULL == input || NULL == data_len) {
862 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
863 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000864
865 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000866 *data_len = input_len - padding_len;
867
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100868 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100869 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100870
871 /* The number of bytes checked must be independent of padding_len,
872 * so pick input_len, which is usually 8 or 16 (one block) */
873 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100875 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
876 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
877 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100879
Dave Rodgmand03f4832023-09-22 09:52:15 +0100880 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200885/*
886 * One and zeros padding: fill with 80 00 ... 00
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888static void add_one_and_zeros_padding(unsigned char *output,
889 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200890{
891 size_t padding_len = output_len - data_len;
892 unsigned char i = 0;
893
894 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200896 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200898}
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
901 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200902{
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (NULL == input || NULL == data_len) {
904 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
905 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200906
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100907 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
908 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
909
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100910 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100911
Dave Rodgman437500c2023-09-19 21:36:43 +0100912 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100913 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
914
915 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
916
917 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
918
Dave Rodgmanfd965792023-09-19 21:51:50 +0100919 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100920
921 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100922 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200923
Dave Rodgmand03f4832023-09-22 09:52:15 +0100924 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200925}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200929/*
930 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932static void add_zeros_and_len_padding(unsigned char *output,
933 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200934{
935 size_t padding_len = output_len - data_len;
936 unsigned char i = 0;
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200939 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200941 output[output_len - 1] = (unsigned char) padding_len;
942}
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
945 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200946{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100947 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100948 unsigned char padding_len;
949 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (NULL == input || NULL == data_len) {
952 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
953 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200954
955 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200956 *data_len = input_len - padding_len;
957
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100958 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100959 bad = mbedtls_ct_uint_gt(padding_len, input_len);
960 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100961
962 /* The number of bytes checked must be independent of padding_len */
963 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100965 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100966 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100967 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100968 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100970
Dave Rodgmand03f4832023-09-22 09:52:15 +0100971 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200976/*
977 * Zero padding: fill with 00 ... 00
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979static void add_zeros_padding(unsigned char *output,
980 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200981{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100982 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200983}
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985static int get_zeros_padding(unsigned char *input, size_t input_len,
986 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200987{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100988 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100989 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (NULL == input || NULL == data_len) {
992 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100993 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 *data_len = 0;
996 for (i = input_len; i > 0; i--) {
997 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100998 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
999 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 }
1001
1002 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001005
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001006/*
1007 * No padding: don't pad :)
1008 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001010 * but a trivial get_padding function
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int get_no_padding(unsigned char *input, size_t input_len,
1013 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001014{
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (NULL == input || NULL == data_len) {
1016 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1017 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001018
1019 *data_len = input_len;
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1026 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001027{
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (ctx->cipher_info == NULL) {
1029 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1030 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001031
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001032#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001034 /* While PSA Crypto has an API for multipart
1035 * operations, we currently don't make it
1036 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001038 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001039#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001040
Paul Bakker8123e9d2011-01-06 15:37:30 +00001041 *olen = 0;
1042
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001043#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1044 /* CBC mode requires padding so we make sure a call to
1045 * mbedtls_cipher_set_padding_mode has been done successfully. */
1046 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1047 if (ctx->get_padding == NULL) {
1048 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1049 }
1050 }
1051#endif
1052
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001053 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1054 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1055 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1056 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1057 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1058 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1059 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001061 }
1062
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001063 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1064 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001066 }
1067
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001068 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ctx->unprocessed_len != 0) {
1070 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1071 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001074 }
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001077 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001078 int ret = 0;
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001081 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (NULL == ctx->add_padding) {
1083 if (0 != ctx->unprocessed_len) {
1084 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1085 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001088 }
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1091 ctx->unprocessed_len);
1092 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001093 /*
1094 * For decrypt operations, expect a full block,
1095 * or an empty block if no padding
1096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1098 return 0;
1099 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001102 }
1103
1104 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001105 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001106 ctx->operation,
1107 mbedtls_cipher_get_block_size(
1108 ctx),
1109 ctx->iv,
1110 ctx->unprocessed_data,
1111 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001113 }
1114
1115 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (MBEDTLS_DECRYPT == ctx->operation) {
1117 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1118 olen);
1119 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001120
1121 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 *olen = mbedtls_cipher_get_block_size(ctx);
1123 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001124 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001125#else
1126 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001130}
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001133int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1134 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001135{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001136 if (NULL == ctx->cipher_info ||
1137 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001139 }
1140
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001141#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001143 /* While PSA Crypto knows about CBC padding
1144 * schemes, we currently don't make them
1145 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (mode != MBEDTLS_PADDING_NONE) {
1147 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1148 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001151 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001152#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 case MBEDTLS_PADDING_PKCS7:
1157 ctx->add_padding = add_pkcs_padding;
1158 ctx->get_padding = get_pkcs_padding;
1159 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1163 ctx->add_padding = add_one_and_zeros_padding;
1164 ctx->get_padding = get_one_and_zeros_padding;
1165 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1169 ctx->add_padding = add_zeros_and_len_padding;
1170 ctx->get_padding = get_zeros_and_len_padding;
1171 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 case MBEDTLS_PADDING_ZEROS:
1175 ctx->add_padding = add_zeros_padding;
1176 ctx->get_padding = get_zeros_padding;
1177 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001178#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 case MBEDTLS_PADDING_NONE:
1180 ctx->add_padding = NULL;
1181 ctx->get_padding = get_no_padding;
1182 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 default:
1185 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001191
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001192#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001193int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1194 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001195{
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (ctx->cipher_info == NULL) {
1197 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1198 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (MBEDTLS_ENCRYPT != ctx->operation) {
1201 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1202 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001203
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001204#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001206 /* While PSA Crypto has an API for multipart
1207 * operations, we currently don't make it
1208 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001210 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001212
Daniel King8fe47012016-05-17 20:33:28 -03001213#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001214 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001215 size_t output_length;
1216 /* The code here doesn't yet support alternative implementations
1217 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1219 NULL, 0, &output_length,
1220 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001221 }
Daniel King8fe47012016-05-17 20:33:28 -03001222#endif
1223
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001224#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001225 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001226 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (tag_len != 16U) {
1228 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1229 }
Daniel King8fe47012016-05-17 20:33:28 -03001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return mbedtls_chachapoly_finish(
1232 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001233 }
1234#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001237}
Paul Bakker9af723c2014-05-01 13:03:14 +02001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1240 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001241{
Daniel King8fe47012016-05-17 20:33:28 -03001242 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ctx->cipher_info == NULL) {
1246 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1247 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (MBEDTLS_DECRYPT != ctx->operation) {
1250 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001251 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001252
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001253#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001255 /* While PSA Crypto has an API for multipart
1256 * operations, we currently don't make it
1257 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001259 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001260#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001261
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001262 /* Status to return on a non-authenticated algorithm. */
1263 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001264
Daniel King8fe47012016-05-17 20:33:28 -03001265#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001266 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001267 size_t output_length;
1268 /* The code here doesn't yet support alternative implementations
1269 * that can delay up to a block of output. */
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (tag_len > sizeof(check_tag)) {
1272 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1273 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (0 != (ret = mbedtls_gcm_finish(
1276 (mbedtls_gcm_context *) ctx->cipher_ctx,
1277 NULL, 0, &output_length,
1278 check_tag, tag_len))) {
1279 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001280 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001281
1282 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001284 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001285 goto exit;
1286 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001287 }
Daniel King8fe47012016-05-17 20:33:28 -03001288#endif /* MBEDTLS_GCM_C */
1289
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001290#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001291 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001292 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (tag_len != sizeof(check_tag)) {
1294 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1295 }
Daniel King8fe47012016-05-17 20:33:28 -03001296
Hanno Becker18597cd2018-11-09 16:36:33 +00001297 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1299 if (ret != 0) {
1300 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001301 }
1302
1303 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001305 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001306 goto exit;
1307 }
Daniel King8fe47012016-05-17 20:33:28 -03001308 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001309#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001310
Gilles Peskinecd742982021-12-13 16:57:47 +01001311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_platform_zeroize(check_tag, tag_len);
1313 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001314}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001315#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001316
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001317/*
1318 * Packet-oriented wrapper for non-AEAD modes
1319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1321 const unsigned char *iv, size_t iv_len,
1322 const unsigned char *input, size_t ilen,
1323 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001324{
Janos Follath24eed8d2019-11-22 13:21:35 +00001325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001326 size_t finish_olen;
1327
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001328#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001330 /* As in the non-PSA case, we don't check that
1331 * a key has been set. If not, the key slot will
1332 * still be in its default state of 0, which is
1333 * guaranteed to be invalid, hence the PSA-call
1334 * below will gracefully fail. */
1335 mbedtls_cipher_context_psa * const cipher_psa =
1336 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1337
1338 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001339 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001340 size_t part_len;
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (ctx->operation == MBEDTLS_DECRYPT) {
1343 status = psa_cipher_decrypt_setup(&cipher_op,
1344 cipher_psa->slot,
1345 cipher_psa->alg);
1346 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1347 status = psa_cipher_encrypt_setup(&cipher_op,
1348 cipher_psa->slot,
1349 cipher_psa->alg);
1350 } else {
1351 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001352 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001353
1354 /* In the following, we can immediately return on an error,
1355 * because the PSA Crypto API guarantees that cipher operations
1356 * are terminated by unsuccessful calls to psa_cipher_update(),
1357 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (status != PSA_SUCCESS) {
1359 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001360 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001361
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001362 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1364 if (status != PSA_SUCCESS) {
1365 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1366 }
1367 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 status = psa_cipher_update(&cipher_op,
1370 input, ilen,
1371 output, ilen, olen);
1372 if (status != PSA_SUCCESS) {
1373 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1374 }
1375
1376 status = psa_cipher_finish(&cipher_op,
1377 output + *olen, ilen - *olen,
1378 &part_len);
1379 if (status != PSA_SUCCESS) {
1380 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1381 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001382
1383 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001385 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001386#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1389 return ret;
1390 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1393 return ret;
1394 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1397 output, olen)) != 0) {
1398 return ret;
1399 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1402 &finish_olen)) != 0) {
1403 return ret;
1404 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001405
1406 *olen += finish_olen;
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001409}
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001412/*
TRodziewicz18efb732021-04-29 23:12:19 +02001413 * Packet-oriented encryption for AEAD modes: internal function used by
1414 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1417 const unsigned char *iv, size_t iv_len,
1418 const unsigned char *ad, size_t ad_len,
1419 const unsigned char *input, size_t ilen,
1420 unsigned char *output, size_t *olen,
1421 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001422{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001423#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001425 /* As in the non-PSA case, we don't check that
1426 * a key has been set. If not, the key slot will
1427 * still be in its default state of 0, which is
1428 * guaranteed to be invalid, hence the PSA-call
1429 * below will gracefully fail. */
1430 mbedtls_cipher_context_psa * const cipher_psa =
1431 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1432
1433 psa_status_t status;
1434
1435 /* PSA Crypto API always writes the authentication tag
1436 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (output == NULL || tag != output + ilen) {
1438 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1439 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 status = psa_aead_encrypt(cipher_psa->slot,
1442 cipher_psa->alg,
1443 iv, iv_len,
1444 ad, ad_len,
1445 input, ilen,
1446 output, ilen + tag_len, olen);
1447 if (status != PSA_SUCCESS) {
1448 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1449 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001450
1451 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001453 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001454#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001457 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001458 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1460 ilen, iv, iv_len, ad, ad_len,
1461 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_GCM_C */
1464#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001465 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001466 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1468 iv, iv_len, ad, ad_len, input, output,
1469 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001472#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001473 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001474 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001475 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 (tag_len != 16U)) {
1477 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001478 }
1479
1480 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1482 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001483 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001484#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001487}
1488
1489/*
TRodziewicz18efb732021-04-29 23:12:19 +02001490 * Packet-oriented encryption for AEAD modes: internal function used by
1491 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1494 const unsigned char *iv, size_t iv_len,
1495 const unsigned char *ad, size_t ad_len,
1496 const unsigned char *input, size_t ilen,
1497 unsigned char *output, size_t *olen,
1498 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001499{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001500#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001502 /* As in the non-PSA case, we don't check that
1503 * a key has been set. If not, the key slot will
1504 * still be in its default state of 0, which is
1505 * guaranteed to be invalid, hence the PSA-call
1506 * below will gracefully fail. */
1507 mbedtls_cipher_context_psa * const cipher_psa =
1508 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1509
1510 psa_status_t status;
1511
1512 /* PSA Crypto API always writes the authentication tag
1513 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if (input == NULL || tag != input + ilen) {
1515 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1516 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 status = psa_aead_decrypt(cipher_psa->slot,
1519 cipher_psa->alg,
1520 iv, iv_len,
1521 ad, ad_len,
1522 input, ilen + tag_len,
1523 output, ilen, olen);
1524 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1525 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1526 } else if (status != PSA_SUCCESS) {
1527 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1528 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001531 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001532#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001535 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001537
1538 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1540 iv, iv_len, ad, ad_len,
1541 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_GCM_C */
1550#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001551 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001553
1554 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1556 iv, iv_len, ad, ad_len,
1557 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001564 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001566#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001567 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001569
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001570 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001571 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 (tag_len != 16U)) {
1573 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001574 }
1575
1576 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1578 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001581 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 }
Daniel King8fe47012016-05-17 20:33:28 -03001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001585 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001586#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001591
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001592#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1593/*
1594 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1597 const unsigned char *iv, size_t iv_len,
1598 const unsigned char *ad, size_t ad_len,
1599 const unsigned char *input, size_t ilen,
1600 unsigned char *output, size_t output_len,
1601 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001603#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001605#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001606 ctx->psa_enabled == 0 &&
1607#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001608 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1609 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1610 mbedtls_nist_kw_mode_t mode =
1611 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1612 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001613
1614 /* There is no iv, tag or ad associated with KW and KWP,
1615 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1617 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1618 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001619
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001620 (void) iv;
1621 (void) ad;
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1624 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001625 }
1626#endif /* MBEDTLS_NIST_KW_C */
1627
1628#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1629 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (output_len < ilen + tag_len) {
1631 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1632 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1635 input, ilen, output, olen,
1636 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001637 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001639#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001641#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1642}
1643
1644/*
1645 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1648 const unsigned char *iv, size_t iv_len,
1649 const unsigned char *ad, size_t ad_len,
1650 const unsigned char *input, size_t ilen,
1651 unsigned char *output, size_t output_len,
1652 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001653{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001654#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001656#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001657 ctx->psa_enabled == 0 &&
1658#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001659 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1660 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1661 mbedtls_nist_kw_mode_t mode =
1662 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1663 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001664
1665 /* There is no iv, tag or ad associated with KW and KWP,
1666 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1668 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1669 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001670
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001671 (void) iv;
1672 (void) ad;
1673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1675 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001676 }
1677#endif /* MBEDTLS_NIST_KW_C */
1678
1679#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1680 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (ilen < tag_len || output_len < ilen - tag_len) {
1682 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1683 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1686 input, ilen - tag_len, output, olen,
1687 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001688#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001690#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1691}
1692#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_CIPHER_C */