blob: de7f8378e349789a786aecc13f29679347f6d57b [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020057#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000058#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020059#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000060
Jack Lloydffdf2882019-03-07 17:00:32 -050061#if defined(MBEDTLS_NIST_KW_C)
62#include "mbedtls/nist_kw.h"
63#endif
64
Simon Butcher327398a2016-10-05 14:09:11 +010065#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010066
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000068
Dave Rodgman3b46b772023-06-24 13:25:06 +010069static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
70 const mbedtls_cipher_info_t *info)
71{
Dave Rodgmande3de772023-06-24 12:51:06 +010072 return mbedtls_cipher_base_lookup_table[info->base_idx];
73}
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078 int *type;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 def = mbedtls_cipher_definitions;
82 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010086 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020087
88 *type = 0;
89
90 supported_init = 1;
91 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000094}
95
Hanno Becker18597cd2018-11-09 16:36:33 +000096const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010097 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
102 if (def->type == cipher_type) {
103 return def->info;
104 }
105 }
Paul Bakker343a8702011-06-09 14:27:58 +0000106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108}
109
Hanno Becker18597cd2018-11-09 16:36:33 +0000110const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (NULL == cipher_name) {
116 return NULL;
117 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
120 if (!strcmp(def->info->name, cipher_name)) {
121 return def->info;
122 }
123 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126}
127
Hanno Becker18597cd2018-11-09 16:36:33 +0000128const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
129 const mbedtls_cipher_id_t cipher_id,
130 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100136 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100137 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 def->info->mode == mode) {
139 return def->info;
140 }
141 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144}
145
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200146#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
147static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
148 mbedtls_cipher_type_t cipher)
149{
150 switch (cipher) {
151 case MBEDTLS_CIPHER_AES_128_CCM:
152 case MBEDTLS_CIPHER_AES_192_CCM:
153 case MBEDTLS_CIPHER_AES_256_CCM:
154 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
155 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_128_GCM:
158 case MBEDTLS_CIPHER_AES_192_GCM:
159 case MBEDTLS_CIPHER_AES_256_GCM:
160 case MBEDTLS_CIPHER_AES_128_CBC:
161 case MBEDTLS_CIPHER_AES_192_CBC:
162 case MBEDTLS_CIPHER_AES_256_CBC:
163 case MBEDTLS_CIPHER_AES_128_ECB:
164 case MBEDTLS_CIPHER_AES_192_ECB:
165 case MBEDTLS_CIPHER_AES_256_ECB:
166 return PSA_KEY_TYPE_AES;
167
168 /* ARIA not yet supported in PSA. */
169 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
170 case MBEDTLS_CIPHER_ARIA_192_CCM:
171 case MBEDTLS_CIPHER_ARIA_256_CCM:
172 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
173 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_128_GCM:
176 case MBEDTLS_CIPHER_ARIA_192_GCM:
177 case MBEDTLS_CIPHER_ARIA_256_GCM:
178 case MBEDTLS_CIPHER_ARIA_128_CBC:
179 case MBEDTLS_CIPHER_ARIA_192_CBC:
180 case MBEDTLS_CIPHER_ARIA_256_CBC:
181 return( PSA_KEY_TYPE_ARIA ); */
182
183 default:
184 return 0;
185 }
186}
187
188static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
189 mbedtls_cipher_mode_t mode, size_t taglen)
190{
191 switch (mode) {
192 case MBEDTLS_MODE_ECB:
193 return PSA_ALG_ECB_NO_PADDING;
194 case MBEDTLS_MODE_GCM:
195 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
196 case MBEDTLS_MODE_CCM:
197 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
198 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
199 return PSA_ALG_CCM_STAR_NO_TAG;
200 case MBEDTLS_MODE_CBC:
201 if (taglen == 0) {
202 return PSA_ALG_CBC_NO_PADDING;
203 } else {
204 return 0;
205 }
206 default:
207 return 0;
208 }
209}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200210#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200213{
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200215}
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200220 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200222
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200223#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (ctx->psa_enabled == 1) {
225 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000226 mbedtls_cipher_context_psa * const cipher_psa =
227 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000230 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000232 }
233
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100234 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000235 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000238 return;
239 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200240#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000241
Simon Butcher327398a2016-10-05 14:09:11 +0100242#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100244 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100246 }
247#endif
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100250 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200254}
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
257 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (cipher_info == NULL) {
260 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
261 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Dave Rodgmande3de772023-06-24 12:51:06 +0100265 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
267 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268
269 ctx->cipher_info = cipher_info;
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200272 /*
273 * Ignore possible errors caused by a cipher mode that doesn't use padding
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200277#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000283}
284
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200285#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
287 const mbedtls_cipher_info_t *cipher_info,
288 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000289{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000290 psa_algorithm_t alg;
291 mbedtls_cipher_context_psa *cipher_psa;
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (NULL == cipher_info || NULL == ctx) {
294 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
295 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000296
Hanno Becker4ee7e762018-11-17 22:00:38 +0000297 /* Check that the underlying cipher mode and cipher type are
298 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100299 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (alg == 0) {
301 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
302 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100303 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
305 }
Hanno Becker6118e432018-11-09 16:47:20 +0000306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
310 if (cipher_psa == NULL) {
311 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
312 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000313 cipher_psa->alg = alg;
314 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000315 ctx->cipher_info = cipher_info;
316 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000318}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200319#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
322 const unsigned char *key,
323 int key_bitlen,
324 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325{
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100327 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 }
329 if (ctx->cipher_info == NULL) {
330 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
331 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000332
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200333#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000335 mbedtls_cipher_context_psa * const cipher_psa =
336 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000339
340 psa_status_t status;
341 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000343
344 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (key_bitlen % 8 != 0) {
346 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
347 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000348
349 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
351 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
352 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000353
Andrzej Kurekc7509322019-01-08 09:36:01 -0500354 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100355 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (key_type == 0) {
357 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
358 }
359 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000360
361 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500362 * (encrypt vs. decrypt): it is possible to setup a key for encryption
363 * and use it for AEAD decryption. Until tests relying on this
364 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
367 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 status = psa_import_key(&attributes, key, key_bytelen,
370 &cipher_psa->slot);
371 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200372 case PSA_SUCCESS:
373 break;
374 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200376 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200378 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200380 }
381 /* Indicate that we own the key slot and need to
382 * destroy it in mbedtls_cipher_free(). */
383 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000384
385 ctx->key_bitlen = key_bitlen;
386 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000388 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200389#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100392 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200394 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200395
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200396 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 ctx->operation = operation;
398
Paul Bakker343a8702011-06-09 14:27:58 +0000399 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100400 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100403 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
404 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
405 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100406 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100407 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000408 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100411 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100412 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416}
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
419 const unsigned char *iv,
420 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200422 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (ctx->cipher_info == NULL) {
425 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
426 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200427#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000429 /* While PSA Crypto has an API for multipart
430 * operations, we currently don't make it
431 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000433 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200434#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000435
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200436 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
438 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
439 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200442 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100444 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200445
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200446 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (actual_iv_size > iv_len) {
448 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
449 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200450 }
451
Daniel Kingbd920622016-05-15 19:56:20 -0300452#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100453 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100454 /* Even though the actual_iv_size is overwritten with a correct value
455 * of 12 from the cipher info, return an error to indicate that
456 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (iv_len != 12) {
458 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
459 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
462 iv,
463 0U)) { /* Initial counter value */
464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300465 }
466 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100467#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100468 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 iv_len != 12) {
470 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
471 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100472#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300473#endif
474
Gilles Peskine295fc132021-04-15 18:32:23 +0200475#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100476 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
478 ctx->operation,
479 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200480 }
481#endif
482
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200483#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100484 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200485 int set_lengths_result;
486 int ccm_star_mode;
487
488 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 (mbedtls_ccm_context *) ctx->cipher_ctx,
490 0, 0, 0);
491 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200492 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200496 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200498 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200500 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
504 ccm_star_mode,
505 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200506 }
507#endif
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (actual_iv_size != 0) {
510 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300511 ctx->iv_size = actual_iv_size;
512 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200515}
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (ctx->cipher_info == NULL) {
520 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
521 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200522
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200523#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000525 /* We don't support resetting PSA-based
526 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000528 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200529#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000530
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531 ctx->unprocessed_len = 0;
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200534}
535
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200536#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100537int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
538 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200539{
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (ctx->cipher_info == NULL) {
541 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
542 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200543
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200544#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000546 /* While PSA Crypto has an API for multipart
547 * operations, we currently don't make it
548 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000550 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200551#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000552
Daniel King8fe47012016-05-17 20:33:28 -0300553#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100554 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
556 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200557 }
Daniel King8fe47012016-05-17 20:33:28 -0300558#endif
559
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200560#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100561 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300562 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200563 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200566 ? MBEDTLS_CHACHAPOLY_ENCRYPT
567 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
570 ctx->iv,
571 mode);
572 if (result != 0) {
573 return result;
574 }
Daniel King8fe47012016-05-17 20:33:28 -0300575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
577 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300578 }
579#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200583#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
586 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587{
Janos Follath24eed8d2019-11-22 13:21:35 +0000588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500589 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (ctx->cipher_info == NULL) {
592 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
593 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200595#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000597 /* While PSA Crypto has an API for multipart
598 * operations, we currently don't make it
599 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000601 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200602#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000603
Paul Bakker6c212762013-12-16 15:24:50 +0100604 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 block_size = mbedtls_cipher_get_block_size(ctx);
606 if (0 == block_size) {
607 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100608 }
Paul Bakker6c212762013-12-16 15:24:50 +0100609
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100610 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (ilen != block_size) {
612 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
613 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200614
615 *olen = ilen;
616
Dave Rodgmande3de772023-06-24 12:51:06 +0100617 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100618 ctx->operation, input,
619 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200621 }
622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200624 }
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100627 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
629 input, ilen,
630 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200631 }
632#endif
633
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200634#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100635 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
637 input, ilen,
638 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200639 }
640#endif
641
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200642#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100643 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200644 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
646 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200647 }
648#endif
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (input == output &&
651 (ctx->unprocessed_len != 0 || ilen % block_size)) {
652 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100653 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100656 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200657 size_t copy_len = 0;
658
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659 /*
660 * If there is not enough data for a full block, cache it.
661 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
663 ilen <= block_size - ctx->unprocessed_len) ||
664 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
665 ilen < block_size - ctx->unprocessed_len) ||
666 (ctx->operation == MBEDTLS_ENCRYPT &&
667 ilen < block_size - ctx->unprocessed_len)) {
668 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
669 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
671 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 }
674
675 /*
676 * Process cached data first
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100679 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
682 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683
Dave Rodgmande3de772023-06-24 12:51:06 +0100684 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100685 ctx->operation,
686 block_size, ctx->iv,
687 ctx->
688 unprocessed_data,
689 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691 }
692
Janos Follath98e28a72016-05-31 14:03:54 +0100693 *olen += block_size;
694 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695 ctx->unprocessed_len = 0;
696
697 input += copy_len;
698 ilen -= copy_len;
699 }
700
701 /*
702 * Cache final, incomplete block
703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700705 /* Encryption: only cache partial blocks
706 * Decryption w/ padding: always keep at least one whole block
707 * Decryption w/o padding: only cache partial blocks
708 */
Janos Follath98e28a72016-05-31 14:03:54 +0100709 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700711 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100713 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700714 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
717 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000718
719 ctx->unprocessed_len += copy_len;
720 ilen -= copy_len;
721 }
722
723 /*
724 * Process remaining full blocks
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100727 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100728 ctx->operation,
729 ilen, ctx->iv,
730 input,
731 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200734
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735 *olen += ilen;
736 }
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100743 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100744 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100745 ctx->operation, ilen,
746 &ctx->unprocessed_len,
747 ctx->iv,
748 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000750 }
751
752 *olen = ilen;
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000757
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100758#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100759 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100760 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100761 ilen,
762 &ctx->unprocessed_len,
763 ctx->iv,
764 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100766 }
767
768 *olen = ilen;
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100771 }
772#endif /* MBEDTLS_CIPHER_MODE_OFB */
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100775 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100776 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100777 ilen,
778 &ctx->unprocessed_len,
779 ctx->iv,
780 ctx->unprocessed_data,
781 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000783 }
784
785 *olen = ilen;
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000790
Jaeden Ameroc6539902018-04-30 17:17:41 +0100791#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100792 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100794 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100796 }
797
Dave Rodgmande3de772023-06-24 12:51:06 +0100798 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100799 ctx->operation,
800 ilen,
801 ctx->iv,
802 input,
803 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ret != 0) {
805 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100806 }
807
808 *olen = ilen;
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100811 }
812#endif /* MBEDTLS_CIPHER_MODE_XTS */
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100815 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100816 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100817 ilen, input,
818 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200820 }
821
822 *olen = ilen;
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200825 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000829}
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
832#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833/*
834 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
835 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static void add_pkcs_padding(unsigned char *output, size_t output_len,
837 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000838{
Paul Bakker23986e52011-04-24 08:57:21 +0000839 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100840 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000843 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845}
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847static int get_pkcs_padding(unsigned char *input, size_t input_len,
848 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000849{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100850 size_t i, pad_idx;
851 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (NULL == input || NULL == data_len) {
854 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
855 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000856
857 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858 *data_len = input_len - padding_len;
859
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100860 /* Avoid logical || since it results in a branch */
861 bad |= padding_len > input_len;
862 bad |= padding_len == 0;
863
864 /* The number of bytes checked must be independent of padding_len,
865 * so pick input_len, which is usually 8 or 16 (one block) */
866 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 for (i = 0; i < input_len; i++) {
868 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
869 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200876/*
877 * One and zeros padding: fill with 80 00 ... 00
878 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879static void add_one_and_zeros_padding(unsigned char *output,
880 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200881{
882 size_t padding_len = output_len - data_len;
883 unsigned char i = 0;
884
885 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200887 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200889}
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
892 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200893{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100894 size_t i;
895 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (NULL == input || NULL == data_len) {
898 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
899 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200900
Micha Krausba8316f2017-12-23 23:40:08 +0100901 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100902 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100904 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 done |= (input[i - 1] != 0);
906 *data_len |= (i - 1) * (done != prev_done);
907 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100908 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200911
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200916/*
917 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
918 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919static void add_zeros_and_len_padding(unsigned char *output,
920 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200921{
922 size_t padding_len = output_len - data_len;
923 unsigned char i = 0;
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200926 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200928 output[output_len - 1] = (unsigned char) padding_len;
929}
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
932 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200933{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100934 size_t i, pad_idx;
935 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (NULL == input || NULL == data_len) {
938 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
939 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200940
941 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200942 *data_len = input_len - padding_len;
943
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100944 /* Avoid logical || since it results in a branch */
945 bad |= padding_len > input_len;
946 bad |= padding_len == 0;
947
948 /* The number of bytes checked must be independent of padding_len */
949 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 for (i = 0; i < input_len - 1; i++) {
951 bad |= input[i] * (i >= pad_idx);
952 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200959/*
960 * Zero padding: fill with 00 ... 00
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static void add_zeros_padding(unsigned char *output,
963 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200964{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200965 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200968 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200970}
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972static int get_zeros_padding(unsigned char *input, size_t input_len,
973 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200974{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100975 size_t i;
976 unsigned char done = 0, prev_done;
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (NULL == input || NULL == data_len) {
979 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100980 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 *data_len = 0;
983 for (i = input_len; i > 0; i--) {
984 prev_done = done;
985 done |= (input[i-1] != 0);
986 *data_len |= i * (done != prev_done);
987 }
988
989 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200992
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200993/*
994 * No padding: don't pad :)
995 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200997 * but a trivial get_padding function
998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999static int get_no_padding(unsigned char *input, size_t input_len,
1000 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001001{
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (NULL == input || NULL == data_len) {
1003 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1004 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001005
1006 *data_len = input_len;
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1013 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014{
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (ctx->cipher_info == NULL) {
1016 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1017 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001018
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001019#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001021 /* While PSA Crypto has an API for multipart
1022 * operations, we currently don't make it
1023 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001025 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001026#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001027
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028 *olen = 0;
1029
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001030 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1031 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1032 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1033 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1034 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1035 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1036 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001038 }
1039
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001040 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1041 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001043 }
1044
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001045 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (ctx->unprocessed_len != 0) {
1047 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1048 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001051 }
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001054 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001055 int ret = 0;
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001058 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (NULL == ctx->add_padding) {
1060 if (0 != ctx->unprocessed_len) {
1061 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1062 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001065 }
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1068 ctx->unprocessed_len);
1069 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070 /*
1071 * For decrypt operations, expect a full block,
1072 * or an empty block if no padding
1073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1075 return 0;
1076 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001079 }
1080
1081 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001082 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001083 ctx->operation,
1084 mbedtls_cipher_get_block_size(
1085 ctx),
1086 ctx->iv,
1087 ctx->unprocessed_data,
1088 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001090 }
1091
1092 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (MBEDTLS_DECRYPT == ctx->operation) {
1094 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1095 olen);
1096 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001097
1098 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 *olen = mbedtls_cipher_get_block_size(ctx);
1100 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001101 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001102#else
1103 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001107}
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001110int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1111 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001112{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001113 if (NULL == ctx->cipher_info ||
1114 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001116 }
1117
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001118#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001120 /* While PSA Crypto knows about CBC padding
1121 * schemes, we currently don't make them
1122 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (mode != MBEDTLS_PADDING_NONE) {
1124 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1125 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001128 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001129#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 case MBEDTLS_PADDING_PKCS7:
1134 ctx->add_padding = add_pkcs_padding;
1135 ctx->get_padding = get_pkcs_padding;
1136 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1140 ctx->add_padding = add_one_and_zeros_padding;
1141 ctx->get_padding = get_one_and_zeros_padding;
1142 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1146 ctx->add_padding = add_zeros_and_len_padding;
1147 ctx->get_padding = get_zeros_and_len_padding;
1148 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 case MBEDTLS_PADDING_ZEROS:
1152 ctx->add_padding = add_zeros_padding;
1153 ctx->get_padding = get_zeros_padding;
1154 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001155#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 case MBEDTLS_PADDING_NONE:
1157 ctx->add_padding = NULL;
1158 ctx->get_padding = get_no_padding;
1159 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 default:
1162 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001163 }
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001168
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001169#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1171 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001172{
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (ctx->cipher_info == NULL) {
1174 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1175 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (MBEDTLS_ENCRYPT != ctx->operation) {
1178 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1179 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001180
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001181#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001183 /* While PSA Crypto has an API for multipart
1184 * operations, we currently don't make it
1185 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001187 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001188#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001189
Daniel King8fe47012016-05-17 20:33:28 -03001190#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001191 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001192 size_t output_length;
1193 /* The code here doesn't yet support alternative implementations
1194 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1196 NULL, 0, &output_length,
1197 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001198 }
Daniel King8fe47012016-05-17 20:33:28 -03001199#endif
1200
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001201#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001202 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001203 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (tag_len != 16U) {
1205 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1206 }
Daniel King8fe47012016-05-17 20:33:28 -03001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return mbedtls_chachapoly_finish(
1209 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001210 }
1211#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001214}
Paul Bakker9af723c2014-05-01 13:03:14 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1217 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001218{
Daniel King8fe47012016-05-17 20:33:28 -03001219 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (ctx->cipher_info == NULL) {
1223 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1224 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (MBEDTLS_DECRYPT != ctx->operation) {
1227 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001228 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001229
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001230#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001232 /* While PSA Crypto has an API for multipart
1233 * operations, we currently don't make it
1234 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001236 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001237#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001238
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001239 /* Status to return on a non-authenticated algorithm. */
1240 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001241
Daniel King8fe47012016-05-17 20:33:28 -03001242#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001243 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001244 size_t output_length;
1245 /* The code here doesn't yet support alternative implementations
1246 * that can delay up to a block of output. */
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (tag_len > sizeof(check_tag)) {
1249 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1250 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (0 != (ret = mbedtls_gcm_finish(
1253 (mbedtls_gcm_context *) ctx->cipher_ctx,
1254 NULL, 0, &output_length,
1255 check_tag, tag_len))) {
1256 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001257 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001258
1259 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001261 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001262 goto exit;
1263 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001264 }
Daniel King8fe47012016-05-17 20:33:28 -03001265#endif /* MBEDTLS_GCM_C */
1266
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001267#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001268 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001269 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (tag_len != sizeof(check_tag)) {
1271 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1272 }
Daniel King8fe47012016-05-17 20:33:28 -03001273
Hanno Becker18597cd2018-11-09 16:36:33 +00001274 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1276 if (ret != 0) {
1277 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001278 }
1279
1280 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001282 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001283 goto exit;
1284 }
Daniel King8fe47012016-05-17 20:33:28 -03001285 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001286#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001287
Gilles Peskinecd742982021-12-13 16:57:47 +01001288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_platform_zeroize(check_tag, tag_len);
1290 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001291}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001292#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001293
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001294/*
1295 * Packet-oriented wrapper for non-AEAD modes
1296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1298 const unsigned char *iv, size_t iv_len,
1299 const unsigned char *input, size_t ilen,
1300 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001301{
Janos Follath24eed8d2019-11-22 13:21:35 +00001302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001303 size_t finish_olen;
1304
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001305#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001307 /* As in the non-PSA case, we don't check that
1308 * a key has been set. If not, the key slot will
1309 * still be in its default state of 0, which is
1310 * guaranteed to be invalid, hence the PSA-call
1311 * below will gracefully fail. */
1312 mbedtls_cipher_context_psa * const cipher_psa =
1313 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1314
1315 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001316 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001317 size_t part_len;
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (ctx->operation == MBEDTLS_DECRYPT) {
1320 status = psa_cipher_decrypt_setup(&cipher_op,
1321 cipher_psa->slot,
1322 cipher_psa->alg);
1323 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1324 status = psa_cipher_encrypt_setup(&cipher_op,
1325 cipher_psa->slot,
1326 cipher_psa->alg);
1327 } else {
1328 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001329 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001330
1331 /* In the following, we can immediately return on an error,
1332 * because the PSA Crypto API guarantees that cipher operations
1333 * are terminated by unsuccessful calls to psa_cipher_update(),
1334 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (status != PSA_SUCCESS) {
1336 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001337 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001338
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001339 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1341 if (status != PSA_SUCCESS) {
1342 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1343 }
1344 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 status = psa_cipher_update(&cipher_op,
1347 input, ilen,
1348 output, ilen, olen);
1349 if (status != PSA_SUCCESS) {
1350 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1351 }
1352
1353 status = psa_cipher_finish(&cipher_op,
1354 output + *olen, ilen - *olen,
1355 &part_len);
1356 if (status != PSA_SUCCESS) {
1357 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1358 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001359
1360 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001362 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001363#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1366 return ret;
1367 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1370 return ret;
1371 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1374 output, olen)) != 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_finish(ctx, output + *olen,
1379 &finish_olen)) != 0) {
1380 return ret;
1381 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001382
1383 *olen += finish_olen;
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001386}
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001389/*
TRodziewicz18efb732021-04-29 23:12:19 +02001390 * Packet-oriented encryption for AEAD modes: internal function used by
1391 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1394 const unsigned char *iv, size_t iv_len,
1395 const unsigned char *ad, size_t ad_len,
1396 const unsigned char *input, size_t ilen,
1397 unsigned char *output, size_t *olen,
1398 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001399{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001400#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001402 /* As in the non-PSA case, we don't check that
1403 * a key has been set. If not, the key slot will
1404 * still be in its default state of 0, which is
1405 * guaranteed to be invalid, hence the PSA-call
1406 * below will gracefully fail. */
1407 mbedtls_cipher_context_psa * const cipher_psa =
1408 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1409
1410 psa_status_t status;
1411
1412 /* PSA Crypto API always writes the authentication tag
1413 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (output == NULL || tag != output + ilen) {
1415 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1416 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 status = psa_aead_encrypt(cipher_psa->slot,
1419 cipher_psa->alg,
1420 iv, iv_len,
1421 ad, ad_len,
1422 input, ilen,
1423 output, ilen + tag_len, olen);
1424 if (status != PSA_SUCCESS) {
1425 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1426 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001427
1428 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001430 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001431#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001434 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001435 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1437 ilen, iv, iv_len, ad, ad_len,
1438 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#endif /* MBEDTLS_GCM_C */
1441#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001442 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001443 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1445 iv, iv_len, ad, ad_len, input, output,
1446 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001449#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001450 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001451 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001452 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 (tag_len != 16U)) {
1454 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001455 }
1456
1457 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1459 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001460 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001461#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001464}
1465
1466/*
TRodziewicz18efb732021-04-29 23:12:19 +02001467 * Packet-oriented encryption for AEAD modes: internal function used by
1468 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1471 const unsigned char *iv, size_t iv_len,
1472 const unsigned char *ad, size_t ad_len,
1473 const unsigned char *input, size_t ilen,
1474 unsigned char *output, size_t *olen,
1475 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001476{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001477#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001479 /* As in the non-PSA case, we don't check that
1480 * a key has been set. If not, the key slot will
1481 * still be in its default state of 0, which is
1482 * guaranteed to be invalid, hence the PSA-call
1483 * below will gracefully fail. */
1484 mbedtls_cipher_context_psa * const cipher_psa =
1485 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1486
1487 psa_status_t status;
1488
1489 /* PSA Crypto API always writes the authentication tag
1490 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (input == NULL || tag != input + ilen) {
1492 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1493 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 status = psa_aead_decrypt(cipher_psa->slot,
1496 cipher_psa->alg,
1497 iv, iv_len,
1498 ad, ad_len,
1499 input, ilen + tag_len,
1500 output, ilen, olen);
1501 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1502 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1503 } else if (status != PSA_SUCCESS) {
1504 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1505 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001508 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001509#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001512 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001514
1515 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1517 iv, iv_len, ad, ad_len,
1518 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_GCM_C */
1527#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001528 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001530
1531 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1533 iv, iv_len, ad, ad_len,
1534 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001543#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001544 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001546
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001547 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001548 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 (tag_len != 16U)) {
1550 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001551 }
1552
1553 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1555 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001558 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 }
Daniel King8fe47012016-05-17 20:33:28 -03001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001562 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001563#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001568
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001569#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1570/*
1571 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1572 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1574 const unsigned char *iv, size_t iv_len,
1575 const unsigned char *ad, size_t ad_len,
1576 const unsigned char *input, size_t ilen,
1577 unsigned char *output, size_t output_len,
1578 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001580#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001582#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001583 ctx->psa_enabled == 0 &&
1584#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001585 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1586 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1587 mbedtls_nist_kw_mode_t mode =
1588 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1589 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001590
1591 /* There is no iv, tag or ad associated with KW and KWP,
1592 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1594 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1595 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001596
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001597 (void) iv;
1598 (void) ad;
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1601 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001602 }
1603#endif /* MBEDTLS_NIST_KW_C */
1604
1605#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1606 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (output_len < ilen + tag_len) {
1608 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1609 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1612 input, ilen, output, olen,
1613 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001614 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001618#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1619}
1620
1621/*
1622 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1625 const unsigned char *iv, size_t iv_len,
1626 const unsigned char *ad, size_t ad_len,
1627 const unsigned char *input, size_t ilen,
1628 unsigned char *output, size_t output_len,
1629 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001630{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001631#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001633#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001634 ctx->psa_enabled == 0 &&
1635#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001636 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1637 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1638 mbedtls_nist_kw_mode_t mode =
1639 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1640 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001641
1642 /* There is no iv, tag or ad associated with KW and KWP,
1643 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1645 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1646 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001647
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001648 (void) iv;
1649 (void) ad;
1650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1652 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001653 }
1654#endif /* MBEDTLS_NIST_KW_C */
1655
1656#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1657 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (ilen < tag_len || output_len < ilen - tag_len) {
1659 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1660 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1663 input, ilen - tag_len, output, olen,
1664 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001665#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001667#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1668}
1669#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#endif /* MBEDTLS_CIPHER_C */