blob: daeea13767c8f3224c0e894f4eedb0d0d4e6168c [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
Paul Bakker8123e9d2011-01-06 15:37:30 +000045#include <stdlib.h>
46
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020047#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020048#define POLARSSL_CIPHER_MODE_STREAM
49#endif
50
Paul Bakker6edcd412013-10-29 15:22:54 +010051#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
52 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000053#define strcasecmp _stricmp
54#endif
55
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020056static int supported_init = 0;
Paul Bakker72f62662011-01-16 21:27:44 +000057
58const int *cipher_list( void )
59{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020060 const cipher_definition_t *def;
61 int *type;
62
63 if( ! supported_init )
64 {
65 def = cipher_definitions;
66 type = supported_ciphers;
67
68 while( def->type != 0 )
69 *type++ = (*def++).type;
70
71 *type = 0;
72
73 supported_init = 1;
74 }
75
Paul Bakker72f62662011-01-16 21:27:44 +000076 return supported_ciphers;
77}
78
Paul Bakkerec1b9842012-01-14 18:24:43 +000079const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +000080{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020081 const cipher_definition_t *def;
Paul Bakker5e0efa72013-09-08 23:04:04 +020082
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020083 for( def = cipher_definitions; def->info != NULL; def++ )
84 if( def->type == cipher_type )
85 return( def->info );
Paul Bakker343a8702011-06-09 14:27:58 +000086
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020087 return NULL;
Paul Bakker8123e9d2011-01-06 15:37:30 +000088}
89
90const cipher_info_t *cipher_info_from_string( const char *cipher_name )
91{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020092 const cipher_definition_t *def;
93
Paul Bakker8123e9d2011-01-06 15:37:30 +000094 if( NULL == cipher_name )
95 return NULL;
96
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +020097 for( def = cipher_definitions; def->info != NULL; def++ )
98 if( ! strcasecmp( def->info->name, cipher_name ) )
99 return( def->info );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000100
Paul Bakker8123e9d2011-01-06 15:37:30 +0000101 return NULL;
102}
103
Paul Bakkerf46b6952013-09-09 00:08:26 +0200104const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
105 int key_length,
106 const cipher_mode_t mode )
107{
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200108 const cipher_definition_t *def;
Paul Bakkerf46b6952013-09-09 00:08:26 +0200109
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +0200110 for( def = cipher_definitions; def->info != NULL; def++ )
111 if( def->info->base->cipher == cipher_id &&
112 def->info->key_length == (unsigned) key_length &&
113 def->info->mode == mode )
114 return( def->info );
Paul Bakkerf46b6952013-09-09 00:08:26 +0200115
116 return NULL;
117}
118
Paul Bakker8123e9d2011-01-06 15:37:30 +0000119int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
120{
121 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000122 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123
Paul Bakker279432a2012-04-26 10:09:35 +0000124 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125
Paul Bakker343a8702011-06-09 14:27:58 +0000126 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000127 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128
129 ctx->cipher_info = cipher_info;
130
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200131#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200132 /*
133 * Ignore possible errors caused by a cipher mode that doesn't use padding
134 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200135#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200136 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200137#else
138 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
139#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200140#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200141
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142 return 0;
143}
144
145int cipher_free_ctx( cipher_context_t *ctx )
146{
147 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000148 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149
Paul Bakker343a8702011-06-09 14:27:58 +0000150 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151
152 return 0;
153}
154
155int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
156 int key_length, const operation_t operation )
157{
158 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000159 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200161 if( (int) ctx->cipher_info->key_length != key_length )
162 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
163
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164 ctx->key_length = key_length;
165 ctx->operation = operation;
166
Paul Bakker343a8702011-06-09 14:27:58 +0000167 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000168 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000169 */
170 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000171 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000172 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
173 {
174 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000176 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000177
Paul Bakker343a8702011-06-09 14:27:58 +0000178 if( POLARSSL_DECRYPT == operation )
179 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180 ctx->key_length );
181
Paul Bakkerff61a782011-06-09 15:42:02 +0000182 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000183}
184
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200185int cipher_set_iv( cipher_context_t *ctx,
186 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000187{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200188 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200189
Paul Bakker8123e9d2011-01-06 15:37:30 +0000190 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000191 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200193 /* avoid buffer overflow in ctx->iv */
194 if( iv_len > POLARSSL_MAX_IV_LENGTH )
195 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
196
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200197 if( ctx->cipher_info->accepts_variable_iv_size )
198 actual_iv_size = iv_len;
199 else
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200200 {
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200201 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200202
Manuel Pégourié-Gonnarde0dca4a2013-10-24 16:54:25 +0200203 /* avoid reading past the end of input buffer */
204 if( actual_iv_size > iv_len )
205 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
206 }
207
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200208 memcpy( ctx->iv, iv, actual_iv_size );
209 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200210
211 return 0;
212}
213
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200214int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200215{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200216 if( NULL == ctx || NULL == ctx->cipher_info )
217 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
218
Paul Bakker8123e9d2011-01-06 15:37:30 +0000219 ctx->unprocessed_len = 0;
220
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200221 return 0;
222}
223
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200224#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200225int cipher_update_ad( cipher_context_t *ctx,
226 const unsigned char *ad, size_t ad_len )
227{
228 if( NULL == ctx || NULL == ctx->cipher_info )
229 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
230
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200231#if defined(POLARSSL_GCM_C)
232 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
233 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200234 return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200235 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200236 }
237#endif
238
Paul Bakker8123e9d2011-01-06 15:37:30 +0000239 return 0;
240}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200241#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200243int cipher_update( cipher_context_t *ctx, const unsigned char *input,
244 size_t ilen, unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245{
Paul Bakkerff61a782011-06-09 15:42:02 +0000246 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000247
Paul Bakker68884e32013-01-07 18:20:04 +0100248 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000249 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000250 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000251 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000252
Paul Bakker6c212762013-12-16 15:24:50 +0100253 *olen = 0;
254
Paul Bakker5e0efa72013-09-08 23:04:04 +0200255 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
256 {
257 if( ilen != cipher_get_block_size( ctx ) )
258 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
259
260 *olen = ilen;
261
262 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
263 ctx->operation, input, output ) ) )
264 {
265 return ret;
266 }
267
268 return 0;
269 }
270
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200271#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200272 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200273 {
274 *olen = ilen;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200275 return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
276 output );
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200277 }
278#endif
279
Paul Bakker68884e32013-01-07 18:20:04 +0100280 if( input == output &&
281 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
282 {
283 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
284 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200286#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200287 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200289 size_t copy_len = 0;
290
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291 /*
292 * If there is not enough data for a full block, cache it.
293 */
294 if( ( ctx->operation == POLARSSL_DECRYPT &&
295 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
296 ( ctx->operation == POLARSSL_ENCRYPT &&
297 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
298 {
299 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
300 ilen );
301
302 ctx->unprocessed_len += ilen;
303 return 0;
304 }
305
306 /*
307 * Process cached data first
308 */
309 if( ctx->unprocessed_len != 0 )
310 {
311 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
312
313 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
314 copy_len );
315
Paul Bakkerff61a782011-06-09 15:42:02 +0000316 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000318 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000320 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321 }
322
323 *olen += cipher_get_block_size( ctx );
324 output += cipher_get_block_size( ctx );
325 ctx->unprocessed_len = 0;
326
327 input += copy_len;
328 ilen -= copy_len;
329 }
330
331 /*
332 * Cache final, incomplete block
333 */
334 if( 0 != ilen )
335 {
336 copy_len = ilen % cipher_get_block_size( ctx );
337 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
338 copy_len = cipher_get_block_size(ctx);
339
340 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
341 copy_len );
342
343 ctx->unprocessed_len += copy_len;
344 ilen -= copy_len;
345 }
346
347 /*
348 * Process remaining full blocks
349 */
350 if( ilen )
351 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000352 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
353 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000355 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200357
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358 *olen += ilen;
359 }
360
361 return 0;
362 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200363#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000364
Paul Bakker68884e32013-01-07 18:20:04 +0100365#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000366 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000367 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000368 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000369 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000370 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000371 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000372 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000373 }
374
375 *olen = ilen;
376
377 return 0;
378 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200379#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000380
Paul Bakker68884e32013-01-07 18:20:04 +0100381#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000382 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
383 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000384 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000385 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000386 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000387 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000388 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000389 }
390
391 *olen = ilen;
392
393 return 0;
394 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200395#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000396
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200397#if defined(POLARSSL_CIPHER_MODE_STREAM)
398 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
399 {
400 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
401 ilen, input, output ) ) )
402 {
403 return ret;
404 }
405
406 *olen = ilen;
407
408 return 0;
409 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200410#endif /* POLARSSL_CIPHER_MODE_STREAM */
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200411
Paul Bakkerff61a782011-06-09 15:42:02 +0000412 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413}
414
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200415#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200416#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200417/*
418 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
419 */
Paul Bakker23986e52011-04-24 08:57:21 +0000420static void add_pkcs_padding( unsigned char *output, size_t output_len,
421 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000422{
Paul Bakker23986e52011-04-24 08:57:21 +0000423 size_t padding_len = output_len - data_len;
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100424 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425
426 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000427 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000428}
429
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200430static int get_pkcs_padding( unsigned char *input, size_t input_len,
431 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432{
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100433 size_t i, pad_idx;
434 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000435
Paul Bakkera885d682011-01-20 16:35:05 +0000436 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000437 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438
439 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000440 *data_len = input_len - padding_len;
441
Manuel Pégourié-Gonnardf8ab0692013-10-27 17:21:14 +0100442 /* Avoid logical || since it results in a branch */
443 bad |= padding_len > input_len;
444 bad |= padding_len == 0;
445
446 /* The number of bytes checked must be independent of padding_len,
447 * so pick input_len, which is usually 8 or 16 (one block) */
448 pad_idx = input_len - padding_len;
449 for( i = 0; i < input_len; i++ )
450 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
451
452 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453}
Paul Bakker48e93c82013-08-14 12:21:18 +0200454#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455
Paul Bakker48e93c82013-08-14 12:21:18 +0200456#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200457/*
458 * One and zeros padding: fill with 80 00 ... 00
459 */
460static void add_one_and_zeros_padding( unsigned char *output,
461 size_t output_len, size_t data_len )
462{
463 size_t padding_len = output_len - data_len;
464 unsigned char i = 0;
465
466 output[data_len] = 0x80;
467 for( i = 1; i < padding_len; i++ )
468 output[data_len + i] = 0x00;
469}
470
471static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
472 size_t *data_len )
473{
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100474 size_t i;
475 unsigned char done = 0, prev_done, bad;
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200476
477 if( NULL == input || NULL == data_len )
478 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
479
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100480 bad = 0xFF;
481 *data_len = 0;
482 for( i = input_len; i > 0; i-- )
483 {
484 prev_done = done;
485 done |= ( input[i-1] != 0 );
486 *data_len |= ( i - 1 ) * ( done != prev_done );
487 bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
488 }
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200489
Manuel Pégourié-Gonnard6c329902013-10-27 18:25:03 +0100490 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200491
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200492}
Paul Bakker48e93c82013-08-14 12:21:18 +0200493#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200494
Paul Bakker48e93c82013-08-14 12:21:18 +0200495#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200496/*
497 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
498 */
499static void add_zeros_and_len_padding( unsigned char *output,
500 size_t output_len, size_t data_len )
501{
502 size_t padding_len = output_len - data_len;
503 unsigned char i = 0;
504
505 for( i = 1; i < padding_len; i++ )
506 output[data_len + i - 1] = 0x00;
507 output[output_len - 1] = (unsigned char) padding_len;
508}
509
510static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
511 size_t *data_len )
512{
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100513 size_t i, pad_idx;
514 unsigned char padding_len, bad = 0;
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200515
516 if( NULL == input || NULL == data_len )
517 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
518
519 padding_len = input[input_len - 1];
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200520 *data_len = input_len - padding_len;
521
Manuel Pégourié-Gonnardd17df512013-10-27 17:32:43 +0100522 /* Avoid logical || since it results in a branch */
523 bad |= padding_len > input_len;
524 bad |= padding_len == 0;
525
526 /* The number of bytes checked must be independent of padding_len */
527 pad_idx = input_len - padding_len;
528 for( i = 0; i < input_len - 1; i++ )
529 bad |= input[i] * ( i >= pad_idx );
530
531 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200532}
Paul Bakker48e93c82013-08-14 12:21:18 +0200533#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200534
Paul Bakker48e93c82013-08-14 12:21:18 +0200535#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200536/*
537 * Zero padding: fill with 00 ... 00
538 */
539static void add_zeros_padding( unsigned char *output,
540 size_t output_len, size_t data_len )
541{
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200542 size_t i;
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200543
544 for( i = data_len; i < output_len; i++ )
545 output[i] = 0x00;
546}
547
548static int get_zeros_padding( unsigned char *input, size_t input_len,
549 size_t *data_len )
550{
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100551 size_t i;
552 unsigned char done = 0, prev_done;
553
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200554 if( NULL == input || NULL == data_len )
555 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
556
Manuel Pégourié-Gonnarde68bf172013-10-27 18:26:39 +0100557 *data_len = 0;
558 for( i = input_len; i > 0; i-- )
559 {
560 prev_done = done;
561 done |= ( input[i-1] != 0 );
562 *data_len |= i * ( done != prev_done );
563 }
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200564
565 return 0;
566}
Paul Bakker48e93c82013-08-14 12:21:18 +0200567#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200568
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200569/*
570 * No padding: don't pad :)
571 *
572 * There is no add_padding function (check for NULL in cipher_finish)
573 * but a trivial get_padding function
574 */
575static int get_no_padding( unsigned char *input, size_t input_len,
576 size_t *data_len )
577{
578 if( NULL == input || NULL == data_len )
579 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
580
581 *data_len = input_len;
582
583 return 0;
584}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200585#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200586
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200587int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200588 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589{
590 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000591 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
593 *olen = 0;
594
Paul Bakker6132d0a2012-07-04 17:10:40 +0000595 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000596 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200597 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200598 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000599 {
600 return 0;
601 }
602
Paul Bakker5e0efa72013-09-08 23:04:04 +0200603 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
604 {
605 if( ctx->unprocessed_len != 0 )
606 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
607
608 return 0;
609 }
610
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200611#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
613 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200614 int ret = 0;
615
Paul Bakker8123e9d2011-01-06 15:37:30 +0000616 if( POLARSSL_ENCRYPT == ctx->operation )
617 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200618 /* check for 'no padding' mode */
619 if( NULL == ctx->add_padding )
620 {
621 if( 0 != ctx->unprocessed_len )
622 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
623
624 return 0;
625 }
626
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200627 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628 ctx->unprocessed_len );
629 }
630 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
631 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200632 /*
633 * For decrypt operations, expect a full block,
634 * or an empty block if no padding
635 */
636 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
637 return 0;
638
Paul Bakkerff61a782011-06-09 15:42:02 +0000639 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 }
641
642 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000643 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
644 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
645 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000646 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000647 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648 }
649
650 /* Set output size for decryption */
651 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200652 return ctx->get_padding( output, cipher_get_block_size( ctx ),
653 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654
655 /* Set output size for encryption */
656 *olen = cipher_get_block_size( ctx );
657 return 0;
658 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200659#else
660 ((void) output);
661#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
Paul Bakkerff61a782011-06-09 15:42:02 +0000663 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664}
665
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200666#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200667int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
668{
669 if( NULL == ctx ||
670 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
671 {
672 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
673 }
674
Paul Bakker1a45d912013-08-14 12:04:26 +0200675 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200676 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200677#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200678 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200679 ctx->add_padding = add_pkcs_padding;
680 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200681 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200682#endif
683#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200684 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200685 ctx->add_padding = add_one_and_zeros_padding;
686 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200687 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200688#endif
689#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200690 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200691 ctx->add_padding = add_zeros_and_len_padding;
692 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200693 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200694#endif
695#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200696 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200697 ctx->add_padding = add_zeros_padding;
698 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200699 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200700#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200701 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200702 ctx->add_padding = NULL;
703 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200704 break;
705
706 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200707 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200708 }
709
Paul Bakker1a45d912013-08-14 12:04:26 +0200710 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200711}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200712#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200713
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200714#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200715int cipher_write_tag( cipher_context_t *ctx,
716 unsigned char *tag, size_t tag_len )
717{
718 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
719 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
720
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200721 if( POLARSSL_ENCRYPT != ctx->operation )
722 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
723
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200724#if defined(POLARSSL_GCM_C)
725 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200726 return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200727#endif
728
729 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200730}
Paul Bakker9af723c2014-05-01 13:03:14 +0200731
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200732int cipher_check_tag( cipher_context_t *ctx,
733 const unsigned char *tag, size_t tag_len )
734{
735 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200736
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200737 if( NULL == ctx || NULL == ctx->cipher_info ||
738 POLARSSL_DECRYPT != ctx->operation )
739 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200740 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200741 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200742
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200743#if defined(POLARSSL_GCM_C)
744 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
745 {
746 unsigned char check_tag[16];
747 size_t i;
748 int diff;
749
750 if( tag_len > sizeof( check_tag ) )
751 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
752
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200753 if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
754 check_tag, tag_len ) ) )
755 {
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200756 return( ret );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200757 }
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200758
759 /* Check the tag in "constant-time" */
760 for( diff = 0, i = 0; i < tag_len; i++ )
761 diff |= tag[i] ^ check_tag[i];
762
763 if( diff != 0 )
Manuel Pégourié-Gonnard4fee79b2013-09-19 18:09:14 +0200764 return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200765
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200766 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200767 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200768#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200769
770 return( 0 );
771}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200772#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200773
Manuel Pégourié-Gonnard3c1d1502014-05-12 13:46:08 +0200774/*
775 * Packet-oriented wrapper for non-AEAD modes
776 */
777int cipher_crypt( cipher_context_t *ctx,
778 const unsigned char *iv, size_t iv_len,
779 const unsigned char *input, size_t ilen,
780 unsigned char *output, size_t *olen )
781{
782 int ret;
783 size_t finish_olen;
784
785 if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
786 return( ret );
787
788 if( ( ret = cipher_reset( ctx ) ) != 0 )
789 return( ret );
790
791 if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
792 return( ret );
793
794 if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
795 return( ret );
796
797 *olen += finish_olen;
798
799 return( 0 );
800}
801
Paul Bakker8123e9d2011-01-06 15:37:30 +0000802#if defined(POLARSSL_SELF_TEST)
803
Paul Bakker8123e9d2011-01-06 15:37:30 +0000804/*
805 * Checkup routine
806 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000807int cipher_self_test( int verbose )
808{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000809 ((void) verbose);
810
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811 return( 0 );
812}
813
Paul Bakker9af723c2014-05-01 13:03:14 +0200814#endif /* POLARSSL_SELF_TEST */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000815
Paul Bakker9af723c2014-05-01 13:03:14 +0200816#endif /* POLARSSL_CIPHER_C */