blob: 160f4bcad432e151a1a0c28d3a910424d1245399 [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
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020037#if defined(POLARSSL_GCM_C)
38#include "polarssl/gcm.h"
39#endif
40
Paul Bakker8123e9d2011-01-06 15:37:30 +000041#include <stdlib.h>
42
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +020043#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020044#define POLARSSL_CIPHER_MODE_STREAM
45#endif
46
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000047#if defined _MSC_VER && !defined strcasecmp
48#define strcasecmp _stricmp
49#endif
50
Paul Bakker72f62662011-01-16 21:27:44 +000051static const int supported_ciphers[] = {
52
53#if defined(POLARSSL_AES_C)
54 POLARSSL_CIPHER_AES_128_CBC,
55 POLARSSL_CIPHER_AES_192_CBC,
56 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000057
58#if defined(POLARSSL_CIPHER_MODE_CFB)
59 POLARSSL_CIPHER_AES_128_CFB128,
60 POLARSSL_CIPHER_AES_192_CFB128,
61 POLARSSL_CIPHER_AES_256_CFB128,
62#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
63
64#if defined(POLARSSL_CIPHER_MODE_CTR)
65 POLARSSL_CIPHER_AES_128_CTR,
66 POLARSSL_CIPHER_AES_192_CTR,
67 POLARSSL_CIPHER_AES_256_CTR,
68#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
69
Paul Bakker72f62662011-01-16 21:27:44 +000070#endif /* defined(POLARSSL_AES_C) */
71
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020072#if defined(POLARSSL_ARC4_C)
73 POLARSSL_CIPHER_ARC4_128,
74#endif
75
Paul Bakker72f62662011-01-16 21:27:44 +000076#if defined(POLARSSL_CAMELLIA_C)
77 POLARSSL_CIPHER_CAMELLIA_128_CBC,
78 POLARSSL_CIPHER_CAMELLIA_192_CBC,
79 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +000080
81#if defined(POLARSSL_CIPHER_MODE_CFB)
82 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
83 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
84 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
85#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
86
87#if defined(POLARSSL_CIPHER_MODE_CTR)
88 POLARSSL_CIPHER_CAMELLIA_128_CTR,
89 POLARSSL_CIPHER_CAMELLIA_192_CTR,
90 POLARSSL_CIPHER_CAMELLIA_256_CTR,
91#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
92
Paul Bakker72f62662011-01-16 21:27:44 +000093#endif /* defined(POLARSSL_CAMELLIA_C) */
94
95#if defined(POLARSSL_DES_C)
96 POLARSSL_CIPHER_DES_CBC,
97 POLARSSL_CIPHER_DES_EDE_CBC,
98 POLARSSL_CIPHER_DES_EDE3_CBC,
99#endif /* defined(POLARSSL_DES_C) */
100
Paul Bakker6132d0a2012-07-04 17:10:40 +0000101#if defined(POLARSSL_BLOWFISH_C)
102 POLARSSL_CIPHER_BLOWFISH_CBC,
103
104#if defined(POLARSSL_CIPHER_MODE_CFB)
105 POLARSSL_CIPHER_BLOWFISH_CFB64,
106#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
107
108#if defined(POLARSSL_CIPHER_MODE_CTR)
109 POLARSSL_CIPHER_BLOWFISH_CTR,
110#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
111
112#endif /* defined(POLARSSL_BLOWFISH_C) */
113
Paul Bakkerfab5c822012-02-06 16:45:10 +0000114#if defined(POLARSSL_CIPHER_NULL_CIPHER)
115 POLARSSL_CIPHER_NULL,
116#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
117
Paul Bakker72f62662011-01-16 21:27:44 +0000118 0
119};
120
121const int *cipher_list( void )
122{
123 return supported_ciphers;
124}
125
Paul Bakkerec1b9842012-01-14 18:24:43 +0000126const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127{
128 /* Find static cipher information */
129 switch ( cipher_type )
130 {
131#if defined(POLARSSL_AES_C)
132 case POLARSSL_CIPHER_AES_128_CBC:
133 return &aes_128_cbc_info;
134 case POLARSSL_CIPHER_AES_192_CBC:
135 return &aes_192_cbc_info;
136 case POLARSSL_CIPHER_AES_256_CBC:
137 return &aes_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000138
139#if defined(POLARSSL_CIPHER_MODE_CFB)
140 case POLARSSL_CIPHER_AES_128_CFB128:
141 return &aes_128_cfb128_info;
142 case POLARSSL_CIPHER_AES_192_CFB128:
143 return &aes_192_cfb128_info;
144 case POLARSSL_CIPHER_AES_256_CFB128:
145 return &aes_256_cfb128_info;
146#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
147
148#if defined(POLARSSL_CIPHER_MODE_CTR)
149 case POLARSSL_CIPHER_AES_128_CTR:
150 return &aes_128_ctr_info;
151 case POLARSSL_CIPHER_AES_192_CTR:
152 return &aes_192_ctr_info;
153 case POLARSSL_CIPHER_AES_256_CTR:
154 return &aes_256_ctr_info;
155#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
156
Paul Bakker68884e32013-01-07 18:20:04 +0100157#if defined(POLARSSL_GCM_C)
158 case POLARSSL_CIPHER_AES_128_GCM:
159 return &aes_128_gcm_info;
160 case POLARSSL_CIPHER_AES_256_GCM:
161 return &aes_256_gcm_info;
162#endif /* defined(POLARSSL_GCM_C) */
163
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164#endif
165
166#if defined(POLARSSL_CAMELLIA_C)
167 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
168 return &camellia_128_cbc_info;
169 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
170 return &camellia_192_cbc_info;
171 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
172 return &camellia_256_cbc_info;
Paul Bakker343a8702011-06-09 14:27:58 +0000173
174#if defined(POLARSSL_CIPHER_MODE_CFB)
175 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
176 return &camellia_128_cfb128_info;
177 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
178 return &camellia_192_cfb128_info;
179 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
180 return &camellia_256_cfb128_info;
181#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
182
183#if defined(POLARSSL_CIPHER_MODE_CTR)
184 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
185 return &camellia_128_ctr_info;
186 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
187 return &camellia_192_ctr_info;
188 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
189 return &camellia_256_ctr_info;
190#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
191
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192#endif
193
194#if defined(POLARSSL_DES_C)
195 case POLARSSL_CIPHER_DES_CBC:
196 return &des_cbc_info;
197 case POLARSSL_CIPHER_DES_EDE_CBC:
198 return &des_ede_cbc_info;
199 case POLARSSL_CIPHER_DES_EDE3_CBC:
200 return &des_ede3_cbc_info;
201#endif
202
Paul Bakker68884e32013-01-07 18:20:04 +0100203#if defined(POLARSSL_ARC4_C)
204 case POLARSSL_CIPHER_ARC4_128:
205 return &arc4_128_info;
206#endif
207
Paul Bakker6132d0a2012-07-04 17:10:40 +0000208#if defined(POLARSSL_BLOWFISH_C)
209 case POLARSSL_CIPHER_BLOWFISH_CBC:
210 return &blowfish_cbc_info;
211
212#if defined(POLARSSL_CIPHER_MODE_CFB)
213 case POLARSSL_CIPHER_BLOWFISH_CFB64:
214 return &blowfish_cfb64_info;
215#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
216
217#if defined(POLARSSL_CIPHER_MODE_CTR)
218 case POLARSSL_CIPHER_BLOWFISH_CTR:
219 return &blowfish_ctr_info;
220#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
221
222#endif
223
Paul Bakkerfab5c822012-02-06 16:45:10 +0000224#if defined(POLARSSL_CIPHER_NULL_CIPHER)
225 case POLARSSL_CIPHER_NULL:
226 return &null_cipher_info;
227#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
228
Paul Bakker8123e9d2011-01-06 15:37:30 +0000229 default:
230 return NULL;
231 }
232}
233
234const cipher_info_t *cipher_info_from_string( const char *cipher_name )
235{
236 if( NULL == cipher_name )
237 return NULL;
238
Paul Bakker343a8702011-06-09 14:27:58 +0000239 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000240#if defined(POLARSSL_CAMELLIA_C)
241 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
242 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
243 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
244 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
245 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
246 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000247
248#if defined(POLARSSL_CIPHER_MODE_CFB)
249 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
250 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
251 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
252 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
253 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
254 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
255#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
256
257#if defined(POLARSSL_CIPHER_MODE_CTR)
258 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
259 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
260 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
261 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
262 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
263 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
264#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000265#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000266
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267#if defined(POLARSSL_AES_C)
268 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
269 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
270 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
271 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
272 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
273 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Paul Bakker343a8702011-06-09 14:27:58 +0000274
275#if defined(POLARSSL_CIPHER_MODE_CFB)
276 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
277 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
278 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
279 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
280 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
281 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
282#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
283
284#if defined(POLARSSL_CIPHER_MODE_CTR)
285 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
286 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
287 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
288 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
289 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
290 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
291#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200292
293#if defined(POLARSSL_GCM_C)
294 if( !strcasecmp( "AES-128-GCM", cipher_name ) )
295 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_GCM );
296 if( !strcasecmp( "AES-256-GCM", cipher_name ) )
297 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_GCM );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200299#endif /* POLARSSL_AES_C */
Paul Bakker343a8702011-06-09 14:27:58 +0000300
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200301#if defined(POLARSSL_ARC4_C)
302 if( !strcasecmp( "ARC4-128", cipher_name ) )
303 return( cipher_info_from_type( POLARSSL_CIPHER_ARC4_128 ) );
304#endif
305
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306#if defined(POLARSSL_DES_C)
307 if( !strcasecmp( "DES-CBC", cipher_name ) )
308 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
309 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
310 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
311 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
312 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
313#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000314
Paul Bakker6132d0a2012-07-04 17:10:40 +0000315#if defined(POLARSSL_BLOWFISH_C)
316 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
317 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
318
319#if defined(POLARSSL_CIPHER_MODE_CFB)
320 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
321 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
322#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
323
324#if defined(POLARSSL_CIPHER_MODE_CTR)
325 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
326 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
327#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
328#endif
329
Paul Bakkerfab5c822012-02-06 16:45:10 +0000330#if defined(POLARSSL_CIPHER_NULL_CIPHER)
331 if( !strcasecmp( "NULL", cipher_name ) )
332 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
333#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
334
Paul Bakker8123e9d2011-01-06 15:37:30 +0000335 return NULL;
336}
337
338int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
339{
340 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000341 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000342
Paul Bakker279432a2012-04-26 10:09:35 +0000343 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
Paul Bakker343a8702011-06-09 14:27:58 +0000345 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000346 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
348 ctx->cipher_info = cipher_info;
349
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200350 /*
351 * Ignore possible errors caused by a cipher mode that doesn't use padding
352 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200353#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200354 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200355#else
356 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
357#endif
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200358
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359 return 0;
360}
361
362int cipher_free_ctx( cipher_context_t *ctx )
363{
364 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000365 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366
Paul Bakker343a8702011-06-09 14:27:58 +0000367 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000368
369 return 0;
370}
371
372int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
373 int key_length, const operation_t operation )
374{
375 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000376 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377
378 ctx->key_length = key_length;
379 ctx->operation = operation;
380
Paul Bakker343a8702011-06-09 14:27:58 +0000381 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000382 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000383 */
384 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000385 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000386 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
387 {
388 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000390 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
Paul Bakker343a8702011-06-09 14:27:58 +0000392 if( POLARSSL_DECRYPT == operation )
393 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394 ctx->key_length );
395
Paul Bakkerff61a782011-06-09 15:42:02 +0000396 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397}
398
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200399int cipher_set_iv( cipher_context_t *ctx,
400 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000401{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200402 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200403
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000405 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200407 if( ctx->cipher_info->accepts_variable_iv_size )
408 actual_iv_size = iv_len;
409 else
410 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200411
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200412 memcpy( ctx->iv, iv, actual_iv_size );
413 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200414
415 return 0;
416}
417
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200418int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200419{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200420 if( NULL == ctx || NULL == ctx->cipher_info )
421 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
422
Paul Bakker8123e9d2011-01-06 15:37:30 +0000423 ctx->unprocessed_len = 0;
424
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200425 return 0;
426}
427
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200428#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200429int cipher_update_ad( cipher_context_t *ctx,
430 const unsigned char *ad, size_t ad_len )
431{
432 if( NULL == ctx || NULL == ctx->cipher_info )
433 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
434
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200435#if defined(POLARSSL_GCM_C)
436 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
437 {
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200438 /* Make sure we're called right after cipher_reset() */
439 if( ((gcm_context *) ctx->cipher_ctx)->len != 0 ||
440 ((gcm_context *) ctx->cipher_ctx)->add_len != 0 )
441 {
442 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
443 }
444
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200445 return gcm_starts( ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200446 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200447 }
448#endif
449
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450 return 0;
451}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200452#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453
Paul Bakker23986e52011-04-24 08:57:21 +0000454int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
455 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456{
Paul Bakkerff61a782011-06-09 15:42:02 +0000457 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000458 size_t copy_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
Paul Bakker68884e32013-01-07 18:20:04 +0100460 *olen = 0;
461
462 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000463 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000464 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000465 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466
Paul Bakker68884e32013-01-07 18:20:04 +0100467 if( input == output &&
468 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
469 {
470 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
471 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200473 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC ||
474 ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475 {
476 /*
477 * If there is not enough data for a full block, cache it.
478 */
479 if( ( ctx->operation == POLARSSL_DECRYPT &&
480 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
481 ( ctx->operation == POLARSSL_ENCRYPT &&
482 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
483 {
484 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
485 ilen );
486
487 ctx->unprocessed_len += ilen;
488 return 0;
489 }
490
491 /*
492 * Process cached data first
493 */
494 if( ctx->unprocessed_len != 0 )
495 {
496 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
497
498 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
499 copy_len );
500
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200501#if defined(POLARSSL_GCM_C)
502 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
503 {
504 if( 0 != ( ret = gcm_update( ctx->cipher_ctx,
505 cipher_get_block_size( ctx ),
506 ctx->unprocessed_data, output ) ) )
507 {
508 return ret;
509 }
510 }
511 else
512#endif
Paul Bakkerff61a782011-06-09 15:42:02 +0000513 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000515 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000517 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000518 }
519
520 *olen += cipher_get_block_size( ctx );
521 output += cipher_get_block_size( ctx );
522 ctx->unprocessed_len = 0;
523
524 input += copy_len;
525 ilen -= copy_len;
526 }
527
528 /*
529 * Cache final, incomplete block
530 */
531 if( 0 != ilen )
532 {
533 copy_len = ilen % cipher_get_block_size( ctx );
534 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
535 copy_len = cipher_get_block_size(ctx);
536
537 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
538 copy_len );
539
540 ctx->unprocessed_len += copy_len;
541 ilen -= copy_len;
542 }
543
544 /*
545 * Process remaining full blocks
546 */
547 if( ilen )
548 {
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200549#if defined(POLARSSL_GCM_C)
550 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
551 {
552 if( 0 != ( ret = gcm_update( ctx->cipher_ctx,
553 ilen, input, output ) ) )
554 {
555 return ret;
556 }
557 }
558 else
559#endif
Paul Bakkerff61a782011-06-09 15:42:02 +0000560 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
561 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000563 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200565
Paul Bakker8123e9d2011-01-06 15:37:30 +0000566 *olen += ilen;
567 }
568
569 return 0;
570 }
571
Paul Bakker68884e32013-01-07 18:20:04 +0100572#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000573 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000574 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000575 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000576 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000577 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000578 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000579 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000580 }
581
582 *olen = ilen;
583
584 return 0;
585 }
Paul Bakker68884e32013-01-07 18:20:04 +0100586#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000587
Paul Bakker68884e32013-01-07 18:20:04 +0100588#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000589 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
590 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000591 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000592 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000593 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000594 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000595 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000596 }
597
598 *olen = ilen;
599
600 return 0;
601 }
Paul Bakker68884e32013-01-07 18:20:04 +0100602#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000603
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200604#if defined(POLARSSL_CIPHER_MODE_STREAM)
605 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
606 {
607 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
608 ilen, input, output ) ) )
609 {
610 return ret;
611 }
612
613 *olen = ilen;
614
615 return 0;
616 }
617#endif
618
Paul Bakkerff61a782011-06-09 15:42:02 +0000619 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620}
621
Paul Bakker48e93c82013-08-14 12:21:18 +0200622#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200623/*
624 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
625 */
Paul Bakker23986e52011-04-24 08:57:21 +0000626static void add_pkcs_padding( unsigned char *output, size_t output_len,
627 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628{
Paul Bakker23986e52011-04-24 08:57:21 +0000629 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630 unsigned char i = 0;
631
632 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000633 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634}
635
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200636static int get_pkcs_padding( unsigned char *input, size_t input_len,
637 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000639 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640
Paul Bakkera885d682011-01-20 16:35:05 +0000641 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000642 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
644 padding_len = input[input_len - 1];
645
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200646 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000647 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
Paul Bakkera885d682011-01-20 16:35:05 +0000649 for( i = input_len - padding_len; i < input_len; i++ )
650 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000651 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
653 *data_len = input_len - padding_len;
654
655 return 0;
656}
Paul Bakker48e93c82013-08-14 12:21:18 +0200657#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
Paul Bakker48e93c82013-08-14 12:21:18 +0200659#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200660/*
661 * One and zeros padding: fill with 80 00 ... 00
662 */
663static void add_one_and_zeros_padding( unsigned char *output,
664 size_t output_len, size_t data_len )
665{
666 size_t padding_len = output_len - data_len;
667 unsigned char i = 0;
668
669 output[data_len] = 0x80;
670 for( i = 1; i < padding_len; i++ )
671 output[data_len + i] = 0x00;
672}
673
674static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
675 size_t *data_len )
676{
677 unsigned char *p = input + input_len - 1;
678
679 if( NULL == input || NULL == data_len )
680 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
681
682 while( *p == 0x00 && p > input )
683 --p;
684
685 if( *p != 0x80 )
686 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
687
688 *data_len = p - input;
689
690 return 0;
691}
Paul Bakker48e93c82013-08-14 12:21:18 +0200692#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200693
Paul Bakker48e93c82013-08-14 12:21:18 +0200694#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200695/*
696 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
697 */
698static void add_zeros_and_len_padding( unsigned char *output,
699 size_t output_len, size_t data_len )
700{
701 size_t padding_len = output_len - data_len;
702 unsigned char i = 0;
703
704 for( i = 1; i < padding_len; i++ )
705 output[data_len + i - 1] = 0x00;
706 output[output_len - 1] = (unsigned char) padding_len;
707}
708
709static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
710 size_t *data_len )
711{
712 unsigned int i, padding_len = 0;
713
714 if( NULL == input || NULL == data_len )
715 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
716
717 padding_len = input[input_len - 1];
718
719 if( padding_len > input_len || padding_len == 0 )
720 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
721
722 for( i = input_len - padding_len; i < input_len - 1; i++ )
723 if( input[i] != 0x00 )
724 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
725
726 *data_len = input_len - padding_len;
727
728 return 0;
729}
Paul Bakker48e93c82013-08-14 12:21:18 +0200730#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200731
Paul Bakker48e93c82013-08-14 12:21:18 +0200732#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200733/*
734 * Zero padding: fill with 00 ... 00
735 */
736static void add_zeros_padding( unsigned char *output,
737 size_t output_len, size_t data_len )
738{
739 unsigned char i;
740
741 for( i = data_len; i < output_len; i++ )
742 output[i] = 0x00;
743}
744
745static int get_zeros_padding( unsigned char *input, size_t input_len,
746 size_t *data_len )
747{
748 unsigned char *p = input + input_len - 1;
749 if( NULL == input || NULL == data_len )
750 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
751
752 while( *p == 0x00 && p > input )
753 --p;
754
755 *data_len = *p == 0x00 ? 0 : p - input + 1;
756
757 return 0;
758}
Paul Bakker48e93c82013-08-14 12:21:18 +0200759#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200760
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200761/*
762 * No padding: don't pad :)
763 *
764 * There is no add_padding function (check for NULL in cipher_finish)
765 * but a trivial get_padding function
766 */
767static int get_no_padding( unsigned char *input, size_t input_len,
768 size_t *data_len )
769{
770 if( NULL == input || NULL == data_len )
771 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
772
773 *data_len = input_len;
774
775 return 0;
776}
777
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200778int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200779 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780{
Paul Bakkerff61a782011-06-09 15:42:02 +0000781 int ret = 0;
782
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +0000784 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785
786 *olen = 0;
787
Paul Bakker6132d0a2012-07-04 17:10:40 +0000788 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +0000789 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200790 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +0000791 {
792 return 0;
793 }
794
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200795#if defined(POLARSSL_GCM_C)
796 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
797 {
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200798 if( 0 != ( ret = gcm_update( ctx->cipher_ctx,
799 ctx->unprocessed_len, ctx->unprocessed_data,
800 output ) ) )
801 {
802 return( ret );
803 }
804
805 *olen += ctx->unprocessed_len;
806
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200807 return( 0 );
808 }
809#endif
810
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
812 {
813 if( POLARSSL_ENCRYPT == ctx->operation )
814 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200815 /* check for 'no padding' mode */
816 if( NULL == ctx->add_padding )
817 {
818 if( 0 != ctx->unprocessed_len )
819 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
820
821 return 0;
822 }
823
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200824 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +0000825 ctx->unprocessed_len );
826 }
827 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
828 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200829 /*
830 * For decrypt operations, expect a full block,
831 * or an empty block if no padding
832 */
833 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
834 return 0;
835
Paul Bakkerff61a782011-06-09 15:42:02 +0000836 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000837 }
838
839 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +0000840 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
841 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
842 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000843 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000844 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000845 }
846
847 /* Set output size for decryption */
848 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200849 return ctx->get_padding( output, cipher_get_block_size( ctx ),
850 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851
852 /* Set output size for encryption */
853 *olen = cipher_get_block_size( ctx );
854 return 0;
855 }
856
Paul Bakkerff61a782011-06-09 15:42:02 +0000857 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000858}
859
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200860int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
861{
862 if( NULL == ctx ||
863 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
864 {
865 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
866 }
867
Paul Bakker1a45d912013-08-14 12:04:26 +0200868 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200869 {
Paul Bakker48e93c82013-08-14 12:21:18 +0200870#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +0200871 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200872 ctx->add_padding = add_pkcs_padding;
873 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200874 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200875#endif
876#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200877 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200878 ctx->add_padding = add_one_and_zeros_padding;
879 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200880 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200881#endif
882#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +0200883 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200884 ctx->add_padding = add_zeros_and_len_padding;
885 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200886 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200887#endif
888#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +0200889 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200890 ctx->add_padding = add_zeros_padding;
891 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200892 break;
Paul Bakker48e93c82013-08-14 12:21:18 +0200893#endif
Paul Bakker1a45d912013-08-14 12:04:26 +0200894 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200895 ctx->add_padding = NULL;
896 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +0200897 break;
898
899 default:
Paul Bakker48e93c82013-08-14 12:21:18 +0200900 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200901 }
902
Paul Bakker1a45d912013-08-14 12:04:26 +0200903 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200904}
905
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200906#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200907int cipher_write_tag( cipher_context_t *ctx,
908 unsigned char *tag, size_t tag_len )
909{
910 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
911 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
912
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200913 if( POLARSSL_ENCRYPT != ctx->operation )
914 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
915
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200916#if defined(POLARSSL_GCM_C)
917 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
918 return gcm_finish( ctx->cipher_ctx, tag, tag_len );
919#endif
920
921 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200922}
923
924int cipher_check_tag( cipher_context_t *ctx,
925 const unsigned char *tag, size_t tag_len )
926{
927 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200928
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200929 if( NULL == ctx || NULL == ctx->cipher_info ||
930 POLARSSL_DECRYPT != ctx->operation )
931 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200932 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200933 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200934
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200935#if defined(POLARSSL_GCM_C)
936 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
937 {
938 unsigned char check_tag[16];
939 size_t i;
940 int diff;
941
942 if( tag_len > sizeof( check_tag ) )
943 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
944
945 if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
946 return( ret );
947
948 /* Check the tag in "constant-time" */
949 for( diff = 0, i = 0; i < tag_len; i++ )
950 diff |= tag[i] ^ check_tag[i];
951
952 if( diff != 0 )
953 return( POLARSSL_ERR_GCM_AUTH_FAILED );
954
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200955 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200956 }
957#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200958
959 return( 0 );
960}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200961#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200962
Paul Bakker8123e9d2011-01-06 15:37:30 +0000963#if defined(POLARSSL_SELF_TEST)
964
965#include <stdio.h>
966
967#define ASSERT(x) if (!(x)) { \
968 printf( "failed with %i at %s\n", value, (#x) ); \
969 return( 1 ); \
970}
971/*
972 * Checkup routine
973 */
974
975int cipher_self_test( int verbose )
976{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000977 ((void) verbose);
978
Paul Bakker8123e9d2011-01-06 15:37:30 +0000979 return( 0 );
980}
981
982#endif
983
984#endif