blob: 0bff79ee7399957db15ff8aa8b7993f2c5c2e93c [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 Becker4ccfc402018-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 Becker18597cd2018-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 Becker18597cd2018-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 Becker18597cd2018-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 Becker18597cd2018-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
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000169#if defined(MBEDTLS_USE_PSA_CRYPTO)
170 if( ctx->psa_enabled == 1 )
171 {
Hanno Becker6118e432018-11-09 16:47:20 +0000172 if( ctx->cipher_ctx != NULL )
173 {
174 mbedtls_cipher_context_psa * const cipher_psa =
175 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
176
177 if( cipher_psa->slot_state == 1 )
178 {
179 /* TODO: Destroy PSA key */
180 }
181
182 mbedtls_platform_zeroize( cipher_psa, sizeof( *cipher_psa ) );
183 mbedtls_free( cipher_psa );
184 }
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000185
186 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
187 return;
188 }
189#endif /* MBEDTLS_USE_PSA_CRYPTO */
190
Simon Butcher327398a2016-10-05 14:09:11 +0100191#if defined(MBEDTLS_CMAC_C)
192 if( ctx->cmac_ctx )
193 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500194 mbedtls_platform_zeroize( ctx->cmac_ctx,
195 sizeof( mbedtls_cmac_context_t ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100196 mbedtls_free( ctx->cmac_ctx );
197 }
198#endif
199
Paul Bakker84bbeb52014-07-01 14:53:22 +0200200 if( ctx->cipher_ctx )
201 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
202
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500203 mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200204}
205
Hanno Becker18597cd2018-11-09 16:36:33 +0000206int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
207 const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208{
209 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213
Paul Bakker343a8702011-06-09 14:27:58 +0000214 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216
217 ctx->cipher_info = cipher_info;
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200220 /*
221 * Ignore possible errors caused by a cipher mode that doesn't use padding
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
224 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200225#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200229
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231}
232
Hanno Becker4ccfc402018-11-09 16:10:57 +0000233#if defined(MBEDTLS_USE_PSA_CRYPTO)
234int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
235 const mbedtls_cipher_info_t *cipher_info )
236{
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000237 if( NULL == cipher_info || NULL == ctx )
238 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
239
Hanno Becker6118e432018-11-09 16:47:20 +0000240 ctx->cipher_ctx = mbedtls_calloc( 1, sizeof(mbedtls_cipher_context_psa ) );
241 if( ctx->cipher_ctx == NULL )
242 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
243
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000244 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
245
246 ctx->cipher_info = cipher_info;
247 ctx->psa_enabled = 1;
248 return( 0 );
Hanno Becker4ccfc402018-11-09 16:10:57 +0000249}
250#endif /* MBEDTLS_USE_PSA_CRYPTO */
251
Hanno Becker18597cd2018-11-09 16:36:33 +0000252int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
253 const unsigned char *key,
254 int key_bitlen,
255 const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256{
257 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000260#if defined(MBEDTLS_USE_PSA_CRYPTO)
261 if( ctx->psa_enabled == 1 )
262 {
Hanno Becker6118e432018-11-09 16:47:20 +0000263 /* TODO: Allocate and setup PSA key slot from raw key material. */
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000264 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
265 }
266#endif /* MBEDTLS_USE_PSA_CRYPTO */
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200269 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200272 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200273
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200274 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275 ctx->operation = operation;
276
Paul Bakker343a8702011-06-09 14:27:58 +0000277 /*
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100278 * For OFB, CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( MBEDTLS_ENCRYPT == operation ||
281 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100282 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000284 {
285 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Hanno Becker18597cd2018-11-09 16:36:33 +0000286 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000287 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000290 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Hanno Becker18597cd2018-11-09 16:36:33 +0000291 ctx->key_bitlen );
292
Paul Bakker8123e9d2011-01-06 15:37:30 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200298 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200300 size_t actual_iv_size;
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300301 if( NULL == ctx || NULL == ctx->cipher_info )
302 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
303 else if( NULL == iv && iv_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000306#if defined(MBEDTLS_USE_PSA_CRYPTO)
307 if( ctx->psa_enabled == 1 )
308 {
309 /* While PSA Crypto has an API for multipart
310 * operations, we currently don't make it
311 * accessible through the cipher layer. */
312 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
313 }
314#endif /* MBEDTLS_USE_PSA_CRYPTO */
315
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300316 if( NULL == iv && iv_len == 0 )
317 ctx->iv_size = 0;
318
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200319 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
321 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200324 actual_iv_size = iv_len;
325 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200326 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200327 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200328
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200329 /* avoid reading past the end of input buffer */
330 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200332 }
333
Daniel Kingbd920622016-05-15 19:56:20 -0300334#if defined(MBEDTLS_CHACHA20_C)
335 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
336 {
337 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
338 iv,
339 0U ) ) /* Initial counter value */
340 {
341 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
342 }
343 }
344#endif
345
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300346 if ( actual_iv_size != 0 )
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300347 {
348 memcpy( ctx->iv, iv, actual_iv_size );
349 ctx->iv_size = actual_iv_size;
350 }
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200351
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200352 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200353}
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200356{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200357 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200359
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000360#if defined(MBEDTLS_USE_PSA_CRYPTO)
361 if( ctx->psa_enabled == 1 )
362 {
363 /* We don't support resetting PSA-based
364 * cipher contexts, yet. */
365 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
366 }
367#endif /* MBEDTLS_USE_PSA_CRYPTO */
368
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369 ctx->unprocessed_len = 0;
370
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200371 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200372}
373
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200374#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200376 const unsigned char *ad, size_t ad_len )
377{
378 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200380
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000381#if defined(MBEDTLS_USE_PSA_CRYPTO)
382 if( ctx->psa_enabled == 1 )
383 {
384 /* While PSA Crypto has an API for multipart
385 * operations, we currently don't make it
386 * accessible through the cipher layer. */
387 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
388 }
389#endif /* MBEDTLS_USE_PSA_CRYPTO */
390
Daniel King8fe47012016-05-17 20:33:28 -0300391#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200395 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200396 }
Daniel King8fe47012016-05-17 20:33:28 -0300397#endif
398
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200399#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -0300400 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
401 {
402 int result;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200403 mbedtls_chachapoly_mode_t mode;
Daniel King8fe47012016-05-17 20:33:28 -0300404
405 mode = ( ctx->operation == MBEDTLS_ENCRYPT )
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200406 ? MBEDTLS_CHACHAPOLY_ENCRYPT
407 : MBEDTLS_CHACHAPOLY_DECRYPT;
Daniel King8fe47012016-05-17 20:33:28 -0300408
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200409 result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Daniel King8fe47012016-05-17 20:33:28 -0300410 ctx->iv,
411 mode );
412 if ( result != 0 )
413 return( result );
414
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200415 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
Manuel Pégourié-Gonnard5ef92d32018-05-09 09:34:25 +0200416 ad, ad_len );
Daniel King8fe47012016-05-17 20:33:28 -0300417 }
418#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200419
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200420 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000421}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200422#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200425 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426{
Paul Bakkerff61a782011-06-09 15:42:02 +0000427 int ret;
Janos Follath98e28a72016-05-31 14:03:54 +0100428 size_t block_size = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429
Paul Bakker68884e32013-01-07 18:20:04 +0100430 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000433 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000435#if defined(MBEDTLS_USE_PSA_CRYPTO)
436 if( ctx->psa_enabled == 1 )
437 {
438 /* While PSA Crypto has an API for multipart
439 * operations, we currently don't make it
440 * accessible through the cipher layer. */
441 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
442 }
443#endif /* MBEDTLS_USE_PSA_CRYPTO */
444
Paul Bakker6c212762013-12-16 15:24:50 +0100445 *olen = 0;
Janos Follath98e28a72016-05-31 14:03:54 +0100446 block_size = mbedtls_cipher_get_block_size( ctx );
Paul Bakker6c212762013-12-16 15:24:50 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200449 {
Janos Follath98e28a72016-05-31 14:03:54 +0100450 if( ilen != block_size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200452
453 *olen = ilen;
454
455 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
456 ctx->operation, input, output ) ) )
457 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200458 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200459 }
460
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200461 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200462 }
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_GCM_C)
465 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200466 {
467 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200469 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200470 }
471#endif
472
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +0200473#if defined(MBEDTLS_CHACHAPOLY_C)
474 if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
475 {
476 *olen = ilen;
477 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
478 ilen, input, output );
479 }
480#endif
481
Janos Follath98e28a72016-05-31 14:03:54 +0100482 if ( 0 == block_size )
483 {
484 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
485 }
486
Paul Bakker68884e32013-01-07 18:20:04 +0100487 if( input == output &&
Janos Follath98e28a72016-05-31 14:03:54 +0100488 ( ctx->unprocessed_len != 0 || ilen % block_size ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100491 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_CIPHER_MODE_CBC)
494 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200496 size_t copy_len = 0;
497
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498 /*
499 * If there is not enough data for a full block, cache it.
500 */
Andy Leiserson79e77892017-04-28 20:01:49 -0700501 if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000502 ilen <= block_size - ctx->unprocessed_len ) ||
Andy Leiserson79e77892017-04-28 20:01:49 -0700503 ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
504 ilen < block_size - ctx->unprocessed_len ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 ( ctx->operation == MBEDTLS_ENCRYPT &&
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000506 ilen < block_size - ctx->unprocessed_len ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507 {
508 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
509 ilen );
510
511 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200512 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513 }
514
515 /*
516 * Process cached data first
517 */
Janos Follath98e28a72016-05-31 14:03:54 +0100518 if( 0 != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000519 {
Janos Follath98e28a72016-05-31 14:03:54 +0100520 copy_len = block_size - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521
522 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
523 copy_len );
524
Paul Bakkerff61a782011-06-09 15:42:02 +0000525 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Janos Follath98e28a72016-05-31 14:03:54 +0100526 ctx->operation, block_size, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000527 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200529 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000530 }
531
Janos Follath98e28a72016-05-31 14:03:54 +0100532 *olen += block_size;
533 output += block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534 ctx->unprocessed_len = 0;
535
536 input += copy_len;
537 ilen -= copy_len;
538 }
539
540 /*
541 * Cache final, incomplete block
542 */
543 if( 0 != ilen )
544 {
Janos Follath98e28a72016-05-31 14:03:54 +0100545 if( 0 == block_size )
546 {
547 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
548 }
549
Andy Leiserson79e77892017-04-28 20:01:49 -0700550 /* Encryption: only cache partial blocks
551 * Decryption w/ padding: always keep at least one whole block
552 * Decryption w/o padding: only cache partial blocks
553 */
Janos Follath98e28a72016-05-31 14:03:54 +0100554 copy_len = ilen % block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700555 if( copy_len == 0 &&
556 ctx->operation == MBEDTLS_DECRYPT &&
557 NULL != ctx->add_padding)
558 {
Janos Follath98e28a72016-05-31 14:03:54 +0100559 copy_len = block_size;
Andy Leiserson79e77892017-04-28 20:01:49 -0700560 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561
562 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
563 copy_len );
564
565 ctx->unprocessed_len += copy_len;
566 ilen -= copy_len;
567 }
568
569 /*
570 * Process remaining full blocks
571 */
572 if( ilen )
573 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000574 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
575 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200577 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200579
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580 *olen += ilen;
581 }
582
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200583 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#if defined(MBEDTLS_CIPHER_MODE_CFB)
588 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000589 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000590 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000591 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000592 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000593 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200594 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000595 }
596
597 *olen = ilen;
598
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200599 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000602
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100603#if defined(MBEDTLS_CIPHER_MODE_OFB)
604 if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
605 {
606 if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
607 ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
608 {
609 return( ret );
610 }
611
612 *olen = ilen;
613
614 return( 0 );
615 }
616#endif /* MBEDTLS_CIPHER_MODE_OFB */
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_CIPHER_MODE_CTR)
619 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000620 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000621 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000622 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000623 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000624 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200625 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000626 }
627
628 *olen = ilen;
629
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200630 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000631 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000633
Jaeden Ameroc6539902018-04-30 17:17:41 +0100634#if defined(MBEDTLS_CIPHER_MODE_XTS)
635 if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
636 {
637 if( ctx->unprocessed_len > 0 ) {
638 /* We can only process an entire data unit at a time. */
639 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
640 }
641
642 ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
643 ctx->operation, ilen, ctx->iv, input, output );
644 if( ret != 0 )
645 {
646 return( ret );
647 }
648
649 *olen = ilen;
650
651 return( 0 );
652 }
653#endif /* MBEDTLS_CIPHER_MODE_XTS */
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_CIPHER_MODE_STREAM)
656 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200657 {
658 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
659 ilen, input, output ) ) )
660 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200662 }
663
664 *olen = ilen;
665
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671}
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
674#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200675/*
676 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
677 */
Paul Bakker23986e52011-04-24 08:57:21 +0000678static void add_pkcs_padding( unsigned char *output, size_t output_len,
679 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680{
Paul Bakker23986e52011-04-24 08:57:21 +0000681 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100682 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683
684 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000685 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686}
687
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200688static int get_pkcs_padding( unsigned char *input, size_t input_len,
689 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000690{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100691 size_t i, pad_idx;
692 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693
Paul Bakkera885d682011-01-20 16:35:05 +0000694 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000696
697 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698 *data_len = input_len - padding_len;
699
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +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 * so pick input_len, which is usually 8 or 16 (one block) */
706 pad_idx = input_len - padding_len;
707 for( i = 0; i < input_len; i++ )
708 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200715/*
716 * One and zeros padding: fill with 80 00 ... 00
717 */
718static void add_one_and_zeros_padding( unsigned char *output,
719 size_t output_len, size_t data_len )
720{
721 size_t padding_len = output_len - data_len;
722 unsigned char i = 0;
723
724 output[data_len] = 0x80;
725 for( i = 1; i < padding_len; i++ )
726 output[data_len + i] = 0x00;
727}
728
729static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
730 size_t *data_len )
731{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100732 size_t i;
733 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200734
735 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200737
Micha Krausba8316f2017-12-23 23:40:08 +0100738 bad = 0x80;
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100739 *data_len = 0;
740 for( i = input_len; i > 0; i-- )
741 {
742 prev_done = done;
Micha Krausba8316f2017-12-23 23:40:08 +0100743 done |= ( input[i - 1] != 0 );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100744 *data_len |= ( i - 1 ) * ( done != prev_done );
Micha Krausba8316f2017-12-23 23:40:08 +0100745 bad ^= input[i - 1] * ( done != prev_done );
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100746 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200749
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200754/*
755 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
756 */
757static void add_zeros_and_len_padding( unsigned char *output,
758 size_t output_len, size_t data_len )
759{
760 size_t padding_len = output_len - data_len;
761 unsigned char i = 0;
762
763 for( i = 1; i < padding_len; i++ )
764 output[data_len + i - 1] = 0x00;
765 output[output_len - 1] = (unsigned char) padding_len;
766}
767
768static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
769 size_t *data_len )
770{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100771 size_t i, pad_idx;
772 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200773
774 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200776
777 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200778 *data_len = input_len - padding_len;
779
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100780 /* Avoid logical || since it results in a branch */
781 bad |= padding_len > input_len;
782 bad |= padding_len == 0;
783
784 /* The number of bytes checked must be independent of padding_len */
785 pad_idx = input_len - padding_len;
786 for( i = 0; i < input_len - 1; i++ )
787 bad |= input[i] * ( i >= pad_idx );
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200794/*
795 * Zero padding: fill with 00 ... 00
796 */
797static void add_zeros_padding( unsigned char *output,
798 size_t output_len, size_t data_len )
799{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200800 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200801
802 for( i = data_len; i < output_len; i++ )
803 output[i] = 0x00;
804}
805
806static int get_zeros_padding( unsigned char *input, size_t input_len,
807 size_t *data_len )
808{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100809 size_t i;
810 unsigned char done = 0, prev_done;
811
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200812 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200814
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100815 *data_len = 0;
816 for( i = input_len; i > 0; i-- )
817 {
818 prev_done = done;
819 done |= ( input[i-1] != 0 );
820 *data_len |= i * ( done != prev_done );
821 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200822
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200823 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200826
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200827/*
828 * No padding: don't pad :)
829 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200831 * but a trivial get_padding function
832 */
833static int get_no_padding( unsigned char *input, size_t input_len,
834 size_t *data_len )
835{
836 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200838
839 *data_len = input_len;
840
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200841 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200846 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847{
848 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000851#if defined(MBEDTLS_USE_PSA_CRYPTO)
852 if( ctx->psa_enabled == 1 )
853 {
854 /* While PSA Crypto has an API for multipart
855 * operations, we currently don't make it
856 * accessible through the cipher layer. */
857 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
858 }
859#endif /* MBEDTLS_USE_PSA_CRYPTO */
860
Paul Bakker8123e9d2011-01-06 15:37:30 +0000861 *olen = 0;
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100864 MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
866 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
Jaeden Ameroc6539902018-04-30 17:17:41 +0100867 MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000869 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200870 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000871 }
872
Daniel King8fe47012016-05-17 20:33:28 -0300873 if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
874 ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
Daniel Kingbd920622016-05-15 19:56:20 -0300875 {
876 return( 0 );
877 }
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200880 {
881 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200883
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200884 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200885 }
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_CIPHER_MODE_CBC)
888 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000889 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200890 int ret = 0;
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000893 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200894 /* check for 'no padding' mode */
895 if( NULL == ctx->add_padding )
896 {
897 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200899
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200900 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200901 }
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000904 ctx->unprocessed_len );
905 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000907 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200908 /*
909 * For decrypt operations, expect a full block,
910 * or an empty block if no padding
911 */
912 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200913 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000916 }
917
918 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000919 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000921 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000922 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200923 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000924 }
925
926 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 if( MBEDTLS_DECRYPT == ctx->operation )
928 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200929 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000930
931 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200933 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000934 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200935#else
936 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000940}
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Hanno Becker18597cd2018-11-09 16:36:33 +0000943int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
944 mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200945{
946 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200950 }
951
Hanno Beckerce1ddee2018-11-09 16:20:29 +0000952#if defined(MBEDTLS_USE_PSA_CRYPTO)
953 if( ctx->psa_enabled == 1 )
954 {
955 /* While PSA Crypto knows about CBC padding
956 * schemes, we currently don't make them
957 * accessible through the cipher layer. */
958 if( mode != MBEDTLS_PADDING_NONE )
959 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
960
961 return( 0 );
962 }
963#endif /* MBEDTLS_USE_PSA_CRYPTO */
964
Paul Bakker1a45d912013-08-14 12:04:26 +0200965 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
968 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200969 ctx->add_padding = add_pkcs_padding;
970 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200971 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
974 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200975 ctx->add_padding = add_one_and_zeros_padding;
976 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200977 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
980 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200981 ctx->add_padding = add_zeros_and_len_padding;
982 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200983 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
986 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200987 ctx->add_padding = add_zeros_padding;
988 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200989 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200990#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200992 ctx->add_padding = NULL;
993 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200994 break;
995
996 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200998 }
999
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001000 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001003
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001004#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001006 unsigned char *tag, size_t tag_len )
1007{
1008 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( MBEDTLS_ENCRYPT != ctx->operation )
1012 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001013
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001014#if defined(MBEDTLS_USE_PSA_CRYPTO)
1015 if( ctx->psa_enabled == 1 )
1016 {
1017 /* While PSA Crypto has an API for multipart
1018 * operations, we currently don't make it
1019 * accessible through the cipher layer. */
1020 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1021
1022 return( 0 );
1023 }
1024#endif /* MBEDTLS_USE_PSA_CRYPTO */
1025
Daniel King8fe47012016-05-17 20:33:28 -03001026#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Hanno Becker18597cd2018-11-09 16:36:33 +00001028 return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
1029 tag, tag_len ) );
Daniel King8fe47012016-05-17 20:33:28 -03001030#endif
1031
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001032#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001033 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1034 {
1035 /* Don't allow truncated MAC for Poly1305 */
1036 if ( tag_len != 16U )
1037 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1038
Hanno Becker18597cd2018-11-09 16:36:33 +00001039 return( mbedtls_chachapoly_finish(
1040 (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001041 }
1042#endif
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001043
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001044 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001045}
Paul Bakker9af723c2014-05-01 13:03:14 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001048 const unsigned char *tag, size_t tag_len )
1049{
Daniel King8fe47012016-05-17 20:33:28 -03001050 unsigned char check_tag[16];
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001051 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001052
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001053 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001057 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001058
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001059#if defined(MBEDTLS_USE_PSA_CRYPTO)
1060 if( ctx->psa_enabled == 1 )
1061 {
1062 /* While PSA Crypto has an API for multipart
1063 * operations, we currently don't make it
1064 * accessible through the cipher layer. */
1065 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1066 }
1067#endif /* MBEDTLS_USE_PSA_CRYPTO */
1068
Daniel King8fe47012016-05-17 20:33:28 -03001069#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001071 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001072 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001074
Hanno Becker18597cd2018-11-09 16:36:33 +00001075 if( 0 != ( ret = mbedtls_gcm_finish(
1076 (mbedtls_gcm_context *) ctx->cipher_ctx,
1077 check_tag, tag_len ) ) )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001079 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001080 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001081
1082 /* Check the tag in "constant-time" */
Daniel King8fe47012016-05-17 20:33:28 -03001083 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001085
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001086 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001087 }
Daniel King8fe47012016-05-17 20:33:28 -03001088#endif /* MBEDTLS_GCM_C */
1089
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001090#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001091 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1092 {
1093 /* Don't allow truncated MAC for Poly1305 */
1094 if ( tag_len != sizeof( check_tag ) )
1095 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1096
Hanno Becker18597cd2018-11-09 16:36:33 +00001097 ret = mbedtls_chachapoly_finish(
1098 (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
Daniel King8fe47012016-05-17 20:33:28 -03001099 if ( ret != 0 )
1100 {
1101 return( ret );
1102 }
1103
1104 /* Check the tag in "constant-time" */
1105 if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
1106 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1107
1108 return( 0 );
1109 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001110#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001111
1112 return( 0 );
1113}
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001114#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001115
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001116/*
1117 * Packet-oriented wrapper for non-AEAD modes
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001120 const unsigned char *iv, size_t iv_len,
1121 const unsigned char *input, size_t ilen,
1122 unsigned char *output, size_t *olen )
1123{
1124 int ret;
1125 size_t finish_olen;
1126
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001127#if defined(MBEDTLS_USE_PSA_CRYPTO)
1128 if( ctx->psa_enabled == 1 )
1129 {
1130 /* TODO */
1131 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1132 }
1133#endif /* MBEDTLS_USE_PSA_CRYPTO */
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001136 return( ret );
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001139 return( ret );
1140
Hanno Becker18597cd2018-11-09 16:36:33 +00001141 if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
1142 output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001143 return( ret );
1144
Hanno Becker18597cd2018-11-09 16:36:33 +00001145 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
1146 &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +02001147 return( ret );
1148
1149 *olen += finish_olen;
1150
1151 return( 0 );
1152}
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001155/*
1156 * Packet-oriented encryption for AEAD modes
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001159 const unsigned char *iv, size_t iv_len,
1160 const unsigned char *ad, size_t ad_len,
1161 const unsigned char *input, size_t ilen,
1162 unsigned char *output, size_t *olen,
1163 unsigned char *tag, size_t tag_len )
1164{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001165#if defined(MBEDTLS_USE_PSA_CRYPTO)
1166 if( ctx->psa_enabled == 1 )
1167 {
1168 /* TODO */
1169 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1170 }
1171#endif /* MBEDTLS_USE_PSA_CRYPTO */
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_GCM_C)
1174 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001175 {
1176 *olen = ilen;
Hanno Becker18597cd2018-11-09 16:36:33 +00001177 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1178 ilen, iv, iv_len, ad, ad_len,
1179 input, output, tag_len, tag ) );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#endif /* MBEDTLS_GCM_C */
1182#if defined(MBEDTLS_CCM_C)
1183 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001184 {
1185 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001187 iv, iv_len, ad, ad_len, input, output,
1188 tag, tag_len ) );
1189 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001191#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001192 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1193 {
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001194 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001195 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001196 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001197 {
1198 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1199 }
1200
1201 *olen = ilen;
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +02001202 return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001203 ilen, iv, ad, ad_len, input, output, tag ) );
Daniel King8fe47012016-05-17 20:33:28 -03001204 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001205#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001208}
1209
1210/*
1211 * Packet-oriented decryption for AEAD modes
1212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001214 const unsigned char *iv, size_t iv_len,
1215 const unsigned char *ad, size_t ad_len,
1216 const unsigned char *input, size_t ilen,
1217 unsigned char *output, size_t *olen,
1218 const unsigned char *tag, size_t tag_len )
1219{
Hanno Beckerce1ddee2018-11-09 16:20:29 +00001220#if defined(MBEDTLS_USE_PSA_CRYPTO)
1221 if( ctx->psa_enabled == 1 )
1222 {
1223 /* TODO */
1224 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1225 }
1226#endif /* MBEDTLS_USE_PSA_CRYPTO */
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_GCM_C)
1229 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001230 {
1231 int ret;
1232
1233 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001235 iv, iv_len, ad, ad_len,
1236 tag, tag_len, input, output );
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
1239 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001240
1241 return( ret );
1242 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#endif /* MBEDTLS_GCM_C */
1244#if defined(MBEDTLS_CCM_C)
1245 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001246 {
1247 int ret;
1248
1249 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001251 iv, iv_len, ad, ad_len,
1252 input, output, tag, tag_len );
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
1255 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001256
1257 return( ret );
1258 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001260#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001261 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
1262 {
Daniel King8fe47012016-05-17 20:33:28 -03001263 int ret;
1264
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001265 /* ChachaPoly has fixed length nonce and MAC (tag) */
Daniel King8fe47012016-05-17 20:33:28 -03001266 if ( ( iv_len != ctx->cipher_info->iv_size ) ||
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001267 ( tag_len != 16U ) )
Daniel King8fe47012016-05-17 20:33:28 -03001268 {
1269 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1270 }
1271
1272 *olen = ilen;
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001273 ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
1274 iv, ad, ad_len, tag, input, output );
Daniel King8fe47012016-05-17 20:33:28 -03001275
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001276 if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
1277 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Daniel King8fe47012016-05-17 20:33:28 -03001278
Manuel Pégourié-Gonnardfe725de2018-05-08 09:38:09 +02001279 return( ret );
Daniel King8fe47012016-05-17 20:33:28 -03001280 }
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001281#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +02001286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#endif /* MBEDTLS_CIPHER_C */