blob: 2f57f95529cbba9266444712401f68ca46d9978a [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file cipher.c
3 *
4 * \brief Generic cipher wrapper for mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Jerome Forissier79013242021-07-28 10:24:04 +02008 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +020010 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Jens Wiklander817466c2018-05-22 13:49:31 +020022 */
23
Jerome Forissier79013242021-07-28 10:24:04 +020024#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020025
26#if defined(MBEDTLS_CIPHER_C)
27
28#include "mbedtls/cipher.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020029#include "cipher_wrap.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010030#include "mbedtls/platform_util.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020031#include "mbedtls/error.h"
Jerome Forissier039e02d2022-08-09 17:10:15 +020032#include "mbedtls/constant_time.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020033
34#include <stdlib.h>
35#include <string.h>
36
Jens Wiklander3d3b0592019-03-20 15:30:29 +010037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
39#endif
40
Jens Wiklander817466c2018-05-22 13:49:31 +020041#if defined(MBEDTLS_GCM_C)
42#include "mbedtls/gcm.h"
43#endif
44
45#if defined(MBEDTLS_CCM_C)
46#include "mbedtls/ccm.h"
47#endif
48
Jens Wiklander3d3b0592019-03-20 15:30:29 +010049#if defined(MBEDTLS_CHACHA20_C)
50#include "mbedtls/chacha20.h"
51#endif
52
Jens Wiklander817466c2018-05-22 13:49:31 +020053#if defined(MBEDTLS_CMAC_C)
54#include "mbedtls/cmac.h"
55#endif
56
Jerome Forissier11fa71b2020-04-20 17:17:56 +020057#if defined(MBEDTLS_USE_PSA_CRYPTO)
58#include "psa/crypto.h"
59#include "mbedtls/psa_util.h"
60#endif /* MBEDTLS_USE_PSA_CRYPTO */
61
62#if defined(MBEDTLS_NIST_KW_C)
63#include "mbedtls/nist_kw.h"
64#endif
65
Jens Wiklander817466c2018-05-22 13:49:31 +020066#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020067
Jens Wiklander817466c2018-05-22 13:49:31 +020068static int supported_init = 0;
69
Jens Wiklander32b31802023-10-06 16:59:46 +020070const int *mbedtls_cipher_list(void)
Jens Wiklander817466c2018-05-22 13:49:31 +020071{
72 const mbedtls_cipher_definition_t *def;
73 int *type;
74
Jens Wiklander32b31802023-10-06 16:59:46 +020075 if (!supported_init) {
Jens Wiklander817466c2018-05-22 13:49:31 +020076 def = mbedtls_cipher_definitions;
77 type = mbedtls_cipher_supported;
78
Jens Wiklander32b31802023-10-06 16:59:46 +020079 while (def->type != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +020080 *type++ = (*def++).type;
Jens Wiklander32b31802023-10-06 16:59:46 +020081 }
Jens Wiklander817466c2018-05-22 13:49:31 +020082
83 *type = 0;
84
85 supported_init = 1;
86 }
87
Jens Wiklander32b31802023-10-06 16:59:46 +020088 return mbedtls_cipher_supported;
Jens Wiklander817466c2018-05-22 13:49:31 +020089}
90
Jerome Forissier11fa71b2020-04-20 17:17:56 +020091const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Jens Wiklander32b31802023-10-06 16:59:46 +020092 const mbedtls_cipher_type_t cipher_type)
Jens Wiklander817466c2018-05-22 13:49:31 +020093{
94 const mbedtls_cipher_definition_t *def;
95
Jens Wiklander32b31802023-10-06 16:59:46 +020096 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
97 if (def->type == cipher_type) {
98 return def->info;
99 }
100 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200101
Jens Wiklander32b31802023-10-06 16:59:46 +0200102 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200103}
104
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200105const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Jens Wiklander32b31802023-10-06 16:59:46 +0200106 const char *cipher_name)
Jens Wiklander817466c2018-05-22 13:49:31 +0200107{
108 const mbedtls_cipher_definition_t *def;
109
Jens Wiklander32b31802023-10-06 16:59:46 +0200110 if (NULL == cipher_name) {
111 return NULL;
112 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200113
Jens Wiklander32b31802023-10-06 16:59:46 +0200114 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
115 if (!strcmp(def->info->name, cipher_name)) {
116 return def->info;
117 }
118 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200119
Jens Wiklander32b31802023-10-06 16:59:46 +0200120 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200121}
122
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200123const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
124 const mbedtls_cipher_id_t cipher_id,
125 int key_bitlen,
Jens Wiklander32b31802023-10-06 16:59:46 +0200126 const mbedtls_cipher_mode_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +0200127{
128 const mbedtls_cipher_definition_t *def;
129
Jens Wiklander32b31802023-10-06 16:59:46 +0200130 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
131 if (def->info->base->cipher == cipher_id &&
Jens Wiklander817466c2018-05-22 13:49:31 +0200132 def->info->key_bitlen == (unsigned) key_bitlen &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200133 def->info->mode == mode) {
134 return def->info;
135 }
136 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200137
Jens Wiklander32b31802023-10-06 16:59:46 +0200138 return NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200139}
140
Jens Wiklander32b31802023-10-06 16:59:46 +0200141void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200142{
Jens Wiklander32b31802023-10-06 16:59:46 +0200143 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200144}
145
Jens Wiklander32b31802023-10-06 16:59:46 +0200146void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200147{
Jens Wiklander32b31802023-10-06 16:59:46 +0200148 if (ctx == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200149 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200150 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200151
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200153 if (ctx->psa_enabled == 1) {
154 if (ctx->cipher_ctx != NULL) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200155 mbedtls_cipher_context_psa * const cipher_psa =
156 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
157
Jens Wiklander32b31802023-10-06 16:59:46 +0200158 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200159 /* xxx_free() doesn't allow to return failures. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200160 (void) psa_destroy_key(cipher_psa->slot);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200161 }
162
Jens Wiklander32b31802023-10-06 16:59:46 +0200163 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
164 mbedtls_free(cipher_psa);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200165 }
166
Jens Wiklander32b31802023-10-06 16:59:46 +0200167 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200168 return;
169 }
170#endif /* MBEDTLS_USE_PSA_CRYPTO */
171
Jens Wiklander817466c2018-05-22 13:49:31 +0200172#if defined(MBEDTLS_CMAC_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200173 if (ctx->cmac_ctx) {
174 mbedtls_platform_zeroize(ctx->cmac_ctx,
175 sizeof(mbedtls_cmac_context_t));
176 mbedtls_free(ctx->cmac_ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200177 }
178#endif
179
Jens Wiklander32b31802023-10-06 16:59:46 +0200180 if (ctx->cipher_ctx) {
181 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
182 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200183
Jens Wiklander32b31802023-10-06 16:59:46 +0200184 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200185}
186
Jens Wiklander32b31802023-10-06 16:59:46 +0200187int mbedtls_cipher_clone(mbedtls_cipher_context_t *dst,
188 const mbedtls_cipher_context_t *src)
Edison Ai12484fc2018-12-19 15:36:28 +0800189{
Jens Wiklander32b31802023-10-06 16:59:46 +0200190 if (dst == NULL || dst->cipher_info == NULL ||
191 src == NULL || src->cipher_info == NULL) {
192 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Edison Ai12484fc2018-12-19 15:36:28 +0800193 }
194
195 dst->cipher_info = src->cipher_info;
196 dst->key_bitlen = src->key_bitlen;
197 dst->operation = src->operation;
198#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
199 dst->add_padding = src->add_padding;
200 dst->get_padding = src->get_padding;
201#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200202 memcpy(dst->unprocessed_data, src->unprocessed_data, MBEDTLS_MAX_BLOCK_LENGTH);
Edison Ai12484fc2018-12-19 15:36:28 +0800203 dst->unprocessed_len = src->unprocessed_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200204 memcpy(dst->iv, src->iv, MBEDTLS_MAX_IV_LENGTH);
Edison Ai12484fc2018-12-19 15:36:28 +0800205 dst->iv_size = src->iv_size;
Jens Wiklander32b31802023-10-06 16:59:46 +0200206 if (dst->cipher_info->base->ctx_clone_func)
207 dst->cipher_info->base->ctx_clone_func(dst->cipher_ctx, src->cipher_ctx);
Edison Ai12484fc2018-12-19 15:36:28 +0800208
209#if defined(MBEDTLS_CMAC_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200210 if (dst->cmac_ctx != NULL && src->cmac_ctx != NULL)
211 memcpy(dst->cmac_ctx, src->cmac_ctx, sizeof(mbedtls_cmac_context_t));
Edison Ai12484fc2018-12-19 15:36:28 +0800212#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200213 return 0;
Edison Ai12484fc2018-12-19 15:36:28 +0800214}
215
Jens Wiklander32b31802023-10-06 16:59:46 +0200216int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
217 const mbedtls_cipher_info_t *cipher_info)
Jens Wiklander817466c2018-05-22 13:49:31 +0200218{
Jens Wiklander32b31802023-10-06 16:59:46 +0200219 if (cipher_info == NULL) {
220 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
221 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200222
Jens Wiklander32b31802023-10-06 16:59:46 +0200223 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jens Wiklander817466c2018-05-22 13:49:31 +0200224
Jens Wiklander32b31802023-10-06 16:59:46 +0200225 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
226 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
227 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200228
229 ctx->cipher_info = cipher_info;
230
231#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
232 /*
233 * Ignore possible errors caused by a cipher mode that doesn't use padding
234 */
235#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Jens Wiklander32b31802023-10-06 16:59:46 +0200236 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Jens Wiklander817466c2018-05-22 13:49:31 +0200237#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200238 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Jens Wiklander817466c2018-05-22 13:49:31 +0200239#endif
240#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
241
Jens Wiklander32b31802023-10-06 16:59:46 +0200242 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200243}
244
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200246#if !defined(MBEDTLS_DEPRECATED_REMOVED)
247int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
248 const mbedtls_cipher_info_t *cipher_info,
249 size_t taglen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200250{
251 psa_algorithm_t alg;
252 mbedtls_cipher_context_psa *cipher_psa;
253
Jens Wiklander32b31802023-10-06 16:59:46 +0200254 if (NULL == cipher_info || NULL == ctx) {
255 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
256 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200257
258 /* Check that the underlying cipher mode and cipher type are
259 * supported by the underlying PSA Crypto implementation. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200260 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
261 if (alg == 0) {
262 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
263 }
264 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
265 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
266 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200267
Jens Wiklander32b31802023-10-06 16:59:46 +0200268 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200269
Jens Wiklander32b31802023-10-06 16:59:46 +0200270 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
271 if (cipher_psa == NULL) {
272 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
273 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200274 cipher_psa->alg = alg;
275 ctx->cipher_ctx = cipher_psa;
276 ctx->cipher_info = cipher_info;
277 ctx->psa_enabled = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200278 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200279}
Jens Wiklander32b31802023-10-06 16:59:46 +0200280#endif /* MBEDTLS_DEPRECATED_REMOVED */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200281#endif /* MBEDTLS_USE_PSA_CRYPTO */
282
Jens Wiklander32b31802023-10-06 16:59:46 +0200283int mbedtls_cipher_setup_info(mbedtls_cipher_context_t *ctx,
284 const mbedtls_cipher_info_t *cipher_info )
Edison Ai12484fc2018-12-19 15:36:28 +0800285{
Jens Wiklander32b31802023-10-06 16:59:46 +0200286 if (NULL == cipher_info || NULL == ctx)
287 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Edison Ai12484fc2018-12-19 15:36:28 +0800288
289 ctx->cipher_info = cipher_info;
Jens Wiklander32b31802023-10-06 16:59:46 +0200290 return 0;
Edison Ai12484fc2018-12-19 15:36:28 +0800291}
292
Jens Wiklander32b31802023-10-06 16:59:46 +0200293int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
294 const unsigned char *key,
295 int key_bitlen,
296 const mbedtls_operation_t operation)
Jens Wiklander817466c2018-05-22 13:49:31 +0200297{
Jens Wiklander32b31802023-10-06 16:59:46 +0200298 if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) {
299 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
300 }
301 if (ctx->cipher_info == NULL) {
302 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
303 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200304
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200305#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200306 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200307 mbedtls_cipher_context_psa * const cipher_psa =
308 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
309
Jens Wiklander32b31802023-10-06 16:59:46 +0200310 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200311
312 psa_status_t status;
313 psa_key_type_t key_type;
314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
315
316 /* PSA Crypto API only accepts byte-aligned keys. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200317 if (key_bitlen % 8 != 0) {
318 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
319 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200320
321 /* Don't allow keys to be set multiple times. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200322 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
323 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
324 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200325
326 key_type = mbedtls_psa_translate_cipher_type(
Jens Wiklander32b31802023-10-06 16:59:46 +0200327 ctx->cipher_info->type);
328 if (key_type == 0) {
329 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
330 }
331 psa_set_key_type(&attributes, key_type);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200332
333 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
334 * (encrypt vs. decrypt): it is possible to setup a key for encryption
335 * and use it for AEAD decryption. Until tests relying on this
336 * are changed, allow any usage in PSA. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200337 psa_set_key_usage_flags(&attributes,
338 /* mbedtls_psa_translate_cipher_operation( operation ); */
339 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
340 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200341
Jens Wiklander32b31802023-10-06 16:59:46 +0200342 status = psa_import_key(&attributes, key, key_bytelen,
343 &cipher_psa->slot);
344 switch (status) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200345 case PSA_SUCCESS:
346 break;
347 case PSA_ERROR_INSUFFICIENT_MEMORY:
Jens Wiklander32b31802023-10-06 16:59:46 +0200348 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200349 case PSA_ERROR_NOT_SUPPORTED:
Jens Wiklander32b31802023-10-06 16:59:46 +0200350 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200351 default:
Jens Wiklander32b31802023-10-06 16:59:46 +0200352 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200353 }
354 /* Indicate that we own the key slot and need to
355 * destroy it in mbedtls_cipher_free(). */
356 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
357
358 ctx->key_bitlen = key_bitlen;
359 ctx->operation = operation;
Jens Wiklander32b31802023-10-06 16:59:46 +0200360 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200361 }
362#endif /* MBEDTLS_USE_PSA_CRYPTO */
363
Jens Wiklander32b31802023-10-06 16:59:46 +0200364 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
365 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
366 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200367 }
368
369 ctx->key_bitlen = key_bitlen;
370 ctx->operation = operation;
371
372 /*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100373 * For OFB, CFB and CTR mode always use the encryption key schedule
Jens Wiklander817466c2018-05-22 13:49:31 +0200374 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200375 if (MBEDTLS_ENCRYPT == operation ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200376 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100377 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200378 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
379 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
380 ctx->key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200381 }
382
Jens Wiklander32b31802023-10-06 16:59:46 +0200383 if (MBEDTLS_DECRYPT == operation) {
384 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
385 ctx->key_bitlen);
386 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200387
Jens Wiklander32b31802023-10-06 16:59:46 +0200388 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200389}
390
Jens Wiklander32b31802023-10-06 16:59:46 +0200391int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
392 const unsigned char *iv,
393 size_t iv_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200394{
395 size_t actual_iv_size;
396
Jens Wiklander32b31802023-10-06 16:59:46 +0200397 if (ctx->cipher_info == NULL) {
398 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
399 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200400#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200401 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200402 /* While PSA Crypto has an API for multipart
403 * operations, we currently don't make it
404 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200405 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200406 }
407#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jens Wiklander817466c2018-05-22 13:49:31 +0200408
409 /* avoid buffer overflow in ctx->iv */
Jens Wiklander32b31802023-10-06 16:59:46 +0200410 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
411 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
412 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200413
Jens Wiklander32b31802023-10-06 16:59:46 +0200414 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200415 actual_iv_size = iv_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200416 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +0200417 actual_iv_size = ctx->cipher_info->iv_size;
418
419 /* avoid reading past the end of input buffer */
Jens Wiklander32b31802023-10-06 16:59:46 +0200420 if (actual_iv_size > iv_len) {
421 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
422 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200423 }
424
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100425#if defined(MBEDTLS_CHACHA20_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200426 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Jerome Forissier039e02d2022-08-09 17:10:15 +0200427 /* Even though the actual_iv_size is overwritten with a correct value
428 * of 12 from the cipher info, return an error to indicate that
429 * the input iv_len is wrong. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200430 if (iv_len != 12) {
431 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
432 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200433
Jens Wiklander32b31802023-10-06 16:59:46 +0200434 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
435 iv,
436 0U)) { /* Initial counter value */
437 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100438 }
439 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200440#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200441 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
442 iv_len != 12) {
443 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
444 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200445#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100446#endif
447
Jens Wiklander32b31802023-10-06 16:59:46 +0200448#if defined(MBEDTLS_GCM_C)
449 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
450 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
451 ctx->operation,
452 iv, iv_len);
453 }
454#endif
455
456#if defined(MBEDTLS_CCM_C)
457 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode) {
458 int set_lengths_result;
459 int ccm_star_mode;
460
461 set_lengths_result = mbedtls_ccm_set_lengths(
462 (mbedtls_ccm_context *) ctx->cipher_ctx,
463 0, 0, 0);
464 if (set_lengths_result != 0) {
465 return set_lengths_result;
466 }
467
468 if (ctx->operation == MBEDTLS_DECRYPT) {
469 ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
470 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
471 ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
472 } else {
473 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
474 }
475
476 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
477 ccm_star_mode,
478 iv, iv_len);
479 }
480#endif
481
482 if (actual_iv_size != 0) {
483 memcpy(ctx->iv, iv, actual_iv_size);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100484 ctx->iv_size = actual_iv_size;
485 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200486
Jens Wiklander32b31802023-10-06 16:59:46 +0200487 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200488}
489
Jens Wiklander32b31802023-10-06 16:59:46 +0200490int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200491{
Jens Wiklander32b31802023-10-06 16:59:46 +0200492 if (ctx->cipher_info == NULL) {
493 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
494 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200495
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200496#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200497 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200498 /* We don't support resetting PSA-based
499 * cipher contexts, yet. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200500 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200501 }
502#endif /* MBEDTLS_USE_PSA_CRYPTO */
503
Jens Wiklander817466c2018-05-22 13:49:31 +0200504 ctx->unprocessed_len = 0;
505
Jens Wiklander32b31802023-10-06 16:59:46 +0200506 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200507}
508
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100509#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200510int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
511 const unsigned char *ad, size_t ad_len)
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
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200517#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200518 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200519 /* While PSA Crypto has an API for multipart
520 * operations, we currently don't make it
521 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200522 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200523 }
524#endif /* MBEDTLS_USE_PSA_CRYPTO */
525
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100526#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200527 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
528 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
529 ad, ad_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200530 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100531#endif
532
533#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200534 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100535 int result;
536 mbedtls_chachapoly_mode_t mode;
537
Jens Wiklander32b31802023-10-06 16:59:46 +0200538 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100539 ? MBEDTLS_CHACHAPOLY_ENCRYPT
540 : MBEDTLS_CHACHAPOLY_DECRYPT;
541
Jens Wiklander32b31802023-10-06 16:59:46 +0200542 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
543 ctx->iv,
544 mode);
545 if (result != 0) {
546 return result;
547 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100548
Jens Wiklander32b31802023-10-06 16:59:46 +0200549 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
550 ad, ad_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100551 }
552#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200553
Jens Wiklander32b31802023-10-06 16:59:46 +0200554 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200555}
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100556#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +0200557
Jens Wiklander32b31802023-10-06 16:59:46 +0200558int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
559 size_t ilen, unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200560{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100562 size_t block_size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200563
Jens Wiklander32b31802023-10-06 16:59:46 +0200564 if (ctx->cipher_info == NULL) {
565 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
566 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200567
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200568#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200569 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200570 /* While PSA Crypto has an API for multipart
571 * operations, we currently don't make it
572 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200573 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200574 }
575#endif /* MBEDTLS_USE_PSA_CRYPTO */
576
Jens Wiklander817466c2018-05-22 13:49:31 +0200577 *olen = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200578 block_size = mbedtls_cipher_get_block_size(ctx);
579 if (0 == block_size) {
580 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200581 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200582
Jens Wiklander32b31802023-10-06 16:59:46 +0200583 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
584 if (ilen != block_size) {
585 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
586 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200587
588 *olen = ilen;
589
Jens Wiklander32b31802023-10-06 16:59:46 +0200590 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
591 ctx->operation, input, output))) {
592 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200593 }
594
Jens Wiklander32b31802023-10-06 16:59:46 +0200595 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200596 }
597
598#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200599 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
600 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
601 input, ilen,
602 output, ilen, olen);
603 }
604#endif
605
606#if defined(MBEDTLS_CCM_C)
607 if (ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
608 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
609 input, ilen,
610 output, ilen, olen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100611 }
612#endif
613
614#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200615 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100616 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200617 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
618 ilen, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200619 }
620#endif
621
Jens Wiklander32b31802023-10-06 16:59:46 +0200622 if (input == output &&
623 (ctx->unprocessed_len != 0 || ilen % block_size)) {
624 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200625 }
626
627#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +0200628 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200629 size_t copy_len = 0;
630
631 /*
632 * If there is not enough data for a full block, cache it.
633 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200634 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
635 ilen <= block_size - ctx->unprocessed_len) ||
636 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
637 ilen < block_size - ctx->unprocessed_len) ||
638 (ctx->operation == MBEDTLS_ENCRYPT &&
639 ilen < block_size - ctx->unprocessed_len)) {
640 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
641 ilen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200642
643 ctx->unprocessed_len += ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200644 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200645 }
646
647 /*
648 * Process cached data first
649 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200650 if (0 != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200651 copy_len = block_size - ctx->unprocessed_len;
652
Jens Wiklander32b31802023-10-06 16:59:46 +0200653 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
654 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200655
Jens Wiklander32b31802023-10-06 16:59:46 +0200656 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
657 ctx->operation, block_size, ctx->iv,
658 ctx->unprocessed_data, output))) {
659 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200660 }
661
662 *olen += block_size;
663 output += block_size;
664 ctx->unprocessed_len = 0;
665
666 input += copy_len;
667 ilen -= copy_len;
668 }
669
670 /*
671 * Cache final, incomplete block
672 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200673 if (0 != ilen) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100674 /* Encryption: only cache partial blocks
675 * Decryption w/ padding: always keep at least one whole block
676 * Decryption w/o padding: only cache partial blocks
677 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200678 copy_len = ilen % block_size;
Jens Wiklander32b31802023-10-06 16:59:46 +0200679 if (copy_len == 0 &&
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100680 ctx->operation == MBEDTLS_DECRYPT &&
Jens Wiklander32b31802023-10-06 16:59:46 +0200681 NULL != ctx->add_padding) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200682 copy_len = block_size;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100683 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200684
Jens Wiklander32b31802023-10-06 16:59:46 +0200685 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
686 copy_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200687
688 ctx->unprocessed_len += copy_len;
689 ilen -= copy_len;
690 }
691
692 /*
693 * Process remaining full blocks
694 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200695 if (ilen) {
696 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
697 ctx->operation, ilen, ctx->iv, input,
698 output))) {
699 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200700 }
701
702 *olen += ilen;
703 }
704
Jens Wiklander32b31802023-10-06 16:59:46 +0200705 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200706 }
707#endif /* MBEDTLS_CIPHER_MODE_CBC */
708
709#if defined(MBEDTLS_CIPHER_MODE_CFB)
Jens Wiklander32b31802023-10-06 16:59:46 +0200710 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
711 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
712 ctx->operation, ilen,
713 &ctx->unprocessed_len, ctx->iv,
714 input, output))) {
715 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200716 }
717
718 *olen = ilen;
719
Jens Wiklander32b31802023-10-06 16:59:46 +0200720 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200721 }
722#endif /* MBEDTLS_CIPHER_MODE_CFB */
723
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100724#if defined(MBEDTLS_CIPHER_MODE_OFB)
Jens Wiklander32b31802023-10-06 16:59:46 +0200725 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
726 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
727 ilen, &ctx->unprocessed_len, ctx->iv,
728 input, output))) {
729 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100730 }
731
732 *olen = ilen;
733
Jens Wiklander32b31802023-10-06 16:59:46 +0200734 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100735 }
736#endif /* MBEDTLS_CIPHER_MODE_OFB */
737
Jens Wiklander817466c2018-05-22 13:49:31 +0200738#if defined(MBEDTLS_CIPHER_MODE_CTR)
Jens Wiklander32b31802023-10-06 16:59:46 +0200739 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
740 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
741 ilen, &ctx->unprocessed_len, ctx->iv,
742 ctx->unprocessed_data, input, output))) {
743 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_CTR */
751
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100752#if defined(MBEDTLS_CIPHER_MODE_XTS)
Jens Wiklander32b31802023-10-06 16:59:46 +0200753 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
754 if (ctx->unprocessed_len > 0) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100755 /* We can only process an entire data unit at a time. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200756 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100757 }
758
Jens Wiklander32b31802023-10-06 16:59:46 +0200759 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
760 ctx->operation, ilen, ctx->iv, input, output);
761 if (ret != 0) {
762 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100763 }
764
765 *olen = ilen;
766
Jens Wiklander32b31802023-10-06 16:59:46 +0200767 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100768 }
769#endif /* MBEDTLS_CIPHER_MODE_XTS */
770
Jens Wiklander817466c2018-05-22 13:49:31 +0200771#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Jens Wiklander32b31802023-10-06 16:59:46 +0200772 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
773 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
774 ilen, input, output))) {
775 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200776 }
777
778 *olen = ilen;
779
Jens Wiklander32b31802023-10-06 16:59:46 +0200780 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200781 }
782#endif /* MBEDTLS_CIPHER_MODE_STREAM */
783
Jens Wiklander32b31802023-10-06 16:59:46 +0200784 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +0200785}
786
787#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
788#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
789/*
790 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
791 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200792static void add_pkcs_padding(unsigned char *output, size_t output_len,
793 size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200794{
795 size_t padding_len = output_len - data_len;
796 unsigned char i;
797
Jens Wiklander32b31802023-10-06 16:59:46 +0200798 for (i = 0; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200799 output[data_len + i] = (unsigned char) padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200800 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200801}
802
Jens Wiklander32b31802023-10-06 16:59:46 +0200803static int get_pkcs_padding(unsigned char *input, size_t input_len,
804 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200805{
806 size_t i, pad_idx;
807 unsigned char padding_len, bad = 0;
808
Jens Wiklander32b31802023-10-06 16:59:46 +0200809 if (NULL == input || NULL == data_len) {
810 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
811 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200812
813 padding_len = input[input_len - 1];
814 *data_len = input_len - padding_len;
815
816 /* Avoid logical || since it results in a branch */
817 bad |= padding_len > input_len;
818 bad |= padding_len == 0;
819
820 /* The number of bytes checked must be independent of padding_len,
821 * so pick input_len, which is usually 8 or 16 (one block) */
822 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200823 for (i = 0; i < input_len; i++) {
824 bad |= (input[i] ^ padding_len) * (i >= pad_idx);
825 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200826
Jens Wiklander32b31802023-10-06 16:59:46 +0200827 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Jens Wiklander817466c2018-05-22 13:49:31 +0200828}
829#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
830
831#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
832/*
833 * One and zeros padding: fill with 80 00 ... 00
834 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200835static void add_one_and_zeros_padding(unsigned char *output,
836 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200837{
838 size_t padding_len = output_len - data_len;
839 unsigned char i = 0;
840
841 output[data_len] = 0x80;
Jens Wiklander32b31802023-10-06 16:59:46 +0200842 for (i = 1; i < padding_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200843 output[data_len + i] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200844 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200845}
846
Jens Wiklander32b31802023-10-06 16:59:46 +0200847static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
848 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200849{
850 size_t i;
851 unsigned char done = 0, prev_done, bad;
852
Jens Wiklander32b31802023-10-06 16:59:46 +0200853 if (NULL == input || NULL == data_len) {
854 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
855 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200856
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100857 bad = 0x80;
Jens Wiklander817466c2018-05-22 13:49:31 +0200858 *data_len = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200859 for (i = input_len; i > 0; i--) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200860 prev_done = done;
Jens Wiklander32b31802023-10-06 16:59:46 +0200861 done |= (input[i - 1] != 0);
862 *data_len |= (i - 1) * (done != prev_done);
863 bad ^= input[i - 1] * (done != prev_done);
Jens Wiklander817466c2018-05-22 13:49:31 +0200864 }
865
Jens Wiklander32b31802023-10-06 16:59:46 +0200866 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Jens Wiklander817466c2018-05-22 13:49:31 +0200867
868}
869#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
870
871#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
872/*
873 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
874 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200875static void add_zeros_and_len_padding(unsigned char *output,
876 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200877{
878 size_t padding_len = output_len - data_len;
879 unsigned char i = 0;
880
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 - 1] = 0x00;
Jens Wiklander32b31802023-10-06 16:59:46 +0200883 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200884 output[output_len - 1] = (unsigned char) padding_len;
885}
886
Jens Wiklander32b31802023-10-06 16:59:46 +0200887static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
888 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200889{
890 size_t i, pad_idx;
891 unsigned char padding_len, bad = 0;
892
Jens Wiklander32b31802023-10-06 16:59:46 +0200893 if (NULL == input || NULL == data_len) {
894 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
895 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200896
897 padding_len = input[input_len - 1];
898 *data_len = input_len - padding_len;
899
900 /* Avoid logical || since it results in a branch */
901 bad |= padding_len > input_len;
902 bad |= padding_len == 0;
903
904 /* The number of bytes checked must be independent of padding_len */
905 pad_idx = input_len - padding_len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200906 for (i = 0; i < input_len - 1; i++) {
907 bad |= input[i] * (i >= pad_idx);
908 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200909
Jens Wiklander32b31802023-10-06 16:59:46 +0200910 return MBEDTLS_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Jens Wiklander817466c2018-05-22 13:49:31 +0200911}
912#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
913
914#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
915/*
916 * Zero padding: fill with 00 ... 00
917 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200918static void add_zeros_padding(unsigned char *output,
919 size_t output_len, size_t data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200920{
921 size_t i;
922
Jens Wiklander32b31802023-10-06 16:59:46 +0200923 for (i = data_len; i < output_len; i++) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200924 output[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_zeros_padding(unsigned char *input, size_t input_len,
929 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200930{
931 size_t i;
932 unsigned char done = 0, prev_done;
933
Jens Wiklander32b31802023-10-06 16:59:46 +0200934 if (NULL == input || NULL == data_len) {
935 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200936 }
937
Jens Wiklander32b31802023-10-06 16:59:46 +0200938 *data_len = 0;
939 for (i = input_len; i > 0; i--) {
940 prev_done = done;
941 done |= (input[i-1] != 0);
942 *data_len |= i * (done != prev_done);
943 }
944
945 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200946}
947#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
948
949/*
950 * No padding: don't pad :)
951 *
952 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
953 * but a trivial get_padding function
954 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200955static int get_no_padding(unsigned char *input, size_t input_len,
956 size_t *data_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200957{
Jens Wiklander32b31802023-10-06 16:59:46 +0200958 if (NULL == input || NULL == data_len) {
959 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
960 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200961
962 *data_len = input_len;
963
Jens Wiklander32b31802023-10-06 16:59:46 +0200964 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200965}
966#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
967
Jens Wiklander32b31802023-10-06 16:59:46 +0200968int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
969 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200970{
Jens Wiklander32b31802023-10-06 16:59:46 +0200971 if (ctx->cipher_info == NULL) {
972 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
973 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200974
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200975#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200976 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200977 /* While PSA Crypto has an API for multipart
978 * operations, we currently don't make it
979 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200980 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200981 }
982#endif /* MBEDTLS_USE_PSA_CRYPTO */
983
Jens Wiklander817466c2018-05-22 13:49:31 +0200984 *olen = 0;
985
Jens Wiklander32b31802023-10-06 16:59:46 +0200986 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100987 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Jens Wiklander817466c2018-05-22 13:49:31 +0200988 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
989 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200990 MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100991 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200992 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
993 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200994 }
995
Jens Wiklander32b31802023-10-06 16:59:46 +0200996 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
997 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
998 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100999 }
1000
Jens Wiklander32b31802023-10-06 16:59:46 +02001001 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
1002 if (ctx->unprocessed_len != 0) {
1003 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1004 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001005
Jens Wiklander32b31802023-10-06 16:59:46 +02001006 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001007 }
1008
1009#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +02001010 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001011 int ret = 0;
1012
Jens Wiklander32b31802023-10-06 16:59:46 +02001013 if (MBEDTLS_ENCRYPT == ctx->operation) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001014 /* check for 'no padding' mode */
Jens Wiklander32b31802023-10-06 16:59:46 +02001015 if (NULL == ctx->add_padding) {
1016 if (0 != ctx->unprocessed_len) {
1017 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1018 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001019
Jens Wiklander32b31802023-10-06 16:59:46 +02001020 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001021 }
1022
Jens Wiklander32b31802023-10-06 16:59:46 +02001023 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1024 ctx->unprocessed_len);
1025 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001026 /*
1027 * For decrypt operations, expect a full block,
1028 * or an empty block if no padding
1029 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001030 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1031 return 0;
1032 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001033
Jens Wiklander32b31802023-10-06 16:59:46 +02001034 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001035 }
1036
1037 /* cipher block */
Jens Wiklander32b31802023-10-06 16:59:46 +02001038 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
1039 ctx->operation,
1040 mbedtls_cipher_get_block_size(ctx),
1041 ctx->iv,
1042 ctx->unprocessed_data, output))) {
1043 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001044 }
1045
1046 /* Set output size for decryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001047 if (MBEDTLS_DECRYPT == ctx->operation) {
1048 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1049 olen);
1050 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001051
1052 /* Set output size for encryption */
Jens Wiklander32b31802023-10-06 16:59:46 +02001053 *olen = mbedtls_cipher_get_block_size(ctx);
1054 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001055 }
1056#else
1057 ((void) output);
1058#endif /* MBEDTLS_CIPHER_MODE_CBC */
1059
Jens Wiklander32b31802023-10-06 16:59:46 +02001060 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001061}
1062
1063#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Jens Wiklander32b31802023-10-06 16:59:46 +02001064int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1065 mbedtls_cipher_padding_t mode)
Jens Wiklander817466c2018-05-22 13:49:31 +02001066{
Jens Wiklander32b31802023-10-06 16:59:46 +02001067 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1068 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001069 }
1070
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001071#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001072 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001073 /* While PSA Crypto knows about CBC padding
1074 * schemes, we currently don't make them
1075 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001076 if (mode != MBEDTLS_PADDING_NONE) {
1077 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1078 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001079
Jens Wiklander32b31802023-10-06 16:59:46 +02001080 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001081 }
1082#endif /* MBEDTLS_USE_PSA_CRYPTO */
1083
Jens Wiklander32b31802023-10-06 16:59:46 +02001084 switch (mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001085#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Jens Wiklander32b31802023-10-06 16:59:46 +02001086 case MBEDTLS_PADDING_PKCS7:
1087 ctx->add_padding = add_pkcs_padding;
1088 ctx->get_padding = get_pkcs_padding;
1089 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001090#endif
1091#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001092 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1093 ctx->add_padding = add_one_and_zeros_padding;
1094 ctx->get_padding = get_one_and_zeros_padding;
1095 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001096#endif
1097#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Jens Wiklander32b31802023-10-06 16:59:46 +02001098 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1099 ctx->add_padding = add_zeros_and_len_padding;
1100 ctx->get_padding = get_zeros_and_len_padding;
1101 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001102#endif
1103#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Jens Wiklander32b31802023-10-06 16:59:46 +02001104 case MBEDTLS_PADDING_ZEROS:
1105 ctx->add_padding = add_zeros_padding;
1106 ctx->get_padding = get_zeros_padding;
1107 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001108#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001109 case MBEDTLS_PADDING_NONE:
1110 ctx->add_padding = NULL;
1111 ctx->get_padding = get_no_padding;
1112 break;
Jens Wiklander817466c2018-05-22 13:49:31 +02001113
Jens Wiklander32b31802023-10-06 16:59:46 +02001114 default:
1115 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001116 }
1117
Jens Wiklander32b31802023-10-06 16:59:46 +02001118 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001119}
1120#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1121
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001122#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001123int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1124 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001125{
Jens Wiklander32b31802023-10-06 16:59:46 +02001126 if (ctx->cipher_info == NULL) {
1127 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1128 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001129
Jens Wiklander32b31802023-10-06 16:59:46 +02001130 if (MBEDTLS_ENCRYPT != ctx->operation) {
1131 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1132 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001133
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001135 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001136 /* While PSA Crypto has an API for multipart
1137 * operations, we currently don't make it
1138 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001139 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001140 }
1141#endif /* MBEDTLS_USE_PSA_CRYPTO */
1142
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001143#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001144 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1145 size_t output_length;
1146 /* The code here doesn't yet support alternative implementations
1147 * that can delay up to a block of output. */
1148 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1149 NULL, 0, &output_length,
1150 tag, tag_len);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001151 }
1152#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001153
Jens Wiklander32b31802023-10-06 16:59:46 +02001154#if defined(MBEDTLS_CHACHAPOLY_C)
1155 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1156 /* Don't allow truncated MAC for Poly1305 */
1157 if (tag_len != 16U) {
1158 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1159 }
1160
1161 return mbedtls_chachapoly_finish(
1162 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1163 }
1164#endif
1165
1166 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001167}
1168
Jens Wiklander32b31802023-10-06 16:59:46 +02001169int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1170 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001171{
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001172 unsigned char check_tag[16];
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001174
Jens Wiklander32b31802023-10-06 16:59:46 +02001175 if (ctx->cipher_info == NULL) {
1176 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1177 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001178
Jens Wiklander32b31802023-10-06 16:59:46 +02001179 if (MBEDTLS_DECRYPT != ctx->operation) {
1180 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +02001181 }
1182
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001183#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001184 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001185 /* While PSA Crypto has an API for multipart
1186 * operations, we currently don't make it
1187 * accessible through the cipher layer. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001188 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001189 }
1190#endif /* MBEDTLS_USE_PSA_CRYPTO */
1191
Jens Wiklander32b31802023-10-06 16:59:46 +02001192 /* Status to return on a non-authenticated algorithm. */
1193 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001194
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001195#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001196 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1197 size_t output_length;
1198 /* The code here doesn't yet support alternative implementations
1199 * that can delay up to a block of output. */
Jens Wiklander817466c2018-05-22 13:49:31 +02001200
Jens Wiklander32b31802023-10-06 16:59:46 +02001201 if (tag_len > sizeof(check_tag)) {
1202 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1203 }
1204
1205 if (0 != (ret = mbedtls_gcm_finish(
1206 (mbedtls_gcm_context *) ctx->cipher_ctx,
1207 NULL, 0, &output_length,
1208 check_tag, tag_len))) {
1209 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001210 }
1211
1212 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001213 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001214 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1215 goto exit;
1216 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001217 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001218#endif /* MBEDTLS_GCM_C */
1219
1220#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001221 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001222 /* Don't allow truncated MAC for Poly1305 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001223 if (tag_len != sizeof(check_tag)) {
1224 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1225 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001226
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001227 ret = mbedtls_chachapoly_finish(
Jens Wiklander32b31802023-10-06 16:59:46 +02001228 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1229 if (ret != 0) {
1230 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001231 }
1232
1233 /* Check the tag in "constant-time" */
Jens Wiklander32b31802023-10-06 16:59:46 +02001234 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Jerome Forissier039e02d2022-08-09 17:10:15 +02001235 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1236 goto exit;
1237 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001238 }
1239#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001240
Jerome Forissier039e02d2022-08-09 17:10:15 +02001241exit:
Jens Wiklander32b31802023-10-06 16:59:46 +02001242 mbedtls_platform_zeroize(check_tag, tag_len);
1243 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001244}
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001245#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001246
1247/*
1248 * Packet-oriented wrapper for non-AEAD modes
1249 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001250int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1251 const unsigned char *iv, size_t iv_len,
1252 const unsigned char *input, size_t ilen,
1253 unsigned char *output, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001254{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001256 size_t finish_olen;
1257
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001259 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001260 /* As in the non-PSA case, we don't check that
1261 * a key has been set. If not, the key slot will
1262 * still be in its default state of 0, which is
1263 * guaranteed to be invalid, hence the PSA-call
1264 * below will gracefully fail. */
1265 mbedtls_cipher_context_psa * const cipher_psa =
1266 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1267
1268 psa_status_t status;
1269 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1270 size_t part_len;
1271
Jens Wiklander32b31802023-10-06 16:59:46 +02001272 if (ctx->operation == MBEDTLS_DECRYPT) {
1273 status = psa_cipher_decrypt_setup(&cipher_op,
1274 cipher_psa->slot,
1275 cipher_psa->alg);
1276 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1277 status = psa_cipher_encrypt_setup(&cipher_op,
1278 cipher_psa->slot,
1279 cipher_psa->alg);
1280 } else {
1281 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001282 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001283
1284 /* In the following, we can immediately return on an error,
1285 * because the PSA Crypto API guarantees that cipher operations
1286 * are terminated by unsuccessful calls to psa_cipher_update(),
1287 * and by any call to psa_cipher_finish(). */
Jens Wiklander32b31802023-10-06 16:59:46 +02001288 if (status != PSA_SUCCESS) {
1289 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerome Forissier039e02d2022-08-09 17:10:15 +02001290 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001291
Jens Wiklander32b31802023-10-06 16:59:46 +02001292 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1293 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1294 if (status != PSA_SUCCESS) {
1295 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1296 }
1297 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001298
Jens Wiklander32b31802023-10-06 16:59:46 +02001299 status = psa_cipher_update(&cipher_op,
1300 input, ilen,
1301 output, ilen, olen);
1302 if (status != PSA_SUCCESS) {
1303 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1304 }
1305
1306 status = psa_cipher_finish(&cipher_op,
1307 output + *olen, ilen - *olen,
1308 &part_len);
1309 if (status != PSA_SUCCESS) {
1310 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1311 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001312
1313 *olen += part_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001314 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001315 }
1316#endif /* MBEDTLS_USE_PSA_CRYPTO */
1317
Jens Wiklander32b31802023-10-06 16:59:46 +02001318 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1319 return ret;
1320 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001321
Jens Wiklander32b31802023-10-06 16:59:46 +02001322 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1323 return ret;
1324 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001325
Jens Wiklander32b31802023-10-06 16:59:46 +02001326 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1327 output, olen)) != 0) {
1328 return ret;
1329 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001330
Jens Wiklander32b31802023-10-06 16:59:46 +02001331 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1332 &finish_olen)) != 0) {
1333 return ret;
1334 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001335
1336 *olen += finish_olen;
1337
Jens Wiklander32b31802023-10-06 16:59:46 +02001338 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001339}
1340
1341#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1342/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001343 * Packet-oriented encryption for AEAD modes: internal function used by
1344 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001345 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001346static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1347 const unsigned char *iv, size_t iv_len,
1348 const unsigned char *ad, size_t ad_len,
1349 const unsigned char *input, size_t ilen,
1350 unsigned char *output, size_t *olen,
1351 unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001352{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001353#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001354 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001355 /* As in the non-PSA case, we don't check that
1356 * a key has been set. If not, the key slot will
1357 * still be in its default state of 0, which is
1358 * guaranteed to be invalid, hence the PSA-call
1359 * below will gracefully fail. */
1360 mbedtls_cipher_context_psa * const cipher_psa =
1361 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1362
1363 psa_status_t status;
1364
1365 /* PSA Crypto API always writes the authentication tag
1366 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001367 if (output == NULL || tag != output + ilen) {
1368 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1369 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001370
Jens Wiklander32b31802023-10-06 16:59:46 +02001371 status = psa_aead_encrypt(cipher_psa->slot,
1372 cipher_psa->alg,
1373 iv, iv_len,
1374 ad, ad_len,
1375 input, ilen,
1376 output, ilen + tag_len, olen);
1377 if (status != PSA_SUCCESS) {
1378 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1379 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001380
1381 *olen -= tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001382 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001383 }
1384#endif /* MBEDTLS_USE_PSA_CRYPTO */
1385
Jens Wiklander817466c2018-05-22 13:49:31 +02001386#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001387 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001388 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001389 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1390 ilen, iv, iv_len, ad, ad_len,
1391 input, output, tag_len, tag);
Jens Wiklander817466c2018-05-22 13:49:31 +02001392 }
1393#endif /* MBEDTLS_GCM_C */
1394#if defined(MBEDTLS_CCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001395 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001396 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001397 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1398 iv, iv_len, ad, ad_len, input, output,
1399 tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001400 }
1401#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001402#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001403 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001404 /* ChachaPoly has fixed length nonce and MAC (tag) */
Jens Wiklander32b31802023-10-06 16:59:46 +02001405 if ((iv_len != ctx->cipher_info->iv_size) ||
1406 (tag_len != 16U)) {
1407 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001408 }
1409
1410 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001411 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1412 ilen, iv, ad, ad_len, input, output, tag);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001413 }
1414#endif /* MBEDTLS_CHACHAPOLY_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001415
Jens Wiklander32b31802023-10-06 16:59:46 +02001416 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001417}
1418
1419/*
Jens Wiklander32b31802023-10-06 16:59:46 +02001420 * Packet-oriented encryption for AEAD modes: internal function used by
1421 * mbedtls_cipher_auth_encrypt_ext().
Jens Wiklander817466c2018-05-22 13:49:31 +02001422 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001423static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1424 const unsigned char *iv, size_t iv_len,
1425 const unsigned char *ad, size_t ad_len,
1426 const unsigned char *input, size_t ilen,
1427 unsigned char *output, size_t *olen,
1428 const unsigned char *tag, size_t tag_len)
Jens Wiklander817466c2018-05-22 13:49:31 +02001429{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001430#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jens Wiklander32b31802023-10-06 16:59:46 +02001431 if (ctx->psa_enabled == 1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001432 /* As in the non-PSA case, we don't check that
1433 * a key has been set. If not, the key slot will
1434 * still be in its default state of 0, which is
1435 * guaranteed to be invalid, hence the PSA-call
1436 * below will gracefully fail. */
1437 mbedtls_cipher_context_psa * const cipher_psa =
1438 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1439
1440 psa_status_t status;
1441
1442 /* PSA Crypto API always writes the authentication tag
1443 * at the end of the encrypted message. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001444 if (input == NULL || tag != input + ilen) {
1445 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1446 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001447
Jens Wiklander32b31802023-10-06 16:59:46 +02001448 status = psa_aead_decrypt(cipher_psa->slot,
1449 cipher_psa->alg,
1450 iv, iv_len,
1451 ad, ad_len,
1452 input, ilen + tag_len,
1453 output, ilen, olen);
1454 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1455 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1456 } else if (status != PSA_SUCCESS) {
1457 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1458 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001459
Jens Wiklander32b31802023-10-06 16:59:46 +02001460 return 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001461 }
1462#endif /* MBEDTLS_USE_PSA_CRYPTO */
1463
Jens Wiklander817466c2018-05-22 13:49:31 +02001464#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001465 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001467
1468 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001469 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1470 iv, iv_len, ad, ad_len,
1471 tag, tag_len, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001472
Jens Wiklander32b31802023-10-06 16:59:46 +02001473 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001474 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001475 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001476
Jens Wiklander32b31802023-10-06 16:59:46 +02001477 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001478 }
1479#endif /* MBEDTLS_GCM_C */
1480#if defined(MBEDTLS_CCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001481 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +02001483
1484 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001485 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1486 iv, iv_len, ad, ad_len,
1487 input, output, tag, tag_len);
Jens Wiklander817466c2018-05-22 13:49:31 +02001488
Jens Wiklander32b31802023-10-06 16:59:46 +02001489 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Jens Wiklander817466c2018-05-22 13:49:31 +02001490 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001491 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001492
Jens Wiklander32b31802023-10-06 16:59:46 +02001493 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +02001494 }
1495#endif /* MBEDTLS_CCM_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001496#if defined(MBEDTLS_CHACHAPOLY_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001497 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001499
1500 /* ChachaPoly has fixed length nonce and MAC (tag) */
Jens Wiklander32b31802023-10-06 16:59:46 +02001501 if ((iv_len != ctx->cipher_info->iv_size) ||
1502 (tag_len != 16U)) {
1503 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001504 }
1505
1506 *olen = ilen;
Jens Wiklander32b31802023-10-06 16:59:46 +02001507 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1508 iv, ad, ad_len, tag, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001509
Jens Wiklander32b31802023-10-06 16:59:46 +02001510 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001511 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Jens Wiklander32b31802023-10-06 16:59:46 +02001512 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001513
Jens Wiklander32b31802023-10-06 16:59:46 +02001514 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001515 }
1516#endif /* MBEDTLS_CHACHAPOLY_C */
Jerome Forissier79013242021-07-28 10:24:04 +02001517
Jens Wiklander32b31802023-10-06 16:59:46 +02001518 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001519}
Jerome Forissier79013242021-07-28 10:24:04 +02001520#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1521
1522#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1523/*
1524 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1525 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001526int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1527 const unsigned char *iv, size_t iv_len,
1528 const unsigned char *ad, size_t ad_len,
1529 const unsigned char *input, size_t ilen,
1530 unsigned char *output, size_t output_len,
1531 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001532{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001533#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001534 if (
Jerome Forissier79013242021-07-28 10:24:04 +02001535#if defined(MBEDTLS_USE_PSA_CRYPTO)
1536 ctx->psa_enabled == 0 &&
1537#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001538 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1539 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1540 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1541 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001542
Jerome Forissier79013242021-07-28 10:24:04 +02001543 /* There is no iv, tag or ad associated with KW and KWP,
1544 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001545 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1546 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1547 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001548
Jerome Forissier79013242021-07-28 10:24:04 +02001549 (void) iv;
1550 (void) ad;
1551
Jens Wiklander32b31802023-10-06 16:59:46 +02001552 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1553 output, olen, output_len);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001554 }
1555#endif /* MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001556
Jerome Forissier79013242021-07-28 10:24:04 +02001557#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1558 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001559 if (output_len < ilen + tag_len) {
1560 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1561 }
Jerome Forissier79013242021-07-28 10:24:04 +02001562
Jens Wiklander32b31802023-10-06 16:59:46 +02001563 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1564 input, ilen, output, olen,
1565 output + ilen, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001566 *olen += tag_len;
Jens Wiklander32b31802023-10-06 16:59:46 +02001567 return ret;
Jerome Forissier79013242021-07-28 10:24:04 +02001568#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001569 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jens Wiklander817466c2018-05-22 13:49:31 +02001570#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Jerome Forissier79013242021-07-28 10:24:04 +02001571}
1572
1573/*
1574 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1575 */
Jens Wiklander32b31802023-10-06 16:59:46 +02001576int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1577 const unsigned char *iv, size_t iv_len,
1578 const unsigned char *ad, size_t ad_len,
1579 const unsigned char *input, size_t ilen,
1580 unsigned char *output, size_t output_len,
1581 size_t *olen, size_t tag_len)
Jerome Forissier79013242021-07-28 10:24:04 +02001582{
Jerome Forissier79013242021-07-28 10:24:04 +02001583#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001584 if (
Jerome Forissier79013242021-07-28 10:24:04 +02001585#if defined(MBEDTLS_USE_PSA_CRYPTO)
1586 ctx->psa_enabled == 0 &&
1587#endif
Jens Wiklander32b31802023-10-06 16:59:46 +02001588 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1589 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1590 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1591 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Jerome Forissier79013242021-07-28 10:24:04 +02001592
1593 /* There is no iv, tag or ad associated with KW and KWP,
1594 * so these length should be 0 as documented. */
Jens Wiklander32b31802023-10-06 16:59:46 +02001595 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1596 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1597 }
Jerome Forissier79013242021-07-28 10:24:04 +02001598
1599 (void) iv;
1600 (void) ad;
1601
Jens Wiklander32b31802023-10-06 16:59:46 +02001602 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1603 output, olen, output_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001604 }
1605#endif /* MBEDTLS_NIST_KW_C */
1606
1607#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1608 /* AEAD case: check length before passing on to shared function */
Jens Wiklander32b31802023-10-06 16:59:46 +02001609 if (ilen < tag_len || output_len < ilen - tag_len) {
1610 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1611 }
Jerome Forissier79013242021-07-28 10:24:04 +02001612
Jens Wiklander32b31802023-10-06 16:59:46 +02001613 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1614 input, ilen - tag_len, output, olen,
1615 input + ilen - tag_len, tag_len);
Jerome Forissier79013242021-07-28 10:24:04 +02001616#else
Jens Wiklander32b31802023-10-06 16:59:46 +02001617 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jerome Forissier79013242021-07-28 10:24:04 +02001618#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1619}
1620#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
Jens Wiklander817466c2018-05-22 13:49:31 +02001621
1622#endif /* MBEDTLS_CIPHER_C */