blob: 14ff37111c8dfedd8084bfdb16178d3ac11b5d81 [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
61#if defined(MBEDTLS_PLATFORM_C)
62#include "mbedtls/platform.h"
63#else
64#define mbedtls_calloc calloc
65#define mbedtls_free free
66#endif
67
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010068#define CIPHER_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
70#define CIPHER_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
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 */
Daniel King8fe47012016-05-17 20:33:28 -030079static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
80{
81 const unsigned char *p1 = (const unsigned char*) v1;
82 const unsigned char *p2 = (const unsigned char*) v2;
83 size_t i;
84 unsigned char diff;
85
86 for( diff = 0, i = 0; i < len; i++ )
87 diff |= p1[i] ^ p2[i];
88
89 return (int)diff;
90}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020091#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -030092
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020093static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020098 int *type;
99
100 if( ! supported_init )
101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 def = mbedtls_cipher_definitions;
103 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104
105 while( def->type != 0 )
106 *type++ = (*def++).type;
107
108 *type = 0;
109
110 supported_init = 1;
111 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +0000114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200121 if( def->type == cipher_type )
122 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000123
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200130
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200132 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200135 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200136 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000137
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200138 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200142 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200148 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200149 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200150 def->info->mode == mode )
151 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200152
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200153 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200154}
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200157{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100158 CIPHER_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200163{
164 if( ctx == NULL )
165 return;
166
Simon Butcher327398a2016-10-05 14:09:11 +0100167#if defined(MBEDTLS_CMAC_C)
168 if( ctx->cmac_ctx )
169 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500170 mbedtls_platform_zeroize( ctx->cmac_ctx,
171 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100172 mbedtls_free( ctx->cmac_ctx );
173 }
174#endif
175
Paul Bakker84bbeb52014-07-01 14:53:22 +0200176 if( ctx->cipher_ctx )
177 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
178
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500179 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180}
181
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200182int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183{
k-stachowiaka5390702018-12-17 11:27:03 +0100184 CIPHER_VALIDATE_RET( ctx != NULL );
185 CIPHER_VALIDATE_RET( cipher_info != NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
Paul Bakker343a8702011-06-09 14:27:58 +0000189 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
192 ctx->cipher_info = cipher_info;
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200195 /*
196 * Ignore possible errors caused by a cipher mode that doesn't use padding
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
199 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200200#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200204
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200205 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206}
207
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100208int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
209 const unsigned char *key,
210 int key_bitlen,
211 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212{
k-stachowiaka5390702018-12-17 11:27:03 +0100213 CIPHER_VALIDATE_RET( ctx != NULL );
214 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
215 CIPHER_VALIDATE_RET( key != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100216 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
217 operation == MBEDTLS_DECRYPT );
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200220 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200223 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200224
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200225 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226 ctx->operation = operation;
227
Paul Bakker343a8702011-06-09 14:27:58 +0000228 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100229 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( MBEDTLS_ENCRYPT == operation ||
232 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100233 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000235 {
236 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200237 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000238 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000241 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200242 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245}
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100248 const unsigned char *iv,
249 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000250{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200251 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100252
k-stachowiaka5390702018-12-17 11:27:03 +0100253 CIPHER_VALIDATE_RET( ctx != NULL );
254 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
255 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200257 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
259 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200262 actual_iv_size = iv_len;
263 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200264 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200265 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200266
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200267 /* avoid reading past the end of input buffer */
268 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200270 }
271
Daniel Kingbd920622016-05-15 19:56:20 -0300272#if defined(MBEDTLS_CHACHA20_C)
273 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
274 {
275 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
276 iv,
277 0U ) ) /* Initial counter value */
278 {
279 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
280 }
281 }
282#endif
283
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300284 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300285 {
286 memcpy( ctx->iv, iv, actual_iv_size );
287 ctx->iv_size = actual_iv_size;
288 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200289
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200290 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200291}
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200294{
k-stachowiaka5390702018-12-17 11:27:03 +0100295 CIPHER_VALIDATE_RET( ctx != NULL );
296 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200297
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298 ctx->unprocessed_len = 0;
299
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200300 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200301}
302
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200303#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200305 const unsigned char *ad, size_t ad_len )
306{
k-stachowiaka5390702018-12-17 11:27:03 +0100307 CIPHER_VALIDATE_RET( ctx != NULL );
308 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100309 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
310
Daniel King8fe47012016-05-17 20:33:28 -0300311#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200315 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200316 }
Daniel King8fe47012016-05-17 20:33:28 -0300317#endif
318
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200319#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300320 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
321 {
322 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200323 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300324
325 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200326 ? MBEDTLS_CHACHAPOLY_ENCRYPT
327 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300328
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200329 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300330 ctx->iv,
331 mode );
332 if ( result != 0 )
333 return( result );
334
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200335 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200336 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300337 }
338#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200339
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200340 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200342#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200345 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346{
Paul Bakkerff61a782011-06-09 15:42:02 +0000347 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100348 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
k-stachowiaka5390702018-12-17 11:27:03 +0100350 CIPHER_VALIDATE_RET( ctx != NULL );
351 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100352 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100353 CIPHER_VALIDATE_RET( output != NULL );
354 CIPHER_VALIDATE_RET( olen != NULL );
355
Paul Bakker6c212762013-12-16 15:24:50 +0100356 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100357 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200360 {
Janos Follath98e28a72016-05-31 14:03:54 +0100361 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200363
364 *olen = ilen;
365
366 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
367 ctx->operation, input, output ) ) )
368 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200370 }
371
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200372 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200373 }
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_GCM_C)
376 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200377 {
378 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200380 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200381 }
382#endif
383
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200384#if defined(MBEDTLS_CHACHAPOLY_C)
385 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
386 {
387 *olen = ilen;
388 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
389 ilen, input, output );
390 }
391#endif
392
Janos Follath98e28a72016-05-31 14:03:54 +0100393 if ( 0 == block_size )
394 {
395 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
396 }
397
Paul Bakker68884e32013-01-07 18:20:04 +0100398 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100399 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100402 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_CIPHER_MODE_CBC)
405 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200407 size_t copy_len = 0;
408
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409 /*
410 * If there is not enough data for a full block, cache it.
411 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700412 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000413 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700414 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
415 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000417 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418 {
419 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
420 ilen );
421
422 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200423 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 }
425
426 /*
427 * Process cached data first
428 */
Janos Follath98e28a72016-05-31 14:03:54 +0100429 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 {
Janos Follath98e28a72016-05-31 14:03:54 +0100431 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432
433 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
434 copy_len );
435
Paul Bakkerff61a782011-06-09 15:42:02 +0000436 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100437 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000438 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200440 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 }
442
Janos Follath98e28a72016-05-31 14:03:54 +0100443 *olen += block_size;
444 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445 ctx->unprocessed_len = 0;
446
447 input += copy_len;
448 ilen -= copy_len;
449 }
450
451 /*
452 * Cache final, incomplete block
453 */
454 if( 0 != ilen )
455 {
Janos Follath98e28a72016-05-31 14:03:54 +0100456 if( 0 == block_size )
457 {
458 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
459 }
460
Andy Leiserson79e77892017-04-28 20:01:49 -0700461 /* Encryption: only cache partial blocks
462 * Decryption w/ padding: always keep at least one whole block
463 * Decryption w/o padding: only cache partial blocks
464 */
Janos Follath98e28a72016-05-31 14:03:54 +0100465 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700466 if( copy_len == 0 &&
467 ctx->operation == MBEDTLS_DECRYPT &&
468 NULL != ctx->add_padding)
469 {
Janos Follath98e28a72016-05-31 14:03:54 +0100470 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700471 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472
473 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
474 copy_len );
475
476 ctx->unprocessed_len += copy_len;
477 ilen -= copy_len;
478 }
479
480 /*
481 * Process remaining full blocks
482 */
483 if( ilen )
484 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000485 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
486 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200488 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200490
Paul Bakker8123e9d2011-01-06 15:37:30 +0000491 *olen += ilen;
492 }
493
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_CIPHER_MODE_CFB)
499 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000500 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000501 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000502 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000503 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000504 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200505 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000506 }
507
508 *olen = ilen;
509
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000511 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000513
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100514#if defined(MBEDTLS_CIPHER_MODE_OFB)
515 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
516 {
517 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
518 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
519 {
520 return( ret );
521 }
522
523 *olen = ilen;
524
525 return( 0 );
526 }
527#endif /* MBEDTLS_CIPHER_MODE_OFB */
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_CIPHER_MODE_CTR)
530 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000531 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000532 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000533 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000534 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000535 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200536 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000537 }
538
539 *olen = ilen;
540
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200541 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000544
Jaeden Ameroc6539902018-04-30 17:17:41 +0100545#if defined(MBEDTLS_CIPHER_MODE_XTS)
546 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
547 {
548 if( ctx->unprocessed_len > 0 ) {
549 /* We can only process an entire data unit at a time. */
550 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
551 }
552
553 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
554 ctx->operation, ilen, ctx->iv, input, output );
555 if( ret != 0 )
556 {
557 return( ret );
558 }
559
560 *olen = ilen;
561
562 return( 0 );
563 }
564#endif /* MBEDTLS_CIPHER_MODE_XTS */
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_CIPHER_MODE_STREAM)
567 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200568 {
569 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
570 ilen, input, output ) ) )
571 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200572 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200573 }
574
575 *olen = ilen;
576
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200577 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582}
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
585#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200586/*
587 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
588 */
Paul Bakker23986e52011-04-24 08:57:21 +0000589static void add_pkcs_padding( unsigned char *output, size_t output_len,
590 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591{
Paul Bakker23986e52011-04-24 08:57:21 +0000592 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100593 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
595 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000596 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597}
598
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200599static int get_pkcs_padding( unsigned char *input, size_t input_len,
600 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100602 size_t i, pad_idx;
603 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000604
Paul Bakkera885d682011-01-20 16:35:05 +0000605 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
608 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609 *data_len = input_len - padding_len;
610
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100611 /* Avoid logical || since it results in a branch */
612 bad |= padding_len > input_len;
613 bad |= padding_len == 0;
614
615 /* The number of bytes checked must be independent of padding_len,
616 * so pick input_len, which is usually 8 or 16 (one block) */
617 pad_idx = input_len - padding_len;
618 for( i = 0; i < input_len; i++ )
619 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200626/*
627 * One and zeros padding: fill with 80 00 ... 00
628 */
629static void add_one_and_zeros_padding( unsigned char *output,
630 size_t output_len, size_t data_len )
631{
632 size_t padding_len = output_len - data_len;
633 unsigned char i = 0;
634
635 output[data_len] = 0x80;
636 for( i = 1; i < padding_len; i++ )
637 output[data_len + i] = 0x00;
638}
639
640static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
641 size_t *data_len )
642{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100643 size_t i;
644 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200645
646 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200648
Micha Krausba8316f2017-12-23 23:40:08 +0100649 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100650 *data_len = 0;
651 for( i = input_len; i > 0; i-- )
652 {
653 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100654 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100655 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100656 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100657 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200660
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200665/*
666 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
667 */
668static void add_zeros_and_len_padding( unsigned char *output,
669 size_t output_len, size_t data_len )
670{
671 size_t padding_len = output_len - data_len;
672 unsigned char i = 0;
673
674 for( i = 1; i < padding_len; i++ )
675 output[data_len + i - 1] = 0x00;
676 output[output_len - 1] = (unsigned char) padding_len;
677}
678
679static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
680 size_t *data_len )
681{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100682 size_t i, pad_idx;
683 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200684
685 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200687
688 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200689 *data_len = input_len - padding_len;
690
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100691 /* Avoid logical || since it results in a branch */
692 bad |= padding_len > input_len;
693 bad |= padding_len == 0;
694
695 /* The number of bytes checked must be independent of padding_len */
696 pad_idx = input_len - padding_len;
697 for( i = 0; i < input_len - 1; i++ )
698 bad |= input[i] * ( i >= pad_idx );
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200705/*
706 * Zero padding: fill with 00 ... 00
707 */
708static void add_zeros_padding( unsigned char *output,
709 size_t output_len, size_t data_len )
710{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200711 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200712
713 for( i = data_len; i < output_len; i++ )
714 output[i] = 0x00;
715}
716
717static int get_zeros_padding( unsigned char *input, size_t input_len,
718 size_t *data_len )
719{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100720 size_t i;
721 unsigned char done = 0, prev_done;
722
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200723 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200725
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100726 *data_len = 0;
727 for( i = input_len; i > 0; i-- )
728 {
729 prev_done = done;
730 done |= ( input[i-1] != 0 );
731 *data_len |= i * ( done != prev_done );
732 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200733
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200737
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200738/*
739 * No padding: don't pad :)
740 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200742 * but a trivial get_padding function
743 */
744static int get_no_padding( unsigned char *input, size_t input_len,
745 size_t *data_len )
746{
747 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200749
750 *data_len = input_len;
751
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200752 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200757 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758{
k-stachowiaka5390702018-12-17 11:27:03 +0100759 CIPHER_VALIDATE_RET( ctx != NULL );
760 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100761 CIPHER_VALIDATE_RET( output != NULL );
762 CIPHER_VALIDATE_RET( olen != NULL );
763
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764 *olen = 0;
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100767 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
769 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100770 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000772 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200773 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000774 }
775
Daniel King8fe47012016-05-17 20:33:28 -0300776 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
777 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300778 {
779 return( 0 );
780 }
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200783 {
784 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200786
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200787 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200788 }
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_CIPHER_MODE_CBC)
791 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200793 int ret = 0;
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200797 /* check for 'no padding' mode */
798 if( NULL == ctx->add_padding )
799 {
800 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200802
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200803 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000807 ctx->unprocessed_len );
808 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000810 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200811 /*
812 * For decrypt operations, expect a full block,
813 * or an empty block if no padding
814 */
815 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200816 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819 }
820
821 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000822 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000824 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000825 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200826 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827 }
828
829 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 if( MBEDTLS_DECRYPT == ctx->operation )
831 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200832 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000833
834 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200836 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000837 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200838#else
839 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843}
844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100846int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
847 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200848{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100849 CIPHER_VALIDATE_RET( ctx != NULL );
850 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
851
852 if( MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200855 }
856
Paul Bakker1a45d912013-08-14 12:04:26 +0200857 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
860 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200861 ctx->add_padding = add_pkcs_padding;
862 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200863 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
866 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200867 ctx->add_padding = add_one_and_zeros_padding;
868 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200869 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
872 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200873 ctx->add_padding = add_zeros_and_len_padding;
874 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200875 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
878 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200879 ctx->add_padding = add_zeros_padding;
880 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200881 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200882#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200884 ctx->add_padding = NULL;
885 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200886 break;
887
888 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200890 }
891
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200892 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200895
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200896#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200898 unsigned char *tag, size_t tag_len )
899{
k-stachowiaka5390702018-12-17 11:27:03 +0100900 CIPHER_VALIDATE_RET( ctx != NULL );
901 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100902 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 if( MBEDTLS_ENCRYPT != ctx->operation )
905 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200906
Daniel King8fe47012016-05-17 20:33:28 -0300907#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
909 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300910#endif
911
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200912#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300913 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
914 {
915 /* Don't allow truncated MAC for Poly1305 */
916 if ( tag_len != 16U )
917 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
918
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200919 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300920 tag );
921 }
922#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200923
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200924 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200925}
Paul Bakker9af723c2014-05-01 13:03:14 +0200926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200928 const unsigned char *tag, size_t tag_len )
929{
Daniel King8fe47012016-05-17 20:33:28 -0300930 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200931 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200932
k-stachowiaka5390702018-12-17 11:27:03 +0100933 CIPHER_VALIDATE_RET( ctx != NULL );
934 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100935 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100936
k-stachowiaka5390702018-12-17 11:27:03 +0100937 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200940 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200941
Daniel King8fe47012016-05-17 20:33:28 -0300942#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200944 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200945 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200949 check_tag, tag_len ) ) )
950 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200951 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200952 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200953
954 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300955 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200957
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200958 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200959 }
Daniel King8fe47012016-05-17 20:33:28 -0300960#endif /* MBEDTLS_GCM_C */
961
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200962#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300963 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
964 {
965 /* Don't allow truncated MAC for Poly1305 */
966 if ( tag_len != sizeof( check_tag ) )
967 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
968
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200969 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300970 check_tag );
971 if ( ret != 0 )
972 {
973 return( ret );
974 }
975
976 /* Check the tag in "constant-time" */
977 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
978 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
979
980 return( 0 );
981 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200982#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200983
984 return( 0 );
985}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200986#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200987
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200988/*
989 * Packet-oriented wrapper for non-AEAD modes
990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200992 const unsigned char *iv, size_t iv_len,
993 const unsigned char *input, size_t ilen,
994 unsigned char *output, size_t *olen )
995{
996 int ret;
997 size_t finish_olen;
998
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100999 CIPHER_VALIDATE_RET( ctx != NULL );
1000 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1001 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001002 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001003 CIPHER_VALIDATE_RET( output != NULL );
1004 CIPHER_VALIDATE_RET( olen != NULL );
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001007 return( ret );
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001010 return( ret );
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001013 return( ret );
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001016 return( ret );
1017
1018 *olen += finish_olen;
1019
1020 return( 0 );
1021}
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001024/*
1025 * Packet-oriented encryption for AEAD modes
1026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001028 const unsigned char *iv, size_t iv_len,
1029 const unsigned char *ad, size_t ad_len,
1030 const unsigned char *input, size_t ilen,
1031 unsigned char *output, size_t *olen,
1032 unsigned char *tag, size_t tag_len )
1033{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001034 CIPHER_VALIDATE_RET( ctx != NULL );
1035 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1036 CIPHER_VALIDATE_RET( iv != NULL );
1037 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001038 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001039 CIPHER_VALIDATE_RET( output != NULL );
1040 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001041 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_GCM_C)
1044 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001045 {
1046 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001048 iv, iv_len, ad, ad_len, input, output,
1049 tag_len, tag ) );
1050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_GCM_C */
1052#if defined(MBEDTLS_CCM_C)
1053 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001054 {
1055 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001057 iv, iv_len, ad, ad_len, input, output,
1058 tag, tag_len ) );
1059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001061#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001062 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1063 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001064 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001065 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001066 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001067 {
1068 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1069 }
1070
1071 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001072 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001073 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001074 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001075#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001078}
1079
1080/*
1081 * Packet-oriented decryption for AEAD modes
1082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001084 const unsigned char *iv, size_t iv_len,
1085 const unsigned char *ad, size_t ad_len,
1086 const unsigned char *input, size_t ilen,
1087 unsigned char *output, size_t *olen,
1088 const unsigned char *tag, size_t tag_len )
1089{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001090 CIPHER_VALIDATE_RET( ctx != NULL );
1091 CIPHER_VALIDATE_RET( ctx->cipher_info != NULL );
1092 CIPHER_VALIDATE_RET( iv != NULL );
1093 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001094 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001095 CIPHER_VALIDATE_RET( output != NULL );
1096 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001097 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_GCM_C)
1100 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001101 {
1102 int ret;
1103
1104 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001106 iv, iv_len, ad, ad_len,
1107 tag, tag_len, input, output );
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1110 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001111
1112 return( ret );
1113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_GCM_C */
1115#if defined(MBEDTLS_CCM_C)
1116 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001117 {
1118 int ret;
1119
1120 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001122 iv, iv_len, ad, ad_len,
1123 input, output, tag, tag_len );
1124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1126 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001127
1128 return( ret );
1129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001131#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001132 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1133 {
Daniel King8fe47012016-05-17 20:33:28 -03001134 int ret;
1135
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001136 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001137 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001138 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001139 {
1140 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1141 }
1142
1143 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001144 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1145 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001146
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001147 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1148 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001149
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001150 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001151 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001152#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_CIPHER_C */