blob: 4a25b6a040717f1124839f8a18772855db677860 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \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"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Hanno Becker4ccfc402018-11-09 16:10:57 +000057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020059#include "psa_util_internal.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000060#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
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
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000224#if defined(MBEDTLS_USE_PSA_CRYPTO)
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
236 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000237 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000240 return;
241 }
242#endif /* MBEDTLS_USE_PSA_CRYPTO */
243
Simon Butcher327398a2016-10-05 14:09:11 +0100244#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (ctx->cmac_ctx) {
246 mbedtls_platform_zeroize(ctx->cmac_ctx,
247 sizeof(mbedtls_cmac_context_t));
248 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100249 }
250#endif
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100253 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200257}
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
260 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261{
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (cipher_info == NULL) {
263 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
264 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
Dave Rodgmande3de772023-06-24 12:51:06 +0100268 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
270 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271
272 ctx->cipher_info = cipher_info;
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200275 /*
276 * Ignore possible errors caused by a cipher mode that doesn't use padding
277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200280#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000286}
287
Hanno Becker4ccfc402018-11-09 16:10:57 +0000288#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200289#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
291 const mbedtls_cipher_info_t *cipher_info,
292 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000293{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000294 psa_algorithm_t alg;
295 mbedtls_cipher_context_psa *cipher_psa;
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (NULL == cipher_info || NULL == ctx) {
298 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
299 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000300
Hanno Becker4ee7e762018-11-17 22:00:38 +0000301 /* Check that the underlying cipher mode and cipher type are
302 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100303 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (alg == 0) {
305 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
306 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100307 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
309 }
Hanno Becker6118e432018-11-09 16:47:20 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
314 if (cipher_psa == NULL) {
315 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
316 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000317 cipher_psa->alg = alg;
318 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000319 ctx->cipher_info = cipher_info;
320 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000322}
Przemek Stekielef1fb4a2022-05-06 10:55:10 +0200323#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000324#endif /* MBEDTLS_USE_PSA_CRYPTO */
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
327 const unsigned char *key,
328 int key_bitlen,
329 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330{
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100332 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 }
334 if (ctx->cipher_info == NULL) {
335 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
336 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000338#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000340 mbedtls_cipher_context_psa * const cipher_psa =
341 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000344
345 psa_status_t status;
346 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000348
349 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (key_bitlen % 8 != 0) {
351 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
352 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000353
354 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
356 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
357 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000358
Andrzej Kurekc7509322019-01-08 09:36:01 -0500359 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100360 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (key_type == 0) {
362 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
363 }
364 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000365
366 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500367 * (encrypt vs. decrypt): it is possible to setup a key for encryption
368 * and use it for AEAD decryption. Until tests relying on this
369 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
372 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 status = psa_import_key(&attributes, key, key_bytelen,
375 &cipher_psa->slot);
376 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200377 case PSA_SUCCESS:
378 break;
379 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200381 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200383 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200385 }
386 /* Indicate that we own the key slot and need to
387 * destroy it in mbedtls_cipher_free(). */
388 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000389
390 ctx->key_bitlen = key_bitlen;
391 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000393 }
394#endif /* MBEDTLS_USE_PSA_CRYPTO */
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100397 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200399 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200400
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200401 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402 ctx->operation = operation;
403
Paul Bakker343a8702011-06-09 14:27:58 +0000404 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100405 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100408 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
409 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
410 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100411 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100412 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000413 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100416 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100417 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421}
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
424 const unsigned char *iv,
425 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200427 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ctx->cipher_info == NULL) {
430 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
431 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000434 /* While PSA Crypto has an API for multipart
435 * operations, we currently don't make it
436 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000438 }
439#endif /* MBEDTLS_USE_PSA_CRYPTO */
440
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200441 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
443 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
444 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200447 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100449 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200450
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200451 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (actual_iv_size > iv_len) {
453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
454 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200455 }
456
Daniel Kingbd920622016-05-15 19:56:20 -0300457#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100458 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100459 /* Even though the actual_iv_size is overwritten with a correct value
460 * of 12 from the cipher info, return an error to indicate that
461 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (iv_len != 12) {
463 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
464 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
467 iv,
468 0U)) { /* Initial counter value */
469 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300470 }
471 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100472#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100473 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 iv_len != 12) {
475 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
476 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100477#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300478#endif
479
Gilles Peskine295fc132021-04-15 18:32:23 +0200480#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100481 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
483 ctx->operation,
484 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200485 }
486#endif
487
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200488#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100489 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200490 int set_lengths_result;
491 int ccm_star_mode;
492
493 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 (mbedtls_ccm_context *) ctx->cipher_ctx,
495 0, 0, 0);
496 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200497 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200501 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200503 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200505 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
509 ccm_star_mode,
510 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200511 }
512#endif
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (actual_iv_size != 0) {
515 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300516 ctx->iv_size = actual_iv_size;
517 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200520}
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200523{
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ctx->cipher_info == NULL) {
525 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
526 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200527
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000528#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000530 /* We don't support resetting PSA-based
531 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000533 }
534#endif /* MBEDTLS_USE_PSA_CRYPTO */
535
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536 ctx->unprocessed_len = 0;
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200539}
540
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200541#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
543 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200544{
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ctx->cipher_info == NULL) {
546 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
547 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200548
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000551 /* While PSA Crypto has an API for multipart
552 * operations, we currently don't make it
553 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000555 }
556#endif /* MBEDTLS_USE_PSA_CRYPTO */
557
Daniel King8fe47012016-05-17 20:33:28 -0300558#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100559 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
561 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200562 }
Daniel King8fe47012016-05-17 20:33:28 -0300563#endif
564
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200565#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100566 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300567 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200568 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200571 ? MBEDTLS_CHACHAPOLY_ENCRYPT
572 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
575 ctx->iv,
576 mode);
577 if (result != 0) {
578 return result;
579 }
Daniel King8fe47012016-05-17 20:33:28 -0300580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
582 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300583 }
584#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200588#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
591 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592{
Janos Follath24eed8d2019-11-22 13:21:35 +0000593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500594 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ctx->cipher_info == NULL) {
597 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
598 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000600#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000602 /* While PSA Crypto has an API for multipart
603 * operations, we currently don't make it
604 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000606 }
607#endif /* MBEDTLS_USE_PSA_CRYPTO */
608
Paul Bakker6c212762013-12-16 15:24:50 +0100609 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 block_size = mbedtls_cipher_get_block_size(ctx);
611 if (0 == block_size) {
612 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100613 }
Paul Bakker6c212762013-12-16 15:24:50 +0100614
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100615 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (ilen != block_size) {
617 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
618 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200619
620 *olen = ilen;
621
Dave Rodgmande3de772023-06-24 12:51:06 +0100622 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100623 ctx->operation, input,
624 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200626 }
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100632 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
634 input, ilen,
635 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200636 }
637#endif
638
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200639#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100640 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
642 input, ilen,
643 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200644 }
645#endif
646
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200647#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100648 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200649 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
651 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200652 }
653#endif
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (input == output &&
656 (ctx->unprocessed_len != 0 || ilen % block_size)) {
657 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100658 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100661 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200662 size_t copy_len = 0;
663
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 /*
665 * If there is not enough data for a full block, cache it.
666 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
668 ilen <= block_size - ctx->unprocessed_len) ||
669 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
670 ilen < block_size - ctx->unprocessed_len) ||
671 (ctx->operation == MBEDTLS_ENCRYPT &&
672 ilen < block_size - ctx->unprocessed_len)) {
673 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
674 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
676 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678 }
679
680 /*
681 * Process cached data first
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100684 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
687 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000688
Dave Rodgmande3de772023-06-24 12:51:06 +0100689 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100690 ctx->operation,
691 block_size, ctx->iv,
692 ctx->
693 unprocessed_data,
694 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696 }
697
Janos Follath98e28a72016-05-31 14:03:54 +0100698 *olen += block_size;
699 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700 ctx->unprocessed_len = 0;
701
702 input += copy_len;
703 ilen -= copy_len;
704 }
705
706 /*
707 * Cache final, incomplete block
708 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700710 /* Encryption: only cache partial blocks
711 * Decryption w/ padding: always keep at least one whole block
712 * Decryption w/o padding: only cache partial blocks
713 */
Janos Follath98e28a72016-05-31 14:03:54 +0100714 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700716 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100718 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700719 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
722 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723
724 ctx->unprocessed_len += copy_len;
725 ilen -= copy_len;
726 }
727
728 /*
729 * Process remaining full blocks
730 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100732 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100733 ctx->operation,
734 ilen, ctx->iv,
735 input,
736 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200739
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740 *olen += ilen;
741 }
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100748 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100749 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100750 ctx->operation, ilen,
751 &ctx->unprocessed_len,
752 ctx->iv,
753 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000755 }
756
757 *olen = ilen;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000760 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000762
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100763#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100764 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100765 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100766 ilen,
767 &ctx->unprocessed_len,
768 ctx->iv,
769 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100771 }
772
773 *olen = ilen;
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100776 }
777#endif /* MBEDTLS_CIPHER_MODE_OFB */
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100780 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100781 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100782 ilen,
783 &ctx->unprocessed_len,
784 ctx->iv,
785 ctx->unprocessed_data,
786 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000788 }
789
790 *olen = ilen;
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000795
Jaeden Ameroc6539902018-04-30 17:17:41 +0100796#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100797 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100799 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100801 }
802
Dave Rodgmande3de772023-06-24 12:51:06 +0100803 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100804 ctx->operation,
805 ilen,
806 ctx->iv,
807 input,
808 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (ret != 0) {
810 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100811 }
812
813 *olen = ilen;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100816 }
817#endif /* MBEDTLS_CIPHER_MODE_XTS */
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100820 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100821 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100822 ilen, input,
823 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200825 }
826
827 *olen = ilen;
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834}
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
837#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200838/*
839 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
840 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841static void add_pkcs_padding(unsigned char *output, size_t output_len,
842 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843{
Paul Bakker23986e52011-04-24 08:57:21 +0000844 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100845 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000848 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850}
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852static int get_pkcs_padding(unsigned char *input, size_t input_len,
853 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100855 size_t i, pad_idx;
856 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (NULL == input || NULL == data_len) {
859 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
860 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000861
862 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863 *data_len = input_len - padding_len;
864
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100865 /* Avoid logical || since it results in a branch */
866 bad |= padding_len > input_len;
867 bad |= padding_len == 0;
868
869 /* The number of bytes checked must be independent of padding_len,
870 * so pick input_len, which is usually 8 or 16 (one block) */
871 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 for (i = 0; i < input_len; i++) {
873 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
874 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200881/*
882 * One and zeros padding: fill with 80 00 ... 00
883 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884static void add_one_and_zeros_padding(unsigned char *output,
885 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200886{
887 size_t padding_len = output_len - data_len;
888 unsigned char i = 0;
889
890 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200892 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200894}
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
897 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200898{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100899 size_t i;
900 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (NULL == input || NULL == data_len) {
903 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
904 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200905
Micha Krausba8316f2017-12-23 23:40:08 +0100906 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100907 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100909 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 done |= (input[i - 1] != 0);
911 *data_len |= (i - 1) * (done != prev_done);
912 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100913 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200916
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200921/*
922 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924static void add_zeros_and_len_padding(unsigned char *output,
925 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200926{
927 size_t padding_len = output_len - data_len;
928 unsigned char i = 0;
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200931 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200933 output[output_len - 1] = (unsigned char) padding_len;
934}
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
937 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200938{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100939 size_t i, pad_idx;
940 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (NULL == input || NULL == data_len) {
943 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
944 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200945
946 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200947 *data_len = input_len - padding_len;
948
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100949 /* Avoid logical || since it results in a branch */
950 bad |= padding_len > input_len;
951 bad |= padding_len == 0;
952
953 /* The number of bytes checked must be independent of padding_len */
954 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 for (i = 0; i < input_len - 1; i++) {
956 bad |= input[i] * (i >= pad_idx);
957 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200964/*
965 * Zero padding: fill with 00 ... 00
966 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967static void add_zeros_padding(unsigned char *output,
968 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200969{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200970 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200973 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200975}
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977static int get_zeros_padding(unsigned char *input, size_t input_len,
978 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200979{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100980 size_t i;
981 unsigned char done = 0, prev_done;
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (NULL == input || NULL == data_len) {
984 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100985 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 *data_len = 0;
988 for (i = input_len; i > 0; i--) {
989 prev_done = done;
990 done |= (input[i-1] != 0);
991 *data_len |= i * (done != prev_done);
992 }
993
994 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200995}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200997
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998/*
999 * No padding: don't pad :)
1000 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001002 * but a trivial get_padding function
1003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004static int get_no_padding(unsigned char *input, size_t input_len,
1005 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001006{
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (NULL == input || NULL == data_len) {
1008 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1009 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001010
1011 *data_len = input_len;
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1018 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001019{
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (ctx->cipher_info == NULL) {
1021 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1022 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001023
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001024#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001026 /* While PSA Crypto has an API for multipart
1027 * operations, we currently don't make it
1028 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001030 }
1031#endif /* MBEDTLS_USE_PSA_CRYPTO */
1032
Paul Bakker8123e9d2011-01-06 15:37:30 +00001033 *olen = 0;
1034
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001035 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1036 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1037 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1038 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1039 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001043 }
1044
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001045 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1046 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001048 }
1049
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001050 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ctx->unprocessed_len != 0) {
1052 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1053 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001056 }
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001059 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001060 int ret = 0;
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001063 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (NULL == ctx->add_padding) {
1065 if (0 != ctx->unprocessed_len) {
1066 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1067 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1073 ctx->unprocessed_len);
1074 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001075 /*
1076 * For decrypt operations, expect a full block,
1077 * or an empty block if no padding
1078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1080 return 0;
1081 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001084 }
1085
1086 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001087 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001088 ctx->operation,
1089 mbedtls_cipher_get_block_size(
1090 ctx),
1091 ctx->iv,
1092 ctx->unprocessed_data,
1093 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001095 }
1096
1097 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (MBEDTLS_DECRYPT == ctx->operation) {
1099 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1100 olen);
1101 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001102
1103 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 *olen = mbedtls_cipher_get_block_size(ctx);
1105 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001107#else
1108 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001112}
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001115int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1116 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001117{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001118 if (NULL == ctx->cipher_info ||
1119 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001121 }
1122
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001123#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001125 /* While PSA Crypto knows about CBC padding
1126 * schemes, we currently don't make them
1127 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (mode != MBEDTLS_PADDING_NONE) {
1129 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1130 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001133 }
1134#endif /* MBEDTLS_USE_PSA_CRYPTO */
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 case MBEDTLS_PADDING_PKCS7:
1139 ctx->add_padding = add_pkcs_padding;
1140 ctx->get_padding = get_pkcs_padding;
1141 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1145 ctx->add_padding = add_one_and_zeros_padding;
1146 ctx->get_padding = get_one_and_zeros_padding;
1147 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1151 ctx->add_padding = add_zeros_and_len_padding;
1152 ctx->get_padding = get_zeros_and_len_padding;
1153 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 case MBEDTLS_PADDING_ZEROS:
1157 ctx->add_padding = add_zeros_padding;
1158 ctx->get_padding = get_zeros_padding;
1159 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001160#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 case MBEDTLS_PADDING_NONE:
1162 ctx->add_padding = NULL;
1163 ctx->get_padding = get_no_padding;
1164 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 default:
1167 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001168 }
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001173
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001174#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001175int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1176 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001177{
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (ctx->cipher_info == NULL) {
1179 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1180 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (MBEDTLS_ENCRYPT != ctx->operation) {
1183 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1184 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001186#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001188 /* While PSA Crypto has an API for multipart
1189 * operations, we currently don't make it
1190 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001192 }
1193#endif /* MBEDTLS_USE_PSA_CRYPTO */
1194
Daniel King8fe47012016-05-17 20:33:28 -03001195#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001196 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001197 size_t output_length;
1198 /* The code here doesn't yet support alternative implementations
1199 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1201 NULL, 0, &output_length,
1202 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001203 }
Daniel King8fe47012016-05-17 20:33:28 -03001204#endif
1205
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001206#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001207 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001208 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (tag_len != 16U) {
1210 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1211 }
Daniel King8fe47012016-05-17 20:33:28 -03001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 return mbedtls_chachapoly_finish(
1214 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001215 }
1216#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001219}
Paul Bakker9af723c2014-05-01 13:03:14 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1222 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001223{
Daniel King8fe47012016-05-17 20:33:28 -03001224 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (ctx->cipher_info == NULL) {
1228 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1229 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (MBEDTLS_DECRYPT != ctx->operation) {
1232 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001233 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001234
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001235#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001237 /* While PSA Crypto has an API for multipart
1238 * operations, we currently don't make it
1239 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001241 }
1242#endif /* MBEDTLS_USE_PSA_CRYPTO */
1243
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001244 /* Status to return on a non-authenticated algorithm. */
1245 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001246
Daniel King8fe47012016-05-17 20:33:28 -03001247#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001248 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001249 size_t output_length;
1250 /* The code here doesn't yet support alternative implementations
1251 * that can delay up to a block of output. */
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (tag_len > sizeof(check_tag)) {
1254 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1255 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (0 != (ret = mbedtls_gcm_finish(
1258 (mbedtls_gcm_context *) ctx->cipher_ctx,
1259 NULL, 0, &output_length,
1260 check_tag, tag_len))) {
1261 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001262 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001263
1264 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001266 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001267 goto exit;
1268 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001269 }
Daniel King8fe47012016-05-17 20:33:28 -03001270#endif /* MBEDTLS_GCM_C */
1271
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001272#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001273 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001274 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (tag_len != sizeof(check_tag)) {
1276 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1277 }
Daniel King8fe47012016-05-17 20:33:28 -03001278
Hanno Becker18597cd2018-11-09 16:36:33 +00001279 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1281 if (ret != 0) {
1282 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001283 }
1284
1285 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001287 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001288 goto exit;
1289 }
Daniel King8fe47012016-05-17 20:33:28 -03001290 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001291#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001292
Gilles Peskinecd742982021-12-13 16:57:47 +01001293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_platform_zeroize(check_tag, tag_len);
1295 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001296}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001297#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001298
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001299/*
1300 * Packet-oriented wrapper for non-AEAD modes
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1303 const unsigned char *iv, size_t iv_len,
1304 const unsigned char *input, size_t ilen,
1305 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001306{
Janos Follath24eed8d2019-11-22 13:21:35 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001308 size_t finish_olen;
1309
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001310#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001312 /* As in the non-PSA case, we don't check that
1313 * a key has been set. If not, the key slot will
1314 * still be in its default state of 0, which is
1315 * guaranteed to be invalid, hence the PSA-call
1316 * below will gracefully fail. */
1317 mbedtls_cipher_context_psa * const cipher_psa =
1318 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1319
1320 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001321 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001322 size_t part_len;
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (ctx->operation == MBEDTLS_DECRYPT) {
1325 status = psa_cipher_decrypt_setup(&cipher_op,
1326 cipher_psa->slot,
1327 cipher_psa->alg);
1328 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1329 status = psa_cipher_encrypt_setup(&cipher_op,
1330 cipher_psa->slot,
1331 cipher_psa->alg);
1332 } else {
1333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001334 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001335
1336 /* In the following, we can immediately return on an error,
1337 * because the PSA Crypto API guarantees that cipher operations
1338 * are terminated by unsuccessful calls to psa_cipher_update(),
1339 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (status != PSA_SUCCESS) {
1341 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001342 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001343
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001344 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1346 if (status != PSA_SUCCESS) {
1347 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1348 }
1349 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 status = psa_cipher_update(&cipher_op,
1352 input, ilen,
1353 output, ilen, olen);
1354 if (status != PSA_SUCCESS) {
1355 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1356 }
1357
1358 status = psa_cipher_finish(&cipher_op,
1359 output + *olen, ilen - *olen,
1360 &part_len);
1361 if (status != PSA_SUCCESS) {
1362 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1363 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001364
1365 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001367 }
1368#endif /* MBEDTLS_USE_PSA_CRYPTO */
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1371 return ret;
1372 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1375 return ret;
1376 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1379 output, olen)) != 0) {
1380 return ret;
1381 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1384 &finish_olen)) != 0) {
1385 return ret;
1386 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001387
1388 *olen += finish_olen;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001391}
1392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001394/*
TRodziewicz18efb732021-04-29 23:12:19 +02001395 * Packet-oriented encryption for AEAD modes: internal function used by
1396 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1399 const unsigned char *iv, size_t iv_len,
1400 const unsigned char *ad, size_t ad_len,
1401 const unsigned char *input, size_t ilen,
1402 unsigned char *output, size_t *olen,
1403 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001405#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001407 /* As in the non-PSA case, we don't check that
1408 * a key has been set. If not, the key slot will
1409 * still be in its default state of 0, which is
1410 * guaranteed to be invalid, hence the PSA-call
1411 * below will gracefully fail. */
1412 mbedtls_cipher_context_psa * const cipher_psa =
1413 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1414
1415 psa_status_t status;
1416
1417 /* PSA Crypto API always writes the authentication tag
1418 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (output == NULL || tag != output + ilen) {
1420 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1421 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 status = psa_aead_encrypt(cipher_psa->slot,
1424 cipher_psa->alg,
1425 iv, iv_len,
1426 ad, ad_len,
1427 input, ilen,
1428 output, ilen + tag_len, olen);
1429 if (status != PSA_SUCCESS) {
1430 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1431 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001432
1433 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001435 }
1436#endif /* MBEDTLS_USE_PSA_CRYPTO */
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001439 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001440 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1442 ilen, iv, iv_len, ad, ad_len,
1443 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_GCM_C */
1446#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001447 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001448 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1450 iv, iv_len, ad, ad_len, input, output,
1451 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001452 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001454#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001455 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001456 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001457 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 (tag_len != 16U)) {
1459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001460 }
1461
1462 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1464 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001466#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001469}
1470
1471/*
TRodziewicz18efb732021-04-29 23:12:19 +02001472 * Packet-oriented encryption for AEAD modes: internal function used by
1473 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1476 const unsigned char *iv, size_t iv_len,
1477 const unsigned char *ad, size_t ad_len,
1478 const unsigned char *input, size_t ilen,
1479 unsigned char *output, size_t *olen,
1480 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001481{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001482#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001484 /* As in the non-PSA case, we don't check that
1485 * a key has been set. If not, the key slot will
1486 * still be in its default state of 0, which is
1487 * guaranteed to be invalid, hence the PSA-call
1488 * below will gracefully fail. */
1489 mbedtls_cipher_context_psa * const cipher_psa =
1490 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1491
1492 psa_status_t status;
1493
1494 /* PSA Crypto API always writes the authentication tag
1495 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (input == NULL || tag != input + ilen) {
1497 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1498 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 status = psa_aead_decrypt(cipher_psa->slot,
1501 cipher_psa->alg,
1502 iv, iv_len,
1503 ad, ad_len,
1504 input, ilen + tag_len,
1505 output, ilen, olen);
1506 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1507 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1508 } else if (status != PSA_SUCCESS) {
1509 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1510 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001513 }
1514#endif /* MBEDTLS_USE_PSA_CRYPTO */
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001517 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001519
1520 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1522 iv, iv_len, ad, ad_len,
1523 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_GCM_C */
1532#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001533 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001535
1536 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1538 iv, iv_len, ad, ad_len,
1539 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001548#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001549 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001551
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001552 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001553 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 (tag_len != 16U)) {
1555 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001556 }
1557
1558 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1560 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001563 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 }
Daniel King8fe47012016-05-17 20:33:28 -03001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001567 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001568#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001573
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001574#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1575/*
1576 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1579 const unsigned char *iv, size_t iv_len,
1580 const unsigned char *ad, size_t ad_len,
1581 const unsigned char *input, size_t ilen,
1582 unsigned char *output, size_t output_len,
1583 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001584{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001585#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001587#if defined(MBEDTLS_USE_PSA_CRYPTO)
1588 ctx->psa_enabled == 0 &&
1589#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001590 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1591 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1592 mbedtls_nist_kw_mode_t mode =
1593 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1594 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001595
1596 /* There is no iv, tag or ad associated with KW and KWP,
1597 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1599 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1600 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001601
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001602 (void) iv;
1603 (void) ad;
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1606 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001607 }
1608#endif /* MBEDTLS_NIST_KW_C */
1609
1610#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1611 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (output_len < ilen + tag_len) {
1613 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1614 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1617 input, ilen, output, olen,
1618 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001619 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001621#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001623#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1624}
1625
1626/*
1627 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1630 const unsigned char *iv, size_t iv_len,
1631 const unsigned char *ad, size_t ad_len,
1632 const unsigned char *input, size_t ilen,
1633 unsigned char *output, size_t output_len,
1634 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001635{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001636#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001638#if defined(MBEDTLS_USE_PSA_CRYPTO)
1639 ctx->psa_enabled == 0 &&
1640#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001641 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1642 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1643 mbedtls_nist_kw_mode_t mode =
1644 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1645 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001646
1647 /* There is no iv, tag or ad associated with KW and KWP,
1648 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1650 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1651 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001652
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001653 (void) iv;
1654 (void) ad;
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1657 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001658 }
1659#endif /* MBEDTLS_NIST_KW_C */
1660
1661#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1662 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ilen < tag_len || output_len < ilen - tag_len) {
1664 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1665 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1668 input, ilen - tag_len, output, olen,
1669 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001670#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001672#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1673}
1674#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_CIPHER_C */