blob: c455ff9b714a7d9809cfec6d78f26ba954280d51 [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)
Paul Bakker5e0efa72013-09-08 23:04:04 +020054 POLARSSL_CIPHER_AES_128_ECB,
55 POLARSSL_CIPHER_AES_192_ECB,
56 POLARSSL_CIPHER_AES_256_ECB,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020057
58#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker72f62662011-01-16 21:27:44 +000059 POLARSSL_CIPHER_AES_128_CBC,
60 POLARSSL_CIPHER_AES_192_CBC,
61 POLARSSL_CIPHER_AES_256_CBC,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020062#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +000063
64#if defined(POLARSSL_CIPHER_MODE_CFB)
65 POLARSSL_CIPHER_AES_128_CFB128,
66 POLARSSL_CIPHER_AES_192_CFB128,
67 POLARSSL_CIPHER_AES_256_CFB128,
68#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
69
70#if defined(POLARSSL_CIPHER_MODE_CTR)
71 POLARSSL_CIPHER_AES_128_CTR,
72 POLARSSL_CIPHER_AES_192_CTR,
73 POLARSSL_CIPHER_AES_256_CTR,
74#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
75
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +020076#if defined(POLARSSL_GCM_C)
77 POLARSSL_CIPHER_AES_128_GCM,
78 POLARSSL_CIPHER_AES_192_GCM,
79 POLARSSL_CIPHER_AES_256_GCM,
80#endif /* defined(POLARSSL_GCM_C) */
81
Paul Bakker72f62662011-01-16 21:27:44 +000082#endif /* defined(POLARSSL_AES_C) */
83
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020084#if defined(POLARSSL_ARC4_C)
85 POLARSSL_CIPHER_ARC4_128,
86#endif
87
Paul Bakker72f62662011-01-16 21:27:44 +000088#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +020089 POLARSSL_CIPHER_CAMELLIA_128_ECB,
90 POLARSSL_CIPHER_CAMELLIA_192_ECB,
91 POLARSSL_CIPHER_CAMELLIA_256_ECB,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020092
93#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker72f62662011-01-16 21:27:44 +000094 POLARSSL_CIPHER_CAMELLIA_128_CBC,
95 POLARSSL_CIPHER_CAMELLIA_192_CBC,
96 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +020097#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +000098
99#if defined(POLARSSL_CIPHER_MODE_CFB)
100 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
101 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
102 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
103#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
104
105#if defined(POLARSSL_CIPHER_MODE_CTR)
106 POLARSSL_CIPHER_CAMELLIA_128_CTR,
107 POLARSSL_CIPHER_CAMELLIA_192_CTR,
108 POLARSSL_CIPHER_CAMELLIA_256_CTR,
109#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
110
Paul Bakker72f62662011-01-16 21:27:44 +0000111#endif /* defined(POLARSSL_CAMELLIA_C) */
112
113#if defined(POLARSSL_DES_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200114 POLARSSL_CIPHER_DES_ECB,
115 POLARSSL_CIPHER_DES_EDE_ECB,
116 POLARSSL_CIPHER_DES_EDE3_ECB,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200117
118#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker72f62662011-01-16 21:27:44 +0000119 POLARSSL_CIPHER_DES_CBC,
120 POLARSSL_CIPHER_DES_EDE_CBC,
121 POLARSSL_CIPHER_DES_EDE3_CBC,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200122#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker72f62662011-01-16 21:27:44 +0000123#endif /* defined(POLARSSL_DES_C) */
124
Paul Bakker6132d0a2012-07-04 17:10:40 +0000125#if defined(POLARSSL_BLOWFISH_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200126 POLARSSL_CIPHER_BLOWFISH_ECB,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200127
128#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000129 POLARSSL_CIPHER_BLOWFISH_CBC,
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200130#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000131
132#if defined(POLARSSL_CIPHER_MODE_CFB)
133 POLARSSL_CIPHER_BLOWFISH_CFB64,
134#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
135
136#if defined(POLARSSL_CIPHER_MODE_CTR)
137 POLARSSL_CIPHER_BLOWFISH_CTR,
138#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
139
140#endif /* defined(POLARSSL_BLOWFISH_C) */
141
Paul Bakkerfab5c822012-02-06 16:45:10 +0000142#if defined(POLARSSL_CIPHER_NULL_CIPHER)
143 POLARSSL_CIPHER_NULL,
144#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
145
Paul Bakker72f62662011-01-16 21:27:44 +0000146 0
147};
148
149const int *cipher_list( void )
150{
151 return supported_ciphers;
152}
153
Paul Bakkerec1b9842012-01-14 18:24:43 +0000154const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155{
156 /* Find static cipher information */
157 switch ( cipher_type )
158 {
159#if defined(POLARSSL_AES_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200160 case POLARSSL_CIPHER_AES_128_ECB:
161 return &aes_128_ecb_info;
162 case POLARSSL_CIPHER_AES_192_ECB:
163 return &aes_192_ecb_info;
164 case POLARSSL_CIPHER_AES_256_ECB:
165 return &aes_256_ecb_info;
166
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200167#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168 case POLARSSL_CIPHER_AES_128_CBC:
169 return &aes_128_cbc_info;
170 case POLARSSL_CIPHER_AES_192_CBC:
171 return &aes_192_cbc_info;
172 case POLARSSL_CIPHER_AES_256_CBC:
173 return &aes_256_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200174#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000175
176#if defined(POLARSSL_CIPHER_MODE_CFB)
177 case POLARSSL_CIPHER_AES_128_CFB128:
178 return &aes_128_cfb128_info;
179 case POLARSSL_CIPHER_AES_192_CFB128:
180 return &aes_192_cfb128_info;
181 case POLARSSL_CIPHER_AES_256_CFB128:
182 return &aes_256_cfb128_info;
183#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
184
185#if defined(POLARSSL_CIPHER_MODE_CTR)
186 case POLARSSL_CIPHER_AES_128_CTR:
187 return &aes_128_ctr_info;
188 case POLARSSL_CIPHER_AES_192_CTR:
189 return &aes_192_ctr_info;
190 case POLARSSL_CIPHER_AES_256_CTR:
191 return &aes_256_ctr_info;
192#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
193
Paul Bakker68884e32013-01-07 18:20:04 +0100194#if defined(POLARSSL_GCM_C)
195 case POLARSSL_CIPHER_AES_128_GCM:
196 return &aes_128_gcm_info;
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200197 case POLARSSL_CIPHER_AES_192_GCM:
198 return &aes_192_gcm_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100199 case POLARSSL_CIPHER_AES_256_GCM:
200 return &aes_256_gcm_info;
201#endif /* defined(POLARSSL_GCM_C) */
202
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203#endif
204
205#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200206 case POLARSSL_CIPHER_CAMELLIA_128_ECB:
207 return &camellia_128_ecb_info;
208 case POLARSSL_CIPHER_CAMELLIA_192_ECB:
209 return &camellia_192_ecb_info;
210 case POLARSSL_CIPHER_CAMELLIA_256_ECB:
211 return &camellia_256_ecb_info;
212
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200213#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214 case POLARSSL_CIPHER_CAMELLIA_128_CBC:
215 return &camellia_128_cbc_info;
216 case POLARSSL_CIPHER_CAMELLIA_192_CBC:
217 return &camellia_192_cbc_info;
218 case POLARSSL_CIPHER_CAMELLIA_256_CBC:
219 return &camellia_256_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200220#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000221
222#if defined(POLARSSL_CIPHER_MODE_CFB)
223 case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
224 return &camellia_128_cfb128_info;
225 case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
226 return &camellia_192_cfb128_info;
227 case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
228 return &camellia_256_cfb128_info;
229#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
230
231#if defined(POLARSSL_CIPHER_MODE_CTR)
232 case POLARSSL_CIPHER_CAMELLIA_128_CTR:
233 return &camellia_128_ctr_info;
234 case POLARSSL_CIPHER_CAMELLIA_192_CTR:
235 return &camellia_192_ctr_info;
236 case POLARSSL_CIPHER_CAMELLIA_256_CTR:
237 return &camellia_256_ctr_info;
238#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
239
Paul Bakker8123e9d2011-01-06 15:37:30 +0000240#endif
241
242#if defined(POLARSSL_DES_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200243 case POLARSSL_CIPHER_DES_ECB:
244 return &des_ecb_info;
245 case POLARSSL_CIPHER_DES_EDE_ECB:
246 return &des_ede_ecb_info;
247 case POLARSSL_CIPHER_DES_EDE3_ECB:
248 return &des_ede3_ecb_info;
249
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200250#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000251 case POLARSSL_CIPHER_DES_CBC:
252 return &des_cbc_info;
253 case POLARSSL_CIPHER_DES_EDE_CBC:
254 return &des_ede_cbc_info;
255 case POLARSSL_CIPHER_DES_EDE3_CBC:
256 return &des_ede3_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200257#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258#endif
259
Paul Bakker68884e32013-01-07 18:20:04 +0100260#if defined(POLARSSL_ARC4_C)
261 case POLARSSL_CIPHER_ARC4_128:
262 return &arc4_128_info;
263#endif
264
Paul Bakker6132d0a2012-07-04 17:10:40 +0000265#if defined(POLARSSL_BLOWFISH_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200266 case POLARSSL_CIPHER_BLOWFISH_ECB:
267 return &blowfish_ecb_info;
268
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200269#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000270 case POLARSSL_CIPHER_BLOWFISH_CBC:
271 return &blowfish_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200272#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000273
274#if defined(POLARSSL_CIPHER_MODE_CFB)
275 case POLARSSL_CIPHER_BLOWFISH_CFB64:
276 return &blowfish_cfb64_info;
277#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
278
279#if defined(POLARSSL_CIPHER_MODE_CTR)
280 case POLARSSL_CIPHER_BLOWFISH_CTR:
281 return &blowfish_ctr_info;
282#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
283
284#endif
285
Paul Bakkerfab5c822012-02-06 16:45:10 +0000286#if defined(POLARSSL_CIPHER_NULL_CIPHER)
287 case POLARSSL_CIPHER_NULL:
288 return &null_cipher_info;
289#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
290
Paul Bakker8123e9d2011-01-06 15:37:30 +0000291 default:
292 return NULL;
293 }
294}
295
296const cipher_info_t *cipher_info_from_string( const char *cipher_name )
297{
298 if( NULL == cipher_name )
299 return NULL;
300
Paul Bakker343a8702011-06-09 14:27:58 +0000301 /* Get the appropriate cipher information */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000302#if defined(POLARSSL_CAMELLIA_C)
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200303#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304 if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
305 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
306 if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
307 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
308 if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
309 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200310#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000311
312#if defined(POLARSSL_CIPHER_MODE_CFB)
313 if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
314 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
315 if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
316 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
317 if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
318 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
319#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
320
321#if defined(POLARSSL_CIPHER_MODE_CTR)
322 if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
323 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
324 if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
325 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
326 if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
327 return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
328#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000330
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200332#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333 if( !strcasecmp( "AES-128-CBC", cipher_name ) )
334 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
335 if( !strcasecmp( "AES-192-CBC", cipher_name ) )
336 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
337 if( !strcasecmp( "AES-256-CBC", cipher_name ) )
338 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200339#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000340
341#if defined(POLARSSL_CIPHER_MODE_CFB)
342 if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
343 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
344 if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
345 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
346 if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
347 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
348#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
349
350#if defined(POLARSSL_CIPHER_MODE_CTR)
351 if( !strcasecmp( "AES-128-CTR", cipher_name ) )
352 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
353 if( !strcasecmp( "AES-192-CTR", cipher_name ) )
354 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
355 if( !strcasecmp( "AES-256-CTR", cipher_name ) )
356 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
357#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200358
359#if defined(POLARSSL_GCM_C)
360 if( !strcasecmp( "AES-128-GCM", cipher_name ) )
361 return cipher_info_from_type( POLARSSL_CIPHER_AES_128_GCM );
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200362 if( !strcasecmp( "AES-192-GCM", cipher_name ) )
363 return cipher_info_from_type( POLARSSL_CIPHER_AES_192_GCM );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200364 if( !strcasecmp( "AES-256-GCM", cipher_name ) )
365 return cipher_info_from_type( POLARSSL_CIPHER_AES_256_GCM );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366#endif
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200367#endif /* POLARSSL_AES_C */
Paul Bakker343a8702011-06-09 14:27:58 +0000368
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200369#if defined(POLARSSL_ARC4_C)
370 if( !strcasecmp( "ARC4-128", cipher_name ) )
371 return( cipher_info_from_type( POLARSSL_CIPHER_ARC4_128 ) );
372#endif
373
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374#if defined(POLARSSL_DES_C)
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200375#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376 if( !strcasecmp( "DES-CBC", cipher_name ) )
377 return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
378 if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
379 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
380 if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
381 return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200382#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000383#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000384
Paul Bakker6132d0a2012-07-04 17:10:40 +0000385#if defined(POLARSSL_BLOWFISH_C)
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200386#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000387 if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
388 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200389#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000390
391#if defined(POLARSSL_CIPHER_MODE_CFB)
392 if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
393 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
394#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
395
396#if defined(POLARSSL_CIPHER_MODE_CTR)
397 if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
398 return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
399#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
400#endif
401
Paul Bakkerfab5c822012-02-06 16:45:10 +0000402#if defined(POLARSSL_CIPHER_NULL_CIPHER)
403 if( !strcasecmp( "NULL", cipher_name ) )
404 return cipher_info_from_type( POLARSSL_CIPHER_NULL );
405#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
406
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 return NULL;
408}
409
Paul Bakkerf46b6952013-09-09 00:08:26 +0200410const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
411 int key_length,
412 const cipher_mode_t mode )
413{
414#if defined(POLARSSL_AES_C)
415 if( cipher_id == POLARSSL_CIPHER_ID_AES )
416 {
417 if( mode == POLARSSL_MODE_ECB )
418 {
419 if( key_length == 128 )
420 return &aes_128_ecb_info;
421 if( key_length == 192 )
422 return &aes_192_ecb_info;
423 if( key_length == 256 )
424 return &aes_256_ecb_info;
425 }
426
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200427#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200428 if( mode == POLARSSL_MODE_CBC )
429 {
430 if( key_length == 128 )
431 return &aes_128_cbc_info;
432 if( key_length == 192 )
433 return &aes_192_cbc_info;
434 if( key_length == 256 )
435 return &aes_256_cbc_info;
436 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200437#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerf46b6952013-09-09 00:08:26 +0200438
439#if defined(POLARSSL_CIPHER_MODE_CFB)
440 if( mode == POLARSSL_MODE_CFB )
441 {
442 if( key_length == 128 )
443 return &aes_128_cfb128_info;
444 if( key_length == 192 )
445 return &aes_192_cfb128_info;
446 if( key_length == 256 )
447 return &aes_256_cfb128_info;
448 }
449#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
450
451#if defined(POLARSSL_CIPHER_MODE_CTR)
452 if( mode == POLARSSL_MODE_CTR )
453 {
454 if( key_length == 128 )
455 return &aes_128_ctr_info;
456 if( key_length == 192 )
457 return &aes_192_ctr_info;
458 if( key_length == 256 )
459 return &aes_256_ctr_info;
460 }
461#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
462
463#if defined(POLARSSL_GCM_C)
464 if( mode == POLARSSL_MODE_GCM )
465 {
466 if( key_length == 128 )
467 return &aes_128_gcm_info;
468 if( key_length == 192 )
469 return &aes_192_gcm_info;
470 if( key_length == 256 )
471 return &aes_256_gcm_info;
472 }
473#endif /* defined(POLARSSL_GCM_C) */
474 }
475#endif
476
477#if defined(POLARSSL_CAMELLIA_C)
478 if( cipher_id == POLARSSL_CIPHER_ID_CAMELLIA )
479 {
480 if( mode == POLARSSL_MODE_ECB )
481 {
482 if( key_length == 128 )
483 return &camellia_128_ecb_info;
484 if( key_length == 192 )
485 return &camellia_192_ecb_info;
486 if( key_length == 256 )
487 return &camellia_256_ecb_info;
488 }
489
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200490#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200491 if( mode == POLARSSL_MODE_CBC )
492 {
493 if( key_length == 128 )
494 return &camellia_128_cbc_info;
495 if( key_length == 192 )
496 return &camellia_192_cbc_info;
497 if( key_length == 256 )
498 return &camellia_256_cbc_info;
499 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200500#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerf46b6952013-09-09 00:08:26 +0200501
502#if defined(POLARSSL_CIPHER_MODE_CFB)
503 if( mode == POLARSSL_MODE_CFB )
504 {
505 if( key_length == 128 )
506 return &camellia_128_cfb128_info;
507 if( key_length == 192 )
508 return &camellia_192_cfb128_info;
509 if( key_length == 256 )
510 return &camellia_256_cfb128_info;
511 }
512#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
513
514#if defined(POLARSSL_CIPHER_MODE_CTR)
515 if( mode == POLARSSL_MODE_CTR )
516 {
517 if( key_length == 128 )
518 return &camellia_128_ctr_info;
519 if( key_length == 192 )
520 return &camellia_192_ctr_info;
521 if( key_length == 256 )
522 return &camellia_256_ctr_info;
523 }
524#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
525 }
526#endif
527
528#if defined(POLARSSL_DES_C)
Paul Bakker2a6a3a72013-09-10 14:29:28 +0200529 if( cipher_id == POLARSSL_CIPHER_ID_DES && key_length == 64 )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200530 {
531 if( mode == POLARSSL_MODE_ECB )
532 return &des_ecb_info;
533
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200534#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200535 if( mode == POLARSSL_MODE_CBC )
536 return &des_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200537#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerf46b6952013-09-09 00:08:26 +0200538 }
539
540 if( cipher_id == POLARSSL_CIPHER_ID_3DES )
541 {
542 if( mode == POLARSSL_MODE_ECB )
543 {
544 if( key_length == 128 )
545 return &des_ede_ecb_info;
546 if( key_length == 192 )
547 return &des_ede3_ecb_info;
548 }
549
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200550#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200551 if( mode == POLARSSL_MODE_CBC )
552 {
553 if( key_length == 128 )
554 return &des_ede_cbc_info;
555 if( key_length == 192 )
556 return &des_ede3_cbc_info;
557 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200558#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerf46b6952013-09-09 00:08:26 +0200559 }
560#endif
561
562#if defined(POLARSSL_ARC4_C)
Paul Bakker2a6a3a72013-09-10 14:29:28 +0200563 if( cipher_id == POLARSSL_CIPHER_ID_ARC4 &&
564 key_length == 128 && mode == POLARSSL_MODE_STREAM )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200565 return &arc4_128_info;
566#endif
567
568#if defined(POLARSSL_BLOWFISH_C)
Paul Bakker2a6a3a72013-09-10 14:29:28 +0200569 if( cipher_id == POLARSSL_CIPHER_ID_BLOWFISH && key_length == 128 )
Paul Bakkerf46b6952013-09-09 00:08:26 +0200570 {
571 if( mode == POLARSSL_MODE_ECB )
572 return &blowfish_ecb_info;
573
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200574#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerf46b6952013-09-09 00:08:26 +0200575 if( mode == POLARSSL_MODE_CBC )
576 return &blowfish_cbc_info;
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200577#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerf46b6952013-09-09 00:08:26 +0200578
579#if defined(POLARSSL_CIPHER_MODE_CFB)
580 if( mode == POLARSSL_MODE_CFB )
581 return &blowfish_cfb64_info;
582#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
583
584#if defined(POLARSSL_CIPHER_MODE_CTR)
585 if( mode == POLARSSL_MODE_CTR )
586 return &blowfish_ctr_info;
587#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
588 }
589#endif
590
591#if defined(POLARSSL_CIPHER_NULL_CIPHER)
592 if( cipher_id == POLARSSL_CIPHER_ID_NULL )
593 return &null_cipher_info;
594#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
595
596 return NULL;
597}
598
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
600{
601 if( NULL == cipher_info || NULL == ctx )
Paul Bakkerff61a782011-06-09 15:42:02 +0000602 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
Paul Bakker279432a2012-04-26 10:09:35 +0000604 memset( ctx, 0, sizeof( cipher_context_t ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Paul Bakker343a8702011-06-09 14:27:58 +0000606 if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
Paul Bakkerff61a782011-06-09 15:42:02 +0000607 return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608
609 ctx->cipher_info = cipher_info;
610
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200611#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200612 /*
613 * Ignore possible errors caused by a cipher mode that doesn't use padding
614 */
Paul Bakker48e93c82013-08-14 12:21:18 +0200615#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200616 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
Paul Bakker48e93c82013-08-14 12:21:18 +0200617#else
618 (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
619#endif
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200620#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200621
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622 return 0;
623}
624
625int cipher_free_ctx( cipher_context_t *ctx )
626{
627 if( ctx == NULL || ctx->cipher_info == NULL )
Paul Bakkerff61a782011-06-09 15:42:02 +0000628 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629
Paul Bakker343a8702011-06-09 14:27:58 +0000630 ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631
632 return 0;
633}
634
635int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
636 int key_length, const operation_t operation )
637{
638 if( NULL == ctx || NULL == ctx->cipher_info )
Paul Bakkerff61a782011-06-09 15:42:02 +0000639 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640
Manuel Pégourié-Gonnarddd0f57f2013-09-16 11:47:43 +0200641 if( (int) ctx->cipher_info->key_length != key_length )
642 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
643
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644 ctx->key_length = key_length;
645 ctx->operation = operation;
646
Paul Bakker343a8702011-06-09 14:27:58 +0000647 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000648 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000649 */
650 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000651 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000652 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
653 {
654 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000656 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657
Paul Bakker343a8702011-06-09 14:27:58 +0000658 if( POLARSSL_DECRYPT == operation )
659 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 ctx->key_length );
661
Paul Bakkerff61a782011-06-09 15:42:02 +0000662 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000663}
664
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200665int cipher_set_iv( cipher_context_t *ctx,
666 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200668 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200669
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000671 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200673 if( ctx->cipher_info->accepts_variable_iv_size )
674 actual_iv_size = iv_len;
675 else
676 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200677
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200678 memcpy( ctx->iv, iv, actual_iv_size );
679 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200680
681 return 0;
682}
683
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200684int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200685{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200686 if( NULL == ctx || NULL == ctx->cipher_info )
687 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
688
Paul Bakker8123e9d2011-01-06 15:37:30 +0000689 ctx->unprocessed_len = 0;
690
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200691 return 0;
692}
693
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200694#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200695int cipher_update_ad( cipher_context_t *ctx,
696 const unsigned char *ad, size_t ad_len )
697{
698 if( NULL == ctx || NULL == ctx->cipher_info )
699 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
700
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200701#if defined(POLARSSL_GCM_C)
702 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
703 {
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200704 return gcm_starts( ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200705 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200706 }
707#endif
708
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709 return 0;
710}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200711#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712
Paul Bakker23986e52011-04-24 08:57:21 +0000713int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
714 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715{
Paul Bakkerff61a782011-06-09 15:42:02 +0000716 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717
Paul Bakker68884e32013-01-07 18:20:04 +0100718 *olen = 0;
719
720 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000721 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000722 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000723 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000724
Paul Bakker5e0efa72013-09-08 23:04:04 +0200725 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
726 {
727 if( ilen != cipher_get_block_size( ctx ) )
728 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
729
730 *olen = ilen;
731
732 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
733 ctx->operation, input, output ) ) )
734 {
735 return ret;
736 }
737
738 return 0;
739 }
740
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200741#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200742 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200743 {
744 *olen = ilen;
745 return gcm_update( ctx->cipher_ctx, ilen, input, output );
746 }
747#endif
748
Paul Bakker68884e32013-01-07 18:20:04 +0100749 if( input == output &&
750 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
751 {
752 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
753 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200755#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200756 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200758 size_t copy_len = 0;
759
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 /*
761 * If there is not enough data for a full block, cache it.
762 */
763 if( ( ctx->operation == POLARSSL_DECRYPT &&
764 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
765 ( ctx->operation == POLARSSL_ENCRYPT &&
766 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
767 {
768 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
769 ilen );
770
771 ctx->unprocessed_len += ilen;
772 return 0;
773 }
774
775 /*
776 * Process cached data first
777 */
778 if( ctx->unprocessed_len != 0 )
779 {
780 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
781
782 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
783 copy_len );
784
Paul Bakkerff61a782011-06-09 15:42:02 +0000785 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000787 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000788 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000789 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790 }
791
792 *olen += cipher_get_block_size( ctx );
793 output += cipher_get_block_size( ctx );
794 ctx->unprocessed_len = 0;
795
796 input += copy_len;
797 ilen -= copy_len;
798 }
799
800 /*
801 * Cache final, incomplete block
802 */
803 if( 0 != ilen )
804 {
805 copy_len = ilen % cipher_get_block_size( ctx );
806 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
807 copy_len = cipher_get_block_size(ctx);
808
809 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
810 copy_len );
811
812 ctx->unprocessed_len += copy_len;
813 ilen -= copy_len;
814 }
815
816 /*
817 * Process remaining full blocks
818 */
819 if( ilen )
820 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000821 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
822 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000823 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000824 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000825 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200826
Paul Bakker8123e9d2011-01-06 15:37:30 +0000827 *olen += ilen;
828 }
829
830 return 0;
831 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200832#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000833
Paul Bakker68884e32013-01-07 18:20:04 +0100834#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000835 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000836 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000837 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000838 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000839 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000840 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000841 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000842 }
843
844 *olen = ilen;
845
846 return 0;
847 }
Paul Bakker68884e32013-01-07 18:20:04 +0100848#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000849
Paul Bakker68884e32013-01-07 18:20:04 +0100850#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000851 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
852 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000853 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000854 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000855 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000856 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000857 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000858 }
859
860 *olen = ilen;
861
862 return 0;
863 }
Paul Bakker68884e32013-01-07 18:20:04 +0100864#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000865
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200866#if defined(POLARSSL_CIPHER_MODE_STREAM)
867 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
868 {
869 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
870 ilen, input, output ) ) )
871 {
872 return ret;
873 }
874
875 *olen = ilen;
876
877 return 0;
878 }
879#endif
880
Paul Bakkerff61a782011-06-09 15:42:02 +0000881 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000882}
883
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200884#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200885#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200886/*
887 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
888 */
Paul Bakker23986e52011-04-24 08:57:21 +0000889static void add_pkcs_padding( unsigned char *output, size_t output_len,
890 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000891{
Paul Bakker23986e52011-04-24 08:57:21 +0000892 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000893 unsigned char i = 0;
894
895 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000896 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000897}
898
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200899static int get_pkcs_padding( unsigned char *input, size_t input_len,
900 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000901{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000902 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000903
Paul Bakkera885d682011-01-20 16:35:05 +0000904 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000905 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000906
907 padding_len = input[input_len - 1];
908
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200909 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000910 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000911
Paul Bakkera885d682011-01-20 16:35:05 +0000912 for( i = input_len - padding_len; i < input_len; i++ )
913 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000914 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000915
916 *data_len = input_len - padding_len;
917
918 return 0;
919}
Paul Bakker48e93c82013-08-14 12:21:18 +0200920#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000921
Paul Bakker48e93c82013-08-14 12:21:18 +0200922#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200923/*
924 * One and zeros padding: fill with 80 00 ... 00
925 */
926static void add_one_and_zeros_padding( unsigned char *output,
927 size_t output_len, size_t data_len )
928{
929 size_t padding_len = output_len - data_len;
930 unsigned char i = 0;
931
932 output[data_len] = 0x80;
933 for( i = 1; i < padding_len; i++ )
934 output[data_len + i] = 0x00;
935}
936
937static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
938 size_t *data_len )
939{
940 unsigned char *p = input + input_len - 1;
941
942 if( NULL == input || NULL == data_len )
943 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
944
945 while( *p == 0x00 && p > input )
946 --p;
947
948 if( *p != 0x80 )
949 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
950
951 *data_len = p - input;
952
953 return 0;
954}
Paul Bakker48e93c82013-08-14 12:21:18 +0200955#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200956
Paul Bakker48e93c82013-08-14 12:21:18 +0200957#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200958/*
959 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
960 */
961static void add_zeros_and_len_padding( unsigned char *output,
962 size_t output_len, size_t data_len )
963{
964 size_t padding_len = output_len - data_len;
965 unsigned char i = 0;
966
967 for( i = 1; i < padding_len; i++ )
968 output[data_len + i - 1] = 0x00;
969 output[output_len - 1] = (unsigned char) padding_len;
970}
971
972static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
973 size_t *data_len )
974{
975 unsigned int i, padding_len = 0;
976
977 if( NULL == input || NULL == data_len )
978 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
979
980 padding_len = input[input_len - 1];
981
982 if( padding_len > input_len || padding_len == 0 )
983 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
984
985 for( i = input_len - padding_len; i < input_len - 1; i++ )
986 if( input[i] != 0x00 )
987 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
988
989 *data_len = input_len - padding_len;
990
991 return 0;
992}
Paul Bakker48e93c82013-08-14 12:21:18 +0200993#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200994
Paul Bakker48e93c82013-08-14 12:21:18 +0200995#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200996/*
997 * Zero padding: fill with 00 ... 00
998 */
999static void add_zeros_padding( unsigned char *output,
1000 size_t output_len, size_t data_len )
1001{
1002 unsigned char i;
1003
1004 for( i = data_len; i < output_len; i++ )
1005 output[i] = 0x00;
1006}
1007
1008static int get_zeros_padding( unsigned char *input, size_t input_len,
1009 size_t *data_len )
1010{
1011 unsigned char *p = input + input_len - 1;
1012 if( NULL == input || NULL == data_len )
1013 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1014
1015 while( *p == 0x00 && p > input )
1016 --p;
1017
1018 *data_len = *p == 0x00 ? 0 : p - input + 1;
1019
1020 return 0;
1021}
Paul Bakker48e93c82013-08-14 12:21:18 +02001022#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001023
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001024/*
1025 * No padding: don't pad :)
1026 *
1027 * There is no add_padding function (check for NULL in cipher_finish)
1028 * but a trivial get_padding function
1029 */
1030static int get_no_padding( unsigned char *input, size_t input_len,
1031 size_t *data_len )
1032{
1033 if( NULL == input || NULL == data_len )
1034 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1035
1036 *data_len = input_len;
1037
1038 return 0;
1039}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001040#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001041
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +02001042int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001043 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001044{
1045 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +00001046 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001047
1048 *olen = 0;
1049
Paul Bakker6132d0a2012-07-04 17:10:40 +00001050 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +00001051 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +02001052 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001053 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +00001054 {
1055 return 0;
1056 }
1057
Paul Bakker5e0efa72013-09-08 23:04:04 +02001058 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
1059 {
1060 if( ctx->unprocessed_len != 0 )
1061 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1062
1063 return 0;
1064 }
1065
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001066#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001067 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
1068 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001069 int ret = 0;
1070
Paul Bakker8123e9d2011-01-06 15:37:30 +00001071 if( POLARSSL_ENCRYPT == ctx->operation )
1072 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001073 /* check for 'no padding' mode */
1074 if( NULL == ctx->add_padding )
1075 {
1076 if( 0 != ctx->unprocessed_len )
1077 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1078
1079 return 0;
1080 }
1081
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001082 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +00001083 ctx->unprocessed_len );
1084 }
1085 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
1086 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001087 /*
1088 * For decrypt operations, expect a full block,
1089 * or an empty block if no padding
1090 */
1091 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
1092 return 0;
1093
Paul Bakkerff61a782011-06-09 15:42:02 +00001094 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001095 }
1096
1097 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001098 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
1099 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
1100 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001101 {
Paul Bakkerff61a782011-06-09 15:42:02 +00001102 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001103 }
1104
1105 /* Set output size for decryption */
1106 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001107 return ctx->get_padding( output, cipher_get_block_size( ctx ),
1108 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001109
1110 /* Set output size for encryption */
1111 *olen = cipher_get_block_size( ctx );
1112 return 0;
1113 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001114#else
1115 ((void) output);
1116#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001117
Paul Bakkerff61a782011-06-09 15:42:02 +00001118 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001119}
1120
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001121#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001122int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
1123{
1124 if( NULL == ctx ||
1125 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
1126 {
1127 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1128 }
1129
Paul Bakker1a45d912013-08-14 12:04:26 +02001130 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001131 {
Paul Bakker48e93c82013-08-14 12:21:18 +02001132#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +02001133 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001134 ctx->add_padding = add_pkcs_padding;
1135 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001136 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001137#endif
1138#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +02001139 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001140 ctx->add_padding = add_one_and_zeros_padding;
1141 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001142 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001143#endif
1144#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +02001145 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001146 ctx->add_padding = add_zeros_and_len_padding;
1147 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001148 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001149#endif
1150#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +02001151 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001152 ctx->add_padding = add_zeros_padding;
1153 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001154 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001155#endif
Paul Bakker1a45d912013-08-14 12:04:26 +02001156 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001157 ctx->add_padding = NULL;
1158 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001159 break;
1160
1161 default:
Paul Bakker48e93c82013-08-14 12:21:18 +02001162 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001163 }
1164
Paul Bakker1a45d912013-08-14 12:04:26 +02001165 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001166}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001167#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001168
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001169#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001170int cipher_write_tag( cipher_context_t *ctx,
1171 unsigned char *tag, size_t tag_len )
1172{
1173 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
1174 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1175
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001176 if( POLARSSL_ENCRYPT != ctx->operation )
1177 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1178
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001179#if defined(POLARSSL_GCM_C)
1180 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
1181 return gcm_finish( ctx->cipher_ctx, tag, tag_len );
1182#endif
1183
1184 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001185}
1186
1187int cipher_check_tag( cipher_context_t *ctx,
1188 const unsigned char *tag, size_t tag_len )
1189{
1190 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001191
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001192 if( NULL == ctx || NULL == ctx->cipher_info ||
1193 POLARSSL_DECRYPT != ctx->operation )
1194 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001195 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001196 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001197
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001198#if defined(POLARSSL_GCM_C)
1199 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
1200 {
1201 unsigned char check_tag[16];
1202 size_t i;
1203 int diff;
1204
1205 if( tag_len > sizeof( check_tag ) )
1206 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1207
1208 if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
1209 return( ret );
1210
1211 /* Check the tag in "constant-time" */
1212 for( diff = 0, i = 0; i < tag_len; i++ )
1213 diff |= tag[i] ^ check_tag[i];
1214
1215 if( diff != 0 )
1216 return( POLARSSL_ERR_GCM_AUTH_FAILED );
1217
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001218 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001219 }
1220#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001221
1222 return( 0 );
1223}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001224#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001225
Paul Bakker8123e9d2011-01-06 15:37:30 +00001226#if defined(POLARSSL_SELF_TEST)
1227
1228#include <stdio.h>
1229
1230#define ASSERT(x) if (!(x)) { \
1231 printf( "failed with %i at %s\n", value, (#x) ); \
1232 return( 1 ); \
1233}
1234/*
1235 * Checkup routine
1236 */
1237
1238int cipher_self_test( int verbose )
1239{
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001240 ((void) verbose);
1241
Paul Bakker8123e9d2011-01-06 15:37:30 +00001242 return( 0 );
1243}
1244
1245#endif
1246
1247#endif