blob: 3f2f1a8b2db9cf79b04bb040f694a540da18f807 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file cipher.c
3 *
Tom Van Eyckc1633172024-04-09 18:44:13 +02004 * \brief Generic cipher wrapper for Mbed TLS
Jens Wiklander817466c2018-05-22 13:49:31 +02005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Jerome Forissier79013242021-07-28 10:24:04 +02008 * Copyright The Mbed TLS Contributors
Tom Van Eyckc1633172024-04-09 18:44:13 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +020010 */
11
Jerome Forissier79013242021-07-28 10:24:04 +020012#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020013
14#if defined(MBEDTLS_CIPHER_C)
15
16#include "mbedtls/cipher.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020017#include "cipher_wrap.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010018#include "mbedtls/platform_util.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020019#include "mbedtls/error.h"
Jerome Forissier039e02d2022-08-09 17:10:15 +020020#include "mbedtls/constant_time.h"
Tom Van Eyckc1633172024-04-09 18:44:13 +020021#include "constant_time_internal.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020022
23#include <stdlib.h>
24#include <string.h>
25
Jens Wiklander3d3b0592019-03-20 15:30:29 +010026#if defined(MBEDTLS_CHACHAPOLY_C)
27#include "mbedtls/chachapoly.h"
28#endif
29
Jens Wiklander817466c2018-05-22 13:49:31 +020030#if defined(MBEDTLS_GCM_C)
31#include "mbedtls/gcm.h"
32#endif
33
34#if defined(MBEDTLS_CCM_C)
35#include "mbedtls/ccm.h"
36#endif
37
Jens Wiklander3d3b0592019-03-20 15:30:29 +010038#if defined(MBEDTLS_CHACHA20_C)
39#include "mbedtls/chacha20.h"
40#endif
41
Jens Wiklander817466c2018-05-22 13:49:31 +020042#if defined(MBEDTLS_CMAC_C)
43#include "mbedtls/cmac.h"
44#endif
45
Tom Van Eyckc1633172024-04-09 18:44:13 +020046#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier11fa71b2020-04-20 17:17:56 +020047#include "psa/crypto.h"
Tom Van Eyckc1633172024-04-09 18:44:13 +020048#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +020049
50#if defined(MBEDTLS_NIST_KW_C)
51#include "mbedtls/nist_kw.h"
52#endif
53
Jens Wiklander817466c2018-05-22 13:49:31 +020054#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020055
Jens Wiklander817466c2018-05-22 13:49:31 +020056static int supported_init = 0;
57
Tom Van Eyckc1633172024-04-09 18:44:13 +020058static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base(
59 const mbedtls_cipher_info_t *info)
60{
61 return mbedtls_cipher_base_lookup_table[info->base_idx];
62}
63
Jens Wiklander32b31802023-10-06 16:59:46 +020064const int *mbedtls_cipher_list(void)
Jens Wiklander817466c2018-05-22 13:49:31 +020065{
66 const mbedtls_cipher_definition_t *def;
67 int *type;
68
Jens Wiklander32b31802023-10-06 16:59:46 +020069 if (!supported_init) {
Jens Wiklander817466c2018-05-22 13:49:31 +020070 def = mbedtls_cipher_definitions;
71 type = mbedtls_cipher_supported;
72
Jens Wiklander32b31802023-10-06 16:59:46 +020073 while (def->type != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +020074 *type++ = (*def++).type;
Jens Wiklander32b31802023-10-06 16:59:46 +020075 }
Jens Wiklander817466c2018-05-22 13:49:31 +020076
77 *type = 0;
78
79 supported_init = 1;
80 }
81
Jens Wiklander32b31802023-10-06 16:59:46 +020082 return mbedtls_cipher_supported;
Jens Wiklander817466c2018-05-22 13:49:31 +020083}
84
Jerome Forissier11fa71b2020-04-20 17:17:56 +020085const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Jens Wiklander32b31802023-10-06 16:59:46 +020086 const mbedtls_cipher_type_t cipher_type)
Jens Wiklander817466c2018-05-22 13:49:31 +020087{
88 const mbedtls_cipher_definition_t *def;
89
Jens Wiklander32b31802023-10-06 16:59:46 +020090 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
91 if (def->type == cipher_type) {
92 return def->info;
93 }
94 }
Jens Wiklander817466c2018-05-22 13:49:31 +020095
Jens Wiklander32b31802023-10-06 16:59:46 +020096 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +020097}
98
Jerome Forissier11fa71b2020-04-20 17:17:56 +020099const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Jens Wiklander32b31802023-10-06 16:59:46 +0200100 const char *cipher_name)
Jens Wiklander817466c2018-05-22 13:49:31 +0200101{
102 const mbedtls_cipher_definition_t *def;
103
Jens Wiklander32b31802023-10-06 16:59:46 +0200104 if (NULL == cipher_name) {
105 return NULL;
106 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200107
Jens Wiklander32b31802023-10-06 16:59:46 +0200108 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
109 if (!strcmp(def->info->name, cipher_name)) {
110 return def->info;
111 }
112 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200113
Jens Wiklander32b31802023-10-06 16:59:46 +0200114 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200115}
116
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200117const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
118 const mbedtls_cipher_id_t cipher_id,
119 int key_bitlen,
Jens Wiklander32b31802023-10-06 16:59:46 +0200120 const mbedtls_cipher_mode_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +0200121{
122 const mbedtls_cipher_definition_t *def;
123
Jens Wiklander32b31802023-10-06 16:59:46 +0200124 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200125 if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id &&
126 mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200127 def->info->mode == mode) {
128 return def->info;
129 }
130 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200131
Jens Wiklander32b31802023-10-06 16:59:46 +0200132 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200133}
134
Tom Van Eyckc1633172024-04-09 18:44:13 +0200135#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
136static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
137 mbedtls_cipher_type_t cipher)
138{
139 switch (cipher) {
140 case MBEDTLS_CIPHER_AES_128_CCM:
141 case MBEDTLS_CIPHER_AES_192_CCM:
142 case MBEDTLS_CIPHER_AES_256_CCM:
143 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
144 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
145 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
146 case MBEDTLS_CIPHER_AES_128_GCM:
147 case MBEDTLS_CIPHER_AES_192_GCM:
148 case MBEDTLS_CIPHER_AES_256_GCM:
149 case MBEDTLS_CIPHER_AES_128_CBC:
150 case MBEDTLS_CIPHER_AES_192_CBC:
151 case MBEDTLS_CIPHER_AES_256_CBC:
152 case MBEDTLS_CIPHER_AES_128_ECB:
153 case MBEDTLS_CIPHER_AES_192_ECB:
154 case MBEDTLS_CIPHER_AES_256_ECB:
155 return PSA_KEY_TYPE_AES;
156
157 /* ARIA not yet supported in PSA. */
158 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
159 case MBEDTLS_CIPHER_ARIA_192_CCM:
160 case MBEDTLS_CIPHER_ARIA_256_CCM:
161 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
162 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
163 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
164 case MBEDTLS_CIPHER_ARIA_128_GCM:
165 case MBEDTLS_CIPHER_ARIA_192_GCM:
166 case MBEDTLS_CIPHER_ARIA_256_GCM:
167 case MBEDTLS_CIPHER_ARIA_128_CBC:
168 case MBEDTLS_CIPHER_ARIA_192_CBC:
169 case MBEDTLS_CIPHER_ARIA_256_CBC:
170 return( PSA_KEY_TYPE_ARIA ); */
171
172 default:
173 return 0;
174 }
175}
176
177static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
178 mbedtls_cipher_mode_t mode, size_t taglen)
179{
180 switch (mode) {
181 case MBEDTLS_MODE_ECB:
182 return PSA_ALG_ECB_NO_PADDING;
183 case MBEDTLS_MODE_GCM:
184 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
185 case MBEDTLS_MODE_CCM:
186 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
187 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
188 return PSA_ALG_CCM_STAR_NO_TAG;
189 case MBEDTLS_MODE_CBC:
190 if (taglen == 0) {
191 return PSA_ALG_CBC_NO_PADDING;
192 } else {
193 return 0;
194 }
195 default:
196 return 0;
197 }
198}
199#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
200
Jens Wiklander32b31802023-10-06 16:59:46 +0200201void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200202{
Jens Wiklander32b31802023-10-06 16:59:46 +0200203 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200204}
205
Jens Wiklander32b31802023-10-06 16:59:46 +0200206void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200207{
Jens Wiklander32b31802023-10-06 16:59:46 +0200208 if (ctx == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200209 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200210 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200211
Tom Van Eyckc1633172024-04-09 18:44:13 +0200212#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200213 if (ctx->psa_enabled == 1) {
214 if (ctx->cipher_ctx != NULL) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200215 mbedtls_cipher_context_psa * const cipher_psa =
216 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
217
Jens Wiklander32b31802023-10-06 16:59:46 +0200218 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200219 /* xxx_free() doesn't allow to return failures. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200220 (void) psa_destroy_key(cipher_psa->slot);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200221 }
222
Tom Van Eyckc1633172024-04-09 18:44:13 +0200223 mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200224 }
225
Jens Wiklander32b31802023-10-06 16:59:46 +0200226 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200227 return;
228 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200229#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200230
Jens Wiklander817466c2018-05-22 13:49:31 +0200231#if defined(MBEDTLS_CMAC_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200232 if (ctx->cmac_ctx) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200233 mbedtls_zeroize_and_free(ctx->cmac_ctx,
Jens Wiklander32b31802023-10-06 16:59:46 +0200234 sizeof(mbedtls_cmac_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200235 }
236#endif
237
Jens Wiklander32b31802023-10-06 16:59:46 +0200238 if (ctx->cipher_ctx) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200239 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
Jens Wiklander32b31802023-10-06 16:59:46 +0200240 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200241
Jens Wiklander32b31802023-10-06 16:59:46 +0200242 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200243}
244
Edison Aiab0eb552018-12-19 15:36:28 +0800245int mbedtls_cipher_clone(mbedtls_cipher_context_t *dst,
246 const mbedtls_cipher_context_t *src)
247{
248 if (dst == NULL || dst->cipher_info == NULL ||
249 src == NULL || src->cipher_info == NULL) {
250 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
251 }
252
253 dst->cipher_info = src->cipher_info;
254 dst->key_bitlen = src->key_bitlen;
255 dst->operation = src->operation;
256#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
257 dst->add_padding = src->add_padding;
258 dst->get_padding = src->get_padding;
259#endif
260 memcpy(dst->unprocessed_data, src->unprocessed_data, MBEDTLS_MAX_BLOCK_LENGTH);
261 dst->unprocessed_len = src->unprocessed_len;
262 memcpy(dst->iv, src->iv, MBEDTLS_MAX_IV_LENGTH);
263 dst->iv_size = src->iv_size;
264 if (mbedtls_cipher_get_base(dst->cipher_info)->ctx_clone_func)
265 mbedtls_cipher_get_base(dst->cipher_info)->ctx_clone_func(dst->cipher_ctx, src->cipher_ctx);
266
267#if defined(MBEDTLS_CMAC_C)
268 if (dst->cmac_ctx != NULL && src->cmac_ctx != NULL)
269 memcpy(dst->cmac_ctx, src->cmac_ctx, sizeof(mbedtls_cmac_context_t));
270#endif
271 return 0;
272}
273
Jens Wiklander32b31802023-10-06 16:59:46 +0200274int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
275 const mbedtls_cipher_info_t *cipher_info)
Jens Wiklander817466c2018-05-22 13:49:31 +0200276{
Jens Wiklander32b31802023-10-06 16:59:46 +0200277 if (cipher_info == NULL) {
278 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
279 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200280
Jens Wiklander32b31802023-10-06 16:59:46 +0200281 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200282
Tom Van Eyckc1633172024-04-09 18:44:13 +0200283 if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
284 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
285 if (ctx->cipher_ctx == NULL) {
286 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
287 }
Jens Wiklander32b31802023-10-06 16:59:46 +0200288 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200289
290 ctx->cipher_info = cipher_info;
291
Jens Wiklander32b31802023-10-06 16:59:46 +0200292 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200293}
294
Tom Van Eyckc1633172024-04-09 18:44:13 +0200295#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200296int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
297 const mbedtls_cipher_info_t *cipher_info,
298 size_t taglen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200299{
300 psa_algorithm_t alg;
301 mbedtls_cipher_context_psa *cipher_psa;
302
Jens Wiklander32b31802023-10-06 16:59:46 +0200303 if (NULL == cipher_info || NULL == ctx) {
304 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
305 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200306
307 /* Check that the underlying cipher mode and cipher type are
308 * supported by the underlying PSA Crypto implementation. */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200309 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Jens Wiklander32b31802023-10-06 16:59:46 +0200310 if (alg == 0) {
311 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
312 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200313 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200314 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
315 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200316
Jens Wiklander32b31802023-10-06 16:59:46 +0200317 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200318
Jens Wiklander32b31802023-10-06 16:59:46 +0200319 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
320 if (cipher_psa == NULL) {
321 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
322 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200323 cipher_psa->alg = alg;
324 ctx->cipher_ctx = cipher_psa;
325 ctx->cipher_info = cipher_info;
326 ctx->psa_enabled = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200327 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200328}
Tom Van Eyckc1633172024-04-09 18:44:13 +0200329#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Edison Ai12484fc2018-12-19 15:36:28 +0800330
Edison Aiab0eb552018-12-19 15:36:28 +0800331int mbedtls_cipher_setup_info(mbedtls_cipher_context_t *ctx,
332 const mbedtls_cipher_info_t *cipher_info )
333{
334 if (NULL == cipher_info || NULL == ctx)
335 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
336
337 ctx->cipher_info = cipher_info;
338 return 0;
339}
340
Jens Wiklander32b31802023-10-06 16:59:46 +0200341int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
342 const unsigned char *key,
343 int key_bitlen,
344 const mbedtls_operation_t operation)
Jens Wiklander817466c2018-05-22 13:49:31 +0200345{
Jens Wiklander32b31802023-10-06 16:59:46 +0200346 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
347 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
348 }
349 if (ctx->cipher_info == NULL) {
350 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
351 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200352#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
353 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
354 MBEDTLS_DECRYPT == operation) {
355 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
356 }
357#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200358
Tom Van Eyckc1633172024-04-09 18:44:13 +0200359#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200360 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200361 mbedtls_cipher_context_psa * const cipher_psa =
362 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
363
Jens Wiklander32b31802023-10-06 16:59:46 +0200364 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200365
366 psa_status_t status;
367 psa_key_type_t key_type;
368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
369
370 /* PSA Crypto API only accepts byte-aligned keys. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200371 if (key_bitlen % 8 != 0) {
372 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
373 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200374
375 /* Don't allow keys to be set multiple times. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200376 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
377 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
378 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200379
380 key_type = mbedtls_psa_translate_cipher_type(
Tom Van Eyckc1633172024-04-09 18:44:13 +0200381 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Jens Wiklander32b31802023-10-06 16:59:46 +0200382 if (key_type == 0) {
383 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
384 }
385 psa_set_key_type(&attributes, key_type);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200386
387 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
388 * (encrypt vs. decrypt): it is possible to setup a key for encryption
389 * and use it for AEAD decryption. Until tests relying on this
390 * are changed, allow any usage in PSA. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200391 psa_set_key_usage_flags(&attributes,
Jens Wiklander32b31802023-10-06 16:59:46 +0200392 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
393 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200394
Jens Wiklander32b31802023-10-06 16:59:46 +0200395 status = psa_import_key(&attributes, key, key_bytelen,
396 &cipher_psa->slot);
397 switch (status) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200398 case PSA_SUCCESS:
399 break;
400 case PSA_ERROR_INSUFFICIENT_MEMORY:
Jens Wiklander32b31802023-10-06 16:59:46 +0200401 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200402 case PSA_ERROR_NOT_SUPPORTED:
Jens Wiklander32b31802023-10-06 16:59:46 +0200403 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200404 default:
Jens Wiklander32b31802023-10-06 16:59:46 +0200405 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200406 }
407 /* Indicate that we own the key slot and need to
408 * destroy it in mbedtls_cipher_free(). */
409 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
410
411 ctx->key_bitlen = key_bitlen;
412 ctx->operation = operation;
Jens Wiklander32b31802023-10-06 16:59:46 +0200413 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200414 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200415#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200416
Jens Wiklander32b31802023-10-06 16:59:46 +0200417 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Tom Van Eyckc1633172024-04-09 18:44:13 +0200418 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200419 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200420 }
421
422 ctx->key_bitlen = key_bitlen;
423 ctx->operation = operation;
424
Tom Van Eyckc1633172024-04-09 18:44:13 +0200425#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Jens Wiklander817466c2018-05-22 13:49:31 +0200426 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100427 * For OFB, CFB and CTR mode always use the encryption key schedule
Jens Wiklander817466c2018-05-22 13:49:31 +0200428 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200429 if (MBEDTLS_ENCRYPT == operation ||
Tom Van Eyckc1633172024-04-09 18:44:13 +0200430 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
431 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
432 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
433 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
434 ctx->key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200435 }
436
Jens Wiklander32b31802023-10-06 16:59:46 +0200437 if (MBEDTLS_DECRYPT == operation) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200438 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
439 ctx->key_bitlen);
Jens Wiklander32b31802023-10-06 16:59:46 +0200440 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200441#else
442 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
443 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
444 ctx->key_bitlen);
445 }
446#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200447
Jens Wiklander32b31802023-10-06 16:59:46 +0200448 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200449}
450
Jens Wiklander32b31802023-10-06 16:59:46 +0200451int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
452 const unsigned char *iv,
453 size_t iv_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200454{
455 size_t actual_iv_size;
456
Jens Wiklander32b31802023-10-06 16:59:46 +0200457 if (ctx->cipher_info == NULL) {
458 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
459 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200460#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200461 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200462 /* While PSA Crypto has an API for multipart
463 * operations, we currently don't make it
464 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200465 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200466 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200467#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200468
469 /* avoid buffer overflow in ctx->iv */
Jens Wiklander32b31802023-10-06 16:59:46 +0200470 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
471 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
472 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200473
Jens Wiklander32b31802023-10-06 16:59:46 +0200474 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200475 actual_iv_size = iv_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200476 } else {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200477 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Jens Wiklander817466c2018-05-22 13:49:31 +0200478
479 /* avoid reading past the end of input buffer */
Jens Wiklander32b31802023-10-06 16:59:46 +0200480 if (actual_iv_size > iv_len) {
481 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
482 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200483 }
484
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100485#if defined(MBEDTLS_CHACHA20_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200486 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Jerome Forissier039e02d2022-08-09 17:10:15 +0200487 /* Even though the actual_iv_size is overwritten with a correct value
488 * of 12 from the cipher info, return an error to indicate that
489 * the input iv_len is wrong. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200490 if (iv_len != 12) {
491 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
492 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200493
Jens Wiklander32b31802023-10-06 16:59:46 +0200494 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
495 iv,
496 0U)) { /* Initial counter value */
497 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100498 }
499 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200500#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200501 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200502 iv_len != 12) {
503 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
504 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200505#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100506#endif
507
Jens Wiklander32b31802023-10-06 16:59:46 +0200508#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200509 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200510 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
511 ctx->operation,
512 iv, iv_len);
513 }
514#endif
515
516#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200517 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200518 int set_lengths_result;
519 int ccm_star_mode;
520
521 set_lengths_result = mbedtls_ccm_set_lengths(
522 (mbedtls_ccm_context *) ctx->cipher_ctx,
523 0, 0, 0);
524 if (set_lengths_result != 0) {
525 return set_lengths_result;
526 }
527
528 if (ctx->operation == MBEDTLS_DECRYPT) {
529 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
530 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
531 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
532 } else {
533 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
534 }
535
536 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
537 ccm_star_mode,
538 iv, iv_len);
539 }
540#endif
541
542 if (actual_iv_size != 0) {
543 memcpy(ctx->iv, iv, actual_iv_size);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100544 ctx->iv_size = actual_iv_size;
545 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200546
Jens Wiklander32b31802023-10-06 16:59:46 +0200547 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200548}
549
Jens Wiklander32b31802023-10-06 16:59:46 +0200550int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200551{
Jens Wiklander32b31802023-10-06 16:59:46 +0200552 if (ctx->cipher_info == NULL) {
553 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
554 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200555
Tom Van Eyckc1633172024-04-09 18:44:13 +0200556#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200557 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200558 /* We don't support resetting PSA-based
559 * cipher contexts, yet. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200560 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200561 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200562#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200563
Jens Wiklander817466c2018-05-22 13:49:31 +0200564 ctx->unprocessed_len = 0;
565
Jens Wiklander32b31802023-10-06 16:59:46 +0200566 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200567}
568
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100569#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200570int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
571 const unsigned char *ad, size_t ad_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200572{
Jens Wiklander32b31802023-10-06 16:59:46 +0200573 if (ctx->cipher_info == NULL) {
574 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
575 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200576
Tom Van Eyckc1633172024-04-09 18:44:13 +0200577#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200578 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200579 /* While PSA Crypto has an API for multipart
580 * operations, we currently don't make it
581 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200582 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200583 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200584#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200585
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100586#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200587 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200588 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
589 ad, ad_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200590 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100591#endif
592
593#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200594 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100595 int result;
596 mbedtls_chachapoly_mode_t mode;
597
Jens Wiklander32b31802023-10-06 16:59:46 +0200598 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100599 ? MBEDTLS_CHACHAPOLY_ENCRYPT
600 : MBEDTLS_CHACHAPOLY_DECRYPT;
601
Jens Wiklander32b31802023-10-06 16:59:46 +0200602 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
603 ctx->iv,
604 mode);
605 if (result != 0) {
606 return result;
607 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100608
Jens Wiklander32b31802023-10-06 16:59:46 +0200609 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
610 ad, ad_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100611 }
612#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200613
Jens Wiklander32b31802023-10-06 16:59:46 +0200614 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200615}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100616#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200617
Jens Wiklander32b31802023-10-06 16:59:46 +0200618int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
619 size_t ilen, unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200620{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100622 size_t block_size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200623
Jens Wiklander32b31802023-10-06 16:59:46 +0200624 if (ctx->cipher_info == NULL) {
625 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
626 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200627
Tom Van Eyckc1633172024-04-09 18:44:13 +0200628#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200629 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200630 /* While PSA Crypto has an API for multipart
631 * operations, we currently don't make it
632 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200633 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200634 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200635#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200636
Jens Wiklander817466c2018-05-22 13:49:31 +0200637 *olen = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200638 block_size = mbedtls_cipher_get_block_size(ctx);
639 if (0 == block_size) {
640 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200641 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200642
Tom Van Eyckc1633172024-04-09 18:44:13 +0200643 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200644 if (ilen != block_size) {
645 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
646 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200647
648 *olen = ilen;
649
Tom Van Eyckc1633172024-04-09 18:44:13 +0200650 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
651 ctx->operation, input,
652 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200653 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200654 }
655
Jens Wiklander32b31802023-10-06 16:59:46 +0200656 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200657 }
658
659#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200660 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200661 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
662 input, ilen,
663 output, ilen, olen);
664 }
665#endif
666
667#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200668 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200669 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
670 input, ilen,
671 output, ilen, olen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100672 }
673#endif
674
675#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200676 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100677 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200678 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
679 ilen, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200680 }
681#endif
682
Jens Wiklander32b31802023-10-06 16:59:46 +0200683 if (input == output &&
684 (ctx->unprocessed_len != 0 || ilen % block_size)) {
685 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200686 }
687
688#if defined(MBEDTLS_CIPHER_MODE_CBC)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200689 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200690 size_t copy_len = 0;
691
692 /*
693 * If there is not enough data for a full block, cache it.
694 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200695 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
696 ilen <= block_size - ctx->unprocessed_len) ||
697 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
698 ilen < block_size - ctx->unprocessed_len) ||
699 (ctx->operation == MBEDTLS_ENCRYPT &&
700 ilen < block_size - ctx->unprocessed_len)) {
701 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
702 ilen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200703
704 ctx->unprocessed_len += ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200705 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200706 }
707
708 /*
709 * Process cached data first
710 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200711 if (0 != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200712 copy_len = block_size - ctx->unprocessed_len;
713
Jens Wiklander32b31802023-10-06 16:59:46 +0200714 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
715 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200716
Tom Van Eyckc1633172024-04-09 18:44:13 +0200717 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
718 ctx->operation,
719 block_size, ctx->iv,
720 ctx->
721 unprocessed_data,
722 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200723 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200724 }
725
726 *olen += block_size;
727 output += block_size;
728 ctx->unprocessed_len = 0;
729
730 input += copy_len;
731 ilen -= copy_len;
732 }
733
734 /*
735 * Cache final, incomplete block
736 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200737 if (0 != ilen) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100738 /* Encryption: only cache partial blocks
739 * Decryption w/ padding: always keep at least one whole block
740 * Decryption w/o padding: only cache partial blocks
741 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200742 copy_len = ilen % block_size;
Jens Wiklander32b31802023-10-06 16:59:46 +0200743 if (copy_len == 0 &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100744 ctx->operation == MBEDTLS_DECRYPT &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200745 NULL != ctx->add_padding) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200746 copy_len = block_size;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100747 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200748
Jens Wiklander32b31802023-10-06 16:59:46 +0200749 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
750 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200751
752 ctx->unprocessed_len += copy_len;
753 ilen -= copy_len;
754 }
755
756 /*
757 * Process remaining full blocks
758 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200759 if (ilen) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200760 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
761 ctx->operation,
762 ilen, ctx->iv,
763 input,
764 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200765 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200766 }
767
768 *olen += ilen;
769 }
770
Jens Wiklander32b31802023-10-06 16:59:46 +0200771 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200772 }
773#endif /* MBEDTLS_CIPHER_MODE_CBC */
774
775#if defined(MBEDTLS_CIPHER_MODE_CFB)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200776 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
777 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
778 ctx->operation, ilen,
779 &ctx->unprocessed_len,
780 ctx->iv,
781 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200782 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200783 }
784
785 *olen = ilen;
786
Jens Wiklander32b31802023-10-06 16:59:46 +0200787 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200788 }
789#endif /* MBEDTLS_CIPHER_MODE_CFB */
790
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100791#if defined(MBEDTLS_CIPHER_MODE_OFB)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200792 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
793 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
794 ilen,
795 &ctx->unprocessed_len,
796 ctx->iv,
797 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200798 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100799 }
800
801 *olen = ilen;
802
Jens Wiklander32b31802023-10-06 16:59:46 +0200803 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100804 }
805#endif /* MBEDTLS_CIPHER_MODE_OFB */
806
Jens Wiklander817466c2018-05-22 13:49:31 +0200807#if defined(MBEDTLS_CIPHER_MODE_CTR)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200808 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
809 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
810 ilen,
811 &ctx->unprocessed_len,
812 ctx->iv,
813 ctx->unprocessed_data,
814 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200815 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200816 }
817
818 *olen = ilen;
819
Jens Wiklander32b31802023-10-06 16:59:46 +0200820 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200821 }
822#endif /* MBEDTLS_CIPHER_MODE_CTR */
823
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100824#if defined(MBEDTLS_CIPHER_MODE_XTS)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200825 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200826 if (ctx->unprocessed_len > 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100827 /* We can only process an entire data unit at a time. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200828 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100829 }
830
Tom Van Eyckc1633172024-04-09 18:44:13 +0200831 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
832 ctx->operation,
833 ilen,
834 ctx->iv,
835 input,
836 output);
Jens Wiklander32b31802023-10-06 16:59:46 +0200837 if (ret != 0) {
838 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100839 }
840
841 *olen = ilen;
842
Jens Wiklander32b31802023-10-06 16:59:46 +0200843 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100844 }
845#endif /* MBEDTLS_CIPHER_MODE_XTS */
846
Jens Wiklander817466c2018-05-22 13:49:31 +0200847#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200848 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
849 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
850 ilen, input,
851 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200852 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200853 }
854
855 *olen = ilen;
856
Jens Wiklander32b31802023-10-06 16:59:46 +0200857 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200858 }
859#endif /* MBEDTLS_CIPHER_MODE_STREAM */
860
Jens Wiklander32b31802023-10-06 16:59:46 +0200861 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200862}
863
864#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
865#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
866/*
867 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
868 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200869static void add_pkcs_padding(unsigned char *output, size_t output_len,
870 size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200871{
872 size_t padding_len = output_len - data_len;
873 unsigned char i;
874
Jens Wiklander32b31802023-10-06 16:59:46 +0200875 for (i = 0; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200876 output[data_len + i] = (unsigned char) padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200877 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200878}
879
Jens Wiklander32b31802023-10-06 16:59:46 +0200880static int get_pkcs_padding(unsigned char *input, size_t input_len,
881 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200882{
883 size_t i, pad_idx;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200884 unsigned char padding_len;
Jens Wiklander817466c2018-05-22 13:49:31 +0200885
Jens Wiklander32b31802023-10-06 16:59:46 +0200886 if (NULL == input || NULL == data_len) {
887 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
888 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200889
890 padding_len = input[input_len - 1];
891 *data_len = input_len - padding_len;
892
Tom Van Eyckc1633172024-04-09 18:44:13 +0200893 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
894 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200895
896 /* The number of bytes checked must be independent of padding_len,
897 * so pick input_len, which is usually 8 or 16 (one block) */
898 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200899 for (i = 0; i < input_len; i++) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200900 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
901 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
902 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Jens Wiklander32b31802023-10-06 16:59:46 +0200903 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200904
Tom Van Eyckc1633172024-04-09 18:44:13 +0200905 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200906}
907#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
908
909#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
910/*
911 * One and zeros padding: fill with 80 00 ... 00
912 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200913static void add_one_and_zeros_padding(unsigned char *output,
914 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200915{
916 size_t padding_len = output_len - data_len;
917 unsigned char i = 0;
918
919 output[data_len] = 0x80;
Jens Wiklander32b31802023-10-06 16:59:46 +0200920 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200921 output[data_len + i] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200922 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200923}
924
Jens Wiklander32b31802023-10-06 16:59:46 +0200925static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
926 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200927{
Jens Wiklander32b31802023-10-06 16:59:46 +0200928 if (NULL == input || NULL == data_len) {
929 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
930 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200931
Tom Van Eyckc1633172024-04-09 18:44:13 +0200932 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
933 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
934
Jens Wiklander817466c2018-05-22 13:49:31 +0200935 *data_len = 0;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200936
937 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
938 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
939
940 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
941
942 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
943
944 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
945
946 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Jens Wiklander817466c2018-05-22 13:49:31 +0200947 }
948
Tom Van Eyckc1633172024-04-09 18:44:13 +0200949 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200950}
951#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
952
953#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
954/*
955 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
956 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200957static void add_zeros_and_len_padding(unsigned char *output,
958 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200959{
960 size_t padding_len = output_len - data_len;
961 unsigned char i = 0;
962
Jens Wiklander32b31802023-10-06 16:59:46 +0200963 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200964 output[data_len + i - 1] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200965 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200966 output[output_len - 1] = (unsigned char) padding_len;
967}
968
Jens Wiklander32b31802023-10-06 16:59:46 +0200969static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
970 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200971{
972 size_t i, pad_idx;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200973 unsigned char padding_len;
974 mbedtls_ct_condition_t bad;
Jens Wiklander817466c2018-05-22 13:49:31 +0200975
Jens Wiklander32b31802023-10-06 16:59:46 +0200976 if (NULL == input || NULL == data_len) {
977 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
978 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200979
980 padding_len = input[input_len - 1];
981 *data_len = input_len - padding_len;
982
983 /* Avoid logical || since it results in a branch */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200984 bad = mbedtls_ct_uint_gt(padding_len, input_len);
985 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200986
987 /* The number of bytes checked must be independent of padding_len */
988 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200989 for (i = 0; i < input_len - 1; i++) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200990 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
991 mbedtls_ct_condition_t nonzero_pad_byte;
992 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
993 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Jens Wiklander32b31802023-10-06 16:59:46 +0200994 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200995
Tom Van Eyckc1633172024-04-09 18:44:13 +0200996 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200997}
998#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
999
1000#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1001/*
1002 * Zero padding: fill with 00 ... 00
1003 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001004static void add_zeros_padding(unsigned char *output,
1005 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001006{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001007 memset(output + data_len, 0, output_len - data_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001008}
1009
Jens Wiklander32b31802023-10-06 16:59:46 +02001010static int get_zeros_padding(unsigned char *input, size_t input_len,
1011 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001012{
1013 size_t i;
Tom Van Eyckc1633172024-04-09 18:44:13 +02001014 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Jens Wiklander817466c2018-05-22 13:49:31 +02001015
Jens Wiklander32b31802023-10-06 16:59:46 +02001016 if (NULL == input || NULL == data_len) {
1017 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001018 }
1019
Jens Wiklander32b31802023-10-06 16:59:46 +02001020 *data_len = 0;
1021 for (i = input_len; i > 0; i--) {
1022 prev_done = done;
Tom Van Eyckc1633172024-04-09 18:44:13 +02001023 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
1024 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Jens Wiklander32b31802023-10-06 16:59:46 +02001025 }
1026
1027 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001028}
1029#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
1030
1031/*
1032 * No padding: don't pad :)
1033 *
1034 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
1035 * but a trivial get_padding function
1036 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001037static int get_no_padding(unsigned char *input, size_t input_len,
1038 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001039{
Jens Wiklander32b31802023-10-06 16:59:46 +02001040 if (NULL == input || NULL == data_len) {
1041 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1042 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001043
1044 *data_len = input_len;
1045
Jens Wiklander32b31802023-10-06 16:59:46 +02001046 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001047}
1048#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1049
Jens Wiklander32b31802023-10-06 16:59:46 +02001050int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1051 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001052{
Jens Wiklander32b31802023-10-06 16:59:46 +02001053 if (ctx->cipher_info == NULL) {
1054 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1055 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001056
Tom Van Eyckc1633172024-04-09 18:44:13 +02001057#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001058 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001059 /* While PSA Crypto has an API for multipart
1060 * operations, we currently don't make it
1061 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001062 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001063 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001064#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001065
Jens Wiklander817466c2018-05-22 13:49:31 +02001066 *olen = 0;
1067
Tom Van Eyckc1633172024-04-09 18:44:13 +02001068#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1069 /* CBC mode requires padding so we make sure a call to
1070 * mbedtls_cipher_set_padding_mode has been done successfully. */
1071 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1072 if (ctx->get_padding == NULL) {
1073 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1074 }
1075 }
1076#endif
1077
1078 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1079 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1080 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1081 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1082 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1083 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1084 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001085 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001086 }
1087
Tom Van Eyckc1633172024-04-09 18:44:13 +02001088 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1089 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001090 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001091 }
1092
Tom Van Eyckc1633172024-04-09 18:44:13 +02001093 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001094 if (ctx->unprocessed_len != 0) {
1095 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1096 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001097
Jens Wiklander32b31802023-10-06 16:59:46 +02001098 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001099 }
1100
1101#if defined(MBEDTLS_CIPHER_MODE_CBC)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001102 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001103 int ret = 0;
1104
Jens Wiklander32b31802023-10-06 16:59:46 +02001105 if (MBEDTLS_ENCRYPT == ctx->operation) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001106 /* check for 'no padding' mode */
Jens Wiklander32b31802023-10-06 16:59:46 +02001107 if (NULL == ctx->add_padding) {
1108 if (0 != ctx->unprocessed_len) {
1109 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1110 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001111
Jens Wiklander32b31802023-10-06 16:59:46 +02001112 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001113 }
1114
Jens Wiklander32b31802023-10-06 16:59:46 +02001115 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1116 ctx->unprocessed_len);
1117 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001118 /*
1119 * For decrypt operations, expect a full block,
1120 * or an empty block if no padding
1121 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001122 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1123 return 0;
1124 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001125
Jens Wiklander32b31802023-10-06 16:59:46 +02001126 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001127 }
1128
1129 /* cipher block */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001130 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
1131 ctx->operation,
1132 mbedtls_cipher_get_block_size(
1133 ctx),
1134 ctx->iv,
1135 ctx->unprocessed_data,
1136 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001137 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001138 }
1139
1140 /* Set output size for decryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001141 if (MBEDTLS_DECRYPT == ctx->operation) {
1142 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1143 olen);
1144 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001145
1146 /* Set output size for encryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001147 *olen = mbedtls_cipher_get_block_size(ctx);
1148 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001149 }
1150#else
1151 ((void) output);
1152#endif /* MBEDTLS_CIPHER_MODE_CBC */
1153
Jens Wiklander32b31802023-10-06 16:59:46 +02001154 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001155}
1156
1157#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Jens Wiklander32b31802023-10-06 16:59:46 +02001158int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1159 mbedtls_cipher_padding_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +02001160{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001161 if (NULL == ctx->cipher_info ||
1162 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001163 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001164 }
1165
Tom Van Eyckc1633172024-04-09 18:44:13 +02001166#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001167 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001168 /* While PSA Crypto knows about CBC padding
1169 * schemes, we currently don't make them
1170 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001171 if (mode != MBEDTLS_PADDING_NONE) {
1172 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1173 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001174
Jens Wiklander32b31802023-10-06 16:59:46 +02001175 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001176 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001177#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001178
Jens Wiklander32b31802023-10-06 16:59:46 +02001179 switch (mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001180#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Jens Wiklander32b31802023-10-06 16:59:46 +02001181 case MBEDTLS_PADDING_PKCS7:
1182 ctx->add_padding = add_pkcs_padding;
1183 ctx->get_padding = get_pkcs_padding;
1184 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001185#endif
1186#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001187 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1188 ctx->add_padding = add_one_and_zeros_padding;
1189 ctx->get_padding = get_one_and_zeros_padding;
1190 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001191#endif
1192#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Jens Wiklander32b31802023-10-06 16:59:46 +02001193 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1194 ctx->add_padding = add_zeros_and_len_padding;
1195 ctx->get_padding = get_zeros_and_len_padding;
1196 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001197#endif
1198#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001199 case MBEDTLS_PADDING_ZEROS:
1200 ctx->add_padding = add_zeros_padding;
1201 ctx->get_padding = get_zeros_padding;
1202 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001203#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001204 case MBEDTLS_PADDING_NONE:
1205 ctx->add_padding = NULL;
1206 ctx->get_padding = get_no_padding;
1207 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001208
Jens Wiklander32b31802023-10-06 16:59:46 +02001209 default:
1210 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001211 }
1212
Jens Wiklander32b31802023-10-06 16:59:46 +02001213 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001214}
1215#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1216
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001217#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001218int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1219 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001220{
Jens Wiklander32b31802023-10-06 16:59:46 +02001221 if (ctx->cipher_info == NULL) {
1222 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1223 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001224
Jens Wiklander32b31802023-10-06 16:59:46 +02001225 if (MBEDTLS_ENCRYPT != ctx->operation) {
1226 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1227 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001228
Tom Van Eyckc1633172024-04-09 18:44:13 +02001229#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001230 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001231 /* While PSA Crypto has an API for multipart
1232 * operations, we currently don't make it
1233 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001234 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001235 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001236#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001237
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001238#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001239 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001240 size_t output_length;
1241 /* The code here doesn't yet support alternative implementations
1242 * that can delay up to a block of output. */
1243 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1244 NULL, 0, &output_length,
1245 tag, tag_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001246 }
1247#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001248
Jens Wiklander32b31802023-10-06 16:59:46 +02001249#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001250 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001251 /* Don't allow truncated MAC for Poly1305 */
1252 if (tag_len != 16U) {
1253 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1254 }
1255
1256 return mbedtls_chachapoly_finish(
1257 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1258 }
1259#endif
1260
1261 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001262}
1263
Jens Wiklander32b31802023-10-06 16:59:46 +02001264int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1265 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001266{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001267 unsigned char check_tag[16];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001269
Jens Wiklander32b31802023-10-06 16:59:46 +02001270 if (ctx->cipher_info == NULL) {
1271 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1272 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001273
Jens Wiklander32b31802023-10-06 16:59:46 +02001274 if (MBEDTLS_DECRYPT != ctx->operation) {
1275 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001276 }
1277
Tom Van Eyckc1633172024-04-09 18:44:13 +02001278#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001279 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001280 /* While PSA Crypto has an API for multipart
1281 * operations, we currently don't make it
1282 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001283 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001284 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001285#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001286
Jens Wiklander32b31802023-10-06 16:59:46 +02001287 /* Status to return on a non-authenticated algorithm. */
1288 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001289
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001290#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001291 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001292 size_t output_length;
1293 /* The code here doesn't yet support alternative implementations
1294 * that can delay up to a block of output. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001295
Jens Wiklander32b31802023-10-06 16:59:46 +02001296 if (tag_len > sizeof(check_tag)) {
1297 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1298 }
1299
1300 if (0 != (ret = mbedtls_gcm_finish(
1301 (mbedtls_gcm_context *) ctx->cipher_ctx,
1302 NULL, 0, &output_length,
1303 check_tag, tag_len))) {
1304 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001305 }
1306
1307 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001308 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001309 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1310 goto exit;
1311 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001312 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001313#endif /* MBEDTLS_GCM_C */
1314
1315#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001316 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001317 /* Don't allow truncated MAC for Poly1305 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001318 if (tag_len != sizeof(check_tag)) {
1319 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1320 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001321
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001322 ret = mbedtls_chachapoly_finish(
Jens Wiklander32b31802023-10-06 16:59:46 +02001323 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1324 if (ret != 0) {
1325 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001326 }
1327
1328 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001329 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001330 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1331 goto exit;
1332 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001333 }
1334#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001335
Jerome Forissier039e02d2022-08-09 17:10:15 +02001336exit:
Jens Wiklander32b31802023-10-06 16:59:46 +02001337 mbedtls_platform_zeroize(check_tag, tag_len);
1338 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001339}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001340#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001341
1342/*
1343 * Packet-oriented wrapper for non-AEAD modes
1344 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001345int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1346 const unsigned char *iv, size_t iv_len,
1347 const unsigned char *input, size_t ilen,
1348 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001349{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001351 size_t finish_olen;
1352
Tom Van Eyckc1633172024-04-09 18:44:13 +02001353#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001354 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001355 /* As in the non-PSA case, we don't check that
1356 * a key has been set. If not, the key slot will
1357 * still be in its default state of 0, which is
1358 * guaranteed to be invalid, hence the PSA-call
1359 * below will gracefully fail. */
1360 mbedtls_cipher_context_psa * const cipher_psa =
1361 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1362
1363 psa_status_t status;
1364 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1365 size_t part_len;
1366
Jens Wiklander32b31802023-10-06 16:59:46 +02001367 if (ctx->operation == MBEDTLS_DECRYPT) {
1368 status = psa_cipher_decrypt_setup(&cipher_op,
1369 cipher_psa->slot,
1370 cipher_psa->alg);
1371 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1372 status = psa_cipher_encrypt_setup(&cipher_op,
1373 cipher_psa->slot,
1374 cipher_psa->alg);
1375 } else {
1376 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001377 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001378
1379 /* In the following, we can immediately return on an error,
1380 * because the PSA Crypto API guarantees that cipher operations
1381 * are terminated by unsuccessful calls to psa_cipher_update(),
1382 * and by any call to psa_cipher_finish(). */
Jens Wiklander32b31802023-10-06 16:59:46 +02001383 if (status != PSA_SUCCESS) {
1384 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001385 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001386
Tom Van Eyckc1633172024-04-09 18:44:13 +02001387 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001388 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1389 if (status != PSA_SUCCESS) {
1390 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1391 }
1392 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001393
Jens Wiklander32b31802023-10-06 16:59:46 +02001394 status = psa_cipher_update(&cipher_op,
1395 input, ilen,
1396 output, ilen, olen);
1397 if (status != PSA_SUCCESS) {
1398 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1399 }
1400
1401 status = psa_cipher_finish(&cipher_op,
1402 output + *olen, ilen - *olen,
1403 &part_len);
1404 if (status != PSA_SUCCESS) {
1405 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1406 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001407
1408 *olen += part_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001409 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001410 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001411#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001412
Jens Wiklander32b31802023-10-06 16:59:46 +02001413 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1414 return ret;
1415 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001416
Jens Wiklander32b31802023-10-06 16:59:46 +02001417 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1418 return ret;
1419 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001420
Jens Wiklander32b31802023-10-06 16:59:46 +02001421 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1422 output, olen)) != 0) {
1423 return ret;
1424 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001425
Jens Wiklander32b31802023-10-06 16:59:46 +02001426 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1427 &finish_olen)) != 0) {
1428 return ret;
1429 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001430
1431 *olen += finish_olen;
1432
Jens Wiklander32b31802023-10-06 16:59:46 +02001433 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001434}
1435
1436#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1437/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001438 * Packet-oriented encryption for AEAD modes: internal function used by
1439 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001440 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001441static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1442 const unsigned char *iv, size_t iv_len,
1443 const unsigned char *ad, size_t ad_len,
1444 const unsigned char *input, size_t ilen,
1445 unsigned char *output, size_t *olen,
1446 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001447{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001448#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001449 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001450 /* As in the non-PSA case, we don't check that
1451 * a key has been set. If not, the key slot will
1452 * still be in its default state of 0, which is
1453 * guaranteed to be invalid, hence the PSA-call
1454 * below will gracefully fail. */
1455 mbedtls_cipher_context_psa * const cipher_psa =
1456 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1457
1458 psa_status_t status;
1459
1460 /* PSA Crypto API always writes the authentication tag
1461 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001462 if (output == NULL || tag != output + ilen) {
1463 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1464 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001465
Jens Wiklander32b31802023-10-06 16:59:46 +02001466 status = psa_aead_encrypt(cipher_psa->slot,
1467 cipher_psa->alg,
1468 iv, iv_len,
1469 ad, ad_len,
1470 input, ilen,
1471 output, ilen + tag_len, olen);
1472 if (status != PSA_SUCCESS) {
1473 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1474 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001475
1476 *olen -= tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001477 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001478 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001479#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001480
Jens Wiklander817466c2018-05-22 13:49:31 +02001481#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001482 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001483 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001484 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1485 ilen, iv, iv_len, ad, ad_len,
1486 input, output, tag_len, tag);
Jens Wiklander817466c2018-05-22 13:49:31 +02001487 }
1488#endif /* MBEDTLS_GCM_C */
1489#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001490 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001491 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001492 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1493 iv, iv_len, ad, ad_len, input, output,
1494 tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001495 }
1496#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001497#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001498 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001499 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001500 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001501 (tag_len != 16U)) {
1502 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001503 }
1504
1505 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001506 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1507 ilen, iv, ad, ad_len, input, output, tag);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001508 }
1509#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001510
Jens Wiklander32b31802023-10-06 16:59:46 +02001511 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001512}
1513
1514/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001515 * Packet-oriented encryption for AEAD modes: internal function used by
1516 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001517 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001518static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1519 const unsigned char *iv, size_t iv_len,
1520 const unsigned char *ad, size_t ad_len,
1521 const unsigned char *input, size_t ilen,
1522 unsigned char *output, size_t *olen,
1523 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001524{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001525#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001526 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001527 /* As in the non-PSA case, we don't check that
1528 * a key has been set. If not, the key slot will
1529 * still be in its default state of 0, which is
1530 * guaranteed to be invalid, hence the PSA-call
1531 * below will gracefully fail. */
1532 mbedtls_cipher_context_psa * const cipher_psa =
1533 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1534
1535 psa_status_t status;
1536
1537 /* PSA Crypto API always writes the authentication tag
1538 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001539 if (input == NULL || tag != input + ilen) {
1540 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1541 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001542
Jens Wiklander32b31802023-10-06 16:59:46 +02001543 status = psa_aead_decrypt(cipher_psa->slot,
1544 cipher_psa->alg,
1545 iv, iv_len,
1546 ad, ad_len,
1547 input, ilen + tag_len,
1548 output, ilen, olen);
1549 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1550 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1551 } else if (status != PSA_SUCCESS) {
1552 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1553 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001554
Jens Wiklander32b31802023-10-06 16:59:46 +02001555 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001556 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001557#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001558
Jens Wiklander817466c2018-05-22 13:49:31 +02001559#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001560 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001562
1563 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001564 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1565 iv, iv_len, ad, ad_len,
1566 tag, tag_len, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001567
Jens Wiklander32b31802023-10-06 16:59:46 +02001568 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001569 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001570 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001571
Jens Wiklander32b31802023-10-06 16:59:46 +02001572 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001573 }
1574#endif /* MBEDTLS_GCM_C */
1575#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001576 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001578
1579 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001580 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1581 iv, iv_len, ad, ad_len,
1582 input, output, tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001583
Jens Wiklander32b31802023-10-06 16:59:46 +02001584 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001585 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001586 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001587
Jens Wiklander32b31802023-10-06 16:59:46 +02001588 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001589 }
1590#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001591#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001592 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001594
1595 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001596 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001597 (tag_len != 16U)) {
1598 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001599 }
1600
1601 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001602 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1603 iv, ad, ad_len, tag, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001604
Jens Wiklander32b31802023-10-06 16:59:46 +02001605 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001606 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001607 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001608
Jens Wiklander32b31802023-10-06 16:59:46 +02001609 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001610 }
1611#endif /* MBEDTLS_CHACHAPOLY_C */
Jerome Forissier79013242021-07-28 10:24:04 +02001612
Jens Wiklander32b31802023-10-06 16:59:46 +02001613 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001614}
Jerome Forissier79013242021-07-28 10:24:04 +02001615#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1616
1617#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1618/*
1619 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1620 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001621int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1622 const unsigned char *iv, size_t iv_len,
1623 const unsigned char *ad, size_t ad_len,
1624 const unsigned char *input, size_t ilen,
1625 unsigned char *output, size_t output_len,
1626 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001627{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001628#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001629 if (
Tom Van Eyckc1633172024-04-09 18:44:13 +02001630#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001631 ctx->psa_enabled == 0 &&
1632#endif
Tom Van Eyckc1633172024-04-09 18:44:13 +02001633 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1634 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1635 mbedtls_nist_kw_mode_t mode =
1636 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1637 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001638
Jerome Forissier79013242021-07-28 10:24:04 +02001639 /* There is no iv, tag or ad associated with KW and KWP,
1640 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001641 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1642 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1643 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001644
Jerome Forissier79013242021-07-28 10:24:04 +02001645 (void) iv;
1646 (void) ad;
1647
Jens Wiklander32b31802023-10-06 16:59:46 +02001648 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1649 output, olen, output_len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001650 }
1651#endif /* MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001652
Jerome Forissier79013242021-07-28 10:24:04 +02001653#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1654 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001655 if (output_len < ilen + tag_len) {
1656 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1657 }
Jerome Forissier79013242021-07-28 10:24:04 +02001658
Jens Wiklander32b31802023-10-06 16:59:46 +02001659 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1660 input, ilen, output, olen,
1661 output + ilen, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001662 *olen += tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001663 return ret;
Jerome Forissier79013242021-07-28 10:24:04 +02001664#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001665 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001666#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Jerome Forissier79013242021-07-28 10:24:04 +02001667}
1668
1669/*
1670 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1671 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001672int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1673 const unsigned char *iv, size_t iv_len,
1674 const unsigned char *ad, size_t ad_len,
1675 const unsigned char *input, size_t ilen,
1676 unsigned char *output, size_t output_len,
1677 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001678{
Jerome Forissier79013242021-07-28 10:24:04 +02001679#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001680 if (
Tom Van Eyckc1633172024-04-09 18:44:13 +02001681#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001682 ctx->psa_enabled == 0 &&
1683#endif
Tom Van Eyckc1633172024-04-09 18:44:13 +02001684 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1685 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1686 mbedtls_nist_kw_mode_t mode =
1687 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1688 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier79013242021-07-28 10:24:04 +02001689
1690 /* There is no iv, tag or ad associated with KW and KWP,
1691 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001692 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1693 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1694 }
Jerome Forissier79013242021-07-28 10:24:04 +02001695
1696 (void) iv;
1697 (void) ad;
1698
Jens Wiklander32b31802023-10-06 16:59:46 +02001699 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1700 output, olen, output_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001701 }
1702#endif /* MBEDTLS_NIST_KW_C */
1703
1704#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1705 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001706 if (ilen < tag_len || output_len < ilen - tag_len) {
1707 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1708 }
Jerome Forissier79013242021-07-28 10:24:04 +02001709
Jens Wiklander32b31802023-10-06 16:59:46 +02001710 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1711 input, ilen - tag_len, output, olen,
1712 input + ilen - tag_len, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001713#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001714 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001715#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1716}
1717#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001718
1719#endif /* MBEDTLS_CIPHER_C */