blob: b37fc768c5466cafb54938af7a1b24d6b14be954 [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 );
k-stachowiak95070a82018-12-19 14:48:37 +0100185 if( cipher_info == NULL )
186 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189
Paul Bakker343a8702011-06-09 14:27:58 +0000190 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
193 ctx->cipher_info = cipher_info;
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200196 /*
197 * Ignore possible errors caused by a cipher mode that doesn't use padding
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
200 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200201#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200205
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200206 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207}
208
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100209int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
210 const unsigned char *key,
211 int key_bitlen,
212 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213{
k-stachowiaka5390702018-12-17 11:27:03 +0100214 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100215 CIPHER_VALIDATE_RET( key != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100216 CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
217 operation == MBEDTLS_DECRYPT );
k-stachowiak95070a82018-12-19 14:48:37 +0100218 if( ctx->cipher_info == NULL )
219 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200222 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200225 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200226
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200227 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228 ctx->operation = operation;
229
Paul Bakker343a8702011-06-09 14:27:58 +0000230 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100231 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( MBEDTLS_ENCRYPT == operation ||
234 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100235 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000237 {
238 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200239 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000240 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000243 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200244 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247}
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100250 const unsigned char *iv,
251 size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200253 size_t actual_iv_size;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100254
k-stachowiaka5390702018-12-17 11:27:03 +0100255 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiaka5390702018-12-17 11:27:03 +0100256 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100257 if( ctx->cipher_info == NULL )
258 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200260 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
262 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200265 actual_iv_size = iv_len;
266 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200267 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200268 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200269
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200270 /* avoid reading past the end of input buffer */
271 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200273 }
274
Daniel Kingbd920622016-05-15 19:56:20 -0300275#if defined(MBEDTLS_CHACHA20_C)
276 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
277 {
278 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
279 iv,
280 0U ) ) /* Initial counter value */
281 {
282 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
283 }
284 }
285#endif
286
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300287 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300288 {
289 memcpy( ctx->iv, iv, actual_iv_size );
290 ctx->iv_size = actual_iv_size;
291 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200292
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200293 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200294}
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200297{
k-stachowiaka5390702018-12-17 11:27:03 +0100298 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100299 if( ctx->cipher_info == NULL )
300 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200301
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 ctx->unprocessed_len = 0;
303
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200304 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200305}
306
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200307#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200309 const unsigned char *ad, size_t ad_len )
310{
k-stachowiaka5390702018-12-17 11:27:03 +0100311 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100312 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100313 if( ctx->cipher_info == NULL )
314 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100315
Daniel King8fe47012016-05-17 20:33:28 -0300316#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200320 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200321 }
Daniel King8fe47012016-05-17 20:33:28 -0300322#endif
323
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200324#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300325 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
326 {
327 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200328 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300329
330 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200331 ? MBEDTLS_CHACHAPOLY_ENCRYPT
332 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300333
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200334 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300335 ctx->iv,
336 mode );
337 if ( result != 0 )
338 return( result );
339
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200340 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200341 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300342 }
343#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200344
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200345 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000346}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200347#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200350 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351{
Paul Bakkerff61a782011-06-09 15:42:02 +0000352 int ret;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100353 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
k-stachowiaka5390702018-12-17 11:27:03 +0100355 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100356 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100357 CIPHER_VALIDATE_RET( output != NULL );
358 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100359 if( ctx->cipher_info == NULL )
360 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100361
Paul Bakker6c212762013-12-16 15:24:50 +0100362 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100363 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200366 {
Janos Follath98e28a72016-05-31 14:03:54 +0100367 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200369
370 *olen = ilen;
371
372 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
373 ctx->operation, input, output ) ) )
374 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200375 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200376 }
377
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200378 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200379 }
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_GCM_C)
382 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200383 {
384 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200386 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200387 }
388#endif
389
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200390#if defined(MBEDTLS_CHACHAPOLY_C)
391 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
392 {
393 *olen = ilen;
394 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
395 ilen, input, output );
396 }
397#endif
398
Janos Follath98e28a72016-05-31 14:03:54 +0100399 if ( 0 == block_size )
400 {
401 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
402 }
403
Paul Bakker68884e32013-01-07 18:20:04 +0100404 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100405 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100408 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_CIPHER_MODE_CBC)
411 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200413 size_t copy_len = 0;
414
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 /*
416 * If there is not enough data for a full block, cache it.
417 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700418 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000419 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700420 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
421 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000423 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000424 {
425 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
426 ilen );
427
428 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200429 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000430 }
431
432 /*
433 * Process cached data first
434 */
Janos Follath98e28a72016-05-31 14:03:54 +0100435 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 {
Janos Follath98e28a72016-05-31 14:03:54 +0100437 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438
439 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
440 copy_len );
441
Paul Bakkerff61a782011-06-09 15:42:02 +0000442 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100443 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000444 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200446 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 }
448
Janos Follath98e28a72016-05-31 14:03:54 +0100449 *olen += block_size;
450 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 ctx->unprocessed_len = 0;
452
453 input += copy_len;
454 ilen -= copy_len;
455 }
456
457 /*
458 * Cache final, incomplete block
459 */
460 if( 0 != ilen )
461 {
Janos Follath98e28a72016-05-31 14:03:54 +0100462 if( 0 == block_size )
463 {
464 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
465 }
466
Andy Leiserson79e77892017-04-28 20:01:49 -0700467 /* Encryption: only cache partial blocks
468 * Decryption w/ padding: always keep at least one whole block
469 * Decryption w/o padding: only cache partial blocks
470 */
Janos Follath98e28a72016-05-31 14:03:54 +0100471 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700472 if( copy_len == 0 &&
473 ctx->operation == MBEDTLS_DECRYPT &&
474 NULL != ctx->add_padding)
475 {
Janos Follath98e28a72016-05-31 14:03:54 +0100476 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700477 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478
479 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
480 copy_len );
481
482 ctx->unprocessed_len += copy_len;
483 ilen -= copy_len;
484 }
485
486 /*
487 * Process remaining full blocks
488 */
489 if( ilen )
490 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000491 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
492 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200496
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497 *olen += ilen;
498 }
499
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200500 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#if defined(MBEDTLS_CIPHER_MODE_CFB)
505 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000506 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000507 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000508 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000509 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000510 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200511 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000512 }
513
514 *olen = ilen;
515
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200516 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000519
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100520#if defined(MBEDTLS_CIPHER_MODE_OFB)
521 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
522 {
523 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
524 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
525 {
526 return( ret );
527 }
528
529 *olen = ilen;
530
531 return( 0 );
532 }
533#endif /* MBEDTLS_CIPHER_MODE_OFB */
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_CIPHER_MODE_CTR)
536 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000537 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000538 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000539 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000540 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000541 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200542 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000543 }
544
545 *olen = ilen;
546
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200547 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000550
Jaeden Ameroc6539902018-04-30 17:17:41 +0100551#if defined(MBEDTLS_CIPHER_MODE_XTS)
552 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
553 {
554 if( ctx->unprocessed_len > 0 ) {
555 /* We can only process an entire data unit at a time. */
556 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
557 }
558
559 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
560 ctx->operation, ilen, ctx->iv, input, output );
561 if( ret != 0 )
562 {
563 return( ret );
564 }
565
566 *olen = ilen;
567
568 return( 0 );
569 }
570#endif /* MBEDTLS_CIPHER_MODE_XTS */
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_CIPHER_MODE_STREAM)
573 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200574 {
575 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
576 ilen, input, output ) ) )
577 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200578 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200579 }
580
581 *olen = ilen;
582
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200583 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588}
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
591#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200592/*
593 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
594 */
Paul Bakker23986e52011-04-24 08:57:21 +0000595static void add_pkcs_padding( unsigned char *output, size_t output_len,
596 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597{
Paul Bakker23986e52011-04-24 08:57:21 +0000598 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100599 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600
601 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000602 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603}
604
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200605static int get_pkcs_padding( unsigned char *input, size_t input_len,
606 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100608 size_t i, pad_idx;
609 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
Paul Bakkera885d682011-01-20 16:35:05 +0000611 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
614 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615 *data_len = input_len - padding_len;
616
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100617 /* Avoid logical || since it results in a branch */
618 bad |= padding_len > input_len;
619 bad |= padding_len == 0;
620
621 /* The number of bytes checked must be independent of padding_len,
622 * so pick input_len, which is usually 8 or 16 (one block) */
623 pad_idx = input_len - padding_len;
624 for( i = 0; i < input_len; i++ )
625 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200632/*
633 * One and zeros padding: fill with 80 00 ... 00
634 */
635static void add_one_and_zeros_padding( unsigned char *output,
636 size_t output_len, size_t data_len )
637{
638 size_t padding_len = output_len - data_len;
639 unsigned char i = 0;
640
641 output[data_len] = 0x80;
642 for( i = 1; i < padding_len; i++ )
643 output[data_len + i] = 0x00;
644}
645
646static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
647 size_t *data_len )
648{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100649 size_t i;
650 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200651
652 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200654
Micha Krausba8316f2017-12-23 23:40:08 +0100655 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100656 *data_len = 0;
657 for( i = input_len; i > 0; i-- )
658 {
659 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100660 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100661 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100662 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100663 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200666
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200671/*
672 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
673 */
674static void add_zeros_and_len_padding( unsigned char *output,
675 size_t output_len, size_t data_len )
676{
677 size_t padding_len = output_len - data_len;
678 unsigned char i = 0;
679
680 for( i = 1; i < padding_len; i++ )
681 output[data_len + i - 1] = 0x00;
682 output[output_len - 1] = (unsigned char) padding_len;
683}
684
685static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
686 size_t *data_len )
687{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100688 size_t i, pad_idx;
689 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200690
691 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200693
694 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200695 *data_len = input_len - padding_len;
696
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100697 /* Avoid logical || since it results in a branch */
698 bad |= padding_len > input_len;
699 bad |= padding_len == 0;
700
701 /* The number of bytes checked must be independent of padding_len */
702 pad_idx = input_len - padding_len;
703 for( i = 0; i < input_len - 1; i++ )
704 bad |= input[i] * ( i >= pad_idx );
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200711/*
712 * Zero padding: fill with 00 ... 00
713 */
714static void add_zeros_padding( unsigned char *output,
715 size_t output_len, size_t data_len )
716{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200717 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200718
719 for( i = data_len; i < output_len; i++ )
720 output[i] = 0x00;
721}
722
723static int get_zeros_padding( unsigned char *input, size_t input_len,
724 size_t *data_len )
725{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100726 size_t i;
727 unsigned char done = 0, prev_done;
728
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200729 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200731
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100732 *data_len = 0;
733 for( i = input_len; i > 0; i-- )
734 {
735 prev_done = done;
736 done |= ( input[i-1] != 0 );
737 *data_len |= i * ( done != prev_done );
738 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200739
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200740 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200743
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200744/*
745 * No padding: don't pad :)
746 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200748 * but a trivial get_padding function
749 */
750static int get_no_padding( unsigned char *input, size_t input_len,
751 size_t *data_len )
752{
753 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200755
756 *data_len = input_len;
757
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200758 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200763 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764{
k-stachowiaka5390702018-12-17 11:27:03 +0100765 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100766 CIPHER_VALIDATE_RET( output != NULL );
767 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100768 if( ctx->cipher_info == NULL )
769 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100770
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771 *olen = 0;
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100774 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
776 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100777 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000779 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200780 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000781 }
782
Daniel King8fe47012016-05-17 20:33:28 -0300783 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
784 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300785 {
786 return( 0 );
787 }
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200790 {
791 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200793
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200794 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200795 }
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_CIPHER_MODE_CBC)
798 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200800 int ret = 0;
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000803 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200804 /* check for 'no padding' mode */
805 if( NULL == ctx->add_padding )
806 {
807 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200809
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200810 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000814 ctx->unprocessed_len );
815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000817 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200818 /*
819 * For decrypt operations, expect a full block,
820 * or an empty block if no padding
821 */
822 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200823 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000826 }
827
828 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000829 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000831 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000832 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200833 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834 }
835
836 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 if( MBEDTLS_DECRYPT == ctx->operation )
838 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200839 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000840
841 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200843 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000844 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200845#else
846 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850}
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100853int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
854 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200855{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100856 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100857 if( ctx->cipher_info == NULL )
858 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100859
860 if( MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200863 }
864
Paul Bakker1a45d912013-08-14 12:04:26 +0200865 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
868 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200869 ctx->add_padding = add_pkcs_padding;
870 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200871 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
874 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200875 ctx->add_padding = add_one_and_zeros_padding;
876 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200877 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
880 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200881 ctx->add_padding = add_zeros_and_len_padding;
882 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200883 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200884#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
886 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200887 ctx->add_padding = add_zeros_padding;
888 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200889 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200892 ctx->add_padding = NULL;
893 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200894 break;
895
896 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200898 }
899
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200900 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200901}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200903
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200904#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200906 unsigned char *tag, size_t tag_len )
907{
k-stachowiaka5390702018-12-17 11:27:03 +0100908 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100909 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100910 if( ctx->cipher_info == NULL )
911 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 if( MBEDTLS_ENCRYPT != ctx->operation )
914 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200915
Daniel King8fe47012016-05-17 20:33:28 -0300916#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
918 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Daniel King8fe47012016-05-17 20:33:28 -0300919#endif
920
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200921#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300922 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
923 {
924 /* Don't allow truncated MAC for Poly1305 */
925 if ( tag_len != 16U )
926 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
927
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200928 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300929 tag );
930 }
931#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200932
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200933 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200934}
Paul Bakker9af723c2014-05-01 13:03:14 +0200935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200937 const unsigned char *tag, size_t tag_len )
938{
Daniel King8fe47012016-05-17 20:33:28 -0300939 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200940 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200941
k-stachowiaka5390702018-12-17 11:27:03 +0100942 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100943 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100944 if( ctx->cipher_info == NULL )
945 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100946
k-stachowiaka5390702018-12-17 11:27:03 +0100947 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200950 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200951
Daniel King8fe47012016-05-17 20:33:28 -0300952#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200954 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200955 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200959 check_tag, tag_len ) ) )
960 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200961 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200962 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200963
964 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300965 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200967
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200968 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200969 }
Daniel King8fe47012016-05-17 20:33:28 -0300970#endif /* MBEDTLS_GCM_C */
971
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200972#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300973 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
974 {
975 /* Don't allow truncated MAC for Poly1305 */
976 if ( tag_len != sizeof( check_tag ) )
977 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
978
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200979 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300980 check_tag );
981 if ( ret != 0 )
982 {
983 return( ret );
984 }
985
986 /* Check the tag in "constant-time" */
987 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
988 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
989
990 return( 0 );
991 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200992#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200993
994 return( 0 );
995}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200996#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200997
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200998/*
999 * Packet-oriented wrapper for non-AEAD modes
1000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001002 const unsigned char *iv, size_t iv_len,
1003 const unsigned char *input, size_t ilen,
1004 unsigned char *output, size_t *olen )
1005{
1006 int ret;
1007 size_t finish_olen;
1008
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001009 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001010 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001011 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001012 CIPHER_VALIDATE_RET( output != NULL );
1013 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +01001014 if( ctx->cipher_info == NULL )
1015 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001018 return( ret );
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001021 return( ret );
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001024 return( ret );
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001027 return( ret );
1028
1029 *olen += finish_olen;
1030
1031 return( 0 );
1032}
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001035/*
1036 * Packet-oriented encryption for AEAD modes
1037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001039 const unsigned char *iv, size_t iv_len,
1040 const unsigned char *ad, size_t ad_len,
1041 const unsigned char *input, size_t ilen,
1042 unsigned char *output, size_t *olen,
1043 unsigned char *tag, size_t tag_len )
1044{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001045 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001046 CIPHER_VALIDATE_RET( iv != NULL );
1047 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001048 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001049 CIPHER_VALIDATE_RET( output != NULL );
1050 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001051 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +01001052 if( ctx->cipher_info == NULL )
1053 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#if defined(MBEDTLS_GCM_C)
1056 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001057 {
1058 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001060 iv, iv_len, ad, ad_len, input, output,
1061 tag_len, tag ) );
1062 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_GCM_C */
1064#if defined(MBEDTLS_CCM_C)
1065 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001066 {
1067 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001069 iv, iv_len, ad, ad_len, input, output,
1070 tag, tag_len ) );
1071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001073#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001074 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1075 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001076 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001077 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001078 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001079 {
1080 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1081 }
1082
1083 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001084 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001085 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001086 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001087#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001090}
1091
1092/*
1093 * Packet-oriented decryption for AEAD modes
1094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001096 const unsigned char *iv, size_t iv_len,
1097 const unsigned char *ad, size_t ad_len,
1098 const unsigned char *input, size_t ilen,
1099 unsigned char *output, size_t *olen,
1100 const unsigned char *tag, size_t tag_len )
1101{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001102 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001103 CIPHER_VALIDATE_RET( iv != NULL );
1104 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001105 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001106 CIPHER_VALIDATE_RET( output != NULL );
1107 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001108 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +01001109 if( ctx->cipher_info == NULL )
1110 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_GCM_C)
1113 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001114 {
1115 int ret;
1116
1117 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001119 iv, iv_len, ad, ad_len,
1120 tag, tag_len, input, output );
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1123 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001124
1125 return( ret );
1126 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_GCM_C */
1128#if defined(MBEDTLS_CCM_C)
1129 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001130 {
1131 int ret;
1132
1133 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001135 iv, iv_len, ad, ad_len,
1136 input, output, tag, tag_len );
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1139 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001140
1141 return( ret );
1142 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001144#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001145 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1146 {
Daniel King8fe47012016-05-17 20:33:28 -03001147 int ret;
1148
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001149 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001150 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001151 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001152 {
1153 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1154 }
1155
1156 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001157 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1158 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001159
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001160 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1161 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001162
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001163 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001164 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001165#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_CIPHER_C */