blob: 41cd96f7853bf8e8849966726f7f8e17a329d479 [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"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020060#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000061
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200147#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
148static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
149 mbedtls_cipher_type_t cipher)
150{
151 switch (cipher) {
152 case MBEDTLS_CIPHER_AES_128_CCM:
153 case MBEDTLS_CIPHER_AES_192_CCM:
154 case MBEDTLS_CIPHER_AES_256_CCM:
155 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
158 case MBEDTLS_CIPHER_AES_128_GCM:
159 case MBEDTLS_CIPHER_AES_192_GCM:
160 case MBEDTLS_CIPHER_AES_256_GCM:
161 case MBEDTLS_CIPHER_AES_128_CBC:
162 case MBEDTLS_CIPHER_AES_192_CBC:
163 case MBEDTLS_CIPHER_AES_256_CBC:
164 case MBEDTLS_CIPHER_AES_128_ECB:
165 case MBEDTLS_CIPHER_AES_192_ECB:
166 case MBEDTLS_CIPHER_AES_256_ECB:
167 return PSA_KEY_TYPE_AES;
168
169 /* ARIA not yet supported in PSA. */
170 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
171 case MBEDTLS_CIPHER_ARIA_192_CCM:
172 case MBEDTLS_CIPHER_ARIA_256_CCM:
173 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
176 case MBEDTLS_CIPHER_ARIA_128_GCM:
177 case MBEDTLS_CIPHER_ARIA_192_GCM:
178 case MBEDTLS_CIPHER_ARIA_256_GCM:
179 case MBEDTLS_CIPHER_ARIA_128_CBC:
180 case MBEDTLS_CIPHER_ARIA_192_CBC:
181 case MBEDTLS_CIPHER_ARIA_256_CBC:
182 return( PSA_KEY_TYPE_ARIA ); */
183
184 default:
185 return 0;
186 }
187}
188
189static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
190 mbedtls_cipher_mode_t mode, size_t taglen)
191{
192 switch (mode) {
193 case MBEDTLS_MODE_ECB:
194 return PSA_ALG_ECB_NO_PADDING;
195 case MBEDTLS_MODE_GCM:
196 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
197 case MBEDTLS_MODE_CCM:
198 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
199 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
200 return PSA_ALG_CCM_STAR_NO_TAG;
201 case MBEDTLS_MODE_CBC:
202 if (taglen == 0) {
203 return PSA_ALG_CBC_NO_PADDING;
204 } else {
205 return 0;
206 }
207 default:
208 return 0;
209 }
210}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200221 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (ctx->psa_enabled == 1) {
226 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000227 mbedtls_cipher_context_psa * const cipher_psa =
228 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000233 }
234
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100235 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100245 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100247 }
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100251 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
258 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (cipher_info == NULL) {
261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Dave Rodgmande3de772023-06-24 12:51:06 +0100266 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
268 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
270 ctx->cipher_info = cipher_info;
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200273 /*
274 * Ignore possible errors caused by a cipher mode that doesn't use padding
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200278#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284}
285
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200286#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
288 const mbedtls_cipher_info_t *cipher_info,
289 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000290{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000291 psa_algorithm_t alg;
292 mbedtls_cipher_context_psa *cipher_psa;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (NULL == cipher_info || NULL == ctx) {
295 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
296 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000297
Hanno Becker4ee7e762018-11-17 22:00:38 +0000298 /* Check that the underlying cipher mode and cipher type are
299 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100300 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (alg == 0) {
302 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
303 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100304 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
306 }
Hanno Becker6118e432018-11-09 16:47:20 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
311 if (cipher_psa == NULL) {
312 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
313 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000314 cipher_psa->alg = alg;
315 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000316 ctx->cipher_info = cipher_info;
317 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000319}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200320#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
323 const unsigned char *key,
324 int key_bitlen,
325 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326{
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100328 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
330 if (ctx->cipher_info == NULL) {
331 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
332 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200334#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000336 mbedtls_cipher_context_psa * const cipher_psa =
337 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000340
341 psa_status_t status;
342 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000344
345 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (key_bitlen % 8 != 0) {
347 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
348 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000349
350 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
352 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
353 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000354
Andrzej Kurekc7509322019-01-08 09:36:01 -0500355 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100356 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (key_type == 0) {
358 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
359 }
360 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000361
362 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500363 * (encrypt vs. decrypt): it is possible to setup a key for encryption
364 * and use it for AEAD decryption. Until tests relying on this
365 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
368 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 status = psa_import_key(&attributes, key, key_bytelen,
371 &cipher_psa->slot);
372 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200373 case PSA_SUCCESS:
374 break;
375 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200377 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200379 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200381 }
382 /* Indicate that we own the key slot and need to
383 * destroy it in mbedtls_cipher_free(). */
384 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000385
386 ctx->key_bitlen = key_bitlen;
387 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000389 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200390#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100393 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200395 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200396
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200397 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398 ctx->operation = operation;
399
Paul Bakker343a8702011-06-09 14:27:58 +0000400 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100401 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000402 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100404 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
405 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
406 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100407 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100408 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000409 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100412 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100413 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417}
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
420 const unsigned char *iv,
421 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200423 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ctx->cipher_info == NULL) {
426 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
427 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200428#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000430 /* While PSA Crypto has an API for multipart
431 * operations, we currently don't make it
432 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000434 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200435#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000436
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200437 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
439 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
440 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200443 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100445 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200446
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200447 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (actual_iv_size > iv_len) {
449 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
450 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200451 }
452
Daniel Kingbd920622016-05-15 19:56:20 -0300453#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100454 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100455 /* Even though the actual_iv_size is overwritten with a correct value
456 * of 12 from the cipher info, return an error to indicate that
457 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (iv_len != 12) {
459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
460 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
463 iv,
464 0U)) { /* Initial counter value */
465 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300466 }
467 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100468#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100469 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 iv_len != 12) {
471 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
472 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100473#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300474#endif
475
Gilles Peskine295fc132021-04-15 18:32:23 +0200476#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100477 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
479 ctx->operation,
480 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200481 }
482#endif
483
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200484#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100485 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200486 int set_lengths_result;
487 int ccm_star_mode;
488
489 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 (mbedtls_ccm_context *) ctx->cipher_ctx,
491 0, 0, 0);
492 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200493 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200497 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200499 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200501 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
505 ccm_star_mode,
506 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200507 }
508#endif
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (actual_iv_size != 0) {
511 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300512 ctx->iv_size = actual_iv_size;
513 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200516}
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200519{
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (ctx->cipher_info == NULL) {
521 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
522 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200523
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200524#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000526 /* We don't support resetting PSA-based
527 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000529 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200530#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000531
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532 ctx->unprocessed_len = 0;
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200535}
536
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200537#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100538int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
539 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200540{
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (ctx->cipher_info == NULL) {
542 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
543 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200544
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200545#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000547 /* While PSA Crypto has an API for multipart
548 * operations, we currently don't make it
549 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000551 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200552#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000553
Daniel King8fe47012016-05-17 20:33:28 -0300554#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100555 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
557 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200558 }
Daniel King8fe47012016-05-17 20:33:28 -0300559#endif
560
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200561#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100562 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300563 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200564 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200567 ? MBEDTLS_CHACHAPOLY_ENCRYPT
568 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
571 ctx->iv,
572 mode);
573 if (result != 0) {
574 return result;
575 }
Daniel King8fe47012016-05-17 20:33:28 -0300576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
578 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300579 }
580#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200584#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
587 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588{
Janos Follath24eed8d2019-11-22 13:21:35 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500590 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (ctx->cipher_info == NULL) {
593 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
594 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200596#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000598 /* While PSA Crypto has an API for multipart
599 * operations, we currently don't make it
600 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000602 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200603#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000604
Paul Bakker6c212762013-12-16 15:24:50 +0100605 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 block_size = mbedtls_cipher_get_block_size(ctx);
607 if (0 == block_size) {
608 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100609 }
Paul Bakker6c212762013-12-16 15:24:50 +0100610
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100611 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (ilen != block_size) {
613 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
614 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615
616 *olen = ilen;
617
Dave Rodgmande3de772023-06-24 12:51:06 +0100618 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100619 ctx->operation, input,
620 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622 }
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625 }
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100628 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
630 input, ilen,
631 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200632 }
633#endif
634
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200635#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100636 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
638 input, ilen,
639 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200640 }
641#endif
642
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200643#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100644 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200645 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
647 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200648 }
649#endif
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (input == output &&
652 (ctx->unprocessed_len != 0 || ilen % block_size)) {
653 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100654 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100657 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200658 size_t copy_len = 0;
659
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 /*
661 * If there is not enough data for a full block, cache it.
662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
664 ilen <= block_size - ctx->unprocessed_len) ||
665 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
666 ilen < block_size - ctx->unprocessed_len) ||
667 (ctx->operation == MBEDTLS_ENCRYPT &&
668 ilen < block_size - ctx->unprocessed_len)) {
669 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
670 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671
672 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 }
675
676 /*
677 * Process cached data first
678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100680 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
683 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684
Dave Rodgmande3de772023-06-24 12:51:06 +0100685 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100686 ctx->operation,
687 block_size, ctx->iv,
688 ctx->
689 unprocessed_data,
690 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000692 }
693
Janos Follath98e28a72016-05-31 14:03:54 +0100694 *olen += block_size;
695 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696 ctx->unprocessed_len = 0;
697
698 input += copy_len;
699 ilen -= copy_len;
700 }
701
702 /*
703 * Cache final, incomplete block
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700706 /* Encryption: only cache partial blocks
707 * Decryption w/ padding: always keep at least one whole block
708 * Decryption w/o padding: only cache partial blocks
709 */
Janos Follath98e28a72016-05-31 14:03:54 +0100710 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700712 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100714 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700715 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
718 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719
720 ctx->unprocessed_len += copy_len;
721 ilen -= copy_len;
722 }
723
724 /*
725 * Process remaining full blocks
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100728 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100729 ctx->operation,
730 ilen, ctx->iv,
731 input,
732 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200735
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736 *olen += ilen;
737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100744 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100745 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100746 ctx->operation, ilen,
747 &ctx->unprocessed_len,
748 ctx->iv,
749 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000751 }
752
753 *olen = ilen;
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000758
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100759#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100760 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100761 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100762 ilen,
763 &ctx->unprocessed_len,
764 ctx->iv,
765 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100767 }
768
769 *olen = ilen;
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100772 }
773#endif /* MBEDTLS_CIPHER_MODE_OFB */
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100776 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100777 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100778 ilen,
779 &ctx->unprocessed_len,
780 ctx->iv,
781 ctx->unprocessed_data,
782 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000784 }
785
786 *olen = ilen;
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000791
Jaeden Ameroc6539902018-04-30 17:17:41 +0100792#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100793 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100795 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100797 }
798
Dave Rodgmande3de772023-06-24 12:51:06 +0100799 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100800 ctx->operation,
801 ilen,
802 ctx->iv,
803 input,
804 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (ret != 0) {
806 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100807 }
808
809 *olen = ilen;
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100812 }
813#endif /* MBEDTLS_CIPHER_MODE_XTS */
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100816 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100817 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100818 ilen, input,
819 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200821 }
822
823 *olen = ilen;
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830}
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
833#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200834/*
835 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837static void add_pkcs_padding(unsigned char *output, size_t output_len,
838 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000839{
Paul Bakker23986e52011-04-24 08:57:21 +0000840 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100841 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000844 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846}
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848static int get_pkcs_padding(unsigned char *input, size_t input_len,
849 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100851 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100852 unsigned char padding_len;
853 mbedtls_ct_condition_t bad = MBEDTLS_CT_FALSE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (NULL == input || NULL == data_len) {
856 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
857 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858
859 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860 *data_len = input_len - padding_len;
861
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100862 bad = mbedtls_ct_uint_gt(padding_len, input_len);
863 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100864
865 /* The number of bytes checked must be independent of padding_len,
866 * so pick input_len, which is usually 8 or 16 (one block) */
867 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 for (i = 0; i < input_len; i++) {
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100869 mbedtls_ct_condition_t dont_ignore = mbedtls_ct_uint_ge(i, pad_idx);
870 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
871 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(dont_ignore, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100873
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100874 return mbedtls_ct_uint_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200879/*
880 * One and zeros padding: fill with 80 00 ... 00
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882static void add_one_and_zeros_padding(unsigned char *output,
883 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200884{
885 size_t padding_len = output_len - data_len;
886 unsigned char i = 0;
887
888 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200890 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200892}
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
895 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200896{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100897 size_t i;
898 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (NULL == input || NULL == data_len) {
901 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
902 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200903
Micha Krausba8316f2017-12-23 23:40:08 +0100904 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100905 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100907 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 done |= (input[i - 1] != 0);
909 *data_len |= (i - 1) * (done != prev_done);
910 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100911 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200914
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200919/*
920 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922static void add_zeros_and_len_padding(unsigned char *output,
923 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200924{
925 size_t padding_len = output_len - data_len;
926 unsigned char i = 0;
927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200929 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200931 output[output_len - 1] = (unsigned char) padding_len;
932}
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
935 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200936{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100937 size_t i, pad_idx;
938 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (NULL == input || NULL == data_len) {
941 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
942 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200943
944 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200945 *data_len = input_len - padding_len;
946
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100947 /* Avoid logical || since it results in a branch */
948 bad |= padding_len > input_len;
949 bad |= padding_len == 0;
950
951 /* The number of bytes checked must be independent of padding_len */
952 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 for (i = 0; i < input_len - 1; i++) {
954 bad |= input[i] * (i >= pad_idx);
955 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200962/*
963 * Zero padding: fill with 00 ... 00
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965static void add_zeros_padding(unsigned char *output,
966 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200967{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200968 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200971 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200973}
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static int get_zeros_padding(unsigned char *input, size_t input_len,
976 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200977{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100978 size_t i;
979 unsigned char done = 0, prev_done;
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (NULL == input || NULL == data_len) {
982 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100983 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 *data_len = 0;
986 for (i = input_len; i > 0; i--) {
987 prev_done = done;
988 done |= (input[i-1] != 0);
989 *data_len |= i * (done != prev_done);
990 }
991
992 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200993}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200995
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200996/*
997 * No padding: don't pad :)
998 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001000 * but a trivial get_padding function
1001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002static int get_no_padding(unsigned char *input, size_t input_len,
1003 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (NULL == input || NULL == data_len) {
1006 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1007 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001008
1009 *data_len = input_len;
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1016 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 if (ctx->cipher_info == NULL) {
1019 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1020 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001021
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001022#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001024 /* While PSA Crypto has an API for multipart
1025 * operations, we currently don't make it
1026 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001028 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001029#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001030
Paul Bakker8123e9d2011-01-06 15:37:30 +00001031 *olen = 0;
1032
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001033 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1034 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1035 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1036 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1037 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1038 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1039 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001041 }
1042
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001043 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1044 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001046 }
1047
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001048 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ctx->unprocessed_len != 0) {
1050 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1051 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001054 }
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001057 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001058 int ret = 0;
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001061 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (NULL == ctx->add_padding) {
1063 if (0 != ctx->unprocessed_len) {
1064 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1065 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1071 ctx->unprocessed_len);
1072 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001073 /*
1074 * For decrypt operations, expect a full block,
1075 * or an empty block if no padding
1076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1078 return 0;
1079 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001082 }
1083
1084 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001085 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001086 ctx->operation,
1087 mbedtls_cipher_get_block_size(
1088 ctx),
1089 ctx->iv,
1090 ctx->unprocessed_data,
1091 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001093 }
1094
1095 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (MBEDTLS_DECRYPT == ctx->operation) {
1097 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1098 olen);
1099 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100
1101 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 *olen = mbedtls_cipher_get_block_size(ctx);
1103 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001104 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001105#else
1106 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001110}
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1114 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001115{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001116 if (NULL == ctx->cipher_info ||
1117 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001119 }
1120
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001121#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001123 /* While PSA Crypto knows about CBC padding
1124 * schemes, we currently don't make them
1125 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (mode != MBEDTLS_PADDING_NONE) {
1127 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1128 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001131 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001132#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 case MBEDTLS_PADDING_PKCS7:
1137 ctx->add_padding = add_pkcs_padding;
1138 ctx->get_padding = get_pkcs_padding;
1139 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001140#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1143 ctx->add_padding = add_one_and_zeros_padding;
1144 ctx->get_padding = get_one_and_zeros_padding;
1145 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1149 ctx->add_padding = add_zeros_and_len_padding;
1150 ctx->get_padding = get_zeros_and_len_padding;
1151 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001152#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 case MBEDTLS_PADDING_ZEROS:
1155 ctx->add_padding = add_zeros_padding;
1156 ctx->get_padding = get_zeros_padding;
1157 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 case MBEDTLS_PADDING_NONE:
1160 ctx->add_padding = NULL;
1161 ctx->get_padding = get_no_padding;
1162 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 default:
1165 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001171
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001172#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001173int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1174 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001175{
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (ctx->cipher_info == NULL) {
1177 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1178 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (MBEDTLS_ENCRYPT != ctx->operation) {
1181 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1182 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001183
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001184#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001186 /* While PSA Crypto has an API for multipart
1187 * operations, we currently don't make it
1188 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001190 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001191#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001192
Daniel King8fe47012016-05-17 20:33:28 -03001193#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001194 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001195 size_t output_length;
1196 /* The code here doesn't yet support alternative implementations
1197 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1199 NULL, 0, &output_length,
1200 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001201 }
Daniel King8fe47012016-05-17 20:33:28 -03001202#endif
1203
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001204#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001205 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001206 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (tag_len != 16U) {
1208 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1209 }
Daniel King8fe47012016-05-17 20:33:28 -03001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 return mbedtls_chachapoly_finish(
1212 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001213 }
1214#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001217}
Paul Bakker9af723c2014-05-01 13:03:14 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1220 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001221{
Daniel King8fe47012016-05-17 20:33:28 -03001222 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (ctx->cipher_info == NULL) {
1226 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1227 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (MBEDTLS_DECRYPT != ctx->operation) {
1230 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001231 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001232
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001233#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001235 /* While PSA Crypto has an API for multipart
1236 * operations, we currently don't make it
1237 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001239 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001240#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001241
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001242 /* Status to return on a non-authenticated algorithm. */
1243 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001244
Daniel King8fe47012016-05-17 20:33:28 -03001245#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001246 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001247 size_t output_length;
1248 /* The code here doesn't yet support alternative implementations
1249 * that can delay up to a block of output. */
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (tag_len > sizeof(check_tag)) {
1252 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1253 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (0 != (ret = mbedtls_gcm_finish(
1256 (mbedtls_gcm_context *) ctx->cipher_ctx,
1257 NULL, 0, &output_length,
1258 check_tag, tag_len))) {
1259 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001260 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001261
1262 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001264 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001265 goto exit;
1266 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001267 }
Daniel King8fe47012016-05-17 20:33:28 -03001268#endif /* MBEDTLS_GCM_C */
1269
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001270#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001271 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001272 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (tag_len != sizeof(check_tag)) {
1274 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1275 }
Daniel King8fe47012016-05-17 20:33:28 -03001276
Hanno Becker18597cd2018-11-09 16:36:33 +00001277 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1279 if (ret != 0) {
1280 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001281 }
1282
1283 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001285 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001286 goto exit;
1287 }
Daniel King8fe47012016-05-17 20:33:28 -03001288 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001289#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001290
Gilles Peskinecd742982021-12-13 16:57:47 +01001291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 mbedtls_platform_zeroize(check_tag, tag_len);
1293 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001294}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001295#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001296
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001297/*
1298 * Packet-oriented wrapper for non-AEAD modes
1299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1301 const unsigned char *iv, size_t iv_len,
1302 const unsigned char *input, size_t ilen,
1303 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001304{
Janos Follath24eed8d2019-11-22 13:21:35 +00001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001306 size_t finish_olen;
1307
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001308#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001310 /* As in the non-PSA case, we don't check that
1311 * a key has been set. If not, the key slot will
1312 * still be in its default state of 0, which is
1313 * guaranteed to be invalid, hence the PSA-call
1314 * below will gracefully fail. */
1315 mbedtls_cipher_context_psa * const cipher_psa =
1316 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1317
1318 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001319 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001320 size_t part_len;
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (ctx->operation == MBEDTLS_DECRYPT) {
1323 status = psa_cipher_decrypt_setup(&cipher_op,
1324 cipher_psa->slot,
1325 cipher_psa->alg);
1326 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1327 status = psa_cipher_encrypt_setup(&cipher_op,
1328 cipher_psa->slot,
1329 cipher_psa->alg);
1330 } else {
1331 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001332 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001333
1334 /* In the following, we can immediately return on an error,
1335 * because the PSA Crypto API guarantees that cipher operations
1336 * are terminated by unsuccessful calls to psa_cipher_update(),
1337 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (status != PSA_SUCCESS) {
1339 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001340 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001341
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001342 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1344 if (status != PSA_SUCCESS) {
1345 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1346 }
1347 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 status = psa_cipher_update(&cipher_op,
1350 input, ilen,
1351 output, ilen, olen);
1352 if (status != PSA_SUCCESS) {
1353 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1354 }
1355
1356 status = psa_cipher_finish(&cipher_op,
1357 output + *olen, ilen - *olen,
1358 &part_len);
1359 if (status != PSA_SUCCESS) {
1360 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1361 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001362
1363 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001365 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001366#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1369 return ret;
1370 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1373 return ret;
1374 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1377 output, olen)) != 0) {
1378 return ret;
1379 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1382 &finish_olen)) != 0) {
1383 return ret;
1384 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001385
1386 *olen += finish_olen;
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001389}
1390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001392/*
TRodziewicz18efb732021-04-29 23:12:19 +02001393 * Packet-oriented encryption for AEAD modes: internal function used by
1394 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1397 const unsigned char *iv, size_t iv_len,
1398 const unsigned char *ad, size_t ad_len,
1399 const unsigned char *input, size_t ilen,
1400 unsigned char *output, size_t *olen,
1401 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001402{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001403#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001405 /* As in the non-PSA case, we don't check that
1406 * a key has been set. If not, the key slot will
1407 * still be in its default state of 0, which is
1408 * guaranteed to be invalid, hence the PSA-call
1409 * below will gracefully fail. */
1410 mbedtls_cipher_context_psa * const cipher_psa =
1411 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1412
1413 psa_status_t status;
1414
1415 /* PSA Crypto API always writes the authentication tag
1416 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (output == NULL || tag != output + ilen) {
1418 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1419 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 status = psa_aead_encrypt(cipher_psa->slot,
1422 cipher_psa->alg,
1423 iv, iv_len,
1424 ad, ad_len,
1425 input, ilen,
1426 output, ilen + tag_len, olen);
1427 if (status != PSA_SUCCESS) {
1428 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1429 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001430
1431 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001433 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001434#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001437 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001438 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1440 ilen, iv, iv_len, ad, ad_len,
1441 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001442 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_GCM_C */
1444#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001445 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001446 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1448 iv, iv_len, ad, ad_len, input, output,
1449 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001452#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001453 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001454 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001455 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 (tag_len != 16U)) {
1457 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001458 }
1459
1460 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1462 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001463 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001464#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001467}
1468
1469/*
TRodziewicz18efb732021-04-29 23:12:19 +02001470 * Packet-oriented encryption for AEAD modes: internal function used by
1471 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1474 const unsigned char *iv, size_t iv_len,
1475 const unsigned char *ad, size_t ad_len,
1476 const unsigned char *input, size_t ilen,
1477 unsigned char *output, size_t *olen,
1478 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001479{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001480#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001482 /* As in the non-PSA case, we don't check that
1483 * a key has been set. If not, the key slot will
1484 * still be in its default state of 0, which is
1485 * guaranteed to be invalid, hence the PSA-call
1486 * below will gracefully fail. */
1487 mbedtls_cipher_context_psa * const cipher_psa =
1488 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1489
1490 psa_status_t status;
1491
1492 /* PSA Crypto API always writes the authentication tag
1493 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (input == NULL || tag != input + ilen) {
1495 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1496 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 status = psa_aead_decrypt(cipher_psa->slot,
1499 cipher_psa->alg,
1500 iv, iv_len,
1501 ad, ad_len,
1502 input, ilen + tag_len,
1503 output, ilen, olen);
1504 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1505 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1506 } else if (status != PSA_SUCCESS) {
1507 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1508 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001511 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001512#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001515 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001517
1518 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1520 iv, iv_len, ad, ad_len,
1521 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_GCM_C */
1530#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001531 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001533
1534 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1536 iv, iv_len, ad, ad_len,
1537 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001544 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001546#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001547 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001549
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001550 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001551 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 (tag_len != 16U)) {
1553 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001554 }
1555
1556 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1558 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001561 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 }
Daniel King8fe47012016-05-17 20:33:28 -03001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001565 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001566#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001571
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1573/*
1574 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1577 const unsigned char *iv, size_t iv_len,
1578 const unsigned char *ad, size_t ad_len,
1579 const unsigned char *input, size_t ilen,
1580 unsigned char *output, size_t output_len,
1581 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001582{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001583#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001585#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001586 ctx->psa_enabled == 0 &&
1587#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001588 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1589 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1590 mbedtls_nist_kw_mode_t mode =
1591 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1592 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001593
1594 /* There is no iv, tag or ad associated with KW and KWP,
1595 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1597 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1598 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001599
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001600 (void) iv;
1601 (void) ad;
1602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1604 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001605 }
1606#endif /* MBEDTLS_NIST_KW_C */
1607
1608#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1609 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (output_len < ilen + tag_len) {
1611 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1612 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1615 input, ilen, output, olen,
1616 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001617 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001619#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001621#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1622}
1623
1624/*
1625 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001627int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1628 const unsigned char *iv, size_t iv_len,
1629 const unsigned char *ad, size_t ad_len,
1630 const unsigned char *input, size_t ilen,
1631 unsigned char *output, size_t output_len,
1632 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001633{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001634#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001636#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001637 ctx->psa_enabled == 0 &&
1638#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001639 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1640 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1641 mbedtls_nist_kw_mode_t mode =
1642 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1643 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001644
1645 /* There is no iv, tag or ad associated with KW and KWP,
1646 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1648 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1649 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001650
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001651 (void) iv;
1652 (void) ad;
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1655 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001656 }
1657#endif /* MBEDTLS_NIST_KW_C */
1658
1659#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1660 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (ilen < tag_len || output_len < ilen - tag_len) {
1662 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1663 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1666 input, ilen - tag_len, output, olen,
1667 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001668#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001670#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1671}
1672#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#endif /* MBEDTLS_CIPHER_C */