blob: ee2a1cdf22ee6c68dae23252d967904ecf800659 [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
641 ctx->key_length = key_length;
642 ctx->operation = operation;
643
Paul Bakker343a8702011-06-09 14:27:58 +0000644 /*
Paul Bakker6132d0a2012-07-04 17:10:40 +0000645 * For CFB and CTR mode always use the encryption key schedule
Paul Bakker343a8702011-06-09 14:27:58 +0000646 */
647 if( POLARSSL_ENCRYPT == operation ||
Paul Bakker6132d0a2012-07-04 17:10:40 +0000648 POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakker343a8702011-06-09 14:27:58 +0000649 POLARSSL_MODE_CTR == ctx->cipher_info->mode )
650 {
651 return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652 ctx->key_length );
Paul Bakker343a8702011-06-09 14:27:58 +0000653 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654
Paul Bakker343a8702011-06-09 14:27:58 +0000655 if( POLARSSL_DECRYPT == operation )
656 return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657 ctx->key_length );
658
Paul Bakkerff61a782011-06-09 15:42:02 +0000659 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660}
661
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200662int cipher_set_iv( cipher_context_t *ctx,
663 const unsigned char *iv, size_t iv_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664{
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200665 size_t actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200666
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667 if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
Paul Bakkerff61a782011-06-09 15:42:02 +0000668 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200670 if( ctx->cipher_info->accepts_variable_iv_size )
671 actual_iv_size = iv_len;
672 else
673 actual_iv_size = ctx->cipher_info->iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200674
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200675 memcpy( ctx->iv, iv, actual_iv_size );
676 ctx->iv_size = actual_iv_size;
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200677
678 return 0;
679}
680
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200681int cipher_reset( cipher_context_t *ctx )
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200682{
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200683 if( NULL == ctx || NULL == ctx->cipher_info )
684 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
685
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686 ctx->unprocessed_len = 0;
687
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200688 return 0;
689}
690
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200691#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200692int cipher_update_ad( cipher_context_t *ctx,
693 const unsigned char *ad, size_t ad_len )
694{
695 if( NULL == ctx || NULL == ctx->cipher_info )
696 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
697
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200698#if defined(POLARSSL_GCM_C)
699 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
700 {
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200701 return gcm_starts( ctx->cipher_ctx, ctx->operation,
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200702 ctx->iv, ctx->iv_size, ad, ad_len );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200703 }
704#endif
705
Paul Bakker8123e9d2011-01-06 15:37:30 +0000706 return 0;
707}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +0200708#endif /* POLARSSL_CIPHER_MODE_AEAD */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709
Paul Bakker23986e52011-04-24 08:57:21 +0000710int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
711 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712{
Paul Bakkerff61a782011-06-09 15:42:02 +0000713 int ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000714
Paul Bakker68884e32013-01-07 18:20:04 +0100715 *olen = 0;
716
717 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkera885d682011-01-20 16:35:05 +0000718 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000719 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakkera885d682011-01-20 16:35:05 +0000720 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000721
Paul Bakker5e0efa72013-09-08 23:04:04 +0200722 if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
723 {
724 if( ilen != cipher_get_block_size( ctx ) )
725 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
726
727 *olen = ilen;
728
729 if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
730 ctx->operation, input, output ) ) )
731 {
732 return ret;
733 }
734
735 return 0;
736 }
737
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200738#if defined(POLARSSL_GCM_C)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200739 if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200740 {
741 *olen = ilen;
742 return gcm_update( ctx->cipher_ctx, ilen, input, output );
743 }
744#endif
745
Paul Bakker68884e32013-01-07 18:20:04 +0100746 if( input == output &&
747 ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
748 {
749 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
750 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200752#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +0200753 if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200755 size_t copy_len = 0;
756
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757 /*
758 * If there is not enough data for a full block, cache it.
759 */
760 if( ( ctx->operation == POLARSSL_DECRYPT &&
761 ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
762 ( ctx->operation == POLARSSL_ENCRYPT &&
763 ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
764 {
765 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
766 ilen );
767
768 ctx->unprocessed_len += ilen;
769 return 0;
770 }
771
772 /*
773 * Process cached data first
774 */
775 if( ctx->unprocessed_len != 0 )
776 {
777 copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
778
779 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
780 copy_len );
781
Paul Bakkerff61a782011-06-09 15:42:02 +0000782 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000783 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000784 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000786 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000787 }
788
789 *olen += cipher_get_block_size( ctx );
790 output += cipher_get_block_size( ctx );
791 ctx->unprocessed_len = 0;
792
793 input += copy_len;
794 ilen -= copy_len;
795 }
796
797 /*
798 * Cache final, incomplete block
799 */
800 if( 0 != ilen )
801 {
802 copy_len = ilen % cipher_get_block_size( ctx );
803 if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
804 copy_len = cipher_get_block_size(ctx);
805
806 memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
807 copy_len );
808
809 ctx->unprocessed_len += copy_len;
810 ilen -= copy_len;
811 }
812
813 /*
814 * Process remaining full blocks
815 */
816 if( ilen )
817 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000818 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
819 ctx->operation, ilen, ctx->iv, input, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000820 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000821 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000822 }
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200823
Paul Bakker8123e9d2011-01-06 15:37:30 +0000824 *olen += ilen;
825 }
826
827 return 0;
828 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200829#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000830
Paul Bakker68884e32013-01-07 18:20:04 +0100831#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000832 if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
Paul Bakker343a8702011-06-09 14:27:58 +0000833 {
Paul Bakker6132d0a2012-07-04 17:10:40 +0000834 if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000835 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000836 input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000837 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000838 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000839 }
840
841 *olen = ilen;
842
843 return 0;
844 }
Paul Bakker68884e32013-01-07 18:20:04 +0100845#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000846
Paul Bakker68884e32013-01-07 18:20:04 +0100847#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000848 if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
849 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000850 if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
Paul Bakker343a8702011-06-09 14:27:58 +0000851 ilen, &ctx->unprocessed_len, ctx->iv,
Paul Bakkerff61a782011-06-09 15:42:02 +0000852 ctx->unprocessed_data, input, output ) ) )
Paul Bakker343a8702011-06-09 14:27:58 +0000853 {
Paul Bakkerff61a782011-06-09 15:42:02 +0000854 return ret;
Paul Bakker343a8702011-06-09 14:27:58 +0000855 }
856
857 *olen = ilen;
858
859 return 0;
860 }
Paul Bakker68884e32013-01-07 18:20:04 +0100861#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000862
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200863#if defined(POLARSSL_CIPHER_MODE_STREAM)
864 if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
865 {
866 if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
867 ilen, input, output ) ) )
868 {
869 return ret;
870 }
871
872 *olen = ilen;
873
874 return 0;
875 }
876#endif
877
Paul Bakkerff61a782011-06-09 15:42:02 +0000878 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000879}
880
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200881#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Paul Bakker48e93c82013-08-14 12:21:18 +0200882#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200883/*
884 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
885 */
Paul Bakker23986e52011-04-24 08:57:21 +0000886static void add_pkcs_padding( unsigned char *output, size_t output_len,
887 size_t data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000888{
Paul Bakker23986e52011-04-24 08:57:21 +0000889 size_t padding_len = output_len - data_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000890 unsigned char i = 0;
891
892 for( i = 0; i < padding_len; i++ )
Paul Bakker23986e52011-04-24 08:57:21 +0000893 output[data_len + i] = (unsigned char) padding_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000894}
895
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +0200896static int get_pkcs_padding( unsigned char *input, size_t input_len,
897 size_t *data_len )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000898{
Paul Bakkerec1b9842012-01-14 18:24:43 +0000899 unsigned int i, padding_len = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000900
Paul Bakkera885d682011-01-20 16:35:05 +0000901 if( NULL == input || NULL == data_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000902 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000903
904 padding_len = input[input_len - 1];
905
Manuel Pégourié-Gonnardb7d24bc2013-07-26 10:58:48 +0200906 if( padding_len > input_len || padding_len == 0 )
Paul Bakkerff61a782011-06-09 15:42:02 +0000907 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000908
Paul Bakkera885d682011-01-20 16:35:05 +0000909 for( i = input_len - padding_len; i < input_len; i++ )
910 if( input[i] != padding_len )
Paul Bakkerff61a782011-06-09 15:42:02 +0000911 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000912
913 *data_len = input_len - padding_len;
914
915 return 0;
916}
Paul Bakker48e93c82013-08-14 12:21:18 +0200917#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000918
Paul Bakker48e93c82013-08-14 12:21:18 +0200919#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200920/*
921 * One and zeros padding: fill with 80 00 ... 00
922 */
923static void add_one_and_zeros_padding( unsigned char *output,
924 size_t output_len, size_t data_len )
925{
926 size_t padding_len = output_len - data_len;
927 unsigned char i = 0;
928
929 output[data_len] = 0x80;
930 for( i = 1; i < padding_len; i++ )
931 output[data_len + i] = 0x00;
932}
933
934static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
935 size_t *data_len )
936{
937 unsigned char *p = input + input_len - 1;
938
939 if( NULL == input || NULL == data_len )
940 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
941
942 while( *p == 0x00 && p > input )
943 --p;
944
945 if( *p != 0x80 )
946 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
947
948 *data_len = p - input;
949
950 return 0;
951}
Paul Bakker48e93c82013-08-14 12:21:18 +0200952#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +0200953
Paul Bakker48e93c82013-08-14 12:21:18 +0200954#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200955/*
956 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
957 */
958static void add_zeros_and_len_padding( unsigned char *output,
959 size_t output_len, size_t data_len )
960{
961 size_t padding_len = output_len - data_len;
962 unsigned char i = 0;
963
964 for( i = 1; i < padding_len; i++ )
965 output[data_len + i - 1] = 0x00;
966 output[output_len - 1] = (unsigned char) padding_len;
967}
968
969static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
970 size_t *data_len )
971{
972 unsigned int i, padding_len = 0;
973
974 if( NULL == input || NULL == data_len )
975 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
976
977 padding_len = input[input_len - 1];
978
979 if( padding_len > input_len || padding_len == 0 )
980 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
981
982 for( i = input_len - padding_len; i < input_len - 1; i++ )
983 if( input[i] != 0x00 )
984 return POLARSSL_ERR_CIPHER_INVALID_PADDING;
985
986 *data_len = input_len - padding_len;
987
988 return 0;
989}
Paul Bakker48e93c82013-08-14 12:21:18 +0200990#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +0200991
Paul Bakker48e93c82013-08-14 12:21:18 +0200992#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +0200993/*
994 * Zero padding: fill with 00 ... 00
995 */
996static void add_zeros_padding( unsigned char *output,
997 size_t output_len, size_t data_len )
998{
999 unsigned char i;
1000
1001 for( i = data_len; i < output_len; i++ )
1002 output[i] = 0x00;
1003}
1004
1005static int get_zeros_padding( unsigned char *input, size_t input_len,
1006 size_t *data_len )
1007{
1008 unsigned char *p = input + input_len - 1;
1009 if( NULL == input || NULL == data_len )
1010 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1011
1012 while( *p == 0x00 && p > input )
1013 --p;
1014
1015 *data_len = *p == 0x00 ? 0 : p - input + 1;
1016
1017 return 0;
1018}
Paul Bakker48e93c82013-08-14 12:21:18 +02001019#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001020
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001021/*
1022 * No padding: don't pad :)
1023 *
1024 * There is no add_padding function (check for NULL in cipher_finish)
1025 * but a trivial get_padding function
1026 */
1027static int get_no_padding( unsigned char *input, size_t input_len,
1028 size_t *data_len )
1029{
1030 if( NULL == input || NULL == data_len )
1031 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1032
1033 *data_len = input_len;
1034
1035 return 0;
1036}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001037#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001038
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +02001039int cipher_finish( cipher_context_t *ctx,
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001040 unsigned char *output, size_t *olen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001041{
Paul Bakker8123e9d2011-01-06 15:37:30 +00001042 if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
Paul Bakkerff61a782011-06-09 15:42:02 +00001043 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001044
1045 *olen = 0;
1046
Paul Bakker6132d0a2012-07-04 17:10:40 +00001047 if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
Paul Bakkerfab5c822012-02-06 16:45:10 +00001048 POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb8bd5932013-09-05 13:38:15 +02001049 POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001050 POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
Paul Bakker343a8702011-06-09 14:27:58 +00001051 {
1052 return 0;
1053 }
1054
Paul Bakker5e0efa72013-09-08 23:04:04 +02001055 if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
1056 {
1057 if( ctx->unprocessed_len != 0 )
1058 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1059
1060 return 0;
1061 }
1062
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001063#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001064 if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
1065 {
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001066 int ret = 0;
1067
Paul Bakker8123e9d2011-01-06 15:37:30 +00001068 if( POLARSSL_ENCRYPT == ctx->operation )
1069 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001070 /* check for 'no padding' mode */
1071 if( NULL == ctx->add_padding )
1072 {
1073 if( 0 != ctx->unprocessed_len )
1074 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
1075
1076 return 0;
1077 }
1078
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001079 ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
Paul Bakker8123e9d2011-01-06 15:37:30 +00001080 ctx->unprocessed_len );
1081 }
1082 else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
1083 {
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001084 /*
1085 * For decrypt operations, expect a full block,
1086 * or an empty block if no padding
1087 */
1088 if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
1089 return 0;
1090
Paul Bakkerff61a782011-06-09 15:42:02 +00001091 return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001092 }
1093
1094 /* cipher block */
Paul Bakkerff61a782011-06-09 15:42:02 +00001095 if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
1096 ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
1097 ctx->unprocessed_data, output ) ) )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001098 {
Paul Bakkerff61a782011-06-09 15:42:02 +00001099 return ret;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100 }
1101
1102 /* Set output size for decryption */
1103 if( POLARSSL_DECRYPT == ctx->operation )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001104 return ctx->get_padding( output, cipher_get_block_size( ctx ),
1105 olen );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106
1107 /* Set output size for encryption */
1108 *olen = cipher_get_block_size( ctx );
1109 return 0;
1110 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001111#else
1112 ((void) output);
1113#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001114
Paul Bakkerff61a782011-06-09 15:42:02 +00001115 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker8123e9d2011-01-06 15:37:30 +00001116}
1117
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001118#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001119int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
1120{
1121 if( NULL == ctx ||
1122 POLARSSL_MODE_CBC != ctx->cipher_info->mode )
1123 {
1124 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1125 }
1126
Paul Bakker1a45d912013-08-14 12:04:26 +02001127 switch( mode )
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001128 {
Paul Bakker48e93c82013-08-14 12:21:18 +02001129#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
Paul Bakker1a45d912013-08-14 12:04:26 +02001130 case POLARSSL_PADDING_PKCS7:
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001131 ctx->add_padding = add_pkcs_padding;
1132 ctx->get_padding = get_pkcs_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001133 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001134#endif
1135#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +02001136 case POLARSSL_PADDING_ONE_AND_ZEROS:
Manuel Pégourié-Gonnard679f9e92013-07-26 12:46:02 +02001137 ctx->add_padding = add_one_and_zeros_padding;
1138 ctx->get_padding = get_one_and_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001139 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001140#endif
1141#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
Paul Bakker1a45d912013-08-14 12:04:26 +02001142 case POLARSSL_PADDING_ZEROS_AND_LEN:
Manuel Pégourié-Gonnard8d4291b2013-07-26 14:55:18 +02001143 ctx->add_padding = add_zeros_and_len_padding;
1144 ctx->get_padding = get_zeros_and_len_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001145 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001146#endif
1147#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
Paul Bakker1a45d912013-08-14 12:04:26 +02001148 case POLARSSL_PADDING_ZEROS:
Manuel Pégourié-Gonnard0e7d2c02013-07-26 16:05:14 +02001149 ctx->add_padding = add_zeros_padding;
1150 ctx->get_padding = get_zeros_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001151 break;
Paul Bakker48e93c82013-08-14 12:21:18 +02001152#endif
Paul Bakker1a45d912013-08-14 12:04:26 +02001153 case POLARSSL_PADDING_NONE:
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001154 ctx->add_padding = NULL;
1155 ctx->get_padding = get_no_padding;
Paul Bakker1a45d912013-08-14 12:04:26 +02001156 break;
1157
1158 default:
Paul Bakker48e93c82013-08-14 12:21:18 +02001159 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +02001160 }
1161
Paul Bakker1a45d912013-08-14 12:04:26 +02001162 return 0;
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001163}
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001164#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnardac56a1a2013-07-25 12:31:10 +02001165
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001166#if defined(POLARSSL_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001167int cipher_write_tag( cipher_context_t *ctx,
1168 unsigned char *tag, size_t tag_len )
1169{
1170 if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
1171 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1172
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001173 if( POLARSSL_ENCRYPT != ctx->operation )
1174 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1175
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001176#if defined(POLARSSL_GCM_C)
1177 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
1178 return gcm_finish( ctx->cipher_ctx, tag, tag_len );
1179#endif
1180
1181 return 0;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001182}
1183
1184int cipher_check_tag( cipher_context_t *ctx,
1185 const unsigned char *tag, size_t tag_len )
1186{
1187 int ret;
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001188
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001189 if( NULL == ctx || NULL == ctx->cipher_info ||
1190 POLARSSL_DECRYPT != ctx->operation )
1191 {
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001192 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001193 }
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001194
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001195#if defined(POLARSSL_GCM_C)
1196 if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
1197 {
1198 unsigned char check_tag[16];
1199 size_t i;
1200 int diff;
1201
1202 if( tag_len > sizeof( check_tag ) )
1203 return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
1204
1205 if( 0 != ( ret = gcm_finish( ctx->cipher_ctx, check_tag, tag_len ) ) )
1206 return( ret );
1207
1208 /* Check the tag in "constant-time" */
1209 for( diff = 0, i = 0; i < tag_len; i++ )
1210 diff |= tag[i] ^ check_tag[i];
1211
1212 if( diff != 0 )
1213 return( POLARSSL_ERR_GCM_AUTH_FAILED );
1214
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001215 return( 0 );
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001216 }
1217#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001218
1219 return( 0 );
1220}
Manuel Pégourié-Gonnard43a47802013-09-03 16:35:53 +02001221#endif /* POLARSSL_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +02001222
Paul Bakker8123e9d2011-01-06 15:37:30 +00001223#if defined(POLARSSL_SELF_TEST)
1224
1225#include <stdio.h>
1226
1227#define ASSERT(x) if (!(x)) { \
1228 printf( "failed with %i at %s\n", value, (#x) ); \
1229 return( 1 ); \
1230}
1231/*
1232 * Checkup routine
1233 */
1234
1235int cipher_self_test( int verbose )
1236{
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001237 ((void) verbose);
1238
Paul Bakker8123e9d2011-01-06 15:37:30 +00001239 return( 0 );
1240}
1241
1242#endif
1243
1244#endif