blob: 37a2effc8f5c6e0059a2fbda78e996ceaac41dac [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Gilles Peskinef08ca832023-09-12 19:21:54 +02004 * \brief Generic cipher wrapper for Mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker8123e9d2011-01-06 15:37:30 +000010 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020017#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050018#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000019#include "mbedtls/error.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020020#include "mbedtls/constant_time.h"
Dave Rodgman9f3f73d2023-09-20 14:29:45 +010021#include "constant_time_internal.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000022
Rich Evans00ab4702015-02-06 13:43:58 +000023#include <stdlib.h>
24#include <string.h>
25
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020026#if defined(MBEDTLS_CHACHAPOLY_C)
27#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030028#endif
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020036#endif
37
Daniel Kingbd920622016-05-15 19:56:20 -030038#if defined(MBEDTLS_CHACHA20_C)
39#include "mbedtls/chacha20.h"
40#endif
41
Simon Butcher327398a2016-10-05 14:09:11 +010042#if defined(MBEDTLS_CMAC_C)
43#include "mbedtls/cmac.h"
44#endif
45
Hanno Becker4ccfc402018-11-09 16:10:57 +000046#if defined(MBEDTLS_USE_PSA_CRYPTO)
47#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000048#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000049#endif /* MBEDTLS_USE_PSA_CRYPTO */
50
Jack Lloydffdf2882019-03-07 17:00:32 -050051#if defined(MBEDTLS_NIST_KW_C)
52#include "mbedtls/nist_kw.h"
53#endif
54
Simon Butcher327398a2016-10-05 14:09:11 +010055#include "mbedtls/platform.h"
Simon Butcher327398a2016-10-05 14:09:11 +010056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057#define CIPHER_VALIDATE_RET(cond) \
58 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)
59#define CIPHER_VALIDATE(cond) \
60 MBEDTLS_INTERNAL_VALIDATE(cond)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050061
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020062static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064const int *mbedtls_cipher_list(void)
Paul Bakker72f62662011-01-16 21:27:44 +000065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067 int *type;
68
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 if (!supported_init) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 def = mbedtls_cipher_definitions;
71 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 while (def->type != 0) {
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020074 *type++ = (*def++).type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020076
77 *type = 0;
78
79 supported_init = 1;
80 }
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 return mbedtls_cipher_supported;
Paul Bakker72f62662011-01-16 21:27:44 +000083}
84
Hanno Becker18597cd2018-11-09 16:36:33 +000085const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 const mbedtls_cipher_type_t cipher_type)
Paul Bakker8123e9d2011-01-06 15:37:30 +000087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
91 if (def->type == cipher_type) {
92 return def->info;
93 }
94 }
Paul Bakker343a8702011-06-09 14:27:58 +000095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000097}
98
Hanno Becker18597cd2018-11-09 16:36:33 +000099const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 const char *cipher_name)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if (NULL == cipher_name) {
105 return NULL;
106 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
109 if (!strcmp(def->info->name, cipher_name)) {
110 return def->info;
111 }
112 }
Paul Bakkerfab5c822012-02-06 16:45:10 +0000113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000115}
116
Hanno Becker18597cd2018-11-09 16:36:33 +0000117const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
118 const mbedtls_cipher_id_t cipher_id,
119 int key_bitlen,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 const mbedtls_cipher_mode_t mode)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
125 if (def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200126 def->info->key_bitlen == (unsigned) key_bitlen &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 def->info->mode == mode) {
128 return def->info;
129 }
130 }
Paul Bakkerf46b6952013-09-09 00:08:26 +0200131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 return NULL;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200133}
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200136{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 CIPHER_VALIDATE(ctx != NULL);
138 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200139}
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
Paul Bakker84bbeb52014-07-01 14:53:22 +0200142{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if (ctx == NULL) {
Paul Bakker84bbeb52014-07-01 14:53:22 +0200144 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200146
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000147#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 if (ctx->psa_enabled == 1) {
149 if (ctx->cipher_ctx != NULL) {
Hanno Becker6118e432018-11-09 16:47:20 +0000150 mbedtls_cipher_context_psa * const cipher_psa =
151 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000154 /* xxx_free() doesn't allow to return failures. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 (void) psa_destroy_key(cipher_psa->slot);
Hanno Becker6118e432018-11-09 16:47:20 +0000156 }
157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
159 mbedtls_free(cipher_psa);
Hanno Becker6118e432018-11-09 16:47:20 +0000160 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000163 return;
164 }
165#endif /* MBEDTLS_USE_PSA_CRYPTO */
166
Simon Butcher327398a2016-10-05 14:09:11 +0100167#if defined(MBEDTLS_CMAC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if (ctx->cmac_ctx) {
169 mbedtls_platform_zeroize(ctx->cmac_ctx,
170 sizeof(mbedtls_cmac_context_t));
171 mbedtls_free(ctx->cmac_ctx);
Simon Butcher327398a2016-10-05 14:09:11 +0100172 }
173#endif
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 if (ctx->cipher_ctx) {
176 ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
177 }
Paul Bakker84bbeb52014-07-01 14:53:22 +0200178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180}
181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
183 const mbedtls_cipher_info_t *cipher_info)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 CIPHER_VALIDATE_RET(ctx != NULL);
186 if (cipher_info == NULL) {
187 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
188 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
193 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
194 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000195
196 ctx->cipher_info = cipher_info;
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200199 /*
200 * Ignore possible errors caused by a cipher mode that doesn't use padding
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
Paul Bakker48e93c82013-08-14 12:21:18 +0200204#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
Paul Bakker48e93c82013-08-14 12:21:18 +0200206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210}
211
Hanno Becker4ccfc402018-11-09 16:10:57 +0000212#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
214 const mbedtls_cipher_info_t *cipher_info,
215 size_t taglen)
Hanno Becker4ccfc402018-11-09 16:10:57 +0000216{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000217 psa_algorithm_t alg;
218 mbedtls_cipher_context_psa *cipher_psa;
219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 if (NULL == cipher_info || NULL == ctx) {
221 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
222 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000223
Hanno Becker4ee7e762018-11-17 22:00:38 +0000224 /* Check that the underlying cipher mode and cipher type are
225 * supported by the underlying PSA Crypto implementation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
227 if (alg == 0) {
228 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
229 }
230 if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
231 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
232 }
Hanno Becker6118e432018-11-09 16:47:20 +0000233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
237 if (cipher_psa == NULL) {
238 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
239 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000240 cipher_psa->alg = alg;
241 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242 ctx->cipher_info = cipher_info;
243 ctx->psa_enabled = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 return 0;
Hanno Becker4ccfc402018-11-09 16:10:57 +0000245}
246#endif /* MBEDTLS_USE_PSA_CRYPTO */
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
249 const unsigned char *key,
250 int key_bitlen,
251 const mbedtls_operation_t operation)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 CIPHER_VALIDATE_RET(ctx != NULL);
254 CIPHER_VALIDATE_RET(key != NULL);
255 CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
256 operation == MBEDTLS_DECRYPT);
257 if (ctx->cipher_info == NULL) {
258 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
259 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000260
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000261#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (ctx->psa_enabled == 1) {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000263 mbedtls_cipher_context_psa * const cipher_psa =
264 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000267
268 psa_status_t status;
269 psa_key_type_t key_type;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000271
272 /* PSA Crypto API only accepts byte-aligned keys. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (key_bitlen % 8 != 0) {
274 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
275 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000276
277 /* Don't allow keys to be set multiple times. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
279 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
280 }
Hanno Beckeredda8b82018-11-12 11:59:30 +0000281
Andrzej Kurekc7509322019-01-08 09:36:01 -0500282 key_type = mbedtls_psa_translate_cipher_type(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 ctx->cipher_info->type);
284 if (key_type == 0) {
285 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
286 }
287 psa_set_key_type(&attributes, key_type);
Hanno Beckera395d8f2018-11-12 13:33:16 +0000288
289 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500290 * (encrypt vs. decrypt): it is possible to setup a key for encryption
291 * and use it for AEAD decryption. Until tests relying on this
292 * are changed, allow any usage in PSA. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 psa_set_key_usage_flags(&attributes,
294 /* mbedtls_psa_translate_cipher_operation( operation ); */
295 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
296 psa_set_key_algorithm(&attributes, cipher_psa->alg);
Hanno Beckeredda8b82018-11-12 11:59:30 +0000297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 status = psa_import_key(&attributes, key, key_bytelen,
299 &cipher_psa->slot);
300 switch (status) {
Gilles Peskined2d45c12019-05-27 14:53:13 +0200301 case PSA_SUCCESS:
302 break;
303 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200305 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200307 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200309 }
310 /* Indicate that we own the key slot and need to
311 * destroy it in mbedtls_cipher_free(). */
312 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000313
314 ctx->key_bitlen = key_bitlen;
315 ctx->operation = operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000317 }
318#endif /* MBEDTLS_USE_PSA_CRYPTO */
319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
321 (int) ctx->cipher_info->key_bitlen != key_bitlen) {
322 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200323 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200324
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200325 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326 ctx->operation = operation;
327
Paul Bakker343a8702011-06-09 14:27:58 +0000328 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100329 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000330 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 if (MBEDTLS_ENCRYPT == operation ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100333 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
335 return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
336 ctx->key_bitlen);
Paul Bakker343a8702011-06-09 14:27:58 +0000337 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (MBEDTLS_DECRYPT == operation) {
340 return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
341 ctx->key_bitlen);
342 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345}
346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
348 const unsigned char *iv,
349 size_t iv_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000350{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200351 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 CIPHER_VALIDATE_RET(ctx != NULL);
354 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
355 if (ctx->cipher_info == NULL) {
356 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
357 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000358#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000360 /* While PSA Crypto has an API for multipart
361 * operations, we currently don't make it
362 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000364 }
365#endif /* MBEDTLS_USE_PSA_CRYPTO */
366
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200367 /* avoid buffer overflow in ctx->iv */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
369 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
370 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200373 actual_iv_size = iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 } else {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200375 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200376
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200377 /* avoid reading past the end of input buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 if (actual_iv_size > iv_len) {
379 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
380 }
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200381 }
382
Daniel Kingbd920622016-05-15 19:56:20 -0300383#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100385 /* Even though the actual_iv_size is overwritten with a correct value
386 * of 12 from the cipher info, return an error to indicate that
387 * the input iv_len is wrong. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 if (iv_len != 12) {
389 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
390 }
Andrzej Kurek5375fd92021-12-02 09:29:49 +0100391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
393 iv,
394 0U)) { /* Initial counter value */
395 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel Kingbd920622016-05-15 19:56:20 -0300396 }
397 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100398#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
400 iv_len != 12) {
401 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
402 }
Andrzej Kurekd3530432021-12-02 09:31:58 +0100403#endif
Daniel Kingbd920622016-05-15 19:56:20 -0300404#endif
405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 if (actual_iv_size != 0) {
407 memcpy(ctx->iv, iv, actual_iv_size);
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300408 ctx->iv_size = actual_iv_size;
409 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return 0;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200412}
413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200415{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 CIPHER_VALIDATE_RET(ctx != NULL);
417 if (ctx->cipher_info == NULL) {
418 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
419 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200420
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000421#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000423 /* We don't support resetting PSA-based
424 * cipher contexts, yet. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000426 }
427#endif /* MBEDTLS_USE_PSA_CRYPTO */
428
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429 ctx->unprocessed_len = 0;
430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 return 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200432}
433
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200434#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
436 const unsigned char *ad, size_t ad_len)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200437{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 CIPHER_VALIDATE_RET(ctx != NULL);
439 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
440 if (ctx->cipher_info == NULL) {
441 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
442 }
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200443
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000444#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000446 /* While PSA Crypto has an API for multipart
447 * operations, we currently don't make it
448 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000450 }
451#endif /* MBEDTLS_USE_PSA_CRYPTO */
452
Daniel King8fe47012016-05-17 20:33:28 -0300453#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
455 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
456 ctx->iv, ctx->iv_size, ad, ad_len);
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200457 }
Daniel King8fe47012016-05-17 20:33:28 -0300458#endif
459
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200460#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -0300462 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200463 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 mode = (ctx->operation == MBEDTLS_ENCRYPT)
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200466 ? MBEDTLS_CHACHAPOLY_ENCRYPT
467 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
470 ctx->iv,
471 mode);
472 if (result != 0) {
473 return result;
474 }
Daniel King8fe47012016-05-17 20:33:28 -0300475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
477 ad, ad_len);
Daniel King8fe47012016-05-17 20:33:28 -0300478 }
479#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200483#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
486 size_t ilen, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487{
Janos Follath24eed8d2019-11-22 13:21:35 +0000488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 CIPHER_VALIDATE_RET(ctx != NULL);
492 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
493 CIPHER_VALIDATE_RET(output != NULL);
494 CIPHER_VALIDATE_RET(olen != NULL);
495 if (ctx->cipher_info == NULL) {
496 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
497 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000501 /* While PSA Crypto has an API for multipart
502 * operations, we currently don't make it
503 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000505 }
506#endif /* MBEDTLS_USE_PSA_CRYPTO */
507
Paul Bakker6c212762013-12-16 15:24:50 +0100508 *olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 block_size = mbedtls_cipher_get_block_size(ctx);
510 if (0 == block_size) {
511 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
Gilles Peskinea2bdcb92020-01-21 15:02:14 +0100512 }
Paul Bakker6c212762013-12-16 15:24:50 +0100513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
515 if (ilen != block_size) {
516 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
517 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200518
519 *olen = ilen;
520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
522 ctx->operation, input, output))) {
523 return ret;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200524 }
525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200527 }
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200531 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
533 output);
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200534 }
535#endif
536
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200537#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200539 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
541 ilen, input, output);
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200542 }
543#endif
544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 if (input == output &&
546 (ctx->unprocessed_len != 0 || ilen % block_size)) {
547 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +0100548 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100551 if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200552 size_t copy_len = 0;
553
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554 /*
555 * If there is not enough data for a full block, cache it.
556 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
558 ilen <= block_size - ctx->unprocessed_len) ||
559 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
560 ilen < block_size - ctx->unprocessed_len) ||
561 (ctx->operation == MBEDTLS_ENCRYPT &&
562 ilen < block_size - ctx->unprocessed_len)) {
563 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
564 ilen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565
566 ctx->unprocessed_len += ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 }
569
570 /*
571 * Process cached data first
572 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 if (0 != ctx->unprocessed_len) {
Janos Follath98e28a72016-05-31 14:03:54 +0100574 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
577 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
580 ctx->operation, block_size, ctx->iv,
581 ctx->unprocessed_data, output))) {
582 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583 }
584
Janos Follath98e28a72016-05-31 14:03:54 +0100585 *olen += block_size;
586 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587 ctx->unprocessed_len = 0;
588
589 input += copy_len;
590 ilen -= copy_len;
591 }
592
593 /*
594 * Cache final, incomplete block
595 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 if (0 != ilen) {
Andy Leiserson79e77892017-04-28 20:01:49 -0700597 /* Encryption: only cache partial blocks
598 * Decryption w/ padding: always keep at least one whole block
599 * Decryption w/o padding: only cache partial blocks
600 */
Janos Follath98e28a72016-05-31 14:03:54 +0100601 copy_len = ilen % block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 if (copy_len == 0 &&
Andy Leiserson79e77892017-04-28 20:01:49 -0700603 ctx->operation == MBEDTLS_DECRYPT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 NULL != ctx->add_padding) {
Janos Follath98e28a72016-05-31 14:03:54 +0100605 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700606 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
609 copy_len);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
611 ctx->unprocessed_len += copy_len;
612 ilen -= copy_len;
613 }
614
615 /*
616 * Process remaining full blocks
617 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 if (ilen) {
619 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
620 ctx->operation, ilen, ctx->iv, input,
621 output))) {
622 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000623 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200624
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 *olen += ilen;
626 }
627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
634 if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
635 ctx->operation, ilen,
636 &ctx->unprocessed_len, ctx->iv,
637 input, output))) {
638 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000639 }
640
641 *olen = ilen;
642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000644 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000646
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100647#if defined(MBEDTLS_CIPHER_MODE_OFB)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
649 if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
650 ilen, &ctx->unprocessed_len, ctx->iv,
651 input, output))) {
652 return ret;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100653 }
654
655 *olen = ilen;
656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 return 0;
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100658 }
659#endif /* MBEDTLS_CIPHER_MODE_OFB */
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_CIPHER_MODE_CTR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662 if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
663 if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
664 ilen, &ctx->unprocessed_len, ctx->iv,
665 ctx->unprocessed_data, input, output))) {
666 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000667 }
668
669 *olen = ilen;
670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000674
Jaeden Ameroc6539902018-04-30 17:17:41 +0100675#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
677 if (ctx->unprocessed_len > 0) {
Jaeden Ameroc6539902018-04-30 17:17:41 +0100678 /* We can only process an entire data unit at a time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100680 }
681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100682 ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
683 ctx->operation, ilen, ctx->iv, input, output);
684 if (ret != 0) {
685 return ret;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100686 }
687
688 *olen = ilen;
689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 return 0;
Jaeden Ameroc6539902018-04-30 17:17:41 +0100691 }
692#endif /* MBEDTLS_CIPHER_MODE_XTS */
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
696 if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
697 ilen, input, output))) {
698 return ret;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200699 }
700
701 *olen = ilen;
702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 return 0;
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200704 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708}
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
711#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200712/*
713 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
714 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715static void add_pkcs_padding(unsigned char *output, size_t output_len,
716 size_t data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717{
Paul Bakker23986e52011-04-24 08:57:21 +0000718 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100719 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721 for (i = 0; i < padding_len; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +0000722 output[data_len + i] = (unsigned char) padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724}
725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726static int get_pkcs_padding(unsigned char *input, size_t input_len,
727 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100729 size_t i, pad_idx;
730 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 if (NULL == input || NULL == data_len) {
733 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
734 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735
736 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000737 *data_len = input_len - padding_len;
738
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100739 /* Avoid logical || since it results in a branch */
Dave Rodgmane0ad9a42023-09-20 19:23:58 +0100740 bad |= ~mbedtls_ct_size_mask_ge(input_len, padding_len);
741 bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100742
743 /* The number of bytes checked must be independent of padding_len,
744 * so pick input_len, which is usually 8 or 16 (one block) */
745 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 for (i = 0; i < input_len; i++) {
Dave Rodgman9f3f73d2023-09-20 14:29:45 +0100747 size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
748 bad |= (input[i] ^ padding_len) & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 }
Dave Rodgman5ea6bb02023-09-20 20:14:15 +0100750 return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200755/*
756 * One and zeros padding: fill with 80 00 ... 00
757 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758static void add_one_and_zeros_padding(unsigned char *output,
759 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200760{
761 size_t padding_len = output_len - data_len;
762 unsigned char i = 0;
763
764 output[data_len] = 0x80;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200766 output[data_len + i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200768}
769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
771 size_t *data_len)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200772{
Dave Rodgmandf254f62023-09-20 14:46:12 +0100773 unsigned int bad = 1;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100775 if (NULL == input || NULL == data_len) {
776 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
777 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200778
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100779 *data_len = 0;
Dave Rodgmandf254f62023-09-20 14:46:12 +0100780 size_t in_padding = ~0;
781
782 for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
783 size_t is_nonzero = mbedtls_ct_uint_mask(input[i]);
784
785 size_t hit_first_nonzero = is_nonzero & in_padding;
786
787 *data_len = (*data_len & ~hit_first_nonzero) | ((size_t) i & hit_first_nonzero);
788
Dave Rodgman1d523682023-09-20 16:26:49 +0100789 bad = mbedtls_ct_uint_if((unsigned int) hit_first_nonzero,
790 !mbedtls_ct_size_bool_eq(input[i], 0x80), bad);
Dave Rodgmandf254f62023-09-20 14:46:12 +0100791
792 in_padding = in_padding & ~is_nonzero;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100793 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200794
Dave Rodgman5ea6bb02023-09-20 20:14:15 +0100795 return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200800/*
801 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
802 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803static void add_zeros_and_len_padding(unsigned char *output,
804 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200805{
806 size_t padding_len = output_len - data_len;
807 unsigned char i = 0;
808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809 for (i = 1; i < padding_len; i++) {
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200810 output[data_len + i - 1] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200812 output[output_len - 1] = (unsigned char) padding_len;
813}
814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
816 size_t *data_len)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200817{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100818 size_t i, pad_idx;
819 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if (NULL == input || NULL == data_len) {
822 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
823 }
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200824
825 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200826 *data_len = input_len - padding_len;
827
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100828 /* Avoid logical || since it results in a branch */
Dave Rodgman51773aa2023-09-20 14:51:21 +0100829 bad |= mbedtls_ct_size_mask_ge(padding_len, input_len + 1);
830 bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100831
832 /* The number of bytes checked must be independent of padding_len */
833 pad_idx = input_len - padding_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 for (i = 0; i < input_len - 1; i++) {
Dave Rodgman1d523682023-09-20 16:26:49 +0100835 size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
Dave Rodgman51773aa2023-09-20 14:51:21 +0100836 bad |= input[i] & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 }
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100838
Dave Rodgman5ea6bb02023-09-20 20:14:15 +0100839 return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200844/*
845 * Zero padding: fill with 00 ... 00
846 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847static void add_zeros_padding(unsigned char *output,
848 size_t output_len, size_t data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200849{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200850 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 for (i = data_len; i < output_len; i++) {
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200853 output[i] = 0x00;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200855}
856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857static int get_zeros_padding(unsigned char *input, size_t input_len,
858 size_t *data_len)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200859{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100860 size_t i;
861 unsigned char done = 0, prev_done;
862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 if (NULL == input || NULL == data_len) {
864 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100865 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100867 *data_len = 0;
868 for (i = input_len; i > 0; i--) {
869 prev_done = done;
Dave Rodgmane0ad9a42023-09-20 19:23:58 +0100870 done |= !mbedtls_ct_size_bool_eq(input[i-1], 0);
Dave Rodgmanc1a17f52023-09-20 14:54:29 +0100871 size_t mask = mbedtls_ct_size_mask(done ^ prev_done);
872 *data_len |= i & mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 }
874
875 return 0;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200878
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200879/*
880 * No padding: don't pad :)
881 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200883 * but a trivial get_padding function
884 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100885static int get_no_padding(unsigned char *input, size_t input_len,
886 size_t *data_len)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200887{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (NULL == input || NULL == data_len) {
889 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
890 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200891
892 *data_len = input_len;
893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100894 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
899 unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000900{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901 CIPHER_VALIDATE_RET(ctx != NULL);
902 CIPHER_VALIDATE_RET(output != NULL);
903 CIPHER_VALIDATE_RET(olen != NULL);
904 if (ctx->cipher_info == NULL) {
905 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
906 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000907
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000908#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000910 /* While PSA Crypto has an API for multipart
911 * operations, we currently don't make it
912 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000914 }
915#endif /* MBEDTLS_USE_PSA_CRYPTO */
916
Paul Bakker8123e9d2011-01-06 15:37:30 +0000917 *olen = 0;
918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100919 if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100920 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
922 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100923 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100924 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
925 return 0;
Paul Bakker343a8702011-06-09 14:27:58 +0000926 }
927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
929 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
930 return 0;
Daniel Kingbd920622016-05-15 19:56:20 -0300931 }
932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100933 if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
934 if (ctx->unprocessed_len != 0) {
935 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
936 }
Paul Bakker5e0efa72013-09-08 23:04:04 +0200937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938 return 0;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200939 }
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200943 int ret = 0;
944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 if (MBEDTLS_ENCRYPT == ctx->operation) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200946 /* check for 'no padding' mode */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 if (NULL == ctx->add_padding) {
948 if (0 != ctx->unprocessed_len) {
949 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
950 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 return 0;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200953 }
954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
956 ctx->unprocessed_len);
957 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200958 /*
959 * For decrypt operations, expect a full block,
960 * or an empty block if no padding
961 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
963 return 0;
964 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000967 }
968
969 /* cipher block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100970 if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
971 ctx->operation,
972 mbedtls_cipher_get_block_size(ctx),
973 ctx->iv,
974 ctx->unprocessed_data, output))) {
975 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000976 }
977
978 /* Set output size for decryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 if (MBEDTLS_DECRYPT == ctx->operation) {
980 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
981 olen);
982 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000983
984 /* Set output size for encryption */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 *olen = mbedtls_cipher_get_block_size(ctx);
986 return 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000987 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200988#else
989 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000993}
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
997 mbedtls_cipher_padding_t mode)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200998{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 CIPHER_VALIDATE_RET(ctx != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
1002 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001003 }
1004
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001006 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001007 /* While PSA Crypto knows about CBC padding
1008 * schemes, we currently don't make them
1009 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (mode != MBEDTLS_PADDING_NONE) {
1011 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1012 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001015 }
1016#endif /* MBEDTLS_USE_PSA_CRYPTO */
1017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001018 switch (mode) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 case MBEDTLS_PADDING_PKCS7:
1021 ctx->add_padding = add_pkcs_padding;
1022 ctx->get_padding = get_pkcs_padding;
1023 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 case MBEDTLS_PADDING_ONE_AND_ZEROS:
1027 ctx->add_padding = add_one_and_zeros_padding;
1028 ctx->get_padding = get_one_and_zeros_padding;
1029 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 case MBEDTLS_PADDING_ZEROS_AND_LEN:
1033 ctx->add_padding = add_zeros_and_len_padding;
1034 ctx->get_padding = get_zeros_and_len_padding;
1035 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001038 case MBEDTLS_PADDING_ZEROS:
1039 ctx->add_padding = add_zeros_padding;
1040 ctx->get_padding = get_zeros_padding;
1041 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001042#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 case MBEDTLS_PADDING_NONE:
1044 ctx->add_padding = NULL;
1045 ctx->get_padding = get_no_padding;
1046 break;
Paul Bakker1a45d912013-08-14 12:04:26 +02001047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 default:
1049 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001050 }
1051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001055
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001056#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1058 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001059{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 CIPHER_VALIDATE_RET(ctx != NULL);
1061 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1062 if (ctx->cipher_info == NULL) {
1063 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1064 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 if (MBEDTLS_ENCRYPT != ctx->operation) {
1067 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1068 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001069
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001070#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001072 /* While PSA Crypto has an API for multipart
1073 * operations, we currently don't make it
1074 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001076 }
1077#endif /* MBEDTLS_USE_PSA_CRYPTO */
1078
Daniel King8fe47012016-05-17 20:33:28 -03001079#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1081 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1082 tag, tag_len);
Daniel King8fe47012016-05-17 20:33:28 -03001083 }
1084#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086#if defined(MBEDTLS_CHACHAPOLY_C)
1087 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
1088 /* Don't allow truncated MAC for Poly1305 */
1089 if (tag_len != 16U) {
1090 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1091 }
1092
1093 return mbedtls_chachapoly_finish(
1094 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1095 }
1096#endif
1097
1098 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001099}
Paul Bakker9af723c2014-05-01 13:03:14 +02001100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1102 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001103{
Daniel King8fe47012016-05-17 20:33:28 -03001104 unsigned char check_tag[16];
Janos Follath24eed8d2019-11-22 13:21:35 +00001105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 CIPHER_VALIDATE_RET(ctx != NULL);
1108 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
1109 if (ctx->cipher_info == NULL) {
1110 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1111 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 if (MBEDTLS_DECRYPT != ctx->operation) {
1114 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001115 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001116
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 if (ctx->psa_enabled == 1) {
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001119 /* While PSA Crypto has an API for multipart
1120 * operations, we currently don't make it
1121 * accessible through the cipher layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001123 }
1124#endif /* MBEDTLS_USE_PSA_CRYPTO */
1125
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001126 /* Status to return on a non-authenticated algorithm. It would make sense
1127 * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
1128 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
1129 * unit tests assume 0. */
1130 ret = 0;
1131
Daniel King8fe47012016-05-17 20:33:28 -03001132#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
1134 if (tag_len > sizeof(check_tag)) {
1135 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1136 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 if (0 != (ret = mbedtls_gcm_finish(
1139 (mbedtls_gcm_context *) ctx->cipher_ctx,
1140 check_tag, tag_len))) {
1141 return ret;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001142 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001143
1144 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001146 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001147 goto exit;
1148 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001149 }
Daniel King8fe47012016-05-17 20:33:28 -03001150#endif /* MBEDTLS_GCM_C */
1151
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001152#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Daniel King8fe47012016-05-17 20:33:28 -03001154 /* Don't allow truncated MAC for Poly1305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 if (tag_len != sizeof(check_tag)) {
1156 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1157 }
Daniel King8fe47012016-05-17 20:33:28 -03001158
Hanno Becker18597cd2018-11-09 16:36:33 +00001159 ret = mbedtls_chachapoly_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1161 if (ret != 0) {
1162 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001163 }
1164
1165 /* Check the tag in "constant-time" */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
Gilles Peskinedc269bb2021-12-13 12:32:43 +01001167 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskinef9a05012021-12-13 16:57:47 +01001168 goto exit;
1169 }
Daniel King8fe47012016-05-17 20:33:28 -03001170 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001171#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001172
Gilles Peskinef9a05012021-12-13 16:57:47 +01001173exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 mbedtls_platform_zeroize(check_tag, tag_len);
1175 return ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001176}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001177#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001178
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001179/*
1180 * Packet-oriented wrapper for non-AEAD modes
1181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1183 const unsigned char *iv, size_t iv_len,
1184 const unsigned char *input, size_t ilen,
1185 unsigned char *output, size_t *olen)
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001186{
Janos Follath24eed8d2019-11-22 13:21:35 +00001187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001188 size_t finish_olen;
1189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001190 CIPHER_VALIDATE_RET(ctx != NULL);
1191 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1192 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1193 CIPHER_VALIDATE_RET(output != NULL);
1194 CIPHER_VALIDATE_RET(olen != NULL);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001195
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 if (ctx->psa_enabled == 1) {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001198 /* As in the non-PSA case, we don't check that
1199 * a key has been set. If not, the key slot will
1200 * still be in its default state of 0, which is
1201 * guaranteed to be invalid, hence the PSA-call
1202 * below will gracefully fail. */
1203 mbedtls_cipher_context_psa * const cipher_psa =
1204 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1205
1206 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001207 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001208 size_t part_len;
1209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210 if (ctx->operation == MBEDTLS_DECRYPT) {
1211 status = psa_cipher_decrypt_setup(&cipher_op,
1212 cipher_psa->slot,
1213 cipher_psa->alg);
1214 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1215 status = psa_cipher_encrypt_setup(&cipher_op,
1216 cipher_psa->slot,
1217 cipher_psa->alg);
1218 } else {
1219 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001220 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001221
1222 /* In the following, we can immediately return on an error,
1223 * because the PSA Crypto API guarantees that cipher operations
1224 * are terminated by unsuccessful calls to psa_cipher_update(),
1225 * and by any call to psa_cipher_finish(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 if (status != PSA_SUCCESS) {
1227 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +02001228 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
1231 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1232 if (status != PSA_SUCCESS) {
1233 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1234 }
1235 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 status = psa_cipher_update(&cipher_op,
1238 input, ilen,
1239 output, ilen, olen);
1240 if (status != PSA_SUCCESS) {
1241 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1242 }
1243
1244 status = psa_cipher_finish(&cipher_op,
1245 output + *olen, ilen - *olen,
1246 &part_len);
1247 if (status != PSA_SUCCESS) {
1248 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1249 }
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001250
1251 *olen += part_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001253 }
1254#endif /* MBEDTLS_USE_PSA_CRYPTO */
1255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001256 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1257 return ret;
1258 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1261 return ret;
1262 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1265 output, olen)) != 0) {
1266 return ret;
1267 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1270 &finish_olen)) != 0) {
1271 return ret;
1272 }
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001273
1274 *olen += finish_olen;
1275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 return 0;
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001277}
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001280/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001281 * Packet-oriented encryption for AEAD modes: internal function shared by
1282 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001283 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1285 const unsigned char *iv, size_t iv_len,
1286 const unsigned char *ad, size_t ad_len,
1287 const unsigned char *input, size_t ilen,
1288 unsigned char *output, size_t *olen,
1289 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001290{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001291#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001292 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001293 /* As in the non-PSA case, we don't check that
1294 * a key has been set. If not, the key slot will
1295 * still be in its default state of 0, which is
1296 * guaranteed to be invalid, hence the PSA-call
1297 * below will gracefully fail. */
1298 mbedtls_cipher_context_psa * const cipher_psa =
1299 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1300
1301 psa_status_t status;
1302
1303 /* PSA Crypto API always writes the authentication tag
1304 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001305 if (output == NULL || tag != output + ilen) {
1306 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1307 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 status = psa_aead_encrypt(cipher_psa->slot,
1310 cipher_psa->alg,
1311 iv, iv_len,
1312 ad, ad_len,
1313 input, ilen,
1314 output, ilen + tag_len, olen);
1315 if (status != PSA_SUCCESS) {
1316 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1317 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001318
1319 *olen -= tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001321 }
1322#endif /* MBEDTLS_USE_PSA_CRYPTO */
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001326 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1328 ilen, iv, iv_len, ad, ad_len,
1329 input, output, tag_len, tag);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_GCM_C */
1332#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001334 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1336 iv, iv_len, ad, ad_len, input, output,
1337 tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001340#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001342 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 if ((iv_len != ctx->cipher_info->iv_size) ||
1344 (tag_len != 16U)) {
1345 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001346 }
1347
1348 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1350 ilen, iv, ad, ad_len, input, output, tag);
Daniel King8fe47012016-05-17 20:33:28 -03001351 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001352#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001355}
1356
1357/*
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001358 * Packet-oriented encryption for AEAD modes: internal function shared by
1359 * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1362 const unsigned char *iv, size_t iv_len,
1363 const unsigned char *ad, size_t ad_len,
1364 const unsigned char *input, size_t ilen,
1365 unsigned char *output, size_t *olen,
1366 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001367{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 if (ctx->psa_enabled == 1) {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001370 /* As in the non-PSA case, we don't check that
1371 * a key has been set. If not, the key slot will
1372 * still be in its default state of 0, which is
1373 * guaranteed to be invalid, hence the PSA-call
1374 * below will gracefully fail. */
1375 mbedtls_cipher_context_psa * const cipher_psa =
1376 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1377
1378 psa_status_t status;
1379
1380 /* PSA Crypto API always writes the authentication tag
1381 * at the end of the encrypted message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 if (input == NULL || tag != input + ilen) {
1383 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1384 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386 status = psa_aead_decrypt(cipher_psa->slot,
1387 cipher_psa->alg,
1388 iv, iv_len,
1389 ad, ad_len,
1390 input, ilen + tag_len,
1391 output, ilen, olen);
1392 if (status == PSA_ERROR_INVALID_SIGNATURE) {
1393 return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
1394 } else if (status != PSA_SUCCESS) {
1395 return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
1396 }
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 return 0;
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001399 }
1400#endif /* MBEDTLS_USE_PSA_CRYPTO */
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_GCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001405
1406 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1408 iv, iv_len, ad, ad_len,
1409 tag, tag_len, input, output);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 }
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 return ret;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001416 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_GCM_C */
1418#if defined(MBEDTLS_CCM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001421
1422 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1424 iv, iv_len, ad, ad_len,
1425 input, output, tag, tag_len);
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 return ret;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001434#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001435 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Daniel King8fe47012016-05-17 20:33:28 -03001437
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001438 /* ChachaPoly has fixed length nonce and MAC (tag) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 if ((iv_len != ctx->cipher_info->iv_size) ||
1440 (tag_len != 16U)) {
1441 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Daniel King8fe47012016-05-17 20:33:28 -03001442 }
1443
1444 *olen = ilen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1446 iv, ad, ad_len, tag, input, output);
Daniel King8fe47012016-05-17 20:33:28 -03001447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001449 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001450 }
Daniel King8fe47012016-05-17 20:33:28 -03001451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 return ret;
Daniel King8fe47012016-05-17 20:33:28 -03001453 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001454#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001457}
1458
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001459#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001460/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001461 * Packet-oriented encryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001462 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
1464 const unsigned char *iv, size_t iv_len,
1465 const unsigned char *ad, size_t ad_len,
1466 const unsigned char *input, size_t ilen,
1467 unsigned char *output, size_t *olen,
1468 unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001469{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 CIPHER_VALIDATE_RET(ctx != NULL);
1471 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1472 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1473 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1474 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1475 CIPHER_VALIDATE_RET(olen != NULL);
1476 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1479 input, ilen, output, olen,
1480 tag, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001481}
1482
1483/*
Gilles Peskine4e0a4d42020-12-04 00:48:14 +01001484 * Packet-oriented decryption for AEAD modes: public legacy function.
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001485 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001486int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
1487 const unsigned char *iv, size_t iv_len,
1488 const unsigned char *ad, size_t ad_len,
1489 const unsigned char *input, size_t ilen,
1490 unsigned char *output, size_t *olen,
1491 const unsigned char *tag, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001492{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 CIPHER_VALIDATE_RET(ctx != NULL);
1494 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1495 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1496 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1497 CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
1498 CIPHER_VALIDATE_RET(olen != NULL);
1499 CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
1502 input, ilen, output, olen,
1503 tag, tag_len);
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001504}
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +01001505#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001507
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001508#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
1509/*
1510 * Packet-oriented encryption for AEAD/NIST_KW: public function.
1511 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1513 const unsigned char *iv, size_t iv_len,
1514 const unsigned char *ad, size_t ad_len,
1515 const unsigned char *input, size_t ilen,
1516 unsigned char *output, size_t output_len,
1517 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001518{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001519 CIPHER_VALIDATE_RET(ctx != NULL);
1520 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1521 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1522 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1523 CIPHER_VALIDATE_RET(output != NULL);
1524 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001525
1526#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001528#if defined(MBEDTLS_USE_PSA_CRYPTO)
1529 ctx->psa_enabled == 0 &&
1530#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531 (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1532 MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
1533 mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
1534 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001535
1536 /* There is no iv, tag or ad associated with KW and KWP,
1537 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1539 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1540 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001541
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001542 (void) iv;
1543 (void) ad;
1544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001545 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1546 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001547 }
1548#endif /* MBEDTLS_NIST_KW_C */
1549
1550#if defined(MBEDTLS_CIPHER_MODE_AEAD)
1551 /* AEAD case: check length before passing on to shared function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 if (output_len < ilen + tag_len) {
1553 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1554 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1557 input, ilen, output, olen,
1558 output + ilen, tag_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001559 *olen += tag_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560 return ret;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001561#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001563#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1564}
1565
1566/*
1567 * Packet-oriented decryption for AEAD/NIST_KW: public function.
1568 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001569int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1570 const unsigned char *iv, size_t iv_len,
1571 const unsigned char *ad, size_t ad_len,
1572 const unsigned char *input, size_t ilen,
1573 unsigned char *output, size_t output_len,
1574 size_t *olen, size_t tag_len)
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001575{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001576 CIPHER_VALIDATE_RET(ctx != NULL);
1577 CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
1578 CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
1579 CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
1580 CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
1581 CIPHER_VALIDATE_RET(olen != NULL);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001582
1583#if defined(MBEDTLS_NIST_KW_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 if (
Gilles Peskinea56d3d92020-12-04 00:47:07 +01001585#if defined(MBEDTLS_USE_PSA_CRYPTO)
1586 ctx->psa_enabled == 0 &&
1587#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 (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;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001592
1593 /* There is no iv, tag or ad associated with KW and KWP,
1594 * so these length should be 0 as documented. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
1596 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1597 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001598
Manuel Pégourié-Gonnard841b6fa2020-12-07 10:42:21 +01001599 (void) iv;
1600 (void) ad;
1601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1603 output, olen, output_len);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001604 }
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 if (ilen < tag_len || output_len < ilen - tag_len) {
1610 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1611 }
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 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);
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001616#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardfaddf982020-11-25 13:39:47 +01001618#endif /* MBEDTLS_CIPHER_MODE_AEAD */
1619}
1620#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_CIPHER_C */