blob: 469f1b0cc5c507704114beea648bc04b2dfc86ff [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020060#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000061
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200147#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
148static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
149 mbedtls_cipher_type_t cipher)
150{
151 switch (cipher) {
152 case MBEDTLS_CIPHER_AES_128_CCM:
153 case MBEDTLS_CIPHER_AES_192_CCM:
154 case MBEDTLS_CIPHER_AES_256_CCM:
155 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
158 case MBEDTLS_CIPHER_AES_128_GCM:
159 case MBEDTLS_CIPHER_AES_192_GCM:
160 case MBEDTLS_CIPHER_AES_256_GCM:
161 case MBEDTLS_CIPHER_AES_128_CBC:
162 case MBEDTLS_CIPHER_AES_192_CBC:
163 case MBEDTLS_CIPHER_AES_256_CBC:
164 case MBEDTLS_CIPHER_AES_128_ECB:
165 case MBEDTLS_CIPHER_AES_192_ECB:
166 case MBEDTLS_CIPHER_AES_256_ECB:
167 return PSA_KEY_TYPE_AES;
168
169 /* ARIA not yet supported in PSA. */
170 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
171 case MBEDTLS_CIPHER_ARIA_192_CCM:
172 case MBEDTLS_CIPHER_ARIA_256_CCM:
173 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
176 case MBEDTLS_CIPHER_ARIA_128_GCM:
177 case MBEDTLS_CIPHER_ARIA_192_GCM:
178 case MBEDTLS_CIPHER_ARIA_256_GCM:
179 case MBEDTLS_CIPHER_ARIA_128_CBC:
180 case MBEDTLS_CIPHER_ARIA_192_CBC:
181 case MBEDTLS_CIPHER_ARIA_256_CBC:
182 return( PSA_KEY_TYPE_ARIA ); */
183
184 default:
185 return 0;
186 }
187}
188
189static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
190 mbedtls_cipher_mode_t mode, size_t taglen)
191{
192 switch (mode) {
193 case MBEDTLS_MODE_ECB:
194 return PSA_ALG_ECB_NO_PADDING;
195 case MBEDTLS_MODE_GCM:
196 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
197 case MBEDTLS_MODE_CCM:
198 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
199 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
200 return PSA_ALG_CCM_STAR_NO_TAG;
201 case MBEDTLS_MODE_CBC:
202 if (taglen == 0) {
203 return PSA_ALG_CBC_NO_PADDING;
204 } else {
205 return 0;
206 }
207 default:
208 return 0;
209 }
210}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200221 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (ctx->psa_enabled == 1) {
226 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000227 mbedtls_cipher_context_psa * const cipher_psa =
228 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000233 }
234
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100235 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100245 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100247 }
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100251 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
258 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (cipher_info == NULL) {
261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Dave Rodgmande3de772023-06-24 12:51:06 +0100266 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
268 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
270 ctx->cipher_info = cipher_info;
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200273 /*
274 * Ignore possible errors caused by a cipher mode that doesn't use padding
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200278#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284}
285
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200286#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
288 const mbedtls_cipher_info_t *cipher_info,
289 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000290{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000291 psa_algorithm_t alg;
292 mbedtls_cipher_context_psa *cipher_psa;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (NULL == cipher_info || NULL == ctx) {
295 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
296 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000297
Hanno Becker4ee7e762018-11-17 22:00:38 +0000298 /* Check that the underlying cipher mode and cipher type are
299 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100300 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (alg == 0) {
302 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
303 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100304 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
306 }
Hanno Becker6118e432018-11-09 16:47:20 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
311 if (cipher_psa == NULL) {
312 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
313 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000314 cipher_psa->alg = alg;
315 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000316 ctx->cipher_info = cipher_info;
317 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000319}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200320#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
323 const unsigned char *key,
324 int key_bitlen,
325 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326{
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100328 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
330 if (ctx->cipher_info == NULL) {
331 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
332 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200334#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000336 mbedtls_cipher_context_psa * const cipher_psa =
337 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000340
341 psa_status_t status;
342 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000344
345 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (key_bitlen % 8 != 0) {
347 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
348 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000349
350 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
352 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
353 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000354
Andrzej Kurekc7509322019-01-08 09:36:01 -0500355 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100356 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (key_type == 0) {
358 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
359 }
360 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000361
362 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500363 * (encrypt vs. decrypt): it is possible to setup a key for encryption
364 * and use it for AEAD decryption. Until tests relying on this
365 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
368 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 status = psa_import_key(&attributes, key, key_bytelen,
371 &cipher_psa->slot);
372 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200373 case PSA_SUCCESS:
374 break;
375 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200377 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200379 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200381 }
382 /* Indicate that we own the key slot and need to
383 * destroy it in mbedtls_cipher_free(). */
384 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000385
386 ctx->key_bitlen = key_bitlen;
387 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000389 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200390#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100393 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200395 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200396
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200397 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398 ctx->operation = operation;
399
Paul Bakker343a8702011-06-09 14:27:58 +0000400 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100401 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000402 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100404 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
405 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
406 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100407 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100408 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000409 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100412 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100413 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417}
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
420 const unsigned char *iv,
421 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200423 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (ctx->cipher_info == NULL) {
426 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
427 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200428#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000430 /* While PSA Crypto has an API for multipart
431 * operations, we currently don't make it
432 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000434 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200435#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000436
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200437 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
439 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
440 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200443 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100445 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200446
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200447 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (actual_iv_size > iv_len) {
449 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
450 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200451 }
452
Daniel Kingbd920622016-05-15 19:56:20 -0300453#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100454 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100455 /* Even though the actual_iv_size is overwritten with a correct value
456 * of 12 from the cipher info, return an error to indicate that
457 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (iv_len != 12) {
459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
460 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
463 iv,
464 0U)) { /* Initial counter value */
465 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300466 }
467 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100468#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100469 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 iv_len != 12) {
471 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
472 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100473#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300474#endif
475
Gilles Peskine295fc132021-04-15 18:32:23 +0200476#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100477 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
479 ctx->operation,
480 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200481 }
482#endif
483
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200484#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100485 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200486 int set_lengths_result;
487 int ccm_star_mode;
488
489 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 (mbedtls_ccm_context *) ctx->cipher_ctx,
491 0, 0, 0);
492 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200493 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200497 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200499 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200501 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
505 ccm_star_mode,
506 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200507 }
508#endif
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (actual_iv_size != 0) {
511 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300512 ctx->iv_size = actual_iv_size;
513 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200516}
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200519{
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (ctx->cipher_info == NULL) {
521 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
522 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200523
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200524#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000526 /* We don't support resetting PSA-based
527 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000529 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200530#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000531
Paul Bakker8123e9d2011-01-06 15:37:30 +0000532 ctx->unprocessed_len = 0;
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200535}
536
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200537#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100538int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
539 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200540{
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (ctx->cipher_info == NULL) {
542 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
543 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200544
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200545#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000547 /* While PSA Crypto has an API for multipart
548 * operations, we currently don't make it
549 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000551 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200552#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000553
Daniel King8fe47012016-05-17 20:33:28 -0300554#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100555 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
557 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200558 }
Daniel King8fe47012016-05-17 20:33:28 -0300559#endif
560
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200561#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100562 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300563 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200564 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200567 ? MBEDTLS_CHACHAPOLY_ENCRYPT
568 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
571 ctx->iv,
572 mode);
573 if (result != 0) {
574 return result;
575 }
Daniel King8fe47012016-05-17 20:33:28 -0300576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
578 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300579 }
580#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200584#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
587 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588{
Janos Follath24eed8d2019-11-22 13:21:35 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500590 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (ctx->cipher_info == NULL) {
593 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
594 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200596#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000598 /* While PSA Crypto has an API for multipart
599 * operations, we currently don't make it
600 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000602 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200603#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000604
Paul Bakker6c212762013-12-16 15:24:50 +0100605 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 block_size = mbedtls_cipher_get_block_size(ctx);
607 if (0 == block_size) {
608 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100609 }
Paul Bakker6c212762013-12-16 15:24:50 +0100610
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100611 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (ilen != block_size) {
613 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
614 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615
616 *olen = ilen;
617
Dave Rodgmande3de772023-06-24 12:51:06 +0100618 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100619 ctx->operation, input,
620 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622 }
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200625 }
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100628 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
630 input, ilen,
631 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200632 }
633#endif
634
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200635#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100636 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
638 input, ilen,
639 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200640 }
641#endif
642
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200643#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100644 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200645 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
647 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200648 }
649#endif
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (input == output &&
652 (ctx->unprocessed_len != 0 || ilen % block_size)) {
653 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100654 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100657 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200658 size_t copy_len = 0;
659
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 /*
661 * If there is not enough data for a full block, cache it.
662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
664 ilen <= block_size - ctx->unprocessed_len) ||
665 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
666 ilen < block_size - ctx->unprocessed_len) ||
667 (ctx->operation == MBEDTLS_ENCRYPT &&
668 ilen < block_size - ctx->unprocessed_len)) {
669 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
670 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671
672 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 }
675
676 /*
677 * Process cached data first
678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100680 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
683 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684
Dave Rodgmande3de772023-06-24 12:51:06 +0100685 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100686 ctx->operation,
687 block_size, ctx->iv,
688 ctx->
689 unprocessed_data,
690 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000692 }
693
Janos Follath98e28a72016-05-31 14:03:54 +0100694 *olen += block_size;
695 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696 ctx->unprocessed_len = 0;
697
698 input += copy_len;
699 ilen -= copy_len;
700 }
701
702 /*
703 * Cache final, incomplete block
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700706 /* Encryption: only cache partial blocks
707 * Decryption w/ padding: always keep at least one whole block
708 * Decryption w/o padding: only cache partial blocks
709 */
Janos Follath98e28a72016-05-31 14:03:54 +0100710 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700712 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100714 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700715 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
718 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719
720 ctx->unprocessed_len += copy_len;
721 ilen -= copy_len;
722 }
723
724 /*
725 * Process remaining full blocks
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100728 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100729 ctx->operation,
730 ilen, ctx->iv,
731 input,
732 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200735
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736 *olen += ilen;
737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100744 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100745 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100746 ctx->operation, ilen,
747 &ctx->unprocessed_len,
748 ctx->iv,
749 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000751 }
752
753 *olen = ilen;
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000758
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100759#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100760 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100761 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100762 ilen,
763 &ctx->unprocessed_len,
764 ctx->iv,
765 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100767 }
768
769 *olen = ilen;
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100772 }
773#endif /* MBEDTLS_CIPHER_MODE_OFB */
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100776 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100777 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100778 ilen,
779 &ctx->unprocessed_len,
780 ctx->iv,
781 ctx->unprocessed_data,
782 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000784 }
785
786 *olen = ilen;
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000791
Jaeden Ameroc6539902018-04-30 17:17:41 +0100792#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100793 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100795 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100797 }
798
Dave Rodgmande3de772023-06-24 12:51:06 +0100799 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100800 ctx->operation,
801 ilen,
802 ctx->iv,
803 input,
804 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (ret != 0) {
806 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100807 }
808
809 *olen = ilen;
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100812 }
813#endif /* MBEDTLS_CIPHER_MODE_XTS */
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100816 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100817 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100818 ilen, input,
819 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200821 }
822
823 *olen = ilen;
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830}
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
833#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200834/*
835 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
836 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837static void add_pkcs_padding(unsigned char *output, size_t output_len,
838 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000839{
Paul Bakker23986e52011-04-24 08:57:21 +0000840 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100841 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000844 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846}
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848static int get_pkcs_padding(unsigned char *input, size_t input_len,
849 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100851 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100852 unsigned char padding_len;
853 mbedtls_ct_condition_t bad = MBEDTLS_CT_FALSE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (NULL == input || NULL == data_len) {
856 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
857 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858
859 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860 *data_len = input_len - padding_len;
861
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100862 bad = mbedtls_ct_uint_gt(padding_len, input_len);
863 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100864
865 /* The number of bytes checked must be independent of padding_len,
866 * so pick input_len, which is usually 8 or 16 (one block) */
867 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 for (i = 0; i < input_len; i++) {
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100869 mbedtls_ct_condition_t dont_ignore = mbedtls_ct_uint_ge(i, pad_idx);
870 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
871 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(dont_ignore, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100873
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100874 return mbedtls_ct_uint_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200879/*
880 * One and zeros padding: fill with 80 00 ... 00
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882static void add_one_and_zeros_padding(unsigned char *output,
883 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200884{
885 size_t padding_len = output_len - data_len;
886 unsigned char i = 0;
887
888 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200890 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200892}
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
895 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200896{
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (NULL == input || NULL == data_len) {
898 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
899 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200900
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100901 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
902 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
903
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100904 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100905
906 for (int i = input_len - 1; i >= 0; i--) {
907 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
908
909 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
910
911 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
912
913 bad = mbedtls_ct_uint_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
914
915 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100916 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200917
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100918 return -((int) mbedtls_ct_uint_if_else_0(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING));
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200923/*
924 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
925 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926static void add_zeros_and_len_padding(unsigned char *output,
927 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200928{
929 size_t padding_len = output_len - data_len;
930 unsigned char i = 0;
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200933 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200935 output[output_len - 1] = (unsigned char) padding_len;
936}
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
939 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200940{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100941 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100942 unsigned char padding_len;
943 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (NULL == input || NULL == data_len) {
946 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
947 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200948
949 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200950 *data_len = input_len - padding_len;
951
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100952 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100953 bad = mbedtls_ct_uint_gt(padding_len, input_len);
954 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100955
956 /* The number of bytes checked must be independent of padding_len */
957 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100959 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
960 mbedtls_ct_condition_t nonzero_pad_byte = mbedtls_ct_uint_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
961 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100963
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100964 return -((int) mbedtls_ct_uint_if_else_0(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING));
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200969/*
970 * Zero padding: fill with 00 ... 00
971 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972static void add_zeros_padding(unsigned char *output,
973 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200974{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200975 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200978 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200980}
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982static int get_zeros_padding(unsigned char *input, size_t input_len,
983 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200984{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100985 size_t i;
986 unsigned char done = 0, prev_done;
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (NULL == input || NULL == data_len) {
989 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100990 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 *data_len = 0;
993 for (i = input_len; i > 0; i--) {
994 prev_done = done;
995 done |= (input[i-1] != 0);
996 *data_len |= i * (done != prev_done);
997 }
998
999 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001002
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003/*
1004 * No padding: don't pad :)
1005 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001007 * but a trivial get_padding function
1008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009static int get_no_padding(unsigned char *input, size_t input_len,
1010 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001011{
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (NULL == input || NULL == data_len) {
1013 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1014 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001015
1016 *data_len = input_len;
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1023 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001024{
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (ctx->cipher_info == NULL) {
1026 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1027 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001029#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001031 /* While PSA Crypto has an API for multipart
1032 * operations, we currently don't make it
1033 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001035 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001036#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001037
Paul Bakker8123e9d2011-01-06 15:37:30 +00001038 *olen = 0;
1039
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001040 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1042 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1043 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1044 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1045 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1046 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001048 }
1049
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001050 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1051 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001053 }
1054
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001055 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (ctx->unprocessed_len != 0) {
1057 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1058 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001061 }
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001064 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001065 int ret = 0;
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001068 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (NULL == ctx->add_padding) {
1070 if (0 != ctx->unprocessed_len) {
1071 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1072 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1078 ctx->unprocessed_len);
1079 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001080 /*
1081 * For decrypt operations, expect a full block,
1082 * or an empty block if no padding
1083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1085 return 0;
1086 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001089 }
1090
1091 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001092 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001093 ctx->operation,
1094 mbedtls_cipher_get_block_size(
1095 ctx),
1096 ctx->iv,
1097 ctx->unprocessed_data,
1098 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100 }
1101
1102 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (MBEDTLS_DECRYPT == ctx->operation) {
1104 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1105 olen);
1106 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001107
1108 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 *olen = mbedtls_cipher_get_block_size(ctx);
1110 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001111 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001112#else
1113 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001117}
1118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1121 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001122{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001123 if (NULL == ctx->cipher_info ||
1124 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001126 }
1127
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001128#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001130 /* While PSA Crypto knows about CBC padding
1131 * schemes, we currently don't make them
1132 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (mode != MBEDTLS_PADDING_NONE) {
1134 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1135 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001138 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001139#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 case MBEDTLS_PADDING_PKCS7:
1144 ctx->add_padding = add_pkcs_padding;
1145 ctx->get_padding = get_pkcs_padding;
1146 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1150 ctx->add_padding = add_one_and_zeros_padding;
1151 ctx->get_padding = get_one_and_zeros_padding;
1152 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1156 ctx->add_padding = add_zeros_and_len_padding;
1157 ctx->get_padding = get_zeros_and_len_padding;
1158 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 case MBEDTLS_PADDING_ZEROS:
1162 ctx->add_padding = add_zeros_padding;
1163 ctx->get_padding = get_zeros_padding;
1164 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001165#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 case MBEDTLS_PADDING_NONE:
1167 ctx->add_padding = NULL;
1168 ctx->get_padding = get_no_padding;
1169 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 default:
1172 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001173 }
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001178
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001179#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001180int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1181 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182{
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if (ctx->cipher_info == NULL) {
1184 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1185 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (MBEDTLS_ENCRYPT != ctx->operation) {
1188 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1189 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001190
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001191#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001193 /* While PSA Crypto has an API for multipart
1194 * operations, we currently don't make it
1195 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001197 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001198#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001199
Daniel King8fe47012016-05-17 20:33:28 -03001200#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001201 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001202 size_t output_length;
1203 /* The code here doesn't yet support alternative implementations
1204 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1206 NULL, 0, &output_length,
1207 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001208 }
Daniel King8fe47012016-05-17 20:33:28 -03001209#endif
1210
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001211#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001212 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001213 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (tag_len != 16U) {
1215 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1216 }
Daniel King8fe47012016-05-17 20:33:28 -03001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return mbedtls_chachapoly_finish(
1219 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001220 }
1221#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001224}
Paul Bakker9af723c2014-05-01 13:03:14 +02001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1227 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001228{
Daniel King8fe47012016-05-17 20:33:28 -03001229 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (ctx->cipher_info == NULL) {
1233 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1234 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (MBEDTLS_DECRYPT != ctx->operation) {
1237 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001238 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001239
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001240#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001242 /* While PSA Crypto has an API for multipart
1243 * operations, we currently don't make it
1244 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001246 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001247#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001248
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001249 /* Status to return on a non-authenticated algorithm. */
1250 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001251
Daniel King8fe47012016-05-17 20:33:28 -03001252#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001253 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001254 size_t output_length;
1255 /* The code here doesn't yet support alternative implementations
1256 * that can delay up to a block of output. */
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (tag_len > sizeof(check_tag)) {
1259 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1260 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (0 != (ret = mbedtls_gcm_finish(
1263 (mbedtls_gcm_context *) ctx->cipher_ctx,
1264 NULL, 0, &output_length,
1265 check_tag, tag_len))) {
1266 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001267 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001268
1269 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001271 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001272 goto exit;
1273 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001274 }
Daniel King8fe47012016-05-17 20:33:28 -03001275#endif /* MBEDTLS_GCM_C */
1276
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001277#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001278 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001279 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (tag_len != sizeof(check_tag)) {
1281 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1282 }
Daniel King8fe47012016-05-17 20:33:28 -03001283
Hanno Becker18597cd2018-11-09 16:36:33 +00001284 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1286 if (ret != 0) {
1287 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001288 }
1289
1290 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001292 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001293 goto exit;
1294 }
Daniel King8fe47012016-05-17 20:33:28 -03001295 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001296#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001297
Gilles Peskinecd742982021-12-13 16:57:47 +01001298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 mbedtls_platform_zeroize(check_tag, tag_len);
1300 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001301}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001302#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001303
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001304/*
1305 * Packet-oriented wrapper for non-AEAD modes
1306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1308 const unsigned char *iv, size_t iv_len,
1309 const unsigned char *input, size_t ilen,
1310 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001311{
Janos Follath24eed8d2019-11-22 13:21:35 +00001312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001313 size_t finish_olen;
1314
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001315#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001317 /* As in the non-PSA case, we don't check that
1318 * a key has been set. If not, the key slot will
1319 * still be in its default state of 0, which is
1320 * guaranteed to be invalid, hence the PSA-call
1321 * below will gracefully fail. */
1322 mbedtls_cipher_context_psa * const cipher_psa =
1323 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1324
1325 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001326 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001327 size_t part_len;
1328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (ctx->operation == MBEDTLS_DECRYPT) {
1330 status = psa_cipher_decrypt_setup(&cipher_op,
1331 cipher_psa->slot,
1332 cipher_psa->alg);
1333 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1334 status = psa_cipher_encrypt_setup(&cipher_op,
1335 cipher_psa->slot,
1336 cipher_psa->alg);
1337 } else {
1338 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001339 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001340
1341 /* In the following, we can immediately return on an error,
1342 * because the PSA Crypto API guarantees that cipher operations
1343 * are terminated by unsuccessful calls to psa_cipher_update(),
1344 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (status != PSA_SUCCESS) {
1346 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001347 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001348
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001349 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1351 if (status != PSA_SUCCESS) {
1352 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1353 }
1354 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 status = psa_cipher_update(&cipher_op,
1357 input, ilen,
1358 output, ilen, olen);
1359 if (status != PSA_SUCCESS) {
1360 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1361 }
1362
1363 status = psa_cipher_finish(&cipher_op,
1364 output + *olen, ilen - *olen,
1365 &part_len);
1366 if (status != PSA_SUCCESS) {
1367 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1368 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001369
1370 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001372 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001373#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 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_reset(ctx)) != 0) {
1380 return ret;
1381 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1384 output, olen)) != 0) {
1385 return ret;
1386 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1389 &finish_olen)) != 0) {
1390 return ret;
1391 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001392
1393 *olen += finish_olen;
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001396}
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001399/*
TRodziewicz18efb732021-04-29 23:12:19 +02001400 * Packet-oriented encryption for AEAD modes: internal function used by
1401 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1404 const unsigned char *iv, size_t iv_len,
1405 const unsigned char *ad, size_t ad_len,
1406 const unsigned char *input, size_t ilen,
1407 unsigned char *output, size_t *olen,
1408 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001409{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001410#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001412 /* As in the non-PSA case, we don't check that
1413 * a key has been set. If not, the key slot will
1414 * still be in its default state of 0, which is
1415 * guaranteed to be invalid, hence the PSA-call
1416 * below will gracefully fail. */
1417 mbedtls_cipher_context_psa * const cipher_psa =
1418 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1419
1420 psa_status_t status;
1421
1422 /* PSA Crypto API always writes the authentication tag
1423 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (output == NULL || tag != output + ilen) {
1425 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1426 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 status = psa_aead_encrypt(cipher_psa->slot,
1429 cipher_psa->alg,
1430 iv, iv_len,
1431 ad, ad_len,
1432 input, ilen,
1433 output, ilen + tag_len, olen);
1434 if (status != PSA_SUCCESS) {
1435 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1436 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001437
1438 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001440 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001441#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001444 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001445 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1447 ilen, iv, iv_len, ad, ad_len,
1448 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_GCM_C */
1451#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001452 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001453 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1455 iv, iv_len, ad, ad_len, input, output,
1456 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001459#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001460 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001461 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001462 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 (tag_len != 16U)) {
1464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
1466
1467 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1469 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001470 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001471#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001474}
1475
1476/*
TRodziewicz18efb732021-04-29 23:12:19 +02001477 * Packet-oriented encryption for AEAD modes: internal function used by
1478 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1481 const unsigned char *iv, size_t iv_len,
1482 const unsigned char *ad, size_t ad_len,
1483 const unsigned char *input, size_t ilen,
1484 unsigned char *output, size_t *olen,
1485 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001486{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001487#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001489 /* As in the non-PSA case, we don't check that
1490 * a key has been set. If not, the key slot will
1491 * still be in its default state of 0, which is
1492 * guaranteed to be invalid, hence the PSA-call
1493 * below will gracefully fail. */
1494 mbedtls_cipher_context_psa * const cipher_psa =
1495 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1496
1497 psa_status_t status;
1498
1499 /* PSA Crypto API always writes the authentication tag
1500 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (input == NULL || tag != input + ilen) {
1502 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1503 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 status = psa_aead_decrypt(cipher_psa->slot,
1506 cipher_psa->alg,
1507 iv, iv_len,
1508 ad, ad_len,
1509 input, ilen + tag_len,
1510 output, ilen, olen);
1511 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1512 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1513 } else if (status != PSA_SUCCESS) {
1514 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1515 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001518 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001519#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001522 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001524
1525 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1527 iv, iv_len, ad, ad_len,
1528 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001535 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_GCM_C */
1537#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001538 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001540
1541 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1543 iv, iv_len, ad, ad_len,
1544 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001553#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001554 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001556
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001557 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001558 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 (tag_len != 16U)) {
1560 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001561 }
1562
1563 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1565 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001568 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 }
Daniel King8fe47012016-05-17 20:33:28 -03001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001572 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001573#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001578
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1580/*
1581 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1582 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1584 const unsigned char *iv, size_t iv_len,
1585 const unsigned char *ad, size_t ad_len,
1586 const unsigned char *input, size_t ilen,
1587 unsigned char *output, size_t output_len,
1588 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001590#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001592#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001593 ctx->psa_enabled == 0 &&
1594#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001595 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1596 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1597 mbedtls_nist_kw_mode_t mode =
1598 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1599 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001600
1601 /* There is no iv, tag or ad associated with KW and KWP,
1602 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1604 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1605 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001606
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001607 (void) iv;
1608 (void) ad;
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1611 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001612 }
1613#endif /* MBEDTLS_NIST_KW_C */
1614
1615#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1616 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (output_len < ilen + tag_len) {
1618 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1619 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1622 input, ilen, output, olen,
1623 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001624 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001626#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001628#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1629}
1630
1631/*
1632 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1635 const unsigned char *iv, size_t iv_len,
1636 const unsigned char *ad, size_t ad_len,
1637 const unsigned char *input, size_t ilen,
1638 unsigned char *output, size_t output_len,
1639 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001640{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001641#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001643#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001644 ctx->psa_enabled == 0 &&
1645#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001646 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1647 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1648 mbedtls_nist_kw_mode_t mode =
1649 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1650 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001651
1652 /* There is no iv, tag or ad associated with KW and KWP,
1653 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1655 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1656 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001657
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001658 (void) iv;
1659 (void) ad;
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1662 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001663 }
1664#endif /* MBEDTLS_NIST_KW_C */
1665
1666#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1667 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (ilen < tag_len || output_len < ilen - tag_len) {
1669 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1670 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1673 input, ilen - tag_len, output, olen,
1674 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001675#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001677#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1678}
1679#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_CIPHER_C */