blob: 63f1f411d5f980b441f7950d522c7582aa3de0e4 [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
Simon Butcher327398a2016-10-05 14:09:11 +010066#if defined(MBEDTLS_PLATFORM_C)
67#include "mbedtls/platform.h"
68#else
69#define mbedtls_calloc calloc
70#define mbedtls_free free
71#endif
72
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073#define CIPHER_VALIDATE_RET( cond ) \
74 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
75#define CIPHER_VALIDATE( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE( cond )
77
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020078#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030079/* Compare the contents of two buffers in constant time.
80 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030081 * a non-zero value.
82 * This is currently only used by GCM and ChaCha20+Poly1305.
83 */
Hanno Becker18597cd2018-11-09 16:36:33 +000084static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
85 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030086{
87 const unsigned char *p1 = (const unsigned char*) v1;
88 const unsigned char *p2 = (const unsigned char*) v2;
89 size_t i;
90 unsigned char diff;
91
92 for( diff = 0, i = 0; i < len; i++ )
93 diff |= p1[i] ^ p2[i];
94
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -030096}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020097#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030098
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020099static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104 int *type;
105
106 if( ! supported_init )
107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 def = mbedtls_cipher_definitions;
109 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200110
111 while( def->type != 0 )
112 *type++ = (*def++).type;
113
114 *type = 0;
115
116 supported_init = 1;
117 }
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000120}
121
Hanno Becker18597cd2018-11-09 16:36:33 +0000122const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
123 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200128 if( def->type == cipher_type )
129 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000130
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200131 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132}
133
Hanno Becker18597cd2018-11-09 16:36:33 +0000134const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
135 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200138
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200140 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200143 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200144 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000145
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200146 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147}
148
Hanno Becker18597cd2018-11-09 16:36:33 +0000149const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
150 const mbedtls_cipher_id_t cipher_id,
151 int key_bitlen,
152 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200157 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200158 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200159 def->info->mode == mode )
160 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200161
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200162 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200163}
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200166{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200169}
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200172{
173 if( ctx == NULL )
174 return;
175
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000176#if defined(MBEDTLS_USE_PSA_CRYPTO)
177 if( ctx->psa_enabled == 1 )
178 {
Hanno Becker6118e432018-11-09 16:47:20 +0000179 if( ctx->cipher_ctx != NULL )
180 {
181 mbedtls_cipher_context_psa * const cipher_psa =
182 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
183
Hanno Becker19086552018-11-17 22:11:16 +0000184 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker6118e432018-11-09 16:47:20 +0000185 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000186 /* xxx_free() doesn't allow to return failures. */
187 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000188 }
189
190 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
191 mbedtls_free( cipher_psa );
192 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000193
194 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
195 return;
196 }
197#endif /* MBEDTLS_USE_PSA_CRYPTO */
198
Simon Butcher327398a2016-10-05 14:09:11 +0100199#if defined(MBEDTLS_CMAC_C)
200 if( ctx->cmac_ctx )
201 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500202 mbedtls_platform_zeroize( ctx->cmac_ctx,
203 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100204 mbedtls_free( ctx->cmac_ctx );
205 }
206#endif
207
Paul Bakker84bbeb52014-07-01 14:53:22 +0200208 if( ctx->cipher_ctx )
209 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
210
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500211 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200212}
213
Hanno Becker18597cd2018-11-09 16:36:33 +0000214int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
215 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 CIPHER_VALIDATE_RET( ctx != NULL );
218 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222
Paul Bakker343a8702011-06-09 14:27:58 +0000223 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225
226 ctx->cipher_info = cipher_info;
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200229 /*
230 * Ignore possible errors caused by a cipher mode that doesn't use padding
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
233 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200234#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200238
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200239 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000240}
241
Hanno Becker4ccfc402018-11-09 16:10:57 +0000242#if defined(MBEDTLS_USE_PSA_CRYPTO)
243int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Becker20120b32018-11-12 16:26:27 +0000244 const mbedtls_cipher_info_t *cipher_info,
245 size_t taglen )
Hanno Becker4ccfc402018-11-09 16:10:57 +0000246{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000247 psa_algorithm_t alg;
248 mbedtls_cipher_context_psa *cipher_psa;
249
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000250 if( NULL == cipher_info || NULL == ctx )
251 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
252
Hanno Becker4ee7e762018-11-17 22:00:38 +0000253 /* Check that the underlying cipher mode and cipher type are
254 * supported by the underlying PSA Crypto implementation. */
Hanno Becker20120b32018-11-12 16:26:27 +0000255 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker4ee7e762018-11-17 22:00:38 +0000256 if( alg == 0 )
257 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
258 if( mbedtls_psa_translate_cipher_type( cipher_info->type ) == 0 )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000259 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000260
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000261 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
262
Hanno Beckeredda8b82018-11-12 11:59:30 +0000263 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
264 if( cipher_psa == NULL )
265 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
266 cipher_psa->alg = alg;
267 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000268 ctx->cipher_info = cipher_info;
269 ctx->psa_enabled = 1;
270 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000271}
272#endif /* MBEDTLS_USE_PSA_CRYPTO */
273
Hanno Becker18597cd2018-11-09 16:36:33 +0000274int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
275 const unsigned char *key,
276 int key_bitlen,
277 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500279 CIPHER_VALIDATE_RET( ctx != NULL );
280 CIPHER_VALIDATE_RET( key != NULL );
281 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100282 operation == MBEDTLS_DECRYPT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000286#if defined(MBEDTLS_USE_PSA_CRYPTO)
287 if( ctx->psa_enabled == 1 )
288 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000289 mbedtls_cipher_context_psa * const cipher_psa =
290 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
291
292 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
293
294 psa_status_t status;
295 psa_key_type_t key_type;
296 psa_key_usage_t key_usage;
297 psa_key_policy_t key_policy;
298
299 /* PSA Crypto API only accepts byte-aligned keys. */
300 if( key_bitlen % 8 != 0 )
301 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
302
303 /* Don't allow keys to be set multiple times. */
Hanno Becker19086552018-11-17 22:11:16 +0000304 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckeredda8b82018-11-12 11:59:30 +0000305 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
306
Andrzej Kurekc7509322019-01-08 09:36:01 -0500307 key_type = mbedtls_psa_translate_cipher_type(
308 ctx->cipher_info->type );
309 if( key_type == 0 )
310 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
311
312 /* Allocate a key slot to use. */
Hanno Becker242da1e2019-01-25 14:29:12 +0000313 status = psa_allocate_key( &cipher_psa->slot );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000314 if( status != PSA_SUCCESS )
315 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000316
Andrzej Kurek08dfcea2019-01-14 05:01:28 -0500317 /* Indicate that we own the key slot and need to
318 * destroy it in mbedtls_cipher_free(). */
319 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
320
321 /* From that point on, the responsibility for destroying the
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500322 * key slot is on mbedtls_cipher_free(). This includes the case
323 * where the policy setup or key import below fail, as
324 * mbedtls_cipher_free() needs to be called in any case. */
Andrzej Kurek08dfcea2019-01-14 05:01:28 -0500325
Hanno Beckeredda8b82018-11-12 11:59:30 +0000326 /* Setup policy for the new key slot. */
Hanno Becker2169a5e2019-01-25 14:29:33 +0000327 key_policy = psa_key_policy_init();
Hanno Beckera395d8f2018-11-12 13:33:16 +0000328
329 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekf410a5c2019-01-15 03:33:35 -0500330 * (encrypt vs. decrypt): it is possible to setup a key for encryption
331 * and use it for AEAD decryption. Until tests relying on this
332 * are changed, allow any usage in PSA. */
Hanno Beckera395d8f2018-11-12 13:33:16 +0000333 /* key_usage = mbedtls_psa_translate_cipher_operation( operation ); */
334 key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000335 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
336 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
337 if( status != PSA_SUCCESS )
338 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
339
340 /* Populate new key slot. */
Hanno Beckeredda8b82018-11-12 11:59:30 +0000341 status = psa_import_key( cipher_psa->slot,
342 key_type, key, key_bytelen );
343 if( status != PSA_SUCCESS )
344 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
345
346 ctx->key_bitlen = key_bitlen;
347 ctx->operation = operation;
348 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000349 }
350#endif /* MBEDTLS_USE_PSA_CRYPTO */
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200353 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200356 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200357
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200358 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 ctx->operation = operation;
360
Paul Bakker343a8702011-06-09 14:27:58 +0000361 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100362 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( MBEDTLS_ENCRYPT == operation ||
365 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100366 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000368 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
370 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000371 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
375 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378}
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500381 const unsigned char *iv,
382 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200384 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 CIPHER_VALIDATE_RET( ctx != NULL );
387 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
388 if( ctx->cipher_info == NULL )
389 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000390#if defined(MBEDTLS_USE_PSA_CRYPTO)
391 if( ctx->psa_enabled == 1 )
392 {
393 /* While PSA Crypto has an API for multipart
394 * operations, we currently don't make it
395 * accessible through the cipher layer. */
396 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
397 }
398#endif /* MBEDTLS_USE_PSA_CRYPTO */
399
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200400 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
402 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200405 actual_iv_size = iv_len;
406 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200407 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200408 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200409
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200410 /* avoid reading past the end of input buffer */
411 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200413 }
414
Daniel Kingbd920622016-05-15 19:56:20 -0300415#if defined(MBEDTLS_CHACHA20_C)
416 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
417 {
418 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
419 iv,
420 0U ) ) /* Initial counter value */
421 {
422 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
423 }
424 }
425#endif
426
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300427 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300428 {
429 memcpy( ctx->iv, iv, actual_iv_size );
430 ctx->iv_size = actual_iv_size;
431 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200432
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200433 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200434}
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200437{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500438 CIPHER_VALIDATE_RET( ctx != NULL );
439 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200441
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000442#if defined(MBEDTLS_USE_PSA_CRYPTO)
443 if( ctx->psa_enabled == 1 )
444 {
445 /* We don't support resetting PSA-based
446 * cipher contexts, yet. */
447 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
448 }
449#endif /* MBEDTLS_USE_PSA_CRYPTO */
450
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 ctx->unprocessed_len = 0;
452
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200453 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200454}
455
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200456#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200458 const unsigned char *ad, size_t ad_len )
459{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500460 CIPHER_VALIDATE_RET( ctx != NULL );
461 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
462 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200464
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000465#if defined(MBEDTLS_USE_PSA_CRYPTO)
466 if( ctx->psa_enabled == 1 )
467 {
468 /* While PSA Crypto has an API for multipart
469 * operations, we currently don't make it
470 * accessible through the cipher layer. */
471 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
472 }
473#endif /* MBEDTLS_USE_PSA_CRYPTO */
474
Daniel King8fe47012016-05-17 20:33:28 -0300475#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200477 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
479 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200480 }
Daniel King8fe47012016-05-17 20:33:28 -0300481#endif
482
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200483#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300484 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
485 {
486 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200487 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300488
489 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200490 ? MBEDTLS_CHACHAPOLY_ENCRYPT
491 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300492
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200493 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300494 ctx->iv,
495 mode );
496 if ( result != 0 )
497 return( result );
498
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500499 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
500 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300501 }
502#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200503
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200504 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200506#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200509 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510{
Paul Bakkerff61a782011-06-09 15:42:02 +0000511 int ret;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500512 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 CIPHER_VALIDATE_RET( ctx != NULL );
515 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
516 CIPHER_VALIDATE_RET( output != NULL );
517 CIPHER_VALIDATE_RET( olen != NULL );
518 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000521#if defined(MBEDTLS_USE_PSA_CRYPTO)
522 if( ctx->psa_enabled == 1 )
523 {
524 /* While PSA Crypto has an API for multipart
525 * operations, we currently don't make it
526 * accessible through the cipher layer. */
527 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
528 }
529#endif /* MBEDTLS_USE_PSA_CRYPTO */
530
Paul Bakker6c212762013-12-16 15:24:50 +0100531 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100532 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200535 {
Janos Follath98e28a72016-05-31 14:03:54 +0100536 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200538
539 *olen = ilen;
540
541 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
542 ctx->operation, input, output ) ) )
543 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200544 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200545 }
546
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200547 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_GCM_C)
551 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200552 {
553 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
555 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200556 }
557#endif
558
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200559#if defined(MBEDTLS_CHACHAPOLY_C)
560 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
561 {
562 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
564 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200565 }
566#endif
567
Janos Follath98e28a72016-05-31 14:03:54 +0100568 if ( 0 == block_size )
569 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500570 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100571 }
572
Paul Bakker68884e32013-01-07 18:20:04 +0100573 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100574 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100577 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_CIPHER_MODE_CBC)
580 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200582 size_t copy_len = 0;
583
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 /*
585 * If there is not enough data for a full block, cache it.
586 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700587 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000588 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700589 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
590 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000592 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593 {
594 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
595 ilen );
596
597 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200598 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599 }
600
601 /*
602 * Process cached data first
603 */
Janos Follath98e28a72016-05-31 14:03:54 +0100604 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605 {
Janos Follath98e28a72016-05-31 14:03:54 +0100606 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
608 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
609 copy_len );
610
Paul Bakkerff61a782011-06-09 15:42:02 +0000611 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100612 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000613 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000614 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200615 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 }
617
Janos Follath98e28a72016-05-31 14:03:54 +0100618 *olen += block_size;
619 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620 ctx->unprocessed_len = 0;
621
622 input += copy_len;
623 ilen -= copy_len;
624 }
625
626 /*
627 * Cache final, incomplete block
628 */
629 if( 0 != ilen )
630 {
Janos Follath98e28a72016-05-31 14:03:54 +0100631 if( 0 == block_size )
632 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500633 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100634 }
635
Andy Leiserson79e77892017-04-28 20:01:49 -0700636 /* Encryption: only cache partial blocks
637 * Decryption w/ padding: always keep at least one whole block
638 * Decryption w/o padding: only cache partial blocks
639 */
Janos Follath98e28a72016-05-31 14:03:54 +0100640 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700641 if( copy_len == 0 &&
642 ctx->operation == MBEDTLS_DECRYPT &&
643 NULL != ctx->add_padding)
644 {
Janos Follath98e28a72016-05-31 14:03:54 +0100645 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700646 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647
648 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
649 copy_len );
650
651 ctx->unprocessed_len += copy_len;
652 ilen -= copy_len;
653 }
654
655 /*
656 * Process remaining full blocks
657 */
658 if( ilen )
659 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000660 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
661 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200663 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200665
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666 *olen += ilen;
667 }
668
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200669 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_CIPHER_MODE_CFB)
674 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000675 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000676 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000677 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000678 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000679 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000681 }
682
683 *olen = ilen;
684
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200685 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000686 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000688
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100689#if defined(MBEDTLS_CIPHER_MODE_OFB)
690 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
691 {
692 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
693 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
694 {
695 return( ret );
696 }
697
698 *olen = ilen;
699
700 return( 0 );
701 }
702#endif /* MBEDTLS_CIPHER_MODE_OFB */
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_CIPHER_MODE_CTR)
705 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000706 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000707 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000708 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000709 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000710 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200711 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000712 }
713
714 *olen = ilen;
715
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000719
Jaeden Ameroc6539902018-04-30 17:17:41 +0100720#if defined(MBEDTLS_CIPHER_MODE_XTS)
721 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
722 {
723 if( ctx->unprocessed_len > 0 ) {
724 /* We can only process an entire data unit at a time. */
725 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
726 }
727
728 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
729 ctx->operation, ilen, ctx->iv, input, output );
730 if( ret != 0 )
731 {
732 return( ret );
733 }
734
735 *olen = ilen;
736
737 return( 0 );
738 }
739#endif /* MBEDTLS_CIPHER_MODE_XTS */
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#if defined(MBEDTLS_CIPHER_MODE_STREAM)
742 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200743 {
744 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
745 ilen, input, output ) ) )
746 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200747 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200748 }
749
750 *olen = ilen;
751
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200752 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200753 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757}
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
760#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200761/*
762 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
763 */
Paul Bakker23986e52011-04-24 08:57:21 +0000764static void add_pkcs_padding( unsigned char *output, size_t output_len,
765 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766{
Paul Bakker23986e52011-04-24 08:57:21 +0000767 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100768 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769
770 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000771 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772}
773
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200774static int get_pkcs_padding( unsigned char *input, size_t input_len,
775 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100777 size_t i, pad_idx;
778 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000779
Paul Bakkera885d682011-01-20 16:35:05 +0000780 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782
783 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784 *data_len = input_len - padding_len;
785
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100786 /* Avoid logical || since it results in a branch */
787 bad |= padding_len > input_len;
788 bad |= padding_len == 0;
789
790 /* The number of bytes checked must be independent of padding_len,
791 * so pick input_len, which is usually 8 or 16 (one block) */
792 pad_idx = input_len - padding_len;
793 for( i = 0; i < input_len; i++ )
794 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200801/*
802 * One and zeros padding: fill with 80 00 ... 00
803 */
804static void add_one_and_zeros_padding( unsigned char *output,
805 size_t output_len, size_t data_len )
806{
807 size_t padding_len = output_len - data_len;
808 unsigned char i = 0;
809
810 output[data_len] = 0x80;
811 for( i = 1; i < padding_len; i++ )
812 output[data_len + i] = 0x00;
813}
814
815static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
816 size_t *data_len )
817{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100818 size_t i;
819 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200820
821 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823
Micha Krausba8316f2017-12-23 23:40:08 +0100824 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100825 *data_len = 0;
826 for( i = input_len; i > 0; i-- )
827 {
828 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100829 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100830 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100831 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100832 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200835
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200840/*
841 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
842 */
843static void add_zeros_and_len_padding( unsigned char *output,
844 size_t output_len, size_t data_len )
845{
846 size_t padding_len = output_len - data_len;
847 unsigned char i = 0;
848
849 for( i = 1; i < padding_len; i++ )
850 output[data_len + i - 1] = 0x00;
851 output[output_len - 1] = (unsigned char) padding_len;
852}
853
854static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
855 size_t *data_len )
856{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100857 size_t i, pad_idx;
858 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200859
860 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200862
863 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200864 *data_len = input_len - padding_len;
865
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100866 /* Avoid logical || since it results in a branch */
867 bad |= padding_len > input_len;
868 bad |= padding_len == 0;
869
870 /* The number of bytes checked must be independent of padding_len */
871 pad_idx = input_len - padding_len;
872 for( i = 0; i < input_len - 1; i++ )
873 bad |= input[i] * ( i >= pad_idx );
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200880/*
881 * Zero padding: fill with 00 ... 00
882 */
883static void add_zeros_padding( unsigned char *output,
884 size_t output_len, size_t data_len )
885{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200886 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887
888 for( i = data_len; i < output_len; i++ )
889 output[i] = 0x00;
890}
891
892static int get_zeros_padding( unsigned char *input, size_t input_len,
893 size_t *data_len )
894{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100895 size_t i;
896 unsigned char done = 0, prev_done;
897
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200898 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200900
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100901 *data_len = 0;
902 for( i = input_len; i > 0; i-- )
903 {
904 prev_done = done;
905 done |= ( input[i-1] != 0 );
906 *data_len |= i * ( done != prev_done );
907 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200908
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200909 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200912
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200913/*
914 * No padding: don't pad :)
915 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200917 * but a trivial get_padding function
918 */
919static int get_no_padding( unsigned char *input, size_t input_len,
920 size_t *data_len )
921{
922 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200924
925 *data_len = input_len;
926
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200927 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200932 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000933{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500934 CIPHER_VALIDATE_RET( ctx != NULL );
935 CIPHER_VALIDATE_RET( output != NULL );
936 CIPHER_VALIDATE_RET( olen != NULL );
937 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000939
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000940#if defined(MBEDTLS_USE_PSA_CRYPTO)
941 if( ctx->psa_enabled == 1 )
942 {
943 /* While PSA Crypto has an API for multipart
944 * operations, we currently don't make it
945 * accessible through the cipher layer. */
946 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
947 }
948#endif /* MBEDTLS_USE_PSA_CRYPTO */
949
Paul Bakker8123e9d2011-01-06 15:37:30 +0000950 *olen = 0;
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100953 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
955 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100956 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000958 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200959 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000960 }
961
Daniel King8fe47012016-05-17 20:33:28 -0300962 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
963 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300964 {
965 return( 0 );
966 }
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200969 {
970 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200972
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200973 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200974 }
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_CIPHER_MODE_CBC)
977 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200979 int ret = 0;
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000982 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200983 /* check for 'no padding' mode */
984 if( NULL == ctx->add_padding )
985 {
986 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200990 }
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000993 ctx->unprocessed_len );
994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200997 /*
998 * For decrypt operations, expect a full block,
999 * or an empty block if no padding
1000 */
1001 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001002 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005 }
1006
1007 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001008 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001010 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001011 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001012 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001013 }
1014
1015 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001017 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1018 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001019
1020 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001022 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001023 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001024#else
1025 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001029}
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001032int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1033 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001034{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001035 CIPHER_VALIDATE_RET( ctx != NULL );
1036
1037 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001040 }
1041
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001042#if defined(MBEDTLS_USE_PSA_CRYPTO)
1043 if( ctx->psa_enabled == 1 )
1044 {
1045 /* While PSA Crypto knows about CBC padding
1046 * schemes, we currently don't make them
1047 * accessible through the cipher layer. */
1048 if( mode != MBEDTLS_PADDING_NONE )
1049 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1050
1051 return( 0 );
1052 }
1053#endif /* MBEDTLS_USE_PSA_CRYPTO */
1054
Paul Bakker1a45d912013-08-14 12:04:26 +02001055 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1058 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001059 ctx->add_padding = add_pkcs_padding;
1060 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001061 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1064 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001065 ctx->add_padding = add_one_and_zeros_padding;
1066 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001067 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1070 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001071 ctx->add_padding = add_zeros_and_len_padding;
1072 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001073 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1076 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001077 ctx->add_padding = add_zeros_padding;
1078 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001079 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001080#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001082 ctx->add_padding = NULL;
1083 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001084 break;
1085
1086 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001088 }
1089
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001090 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001093
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001094#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001096 unsigned char *tag, size_t tag_len )
1097{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001098 CIPHER_VALIDATE_RET( ctx != NULL );
1099 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1100 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( MBEDTLS_ENCRYPT != ctx->operation )
1104 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001105
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001106#if defined(MBEDTLS_USE_PSA_CRYPTO)
1107 if( ctx->psa_enabled == 1 )
1108 {
1109 /* While PSA Crypto has an API for multipart
1110 * operations, we currently don't make it
1111 * accessible through the cipher layer. */
1112 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001113 }
1114#endif /* MBEDTLS_USE_PSA_CRYPTO */
1115
Daniel King8fe47012016-05-17 20:33:28 -03001116#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001118 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1119 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001120#endif
1121
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001122#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001123 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1124 {
1125 /* Don't allow truncated MAC for Poly1305 */
1126 if ( tag_len != 16U )
1127 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1128
Hanno Becker18597cd2018-11-09 16:36:33 +00001129 return( mbedtls_chachapoly_finish(
1130 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001131 }
1132#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001133
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001134 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001135}
Paul Bakker9af723c2014-05-01 13:03:14 +02001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001138 const unsigned char *tag, size_t tag_len )
1139{
Daniel King8fe47012016-05-17 20:33:28 -03001140 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001141 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001142
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001143 CIPHER_VALIDATE_RET( ctx != NULL );
1144 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1145 if( ctx->cipher_info == NULL )
1146 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1147
1148 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001151 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001152
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001153#if defined(MBEDTLS_USE_PSA_CRYPTO)
1154 if( ctx->psa_enabled == 1 )
1155 {
1156 /* While PSA Crypto has an API for multipart
1157 * operations, we currently don't make it
1158 * accessible through the cipher layer. */
1159 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1160 }
1161#endif /* MBEDTLS_USE_PSA_CRYPTO */
1162
Daniel King8fe47012016-05-17 20:33:28 -03001163#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001165 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001168
Hanno Becker18597cd2018-11-09 16:36:33 +00001169 if( 0 != ( ret = mbedtls_gcm_finish(
1170 (mbedtls_gcm_context *) ctx->cipher_ctx,
1171 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001172 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001173 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001174 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001175
1176 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001177 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001179
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001180 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001181 }
Daniel King8fe47012016-05-17 20:33:28 -03001182#endif /* MBEDTLS_GCM_C */
1183
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001184#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001185 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1186 {
1187 /* Don't allow truncated MAC for Poly1305 */
1188 if ( tag_len != sizeof( check_tag ) )
1189 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1190
Hanno Becker18597cd2018-11-09 16:36:33 +00001191 ret = mbedtls_chachapoly_finish(
1192 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001193 if ( ret != 0 )
1194 {
1195 return( ret );
1196 }
1197
1198 /* Check the tag in "constant-time" */
1199 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1200 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1201
1202 return( 0 );
1203 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001204#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001205
1206 return( 0 );
1207}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001208#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001209
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001210/*
1211 * Packet-oriented wrapper for non-AEAD modes
1212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001214 const unsigned char *iv, size_t iv_len,
1215 const unsigned char *input, size_t ilen,
1216 unsigned char *output, size_t *olen )
1217{
1218 int ret;
1219 size_t finish_olen;
1220
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001221 CIPHER_VALIDATE_RET( ctx != NULL );
1222 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1223 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1224 CIPHER_VALIDATE_RET( output != NULL );
1225 CIPHER_VALIDATE_RET( olen != NULL );
1226
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001227#if defined(MBEDTLS_USE_PSA_CRYPTO)
1228 if( ctx->psa_enabled == 1 )
1229 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001230 /* As in the non-PSA case, we don't check that
1231 * a key has been set. If not, the key slot will
1232 * still be in its default state of 0, which is
1233 * guaranteed to be invalid, hence the PSA-call
1234 * below will gracefully fail. */
1235 mbedtls_cipher_context_psa * const cipher_psa =
1236 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1237
1238 psa_status_t status;
1239 psa_cipher_operation_t cipher_op;
1240 size_t part_len;
1241
1242 if( ctx->operation == MBEDTLS_DECRYPT )
1243 {
1244 status = psa_cipher_decrypt_setup( &cipher_op,
1245 cipher_psa->slot,
1246 cipher_psa->alg );
1247 }
1248 else if( ctx->operation == MBEDTLS_ENCRYPT )
1249 {
1250 status = psa_cipher_encrypt_setup( &cipher_op,
1251 cipher_psa->slot,
1252 cipher_psa->alg );
1253 }
1254 else
1255 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1256
1257 /* In the following, we can immediately return on an error,
1258 * because the PSA Crypto API guarantees that cipher operations
1259 * are terminated by unsuccessful calls to psa_cipher_update(),
1260 * and by any call to psa_cipher_finish(). */
1261 if( status != PSA_SUCCESS )
1262 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1263
1264 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1265 if( status != PSA_SUCCESS )
1266 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1267
1268 status = psa_cipher_update( &cipher_op,
1269 input, ilen,
1270 output, ilen, olen );
1271 if( status != PSA_SUCCESS )
1272 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1273
1274 status = psa_cipher_finish( &cipher_op,
1275 output + *olen, ilen - *olen,
1276 &part_len );
1277 if( status != PSA_SUCCESS )
1278 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1279
1280 *olen += part_len;
1281 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001282 }
1283#endif /* MBEDTLS_USE_PSA_CRYPTO */
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001286 return( ret );
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001289 return( ret );
1290
Hanno Becker18597cd2018-11-09 16:36:33 +00001291 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1292 output, olen ) ) != 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_finish( ctx, output + *olen,
1296 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001297 return( ret );
1298
1299 *olen += finish_olen;
1300
1301 return( 0 );
1302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001305/*
1306 * Packet-oriented encryption for AEAD modes
1307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001309 const unsigned char *iv, size_t iv_len,
1310 const unsigned char *ad, size_t ad_len,
1311 const unsigned char *input, size_t ilen,
1312 unsigned char *output, size_t *olen,
1313 unsigned char *tag, size_t tag_len )
1314{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315 CIPHER_VALIDATE_RET( ctx != NULL );
1316 CIPHER_VALIDATE_RET( iv != NULL );
1317 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1318 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1319 CIPHER_VALIDATE_RET( output != NULL );
1320 CIPHER_VALIDATE_RET( olen != NULL );
1321 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1322
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001323#if defined(MBEDTLS_USE_PSA_CRYPTO)
1324 if( ctx->psa_enabled == 1 )
1325 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001326 /* As in the non-PSA case, we don't check that
1327 * a key has been set. If not, the key slot will
1328 * still be in its default state of 0, which is
1329 * guaranteed to be invalid, hence the PSA-call
1330 * below will gracefully fail. */
1331 mbedtls_cipher_context_psa * const cipher_psa =
1332 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1333
1334 psa_status_t status;
1335
1336 /* PSA Crypto API always writes the authentication tag
1337 * at the end of the encrypted message. */
1338 if( tag != output + ilen )
1339 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1340
1341 status = psa_aead_encrypt( cipher_psa->slot,
1342 cipher_psa->alg,
1343 iv, iv_len,
1344 ad, ad_len,
1345 input, ilen,
1346 output, ilen + tag_len, olen );
1347 if( status != PSA_SUCCESS )
1348 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1349
1350 *olen -= tag_len;
1351 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001352 }
1353#endif /* MBEDTLS_USE_PSA_CRYPTO */
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_GCM_C)
1356 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001357 {
1358 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001359 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1360 ilen, iv, iv_len, ad, ad_len,
1361 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_GCM_C */
1364#if defined(MBEDTLS_CCM_C)
1365 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001366 {
1367 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001369 iv, iv_len, ad, ad_len, input, output,
1370 tag, tag_len ) );
1371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001373#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001374 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1375 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001376 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001377 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001378 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001379 {
1380 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1381 }
1382
1383 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001384 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001385 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001386 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001387#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001390}
1391
1392/*
1393 * Packet-oriented decryption for AEAD modes
1394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001396 const unsigned char *iv, size_t iv_len,
1397 const unsigned char *ad, size_t ad_len,
1398 const unsigned char *input, size_t ilen,
1399 unsigned char *output, size_t *olen,
1400 const unsigned char *tag, size_t tag_len )
1401{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001402 CIPHER_VALIDATE_RET( ctx != NULL );
1403 CIPHER_VALIDATE_RET( iv != NULL );
1404 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1405 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1406 CIPHER_VALIDATE_RET( output != NULL );
1407 CIPHER_VALIDATE_RET( olen != NULL );
1408 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001409
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001410#if defined(MBEDTLS_USE_PSA_CRYPTO)
1411 if( ctx->psa_enabled == 1 )
1412 {
Hanno Beckerfe73ade2018-11-12 16:26:46 +00001413 /* As in the non-PSA case, we don't check that
1414 * a key has been set. If not, the key slot will
1415 * still be in its default state of 0, which is
1416 * guaranteed to be invalid, hence the PSA-call
1417 * below will gracefully fail. */
1418 mbedtls_cipher_context_psa * const cipher_psa =
1419 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1420
1421 psa_status_t status;
1422
1423 /* PSA Crypto API always writes the authentication tag
1424 * at the end of the encrypted message. */
1425 if( tag != input + ilen )
1426 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1427
1428 status = psa_aead_decrypt( cipher_psa->slot,
1429 cipher_psa->alg,
1430 iv, iv_len,
1431 ad, ad_len,
1432 input, ilen + tag_len,
1433 output, ilen, olen );
1434 if( status == PSA_ERROR_INVALID_SIGNATURE )
1435 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1436 else if( status != PSA_SUCCESS )
1437 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1438
1439 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001440 }
1441#endif /* MBEDTLS_USE_PSA_CRYPTO */
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_GCM_C)
1444 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001445 {
1446 int ret;
1447
1448 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001450 iv, iv_len, ad, ad_len,
1451 tag, tag_len, input, output );
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1454 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001455
1456 return( ret );
1457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_GCM_C */
1459#if defined(MBEDTLS_CCM_C)
1460 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001461 {
1462 int ret;
1463
1464 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001466 iv, iv_len, ad, ad_len,
1467 input, output, tag, tag_len );
1468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1470 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001471
1472 return( ret );
1473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001475#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001476 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1477 {
Daniel King8fe47012016-05-17 20:33:28 -03001478 int ret;
1479
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001480 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001481 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001482 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001483 {
1484 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1485 }
1486
1487 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001488 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1489 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001490
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001491 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1492 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001493
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001494 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001495 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001496#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_CIPHER_C */