blob: 5cd30f8ad912befd462380bfe2bd27f0a46a44f7 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
Paul Bakker7dc4c442014-02-01 22:50:26 +01003 *
Paul Bakker8123e9d2011-01-06 15:37:30 +00004 * \brief Generic cipher wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000035
36#if defined(POLARSSL_CIPHER_C)
37
38#include "polarssl/cipher.h"
39#include "polarssl/cipher_wrap.h"
40
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020041#if defined(POLARSSL_GCM_C)
42#include "polarssl/gcm.h"
43#endif
44
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020045#if defined(POLARSSL_CCM_C)
46#include "polarssl/ccm.h"
47#endif
48
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include <stdlib.h>
50
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020051#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020052#define POLARSSL_CIPHER_MODE_STREAM
53#endif
54
Paul Bakker6edcd412013-10-29 15:22:54 +010055#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
56 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000057#define strcasecmp _stricmp
58#endif
59
Paul Bakker34617722014-06-13 17:20:13 +020060/* Implementation that should never be optimized out by the compiler */
61static void polarssl_zeroize( void *v, size_t n ) {
62 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
63}
64
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020065static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000066
67const int *cipher_list( void )
68{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020069 const cipher_definition_t *def;
70 int *type;
71
72 if( ! supported_init )
73 {
74 def = cipher_definitions;
75 type = supported_ciphers;
76
77 while( def->type != 0 )
78 *type++ = (*def++).type;
79
80 *type = 0;
81
82 supported_init = 1;
83 }
84
Paul Bakkerd8bb8262014-06-17 14:06:49 +020085 return( supported_ciphers );
Paul Bakker72f62662011-01-16 21:27:44 +000086}
87
Paul Bakkerec1b9842012-01-14 18:24:43 +000088const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000089{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020090 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020091
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020092 for( def = cipher_definitions; def->info != NULL; def++ )
93 if( def->type == cipher_type )
94 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000095
Paul Bakkerd8bb8262014-06-17 14:06:49 +020096 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000097}
98
99const cipher_info_t *cipher_info_from_string( const char *cipher_name )
100{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101 const cipher_definition_t *def;
102
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200104 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200106 for( def = cipher_definitions; def->info != NULL; def++ )
107 if( ! strcasecmp( def->info->name, cipher_name ) )
108 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000109
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200110 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111}
112
Paul Bakkerf46b6952013-09-09 00:08:26 +0200113const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
114 int key_length,
115 const cipher_mode_t mode )
116{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200118
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200119 for( def = cipher_definitions; def->info != NULL; def++ )
120 if( def->info->base->cipher == cipher_id &&
121 def->info->key_length == (unsigned) key_length &&
122 def->info->mode == mode )
123 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200125 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200126}
127
Paul Bakker84bbeb52014-07-01 14:53:22 +0200128void cipher_init( cipher_context_t *ctx )
129{
130 memset( ctx, 0, sizeof( cipher_context_t ) );
131}
132
133void cipher_free( cipher_context_t *ctx )
134{
135 if( ctx == NULL )
136 return;
137
138 if( ctx->cipher_ctx )
139 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
140
141 polarssl_zeroize( ctx, sizeof(cipher_context_t) );
142}
143
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
145{
146 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200147 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Paul Bakker279432a2012-04-26 10:09:35 +0000149 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000150
Paul Bakker343a8702011-06-09 14:27:58 +0000151 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200152 return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153
154 ctx->cipher_info = cipher_info;
155
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200156#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200157 /*
158 * Ignore possible errors caused by a cipher mode that doesn't use padding
159 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200160#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200161 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200162#else
163 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
164#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200165#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200166
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200167 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168}
169
Paul Bakker84bbeb52014-07-01 14:53:22 +0200170/* Deprecated, redirects to cipher_free() */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171int cipher_free_ctx( cipher_context_t *ctx )
172{
Paul Bakker84bbeb52014-07-01 14:53:22 +0200173 cipher_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200175 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176}
177
178int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
179 int key_length, const operation_t operation )
180{
181 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200182 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200184 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
185 (int) ctx->cipher_info->key_length != key_length )
186 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200187 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200188 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200189
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 ctx->key_length = key_length;
191 ctx->operation = operation;
192
Paul Bakker343a8702011-06-09 14:27:58 +0000193 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000194 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000195 */
196 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000197 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000198 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
199 {
200 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000201 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000202 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203
Paul Bakker343a8702011-06-09 14:27:58 +0000204 if( POLARSSL_DECRYPT == operation )
205 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206 ctx->key_length );
207
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209}
210
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200211int cipher_set_iv( cipher_context_t *ctx,
212 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200214 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200215
Paul Bakker8123e9d2011-01-06 15:37:30 +0000216 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200217 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200219 /* avoid buffer overflow in ctx->iv */
220 if( iv_len > POLARSSL_MAX_IV_LENGTH )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200221 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200222
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200223 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200224 actual_iv_size = iv_len;
225 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200226 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200227 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200228
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200229 /* avoid reading past the end of input buffer */
230 if( actual_iv_size > iv_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200231 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200232 }
233
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200234 memcpy( ctx->iv, iv, actual_iv_size );
235 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200236
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200237 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200238}
239
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200240int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200241{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200242 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200243 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200244
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245 ctx->unprocessed_len = 0;
246
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200247 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200248}
249
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200250#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200251int cipher_update_ad( cipher_context_t *ctx,
252 const unsigned char *ad, size_t ad_len )
253{
254 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200255 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200256
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200257 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
258 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200259 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200260 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200261 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200262
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200263 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200265#endif /* POLARSSL_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000266
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200267int cipher_update( cipher_context_t *ctx, const unsigned char *input,
268 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000269{
Paul Bakkerff61a782011-06-09 15:42:02 +0000270 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000271
Paul Bakker68884e32013-01-07 18:20:04 +0100272 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000273 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200274 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000275 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000276
Paul Bakker6c212762013-12-16 15:24:50 +0100277 *olen = 0;
278
Paul Bakker5e0efa72013-09-08 23:04:04 +0200279 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
280 {
281 if( ilen != cipher_get_block_size( ctx ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200282 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200283
284 *olen = ilen;
285
286 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
287 ctx->operation, input, output ) ) )
288 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200289 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200290 }
291
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200292 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200293 }
294
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200295#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200296 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200297 {
298 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200299 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
300 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200301 }
302#endif
303
Paul Bakker68884e32013-01-07 18:20:04 +0100304 if( input == output &&
305 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
306 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200307 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100308 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200310#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200311 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200313 size_t copy_len = 0;
314
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315 /*
316 * If there is not enough data for a full block, cache it.
317 */
318 if( ( ctx->operation == POLARSSL_DECRYPT &&
319 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
320 ( ctx->operation == POLARSSL_ENCRYPT &&
321 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
322 {
323 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
324 ilen );
325
326 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200327 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328 }
329
330 /*
331 * Process cached data first
332 */
333 if( ctx->unprocessed_len != 0 )
334 {
335 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
336
337 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
338 copy_len );
339
Paul Bakkerff61a782011-06-09 15:42:02 +0000340 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000342 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000343 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200344 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345 }
346
347 *olen += cipher_get_block_size( ctx );
348 output += cipher_get_block_size( ctx );
349 ctx->unprocessed_len = 0;
350
351 input += copy_len;
352 ilen -= copy_len;
353 }
354
355 /*
356 * Cache final, incomplete block
357 */
358 if( 0 != ilen )
359 {
360 copy_len = ilen % cipher_get_block_size( ctx );
361 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
Paul Bakker66d5d072014-06-17 16:39:18 +0200362 copy_len = cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363
364 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
365 copy_len );
366
367 ctx->unprocessed_len += copy_len;
368 ilen -= copy_len;
369 }
370
371 /*
372 * Process remaining full blocks
373 */
374 if( ilen )
375 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000376 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
377 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200379 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200381
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382 *olen += ilen;
383 }
384
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200385 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200387#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388
Paul Bakker68884e32013-01-07 18:20:04 +0100389#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000390 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000391 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000392 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000393 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000394 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000395 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200396 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000397 }
398
399 *olen = ilen;
400
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200401 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000402 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200403#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000404
Paul Bakker68884e32013-01-07 18:20:04 +0100405#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000406 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
407 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000408 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000409 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000410 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000411 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200412 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000413 }
414
415 *olen = ilen;
416
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200417 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000418 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200419#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000420
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200421#if defined(POLARSSL_CIPHER_MODE_STREAM)
422 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
423 {
424 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
425 ilen, input, output ) ) )
426 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200427 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200428 }
429
430 *olen = ilen;
431
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200432 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200433 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200434#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200435
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200436 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437}
438
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200439#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200440#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200441/*
442 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
443 */
Paul Bakker23986e52011-04-24 08:57:21 +0000444static void add_pkcs_padding( unsigned char *output, size_t output_len,
445 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
Paul Bakker23986e52011-04-24 08:57:21 +0000447 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100448 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449
450 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000451 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452}
453
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200454static int get_pkcs_padding( unsigned char *input, size_t input_len,
455 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100457 size_t i, pad_idx;
458 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
Paul Bakkera885d682011-01-20 16:35:05 +0000460 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200461 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462
463 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464 *data_len = input_len - padding_len;
465
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100466 /* Avoid logical || since it results in a branch */
467 bad |= padding_len > input_len;
468 bad |= padding_len == 0;
469
470 /* The number of bytes checked must be independent of padding_len,
471 * so pick input_len, which is usually 8 or 16 (one block) */
472 pad_idx = input_len - padding_len;
473 for( i = 0; i < input_len; i++ )
474 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
475
Paul Bakker66d5d072014-06-17 16:39:18 +0200476 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477}
Paul Bakker48e93c82013-08-14 12:21:18 +0200478#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479
Paul Bakker48e93c82013-08-14 12:21:18 +0200480#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200481/*
482 * One and zeros padding: fill with 80 00 ... 00
483 */
484static void add_one_and_zeros_padding( unsigned char *output,
485 size_t output_len, size_t data_len )
486{
487 size_t padding_len = output_len - data_len;
488 unsigned char i = 0;
489
490 output[data_len] = 0x80;
491 for( i = 1; i < padding_len; i++ )
492 output[data_len + i] = 0x00;
493}
494
495static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
496 size_t *data_len )
497{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100498 size_t i;
499 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200500
501 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200502 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200503
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100504 bad = 0xFF;
505 *data_len = 0;
506 for( i = input_len; i > 0; i-- )
507 {
508 prev_done = done;
509 done |= ( input[i-1] != 0 );
510 *data_len |= ( i - 1 ) * ( done != prev_done );
511 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
512 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200513
Paul Bakker66d5d072014-06-17 16:39:18 +0200514 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200515
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200516}
Paul Bakker48e93c82013-08-14 12:21:18 +0200517#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200518
Paul Bakker48e93c82013-08-14 12:21:18 +0200519#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200520/*
521 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
522 */
523static void add_zeros_and_len_padding( unsigned char *output,
524 size_t output_len, size_t data_len )
525{
526 size_t padding_len = output_len - data_len;
527 unsigned char i = 0;
528
529 for( i = 1; i < padding_len; i++ )
530 output[data_len + i - 1] = 0x00;
531 output[output_len - 1] = (unsigned char) padding_len;
532}
533
534static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
535 size_t *data_len )
536{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100537 size_t i, pad_idx;
538 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200539
540 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200541 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200542
543 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200544 *data_len = input_len - padding_len;
545
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100546 /* Avoid logical || since it results in a branch */
547 bad |= padding_len > input_len;
548 bad |= padding_len == 0;
549
550 /* The number of bytes checked must be independent of padding_len */
551 pad_idx = input_len - padding_len;
552 for( i = 0; i < input_len - 1; i++ )
553 bad |= input[i] * ( i >= pad_idx );
554
Paul Bakker66d5d072014-06-17 16:39:18 +0200555 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200556}
Paul Bakker48e93c82013-08-14 12:21:18 +0200557#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200558
Paul Bakker48e93c82013-08-14 12:21:18 +0200559#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200560/*
561 * Zero padding: fill with 00 ... 00
562 */
563static void add_zeros_padding( unsigned char *output,
564 size_t output_len, size_t data_len )
565{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200566 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200567
568 for( i = data_len; i < output_len; i++ )
569 output[i] = 0x00;
570}
571
572static int get_zeros_padding( unsigned char *input, size_t input_len,
573 size_t *data_len )
574{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100575 size_t i;
576 unsigned char done = 0, prev_done;
577
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200578 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200580
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100581 *data_len = 0;
582 for( i = input_len; i > 0; i-- )
583 {
584 prev_done = done;
585 done |= ( input[i-1] != 0 );
586 *data_len |= i * ( done != prev_done );
587 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200588
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200589 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200590}
Paul Bakker48e93c82013-08-14 12:21:18 +0200591#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200592
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200593/*
594 * No padding: don't pad :)
595 *
596 * There is no add_padding function (check for NULL in cipher_finish)
597 * but a trivial get_padding function
598 */
599static int get_no_padding( unsigned char *input, size_t input_len,
600 size_t *data_len )
601{
602 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200603 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200604
605 *data_len = input_len;
606
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200607 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200608}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200609#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200610
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200611int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200612 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613{
614 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200615 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616
617 *olen = 0;
618
Paul Bakker6132d0a2012-07-04 17:10:40 +0000619 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000620 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200621 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200622 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000623 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200624 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000625 }
626
Paul Bakker5e0efa72013-09-08 23:04:04 +0200627 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
628 {
629 if( ctx->unprocessed_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200630 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200631
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200633 }
634
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200635#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000636 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
637 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200638 int ret = 0;
639
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 if( POLARSSL_ENCRYPT == ctx->operation )
641 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200642 /* check for 'no padding' mode */
643 if( NULL == ctx->add_padding )
644 {
645 if( 0 != ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200647
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200648 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200649 }
650
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200651 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 ctx->unprocessed_len );
653 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200654 else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200656 /*
657 * For decrypt operations, expect a full block,
658 * or an empty block if no padding
659 */
660 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200661 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200662
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200663 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 }
665
666 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000667 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
668 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
669 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672 }
673
674 /* Set output size for decryption */
675 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200676 return ctx->get_padding( output, cipher_get_block_size( ctx ),
677 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
679 /* Set output size for encryption */
680 *olen = cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200681 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200683#else
684 ((void) output);
685#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200687 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000688}
689
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200690#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200691int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
692{
693 if( NULL == ctx ||
694 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
695 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200696 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200697 }
698
Paul Bakker1a45d912013-08-14 12:04:26 +0200699 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200700 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200701#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200702 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200703 ctx->add_padding = add_pkcs_padding;
704 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200705 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200706#endif
707#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200708 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200709 ctx->add_padding = add_one_and_zeros_padding;
710 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200711 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200712#endif
713#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200714 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200715 ctx->add_padding = add_zeros_and_len_padding;
716 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200717 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200718#endif
719#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200720 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200721 ctx->add_padding = add_zeros_padding;
722 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200723 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200724#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200725 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200726 ctx->add_padding = NULL;
727 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200728 break;
729
730 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200731 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200732 }
733
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200734 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200735}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200736#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200737
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200738#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200739int cipher_write_tag( cipher_context_t *ctx,
740 unsigned char *tag, size_t tag_len )
741{
742 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200743 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200744
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200745 if( POLARSSL_ENCRYPT != ctx->operation )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200746 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200747
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200748 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200749 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200750
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200751 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200752}
Paul Bakker9af723c2014-05-01 13:03:14 +0200753
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200754int cipher_check_tag( cipher_context_t *ctx,
755 const unsigned char *tag, size_t tag_len )
756{
757 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200758
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200759 if( NULL == ctx || NULL == ctx->cipher_info ||
760 POLARSSL_DECRYPT != ctx->operation )
761 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200762 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200763 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200764
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200765 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
766 {
767 unsigned char check_tag[16];
768 size_t i;
769 int diff;
770
771 if( tag_len > sizeof( check_tag ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200772 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200773
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200774 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
775 check_tag, tag_len ) ) )
776 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200777 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200779
780 /* Check the tag in "constant-time" */
781 for( diff = 0, i = 0; i < tag_len; i++ )
782 diff |= tag[i] ^ check_tag[i];
783
784 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200785 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200786
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200787 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200788 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200789
790 return( 0 );
791}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200792#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200793
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200794/*
795 * Packet-oriented wrapper for non-AEAD modes
796 */
797int cipher_crypt( cipher_context_t *ctx,
798 const unsigned char *iv, size_t iv_len,
799 const unsigned char *input, size_t ilen,
800 unsigned char *output, size_t *olen )
801{
802 int ret;
803 size_t finish_olen;
804
805 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
806 return( ret );
807
808 if( ( ret = cipher_reset( ctx ) ) != 0 )
809 return( ret );
810
811 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
812 return( ret );
813
814 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
815 return( ret );
816
817 *olen += finish_olen;
818
819 return( 0 );
820}
821
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200822#if defined(POLARSSL_CIPHER_MODE_AEAD)
823/*
824 * Packet-oriented encryption for AEAD modes
825 */
826int cipher_auth_encrypt( cipher_context_t *ctx,
827 const unsigned char *iv, size_t iv_len,
828 const unsigned char *ad, size_t ad_len,
829 const unsigned char *input, size_t ilen,
830 unsigned char *output, size_t *olen,
831 unsigned char *tag, size_t tag_len )
832{
833#if defined(POLARSSL_GCM_C)
834 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
835 {
836 *olen = ilen;
837 return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
838 iv, iv_len, ad, ad_len, input, output,
839 tag_len, tag ) );
840 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200841#endif /* POLARSSL_GCM_C */
842#if defined(POLARSSL_CCM_C)
843 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
844 {
845 *olen = ilen;
846 return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
847 iv, iv_len, ad, ad_len, input, output,
848 tag, tag_len ) );
849 }
850#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200851
852 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
853}
854
855/*
856 * Packet-oriented decryption for AEAD modes
857 */
858int cipher_auth_decrypt( cipher_context_t *ctx,
859 const unsigned char *iv, size_t iv_len,
860 const unsigned char *ad, size_t ad_len,
861 const unsigned char *input, size_t ilen,
862 unsigned char *output, size_t *olen,
863 const unsigned char *tag, size_t tag_len )
864{
865#if defined(POLARSSL_GCM_C)
866 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
867 {
868 int ret;
869
870 *olen = ilen;
871 ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
872 iv, iv_len, ad, ad_len,
873 tag, tag_len, input, output );
874
875 if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
876 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
877
878 return( ret );
879 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200880#endif /* POLARSSL_GCM_C */
881#if defined(POLARSSL_CCM_C)
882 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
883 {
884 int ret;
885
886 *olen = ilen;
887 ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
888 iv, iv_len, ad, ad_len,
889 input, output, tag, tag_len );
890
891 if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
892 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
893
894 return( ret );
895 }
896#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200897
898 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
899}
900#endif /* POLARSSL_CIPHER_MODE_AEAD */
901
902
Paul Bakker8123e9d2011-01-06 15:37:30 +0000903#if defined(POLARSSL_SELF_TEST)
904
Paul Bakker8123e9d2011-01-06 15:37:30 +0000905/*
906 * Checkup routine
907 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000908int cipher_self_test( int verbose )
909{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000910 ((void) verbose);
911
Paul Bakker8123e9d2011-01-06 15:37:30 +0000912 return( 0 );
913}
914
Paul Bakker9af723c2014-05-01 13:03:14 +0200915#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000916
Paul Bakker9af723c2014-05-01 13:03:14 +0200917#endif /* POLARSSL_CIPHER_C */