blob: 0b7c887f03903a75df595b7bf387b64a80600b85 [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
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 Becker21967c52018-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 Becker21967c52018-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 Becker21967c52018-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 Becker21967c52018-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 Becker1cb36532018-11-09 16:20:29 +0000170#if defined(MBEDTLS_USE_PSA_CRYPTO)
171 if( ctx->psa_enabled == 1 )
172 {
Hanno Becker73f59fc2018-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 Beckerd9ca5cf2018-11-12 11:59:30 +0000180 /* xxx_free() doesn't allow to return failures. */
181 (void) psa_destroy_key( cipher_psa->slot );
Hanno Becker73f59fc2018-11-09 16:47:20 +0000182 }
183
184 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
185 mbedtls_free( cipher_psa );
186 }
Hanno Becker1cb36532018-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 Becker21967c52018-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 Becker098c9de2018-11-09 16:10:57 +0000235#if defined(MBEDTLS_USE_PSA_CRYPTO)
236int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
Hanno Beckerf1336402018-11-12 16:26:27 +0000237 const mbedtls_cipher_info_t *cipher_info,
238 size_t taglen )
Hanno Becker098c9de2018-11-09 16:10:57 +0000239{
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000240 psa_algorithm_t alg;
241 mbedtls_cipher_context_psa *cipher_psa;
242
Hanno Becker1cb36532018-11-09 16:20:29 +0000243 if( NULL == cipher_info || NULL == ctx )
244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
245
Hanno Beckerf1336402018-11-12 16:26:27 +0000246 alg = mbedtls_psa_translate_cipher_mode( cipher_info->mode, taglen );
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000247 if( alg == 0)
248 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Hanno Becker73f59fc2018-11-09 16:47:20 +0000249
Hanno Becker1cb36532018-11-09 16:20:29 +0000250 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
251
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000252 cipher_psa = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
253 if( cipher_psa == NULL )
254 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
255 cipher_psa->alg = alg;
256 ctx->cipher_ctx = cipher_psa;
Hanno Becker1cb36532018-11-09 16:20:29 +0000257 ctx->cipher_info = cipher_info;
258 ctx->psa_enabled = 1;
259 return( 0 );
Hanno Becker098c9de2018-11-09 16:10:57 +0000260}
261#endif /* MBEDTLS_USE_PSA_CRYPTO */
262
Hanno Becker21967c52018-11-09 16:36:33 +0000263int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
264 const unsigned char *key,
265 int key_bitlen,
266 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267{
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000268 if( NULL == ctx || NULL == ctx->cipher_info ||
269 NULL == ctx->cipher_ctx )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000272 }
273
274 if( operation != MBEDTLS_DECRYPT &&
275 operation != MBEDTLS_ENCRYPT )
276 {
277 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
278 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279
Hanno Becker1cb36532018-11-09 16:20:29 +0000280#if defined(MBEDTLS_USE_PSA_CRYPTO)
281 if( ctx->psa_enabled == 1 )
282 {
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000283 mbedtls_cipher_context_psa * const cipher_psa =
284 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
285
286 size_t const key_bytelen = ( (size_t) key_bitlen + 7 ) / 8;
287
288 psa_status_t status;
289 psa_key_type_t key_type;
290 psa_key_usage_t key_usage;
291 psa_key_policy_t key_policy;
292
293 /* PSA Crypto API only accepts byte-aligned keys. */
294 if( key_bitlen % 8 != 0 )
295 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
296
297 /* Don't allow keys to be set multiple times. */
298 if( cipher_psa->slot_state != 0 )
299 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
300
301 /* Find a fresh key slot to use. */
302 status = mbedtls_psa_get_free_key_slot( &cipher_psa->slot );
303 if( status != PSA_SUCCESS )
304 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
305 cipher_psa->slot_state = 1; /* Indicate that we own the key slot. */
306
307 /* From that point on, the responsibility for destroying the
308 * key slot is on mbedtls_cipher_free(). This includes the case
309 * where the policy setup or key import below fail, as
310 * mbedtls_cipher_free() needs to be called in any case. */
311
312 /* Setup policy for the new key slot. */
313 psa_key_policy_init( &key_policy );
Hanno Becker884f6af2018-11-12 13:33:16 +0000314
315 /* Mbed TLS' cipher layer doesn't enforce the mode of operation
316 * (encrypt vs. decrypt): it is possible to setup a key for encryption
317 * and use it for AEAD decryption. Until tests relying on this
318 * are changed, allow any usage in PSA. */
319 /* key_usage = mbedtls_psa_translate_cipher_operation( operation ); */
320 key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000321 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
322 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
323 if( status != PSA_SUCCESS )
324 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
325
326 /* Populate new key slot. */
327 key_type = mbedtls_psa_translate_cipher_type(
328 ctx->cipher_info->type );
329 if( key_type == 0 )
330 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
331 status = psa_import_key( cipher_psa->slot,
332 key_type, key, key_bytelen );
333 if( status != PSA_SUCCESS )
334 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
335
336 ctx->key_bitlen = key_bitlen;
337 ctx->operation = operation;
338 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +0000339 }
340#endif /* MBEDTLS_USE_PSA_CRYPTO */
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200343 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200346 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200347
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200348 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349 ctx->operation = operation;
350
Paul Bakker343a8702011-06-09 14:27:58 +0000351 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100352 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( MBEDTLS_ENCRYPT == operation ||
355 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100356 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000358 {
359 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000360 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000361 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000364 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000365 ctx->key_bitlen );
366
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369}
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200372 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200374 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300375 if( NULL == ctx || NULL == ctx->cipher_info )
376 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
377 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379
Hanno Becker1cb36532018-11-09 16:20:29 +0000380#if defined(MBEDTLS_USE_PSA_CRYPTO)
381 if( ctx->psa_enabled == 1 )
382 {
383 /* While PSA Crypto has an API for multipart
384 * operations, we currently don't make it
385 * accessible through the cipher layer. */
386 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
387 }
388#endif /* MBEDTLS_USE_PSA_CRYPTO */
389
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300390 if( NULL == iv && iv_len == 0 )
391 ctx->iv_size = 0;
392
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200393 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
395 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200398 actual_iv_size = iv_len;
399 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200400 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200401 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200402
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200403 /* avoid reading past the end of input buffer */
404 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200406 }
407
Daniel Kingbd920622016-05-15 19:56:20 -0300408#if defined(MBEDTLS_CHACHA20_C)
409 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
410 {
411 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
412 iv,
413 0U ) ) /* Initial counter value */
414 {
415 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
416 }
417 }
418#endif
419
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300420 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300421 {
422 memcpy( ctx->iv, iv, actual_iv_size );
423 ctx->iv_size = actual_iv_size;
424 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200425
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200430{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200431 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200433
Hanno Becker1cb36532018-11-09 16:20:29 +0000434#if defined(MBEDTLS_USE_PSA_CRYPTO)
435 if( ctx->psa_enabled == 1 )
436 {
437 /* We don't support resetting PSA-based
438 * cipher contexts, yet. */
439 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
440 }
441#endif /* MBEDTLS_USE_PSA_CRYPTO */
442
Paul Bakker8123e9d2011-01-06 15:37:30 +0000443 ctx->unprocessed_len = 0;
444
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200445 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200446}
447
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200448#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200450 const unsigned char *ad, size_t ad_len )
451{
452 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200454
Hanno Becker1cb36532018-11-09 16:20:29 +0000455#if defined(MBEDTLS_USE_PSA_CRYPTO)
456 if( ctx->psa_enabled == 1 )
457 {
458 /* While PSA Crypto has an API for multipart
459 * operations, we currently don't make it
460 * accessible through the cipher layer. */
461 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
462 }
463#endif /* MBEDTLS_USE_PSA_CRYPTO */
464
Daniel King8fe47012016-05-17 20:33:28 -0300465#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200469 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200470 }
Daniel King8fe47012016-05-17 20:33:28 -0300471#endif
472
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200473#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300474 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
475 {
476 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200477 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300478
479 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200480 ? MBEDTLS_CHACHAPOLY_ENCRYPT
481 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300482
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200483 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300484 ctx->iv,
485 mode );
486 if ( result != 0 )
487 return( result );
488
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200489 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200490 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300491 }
492#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200493
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200496#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200499 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500{
Paul Bakkerff61a782011-06-09 15:42:02 +0000501 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100502 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503
Paul Bakker68884e32013-01-07 18:20:04 +0100504 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000507 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508
Hanno Becker1cb36532018-11-09 16:20:29 +0000509#if defined(MBEDTLS_USE_PSA_CRYPTO)
510 if( ctx->psa_enabled == 1 )
511 {
512 /* While PSA Crypto has an API for multipart
513 * operations, we currently don't make it
514 * accessible through the cipher layer. */
515 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
516 }
517#endif /* MBEDTLS_USE_PSA_CRYPTO */
518
Paul Bakker6c212762013-12-16 15:24:50 +0100519 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100520 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200523 {
Janos Follath98e28a72016-05-31 14:03:54 +0100524 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200526
527 *olen = ilen;
528
529 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
530 ctx->operation, input, output ) ) )
531 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200532 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200533 }
534
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_GCM_C)
539 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200540 {
541 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200543 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200544 }
545#endif
546
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200547#if defined(MBEDTLS_CHACHAPOLY_C)
548 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
549 {
550 *olen = ilen;
551 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
552 ilen, input, output );
553 }
554#endif
555
Janos Follath98e28a72016-05-31 14:03:54 +0100556 if ( 0 == block_size )
557 {
558 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
559 }
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100562 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100565 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_CIPHER_MODE_CBC)
568 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200570 size_t copy_len = 0;
571
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 /*
573 * If there is not enough data for a full block, cache it.
574 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700575 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000576 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700577 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
578 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000580 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581 {
582 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
583 ilen );
584
585 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200586 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587 }
588
589 /*
590 * Process cached data first
591 */
Janos Follath98e28a72016-05-31 14:03:54 +0100592 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593 {
Janos Follath98e28a72016-05-31 14:03:54 +0100594 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
596 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
597 copy_len );
598
Paul Bakkerff61a782011-06-09 15:42:02 +0000599 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100600 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000601 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200603 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604 }
605
Janos Follath98e28a72016-05-31 14:03:54 +0100606 *olen += block_size;
607 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608 ctx->unprocessed_len = 0;
609
610 input += copy_len;
611 ilen -= copy_len;
612 }
613
614 /*
615 * Cache final, incomplete block
616 */
617 if( 0 != ilen )
618 {
Janos Follath98e28a72016-05-31 14:03:54 +0100619 if( 0 == block_size )
620 {
621 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
622 }
623
Andy Leiserson79e77892017-04-28 20:01:49 -0700624 /* Encryption: only cache partial blocks
625 * Decryption w/ padding: always keep at least one whole block
626 * Decryption w/o padding: only cache partial blocks
627 */
Janos Follath98e28a72016-05-31 14:03:54 +0100628 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700629 if( copy_len == 0 &&
630 ctx->operation == MBEDTLS_DECRYPT &&
631 NULL != ctx->add_padding)
632 {
Janos Follath98e28a72016-05-31 14:03:54 +0100633 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700634 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635
636 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
637 copy_len );
638
639 ctx->unprocessed_len += copy_len;
640 ilen -= copy_len;
641 }
642
643 /*
644 * Process remaining full blocks
645 */
646 if( ilen )
647 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000648 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
649 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200653
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 *olen += ilen;
655 }
656
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200657 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_CIPHER_MODE_CFB)
662 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000663 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000664 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000665 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000666 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000667 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200668 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000669 }
670
671 *olen = ilen;
672
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000676
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100677#if defined(MBEDTLS_CIPHER_MODE_OFB)
678 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
679 {
680 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
681 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
682 {
683 return( ret );
684 }
685
686 *olen = ilen;
687
688 return( 0 );
689 }
690#endif /* MBEDTLS_CIPHER_MODE_OFB */
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_CIPHER_MODE_CTR)
693 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000694 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000695 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000696 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000697 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000698 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200699 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000700 }
701
702 *olen = ilen;
703
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200704 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000707
Jaeden Ameroc6539902018-04-30 17:17:41 +0100708#if defined(MBEDTLS_CIPHER_MODE_XTS)
709 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
710 {
711 if( ctx->unprocessed_len > 0 ) {
712 /* We can only process an entire data unit at a time. */
713 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
714 }
715
716 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
717 ctx->operation, ilen, ctx->iv, input, output );
718 if( ret != 0 )
719 {
720 return( ret );
721 }
722
723 *olen = ilen;
724
725 return( 0 );
726 }
727#endif /* MBEDTLS_CIPHER_MODE_XTS */
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_CIPHER_MODE_STREAM)
730 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200731 {
732 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
733 ilen, input, output ) ) )
734 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200736 }
737
738 *olen = ilen;
739
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200740 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000745}
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
748#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200749/*
750 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
751 */
Paul Bakker23986e52011-04-24 08:57:21 +0000752static void add_pkcs_padding( unsigned char *output, size_t output_len,
753 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754{
Paul Bakker23986e52011-04-24 08:57:21 +0000755 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100756 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757
758 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000759 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760}
761
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200762static int get_pkcs_padding( unsigned char *input, size_t input_len,
763 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100765 size_t i, pad_idx;
766 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767
Paul Bakkera885d682011-01-20 16:35:05 +0000768 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
771 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772 *data_len = input_len - padding_len;
773
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100774 /* Avoid logical || since it results in a branch */
775 bad |= padding_len > input_len;
776 bad |= padding_len == 0;
777
778 /* The number of bytes checked must be independent of padding_len,
779 * so pick input_len, which is usually 8 or 16 (one block) */
780 pad_idx = input_len - padding_len;
781 for( i = 0; i < input_len; i++ )
782 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200789/*
790 * One and zeros padding: fill with 80 00 ... 00
791 */
792static void add_one_and_zeros_padding( unsigned char *output,
793 size_t output_len, size_t data_len )
794{
795 size_t padding_len = output_len - data_len;
796 unsigned char i = 0;
797
798 output[data_len] = 0x80;
799 for( i = 1; i < padding_len; i++ )
800 output[data_len + i] = 0x00;
801}
802
803static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
804 size_t *data_len )
805{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100806 size_t i;
807 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200808
809 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200811
Micha Krausba8316f2017-12-23 23:40:08 +0100812 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100813 *data_len = 0;
814 for( i = input_len; i > 0; i-- )
815 {
816 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100817 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100818 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100819 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100820 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200823
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200828/*
829 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
830 */
831static void add_zeros_and_len_padding( unsigned char *output,
832 size_t output_len, size_t data_len )
833{
834 size_t padding_len = output_len - data_len;
835 unsigned char i = 0;
836
837 for( i = 1; i < padding_len; i++ )
838 output[data_len + i - 1] = 0x00;
839 output[output_len - 1] = (unsigned char) padding_len;
840}
841
842static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
843 size_t *data_len )
844{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100845 size_t i, pad_idx;
846 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200847
848 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200850
851 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200852 *data_len = input_len - padding_len;
853
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100854 /* Avoid logical || since it results in a branch */
855 bad |= padding_len > input_len;
856 bad |= padding_len == 0;
857
858 /* The number of bytes checked must be independent of padding_len */
859 pad_idx = input_len - padding_len;
860 for( i = 0; i < input_len - 1; i++ )
861 bad |= input[i] * ( i >= pad_idx );
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200868/*
869 * Zero padding: fill with 00 ... 00
870 */
871static void add_zeros_padding( unsigned char *output,
872 size_t output_len, size_t data_len )
873{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200874 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200875
876 for( i = data_len; i < output_len; i++ )
877 output[i] = 0x00;
878}
879
880static int get_zeros_padding( unsigned char *input, size_t input_len,
881 size_t *data_len )
882{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100883 size_t i;
884 unsigned char done = 0, prev_done;
885
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200886 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200888
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100889 *data_len = 0;
890 for( i = input_len; i > 0; i-- )
891 {
892 prev_done = done;
893 done |= ( input[i-1] != 0 );
894 *data_len |= i * ( done != prev_done );
895 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200896
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200897 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200900
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200901/*
902 * No padding: don't pad :)
903 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200905 * but a trivial get_padding function
906 */
907static int get_no_padding( unsigned char *input, size_t input_len,
908 size_t *data_len )
909{
910 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200912
913 *data_len = input_len;
914
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200915 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200920 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000921{
922 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000924
Hanno Becker1cb36532018-11-09 16:20:29 +0000925#if defined(MBEDTLS_USE_PSA_CRYPTO)
926 if( ctx->psa_enabled == 1 )
927 {
928 /* While PSA Crypto has an API for multipart
929 * operations, we currently don't make it
930 * accessible through the cipher layer. */
931 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
932 }
933#endif /* MBEDTLS_USE_PSA_CRYPTO */
934
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935 *olen = 0;
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100938 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
940 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100941 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000943 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200944 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000945 }
946
Daniel King8fe47012016-05-17 20:33:28 -0300947 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
948 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300949 {
950 return( 0 );
951 }
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200954 {
955 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200957
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200958 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200959 }
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_CIPHER_MODE_CBC)
962 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000963 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200964 int ret = 0;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000967 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200968 /* check for 'no padding' mode */
969 if( NULL == ctx->add_padding )
970 {
971 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200973
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200974 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975 }
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000978 ctx->unprocessed_len );
979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000981 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200982 /*
983 * For decrypt operations, expect a full block,
984 * or an empty block if no padding
985 */
986 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000990 }
991
992 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000993 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000995 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000996 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200997 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000998 }
999
1000 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 if( MBEDTLS_DECRYPT == ctx->operation )
1002 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001003 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001004
1005 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001007 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001008 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001009#else
1010 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001014}
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker21967c52018-11-09 16:36:33 +00001017int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1018 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001019{
1020 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001024 }
1025
Hanno Becker1cb36532018-11-09 16:20:29 +00001026#if defined(MBEDTLS_USE_PSA_CRYPTO)
1027 if( ctx->psa_enabled == 1 )
1028 {
1029 /* While PSA Crypto knows about CBC padding
1030 * schemes, we currently don't make them
1031 * accessible through the cipher layer. */
1032 if( mode != MBEDTLS_PADDING_NONE )
1033 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1034
1035 return( 0 );
1036 }
1037#endif /* MBEDTLS_USE_PSA_CRYPTO */
1038
Paul Bakker1a45d912013-08-14 12:04:26 +02001039 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1042 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001043 ctx->add_padding = add_pkcs_padding;
1044 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001045 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1048 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001049 ctx->add_padding = add_one_and_zeros_padding;
1050 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001051 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
1054 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001055 ctx->add_padding = add_zeros_and_len_padding;
1056 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001057 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
1060 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001061 ctx->add_padding = add_zeros_padding;
1062 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001063 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001066 ctx->add_padding = NULL;
1067 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001068 break;
1069
1070 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001072 }
1073
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001074 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001077
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001078#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001080 unsigned char *tag, size_t tag_len )
1081{
1082 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 if( MBEDTLS_ENCRYPT != ctx->operation )
1086 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001087
Hanno Becker1cb36532018-11-09 16:20:29 +00001088#if defined(MBEDTLS_USE_PSA_CRYPTO)
1089 if( ctx->psa_enabled == 1 )
1090 {
1091 /* While PSA Crypto has an API for multipart
1092 * operations, we currently don't make it
1093 * accessible through the cipher layer. */
1094 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1095
1096 return( 0 );
1097 }
1098#endif /* MBEDTLS_USE_PSA_CRYPTO */
1099
Daniel King8fe47012016-05-17 20:33:28 -03001100#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker21967c52018-11-09 16:36:33 +00001102 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1103 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001104#endif
1105
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001106#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001107 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1108 {
1109 /* Don't allow truncated MAC for Poly1305 */
1110 if ( tag_len != 16U )
1111 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1112
Hanno Becker21967c52018-11-09 16:36:33 +00001113 return( mbedtls_chachapoly_finish(
1114 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001115 }
1116#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001117
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001118 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001119}
Paul Bakker9af723c2014-05-01 13:03:14 +02001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001122 const unsigned char *tag, size_t tag_len )
1123{
Daniel King8fe47012016-05-17 20:33:28 -03001124 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001125 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001126
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001127 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001131 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001132
Hanno Becker1cb36532018-11-09 16:20:29 +00001133#if defined(MBEDTLS_USE_PSA_CRYPTO)
1134 if( ctx->psa_enabled == 1 )
1135 {
1136 /* While PSA Crypto has an API for multipart
1137 * operations, we currently don't make it
1138 * accessible through the cipher layer. */
1139 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1140 }
1141#endif /* MBEDTLS_USE_PSA_CRYPTO */
1142
Daniel King8fe47012016-05-17 20:33:28 -03001143#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001145 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001148
Hanno Becker21967c52018-11-09 16:36:33 +00001149 if( 0 != ( ret = mbedtls_gcm_finish(
1150 (mbedtls_gcm_context *) ctx->cipher_ctx,
1151 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001152 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001153 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001154 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001155
1156 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001157 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001159
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001160 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001161 }
Daniel King8fe47012016-05-17 20:33:28 -03001162#endif /* MBEDTLS_GCM_C */
1163
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001164#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001165 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1166 {
1167 /* Don't allow truncated MAC for Poly1305 */
1168 if ( tag_len != sizeof( check_tag ) )
1169 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1170
Hanno Becker21967c52018-11-09 16:36:33 +00001171 ret = mbedtls_chachapoly_finish(
1172 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001173 if ( ret != 0 )
1174 {
1175 return( ret );
1176 }
1177
1178 /* Check the tag in "constant-time" */
1179 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1180 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1181
1182 return( 0 );
1183 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001184#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185
1186 return( 0 );
1187}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001188#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001189
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001190/*
1191 * Packet-oriented wrapper for non-AEAD modes
1192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001194 const unsigned char *iv, size_t iv_len,
1195 const unsigned char *input, size_t ilen,
1196 unsigned char *output, size_t *olen )
1197{
1198 int ret;
1199 size_t finish_olen;
1200
Hanno Becker1cb36532018-11-09 16:20:29 +00001201#if defined(MBEDTLS_USE_PSA_CRYPTO)
1202 if( ctx->psa_enabled == 1 )
1203 {
Hanno Becker3c852a92018-11-12 12:36:17 +00001204 /* As in the non-PSA case, we don't check that
1205 * a key has been set. If not, the key slot will
1206 * still be in its default state of 0, which is
1207 * guaranteed to be invalid, hence the PSA-call
1208 * below will gracefully fail. */
1209 mbedtls_cipher_context_psa * const cipher_psa =
1210 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1211
1212 psa_status_t status;
1213 psa_cipher_operation_t cipher_op;
1214 size_t part_len;
1215
1216 if( ctx->operation == MBEDTLS_DECRYPT )
1217 {
1218 status = psa_cipher_decrypt_setup( &cipher_op,
1219 cipher_psa->slot,
1220 cipher_psa->alg );
1221 }
1222 else if( ctx->operation == MBEDTLS_ENCRYPT )
1223 {
1224 status = psa_cipher_encrypt_setup( &cipher_op,
1225 cipher_psa->slot,
1226 cipher_psa->alg );
1227 }
1228 else
1229 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1230
1231 /* In the following, we can immediately return on an error,
1232 * because the PSA Crypto API guarantees that cipher operations
1233 * are terminated by unsuccessful calls to psa_cipher_update(),
1234 * and by any call to psa_cipher_finish(). */
1235 if( status != PSA_SUCCESS )
1236 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1237
1238 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1239 if( status != PSA_SUCCESS )
1240 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1241
1242 status = psa_cipher_update( &cipher_op,
1243 input, ilen,
1244 output, ilen, olen );
1245 if( status != PSA_SUCCESS )
1246 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1247
1248 status = psa_cipher_finish( &cipher_op,
1249 output + *olen, ilen - *olen,
1250 &part_len );
1251 if( status != PSA_SUCCESS )
1252 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1253
1254 *olen += part_len;
1255 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +00001256 }
1257#endif /* MBEDTLS_USE_PSA_CRYPTO */
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001260 return( ret );
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001263 return( ret );
1264
Hanno Becker21967c52018-11-09 16:36:33 +00001265 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1266 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001267 return( ret );
1268
Hanno Becker21967c52018-11-09 16:36:33 +00001269 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1270 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001271 return( ret );
1272
1273 *olen += finish_olen;
1274
1275 return( 0 );
1276}
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001279/*
1280 * Packet-oriented encryption for AEAD modes
1281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001283 const unsigned char *iv, size_t iv_len,
1284 const unsigned char *ad, size_t ad_len,
1285 const unsigned char *input, size_t ilen,
1286 unsigned char *output, size_t *olen,
1287 unsigned char *tag, size_t tag_len )
1288{
Hanno Becker1cb36532018-11-09 16:20:29 +00001289#if defined(MBEDTLS_USE_PSA_CRYPTO)
1290 if( ctx->psa_enabled == 1 )
1291 {
1292 /* TODO */
1293 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1294 }
1295#endif /* MBEDTLS_USE_PSA_CRYPTO */
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#if defined(MBEDTLS_GCM_C)
1298 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001299 {
1300 *olen = ilen;
Hanno Becker21967c52018-11-09 16:36:33 +00001301 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1302 ilen, iv, iv_len, ad, ad_len,
1303 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_GCM_C */
1306#if defined(MBEDTLS_CCM_C)
1307 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001308 {
1309 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001311 iv, iv_len, ad, ad_len, input, output,
1312 tag, tag_len ) );
1313 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001315#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001316 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1317 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001318 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001319 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001320 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001321 {
1322 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1323 }
1324
1325 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001326 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001327 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001328 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001329#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001332}
1333
1334/*
1335 * Packet-oriented decryption for AEAD modes
1336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001338 const unsigned char *iv, size_t iv_len,
1339 const unsigned char *ad, size_t ad_len,
1340 const unsigned char *input, size_t ilen,
1341 unsigned char *output, size_t *olen,
1342 const unsigned char *tag, size_t tag_len )
1343{
Hanno Becker1cb36532018-11-09 16:20:29 +00001344#if defined(MBEDTLS_USE_PSA_CRYPTO)
1345 if( ctx->psa_enabled == 1 )
1346 {
1347 /* TODO */
1348 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1349 }
1350#endif /* MBEDTLS_USE_PSA_CRYPTO */
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_GCM_C)
1353 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001354 {
1355 int ret;
1356
1357 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001359 iv, iv_len, ad, ad_len,
1360 tag, tag_len, input, output );
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1363 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001364
1365 return( ret );
1366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_GCM_C */
1368#if defined(MBEDTLS_CCM_C)
1369 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001370 {
1371 int ret;
1372
1373 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001375 iv, iv_len, ad, ad_len,
1376 input, output, tag, tag_len );
1377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1379 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001380
1381 return( ret );
1382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001384#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001385 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1386 {
Daniel King8fe47012016-05-17 20:33:28 -03001387 int ret;
1388
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001389 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001390 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001391 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001392 {
1393 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1394 }
1395
1396 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001397 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1398 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001399
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001400 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1401 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001402
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001403 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001404 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001405#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_CIPHER_C */