blob: 4e97bf121611e9e0835b49bcb1255a19af6461af [file] [log] [blame] [raw]
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é-Gonnarda658a402015-01-23 09:45:19 +00008 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000010 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
Paul Bakker8123e9d2011-01-06 15:37:30 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020036#include "mbedtls/cipher_internal.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020047#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
50#define MBEDTLS_CIPHER_MODE_STREAM
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020051#endif
52
Paul Bakker6edcd412013-10-29 15:22:54 +010053#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
54 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000055#define strcasecmp _stricmp
56#endif
57
Paul Bakker34617722014-06-13 17:20:13 +020058/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020060 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
61}
62
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020063static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020068 int *type;
69
70 if( ! supported_init )
71 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 def = mbedtls_cipher_definitions;
73 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020074
75 while( def->type != 0 )
76 *type++ = (*def++).type;
77
78 *type = 0;
79
80 supported_init = 1;
81 }
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000084}
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020091 if( def->type == cipher_type )
92 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000093
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200100
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200105 if( ! strcasecmp( def->info->name, cipher_name ) )
106 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000107
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200108 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Paul Bakkerf46b6952013-09-09 00:08:26 +0200112 int key_length,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200118 if( def->info->base->cipher == cipher_id &&
119 def->info->key_length == (unsigned) key_length &&
120 def->info->mode == mode )
121 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200122
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200132{
133 if( ctx == NULL )
134 return;
135
136 if( ctx->cipher_ctx )
137 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200140}
141
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200142int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000143{
144 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Paul Bakker343a8702011-06-09 14:27:58 +0000149 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151
152 ctx->cipher_info = cipher_info;
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200155 /*
156 * Ignore possible errors caused by a cipher mode that doesn't use padding
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
159 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200160#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200164
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200165 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166}
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
169 int key_length, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170{
171 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200175 (int) ctx->cipher_info->key_length != key_length )
176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200178 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200179
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180 ctx->key_length = key_length;
181 ctx->operation = operation;
Paul Bakkerfab5c822012-02-06 16:45:10 +0000182
Paul Bakker343a8702011-06-09 14:27:58 +0000183 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000184 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 if( MBEDTLS_ENCRYPT == operation ||
187 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
188 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000189 {
190 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000192 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000195 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196 ctx->key_length );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200199}
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200202 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200204 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200205
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200209 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
211 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200214 actual_iv_size = iv_len;
215 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200216 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200217 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200218
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200219 /* avoid reading past the end of input buffer */
220 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200222 }
223
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200224 memcpy( ctx->iv, iv, actual_iv_size );
225 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200226
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200227 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200231{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200232 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200234
Paul Bakker8123e9d2011-01-06 15:37:30 +0000235 ctx->unprocessed_len = 0;
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200236
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200237 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_GCM_C)
241int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200242 const unsigned char *ad, size_t ad_len )
243{
244 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200250 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200251 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200253 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200258 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259{
Paul Bakkerff61a782011-06-09 15:42:02 +0000260 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261
Paul Bakker68884e32013-01-07 18:20:04 +0100262 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000265 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Paul Bakker6c212762013-12-16 15:24:50 +0100267 *olen = 0;
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
272 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200273
274 *olen = ilen;
275
276 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
277 ctx->operation, input, output ) ) )
278 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200279 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200280 }
281
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200282 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200283 }
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_GCM_C)
286 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200287 {
288 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200290 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200291 }
292#endif
293
Paul Bakker68884e32013-01-07 18:20:04 +0100294 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100298 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_CIPHER_MODE_CBC)
301 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200303 size_t copy_len = 0;
304
Paul Bakker8123e9d2011-01-06 15:37:30 +0000305 /*
306 * If there is not enough data for a full block, cache it.
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ctx->operation == MBEDTLS_DECRYPT &&
309 ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) ||
310 ( ctx->operation == MBEDTLS_ENCRYPT &&
311 ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312 {
313 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
314 ilen );
315
316 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200317 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000318 }
319
320 /*
321 * Process cached data first
322 */
323 if( ctx->unprocessed_len != 0 )
324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326
327 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
328 copy_len );
329
Paul Bakkerff61a782011-06-09 15:42:02 +0000330 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000332 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200334 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 }
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 *olen += mbedtls_cipher_get_block_size( ctx );
338 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339 ctx->unprocessed_len = 0;
340
341 input += copy_len;
342 ilen -= copy_len;
343 }
344
345 /*
346 * Cache final, incomplete block
347 */
348 if( 0 != ilen )
349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
351 if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
352 copy_len = mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000353
354 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
355 copy_len );
356
357 ctx->unprocessed_len += copy_len;
358 ilen -= copy_len;
359 }
360
361 /*
362 * Process remaining full blocks
363 */
364 if( ilen )
365 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000366 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
367 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200369 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200371
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 *olen += ilen;
373 }
Paul Bakker343a8702011-06-09 14:27:58 +0000374
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200375 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_CIPHER_MODE_CFB)
380 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000381 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000382 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000383 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000384 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000385 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200386 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000387 }
388
389 *olen = ilen;
390
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200391 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000392 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_CIPHER_MODE_CTR)
396 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000397 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000398 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000399 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000400 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000401 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200402 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000403 }
404
405 *olen = ilen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000408 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_CIPHER_MODE_STREAM)
412 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200413 {
414 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
415 ilen, input, output ) ) )
416 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200418 }
419
420 *olen = ilen;
421
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200422 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
430#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200431/*
432 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
433 */
Paul Bakker23986e52011-04-24 08:57:21 +0000434static void add_pkcs_padding( unsigned char *output, size_t output_len,
435 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436{
Paul Bakker23986e52011-04-24 08:57:21 +0000437 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100438 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439
440 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000441 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442}
443
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200444static int get_pkcs_padding( unsigned char *input, size_t input_len,
445 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100447 size_t i, pad_idx;
448 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449
Paul Bakkera885d682011-01-20 16:35:05 +0000450 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452
453 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000454 *data_len = input_len - padding_len;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200455
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100456 /* Avoid logical || since it results in a branch */
457 bad |= padding_len > input_len;
458 bad |= padding_len == 0;
459
460 /* The number of bytes checked must be independent of padding_len,
461 * so pick input_len, which is usually 8 or 16 (one block) */
462 pad_idx = input_len - padding_len;
463 for( i = 0; i < input_len; i++ )
464 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200471/*
472 * One and zeros padding: fill with 80 00 ... 00
473 */
474static void add_one_and_zeros_padding( unsigned char *output,
475 size_t output_len, size_t data_len )
476{
477 size_t padding_len = output_len - data_len;
478 unsigned char i = 0;
479
480 output[data_len] = 0x80;
481 for( i = 1; i < padding_len; i++ )
482 output[data_len + i] = 0x00;
483}
484
485static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
486 size_t *data_len )
487{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100488 size_t i;
489 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200490
491 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200493
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100494 bad = 0xFF;
495 *data_len = 0;
496 for( i = input_len; i > 0; i-- )
497 {
498 prev_done = done;
499 done |= ( input[i-1] != 0 );
500 *data_len |= ( i - 1 ) * ( done != prev_done );
501 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
502 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200505
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200510/*
511 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
512 */
513static void add_zeros_and_len_padding( unsigned char *output,
514 size_t output_len, size_t data_len )
515{
516 size_t padding_len = output_len - data_len;
517 unsigned char i = 0;
518
519 for( i = 1; i < padding_len; i++ )
520 output[data_len + i - 1] = 0x00;
521 output[output_len - 1] = (unsigned char) padding_len;
522}
523
524static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
525 size_t *data_len )
526{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100527 size_t i, pad_idx;
528 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200529
530 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200532
533 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200534 *data_len = input_len - padding_len;
535
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100536 /* Avoid logical || since it results in a branch */
537 bad |= padding_len > input_len;
538 bad |= padding_len == 0;
539
540 /* The number of bytes checked must be independent of padding_len */
541 pad_idx = input_len - padding_len;
542 for( i = 0; i < input_len - 1; i++ )
543 bad |= input[i] * ( i >= pad_idx );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200546}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200550/*
551 * Zero padding: fill with 00 ... 00
552 */
553static void add_zeros_padding( unsigned char *output,
554 size_t output_len, size_t data_len )
555{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200556 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200557
558 for( i = data_len; i < output_len; i++ )
559 output[i] = 0x00;
560}
561
562static int get_zeros_padding( unsigned char *input, size_t input_len,
563 size_t *data_len )
564{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100565 size_t i;
566 unsigned char done = 0, prev_done;
567
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200568 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200570
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100571 *data_len = 0;
572 for( i = input_len; i > 0; i-- )
573 {
574 prev_done = done;
575 done |= ( input[i-1] != 0 );
576 *data_len |= i * ( done != prev_done );
577 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200578
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200582
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200583/*
584 * No padding: don't pad :)
585 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200587 * but a trivial get_padding function
588 */
589static int get_no_padding( unsigned char *input, size_t input_len,
590 size_t *data_len )
591{
592 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200594
595 *data_len = input_len;
596
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200597 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200602 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603{
604 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
607 *olen = 0;
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
610 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
611 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
612 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000613 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200614 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000615 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200618 {
619 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200621
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200622 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200623 }
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_CIPHER_MODE_CBC)
626 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200628 int ret = 0;
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200632 /* check for 'no padding' mode */
633 if( NULL == ctx->add_padding )
634 {
635 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200637
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200638 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200639 }
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000642 ctx->unprocessed_len );
643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646 /*
647 * For decrypt operations, expect a full block,
648 * or an empty block if no padding
649 */
650 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200651 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 }
655
656 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000657 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000659 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 }
663
664 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( MBEDTLS_DECRYPT == ctx->operation )
666 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200667 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668
669 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200673#else
674 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
681int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200682{
683 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200687 }
688
Paul Bakker1a45d912013-08-14 12:04:26 +0200689 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
692 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200693 ctx->add_padding = add_pkcs_padding;
694 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200695 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
698 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200699 ctx->add_padding = add_one_and_zeros_padding;
700 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
704 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200705 ctx->add_padding = add_zeros_and_len_padding;
706 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200707 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
710 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200711 ctx->add_padding = add_zeros_padding;
712 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200713 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200716 ctx->add_padding = NULL;
717 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200718 break;
719
720 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200722 }
723
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200724 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_GCM_C)
729int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200730 unsigned char *tag, size_t tag_len )
731{
732 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 if( MBEDTLS_ENCRYPT != ctx->operation )
736 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
739 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200740
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200741 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200742}
Paul Bakker9af723c2014-05-01 13:03:14 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200745 const unsigned char *tag, size_t tag_len )
746{
747 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200748
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200749 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200753 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200756 {
757 unsigned char check_tag[16];
758 size_t i;
759 int diff;
760
761 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200765 check_tag, tag_len ) ) )
766 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200767 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200768 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200769
770 /* Check the tag in "constant-time" */
771 for( diff = 0, i = 0; i < tag_len; i++ )
772 diff |= tag[i] ^ check_tag[i];
773
774 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200778 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779
780 return( 0 );
781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200783
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200784/*
785 * Packet-oriented wrapper for non-AEAD modes
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200788 const unsigned char *iv, size_t iv_len,
789 const unsigned char *input, size_t ilen,
790 unsigned char *output, size_t *olen )
791{
792 int ret;
793 size_t finish_olen;
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200796 return( ret );
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200799 return( ret );
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200802 return( ret );
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200805 return( ret );
806
807 *olen += finish_olen;
808
809 return( 0 );
810}
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200813/*
814 * Packet-oriented encryption for AEAD modes
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200817 const unsigned char *iv, size_t iv_len,
818 const unsigned char *ad, size_t ad_len,
819 const unsigned char *input, size_t ilen,
820 unsigned char *output, size_t *olen,
821 unsigned char *tag, size_t tag_len )
822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_GCM_C)
824 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200825 {
826 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200828 iv, iv_len, ad, ad_len, input, output,
829 tag_len, tag ) );
830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_GCM_C */
832#if defined(MBEDTLS_CCM_C)
833 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200834 {
835 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200837 iv, iv_len, ad, ad_len, input, output,
838 tag, tag_len ) );
839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200843}
844
845/*
846 * Packet-oriented decryption for AEAD modes
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200849 const unsigned char *iv, size_t iv_len,
850 const unsigned char *ad, size_t ad_len,
851 const unsigned char *input, size_t ilen,
852 unsigned char *output, size_t *olen,
853 const unsigned char *tag, size_t tag_len )
854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_GCM_C)
856 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200857 {
858 int ret;
859
860 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200862 iv, iv_len, ad, ad_len,
863 tag, tag_len, input, output );
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
866 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200867
868 return( ret );
869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_GCM_C */
871#if defined(MBEDTLS_CCM_C)
872 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200873 {
874 int ret;
875
876 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200878 iv, iv_len, ad, ad_len,
879 input, output, tag, tag_len );
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
882 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200883
884 return( ret );
885 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#endif /* MBEDTLS_CIPHER_C */