blob: 34a8170bc2a608c985b71a2c91d1aa512530de64 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020035#include "mbedtls/cipher_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.h>
40
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020041#if defined(MBEDTLS_CHACHAPOLY_C)
42#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020051#endif
52
Daniel Kingbd920622016-05-15 19:56:20 -030053#if defined(MBEDTLS_CHACHA20_C)
54#include "mbedtls/chacha20.h"
55#endif
56
Simon Butcher327398a2016-10-05 14:09:11 +010057#if defined(MBEDTLS_CMAC_C)
58#include "mbedtls/cmac.h"
59#endif
60
Hanno Becker098c9de2018-11-09 16:10:57 +000061#if defined(MBEDTLS_USE_PSA_CRYPTO)
62#include "psa/crypto.h"
63#endif /* MBEDTLS_USE_PSA_CRYPTO */
64
Simon Butcher327398a2016-10-05 14:09:11 +010065#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#define mbedtls_calloc calloc
69#define mbedtls_free free
70#endif
71
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020072#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -030073/* Compare the contents of two buffers in constant time.
74 * Returns 0 if the contents are bitwise identical, otherwise returns
Daniel King16b04ce2016-05-18 13:38:22 -030075 * a non-zero value.
76 * This is currently only used by GCM and ChaCha20+Poly1305.
77 */
Hanno Becker21967c52018-11-09 16:36:33 +000078static int mbedtls_constant_time_memcmp( const void *v1, const void *v2,
79 size_t len )
Daniel King8fe47012016-05-17 20:33:28 -030080{
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
Hanno Becker21967c52018-11-09 16:36:33 +0000116const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
117 const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200122 if( def->type == cipher_type )
123 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +0000124
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200125 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126}
127
Hanno Becker21967c52018-11-09 16:36:33 +0000128const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
129 const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200132
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200134 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200137 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200138 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000139
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200140 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141}
142
Hanno Becker21967c52018-11-09 16:36:33 +0000143const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
144 const mbedtls_cipher_id_t cipher_id,
145 int key_bitlen,
146 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200151 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200152 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200153 def->info->mode == mode )
154 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200155
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200156 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200157}
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200165{
166 if( ctx == NULL )
167 return;
168
Simon Butcher327398a2016-10-05 14:09:11 +0100169#if defined(MBEDTLS_CMAC_C)
170 if( ctx->cmac_ctx )
171 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500172 mbedtls_platform_zeroize( ctx->cmac_ctx,
173 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100174 mbedtls_free( ctx->cmac_ctx );
175 }
176#endif
177
Paul Bakker84bbeb52014-07-01 14:53:22 +0200178 if( ctx->cipher_ctx )
179 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
180
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500181 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200182}
183
Hanno Becker21967c52018-11-09 16:36:33 +0000184int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
185 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186{
187 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191
Paul Bakker343a8702011-06-09 14:27:58 +0000192 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194
195 ctx->cipher_info = cipher_info;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200198 /*
199 * Ignore possible errors caused by a cipher mode that doesn't use padding
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
202 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200203#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200207
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209}
210
Hanno Becker098c9de2018-11-09 16:10:57 +0000211#if defined(MBEDTLS_USE_PSA_CRYPTO)
212int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
213 const mbedtls_cipher_info_t *cipher_info )
214{
215 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
216}
217#endif /* MBEDTLS_USE_PSA_CRYPTO */
218
Hanno Becker21967c52018-11-09 16:36:33 +0000219int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
220 const unsigned char *key,
221 int key_bitlen,
222 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223{
224 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200228 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200231 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200232
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200233 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000234 ctx->operation = operation;
235
Paul Bakker343a8702011-06-09 14:27:58 +0000236 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100237 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( MBEDTLS_ENCRYPT == operation ||
240 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100241 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000243 {
244 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000245 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000246 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000249 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Hanno Becker21967c52018-11-09 16:36:33 +0000250 ctx->key_bitlen );
251
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254}
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200257 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200259 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300260 if( NULL == ctx || NULL == ctx->cipher_info )
261 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
262 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300265 if( NULL == iv && iv_len == 0 )
266 ctx->iv_size = 0;
267
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200268 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
270 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200273 actual_iv_size = iv_len;
274 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200275 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200276 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200277
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200278 /* avoid reading past the end of input buffer */
279 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200281 }
282
Daniel Kingbd920622016-05-15 19:56:20 -0300283#if defined(MBEDTLS_CHACHA20_C)
284 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
285 {
286 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
287 iv,
288 0U ) ) /* Initial counter value */
289 {
290 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
291 }
292 }
293#endif
294
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300295 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300296 {
297 memcpy( ctx->iv, iv, actual_iv_size );
298 ctx->iv_size = actual_iv_size;
299 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200300
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200301 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200302}
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200305{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200306 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200308
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309 ctx->unprocessed_len = 0;
310
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200311 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200312}
313
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200314#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200316 const unsigned char *ad, size_t ad_len )
317{
318 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200320
Daniel King8fe47012016-05-17 20:33:28 -0300321#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200325 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200326 }
Daniel King8fe47012016-05-17 20:33:28 -0300327#endif
328
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200329#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300330 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
331 {
332 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200333 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300334
335 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200336 ? MBEDTLS_CHACHAPOLY_ENCRYPT
337 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300338
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200339 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300340 ctx->iv,
341 mode );
342 if ( result != 0 )
343 return( result );
344
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200345 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200346 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300347 }
348#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200349
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200350 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200352#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200355 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356{
Paul Bakkerff61a782011-06-09 15:42:02 +0000357 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100358 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359
Paul Bakker68884e32013-01-07 18:20:04 +0100360 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000363 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000364
Paul Bakker6c212762013-12-16 15:24:50 +0100365 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100366 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200369 {
Janos Follath98e28a72016-05-31 14:03:54 +0100370 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200372
373 *olen = ilen;
374
375 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
376 ctx->operation, input, output ) ) )
377 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200378 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200379 }
380
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200381 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200382 }
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_GCM_C)
385 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200386 {
387 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200389 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200390 }
391#endif
392
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200393#if defined(MBEDTLS_CHACHAPOLY_C)
394 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
395 {
396 *olen = ilen;
397 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
398 ilen, input, output );
399 }
400#endif
401
Janos Follath98e28a72016-05-31 14:03:54 +0100402 if ( 0 == block_size )
403 {
404 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
405 }
406
Paul Bakker68884e32013-01-07 18:20:04 +0100407 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100408 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100411 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_CIPHER_MODE_CBC)
414 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000415 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200416 size_t copy_len = 0;
417
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418 /*
419 * If there is not enough data for a full block, cache it.
420 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700421 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000422 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700423 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
424 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000426 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 {
428 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
429 ilen );
430
431 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200432 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433 }
434
435 /*
436 * Process cached data first
437 */
Janos Follath98e28a72016-05-31 14:03:54 +0100438 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439 {
Janos Follath98e28a72016-05-31 14:03:54 +0100440 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441
442 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
443 copy_len );
444
Paul Bakkerff61a782011-06-09 15:42:02 +0000445 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100446 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000447 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200449 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 }
451
Janos Follath98e28a72016-05-31 14:03:54 +0100452 *olen += block_size;
453 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 ctx->unprocessed_len = 0;
455
456 input += copy_len;
457 ilen -= copy_len;
458 }
459
460 /*
461 * Cache final, incomplete block
462 */
463 if( 0 != ilen )
464 {
Janos Follath98e28a72016-05-31 14:03:54 +0100465 if( 0 == block_size )
466 {
467 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
468 }
469
Andy Leiserson79e77892017-04-28 20:01:49 -0700470 /* Encryption: only cache partial blocks
471 * Decryption w/ padding: always keep at least one whole block
472 * Decryption w/o padding: only cache partial blocks
473 */
Janos Follath98e28a72016-05-31 14:03:54 +0100474 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700475 if( copy_len == 0 &&
476 ctx->operation == MBEDTLS_DECRYPT &&
477 NULL != ctx->add_padding)
478 {
Janos Follath98e28a72016-05-31 14:03:54 +0100479 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700480 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481
482 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
483 copy_len );
484
485 ctx->unprocessed_len += copy_len;
486 ilen -= copy_len;
487 }
488
489 /*
490 * Process remaining full blocks
491 */
492 if( ilen )
493 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000494 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
495 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200499
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500 *olen += ilen;
501 }
502
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_CIPHER_MODE_CFB)
508 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000509 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000510 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000511 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000512 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000513 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200514 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000515 }
516
517 *olen = ilen;
518
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200519 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000522
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100523#if defined(MBEDTLS_CIPHER_MODE_OFB)
524 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
525 {
526 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
527 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
528 {
529 return( ret );
530 }
531
532 *olen = ilen;
533
534 return( 0 );
535 }
536#endif /* MBEDTLS_CIPHER_MODE_OFB */
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_CIPHER_MODE_CTR)
539 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000540 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000541 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000542 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000543 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000544 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200545 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000546 }
547
548 *olen = ilen;
549
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200550 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000553
Jaeden Ameroc6539902018-04-30 17:17:41 +0100554#if defined(MBEDTLS_CIPHER_MODE_XTS)
555 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
556 {
557 if( ctx->unprocessed_len > 0 ) {
558 /* We can only process an entire data unit at a time. */
559 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
560 }
561
562 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
563 ctx->operation, ilen, ctx->iv, input, output );
564 if( ret != 0 )
565 {
566 return( ret );
567 }
568
569 *olen = ilen;
570
571 return( 0 );
572 }
573#endif /* MBEDTLS_CIPHER_MODE_XTS */
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_CIPHER_MODE_STREAM)
576 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200577 {
578 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
579 ilen, input, output ) ) )
580 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200581 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200582 }
583
584 *olen = ilen;
585
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200586 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200587 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591}
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
594#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200595/*
596 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
597 */
Paul Bakker23986e52011-04-24 08:57:21 +0000598static void add_pkcs_padding( unsigned char *output, size_t output_len,
599 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600{
Paul Bakker23986e52011-04-24 08:57:21 +0000601 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100602 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
604 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000605 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606}
607
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200608static int get_pkcs_padding( unsigned char *input, size_t input_len,
609 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100611 size_t i, pad_idx;
612 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
Paul Bakkera885d682011-01-20 16:35:05 +0000614 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616
617 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618 *data_len = input_len - padding_len;
619
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100620 /* Avoid logical || since it results in a branch */
621 bad |= padding_len > input_len;
622 bad |= padding_len == 0;
623
624 /* The number of bytes checked must be independent of padding_len,
625 * so pick input_len, which is usually 8 or 16 (one block) */
626 pad_idx = input_len - padding_len;
627 for( i = 0; i < input_len; i++ )
628 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200635/*
636 * One and zeros padding: fill with 80 00 ... 00
637 */
638static void add_one_and_zeros_padding( unsigned char *output,
639 size_t output_len, size_t data_len )
640{
641 size_t padding_len = output_len - data_len;
642 unsigned char i = 0;
643
644 output[data_len] = 0x80;
645 for( i = 1; i < padding_len; i++ )
646 output[data_len + i] = 0x00;
647}
648
649static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
650 size_t *data_len )
651{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100652 size_t i;
653 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200654
655 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200657
Micha Krausba8316f2017-12-23 23:40:08 +0100658 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100659 *data_len = 0;
660 for( i = input_len; i > 0; i-- )
661 {
662 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100663 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100664 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100665 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100666 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200669
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200674/*
675 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
676 */
677static void add_zeros_and_len_padding( unsigned char *output,
678 size_t output_len, size_t data_len )
679{
680 size_t padding_len = output_len - data_len;
681 unsigned char i = 0;
682
683 for( i = 1; i < padding_len; i++ )
684 output[data_len + i - 1] = 0x00;
685 output[output_len - 1] = (unsigned char) padding_len;
686}
687
688static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
689 size_t *data_len )
690{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100691 size_t i, pad_idx;
692 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200693
694 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200696
697 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200698 *data_len = input_len - padding_len;
699
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100700 /* Avoid logical || since it results in a branch */
701 bad |= padding_len > input_len;
702 bad |= padding_len == 0;
703
704 /* The number of bytes checked must be independent of padding_len */
705 pad_idx = input_len - padding_len;
706 for( i = 0; i < input_len - 1; i++ )
707 bad |= input[i] * ( i >= pad_idx );
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200710}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200714/*
715 * Zero padding: fill with 00 ... 00
716 */
717static void add_zeros_padding( unsigned char *output,
718 size_t output_len, size_t data_len )
719{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200720 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200721
722 for( i = data_len; i < output_len; i++ )
723 output[i] = 0x00;
724}
725
726static int get_zeros_padding( unsigned char *input, size_t input_len,
727 size_t *data_len )
728{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100729 size_t i;
730 unsigned char done = 0, prev_done;
731
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200732 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200734
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100735 *data_len = 0;
736 for( i = input_len; i > 0; i-- )
737 {
738 prev_done = done;
739 done |= ( input[i-1] != 0 );
740 *data_len |= i * ( done != prev_done );
741 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200742
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200743 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200744}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200746
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200747/*
748 * No padding: don't pad :)
749 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200751 * but a trivial get_padding function
752 */
753static int get_no_padding( unsigned char *input, size_t input_len,
754 size_t *data_len )
755{
756 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200758
759 *data_len = input_len;
760
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200761 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200766 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767{
768 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000770
771 *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)
Hanno Becker21967c52018-11-09 16:36:33 +0000853int 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{
856 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200860 }
861
Paul Bakker1a45d912013-08-14 12:04:26 +0200862 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
865 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200866 ctx->add_padding = add_pkcs_padding;
867 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200868 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
871 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200872 ctx->add_padding = add_one_and_zeros_padding;
873 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200874 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200875#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
877 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200878 ctx->add_padding = add_zeros_and_len_padding;
879 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200880 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
883 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200884 ctx->add_padding = add_zeros_padding;
885 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200886 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200889 ctx->add_padding = NULL;
890 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200891 break;
892
893 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895 }
896
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200897 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200898}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200900
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200901#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200903 unsigned char *tag, size_t tag_len )
904{
905 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 if( MBEDTLS_ENCRYPT != ctx->operation )
909 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200910
Daniel King8fe47012016-05-17 20:33:28 -0300911#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker21967c52018-11-09 16:36:33 +0000913 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
914 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -0300915#endif
916
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200917#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300918 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
919 {
920 /* Don't allow truncated MAC for Poly1305 */
921 if ( tag_len != 16U )
922 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
923
Hanno Becker21967c52018-11-09 16:36:33 +0000924 return( mbedtls_chachapoly_finish(
925 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -0300926 }
927#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200928
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200929 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200930}
Paul Bakker9af723c2014-05-01 13:03:14 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200933 const unsigned char *tag, size_t tag_len )
934{
Daniel King8fe47012016-05-17 20:33:28 -0300935 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200936 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200937
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200938 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200942 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200943
Daniel King8fe47012016-05-17 20:33:28 -0300944#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200946 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200947 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200949
Hanno Becker21967c52018-11-09 16:36:33 +0000950 if( 0 != ( ret = mbedtls_gcm_finish(
951 (mbedtls_gcm_context *) ctx->cipher_ctx,
952 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200953 {
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
Hanno Becker21967c52018-11-09 16:36:33 +0000972 ret = mbedtls_chachapoly_finish(
973 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -0300974 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001003 return( ret );
1004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001006 return( ret );
1007
Hanno Becker21967c52018-11-09 16:36:33 +00001008 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1009 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001010 return( ret );
1011
Hanno Becker21967c52018-11-09 16:36:33 +00001012 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1013 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001014 return( ret );
1015
1016 *olen += finish_olen;
1017
1018 return( 0 );
1019}
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001022/*
1023 * Packet-oriented encryption for AEAD modes
1024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001026 const unsigned char *iv, size_t iv_len,
1027 const unsigned char *ad, size_t ad_len,
1028 const unsigned char *input, size_t ilen,
1029 unsigned char *output, size_t *olen,
1030 unsigned char *tag, size_t tag_len )
1031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_GCM_C)
1033 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001034 {
1035 *olen = ilen;
Hanno Becker21967c52018-11-09 16:36:33 +00001036 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1037 ilen, iv, iv_len, ad, ad_len,
1038 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#endif /* MBEDTLS_GCM_C */
1041#if defined(MBEDTLS_CCM_C)
1042 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001043 {
1044 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001046 iv, iv_len, ad, ad_len, input, output,
1047 tag, tag_len ) );
1048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001050#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001051 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1052 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001053 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001054 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001055 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001056 {
1057 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1058 }
1059
1060 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001061 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001062 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001063 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001064#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001067}
1068
1069/*
1070 * Packet-oriented decryption for AEAD modes
1071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001073 const unsigned char *iv, size_t iv_len,
1074 const unsigned char *ad, size_t ad_len,
1075 const unsigned char *input, size_t ilen,
1076 unsigned char *output, size_t *olen,
1077 const unsigned char *tag, size_t tag_len )
1078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#if defined(MBEDTLS_GCM_C)
1080 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001081 {
1082 int ret;
1083
1084 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001086 iv, iv_len, ad, ad_len,
1087 tag, tag_len, input, output );
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1090 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001091
1092 return( ret );
1093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#endif /* MBEDTLS_GCM_C */
1095#if defined(MBEDTLS_CCM_C)
1096 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001097 {
1098 int ret;
1099
1100 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001102 iv, iv_len, ad, ad_len,
1103 input, output, tag, tag_len );
1104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1106 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001107
1108 return( ret );
1109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001111#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001112 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1113 {
Daniel King8fe47012016-05-17 20:33:28 -03001114 int ret;
1115
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001116 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001117 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001118 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001119 {
1120 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1121 }
1122
1123 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001124 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1125 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001126
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001127 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1128 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001129
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001130 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001131 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001132#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#endif /* MBEDTLS_CIPHER_C */