blob: 9f9f1075c726f87be4a0c2c2084a6d29147dde17 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinee820c0a2023-08-03 17:45:20 +02004 * \brief Generic cipher wrapper for Mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Dave Rodgman6b7e2a52023-09-18 19:00:44 +010033#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stdlib.h>
36#include <string.h>
37
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020038#if defined(MBEDTLS_CHACHAPOLY_C)
39#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020048#endif
49
Daniel Kingbd920622016-05-15 19:56:20 -030050#if defined(MBEDTLS_CHACHA20_C)
51#include "mbedtls/chacha20.h"
52#endif
53
Simon Butcher327398a2016-10-05 14:09:11 +010054#if defined(MBEDTLS_CMAC_C)
55#include "mbedtls/cmac.h"
56#endif
57
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000059#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020060#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000061
Jack Lloydffdf2882019-03-07 17:00:32 -050062#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Simon Butcher327398a2016-10-05 14:09:11 +010066#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010067
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000069
Dave Rodgman3b46b772023-06-24 13:25:06 +010070static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
71 const mbedtls_cipher_info_t *info)
72{
Dave Rodgmande3de772023-06-24 12:51:06 +010073 return mbedtls_cipher_base_lookup_table[info->base_idx];
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020079 int *type;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 def = mbedtls_cipher_definitions;
83 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088
89 *type = 0;
90
91 supported_init = 1;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000095}
96
Hanno Becker18597cd2018-11-09 16:36:33 +000097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010098 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
103 if (def->type == cipher_type) {
104 return def->info;
105 }
106 }
Paul Bakker343a8702011-06-09 14:27:58 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Hanno Becker18597cd2018-11-09 16:36:33 +0000111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (NULL == cipher_name) {
117 return NULL;
118 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
121 if (!strcmp(def->info->name, cipher_name)) {
122 return def->info;
123 }
124 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
130 const mbedtls_cipher_id_t cipher_id,
131 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100137 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100138 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 def->info->mode == mode) {
140 return def->info;
141 }
142 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200145}
146
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200147#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
148static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
149 mbedtls_cipher_type_t cipher)
150{
151 switch (cipher) {
152 case MBEDTLS_CIPHER_AES_128_CCM:
153 case MBEDTLS_CIPHER_AES_192_CCM:
154 case MBEDTLS_CIPHER_AES_256_CCM:
155 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
158 case MBEDTLS_CIPHER_AES_128_GCM:
159 case MBEDTLS_CIPHER_AES_192_GCM:
160 case MBEDTLS_CIPHER_AES_256_GCM:
161 case MBEDTLS_CIPHER_AES_128_CBC:
162 case MBEDTLS_CIPHER_AES_192_CBC:
163 case MBEDTLS_CIPHER_AES_256_CBC:
164 case MBEDTLS_CIPHER_AES_128_ECB:
165 case MBEDTLS_CIPHER_AES_192_ECB:
166 case MBEDTLS_CIPHER_AES_256_ECB:
167 return PSA_KEY_TYPE_AES;
168
169 /* ARIA not yet supported in PSA. */
170 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
171 case MBEDTLS_CIPHER_ARIA_192_CCM:
172 case MBEDTLS_CIPHER_ARIA_256_CCM:
173 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
176 case MBEDTLS_CIPHER_ARIA_128_GCM:
177 case MBEDTLS_CIPHER_ARIA_192_GCM:
178 case MBEDTLS_CIPHER_ARIA_256_GCM:
179 case MBEDTLS_CIPHER_ARIA_128_CBC:
180 case MBEDTLS_CIPHER_ARIA_192_CBC:
181 case MBEDTLS_CIPHER_ARIA_256_CBC:
182 return( PSA_KEY_TYPE_ARIA ); */
183
184 default:
185 return 0;
186 }
187}
188
189static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
190 mbedtls_cipher_mode_t mode, size_t taglen)
191{
192 switch (mode) {
193 case MBEDTLS_MODE_ECB:
194 return PSA_ALG_ECB_NO_PADDING;
195 case MBEDTLS_MODE_GCM:
196 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
197 case MBEDTLS_MODE_CCM:
198 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
199 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
200 return PSA_ALG_CCM_STAR_NO_TAG;
201 case MBEDTLS_MODE_CBC:
202 if (taglen == 0) {
203 return PSA_ALG_CBC_NO_PADDING;
204 } else {
205 return 0;
206 }
207 default:
208 return 0;
209 }
210}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200211#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200219{
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200221 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200223
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (ctx->psa_enabled == 1) {
226 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000227 mbedtls_cipher_context_psa * const cipher_psa =
228 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000231 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000233 }
234
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100235 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100245 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100247 }
248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100251 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200255}
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
258 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (cipher_info == NULL) {
261 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
262 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Dave Rodgmande3de772023-06-24 12:51:06 +0100266 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
268 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269
270 ctx->cipher_info = cipher_info;
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000273}
274
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200275#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
277 const mbedtls_cipher_info_t *cipher_info,
278 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000279{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000280 psa_algorithm_t alg;
281 mbedtls_cipher_context_psa *cipher_psa;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (NULL == cipher_info || NULL == ctx) {
284 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
285 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000286
Hanno Becker4ee7e762018-11-17 22:00:38 +0000287 /* Check that the underlying cipher mode and cipher type are
288 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100289 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (alg == 0) {
291 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
292 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100293 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
295 }
Hanno Becker6118e432018-11-09 16:47:20 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
300 if (cipher_psa == NULL) {
301 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
302 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000303 cipher_psa->alg = alg;
304 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000305 ctx->cipher_info = cipher_info;
306 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000308}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200309#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
312 const unsigned char *key,
313 int key_bitlen,
314 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315{
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100317 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
319 if (ctx->cipher_info == NULL) {
320 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
321 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000322
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200323#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000325 mbedtls_cipher_context_psa * const cipher_psa =
326 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000329
330 psa_status_t status;
331 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000333
334 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (key_bitlen % 8 != 0) {
336 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
337 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000338
339 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
341 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
342 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000343
Andrzej Kurekc7509322019-01-08 09:36:01 -0500344 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100345 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (key_type == 0) {
347 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
348 }
349 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000350
351 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500352 * (encrypt vs. decrypt): it is possible to setup a key for encryption
353 * and use it for AEAD decryption. Until tests relying on this
354 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
357 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 status = psa_import_key(&attributes, key, key_bytelen,
360 &cipher_psa->slot);
361 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200362 case PSA_SUCCESS:
363 break;
364 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200366 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200368 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200370 }
371 /* Indicate that we own the key slot and need to
372 * destroy it in mbedtls_cipher_free(). */
373 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000374
375 ctx->key_bitlen = key_bitlen;
376 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000378 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200379#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100382 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200384 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200385
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200386 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387 ctx->operation = operation;
388
Paul Bakker343a8702011-06-09 14:27:58 +0000389 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100390 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100393 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
394 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
395 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100396 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100397 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000398 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100401 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100402 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406}
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
409 const unsigned char *iv,
410 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200412 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ctx->cipher_info == NULL) {
415 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
416 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200417#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000419 /* While PSA Crypto has an API for multipart
420 * operations, we currently don't make it
421 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000423 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200424#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000425
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200426 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
428 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
429 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200432 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100434 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200435
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200436 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (actual_iv_size > iv_len) {
438 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
439 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200440 }
441
Daniel Kingbd920622016-05-15 19:56:20 -0300442#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100443 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100444 /* Even though the actual_iv_size is overwritten with a correct value
445 * of 12 from the cipher info, return an error to indicate that
446 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (iv_len != 12) {
448 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
449 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
452 iv,
453 0U)) { /* Initial counter value */
454 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300455 }
456 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100457#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100458 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 iv_len != 12) {
460 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
461 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100462#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300463#endif
464
Gilles Peskine295fc132021-04-15 18:32:23 +0200465#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100466 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
468 ctx->operation,
469 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200470 }
471#endif
472
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200473#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100474 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200475 int set_lengths_result;
476 int ccm_star_mode;
477
478 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 (mbedtls_ccm_context *) ctx->cipher_ctx,
480 0, 0, 0);
481 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200482 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200486 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200488 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200490 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
494 ccm_star_mode,
495 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200496 }
497#endif
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (actual_iv_size != 0) {
500 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300501 ctx->iv_size = actual_iv_size;
502 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200508{
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (ctx->cipher_info == NULL) {
510 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
511 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200512
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200513#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000515 /* We don't support resetting PSA-based
516 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000518 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200519#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000520
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521 ctx->unprocessed_len = 0;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200524}
525
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200526#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
528 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200529{
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (ctx->cipher_info == NULL) {
531 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
532 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200533
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200534#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000536 /* While PSA Crypto has an API for multipart
537 * operations, we currently don't make it
538 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000540 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200541#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000542
Daniel King8fe47012016-05-17 20:33:28 -0300543#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100544 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
546 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200547 }
Daniel King8fe47012016-05-17 20:33:28 -0300548#endif
549
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200550#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100551 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300552 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200553 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200556 ? MBEDTLS_CHACHAPOLY_ENCRYPT
557 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
560 ctx->iv,
561 mode);
562 if (result != 0) {
563 return result;
564 }
Daniel King8fe47012016-05-17 20:33:28 -0300565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
567 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300568 }
569#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200573#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
576 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577{
Janos Follath24eed8d2019-11-22 13:21:35 +0000578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500579 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (ctx->cipher_info == NULL) {
582 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
583 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200585#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000587 /* While PSA Crypto has an API for multipart
588 * operations, we currently don't make it
589 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000591 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200592#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000593
Paul Bakker6c212762013-12-16 15:24:50 +0100594 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 block_size = mbedtls_cipher_get_block_size(ctx);
596 if (0 == block_size) {
597 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100598 }
Paul Bakker6c212762013-12-16 15:24:50 +0100599
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100600 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ilen != block_size) {
602 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
603 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200604
605 *olen = ilen;
606
Dave Rodgmande3de772023-06-24 12:51:06 +0100607 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100608 ctx->operation, input,
609 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200611 }
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200614 }
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100617 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
619 input, ilen,
620 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200621 }
622#endif
623
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200624#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100625 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
627 input, ilen,
628 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200629 }
630#endif
631
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200632#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100633 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200634 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
636 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200637 }
638#endif
639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (input == output &&
641 (ctx->unprocessed_len != 0 || ilen % block_size)) {
642 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100643 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100646 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200647 size_t copy_len = 0;
648
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 /*
650 * If there is not enough data for a full block, cache it.
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
653 ilen <= block_size - ctx->unprocessed_len) ||
654 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
655 ilen < block_size - ctx->unprocessed_len) ||
656 (ctx->operation == MBEDTLS_ENCRYPT &&
657 ilen < block_size - ctx->unprocessed_len)) {
658 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
659 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660
661 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 }
664
665 /*
666 * Process cached data first
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100669 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
672 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673
Dave Rodgmande3de772023-06-24 12:51:06 +0100674 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100675 ctx->operation,
676 block_size, ctx->iv,
677 ctx->
678 unprocessed_data,
679 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 }
682
Janos Follath98e28a72016-05-31 14:03:54 +0100683 *olen += block_size;
684 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685 ctx->unprocessed_len = 0;
686
687 input += copy_len;
688 ilen -= copy_len;
689 }
690
691 /*
692 * Cache final, incomplete block
693 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700695 /* Encryption: only cache partial blocks
696 * Decryption w/ padding: always keep at least one whole block
697 * Decryption w/o padding: only cache partial blocks
698 */
Janos Follath98e28a72016-05-31 14:03:54 +0100699 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700701 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100703 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700704 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
707 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708
709 ctx->unprocessed_len += copy_len;
710 ilen -= copy_len;
711 }
712
713 /*
714 * Process remaining full blocks
715 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100717 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100718 ctx->operation,
719 ilen, ctx->iv,
720 input,
721 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200724
Paul Bakker8123e9d2011-01-06 15:37:30 +0000725 *olen += ilen;
726 }
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100733 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100734 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100735 ctx->operation, ilen,
736 &ctx->unprocessed_len,
737 ctx->iv,
738 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000740 }
741
742 *olen = ilen;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000745 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000747
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100748#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100749 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100750 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100751 ilen,
752 &ctx->unprocessed_len,
753 ctx->iv,
754 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100756 }
757
758 *olen = ilen;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100761 }
762#endif /* MBEDTLS_CIPHER_MODE_OFB */
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100765 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100766 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100767 ilen,
768 &ctx->unprocessed_len,
769 ctx->iv,
770 ctx->unprocessed_data,
771 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000773 }
774
775 *olen = ilen;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000778 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000780
Jaeden Ameroc6539902018-04-30 17:17:41 +0100781#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100782 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100784 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100786 }
787
Dave Rodgmande3de772023-06-24 12:51:06 +0100788 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100789 ctx->operation,
790 ilen,
791 ctx->iv,
792 input,
793 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ret != 0) {
795 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100796 }
797
798 *olen = ilen;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100801 }
802#endif /* MBEDTLS_CIPHER_MODE_XTS */
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100805 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100806 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100807 ilen, input,
808 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200810 }
811
812 *olen = ilen;
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819}
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
822#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823/*
824 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
825 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826static void add_pkcs_padding(unsigned char *output, size_t output_len,
827 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000828{
Paul Bakker23986e52011-04-24 08:57:21 +0000829 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100830 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000833 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000835}
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837static int get_pkcs_padding(unsigned char *input, size_t input_len,
838 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000839{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100840 size_t i, pad_idx;
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100841 unsigned char padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (NULL == input || NULL == data_len) {
844 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
845 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846
847 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000848 *data_len = input_len - padding_len;
849
Dave Rodgmane834d6c2023-09-20 19:06:53 +0100850 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
Dave Rodgman6b7e2a52023-09-18 19:00:44 +0100851 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100852
853 /* The number of bytes checked must be independent of padding_len,
854 * so pick input_len, which is usually 8 or 16 (one block) */
855 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 for (i = 0; i < input_len; i++) {
Dave Rodgmanc43a0a42023-09-20 19:07:22 +0100857 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
858 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
859 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100861
Dave Rodgmand03f4832023-09-22 09:52:15 +0100862 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200867/*
868 * One and zeros padding: fill with 80 00 ... 00
869 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870static void add_one_and_zeros_padding(unsigned char *output,
871 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200872{
873 size_t padding_len = output_len - data_len;
874 unsigned char i = 0;
875
876 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200878 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200880}
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
883 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200884{
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (NULL == input || NULL == data_len) {
886 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
887 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200888
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100889 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
890 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
891
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100892 *data_len = 0;
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100893
Dave Rodgman437500c2023-09-19 21:36:43 +0100894 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100895 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
896
897 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
898
899 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
900
Dave Rodgmanfd965792023-09-19 21:51:50 +0100901 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
Dave Rodgman89a9bd52023-09-19 14:37:50 +0100902
903 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100904 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200905
Dave Rodgmand03f4832023-09-22 09:52:15 +0100906 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200911/*
912 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914static void add_zeros_and_len_padding(unsigned char *output,
915 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200916{
917 size_t padding_len = output_len - data_len;
918 unsigned char i = 0;
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200921 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200923 output[output_len - 1] = (unsigned char) padding_len;
924}
925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
927 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200928{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100929 size_t i, pad_idx;
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100930 unsigned char padding_len;
931 mbedtls_ct_condition_t bad;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (NULL == input || NULL == data_len) {
934 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
935 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200936
937 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200938 *data_len = input_len - padding_len;
939
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100940 /* Avoid logical || since it results in a branch */
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100941 bad = mbedtls_ct_uint_gt(padding_len, input_len);
942 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100943
944 /* The number of bytes checked must be independent of padding_len */
945 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100947 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
Dave Rodgman6be4bcf2023-09-19 19:47:51 +0100948 mbedtls_ct_condition_t nonzero_pad_byte;
Dave Rodgmanfd965792023-09-19 21:51:50 +0100949 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
Dave Rodgman6cec41c2023-09-18 21:51:55 +0100950 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100952
Dave Rodgmand03f4832023-09-22 09:52:15 +0100953 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200958/*
959 * Zero padding: fill with 00 ... 00
960 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961static void add_zeros_padding(unsigned char *output,
962 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200963{
Dave Rodgmanf8182d92023-09-19 16:25:17 +0100964 memset(output + data_len, 0, output_len - data_len);
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200965}
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967static int get_zeros_padding(unsigned char *input, size_t input_len,
968 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200969{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100970 size_t i;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100971 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (NULL == input || NULL == data_len) {
974 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100975 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 *data_len = 0;
978 for (i = input_len; i > 0; i--) {
979 prev_done = done;
Dave Rodgmand8c68a92023-09-19 16:19:38 +0100980 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
981 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
983
984 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200987
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988/*
989 * No padding: don't pad :)
990 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992 * but a trivial get_padding function
993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994static int get_no_padding(unsigned char *input, size_t input_len,
995 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200996{
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (NULL == input || NULL == data_len) {
998 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
999 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001000
1001 *data_len = input_len;
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1008 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001009{
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (ctx->cipher_info == NULL) {
1011 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1012 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001013
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001014#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001016 /* While PSA Crypto has an API for multipart
1017 * operations, we currently don't make it
1018 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001020 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001021#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001022
Paul Bakker8123e9d2011-01-06 15:37:30 +00001023 *olen = 0;
1024
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001025#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1026 /* CBC mode requires padding so we make sure a call to
1027 * mbedtls_cipher_set_padding_mode has been done successfully. */
1028 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1029 if (ctx->get_padding == NULL) {
1030 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1031 }
1032 }
1033#endif
1034
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001035 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1036 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1037 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1038 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1039 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001043 }
1044
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001045 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1046 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001048 }
1049
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001050 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ctx->unprocessed_len != 0) {
1052 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1053 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001056 }
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001059 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001060 int ret = 0;
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001063 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (NULL == ctx->add_padding) {
1065 if (0 != ctx->unprocessed_len) {
1066 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1067 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1073 ctx->unprocessed_len);
1074 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001075 /*
1076 * For decrypt operations, expect a full block,
1077 * or an empty block if no padding
1078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1080 return 0;
1081 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001084 }
1085
1086 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001087 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001088 ctx->operation,
1089 mbedtls_cipher_get_block_size(
1090 ctx),
1091 ctx->iv,
1092 ctx->unprocessed_data,
1093 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001095 }
1096
1097 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (MBEDTLS_DECRYPT == ctx->operation) {
1099 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1100 olen);
1101 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001102
1103 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 *olen = mbedtls_cipher_get_block_size(ctx);
1105 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001107#else
1108 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001112}
1113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001115int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1116 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001117{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001118 if (NULL == ctx->cipher_info ||
1119 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001121 }
1122
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001123#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001125 /* While PSA Crypto knows about CBC padding
1126 * schemes, we currently don't make them
1127 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (mode != MBEDTLS_PADDING_NONE) {
1129 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1130 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001133 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001134#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 case MBEDTLS_PADDING_PKCS7:
1139 ctx->add_padding = add_pkcs_padding;
1140 ctx->get_padding = get_pkcs_padding;
1141 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1145 ctx->add_padding = add_one_and_zeros_padding;
1146 ctx->get_padding = get_one_and_zeros_padding;
1147 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1151 ctx->add_padding = add_zeros_and_len_padding;
1152 ctx->get_padding = get_zeros_and_len_padding;
1153 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 case MBEDTLS_PADDING_ZEROS:
1157 ctx->add_padding = add_zeros_padding;
1158 ctx->get_padding = get_zeros_padding;
1159 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001160#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 case MBEDTLS_PADDING_NONE:
1162 ctx->add_padding = NULL;
1163 ctx->get_padding = get_no_padding;
1164 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 default:
1167 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001168 }
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001173
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001174#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001175int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1176 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001177{
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (ctx->cipher_info == NULL) {
1179 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1180 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (MBEDTLS_ENCRYPT != ctx->operation) {
1183 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1184 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001186#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001188 /* While PSA Crypto has an API for multipart
1189 * operations, we currently don't make it
1190 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001192 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001193#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001194
Daniel King8fe47012016-05-17 20:33:28 -03001195#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001196 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001197 size_t output_length;
1198 /* The code here doesn't yet support alternative implementations
1199 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1201 NULL, 0, &output_length,
1202 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001203 }
Daniel King8fe47012016-05-17 20:33:28 -03001204#endif
1205
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001206#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001207 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001208 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (tag_len != 16U) {
1210 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1211 }
Daniel King8fe47012016-05-17 20:33:28 -03001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 return mbedtls_chachapoly_finish(
1214 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001215 }
1216#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001219}
Paul Bakker9af723c2014-05-01 13:03:14 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1222 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001223{
Daniel King8fe47012016-05-17 20:33:28 -03001224 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (ctx->cipher_info == NULL) {
1228 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1229 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (MBEDTLS_DECRYPT != ctx->operation) {
1232 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001233 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001234
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001235#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001237 /* While PSA Crypto has an API for multipart
1238 * operations, we currently don't make it
1239 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001241 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001242#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001243
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001244 /* Status to return on a non-authenticated algorithm. */
1245 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001246
Daniel King8fe47012016-05-17 20:33:28 -03001247#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001248 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001249 size_t output_length;
1250 /* The code here doesn't yet support alternative implementations
1251 * that can delay up to a block of output. */
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (tag_len > sizeof(check_tag)) {
1254 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1255 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (0 != (ret = mbedtls_gcm_finish(
1258 (mbedtls_gcm_context *) ctx->cipher_ctx,
1259 NULL, 0, &output_length,
1260 check_tag, tag_len))) {
1261 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001262 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001263
1264 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001266 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001267 goto exit;
1268 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001269 }
Daniel King8fe47012016-05-17 20:33:28 -03001270#endif /* MBEDTLS_GCM_C */
1271
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001272#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001273 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001274 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (tag_len != sizeof(check_tag)) {
1276 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1277 }
Daniel King8fe47012016-05-17 20:33:28 -03001278
Hanno Becker18597cd2018-11-09 16:36:33 +00001279 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1281 if (ret != 0) {
1282 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001283 }
1284
1285 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001287 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001288 goto exit;
1289 }
Daniel King8fe47012016-05-17 20:33:28 -03001290 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001291#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001292
Gilles Peskinecd742982021-12-13 16:57:47 +01001293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_platform_zeroize(check_tag, tag_len);
1295 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001296}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001297#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001298
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001299/*
1300 * Packet-oriented wrapper for non-AEAD modes
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1303 const unsigned char *iv, size_t iv_len,
1304 const unsigned char *input, size_t ilen,
1305 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001306{
Janos Follath24eed8d2019-11-22 13:21:35 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001308 size_t finish_olen;
1309
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001310#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001312 /* As in the non-PSA case, we don't check that
1313 * a key has been set. If not, the key slot will
1314 * still be in its default state of 0, which is
1315 * guaranteed to be invalid, hence the PSA-call
1316 * below will gracefully fail. */
1317 mbedtls_cipher_context_psa * const cipher_psa =
1318 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1319
1320 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001321 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001322 size_t part_len;
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (ctx->operation == MBEDTLS_DECRYPT) {
1325 status = psa_cipher_decrypt_setup(&cipher_op,
1326 cipher_psa->slot,
1327 cipher_psa->alg);
1328 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1329 status = psa_cipher_encrypt_setup(&cipher_op,
1330 cipher_psa->slot,
1331 cipher_psa->alg);
1332 } else {
1333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001334 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001335
1336 /* In the following, we can immediately return on an error,
1337 * because the PSA Crypto API guarantees that cipher operations
1338 * are terminated by unsuccessful calls to psa_cipher_update(),
1339 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (status != PSA_SUCCESS) {
1341 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001342 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001343
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001344 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1346 if (status != PSA_SUCCESS) {
1347 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1348 }
1349 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 status = psa_cipher_update(&cipher_op,
1352 input, ilen,
1353 output, ilen, olen);
1354 if (status != PSA_SUCCESS) {
1355 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1356 }
1357
1358 status = psa_cipher_finish(&cipher_op,
1359 output + *olen, ilen - *olen,
1360 &part_len);
1361 if (status != PSA_SUCCESS) {
1362 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1363 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001364
1365 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001367 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001368#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1371 return ret;
1372 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1375 return ret;
1376 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1379 output, olen)) != 0) {
1380 return ret;
1381 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1384 &finish_olen)) != 0) {
1385 return ret;
1386 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001387
1388 *olen += finish_olen;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001391}
1392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001394/*
TRodziewicz18efb732021-04-29 23:12:19 +02001395 * Packet-oriented encryption for AEAD modes: internal function used by
1396 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1399 const unsigned char *iv, size_t iv_len,
1400 const unsigned char *ad, size_t ad_len,
1401 const unsigned char *input, size_t ilen,
1402 unsigned char *output, size_t *olen,
1403 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001405#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001407 /* As in the non-PSA case, we don't check that
1408 * a key has been set. If not, the key slot will
1409 * still be in its default state of 0, which is
1410 * guaranteed to be invalid, hence the PSA-call
1411 * below will gracefully fail. */
1412 mbedtls_cipher_context_psa * const cipher_psa =
1413 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1414
1415 psa_status_t status;
1416
1417 /* PSA Crypto API always writes the authentication tag
1418 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (output == NULL || tag != output + ilen) {
1420 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1421 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 status = psa_aead_encrypt(cipher_psa->slot,
1424 cipher_psa->alg,
1425 iv, iv_len,
1426 ad, ad_len,
1427 input, ilen,
1428 output, ilen + tag_len, olen);
1429 if (status != PSA_SUCCESS) {
1430 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1431 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001432
1433 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001435 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001436#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001439 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001440 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1442 ilen, iv, iv_len, ad, ad_len,
1443 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_GCM_C */
1446#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001447 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001448 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1450 iv, iv_len, ad, ad_len, input, output,
1451 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001452 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001454#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001455 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001456 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001457 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 (tag_len != 16U)) {
1459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001460 }
1461
1462 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1464 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001465 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001466#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001469}
1470
1471/*
TRodziewicz18efb732021-04-29 23:12:19 +02001472 * Packet-oriented encryption for AEAD modes: internal function used by
1473 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1476 const unsigned char *iv, size_t iv_len,
1477 const unsigned char *ad, size_t ad_len,
1478 const unsigned char *input, size_t ilen,
1479 unsigned char *output, size_t *olen,
1480 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001481{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001482#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001484 /* As in the non-PSA case, we don't check that
1485 * a key has been set. If not, the key slot will
1486 * still be in its default state of 0, which is
1487 * guaranteed to be invalid, hence the PSA-call
1488 * below will gracefully fail. */
1489 mbedtls_cipher_context_psa * const cipher_psa =
1490 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1491
1492 psa_status_t status;
1493
1494 /* PSA Crypto API always writes the authentication tag
1495 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (input == NULL || tag != input + ilen) {
1497 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1498 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 status = psa_aead_decrypt(cipher_psa->slot,
1501 cipher_psa->alg,
1502 iv, iv_len,
1503 ad, ad_len,
1504 input, ilen + tag_len,
1505 output, ilen, olen);
1506 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1507 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1508 } else if (status != PSA_SUCCESS) {
1509 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1510 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001513 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001514#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001517 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001519
1520 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1522 iv, iv_len, ad, ad_len,
1523 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_GCM_C */
1532#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001533 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001535
1536 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1538 iv, iv_len, ad, ad_len,
1539 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001546 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001548#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001549 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001551
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001552 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001553 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 (tag_len != 16U)) {
1555 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001556 }
1557
1558 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1560 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001563 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 }
Daniel King8fe47012016-05-17 20:33:28 -03001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001567 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001568#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001573
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001574#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1575/*
1576 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1579 const unsigned char *iv, size_t iv_len,
1580 const unsigned char *ad, size_t ad_len,
1581 const unsigned char *input, size_t ilen,
1582 unsigned char *output, size_t output_len,
1583 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001584{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001585#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001587#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001588 ctx->psa_enabled == 0 &&
1589#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001590 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1591 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1592 mbedtls_nist_kw_mode_t mode =
1593 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1594 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001595
1596 /* There is no iv, tag or ad associated with KW and KWP,
1597 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1599 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1600 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001601
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001602 (void) iv;
1603 (void) ad;
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1606 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001607 }
1608#endif /* MBEDTLS_NIST_KW_C */
1609
1610#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1611 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (output_len < ilen + tag_len) {
1613 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1614 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1617 input, ilen, output, olen,
1618 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001619 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001621#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001623#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1624}
1625
1626/*
1627 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1630 const unsigned char *iv, size_t iv_len,
1631 const unsigned char *ad, size_t ad_len,
1632 const unsigned char *input, size_t ilen,
1633 unsigned char *output, size_t output_len,
1634 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001635{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001636#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001638#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001639 ctx->psa_enabled == 0 &&
1640#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001641 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1642 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1643 mbedtls_nist_kw_mode_t mode =
1644 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1645 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001646
1647 /* There is no iv, tag or ad associated with KW and KWP,
1648 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1650 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1651 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001652
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001653 (void) iv;
1654 (void) ad;
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1657 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001658 }
1659#endif /* MBEDTLS_NIST_KW_C */
1660
1661#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1662 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ilen < tag_len || output_len < ilen - tag_len) {
1664 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1665 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1668 input, ilen - tag_len, output, olen,
1669 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001670#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001672#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1673}
1674#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_CIPHER_C */