blob: 60e1d911e39d43feb60c9c6ec047e9486baeba0b [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 *
Paul Bakker68884e32013-01-07 18:20:04 +01008 * Copyright (C) 2006-2013, 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
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_CIPHER_C)
33
34#include "polarssl/cipher.h"
35#include "polarssl/cipher_wrap.h"
36
Paul Bakker8123e9d2011-01-06 15:37:30 +000037#include <stdlib.h>
38
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020039#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020040#define POLARSSL_CIPHER_MODE_STREAM
41#endif
42
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000043#if defined _MSC_VER && !defined strcasecmp
44#define strcasecmp _stricmp
45#endif
46
Paul Bakker72f62662011-01-16 21:27:44 +000047static const int supported_ciphers[] = {
48
49#if defined(POLARSSL_AES_C)
50 POLARSSL_CIPHER_AES_128_CBC,
51 POLARSSL_CIPHER_AES_192_CBC,
52 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000053
54#if defined(POLARSSL_CIPHER_MODE_CFB)
55 POLARSSL_CIPHER_AES_128_CFB128,
56 POLARSSL_CIPHER_AES_192_CFB128,
57 POLARSSL_CIPHER_AES_256_CFB128,
58#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
59
60#if defined(POLARSSL_CIPHER_MODE_CTR)
61 POLARSSL_CIPHER_AES_128_CTR,
62 POLARSSL_CIPHER_AES_192_CTR,
63 POLARSSL_CIPHER_AES_256_CTR,
64#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
65
Paul Bakker72f62662011-01-16 21:27:44 +000066#endif /* defined(POLARSSL_AES_C) */
67
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020068#if defined(POLARSSL_ARC4_C)
69 POLARSSL_CIPHER_ARC4_128,
70#endif
71
Paul Bakker72f62662011-01-16 21:27:44 +000072#if defined(POLARSSL_CAMELLIA_C)
73 POLARSSL_CIPHER_CAMELLIA_128_CBC,
74 POLARSSL_CIPHER_CAMELLIA_192_CBC,
75 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000076
77#if defined(POLARSSL_CIPHER_MODE_CFB)
78 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
79 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
80 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
81#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
82
83#if defined(POLARSSL_CIPHER_MODE_CTR)
84 POLARSSL_CIPHER_CAMELLIA_128_CTR,
85 POLARSSL_CIPHER_CAMELLIA_192_CTR,
86 POLARSSL_CIPHER_CAMELLIA_256_CTR,
87#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
88
Paul Bakker72f62662011-01-16 21:27:44 +000089#endif /* defined(POLARSSL_CAMELLIA_C) */
90
91#if defined(POLARSSL_DES_C)
92 POLARSSL_CIPHER_DES_CBC,
93 POLARSSL_CIPHER_DES_EDE_CBC,
94 POLARSSL_CIPHER_DES_EDE3_CBC,
95#endif /* defined(POLARSSL_DES_C) */
96
Paul Bakker6132d0a2012-07-04 17:10:40 +000097#if defined(POLARSSL_BLOWFISH_C)
98 POLARSSL_CIPHER_BLOWFISH_CBC,
99
100#if defined(POLARSSL_CIPHER_MODE_CFB)
101 POLARSSL_CIPHER_BLOWFISH_CFB64,
102#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
103
104#if defined(POLARSSL_CIPHER_MODE_CTR)
105 POLARSSL_CIPHER_BLOWFISH_CTR,
106#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
107
108#endif /* defined(POLARSSL_BLOWFISH_C) */
109
Paul Bakkerfab5c822012-02-06 16:45:10 +0000110#if defined(POLARSSL_CIPHER_NULL_CIPHER)
111 POLARSSL_CIPHER_NULL,
112#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
113
Paul Bakker72f62662011-01-16 21:27:44 +0000114 0
115};
116
117const int *cipher_list( void )
118{
119 return supported_ciphers;
120}
121
Paul Bakkerec1b9842012-01-14 18:24:43 +0000122const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123{
124 /* Find static cipher information */
125 switch ( cipher_type )
126 {
127#if defined(POLARSSL_AES_C)
128 case POLARSSL_CIPHER_AES_128_CBC:
129 return &aes_128_cbc_info;
130 case POLARSSL_CIPHER_AES_192_CBC:
131 return &aes_192_cbc_info;
132 case POLARSSL_CIPHER_AES_256_CBC:
133 return &aes_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000134
135#if defined(POLARSSL_CIPHER_MODE_CFB)
136 case POLARSSL_CIPHER_AES_128_CFB128:
137 return &aes_128_cfb128_info;
138 case POLARSSL_CIPHER_AES_192_CFB128:
139 return &aes_192_cfb128_info;
140 case POLARSSL_CIPHER_AES_256_CFB128:
141 return &aes_256_cfb128_info;
142#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
143
144#if defined(POLARSSL_CIPHER_MODE_CTR)
145 case POLARSSL_CIPHER_AES_128_CTR:
146 return &aes_128_ctr_info;
147 case POLARSSL_CIPHER_AES_192_CTR:
148 return &aes_192_ctr_info;
149 case POLARSSL_CIPHER_AES_256_CTR:
150 return &aes_256_ctr_info;
151#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
152
Paul Bakker68884e32013-01-07 18:20:04 +0100153#if defined(POLARSSL_GCM_C)
154 case POLARSSL_CIPHER_AES_128_GCM:
155 return &aes_128_gcm_info;
156 case POLARSSL_CIPHER_AES_256_GCM:
157 return &aes_256_gcm_info;
158#endif /* defined(POLARSSL_GCM_C) */
159
Paul Bakker8123e9d2011-01-06 15:37:30 +0000160#endif
161
162#if defined(POLARSSL_CAMELLIA_C)
163 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
164 return &camellia_128_cbc_info;
165 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
166 return &camellia_192_cbc_info;
167 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
168 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000169
170#if defined(POLARSSL_CIPHER_MODE_CFB)
171 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
172 return &camellia_128_cfb128_info;
173 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
174 return &camellia_192_cfb128_info;
175 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
176 return &camellia_256_cfb128_info;
177#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
178
179#if defined(POLARSSL_CIPHER_MODE_CTR)
180 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
181 return &camellia_128_ctr_info;
182 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
183 return &camellia_192_ctr_info;
184 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
185 return &camellia_256_ctr_info;
186#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
187
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188#endif
189
190#if defined(POLARSSL_DES_C)
191 case POLARSSL_CIPHER_DES_CBC:
192 return &des_cbc_info;
193 case POLARSSL_CIPHER_DES_EDE_CBC:
194 return &des_ede_cbc_info;
195 case POLARSSL_CIPHER_DES_EDE3_CBC:
196 return &des_ede3_cbc_info;
197#endif
198
Paul Bakker68884e32013-01-07 18:20:04 +0100199#if defined(POLARSSL_ARC4_C)
200 case POLARSSL_CIPHER_ARC4_128:
201 return &arc4_128_info;
202#endif
203
Paul Bakker6132d0a2012-07-04 17:10:40 +0000204#if defined(POLARSSL_BLOWFISH_C)
205 case POLARSSL_CIPHER_BLOWFISH_CBC:
206 return &blowfish_cbc_info;
207
208#if defined(POLARSSL_CIPHER_MODE_CFB)
209 case POLARSSL_CIPHER_BLOWFISH_CFB64:
210 return &blowfish_cfb64_info;
211#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
212
213#if defined(POLARSSL_CIPHER_MODE_CTR)
214 case POLARSSL_CIPHER_BLOWFISH_CTR:
215 return &blowfish_ctr_info;
216#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
217
218#endif
219
Paul Bakkerfab5c822012-02-06 16:45:10 +0000220#if defined(POLARSSL_CIPHER_NULL_CIPHER)
221 case POLARSSL_CIPHER_NULL:
222 return &null_cipher_info;
223#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
224
Paul Bakker8123e9d2011-01-06 15:37:30 +0000225 default:
226 return NULL;
227 }
228}
229
230const cipher_info_t *cipher_info_from_string( const char *cipher_name )
231{
232 if( NULL == cipher_name )
233 return NULL;
234
Paul Bakker343a8702011-06-09 14:27:58 +0000235 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236#if defined(POLARSSL_CAMELLIA_C)
237 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
238 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
239 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
240 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
241 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
242 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000243
244#if defined(POLARSSL_CIPHER_MODE_CFB)
245 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
246 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
247 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
248 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
249 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
250 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
251#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
252
253#if defined(POLARSSL_CIPHER_MODE_CTR)
254 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
255 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
256 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
257 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
258 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
259 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
260#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000261#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000262
Paul Bakker8123e9d2011-01-06 15:37:30 +0000263#if defined(POLARSSL_AES_C)
264 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
265 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
266 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
267 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
268 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
269 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000270
271#if defined(POLARSSL_CIPHER_MODE_CFB)
272 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
273 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
274 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
275 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
276 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
277 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
278#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
279
280#if defined(POLARSSL_CIPHER_MODE_CTR)
281 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
282 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
283 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
284 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
285 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
286 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
287#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000288#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000289
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200290#if defined(POLARSSL_ARC4_C)
291 if( !strcasecmp( "ARC4-128", cipher_name ) )
292 return( cipher_info_from_type( POLARSSL_CIPHER_ARC4_128 ) );
293#endif
294
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295#if defined(POLARSSL_DES_C)
296 if( !strcasecmp( "DES-CBC", cipher_name ) )
297 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
298 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
299 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
300 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
301 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
302#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000303
Paul Bakker6132d0a2012-07-04 17:10:40 +0000304#if defined(POLARSSL_BLOWFISH_C)
305 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
306 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
307
308#if defined(POLARSSL_CIPHER_MODE_CFB)
309 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
310 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
311#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
312
313#if defined(POLARSSL_CIPHER_MODE_CTR)
314 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
315 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
316#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
317#endif
318
Paul Bakkerfab5c822012-02-06 16:45:10 +0000319#if defined(POLARSSL_CIPHER_NULL_CIPHER)
320 if( !strcasecmp( "NULL", cipher_name ) )
321 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
322#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
323
Paul Bakker8123e9d2011-01-06 15:37:30 +0000324 return NULL;
325}
326
327int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
328{
329 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000330 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331
Paul Bakker279432a2012-04-26 10:09:35 +0000332 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Paul Bakker343a8702011-06-09 14:27:58 +0000334 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000335 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336
337 ctx->cipher_info = cipher_info;
338
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200339 /*
340 * Ignore possible errors caused by a cipher mode that doesn't use padding
341 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200342#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200343 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200344#else
345 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
346#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200347
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 return 0;
349}
350
351int cipher_free_ctx( cipher_context_t *ctx )
352{
353 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000354 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000355
Paul Bakker343a8702011-06-09 14:27:58 +0000356 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357
358 return 0;
359}
360
361int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
362 int key_length, const operation_t operation )
363{
364 if( NULL == ctx || NULL == ctx->cipher_info )
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->key_length = key_length;
368 ctx->operation = operation;
369
Paul Bakker343a8702011-06-09 14:27:58 +0000370 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000371 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000372 */
373 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000374 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000375 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
376 {
377 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000379 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Paul Bakker343a8702011-06-09 14:27:58 +0000381 if( POLARSSL_DECRYPT == operation )
382 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 ctx->key_length );
384
Paul Bakkerff61a782011-06-09 15:42:02 +0000385 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386}
387
388int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
389{
390 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000391 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
393 ctx->unprocessed_len = 0;
394
395 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
396
397 return 0;
398}
399
Paul Bakker23986e52011-04-24 08:57:21 +0000400int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
401 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402{
Paul Bakkerff61a782011-06-09 15:42:02 +0000403 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000404 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405
Paul Bakker68884e32013-01-07 18:20:04 +0100406 *olen = 0;
407
408 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000409 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000410 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000411 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000412
Paul Bakker68884e32013-01-07 18:20:04 +0100413 if( input == output &&
414 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
415 {
416 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
417 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418
419 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
420 {
421 /*
422 * If there is not enough data for a full block, cache it.
423 */
424 if( ( ctx->operation == POLARSSL_DECRYPT &&
425 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
426 ( ctx->operation == POLARSSL_ENCRYPT &&
427 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
428 {
429 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
430 ilen );
431
432 ctx->unprocessed_len += ilen;
433 return 0;
434 }
435
436 /*
437 * Process cached data first
438 */
439 if( ctx->unprocessed_len != 0 )
440 {
441 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
442
443 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
444 copy_len );
445
Paul Bakkerff61a782011-06-09 15:42:02 +0000446 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000448 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000450 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 }
452
453 *olen += cipher_get_block_size( ctx );
454 output += cipher_get_block_size( ctx );
455 ctx->unprocessed_len = 0;
456
457 input += copy_len;
458 ilen -= copy_len;
459 }
460
461 /*
462 * Cache final, incomplete block
463 */
464 if( 0 != ilen )
465 {
466 copy_len = ilen % cipher_get_block_size( ctx );
467 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
468 copy_len = cipher_get_block_size(ctx);
469
470 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
471 copy_len );
472
473 ctx->unprocessed_len += copy_len;
474 ilen -= copy_len;
475 }
476
477 /*
478 * Process remaining full blocks
479 */
480 if( ilen )
481 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000482 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
483 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000485 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486 }
487 *olen += ilen;
488 }
489
490 return 0;
491 }
492
Paul Bakker68884e32013-01-07 18:20:04 +0100493#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000494 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000495 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000496 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000497 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000498 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000499 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000500 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000501 }
502
503 *olen = ilen;
504
505 return 0;
506 }
Paul Bakker68884e32013-01-07 18:20:04 +0100507#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000508
Paul Bakker68884e32013-01-07 18:20:04 +0100509#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000510 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
511 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000512 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000513 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000514 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000515 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000516 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000517 }
518
519 *olen = ilen;
520
521 return 0;
522 }
Paul Bakker68884e32013-01-07 18:20:04 +0100523#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000524
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200525#if defined(POLARSSL_CIPHER_MODE_STREAM)
526 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
527 {
528 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
529 ilen, input, output ) ) )
530 {
531 return ret;
532 }
533
534 *olen = ilen;
535
536 return 0;
537 }
538#endif
539
Paul Bakkerff61a782011-06-09 15:42:02 +0000540 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541}
542
Paul Bakker48e93c82013-08-14 12:21:18 +0200543#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200544/*
545 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
546 */
Paul Bakker23986e52011-04-24 08:57:21 +0000547static void add_pkcs_padding( unsigned char *output, size_t output_len,
548 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000549{
Paul Bakker23986e52011-04-24 08:57:21 +0000550 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551 unsigned char i = 0;
552
553 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000554 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555}
556
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200557static int get_pkcs_padding( unsigned char *input, size_t input_len,
558 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000560 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561
Paul Bakkera885d682011-01-20 16:35:05 +0000562 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000563 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564
565 padding_len = input[input_len - 1];
566
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200567 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000568 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569
Paul Bakkera885d682011-01-20 16:35:05 +0000570 for( i = input_len - padding_len; i < input_len; i++ )
571 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000572 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573
574 *data_len = input_len - padding_len;
575
576 return 0;
577}
Paul Bakker48e93c82013-08-14 12:21:18 +0200578#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Paul Bakker48e93c82013-08-14 12:21:18 +0200580#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200581/*
582 * One and zeros padding: fill with 80 00 ... 00
583 */
584static void add_one_and_zeros_padding( unsigned char *output,
585 size_t output_len, size_t data_len )
586{
587 size_t padding_len = output_len - data_len;
588 unsigned char i = 0;
589
590 output[data_len] = 0x80;
591 for( i = 1; i < padding_len; i++ )
592 output[data_len + i] = 0x00;
593}
594
595static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
596 size_t *data_len )
597{
598 unsigned char *p = input + input_len - 1;
599
600 if( NULL == input || NULL == data_len )
601 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
602
603 while( *p == 0x00 && p > input )
604 --p;
605
606 if( *p != 0x80 )
607 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
608
609 *data_len = p - input;
610
611 return 0;
612}
Paul Bakker48e93c82013-08-14 12:21:18 +0200613#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200614
Paul Bakker48e93c82013-08-14 12:21:18 +0200615#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200616/*
617 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
618 */
619static void add_zeros_and_len_padding( unsigned char *output,
620 size_t output_len, size_t data_len )
621{
622 size_t padding_len = output_len - data_len;
623 unsigned char i = 0;
624
625 for( i = 1; i < padding_len; i++ )
626 output[data_len + i - 1] = 0x00;
627 output[output_len - 1] = (unsigned char) padding_len;
628}
629
630static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
631 size_t *data_len )
632{
633 unsigned int i, padding_len = 0;
634
635 if( NULL == input || NULL == data_len )
636 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
637
638 padding_len = input[input_len - 1];
639
640 if( padding_len > input_len || padding_len == 0 )
641 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
642
643 for( i = input_len - padding_len; i < input_len - 1; i++ )
644 if( input[i] != 0x00 )
645 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
646
647 *data_len = input_len - padding_len;
648
649 return 0;
650}
Paul Bakker48e93c82013-08-14 12:21:18 +0200651#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200652
Paul Bakker48e93c82013-08-14 12:21:18 +0200653#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200654/*
655 * Zero padding: fill with 00 ... 00
656 */
657static void add_zeros_padding( unsigned char *output,
658 size_t output_len, size_t data_len )
659{
660 unsigned char i;
661
662 for( i = data_len; i < output_len; i++ )
663 output[i] = 0x00;
664}
665
666static int get_zeros_padding( unsigned char *input, size_t input_len,
667 size_t *data_len )
668{
669 unsigned char *p = input + input_len - 1;
670 if( NULL == input || NULL == data_len )
671 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
672
673 while( *p == 0x00 && p > input )
674 --p;
675
676 *data_len = *p == 0x00 ? 0 : p - input + 1;
677
678 return 0;
679}
Paul Bakker48e93c82013-08-14 12:21:18 +0200680#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200681
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200682/*
683 * No padding: don't pad :)
684 *
685 * There is no add_padding function (check for NULL in cipher_finish)
686 * but a trivial get_padding function
687 */
688static int get_no_padding( unsigned char *input, size_t input_len,
689 size_t *data_len )
690{
691 if( NULL == input || NULL == data_len )
692 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
693
694 *data_len = input_len;
695
696 return 0;
697}
698
Paul Bakker23986e52011-04-24 08:57:21 +0000699int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700{
Paul Bakkerff61a782011-06-09 15:42:02 +0000701 int ret = 0;
702
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000704 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705
706 *olen = 0;
707
Paul Bakker6132d0a2012-07-04 17:10:40 +0000708 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000709 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200710 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000711 {
712 return 0;
713 }
714
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
716 {
717 if( POLARSSL_ENCRYPT == ctx->operation )
718 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200719 /* check for 'no padding' mode */
720 if( NULL == ctx->add_padding )
721 {
722 if( 0 != ctx->unprocessed_len )
723 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
724
725 return 0;
726 }
727
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200728 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 ctx->unprocessed_len );
730 }
731 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
732 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200733 /*
734 * For decrypt operations, expect a full block,
735 * or an empty block if no padding
736 */
737 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
738 return 0;
739
Paul Bakkerff61a782011-06-09 15:42:02 +0000740 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741 }
742
743 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000744 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
745 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
746 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000748 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749 }
750
751 /* Set output size for decryption */
752 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200753 return ctx->get_padding( output, cipher_get_block_size( ctx ),
754 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755
756 /* Set output size for encryption */
757 *olen = cipher_get_block_size( ctx );
758 return 0;
759 }
760
Paul Bakkerff61a782011-06-09 15:42:02 +0000761 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762}
763
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200764int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
765{
766 if( NULL == ctx ||
767 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
768 {
769 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
770 }
771
Paul Bakker1a45d912013-08-14 12:04:26 +0200772 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200773 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200774#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200775 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200776 ctx->add_padding = add_pkcs_padding;
777 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200778 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200779#endif
780#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200781 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200782 ctx->add_padding = add_one_and_zeros_padding;
783 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200784 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200785#endif
786#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200787 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200788 ctx->add_padding = add_zeros_and_len_padding;
789 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200790 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200791#endif
792#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200793 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200794 ctx->add_padding = add_zeros_padding;
795 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200796 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200797#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200798 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200799 ctx->add_padding = NULL;
800 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200801 break;
802
803 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200804 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200805 }
806
Paul Bakker1a45d912013-08-14 12:04:26 +0200807 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200808}
809
Paul Bakker8123e9d2011-01-06 15:37:30 +0000810#if defined(POLARSSL_SELF_TEST)
811
812#include <stdio.h>
813
814#define ASSERT(x) if (!(x)) { \
815 printf( "failed with %i at %s\n", value, (#x) ); \
816 return( 1 ); \
817}
818/*
819 * Checkup routine
820 */
821
822int cipher_self_test( int verbose )
823{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000824 ((void) verbose);
825
Paul Bakker8123e9d2011-01-06 15:37:30 +0000826 return( 0 );
827}
828
829#endif
830
831#endif