blob: 5a260a0bcfb7c0bf4562e3fe2bf0432e6d84c6a9 [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é-Gonnard37e230c2013-08-28 13:50:42 +020039#if defined(POLARSSL_ARC4_C)
40#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 Bakkerfab5c822012-02-06 16:45:10 +0000370#if defined(POLARSSL_CIPHER_NULL_CIPHER)
371 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
372 return 0;
373#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
374
Paul Bakker343a8702011-06-09 14:27:58 +0000375 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000376 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000377 */
378 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000379 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000380 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
381 {
382 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000384 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
Paul Bakker343a8702011-06-09 14:27:58 +0000386 if( POLARSSL_DECRYPT == operation )
387 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388 ctx->key_length );
389
Paul Bakkerff61a782011-06-09 15:42:02 +0000390 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391}
392
393int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
394{
395 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000396 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397
398 ctx->unprocessed_len = 0;
399
400 memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
401
402 return 0;
403}
404
Paul Bakker23986e52011-04-24 08:57:21 +0000405int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
406 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407{
Paul Bakkerff61a782011-06-09 15:42:02 +0000408 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000409 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
Paul Bakker68884e32013-01-07 18:20:04 +0100411 *olen = 0;
412
413 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000414 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000415 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000416 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417
Paul Bakker68884e32013-01-07 18:20:04 +0100418 if( input == output &&
419 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
420 {
421 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
422 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423
Paul Bakkerfab5c822012-02-06 16:45:10 +0000424#if defined(POLARSSL_CIPHER_NULL_CIPHER)
425 if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
426 {
Paul Bakkerfab5c822012-02-06 16:45:10 +0000427 *olen = ilen;
Paul Bakker68884e32013-01-07 18:20:04 +0100428
429 if( output == input )
430 return( 0 );
431
432 memcpy( output, input, ilen );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000433 return 0;
434 }
435#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
436
Paul Bakker8123e9d2011-01-06 15:37:30 +0000437 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
438 {
439 /*
440 * If there is not enough data for a full block, cache it.
441 */
442 if( ( ctx->operation == POLARSSL_DECRYPT &&
443 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
444 ( ctx->operation == POLARSSL_ENCRYPT &&
445 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
446 {
447 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
448 ilen );
449
450 ctx->unprocessed_len += ilen;
451 return 0;
452 }
453
454 /*
455 * Process cached data first
456 */
457 if( ctx->unprocessed_len != 0 )
458 {
459 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
460
461 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
462 copy_len );
463
Paul Bakkerff61a782011-06-09 15:42:02 +0000464 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000465 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000466 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000468 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 }
470
471 *olen += cipher_get_block_size( ctx );
472 output += cipher_get_block_size( ctx );
473 ctx->unprocessed_len = 0;
474
475 input += copy_len;
476 ilen -= copy_len;
477 }
478
479 /*
480 * Cache final, incomplete block
481 */
482 if( 0 != ilen )
483 {
484 copy_len = ilen % cipher_get_block_size( ctx );
485 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
486 copy_len = cipher_get_block_size(ctx);
487
488 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
489 copy_len );
490
491 ctx->unprocessed_len += copy_len;
492 ilen -= copy_len;
493 }
494
495 /*
496 * Process remaining full blocks
497 */
498 if( ilen )
499 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000500 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
501 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000503 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504 }
505 *olen += ilen;
506 }
507
508 return 0;
509 }
510
Paul Bakker68884e32013-01-07 18:20:04 +0100511#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000512 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000513 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000514 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000515 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000516 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000517 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000518 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000519 }
520
521 *olen = ilen;
522
523 return 0;
524 }
Paul Bakker68884e32013-01-07 18:20:04 +0100525#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000526
Paul Bakker68884e32013-01-07 18:20:04 +0100527#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000528 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
529 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000530 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000531 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000532 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000533 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000534 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000535 }
536
537 *olen = ilen;
538
539 return 0;
540 }
Paul Bakker68884e32013-01-07 18:20:04 +0100541#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000542
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200543#if defined(POLARSSL_CIPHER_MODE_STREAM)
544 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
545 {
546 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
547 ilen, input, output ) ) )
548 {
549 return ret;
550 }
551
552 *olen = ilen;
553
554 return 0;
555 }
556#endif
557
Paul Bakkerff61a782011-06-09 15:42:02 +0000558 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559}
560
Paul Bakker48e93c82013-08-14 12:21:18 +0200561#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200562/*
563 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
564 */
Paul Bakker23986e52011-04-24 08:57:21 +0000565static void add_pkcs_padding( unsigned char *output, size_t output_len,
566 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567{
Paul Bakker23986e52011-04-24 08:57:21 +0000568 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 unsigned char i = 0;
570
571 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000572 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573}
574
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200575static int get_pkcs_padding( unsigned char *input, size_t input_len,
576 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000577{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000578 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Paul Bakkera885d682011-01-20 16:35:05 +0000580 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000581 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582
583 padding_len = input[input_len - 1];
584
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200585 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000586 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
Paul Bakkera885d682011-01-20 16:35:05 +0000588 for( i = input_len - padding_len; i < input_len; i++ )
589 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000590 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
592 *data_len = input_len - padding_len;
593
594 return 0;
595}
Paul Bakker48e93c82013-08-14 12:21:18 +0200596#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597
Paul Bakker48e93c82013-08-14 12:21:18 +0200598#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200599/*
600 * One and zeros padding: fill with 80 00 ... 00
601 */
602static void add_one_and_zeros_padding( unsigned char *output,
603 size_t output_len, size_t data_len )
604{
605 size_t padding_len = output_len - data_len;
606 unsigned char i = 0;
607
608 output[data_len] = 0x80;
609 for( i = 1; i < padding_len; i++ )
610 output[data_len + i] = 0x00;
611}
612
613static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
614 size_t *data_len )
615{
616 unsigned char *p = input + input_len - 1;
617
618 if( NULL == input || NULL == data_len )
619 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
620
621 while( *p == 0x00 && p > input )
622 --p;
623
624 if( *p != 0x80 )
625 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
626
627 *data_len = p - input;
628
629 return 0;
630}
Paul Bakker48e93c82013-08-14 12:21:18 +0200631#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200632
Paul Bakker48e93c82013-08-14 12:21:18 +0200633#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200634/*
635 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
636 */
637static void add_zeros_and_len_padding( unsigned char *output,
638 size_t output_len, size_t data_len )
639{
640 size_t padding_len = output_len - data_len;
641 unsigned char i = 0;
642
643 for( i = 1; i < padding_len; i++ )
644 output[data_len + i - 1] = 0x00;
645 output[output_len - 1] = (unsigned char) padding_len;
646}
647
648static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
649 size_t *data_len )
650{
651 unsigned int i, padding_len = 0;
652
653 if( NULL == input || NULL == data_len )
654 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
655
656 padding_len = input[input_len - 1];
657
658 if( padding_len > input_len || padding_len == 0 )
659 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
660
661 for( i = input_len - padding_len; i < input_len - 1; i++ )
662 if( input[i] != 0x00 )
663 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
664
665 *data_len = input_len - padding_len;
666
667 return 0;
668}
Paul Bakker48e93c82013-08-14 12:21:18 +0200669#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200670
Paul Bakker48e93c82013-08-14 12:21:18 +0200671#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200672/*
673 * Zero padding: fill with 00 ... 00
674 */
675static void add_zeros_padding( unsigned char *output,
676 size_t output_len, size_t data_len )
677{
678 unsigned char i;
679
680 for( i = data_len; i < output_len; i++ )
681 output[i] = 0x00;
682}
683
684static int get_zeros_padding( unsigned char *input, size_t input_len,
685 size_t *data_len )
686{
687 unsigned char *p = input + input_len - 1;
688 if( NULL == input || NULL == data_len )
689 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
690
691 while( *p == 0x00 && p > input )
692 --p;
693
694 *data_len = *p == 0x00 ? 0 : p - input + 1;
695
696 return 0;
697}
Paul Bakker48e93c82013-08-14 12:21:18 +0200698#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200699
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200700/*
701 * No padding: don't pad :)
702 *
703 * There is no add_padding function (check for NULL in cipher_finish)
704 * but a trivial get_padding function
705 */
706static int get_no_padding( unsigned char *input, size_t input_len,
707 size_t *data_len )
708{
709 if( NULL == input || NULL == data_len )
710 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
711
712 *data_len = input_len;
713
714 return 0;
715}
716
Paul Bakker23986e52011-04-24 08:57:21 +0000717int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000718{
Paul Bakkerff61a782011-06-09 15:42:02 +0000719 int ret = 0;
720
Paul Bakker8123e9d2011-01-06 15:37:30 +0000721 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000722 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723
724 *olen = 0;
725
Paul Bakker6132d0a2012-07-04 17:10:40 +0000726 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000727 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200728 POLARSSL_MODE_STREAM == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000729 POLARSSL_MODE_NULL == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000730 {
731 return 0;
732 }
733
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
735 {
736 if( POLARSSL_ENCRYPT == ctx->operation )
737 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200738 /* check for 'no padding' mode */
739 if( NULL == ctx->add_padding )
740 {
741 if( 0 != ctx->unprocessed_len )
742 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
743
744 return 0;
745 }
746
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200747 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748 ctx->unprocessed_len );
749 }
750 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
751 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200752 /*
753 * For decrypt operations, expect a full block,
754 * or an empty block if no padding
755 */
756 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
757 return 0;
758
Paul Bakkerff61a782011-06-09 15:42:02 +0000759 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 }
761
762 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000763 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
764 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
765 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000766 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000767 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768 }
769
770 /* Set output size for decryption */
771 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200772 return ctx->get_padding( output, cipher_get_block_size( ctx ),
773 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000774
775 /* Set output size for encryption */
776 *olen = cipher_get_block_size( ctx );
777 return 0;
778 }
779
Paul Bakkerff61a782011-06-09 15:42:02 +0000780 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781}
782
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200783int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
784{
785 if( NULL == ctx ||
786 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
787 {
788 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
789 }
790
Paul Bakker1a45d912013-08-14 12:04:26 +0200791 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200792 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200793#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200794 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200795 ctx->add_padding = add_pkcs_padding;
796 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200797 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200798#endif
799#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200800 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200801 ctx->add_padding = add_one_and_zeros_padding;
802 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200803 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200804#endif
805#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200806 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200807 ctx->add_padding = add_zeros_and_len_padding;
808 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200809 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200810#endif
811#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200812 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200813 ctx->add_padding = add_zeros_padding;
814 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200815 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200816#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200817 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200818 ctx->add_padding = NULL;
819 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200820 break;
821
822 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200823 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200824 }
825
Paul Bakker1a45d912013-08-14 12:04:26 +0200826 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200827}
828
Paul Bakker8123e9d2011-01-06 15:37:30 +0000829#if defined(POLARSSL_SELF_TEST)
830
831#include <stdio.h>
832
833#define ASSERT(x) if (!(x)) { \
834 printf( "failed with %i at %s\n", value, (#x) ); \
835 return( 1 ); \
836}
837/*
838 * Checkup routine
839 */
840
841int cipher_self_test( int verbose )
842{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000843 ((void) verbose);
844
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845 return( 0 );
846}
847
848#endif
849
850#endif