blob: be44371096a7c55f29c131958e1313bdc7393e61 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Paul Bakker20281562011-11-11 10:34:04 +00004 * \brief Generic cipher wrapper for PolarSSL
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01008 * Copyright (C) 2006-2014, 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker8123e9d2011-01-06 15:37:30 +000031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000035
36#if defined(POLARSSL_CIPHER_C)
37
38#include "polarssl/cipher_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039
40#if defined(POLARSSL_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000041#include "polarssl/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020044#if defined(POLARSSL_ARC4_C)
45#include "polarssl/arc4.h"
46#endif
47
Paul Bakkerf6543712012-03-05 14:01:29 +000048#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000050#endif
51
52#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000053#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000054#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000055
Paul Bakker6132d0a2012-07-04 17:10:40 +000056#if defined(POLARSSL_BLOWFISH_C)
57#include "polarssl/blowfish.h"
58#endif
59
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020060#if defined(POLARSSL_GCM_C)
61#include "polarssl/gcm.h"
62#endif
63
Paul Bakker7dc4c442014-02-01 22:50:26 +010064#if defined(POLARSSL_PLATFORM_C)
65#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020066#else
67#define polarssl_malloc malloc
68#define polarssl_free free
69#endif
70
Paul Bakker8123e9d2011-01-06 15:37:30 +000071#include <stdlib.h>
72
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020073#if defined(POLARSSL_GCM_C)
74/* shared by all GCM ciphers */
75static void *gcm_ctx_alloc( void )
76{
77 return polarssl_malloc( sizeof( gcm_context ) );
78}
79
80static void gcm_ctx_free( void *ctx )
81{
82 gcm_free( ctx );
83 polarssl_free( ctx );
84}
Paul Bakker9af723c2014-05-01 13:03:14 +020085#endif /* POLARSSL_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020086
Paul Bakker8123e9d2011-01-06 15:37:30 +000087#if defined(POLARSSL_AES_C)
88
Paul Bakker5e0efa72013-09-08 23:04:04 +020089static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
90 const unsigned char *input, unsigned char *output )
91{
92 return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
93}
94
Paul Bakkerfae35f02013-03-13 10:33:51 +010095static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000096 unsigned char *iv, const unsigned char *input, unsigned char *output )
97{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020098#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +000099 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200100#else
101 ((void) ctx);
102 ((void) operation);
103 ((void) length);
104 ((void) iv);
105 ((void) input);
106 ((void) output);
107
108 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
109#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110}
111
Paul Bakkerfae35f02013-03-13 10:33:51 +0100112static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000113 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
114{
115#if defined(POLARSSL_CIPHER_MODE_CFB)
116 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
117#else
118 ((void) ctx);
119 ((void) operation);
120 ((void) length);
121 ((void) iv_off);
122 ((void) iv);
123 ((void) input);
124 ((void) output);
125
126 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200127#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000128}
129
Paul Bakkerfae35f02013-03-13 10:33:51 +0100130static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000131 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
132 const unsigned char *input, unsigned char *output )
133{
134#if defined(POLARSSL_CIPHER_MODE_CTR)
135 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
136 stream_block, input, output );
137#else
138 ((void) ctx);
139 ((void) length);
140 ((void) nc_off);
141 ((void) nonce_counter);
142 ((void) stream_block);
143 ((void) input);
144 ((void) output);
145
146 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200147#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000148}
149
Paul Bakkerfae35f02013-03-13 10:33:51 +0100150static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151{
152 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
153}
154
Paul Bakkerfae35f02013-03-13 10:33:51 +0100155static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156{
157 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
158}
159
160static void * aes_ctx_alloc( void )
161{
Paul Bakker6e339b52013-07-03 13:37:05 +0200162 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163}
164
165static void aes_ctx_free( void *ctx )
166{
Paul Bakker6e339b52013-07-03 13:37:05 +0200167 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168}
169
Paul Bakker343a8702011-06-09 14:27:58 +0000170const cipher_base_t aes_info = {
171 POLARSSL_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200172 aes_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000173 aes_crypt_cbc_wrap,
174 aes_crypt_cfb128_wrap,
175 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200176 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000177 aes_setkey_enc_wrap,
178 aes_setkey_dec_wrap,
179 aes_ctx_alloc,
180 aes_ctx_free
181};
182
Paul Bakker5e0efa72013-09-08 23:04:04 +0200183const cipher_info_t aes_128_ecb_info = {
184 POLARSSL_CIPHER_AES_128_ECB,
185 POLARSSL_MODE_ECB,
186 128,
187 "AES-128-ECB",
188 16,
189 0,
190 16,
191 &aes_info
192};
193
194const cipher_info_t aes_192_ecb_info = {
195 POLARSSL_CIPHER_AES_192_ECB,
196 POLARSSL_MODE_ECB,
197 192,
198 "AES-192-ECB",
199 16,
200 0,
201 16,
202 &aes_info
203};
204
205const cipher_info_t aes_256_ecb_info = {
206 POLARSSL_CIPHER_AES_256_ECB,
207 POLARSSL_MODE_ECB,
208 256,
209 "AES-256-ECB",
210 16,
211 0,
212 16,
213 &aes_info
214};
215
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200216#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000218 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000219 POLARSSL_MODE_CBC,
220 128,
221 "AES-128-CBC",
222 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200223 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000224 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000225 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226};
227
228const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000229 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000230 POLARSSL_MODE_CBC,
231 192,
232 "AES-192-CBC",
233 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200234 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000235 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000236 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237};
238
239const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000240 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000241 POLARSSL_MODE_CBC,
242 256,
243 "AES-256-CBC",
244 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200245 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000247 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000248};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200249#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000250
251#if defined(POLARSSL_CIPHER_MODE_CFB)
252const cipher_info_t aes_128_cfb128_info = {
253 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000254 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000255 128,
256 "AES-128-CFB128",
257 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200258 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000259 16,
260 &aes_info
261};
262
263const cipher_info_t aes_192_cfb128_info = {
264 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000265 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000266 192,
267 "AES-192-CFB128",
268 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200269 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000270 16,
271 &aes_info
272};
273
274const cipher_info_t aes_256_cfb128_info = {
275 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000276 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000277 256,
278 "AES-256-CFB128",
279 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200280 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000281 16,
282 &aes_info
283};
284#endif /* POLARSSL_CIPHER_MODE_CFB */
285
286#if defined(POLARSSL_CIPHER_MODE_CTR)
287const cipher_info_t aes_128_ctr_info = {
288 POLARSSL_CIPHER_AES_128_CTR,
289 POLARSSL_MODE_CTR,
290 128,
291 "AES-128-CTR",
292 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200293 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000294 16,
295 &aes_info
296};
297
298const cipher_info_t aes_192_ctr_info = {
299 POLARSSL_CIPHER_AES_192_CTR,
300 POLARSSL_MODE_CTR,
301 192,
302 "AES-192-CTR",
303 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200304 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000305 16,
306 &aes_info
307};
308
309const cipher_info_t aes_256_ctr_info = {
310 POLARSSL_CIPHER_AES_256_CTR,
311 POLARSSL_MODE_CTR,
312 256,
313 "AES-256-CTR",
314 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200315 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000316 16,
317 &aes_info
318};
319#endif /* POLARSSL_CIPHER_MODE_CTR */
320
Paul Bakker68884e32013-01-07 18:20:04 +0100321#if defined(POLARSSL_GCM_C)
Paul Bakker43aff2a2013-09-09 00:10:27 +0200322static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200323{
Paul Bakker43aff2a2013-09-09 00:10:27 +0200324 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
325 key, key_length );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200326}
327
328const cipher_base_t gcm_aes_info = {
329 POLARSSL_CIPHER_ID_AES,
330 NULL,
331 NULL,
332 NULL,
333 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200334 NULL,
Paul Bakker43aff2a2013-09-09 00:10:27 +0200335 gcm_aes_setkey_wrap,
336 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200337 gcm_ctx_alloc,
338 gcm_ctx_free,
339};
340
Paul Bakker68884e32013-01-07 18:20:04 +0100341const cipher_info_t aes_128_gcm_info = {
342 POLARSSL_CIPHER_AES_128_GCM,
343 POLARSSL_MODE_GCM,
344 128,
345 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200346 12,
347 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100348 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200349 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100350};
351
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200352const cipher_info_t aes_192_gcm_info = {
353 POLARSSL_CIPHER_AES_192_GCM,
354 POLARSSL_MODE_GCM,
355 192,
356 "AES-192-GCM",
357 12,
358 1,
359 16,
360 &gcm_aes_info
361};
362
Paul Bakker68884e32013-01-07 18:20:04 +0100363const cipher_info_t aes_256_gcm_info = {
364 POLARSSL_CIPHER_AES_256_GCM,
365 POLARSSL_MODE_GCM,
366 256,
367 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200368 12,
369 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100370 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200371 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100372};
373#endif /* POLARSSL_GCM_C */
374
Paul Bakker9af723c2014-05-01 13:03:14 +0200375#endif /* POLARSSL_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
377#if defined(POLARSSL_CAMELLIA_C)
378
Paul Bakker5e0efa72013-09-08 23:04:04 +0200379static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
380 const unsigned char *input, unsigned char *output )
381{
382 return camellia_crypt_ecb( (camellia_context *) ctx, operation, input, output );
383}
384
Paul Bakkerfae35f02013-03-13 10:33:51 +0100385static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386 unsigned char *iv, const unsigned char *input, unsigned char *output )
387{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200388#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200390#else
391 ((void) ctx);
392 ((void) operation);
393 ((void) length);
394 ((void) iv);
395 ((void) input);
396 ((void) output);
397
398 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
399#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400}
401
Paul Bakkerfae35f02013-03-13 10:33:51 +0100402static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000403 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
404{
405#if defined(POLARSSL_CIPHER_MODE_CFB)
406 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
407#else
408 ((void) ctx);
409 ((void) operation);
410 ((void) length);
411 ((void) iv_off);
412 ((void) iv);
413 ((void) input);
414 ((void) output);
415
416 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200417#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000418}
419
Paul Bakkerfae35f02013-03-13 10:33:51 +0100420static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000421 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
422 const unsigned char *input, unsigned char *output )
423{
424#if defined(POLARSSL_CIPHER_MODE_CTR)
425 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
426 stream_block, input, output );
427#else
428 ((void) ctx);
429 ((void) length);
430 ((void) nc_off);
431 ((void) nonce_counter);
432 ((void) stream_block);
433 ((void) input);
434 ((void) output);
435
436 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200437#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000438}
439
Paul Bakkerfae35f02013-03-13 10:33:51 +0100440static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441{
442 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
443}
444
Paul Bakkerfae35f02013-03-13 10:33:51 +0100445static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000446{
447 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
448}
449
450static void * camellia_ctx_alloc( void )
451{
Paul Bakker6e339b52013-07-03 13:37:05 +0200452 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453}
454
455static void camellia_ctx_free( void *ctx )
456{
Paul Bakker6e339b52013-07-03 13:37:05 +0200457 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458}
459
Paul Bakker343a8702011-06-09 14:27:58 +0000460const cipher_base_t camellia_info = {
461 POLARSSL_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200462 camellia_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000463 camellia_crypt_cbc_wrap,
464 camellia_crypt_cfb128_wrap,
465 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200466 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000467 camellia_setkey_enc_wrap,
468 camellia_setkey_dec_wrap,
469 camellia_ctx_alloc,
470 camellia_ctx_free
471};
472
Paul Bakker5e0efa72013-09-08 23:04:04 +0200473const cipher_info_t camellia_128_ecb_info = {
474 POLARSSL_CIPHER_CAMELLIA_128_ECB,
475 POLARSSL_MODE_ECB,
476 128,
477 "CAMELLIA-128-ECB",
478 16,
479 0,
480 16,
481 &camellia_info
482};
483
484const cipher_info_t camellia_192_ecb_info = {
485 POLARSSL_CIPHER_CAMELLIA_192_ECB,
486 POLARSSL_MODE_ECB,
487 192,
488 "CAMELLIA-192-ECB",
489 16,
490 0,
491 16,
492 &camellia_info
493};
494
495const cipher_info_t camellia_256_ecb_info = {
496 POLARSSL_CIPHER_CAMELLIA_256_ECB,
497 POLARSSL_MODE_ECB,
498 256,
499 "CAMELLIA-256-ECB",
500 16,
501 0,
502 16,
503 &camellia_info
504};
505
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200506#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000508 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000509 POLARSSL_MODE_CBC,
510 128,
511 "CAMELLIA-128-CBC",
512 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200513 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000515 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000516};
517
518const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000519 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000520 POLARSSL_MODE_CBC,
521 192,
522 "CAMELLIA-192-CBC",
523 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200524 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000525 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000526 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527};
528
529const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000530 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000531 POLARSSL_MODE_CBC,
532 256,
533 "CAMELLIA-256-CBC",
534 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200535 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000536 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000537 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200539#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000540
541#if defined(POLARSSL_CIPHER_MODE_CFB)
542const cipher_info_t camellia_128_cfb128_info = {
543 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000544 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000545 128,
546 "CAMELLIA-128-CFB128",
547 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200548 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000549 16,
550 &camellia_info
551};
552
553const cipher_info_t camellia_192_cfb128_info = {
554 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000555 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000556 192,
557 "CAMELLIA-192-CFB128",
558 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200559 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000560 16,
561 &camellia_info
562};
563
564const cipher_info_t camellia_256_cfb128_info = {
565 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000566 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000567 256,
568 "CAMELLIA-256-CFB128",
569 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200570 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000571 16,
572 &camellia_info
573};
574#endif /* POLARSSL_CIPHER_MODE_CFB */
575
576#if defined(POLARSSL_CIPHER_MODE_CTR)
577const cipher_info_t camellia_128_ctr_info = {
578 POLARSSL_CIPHER_CAMELLIA_128_CTR,
579 POLARSSL_MODE_CTR,
580 128,
581 "CAMELLIA-128-CTR",
582 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200583 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000584 16,
585 &camellia_info
586};
587
588const cipher_info_t camellia_192_ctr_info = {
589 POLARSSL_CIPHER_CAMELLIA_192_CTR,
590 POLARSSL_MODE_CTR,
591 192,
592 "CAMELLIA-192-CTR",
593 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200594 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000595 16,
596 &camellia_info
597};
598
599const cipher_info_t camellia_256_ctr_info = {
600 POLARSSL_CIPHER_CAMELLIA_256_CTR,
601 POLARSSL_MODE_CTR,
602 256,
603 "CAMELLIA-256-CTR",
604 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200605 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000606 16,
607 &camellia_info
608};
609#endif /* POLARSSL_CIPHER_MODE_CTR */
610
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200611#if defined(POLARSSL_GCM_C)
612static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
613{
614 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
615 key, key_length );
616}
617
618const cipher_base_t gcm_camellia_info = {
619 POLARSSL_CIPHER_ID_CAMELLIA,
620 NULL,
621 NULL,
622 NULL,
623 NULL,
624 NULL,
625 gcm_camellia_setkey_wrap,
626 gcm_camellia_setkey_wrap,
627 gcm_ctx_alloc,
628 gcm_ctx_free,
629};
630
631const cipher_info_t camellia_128_gcm_info = {
632 POLARSSL_CIPHER_CAMELLIA_128_GCM,
633 POLARSSL_MODE_GCM,
634 128,
635 "CAMELLIA-128-GCM",
636 12,
637 1,
638 16,
639 &gcm_camellia_info
640};
641
642const cipher_info_t camellia_192_gcm_info = {
643 POLARSSL_CIPHER_CAMELLIA_192_GCM,
644 POLARSSL_MODE_GCM,
645 192,
646 "CAMELLIA-192-GCM",
647 12,
648 1,
649 16,
650 &gcm_camellia_info
651};
652
653const cipher_info_t camellia_256_gcm_info = {
654 POLARSSL_CIPHER_CAMELLIA_256_GCM,
655 POLARSSL_MODE_GCM,
656 256,
657 "CAMELLIA-256-GCM",
658 12,
659 1,
660 16,
661 &gcm_camellia_info
662};
663#endif /* POLARSSL_GCM_C */
664
665#endif /* POLARSSL_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666
667#if defined(POLARSSL_DES_C)
668
Paul Bakker5e0efa72013-09-08 23:04:04 +0200669static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
670 const unsigned char *input, unsigned char *output )
671{
672 ((void) operation);
673 return des_crypt_ecb( (des_context *) ctx, input, output );
674}
675
676static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
677 const unsigned char *input, unsigned char *output )
678{
679 ((void) operation);
680 return des3_crypt_ecb( (des3_context *) ctx, input, output );
681}
682
Paul Bakkerfae35f02013-03-13 10:33:51 +0100683static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000684 unsigned char *iv, const unsigned char *input, unsigned char *output )
685{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200686#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200688#else
689 ((void) ctx);
690 ((void) operation);
691 ((void) length);
692 ((void) iv);
693 ((void) input);
694 ((void) output);
695
696 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
697#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698}
699
Paul Bakkerfae35f02013-03-13 10:33:51 +0100700static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701 unsigned char *iv, const unsigned char *input, unsigned char *output )
702{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200703#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000704 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200705#else
706 ((void) ctx);
707 ((void) operation);
708 ((void) length);
709 ((void) iv);
710 ((void) input);
711 ((void) output);
712
713 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
714#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715}
716
Paul Bakkerfae35f02013-03-13 10:33:51 +0100717static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000718 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
719{
720 ((void) ctx);
721 ((void) operation);
722 ((void) length);
723 ((void) iv_off);
724 ((void) iv);
725 ((void) input);
726 ((void) output);
727
728 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
729}
730
Paul Bakkerfae35f02013-03-13 10:33:51 +0100731static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000732 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
733 const unsigned char *input, unsigned char *output )
734{
735 ((void) ctx);
736 ((void) length);
737 ((void) nc_off);
738 ((void) nonce_counter);
739 ((void) stream_block);
740 ((void) input);
741 ((void) output);
742
743 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
744}
745
Paul Bakkerfae35f02013-03-13 10:33:51 +0100746static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000748 ((void) key_length);
749
Paul Bakker8123e9d2011-01-06 15:37:30 +0000750 return des_setkey_dec( (des_context *) ctx, key );
751}
752
Paul Bakkerfae35f02013-03-13 10:33:51 +0100753static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000755 ((void) key_length);
756
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757 return des_setkey_enc( (des_context *) ctx, key );
758}
759
Paul Bakkerfae35f02013-03-13 10:33:51 +0100760static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000761{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000762 ((void) key_length);
763
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764 return des3_set2key_dec( (des3_context *) ctx, key );
765}
766
Paul Bakkerfae35f02013-03-13 10:33:51 +0100767static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000769 ((void) key_length);
770
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771 return des3_set2key_enc( (des3_context *) ctx, key );
772}
773
Paul Bakkerfae35f02013-03-13 10:33:51 +0100774static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000776 ((void) key_length);
777
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778 return des3_set3key_dec( (des3_context *) ctx, key );
779}
780
Paul Bakkerfae35f02013-03-13 10:33:51 +0100781static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000783 ((void) key_length);
784
Paul Bakker8123e9d2011-01-06 15:37:30 +0000785 return des3_set3key_enc( (des3_context *) ctx, key );
786}
787
788static void * des_ctx_alloc( void )
789{
Paul Bakker6e339b52013-07-03 13:37:05 +0200790 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000791}
792
793static void * des3_ctx_alloc( void )
794{
Paul Bakker6e339b52013-07-03 13:37:05 +0200795 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796}
797
798static void des_ctx_free( void *ctx )
799{
Paul Bakker6e339b52013-07-03 13:37:05 +0200800 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000801}
802
Paul Bakker343a8702011-06-09 14:27:58 +0000803const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000804 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200805 des_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000806 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000807 des_crypt_cfb128_wrap,
808 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200809 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000810 des_setkey_enc_wrap,
811 des_setkey_dec_wrap,
812 des_ctx_alloc,
813 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000814};
815
Paul Bakker5e0efa72013-09-08 23:04:04 +0200816const cipher_info_t des_ecb_info = {
817 POLARSSL_CIPHER_DES_ECB,
818 POLARSSL_MODE_ECB,
819 POLARSSL_KEY_LENGTH_DES,
820 "DES-ECB",
821 8,
822 0,
823 8,
824 &des_info
825};
826
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200827#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000828const cipher_info_t des_cbc_info = {
829 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000830 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000831 POLARSSL_KEY_LENGTH_DES,
832 "DES-CBC",
833 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200834 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000835 8,
836 &des_info
837};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200838#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000839
840const cipher_base_t des_ede_info = {
841 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200842 des3_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000843 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000844 des_crypt_cfb128_wrap,
845 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200846 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000847 des3_set2key_enc_wrap,
848 des3_set2key_dec_wrap,
849 des3_ctx_alloc,
850 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851};
852
Paul Bakker5e0efa72013-09-08 23:04:04 +0200853const cipher_info_t des_ede_ecb_info = {
854 POLARSSL_CIPHER_DES_EDE_ECB,
855 POLARSSL_MODE_ECB,
856 POLARSSL_KEY_LENGTH_DES_EDE,
857 "DES-EDE-ECB",
858 8,
859 0,
860 8,
861 &des_ede_info
862};
863
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200864#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000865const cipher_info_t des_ede_cbc_info = {
866 POLARSSL_CIPHER_DES_EDE_CBC,
867 POLARSSL_MODE_CBC,
868 POLARSSL_KEY_LENGTH_DES_EDE,
869 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200870 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200871 0,
Paul Bakker0e342352013-06-24 19:33:02 +0200872 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000873 &des_ede_info
874};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200875#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000876
877const cipher_base_t des_ede3_info = {
878 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200879 des3_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000880 des3_crypt_cbc_wrap,
881 des_crypt_cfb128_wrap,
882 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200883 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000884 des3_set3key_enc_wrap,
885 des3_set3key_dec_wrap,
886 des3_ctx_alloc,
887 des_ctx_free
888};
889
Paul Bakker5e0efa72013-09-08 23:04:04 +0200890const cipher_info_t des_ede3_ecb_info = {
891 POLARSSL_CIPHER_DES_EDE3_ECB,
892 POLARSSL_MODE_ECB,
893 POLARSSL_KEY_LENGTH_DES_EDE3,
894 "DES-EDE3-ECB",
895 8,
896 0,
897 8,
898 &des_ede3_info
899};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200900#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000901const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000902 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000903 POLARSSL_MODE_CBC,
904 POLARSSL_KEY_LENGTH_DES_EDE3,
905 "DES-EDE3-CBC",
906 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200907 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000908 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000909 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000910};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200911#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker9af723c2014-05-01 13:03:14 +0200912#endif /* POLARSSL_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000913
Paul Bakker6132d0a2012-07-04 17:10:40 +0000914#if defined(POLARSSL_BLOWFISH_C)
915
Paul Bakker5e0efa72013-09-08 23:04:04 +0200916static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
917 const unsigned char *input, unsigned char *output )
918{
919 return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input, output );
920}
921
Paul Bakkerfae35f02013-03-13 10:33:51 +0100922static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000923 unsigned char *iv, const unsigned char *input, unsigned char *output )
924{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200925#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +0000926 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200927#else
928 ((void) ctx);
929 ((void) operation);
930 ((void) length);
931 ((void) iv);
932 ((void) input);
933 ((void) output);
934
935 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
936#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000937}
938
Paul Bakkerfae35f02013-03-13 10:33:51 +0100939static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000940 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
941{
942#if defined(POLARSSL_CIPHER_MODE_CFB)
943 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
944#else
945 ((void) ctx);
946 ((void) operation);
947 ((void) length);
948 ((void) iv_off);
949 ((void) iv);
950 ((void) input);
951 ((void) output);
952
953 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200954#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000955}
956
Paul Bakkerfae35f02013-03-13 10:33:51 +0100957static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000958 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
959 const unsigned char *input, unsigned char *output )
960{
961#if defined(POLARSSL_CIPHER_MODE_CTR)
962 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
963 stream_block, input, output );
964#else
965 ((void) ctx);
966 ((void) length);
967 ((void) nc_off);
968 ((void) nonce_counter);
969 ((void) stream_block);
970 ((void) input);
971 ((void) output);
972
973 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200974#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000975}
976
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200977static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000978{
979 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
980}
981
982static void * blowfish_ctx_alloc( void )
983{
Paul Bakker6e339b52013-07-03 13:37:05 +0200984 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000985}
986
987static void blowfish_ctx_free( void *ctx )
988{
Paul Bakker6e339b52013-07-03 13:37:05 +0200989 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000990}
991
992const cipher_base_t blowfish_info = {
993 POLARSSL_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200994 blowfish_crypt_ecb_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000995 blowfish_crypt_cbc_wrap,
996 blowfish_crypt_cfb64_wrap,
997 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200998 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200999 blowfish_setkey_wrap,
1000 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001001 blowfish_ctx_alloc,
1002 blowfish_ctx_free
1003};
1004
Paul Bakker5e0efa72013-09-08 23:04:04 +02001005const cipher_info_t blowfish_ecb_info = {
1006 POLARSSL_CIPHER_BLOWFISH_ECB,
1007 POLARSSL_MODE_ECB,
1008 128,
1009 "BLOWFISH-ECB",
1010 8,
1011 0,
1012 8,
1013 &blowfish_info
1014};
1015
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001016#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001017const cipher_info_t blowfish_cbc_info = {
1018 POLARSSL_CIPHER_BLOWFISH_CBC,
1019 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001020 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001021 "BLOWFISH-CBC",
1022 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001023 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001024 8,
1025 &blowfish_info
1026};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001027#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001028
1029#if defined(POLARSSL_CIPHER_MODE_CFB)
1030const cipher_info_t blowfish_cfb64_info = {
1031 POLARSSL_CIPHER_BLOWFISH_CFB64,
1032 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001033 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001034 "BLOWFISH-CFB64",
1035 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001036 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001037 8,
1038 &blowfish_info
1039};
1040#endif /* POLARSSL_CIPHER_MODE_CFB */
1041
1042#if defined(POLARSSL_CIPHER_MODE_CTR)
1043const cipher_info_t blowfish_ctr_info = {
1044 POLARSSL_CIPHER_BLOWFISH_CTR,
1045 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001046 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001047 "BLOWFISH-CTR",
1048 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001049 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001050 8,
1051 &blowfish_info
1052};
1053#endif /* POLARSSL_CIPHER_MODE_CTR */
1054#endif /* POLARSSL_BLOWFISH_C */
1055
Paul Bakker68884e32013-01-07 18:20:04 +01001056#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001057static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1058 const unsigned char *input,
1059 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001060{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001061 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001062}
1063
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001064static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1065 unsigned int key_length )
1066{
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001067 /* we get key_length in bits, arc4 expects it in bytes */
1068 if( key_length % 8 != 0)
1069 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
1070
1071 arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001072 return( 0 );
1073}
1074
1075static void * arc4_ctx_alloc( void )
1076{
1077 return polarssl_malloc( sizeof( arc4_context ) );
1078}
Paul Bakker68884e32013-01-07 18:20:04 +01001079
1080static void arc4_ctx_free( void *ctx )
1081{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001082 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001083}
1084
1085const cipher_base_t arc4_base_info = {
1086 POLARSSL_CIPHER_ID_ARC4,
1087 NULL,
1088 NULL,
1089 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001090 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001091 arc4_crypt_stream_wrap,
1092 arc4_setkey_wrap,
1093 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001094 arc4_ctx_alloc,
1095 arc4_ctx_free
1096};
1097
1098const cipher_info_t arc4_128_info = {
1099 POLARSSL_CIPHER_ARC4_128,
1100 POLARSSL_MODE_STREAM,
1101 128,
1102 "ARC4-128",
1103 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001104 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001105 1,
1106 &arc4_base_info
1107};
1108#endif /* POLARSSL_ARC4_C */
1109
Paul Bakkerfab5c822012-02-06 16:45:10 +00001110#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001111static int null_crypt_stream( void *ctx, size_t length,
1112 const unsigned char *input,
1113 unsigned char *output )
1114{
1115 ((void) ctx);
1116 memmove( output, input, length );
1117 return( 0 );
1118}
1119
1120static int null_setkey( void *ctx, const unsigned char *key,
1121 unsigned int key_length )
1122{
1123 ((void) ctx);
1124 ((void) key);
1125 ((void) key_length);
1126
1127 return( 0 );
1128}
1129
Paul Bakkerfab5c822012-02-06 16:45:10 +00001130static void * null_ctx_alloc( void )
1131{
1132 return (void *) 1;
1133}
1134
Paul Bakkerfab5c822012-02-06 16:45:10 +00001135static void null_ctx_free( void *ctx )
1136{
1137 ((void) ctx);
1138}
1139
1140const cipher_base_t null_base_info = {
1141 POLARSSL_CIPHER_ID_NULL,
1142 NULL,
1143 NULL,
1144 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001145 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001146 null_crypt_stream,
1147 null_setkey,
1148 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001149 null_ctx_alloc,
1150 null_ctx_free
1151};
1152
1153const cipher_info_t null_cipher_info = {
1154 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001155 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001156 0,
1157 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001158 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001159 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001160 1,
1161 &null_base_info
1162};
1163#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1164
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001165const cipher_definition_t cipher_definitions[] =
1166{
1167#if defined(POLARSSL_AES_C)
1168 { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1169 { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1170 { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1171#if defined(POLARSSL_CIPHER_MODE_CBC)
1172 { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1173 { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1174 { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1175#endif
1176#if defined(POLARSSL_CIPHER_MODE_CFB)
1177 { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1178 { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1179 { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1180#endif
1181#if defined(POLARSSL_CIPHER_MODE_CTR)
1182 { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1183 { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1184 { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1185#endif
1186#if defined(POLARSSL_GCM_C)
1187 { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1188 { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1189 { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1190#endif
1191#endif /* POLARSSL_AES_C */
1192
1193#if defined(POLARSSL_ARC4_C)
1194 { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1195#endif
1196
1197#if defined(POLARSSL_BLOWFISH_C)
1198 { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1199#if defined(POLARSSL_CIPHER_MODE_CBC)
1200 { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1201#endif
1202#if defined(POLARSSL_CIPHER_MODE_CFB)
1203 { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1204#endif
1205#if defined(POLARSSL_CIPHER_MODE_CTR)
1206 { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1207#endif
1208#endif /* POLARSSL_BLOWFISH_C */
1209
1210#if defined(POLARSSL_CAMELLIA_C)
1211 { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1212 { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
Manuel Pégourié-Gonnard13e0d442013-10-24 12:59:00 +02001213 { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001214#if defined(POLARSSL_CIPHER_MODE_CBC)
1215 { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1216 { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1217 { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1218#endif
1219#if defined(POLARSSL_CIPHER_MODE_CFB)
1220 { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1221 { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1222 { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1223#endif
1224#if defined(POLARSSL_CIPHER_MODE_CTR)
1225 { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1226 { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1227 { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1228#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001229#if defined(POLARSSL_GCM_C)
1230 { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1231 { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1232 { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1233#endif
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001234#endif /* POLARSSL_CAMELLIA_C */
1235
1236#if defined(POLARSSL_DES_C)
1237 { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1238 { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1239 { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1240#if defined(POLARSSL_CIPHER_MODE_CBC)
1241 { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1242 { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1243 { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1244#endif
1245#endif /* POLARSSL_DES_C */
1246
1247#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard057e0cf2013-10-14 14:19:31 +02001248 { POLARSSL_CIPHER_NULL, &null_cipher_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001249#endif /* POLARSSL_CIPHER_NULL_CIPHER */
1250
1251 { 0, NULL }
1252};
1253
1254#define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1255int supported_ciphers[NUM_CIPHERS];
1256
Paul Bakker9af723c2014-05-01 13:03:14 +02001257#endif /* POLARSSL_CIPHER_C */