blob: 243c73918e3966cd07e2565f881475f8267e6da4 [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
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020073#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030074/* Compare the contents of two buffers in constant time.
75 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030076 * a non-zero value.
77 * This is currently only used by GCM and ChaCha20+Poly1305.
78 */
Hanno Becker18597cd2018-11-09 16:36:33 +000079static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
80 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030081{
82 const unsigned char *p1 = (const unsigned char*) v1;
83 const unsigned char *p2 = (const unsigned char*) v2;
84 size_t i;
85 unsigned char diff;
86
87 for( diff = 0, i = 0; i < len; i++ )
88 diff |= p1[i] ^ p2[i];
89
90 return (int)diff;
91}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020092#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030093
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020094static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020099 int *type;
100
101 if( ! supported_init )
102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 def = mbedtls_cipher_definitions;
104 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200105
106 while( def->type != 0 )
107 *type++ = (*def++).type;
108
109 *type = 0;
110
111 supported_init = 1;
112 }
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000115}
116
Hanno Becker18597cd2018-11-09 16:36:33 +0000117const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
118 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200123 if( def->type == cipher_type )
124 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000125
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200126 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127}
128
Hanno Becker18597cd2018-11-09 16:36:33 +0000129const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
130 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200133
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200135 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200138 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200139 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000140
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200141 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142}
143
Hanno Becker18597cd2018-11-09 16:36:33 +0000144const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
145 const mbedtls_cipher_id_t cipher_id,
146 int key_bitlen,
147 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200152 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200153 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200154 def->info->mode == mode )
155 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200156
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200157 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200158}
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200163}
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200166{
167 if( ctx == NULL )
168 return;
169
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000170#if defined(MBEDTLS_USE_PSA_CRYPTO)
171 if( ctx->psa_enabled == 1 )
172 {
Hanno Becker6118e432018-11-09 16:47:20 +0000173 if( ctx->cipher_ctx != NULL )
174 {
175 mbedtls_cipher_context_psa * const cipher_psa =
176 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
177
178 if( cipher_psa->slot_state == 1 )
179 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000180 /* xxx_free() doesn't allow to return failures. */
181 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker6118e432018-11-09 16:47:20 +0000182 }
183
184 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
185 mbedtls_free( cipher_psa );
186 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000187
188 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
189 return;
190 }
191#endif /* MBEDTLS_USE_PSA_CRYPTO */
192
Simon Butcher327398a2016-10-05 14:09:11 +0100193#if defined(MBEDTLS_CMAC_C)
194 if( ctx->cmac_ctx )
195 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500196 mbedtls_platform_zeroize( ctx->cmac_ctx,
197 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100198 mbedtls_free( ctx->cmac_ctx );
199 }
200#endif
201
Paul Bakker84bbeb52014-07-01 14:53:22 +0200202 if( ctx->cipher_ctx )
203 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
204
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500205 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200206}
207
Hanno Becker18597cd2018-11-09 16:36:33 +0000208int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
209 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000210{
211 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215
Paul Bakker343a8702011-06-09 14:27:58 +0000216 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
219 ctx->cipher_info = cipher_info;
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200222 /*
223 * Ignore possible errors caused by a cipher mode that doesn't use padding
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
226 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200227#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200231
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200232 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233}
234
Hanno Becker4ccfc402018-11-09 16:10:57 +0000235#if defined(MBEDTLS_USE_PSA_CRYPTO)
236int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
237 const mbedtls_cipher_info_t *cipher_info )
238{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000239 psa_algorithm_t alg;
240 mbedtls_cipher_context_psa *cipher_psa;
241
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000242 if( NULL == cipher_info || NULL == ctx )
243 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
244
Hanno Beckeredda8b82018-11-12 11:59:30 +0000245 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode );
246 if( alg == 0)
247 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker6118e432018-11-09 16:47:20 +0000248
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000249 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
250
Hanno Beckeredda8b82018-11-12 11:59:30 +0000251 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
252 if( cipher_psa == NULL )
253 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
254 cipher_psa->alg = alg;
255 ctx->cipher_ctx = cipher_psa;
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000256 ctx->cipher_info = cipher_info;
257 ctx->psa_enabled = 1;
258 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000259}
260#endif /* MBEDTLS_USE_PSA_CRYPTO */
261
Hanno Becker18597cd2018-11-09 16:36:33 +0000262int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
263 const unsigned char *key,
264 int key_bitlen,
265 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266{
Hanno Beckeredda8b82018-11-12 11:59:30 +0000267 if( NULL == ctx || NULL == ctx->cipher_info ||
268 NULL == ctx->cipher_ctx )
269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckeredda8b82018-11-12 11:59:30 +0000271 }
272
273 if( operation != MBEDTLS_DECRYPT &&
274 operation != MBEDTLS_ENCRYPT )
275 {
276 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
277 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000278
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000279#if defined(MBEDTLS_USE_PSA_CRYPTO)
280 if( ctx->psa_enabled == 1 )
281 {
Hanno Beckeredda8b82018-11-12 11:59:30 +0000282 mbedtls_cipher_context_psa * const cipher_psa =
283 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
284
285 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
286
287 psa_status_t status;
288 psa_key_type_t key_type;
289 psa_key_usage_t key_usage;
290 psa_key_policy_t key_policy;
291
292 /* PSA Crypto API only accepts byte-aligned keys. */
293 if( key_bitlen % 8 != 0 )
294 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
295
296 /* Don't allow keys to be set multiple times. */
297 if( cipher_psa->slot_state != 0 )
298 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
299
300 /* Find a fresh key slot to use. */
301 status = mbedtls_psa_get_free_key_slot( &cipher_psa->slot );
302 if( status != PSA_SUCCESS )
303 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
304 cipher_psa->slot_state = 1; /* Indicate that we own the key slot. */
305
306 /* From that point on, the responsibility for destroying the
307 * key slot is on mbedtls_cipher_free(). This includes the case
308 * where the policy setup or key import below fail, as
309 * mbedtls_cipher_free() needs to be called in any case. */
310
311 /* Setup policy for the new key slot. */
312 psa_key_policy_init( &key_policy );
Hanno Beckera395d8f2018-11-12 13:33:16 +0000313
314 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
315 * (encrypt vs. decrypt): it is possible to setup a key for encryption
316 * and use it for AEAD decryption. Until tests relying on this
317 * are changed, allow any usage in PSA. */
318 /* key_usage = mbedtls_psa_translate_cipher_operation( operation ); */
319 key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
Hanno Beckeredda8b82018-11-12 11:59:30 +0000320 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
321 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
322 if( status != PSA_SUCCESS )
323 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
324
325 /* Populate new key slot. */
326 key_type = mbedtls_psa_translate_cipher_type(
327 ctx->cipher_info->type );
328 if( key_type == 0 )
329 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
330 status = psa_import_key( cipher_psa->slot,
331 key_type, key, key_bytelen );
332 if( status != PSA_SUCCESS )
333 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
334
335 ctx->key_bitlen = key_bitlen;
336 ctx->operation = operation;
337 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000338 }
339#endif /* MBEDTLS_USE_PSA_CRYPTO */
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200342 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200345 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200346
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200347 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 ctx->operation = operation;
349
Paul Bakker343a8702011-06-09 14:27:58 +0000350 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100351 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( MBEDTLS_ENCRYPT == operation ||
354 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100355 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000357 {
358 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Hanno Becker18597cd2018-11-09 16:36:33 +0000359 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000360 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000363 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Hanno Becker18597cd2018-11-09 16:36:33 +0000364 ctx->key_bitlen );
365
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368}
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200371 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200373 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300374 if( NULL == ctx || NULL == ctx->cipher_info )
375 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
376 else if( NULL == iv && iv_len != 0 )
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
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000379#if defined(MBEDTLS_USE_PSA_CRYPTO)
380 if( ctx->psa_enabled == 1 )
381 {
382 /* While PSA Crypto has an API for multipart
383 * operations, we currently don't make it
384 * accessible through the cipher layer. */
385 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
386 }
387#endif /* MBEDTLS_USE_PSA_CRYPTO */
388
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300389 if( NULL == iv && iv_len == 0 )
390 ctx->iv_size = 0;
391
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200392 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
394 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200397 actual_iv_size = iv_len;
398 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200399 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200400 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200401
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200402 /* avoid reading past the end of input buffer */
403 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200405 }
406
Daniel Kingbd920622016-05-15 19:56:20 -0300407#if defined(MBEDTLS_CHACHA20_C)
408 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
409 {
410 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
411 iv,
412 0U ) ) /* Initial counter value */
413 {
414 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
415 }
416 }
417#endif
418
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300419 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300420 {
421 memcpy( ctx->iv, iv, actual_iv_size );
422 ctx->iv_size = actual_iv_size;
423 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200424
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200425 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200429{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200430 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200432
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000433#if defined(MBEDTLS_USE_PSA_CRYPTO)
434 if( ctx->psa_enabled == 1 )
435 {
436 /* We don't support resetting PSA-based
437 * cipher contexts, yet. */
438 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
439 }
440#endif /* MBEDTLS_USE_PSA_CRYPTO */
441
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442 ctx->unprocessed_len = 0;
443
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200444 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200445}
446
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200447#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200449 const unsigned char *ad, size_t ad_len )
450{
451 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200453
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000454#if defined(MBEDTLS_USE_PSA_CRYPTO)
455 if( ctx->psa_enabled == 1 )
456 {
457 /* While PSA Crypto has an API for multipart
458 * operations, we currently don't make it
459 * accessible through the cipher layer. */
460 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
461 }
462#endif /* MBEDTLS_USE_PSA_CRYPTO */
463
Daniel King8fe47012016-05-17 20:33:28 -0300464#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200468 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200469 }
Daniel King8fe47012016-05-17 20:33:28 -0300470#endif
471
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200472#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300473 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
474 {
475 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200476 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300477
478 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200479 ? MBEDTLS_CHACHAPOLY_ENCRYPT
480 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300481
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200482 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300483 ctx->iv,
484 mode );
485 if ( result != 0 )
486 return( result );
487
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200488 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200489 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300490 }
491#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200492
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200493 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200495#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200498 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499{
Paul Bakkerff61a782011-06-09 15:42:02 +0000500 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100501 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Paul Bakker68884e32013-01-07 18:20:04 +0100503 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000506 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000508#if defined(MBEDTLS_USE_PSA_CRYPTO)
509 if( ctx->psa_enabled == 1 )
510 {
511 /* While PSA Crypto has an API for multipart
512 * operations, we currently don't make it
513 * accessible through the cipher layer. */
514 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
515 }
516#endif /* MBEDTLS_USE_PSA_CRYPTO */
517
Paul Bakker6c212762013-12-16 15:24:50 +0100518 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100519 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200522 {
Janos Follath98e28a72016-05-31 14:03:54 +0100523 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200525
526 *olen = ilen;
527
528 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
529 ctx->operation, input, output ) ) )
530 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200531 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200532 }
533
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200534 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200535 }
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_GCM_C)
538 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200539 {
540 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200542 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200543 }
544#endif
545
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200546#if defined(MBEDTLS_CHACHAPOLY_C)
547 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
548 {
549 *olen = ilen;
550 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
551 ilen, input, output );
552 }
553#endif
554
Janos Follath98e28a72016-05-31 14:03:54 +0100555 if ( 0 == block_size )
556 {
557 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
558 }
559
Paul Bakker68884e32013-01-07 18:20:04 +0100560 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100561 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100564 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_CIPHER_MODE_CBC)
567 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200569 size_t copy_len = 0;
570
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 /*
572 * If there is not enough data for a full block, cache it.
573 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700574 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000575 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700576 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
577 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000579 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 {
581 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
582 ilen );
583
584 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200585 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 }
587
588 /*
589 * Process cached data first
590 */
Janos Follath98e28a72016-05-31 14:03:54 +0100591 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 {
Janos Follath98e28a72016-05-31 14:03:54 +0100593 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
595 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
596 copy_len );
597
Paul Bakkerff61a782011-06-09 15:42:02 +0000598 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100599 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000600 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603 }
604
Janos Follath98e28a72016-05-31 14:03:54 +0100605 *olen += block_size;
606 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607 ctx->unprocessed_len = 0;
608
609 input += copy_len;
610 ilen -= copy_len;
611 }
612
613 /*
614 * Cache final, incomplete block
615 */
616 if( 0 != ilen )
617 {
Janos Follath98e28a72016-05-31 14:03:54 +0100618 if( 0 == block_size )
619 {
620 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
621 }
622
Andy Leiserson79e77892017-04-28 20:01:49 -0700623 /* Encryption: only cache partial blocks
624 * Decryption w/ padding: always keep at least one whole block
625 * Decryption w/o padding: only cache partial blocks
626 */
Janos Follath98e28a72016-05-31 14:03:54 +0100627 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700628 if( copy_len == 0 &&
629 ctx->operation == MBEDTLS_DECRYPT &&
630 NULL != ctx->add_padding)
631 {
Janos Follath98e28a72016-05-31 14:03:54 +0100632 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700633 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634
635 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
636 copy_len );
637
638 ctx->unprocessed_len += copy_len;
639 ilen -= copy_len;
640 }
641
642 /*
643 * Process remaining full blocks
644 */
645 if( ilen )
646 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000647 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
648 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200652
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 *olen += ilen;
654 }
655
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200656 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_CIPHER_MODE_CFB)
661 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000662 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000663 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000664 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000665 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000666 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200667 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000668 }
669
670 *olen = ilen;
671
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000675
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100676#if defined(MBEDTLS_CIPHER_MODE_OFB)
677 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
678 {
679 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
680 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
681 {
682 return( ret );
683 }
684
685 *olen = ilen;
686
687 return( 0 );
688 }
689#endif /* MBEDTLS_CIPHER_MODE_OFB */
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_MODE_CTR)
692 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000693 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000694 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000695 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000696 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000697 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200698 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000699 }
700
701 *olen = ilen;
702
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200703 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000704 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000706
Jaeden Ameroc6539902018-04-30 17:17:41 +0100707#if defined(MBEDTLS_CIPHER_MODE_XTS)
708 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
709 {
710 if( ctx->unprocessed_len > 0 ) {
711 /* We can only process an entire data unit at a time. */
712 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
713 }
714
715 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
716 ctx->operation, ilen, ctx->iv, input, output );
717 if( ret != 0 )
718 {
719 return( ret );
720 }
721
722 *olen = ilen;
723
724 return( 0 );
725 }
726#endif /* MBEDTLS_CIPHER_MODE_XTS */
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_MODE_STREAM)
729 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200730 {
731 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
732 ilen, input, output ) ) )
733 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200735 }
736
737 *olen = ilen;
738
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200739 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744}
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
747#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200748/*
749 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
750 */
Paul Bakker23986e52011-04-24 08:57:21 +0000751static void add_pkcs_padding( unsigned char *output, size_t output_len,
752 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753{
Paul Bakker23986e52011-04-24 08:57:21 +0000754 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100755 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000756
757 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000758 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000759}
760
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200761static int get_pkcs_padding( unsigned char *input, size_t input_len,
762 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100764 size_t i, pad_idx;
765 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766
Paul Bakkera885d682011-01-20 16:35:05 +0000767 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769
770 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771 *data_len = input_len - padding_len;
772
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100773 /* Avoid logical || since it results in a branch */
774 bad |= padding_len > input_len;
775 bad |= padding_len == 0;
776
777 /* The number of bytes checked must be independent of padding_len,
778 * so pick input_len, which is usually 8 or 16 (one block) */
779 pad_idx = input_len - padding_len;
780 for( i = 0; i < input_len; i++ )
781 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200788/*
789 * One and zeros padding: fill with 80 00 ... 00
790 */
791static void add_one_and_zeros_padding( unsigned char *output,
792 size_t output_len, size_t data_len )
793{
794 size_t padding_len = output_len - data_len;
795 unsigned char i = 0;
796
797 output[data_len] = 0x80;
798 for( i = 1; i < padding_len; i++ )
799 output[data_len + i] = 0x00;
800}
801
802static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
803 size_t *data_len )
804{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100805 size_t i;
806 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200807
808 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200810
Micha Krausba8316f2017-12-23 23:40:08 +0100811 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100812 *data_len = 0;
813 for( i = input_len; i > 0; i-- )
814 {
815 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100816 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100817 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100818 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100819 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200822
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200827/*
828 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
829 */
830static void add_zeros_and_len_padding( unsigned char *output,
831 size_t output_len, size_t data_len )
832{
833 size_t padding_len = output_len - data_len;
834 unsigned char i = 0;
835
836 for( i = 1; i < padding_len; i++ )
837 output[data_len + i - 1] = 0x00;
838 output[output_len - 1] = (unsigned char) padding_len;
839}
840
841static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
842 size_t *data_len )
843{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100844 size_t i, pad_idx;
845 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200846
847 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200849
850 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200851 *data_len = input_len - padding_len;
852
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100853 /* Avoid logical || since it results in a branch */
854 bad |= padding_len > input_len;
855 bad |= padding_len == 0;
856
857 /* The number of bytes checked must be independent of padding_len */
858 pad_idx = input_len - padding_len;
859 for( i = 0; i < input_len - 1; i++ )
860 bad |= input[i] * ( i >= pad_idx );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200867/*
868 * Zero padding: fill with 00 ... 00
869 */
870static void add_zeros_padding( unsigned char *output,
871 size_t output_len, size_t data_len )
872{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200873 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200874
875 for( i = data_len; i < output_len; i++ )
876 output[i] = 0x00;
877}
878
879static int get_zeros_padding( unsigned char *input, size_t input_len,
880 size_t *data_len )
881{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100882 size_t i;
883 unsigned char done = 0, prev_done;
884
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200885 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100888 *data_len = 0;
889 for( i = input_len; i > 0; i-- )
890 {
891 prev_done = done;
892 done |= ( input[i-1] != 0 );
893 *data_len |= i * ( done != prev_done );
894 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200895
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200896 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200899
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200900/*
901 * No padding: don't pad :)
902 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200904 * but a trivial get_padding function
905 */
906static int get_no_padding( unsigned char *input, size_t input_len,
907 size_t *data_len )
908{
909 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200911
912 *data_len = input_len;
913
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200914 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200919 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000920{
921 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000923
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000924#if defined(MBEDTLS_USE_PSA_CRYPTO)
925 if( ctx->psa_enabled == 1 )
926 {
927 /* While PSA Crypto has an API for multipart
928 * operations, we currently don't make it
929 * accessible through the cipher layer. */
930 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
931 }
932#endif /* MBEDTLS_USE_PSA_CRYPTO */
933
Paul Bakker8123e9d2011-01-06 15:37:30 +0000934 *olen = 0;
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100937 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
939 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100940 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000942 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200943 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000944 }
945
Daniel King8fe47012016-05-17 20:33:28 -0300946 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
947 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300948 {
949 return( 0 );
950 }
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200953 {
954 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200956
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200957 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200958 }
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_CIPHER_MODE_CBC)
961 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000962 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200963 int ret = 0;
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000966 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200967 /* check for 'no padding' mode */
968 if( NULL == ctx->add_padding )
969 {
970 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200972
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200973 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200974 }
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000977 ctx->unprocessed_len );
978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000980 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200981 /*
982 * For decrypt operations, expect a full block,
983 * or an empty block if no padding
984 */
985 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200986 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000989 }
990
991 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000992 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000994 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000995 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200996 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997 }
998
999 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 if( MBEDTLS_DECRYPT == ctx->operation )
1001 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001002 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001003
1004 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001006 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001007 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001008#else
1009 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001013}
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +00001016int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1017 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001018{
1019 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001023 }
1024
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001025#if defined(MBEDTLS_USE_PSA_CRYPTO)
1026 if( ctx->psa_enabled == 1 )
1027 {
1028 /* While PSA Crypto knows about CBC padding
1029 * schemes, we currently don't make them
1030 * accessible through the cipher layer. */
1031 if( mode != MBEDTLS_PADDING_NONE )
1032 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1033
1034 return( 0 );
1035 }
1036#endif /* MBEDTLS_USE_PSA_CRYPTO */
1037
Paul Bakker1a45d912013-08-14 12:04:26 +02001038 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1041 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001042 ctx->add_padding = add_pkcs_padding;
1043 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001044 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1047 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001048 ctx->add_padding = add_one_and_zeros_padding;
1049 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001050 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1053 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001054 ctx->add_padding = add_zeros_and_len_padding;
1055 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001056 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1059 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001060 ctx->add_padding = add_zeros_padding;
1061 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001062 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001065 ctx->add_padding = NULL;
1066 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001067 break;
1068
1069 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001071 }
1072
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001073 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001076
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001077#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001079 unsigned char *tag, size_t tag_len )
1080{
1081 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( MBEDTLS_ENCRYPT != ctx->operation )
1085 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001086
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001087#if defined(MBEDTLS_USE_PSA_CRYPTO)
1088 if( ctx->psa_enabled == 1 )
1089 {
1090 /* While PSA Crypto has an API for multipart
1091 * operations, we currently don't make it
1092 * accessible through the cipher layer. */
1093 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1094
1095 return( 0 );
1096 }
1097#endif /* MBEDTLS_USE_PSA_CRYPTO */
1098
Daniel King8fe47012016-05-17 20:33:28 -03001099#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001101 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1102 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001103#endif
1104
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001105#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001106 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1107 {
1108 /* Don't allow truncated MAC for Poly1305 */
1109 if ( tag_len != 16U )
1110 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1111
Hanno Becker18597cd2018-11-09 16:36:33 +00001112 return( mbedtls_chachapoly_finish(
1113 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001114 }
1115#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001116
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001117 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001118}
Paul Bakker9af723c2014-05-01 13:03:14 +02001119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001121 const unsigned char *tag, size_t tag_len )
1122{
Daniel King8fe47012016-05-17 20:33:28 -03001123 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001124 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001125
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001126 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001130 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001131
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001132#if defined(MBEDTLS_USE_PSA_CRYPTO)
1133 if( ctx->psa_enabled == 1 )
1134 {
1135 /* While PSA Crypto has an API for multipart
1136 * operations, we currently don't make it
1137 * accessible through the cipher layer. */
1138 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1139 }
1140#endif /* MBEDTLS_USE_PSA_CRYPTO */
1141
Daniel King8fe47012016-05-17 20:33:28 -03001142#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001144 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001145 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001147
Hanno Becker18597cd2018-11-09 16:36:33 +00001148 if( 0 != ( ret = mbedtls_gcm_finish(
1149 (mbedtls_gcm_context *) ctx->cipher_ctx,
1150 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001151 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001153 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001154
1155 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001156 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001158
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001159 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001160 }
Daniel King8fe47012016-05-17 20:33:28 -03001161#endif /* MBEDTLS_GCM_C */
1162
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001163#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001164 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1165 {
1166 /* Don't allow truncated MAC for Poly1305 */
1167 if ( tag_len != sizeof( check_tag ) )
1168 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1169
Hanno Becker18597cd2018-11-09 16:36:33 +00001170 ret = mbedtls_chachapoly_finish(
1171 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001172 if ( ret != 0 )
1173 {
1174 return( ret );
1175 }
1176
1177 /* Check the tag in "constant-time" */
1178 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1179 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1180
1181 return( 0 );
1182 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001183#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001184
1185 return( 0 );
1186}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001187#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001189/*
1190 * Packet-oriented wrapper for non-AEAD modes
1191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001193 const unsigned char *iv, size_t iv_len,
1194 const unsigned char *input, size_t ilen,
1195 unsigned char *output, size_t *olen )
1196{
1197 int ret;
1198 size_t finish_olen;
1199
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001200#if defined(MBEDTLS_USE_PSA_CRYPTO)
1201 if( ctx->psa_enabled == 1 )
1202 {
Hanno Becker55e2e3d2018-11-12 12:36:17 +00001203 /* As in the non-PSA case, we don't check that
1204 * a key has been set. If not, the key slot will
1205 * still be in its default state of 0, which is
1206 * guaranteed to be invalid, hence the PSA-call
1207 * below will gracefully fail. */
1208 mbedtls_cipher_context_psa * const cipher_psa =
1209 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1210
1211 psa_status_t status;
1212 psa_cipher_operation_t cipher_op;
1213 size_t part_len;
1214
1215 if( ctx->operation == MBEDTLS_DECRYPT )
1216 {
1217 status = psa_cipher_decrypt_setup( &cipher_op,
1218 cipher_psa->slot,
1219 cipher_psa->alg );
1220 }
1221 else if( ctx->operation == MBEDTLS_ENCRYPT )
1222 {
1223 status = psa_cipher_encrypt_setup( &cipher_op,
1224 cipher_psa->slot,
1225 cipher_psa->alg );
1226 }
1227 else
1228 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1229
1230 /* In the following, we can immediately return on an error,
1231 * because the PSA Crypto API guarantees that cipher operations
1232 * are terminated by unsuccessful calls to psa_cipher_update(),
1233 * and by any call to psa_cipher_finish(). */
1234 if( status != PSA_SUCCESS )
1235 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1236
1237 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1238 if( status != PSA_SUCCESS )
1239 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1240
1241 status = psa_cipher_update( &cipher_op,
1242 input, ilen,
1243 output, ilen, olen );
1244 if( status != PSA_SUCCESS )
1245 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1246
1247 status = psa_cipher_finish( &cipher_op,
1248 output + *olen, ilen - *olen,
1249 &part_len );
1250 if( status != PSA_SUCCESS )
1251 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1252
1253 *olen += part_len;
1254 return( 0 );
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001255 }
1256#endif /* MBEDTLS_USE_PSA_CRYPTO */
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001259 return( ret );
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001262 return( ret );
1263
Hanno Becker18597cd2018-11-09 16:36:33 +00001264 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1265 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001266 return( ret );
1267
Hanno Becker18597cd2018-11-09 16:36:33 +00001268 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1269 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001270 return( ret );
1271
1272 *olen += finish_olen;
1273
1274 return( 0 );
1275}
1276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001278/*
1279 * Packet-oriented encryption for AEAD modes
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001282 const unsigned char *iv, size_t iv_len,
1283 const unsigned char *ad, size_t ad_len,
1284 const unsigned char *input, size_t ilen,
1285 unsigned char *output, size_t *olen,
1286 unsigned char *tag, size_t tag_len )
1287{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001288#if defined(MBEDTLS_USE_PSA_CRYPTO)
1289 if( ctx->psa_enabled == 1 )
1290 {
1291 /* TODO */
1292 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1293 }
1294#endif /* MBEDTLS_USE_PSA_CRYPTO */
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_GCM_C)
1297 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001298 {
1299 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001300 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1301 ilen, iv, iv_len, ad, ad_len,
1302 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#endif /* MBEDTLS_GCM_C */
1305#if defined(MBEDTLS_CCM_C)
1306 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001307 {
1308 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001310 iv, iv_len, ad, ad_len, input, output,
1311 tag, tag_len ) );
1312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001314#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001315 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1316 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001317 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001318 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001319 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001320 {
1321 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1322 }
1323
1324 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001325 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001326 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001327 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001328#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001331}
1332
1333/*
1334 * Packet-oriented decryption for AEAD modes
1335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001337 const unsigned char *iv, size_t iv_len,
1338 const unsigned char *ad, size_t ad_len,
1339 const unsigned char *input, size_t ilen,
1340 unsigned char *output, size_t *olen,
1341 const unsigned char *tag, size_t tag_len )
1342{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001343#if defined(MBEDTLS_USE_PSA_CRYPTO)
1344 if( ctx->psa_enabled == 1 )
1345 {
1346 /* TODO */
1347 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1348 }
1349#endif /* MBEDTLS_USE_PSA_CRYPTO */
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_GCM_C)
1352 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001353 {
1354 int ret;
1355
1356 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001358 iv, iv_len, ad, ad_len,
1359 tag, tag_len, input, output );
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1362 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001363
1364 return( ret );
1365 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_GCM_C */
1367#if defined(MBEDTLS_CCM_C)
1368 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001369 {
1370 int ret;
1371
1372 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001374 iv, iv_len, ad, ad_len,
1375 input, output, tag, tag_len );
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1378 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001379
1380 return( ret );
1381 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001383#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001384 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1385 {
Daniel King8fe47012016-05-17 20:33:28 -03001386 int ret;
1387
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001388 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001389 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001390 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001391 {
1392 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1393 }
1394
1395 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001396 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1397 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001398
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001399 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1400 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001401
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001402 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001403 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001404#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_CIPHER_C */