blob: 13122819e7de6ba7cac54c7d06bd9270b76d8197 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file cipher.c
3 *
4 * \brief Generic cipher wrapper for PolarSSL
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +000010 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
12 * 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
27#include "polarssl/config.h"
28
29#if defined(POLARSSL_CIPHER_C)
30
31#include "polarssl/cipher.h"
32#include "polarssl/cipher_wrap.h"
33
Paul Bakker8123e9d2011-01-06 15:37:30 +000034#include <stdlib.h>
35
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000036#if defined _MSC_VER && !defined strcasecmp
37#define strcasecmp _stricmp
38#endif
39
Paul Bakker312da332014-06-13 17:20:13 +020040/* Implementation that should never be optimized out by the compiler */
41static void polarssl_zeroize( void *v, size_t n ) {
42 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
43}
44
Paul Bakker72f62662011-01-16 21:27:44 +000045static const int supported_ciphers[] = {
46
47#if defined(POLARSSL_AES_C)
48 POLARSSL_CIPHER_AES_128_CBC,
49 POLARSSL_CIPHER_AES_192_CBC,
50 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000051
52#if defined(POLARSSL_CIPHER_MODE_CFB)
53 POLARSSL_CIPHER_AES_128_CFB128,
54 POLARSSL_CIPHER_AES_192_CFB128,
55 POLARSSL_CIPHER_AES_256_CFB128,
56#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
57
58#if defined(POLARSSL_CIPHER_MODE_CTR)
59 POLARSSL_CIPHER_AES_128_CTR,
60 POLARSSL_CIPHER_AES_192_CTR,
61 POLARSSL_CIPHER_AES_256_CTR,
62#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
63
Paul Bakker72f62662011-01-16 21:27:44 +000064#endif /* defined(POLARSSL_AES_C) */
65
66#if defined(POLARSSL_CAMELLIA_C)
67 POLARSSL_CIPHER_CAMELLIA_128_CBC,
68 POLARSSL_CIPHER_CAMELLIA_192_CBC,
69 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000070
71#if defined(POLARSSL_CIPHER_MODE_CFB)
72 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
73 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
74 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
75#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
76
77#if defined(POLARSSL_CIPHER_MODE_CTR)
78 POLARSSL_CIPHER_CAMELLIA_128_CTR,
79 POLARSSL_CIPHER_CAMELLIA_192_CTR,
80 POLARSSL_CIPHER_CAMELLIA_256_CTR,
81#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
82
Paul Bakker72f62662011-01-16 21:27:44 +000083#endif /* defined(POLARSSL_CAMELLIA_C) */
84
85#if defined(POLARSSL_DES_C)
86 POLARSSL_CIPHER_DES_CBC,
87 POLARSSL_CIPHER_DES_EDE_CBC,
88 POLARSSL_CIPHER_DES_EDE3_CBC,
89#endif /* defined(POLARSSL_DES_C) */
90
Paul Bakker6132d0a2012-07-04 17:10:40 +000091#if defined(POLARSSL_BLOWFISH_C)
92 POLARSSL_CIPHER_BLOWFISH_CBC,
93
94#if defined(POLARSSL_CIPHER_MODE_CFB)
95 POLARSSL_CIPHER_BLOWFISH_CFB64,
96#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
97
98#if defined(POLARSSL_CIPHER_MODE_CTR)
99 POLARSSL_CIPHER_BLOWFISH_CTR,
100#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
101
102#endif /* defined(POLARSSL_BLOWFISH_C) */
103
Paul Bakkerfab5c822012-02-06 16:45:10 +0000104#if defined(POLARSSL_CIPHER_NULL_CIPHER)
105 POLARSSL_CIPHER_NULL,
106#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
107
Paul Bakker72f62662011-01-16 21:27:44 +0000108 0
109};
110
111const int *cipher_list( void )
112{
113 return supported_ciphers;
114}
115
Paul Bakkerec1b9842012-01-14 18:24:43 +0000116const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117{
118 /* Find static cipher information */
119 switch ( cipher_type )
120 {
121#if defined(POLARSSL_AES_C)
122 case POLARSSL_CIPHER_AES_128_CBC:
123 return &aes_128_cbc_info;
124 case POLARSSL_CIPHER_AES_192_CBC:
125 return &aes_192_cbc_info;
126 case POLARSSL_CIPHER_AES_256_CBC:
127 return &aes_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000128
129#if defined(POLARSSL_CIPHER_MODE_CFB)
130 case POLARSSL_CIPHER_AES_128_CFB128:
131 return &aes_128_cfb128_info;
132 case POLARSSL_CIPHER_AES_192_CFB128:
133 return &aes_192_cfb128_info;
134 case POLARSSL_CIPHER_AES_256_CFB128:
135 return &aes_256_cfb128_info;
136#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
137
138#if defined(POLARSSL_CIPHER_MODE_CTR)
139 case POLARSSL_CIPHER_AES_128_CTR:
140 return &aes_128_ctr_info;
141 case POLARSSL_CIPHER_AES_192_CTR:
142 return &aes_192_ctr_info;
143 case POLARSSL_CIPHER_AES_256_CTR:
144 return &aes_256_ctr_info;
145#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
146
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147#endif
148
149#if defined(POLARSSL_CAMELLIA_C)
150 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
151 return &camellia_128_cbc_info;
152 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
153 return &camellia_192_cbc_info;
154 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
155 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000156
157#if defined(POLARSSL_CIPHER_MODE_CFB)
158 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
159 return &camellia_128_cfb128_info;
160 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
161 return &camellia_192_cfb128_info;
162 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
163 return &camellia_256_cfb128_info;
164#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
165
166#if defined(POLARSSL_CIPHER_MODE_CTR)
167 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
168 return &camellia_128_ctr_info;
169 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
170 return &camellia_192_ctr_info;
171 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
172 return &camellia_256_ctr_info;
173#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
174
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175#endif
176
177#if defined(POLARSSL_DES_C)
178 case POLARSSL_CIPHER_DES_CBC:
179 return &des_cbc_info;
180 case POLARSSL_CIPHER_DES_EDE_CBC:
181 return &des_ede_cbc_info;
182 case POLARSSL_CIPHER_DES_EDE3_CBC:
183 return &des_ede3_cbc_info;
184#endif
185
Paul Bakker6132d0a2012-07-04 17:10:40 +0000186#if defined(POLARSSL_BLOWFISH_C)
187 case POLARSSL_CIPHER_BLOWFISH_CBC:
188 return &blowfish_cbc_info;
189
190#if defined(POLARSSL_CIPHER_MODE_CFB)
191 case POLARSSL_CIPHER_BLOWFISH_CFB64:
192 return &blowfish_cfb64_info;
193#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
194
195#if defined(POLARSSL_CIPHER_MODE_CTR)
196 case POLARSSL_CIPHER_BLOWFISH_CTR:
197 return &blowfish_ctr_info;
198#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
199
200#endif
201
Paul Bakkerfab5c822012-02-06 16:45:10 +0000202#if defined(POLARSSL_CIPHER_NULL_CIPHER)
203 case POLARSSL_CIPHER_NULL:
204 return &null_cipher_info;
205#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
206
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207 default:
208 return NULL;
209 }
210}
211
212const cipher_info_t *cipher_info_from_string( const char *cipher_name )
213{
214 if( NULL == cipher_name )
215 return NULL;
216
Paul Bakker343a8702011-06-09 14:27:58 +0000217 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218#if defined(POLARSSL_CAMELLIA_C)
219 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
220 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
221 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
222 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
223 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
224 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000225
226#if defined(POLARSSL_CIPHER_MODE_CFB)
227 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
228 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
229 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
230 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
231 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
232 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
233#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
234
235#if defined(POLARSSL_CIPHER_MODE_CTR)
236 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
237 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
238 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
239 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
240 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
241 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
242#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000243#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000244
Paul Bakker8123e9d2011-01-06 15:37:30 +0000245#if defined(POLARSSL_AES_C)
246 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
247 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
248 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
249 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
250 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
251 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000252
253#if defined(POLARSSL_CIPHER_MODE_CFB)
254 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
255 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
256 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
257 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
258 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
259 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
260#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
261
262#if defined(POLARSSL_CIPHER_MODE_CTR)
263 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
264 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
265 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
266 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
267 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
268 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
269#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000271
Paul Bakker8123e9d2011-01-06 15:37:30 +0000272#if defined(POLARSSL_DES_C)
273 if( !strcasecmp( "DES-CBC", cipher_name ) )
274 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
275 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
276 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
277 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
278 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
279#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000280
Paul Bakker6132d0a2012-07-04 17:10:40 +0000281#if defined(POLARSSL_BLOWFISH_C)
282 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
283 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
284
285#if defined(POLARSSL_CIPHER_MODE_CFB)
286 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
287 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
288#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
289
290#if defined(POLARSSL_CIPHER_MODE_CTR)
291 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
292 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
293#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
294#endif
295
Paul Bakkerfab5c822012-02-06 16:45:10 +0000296#if defined(POLARSSL_CIPHER_NULL_CIPHER)
297 if( !strcasecmp( "NULL", cipher_name ) )
298 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
299#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
300
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301 return NULL;
302}
303
304int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
305{
306 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000307 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
Paul Bakker279432a2012-04-26 10:09:35 +0000309 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000310
Paul Bakker343a8702011-06-09 14:27:58 +0000311 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000312 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313
314 ctx->cipher_info = cipher_info;
315
316 return 0;
317}
318
319int cipher_free_ctx( cipher_context_t *ctx )
320{
321 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000322 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323
Paul Bakker343a8702011-06-09 14:27:58 +0000324 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker312da332014-06-13 17:20:13 +0200325 polarssl_zeroize( ctx, sizeof(cipher_context_t) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326
327 return 0;
328}
329
330int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
331 int key_length, const operation_t operation )
332{
333 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000334 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335
336 ctx->key_length = key_length;
337 ctx->operation = operation;
338
Paul Bakkerfab5c822012-02-06 16:45:10 +0000339#if defined(POLARSSL_CIPHER_NULL_CIPHER)
340 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
341 return 0;
342#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
343
Paul Bakker343a8702011-06-09 14:27:58 +0000344 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000345 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000346 */
347 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000348 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000349 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
350 {
351 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000353 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
Paul Bakker343a8702011-06-09 14:27:58 +0000355 if( POLARSSL_DECRYPT == operation )
356 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357 ctx->key_length );
358
Paul Bakkerff61a782011-06-09 15:42:02 +0000359 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360}
361
362int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
363{
364 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000365 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366
367 ctx->unprocessed_len = 0;
368
369 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
370
371 return 0;
372}
373
Paul Bakker23986e52011-04-24 08:57:21 +0000374int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
375 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376{
Paul Bakkerff61a782011-06-09 15:42:02 +0000377 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000378 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000379
Paul Bakkera885d682011-01-20 16:35:05 +0000380 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
381 input == output )
382 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000383 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000384 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
386 *olen = 0;
387
Paul Bakkerfab5c822012-02-06 16:45:10 +0000388#if defined(POLARSSL_CIPHER_NULL_CIPHER)
389 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
390 {
391 memcpy( output, input, ilen );
392 *olen = ilen;
393 return 0;
394 }
395#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
396
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
398 {
399 /*
400 * If there is not enough data for a full block, cache it.
401 */
402 if( ( ctx->operation == POLARSSL_DECRYPT &&
403 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
404 ( ctx->operation == POLARSSL_ENCRYPT &&
405 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
406 {
407 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
408 ilen );
409
410 ctx->unprocessed_len += ilen;
411 return 0;
412 }
413
414 /*
415 * Process cached data first
416 */
417 if( ctx->unprocessed_len != 0 )
418 {
419 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
420
421 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
422 copy_len );
423
Paul Bakkerff61a782011-06-09 15:42:02 +0000424 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000425 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000426 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000428 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000429 }
430
431 *olen += cipher_get_block_size( ctx );
432 output += cipher_get_block_size( ctx );
433 ctx->unprocessed_len = 0;
434
435 input += copy_len;
436 ilen -= copy_len;
437 }
438
439 /*
440 * Cache final, incomplete block
441 */
442 if( 0 != ilen )
443 {
444 copy_len = ilen % cipher_get_block_size( ctx );
445 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
446 copy_len = cipher_get_block_size(ctx);
447
448 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
449 copy_len );
450
451 ctx->unprocessed_len += copy_len;
452 ilen -= copy_len;
453 }
454
455 /*
456 * Process remaining full blocks
457 */
458 if( ilen )
459 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000460 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
461 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000463 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464 }
465 *olen += ilen;
466 }
467
468 return 0;
469 }
470
Paul Bakker6132d0a2012-07-04 17:10:40 +0000471 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000472 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000473 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000474 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000475 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000476 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000477 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000478 }
479
480 *olen = ilen;
481
482 return 0;
483 }
484
485 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
486 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000487 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000488 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000489 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000490 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000491 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000492 }
493
494 *olen = ilen;
495
496 return 0;
497 }
498
Paul Bakkerff61a782011-06-09 15:42:02 +0000499 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500}
501
Paul Bakker23986e52011-04-24 08:57:21 +0000502static void add_pkcs_padding( unsigned char *output, size_t output_len,
503 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504{
Paul Bakker23986e52011-04-24 08:57:21 +0000505 size_t padding_len = output_len - data_len;
Paul Bakkere46b1772014-07-07 14:04:00 +0200506 unsigned char i;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507
508 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000509 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510}
511
Paul Bakkerec1b9842012-01-14 18:24:43 +0000512static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t *data_len)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514{
Paul Bakkere46b1772014-07-07 14:04:00 +0200515 unsigned int i, pad_idx;
516 unsigned char padding_len, bad = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Paul Bakkera885d682011-01-20 16:35:05 +0000518 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000519 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520
521 padding_len = input[input_len - 1];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522 *data_len = input_len - padding_len;
523
Paul Bakkere46b1772014-07-07 14:04:00 +0200524 /* Avoid logical || since it results in a branch */
525 bad |= padding_len > input_len;
526 bad |= padding_len == 0;
527
528 /* The number of bytes checked must be independent of padding_len,
529 * so pick input_len, which is usually 8 or 16 (one block) */
530 pad_idx = input_len - padding_len;
531 for( i = 0; i < input_len; i++ )
532 bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
533
534 return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000535}
536
Paul Bakker23986e52011-04-24 08:57:21 +0000537int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538{
Paul Bakkerff61a782011-06-09 15:42:02 +0000539 int ret = 0;
540
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000542 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000543
544 *olen = 0;
545
Paul Bakker6132d0a2012-07-04 17:10:40 +0000546 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000547 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
548 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000549 {
550 return 0;
551 }
552
Paul Bakker8123e9d2011-01-06 15:37:30 +0000553 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
554 {
555 if( POLARSSL_ENCRYPT == ctx->operation )
556 {
557 add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
558 ctx->unprocessed_len );
559 }
560 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
561 {
562 /* For decrypt operations, expect a full block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000563 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564 }
565
566 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000567 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
568 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
569 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000571 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 }
573
574 /* Set output size for decryption */
575 if( POLARSSL_DECRYPT == ctx->operation )
576 return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
577
578 /* Set output size for encryption */
579 *olen = cipher_get_block_size( ctx );
580 return 0;
581 }
582
Paul Bakkerff61a782011-06-09 15:42:02 +0000583 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584}
585
586#if defined(POLARSSL_SELF_TEST)
587
588#include <stdio.h>
589
590#define ASSERT(x) if (!(x)) { \
591 printf( "failed with %i at %s\n", value, (#x) ); \
592 return( 1 ); \
593}
594/*
595 * Checkup routine
596 */
597
598int cipher_self_test( int verbose )
599{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000600 ((void) verbose);
601
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602 return( 0 );
603}
604
605#endif
606
607#endif