blob: 8d010b59ac4e27a8c1884e4b5f839f4bbf0cb7a4 [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
k-stachowiak1a9df6b2018-12-19 16:52:45 +010089 return( (int)diff );
Daniel King8fe47012016-05-17 20:33:28 -030090}
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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100186 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100219 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 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100238 return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
239 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100243 return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
244 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100258 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100300 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100314 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 {
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100319 return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
320 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
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100340 return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
341 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 )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100360 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 );
Gilles Peskine83a56722020-01-21 15:02:14 +0100364 if ( 0 == block_size )
365 {
366 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
367 }
Paul Bakker6c212762013-12-16 15:24:50 +0100368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200370 {
Janos Follath98e28a72016-05-31 14:03:54 +0100371 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200373
374 *olen = ilen;
375
376 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
377 ctx->operation, input, output ) ) )
378 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200379 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200380 }
381
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200382 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200383 }
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_GCM_C)
386 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200387 {
388 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100389 return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
390 output ) );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200391 }
392#endif
393
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200394#if defined(MBEDTLS_CHACHAPOLY_C)
395 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
396 {
397 *olen = ilen;
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100398 return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
399 ilen, input, output ) );
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200400 }
401#endif
402
Paul Bakker68884e32013-01-07 18:20:04 +0100403 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100404 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100407 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#if defined(MBEDTLS_CIPHER_MODE_CBC)
410 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200412 size_t copy_len = 0;
413
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414 /*
415 * If there is not enough data for a full block, cache it.
416 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700417 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000418 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700419 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
420 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000422 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423 {
424 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
425 ilen );
426
427 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200428 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429 }
430
431 /*
432 * Process cached data first
433 */
Janos Follath98e28a72016-05-31 14:03:54 +0100434 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435 {
Janos Follath98e28a72016-05-31 14:03:54 +0100436 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437
438 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
439 copy_len );
440
Paul Bakkerff61a782011-06-09 15:42:02 +0000441 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100442 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000443 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200445 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446 }
447
Janos Follath98e28a72016-05-31 14:03:54 +0100448 *olen += block_size;
449 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 ctx->unprocessed_len = 0;
451
452 input += copy_len;
453 ilen -= copy_len;
454 }
455
456 /*
457 * Cache final, incomplete block
458 */
459 if( 0 != ilen )
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 );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100760 CIPHER_VALIDATE_RET( output != NULL );
761 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100762 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100763 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100764
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765 *olen = 0;
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100768 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
770 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100771 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000773 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200774 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000775 }
776
Daniel King8fe47012016-05-17 20:33:28 -0300777 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
778 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300779 {
780 return( 0 );
781 }
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200784 {
785 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200787
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200788 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200789 }
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_CIPHER_MODE_CBC)
792 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000793 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200794 int ret = 0;
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200798 /* check for 'no padding' mode */
799 if( NULL == ctx->add_padding )
800 {
801 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200803
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200804 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200805 }
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000808 ctx->unprocessed_len );
809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200812 /*
813 * For decrypt operations, expect a full block,
814 * or an empty block if no padding
815 */
816 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200817 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000820 }
821
822 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000823 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000825 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000826 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200827 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000828 }
829
830 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 if( MBEDTLS_DECRYPT == ctx->operation )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100832 return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
833 olen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000834
835 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200837 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000838 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200839#else
840 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000844}
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100847int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
848 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200849{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100850 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100851
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100852 if( NULL == ctx->cipher_info || 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 );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100901 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100902 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100903 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( MBEDTLS_ENCRYPT != ctx->operation )
906 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200907
Daniel King8fe47012016-05-17 20:33:28 -0300908#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100910 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
911 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300912#endif
913
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200914#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300915 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
916 {
917 /* Don't allow truncated MAC for Poly1305 */
918 if ( tag_len != 16U )
919 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
920
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100921 return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
922 tag ) );
Daniel King8fe47012016-05-17 20:33:28 -0300923 }
924#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200925
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200926 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200927}
Paul Bakker9af723c2014-05-01 13:03:14 +0200928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200930 const unsigned char *tag, size_t tag_len )
931{
Daniel King8fe47012016-05-17 20:33:28 -0300932 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200933 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200934
k-stachowiaka5390702018-12-17 11:27:03 +0100935 CIPHER_VALIDATE_RET( ctx != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +0100936 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak95070a82018-12-19 14:48:37 +0100937 if( ctx->cipher_info == NULL )
k-stachowiak1a9df6b2018-12-19 16:52:45 +0100938 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100939
k-stachowiaka5390702018-12-17 11:27:03 +0100940 if( MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200943 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200944
Daniel King8fe47012016-05-17 20:33:28 -0300945#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200947 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200948 if( tag_len > sizeof( check_tag ) )
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200952 check_tag, tag_len ) ) )
953 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200954 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200955 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200956
957 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -0300958 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200960
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200961 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200962 }
Daniel King8fe47012016-05-17 20:33:28 -0300963#endif /* MBEDTLS_GCM_C */
964
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200965#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300966 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
967 {
968 /* Don't allow truncated MAC for Poly1305 */
969 if ( tag_len != sizeof( check_tag ) )
970 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
971
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200972 ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300973 check_tag );
974 if ( ret != 0 )
975 {
976 return( ret );
977 }
978
979 /* Check the tag in "constant-time" */
980 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
981 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
982
983 return( 0 );
984 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200985#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200986
987 return( 0 );
988}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200989#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200990
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200991/*
992 * Packet-oriented wrapper for non-AEAD modes
993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200995 const unsigned char *iv, size_t iv_len,
996 const unsigned char *input, size_t ilen,
997 unsigned char *output, size_t *olen )
998{
999 int ret;
1000 size_t finish_olen;
1001
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001002 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001003 CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001004 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001005 CIPHER_VALIDATE_RET( output != NULL );
1006 CIPHER_VALIDATE_RET( olen != NULL );
1007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001009 return( ret );
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001012 return( ret );
1013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001015 return( ret );
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001018 return( ret );
1019
1020 *olen += finish_olen;
1021
1022 return( 0 );
1023}
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001026/*
1027 * Packet-oriented encryption for AEAD modes
1028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001030 const unsigned char *iv, size_t iv_len,
1031 const unsigned char *ad, size_t ad_len,
1032 const unsigned char *input, size_t ilen,
1033 unsigned char *output, size_t *olen,
1034 unsigned char *tag, size_t tag_len )
1035{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001036 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001037 CIPHER_VALIDATE_RET( iv != NULL );
1038 CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001039 CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001040 CIPHER_VALIDATE_RET( output != NULL );
1041 CIPHER_VALIDATE_RET( olen != NULL );
k-stachowiakc29d94c2018-12-18 15:09:56 +01001042 CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_GCM_C)
1045 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001046 {
1047 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001049 iv, iv_len, ad, ad_len, input, output,
1050 tag_len, tag ) );
1051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_GCM_C */
1053#if defined(MBEDTLS_CCM_C)
1054 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001055 {
1056 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001058 iv, iv_len, ad, ad_len, input, output,
1059 tag, tag_len ) );
1060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001062#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001063 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1064 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001065 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001066 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001067 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001068 {
1069 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1070 }
1071
1072 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001073 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001074 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001075 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001076#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001079}
1080
1081/*
1082 * Packet-oriented decryption for AEAD modes
1083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001085 const unsigned char *iv, size_t iv_len,
1086 const unsigned char *ad, size_t ad_len,
1087 const unsigned char *input, size_t ilen,
1088 unsigned char *output, size_t *olen,
1089 const unsigned char *tag, size_t tag_len )
1090{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001091 CIPHER_VALIDATE_RET( ctx != NULL );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +01001092 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 */