blob: 995d3d2283ea298bd13589cf95853d7af39e1205 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/cipher.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000029#include "cipher_wrap.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020032#include "mbedtls/constant_time.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stdlib.h>
35#include <string.h>
36
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
48
Daniel Kingbd920622016-05-15 19:56:20 -030049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Simon Butcher327398a2016-10-05 14:09:11 +010053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020057#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4ccfc402018-11-09 16:10:57 +000058#include "psa/crypto.h"
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +020059#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +000060
Jack Lloydffdf2882019-03-07 17:00:32 -050061#if defined(MBEDTLS_NIST_KW_C)
62#include "mbedtls/nist_kw.h"
63#endif
64
Simon Butcher327398a2016-10-05 14:09:11 +010065#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010066
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000068
Dave Rodgman3b46b772023-06-24 13:25:06 +010069static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
70 const mbedtls_cipher_info_t *info)
71{
Dave Rodgmande3de772023-06-24 12:51:06 +010072 return mbedtls_cipher_base_lookup_table[info->base_idx];
73}
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020078 int *type;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 def = mbedtls_cipher_definitions;
82 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 *type++ = (*def++).type;
Gilles Peskine449bd832023-01-11 14:50:10 +010086 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020087
88 *type = 0;
89
90 supported_init = 1;
91 }
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000094}
95
Hanno Becker18597cd2018-11-09 16:36:33 +000096const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010097 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
102 if (def->type == cipher_type) {
103 return def->info;
104 }
105 }
Paul Bakker343a8702011-06-09 14:27:58 +0000106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108}
109
Hanno Becker18597cd2018-11-09 16:36:33 +0000110const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 if (NULL == cipher_name) {
116 return NULL;
117 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
120 if (!strcmp(def->info->name, cipher_name)) {
121 return def->info;
122 }
123 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126}
127
Hanno Becker18597cd2018-11-09 16:36:33 +0000128const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
129 const mbedtls_cipher_id_t cipher_id,
130 int key_bitlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100136 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100137 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 def->info->mode == mode) {
139 return def->info;
140 }
141 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144}
145
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200146#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
147static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
148 mbedtls_cipher_type_t cipher)
149{
150 switch (cipher) {
151 case MBEDTLS_CIPHER_AES_128_CCM:
152 case MBEDTLS_CIPHER_AES_192_CCM:
153 case MBEDTLS_CIPHER_AES_256_CCM:
154 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
155 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
156 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
157 case MBEDTLS_CIPHER_AES_128_GCM:
158 case MBEDTLS_CIPHER_AES_192_GCM:
159 case MBEDTLS_CIPHER_AES_256_GCM:
160 case MBEDTLS_CIPHER_AES_128_CBC:
161 case MBEDTLS_CIPHER_AES_192_CBC:
162 case MBEDTLS_CIPHER_AES_256_CBC:
163 case MBEDTLS_CIPHER_AES_128_ECB:
164 case MBEDTLS_CIPHER_AES_192_ECB:
165 case MBEDTLS_CIPHER_AES_256_ECB:
166 return PSA_KEY_TYPE_AES;
167
168 /* ARIA not yet supported in PSA. */
169 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
170 case MBEDTLS_CIPHER_ARIA_192_CCM:
171 case MBEDTLS_CIPHER_ARIA_256_CCM:
172 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
173 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
174 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
175 case MBEDTLS_CIPHER_ARIA_128_GCM:
176 case MBEDTLS_CIPHER_ARIA_192_GCM:
177 case MBEDTLS_CIPHER_ARIA_256_GCM:
178 case MBEDTLS_CIPHER_ARIA_128_CBC:
179 case MBEDTLS_CIPHER_ARIA_192_CBC:
180 case MBEDTLS_CIPHER_ARIA_256_CBC:
181 return( PSA_KEY_TYPE_ARIA ); */
182
183 default:
184 return 0;
185 }
186}
187
188static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
189 mbedtls_cipher_mode_t mode, size_t taglen)
190{
191 switch (mode) {
192 case MBEDTLS_MODE_ECB:
193 return PSA_ALG_ECB_NO_PADDING;
194 case MBEDTLS_MODE_GCM:
195 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
196 case MBEDTLS_MODE_CCM:
197 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
198 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
199 return PSA_ALG_CCM_STAR_NO_TAG;
200 case MBEDTLS_MODE_CBC:
201 if (taglen == 0) {
202 return PSA_ALG_CBC_NO_PADDING;
203 } else {
204 return 0;
205 }
206 default:
207 return 0;
208 }
209}
Manuel Pégourié-Gonnardefcc1f22023-06-07 13:20:24 +0200210#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200213{
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200215}
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200220 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200222
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200223#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (ctx->psa_enabled == 1) {
225 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000226 mbedtls_cipher_context_psa * const cipher_psa =
227 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000230 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000232 }
233
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100234 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Hanno Becker6118e432018-11-09 16:47:20 +0000235 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000238 return;
239 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200240#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000241
Simon Butcher327398a2016-10-05 14:09:11 +0100242#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ctx->cmac_ctx) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100244 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 sizeof(mbedtls_cmac_context_t));
Simon Butcher327398a2016-10-05 14:09:11 +0100246 }
247#endif
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ctx->cipher_ctx) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100250 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200254}
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
257 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (cipher_info == NULL) {
260 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
261 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Dave Rodgmande3de772023-06-24 12:51:06 +0100265 if (NULL == (ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func())) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
267 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268
269 ctx->cipher_info = cipher_info;
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272}
273
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200274#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
276 const mbedtls_cipher_info_t *cipher_info,
277 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000278{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000279 psa_algorithm_t alg;
280 mbedtls_cipher_context_psa *cipher_psa;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (NULL == cipher_info || NULL == ctx) {
283 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
284 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000285
Hanno Becker4ee7e762018-11-17 22:00:38 +0000286 /* Check that the underlying cipher mode and cipher type are
287 * supported by the underlying PSA Crypto implementation. */
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100288 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (alg == 0) {
290 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
291 }
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100292 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
294 }
Hanno Becker6118e432018-11-09 16:47:20 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
299 if (cipher_psa == NULL) {
300 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
301 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000302 cipher_psa->alg = alg;
303 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000304 ctx->cipher_info = cipher_info;
305 ctx->psa_enabled = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000307}
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200308#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker4ccfc402018-11-09 16:10:57 +0000309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
311 const unsigned char *key,
312 int key_bitlen,
313 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314{
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
Tuvshinzaya Erdenekhuu80a6af62022-08-05 15:31:57 +0100316 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 }
318 if (ctx->cipher_info == NULL) {
319 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
320 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200322#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000324 mbedtls_cipher_context_psa * const cipher_psa =
325 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000328
329 psa_status_t status;
330 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000332
333 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (key_bitlen % 8 != 0) {
335 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
336 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000337
338 /* Don't allow keys to be set multiple times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
340 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
341 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000342
Andrzej Kurekc7509322019-01-08 09:36:01 -0500343 key_type = mbedtls_psa_translate_cipher_type(
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100344 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (key_type == 0) {
346 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
347 }
348 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000349
350 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500351 * (encrypt vs. decrypt): it is possible to setup a key for encryption
352 * and use it for AEAD decryption. Until tests relying on this
353 * are changed, allow any usage in PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 psa_set_key_usage_flags(&attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
356 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 status = psa_import_key(&attributes, key, key_bytelen,
359 &cipher_psa->slot);
360 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200361 case PSA_SUCCESS:
362 break;
363 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200365 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200367 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200369 }
370 /* Indicate that we own the key slot and need to
371 * destroy it in mbedtls_cipher_free(). */
372 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000373
374 ctx->key_bitlen = key_bitlen;
375 ctx->operation = operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000377 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200378#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100381 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200383 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200384
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200385 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386 ctx->operation = operation;
387
Paul Bakker343a8702011-06-09 14:27:58 +0000388 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100389 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000390 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (MBEDTLS_ENCRYPT == operation ||
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100392 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
393 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
394 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100395 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100396 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000397 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (MBEDTLS_DECRYPT == operation) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100400 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100401 ctx->key_bitlen);
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405}
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
408 const unsigned char *iv,
409 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200411 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (ctx->cipher_info == NULL) {
414 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
415 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200416#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000418 /* While PSA Crypto has an API for multipart
419 * operations, we currently don't make it
420 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000422 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200423#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000424
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200425 /* avoid buffer overflow in ctx->iv */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
427 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
428 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200431 actual_iv_size = iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 } else {
Dave Rodgmanbb521fd2023-06-24 11:21:25 +0100433 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200434
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200435 /* avoid reading past the end of input buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (actual_iv_size > iv_len) {
437 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
438 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200439 }
440
Daniel Kingbd920622016-05-15 19:56:20 -0300441#if defined(MBEDTLS_CHACHA20_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100442 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100443 /* Even though the actual_iv_size is overwritten with a correct value
444 * of 12 from the cipher info, return an error to indicate that
445 * the input iv_len is wrong. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (iv_len != 12) {
447 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
448 }
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
451 iv,
452 0U)) { /* Initial counter value */
453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300454 }
455 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100456#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100457 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 iv_len != 12) {
459 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
460 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100461#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300462#endif
463
Gilles Peskine295fc132021-04-15 18:32:23 +0200464#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100465 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
467 ctx->operation,
468 iv, iv_len);
Gilles Peskine295fc132021-04-15 18:32:23 +0200469 }
470#endif
471
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200472#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100473 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200474 int set_lengths_result;
475 int ccm_star_mode;
476
477 set_lengths_result = mbedtls_ccm_set_lengths(
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 (mbedtls_ccm_context *) ctx->cipher_ctx,
479 0, 0, 0);
480 if (set_lengths_result != 0) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200481 return set_lengths_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (ctx->operation == MBEDTLS_DECRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200485 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200487 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else {
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200489 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 }
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
493 ccm_star_mode,
494 iv, iv_len);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200495 }
496#endif
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (actual_iv_size != 0) {
499 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300500 ctx->iv_size = actual_iv_size;
501 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (ctx->cipher_info == NULL) {
509 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
510 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200511
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200512#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000514 /* We don't support resetting PSA-based
515 * cipher contexts, yet. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000517 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200518#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000519
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520 ctx->unprocessed_len = 0;
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200523}
524
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200525#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
527 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200528{
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (ctx->cipher_info == NULL) {
530 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
531 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200532
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200533#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000535 /* While PSA Crypto has an API for multipart
536 * operations, we currently don't make it
537 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000539 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200540#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000541
Daniel King8fe47012016-05-17 20:33:28 -0300542#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100543 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
545 ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200546 }
Daniel King8fe47012016-05-17 20:33:28 -0300547#endif
548
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200549#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100550 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -0300551 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200552 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200555 ? MBEDTLS_CHACHAPOLY_ENCRYPT
556 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
559 ctx->iv,
560 mode);
561 if (result != 0) {
562 return result;
563 }
Daniel King8fe47012016-05-17 20:33:28 -0300564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
566 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300567 }
568#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200572#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
575 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576{
Janos Follath24eed8d2019-11-22 13:21:35 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500578 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (ctx->cipher_info == NULL) {
581 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
582 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200584#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000586 /* While PSA Crypto has an API for multipart
587 * operations, we currently don't make it
588 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000590 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +0200591#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000592
Paul Bakker6c212762013-12-16 15:24:50 +0100593 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 block_size = mbedtls_cipher_get_block_size(ctx);
595 if (0 == block_size) {
596 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100597 }
Paul Bakker6c212762013-12-16 15:24:50 +0100598
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100599 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (ilen != block_size) {
601 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
602 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200603
604 *olen = ilen;
605
Dave Rodgmande3de772023-06-24 12:51:06 +0100606 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100607 ctx->operation, input,
608 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200610 }
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100616 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
618 input, ilen,
619 output, ilen, olen);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200620 }
621#endif
622
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200623#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100624 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
626 input, ilen,
627 output, ilen, olen);
Mateusz Starzyk594215b2021-10-14 12:23:06 +0200628 }
629#endif
630
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200631#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100632 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200633 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
635 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200636 }
637#endif
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (input == output &&
640 (ctx->unprocessed_len != 0 || ilen % block_size)) {
641 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100642 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100645 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200646 size_t copy_len = 0;
647
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648 /*
649 * If there is not enough data for a full block, cache it.
650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
652 ilen <= block_size - ctx->unprocessed_len) ||
653 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
654 ilen < block_size - ctx->unprocessed_len) ||
655 (ctx->operation == MBEDTLS_ENCRYPT &&
656 ilen < block_size - ctx->unprocessed_len)) {
657 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
658 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
660 ctx->unprocessed_len += ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 }
663
664 /*
665 * Process cached data first
666 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100668 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
671 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
Dave Rodgmande3de772023-06-24 12:51:06 +0100673 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100674 ctx->operation,
675 block_size, ctx->iv,
676 ctx->
677 unprocessed_data,
678 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680 }
681
Janos Follath98e28a72016-05-31 14:03:54 +0100682 *olen += block_size;
683 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684 ctx->unprocessed_len = 0;
685
686 input += copy_len;
687 ilen -= copy_len;
688 }
689
690 /*
691 * Cache final, incomplete block
692 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700694 /* Encryption: only cache partial blocks
695 * Decryption w/ padding: always keep at least one whole block
696 * Decryption w/o padding: only cache partial blocks
697 */
Janos Follath98e28a72016-05-31 14:03:54 +0100698 copy_len = ilen % block_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700700 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100702 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700703 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
706 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000707
708 ctx->unprocessed_len += copy_len;
709 ilen -= copy_len;
710 }
711
712 /*
713 * Process remaining full blocks
714 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (ilen) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100716 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100717 ctx->operation,
718 ilen, ctx->iv,
719 input,
720 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200723
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724 *olen += ilen;
725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_CIPHER_MODE_CFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100732 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100733 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100734 ctx->operation, ilen,
735 &ctx->unprocessed_len,
736 ctx->iv,
737 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000739 }
740
741 *olen = ilen;
742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000744 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000746
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100747#if defined(MBEDTLS_CIPHER_MODE_OFB)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100748 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100749 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100750 ilen,
751 &ctx->unprocessed_len,
752 ctx->iv,
753 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100755 }
756
757 *olen = ilen;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100760 }
761#endif /* MBEDTLS_CIPHER_MODE_OFB */
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_CIPHER_MODE_CTR)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100764 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100765 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100766 ilen,
767 &ctx->unprocessed_len,
768 ctx->iv,
769 ctx->unprocessed_data,
770 input, output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000772 }
773
774 *olen = ilen;
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000777 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000779
Jaeden Ameroc6539902018-04-30 17:17:41 +0100780#if defined(MBEDTLS_CIPHER_MODE_XTS)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100781 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100783 /* We can only process an entire data unit at a time. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100785 }
786
Dave Rodgmande3de772023-06-24 12:51:06 +0100787 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100788 ctx->operation,
789 ilen,
790 ctx->iv,
791 input,
792 output);
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ret != 0) {
794 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100795 }
796
797 *olen = ilen;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100800 }
801#endif /* MBEDTLS_CIPHER_MODE_XTS */
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +0100804 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
Dave Rodgmande3de772023-06-24 12:51:06 +0100805 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +0100806 ilen, input,
807 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200809 }
810
811 *olen = ilen;
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000818}
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
821#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200822/*
823 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
824 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825static void add_pkcs_padding(unsigned char *output, size_t output_len,
826 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827{
Paul Bakker23986e52011-04-24 08:57:21 +0000828 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100829 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000832 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834}
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static int get_pkcs_padding(unsigned char *input, size_t input_len,
837 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000838{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100839 size_t i, pad_idx;
840 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (NULL == input || NULL == data_len) {
843 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
844 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845
846 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847 *data_len = input_len - padding_len;
848
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100849 /* Avoid logical || since it results in a branch */
850 bad |= padding_len > input_len;
851 bad |= padding_len == 0;
852
853 /* The number of bytes checked must be independent of padding_len,
854 * so pick input_len, which is usually 8 or 16 (one block) */
855 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 for (i = 0; i < input_len; i++) {
857 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
858 }
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200865/*
866 * One and zeros padding: fill with 80 00 ... 00
867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static void add_one_and_zeros_padding(unsigned char *output,
869 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200870{
871 size_t padding_len = output_len - data_len;
872 unsigned char i = 0;
873
874 output[data_len] = 0x80;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200876 output[data_len + i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200878}
879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
881 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200882{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100883 size_t i;
884 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (NULL == input || NULL == data_len) {
887 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
888 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200889
Micha Krausba8316f2017-12-23 23:40:08 +0100890 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100891 *data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 for (i = input_len; i > 0; i--) {
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100893 prev_done = done;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 done |= (input[i - 1] != 0);
895 *data_len |= (i - 1) * (done != prev_done);
896 bad ^= input[i - 1] * (done != prev_done);
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100897 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200900
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200905/*
906 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908static void add_zeros_and_len_padding(unsigned char *output,
909 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200910{
911 size_t padding_len = output_len - data_len;
912 unsigned char i = 0;
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200915 output[data_len + i - 1] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200917 output[output_len - 1] = (unsigned char) padding_len;
918}
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
921 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200922{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100923 size_t i, pad_idx;
924 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (NULL == input || NULL == data_len) {
927 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
928 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200929
930 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200931 *data_len = input_len - padding_len;
932
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100933 /* Avoid logical || since it results in a branch */
934 bad |= padding_len > input_len;
935 bad |= padding_len == 0;
936
937 /* The number of bytes checked must be independent of padding_len */
938 pad_idx = input_len - padding_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 for (i = 0; i < input_len - 1; i++) {
940 bad |= input[i] * (i >= pad_idx);
941 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200948/*
949 * Zero padding: fill with 00 ... 00
950 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951static void add_zeros_padding(unsigned char *output,
952 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200953{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200954 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200957 output[i] = 0x00;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200959}
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961static int get_zeros_padding(unsigned char *input, size_t input_len,
962 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200963{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100964 size_t i;
965 unsigned char done = 0, prev_done;
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (NULL == input || NULL == data_len) {
968 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100969 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 *data_len = 0;
972 for (i = input_len; i > 0; i--) {
973 prev_done = done;
974 done |= (input[i-1] != 0);
975 *data_len |= i * (done != prev_done);
976 }
977
978 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200981
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200982/*
983 * No padding: don't pad :)
984 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200986 * but a trivial get_padding function
987 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988static int get_no_padding(unsigned char *input, size_t input_len,
989 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200990{
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (NULL == input || NULL == data_len) {
992 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
993 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
995 *data_len = input_len;
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1002 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (ctx->cipher_info == NULL) {
1005 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1006 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001007
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001008#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001010 /* While PSA Crypto has an API for multipart
1011 * operations, we currently don't make it
1012 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001014 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001015#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001016
Paul Bakker8123e9d2011-01-06 15:37:30 +00001017 *olen = 0;
1018
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001019#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1020 /* CBC mode requires padding so we make sure a call to
1021 * mbedtls_cipher_set_padding_mode has been done successfully. */
1022 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1023 if (ctx->get_padding == NULL) {
1024 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1025 }
1026 }
1027#endif
1028
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001029 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1030 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1031 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1032 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1033 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1034 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1035 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +00001037 }
1038
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001039 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1040 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -03001042 }
1043
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001044 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (ctx->unprocessed_len != 0) {
1046 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1047 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001050 }
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_CIPHER_MODE_CBC)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001053 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001054 int ret = 0;
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001057 /* check for 'no padding' mode */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (NULL == ctx->add_padding) {
1059 if (0 != ctx->unprocessed_len) {
1060 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1061 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001064 }
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1067 ctx->unprocessed_len);
1068 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001069 /*
1070 * For decrypt operations, expect a full block,
1071 * or an empty block if no padding
1072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1074 return 0;
1075 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001078 }
1079
1080 /* cipher block */
Dave Rodgmande3de772023-06-24 12:51:06 +01001081 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
Dave Rodgman3b46b772023-06-24 13:25:06 +01001082 ctx->operation,
1083 mbedtls_cipher_get_block_size(
1084 ctx),
1085 ctx->iv,
1086 ctx->unprocessed_data,
1087 output))) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001089 }
1090
1091 /* Set output size for decryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (MBEDTLS_DECRYPT == ctx->operation) {
1093 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1094 olen);
1095 }
Paul Bakker8123e9d2011-01-06 15:37:30 +00001096
1097 /* Set output size for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 *olen = mbedtls_cipher_get_block_size(ctx);
1099 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001101#else
1102 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106}
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1110 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001111{
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001112 if (NULL == ctx->cipher_info ||
1113 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001115 }
1116
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001117#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001119 /* While PSA Crypto knows about CBC padding
1120 * schemes, we currently don't make them
1121 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (mode != MBEDTLS_PADDING_NONE) {
1123 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1124 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001127 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001128#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 case MBEDTLS_PADDING_PKCS7:
1133 ctx->add_padding = add_pkcs_padding;
1134 ctx->get_padding = get_pkcs_padding;
1135 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001136#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1139 ctx->add_padding = add_one_and_zeros_padding;
1140 ctx->get_padding = get_one_and_zeros_padding;
1141 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1145 ctx->add_padding = add_zeros_and_len_padding;
1146 ctx->get_padding = get_zeros_and_len_padding;
1147 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001148#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 case MBEDTLS_PADDING_ZEROS:
1151 ctx->add_padding = add_zeros_padding;
1152 ctx->get_padding = get_zeros_padding;
1153 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001154#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 case MBEDTLS_PADDING_NONE:
1156 ctx->add_padding = NULL;
1157 ctx->get_padding = get_no_padding;
1158 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 default:
1161 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001167
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001168#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001169int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1170 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001171{
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (ctx->cipher_info == NULL) {
1173 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1174 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (MBEDTLS_ENCRYPT != ctx->operation) {
1177 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1178 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001179
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001180#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001182 /* While PSA Crypto has an API for multipart
1183 * operations, we currently don't make it
1184 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001186 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001187#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001188
Daniel King8fe47012016-05-17 20:33:28 -03001189#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001190 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001191 size_t output_length;
1192 /* The code here doesn't yet support alternative implementations
1193 * that can delay up to a block of output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1195 NULL, 0, &output_length,
1196 tag, tag_len);
Gilles Peskine5a7be102021-06-23 21:51:32 +02001197 }
Daniel King8fe47012016-05-17 20:33:28 -03001198#endif
1199
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001200#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001201 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001202 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (tag_len != 16U) {
1204 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1205 }
Daniel King8fe47012016-05-17 20:33:28 -03001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 return mbedtls_chachapoly_finish(
1208 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001209 }
1210#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001213}
Paul Bakker9af723c2014-05-01 13:03:14 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1216 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001217{
Daniel King8fe47012016-05-17 20:33:28 -03001218 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (ctx->cipher_info == NULL) {
1222 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1223 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (MBEDTLS_DECRYPT != ctx->operation) {
1226 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001227 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001228
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001229#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001231 /* While PSA Crypto has an API for multipart
1232 * operations, we currently don't make it
1233 * accessible through the cipher layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001235 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001236#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001237
Denis V. Lunev2df73ae2018-11-01 12:22:27 +03001238 /* Status to return on a non-authenticated algorithm. */
1239 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee7835d92021-12-13 12:32:43 +01001240
Daniel King8fe47012016-05-17 20:33:28 -03001241#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001242 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Gilles Peskine5a7be102021-06-23 21:51:32 +02001243 size_t output_length;
1244 /* The code here doesn't yet support alternative implementations
1245 * that can delay up to a block of output. */
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if (tag_len > sizeof(check_tag)) {
1248 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1249 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (0 != (ret = mbedtls_gcm_finish(
1252 (mbedtls_gcm_context *) ctx->cipher_ctx,
1253 NULL, 0, &output_length,
1254 check_tag, tag_len))) {
1255 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001256 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001257
1258 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001260 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001261 goto exit;
1262 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001263 }
Daniel King8fe47012016-05-17 20:33:28 -03001264#endif /* MBEDTLS_GCM_C */
1265
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001266#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001267 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Daniel King8fe47012016-05-17 20:33:28 -03001268 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (tag_len != sizeof(check_tag)) {
1270 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1271 }
Daniel King8fe47012016-05-17 20:33:28 -03001272
Hanno Becker18597cd2018-11-09 16:36:33 +00001273 ret = mbedtls_chachapoly_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1275 if (ret != 0) {
1276 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001277 }
1278
1279 /* Check the tag in "constant-time" */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinee7835d92021-12-13 12:32:43 +01001281 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinecd742982021-12-13 16:57:47 +01001282 goto exit;
1283 }
Daniel King8fe47012016-05-17 20:33:28 -03001284 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001285#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001286
Gilles Peskinecd742982021-12-13 16:57:47 +01001287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 mbedtls_platform_zeroize(check_tag, tag_len);
1289 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001290}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001291#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001292
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001293/*
1294 * Packet-oriented wrapper for non-AEAD modes
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1297 const unsigned char *iv, size_t iv_len,
1298 const unsigned char *input, size_t ilen,
1299 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001300{
Janos Follath24eed8d2019-11-22 13:21:35 +00001301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001302 size_t finish_olen;
1303
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001304#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001306 /* As in the non-PSA case, we don't check that
1307 * a key has been set. If not, the key slot will
1308 * still be in its default state of 0, which is
1309 * guaranteed to be invalid, hence the PSA-call
1310 * below will gracefully fail. */
1311 mbedtls_cipher_context_psa * const cipher_psa =
1312 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1313
1314 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001315 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001316 size_t part_len;
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (ctx->operation == MBEDTLS_DECRYPT) {
1319 status = psa_cipher_decrypt_setup(&cipher_op,
1320 cipher_psa->slot,
1321 cipher_psa->alg);
1322 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1323 status = psa_cipher_encrypt_setup(&cipher_op,
1324 cipher_psa->slot,
1325 cipher_psa->alg);
1326 } else {
1327 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001328 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001329
1330 /* In the following, we can immediately return on an error,
1331 * because the PSA Crypto API guarantees that cipher operations
1332 * are terminated by unsuccessful calls to psa_cipher_update(),
1333 * and by any call to psa_cipher_finish(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (status != PSA_SUCCESS) {
1335 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +02001336 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001337
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001338 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1340 if (status != PSA_SUCCESS) {
1341 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1342 }
1343 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 status = psa_cipher_update(&cipher_op,
1346 input, ilen,
1347 output, ilen, olen);
1348 if (status != PSA_SUCCESS) {
1349 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1350 }
1351
1352 status = psa_cipher_finish(&cipher_op,
1353 output + *olen, ilen - *olen,
1354 &part_len);
1355 if (status != PSA_SUCCESS) {
1356 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1357 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001358
1359 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001361 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001362#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1365 return ret;
1366 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1369 return ret;
1370 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1373 output, olen)) != 0) {
1374 return ret;
1375 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1378 &finish_olen)) != 0) {
1379 return ret;
1380 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001381
1382 *olen += finish_olen;
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001385}
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001388/*
TRodziewicz18efb732021-04-29 23:12:19 +02001389 * Packet-oriented encryption for AEAD modes: internal function used by
1390 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1393 const unsigned char *iv, size_t iv_len,
1394 const unsigned char *ad, size_t ad_len,
1395 const unsigned char *input, size_t ilen,
1396 unsigned char *output, size_t *olen,
1397 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001398{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001399#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001401 /* As in the non-PSA case, we don't check that
1402 * a key has been set. If not, the key slot will
1403 * still be in its default state of 0, which is
1404 * guaranteed to be invalid, hence the PSA-call
1405 * below will gracefully fail. */
1406 mbedtls_cipher_context_psa * const cipher_psa =
1407 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1408
1409 psa_status_t status;
1410
1411 /* PSA Crypto API always writes the authentication tag
1412 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (output == NULL || tag != output + ilen) {
1414 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1415 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 status = psa_aead_encrypt(cipher_psa->slot,
1418 cipher_psa->alg,
1419 iv, iv_len,
1420 ad, ad_len,
1421 input, ilen,
1422 output, ilen + tag_len, olen);
1423 if (status != PSA_SUCCESS) {
1424 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1425 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001426
1427 *olen -= tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001429 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001430#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001433 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001434 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1436 ilen, iv, iv_len, ad, ad_len,
1437 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_GCM_C */
1440#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001441 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001442 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1444 iv, iv_len, ad, ad_len, input, output,
1445 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001448#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001449 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001450 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001451 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 (tag_len != 16U)) {
1453 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001454 }
1455
1456 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1458 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001459 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001460#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001463}
1464
1465/*
TRodziewicz18efb732021-04-29 23:12:19 +02001466 * Packet-oriented encryption for AEAD modes: internal function used by
1467 * mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1470 const unsigned char *iv, size_t iv_len,
1471 const unsigned char *ad, size_t ad_len,
1472 const unsigned char *input, size_t ilen,
1473 unsigned char *output, size_t *olen,
1474 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001475{
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001476#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001478 /* As in the non-PSA case, we don't check that
1479 * a key has been set. If not, the key slot will
1480 * still be in its default state of 0, which is
1481 * guaranteed to be invalid, hence the PSA-call
1482 * below will gracefully fail. */
1483 mbedtls_cipher_context_psa * const cipher_psa =
1484 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1485
1486 psa_status_t status;
1487
1488 /* PSA Crypto API always writes the authentication tag
1489 * at the end of the encrypted message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (input == NULL || tag != input + ilen) {
1491 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1492 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 status = psa_aead_decrypt(cipher_psa->slot,
1495 cipher_psa->alg,
1496 iv, iv_len,
1497 ad, ad_len,
1498 input, ilen + tag_len,
1499 output, ilen, olen);
1500 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1501 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1502 } else if (status != PSA_SUCCESS) {
1503 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1504 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001507 }
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001508#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#if defined(MBEDTLS_GCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001511 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001513
1514 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1516 iv, iv_len, ad, ad_len,
1517 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_GCM_C */
1526#if defined(MBEDTLS_CCM_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001527 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001529
1530 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1532 iv, iv_len, ad, ad_len,
1533 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001540 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001542#if defined(MBEDTLS_CHACHAPOLY_C)
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001543 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001545
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001546 /* ChachaPoly has fixed length nonce and MAC (tag) */
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01001547 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 (tag_len != 16U)) {
1549 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001550 }
1551
1552 *olen = ilen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1554 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001557 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 }
Daniel King8fe47012016-05-17 20:33:28 -03001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001561 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001562#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001567
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001568#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1569/*
1570 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1573 const unsigned char *iv, size_t iv_len,
1574 const unsigned char *ad, size_t ad_len,
1575 const unsigned char *input, size_t ilen,
1576 unsigned char *output, size_t output_len,
1577 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001578{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001579#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001581#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001582 ctx->psa_enabled == 0 &&
1583#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001584 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1585 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1586 mbedtls_nist_kw_mode_t mode =
1587 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1588 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001589
1590 /* There is no iv, tag or ad associated with KW and KWP,
1591 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1593 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1594 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001595
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001596 (void) iv;
1597 (void) ad;
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1600 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001601 }
1602#endif /* MBEDTLS_NIST_KW_C */
1603
1604#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1605 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (output_len < ilen + tag_len) {
1607 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1608 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1611 input, ilen, output, olen,
1612 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001613 *olen += tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001615#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001617#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1618}
1619
1620/*
1621 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1624 const unsigned char *iv, size_t iv_len,
1625 const unsigned char *ad, size_t ad_len,
1626 const unsigned char *input, size_t ilen,
1627 unsigned char *output, size_t output_len,
1628 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001629{
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001630#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (
Manuel Pégourié-Gonnard5c731b02023-06-21 10:37:30 +02001632#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001633 ctx->psa_enabled == 0 &&
1634#endif
Dave Rodgman1b8a3b12023-06-24 17:32:43 +01001635 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1636 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1637 mbedtls_nist_kw_mode_t mode =
1638 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1639 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001640
1641 /* There is no iv, tag or ad associated with KW and KWP,
1642 * so these length should be 0 as documented. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1644 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1645 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001646
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001647 (void) iv;
1648 (void) ad;
1649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1651 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001652 }
1653#endif /* MBEDTLS_NIST_KW_C */
1654
1655#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1656 /* AEAD case: check length before passing on to shared function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (ilen < tag_len || output_len < ilen - tag_len) {
1658 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1659 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1662 input, ilen - tag_len, output, olen,
1663 input + ilen - tag_len, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001664#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001666#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1667}
1668#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_CIPHER_C */