blob: fd04a7de13319947e613b4ee19a159cb3c56d481 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +02004 * \brief Generic cipher wrapper for Mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020060#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000061
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200147#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
148static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
149 mbedtls_cipher_type_t cipher)
150{
151 switch (cipher) {
152 case MBEDTLS_CIPHER_AES_128_CCM:
153 case MBEDTLS_CIPHER_AES_192_CCM:
154 case MBEDTLS_CIPHER_AES_256_CCM:
155 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
158 case MBEDTLS_CIPHER_AES_128_GCM:
159 case MBEDTLS_CIPHER_AES_192_GCM:
160 case MBEDTLS_CIPHER_AES_256_GCM:
161 case MBEDTLS_CIPHER_AES_128_CBC:
162 case MBEDTLS_CIPHER_AES_192_CBC:
163 case MBEDTLS_CIPHER_AES_256_CBC:
164 case MBEDTLS_CIPHER_AES_128_ECB:
165 case MBEDTLS_CIPHER_AES_192_ECB:
166 case MBEDTLS_CIPHER_AES_256_ECB:
167 return PSA_KEY_TYPE_AES;
168
169 /* ARIA not yet supported in PSA. */
170 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
171 case MBEDTLS_CIPHER_ARIA_192_CCM:
172 case MBEDTLS_CIPHER_ARIA_256_CCM:
173 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
176 case MBEDTLS_CIPHER_ARIA_128_GCM:
177 case MBEDTLS_CIPHER_ARIA_192_GCM:
178 case MBEDTLS_CIPHER_ARIA_256_GCM:
179 case MBEDTLS_CIPHER_ARIA_128_CBC:
180 case MBEDTLS_CIPHER_ARIA_192_CBC:
181 case MBEDTLS_CIPHER_ARIA_256_CBC:
182 return( PSA_KEY_TYPE_ARIA ); */
183
184 default:
185 return 0;
186 }
187}
188
189static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
190 mbedtls_cipher_mode_t mode, size_t taglen)
191{
192 switch (mode) {
193 case MBEDTLS_MODE_ECB:
194 return PSA_ALG_ECB_NO_PADDING;
195 case MBEDTLS_MODE_GCM:
196 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
197 case MBEDTLS_MODE_CCM:
198 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
199 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
200 return PSA_ALG_CCM_STAR_NO_TAG;
201 case MBEDTLS_MODE_CBC:
202 if (taglen == 0) {
203 return PSA_ALG_CBC_NO_PADDING;
204 } else {
205 return 0;
206 }
207 default:
208 return 0;
209 }
210}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200221 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (ctx->psa_enabled == 1) {
226 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000227 mbedtls_cipher_context_psa * const cipher_psa =
228 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000233 }
234
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100235 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100245 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100247 }
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100251 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
258 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (cipher_info == NULL) {
261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Valerio Setti79a02de2023-10-12 18:48:55 +0200266 if ((mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) &&
267 (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func()) == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
269 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
271 ctx->cipher_info = cipher_info;
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274}
275
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200276#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100277int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
278 const mbedtls_cipher_info_t *cipher_info,
279 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000280{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000281 psa_algorithm_t alg;
282 mbedtls_cipher_context_psa *cipher_psa;
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (NULL == cipher_info || NULL == ctx) {
285 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
286 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000287
Hanno Becker4ee7e762018-11-17 22:00:38 +0000288 /* Check that the underlying cipher mode and cipher type are
289 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100290 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (alg == 0) {
292 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
293 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100294 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
296 }
Hanno Becker6118e432018-11-09 16:47:20 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
301 if (cipher_psa == NULL) {
302 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
303 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000304 cipher_psa->alg = alg;
305 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000306 ctx->cipher_info = cipher_info;
307 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000309}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200310#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
313 const unsigned char *key,
314 int key_bitlen,
315 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316{
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100318 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 }
320 if (ctx->cipher_info == NULL) {
321 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
322 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200324#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000326 mbedtls_cipher_context_psa * const cipher_psa =
327 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000330
331 psa_status_t status;
332 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000334
335 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (key_bitlen % 8 != 0) {
337 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
338 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000339
340 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
342 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
343 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000344
Andrzej Kurekc7509322019-01-08 09:36:01 -0500345 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100346 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (key_type == 0) {
348 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
349 }
350 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000351
352 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500353 * (encrypt vs. decrypt): it is possible to setup a key for encryption
354 * and use it for AEAD decryption. Until tests relying on this
355 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
358 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 status = psa_import_key(&attributes, key, key_bytelen,
361 &cipher_psa->slot);
362 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200363 case PSA_SUCCESS:
364 break;
365 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200367 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200369 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200371 }
372 /* Indicate that we own the key slot and need to
373 * destroy it in mbedtls_cipher_free(). */
374 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000375
376 ctx->key_bitlen = key_bitlen;
377 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000379 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200380#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100383 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200385 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200386
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200387 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388 ctx->operation = operation;
389
Paul Bakker343a8702011-06-09 14:27:58 +0000390 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100391 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100394 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
395 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
396 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100397 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100398 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000399 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100402 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100403 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407}
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
410 const unsigned char *iv,
411 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200413 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (ctx->cipher_info == NULL) {
416 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
417 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200418#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000420 /* While PSA Crypto has an API for multipart
421 * operations, we currently don't make it
422 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000424 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200425#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000426
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200427 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
429 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
430 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200433 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100435 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200436
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200437 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (actual_iv_size > iv_len) {
439 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
440 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200441 }
442
Daniel Kingbd920622016-05-15 19:56:20 -0300443#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100444 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100445 /* Even though the actual_iv_size is overwritten with a correct value
446 * of 12 from the cipher info, return an error to indicate that
447 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (iv_len != 12) {
449 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
450 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
453 iv,
454 0U)) { /* Initial counter value */
455 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300456 }
457 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100458#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100459 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 iv_len != 12) {
461 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
462 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100463#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300464#endif
465
Gilles Peskine295fc132021-04-15 18:32:23 +0200466#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100467 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
469 ctx->operation,
470 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200471 }
472#endif
473
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200474#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100475 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200476 int set_lengths_result;
477 int ccm_star_mode;
478
479 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 (mbedtls_ccm_context *) ctx->cipher_ctx,
481 0, 0, 0);
482 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200483 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200487 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200489 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200491 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
495 ccm_star_mode,
496 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200497 }
498#endif
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (actual_iv_size != 0) {
501 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300502 ctx->iv_size = actual_iv_size;
503 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200506}
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200509{
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (ctx->cipher_info == NULL) {
511 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
512 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200513
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200514#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000516 /* We don't support resetting PSA-based
517 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000519 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200520#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000521
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522 ctx->unprocessed_len = 0;
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200525}
526
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200527#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100528int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
529 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200530{
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ctx->cipher_info == NULL) {
532 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
533 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200534
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200535#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000537 /* While PSA Crypto has an API for multipart
538 * operations, we currently don't make it
539 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000541 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200542#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000543
Daniel King8fe47012016-05-17 20:33:28 -0300544#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100545 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
547 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200548 }
Daniel King8fe47012016-05-17 20:33:28 -0300549#endif
550
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200551#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100552 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300553 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200554 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200557 ? MBEDTLS_CHACHAPOLY_ENCRYPT
558 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
561 ctx->iv,
562 mode);
563 if (result != 0) {
564 return result;
565 }
Daniel King8fe47012016-05-17 20:33:28 -0300566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
568 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300569 }
570#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200574#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
577 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578{
Janos Follath24eed8d2019-11-22 13:21:35 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500580 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (ctx->cipher_info == NULL) {
583 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
584 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200586#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000588 /* While PSA Crypto has an API for multipart
589 * operations, we currently don't make it
590 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000592 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200593#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000594
Paul Bakker6c212762013-12-16 15:24:50 +0100595 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 block_size = mbedtls_cipher_get_block_size(ctx);
597 if (0 == block_size) {
598 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100599 }
Paul Bakker6c212762013-12-16 15:24:50 +0100600
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100601 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if (ilen != block_size) {
603 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
604 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200605
606 *olen = ilen;
607
Dave Rodgmande3de772023-06-24 12:51:06 +0100608 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100609 ctx->operation, input,
610 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200612 }
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615 }
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100618 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
620 input, ilen,
621 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200622 }
623#endif
624
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200625#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100626 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
628 input, ilen,
629 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200630 }
631#endif
632
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200633#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100634 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200635 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
637 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200638 }
639#endif
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (input == output &&
642 (ctx->unprocessed_len != 0 || ilen % block_size)) {
643 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100644 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100647 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200648 size_t copy_len = 0;
649
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650 /*
651 * If there is not enough data for a full block, cache it.
652 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
654 ilen <= block_size - ctx->unprocessed_len) ||
655 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
656 ilen < block_size - ctx->unprocessed_len) ||
657 (ctx->operation == MBEDTLS_ENCRYPT &&
658 ilen < block_size - ctx->unprocessed_len)) {
659 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
660 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661
662 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 }
665
666 /*
667 * Process cached data first
668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100670 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
673 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674
Dave Rodgmande3de772023-06-24 12:51:06 +0100675 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100676 ctx->operation,
677 block_size, ctx->iv,
678 ctx->
679 unprocessed_data,
680 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682 }
683
Janos Follath98e28a72016-05-31 14:03:54 +0100684 *olen += block_size;
685 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686 ctx->unprocessed_len = 0;
687
688 input += copy_len;
689 ilen -= copy_len;
690 }
691
692 /*
693 * Cache final, incomplete block
694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700696 /* Encryption: only cache partial blocks
697 * Decryption w/ padding: always keep at least one whole block
698 * Decryption w/o padding: only cache partial blocks
699 */
Janos Follath98e28a72016-05-31 14:03:54 +0100700 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700702 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100704 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700705 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
708 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709
710 ctx->unprocessed_len += copy_len;
711 ilen -= copy_len;
712 }
713
714 /*
715 * Process remaining full blocks
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100718 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100719 ctx->operation,
720 ilen, ctx->iv,
721 input,
722 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200725
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 *olen += ilen;
727 }
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100734 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100735 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100736 ctx->operation, ilen,
737 &ctx->unprocessed_len,
738 ctx->iv,
739 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000741 }
742
743 *olen = ilen;
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000746 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000748
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100749#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100750 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100751 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100752 ilen,
753 &ctx->unprocessed_len,
754 ctx->iv,
755 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100757 }
758
759 *olen = ilen;
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100762 }
763#endif /* MBEDTLS_CIPHER_MODE_OFB */
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100766 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100767 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100768 ilen,
769 &ctx->unprocessed_len,
770 ctx->iv,
771 ctx->unprocessed_data,
772 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000774 }
775
776 *olen = ilen;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000779 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000781
Jaeden Ameroc6539902018-04-30 17:17:41 +0100782#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100783 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100785 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100787 }
788
Dave Rodgmande3de772023-06-24 12:51:06 +0100789 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100790 ctx->operation,
791 ilen,
792 ctx->iv,
793 input,
794 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (ret != 0) {
796 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100797 }
798
799 *olen = ilen;
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100802 }
803#endif /* MBEDTLS_CIPHER_MODE_XTS */
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100806 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100807 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100808 ilen, input,
809 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200811 }
812
813 *olen = ilen;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000820}
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
823#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200824/*
825 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
826 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827static void add_pkcs_padding(unsigned char *output, size_t output_len,
828 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000829{
Paul Bakker23986e52011-04-24 08:57:21 +0000830 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100831 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000834 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000836}
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static int get_pkcs_padding(unsigned char *input, size_t input_len,
839 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000840{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100841 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100842 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (NULL == input || NULL == data_len) {
845 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
846 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847
848 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000849 *data_len = input_len - padding_len;
850
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100851 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100852 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100853
854 /* The number of bytes checked must be independent of padding_len,
855 * so pick input_len, which is usually 8 or 16 (one block) */
856 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100858 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
859 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
860 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100862
Dave Rodgmand03f4832023-09-22 09:52:15 +0100863 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200868/*
869 * One and zeros padding: fill with 80 00 ... 00
870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static void add_one_and_zeros_padding(unsigned char *output,
872 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200873{
874 size_t padding_len = output_len - data_len;
875 unsigned char i = 0;
876
877 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200879 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200881}
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
884 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200885{
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (NULL == input || NULL == data_len) {
887 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
888 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200889
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100890 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
891 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
892
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100893 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100894
Dave Rodgman437500c2023-09-19 21:36:43 +0100895 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100896 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
897
898 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
899
900 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
901
Dave Rodgmanfd965792023-09-19 21:51:50 +0100902 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100903
904 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100905 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200906
Dave Rodgmand03f4832023-09-22 09:52:15 +0100907 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200912/*
913 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static void add_zeros_and_len_padding(unsigned char *output,
916 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200917{
918 size_t padding_len = output_len - data_len;
919 unsigned char i = 0;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200922 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200924 output[output_len - 1] = (unsigned char) padding_len;
925}
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
928 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200929{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100930 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100931 unsigned char padding_len;
932 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (NULL == input || NULL == data_len) {
935 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
936 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200937
938 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200939 *data_len = input_len - padding_len;
940
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100941 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100942 bad = mbedtls_ct_uint_gt(padding_len, input_len);
943 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100944
945 /* The number of bytes checked must be independent of padding_len */
946 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100948 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100949 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100950 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100951 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100953
Dave Rodgmand03f4832023-09-22 09:52:15 +0100954 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200959/*
960 * Zero padding: fill with 00 ... 00
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static void add_zeros_padding(unsigned char *output,
963 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200964{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100965 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200966}
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static int get_zeros_padding(unsigned char *input, size_t input_len,
969 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200970{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100971 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100972 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (NULL == input || NULL == data_len) {
975 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100976 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 *data_len = 0;
979 for (i = input_len; i > 0; i--) {
980 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100981 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
982 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
984
985 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200988
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200989/*
990 * No padding: don't pad :)
991 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200993 * but a trivial get_padding function
994 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995static int get_no_padding(unsigned char *input, size_t input_len,
996 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (NULL == input || NULL == data_len) {
999 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1000 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001001
1002 *data_len = input_len;
1003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1009 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001010{
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (ctx->cipher_info == NULL) {
1012 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1013 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001015#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001017 /* While PSA Crypto has an API for multipart
1018 * operations, we currently don't make it
1019 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001021 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001022#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001023
Paul Bakker8123e9d2011-01-06 15:37:30 +00001024 *olen = 0;
1025
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001026#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1027 /* CBC mode requires padding so we make sure a call to
1028 * mbedtls_cipher_set_padding_mode has been done successfully. */
1029 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1030 if (ctx->get_padding == NULL) {
1031 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1032 }
1033 }
1034#endif
1035
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001036 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1037 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1038 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1039 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1042 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001044 }
1045
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001046 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1047 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001049 }
1050
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001051 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ctx->unprocessed_len != 0) {
1053 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1054 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001057 }
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001060 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001061 int ret = 0;
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001064 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (NULL == ctx->add_padding) {
1066 if (0 != ctx->unprocessed_len) {
1067 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1068 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001071 }
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1074 ctx->unprocessed_len);
1075 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001076 /*
1077 * For decrypt operations, expect a full block,
1078 * or an empty block if no padding
1079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1081 return 0;
1082 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001085 }
1086
1087 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001088 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001089 ctx->operation,
1090 mbedtls_cipher_get_block_size(
1091 ctx),
1092 ctx->iv,
1093 ctx->unprocessed_data,
1094 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001096 }
1097
1098 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (MBEDTLS_DECRYPT == ctx->operation) {
1100 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1101 olen);
1102 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001103
1104 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 *olen = mbedtls_cipher_get_block_size(ctx);
1106 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001107 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001108#else
1109 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001113}
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1117 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001118{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001119 if (NULL == ctx->cipher_info ||
1120 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001122 }
1123
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001124#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001126 /* While PSA Crypto knows about CBC padding
1127 * schemes, we currently don't make them
1128 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (mode != MBEDTLS_PADDING_NONE) {
1130 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1131 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001134 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001135#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 case MBEDTLS_PADDING_PKCS7:
1140 ctx->add_padding = add_pkcs_padding;
1141 ctx->get_padding = get_pkcs_padding;
1142 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1146 ctx->add_padding = add_one_and_zeros_padding;
1147 ctx->get_padding = get_one_and_zeros_padding;
1148 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1152 ctx->add_padding = add_zeros_and_len_padding;
1153 ctx->get_padding = get_zeros_and_len_padding;
1154 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 case MBEDTLS_PADDING_ZEROS:
1158 ctx->add_padding = add_zeros_padding;
1159 ctx->get_padding = get_zeros_padding;
1160 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001161#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 case MBEDTLS_PADDING_NONE:
1163 ctx->add_padding = NULL;
1164 ctx->get_padding = get_no_padding;
1165 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 default:
1168 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001169 }
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001174
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001175#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001176int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1177 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001178{
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (ctx->cipher_info == NULL) {
1180 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1181 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (MBEDTLS_ENCRYPT != ctx->operation) {
1184 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1185 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001186
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001187#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001189 /* While PSA Crypto has an API for multipart
1190 * operations, we currently don't make it
1191 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001193 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001194#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001195
Daniel King8fe47012016-05-17 20:33:28 -03001196#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001197 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001198 size_t output_length;
1199 /* The code here doesn't yet support alternative implementations
1200 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1202 NULL, 0, &output_length,
1203 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001204 }
Daniel King8fe47012016-05-17 20:33:28 -03001205#endif
1206
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001207#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001208 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001209 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (tag_len != 16U) {
1211 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1212 }
Daniel King8fe47012016-05-17 20:33:28 -03001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return mbedtls_chachapoly_finish(
1215 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001216 }
1217#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001220}
Paul Bakker9af723c2014-05-01 13:03:14 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1223 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001224{
Daniel King8fe47012016-05-17 20:33:28 -03001225 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (ctx->cipher_info == NULL) {
1229 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1230 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (MBEDTLS_DECRYPT != ctx->operation) {
1233 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001234 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001235
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001236#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001238 /* While PSA Crypto has an API for multipart
1239 * operations, we currently don't make it
1240 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001242 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001243#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001244
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001245 /* Status to return on a non-authenticated algorithm. */
1246 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001247
Daniel King8fe47012016-05-17 20:33:28 -03001248#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001249 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001250 size_t output_length;
1251 /* The code here doesn't yet support alternative implementations
1252 * that can delay up to a block of output. */
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (tag_len > sizeof(check_tag)) {
1255 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1256 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (0 != (ret = mbedtls_gcm_finish(
1259 (mbedtls_gcm_context *) ctx->cipher_ctx,
1260 NULL, 0, &output_length,
1261 check_tag, tag_len))) {
1262 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001263 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001264
1265 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001267 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001268 goto exit;
1269 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001270 }
Daniel King8fe47012016-05-17 20:33:28 -03001271#endif /* MBEDTLS_GCM_C */
1272
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001273#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001274 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001275 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if (tag_len != sizeof(check_tag)) {
1277 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1278 }
Daniel King8fe47012016-05-17 20:33:28 -03001279
Hanno Becker18597cd2018-11-09 16:36:33 +00001280 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1282 if (ret != 0) {
1283 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001284 }
1285
1286 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001288 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001289 goto exit;
1290 }
Daniel King8fe47012016-05-17 20:33:28 -03001291 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001292#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001293
Gilles Peskinecd742982021-12-13 16:57:47 +01001294exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 mbedtls_platform_zeroize(check_tag, tag_len);
1296 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001297}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001298#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001299
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001300/*
1301 * Packet-oriented wrapper for non-AEAD modes
1302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1304 const unsigned char *iv, size_t iv_len,
1305 const unsigned char *input, size_t ilen,
1306 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001307{
Janos Follath24eed8d2019-11-22 13:21:35 +00001308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001309 size_t finish_olen;
1310
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001311#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001313 /* As in the non-PSA case, we don't check that
1314 * a key has been set. If not, the key slot will
1315 * still be in its default state of 0, which is
1316 * guaranteed to be invalid, hence the PSA-call
1317 * below will gracefully fail. */
1318 mbedtls_cipher_context_psa * const cipher_psa =
1319 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1320
1321 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001322 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001323 size_t part_len;
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (ctx->operation == MBEDTLS_DECRYPT) {
1326 status = psa_cipher_decrypt_setup(&cipher_op,
1327 cipher_psa->slot,
1328 cipher_psa->alg);
1329 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1330 status = psa_cipher_encrypt_setup(&cipher_op,
1331 cipher_psa->slot,
1332 cipher_psa->alg);
1333 } else {
1334 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001335 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001336
1337 /* In the following, we can immediately return on an error,
1338 * because the PSA Crypto API guarantees that cipher operations
1339 * are terminated by unsuccessful calls to psa_cipher_update(),
1340 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (status != PSA_SUCCESS) {
1342 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001343 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001344
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001345 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1347 if (status != PSA_SUCCESS) {
1348 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1349 }
1350 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 status = psa_cipher_update(&cipher_op,
1353 input, ilen,
1354 output, ilen, olen);
1355 if (status != PSA_SUCCESS) {
1356 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1357 }
1358
1359 status = psa_cipher_finish(&cipher_op,
1360 output + *olen, ilen - *olen,
1361 &part_len);
1362 if (status != PSA_SUCCESS) {
1363 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1364 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001365
1366 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001368 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001369#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1372 return ret;
1373 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1376 return ret;
1377 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1380 output, olen)) != 0) {
1381 return ret;
1382 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1385 &finish_olen)) != 0) {
1386 return ret;
1387 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001388
1389 *olen += finish_olen;
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001392}
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001395/*
TRodziewicz18efb732021-04-29 23:12:19 +02001396 * Packet-oriented encryption for AEAD modes: internal function used by
1397 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1400 const unsigned char *iv, size_t iv_len,
1401 const unsigned char *ad, size_t ad_len,
1402 const unsigned char *input, size_t ilen,
1403 unsigned char *output, size_t *olen,
1404 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001405{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001406#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001408 /* As in the non-PSA case, we don't check that
1409 * a key has been set. If not, the key slot will
1410 * still be in its default state of 0, which is
1411 * guaranteed to be invalid, hence the PSA-call
1412 * below will gracefully fail. */
1413 mbedtls_cipher_context_psa * const cipher_psa =
1414 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1415
1416 psa_status_t status;
1417
1418 /* PSA Crypto API always writes the authentication tag
1419 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (output == NULL || tag != output + ilen) {
1421 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1422 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 status = psa_aead_encrypt(cipher_psa->slot,
1425 cipher_psa->alg,
1426 iv, iv_len,
1427 ad, ad_len,
1428 input, ilen,
1429 output, ilen + tag_len, olen);
1430 if (status != PSA_SUCCESS) {
1431 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1432 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001433
1434 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001436 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001437#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001440 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001441 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1443 ilen, iv, iv_len, ad, ad_len,
1444 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_GCM_C */
1447#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001448 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001449 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1451 iv, iv_len, ad, ad_len, input, output,
1452 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001455#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001456 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001457 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001458 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 (tag_len != 16U)) {
1460 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001461 }
1462
1463 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1465 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001466 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001467#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001470}
1471
1472/*
TRodziewicz18efb732021-04-29 23:12:19 +02001473 * Packet-oriented encryption for AEAD modes: internal function used by
1474 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1477 const unsigned char *iv, size_t iv_len,
1478 const unsigned char *ad, size_t ad_len,
1479 const unsigned char *input, size_t ilen,
1480 unsigned char *output, size_t *olen,
1481 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001482{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001483#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001485 /* As in the non-PSA case, we don't check that
1486 * a key has been set. If not, the key slot will
1487 * still be in its default state of 0, which is
1488 * guaranteed to be invalid, hence the PSA-call
1489 * below will gracefully fail. */
1490 mbedtls_cipher_context_psa * const cipher_psa =
1491 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1492
1493 psa_status_t status;
1494
1495 /* PSA Crypto API always writes the authentication tag
1496 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (input == NULL || tag != input + ilen) {
1498 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1499 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 status = psa_aead_decrypt(cipher_psa->slot,
1502 cipher_psa->alg,
1503 iv, iv_len,
1504 ad, ad_len,
1505 input, ilen + tag_len,
1506 output, ilen, olen);
1507 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1508 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1509 } else if (status != PSA_SUCCESS) {
1510 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1511 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001514 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001515#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001518 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001520
1521 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1523 iv, iv_len, ad, ad_len,
1524 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_GCM_C */
1533#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001534 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001536
1537 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1539 iv, iv_len, ad, ad_len,
1540 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001549#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001550 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001552
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001553 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001554 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 (tag_len != 16U)) {
1556 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001557 }
1558
1559 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1561 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001564 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 }
Daniel King8fe47012016-05-17 20:33:28 -03001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001568 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001569#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001574
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001575#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1576/*
1577 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1580 const unsigned char *iv, size_t iv_len,
1581 const unsigned char *ad, size_t ad_len,
1582 const unsigned char *input, size_t ilen,
1583 unsigned char *output, size_t output_len,
1584 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001585{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001586#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001588#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001589 ctx->psa_enabled == 0 &&
1590#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001591 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1592 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1593 mbedtls_nist_kw_mode_t mode =
1594 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1595 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001596
1597 /* There is no iv, tag or ad associated with KW and KWP,
1598 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1600 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1601 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001603 (void) iv;
1604 (void) ad;
1605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1607 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001608 }
1609#endif /* MBEDTLS_NIST_KW_C */
1610
1611#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1612 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if (output_len < ilen + tag_len) {
1614 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1615 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1618 input, ilen, output, olen,
1619 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001620 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001622#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001624#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1625}
1626
1627/*
1628 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1631 const unsigned char *iv, size_t iv_len,
1632 const unsigned char *ad, size_t ad_len,
1633 const unsigned char *input, size_t ilen,
1634 unsigned char *output, size_t output_len,
1635 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001636{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001637#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001639#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001640 ctx->psa_enabled == 0 &&
1641#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001642 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1643 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1644 mbedtls_nist_kw_mode_t mode =
1645 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1646 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001647
1648 /* There is no iv, tag or ad associated with KW and KWP,
1649 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1651 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1652 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001653
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001654 (void) iv;
1655 (void) ad;
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1658 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001659 }
1660#endif /* MBEDTLS_NIST_KW_C */
1661
1662#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1663 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (ilen < tag_len || output_len < ilen - tag_len) {
1665 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1666 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1669 input, ilen - tag_len, output, olen,
1670 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001671#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001673#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1674}
1675#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_CIPHER_C */