blob: 4f76b487aa5a95d2f8f06ae26b63e5000e95b028 [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
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020060static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000061
62const int *cipher_list( void )
63{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020064 const cipher_definition_t *def;
65 int *type;
66
67 if( ! supported_init )
68 {
69 def = cipher_definitions;
70 type = supported_ciphers;
71
72 while( def->type != 0 )
73 *type++ = (*def++).type;
74
75 *type = 0;
76
77 supported_init = 1;
78 }
79
Paul Bakker72f62662011-01-16 21:27:44 +000080 return supported_ciphers;
81}
82
Paul Bakkerec1b9842012-01-14 18:24:43 +000083const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000084{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020085 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020086
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020087 for( def = cipher_definitions; def->info != NULL; def++ )
88 if( def->type == cipher_type )
89 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000090
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020091 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000092}
93
94const cipher_info_t *cipher_info_from_string( const char *cipher_name )
95{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020096 const cipher_definition_t *def;
97
Paul Bakker8123e9d2011-01-06 15:37:30 +000098 if( NULL == cipher_name )
99 return NULL;
100
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200101 for( def = cipher_definitions; def->info != NULL; def++ )
102 if( ! strcasecmp( def->info->name, cipher_name ) )
103 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000104
Paul Bakker8123e9d2011-01-06 15:37:30 +0000105 return NULL;
106}
107
Paul Bakkerf46b6952013-09-09 00:08:26 +0200108const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
109 int key_length,
110 const cipher_mode_t mode )
111{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200112 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200113
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200114 for( def = cipher_definitions; def->info != NULL; def++ )
115 if( def->info->base->cipher == cipher_id &&
116 def->info->key_length == (unsigned) key_length &&
117 def->info->mode == mode )
118 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200119
120 return NULL;
121}
122
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
124{
125 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000126 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127
Paul Bakker279432a2012-04-26 10:09:35 +0000128 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129
Paul Bakker343a8702011-06-09 14:27:58 +0000130 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000131 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132
133 ctx->cipher_info = cipher_info;
134
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200135#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200136 /*
137 * Ignore possible errors caused by a cipher mode that doesn't use padding
138 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200139#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200140 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200141#else
142 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
143#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200144#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200145
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146 return 0;
147}
148
149int cipher_free_ctx( cipher_context_t *ctx )
150{
151 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000152 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153
Paul Bakker343a8702011-06-09 14:27:58 +0000154 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155
156 return 0;
157}
158
159int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
160 int key_length, const operation_t operation )
161{
162 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000163 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200165 if( (int) ctx->cipher_info->key_length != key_length )
166 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
167
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168 ctx->key_length = key_length;
169 ctx->operation = operation;
170
Paul Bakker343a8702011-06-09 14:27:58 +0000171 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000172 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000173 */
174 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000175 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000176 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
177 {
178 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000180 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000181
Paul Bakker343a8702011-06-09 14:27:58 +0000182 if( POLARSSL_DECRYPT == operation )
183 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184 ctx->key_length );
185
Paul Bakkerff61a782011-06-09 15:42:02 +0000186 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187}
188
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200189int cipher_set_iv( cipher_context_t *ctx,
190 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000191{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200192 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200193
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000195 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000196
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200197 /* avoid buffer overflow in ctx->iv */
198 if( iv_len > POLARSSL_MAX_IV_LENGTH )
199 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
200
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200201 if( ctx->cipher_info->accepts_variable_iv_size )
202 actual_iv_size = iv_len;
203 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200204 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200205 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200206
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200207 /* avoid reading past the end of input buffer */
208 if( actual_iv_size > iv_len )
209 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
210 }
211
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200212 memcpy( ctx->iv, iv, actual_iv_size );
213 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200214
215 return 0;
216}
217
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200218int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200219{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200220 if( NULL == ctx || NULL == ctx->cipher_info )
221 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
222
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223 ctx->unprocessed_len = 0;
224
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200225 return 0;
226}
227
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200228#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200229int cipher_update_ad( cipher_context_t *ctx,
230 const unsigned char *ad, size_t ad_len )
231{
232 if( NULL == ctx || NULL == ctx->cipher_info )
233 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
234
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200235#if defined(POLARSSL_GCM_C)
236 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
237 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200238 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200239 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200240 }
241#endif
242
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243 return 0;
244}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200245#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000246
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200247int cipher_update( cipher_context_t *ctx, const unsigned char *input,
248 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000249{
Paul Bakkerff61a782011-06-09 15:42:02 +0000250 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251
Paul Bakker68884e32013-01-07 18:20:04 +0100252 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000253 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000254 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000255 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256
Paul Bakker6c212762013-12-16 15:24:50 +0100257 *olen = 0;
258
Paul Bakker5e0efa72013-09-08 23:04:04 +0200259 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
260 {
261 if( ilen != cipher_get_block_size( ctx ) )
262 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
263
264 *olen = ilen;
265
266 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
267 ctx->operation, input, output ) ) )
268 {
269 return ret;
270 }
271
272 return 0;
273 }
274
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200275#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200276 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200277 {
278 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200279 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
280 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200281 }
282#endif
283
Paul Bakker68884e32013-01-07 18:20:04 +0100284 if( input == output &&
285 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
286 {
287 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
288 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000289
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200290#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200291 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000292 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200293 size_t copy_len = 0;
294
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295 /*
296 * If there is not enough data for a full block, cache it.
297 */
298 if( ( ctx->operation == POLARSSL_DECRYPT &&
299 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
300 ( ctx->operation == POLARSSL_ENCRYPT &&
301 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
302 {
303 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
304 ilen );
305
306 ctx->unprocessed_len += ilen;
307 return 0;
308 }
309
310 /*
311 * Process cached data first
312 */
313 if( ctx->unprocessed_len != 0 )
314 {
315 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
316
317 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
318 copy_len );
319
Paul Bakkerff61a782011-06-09 15:42:02 +0000320 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000322 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000324 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325 }
326
327 *olen += cipher_get_block_size( ctx );
328 output += cipher_get_block_size( ctx );
329 ctx->unprocessed_len = 0;
330
331 input += copy_len;
332 ilen -= copy_len;
333 }
334
335 /*
336 * Cache final, incomplete block
337 */
338 if( 0 != ilen )
339 {
340 copy_len = ilen % cipher_get_block_size( ctx );
341 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
342 copy_len = cipher_get_block_size(ctx);
343
344 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
345 copy_len );
346
347 ctx->unprocessed_len += copy_len;
348 ilen -= copy_len;
349 }
350
351 /*
352 * Process remaining full blocks
353 */
354 if( ilen )
355 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000356 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
357 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000359 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200361
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362 *olen += ilen;
363 }
364
365 return 0;
366 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200367#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368
Paul Bakker68884e32013-01-07 18:20:04 +0100369#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000370 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000371 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000372 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000373 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000374 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000375 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000376 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000377 }
378
379 *olen = ilen;
380
381 return 0;
382 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200383#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000384
Paul Bakker68884e32013-01-07 18:20:04 +0100385#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000386 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
387 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000388 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000389 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000390 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000391 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000392 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000393 }
394
395 *olen = ilen;
396
397 return 0;
398 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200399#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000400
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200401#if defined(POLARSSL_CIPHER_MODE_STREAM)
402 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
403 {
404 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
405 ilen, input, output ) ) )
406 {
407 return ret;
408 }
409
410 *olen = ilen;
411
412 return 0;
413 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200414#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200415
Paul Bakkerff61a782011-06-09 15:42:02 +0000416 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417}
418
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200419#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200420#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200421/*
422 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
423 */
Paul Bakker23986e52011-04-24 08:57:21 +0000424static void add_pkcs_padding( unsigned char *output, size_t output_len,
425 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426{
Paul Bakker23986e52011-04-24 08:57:21 +0000427 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100428 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429
430 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000431 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432}
433
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200434static int get_pkcs_padding( unsigned char *input, size_t input_len,
435 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100437 size_t i, pad_idx;
438 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000439
Paul Bakkera885d682011-01-20 16:35:05 +0000440 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000441 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000442
443 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 *data_len = input_len - padding_len;
445
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100446 /* Avoid logical || since it results in a branch */
447 bad |= padding_len > input_len;
448 bad |= padding_len == 0;
449
450 /* The number of bytes checked must be independent of padding_len,
451 * so pick input_len, which is usually 8 or 16 (one block) */
452 pad_idx = input_len - padding_len;
453 for( i = 0; i < input_len; i++ )
454 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
455
456 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457}
Paul Bakker48e93c82013-08-14 12:21:18 +0200458#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
Paul Bakker48e93c82013-08-14 12:21:18 +0200460#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200461/*
462 * One and zeros padding: fill with 80 00 ... 00
463 */
464static void add_one_and_zeros_padding( unsigned char *output,
465 size_t output_len, size_t data_len )
466{
467 size_t padding_len = output_len - data_len;
468 unsigned char i = 0;
469
470 output[data_len] = 0x80;
471 for( i = 1; i < padding_len; i++ )
472 output[data_len + i] = 0x00;
473}
474
475static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
476 size_t *data_len )
477{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100478 size_t i;
479 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200480
481 if( NULL == input || NULL == data_len )
482 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
483
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100484 bad = 0xFF;
485 *data_len = 0;
486 for( i = input_len; i > 0; i-- )
487 {
488 prev_done = done;
489 done |= ( input[i-1] != 0 );
490 *data_len |= ( i - 1 ) * ( done != prev_done );
491 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
492 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200493
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100494 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200495
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200496}
Paul Bakker48e93c82013-08-14 12:21:18 +0200497#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200498
Paul Bakker48e93c82013-08-14 12:21:18 +0200499#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200500/*
501 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
502 */
503static void add_zeros_and_len_padding( unsigned char *output,
504 size_t output_len, size_t data_len )
505{
506 size_t padding_len = output_len - data_len;
507 unsigned char i = 0;
508
509 for( i = 1; i < padding_len; i++ )
510 output[data_len + i - 1] = 0x00;
511 output[output_len - 1] = (unsigned char) padding_len;
512}
513
514static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
515 size_t *data_len )
516{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100517 size_t i, pad_idx;
518 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200519
520 if( NULL == input || NULL == data_len )
521 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
522
523 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200524 *data_len = input_len - padding_len;
525
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100526 /* Avoid logical || since it results in a branch */
527 bad |= padding_len > input_len;
528 bad |= padding_len == 0;
529
530 /* The number of bytes checked must be independent of padding_len */
531 pad_idx = input_len - padding_len;
532 for( i = 0; i < input_len - 1; i++ )
533 bad |= input[i] * ( i >= pad_idx );
534
535 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200536}
Paul Bakker48e93c82013-08-14 12:21:18 +0200537#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200538
Paul Bakker48e93c82013-08-14 12:21:18 +0200539#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200540/*
541 * Zero padding: fill with 00 ... 00
542 */
543static void add_zeros_padding( unsigned char *output,
544 size_t output_len, size_t data_len )
545{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200546 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200547
548 for( i = data_len; i < output_len; i++ )
549 output[i] = 0x00;
550}
551
552static int get_zeros_padding( unsigned char *input, size_t input_len,
553 size_t *data_len )
554{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100555 size_t i;
556 unsigned char done = 0, prev_done;
557
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200558 if( NULL == input || NULL == data_len )
559 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
560
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100561 *data_len = 0;
562 for( i = input_len; i > 0; i-- )
563 {
564 prev_done = done;
565 done |= ( input[i-1] != 0 );
566 *data_len |= i * ( done != prev_done );
567 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200568
569 return 0;
570}
Paul Bakker48e93c82013-08-14 12:21:18 +0200571#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200572
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200573/*
574 * No padding: don't pad :)
575 *
576 * There is no add_padding function (check for NULL in cipher_finish)
577 * but a trivial get_padding function
578 */
579static int get_no_padding( unsigned char *input, size_t input_len,
580 size_t *data_len )
581{
582 if( NULL == input || NULL == data_len )
583 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
584
585 *data_len = input_len;
586
587 return 0;
588}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200589#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200590
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200591int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200592 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593{
594 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000595 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596
597 *olen = 0;
598
Paul Bakker6132d0a2012-07-04 17:10:40 +0000599 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000600 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200601 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200602 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000603 {
604 return 0;
605 }
606
Paul Bakker5e0efa72013-09-08 23:04:04 +0200607 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
608 {
609 if( ctx->unprocessed_len != 0 )
610 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
611
612 return 0;
613 }
614
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200615#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
617 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200618 int ret = 0;
619
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620 if( POLARSSL_ENCRYPT == ctx->operation )
621 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200622 /* check for 'no padding' mode */
623 if( NULL == ctx->add_padding )
624 {
625 if( 0 != ctx->unprocessed_len )
626 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
627
628 return 0;
629 }
630
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200631 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632 ctx->unprocessed_len );
633 }
634 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
635 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200636 /*
637 * For decrypt operations, expect a full block,
638 * or an empty block if no padding
639 */
640 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
641 return 0;
642
Paul Bakkerff61a782011-06-09 15:42:02 +0000643 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644 }
645
646 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000647 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
648 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
649 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000651 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 }
653
654 /* Set output size for decryption */
655 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200656 return ctx->get_padding( output, cipher_get_block_size( ctx ),
657 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
659 /* Set output size for encryption */
660 *olen = cipher_get_block_size( ctx );
661 return 0;
662 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200663#else
664 ((void) output);
665#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666
Paul Bakkerff61a782011-06-09 15:42:02 +0000667 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668}
669
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200670#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200671int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
672{
673 if( NULL == ctx ||
674 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
675 {
676 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
677 }
678
Paul Bakker1a45d912013-08-14 12:04:26 +0200679 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200680 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200681#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200682 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200683 ctx->add_padding = add_pkcs_padding;
684 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200685 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200686#endif
687#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200688 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200689 ctx->add_padding = add_one_and_zeros_padding;
690 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200691 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200692#endif
693#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200694 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200695 ctx->add_padding = add_zeros_and_len_padding;
696 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200697 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200698#endif
699#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200700 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200701 ctx->add_padding = add_zeros_padding;
702 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200703 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200704#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200705 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200706 ctx->add_padding = NULL;
707 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200708 break;
709
710 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200711 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200712 }
713
Paul Bakker1a45d912013-08-14 12:04:26 +0200714 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200715}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200716#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200717
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200718#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200719int cipher_write_tag( cipher_context_t *ctx,
720 unsigned char *tag, size_t tag_len )
721{
722 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
723 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
724
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200725 if( POLARSSL_ENCRYPT != ctx->operation )
726 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
727
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200728#if defined(POLARSSL_GCM_C)
729 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200730 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200731#endif
732
733 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200734}
Paul Bakker9af723c2014-05-01 13:03:14 +0200735
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200736int cipher_check_tag( cipher_context_t *ctx,
737 const unsigned char *tag, size_t tag_len )
738{
739 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200740
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200741 if( NULL == ctx || NULL == ctx->cipher_info ||
742 POLARSSL_DECRYPT != ctx->operation )
743 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200744 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200745 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200746
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200747#if defined(POLARSSL_GCM_C)
748 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
749 {
750 unsigned char check_tag[16];
751 size_t i;
752 int diff;
753
754 if( tag_len > sizeof( check_tag ) )
755 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
756
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200757 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
758 check_tag, tag_len ) ) )
759 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200760 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200761 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200762
763 /* Check the tag in "constant-time" */
764 for( diff = 0, i = 0; i < tag_len; i++ )
765 diff |= tag[i] ^ check_tag[i];
766
767 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200768 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200769
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200770 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200771 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200772#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200773
774 return( 0 );
775}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200776#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200777
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200778/*
779 * Packet-oriented wrapper for non-AEAD modes
780 */
781int cipher_crypt( cipher_context_t *ctx,
782 const unsigned char *iv, size_t iv_len,
783 const unsigned char *input, size_t ilen,
784 unsigned char *output, size_t *olen )
785{
786 int ret;
787 size_t finish_olen;
788
789 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
790 return( ret );
791
792 if( ( ret = cipher_reset( ctx ) ) != 0 )
793 return( ret );
794
795 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
796 return( ret );
797
798 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
799 return( ret );
800
801 *olen += finish_olen;
802
803 return( 0 );
804}
805
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200806#if defined(POLARSSL_CIPHER_MODE_AEAD)
807/*
808 * Packet-oriented encryption for AEAD modes
809 */
810int cipher_auth_encrypt( cipher_context_t *ctx,
811 const unsigned char *iv, size_t iv_len,
812 const unsigned char *ad, size_t ad_len,
813 const unsigned char *input, size_t ilen,
814 unsigned char *output, size_t *olen,
815 unsigned char *tag, size_t tag_len )
816{
817#if defined(POLARSSL_GCM_C)
818 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
819 {
820 *olen = ilen;
821 return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
822 iv, iv_len, ad, ad_len, input, output,
823 tag_len, tag ) );
824 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200825#endif /* POLARSSL_GCM_C */
826#if defined(POLARSSL_CCM_C)
827 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
828 {
829 *olen = ilen;
830 return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
831 iv, iv_len, ad, ad_len, input, output,
832 tag, tag_len ) );
833 }
834#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200835
836 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
837}
838
839/*
840 * Packet-oriented decryption for AEAD modes
841 */
842int cipher_auth_decrypt( cipher_context_t *ctx,
843 const unsigned char *iv, size_t iv_len,
844 const unsigned char *ad, size_t ad_len,
845 const unsigned char *input, size_t ilen,
846 unsigned char *output, size_t *olen,
847 const unsigned char *tag, size_t tag_len )
848{
849#if defined(POLARSSL_GCM_C)
850 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
851 {
852 int ret;
853
854 *olen = ilen;
855 ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
856 iv, iv_len, ad, ad_len,
857 tag, tag_len, input, output );
858
859 if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
860 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
861
862 return( ret );
863 }
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200864#endif /* POLARSSL_GCM_C */
865#if defined(POLARSSL_CCM_C)
866 if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
867 {
868 int ret;
869
870 *olen = ilen;
871 ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
872 iv, iv_len, ad, ad_len,
873 input, output, tag, tag_len );
874
875 if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
876 ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
877
878 return( ret );
879 }
880#endif /* POLARSSL_CCM_C */
Manuel Pégourié-Gonnard4562ffe2014-05-13 12:19:29 +0200881
882 return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
883}
884#endif /* POLARSSL_CIPHER_MODE_AEAD */
885
886
Paul Bakker8123e9d2011-01-06 15:37:30 +0000887#if defined(POLARSSL_SELF_TEST)
888
Paul Bakker8123e9d2011-01-06 15:37:30 +0000889/*
890 * Checkup routine
891 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000892int cipher_self_test( int verbose )
893{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000894 ((void) verbose);
895
Paul Bakker8123e9d2011-01-06 15:37:30 +0000896 return( 0 );
897}
898
Paul Bakker9af723c2014-05-01 13:03:14 +0200899#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000900
Paul Bakker9af723c2014-05-01 13:03:14 +0200901#endif /* POLARSSL_CIPHER_C */