blob: 618277b3b0be36d6193a4b4256e1dc95e03ae4ab [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 Becker098c9de2018-11-09 16:10:57 +000061#if defined(MBEDTLS_USE_PSA_CRYPTO)
62#include "psa/crypto.h"
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +000063#include "mbedtls/psa_util.h"
Hanno Becker098c9de2018-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 Becker21967c52018-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 Becker21967c52018-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 Becker21967c52018-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 Becker21967c52018-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 Becker1cb36532018-11-09 16:20:29 +0000176#if defined(MBEDTLS_USE_PSA_CRYPTO)
177 if( ctx->psa_enabled == 1 )
178 {
Hanno Becker73f59fc2018-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 Beckerce61a322018-11-17 22:11:16 +0000184 if( cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED )
Hanno Becker73f59fc2018-11-09 16:47:20 +0000185 {
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000186 /* xxx_free() doesn't allow to return failures. */
187 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker73f59fc2018-11-09 16:47:20 +0000188 }
189
190 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
191 mbedtls_free( cipher_psa );
192 }
Hanno Becker1cb36532018-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 Becker21967c52018-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 Becker098c9de2018-11-09 16:10:57 +0000242#if defined(MBEDTLS_USE_PSA_CRYPTO)
243int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Beckerf1336402018-11-12 16:26:27 +0000244 const mbedtls_cipher_info_t *cipher_info,
245 size_t taglen )
Hanno Becker098c9de2018-11-09 16:10:57 +0000246{
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000247 psa_algorithm_t alg;
248 mbedtls_cipher_context_psa *cipher_psa;
249
Hanno Becker1cb36532018-11-09 16:20:29 +0000250 if( NULL == cipher_info || NULL == ctx )
251 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
252
Hanno Becker8d88a6e2018-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 Beckerf1336402018-11-12 16:26:27 +0000255 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Becker8d88a6e2018-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 Beckerd9ca5cf2018-11-12 11:59:30 +0000259 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker73f59fc2018-11-09 16:47:20 +0000260
Hanno Becker1cb36532018-11-09 16:20:29 +0000261 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
262
Hanno Beckerd9ca5cf2018-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 Becker1cb36532018-11-09 16:20:29 +0000268 ctx->cipher_info = cipher_info;
269 ctx->psa_enabled = 1;
270 return( 0 );
Hanno Becker098c9de2018-11-09 16:10:57 +0000271}
272#endif /* MBEDTLS_USE_PSA_CRYPTO */
273
Hanno Becker21967c52018-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 ||
282 operation == MBEDTLS_DECRYPT );
283 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000285
286 if( operation != MBEDTLS_DECRYPT &&
287 operation != MBEDTLS_ENCRYPT )
288 {
289 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
290 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291
Hanno Becker1cb36532018-11-09 16:20:29 +0000292#if defined(MBEDTLS_USE_PSA_CRYPTO)
293 if( ctx->psa_enabled == 1 )
294 {
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000295 mbedtls_cipher_context_psa * const cipher_psa =
296 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
297
298 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
299
300 psa_status_t status;
301 psa_key_type_t key_type;
302 psa_key_usage_t key_usage;
303 psa_key_policy_t key_policy;
304
305 /* PSA Crypto API only accepts byte-aligned keys. */
306 if( key_bitlen % 8 != 0 )
307 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
308
309 /* Don't allow keys to be set multiple times. */
Hanno Beckerce61a322018-11-17 22:11:16 +0000310 if( cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET )
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000311 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
312
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500313 key_type = mbedtls_psa_translate_cipher_type(
314 ctx->cipher_info->type );
315 if( key_type == 0 )
316 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
317
318 /* Allocate a key slot to use. */
Hanno Beckere34f6362019-01-25 14:29:12 +0000319 status = psa_allocate_key( &cipher_psa->slot );
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000320 if( status != PSA_SUCCESS )
321 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000322
Andrzej Kureka62a58e2019-01-14 05:01:28 -0500323 /* Indicate that we own the key slot and need to
324 * destroy it in mbedtls_cipher_free(). */
325 cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
326
327 /* From that point on, the responsibility for destroying the
Andrzej Kurekc0a1be02019-01-15 03:33:35 -0500328 * key slot is on mbedtls_cipher_free(). This includes the case
329 * where the policy setup or key import below fail, as
330 * mbedtls_cipher_free() needs to be called in any case. */
Andrzej Kureka62a58e2019-01-14 05:01:28 -0500331
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000332 /* Setup policy for the new key slot. */
Hanno Beckerf99c2ec2019-01-25 14:29:33 +0000333 key_policy = psa_key_policy_init();
Hanno Becker884f6af2018-11-12 13:33:16 +0000334
335 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
Andrzej Kurekc0a1be02019-01-15 03:33:35 -0500336 * (encrypt vs. decrypt): it is possible to setup a key for encryption
337 * and use it for AEAD decryption. Until tests relying on this
338 * are changed, allow any usage in PSA. */
Hanno Becker884f6af2018-11-12 13:33:16 +0000339 /* key_usage = mbedtls_psa_translate_cipher_operation( operation ); */
340 key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000341 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
342 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
343 if( status != PSA_SUCCESS )
344 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
345
346 /* Populate new key slot. */
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000347 status = psa_import_key( cipher_psa->slot,
348 key_type, key, key_bytelen );
349 if( status != PSA_SUCCESS )
350 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
351
352 ctx->key_bitlen = key_bitlen;
353 ctx->operation = operation;
354 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +0000355 }
356#endif /* MBEDTLS_USE_PSA_CRYPTO */
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200359 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200362 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200363
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200364 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000365 ctx->operation = operation;
366
Paul Bakker343a8702011-06-09 14:27:58 +0000367 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100368 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( MBEDTLS_ENCRYPT == operation ||
371 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100372 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000374 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
376 ctx->key_bitlen ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000377 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 if( MBEDTLS_DECRYPT == operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
381 ctx->key_bitlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384}
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 const unsigned char *iv,
388 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200390 size_t actual_iv_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500392 CIPHER_VALIDATE_RET( ctx != NULL );
393 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
394 if( ctx->cipher_info == NULL )
395 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Becker1cb36532018-11-09 16:20:29 +0000396#if defined(MBEDTLS_USE_PSA_CRYPTO)
397 if( ctx->psa_enabled == 1 )
398 {
399 /* While PSA Crypto has an API for multipart
400 * operations, we currently don't make it
401 * accessible through the cipher layer. */
402 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
403 }
404#endif /* MBEDTLS_USE_PSA_CRYPTO */
405
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200406 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
408 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200411 actual_iv_size = iv_len;
412 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200413 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200414 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200415
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200416 /* avoid reading past the end of input buffer */
417 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200419 }
420
Daniel Kingbd920622016-05-15 19:56:20 -0300421#if defined(MBEDTLS_CHACHA20_C)
422 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
423 {
424 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
425 iv,
426 0U ) ) /* Initial counter value */
427 {
428 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
429 }
430 }
431#endif
432
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300433 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300434 {
435 memcpy( ctx->iv, iv, actual_iv_size );
436 ctx->iv_size = actual_iv_size;
437 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200438
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200439 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200440}
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200443{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444 CIPHER_VALIDATE_RET( ctx != NULL );
445 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200447
Hanno Becker1cb36532018-11-09 16:20:29 +0000448#if defined(MBEDTLS_USE_PSA_CRYPTO)
449 if( ctx->psa_enabled == 1 )
450 {
451 /* We don't support resetting PSA-based
452 * cipher contexts, yet. */
453 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
454 }
455#endif /* MBEDTLS_USE_PSA_CRYPTO */
456
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457 ctx->unprocessed_len = 0;
458
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200459 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200460}
461
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200462#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200464 const unsigned char *ad, size_t ad_len )
465{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466 CIPHER_VALIDATE_RET( ctx != NULL );
467 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
468 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200470
Hanno Becker1cb36532018-11-09 16:20:29 +0000471#if defined(MBEDTLS_USE_PSA_CRYPTO)
472 if( ctx->psa_enabled == 1 )
473 {
474 /* While PSA Crypto has an API for multipart
475 * operations, we currently don't make it
476 * accessible through the cipher layer. */
477 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
478 }
479#endif /* MBEDTLS_USE_PSA_CRYPTO */
480
Daniel King8fe47012016-05-17 20:33:28 -0300481#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200483 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
485 ctx->iv, ctx->iv_size, ad, ad_len ) );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200486 }
Daniel King8fe47012016-05-17 20:33:28 -0300487#endif
488
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200489#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300490 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
491 {
492 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200493 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300494
495 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200496 ? MBEDTLS_CHACHAPOLY_ENCRYPT
497 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300498
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200499 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300500 ctx->iv,
501 mode );
502 if ( result != 0 )
503 return( result );
504
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
506 ad, ad_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300507 }
508#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200509
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200512#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200515 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516{
Paul Bakkerff61a782011-06-09 15:42:02 +0000517 int ret;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500518 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000519
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500520 CIPHER_VALIDATE_RET( ctx != NULL );
521 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
522 CIPHER_VALIDATE_RET( output != NULL );
523 CIPHER_VALIDATE_RET( olen != NULL );
524 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526
Hanno Becker1cb36532018-11-09 16:20:29 +0000527#if defined(MBEDTLS_USE_PSA_CRYPTO)
528 if( ctx->psa_enabled == 1 )
529 {
530 /* While PSA Crypto has an API for multipart
531 * operations, we currently don't make it
532 * accessible through the cipher layer. */
533 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
534 }
535#endif /* MBEDTLS_USE_PSA_CRYPTO */
536
Paul Bakker6c212762013-12-16 15:24:50 +0100537 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100538 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200541 {
Janos Follath98e28a72016-05-31 14:03:54 +0100542 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200544
545 *olen = ilen;
546
547 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
548 ctx->operation, input, output ) ) )
549 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200550 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200551 }
552
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200554 }
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_GCM_C)
557 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200558 {
559 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
561 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200562 }
563#endif
564
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200565#if defined(MBEDTLS_CHACHAPOLY_C)
566 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
567 {
568 *olen = ilen;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500569 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
570 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200571 }
572#endif
573
Janos Follath98e28a72016-05-31 14:03:54 +0100574 if ( 0 == block_size )
575 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500576 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100577 }
578
Paul Bakker68884e32013-01-07 18:20:04 +0100579 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100580 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100583 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_CIPHER_MODE_CBC)
586 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200588 size_t copy_len = 0;
589
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590 /*
591 * If there is not enough data for a full block, cache it.
592 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700593 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000594 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700595 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
596 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000598 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599 {
600 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
601 ilen );
602
603 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200604 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605 }
606
607 /*
608 * Process cached data first
609 */
Janos Follath98e28a72016-05-31 14:03:54 +0100610 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611 {
Janos Follath98e28a72016-05-31 14:03:54 +0100612 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
614 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
615 copy_len );
616
Paul Bakkerff61a782011-06-09 15:42:02 +0000617 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100618 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000619 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200621 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622 }
623
Janos Follath98e28a72016-05-31 14:03:54 +0100624 *olen += block_size;
625 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626 ctx->unprocessed_len = 0;
627
628 input += copy_len;
629 ilen -= copy_len;
630 }
631
632 /*
633 * Cache final, incomplete block
634 */
635 if( 0 != ilen )
636 {
Janos Follath98e28a72016-05-31 14:03:54 +0100637 if( 0 == block_size )
638 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500639 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
Janos Follath98e28a72016-05-31 14:03:54 +0100640 }
641
Andy Leiserson79e77892017-04-28 20:01:49 -0700642 /* Encryption: only cache partial blocks
643 * Decryption w/ padding: always keep at least one whole block
644 * Decryption w/o padding: only cache partial blocks
645 */
Janos Follath98e28a72016-05-31 14:03:54 +0100646 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700647 if( copy_len == 0 &&
648 ctx->operation == MBEDTLS_DECRYPT &&
649 NULL != ctx->add_padding)
650 {
Janos Follath98e28a72016-05-31 14:03:54 +0100651 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700652 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653
654 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
655 copy_len );
656
657 ctx->unprocessed_len += copy_len;
658 ilen -= copy_len;
659 }
660
661 /*
662 * Process remaining full blocks
663 */
664 if( ilen )
665 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000666 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
667 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200669 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200671
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 *olen += ilen;
673 }
674
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200675 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_CIPHER_MODE_CFB)
680 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000681 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000682 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000683 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000684 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000685 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200686 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000687 }
688
689 *olen = ilen;
690
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200691 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000694
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100695#if defined(MBEDTLS_CIPHER_MODE_OFB)
696 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
697 {
698 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
699 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
700 {
701 return( ret );
702 }
703
704 *olen = ilen;
705
706 return( 0 );
707 }
708#endif /* MBEDTLS_CIPHER_MODE_OFB */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_CIPHER_MODE_CTR)
711 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000712 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000713 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000714 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000715 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000716 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200717 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000718 }
719
720 *olen = ilen;
721
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200722 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000723 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000725
Jaeden Ameroc6539902018-04-30 17:17:41 +0100726#if defined(MBEDTLS_CIPHER_MODE_XTS)
727 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
728 {
729 if( ctx->unprocessed_len > 0 ) {
730 /* We can only process an entire data unit at a time. */
731 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
732 }
733
734 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
735 ctx->operation, ilen, ctx->iv, input, output );
736 if( ret != 0 )
737 {
738 return( ret );
739 }
740
741 *olen = ilen;
742
743 return( 0 );
744 }
745#endif /* MBEDTLS_CIPHER_MODE_XTS */
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_CIPHER_MODE_STREAM)
748 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200749 {
750 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
751 ilen, input, output ) ) )
752 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200754 }
755
756 *olen = ilen;
757
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200758 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763}
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
766#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200767/*
768 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
769 */
Paul Bakker23986e52011-04-24 08:57:21 +0000770static void add_pkcs_padding( unsigned char *output, size_t output_len,
771 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772{
Paul Bakker23986e52011-04-24 08:57:21 +0000773 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100774 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775
776 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000777 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778}
779
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200780static int get_pkcs_padding( unsigned char *input, size_t input_len,
781 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100783 size_t i, pad_idx;
784 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785
Paul Bakkera885d682011-01-20 16:35:05 +0000786 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000788
789 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790 *data_len = input_len - padding_len;
791
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100792 /* Avoid logical || since it results in a branch */
793 bad |= padding_len > input_len;
794 bad |= padding_len == 0;
795
796 /* The number of bytes checked must be independent of padding_len,
797 * so pick input_len, which is usually 8 or 16 (one block) */
798 pad_idx = input_len - padding_len;
799 for( i = 0; i < input_len; i++ )
800 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200807/*
808 * One and zeros padding: fill with 80 00 ... 00
809 */
810static void add_one_and_zeros_padding( unsigned char *output,
811 size_t output_len, size_t data_len )
812{
813 size_t padding_len = output_len - data_len;
814 unsigned char i = 0;
815
816 output[data_len] = 0x80;
817 for( i = 1; i < padding_len; i++ )
818 output[data_len + i] = 0x00;
819}
820
821static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
822 size_t *data_len )
823{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100824 size_t i;
825 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
827 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200829
Micha Krausba8316f2017-12-23 23:40:08 +0100830 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100831 *data_len = 0;
832 for( i = input_len; i > 0; i-- )
833 {
834 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100835 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100836 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100837 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100838 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200841
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200846/*
847 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
848 */
849static void add_zeros_and_len_padding( unsigned char *output,
850 size_t output_len, size_t data_len )
851{
852 size_t padding_len = output_len - data_len;
853 unsigned char i = 0;
854
855 for( i = 1; i < padding_len; i++ )
856 output[data_len + i - 1] = 0x00;
857 output[output_len - 1] = (unsigned char) padding_len;
858}
859
860static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
861 size_t *data_len )
862{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100863 size_t i, pad_idx;
864 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200865
866 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200868
869 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200870 *data_len = input_len - padding_len;
871
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100872 /* Avoid logical || since it results in a branch */
873 bad |= padding_len > input_len;
874 bad |= padding_len == 0;
875
876 /* The number of bytes checked must be independent of padding_len */
877 pad_idx = input_len - padding_len;
878 for( i = 0; i < input_len - 1; i++ )
879 bad |= input[i] * ( i >= pad_idx );
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200882}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200886/*
887 * Zero padding: fill with 00 ... 00
888 */
889static void add_zeros_padding( unsigned char *output,
890 size_t output_len, size_t data_len )
891{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200892 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200893
894 for( i = data_len; i < output_len; i++ )
895 output[i] = 0x00;
896}
897
898static int get_zeros_padding( unsigned char *input, size_t input_len,
899 size_t *data_len )
900{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100901 size_t i;
902 unsigned char done = 0, prev_done;
903
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200904 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200906
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100907 *data_len = 0;
908 for( i = input_len; i > 0; i-- )
909 {
910 prev_done = done;
911 done |= ( input[i-1] != 0 );
912 *data_len |= i * ( done != prev_done );
913 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200914
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200915 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200918
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200919/*
920 * No padding: don't pad :)
921 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200923 * but a trivial get_padding function
924 */
925static int get_no_padding( unsigned char *input, size_t input_len,
926 size_t *data_len )
927{
928 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200930
931 *data_len = input_len;
932
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200933 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200938 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000939{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500940 CIPHER_VALIDATE_RET( ctx != NULL );
941 CIPHER_VALIDATE_RET( output != NULL );
942 CIPHER_VALIDATE_RET( olen != NULL );
943 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000945
Hanno Becker1cb36532018-11-09 16:20:29 +0000946#if defined(MBEDTLS_USE_PSA_CRYPTO)
947 if( ctx->psa_enabled == 1 )
948 {
949 /* While PSA Crypto has an API for multipart
950 * operations, we currently don't make it
951 * accessible through the cipher layer. */
952 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
953 }
954#endif /* MBEDTLS_USE_PSA_CRYPTO */
955
Paul Bakker8123e9d2011-01-06 15:37:30 +0000956 *olen = 0;
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100959 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
961 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100962 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000964 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200965 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000966 }
967
Daniel King8fe47012016-05-17 20:33:28 -0300968 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
969 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300970 {
971 return( 0 );
972 }
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200975 {
976 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200978
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200979 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200980 }
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_CIPHER_MODE_CBC)
983 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000984 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200985 int ret = 0;
986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000988 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200989 /* check for 'no padding' mode */
990 if( NULL == ctx->add_padding )
991 {
992 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200994
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200995 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200996 }
997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000999 ctx->unprocessed_len );
1000 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001002 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001003 /*
1004 * For decrypt operations, expect a full block,
1005 * or an empty block if no padding
1006 */
1007 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001011 }
1012
1013 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001014 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +00001016 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001017 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001018 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001019 }
1020
1021 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( MBEDTLS_DECRYPT == ctx->operation )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001023 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
1024 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001025
1026 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001028 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001029 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001030#else
1031 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001035}
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker21967c52018-11-09 16:36:33 +00001038int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1039 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001040{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001041 CIPHER_VALIDATE_RET( ctx != NULL );
1042
1043 if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001046 }
1047
Hanno Becker1cb36532018-11-09 16:20:29 +00001048#if defined(MBEDTLS_USE_PSA_CRYPTO)
1049 if( ctx->psa_enabled == 1 )
1050 {
1051 /* While PSA Crypto knows about CBC padding
1052 * schemes, we currently don't make them
1053 * accessible through the cipher layer. */
1054 if( mode != MBEDTLS_PADDING_NONE )
1055 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1056
1057 return( 0 );
1058 }
1059#endif /* MBEDTLS_USE_PSA_CRYPTO */
1060
Paul Bakker1a45d912013-08-14 12:04:26 +02001061 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1064 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001065 ctx->add_padding = add_pkcs_padding;
1066 ctx->get_padding = get_pkcs_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_ONE_AND_ZEROS)
1070 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001071 ctx->add_padding = add_one_and_zeros_padding;
1072 ctx->get_padding = get_one_and_zeros_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_AND_LEN)
1076 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001077 ctx->add_padding = add_zeros_and_len_padding;
1078 ctx->get_padding = get_zeros_and_len_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#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1082 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001083 ctx->add_padding = add_zeros_padding;
1084 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001085 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001088 ctx->add_padding = NULL;
1089 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001090 break;
1091
1092 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001094 }
1095
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001096 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001099
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001100#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001102 unsigned char *tag, size_t tag_len )
1103{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001104 CIPHER_VALIDATE_RET( ctx != NULL );
1105 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1106 if( ctx->cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( MBEDTLS_ENCRYPT != ctx->operation )
1110 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111
Hanno Becker1cb36532018-11-09 16:20:29 +00001112#if defined(MBEDTLS_USE_PSA_CRYPTO)
1113 if( ctx->psa_enabled == 1 )
1114 {
1115 /* While PSA Crypto has an API for multipart
1116 * operations, we currently don't make it
1117 * accessible through the cipher layer. */
1118 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1119
1120 return( 0 );
1121 }
1122#endif /* MBEDTLS_USE_PSA_CRYPTO */
1123
Daniel King8fe47012016-05-17 20:33:28 -03001124#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker21967c52018-11-09 16:36:33 +00001126 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1127 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001128#endif
1129
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001130#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001131 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1132 {
1133 /* Don't allow truncated MAC for Poly1305 */
1134 if ( tag_len != 16U )
1135 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1136
Hanno Becker21967c52018-11-09 16:36:33 +00001137 return( mbedtls_chachapoly_finish(
1138 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001139 }
1140#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001141
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001142 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001143}
Paul Bakker9af723c2014-05-01 13:03:14 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001146 const unsigned char *tag, size_t tag_len )
1147{
Daniel King8fe47012016-05-17 20:33:28 -03001148 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001149 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001150
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001151 CIPHER_VALIDATE_RET( ctx != NULL );
1152 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1153 if( ctx->cipher_info == NULL )
1154 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1155
1156 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001159 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001160
Hanno Becker1cb36532018-11-09 16:20:29 +00001161#if defined(MBEDTLS_USE_PSA_CRYPTO)
1162 if( ctx->psa_enabled == 1 )
1163 {
1164 /* While PSA Crypto has an API for multipart
1165 * operations, we currently don't make it
1166 * accessible through the cipher layer. */
1167 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1168 }
1169#endif /* MBEDTLS_USE_PSA_CRYPTO */
1170
Daniel King8fe47012016-05-17 20:33:28 -03001171#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001173 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001174 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001176
Hanno Becker21967c52018-11-09 16:36:33 +00001177 if( 0 != ( ret = mbedtls_gcm_finish(
1178 (mbedtls_gcm_context *) ctx->cipher_ctx,
1179 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001180 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001181 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001182 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001183
1184 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001185 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001187
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001189 }
Daniel King8fe47012016-05-17 20:33:28 -03001190#endif /* MBEDTLS_GCM_C */
1191
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001192#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001193 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1194 {
1195 /* Don't allow truncated MAC for Poly1305 */
1196 if ( tag_len != sizeof( check_tag ) )
1197 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1198
Hanno Becker21967c52018-11-09 16:36:33 +00001199 ret = mbedtls_chachapoly_finish(
1200 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001201 if ( ret != 0 )
1202 {
1203 return( ret );
1204 }
1205
1206 /* Check the tag in "constant-time" */
1207 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1208 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1209
1210 return( 0 );
1211 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001212#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001213
1214 return( 0 );
1215}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001216#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001217
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001218/*
1219 * Packet-oriented wrapper for non-AEAD modes
1220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001222 const unsigned char *iv, size_t iv_len,
1223 const unsigned char *input, size_t ilen,
1224 unsigned char *output, size_t *olen )
1225{
1226 int ret;
1227 size_t finish_olen;
1228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001229 CIPHER_VALIDATE_RET( ctx != NULL );
1230 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
1231 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1232 CIPHER_VALIDATE_RET( output != NULL );
1233 CIPHER_VALIDATE_RET( olen != NULL );
1234
Hanno Becker1cb36532018-11-09 16:20:29 +00001235#if defined(MBEDTLS_USE_PSA_CRYPTO)
1236 if( ctx->psa_enabled == 1 )
1237 {
Hanno Becker3c852a92018-11-12 12:36:17 +00001238 /* As in the non-PSA case, we don't check that
1239 * a key has been set. If not, the key slot will
1240 * still be in its default state of 0, which is
1241 * guaranteed to be invalid, hence the PSA-call
1242 * below will gracefully fail. */
1243 mbedtls_cipher_context_psa * const cipher_psa =
1244 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1245
1246 psa_status_t status;
1247 psa_cipher_operation_t cipher_op;
1248 size_t part_len;
1249
1250 if( ctx->operation == MBEDTLS_DECRYPT )
1251 {
1252 status = psa_cipher_decrypt_setup( &cipher_op,
1253 cipher_psa->slot,
1254 cipher_psa->alg );
1255 }
1256 else if( ctx->operation == MBEDTLS_ENCRYPT )
1257 {
1258 status = psa_cipher_encrypt_setup( &cipher_op,
1259 cipher_psa->slot,
1260 cipher_psa->alg );
1261 }
1262 else
1263 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1264
1265 /* In the following, we can immediately return on an error,
1266 * because the PSA Crypto API guarantees that cipher operations
1267 * are terminated by unsuccessful calls to psa_cipher_update(),
1268 * and by any call to psa_cipher_finish(). */
1269 if( status != PSA_SUCCESS )
1270 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1271
1272 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1273 if( status != PSA_SUCCESS )
1274 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1275
1276 status = psa_cipher_update( &cipher_op,
1277 input, ilen,
1278 output, ilen, olen );
1279 if( status != PSA_SUCCESS )
1280 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1281
1282 status = psa_cipher_finish( &cipher_op,
1283 output + *olen, ilen - *olen,
1284 &part_len );
1285 if( status != PSA_SUCCESS )
1286 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1287
1288 *olen += part_len;
1289 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +00001290 }
1291#endif /* MBEDTLS_USE_PSA_CRYPTO */
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001294 return( ret );
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001297 return( ret );
1298
Hanno Becker21967c52018-11-09 16:36:33 +00001299 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1300 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001301 return( ret );
1302
Hanno Becker21967c52018-11-09 16:36:33 +00001303 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1304 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001305 return( ret );
1306
1307 *olen += finish_olen;
1308
1309 return( 0 );
1310}
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001313/*
1314 * Packet-oriented encryption for AEAD modes
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001317 const unsigned char *iv, size_t iv_len,
1318 const unsigned char *ad, size_t ad_len,
1319 const unsigned char *input, size_t ilen,
1320 unsigned char *output, size_t *olen,
1321 unsigned char *tag, size_t tag_len )
1322{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001323 CIPHER_VALIDATE_RET( ctx != NULL );
1324 CIPHER_VALIDATE_RET( iv != NULL );
1325 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1326 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1327 CIPHER_VALIDATE_RET( output != NULL );
1328 CIPHER_VALIDATE_RET( olen != NULL );
1329 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1330
Hanno Becker1cb36532018-11-09 16:20:29 +00001331#if defined(MBEDTLS_USE_PSA_CRYPTO)
1332 if( ctx->psa_enabled == 1 )
1333 {
Hanno Becker56150af2018-11-12 16:26:46 +00001334 /* As in the non-PSA case, we don't check that
1335 * a key has been set. If not, the key slot will
1336 * still be in its default state of 0, which is
1337 * guaranteed to be invalid, hence the PSA-call
1338 * below will gracefully fail. */
1339 mbedtls_cipher_context_psa * const cipher_psa =
1340 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1341
1342 psa_status_t status;
1343
1344 /* PSA Crypto API always writes the authentication tag
1345 * at the end of the encrypted message. */
1346 if( tag != output + ilen )
1347 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1348
1349 status = psa_aead_encrypt( cipher_psa->slot,
1350 cipher_psa->alg,
1351 iv, iv_len,
1352 ad, ad_len,
1353 input, ilen,
1354 output, ilen + tag_len, olen );
1355 if( status != PSA_SUCCESS )
1356 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1357
1358 *olen -= tag_len;
1359 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +00001360 }
1361#endif /* MBEDTLS_USE_PSA_CRYPTO */
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_GCM_C)
1364 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001365 {
1366 *olen = ilen;
Hanno Becker21967c52018-11-09 16:36:33 +00001367 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1368 ilen, iv, iv_len, ad, ad_len,
1369 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_GCM_C */
1372#if defined(MBEDTLS_CCM_C)
1373 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001374 {
1375 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001377 iv, iv_len, ad, ad_len, input, output,
1378 tag, tag_len ) );
1379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001381#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001382 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1383 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001384 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001385 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001386 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001387 {
1388 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1389 }
1390
1391 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001392 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001393 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001394 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001395#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001398}
1399
1400/*
1401 * Packet-oriented decryption for AEAD modes
1402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001404 const unsigned char *iv, size_t iv_len,
1405 const unsigned char *ad, size_t ad_len,
1406 const unsigned char *input, size_t ilen,
1407 unsigned char *output, size_t *olen,
1408 const unsigned char *tag, size_t tag_len )
1409{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001410 CIPHER_VALIDATE_RET( ctx != NULL );
1411 CIPHER_VALIDATE_RET( iv != NULL );
1412 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
1413 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
1414 CIPHER_VALIDATE_RET( output != NULL );
1415 CIPHER_VALIDATE_RET( olen != NULL );
1416 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
1417
Hanno Becker1cb36532018-11-09 16:20:29 +00001418#if defined(MBEDTLS_USE_PSA_CRYPTO)
1419 if( ctx->psa_enabled == 1 )
1420 {
Hanno Becker56150af2018-11-12 16:26:46 +00001421 /* As in the non-PSA case, we don't check that
1422 * a key has been set. If not, the key slot will
1423 * still be in its default state of 0, which is
1424 * guaranteed to be invalid, hence the PSA-call
1425 * below will gracefully fail. */
1426 mbedtls_cipher_context_psa * const cipher_psa =
1427 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1428
1429 psa_status_t status;
1430
1431 /* PSA Crypto API always writes the authentication tag
1432 * at the end of the encrypted message. */
1433 if( tag != input + ilen )
1434 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1435
1436 status = psa_aead_decrypt( cipher_psa->slot,
1437 cipher_psa->alg,
1438 iv, iv_len,
1439 ad, ad_len,
1440 input, ilen + tag_len,
1441 output, ilen, olen );
1442 if( status == PSA_ERROR_INVALID_SIGNATURE )
1443 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1444 else if( status != PSA_SUCCESS )
1445 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1446
1447 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +00001448 }
1449#endif /* MBEDTLS_USE_PSA_CRYPTO */
1450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_GCM_C)
1452 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001453 {
1454 int ret;
1455
1456 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001458 iv, iv_len, ad, ad_len,
1459 tag, tag_len, input, output );
1460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1462 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001463
1464 return( ret );
1465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#endif /* MBEDTLS_GCM_C */
1467#if defined(MBEDTLS_CCM_C)
1468 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001469 {
1470 int ret;
1471
1472 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001474 iv, iv_len, ad, ad_len,
1475 input, output, tag, tag_len );
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1478 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001479
1480 return( ret );
1481 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001483#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001484 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1485 {
Daniel King8fe47012016-05-17 20:33:28 -03001486 int ret;
1487
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001488 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001489 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001490 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001491 {
1492 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1493 }
1494
1495 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001496 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1497 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001498
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001499 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1500 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001501
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001502 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001503 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001504#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_CIPHER_C */