blob: a8bf4abeffc45a7caadde68f052d99b85d1f47a7 [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 Bakkerb9e4e2c2014-05-01 14:18:25 +020099 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input,
100 output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200101#else
102 ((void) ctx);
103 ((void) operation);
104 ((void) length);
105 ((void) iv);
106 ((void) input);
107 ((void) output);
108
109 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
110#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111}
112
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200113static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation,
114 size_t length, size_t *iv_off, unsigned char *iv,
115 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000116{
117#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200118 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv,
119 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000120#else
121 ((void) ctx);
122 ((void) operation);
123 ((void) length);
124 ((void) iv_off);
125 ((void) iv);
126 ((void) input);
127 ((void) output);
128
129 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200130#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000131}
132
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200133static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
134 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000135 const unsigned char *input, unsigned char *output )
136{
137#if defined(POLARSSL_CIPHER_MODE_CTR)
138 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
139 stream_block, input, output );
140#else
141 ((void) ctx);
142 ((void) length);
143 ((void) nc_off);
144 ((void) nonce_counter);
145 ((void) stream_block);
146 ((void) input);
147 ((void) output);
148
149 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200150#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000151}
152
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200153static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
154 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155{
156 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
157}
158
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200159static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
160 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161{
162 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
163}
164
165static void * aes_ctx_alloc( void )
166{
Paul Bakker6e339b52013-07-03 13:37:05 +0200167 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168}
169
170static void aes_ctx_free( void *ctx )
171{
Paul Bakker6e339b52013-07-03 13:37:05 +0200172 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000173}
174
Paul Bakker343a8702011-06-09 14:27:58 +0000175const cipher_base_t aes_info = {
176 POLARSSL_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200177 aes_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000178 aes_crypt_cbc_wrap,
179 aes_crypt_cfb128_wrap,
180 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200181 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000182 aes_setkey_enc_wrap,
183 aes_setkey_dec_wrap,
184 aes_ctx_alloc,
185 aes_ctx_free
186};
187
Paul Bakker5e0efa72013-09-08 23:04:04 +0200188const cipher_info_t aes_128_ecb_info = {
189 POLARSSL_CIPHER_AES_128_ECB,
190 POLARSSL_MODE_ECB,
191 128,
192 "AES-128-ECB",
193 16,
194 0,
195 16,
196 &aes_info
197};
198
199const cipher_info_t aes_192_ecb_info = {
200 POLARSSL_CIPHER_AES_192_ECB,
201 POLARSSL_MODE_ECB,
202 192,
203 "AES-192-ECB",
204 16,
205 0,
206 16,
207 &aes_info
208};
209
210const cipher_info_t aes_256_ecb_info = {
211 POLARSSL_CIPHER_AES_256_ECB,
212 POLARSSL_MODE_ECB,
213 256,
214 "AES-256-ECB",
215 16,
216 0,
217 16,
218 &aes_info
219};
220
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200221#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000222const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000223 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000224 POLARSSL_MODE_CBC,
225 128,
226 "AES-128-CBC",
227 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200228 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000229 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000230 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231};
232
233const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000234 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000235 POLARSSL_MODE_CBC,
236 192,
237 "AES-192-CBC",
238 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200239 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000240 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000241 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000242};
243
244const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000245 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 POLARSSL_MODE_CBC,
247 256,
248 "AES-256-CBC",
249 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200250 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000251 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000252 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000253};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200254#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000255
256#if defined(POLARSSL_CIPHER_MODE_CFB)
257const cipher_info_t aes_128_cfb128_info = {
258 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000259 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000260 128,
261 "AES-128-CFB128",
262 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200263 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000264 16,
265 &aes_info
266};
267
268const cipher_info_t aes_192_cfb128_info = {
269 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000270 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000271 192,
272 "AES-192-CFB128",
273 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200274 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000275 16,
276 &aes_info
277};
278
279const cipher_info_t aes_256_cfb128_info = {
280 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000281 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000282 256,
283 "AES-256-CFB128",
284 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200285 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000286 16,
287 &aes_info
288};
289#endif /* POLARSSL_CIPHER_MODE_CFB */
290
291#if defined(POLARSSL_CIPHER_MODE_CTR)
292const cipher_info_t aes_128_ctr_info = {
293 POLARSSL_CIPHER_AES_128_CTR,
294 POLARSSL_MODE_CTR,
295 128,
296 "AES-128-CTR",
297 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200298 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000299 16,
300 &aes_info
301};
302
303const cipher_info_t aes_192_ctr_info = {
304 POLARSSL_CIPHER_AES_192_CTR,
305 POLARSSL_MODE_CTR,
306 192,
307 "AES-192-CTR",
308 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200309 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000310 16,
311 &aes_info
312};
313
314const cipher_info_t aes_256_ctr_info = {
315 POLARSSL_CIPHER_AES_256_CTR,
316 POLARSSL_MODE_CTR,
317 256,
318 "AES-256-CTR",
319 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200320 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000321 16,
322 &aes_info
323};
324#endif /* POLARSSL_CIPHER_MODE_CTR */
325
Paul Bakker68884e32013-01-07 18:20:04 +0100326#if defined(POLARSSL_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200327static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
328 unsigned int key_length )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200329{
Paul Bakker43aff2a2013-09-09 00:10:27 +0200330 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
331 key, key_length );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200332}
333
334const cipher_base_t gcm_aes_info = {
335 POLARSSL_CIPHER_ID_AES,
336 NULL,
337 NULL,
338 NULL,
339 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200340 NULL,
Paul Bakker43aff2a2013-09-09 00:10:27 +0200341 gcm_aes_setkey_wrap,
342 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200343 gcm_ctx_alloc,
344 gcm_ctx_free,
345};
346
Paul Bakker68884e32013-01-07 18:20:04 +0100347const cipher_info_t aes_128_gcm_info = {
348 POLARSSL_CIPHER_AES_128_GCM,
349 POLARSSL_MODE_GCM,
350 128,
351 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200352 12,
353 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100354 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200355 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100356};
357
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200358const cipher_info_t aes_192_gcm_info = {
359 POLARSSL_CIPHER_AES_192_GCM,
360 POLARSSL_MODE_GCM,
361 192,
362 "AES-192-GCM",
363 12,
364 1,
365 16,
366 &gcm_aes_info
367};
368
Paul Bakker68884e32013-01-07 18:20:04 +0100369const cipher_info_t aes_256_gcm_info = {
370 POLARSSL_CIPHER_AES_256_GCM,
371 POLARSSL_MODE_GCM,
372 256,
373 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200374 12,
375 1,
Paul Bakker68884e32013-01-07 18:20:04 +0100376 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200377 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100378};
379#endif /* POLARSSL_GCM_C */
380
Paul Bakker9af723c2014-05-01 13:03:14 +0200381#endif /* POLARSSL_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
383#if defined(POLARSSL_CAMELLIA_C)
384
Paul Bakker5e0efa72013-09-08 23:04:04 +0200385static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
386 const unsigned char *input, unsigned char *output )
387{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200388 return camellia_crypt_ecb( (camellia_context *) ctx, operation, input,
389 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200390}
391
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200392static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation,
393 size_t length, unsigned char *iv,
394 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200396#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200397 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv,
398 input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200399#else
400 ((void) ctx);
401 ((void) operation);
402 ((void) length);
403 ((void) iv);
404 ((void) input);
405 ((void) output);
406
407 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
408#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409}
410
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200411static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation,
412 size_t length, size_t *iv_off, unsigned char *iv,
413 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000414{
415#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200416 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length,
417 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000418#else
419 ((void) ctx);
420 ((void) operation);
421 ((void) length);
422 ((void) iv_off);
423 ((void) iv);
424 ((void) input);
425 ((void) output);
426
427 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200428#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000429}
430
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200431static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
432 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000433 const unsigned char *input, unsigned char *output )
434{
435#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200436 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off,
437 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000438#else
439 ((void) ctx);
440 ((void) length);
441 ((void) nc_off);
442 ((void) nonce_counter);
443 ((void) stream_block);
444 ((void) input);
445 ((void) output);
446
447 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000449}
450
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200451static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
452 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000453{
454 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
455}
456
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200457static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
458 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459{
460 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
461}
462
463static void * camellia_ctx_alloc( void )
464{
Paul Bakker6e339b52013-07-03 13:37:05 +0200465 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466}
467
468static void camellia_ctx_free( void *ctx )
469{
Paul Bakker6e339b52013-07-03 13:37:05 +0200470 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000471}
472
Paul Bakker343a8702011-06-09 14:27:58 +0000473const cipher_base_t camellia_info = {
474 POLARSSL_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200475 camellia_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000476 camellia_crypt_cbc_wrap,
477 camellia_crypt_cfb128_wrap,
478 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200479 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000480 camellia_setkey_enc_wrap,
481 camellia_setkey_dec_wrap,
482 camellia_ctx_alloc,
483 camellia_ctx_free
484};
485
Paul Bakker5e0efa72013-09-08 23:04:04 +0200486const cipher_info_t camellia_128_ecb_info = {
487 POLARSSL_CIPHER_CAMELLIA_128_ECB,
488 POLARSSL_MODE_ECB,
489 128,
490 "CAMELLIA-128-ECB",
491 16,
492 0,
493 16,
494 &camellia_info
495};
496
497const cipher_info_t camellia_192_ecb_info = {
498 POLARSSL_CIPHER_CAMELLIA_192_ECB,
499 POLARSSL_MODE_ECB,
500 192,
501 "CAMELLIA-192-ECB",
502 16,
503 0,
504 16,
505 &camellia_info
506};
507
508const cipher_info_t camellia_256_ecb_info = {
509 POLARSSL_CIPHER_CAMELLIA_256_ECB,
510 POLARSSL_MODE_ECB,
511 256,
512 "CAMELLIA-256-ECB",
513 16,
514 0,
515 16,
516 &camellia_info
517};
518
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200519#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000521 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000522 POLARSSL_MODE_CBC,
523 128,
524 "CAMELLIA-128-CBC",
525 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200526 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000527 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000528 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000529};
530
531const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000532 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000533 POLARSSL_MODE_CBC,
534 192,
535 "CAMELLIA-192-CBC",
536 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200537 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000538 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000539 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000540};
541
542const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000543 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000544 POLARSSL_MODE_CBC,
545 256,
546 "CAMELLIA-256-CBC",
547 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200548 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000549 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000550 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200552#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000553
554#if defined(POLARSSL_CIPHER_MODE_CFB)
555const cipher_info_t camellia_128_cfb128_info = {
556 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000557 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000558 128,
559 "CAMELLIA-128-CFB128",
560 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200561 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000562 16,
563 &camellia_info
564};
565
566const cipher_info_t camellia_192_cfb128_info = {
567 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000568 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000569 192,
570 "CAMELLIA-192-CFB128",
571 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200572 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000573 16,
574 &camellia_info
575};
576
577const cipher_info_t camellia_256_cfb128_info = {
578 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000579 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000580 256,
581 "CAMELLIA-256-CFB128",
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#endif /* POLARSSL_CIPHER_MODE_CFB */
588
589#if defined(POLARSSL_CIPHER_MODE_CTR)
590const cipher_info_t camellia_128_ctr_info = {
591 POLARSSL_CIPHER_CAMELLIA_128_CTR,
592 POLARSSL_MODE_CTR,
593 128,
594 "CAMELLIA-128-CTR",
595 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200596 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000597 16,
598 &camellia_info
599};
600
601const cipher_info_t camellia_192_ctr_info = {
602 POLARSSL_CIPHER_CAMELLIA_192_CTR,
603 POLARSSL_MODE_CTR,
604 192,
605 "CAMELLIA-192-CTR",
606 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200607 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000608 16,
609 &camellia_info
610};
611
612const cipher_info_t camellia_256_ctr_info = {
613 POLARSSL_CIPHER_CAMELLIA_256_CTR,
614 POLARSSL_MODE_CTR,
615 256,
616 "CAMELLIA-256-CTR",
617 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200618 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000619 16,
620 &camellia_info
621};
622#endif /* POLARSSL_CIPHER_MODE_CTR */
623
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200624#if defined(POLARSSL_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200625static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
626 unsigned int key_length )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200627{
628 return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
629 key, key_length );
630}
631
632const cipher_base_t gcm_camellia_info = {
633 POLARSSL_CIPHER_ID_CAMELLIA,
634 NULL,
635 NULL,
636 NULL,
637 NULL,
638 NULL,
639 gcm_camellia_setkey_wrap,
640 gcm_camellia_setkey_wrap,
641 gcm_ctx_alloc,
642 gcm_ctx_free,
643};
644
645const cipher_info_t camellia_128_gcm_info = {
646 POLARSSL_CIPHER_CAMELLIA_128_GCM,
647 POLARSSL_MODE_GCM,
648 128,
649 "CAMELLIA-128-GCM",
650 12,
651 1,
652 16,
653 &gcm_camellia_info
654};
655
656const cipher_info_t camellia_192_gcm_info = {
657 POLARSSL_CIPHER_CAMELLIA_192_GCM,
658 POLARSSL_MODE_GCM,
659 192,
660 "CAMELLIA-192-GCM",
661 12,
662 1,
663 16,
664 &gcm_camellia_info
665};
666
667const cipher_info_t camellia_256_gcm_info = {
668 POLARSSL_CIPHER_CAMELLIA_256_GCM,
669 POLARSSL_MODE_GCM,
670 256,
671 "CAMELLIA-256-GCM",
672 12,
673 1,
674 16,
675 &gcm_camellia_info
676};
677#endif /* POLARSSL_GCM_C */
678
679#endif /* POLARSSL_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680
681#if defined(POLARSSL_DES_C)
682
Paul Bakker5e0efa72013-09-08 23:04:04 +0200683static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
684 const unsigned char *input, unsigned char *output )
685{
686 ((void) operation);
687 return des_crypt_ecb( (des_context *) ctx, input, output );
688}
689
690static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
691 const unsigned char *input, unsigned char *output )
692{
693 ((void) operation);
694 return des3_crypt_ecb( (des3_context *) ctx, input, output );
695}
696
Paul Bakkerfae35f02013-03-13 10:33:51 +0100697static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698 unsigned char *iv, const unsigned char *input, unsigned char *output )
699{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200700#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200701 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input,
702 output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200703#else
704 ((void) ctx);
705 ((void) operation);
706 ((void) length);
707 ((void) iv);
708 ((void) input);
709 ((void) output);
710
711 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
712#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713}
714
Paul Bakkerfae35f02013-03-13 10:33:51 +0100715static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716 unsigned char *iv, const unsigned char *input, unsigned char *output )
717{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200718#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200719 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input,
720 output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200721#else
722 ((void) ctx);
723 ((void) operation);
724 ((void) length);
725 ((void) iv);
726 ((void) input);
727 ((void) output);
728
729 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
730#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731}
732
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200733static int des_crypt_cfb128_wrap( void *ctx, operation_t operation,
734 size_t length, size_t *iv_off, unsigned char *iv,
735 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000736{
737 ((void) ctx);
738 ((void) operation);
739 ((void) length);
740 ((void) iv_off);
741 ((void) iv);
742 ((void) input);
743 ((void) output);
744
745 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
746}
747
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200748static int des_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
749 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000750 const unsigned char *input, unsigned char *output )
751{
752 ((void) ctx);
753 ((void) length);
754 ((void) nc_off);
755 ((void) nonce_counter);
756 ((void) stream_block);
757 ((void) input);
758 ((void) output);
759
760 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
761}
762
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200763static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
764 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000766 ((void) key_length);
767
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768 return des_setkey_dec( (des_context *) ctx, key );
769}
770
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200771static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
772 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000774 ((void) key_length);
775
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776 return des_setkey_enc( (des_context *) ctx, key );
777}
778
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200779static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
780 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000782 ((void) key_length);
783
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784 return des3_set2key_dec( (des3_context *) ctx, key );
785}
786
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200787static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
788 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000789{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000790 ((void) key_length);
791
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 return des3_set2key_enc( (des3_context *) ctx, key );
793}
794
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200795static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
796 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000798 ((void) key_length);
799
Paul Bakker8123e9d2011-01-06 15:37:30 +0000800 return des3_set3key_dec( (des3_context *) ctx, key );
801}
802
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200803static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
804 unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000806 ((void) key_length);
807
Paul Bakker8123e9d2011-01-06 15:37:30 +0000808 return des3_set3key_enc( (des3_context *) ctx, key );
809}
810
811static void * des_ctx_alloc( void )
812{
Paul Bakker6e339b52013-07-03 13:37:05 +0200813 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000814}
815
816static void * des3_ctx_alloc( void )
817{
Paul Bakker6e339b52013-07-03 13:37:05 +0200818 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819}
820
821static void des_ctx_free( void *ctx )
822{
Paul Bakker6e339b52013-07-03 13:37:05 +0200823 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000824}
825
Paul Bakker343a8702011-06-09 14:27:58 +0000826const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000827 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200828 des_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000829 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000830 des_crypt_cfb128_wrap,
831 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200832 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000833 des_setkey_enc_wrap,
834 des_setkey_dec_wrap,
835 des_ctx_alloc,
836 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000837};
838
Paul Bakker5e0efa72013-09-08 23:04:04 +0200839const cipher_info_t des_ecb_info = {
840 POLARSSL_CIPHER_DES_ECB,
841 POLARSSL_MODE_ECB,
842 POLARSSL_KEY_LENGTH_DES,
843 "DES-ECB",
844 8,
845 0,
846 8,
847 &des_info
848};
849
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200850#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000851const cipher_info_t des_cbc_info = {
852 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000853 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000854 POLARSSL_KEY_LENGTH_DES,
855 "DES-CBC",
856 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200857 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000858 8,
859 &des_info
860};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200861#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000862
863const cipher_base_t des_ede_info = {
864 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200865 des3_crypt_ecb_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000866 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000867 des_crypt_cfb128_wrap,
868 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200869 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000870 des3_set2key_enc_wrap,
871 des3_set2key_dec_wrap,
872 des3_ctx_alloc,
873 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000874};
875
Paul Bakker5e0efa72013-09-08 23:04:04 +0200876const cipher_info_t des_ede_ecb_info = {
877 POLARSSL_CIPHER_DES_EDE_ECB,
878 POLARSSL_MODE_ECB,
879 POLARSSL_KEY_LENGTH_DES_EDE,
880 "DES-EDE-ECB",
881 8,
882 0,
883 8,
884 &des_ede_info
885};
886
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200887#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000888const cipher_info_t des_ede_cbc_info = {
889 POLARSSL_CIPHER_DES_EDE_CBC,
890 POLARSSL_MODE_CBC,
891 POLARSSL_KEY_LENGTH_DES_EDE,
892 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200893 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200894 0,
Paul Bakker0e342352013-06-24 19:33:02 +0200895 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000896 &des_ede_info
897};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200898#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000899
900const cipher_base_t des_ede3_info = {
901 POLARSSL_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200902 des3_crypt_ecb_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000903 des3_crypt_cbc_wrap,
904 des_crypt_cfb128_wrap,
905 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200906 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000907 des3_set3key_enc_wrap,
908 des3_set3key_dec_wrap,
909 des3_ctx_alloc,
910 des_ctx_free
911};
912
Paul Bakker5e0efa72013-09-08 23:04:04 +0200913const cipher_info_t des_ede3_ecb_info = {
914 POLARSSL_CIPHER_DES_EDE3_ECB,
915 POLARSSL_MODE_ECB,
916 POLARSSL_KEY_LENGTH_DES_EDE3,
917 "DES-EDE3-ECB",
918 8,
919 0,
920 8,
921 &des_ede3_info
922};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200923#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000924const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000925 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000926 POLARSSL_MODE_CBC,
927 POLARSSL_KEY_LENGTH_DES_EDE3,
928 "DES-EDE3-CBC",
929 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200930 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000931 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000932 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000933};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200934#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker9af723c2014-05-01 13:03:14 +0200935#endif /* POLARSSL_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000936
Paul Bakker6132d0a2012-07-04 17:10:40 +0000937#if defined(POLARSSL_BLOWFISH_C)
938
Paul Bakker5e0efa72013-09-08 23:04:04 +0200939static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
940 const unsigned char *input, unsigned char *output )
941{
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200942 return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input,
943 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200944}
945
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200946static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation,
947 size_t length, unsigned char *iv, const unsigned char *input,
948 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000949{
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200950#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200951 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv,
952 input, output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200953#else
954 ((void) ctx);
955 ((void) operation);
956 ((void) length);
957 ((void) iv);
958 ((void) input);
959 ((void) output);
960
961 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
962#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000963}
964
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200965static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation,
966 size_t length, size_t *iv_off, unsigned char *iv,
967 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000968{
969#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200970 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length,
971 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000972#else
973 ((void) ctx);
974 ((void) operation);
975 ((void) length);
976 ((void) iv_off);
977 ((void) iv);
978 ((void) input);
979 ((void) output);
980
981 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +0200982#endif /* POLARSSL_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +0000983}
984
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200985static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
986 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000987 const unsigned char *input, unsigned char *output )
988{
989#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200990 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off,
991 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000992#else
993 ((void) ctx);
994 ((void) length);
995 ((void) nc_off);
996 ((void) nonce_counter);
997 ((void) stream_block);
998 ((void) input);
999 ((void) output);
1000
1001 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
Paul Bakker9af723c2014-05-01 13:03:14 +02001002#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001003}
1004
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001005static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1006 unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001007{
1008 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
1009}
1010
1011static void * blowfish_ctx_alloc( void )
1012{
Paul Bakker6e339b52013-07-03 13:37:05 +02001013 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001014}
1015
1016static void blowfish_ctx_free( void *ctx )
1017{
Paul Bakker6e339b52013-07-03 13:37:05 +02001018 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001019}
1020
1021const cipher_base_t blowfish_info = {
1022 POLARSSL_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001023 blowfish_crypt_ecb_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001024 blowfish_crypt_cbc_wrap,
1025 blowfish_crypt_cfb64_wrap,
1026 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001027 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001028 blowfish_setkey_wrap,
1029 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001030 blowfish_ctx_alloc,
1031 blowfish_ctx_free
1032};
1033
Paul Bakker5e0efa72013-09-08 23:04:04 +02001034const cipher_info_t blowfish_ecb_info = {
1035 POLARSSL_CIPHER_BLOWFISH_ECB,
1036 POLARSSL_MODE_ECB,
1037 128,
1038 "BLOWFISH-ECB",
1039 8,
1040 0,
1041 8,
1042 &blowfish_info
1043};
1044
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001045#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001046const cipher_info_t blowfish_cbc_info = {
1047 POLARSSL_CIPHER_BLOWFISH_CBC,
1048 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001049 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001050 "BLOWFISH-CBC",
1051 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001052 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001053 8,
1054 &blowfish_info
1055};
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +02001056#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001057
1058#if defined(POLARSSL_CIPHER_MODE_CFB)
1059const cipher_info_t blowfish_cfb64_info = {
1060 POLARSSL_CIPHER_BLOWFISH_CFB64,
1061 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001062 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001063 "BLOWFISH-CFB64",
1064 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001065 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001066 8,
1067 &blowfish_info
1068};
1069#endif /* POLARSSL_CIPHER_MODE_CFB */
1070
1071#if defined(POLARSSL_CIPHER_MODE_CTR)
1072const cipher_info_t blowfish_ctr_info = {
1073 POLARSSL_CIPHER_BLOWFISH_CTR,
1074 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001075 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001076 "BLOWFISH-CTR",
1077 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001078 0,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001079 8,
1080 &blowfish_info
1081};
1082#endif /* POLARSSL_CIPHER_MODE_CTR */
1083#endif /* POLARSSL_BLOWFISH_C */
1084
Paul Bakker68884e32013-01-07 18:20:04 +01001085#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001086static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1087 const unsigned char *input,
1088 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001089{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001090 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001091}
1092
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001093static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1094 unsigned int key_length )
1095{
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001096 /* we get key_length in bits, arc4 expects it in bytes */
1097 if( key_length % 8 != 0)
1098 return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
1099
1100 arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001101 return( 0 );
1102}
1103
1104static void * arc4_ctx_alloc( void )
1105{
1106 return polarssl_malloc( sizeof( arc4_context ) );
1107}
Paul Bakker68884e32013-01-07 18:20:04 +01001108
1109static void arc4_ctx_free( void *ctx )
1110{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001111 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001112}
1113
1114const cipher_base_t arc4_base_info = {
1115 POLARSSL_CIPHER_ID_ARC4,
1116 NULL,
1117 NULL,
1118 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001119 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001120 arc4_crypt_stream_wrap,
1121 arc4_setkey_wrap,
1122 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001123 arc4_ctx_alloc,
1124 arc4_ctx_free
1125};
1126
1127const cipher_info_t arc4_128_info = {
1128 POLARSSL_CIPHER_ARC4_128,
1129 POLARSSL_MODE_STREAM,
1130 128,
1131 "ARC4-128",
1132 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001133 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001134 1,
1135 &arc4_base_info
1136};
1137#endif /* POLARSSL_ARC4_C */
1138
Paul Bakkerfab5c822012-02-06 16:45:10 +00001139#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001140static int null_crypt_stream( void *ctx, size_t length,
1141 const unsigned char *input,
1142 unsigned char *output )
1143{
1144 ((void) ctx);
1145 memmove( output, input, length );
1146 return( 0 );
1147}
1148
1149static int null_setkey( void *ctx, const unsigned char *key,
1150 unsigned int key_length )
1151{
1152 ((void) ctx);
1153 ((void) key);
1154 ((void) key_length);
1155
1156 return( 0 );
1157}
1158
Paul Bakkerfab5c822012-02-06 16:45:10 +00001159static void * null_ctx_alloc( void )
1160{
1161 return (void *) 1;
1162}
1163
Paul Bakkerfab5c822012-02-06 16:45:10 +00001164static void null_ctx_free( void *ctx )
1165{
1166 ((void) ctx);
1167}
1168
1169const cipher_base_t null_base_info = {
1170 POLARSSL_CIPHER_ID_NULL,
1171 NULL,
1172 NULL,
1173 NULL,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001174 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001175 null_crypt_stream,
1176 null_setkey,
1177 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001178 null_ctx_alloc,
1179 null_ctx_free
1180};
1181
1182const cipher_info_t null_cipher_info = {
1183 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001184 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001185 0,
1186 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001187 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001188 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001189 1,
1190 &null_base_info
1191};
1192#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
1193
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001194const cipher_definition_t cipher_definitions[] =
1195{
1196#if defined(POLARSSL_AES_C)
1197 { POLARSSL_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1198 { POLARSSL_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1199 { POLARSSL_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1200#if defined(POLARSSL_CIPHER_MODE_CBC)
1201 { POLARSSL_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1202 { POLARSSL_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1203 { POLARSSL_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1204#endif
1205#if defined(POLARSSL_CIPHER_MODE_CFB)
1206 { POLARSSL_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1207 { POLARSSL_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1208 { POLARSSL_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1209#endif
1210#if defined(POLARSSL_CIPHER_MODE_CTR)
1211 { POLARSSL_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1212 { POLARSSL_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1213 { POLARSSL_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1214#endif
1215#if defined(POLARSSL_GCM_C)
1216 { POLARSSL_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1217 { POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1218 { POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1219#endif
1220#endif /* POLARSSL_AES_C */
1221
1222#if defined(POLARSSL_ARC4_C)
1223 { POLARSSL_CIPHER_ARC4_128, &arc4_128_info },
1224#endif
1225
1226#if defined(POLARSSL_BLOWFISH_C)
1227 { POLARSSL_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1228#if defined(POLARSSL_CIPHER_MODE_CBC)
1229 { POLARSSL_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1230#endif
1231#if defined(POLARSSL_CIPHER_MODE_CFB)
1232 { POLARSSL_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1233#endif
1234#if defined(POLARSSL_CIPHER_MODE_CTR)
1235 { POLARSSL_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1236#endif
1237#endif /* POLARSSL_BLOWFISH_C */
1238
1239#if defined(POLARSSL_CAMELLIA_C)
1240 { POLARSSL_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1241 { POLARSSL_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
Manuel Pégourié-Gonnard13e0d442013-10-24 12:59:00 +02001242 { POLARSSL_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001243#if defined(POLARSSL_CIPHER_MODE_CBC)
1244 { POLARSSL_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1245 { POLARSSL_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1246 { POLARSSL_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1247#endif
1248#if defined(POLARSSL_CIPHER_MODE_CFB)
1249 { POLARSSL_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1250 { POLARSSL_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1251 { POLARSSL_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1252#endif
1253#if defined(POLARSSL_CIPHER_MODE_CTR)
1254 { POLARSSL_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1255 { POLARSSL_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1256 { POLARSSL_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1257#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001258#if defined(POLARSSL_GCM_C)
1259 { POLARSSL_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1260 { POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1261 { POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1262#endif
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001263#endif /* POLARSSL_CAMELLIA_C */
1264
1265#if defined(POLARSSL_DES_C)
1266 { POLARSSL_CIPHER_DES_ECB, &des_ecb_info },
1267 { POLARSSL_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1268 { POLARSSL_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1269#if defined(POLARSSL_CIPHER_MODE_CBC)
1270 { POLARSSL_CIPHER_DES_CBC, &des_cbc_info },
1271 { POLARSSL_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1272 { POLARSSL_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1273#endif
1274#endif /* POLARSSL_DES_C */
1275
1276#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard057e0cf2013-10-14 14:19:31 +02001277 { POLARSSL_CIPHER_NULL, &null_cipher_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001278#endif /* POLARSSL_CIPHER_NULL_CIPHER */
1279
1280 { 0, NULL }
1281};
1282
1283#define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
1284int supported_ciphers[NUM_CIPHERS];
1285
Paul Bakker9af723c2014-05-01 13:03:14 +02001286#endif /* POLARSSL_CIPHER_C */