blob: 0683677edaa47242d302130824272986fe96e588 [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
Jens Wiklander32b31802023-10-06 16:59:46 +0200245int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
246 const mbedtls_cipher_info_t *cipher_info)
Jens Wiklander817466c2018-05-22 13:49:31 +0200247{
Jens Wiklander32b31802023-10-06 16:59:46 +0200248 if (cipher_info == NULL) {
249 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
250 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200251
Jens Wiklander32b31802023-10-06 16:59:46 +0200252 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200253
Tom Van Eyckc1633172024-04-09 18:44:13 +0200254 if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) {
255 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
256 if (ctx->cipher_ctx == NULL) {
257 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
258 }
Jens Wiklander32b31802023-10-06 16:59:46 +0200259 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200260
261 ctx->cipher_info = cipher_info;
262
Jens Wiklander32b31802023-10-06 16:59:46 +0200263 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200264}
265
Tom Van Eyckc1633172024-04-09 18:44:13 +0200266#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200267int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
268 const mbedtls_cipher_info_t *cipher_info,
269 size_t taglen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200270{
271 psa_algorithm_t alg;
272 mbedtls_cipher_context_psa *cipher_psa;
273
Jens Wiklander32b31802023-10-06 16:59:46 +0200274 if (NULL == cipher_info || NULL == ctx) {
275 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
276 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200277
278 /* Check that the underlying cipher mode and cipher type are
279 * supported by the underlying PSA Crypto implementation. */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200280 alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen);
Jens Wiklander32b31802023-10-06 16:59:46 +0200281 if (alg == 0) {
282 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
283 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200284 if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200285 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
286 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200287
Jens Wiklander32b31802023-10-06 16:59:46 +0200288 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200289
Jens Wiklander32b31802023-10-06 16:59:46 +0200290 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
291 if (cipher_psa == NULL) {
292 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
293 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200294 cipher_psa->alg = alg;
295 ctx->cipher_ctx = cipher_psa;
296 ctx->cipher_info = cipher_info;
297 ctx->psa_enabled = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200298 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200299}
Tom Van Eyckc1633172024-04-09 18:44:13 +0200300#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Edison Ai12484fc2018-12-19 15:36:28 +0800301
Jens Wiklander32b31802023-10-06 16:59:46 +0200302int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
303 const unsigned char *key,
304 int key_bitlen,
305 const mbedtls_operation_t operation)
Jens Wiklander817466c2018-05-22 13:49:31 +0200306{
Jens Wiklander32b31802023-10-06 16:59:46 +0200307 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
308 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
309 }
310 if (ctx->cipher_info == NULL) {
311 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
312 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200313#if defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
314 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
315 MBEDTLS_DECRYPT == operation) {
316 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
317 }
318#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200319
Tom Van Eyckc1633172024-04-09 18:44:13 +0200320#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200321 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200322 mbedtls_cipher_context_psa * const cipher_psa =
323 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
324
Jens Wiklander32b31802023-10-06 16:59:46 +0200325 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200326
327 psa_status_t status;
328 psa_key_type_t key_type;
329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
330
331 /* PSA Crypto API only accepts byte-aligned keys. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200332 if (key_bitlen % 8 != 0) {
333 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
334 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200335
336 /* Don't allow keys to be set multiple times. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200337 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
338 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
339 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200340
341 key_type = mbedtls_psa_translate_cipher_type(
Tom Van Eyckc1633172024-04-09 18:44:13 +0200342 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
Jens Wiklander32b31802023-10-06 16:59:46 +0200343 if (key_type == 0) {
344 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
345 }
346 psa_set_key_type(&attributes, key_type);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200347
348 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
349 * (encrypt vs. decrypt): it is possible to setup a key for encryption
350 * and use it for AEAD decryption. Until tests relying on this
351 * are changed, allow any usage in PSA. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200352 psa_set_key_usage_flags(&attributes,
Jens Wiklander32b31802023-10-06 16:59:46 +0200353 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
354 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200355
Jens Wiklander32b31802023-10-06 16:59:46 +0200356 status = psa_import_key(&attributes, key, key_bytelen,
357 &cipher_psa->slot);
358 switch (status) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200359 case PSA_SUCCESS:
360 break;
361 case PSA_ERROR_INSUFFICIENT_MEMORY:
Jens Wiklander32b31802023-10-06 16:59:46 +0200362 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200363 case PSA_ERROR_NOT_SUPPORTED:
Jens Wiklander32b31802023-10-06 16:59:46 +0200364 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200365 default:
Jens Wiklander32b31802023-10-06 16:59:46 +0200366 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200367 }
368 /* Indicate that we own the key slot and need to
369 * destroy it in mbedtls_cipher_free(). */
370 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
371
372 ctx->key_bitlen = key_bitlen;
373 ctx->operation = operation;
Jens Wiklander32b31802023-10-06 16:59:46 +0200374 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200375 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200376#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200377
Jens Wiklander32b31802023-10-06 16:59:46 +0200378 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
Tom Van Eyckc1633172024-04-09 18:44:13 +0200379 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200380 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200381 }
382
383 ctx->key_bitlen = key_bitlen;
384 ctx->operation = operation;
385
Tom Van Eyckc1633172024-04-09 18:44:13 +0200386#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Jens Wiklander817466c2018-05-22 13:49:31 +0200387 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100388 * For OFB, CFB and CTR mode always use the encryption key schedule
Jens Wiklander817466c2018-05-22 13:49:31 +0200389 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200390 if (MBEDTLS_ENCRYPT == operation ||
Tom Van Eyckc1633172024-04-09 18:44:13 +0200391 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
392 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
393 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
394 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
395 ctx->key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200396 }
397
Jens Wiklander32b31802023-10-06 16:59:46 +0200398 if (MBEDTLS_DECRYPT == operation) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200399 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
400 ctx->key_bitlen);
Jens Wiklander32b31802023-10-06 16:59:46 +0200401 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200402#else
403 if (operation == MBEDTLS_ENCRYPT || operation == MBEDTLS_DECRYPT) {
404 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
405 ctx->key_bitlen);
406 }
407#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200408
Jens Wiklander32b31802023-10-06 16:59:46 +0200409 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200410}
411
Jens Wiklander32b31802023-10-06 16:59:46 +0200412int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
413 const unsigned char *iv,
414 size_t iv_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200415{
416 size_t actual_iv_size;
417
Jens Wiklander32b31802023-10-06 16:59:46 +0200418 if (ctx->cipher_info == NULL) {
419 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
420 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200421#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200422 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200423 /* While PSA Crypto has an API for multipart
424 * operations, we currently don't make it
425 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200426 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200427 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200428#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jens Wiklander817466c2018-05-22 13:49:31 +0200429
430 /* avoid buffer overflow in ctx->iv */
Jens Wiklander32b31802023-10-06 16:59:46 +0200431 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
432 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
433 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200434
Jens Wiklander32b31802023-10-06 16:59:46 +0200435 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200436 actual_iv_size = iv_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200437 } else {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200438 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
Jens Wiklander817466c2018-05-22 13:49:31 +0200439
440 /* avoid reading past the end of input buffer */
Jens Wiklander32b31802023-10-06 16:59:46 +0200441 if (actual_iv_size > iv_len) {
442 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
443 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200444 }
445
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100446#if defined(MBEDTLS_CHACHA20_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200447 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
Jerome Forissier039e02d2022-08-09 17:10:15 +0200448 /* Even though the actual_iv_size is overwritten with a correct value
449 * of 12 from the cipher info, return an error to indicate that
450 * the input iv_len is wrong. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200451 if (iv_len != 12) {
452 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
453 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200454
Jens Wiklander32b31802023-10-06 16:59:46 +0200455 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
456 iv,
457 0U)) { /* Initial counter value */
458 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100459 }
460 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200461#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200462 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200463 iv_len != 12) {
464 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
465 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200466#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100467#endif
468
Jens Wiklander32b31802023-10-06 16:59:46 +0200469#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200470 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200471 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
472 ctx->operation,
473 iv, iv_len);
474 }
475#endif
476
477#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200478 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200479 int set_lengths_result;
480 int ccm_star_mode;
481
482 set_lengths_result = mbedtls_ccm_set_lengths(
483 (mbedtls_ccm_context *) ctx->cipher_ctx,
484 0, 0, 0);
485 if (set_lengths_result != 0) {
486 return set_lengths_result;
487 }
488
489 if (ctx->operation == MBEDTLS_DECRYPT) {
490 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
491 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
492 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
493 } else {
494 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
495 }
496
497 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
498 ccm_star_mode,
499 iv, iv_len);
500 }
501#endif
502
503 if (actual_iv_size != 0) {
504 memcpy(ctx->iv, iv, actual_iv_size);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100505 ctx->iv_size = actual_iv_size;
506 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200507
Jens Wiklander32b31802023-10-06 16:59:46 +0200508 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200509}
510
Jens Wiklander32b31802023-10-06 16:59:46 +0200511int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200512{
Jens Wiklander32b31802023-10-06 16:59:46 +0200513 if (ctx->cipher_info == NULL) {
514 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
515 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200516
Tom Van Eyckc1633172024-04-09 18:44:13 +0200517#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200518 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200519 /* We don't support resetting PSA-based
520 * cipher contexts, yet. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200521 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200522 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200523#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200524
Jens Wiklander817466c2018-05-22 13:49:31 +0200525 ctx->unprocessed_len = 0;
526
Jens Wiklander32b31802023-10-06 16:59:46 +0200527 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200528}
529
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100530#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200531int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
532 const unsigned char *ad, size_t ad_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200533{
Jens Wiklander32b31802023-10-06 16:59:46 +0200534 if (ctx->cipher_info == NULL) {
535 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
536 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200537
Tom Van Eyckc1633172024-04-09 18:44:13 +0200538#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200539 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200540 /* While PSA Crypto has an API for multipart
541 * operations, we currently don't make it
542 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200543 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200544 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200545#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200546
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100547#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200548 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200549 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
550 ad, ad_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200551 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100552#endif
553
554#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200555 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100556 int result;
557 mbedtls_chachapoly_mode_t mode;
558
Jens Wiklander32b31802023-10-06 16:59:46 +0200559 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100560 ? MBEDTLS_CHACHAPOLY_ENCRYPT
561 : MBEDTLS_CHACHAPOLY_DECRYPT;
562
Jens Wiklander32b31802023-10-06 16:59:46 +0200563 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
564 ctx->iv,
565 mode);
566 if (result != 0) {
567 return result;
568 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100569
Jens Wiklander32b31802023-10-06 16:59:46 +0200570 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
571 ad, ad_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100572 }
573#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200574
Jens Wiklander32b31802023-10-06 16:59:46 +0200575 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200576}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100577#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200578
Jens Wiklander32b31802023-10-06 16:59:46 +0200579int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
580 size_t ilen, unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200581{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100583 size_t block_size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200584
Jens Wiklander32b31802023-10-06 16:59:46 +0200585 if (ctx->cipher_info == NULL) {
586 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
587 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200588
Tom Van Eyckc1633172024-04-09 18:44:13 +0200589#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200590 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200591 /* While PSA Crypto has an API for multipart
592 * operations, we currently don't make it
593 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200594 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200595 }
Tom Van Eyckc1633172024-04-09 18:44:13 +0200596#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200597
Jens Wiklander817466c2018-05-22 13:49:31 +0200598 *olen = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200599 block_size = mbedtls_cipher_get_block_size(ctx);
600 if (0 == block_size) {
601 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200602 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200603
Tom Van Eyckc1633172024-04-09 18:44:13 +0200604 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200605 if (ilen != block_size) {
606 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
607 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200608
609 *olen = ilen;
610
Tom Van Eyckc1633172024-04-09 18:44:13 +0200611 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
612 ctx->operation, input,
613 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200614 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200615 }
616
Jens Wiklander32b31802023-10-06 16:59:46 +0200617 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200618 }
619
620#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200621 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200622 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
623 input, ilen,
624 output, ilen, olen);
625 }
626#endif
627
628#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200629 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200630 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
631 input, ilen,
632 output, ilen, olen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100633 }
634#endif
635
636#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200637 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100638 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200639 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
640 ilen, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200641 }
642#endif
643
Jens Wiklander32b31802023-10-06 16:59:46 +0200644 if (input == output &&
645 (ctx->unprocessed_len != 0 || ilen % block_size)) {
646 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200647 }
648
649#if defined(MBEDTLS_CIPHER_MODE_CBC)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200650 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200651 size_t copy_len = 0;
652
653 /*
654 * If there is not enough data for a full block, cache it.
655 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200656 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
657 ilen <= block_size - ctx->unprocessed_len) ||
658 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
659 ilen < block_size - ctx->unprocessed_len) ||
660 (ctx->operation == MBEDTLS_ENCRYPT &&
661 ilen < block_size - ctx->unprocessed_len)) {
662 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
663 ilen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200664
665 ctx->unprocessed_len += ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200666 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200667 }
668
669 /*
670 * Process cached data first
671 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200672 if (0 != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200673 copy_len = block_size - ctx->unprocessed_len;
674
Jens Wiklander32b31802023-10-06 16:59:46 +0200675 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
676 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200677
Tom Van Eyckc1633172024-04-09 18:44:13 +0200678 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
679 ctx->operation,
680 block_size, ctx->iv,
681 ctx->
682 unprocessed_data,
683 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200684 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200685 }
686
687 *olen += block_size;
688 output += block_size;
689 ctx->unprocessed_len = 0;
690
691 input += copy_len;
692 ilen -= copy_len;
693 }
694
695 /*
696 * Cache final, incomplete block
697 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200698 if (0 != ilen) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100699 /* Encryption: only cache partial blocks
700 * Decryption w/ padding: always keep at least one whole block
701 * Decryption w/o padding: only cache partial blocks
702 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200703 copy_len = ilen % block_size;
Jens Wiklander32b31802023-10-06 16:59:46 +0200704 if (copy_len == 0 &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100705 ctx->operation == MBEDTLS_DECRYPT &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200706 NULL != ctx->add_padding) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200707 copy_len = block_size;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100708 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200709
Jens Wiklander32b31802023-10-06 16:59:46 +0200710 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
711 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200712
713 ctx->unprocessed_len += copy_len;
714 ilen -= copy_len;
715 }
716
717 /*
718 * Process remaining full blocks
719 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200720 if (ilen) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200721 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
722 ctx->operation,
723 ilen, ctx->iv,
724 input,
725 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200726 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200727 }
728
729 *olen += ilen;
730 }
731
Jens Wiklander32b31802023-10-06 16:59:46 +0200732 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200733 }
734#endif /* MBEDTLS_CIPHER_MODE_CBC */
735
736#if defined(MBEDTLS_CIPHER_MODE_CFB)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200737 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
738 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
739 ctx->operation, ilen,
740 &ctx->unprocessed_len,
741 ctx->iv,
742 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200743 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200744 }
745
746 *olen = ilen;
747
Jens Wiklander32b31802023-10-06 16:59:46 +0200748 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200749 }
750#endif /* MBEDTLS_CIPHER_MODE_CFB */
751
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100752#if defined(MBEDTLS_CIPHER_MODE_OFB)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200753 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
754 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
755 ilen,
756 &ctx->unprocessed_len,
757 ctx->iv,
758 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200759 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100760 }
761
762 *olen = ilen;
763
Jens Wiklander32b31802023-10-06 16:59:46 +0200764 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100765 }
766#endif /* MBEDTLS_CIPHER_MODE_OFB */
767
Jens Wiklander817466c2018-05-22 13:49:31 +0200768#if defined(MBEDTLS_CIPHER_MODE_CTR)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200769 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
770 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
771 ilen,
772 &ctx->unprocessed_len,
773 ctx->iv,
774 ctx->unprocessed_data,
775 input, output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200776 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200777 }
778
779 *olen = ilen;
780
Jens Wiklander32b31802023-10-06 16:59:46 +0200781 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200782 }
783#endif /* MBEDTLS_CIPHER_MODE_CTR */
784
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100785#if defined(MBEDTLS_CIPHER_MODE_XTS)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200786 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200787 if (ctx->unprocessed_len > 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100788 /* We can only process an entire data unit at a time. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200789 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100790 }
791
Tom Van Eyckc1633172024-04-09 18:44:13 +0200792 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
793 ctx->operation,
794 ilen,
795 ctx->iv,
796 input,
797 output);
Jens Wiklander32b31802023-10-06 16:59:46 +0200798 if (ret != 0) {
799 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100800 }
801
802 *olen = ilen;
803
Jens Wiklander32b31802023-10-06 16:59:46 +0200804 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100805 }
806#endif /* MBEDTLS_CIPHER_MODE_XTS */
807
Jens Wiklander817466c2018-05-22 13:49:31 +0200808#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Tom Van Eyckc1633172024-04-09 18:44:13 +0200809 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
810 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
811 ilen, input,
812 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200813 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200814 }
815
816 *olen = ilen;
817
Jens Wiklander32b31802023-10-06 16:59:46 +0200818 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200819 }
820#endif /* MBEDTLS_CIPHER_MODE_STREAM */
821
Jens Wiklander32b31802023-10-06 16:59:46 +0200822 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200823}
824
825#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
826#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
827/*
828 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
829 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200830static void add_pkcs_padding(unsigned char *output, size_t output_len,
831 size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200832{
833 size_t padding_len = output_len - data_len;
834 unsigned char i;
835
Jens Wiklander32b31802023-10-06 16:59:46 +0200836 for (i = 0; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200837 output[data_len + i] = (unsigned char) padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200838 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200839}
840
Jens Wiklander32b31802023-10-06 16:59:46 +0200841static int get_pkcs_padding(unsigned char *input, size_t input_len,
842 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200843{
844 size_t i, pad_idx;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200845 unsigned char padding_len;
Jens Wiklander817466c2018-05-22 13:49:31 +0200846
Jens Wiklander32b31802023-10-06 16:59:46 +0200847 if (NULL == input || NULL == data_len) {
848 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
849 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200850
851 padding_len = input[input_len - 1];
852 *data_len = input_len - padding_len;
853
Tom Van Eyckc1633172024-04-09 18:44:13 +0200854 mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len);
855 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200856
857 /* The number of bytes checked must be independent of padding_len,
858 * so pick input_len, which is usually 8 or 16 (one block) */
859 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200860 for (i = 0; i < input_len; i++) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200861 mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx);
862 mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len);
863 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different));
Jens Wiklander32b31802023-10-06 16:59:46 +0200864 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200865
Tom Van Eyckc1633172024-04-09 18:44:13 +0200866 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200867}
868#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
869
870#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
871/*
872 * One and zeros padding: fill with 80 00 ... 00
873 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200874static void add_one_and_zeros_padding(unsigned char *output,
875 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200876{
877 size_t padding_len = output_len - data_len;
878 unsigned char i = 0;
879
880 output[data_len] = 0x80;
Jens Wiklander32b31802023-10-06 16:59:46 +0200881 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200882 output[data_len + i] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200883 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200884}
885
Jens Wiklander32b31802023-10-06 16:59:46 +0200886static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
887 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200888{
Jens Wiklander32b31802023-10-06 16:59:46 +0200889 if (NULL == input || NULL == data_len) {
890 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
891 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200892
Tom Van Eyckc1633172024-04-09 18:44:13 +0200893 mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE;
894 mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE;
895
Jens Wiklander817466c2018-05-22 13:49:31 +0200896 *data_len = 0;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200897
898 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
899 mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]);
900
901 mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding);
902
903 *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len);
904
905 bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad);
906
907 in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero));
Jens Wiklander817466c2018-05-22 13:49:31 +0200908 }
909
Tom Van Eyckc1633172024-04-09 18:44:13 +0200910 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200911}
912#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
913
914#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
915/*
916 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
917 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200918static void add_zeros_and_len_padding(unsigned char *output,
919 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200920{
921 size_t padding_len = output_len - data_len;
922 unsigned char i = 0;
923
Jens Wiklander32b31802023-10-06 16:59:46 +0200924 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200925 output[data_len + i - 1] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200926 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200927 output[output_len - 1] = (unsigned char) padding_len;
928}
929
Jens Wiklander32b31802023-10-06 16:59:46 +0200930static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
931 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200932{
933 size_t i, pad_idx;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200934 unsigned char padding_len;
935 mbedtls_ct_condition_t bad;
Jens Wiklander817466c2018-05-22 13:49:31 +0200936
Jens Wiklander32b31802023-10-06 16:59:46 +0200937 if (NULL == input || NULL == data_len) {
938 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
939 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200940
941 padding_len = input[input_len - 1];
942 *data_len = input_len - padding_len;
943
944 /* Avoid logical || since it results in a branch */
Tom Van Eyckc1633172024-04-09 18:44:13 +0200945 bad = mbedtls_ct_uint_gt(padding_len, input_len);
946 bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0));
Jens Wiklander817466c2018-05-22 13:49:31 +0200947
948 /* The number of bytes checked must be independent of padding_len */
949 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200950 for (i = 0; i < input_len - 1; i++) {
Tom Van Eyckc1633172024-04-09 18:44:13 +0200951 mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx);
952 mbedtls_ct_condition_t nonzero_pad_byte;
953 nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i]));
954 bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte);
Jens Wiklander32b31802023-10-06 16:59:46 +0200955 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200956
Tom Van Eyckc1633172024-04-09 18:44:13 +0200957 return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING);
Jens Wiklander817466c2018-05-22 13:49:31 +0200958}
959#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
960
961#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
962/*
963 * Zero padding: fill with 00 ... 00
964 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200965static void add_zeros_padding(unsigned char *output,
966 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200967{
Tom Van Eyckc1633172024-04-09 18:44:13 +0200968 memset(output + data_len, 0, output_len - data_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200969}
970
Jens Wiklander32b31802023-10-06 16:59:46 +0200971static int get_zeros_padding(unsigned char *input, size_t input_len,
972 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200973{
974 size_t i;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200975 mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done;
Jens Wiklander817466c2018-05-22 13:49:31 +0200976
Jens Wiklander32b31802023-10-06 16:59:46 +0200977 if (NULL == input || NULL == data_len) {
978 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200979 }
980
Jens Wiklander32b31802023-10-06 16:59:46 +0200981 *data_len = 0;
982 for (i = input_len; i > 0; i--) {
983 prev_done = done;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200984 done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0));
985 *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len);
Jens Wiklander32b31802023-10-06 16:59:46 +0200986 }
987
988 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200989}
990#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
991
992/*
993 * No padding: don't pad :)
994 *
995 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
996 * but a trivial get_padding function
997 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200998static int get_no_padding(unsigned char *input, size_t input_len,
999 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001000{
Jens Wiklander32b31802023-10-06 16:59:46 +02001001 if (NULL == input || NULL == data_len) {
1002 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1003 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001004
1005 *data_len = input_len;
1006
Jens Wiklander32b31802023-10-06 16:59:46 +02001007 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001008}
1009#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1010
Jens Wiklander32b31802023-10-06 16:59:46 +02001011int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1012 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001013{
Jens Wiklander32b31802023-10-06 16:59:46 +02001014 if (ctx->cipher_info == NULL) {
1015 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1016 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001017
Tom Van Eyckc1633172024-04-09 18:44:13 +02001018#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001019 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001020 /* While PSA Crypto has an API for multipart
1021 * operations, we currently don't make it
1022 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001023 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001024 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001025#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001026
Jens Wiklander817466c2018-05-22 13:49:31 +02001027 *olen = 0;
1028
Tom Van Eyckc1633172024-04-09 18:44:13 +02001029#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1030 /* CBC mode requires padding so we make sure a call to
1031 * mbedtls_cipher_set_padding_mode has been done successfully. */
1032 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1033 if (ctx->get_padding == NULL) {
1034 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1035 }
1036 }
1037#endif
1038
1039 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1042 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1043 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1044 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1045 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001046 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001047 }
1048
Tom Van Eyckc1633172024-04-09 18:44:13 +02001049 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1050 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001051 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001052 }
1053
Tom Van Eyckc1633172024-04-09 18:44:13 +02001054 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001055 if (ctx->unprocessed_len != 0) {
1056 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1057 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001058
Jens Wiklander32b31802023-10-06 16:59:46 +02001059 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001060 }
1061
1062#if defined(MBEDTLS_CIPHER_MODE_CBC)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001063 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001064 int ret = 0;
1065
Jens Wiklander32b31802023-10-06 16:59:46 +02001066 if (MBEDTLS_ENCRYPT == ctx->operation) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001067 /* check for 'no padding' mode */
Jens Wiklander32b31802023-10-06 16:59:46 +02001068 if (NULL == ctx->add_padding) {
1069 if (0 != ctx->unprocessed_len) {
1070 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1071 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001072
Jens Wiklander32b31802023-10-06 16:59:46 +02001073 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001074 }
1075
Jens Wiklander32b31802023-10-06 16:59:46 +02001076 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1077 ctx->unprocessed_len);
1078 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001079 /*
1080 * For decrypt operations, expect a full block,
1081 * or an empty block if no padding
1082 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001083 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1084 return 0;
1085 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001086
Jens Wiklander32b31802023-10-06 16:59:46 +02001087 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001088 }
1089
1090 /* cipher block */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001091 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
1092 ctx->operation,
1093 mbedtls_cipher_get_block_size(
1094 ctx),
1095 ctx->iv,
1096 ctx->unprocessed_data,
1097 output))) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001098 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001099 }
1100
1101 /* Set output size for decryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001102 if (MBEDTLS_DECRYPT == ctx->operation) {
1103 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1104 olen);
1105 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001106
1107 /* Set output size for encryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001108 *olen = mbedtls_cipher_get_block_size(ctx);
1109 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001110 }
1111#else
1112 ((void) output);
1113#endif /* MBEDTLS_CIPHER_MODE_CBC */
1114
Jens Wiklander32b31802023-10-06 16:59:46 +02001115 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001116}
1117
1118#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Jens Wiklander32b31802023-10-06 16:59:46 +02001119int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1120 mbedtls_cipher_padding_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +02001121{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001122 if (NULL == ctx->cipher_info ||
1123 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001124 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001125 }
1126
Tom Van Eyckc1633172024-04-09 18:44:13 +02001127#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001128 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001129 /* While PSA Crypto knows about CBC padding
1130 * schemes, we currently don't make them
1131 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001132 if (mode != MBEDTLS_PADDING_NONE) {
1133 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1134 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001135
Jens Wiklander32b31802023-10-06 16:59:46 +02001136 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001137 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001138#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001139
Jens Wiklander32b31802023-10-06 16:59:46 +02001140 switch (mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001141#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Jens Wiklander32b31802023-10-06 16:59:46 +02001142 case MBEDTLS_PADDING_PKCS7:
1143 ctx->add_padding = add_pkcs_padding;
1144 ctx->get_padding = get_pkcs_padding;
1145 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001146#endif
1147#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001148 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1149 ctx->add_padding = add_one_and_zeros_padding;
1150 ctx->get_padding = get_one_and_zeros_padding;
1151 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001152#endif
1153#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Jens Wiklander32b31802023-10-06 16:59:46 +02001154 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1155 ctx->add_padding = add_zeros_and_len_padding;
1156 ctx->get_padding = get_zeros_and_len_padding;
1157 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001158#endif
1159#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001160 case MBEDTLS_PADDING_ZEROS:
1161 ctx->add_padding = add_zeros_padding;
1162 ctx->get_padding = get_zeros_padding;
1163 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001164#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001165 case MBEDTLS_PADDING_NONE:
1166 ctx->add_padding = NULL;
1167 ctx->get_padding = get_no_padding;
1168 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001169
Jens Wiklander32b31802023-10-06 16:59:46 +02001170 default:
1171 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001172 }
1173
Jens Wiklander32b31802023-10-06 16:59:46 +02001174 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001175}
1176#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1177
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001178#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001179int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1180 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001181{
Jens Wiklander32b31802023-10-06 16:59:46 +02001182 if (ctx->cipher_info == NULL) {
1183 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1184 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001185
Jens Wiklander32b31802023-10-06 16:59:46 +02001186 if (MBEDTLS_ENCRYPT != ctx->operation) {
1187 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1188 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001189
Tom Van Eyckc1633172024-04-09 18:44:13 +02001190#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001191 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001192 /* While PSA Crypto has an API for multipart
1193 * operations, we currently don't make it
1194 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001195 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001196 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001197#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001198
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001199#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001200 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001201 size_t output_length;
1202 /* The code here doesn't yet support alternative implementations
1203 * that can delay up to a block of output. */
1204 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1205 NULL, 0, &output_length,
1206 tag, tag_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001207 }
1208#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001209
Jens Wiklander32b31802023-10-06 16:59:46 +02001210#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001211 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001212 /* Don't allow truncated MAC for Poly1305 */
1213 if (tag_len != 16U) {
1214 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1215 }
1216
1217 return mbedtls_chachapoly_finish(
1218 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1219 }
1220#endif
1221
1222 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001223}
1224
Jens Wiklander32b31802023-10-06 16:59:46 +02001225int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1226 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001227{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001228 unsigned char check_tag[16];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001230
Jens Wiklander32b31802023-10-06 16:59:46 +02001231 if (ctx->cipher_info == NULL) {
1232 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1233 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001234
Jens Wiklander32b31802023-10-06 16:59:46 +02001235 if (MBEDTLS_DECRYPT != ctx->operation) {
1236 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001237 }
1238
Tom Van Eyckc1633172024-04-09 18:44:13 +02001239#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001240 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001241 /* While PSA Crypto has an API for multipart
1242 * operations, we currently don't make it
1243 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001244 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001245 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001246#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001247
Jens Wiklander32b31802023-10-06 16:59:46 +02001248 /* Status to return on a non-authenticated algorithm. */
1249 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001250
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001251#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001252 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001253 size_t output_length;
1254 /* The code here doesn't yet support alternative implementations
1255 * that can delay up to a block of output. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001256
Jens Wiklander32b31802023-10-06 16:59:46 +02001257 if (tag_len > sizeof(check_tag)) {
1258 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1259 }
1260
1261 if (0 != (ret = mbedtls_gcm_finish(
1262 (mbedtls_gcm_context *) ctx->cipher_ctx,
1263 NULL, 0, &output_length,
1264 check_tag, tag_len))) {
1265 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001266 }
1267
1268 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001269 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001270 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1271 goto exit;
1272 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001273 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001274#endif /* MBEDTLS_GCM_C */
1275
1276#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001277 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001278 /* Don't allow truncated MAC for Poly1305 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001279 if (tag_len != sizeof(check_tag)) {
1280 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1281 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001282
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001283 ret = mbedtls_chachapoly_finish(
Jens Wiklander32b31802023-10-06 16:59:46 +02001284 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1285 if (ret != 0) {
1286 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001287 }
1288
1289 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001290 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001291 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1292 goto exit;
1293 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001294 }
1295#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001296
Jerome Forissier039e02d2022-08-09 17:10:15 +02001297exit:
Jens Wiklander32b31802023-10-06 16:59:46 +02001298 mbedtls_platform_zeroize(check_tag, tag_len);
1299 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001300}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001301#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001302
1303/*
1304 * Packet-oriented wrapper for non-AEAD modes
1305 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001306int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1307 const unsigned char *iv, size_t iv_len,
1308 const unsigned char *input, size_t ilen,
1309 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001310{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001312 size_t finish_olen;
1313
Tom Van Eyckc1633172024-04-09 18:44:13 +02001314#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001315 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001316 /* As in the non-PSA case, we don't check that
1317 * a key has been set. If not, the key slot will
1318 * still be in its default state of 0, which is
1319 * guaranteed to be invalid, hence the PSA-call
1320 * below will gracefully fail. */
1321 mbedtls_cipher_context_psa * const cipher_psa =
1322 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1323
1324 psa_status_t status;
1325 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1326 size_t part_len;
1327
Jens Wiklander32b31802023-10-06 16:59:46 +02001328 if (ctx->operation == MBEDTLS_DECRYPT) {
1329 status = psa_cipher_decrypt_setup(&cipher_op,
1330 cipher_psa->slot,
1331 cipher_psa->alg);
1332 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1333 status = psa_cipher_encrypt_setup(&cipher_op,
1334 cipher_psa->slot,
1335 cipher_psa->alg);
1336 } else {
1337 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001338 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001339
1340 /* In the following, we can immediately return on an error,
1341 * because the PSA Crypto API guarantees that cipher operations
1342 * are terminated by unsuccessful calls to psa_cipher_update(),
1343 * and by any call to psa_cipher_finish(). */
Jens Wiklander32b31802023-10-06 16:59:46 +02001344 if (status != PSA_SUCCESS) {
1345 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001346 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001347
Tom Van Eyckc1633172024-04-09 18:44:13 +02001348 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
Jens Wiklander32b31802023-10-06 16:59:46 +02001349 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1350 if (status != PSA_SUCCESS) {
1351 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1352 }
1353 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001354
Jens Wiklander32b31802023-10-06 16:59:46 +02001355 status = psa_cipher_update(&cipher_op,
1356 input, ilen,
1357 output, ilen, olen);
1358 if (status != PSA_SUCCESS) {
1359 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1360 }
1361
1362 status = psa_cipher_finish(&cipher_op,
1363 output + *olen, ilen - *olen,
1364 &part_len);
1365 if (status != PSA_SUCCESS) {
1366 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1367 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001368
1369 *olen += part_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001370 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001371 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001372#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001373
Jens Wiklander32b31802023-10-06 16:59:46 +02001374 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1375 return ret;
1376 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001377
Jens Wiklander32b31802023-10-06 16:59:46 +02001378 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1379 return ret;
1380 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001381
Jens Wiklander32b31802023-10-06 16:59:46 +02001382 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1383 output, olen)) != 0) {
1384 return ret;
1385 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001386
Jens Wiklander32b31802023-10-06 16:59:46 +02001387 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1388 &finish_olen)) != 0) {
1389 return ret;
1390 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001391
1392 *olen += finish_olen;
1393
Jens Wiklander32b31802023-10-06 16:59:46 +02001394 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001395}
1396
1397#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1398/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001399 * Packet-oriented encryption for AEAD modes: internal function used by
1400 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001401 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001402static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1403 const unsigned char *iv, size_t iv_len,
1404 const unsigned char *ad, size_t ad_len,
1405 const unsigned char *input, size_t ilen,
1406 unsigned char *output, size_t *olen,
1407 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001408{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001409#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001410 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001411 /* As in the non-PSA case, we don't check that
1412 * a key has been set. If not, the key slot will
1413 * still be in its default state of 0, which is
1414 * guaranteed to be invalid, hence the PSA-call
1415 * below will gracefully fail. */
1416 mbedtls_cipher_context_psa * const cipher_psa =
1417 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1418
1419 psa_status_t status;
1420
1421 /* PSA Crypto API always writes the authentication tag
1422 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001423 if (output == NULL || tag != output + ilen) {
1424 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1425 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001426
Jens Wiklander32b31802023-10-06 16:59:46 +02001427 status = psa_aead_encrypt(cipher_psa->slot,
1428 cipher_psa->alg,
1429 iv, iv_len,
1430 ad, ad_len,
1431 input, ilen,
1432 output, ilen + tag_len, olen);
1433 if (status != PSA_SUCCESS) {
1434 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1435 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001436
1437 *olen -= tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001438 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001439 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001440#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001441
Jens Wiklander817466c2018-05-22 13:49:31 +02001442#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001443 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001444 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001445 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1446 ilen, iv, iv_len, ad, ad_len,
1447 input, output, tag_len, tag);
Jens Wiklander817466c2018-05-22 13:49:31 +02001448 }
1449#endif /* MBEDTLS_GCM_C */
1450#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001451 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001452 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001453 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1454 iv, iv_len, ad, ad_len, input, output,
1455 tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001456 }
1457#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001458#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001459 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001460 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001461 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001462 (tag_len != 16U)) {
1463 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001464 }
1465
1466 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001467 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1468 ilen, iv, ad, ad_len, input, output, tag);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001469 }
1470#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001471
Jens Wiklander32b31802023-10-06 16:59:46 +02001472 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001473}
1474
1475/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001476 * Packet-oriented encryption for AEAD modes: internal function used by
1477 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001478 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001479static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1480 const unsigned char *iv, size_t iv_len,
1481 const unsigned char *ad, size_t ad_len,
1482 const unsigned char *input, size_t ilen,
1483 unsigned char *output, size_t *olen,
1484 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001485{
Tom Van Eyckc1633172024-04-09 18:44:13 +02001486#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jens Wiklander32b31802023-10-06 16:59:46 +02001487 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001488 /* As in the non-PSA case, we don't check that
1489 * a key has been set. If not, the key slot will
1490 * still be in its default state of 0, which is
1491 * guaranteed to be invalid, hence the PSA-call
1492 * below will gracefully fail. */
1493 mbedtls_cipher_context_psa * const cipher_psa =
1494 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1495
1496 psa_status_t status;
1497
1498 /* PSA Crypto API always writes the authentication tag
1499 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001500 if (input == NULL || tag != input + ilen) {
1501 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1502 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001503
Jens Wiklander32b31802023-10-06 16:59:46 +02001504 status = psa_aead_decrypt(cipher_psa->slot,
1505 cipher_psa->alg,
1506 iv, iv_len,
1507 ad, ad_len,
1508 input, ilen + tag_len,
1509 output, ilen, olen);
1510 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1511 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1512 } else if (status != PSA_SUCCESS) {
1513 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1514 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001515
Jens Wiklander32b31802023-10-06 16:59:46 +02001516 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001517 }
Tom Van Eyckc1633172024-04-09 18:44:13 +02001518#endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001519
Jens Wiklander817466c2018-05-22 13:49:31 +02001520#if defined(MBEDTLS_GCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001521 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001523
1524 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001525 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1526 iv, iv_len, ad, ad_len,
1527 tag, tag_len, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001528
Jens Wiklander32b31802023-10-06 16:59:46 +02001529 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001530 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001531 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001532
Jens Wiklander32b31802023-10-06 16:59:46 +02001533 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001534 }
1535#endif /* MBEDTLS_GCM_C */
1536#if defined(MBEDTLS_CCM_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001537 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001539
1540 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001541 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1542 iv, iv_len, ad, ad_len,
1543 input, output, tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001544
Jens Wiklander32b31802023-10-06 16:59:46 +02001545 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001546 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001547 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001548
Jens Wiklander32b31802023-10-06 16:59:46 +02001549 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001550 }
1551#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001552#if defined(MBEDTLS_CHACHAPOLY_C)
Tom Van Eyckc1633172024-04-09 18:44:13 +02001553 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001555
1556 /* ChachaPoly has fixed length nonce and MAC (tag) */
Tom Van Eyckc1633172024-04-09 18:44:13 +02001557 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
Jens Wiklander32b31802023-10-06 16:59:46 +02001558 (tag_len != 16U)) {
1559 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001560 }
1561
1562 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001563 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1564 iv, ad, ad_len, tag, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001565
Jens Wiklander32b31802023-10-06 16:59:46 +02001566 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001567 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001568 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001569
Jens Wiklander32b31802023-10-06 16:59:46 +02001570 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001571 }
1572#endif /* MBEDTLS_CHACHAPOLY_C */
Jerome Forissier79013242021-07-28 10:24:04 +02001573
Jens Wiklander32b31802023-10-06 16:59:46 +02001574 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001575}
Jerome Forissier79013242021-07-28 10:24:04 +02001576#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1577
1578#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1579/*
1580 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1581 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001582int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1583 const unsigned char *iv, size_t iv_len,
1584 const unsigned char *ad, size_t ad_len,
1585 const unsigned char *input, size_t ilen,
1586 unsigned char *output, size_t output_len,
1587 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001588{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001589#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001590 if (
Tom Van Eyckc1633172024-04-09 18:44:13 +02001591#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001592 ctx->psa_enabled == 0 &&
1593#endif
Tom Van Eyckc1633172024-04-09 18:44:13 +02001594 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1595 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1596 mbedtls_nist_kw_mode_t mode =
1597 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1598 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001599
Jerome Forissier79013242021-07-28 10:24:04 +02001600 /* There is no iv, tag or ad associated with KW and KWP,
1601 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001602 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1603 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1604 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001605
Jerome Forissier79013242021-07-28 10:24:04 +02001606 (void) iv;
1607 (void) ad;
1608
Jens Wiklander32b31802023-10-06 16:59:46 +02001609 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1610 output, olen, output_len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001611 }
1612#endif /* MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001613
Jerome Forissier79013242021-07-28 10:24:04 +02001614#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1615 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001616 if (output_len < ilen + tag_len) {
1617 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1618 }
Jerome Forissier79013242021-07-28 10:24:04 +02001619
Jens Wiklander32b31802023-10-06 16:59:46 +02001620 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1621 input, ilen, output, olen,
1622 output + ilen, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001623 *olen += tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001624 return ret;
Jerome Forissier79013242021-07-28 10:24:04 +02001625#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001626 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001627#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Jerome Forissier79013242021-07-28 10:24:04 +02001628}
1629
1630/*
1631 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1632 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001633int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1634 const unsigned char *iv, size_t iv_len,
1635 const unsigned char *ad, size_t ad_len,
1636 const unsigned char *input, size_t ilen,
1637 unsigned char *output, size_t output_len,
1638 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001639{
Jerome Forissier79013242021-07-28 10:24:04 +02001640#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001641 if (
Tom Van Eyckc1633172024-04-09 18:44:13 +02001642#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerome Forissier79013242021-07-28 10:24:04 +02001643 ctx->psa_enabled == 0 &&
1644#endif
Tom Van Eyckc1633172024-04-09 18:44:13 +02001645 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1646 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1647 mbedtls_nist_kw_mode_t mode =
1648 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1649 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier79013242021-07-28 10:24:04 +02001650
1651 /* There is no iv, tag or ad associated with KW and KWP,
1652 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001653 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1654 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1655 }
Jerome Forissier79013242021-07-28 10:24:04 +02001656
1657 (void) iv;
1658 (void) ad;
1659
Jens Wiklander32b31802023-10-06 16:59:46 +02001660 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1661 output, olen, output_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001662 }
1663#endif /* MBEDTLS_NIST_KW_C */
1664
1665#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1666 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001667 if (ilen < tag_len || output_len < ilen - tag_len) {
1668 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1669 }
Jerome Forissier79013242021-07-28 10:24:04 +02001670
Jens Wiklander32b31802023-10-06 16:59:46 +02001671 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1672 input, ilen - tag_len, output, olen,
1673 input + ilen - tag_len, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001674#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001675 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001676#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1677}
1678#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001679
1680#endif /* MBEDTLS_CIPHER_C */