blob: ae89b935a88fd69f8bc47f8d19210a588ea911b9 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
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.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020041#if defined(MBEDTLS_CHACHAPOLY_C)
42#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020051#endif
52
Daniel Kingbd920622016-05-15 19:56:20 -030053#if defined(MBEDTLS_CHACHA20_C)
54#include "mbedtls/chacha20.h"
55#endif
56
Simon Butcher327398a2016-10-05 14:09:11 +010057#if defined(MBEDTLS_CMAC_C)
58#include "mbedtls/cmac.h"
59#endif
60
Hanno Becker4ccfc402018-11-09 16:10:57 +000061#if defined(MBEDTLS_USE_PSA_CRYPTO)
62#include "psa/crypto.h"
Hanno Beckeredda8b82018-11-12 11:59:30 +000063#include "mbedtls/psa_util.h"
Hanno Becker4ccfc402018-11-09 16:10:57 +000064#endif /* MBEDTLS_USE_PSA_CRYPTO */
65
Jack Lloydffdf2882019-03-07 17:00:32 -050066#if defined(MBEDTLS_NIST_KW_C)
67#include "mbedtls/nist_kw.h"
68#endif
69
Simon Butcher327398a2016-10-05 14:09:11 +010070#if defined(MBEDTLS_PLATFORM_C)
71#include "mbedtls/platform.h"
72#else
73#define mbedtls_calloc calloc
74#define mbedtls_free free
75#endif
76
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077#define CIPHER_VALIDATE_RET( cond ) \
78 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
79#define CIPHER_VALIDATE( cond ) \
80 MBEDTLS_INTERNAL_VALIDATE( cond )
81
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020082#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030083/* Compare the contents of two buffers in constant time.
84 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030085 * a non-zero value.
86 * This is currently only used by GCM and ChaCha20+Poly1305.
87 */
Hanno Becker18597cd2018-11-09 16:36:33 +000088static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
89 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030090{
91 const unsigned char *p1 = (const unsigned char*) v1;
92 const unsigned char *p2 = (const unsigned char*) v2;
93 size_t i;
94 unsigned char diff;
95
96 for( diff = 0, i = 0; i < len; i++ )
97 diff |= p1[i] ^ p2[i];
98
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -0300100}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200101#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -0300102
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200103static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200108 int *type;
109
110 if( ! supported_init )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 def = mbedtls_cipher_definitions;
113 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114
115 while( def->type != 0 )
116 *type++ = (*def++).type;
117
118 *type = 0;
119
120 supported_init = 1;
121 }
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000124}
125
Hanno Becker18597cd2018-11-09 16:36:33 +0000126const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
127 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200132 if( def->type == cipher_type )
133 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000134
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200135 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136}
137
Hanno Becker18597cd2018-11-09 16:36:33 +0000138const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
139 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200142
Paul Bakker8123e9d2011-01-06 15:37:30 +0000143 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200144 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200147 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200148 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000149
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200150 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151}
152
Hanno Becker18597cd2018-11-09 16:36:33 +0000153const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
154 const mbedtls_cipher_id_t cipher_id,
155 int key_bitlen,
156 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200161 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200162 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200163 def->info->mode == mode )
164 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200165
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200166 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200170{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200173}
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176{
177 if( ctx == NULL )
178 return;
179
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000180#if defined(MBEDTLS_USE_PSA_CRYPTO)
181 if( ctx->psa_enabled == 1 )
182 {
Hanno Becker6118e432018-11-09 16:47:20 +0000183 if( ctx->cipher_ctx != NULL )
184 {
185 mbedtls_cipher_context_psa * const cipher_psa =
186 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
187
Hanno Becker19086552018-11-17 22:11:16 +0000188 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000189 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000190 /* xxx_free() doesn't allow to return failures. */
191 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000192 }
193
194 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
195 mbedtls_free( cipher_psa );
196 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000197
198 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
199 return;
200 }
201#endif /* MBEDTLS_USE_PSA_CRYPTO */
202
Simon Butcher327398a2016-10-05 14:09:11 +0100203#if defined(MBEDTLS_CMAC_C)
204 if( ctx->cmac_ctx )
205 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500206 mbedtls_platform_zeroize( ctx->cmac_ctx,
207 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100208 mbedtls_free( ctx->cmac_ctx );
209 }
210#endif
211
Paul Bakker84bbeb52014-07-01 14:53:22 +0200212 if( ctx->cipher_ctx )
213 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
214
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500215 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200216}
217
Hanno Becker18597cd2018-11-09 16:36:33 +0000218int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
219 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 CIPHER_VALIDATE_RET( ctx != NULL );
222 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
Paul Bakker343a8702011-06-09 14:27:58 +0000227 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229
230 ctx->cipher_info = cipher_info;
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200233 /*
234 * Ignore possible errors caused by a cipher mode that doesn't use padding
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
237 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200238#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200242
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200243 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244}
245
Hanno Becker4ccfc402018-11-09 16:10:57 +0000246#if defined(MBEDTLS_USE_PSA_CRYPTO)
247int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000248 const mbedtls_cipher_info_t *cipher_info,
249 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000250{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000251 psa_algorithm_t alg;
252 mbedtls_cipher_context_psa *cipher_psa;
253
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000254 if( NULL == cipher_info || NULL == ctx )
255 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
256
Hanno Becker4ee7e762018-11-17 22:00:38 +0000257 /* Check that the underlying cipher mode and cipher type are
258 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000259 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000260 if( alg == 0 )
261 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
262 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000263 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000264
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000265 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
266
Hanno Beckeredda8b82018-11-12 11:59:30 +0000267 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
268 if( cipher_psa == NULL )
269 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
270 cipher_psa->alg = alg;
271 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000272 ctx->cipher_info = cipher_info;
273 ctx->psa_enabled = 1;
274 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000275}
276#endif /* MBEDTLS_USE_PSA_CRYPTO */
277
Hanno Becker18597cd2018-11-09 16:36:33 +0000278int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
279 const unsigned char *key,
280 int key_bitlen,
281 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 CIPHER_VALIDATE_RET( ctx != NULL );
284 CIPHER_VALIDATE_RET( key != NULL );
285 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100286 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500287 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000290#if defined(MBEDTLS_USE_PSA_CRYPTO)
291 if( ctx->psa_enabled == 1 )
292 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000293 mbedtls_cipher_context_psa * const cipher_psa =
294 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
295
296 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
297
298 psa_status_t status;
299 psa_key_type_t key_type;
300 psa_key_usage_t key_usage;
301 psa_key_policy_t key_policy;
302
303 /* PSA Crypto API only accepts byte-aligned keys. */
304 if( key_bitlen % 8 != 0 )
305 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
306
307 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000308 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000309 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
310
Andrzej Kurekc7509322019-01-08 09:36:01 -0500311 key_type = mbedtls_psa_translate_cipher_type(
312 ctx->cipher_info->type );
313 if( key_type == 0 )
314 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
315
316 /* Allocate a key slot to use. */
Hanno Becker242da1e2019-01-25 14:29:12 +0000317 status = psa_allocate_key( &cipher_psa->slot );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000318 if( status != PSA_SUCCESS )
319 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000320
Andrzej Kurek08dfcea2019-01-14 05:01:28 -0500321 /* Indicate that we own the key slot and need to
322 * destroy it in mbedtls_cipher_free(). */
323 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
324
325 /* From that point on, the responsibility for destroying the
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500326 * key slot is on mbedtls_cipher_free(). This includes the case
327 * where the policy setup or key import below fail, as
328 * mbedtls_cipher_free() needs to be called in any case. */
Andrzej Kurek08dfcea2019-01-14 05:01:28 -0500329
Hanno Beckeredda8b82018-11-12 11:59:30 +0000330 /* Setup policy for the new key slot. */
Hanno Becker2169a5e2019-01-25 14:29:33 +0000331 key_policy = psa_key_policy_init();
Hanno Beckera395d8f2018-11-12 13:33:16 +0000332
333 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500334 * (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. */
Hanno Beckera395d8f2018-11-12 13:33:16 +0000337 /* key_usage = mbedtls_psa_translate_cipher_operation( operation ); */
338 key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000339 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
340 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
341 if( status != PSA_SUCCESS )
342 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000343
344 /* Populate new key slot. */
Hanno Beckeredda8b82018-11-12 11:59:30 +0000345 status = psa_import_key( cipher_psa->slot,
346 key_type, key, key_bytelen );
347 if( status != PSA_SUCCESS )
348 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
349
350 ctx->key_bitlen = key_bitlen;
351 ctx->operation = operation;
352 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000353 }
354#endif /* MBEDTLS_USE_PSA_CRYPTO */
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200357 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200360 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200361
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200362 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 ctx->operation = operation;
364
Paul Bakker343a8702011-06-09 14:27:58 +0000365 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100366 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( MBEDTLS_ENCRYPT == operation ||
369 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100370 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000372 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500373 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
374 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000375 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
379 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382}
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 const unsigned char *iv,
386 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200388 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500390 CIPHER_VALIDATE_RET( ctx != NULL );
391 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
392 if( ctx->cipher_info == NULL )
393 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000394#if defined(MBEDTLS_USE_PSA_CRYPTO)
395 if( ctx->psa_enabled == 1 )
396 {
397 /* While PSA Crypto has an API for multipart
398 * operations, we currently don't make it
399 * accessible through the cipher layer. */
400 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
401 }
402#endif /* MBEDTLS_USE_PSA_CRYPTO */
403
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200404 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
406 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200409 actual_iv_size = iv_len;
410 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200411 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200412 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200413
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200414 /* avoid reading past the end of input buffer */
415 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200417 }
418
Daniel Kingbd920622016-05-15 19:56:20 -0300419#if defined(MBEDTLS_CHACHA20_C)
420 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
421 {
422 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
423 iv,
424 0U ) ) /* Initial counter value */
425 {
426 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
427 }
428 }
429#endif
430
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300431 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300432 {
433 memcpy( ctx->iv, iv, actual_iv_size );
434 ctx->iv_size = actual_iv_size;
435 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200436
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200437 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200438}
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200441{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500442 CIPHER_VALIDATE_RET( ctx != NULL );
443 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200445
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000446#if defined(MBEDTLS_USE_PSA_CRYPTO)
447 if( ctx->psa_enabled == 1 )
448 {
449 /* We don't support resetting PSA-based
450 * cipher contexts, yet. */
451 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
452 }
453#endif /* MBEDTLS_USE_PSA_CRYPTO */
454
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455 ctx->unprocessed_len = 0;
456
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200457 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200458}
459
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200460#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200462 const unsigned char *ad, size_t ad_len )
463{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500464 CIPHER_VALIDATE_RET( ctx != NULL );
465 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
466 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200468
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000469#if defined(MBEDTLS_USE_PSA_CRYPTO)
470 if( ctx->psa_enabled == 1 )
471 {
472 /* While PSA Crypto has an API for multipart
473 * operations, we currently don't make it
474 * accessible through the cipher layer. */
475 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
476 }
477#endif /* MBEDTLS_USE_PSA_CRYPTO */
478
Daniel King8fe47012016-05-17 20:33:28 -0300479#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200481 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
483 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200484 }
Daniel King8fe47012016-05-17 20:33:28 -0300485#endif
486
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200487#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300488 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
489 {
490 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200491 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300492
493 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494 ? MBEDTLS_CHACHAPOLY_ENCRYPT
495 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300496
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200497 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300498 ctx->iv,
499 mode );
500 if ( result != 0 )
501 return( result );
502
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500503 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
504 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300505 }
506#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200507
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200510#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200513 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514{
Paul Bakkerff61a782011-06-09 15:42:02 +0000515 int ret;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500516 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500518 CIPHER_VALIDATE_RET( ctx != NULL );
519 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
520 CIPHER_VALIDATE_RET( output != NULL );
521 CIPHER_VALIDATE_RET( olen != NULL );
522 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000525#if defined(MBEDTLS_USE_PSA_CRYPTO)
526 if( ctx->psa_enabled == 1 )
527 {
528 /* While PSA Crypto has an API for multipart
529 * operations, we currently don't make it
530 * accessible through the cipher layer. */
531 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
532 }
533#endif /* MBEDTLS_USE_PSA_CRYPTO */
534
Paul Bakker6c212762013-12-16 15:24:50 +0100535 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100536 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200539 {
Janos Follath98e28a72016-05-31 14:03:54 +0100540 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200542
543 *olen = ilen;
544
545 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
546 ctx->operation, input, output ) ) )
547 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200548 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200549 }
550
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200551 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200552 }
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_GCM_C)
555 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200556 {
557 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
559 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200560 }
561#endif
562
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200563#if defined(MBEDTLS_CHACHAPOLY_C)
564 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
565 {
566 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
568 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200569 }
570#endif
571
Janos Follath98e28a72016-05-31 14:03:54 +0100572 if ( 0 == block_size )
573 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500574 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100575 }
576
Paul Bakker68884e32013-01-07 18:20:04 +0100577 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100578 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100581 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_CIPHER_MODE_CBC)
584 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200586 size_t copy_len = 0;
587
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588 /*
589 * If there is not enough data for a full block, cache it.
590 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700591 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000592 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700593 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
594 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000596 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 {
598 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
599 ilen );
600
601 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603 }
604
605 /*
606 * Process cached data first
607 */
Janos Follath98e28a72016-05-31 14:03:54 +0100608 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609 {
Janos Follath98e28a72016-05-31 14:03:54 +0100610 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611
612 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
613 copy_len );
614
Paul Bakkerff61a782011-06-09 15:42:02 +0000615 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100616 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000617 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200619 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620 }
621
Janos Follath98e28a72016-05-31 14:03:54 +0100622 *olen += block_size;
623 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624 ctx->unprocessed_len = 0;
625
626 input += copy_len;
627 ilen -= copy_len;
628 }
629
630 /*
631 * Cache final, incomplete block
632 */
633 if( 0 != ilen )
634 {
Janos Follath98e28a72016-05-31 14:03:54 +0100635 if( 0 == block_size )
636 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500637 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100638 }
639
Andy Leiserson79e77892017-04-28 20:01:49 -0700640 /* Encryption: only cache partial blocks
641 * Decryption w/ padding: always keep at least one whole block
642 * Decryption w/o padding: only cache partial blocks
643 */
Janos Follath98e28a72016-05-31 14:03:54 +0100644 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700645 if( copy_len == 0 &&
646 ctx->operation == MBEDTLS_DECRYPT &&
647 NULL != ctx->add_padding)
648 {
Janos Follath98e28a72016-05-31 14:03:54 +0100649 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700650 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651
652 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
653 copy_len );
654
655 ctx->unprocessed_len += copy_len;
656 ilen -= copy_len;
657 }
658
659 /*
660 * Process remaining full blocks
661 */
662 if( ilen )
663 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000664 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
665 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200669
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 *olen += ilen;
671 }
672
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_CIPHER_MODE_CFB)
678 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000679 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000680 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000681 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000682 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000683 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200684 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000685 }
686
687 *olen = ilen;
688
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200689 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000690 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000692
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100693#if defined(MBEDTLS_CIPHER_MODE_OFB)
694 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
695 {
696 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
697 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
698 {
699 return( ret );
700 }
701
702 *olen = ilen;
703
704 return( 0 );
705 }
706#endif /* MBEDTLS_CIPHER_MODE_OFB */
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_CIPHER_MODE_CTR)
709 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000710 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000711 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000712 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000713 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000714 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200715 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000716 }
717
718 *olen = ilen;
719
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200720 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000721 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000723
Jaeden Ameroc6539902018-04-30 17:17:41 +0100724#if defined(MBEDTLS_CIPHER_MODE_XTS)
725 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
726 {
727 if( ctx->unprocessed_len > 0 ) {
728 /* We can only process an entire data unit at a time. */
729 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
730 }
731
732 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
733 ctx->operation, ilen, ctx->iv, input, output );
734 if( ret != 0 )
735 {
736 return( ret );
737 }
738
739 *olen = ilen;
740
741 return( 0 );
742 }
743#endif /* MBEDTLS_CIPHER_MODE_XTS */
744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_CIPHER_MODE_STREAM)
746 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200747 {
748 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
749 ilen, input, output ) ) )
750 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200751 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200752 }
753
754 *olen = ilen;
755
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200756 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200757 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000761}
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
764#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200765/*
766 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
767 */
Paul Bakker23986e52011-04-24 08:57:21 +0000768static void add_pkcs_padding( unsigned char *output, size_t output_len,
769 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770{
Paul Bakker23986e52011-04-24 08:57:21 +0000771 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100772 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773
774 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000775 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776}
777
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200778static int get_pkcs_padding( unsigned char *input, size_t input_len,
779 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100781 size_t i, pad_idx;
782 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783
Paul Bakkera885d682011-01-20 16:35:05 +0000784 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786
787 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000788 *data_len = input_len - padding_len;
789
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100790 /* Avoid logical || since it results in a branch */
791 bad |= padding_len > input_len;
792 bad |= padding_len == 0;
793
794 /* The number of bytes checked must be independent of padding_len,
795 * so pick input_len, which is usually 8 or 16 (one block) */
796 pad_idx = input_len - padding_len;
797 for( i = 0; i < input_len; i++ )
798 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200805/*
806 * One and zeros padding: fill with 80 00 ... 00
807 */
808static void add_one_and_zeros_padding( unsigned char *output,
809 size_t output_len, size_t data_len )
810{
811 size_t padding_len = output_len - data_len;
812 unsigned char i = 0;
813
814 output[data_len] = 0x80;
815 for( i = 1; i < padding_len; i++ )
816 output[data_len + i] = 0x00;
817}
818
819static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
820 size_t *data_len )
821{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100822 size_t i;
823 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200824
825 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200827
Micha Krausba8316f2017-12-23 23:40:08 +0100828 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100829 *data_len = 0;
830 for( i = input_len; i > 0; i-- )
831 {
832 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100833 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100834 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100835 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100836 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200839
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200844/*
845 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
846 */
847static void add_zeros_and_len_padding( unsigned char *output,
848 size_t output_len, size_t data_len )
849{
850 size_t padding_len = output_len - data_len;
851 unsigned char i = 0;
852
853 for( i = 1; i < padding_len; i++ )
854 output[data_len + i - 1] = 0x00;
855 output[output_len - 1] = (unsigned char) padding_len;
856}
857
858static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
859 size_t *data_len )
860{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100861 size_t i, pad_idx;
862 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200863
864 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200866
867 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200868 *data_len = input_len - padding_len;
869
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100870 /* Avoid logical || since it results in a branch */
871 bad |= padding_len > input_len;
872 bad |= padding_len == 0;
873
874 /* The number of bytes checked must be independent of padding_len */
875 pad_idx = input_len - padding_len;
876 for( i = 0; i < input_len - 1; i++ )
877 bad |= input[i] * ( i >= pad_idx );
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200884/*
885 * Zero padding: fill with 00 ... 00
886 */
887static void add_zeros_padding( unsigned char *output,
888 size_t output_len, size_t data_len )
889{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200890 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891
892 for( i = data_len; i < output_len; i++ )
893 output[i] = 0x00;
894}
895
896static int get_zeros_padding( unsigned char *input, size_t input_len,
897 size_t *data_len )
898{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100899 size_t i;
900 unsigned char done = 0, prev_done;
901
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200902 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200904
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100905 *data_len = 0;
906 for( i = input_len; i > 0; i-- )
907 {
908 prev_done = done;
909 done |= ( input[i-1] != 0 );
910 *data_len |= i * ( done != prev_done );
911 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200912
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200913 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200916
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200917/*
918 * No padding: don't pad :)
919 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200921 * but a trivial get_padding function
922 */
923static int get_no_padding( unsigned char *input, size_t input_len,
924 size_t *data_len )
925{
926 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200928
929 *data_len = input_len;
930
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200931 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200936 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000937{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500938 CIPHER_VALIDATE_RET( ctx != NULL );
939 CIPHER_VALIDATE_RET( output != NULL );
940 CIPHER_VALIDATE_RET( olen != NULL );
941 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000943
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000944#if defined(MBEDTLS_USE_PSA_CRYPTO)
945 if( ctx->psa_enabled == 1 )
946 {
947 /* While PSA Crypto has an API for multipart
948 * operations, we currently don't make it
949 * accessible through the cipher layer. */
950 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
951 }
952#endif /* MBEDTLS_USE_PSA_CRYPTO */
953
Paul Bakker8123e9d2011-01-06 15:37:30 +0000954 *olen = 0;
955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100957 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
959 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100960 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000962 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200963 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000964 }
965
Daniel King8fe47012016-05-17 20:33:28 -0300966 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
967 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300968 {
969 return( 0 );
970 }
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200973 {
974 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200976
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200977 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200978 }
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_CIPHER_MODE_CBC)
981 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000982 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200983 int ret = 0;
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000986 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200987 /* check for 'no padding' mode */
988 if( NULL == ctx->add_padding )
989 {
990 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200993 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994 }
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997 ctx->unprocessed_len );
998 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001000 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001001 /*
1002 * For decrypt operations, expect a full block,
1003 * or an empty block if no padding
1004 */
1005 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001006 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001009 }
1010
1011 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001012 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001014 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001015 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001016 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001017 }
1018
1019 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001021 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1022 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001023
1024 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001026 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001027 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001028#else
1029 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001033}
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001036int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1037 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001038{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001039 CIPHER_VALIDATE_RET( ctx != NULL );
1040
1041 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001044 }
1045
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001046#if defined(MBEDTLS_USE_PSA_CRYPTO)
1047 if( ctx->psa_enabled == 1 )
1048 {
1049 /* While PSA Crypto knows about CBC padding
1050 * schemes, we currently don't make them
1051 * accessible through the cipher layer. */
1052 if( mode != MBEDTLS_PADDING_NONE )
1053 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1054
1055 return( 0 );
1056 }
1057#endif /* MBEDTLS_USE_PSA_CRYPTO */
1058
Paul Bakker1a45d912013-08-14 12:04:26 +02001059 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1062 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001063 ctx->add_padding = add_pkcs_padding;
1064 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001065 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1068 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001069 ctx->add_padding = add_one_and_zeros_padding;
1070 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001071 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1074 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001075 ctx->add_padding = add_zeros_and_len_padding;
1076 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001077 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1080 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001081 ctx->add_padding = add_zeros_padding;
1082 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001083 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001086 ctx->add_padding = NULL;
1087 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001088 break;
1089
1090 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001092 }
1093
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001094 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001097
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001098#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001100 unsigned char *tag, size_t tag_len )
1101{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001102 CIPHER_VALIDATE_RET( ctx != NULL );
1103 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1104 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 if( MBEDTLS_ENCRYPT != ctx->operation )
1108 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001109
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001110#if defined(MBEDTLS_USE_PSA_CRYPTO)
1111 if( ctx->psa_enabled == 1 )
1112 {
1113 /* While PSA Crypto has an API for multipart
1114 * operations, we currently don't make it
1115 * accessible through the cipher layer. */
1116 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001117 }
1118#endif /* MBEDTLS_USE_PSA_CRYPTO */
1119
Daniel King8fe47012016-05-17 20:33:28 -03001120#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001122 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1123 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001124#endif
1125
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001126#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001127 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1128 {
1129 /* Don't allow truncated MAC for Poly1305 */
1130 if ( tag_len != 16U )
1131 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1132
Hanno Becker18597cd2018-11-09 16:36:33 +00001133 return( mbedtls_chachapoly_finish(
1134 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001135 }
1136#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001137
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001138 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001139}
Paul Bakker9af723c2014-05-01 13:03:14 +02001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001142 const unsigned char *tag, size_t tag_len )
1143{
Daniel King8fe47012016-05-17 20:33:28 -03001144 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001145 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001146
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001147 CIPHER_VALIDATE_RET( ctx != NULL );
1148 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1149 if( ctx->cipher_info == NULL )
1150 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1151
1152 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001155 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001156
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001157#if defined(MBEDTLS_USE_PSA_CRYPTO)
1158 if( ctx->psa_enabled == 1 )
1159 {
1160 /* While PSA Crypto has an API for multipart
1161 * operations, we currently don't make it
1162 * accessible through the cipher layer. */
1163 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1164 }
1165#endif /* MBEDTLS_USE_PSA_CRYPTO */
1166
Daniel King8fe47012016-05-17 20:33:28 -03001167#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001169 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001170 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001172
Hanno Becker18597cd2018-11-09 16:36:33 +00001173 if( 0 != ( ret = mbedtls_gcm_finish(
1174 (mbedtls_gcm_context *) ctx->cipher_ctx,
1175 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001176 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001177 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001178 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001179
1180 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001181 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001183
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001184 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001185 }
Daniel King8fe47012016-05-17 20:33:28 -03001186#endif /* MBEDTLS_GCM_C */
1187
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001188#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001189 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1190 {
1191 /* Don't allow truncated MAC for Poly1305 */
1192 if ( tag_len != sizeof( check_tag ) )
1193 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1194
Hanno Becker18597cd2018-11-09 16:36:33 +00001195 ret = mbedtls_chachapoly_finish(
1196 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001197 if ( ret != 0 )
1198 {
1199 return( ret );
1200 }
1201
1202 /* Check the tag in "constant-time" */
1203 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1204 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1205
1206 return( 0 );
1207 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001208#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001209
1210 return( 0 );
1211}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001212#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001213
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001214/*
1215 * Packet-oriented wrapper for non-AEAD modes
1216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001218 const unsigned char *iv, size_t iv_len,
1219 const unsigned char *input, size_t ilen,
1220 unsigned char *output, size_t *olen )
1221{
1222 int ret;
1223 size_t finish_olen;
1224
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001225 CIPHER_VALIDATE_RET( ctx != NULL );
1226 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1227 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1228 CIPHER_VALIDATE_RET( output != NULL );
1229 CIPHER_VALIDATE_RET( olen != NULL );
1230
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001231#if defined(MBEDTLS_USE_PSA_CRYPTO)
1232 if( ctx->psa_enabled == 1 )
1233 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001234 /* As in the non-PSA case, we don't check that
1235 * a key has been set. If not, the key slot will
1236 * still be in its default state of 0, which is
1237 * guaranteed to be invalid, hence the PSA-call
1238 * below will gracefully fail. */
1239 mbedtls_cipher_context_psa * const cipher_psa =
1240 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1241
1242 psa_status_t status;
Jaeden Amerofe96fbe2019-02-20 10:32:28 +00001243 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001244 size_t part_len;
1245
1246 if( ctx->operation == MBEDTLS_DECRYPT )
1247 {
1248 status = psa_cipher_decrypt_setup( &cipher_op,
1249 cipher_psa->slot,
1250 cipher_psa->alg );
1251 }
1252 else if( ctx->operation == MBEDTLS_ENCRYPT )
1253 {
1254 status = psa_cipher_encrypt_setup( &cipher_op,
1255 cipher_psa->slot,
1256 cipher_psa->alg );
1257 }
1258 else
1259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1260
1261 /* In the following, we can immediately return on an error,
1262 * because the PSA Crypto API guarantees that cipher operations
1263 * are terminated by unsuccessful calls to psa_cipher_update(),
1264 * and by any call to psa_cipher_finish(). */
1265 if( status != PSA_SUCCESS )
1266 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1267
1268 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1269 if( status != PSA_SUCCESS )
1270 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1271
1272 status = psa_cipher_update( &cipher_op,
1273 input, ilen,
1274 output, ilen, olen );
1275 if( status != PSA_SUCCESS )
1276 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1277
1278 status = psa_cipher_finish( &cipher_op,
1279 output + *olen, ilen - *olen,
1280 &part_len );
1281 if( status != PSA_SUCCESS )
1282 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1283
1284 *olen += part_len;
1285 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001286 }
1287#endif /* MBEDTLS_USE_PSA_CRYPTO */
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001290 return( ret );
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001293 return( ret );
1294
Hanno Becker18597cd2018-11-09 16:36:33 +00001295 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1296 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001297 return( ret );
1298
Hanno Becker18597cd2018-11-09 16:36:33 +00001299 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1300 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001301 return( ret );
1302
1303 *olen += finish_olen;
1304
1305 return( 0 );
1306}
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001309/*
1310 * Packet-oriented encryption for AEAD modes
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001313 const unsigned char *iv, size_t iv_len,
1314 const unsigned char *ad, size_t ad_len,
1315 const unsigned char *input, size_t ilen,
1316 unsigned char *output, size_t *olen,
1317 unsigned char *tag, size_t tag_len )
1318{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001319 CIPHER_VALIDATE_RET( ctx != NULL );
1320 CIPHER_VALIDATE_RET( iv != NULL );
1321 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1322 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1323 CIPHER_VALIDATE_RET( output != NULL );
1324 CIPHER_VALIDATE_RET( olen != NULL );
1325 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1326
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001327#if defined(MBEDTLS_USE_PSA_CRYPTO)
1328 if( ctx->psa_enabled == 1 )
1329 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001330 /* As in the non-PSA case, we don't check that
1331 * a key has been set. If not, the key slot will
1332 * still be in its default state of 0, which is
1333 * guaranteed to be invalid, hence the PSA-call
1334 * below will gracefully fail. */
1335 mbedtls_cipher_context_psa * const cipher_psa =
1336 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1337
1338 psa_status_t status;
1339
1340 /* PSA Crypto API always writes the authentication tag
1341 * at the end of the encrypted message. */
1342 if( tag != output + ilen )
1343 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1344
1345 status = psa_aead_encrypt( cipher_psa->slot,
1346 cipher_psa->alg,
1347 iv, iv_len,
1348 ad, ad_len,
1349 input, ilen,
1350 output, ilen + tag_len, olen );
1351 if( status != PSA_SUCCESS )
1352 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1353
1354 *olen -= tag_len;
1355 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001356 }
1357#endif /* MBEDTLS_USE_PSA_CRYPTO */
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#if defined(MBEDTLS_GCM_C)
1360 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001361 {
1362 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001363 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1364 ilen, iv, iv_len, ad, ad_len,
1365 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_GCM_C */
1368#if defined(MBEDTLS_CCM_C)
1369 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001370 {
1371 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001373 iv, iv_len, ad, ad_len, input, output,
1374 tag, tag_len ) );
1375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001377#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001378 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1379 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001380 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001381 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001382 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001383 {
1384 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1385 }
1386
1387 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001388 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001389 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001390 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001391#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001392#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001393 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1394 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001395 {
1396 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1397 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1398
1399 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1400 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1401 {
1402 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1403 }
1404
1405 return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1406 }
1407#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001410}
1411
1412/*
1413 * Packet-oriented decryption for AEAD modes
1414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001416 const unsigned char *iv, size_t iv_len,
1417 const unsigned char *ad, size_t ad_len,
1418 const unsigned char *input, size_t ilen,
1419 unsigned char *output, size_t *olen,
1420 const unsigned char *tag, size_t tag_len )
1421{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001422 CIPHER_VALIDATE_RET( ctx != NULL );
1423 CIPHER_VALIDATE_RET( iv != NULL );
1424 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1425 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1426 CIPHER_VALIDATE_RET( output != NULL );
1427 CIPHER_VALIDATE_RET( olen != NULL );
1428 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001429
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001430#if defined(MBEDTLS_USE_PSA_CRYPTO)
1431 if( ctx->psa_enabled == 1 )
1432 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001433 /* As in the non-PSA case, we don't check that
1434 * a key has been set. If not, the key slot will
1435 * still be in its default state of 0, which is
1436 * guaranteed to be invalid, hence the PSA-call
1437 * below will gracefully fail. */
1438 mbedtls_cipher_context_psa * const cipher_psa =
1439 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1440
1441 psa_status_t status;
1442
1443 /* PSA Crypto API always writes the authentication tag
1444 * at the end of the encrypted message. */
1445 if( tag != input + ilen )
1446 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1447
1448 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_CIPHER_HW_ACCEL_FAILED );
1458
1459 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001460 }
1461#endif /* MBEDTLS_USE_PSA_CRYPTO */
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_GCM_C)
1464 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001465 {
1466 int ret;
1467
1468 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001470 iv, iv_len, ad, ad_len,
1471 tag, tag_len, input, output );
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1474 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001475
1476 return( ret );
1477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_GCM_C */
1479#if defined(MBEDTLS_CCM_C)
1480 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001481 {
1482 int ret;
1483
1484 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001486 iv, iv_len, ad, ad_len,
1487 input, output, tag, tag_len );
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1490 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001491
1492 return( ret );
1493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001495#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001496 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1497 {
Daniel King8fe47012016-05-17 20:33:28 -03001498 int ret;
1499
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001500 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001501 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001502 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001503 {
1504 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1505 }
1506
1507 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001508 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1509 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001510
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001511 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1512 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001513
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001514 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001515 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001516#endif /* MBEDTLS_CHACHAPOLY_C */
Jack Lloydffdf2882019-03-07 17:00:32 -05001517#if defined(MBEDTLS_NIST_KW_C)
Jack Lloyd5f289992019-04-02 10:07:28 -07001518 if( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
1519 MBEDTLS_MODE_KWP == ctx->cipher_info->mode )
Jack Lloydffdf2882019-03-07 17:00:32 -05001520 {
1521 mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
1522 MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
1523
1524 /* There is no iv, tag or ad associated with KW and KWP, these length should be 0 */
1525 if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
1526 {
1527 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1528 }
1529
1530 return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen, output, olen, SIZE_MAX ) );
1531 }
1532#endif /* MBEDTLS_NIST_KW_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_CIPHER_C */