blob: 627d459a3b7ee2aa85f3b503be2e166240b207bc [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é-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
48
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 Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020055 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020058static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060const int *mbedtls_cipher_list( void )
Paul Bakker72f62662011-01-16 21:27:44 +000061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020063 int *type;
64
65 if( ! supported_init )
66 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 def = mbedtls_cipher_definitions;
68 type = mbedtls_cipher_supported;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020069
70 while( def->type != 0 )
71 *type++ = (*def++).type;
72
73 *type = 0;
74
75 supported_init = 1;
76 }
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( mbedtls_cipher_supported );
Paul Bakker72f62662011-01-16 21:27:44 +000079}
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 const mbedtls_cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020086 if( def->type == cipher_type )
87 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000088
Paul Bakkerd8bb8262014-06-17 14:06:49 +020089 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000090}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
Paul Bakker8123e9d2011-01-06 15:37:30 +000093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 const mbedtls_cipher_definition_t *def;
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020095
Paul Bakker8123e9d2011-01-06 15:37:30 +000096 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020097 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200100 if( ! strcmp( def->info->name, cipher_name ) )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000102
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200103 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200107 int key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 const mbedtls_cipher_mode_t mode )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 const mbedtls_cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200113 if( def->info->base->cipher == cipher_id &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200114 def->info->key_bitlen == (unsigned) key_bitlen &&
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115 def->info->mode == mode )
116 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200117
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200118 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200119}
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
Paul Bakker84bbeb52014-07-01 14:53:22 +0200127{
128 if( ctx == NULL )
129 return;
130
131 if( ctx->cipher_ctx )
132 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200135}
136
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200137int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138{
139 if( NULL == cipher_info || NULL == ctx )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000143
Paul Bakker343a8702011-06-09 14:27:58 +0000144 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
147 ctx->cipher_info = cipher_info;
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200150 /*
151 * Ignore possible errors caused by a cipher mode that doesn't use padding
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
154 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200155#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
Paul Bakker48e93c82013-08-14 12:21:18 +0200157#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200159
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200160 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161}
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200164 int key_bitlen, const mbedtls_operation_t operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165{
166 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200170 (int) ctx->cipher_info->key_bitlen != key_bitlen )
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200173 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200174
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200175 ctx->key_bitlen = key_bitlen;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176 ctx->operation = operation;
177
Paul Bakker343a8702011-06-09 14:27:58 +0000178 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000179 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 if( MBEDTLS_ENCRYPT == operation ||
182 MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
183 MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000184 {
185 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200186 ctx->key_bitlen );
Paul Bakker343a8702011-06-09 14:27:58 +0000187 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 if( MBEDTLS_DECRYPT == operation )
Paul Bakker343a8702011-06-09 14:27:58 +0000190 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200191 ctx->key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194}
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200197 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200199 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200200
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200204 /* avoid buffer overflow in ctx->iv */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( iv_len > MBEDTLS_MAX_IV_LENGTH )
206 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200209 actual_iv_size = iv_len;
210 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200211 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200212 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200213
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200214 /* avoid reading past the end of input buffer */
215 if( actual_iv_size > iv_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200217 }
218
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200219 memcpy( ctx->iv, iv, actual_iv_size );
220 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200221
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200222 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200226{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200227 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200229
Paul Bakker8123e9d2011-01-06 15:37:30 +0000230 ctx->unprocessed_len = 0;
231
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200232 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_GCM_C)
236int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200237 const unsigned char *ad, size_t ad_len )
238{
239 if( NULL == ctx || NULL == ctx->cipher_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200245 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200246 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200247
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#endif /* MBEDTLS_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200253 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254{
Paul Bakkerff61a782011-06-09 15:42:02 +0000255 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Paul Bakker68884e32013-01-07 18:20:04 +0100257 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000260 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261
Paul Bakker6c212762013-12-16 15:24:50 +0100262 *olen = 0;
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ilen != mbedtls_cipher_get_block_size( ctx ) )
267 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200268
269 *olen = ilen;
270
271 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
272 ctx->operation, input, output ) ) )
273 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200274 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200275 }
276
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200277 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200278 }
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_GCM_C)
281 if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200282 {
283 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200285 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200286 }
287#endif
288
Paul Bakker68884e32013-01-07 18:20:04 +0100289 if( input == output &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker68884e32013-01-07 18:20:04 +0100291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100293 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_CIPHER_MODE_CBC)
296 if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200298 size_t copy_len = 0;
299
Paul Bakker8123e9d2011-01-06 15:37:30 +0000300 /*
301 * If there is not enough data for a full block, cache it.
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ctx->operation == MBEDTLS_DECRYPT &&
304 ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) ||
305 ( ctx->operation == MBEDTLS_ENCRYPT &&
306 ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000307 {
308 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
309 ilen );
310
311 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200312 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313 }
314
315 /*
316 * Process cached data first
317 */
318 if( ctx->unprocessed_len != 0 )
319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321
322 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
323 copy_len );
324
Paul Bakkerff61a782011-06-09 15:42:02 +0000325 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000327 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200329 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000330 }
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 *olen += mbedtls_cipher_get_block_size( ctx );
333 output += mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000334 ctx->unprocessed_len = 0;
335
336 input += copy_len;
337 ilen -= copy_len;
338 }
339
340 /*
341 * Cache final, incomplete block
342 */
343 if( 0 != ilen )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
346 if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
347 copy_len = mbedtls_cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348
349 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
350 copy_len );
351
352 ctx->unprocessed_len += copy_len;
353 ilen -= copy_len;
354 }
355
356 /*
357 * Process remaining full blocks
358 */
359 if( ilen )
360 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000361 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
362 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200364 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000365 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200366
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367 *olen += ilen;
368 }
369
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200370 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_CIPHER_MODE_CFB)
375 if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000376 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000377 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000378 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000379 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000380 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200381 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000382 }
383
384 *olen = ilen;
385
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200386 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000387 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_CIPHER_MODE_CTR)
391 if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
Paul Bakker343a8702011-06-09 14:27:58 +0000392 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000393 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000394 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000395 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000396 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200397 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000398 }
399
400 *olen = ilen;
401
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200402 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000403 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_CIPHER_MODE_STREAM)
407 if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200408 {
409 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
410 ilen, input, output ) ) )
411 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200413 }
414
415 *olen = ilen;
416
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422}
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
425#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200426/*
427 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
428 */
Paul Bakker23986e52011-04-24 08:57:21 +0000429static void add_pkcs_padding( unsigned char *output, size_t output_len,
430 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000431{
Paul Bakker23986e52011-04-24 08:57:21 +0000432 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100433 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
435 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000436 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437}
438
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200439static int get_pkcs_padding( unsigned char *input, size_t input_len,
440 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100442 size_t i, pad_idx;
443 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444
Paul Bakkera885d682011-01-20 16:35:05 +0000445 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447
448 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 *data_len = input_len - padding_len;
450
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100451 /* Avoid logical || since it results in a branch */
452 bad |= padding_len > input_len;
453 bad |= padding_len == 0;
454
455 /* The number of bytes checked must be independent of padding_len,
456 * so pick input_len, which is usually 8 or 16 (one block) */
457 pad_idx = input_len - padding_len;
458 for( i = 0; i < input_len; i++ )
459 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200466/*
467 * One and zeros padding: fill with 80 00 ... 00
468 */
469static void add_one_and_zeros_padding( unsigned char *output,
470 size_t output_len, size_t data_len )
471{
472 size_t padding_len = output_len - data_len;
473 unsigned char i = 0;
474
475 output[data_len] = 0x80;
476 for( i = 1; i < padding_len; i++ )
477 output[data_len + i] = 0x00;
478}
479
480static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
481 size_t *data_len )
482{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100483 size_t i;
484 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200485
486 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200488
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100489 bad = 0xFF;
490 *data_len = 0;
491 for( i = input_len; i > 0; i-- )
492 {
493 prev_done = done;
494 done |= ( input[i-1] != 0 );
495 *data_len |= ( i - 1 ) * ( done != prev_done );
496 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
497 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200505/*
506 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
507 */
508static void add_zeros_and_len_padding( unsigned char *output,
509 size_t output_len, size_t data_len )
510{
511 size_t padding_len = output_len - data_len;
512 unsigned char i = 0;
513
514 for( i = 1; i < padding_len; i++ )
515 output[data_len + i - 1] = 0x00;
516 output[output_len - 1] = (unsigned char) padding_len;
517}
518
519static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
520 size_t *data_len )
521{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100522 size_t i, pad_idx;
523 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200524
525 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200527
528 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200529 *data_len = input_len - padding_len;
530
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100531 /* Avoid logical || since it results in a branch */
532 bad |= padding_len > input_len;
533 bad |= padding_len == 0;
534
535 /* The number of bytes checked must be independent of padding_len */
536 pad_idx = input_len - padding_len;
537 for( i = 0; i < input_len - 1; i++ )
538 bad |= input[i] * ( i >= pad_idx );
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200545/*
546 * Zero padding: fill with 00 ... 00
547 */
548static void add_zeros_padding( unsigned char *output,
549 size_t output_len, size_t data_len )
550{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200551 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200552
553 for( i = data_len; i < output_len; i++ )
554 output[i] = 0x00;
555}
556
557static int get_zeros_padding( unsigned char *input, size_t input_len,
558 size_t *data_len )
559{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100560 size_t i;
561 unsigned char done = 0, prev_done;
562
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200563 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200565
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100566 *data_len = 0;
567 for( i = input_len; i > 0; i-- )
568 {
569 prev_done = done;
570 done |= ( input[i-1] != 0 );
571 *data_len |= i * ( done != prev_done );
572 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200573
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200574 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200577
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200578/*
579 * No padding: don't pad :)
580 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200582 * but a trivial get_padding function
583 */
584static int get_no_padding( unsigned char *input, size_t input_len,
585 size_t *data_len )
586{
587 if( NULL == input || NULL == data_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200589
590 *data_len = input_len;
591
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200592 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200597 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598{
599 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601
602 *olen = 0;
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
605 MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
606 MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
607 MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000608 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200609 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000610 }
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200613 {
614 if( ctx->unprocessed_len != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200616
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200617 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200618 }
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_CIPHER_MODE_CBC)
621 if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200623 int ret = 0;
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( MBEDTLS_ENCRYPT == ctx->operation )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200627 /* check for 'no padding' mode */
628 if( NULL == ctx->add_padding )
629 {
630 if( 0 != ctx->unprocessed_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200632
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200634 }
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 ctx->unprocessed_len );
638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641 /*
642 * For decrypt operations, expect a full block,
643 * or an empty block if no padding
644 */
645 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 }
650
651 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000652 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000654 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200656 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 }
658
659 /* Set output size for decryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( MBEDTLS_DECRYPT == ctx->operation )
661 return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200662 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663
664 /* Set output size for encryption */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 *olen = mbedtls_cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200666 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200668#else
669 ((void) output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673}
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
676int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200677{
678 if( NULL == ctx ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200682 }
683
Paul Bakker1a45d912013-08-14 12:04:26 +0200684 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
687 case MBEDTLS_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200688 ctx->add_padding = add_pkcs_padding;
689 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200690 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200691#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
693 case MBEDTLS_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200694 ctx->add_padding = add_one_and_zeros_padding;
695 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200696 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200697#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
699 case MBEDTLS_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200700 ctx->add_padding = add_zeros_and_len_padding;
701 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200702 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
705 case MBEDTLS_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200706 ctx->add_padding = add_zeros_padding;
707 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200708 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 case MBEDTLS_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200711 ctx->add_padding = NULL;
712 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200713 break;
714
715 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200717 }
718
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200719 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_GCM_C)
724int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200725 unsigned char *tag, size_t tag_len )
726{
727 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 if( MBEDTLS_ENCRYPT != ctx->operation )
731 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
734 return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200735
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200736 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200737}
Paul Bakker9af723c2014-05-01 13:03:14 +0200738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200740 const unsigned char *tag, size_t tag_len )
741{
742 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200743
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200744 if( NULL == ctx || NULL == ctx->cipher_info ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 MBEDTLS_DECRYPT != ctx->operation )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200748 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200751 {
752 unsigned char check_tag[16];
753 size_t i;
754 int diff;
755
756 if( tag_len > sizeof( check_tag ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200760 check_tag, tag_len ) ) )
761 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200762 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200763 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200764
765 /* Check the tag in "constant-time" */
766 for( diff = 0, i = 0; i < tag_len; i++ )
767 diff |= tag[i] ^ check_tag[i];
768
769 if( diff != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200771
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200772 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200773 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200774
775 return( 0 );
776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200778
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200779/*
780 * Packet-oriented wrapper for non-AEAD modes
781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200783 const unsigned char *iv, size_t iv_len,
784 const unsigned char *input, size_t ilen,
785 unsigned char *output, size_t *olen )
786{
787 int ret;
788 size_t finish_olen;
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200791 return( ret );
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200794 return( ret );
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200797 return( ret );
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200800 return( ret );
801
802 *olen += finish_olen;
803
804 return( 0 );
805}
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200808/*
809 * Packet-oriented encryption for AEAD modes
810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200812 const unsigned char *iv, size_t iv_len,
813 const unsigned char *ad, size_t ad_len,
814 const unsigned char *input, size_t ilen,
815 unsigned char *output, size_t *olen,
816 unsigned char *tag, size_t tag_len )
817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_GCM_C)
819 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200820 {
821 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200823 iv, iv_len, ad, ad_len, input, output,
824 tag_len, tag ) );
825 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#endif /* MBEDTLS_GCM_C */
827#if defined(MBEDTLS_CCM_C)
828 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200829 {
830 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200832 iv, iv_len, ad, ad_len, input, output,
833 tag, tag_len ) );
834 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200838}
839
840/*
841 * Packet-oriented decryption for AEAD modes
842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200844 const unsigned char *iv, size_t iv_len,
845 const unsigned char *ad, size_t ad_len,
846 const unsigned char *input, size_t ilen,
847 unsigned char *output, size_t *olen,
848 const unsigned char *tag, size_t tag_len )
849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_GCM_C)
851 if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200852 {
853 int ret;
854
855 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200857 iv, iv_len, ad, ad_len,
858 tag, tag_len, input, output );
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
861 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200862
863 return( ret );
864 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_GCM_C */
866#if defined(MBEDTLS_CCM_C)
867 if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200868 {
869 int ret;
870
871 *olen = ilen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200873 iv, iv_len, ad, ad_len,
874 input, output, tag, tag_len );
875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
877 ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200878
879 return( ret );
880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#endif /* MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_CIPHER_C */