blob: a09540e9087d11204e8e6fabfbc62fa77ff38aa0 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file cipher.c
3 *
Tom Van Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Aibed9eb02018-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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +0200329#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200330
Edison Aibed9eb02018-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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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 Eyckb0563632024-06-13 16:20:14 +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];
Sungbae Yoo4d211f32024-11-19 02:47:55 +0000891 if (padding_len == 0 || padding_len > input_len) {
892 return MBEDTLS_ERR_CIPHER_INVALID_PADDING;
893 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200894 *data_len = input_len - padding_len;
895
Tom Van Eyckb0563632024-06-13 16:20:14 +0200896 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
897 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200898
899 /* The number of bytes checked must be independent of padding_len,
900 * so pick input_len, which is usually 8 or 16 (one block) */
901 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200902 for (i = 0; i < input_len; i++) {
Tom Van Eyckb0563632024-06-13 16:20:14 +0200903 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
904 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
905 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Jens Wiklander32b31802023-10-06 16:59:46 +0200906 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200907
Tom Van Eyckb0563632024-06-13 16:20:14 +0200908 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200909}
910#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
911
912#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
913/*
914 * One and zeros padding: fill with 80 00 ... 00
915 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200916static void add_one_and_zeros_padding(unsigned char *output,
917 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200918{
919 size_t padding_len = output_len - data_len;
920 unsigned char i = 0;
921
922 output[data_len] = 0x80;
Jens Wiklander32b31802023-10-06 16:59:46 +0200923 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200924 output[data_len + i] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200925 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200926}
927
Jens Wiklander32b31802023-10-06 16:59:46 +0200928static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
929 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200930{
Jens Wiklander32b31802023-10-06 16:59:46 +0200931 if (NULL == input || NULL == data_len) {
932 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
933 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200934
Tom Van Eyckb0563632024-06-13 16:20:14 +0200935 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
936 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
937
Jens Wiklander817466c2018-05-22 13:49:31 +0200938 *data_len = 0;
Tom Van Eyckb0563632024-06-13 16:20:14 +0200939
940 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
941 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
942
943 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
944
945 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
946
947 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
948
949 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Jens Wiklander817466c2018-05-22 13:49:31 +0200950 }
951
Tom Van Eyckb0563632024-06-13 16:20:14 +0200952 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200953}
954#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
955
956#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
957/*
958 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
959 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200960static void add_zeros_and_len_padding(unsigned char *output,
961 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200962{
963 size_t padding_len = output_len - data_len;
964 unsigned char i = 0;
965
Jens Wiklander32b31802023-10-06 16:59:46 +0200966 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200967 output[data_len + i - 1] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200968 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200969 output[output_len - 1] = (unsigned char) padding_len;
970}
971
Jens Wiklander32b31802023-10-06 16:59:46 +0200972static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
973 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200974{
975 size_t i, pad_idx;
Tom Van Eyckb0563632024-06-13 16:20:14 +0200976 unsigned char padding_len;
977 mbedtls_ct_condition_t bad;
Jens Wiklander817466c2018-05-22 13:49:31 +0200978
Jens Wiklander32b31802023-10-06 16:59:46 +0200979 if (NULL == input || NULL == data_len) {
980 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
981 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200982
983 padding_len = input[input_len - 1];
984 *data_len = input_len - padding_len;
985
986 /* Avoid logical || since it results in a branch */
Tom Van Eyckb0563632024-06-13 16:20:14 +0200987 bad = mbedtls_ct_uint_gt(padding_len, input_len);
988 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200989
990 /* The number of bytes checked must be independent of padding_len */
991 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200992 for (i = 0; i < input_len - 1; i++) {
Tom Van Eyckb0563632024-06-13 16:20:14 +0200993 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
994 mbedtls_ct_condition_t nonzero_pad_byte;
995 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
996 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Jens Wiklander32b31802023-10-06 16:59:46 +0200997 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200998
Tom Van Eyckb0563632024-06-13 16:20:14 +0200999 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +02001000}
1001#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
1002
1003#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1004/*
1005 * Zero padding: fill with 00 ... 00
1006 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001007static void add_zeros_padding(unsigned char *output,
1008 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001009{
Tom Van Eyckb0563632024-06-13 16:20:14 +02001010 memset(output + data_len, 0, output_len - data_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001011}
1012
Jens Wiklander32b31802023-10-06 16:59:46 +02001013static int get_zeros_padding(unsigned char *input, size_t input_len,
1014 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001015{
1016 size_t i;
Tom Van Eyckb0563632024-06-13 16:20:14 +02001017 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Jens Wiklander817466c2018-05-22 13:49:31 +02001018
Jens Wiklander32b31802023-10-06 16:59:46 +02001019 if (NULL == input || NULL == data_len) {
1020 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001021 }
1022
Jens Wiklander32b31802023-10-06 16:59:46 +02001023 *data_len = 0;
1024 for (i = input_len; i > 0; i--) {
1025 prev_done = done;
Tom Van Eyckb0563632024-06-13 16:20:14 +02001026 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
1027 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Jens Wiklander32b31802023-10-06 16:59:46 +02001028 }
1029
1030 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001031}
1032#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
1033
1034/*
1035 * No padding: don't pad :)
1036 *
1037 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
1038 * but a trivial get_padding function
1039 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001040static int get_no_padding(unsigned char *input, size_t input_len,
1041 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001042{
Jens Wiklander32b31802023-10-06 16:59:46 +02001043 if (NULL == input || NULL == data_len) {
1044 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1045 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001046
1047 *data_len = input_len;
1048
Jens Wiklander32b31802023-10-06 16:59:46 +02001049 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001050}
1051#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1052
Jens Wiklander32b31802023-10-06 16:59:46 +02001053int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1054 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001055{
Jens Wiklander32b31802023-10-06 16:59:46 +02001056 if (ctx->cipher_info == NULL) {
1057 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1058 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001059
Tom Van Eyckb0563632024-06-13 16:20:14 +02001060#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001061 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001062 /* While PSA Crypto has an API for multipart
1063 * operations, we currently don't make it
1064 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001065 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001066 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001067#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001068
Jens Wiklander817466c2018-05-22 13:49:31 +02001069 *olen = 0;
1070
Tom Van Eyckb0563632024-06-13 16:20:14 +02001071#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1072 /* CBC mode requires padding so we make sure a call to
1073 * mbedtls_cipher_set_padding_mode has been done successfully. */
1074 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1075 if (ctx->get_padding == NULL) {
1076 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1077 }
1078 }
1079#endif
1080
1081 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1082 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1083 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1084 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1085 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1086 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1087 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001088 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001089 }
1090
Tom Van Eyckb0563632024-06-13 16:20:14 +02001091 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1092 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001093 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001094 }
1095
Tom Van Eyckb0563632024-06-13 16:20:14 +02001096 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001097 if (ctx->unprocessed_len != 0) {
1098 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1099 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001100
Jens Wiklander32b31802023-10-06 16:59:46 +02001101 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001102 }
1103
1104#if defined(MBEDTLS_CIPHER_MODE_CBC)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001105 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001106 int ret = 0;
1107
Jens Wiklander32b31802023-10-06 16:59:46 +02001108 if (MBEDTLS_ENCRYPT == ctx->operation) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001109 /* check for 'no padding' mode */
Jens Wiklander32b31802023-10-06 16:59:46 +02001110 if (NULL == ctx->add_padding) {
1111 if (0 != ctx->unprocessed_len) {
1112 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1113 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001114
Jens Wiklander32b31802023-10-06 16:59:46 +02001115 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001116 }
1117
Jens Wiklander32b31802023-10-06 16:59:46 +02001118 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1119 ctx->unprocessed_len);
1120 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001121 /*
1122 * For decrypt operations, expect a full block,
1123 * or an empty block if no padding
1124 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001125 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1126 return 0;
1127 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001128
Jens Wiklander32b31802023-10-06 16:59:46 +02001129 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001130 }
1131
1132 /* cipher block */
Tom Van Eyckb0563632024-06-13 16:20:14 +02001133 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
1134 ctx->operation,
1135 mbedtls_cipher_get_block_size(
1136 ctx),
1137 ctx->iv,
1138 ctx->unprocessed_data,
1139 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001140 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001141 }
1142
1143 /* Set output size for decryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001144 if (MBEDTLS_DECRYPT == ctx->operation) {
1145 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1146 olen);
1147 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001148
1149 /* Set output size for encryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001150 *olen = mbedtls_cipher_get_block_size(ctx);
1151 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001152 }
1153#else
1154 ((void) output);
1155#endif /* MBEDTLS_CIPHER_MODE_CBC */
1156
Jens Wiklander32b31802023-10-06 16:59:46 +02001157 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001158}
1159
1160#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Jens Wiklander32b31802023-10-06 16:59:46 +02001161int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1162 mbedtls_cipher_padding_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +02001163{
Tom Van Eyckb0563632024-06-13 16:20:14 +02001164 if (NULL == ctx->cipher_info ||
1165 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001166 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001167 }
1168
Tom Van Eyckb0563632024-06-13 16:20:14 +02001169#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001170 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001171 /* While PSA Crypto knows about CBC padding
1172 * schemes, we currently don't make them
1173 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001174 if (mode != MBEDTLS_PADDING_NONE) {
1175 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1176 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001177
Jens Wiklander32b31802023-10-06 16:59:46 +02001178 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001179 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001180#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001181
Jens Wiklander32b31802023-10-06 16:59:46 +02001182 switch (mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001183#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Jens Wiklander32b31802023-10-06 16:59:46 +02001184 case MBEDTLS_PADDING_PKCS7:
1185 ctx->add_padding = add_pkcs_padding;
1186 ctx->get_padding = get_pkcs_padding;
1187 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001188#endif
1189#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001190 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1191 ctx->add_padding = add_one_and_zeros_padding;
1192 ctx->get_padding = get_one_and_zeros_padding;
1193 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001194#endif
1195#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Jens Wiklander32b31802023-10-06 16:59:46 +02001196 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1197 ctx->add_padding = add_zeros_and_len_padding;
1198 ctx->get_padding = get_zeros_and_len_padding;
1199 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001200#endif
1201#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001202 case MBEDTLS_PADDING_ZEROS:
1203 ctx->add_padding = add_zeros_padding;
1204 ctx->get_padding = get_zeros_padding;
1205 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001206#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001207 case MBEDTLS_PADDING_NONE:
1208 ctx->add_padding = NULL;
1209 ctx->get_padding = get_no_padding;
1210 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001211
Jens Wiklander32b31802023-10-06 16:59:46 +02001212 default:
1213 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001214 }
1215
Jens Wiklander32b31802023-10-06 16:59:46 +02001216 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001217}
1218#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1219
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001220#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001221int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1222 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001223{
Jens Wiklander32b31802023-10-06 16:59:46 +02001224 if (ctx->cipher_info == NULL) {
1225 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1226 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001227
Jens Wiklander32b31802023-10-06 16:59:46 +02001228 if (MBEDTLS_ENCRYPT != ctx->operation) {
1229 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1230 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001231
Tom Van Eyckb0563632024-06-13 16:20:14 +02001232#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001233 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001234 /* While PSA Crypto has an API for multipart
1235 * operations, we currently don't make it
1236 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001237 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001238 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001239#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001240
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001241#if defined(MBEDTLS_GCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001242 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +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 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1247 NULL, 0, &output_length,
1248 tag, tag_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001249 }
1250#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001251
Jens Wiklander32b31802023-10-06 16:59:46 +02001252#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001253 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001254 /* Don't allow truncated MAC for Poly1305 */
1255 if (tag_len != 16U) {
1256 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1257 }
1258
1259 return mbedtls_chachapoly_finish(
1260 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1261 }
1262#endif
1263
1264 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001265}
1266
Jens Wiklander32b31802023-10-06 16:59:46 +02001267int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1268 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001269{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001270 unsigned char check_tag[16];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001272
Jens Wiklander32b31802023-10-06 16:59:46 +02001273 if (ctx->cipher_info == NULL) {
1274 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1275 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001276
Jens Wiklander32b31802023-10-06 16:59:46 +02001277 if (MBEDTLS_DECRYPT != ctx->operation) {
1278 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001279 }
1280
Tom Van Eyckb0563632024-06-13 16:20:14 +02001281#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001282 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001283 /* While PSA Crypto has an API for multipart
1284 * operations, we currently don't make it
1285 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001286 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001287 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001288#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001289
Jens Wiklander32b31802023-10-06 16:59:46 +02001290 /* Status to return on a non-authenticated algorithm. */
1291 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001292
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001293#if defined(MBEDTLS_GCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001294 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001295 size_t output_length;
1296 /* The code here doesn't yet support alternative implementations
1297 * that can delay up to a block of output. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001298
Jens Wiklander32b31802023-10-06 16:59:46 +02001299 if (tag_len > sizeof(check_tag)) {
1300 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1301 }
1302
1303 if (0 != (ret = mbedtls_gcm_finish(
1304 (mbedtls_gcm_context *) ctx->cipher_ctx,
1305 NULL, 0, &output_length,
1306 check_tag, tag_len))) {
1307 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001308 }
1309
1310 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001311 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001312 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1313 goto exit;
1314 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001315 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001316#endif /* MBEDTLS_GCM_C */
1317
1318#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001319 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001320 /* Don't allow truncated MAC for Poly1305 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001321 if (tag_len != sizeof(check_tag)) {
1322 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1323 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001324
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001325 ret = mbedtls_chachapoly_finish(
Jens Wiklander32b31802023-10-06 16:59:46 +02001326 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1327 if (ret != 0) {
1328 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001329 }
1330
1331 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001332 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001333 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1334 goto exit;
1335 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001336 }
1337#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001338
Jerome Forissier039e02d2022-08-09 17:10:15 +02001339exit:
Jens Wiklander32b31802023-10-06 16:59:46 +02001340 mbedtls_platform_zeroize(check_tag, tag_len);
1341 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001342}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001343#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001344
1345/*
1346 * Packet-oriented wrapper for non-AEAD modes
1347 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001348int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1349 const unsigned char *iv, size_t iv_len,
1350 const unsigned char *input, size_t ilen,
1351 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001352{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001354 size_t finish_olen;
1355
Tom Van Eyckb0563632024-06-13 16:20:14 +02001356#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001357 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001358 /* As in the non-PSA case, we don't check that
1359 * a key has been set. If not, the key slot will
1360 * still be in its default state of 0, which is
1361 * guaranteed to be invalid, hence the PSA-call
1362 * below will gracefully fail. */
1363 mbedtls_cipher_context_psa * const cipher_psa =
1364 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1365
1366 psa_status_t status;
1367 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1368 size_t part_len;
1369
Jens Wiklander32b31802023-10-06 16:59:46 +02001370 if (ctx->operation == MBEDTLS_DECRYPT) {
1371 status = psa_cipher_decrypt_setup(&cipher_op,
1372 cipher_psa->slot,
1373 cipher_psa->alg);
1374 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1375 status = psa_cipher_encrypt_setup(&cipher_op,
1376 cipher_psa->slot,
1377 cipher_psa->alg);
1378 } else {
1379 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001380 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001381
1382 /* In the following, we can immediately return on an error,
1383 * because the PSA Crypto API guarantees that cipher operations
1384 * are terminated by unsuccessful calls to psa_cipher_update(),
1385 * and by any call to psa_cipher_finish(). */
Jens Wiklander32b31802023-10-06 16:59:46 +02001386 if (status != PSA_SUCCESS) {
1387 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001388 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001389
Tom Van Eyckb0563632024-06-13 16:20:14 +02001390 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001391 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1392 if (status != PSA_SUCCESS) {
1393 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1394 }
1395 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001396
Jens Wiklander32b31802023-10-06 16:59:46 +02001397 status = psa_cipher_update(&cipher_op,
1398 input, ilen,
1399 output, ilen, olen);
1400 if (status != PSA_SUCCESS) {
1401 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1402 }
1403
1404 status = psa_cipher_finish(&cipher_op,
1405 output + *olen, ilen - *olen,
1406 &part_len);
1407 if (status != PSA_SUCCESS) {
1408 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1409 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001410
1411 *olen += part_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001412 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001413 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001414#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001415
Jens Wiklander32b31802023-10-06 16:59:46 +02001416 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1417 return ret;
1418 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001419
Jens Wiklander32b31802023-10-06 16:59:46 +02001420 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1421 return ret;
1422 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001423
Jens Wiklander32b31802023-10-06 16:59:46 +02001424 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1425 output, olen)) != 0) {
1426 return ret;
1427 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001428
Jens Wiklander32b31802023-10-06 16:59:46 +02001429 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1430 &finish_olen)) != 0) {
1431 return ret;
1432 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001433
1434 *olen += finish_olen;
1435
Jens Wiklander32b31802023-10-06 16:59:46 +02001436 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001437}
1438
1439#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1440/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001441 * Packet-oriented encryption for AEAD modes: internal function used by
1442 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001443 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001444static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1445 const unsigned char *iv, size_t iv_len,
1446 const unsigned char *ad, size_t ad_len,
1447 const unsigned char *input, size_t ilen,
1448 unsigned char *output, size_t *olen,
1449 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001450{
Tom Van Eyckb0563632024-06-13 16:20:14 +02001451#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001452 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001453 /* As in the non-PSA case, we don't check that
1454 * a key has been set. If not, the key slot will
1455 * still be in its default state of 0, which is
1456 * guaranteed to be invalid, hence the PSA-call
1457 * below will gracefully fail. */
1458 mbedtls_cipher_context_psa * const cipher_psa =
1459 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1460
1461 psa_status_t status;
1462
1463 /* PSA Crypto API always writes the authentication tag
1464 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001465 if (output == NULL || tag != output + ilen) {
1466 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1467 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001468
Jens Wiklander32b31802023-10-06 16:59:46 +02001469 status = psa_aead_encrypt(cipher_psa->slot,
1470 cipher_psa->alg,
1471 iv, iv_len,
1472 ad, ad_len,
1473 input, ilen,
1474 output, ilen + tag_len, olen);
1475 if (status != PSA_SUCCESS) {
1476 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1477 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001478
1479 *olen -= tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001480 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001481 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001482#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001483
Jens Wiklander817466c2018-05-22 13:49:31 +02001484#if defined(MBEDTLS_GCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001485 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001486 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001487 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1488 ilen, iv, iv_len, ad, ad_len,
1489 input, output, tag_len, tag);
Jens Wiklander817466c2018-05-22 13:49:31 +02001490 }
1491#endif /* MBEDTLS_GCM_C */
1492#if defined(MBEDTLS_CCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001493 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001494 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001495 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1496 iv, iv_len, ad, ad_len, input, output,
1497 tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001498 }
1499#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001500#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001501 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001502 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckb0563632024-06-13 16:20:14 +02001503 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001504 (tag_len != 16U)) {
1505 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001506 }
1507
1508 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001509 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1510 ilen, iv, ad, ad_len, input, output, tag);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001511 }
1512#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001513
Jens Wiklander32b31802023-10-06 16:59:46 +02001514 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001515}
1516
1517/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001518 * Packet-oriented encryption for AEAD modes: internal function used by
1519 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001520 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001521static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1522 const unsigned char *iv, size_t iv_len,
1523 const unsigned char *ad, size_t ad_len,
1524 const unsigned char *input, size_t ilen,
1525 unsigned char *output, size_t *olen,
1526 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001527{
Tom Van Eyckb0563632024-06-13 16:20:14 +02001528#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001529 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001530 /* As in the non-PSA case, we don't check that
1531 * a key has been set. If not, the key slot will
1532 * still be in its default state of 0, which is
1533 * guaranteed to be invalid, hence the PSA-call
1534 * below will gracefully fail. */
1535 mbedtls_cipher_context_psa * const cipher_psa =
1536 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1537
1538 psa_status_t status;
1539
1540 /* PSA Crypto API always writes the authentication tag
1541 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001542 if (input == NULL || tag != input + ilen) {
1543 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1544 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001545
Jens Wiklander32b31802023-10-06 16:59:46 +02001546 status = psa_aead_decrypt(cipher_psa->slot,
1547 cipher_psa->alg,
1548 iv, iv_len,
1549 ad, ad_len,
1550 input, ilen + tag_len,
1551 output, ilen, olen);
1552 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1553 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1554 } else if (status != PSA_SUCCESS) {
1555 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1556 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001557
Jens Wiklander32b31802023-10-06 16:59:46 +02001558 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001559 }
Tom Van Eyckb0563632024-06-13 16:20:14 +02001560#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001561
Jens Wiklander817466c2018-05-22 13:49:31 +02001562#if defined(MBEDTLS_GCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001563 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001565
1566 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001567 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1568 iv, iv_len, ad, ad_len,
1569 tag, tag_len, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001570
Jens Wiklander32b31802023-10-06 16:59:46 +02001571 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001572 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001573 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001574
Jens Wiklander32b31802023-10-06 16:59:46 +02001575 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001576 }
1577#endif /* MBEDTLS_GCM_C */
1578#if defined(MBEDTLS_CCM_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001579 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001581
1582 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001583 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1584 iv, iv_len, ad, ad_len,
1585 input, output, tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001586
Jens Wiklander32b31802023-10-06 16:59:46 +02001587 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001588 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001589 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001590
Jens Wiklander32b31802023-10-06 16:59:46 +02001591 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001592 }
1593#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001594#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckb0563632024-06-13 16:20:14 +02001595 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001597
1598 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckb0563632024-06-13 16:20:14 +02001599 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001600 (tag_len != 16U)) {
1601 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001602 }
1603
1604 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001605 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1606 iv, ad, ad_len, tag, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001607
Jens Wiklander32b31802023-10-06 16:59:46 +02001608 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001609 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001610 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001611
Jens Wiklander32b31802023-10-06 16:59:46 +02001612 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001613 }
1614#endif /* MBEDTLS_CHACHAPOLY_C */
Jerome Forissier79013242021-07-28 10:24:04 +02001615
Jens Wiklander32b31802023-10-06 16:59:46 +02001616 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001617}
Jerome Forissier79013242021-07-28 10:24:04 +02001618#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1619
1620#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1621/*
1622 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1623 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001624int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1625 const unsigned char *iv, size_t iv_len,
1626 const unsigned char *ad, size_t ad_len,
1627 const unsigned char *input, size_t ilen,
1628 unsigned char *output, size_t output_len,
1629 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001630{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001631#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001632 if (
Tom Van Eyckb0563632024-06-13 16:20:14 +02001633#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001634 ctx->psa_enabled == 0 &&
1635#endif
Tom Van Eyckb0563632024-06-13 16:20:14 +02001636 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1637 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1638 mbedtls_nist_kw_mode_t mode =
1639 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1640 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001641
Jerome Forissier79013242021-07-28 10:24:04 +02001642 /* There is no iv, tag or ad associated with KW and KWP,
1643 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001644 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1645 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1646 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001647
Jerome Forissier79013242021-07-28 10:24:04 +02001648 (void) iv;
1649 (void) ad;
1650
Jens Wiklander32b31802023-10-06 16:59:46 +02001651 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1652 output, olen, output_len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001653 }
1654#endif /* MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001655
Jerome Forissier79013242021-07-28 10:24:04 +02001656#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1657 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001658 if (output_len < ilen + tag_len) {
1659 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1660 }
Jerome Forissier79013242021-07-28 10:24:04 +02001661
Jens Wiklander32b31802023-10-06 16:59:46 +02001662 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1663 input, ilen, output, olen,
1664 output + ilen, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001665 *olen += tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001666 return ret;
Jerome Forissier79013242021-07-28 10:24:04 +02001667#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001668 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001669#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Jerome Forissier79013242021-07-28 10:24:04 +02001670}
1671
1672/*
1673 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1674 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001675int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1676 const unsigned char *iv, size_t iv_len,
1677 const unsigned char *ad, size_t ad_len,
1678 const unsigned char *input, size_t ilen,
1679 unsigned char *output, size_t output_len,
1680 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001681{
Jerome Forissier79013242021-07-28 10:24:04 +02001682#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001683 if (
Tom Van Eyckb0563632024-06-13 16:20:14 +02001684#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001685 ctx->psa_enabled == 0 &&
1686#endif
Tom Van Eyckb0563632024-06-13 16:20:14 +02001687 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1688 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1689 mbedtls_nist_kw_mode_t mode =
1690 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1691 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier79013242021-07-28 10:24:04 +02001692
1693 /* There is no iv, tag or ad associated with KW and KWP,
1694 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001695 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1696 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1697 }
Jerome Forissier79013242021-07-28 10:24:04 +02001698
1699 (void) iv;
1700 (void) ad;
1701
Jens Wiklander32b31802023-10-06 16:59:46 +02001702 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1703 output, olen, output_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001704 }
1705#endif /* MBEDTLS_NIST_KW_C */
1706
1707#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1708 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001709 if (ilen < tag_len || output_len < ilen - tag_len) {
1710 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1711 }
Jerome Forissier79013242021-07-28 10:24:04 +02001712
Jens Wiklander32b31802023-10-06 16:59:46 +02001713 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1714 input, ilen - tag_len, output, olen,
1715 input + ilen - tag_len, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001716#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001717 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001718#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1719}
1720#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001721
1722#endif /* MBEDTLS_CIPHER_C */