blob: b69d331060a803d6fc950de4dffab9b60e666e6b [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é-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000028#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000032
33#if defined(POLARSSL_CIPHER_C)
34
35#include "polarssl/cipher.h"
36#include "polarssl/cipher_wrap.h"
37
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stdlib.h>
39#include <string.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
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020049#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020050#define POLARSSL_CIPHER_MODE_STREAM
51#endif
52
Paul Bakker6edcd412013-10-29 15:22:54 +010053#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
54 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000055#define strcasecmp _stricmp
56#endif
57
Paul Bakker34617722014-06-13 17:20:13 +020058/* Implementation that should never be optimized out by the compiler */
59static void polarssl_zeroize( void *v, size_t n ) {
60 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
61}
62
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020063static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000064
65const int *cipher_list( void )
66{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020067 const cipher_definition_t *def;
68 int *type;
69
70 if( ! supported_init )
71 {
72 def = cipher_definitions;
73 type = supported_ciphers;
74
75 while( def->type != 0 )
76 *type++ = (*def++).type;
77
78 *type = 0;
79
80 supported_init = 1;
81 }
82
Paul Bakkerd8bb8262014-06-17 14:06:49 +020083 return( supported_ciphers );
Paul Bakker72f62662011-01-16 21:27:44 +000084}
85
Paul Bakkerec1b9842012-01-14 18:24:43 +000086const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000087{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020088 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020089
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020090 for( def = cipher_definitions; def->info != NULL; def++ )
91 if( def->type == cipher_type )
92 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000093
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +000095}
96
97const cipher_info_t *cipher_info_from_string( const char *cipher_name )
98{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020099 const cipher_definition_t *def;
100
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101 if( NULL == cipher_name )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200102 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200104 for( def = cipher_definitions; def->info != NULL; def++ )
105 if( ! strcasecmp( def->info->name, cipher_name ) )
106 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000107
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200108 return( NULL );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000109}
110
Paul Bakkerf46b6952013-09-09 00:08:26 +0200111const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
112 int key_length,
113 const cipher_mode_t mode )
114{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200115 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200116
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200117 for( def = cipher_definitions; def->info != NULL; def++ )
118 if( def->info->base->cipher == cipher_id &&
119 def->info->key_length == (unsigned) key_length &&
120 def->info->mode == mode )
121 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200122
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( NULL );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200124}
125
Paul Bakker84bbeb52014-07-01 14:53:22 +0200126void cipher_init( cipher_context_t *ctx )
127{
128 memset( ctx, 0, sizeof( cipher_context_t ) );
129}
130
131void cipher_free( cipher_context_t *ctx )
132{
133 if( ctx == NULL )
134 return;
135
136 if( ctx->cipher_ctx )
137 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
138
139 polarssl_zeroize( ctx, sizeof(cipher_context_t) );
140}
141
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
143{
144 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200145 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146
Paul Bakker279432a2012-04-26 10:09:35 +0000147 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Paul Bakker343a8702011-06-09 14:27:58 +0000149 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200150 return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151
152 ctx->cipher_info = cipher_info;
153
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200154#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200155 /*
156 * Ignore possible errors caused by a cipher mode that doesn't use padding
157 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200158#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200159 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200160#else
161 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
162#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200163#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200164
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200165 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166}
167
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100168#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000169int cipher_free_ctx( cipher_context_t *ctx )
170{
Paul Bakker84bbeb52014-07-01 14:53:22 +0200171 cipher_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000172
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200173 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000174}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +0100175#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000176
177int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
178 int key_length, const operation_t operation )
179{
180 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200181 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000182
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200183 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
184 (int) ctx->cipher_info->key_length != key_length )
185 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200186 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard398c57b2014-06-23 12:10:59 +0200187 }
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200188
Paul Bakker8123e9d2011-01-06 15:37:30 +0000189 ctx->key_length = key_length;
190 ctx->operation = operation;
191
Paul Bakker343a8702011-06-09 14:27:58 +0000192 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000193 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000194 */
195 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000196 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000197 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
198 {
199 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000201 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202
Paul Bakker343a8702011-06-09 14:27:58 +0000203 if( POLARSSL_DECRYPT == operation )
204 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205 ctx->key_length );
206
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200207 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208}
209
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200210int cipher_set_iv( cipher_context_t *ctx,
211 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200213 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200214
Paul Bakker8123e9d2011-01-06 15:37:30 +0000215 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200216 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200218 /* avoid buffer overflow in ctx->iv */
219 if( iv_len > POLARSSL_MAX_IV_LENGTH )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200220 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200221
Manuel Pégourié-Gonnard81754a02014-06-23 11:33:18 +0200222 if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200223 actual_iv_size = iv_len;
224 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200225 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200226 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200227
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200228 /* avoid reading past the end of input buffer */
229 if( actual_iv_size > iv_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200230 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200231 }
232
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200233 memcpy( ctx->iv, iv, actual_iv_size );
234 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200235
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200236 return( 0 );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200237}
238
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200239int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200240{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200241 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200243
Paul Bakker8123e9d2011-01-06 15:37:30 +0000244 ctx->unprocessed_len = 0;
245
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200246 return( 0 );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200247}
248
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200249#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200250int cipher_update_ad( cipher_context_t *ctx,
251 const unsigned char *ad, size_t ad_len )
252{
253 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200254 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200255
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200256 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
257 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200259 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200260 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200261
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200262 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200264#endif /* POLARSSL_GCM_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200266int cipher_update( cipher_context_t *ctx, const unsigned char *input,
267 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000268{
Paul Bakkerff61a782011-06-09 15:42:02 +0000269 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270
Paul Bakker68884e32013-01-07 18:20:04 +0100271 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000272 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200273 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakkera885d682011-01-20 16:35:05 +0000274 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000275
Paul Bakker6c212762013-12-16 15:24:50 +0100276 *olen = 0;
277
Paul Bakker5e0efa72013-09-08 23:04:04 +0200278 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
279 {
280 if( ilen != cipher_get_block_size( ctx ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200281 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200282
283 *olen = ilen;
284
285 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
286 ctx->operation, input, output ) ) )
287 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200288 return( ret );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200289 }
290
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200291 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200292 }
293
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200294#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200295 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200296 {
297 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200298 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
299 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200300 }
301#endif
302
Paul Bakker68884e32013-01-07 18:20:04 +0100303 if( input == output &&
304 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
305 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200306 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100307 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200309#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200310 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200312 size_t copy_len = 0;
313
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314 /*
315 * If there is not enough data for a full block, cache it.
316 */
317 if( ( ctx->operation == POLARSSL_DECRYPT &&
318 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
319 ( ctx->operation == POLARSSL_ENCRYPT &&
320 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
321 {
322 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
323 ilen );
324
325 ctx->unprocessed_len += ilen;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200326 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 }
328
329 /*
330 * Process cached data first
331 */
332 if( ctx->unprocessed_len != 0 )
333 {
334 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
335
336 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
337 copy_len );
338
Paul Bakkerff61a782011-06-09 15:42:02 +0000339 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000340 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000341 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200343 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 }
345
346 *olen += cipher_get_block_size( ctx );
347 output += cipher_get_block_size( ctx );
348 ctx->unprocessed_len = 0;
349
350 input += copy_len;
351 ilen -= copy_len;
352 }
353
354 /*
355 * Cache final, incomplete block
356 */
357 if( 0 != ilen )
358 {
359 copy_len = ilen % cipher_get_block_size( ctx );
360 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
Paul Bakker66d5d072014-06-17 16:39:18 +0200361 copy_len = cipher_get_block_size( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362
363 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
364 copy_len );
365
366 ctx->unprocessed_len += copy_len;
367 ilen -= copy_len;
368 }
369
370 /*
371 * Process remaining full blocks
372 */
373 if( ilen )
374 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000375 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
376 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200378 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200380
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 *olen += ilen;
382 }
383
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200384 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200386#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387
Paul Bakker68884e32013-01-07 18:20:04 +0100388#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000389 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000390 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000391 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000392 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000393 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000394 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200395 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000396 }
397
398 *olen = ilen;
399
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200400 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000401 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200402#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000403
Paul Bakker68884e32013-01-07 18:20:04 +0100404#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000405 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
406 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000407 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000408 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000409 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000410 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200411 return( ret );
Paul Bakker343a8702011-06-09 14:27:58 +0000412 }
413
414 *olen = ilen;
415
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200416 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000417 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200418#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000419
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200420#if defined(POLARSSL_CIPHER_MODE_STREAM)
421 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
422 {
423 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
424 ilen, input, output ) ) )
425 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200426 return( ret );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200427 }
428
429 *olen = ilen;
430
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200431 return( 0 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200432 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200433#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200434
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200435 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436}
437
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200438#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200439#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200440/*
441 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
442 */
Paul Bakker23986e52011-04-24 08:57:21 +0000443static void add_pkcs_padding( unsigned char *output, size_t output_len,
444 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000445{
Paul Bakker23986e52011-04-24 08:57:21 +0000446 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100447 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448
449 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000450 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451}
452
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200453static int get_pkcs_padding( unsigned char *input, size_t input_len,
454 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100456 size_t i, pad_idx;
457 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
Paul Bakkera885d682011-01-20 16:35:05 +0000459 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200460 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461
462 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463 *data_len = input_len - padding_len;
464
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100465 /* Avoid logical || since it results in a branch */
466 bad |= padding_len > input_len;
467 bad |= padding_len == 0;
468
469 /* The number of bytes checked must be independent of padding_len,
470 * so pick input_len, which is usually 8 or 16 (one block) */
471 pad_idx = input_len - padding_len;
472 for( i = 0; i < input_len; i++ )
473 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
474
Paul Bakker66d5d072014-06-17 16:39:18 +0200475 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476}
Paul Bakker48e93c82013-08-14 12:21:18 +0200477#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000478
Paul Bakker48e93c82013-08-14 12:21:18 +0200479#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200480/*
481 * One and zeros padding: fill with 80 00 ... 00
482 */
483static void add_one_and_zeros_padding( unsigned char *output,
484 size_t output_len, size_t data_len )
485{
486 size_t padding_len = output_len - data_len;
487 unsigned char i = 0;
488
489 output[data_len] = 0x80;
490 for( i = 1; i < padding_len; i++ )
491 output[data_len + i] = 0x00;
492}
493
494static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
495 size_t *data_len )
496{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100497 size_t i;
498 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200499
500 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200501 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200502
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100503 bad = 0xFF;
504 *data_len = 0;
505 for( i = input_len; i > 0; i-- )
506 {
507 prev_done = done;
508 done |= ( input[i-1] != 0 );
509 *data_len |= ( i - 1 ) * ( done != prev_done );
510 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
511 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200512
Paul Bakker66d5d072014-06-17 16:39:18 +0200513 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200514
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200515}
Paul Bakker48e93c82013-08-14 12:21:18 +0200516#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200517
Paul Bakker48e93c82013-08-14 12:21:18 +0200518#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200519/*
520 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
521 */
522static void add_zeros_and_len_padding( unsigned char *output,
523 size_t output_len, size_t data_len )
524{
525 size_t padding_len = output_len - data_len;
526 unsigned char i = 0;
527
528 for( i = 1; i < padding_len; i++ )
529 output[data_len + i - 1] = 0x00;
530 output[output_len - 1] = (unsigned char) padding_len;
531}
532
533static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
534 size_t *data_len )
535{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100536 size_t i, pad_idx;
537 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200538
539 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200540 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200541
542 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200543 *data_len = input_len - padding_len;
544
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100545 /* Avoid logical || since it results in a branch */
546 bad |= padding_len > input_len;
547 bad |= padding_len == 0;
548
549 /* The number of bytes checked must be independent of padding_len */
550 pad_idx = input_len - padding_len;
551 for( i = 0; i < input_len - 1; i++ )
552 bad |= input[i] * ( i >= pad_idx );
553
Paul Bakker66d5d072014-06-17 16:39:18 +0200554 return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200555}
Paul Bakker48e93c82013-08-14 12:21:18 +0200556#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200557
Paul Bakker48e93c82013-08-14 12:21:18 +0200558#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200559/*
560 * Zero padding: fill with 00 ... 00
561 */
562static void add_zeros_padding( unsigned char *output,
563 size_t output_len, size_t data_len )
564{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200565 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200566
567 for( i = data_len; i < output_len; i++ )
568 output[i] = 0x00;
569}
570
571static int get_zeros_padding( unsigned char *input, size_t input_len,
572 size_t *data_len )
573{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100574 size_t i;
575 unsigned char done = 0, prev_done;
576
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200577 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200578 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200579
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100580 *data_len = 0;
581 for( i = input_len; i > 0; i-- )
582 {
583 prev_done = done;
584 done |= ( input[i-1] != 0 );
585 *data_len |= i * ( done != prev_done );
586 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200587
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200588 return( 0 );
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200589}
Paul Bakker48e93c82013-08-14 12:21:18 +0200590#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200591
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200592/*
593 * No padding: don't pad :)
594 *
595 * There is no add_padding function (check for NULL in cipher_finish)
596 * but a trivial get_padding function
597 */
598static int get_no_padding( unsigned char *input, size_t input_len,
599 size_t *data_len )
600{
601 if( NULL == input || NULL == data_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200602 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200603
604 *data_len = input_len;
605
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200606 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200607}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200608#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200609
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200610int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200611 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612{
613 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200614 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000615
616 *olen = 0;
617
Paul Bakker6132d0a2012-07-04 17:10:40 +0000618 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000619 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200620 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200621 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000622 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200623 return( 0 );
Paul Bakker343a8702011-06-09 14:27:58 +0000624 }
625
Paul Bakker5e0efa72013-09-08 23:04:04 +0200626 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
627 {
628 if( ctx->unprocessed_len != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200629 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200630
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200631 return( 0 );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200632 }
633
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200634#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000635 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
636 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200637 int ret = 0;
638
Paul Bakker8123e9d2011-01-06 15:37:30 +0000639 if( POLARSSL_ENCRYPT == ctx->operation )
640 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200641 /* check for 'no padding' mode */
642 if( NULL == ctx->add_padding )
643 {
644 if( 0 != ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200645 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200646
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200647 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200648 }
649
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200650 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651 ctx->unprocessed_len );
652 }
Paul Bakker66d5d072014-06-17 16:39:18 +0200653 else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200655 /*
656 * For decrypt operations, expect a full block,
657 * or an empty block if no padding
658 */
659 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200660 return( 0 );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200661
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200662 return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663 }
664
665 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000666 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
667 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
668 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200670 return( ret );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671 }
672
673 /* Set output size for decryption */
674 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200675 return ctx->get_padding( output, cipher_get_block_size( ctx ),
676 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677
678 /* Set output size for encryption */
679 *olen = cipher_get_block_size( ctx );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200680 return( 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200682#else
683 ((void) output);
684#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200686 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687}
688
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200689#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200690int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
691{
692 if( NULL == ctx ||
693 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
694 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200695 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200696 }
697
Paul Bakker1a45d912013-08-14 12:04:26 +0200698 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200699 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200700#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200702 ctx->add_padding = add_pkcs_padding;
703 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200704 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200705#endif
706#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200707 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200708 ctx->add_padding = add_one_and_zeros_padding;
709 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200710 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200711#endif
712#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200713 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200714 ctx->add_padding = add_zeros_and_len_padding;
715 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200716 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200717#endif
718#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200719 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200720 ctx->add_padding = add_zeros_padding;
721 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200722 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200723#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200724 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200725 ctx->add_padding = NULL;
726 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200727 break;
728
729 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200730 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200731 }
732
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200733 return( 0 );
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200734}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200735#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200736
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200737#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200738int cipher_write_tag( cipher_context_t *ctx,
739 unsigned char *tag, size_t tag_len )
740{
741 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200742 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200743
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200744 if( POLARSSL_ENCRYPT != ctx->operation )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200745 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200746
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200747 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200748 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200749
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200750 return( 0 );
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200751}
Paul Bakker9af723c2014-05-01 13:03:14 +0200752
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200753int cipher_check_tag( cipher_context_t *ctx,
754 const unsigned char *tag, size_t tag_len )
755{
756 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200757
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200758 if( NULL == ctx || NULL == ctx->cipher_info ||
759 POLARSSL_DECRYPT != ctx->operation )
760 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200761 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200762 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200763
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200764 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
765 {
766 unsigned char check_tag[16];
767 size_t i;
768 int diff;
769
770 if( tag_len > sizeof( check_tag ) )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200771 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200772
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200773 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
774 check_tag, tag_len ) ) )
775 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200777 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200778
779 /* Check the tag in "constant-time" */
780 for( diff = 0, i = 0; i < tag_len; i++ )
781 diff |= tag[i] ^ check_tag[i];
782
783 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200784 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200785
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200786 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200787 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200788
789 return( 0 );
790}
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200791#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200792
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200793/*
794 * Packet-oriented wrapper for non-AEAD modes
795 */
796int cipher_crypt( cipher_context_t *ctx,
797 const unsigned char *iv, size_t iv_len,
798 const unsigned char *input, size_t ilen,
799 unsigned char *output, size_t *olen )
800{
801 int ret;
802 size_t finish_olen;
803
804 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
805 return( ret );
806
807 if( ( ret = cipher_reset( ctx ) ) != 0 )
808 return( ret );
809
810 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
811 return( ret );
812
813 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
814 return( ret );
815
816 *olen += finish_olen;
817
818 return( 0 );
819}
820
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200821#if defined(POLARSSL_CIPHER_MODE_AEAD)
822/*
823 * Packet-oriented encryption for AEAD modes
824 */
825int cipher_auth_encrypt( cipher_context_t *ctx,
826 const unsigned char *iv, size_t iv_len,
827 const unsigned char *ad, size_t ad_len,
828 const unsigned char *input, size_t ilen,
829 unsigned char *output, size_t *olen,
830 unsigned char *tag, size_t tag_len )
831{
832#if defined(POLARSSL_GCM_C)
833 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
834 {
835 *olen = ilen;
836 return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
837 iv, iv_len, ad, ad_len, input, output,
838 tag_len, tag ) );
839 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200840#endif /* POLARSSL_GCM_C */
841#if defined(POLARSSL_CCM_C)
842 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
843 {
844 *olen = ilen;
845 return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
846 iv, iv_len, ad, ad_len, input, output,
847 tag, tag_len ) );
848 }
849#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200850
851 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
852}
853
854/*
855 * Packet-oriented decryption for AEAD modes
856 */
857int cipher_auth_decrypt( cipher_context_t *ctx,
858 const unsigned char *iv, size_t iv_len,
859 const unsigned char *ad, size_t ad_len,
860 const unsigned char *input, size_t ilen,
861 unsigned char *output, size_t *olen,
862 const unsigned char *tag, size_t tag_len )
863{
864#if defined(POLARSSL_GCM_C)
865 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
866 {
867 int ret;
868
869 *olen = ilen;
870 ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
871 iv, iv_len, ad, ad_len,
872 tag, tag_len, input, output );
873
874 if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
875 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
876
877 return( ret );
878 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200879#endif /* POLARSSL_GCM_C */
880#if defined(POLARSSL_CCM_C)
881 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
882 {
883 int ret;
884
885 *olen = ilen;
886 ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
887 iv, iv_len, ad, ad_len,
888 input, output, tag, tag_len );
889
890 if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
891 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
892
893 return( ret );
894 }
895#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200896
897 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
898}
899#endif /* POLARSSL_CIPHER_MODE_AEAD */
900
901
Paul Bakker8123e9d2011-01-06 15:37:30 +0000902#if defined(POLARSSL_SELF_TEST)
903
Paul Bakker8123e9d2011-01-06 15:37:30 +0000904/*
905 * Checkup routine
906 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000907int cipher_self_test( int verbose )
908{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000909 ((void) verbose);
910
Paul Bakker8123e9d2011-01-06 15:37:30 +0000911 return( 0 );
912}
913
Paul Bakker9af723c2014-05-01 13:03:14 +0200914#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000915
Paul Bakker9af723c2014-05-01 13:03:14 +0200916#endif /* POLARSSL_CIPHER_C */