blob: a83d3c6a6ed13e6c1d3d274530787009dae11ef4 [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,
237 const mbedtls_cipher_info_t *cipher_info )
238{
Hanno Beckerd9ca5cf2018-11-12 11:59:30 +0000239 psa_algorithm_t alg;
240 mbedtls_cipher_context_psa *cipher_psa;
241
Hanno Becker1cb36532018-11-09 16:20:29 +0000242 if( NULL == cipher_info || NULL == ctx )
243 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
244
Hanno Beckerd9ca5cf2018-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 Becker73f59fc2018-11-09 16:47:20 +0000248
Hanno Becker1cb36532018-11-09 16:20:29 +0000249 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
250
Hanno Beckerd9ca5cf2018-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 Becker1cb36532018-11-09 16:20:29 +0000256 ctx->cipher_info = cipher_info;
257 ctx->psa_enabled = 1;
258 return( 0 );
Hanno Becker098c9de2018-11-09 16:10:57 +0000259}
260#endif /* MBEDTLS_USE_PSA_CRYPTO */
261
Hanno Becker21967c52018-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 Beckerd9ca5cf2018-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 Beckerd9ca5cf2018-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 Becker1cb36532018-11-09 16:20:29 +0000279#if defined(MBEDTLS_USE_PSA_CRYPTO)
280 if( ctx->psa_enabled == 1 )
281 {
Hanno Beckerd9ca5cf2018-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 );
313 key_usage = mbedtls_psa_translate_cipher_operation( operation );
314 psa_key_policy_set_usage( &key_policy, key_usage, cipher_psa->alg );
315 status = psa_set_key_policy( cipher_psa->slot, &key_policy );
316 if( status != PSA_SUCCESS )
317 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
318
319 /* Populate new key slot. */
320 key_type = mbedtls_psa_translate_cipher_type(
321 ctx->cipher_info->type );
322 if( key_type == 0 )
323 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
324 status = psa_import_key( cipher_psa->slot,
325 key_type, key, key_bytelen );
326 if( status != PSA_SUCCESS )
327 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
328
329 ctx->key_bitlen = key_bitlen;
330 ctx->operation = operation;
331 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +0000332 }
333#endif /* MBEDTLS_USE_PSA_CRYPTO */
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200336 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200339 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200340
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200341 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342 ctx->operation = operation;
343
Paul Bakker343a8702011-06-09 14:27:58 +0000344 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100345 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( MBEDTLS_ENCRYPT == operation ||
348 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100349 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000351 {
352 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000353 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000354 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000357 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000358 ctx->key_bitlen );
359
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200365 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200367 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300368 if( NULL == ctx || NULL == ctx->cipher_info )
369 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
370 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372
Hanno Becker1cb36532018-11-09 16:20:29 +0000373#if defined(MBEDTLS_USE_PSA_CRYPTO)
374 if( ctx->psa_enabled == 1 )
375 {
376 /* While PSA Crypto has an API for multipart
377 * operations, we currently don't make it
378 * accessible through the cipher layer. */
379 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
380 }
381#endif /* MBEDTLS_USE_PSA_CRYPTO */
382
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300383 if( NULL == iv && iv_len == 0 )
384 ctx->iv_size = 0;
385
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200386 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
388 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200391 actual_iv_size = iv_len;
392 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200393 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200394 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200395
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200396 /* avoid reading past the end of input buffer */
397 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200399 }
400
Daniel Kingbd920622016-05-15 19:56:20 -0300401#if defined(MBEDTLS_CHACHA20_C)
402 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
403 {
404 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
405 iv,
406 0U ) ) /* Initial counter value */
407 {
408 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
409 }
410 }
411#endif
412
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300413 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300414 {
415 memcpy( ctx->iv, iv, actual_iv_size );
416 ctx->iv_size = actual_iv_size;
417 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200418
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200419 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200420}
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200423{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200424 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200426
Hanno Becker1cb36532018-11-09 16:20:29 +0000427#if defined(MBEDTLS_USE_PSA_CRYPTO)
428 if( ctx->psa_enabled == 1 )
429 {
430 /* We don't support resetting PSA-based
431 * cipher contexts, yet. */
432 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
433 }
434#endif /* MBEDTLS_USE_PSA_CRYPTO */
435
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 ctx->unprocessed_len = 0;
437
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200438 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200439}
440
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200441#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200443 const unsigned char *ad, size_t ad_len )
444{
445 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200447
Hanno Becker1cb36532018-11-09 16:20:29 +0000448#if defined(MBEDTLS_USE_PSA_CRYPTO)
449 if( ctx->psa_enabled == 1 )
450 {
451 /* While PSA Crypto has an API for multipart
452 * operations, we currently don't make it
453 * accessible through the cipher layer. */
454 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
455 }
456#endif /* MBEDTLS_USE_PSA_CRYPTO */
457
Daniel King8fe47012016-05-17 20:33:28 -0300458#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200462 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200463 }
Daniel King8fe47012016-05-17 20:33:28 -0300464#endif
465
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200466#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300467 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
468 {
469 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200470 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300471
472 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200473 ? MBEDTLS_CHACHAPOLY_ENCRYPT
474 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300475
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200476 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300477 ctx->iv,
478 mode );
479 if ( result != 0 )
480 return( result );
481
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200482 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200483 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300484 }
485#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200486
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200487 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000488}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200489#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200492 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493{
Paul Bakkerff61a782011-06-09 15:42:02 +0000494 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100495 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496
Paul Bakker68884e32013-01-07 18:20:04 +0100497 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000500 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501
Hanno Becker1cb36532018-11-09 16:20:29 +0000502#if defined(MBEDTLS_USE_PSA_CRYPTO)
503 if( ctx->psa_enabled == 1 )
504 {
505 /* While PSA Crypto has an API for multipart
506 * operations, we currently don't make it
507 * accessible through the cipher layer. */
508 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
509 }
510#endif /* MBEDTLS_USE_PSA_CRYPTO */
511
Paul Bakker6c212762013-12-16 15:24:50 +0100512 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100513 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200516 {
Janos Follath98e28a72016-05-31 14:03:54 +0100517 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200519
520 *olen = ilen;
521
522 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
523 ctx->operation, input, output ) ) )
524 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200525 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200526 }
527
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200528 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200529 }
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_GCM_C)
532 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200533 {
534 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200536 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200537 }
538#endif
539
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200540#if defined(MBEDTLS_CHACHAPOLY_C)
541 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
542 {
543 *olen = ilen;
544 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
545 ilen, input, output );
546 }
547#endif
548
Janos Follath98e28a72016-05-31 14:03:54 +0100549 if ( 0 == block_size )
550 {
551 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
552 }
553
Paul Bakker68884e32013-01-07 18:20:04 +0100554 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100555 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100558 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_CIPHER_MODE_CBC)
561 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200563 size_t copy_len = 0;
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565 /*
566 * If there is not enough data for a full block, cache it.
567 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700568 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000569 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700570 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
571 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000573 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574 {
575 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
576 ilen );
577
578 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 }
581
582 /*
583 * Process cached data first
584 */
Janos Follath98e28a72016-05-31 14:03:54 +0100585 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 {
Janos Follath98e28a72016-05-31 14:03:54 +0100587 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588
589 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
590 copy_len );
591
Paul Bakkerff61a782011-06-09 15:42:02 +0000592 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100593 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000594 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200596 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 }
598
Janos Follath98e28a72016-05-31 14:03:54 +0100599 *olen += block_size;
600 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601 ctx->unprocessed_len = 0;
602
603 input += copy_len;
604 ilen -= copy_len;
605 }
606
607 /*
608 * Cache final, incomplete block
609 */
610 if( 0 != ilen )
611 {
Janos Follath98e28a72016-05-31 14:03:54 +0100612 if( 0 == block_size )
613 {
614 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
615 }
616
Andy Leiserson79e77892017-04-28 20:01:49 -0700617 /* Encryption: only cache partial blocks
618 * Decryption w/ padding: always keep at least one whole block
619 * Decryption w/o padding: only cache partial blocks
620 */
Janos Follath98e28a72016-05-31 14:03:54 +0100621 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700622 if( copy_len == 0 &&
623 ctx->operation == MBEDTLS_DECRYPT &&
624 NULL != ctx->add_padding)
625 {
Janos Follath98e28a72016-05-31 14:03:54 +0100626 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700627 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628
629 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
630 copy_len );
631
632 ctx->unprocessed_len += copy_len;
633 ilen -= copy_len;
634 }
635
636 /*
637 * Process remaining full blocks
638 */
639 if( ilen )
640 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000641 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
642 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200644 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200646
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647 *olen += ilen;
648 }
649
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200650 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_CIPHER_MODE_CFB)
655 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000656 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000657 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000658 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000659 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000660 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000662 }
663
664 *olen = ilen;
665
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000669
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100670#if defined(MBEDTLS_CIPHER_MODE_OFB)
671 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
672 {
673 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
674 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
675 {
676 return( ret );
677 }
678
679 *olen = ilen;
680
681 return( 0 );
682 }
683#endif /* MBEDTLS_CIPHER_MODE_OFB */
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_CIPHER_MODE_CTR)
686 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000687 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000688 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000689 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000690 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000691 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200692 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000693 }
694
695 *olen = ilen;
696
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200697 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000698 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000700
Jaeden Ameroc6539902018-04-30 17:17:41 +0100701#if defined(MBEDTLS_CIPHER_MODE_XTS)
702 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
703 {
704 if( ctx->unprocessed_len > 0 ) {
705 /* We can only process an entire data unit at a time. */
706 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
707 }
708
709 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
710 ctx->operation, ilen, ctx->iv, input, output );
711 if( ret != 0 )
712 {
713 return( ret );
714 }
715
716 *olen = ilen;
717
718 return( 0 );
719 }
720#endif /* MBEDTLS_CIPHER_MODE_XTS */
721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_CIPHER_MODE_STREAM)
723 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200724 {
725 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
726 ilen, input, output ) ) )
727 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200728 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200729 }
730
731 *olen = ilen;
732
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738}
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
741#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200742/*
743 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
744 */
Paul Bakker23986e52011-04-24 08:57:21 +0000745static void add_pkcs_padding( unsigned char *output, size_t output_len,
746 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747{
Paul Bakker23986e52011-04-24 08:57:21 +0000748 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100749 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750
751 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000752 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753}
754
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200755static int get_pkcs_padding( unsigned char *input, size_t input_len,
756 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100758 size_t i, pad_idx;
759 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
Paul Bakkera885d682011-01-20 16:35:05 +0000761 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763
764 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765 *data_len = input_len - padding_len;
766
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100767 /* Avoid logical || since it results in a branch */
768 bad |= padding_len > input_len;
769 bad |= padding_len == 0;
770
771 /* The number of bytes checked must be independent of padding_len,
772 * so pick input_len, which is usually 8 or 16 (one block) */
773 pad_idx = input_len - padding_len;
774 for( i = 0; i < input_len; i++ )
775 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200782/*
783 * One and zeros padding: fill with 80 00 ... 00
784 */
785static void add_one_and_zeros_padding( unsigned char *output,
786 size_t output_len, size_t data_len )
787{
788 size_t padding_len = output_len - data_len;
789 unsigned char i = 0;
790
791 output[data_len] = 0x80;
792 for( i = 1; i < padding_len; i++ )
793 output[data_len + i] = 0x00;
794}
795
796static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
797 size_t *data_len )
798{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100799 size_t i;
800 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200801
802 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200804
Micha Krausba8316f2017-12-23 23:40:08 +0100805 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100806 *data_len = 0;
807 for( i = input_len; i > 0; i-- )
808 {
809 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100810 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100811 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100812 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100813 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200816
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200817}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200821/*
822 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
823 */
824static void add_zeros_and_len_padding( unsigned char *output,
825 size_t output_len, size_t data_len )
826{
827 size_t padding_len = output_len - data_len;
828 unsigned char i = 0;
829
830 for( i = 1; i < padding_len; i++ )
831 output[data_len + i - 1] = 0x00;
832 output[output_len - 1] = (unsigned char) padding_len;
833}
834
835static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
836 size_t *data_len )
837{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100838 size_t i, pad_idx;
839 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200840
841 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200843
844 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200845 *data_len = input_len - padding_len;
846
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100847 /* Avoid logical || since it results in a branch */
848 bad |= padding_len > input_len;
849 bad |= padding_len == 0;
850
851 /* The number of bytes checked must be independent of padding_len */
852 pad_idx = input_len - padding_len;
853 for( i = 0; i < input_len - 1; i++ )
854 bad |= input[i] * ( i >= pad_idx );
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200861/*
862 * Zero padding: fill with 00 ... 00
863 */
864static void add_zeros_padding( unsigned char *output,
865 size_t output_len, size_t data_len )
866{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200867 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200868
869 for( i = data_len; i < output_len; i++ )
870 output[i] = 0x00;
871}
872
873static int get_zeros_padding( unsigned char *input, size_t input_len,
874 size_t *data_len )
875{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100876 size_t i;
877 unsigned char done = 0, prev_done;
878
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200879 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200881
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100882 *data_len = 0;
883 for( i = input_len; i > 0; i-- )
884 {
885 prev_done = done;
886 done |= ( input[i-1] != 0 );
887 *data_len |= i * ( done != prev_done );
888 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200889
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200890 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200893
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200894/*
895 * No padding: don't pad :)
896 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200898 * but a trivial get_padding function
899 */
900static int get_no_padding( unsigned char *input, size_t input_len,
901 size_t *data_len )
902{
903 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200905
906 *data_len = input_len;
907
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200908 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200913 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000914{
915 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000917
Hanno Becker1cb36532018-11-09 16:20:29 +0000918#if defined(MBEDTLS_USE_PSA_CRYPTO)
919 if( ctx->psa_enabled == 1 )
920 {
921 /* While PSA Crypto has an API for multipart
922 * operations, we currently don't make it
923 * accessible through the cipher layer. */
924 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
925 }
926#endif /* MBEDTLS_USE_PSA_CRYPTO */
927
Paul Bakker8123e9d2011-01-06 15:37:30 +0000928 *olen = 0;
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100931 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
933 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100934 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000936 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200937 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000938 }
939
Daniel King8fe47012016-05-17 20:33:28 -0300940 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
941 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300942 {
943 return( 0 );
944 }
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200947 {
948 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200950
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200951 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200952 }
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_CIPHER_MODE_CBC)
955 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000956 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200957 int ret = 0;
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000960 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200961 /* check for 'no padding' mode */
962 if( NULL == ctx->add_padding )
963 {
964 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200966
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200967 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200968 }
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000971 ctx->unprocessed_len );
972 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000974 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200975 /*
976 * For decrypt operations, expect a full block,
977 * or an empty block if no padding
978 */
979 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200980 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000983 }
984
985 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000986 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000988 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000989 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200990 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000991 }
992
993 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 if( MBEDTLS_DECRYPT == ctx->operation )
995 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200996 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000997
998 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001000 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001001 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001002#else
1003 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001007}
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker21967c52018-11-09 16:36:33 +00001010int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
1011 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001012{
1013 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001017 }
1018
Hanno Becker1cb36532018-11-09 16:20:29 +00001019#if defined(MBEDTLS_USE_PSA_CRYPTO)
1020 if( ctx->psa_enabled == 1 )
1021 {
1022 /* While PSA Crypto knows about CBC padding
1023 * schemes, we currently don't make them
1024 * accessible through the cipher layer. */
1025 if( mode != MBEDTLS_PADDING_NONE )
1026 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1027
1028 return( 0 );
1029 }
1030#endif /* MBEDTLS_USE_PSA_CRYPTO */
1031
Paul Bakker1a45d912013-08-14 12:04:26 +02001032 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
1035 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001036 ctx->add_padding = add_pkcs_padding;
1037 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001038 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
1041 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001042 ctx->add_padding = add_one_and_zeros_padding;
1043 ctx->get_padding = get_one_and_zeros_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_ZEROS_AND_LEN)
1047 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001048 ctx->add_padding = add_zeros_and_len_padding;
1049 ctx->get_padding = get_zeros_and_len_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)
1053 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001054 ctx->add_padding = add_zeros_padding;
1055 ctx->get_padding = get_zeros_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 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001059 ctx->add_padding = NULL;
1060 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001061 break;
1062
1063 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001065 }
1066
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001067 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001070
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001071#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001073 unsigned char *tag, size_t tag_len )
1074{
1075 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( MBEDTLS_ENCRYPT != ctx->operation )
1079 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001080
Hanno Becker1cb36532018-11-09 16:20:29 +00001081#if defined(MBEDTLS_USE_PSA_CRYPTO)
1082 if( ctx->psa_enabled == 1 )
1083 {
1084 /* While PSA Crypto has an API for multipart
1085 * operations, we currently don't make it
1086 * accessible through the cipher layer. */
1087 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1088
1089 return( 0 );
1090 }
1091#endif /* MBEDTLS_USE_PSA_CRYPTO */
1092
Daniel King8fe47012016-05-17 20:33:28 -03001093#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker21967c52018-11-09 16:36:33 +00001095 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1096 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001097#endif
1098
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001099#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001100 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1101 {
1102 /* Don't allow truncated MAC for Poly1305 */
1103 if ( tag_len != 16U )
1104 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1105
Hanno Becker21967c52018-11-09 16:36:33 +00001106 return( mbedtls_chachapoly_finish(
1107 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001108 }
1109#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001110
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001111 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001112}
Paul Bakker9af723c2014-05-01 13:03:14 +02001113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001115 const unsigned char *tag, size_t tag_len )
1116{
Daniel King8fe47012016-05-17 20:33:28 -03001117 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001118 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001119
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001120 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001124 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001125
Hanno Becker1cb36532018-11-09 16:20:29 +00001126#if defined(MBEDTLS_USE_PSA_CRYPTO)
1127 if( ctx->psa_enabled == 1 )
1128 {
1129 /* While PSA Crypto has an API for multipart
1130 * operations, we currently don't make it
1131 * accessible through the cipher layer. */
1132 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1133 }
1134#endif /* MBEDTLS_USE_PSA_CRYPTO */
1135
Daniel King8fe47012016-05-17 20:33:28 -03001136#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001138 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001139 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001141
Hanno Becker21967c52018-11-09 16:36:33 +00001142 if( 0 != ( ret = mbedtls_gcm_finish(
1143 (mbedtls_gcm_context *) ctx->cipher_ctx,
1144 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001145 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001146 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001147 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001148
1149 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001150 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001152
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001153 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001154 }
Daniel King8fe47012016-05-17 20:33:28 -03001155#endif /* MBEDTLS_GCM_C */
1156
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001157#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001158 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1159 {
1160 /* Don't allow truncated MAC for Poly1305 */
1161 if ( tag_len != sizeof( check_tag ) )
1162 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1163
Hanno Becker21967c52018-11-09 16:36:33 +00001164 ret = mbedtls_chachapoly_finish(
1165 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001166 if ( ret != 0 )
1167 {
1168 return( ret );
1169 }
1170
1171 /* Check the tag in "constant-time" */
1172 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1173 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1174
1175 return( 0 );
1176 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001177#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001178
1179 return( 0 );
1180}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001181#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001183/*
1184 * Packet-oriented wrapper for non-AEAD modes
1185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001187 const unsigned char *iv, size_t iv_len,
1188 const unsigned char *input, size_t ilen,
1189 unsigned char *output, size_t *olen )
1190{
1191 int ret;
1192 size_t finish_olen;
1193
Hanno Becker1cb36532018-11-09 16:20:29 +00001194#if defined(MBEDTLS_USE_PSA_CRYPTO)
1195 if( ctx->psa_enabled == 1 )
1196 {
Hanno Becker3c852a92018-11-12 12:36:17 +00001197 /* As in the non-PSA case, we don't check that
1198 * a key has been set. If not, the key slot will
1199 * still be in its default state of 0, which is
1200 * guaranteed to be invalid, hence the PSA-call
1201 * below will gracefully fail. */
1202 mbedtls_cipher_context_psa * const cipher_psa =
1203 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1204
1205 psa_status_t status;
1206 psa_cipher_operation_t cipher_op;
1207 size_t part_len;
1208
1209 if( ctx->operation == MBEDTLS_DECRYPT )
1210 {
1211 status = psa_cipher_decrypt_setup( &cipher_op,
1212 cipher_psa->slot,
1213 cipher_psa->alg );
1214 }
1215 else if( ctx->operation == MBEDTLS_ENCRYPT )
1216 {
1217 status = psa_cipher_encrypt_setup( &cipher_op,
1218 cipher_psa->slot,
1219 cipher_psa->alg );
1220 }
1221 else
1222 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1223
1224 /* In the following, we can immediately return on an error,
1225 * because the PSA Crypto API guarantees that cipher operations
1226 * are terminated by unsuccessful calls to psa_cipher_update(),
1227 * and by any call to psa_cipher_finish(). */
1228 if( status != PSA_SUCCESS )
1229 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1230
1231 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1232 if( status != PSA_SUCCESS )
1233 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1234
1235 status = psa_cipher_update( &cipher_op,
1236 input, ilen,
1237 output, ilen, olen );
1238 if( status != PSA_SUCCESS )
1239 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1240
1241 status = psa_cipher_finish( &cipher_op,
1242 output + *olen, ilen - *olen,
1243 &part_len );
1244 if( status != PSA_SUCCESS )
1245 return( MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED );
1246
1247 *olen += part_len;
1248 return( 0 );
Hanno Becker1cb36532018-11-09 16:20:29 +00001249 }
1250#endif /* MBEDTLS_USE_PSA_CRYPTO */
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001253 return( ret );
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001256 return( ret );
1257
Hanno Becker21967c52018-11-09 16:36:33 +00001258 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1259 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001260 return( ret );
1261
Hanno Becker21967c52018-11-09 16:36:33 +00001262 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1263 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001264 return( ret );
1265
1266 *olen += finish_olen;
1267
1268 return( 0 );
1269}
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001272/*
1273 * Packet-oriented encryption for AEAD modes
1274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001276 const unsigned char *iv, size_t iv_len,
1277 const unsigned char *ad, size_t ad_len,
1278 const unsigned char *input, size_t ilen,
1279 unsigned char *output, size_t *olen,
1280 unsigned char *tag, size_t tag_len )
1281{
Hanno Becker1cb36532018-11-09 16:20:29 +00001282#if defined(MBEDTLS_USE_PSA_CRYPTO)
1283 if( ctx->psa_enabled == 1 )
1284 {
1285 /* TODO */
1286 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1287 }
1288#endif /* MBEDTLS_USE_PSA_CRYPTO */
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_GCM_C)
1291 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001292 {
1293 *olen = ilen;
Hanno Becker21967c52018-11-09 16:36:33 +00001294 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1295 ilen, iv, iv_len, ad, ad_len,
1296 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_GCM_C */
1299#if defined(MBEDTLS_CCM_C)
1300 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001301 {
1302 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001304 iv, iv_len, ad, ad_len, input, output,
1305 tag, tag_len ) );
1306 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001308#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001309 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1310 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001311 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001312 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001313 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001314 {
1315 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1316 }
1317
1318 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001319 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001320 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001321 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001322#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001325}
1326
1327/*
1328 * Packet-oriented decryption for AEAD modes
1329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001331 const unsigned char *iv, size_t iv_len,
1332 const unsigned char *ad, size_t ad_len,
1333 const unsigned char *input, size_t ilen,
1334 unsigned char *output, size_t *olen,
1335 const unsigned char *tag, size_t tag_len )
1336{
Hanno Becker1cb36532018-11-09 16:20:29 +00001337#if defined(MBEDTLS_USE_PSA_CRYPTO)
1338 if( ctx->psa_enabled == 1 )
1339 {
1340 /* TODO */
1341 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1342 }
1343#endif /* MBEDTLS_USE_PSA_CRYPTO */
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_GCM_C)
1346 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001347 {
1348 int ret;
1349
1350 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001352 iv, iv_len, ad, ad_len,
1353 tag, tag_len, input, output );
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1356 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001357
1358 return( ret );
1359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#endif /* MBEDTLS_GCM_C */
1361#if defined(MBEDTLS_CCM_C)
1362 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001363 {
1364 int ret;
1365
1366 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001368 iv, iv_len, ad, ad_len,
1369 input, output, tag, tag_len );
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1372 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001373
1374 return( ret );
1375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001377#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001378 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1379 {
Daniel King8fe47012016-05-17 20:33:28 -03001380 int ret;
1381
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001382 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001383 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001384 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001385 {
1386 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1387 }
1388
1389 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001390 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1391 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001392
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001393 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1394 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001395
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001396 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001397 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001398#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_CIPHER_C */