blob: 490326a6b0bfc7f51d46d214c6186f7e08441066 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
235 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000236 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000239 return;
240 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200241#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242
Simon Butcher327398a2016-10-05 14:09:11 +0100243#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ctx->cmac_ctx) {
245 mbedtls_platform_zeroize(ctx->cmac_ctx,
246 sizeof(mbedtls_cmac_context_t));
247 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100248 }
249#endif
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100252 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200256}
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
259 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260{
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (cipher_info == NULL) {
262 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
263 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Dave Rodgmande3de772023-06-24 12:51:06 +0100267 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
269 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
271 ctx->cipher_info = cipher_info;
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200274 /*
275 * Ignore possible errors caused by a cipher mode that doesn't use padding
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200279#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285}
286
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200287#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
289 const mbedtls_cipher_info_t *cipher_info,
290 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000291{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000292 psa_algorithm_t alg;
293 mbedtls_cipher_context_psa *cipher_psa;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (NULL == cipher_info || NULL == ctx) {
296 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
297 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000298
Hanno Becker4ee7e762018-11-17 22:00:38 +0000299 /* Check that the underlying cipher mode and cipher type are
300 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100301 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (alg == 0) {
303 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
304 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100305 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
307 }
Hanno Becker6118e432018-11-09 16:47:20 +0000308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
312 if (cipher_psa == NULL) {
313 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
314 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000315 cipher_psa->alg = alg;
316 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000317 ctx->cipher_info = cipher_info;
318 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000320}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200321#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
324 const unsigned char *key,
325 int key_bitlen,
326 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327{
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100329 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
331 if (ctx->cipher_info == NULL) {
332 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
333 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200335#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000337 mbedtls_cipher_context_psa * const cipher_psa =
338 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000341
342 psa_status_t status;
343 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000345
346 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (key_bitlen % 8 != 0) {
348 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
349 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000350
351 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
353 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
354 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000355
Andrzej Kurekc7509322019-01-08 09:36:01 -0500356 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100357 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (key_type == 0) {
359 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
360 }
361 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000362
363 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500364 * (encrypt vs. decrypt): it is possible to setup a key for encryption
365 * and use it for AEAD decryption. Until tests relying on this
366 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
369 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 status = psa_import_key(&attributes, key, key_bytelen,
372 &cipher_psa->slot);
373 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200374 case PSA_SUCCESS:
375 break;
376 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200378 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200380 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200382 }
383 /* Indicate that we own the key slot and need to
384 * destroy it in mbedtls_cipher_free(). */
385 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000386
387 ctx->key_bitlen = key_bitlen;
388 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000390 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200391#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100394 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200396 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200397
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200398 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399 ctx->operation = operation;
400
Paul Bakker343a8702011-06-09 14:27:58 +0000401 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100402 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100405 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
406 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
407 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100408 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100409 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000410 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100413 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100414 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418}
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
421 const unsigned char *iv,
422 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200424 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (ctx->cipher_info == NULL) {
427 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
428 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200429#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000431 /* While PSA Crypto has an API for multipart
432 * operations, we currently don't make it
433 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000435 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200436#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000437
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200438 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
440 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
441 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200444 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100446 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200447
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200448 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (actual_iv_size > iv_len) {
450 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
451 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200452 }
453
Daniel Kingbd920622016-05-15 19:56:20 -0300454#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100455 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100456 /* Even though the actual_iv_size is overwritten with a correct value
457 * of 12 from the cipher info, return an error to indicate that
458 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (iv_len != 12) {
460 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
461 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
464 iv,
465 0U)) { /* Initial counter value */
466 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300467 }
468 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100469#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100470 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 iv_len != 12) {
472 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
473 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100474#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300475#endif
476
Gilles Peskine295fc132021-04-15 18:32:23 +0200477#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100478 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
480 ctx->operation,
481 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200482 }
483#endif
484
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200485#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100486 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200487 int set_lengths_result;
488 int ccm_star_mode;
489
490 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 (mbedtls_ccm_context *) ctx->cipher_ctx,
492 0, 0, 0);
493 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200494 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200498 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200500 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200502 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
506 ccm_star_mode,
507 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200508 }
509#endif
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (actual_iv_size != 0) {
512 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300513 ctx->iv_size = actual_iv_size;
514 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200517}
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200520{
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (ctx->cipher_info == NULL) {
522 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
523 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200524
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200525#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000527 /* We don't support resetting PSA-based
528 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000530 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200531#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000532
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533 ctx->unprocessed_len = 0;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200536}
537
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200538#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
540 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200541{
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (ctx->cipher_info == NULL) {
543 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
544 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200545
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200546#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000548 /* While PSA Crypto has an API for multipart
549 * operations, we currently don't make it
550 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000552 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200553#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000554
Daniel King8fe47012016-05-17 20:33:28 -0300555#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100556 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
558 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200559 }
Daniel King8fe47012016-05-17 20:33:28 -0300560#endif
561
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200562#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100563 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300564 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200565 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200568 ? MBEDTLS_CHACHAPOLY_ENCRYPT
569 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
572 ctx->iv,
573 mode);
574 if (result != 0) {
575 return result;
576 }
Daniel King8fe47012016-05-17 20:33:28 -0300577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
579 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300580 }
581#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200585#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
588 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589{
Janos Follath24eed8d2019-11-22 13:21:35 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500591 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (ctx->cipher_info == NULL) {
594 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
595 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200597#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000599 /* While PSA Crypto has an API for multipart
600 * operations, we currently don't make it
601 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000603 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200604#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000605
Paul Bakker6c212762013-12-16 15:24:50 +0100606 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 block_size = mbedtls_cipher_get_block_size(ctx);
608 if (0 == block_size) {
609 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100610 }
Paul Bakker6c212762013-12-16 15:24:50 +0100611
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100612 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (ilen != block_size) {
614 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
615 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200616
617 *olen = ilen;
618
Dave Rodgmande3de772023-06-24 12:51:06 +0100619 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100620 ctx->operation, input,
621 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200623 }
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200626 }
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100629 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
631 input, ilen,
632 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200633 }
634#endif
635
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200636#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100637 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
639 input, ilen,
640 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200641 }
642#endif
643
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200644#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100645 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200646 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
648 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200649 }
650#endif
651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (input == output &&
653 (ctx->unprocessed_len != 0 || ilen % block_size)) {
654 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100655 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100658 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200659 size_t copy_len = 0;
660
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 /*
662 * If there is not enough data for a full block, cache it.
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
665 ilen <= block_size - ctx->unprocessed_len) ||
666 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
667 ilen < block_size - ctx->unprocessed_len) ||
668 (ctx->operation == MBEDTLS_ENCRYPT &&
669 ilen < block_size - ctx->unprocessed_len)) {
670 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
671 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
673 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675 }
676
677 /*
678 * Process cached data first
679 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100681 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
684 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685
Dave Rodgmande3de772023-06-24 12:51:06 +0100686 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100687 ctx->operation,
688 block_size, ctx->iv,
689 ctx->
690 unprocessed_data,
691 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693 }
694
Janos Follath98e28a72016-05-31 14:03:54 +0100695 *olen += block_size;
696 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697 ctx->unprocessed_len = 0;
698
699 input += copy_len;
700 ilen -= copy_len;
701 }
702
703 /*
704 * Cache final, incomplete block
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700707 /* Encryption: only cache partial blocks
708 * Decryption w/ padding: always keep at least one whole block
709 * Decryption w/o padding: only cache partial blocks
710 */
Janos Follath98e28a72016-05-31 14:03:54 +0100711 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700713 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100715 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700716 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
719 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720
721 ctx->unprocessed_len += copy_len;
722 ilen -= copy_len;
723 }
724
725 /*
726 * Process remaining full blocks
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100729 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100730 ctx->operation,
731 ilen, ctx->iv,
732 input,
733 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200736
Paul Bakker8123e9d2011-01-06 15:37:30 +0000737 *olen += ilen;
738 }
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100745 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100746 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100747 ctx->operation, ilen,
748 &ctx->unprocessed_len,
749 ctx->iv,
750 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000752 }
753
754 *olen = ilen;
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000757 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000759
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100760#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100761 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100762 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100763 ilen,
764 &ctx->unprocessed_len,
765 ctx->iv,
766 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100768 }
769
770 *olen = ilen;
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100773 }
774#endif /* MBEDTLS_CIPHER_MODE_OFB */
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100777 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100778 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100779 ilen,
780 &ctx->unprocessed_len,
781 ctx->iv,
782 ctx->unprocessed_data,
783 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000785 }
786
787 *olen = ilen;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000792
Jaeden Ameroc6539902018-04-30 17:17:41 +0100793#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100794 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100796 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100798 }
799
Dave Rodgmande3de772023-06-24 12:51:06 +0100800 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100801 ctx->operation,
802 ilen,
803 ctx->iv,
804 input,
805 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (ret != 0) {
807 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100808 }
809
810 *olen = ilen;
811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100813 }
814#endif /* MBEDTLS_CIPHER_MODE_XTS */
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100817 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100818 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100819 ilen, input,
820 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200822 }
823
824 *olen = ilen;
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000831}
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
834#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200835/*
836 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
837 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static void add_pkcs_padding(unsigned char *output, size_t output_len,
839 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000840{
Paul Bakker23986e52011-04-24 08:57:21 +0000841 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100842 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000845 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847}
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849static int get_pkcs_padding(unsigned char *input, size_t input_len,
850 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100852 size_t i, pad_idx;
853 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (NULL == input || NULL == data_len) {
856 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
857 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858
859 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000860 *data_len = input_len - padding_len;
861
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100862 /* Avoid logical || since it results in a branch */
863 bad |= padding_len > input_len;
864 bad |= padding_len == 0;
865
866 /* The number of bytes checked must be independent of padding_len,
867 * so pick input_len, which is usually 8 or 16 (one block) */
868 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 for (i = 0; i < input_len; i++) {
870 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
871 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200878/*
879 * One and zeros padding: fill with 80 00 ... 00
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881static void add_one_and_zeros_padding(unsigned char *output,
882 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200883{
884 size_t padding_len = output_len - data_len;
885 unsigned char i = 0;
886
887 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200889 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200891}
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
894 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200895{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100896 size_t i;
897 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (NULL == input || NULL == data_len) {
900 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
901 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200902
Micha Krausba8316f2017-12-23 23:40:08 +0100903 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100904 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100906 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 done |= (input[i - 1] != 0);
908 *data_len |= (i - 1) * (done != prev_done);
909 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100910 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200913
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200918/*
919 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921static void add_zeros_and_len_padding(unsigned char *output,
922 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200923{
924 size_t padding_len = output_len - data_len;
925 unsigned char i = 0;
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200928 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200930 output[output_len - 1] = (unsigned char) padding_len;
931}
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
934 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200935{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100936 size_t i, pad_idx;
937 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (NULL == input || NULL == data_len) {
940 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
941 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200942
943 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200944 *data_len = input_len - padding_len;
945
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100946 /* Avoid logical || since it results in a branch */
947 bad |= padding_len > input_len;
948 bad |= padding_len == 0;
949
950 /* The number of bytes checked must be independent of padding_len */
951 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 for (i = 0; i < input_len - 1; i++) {
953 bad |= input[i] * (i >= pad_idx);
954 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200961/*
962 * Zero padding: fill with 00 ... 00
963 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964static void add_zeros_padding(unsigned char *output,
965 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200966{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200967 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200970 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200972}
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974static int get_zeros_padding(unsigned char *input, size_t input_len,
975 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200976{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100977 size_t i;
978 unsigned char done = 0, prev_done;
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (NULL == input || NULL == data_len) {
981 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100982 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 *data_len = 0;
985 for (i = input_len; i > 0; i--) {
986 prev_done = done;
987 done |= (input[i-1] != 0);
988 *data_len |= i * (done != prev_done);
989 }
990
991 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200994
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200995/*
996 * No padding: don't pad :)
997 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200999 * but a trivial get_padding function
1000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001static int get_no_padding(unsigned char *input, size_t input_len,
1002 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (NULL == input || NULL == data_len) {
1005 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1006 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001007
1008 *data_len = input_len;
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1015 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001016{
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (ctx->cipher_info == NULL) {
1018 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1019 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001021#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001023 /* While PSA Crypto has an API for multipart
1024 * operations, we currently don't make it
1025 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001027 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001028#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001029
Paul Bakker8123e9d2011-01-06 15:37:30 +00001030 *olen = 0;
1031
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001032 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1033 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1034 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1035 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1036 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1037 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1038 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001040 }
1041
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001042 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1043 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001045 }
1046
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001047 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ctx->unprocessed_len != 0) {
1049 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1050 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001053 }
1054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001056 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001057 int ret = 0;
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001060 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (NULL == ctx->add_padding) {
1062 if (0 != ctx->unprocessed_len) {
1063 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1064 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001067 }
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1070 ctx->unprocessed_len);
1071 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001072 /*
1073 * For decrypt operations, expect a full block,
1074 * or an empty block if no padding
1075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1077 return 0;
1078 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001081 }
1082
1083 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001084 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001085 ctx->operation,
1086 mbedtls_cipher_get_block_size(
1087 ctx),
1088 ctx->iv,
1089 ctx->unprocessed_data,
1090 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001092 }
1093
1094 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (MBEDTLS_DECRYPT == ctx->operation) {
1096 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1097 olen);
1098 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001099
1100 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 *olen = mbedtls_cipher_get_block_size(ctx);
1102 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001103 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001104#else
1105 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001109}
1110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1113 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001114{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001115 if (NULL == ctx->cipher_info ||
1116 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001118 }
1119
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001120#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001122 /* While PSA Crypto knows about CBC padding
1123 * schemes, we currently don't make them
1124 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (mode != MBEDTLS_PADDING_NONE) {
1126 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1127 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001130 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001131#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 case MBEDTLS_PADDING_PKCS7:
1136 ctx->add_padding = add_pkcs_padding;
1137 ctx->get_padding = get_pkcs_padding;
1138 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001139#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1142 ctx->add_padding = add_one_and_zeros_padding;
1143 ctx->get_padding = get_one_and_zeros_padding;
1144 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1148 ctx->add_padding = add_zeros_and_len_padding;
1149 ctx->get_padding = get_zeros_and_len_padding;
1150 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 case MBEDTLS_PADDING_ZEROS:
1154 ctx->add_padding = add_zeros_padding;
1155 ctx->get_padding = get_zeros_padding;
1156 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001157#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 case MBEDTLS_PADDING_NONE:
1159 ctx->add_padding = NULL;
1160 ctx->get_padding = get_no_padding;
1161 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 default:
1164 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001170
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001171#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001172int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1173 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001174{
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (ctx->cipher_info == NULL) {
1176 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1177 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (MBEDTLS_ENCRYPT != ctx->operation) {
1180 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1181 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001183#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001185 /* While PSA Crypto has an API for multipart
1186 * operations, we currently don't make it
1187 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001189 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001190#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001191
Daniel King8fe47012016-05-17 20:33:28 -03001192#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001193 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001194 size_t output_length;
1195 /* The code here doesn't yet support alternative implementations
1196 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1198 NULL, 0, &output_length,
1199 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001200 }
Daniel King8fe47012016-05-17 20:33:28 -03001201#endif
1202
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001203#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001204 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001205 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (tag_len != 16U) {
1207 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1208 }
Daniel King8fe47012016-05-17 20:33:28 -03001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 return mbedtls_chachapoly_finish(
1211 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001212 }
1213#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001216}
Paul Bakker9af723c2014-05-01 13:03:14 +02001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1219 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001220{
Daniel King8fe47012016-05-17 20:33:28 -03001221 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (ctx->cipher_info == NULL) {
1225 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1226 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (MBEDTLS_DECRYPT != ctx->operation) {
1229 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001230 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001231
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001232#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001234 /* While PSA Crypto has an API for multipart
1235 * operations, we currently don't make it
1236 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001238 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001239#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001240
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001241 /* Status to return on a non-authenticated algorithm. */
1242 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001243
Daniel King8fe47012016-05-17 20:33:28 -03001244#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001245 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001246 size_t output_length;
1247 /* The code here doesn't yet support alternative implementations
1248 * that can delay up to a block of output. */
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (tag_len > sizeof(check_tag)) {
1251 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1252 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (0 != (ret = mbedtls_gcm_finish(
1255 (mbedtls_gcm_context *) ctx->cipher_ctx,
1256 NULL, 0, &output_length,
1257 check_tag, tag_len))) {
1258 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001259 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001260
1261 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001263 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001264 goto exit;
1265 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001266 }
Daniel King8fe47012016-05-17 20:33:28 -03001267#endif /* MBEDTLS_GCM_C */
1268
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001269#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001270 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001271 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (tag_len != sizeof(check_tag)) {
1273 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1274 }
Daniel King8fe47012016-05-17 20:33:28 -03001275
Hanno Becker18597cd2018-11-09 16:36:33 +00001276 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1278 if (ret != 0) {
1279 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001280 }
1281
1282 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001284 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001285 goto exit;
1286 }
Daniel King8fe47012016-05-17 20:33:28 -03001287 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001288#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001289
Gilles Peskinecd742982021-12-13 16:57:47 +01001290exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 mbedtls_platform_zeroize(check_tag, tag_len);
1292 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001293}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001294#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001295
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001296/*
1297 * Packet-oriented wrapper for non-AEAD modes
1298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1300 const unsigned char *iv, size_t iv_len,
1301 const unsigned char *input, size_t ilen,
1302 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001303{
Janos Follath24eed8d2019-11-22 13:21:35 +00001304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001305 size_t finish_olen;
1306
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001307#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001309 /* As in the non-PSA case, we don't check that
1310 * a key has been set. If not, the key slot will
1311 * still be in its default state of 0, which is
1312 * guaranteed to be invalid, hence the PSA-call
1313 * below will gracefully fail. */
1314 mbedtls_cipher_context_psa * const cipher_psa =
1315 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1316
1317 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001318 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001319 size_t part_len;
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (ctx->operation == MBEDTLS_DECRYPT) {
1322 status = psa_cipher_decrypt_setup(&cipher_op,
1323 cipher_psa->slot,
1324 cipher_psa->alg);
1325 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1326 status = psa_cipher_encrypt_setup(&cipher_op,
1327 cipher_psa->slot,
1328 cipher_psa->alg);
1329 } else {
1330 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001331 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001332
1333 /* In the following, we can immediately return on an error,
1334 * because the PSA Crypto API guarantees that cipher operations
1335 * are terminated by unsuccessful calls to psa_cipher_update(),
1336 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (status != PSA_SUCCESS) {
1338 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001339 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001340
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001341 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1343 if (status != PSA_SUCCESS) {
1344 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1345 }
1346 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 status = psa_cipher_update(&cipher_op,
1349 input, ilen,
1350 output, ilen, olen);
1351 if (status != PSA_SUCCESS) {
1352 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1353 }
1354
1355 status = psa_cipher_finish(&cipher_op,
1356 output + *olen, ilen - *olen,
1357 &part_len);
1358 if (status != PSA_SUCCESS) {
1359 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1360 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001361
1362 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001364 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001365#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1368 return ret;
1369 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1372 return ret;
1373 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1376 output, olen)) != 0) {
1377 return ret;
1378 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1381 &finish_olen)) != 0) {
1382 return ret;
1383 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001384
1385 *olen += finish_olen;
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001388}
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001391/*
TRodziewicz18efb732021-04-29 23:12:19 +02001392 * Packet-oriented encryption for AEAD modes: internal function used by
1393 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1396 const unsigned char *iv, size_t iv_len,
1397 const unsigned char *ad, size_t ad_len,
1398 const unsigned char *input, size_t ilen,
1399 unsigned char *output, size_t *olen,
1400 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001402#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001404 /* As in the non-PSA case, we don't check that
1405 * a key has been set. If not, the key slot will
1406 * still be in its default state of 0, which is
1407 * guaranteed to be invalid, hence the PSA-call
1408 * below will gracefully fail. */
1409 mbedtls_cipher_context_psa * const cipher_psa =
1410 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1411
1412 psa_status_t status;
1413
1414 /* PSA Crypto API always writes the authentication tag
1415 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (output == NULL || tag != output + ilen) {
1417 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1418 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 status = psa_aead_encrypt(cipher_psa->slot,
1421 cipher_psa->alg,
1422 iv, iv_len,
1423 ad, ad_len,
1424 input, ilen,
1425 output, ilen + tag_len, olen);
1426 if (status != PSA_SUCCESS) {
1427 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1428 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001429
1430 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001432 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001433#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001436 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001437 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1439 ilen, iv, iv_len, ad, ad_len,
1440 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_GCM_C */
1443#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001444 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001445 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1447 iv, iv_len, ad, ad_len, input, output,
1448 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001451#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001452 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001453 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001454 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 (tag_len != 16U)) {
1456 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001457 }
1458
1459 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1461 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001462 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001463#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001466}
1467
1468/*
TRodziewicz18efb732021-04-29 23:12:19 +02001469 * Packet-oriented encryption for AEAD modes: internal function used by
1470 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1473 const unsigned char *iv, size_t iv_len,
1474 const unsigned char *ad, size_t ad_len,
1475 const unsigned char *input, size_t ilen,
1476 unsigned char *output, size_t *olen,
1477 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001478{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001479#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001481 /* As in the non-PSA case, we don't check that
1482 * a key has been set. If not, the key slot will
1483 * still be in its default state of 0, which is
1484 * guaranteed to be invalid, hence the PSA-call
1485 * below will gracefully fail. */
1486 mbedtls_cipher_context_psa * const cipher_psa =
1487 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1488
1489 psa_status_t status;
1490
1491 /* PSA Crypto API always writes the authentication tag
1492 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (input == NULL || tag != input + ilen) {
1494 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1495 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 status = psa_aead_decrypt(cipher_psa->slot,
1498 cipher_psa->alg,
1499 iv, iv_len,
1500 ad, ad_len,
1501 input, ilen + tag_len,
1502 output, ilen, olen);
1503 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1504 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1505 } else if (status != PSA_SUCCESS) {
1506 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1507 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001510 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001511#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001514 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001516
1517 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1519 iv, iv_len, ad, ad_len,
1520 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_GCM_C */
1529#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001530 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001532
1533 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1535 iv, iv_len, ad, ad_len,
1536 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001545#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001546 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001548
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001549 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001550 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 (tag_len != 16U)) {
1552 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001553 }
1554
1555 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1557 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001560 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 }
Daniel King8fe47012016-05-17 20:33:28 -03001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001564 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001565#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001570
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001571#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1572/*
1573 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1576 const unsigned char *iv, size_t iv_len,
1577 const unsigned char *ad, size_t ad_len,
1578 const unsigned char *input, size_t ilen,
1579 unsigned char *output, size_t output_len,
1580 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001581{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001582#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001584#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001585 ctx->psa_enabled == 0 &&
1586#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001587 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1588 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1589 mbedtls_nist_kw_mode_t mode =
1590 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1591 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001592
1593 /* There is no iv, tag or ad associated with KW and KWP,
1594 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1596 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1597 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001598
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001599 (void) iv;
1600 (void) ad;
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1603 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001604 }
1605#endif /* MBEDTLS_NIST_KW_C */
1606
1607#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1608 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (output_len < ilen + tag_len) {
1610 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1611 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1614 input, ilen, output, olen,
1615 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001618#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001620#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1621}
1622
1623/*
1624 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1627 const unsigned char *iv, size_t iv_len,
1628 const unsigned char *ad, size_t ad_len,
1629 const unsigned char *input, size_t ilen,
1630 unsigned char *output, size_t output_len,
1631 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001632{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001633#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001635#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001636 ctx->psa_enabled == 0 &&
1637#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001638 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1639 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1640 mbedtls_nist_kw_mode_t mode =
1641 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1642 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001643
1644 /* There is no iv, tag or ad associated with KW and KWP,
1645 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1647 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1648 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001649
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001650 (void) iv;
1651 (void) ad;
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1654 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001655 }
1656#endif /* MBEDTLS_NIST_KW_C */
1657
1658#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1659 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (ilen < tag_len || output_len < ilen - tag_len) {
1661 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1662 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1665 input, ilen - tag_len, output, olen,
1666 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001667#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001669#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1670}
1671#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_CIPHER_C */