blob: c8eee54322a74f36605ceda6feb54ec4bb98b18d [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker8123e9d2011-01-06 15:37:30 +00003 *
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 Bakker68884e32013-01-07 18:20:04 +01008 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
12 *
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30#include "polarssl/config.h"
31
32#if defined(POLARSSL_CIPHER_C)
33
34#include "polarssl/cipher_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
36#if defined(POLARSSL_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000037#include "polarssl/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000038#endif
39
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020040#if defined(POLARSSL_ARC4_C)
41#include "polarssl/arc4.h"
42#endif
43
Paul Bakkerf6543712012-03-05 14:01:29 +000044#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000045#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000046#endif
47
48#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000050#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000051
Paul Bakker6132d0a2012-07-04 17:10:40 +000052#if defined(POLARSSL_BLOWFISH_C)
53#include "polarssl/blowfish.h"
54#endif
55
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020056#if defined(POLARSSL_GCM_C)
57#include "polarssl/gcm.h"
58#endif
59
Paul Bakker6e339b52013-07-03 13:37:05 +020060#if defined(POLARSSL_MEMORY_C)
61#include "polarssl/memory.h"
62#else
63#define polarssl_malloc malloc
64#define polarssl_free free
65#endif
66
Paul Bakker8123e9d2011-01-06 15:37:30 +000067#include <stdlib.h>
68
69#if defined(POLARSSL_AES_C)
70
Paul Bakkerfae35f02013-03-13 10:33:51 +010071static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000072 unsigned char *iv, const unsigned char *input, unsigned char *output )
73{
74 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
75}
76
Paul Bakkerfae35f02013-03-13 10:33:51 +010077static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000078 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
79{
80#if defined(POLARSSL_CIPHER_MODE_CFB)
81 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
82#else
83 ((void) ctx);
84 ((void) operation);
85 ((void) length);
86 ((void) iv_off);
87 ((void) iv);
88 ((void) input);
89 ((void) output);
90
91 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
92#endif
93}
94
Paul Bakkerfae35f02013-03-13 10:33:51 +010095static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000096 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
97 const unsigned char *input, unsigned char *output )
98{
99#if defined(POLARSSL_CIPHER_MODE_CTR)
100 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
101 stream_block, input, output );
102#else
103 ((void) ctx);
104 ((void) length);
105 ((void) nc_off);
106 ((void) nonce_counter);
107 ((void) stream_block);
108 ((void) input);
109 ((void) output);
110
111 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
112#endif
113}
114
Paul Bakkerfae35f02013-03-13 10:33:51 +0100115static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000116{
117 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
118}
119
Paul Bakkerfae35f02013-03-13 10:33:51 +0100120static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121{
122 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
123}
124
125static void * aes_ctx_alloc( void )
126{
Paul Bakker6e339b52013-07-03 13:37:05 +0200127 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000128}
129
130static void aes_ctx_free( void *ctx )
131{
Paul Bakker6e339b52013-07-03 13:37:05 +0200132 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133}
134
Paul Bakker343a8702011-06-09 14:27:58 +0000135const cipher_base_t aes_info = {
136 POLARSSL_CIPHER_ID_AES,
137 aes_crypt_cbc_wrap,
138 aes_crypt_cfb128_wrap,
139 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200140 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000141 aes_setkey_enc_wrap,
142 aes_setkey_dec_wrap,
143 aes_ctx_alloc,
144 aes_ctx_free
145};
146
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000148 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 POLARSSL_MODE_CBC,
150 128,
151 "AES-128-CBC",
152 16,
153 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000154 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155};
156
157const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000158 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_MODE_CBC,
160 192,
161 "AES-192-CBC",
162 16,
163 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000164 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165};
166
167const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000168 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000169 POLARSSL_MODE_CBC,
170 256,
171 "AES-256-CBC",
172 16,
173 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000174 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000175};
Paul Bakker343a8702011-06-09 14:27:58 +0000176
177#if defined(POLARSSL_CIPHER_MODE_CFB)
178const cipher_info_t aes_128_cfb128_info = {
179 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000180 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000181 128,
182 "AES-128-CFB128",
183 16,
184 16,
185 &aes_info
186};
187
188const cipher_info_t aes_192_cfb128_info = {
189 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000190 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000191 192,
192 "AES-192-CFB128",
193 16,
194 16,
195 &aes_info
196};
197
198const cipher_info_t aes_256_cfb128_info = {
199 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000200 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000201 256,
202 "AES-256-CFB128",
203 16,
204 16,
205 &aes_info
206};
207#endif /* POLARSSL_CIPHER_MODE_CFB */
208
209#if defined(POLARSSL_CIPHER_MODE_CTR)
210const cipher_info_t aes_128_ctr_info = {
211 POLARSSL_CIPHER_AES_128_CTR,
212 POLARSSL_MODE_CTR,
213 128,
214 "AES-128-CTR",
215 16,
216 16,
217 &aes_info
218};
219
220const cipher_info_t aes_192_ctr_info = {
221 POLARSSL_CIPHER_AES_192_CTR,
222 POLARSSL_MODE_CTR,
223 192,
224 "AES-192-CTR",
225 16,
226 16,
227 &aes_info
228};
229
230const cipher_info_t aes_256_ctr_info = {
231 POLARSSL_CIPHER_AES_256_CTR,
232 POLARSSL_MODE_CTR,
233 256,
234 "AES-256-CTR",
235 16,
236 16,
237 &aes_info
238};
239#endif /* POLARSSL_CIPHER_MODE_CTR */
240
Paul Bakker68884e32013-01-07 18:20:04 +0100241#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200242static void *gcm_ctx_alloc( void )
243{
244 return polarssl_malloc( sizeof( gcm_context ) );
245}
246
247static void gcm_ctx_free( void *ctx )
248{
249 polarssl_free( ctx );
250}
251
252static int gcm_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
253{
254 return gcm_init( (gcm_context *) ctx, key, key_length );
255}
256
257const cipher_base_t gcm_aes_info = {
258 POLARSSL_CIPHER_ID_AES,
259 NULL,
260 NULL,
261 NULL,
262 NULL,
263 gcm_setkey_wrap,
264 gcm_setkey_wrap,
265 gcm_ctx_alloc,
266 gcm_ctx_free,
267};
268
Paul Bakker68884e32013-01-07 18:20:04 +0100269const cipher_info_t aes_128_gcm_info = {
270 POLARSSL_CIPHER_AES_128_GCM,
271 POLARSSL_MODE_GCM,
272 128,
273 "AES-128-GCM",
274 16,
275 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200276 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100277};
278
279const cipher_info_t aes_256_gcm_info = {
280 POLARSSL_CIPHER_AES_256_GCM,
281 POLARSSL_MODE_GCM,
282 256,
283 "AES-256-GCM",
284 16,
285 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200286 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100287};
288#endif /* POLARSSL_GCM_C */
289
Paul Bakker8123e9d2011-01-06 15:37:30 +0000290#endif
291
292#if defined(POLARSSL_CAMELLIA_C)
293
Paul Bakkerfae35f02013-03-13 10:33:51 +0100294static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295 unsigned char *iv, const unsigned char *input, unsigned char *output )
296{
297 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
298}
299
Paul Bakkerfae35f02013-03-13 10:33:51 +0100300static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000301 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
302{
303#if defined(POLARSSL_CIPHER_MODE_CFB)
304 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
305#else
306 ((void) ctx);
307 ((void) operation);
308 ((void) length);
309 ((void) iv_off);
310 ((void) iv);
311 ((void) input);
312 ((void) output);
313
314 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
315#endif
316}
317
Paul Bakkerfae35f02013-03-13 10:33:51 +0100318static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000319 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
320 const unsigned char *input, unsigned char *output )
321{
322#if defined(POLARSSL_CIPHER_MODE_CTR)
323 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
324 stream_block, input, output );
325#else
326 ((void) ctx);
327 ((void) length);
328 ((void) nc_off);
329 ((void) nonce_counter);
330 ((void) stream_block);
331 ((void) input);
332 ((void) output);
333
334 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
335#endif
336}
337
Paul Bakkerfae35f02013-03-13 10:33:51 +0100338static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339{
340 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
341}
342
Paul Bakkerfae35f02013-03-13 10:33:51 +0100343static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344{
345 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
346}
347
348static void * camellia_ctx_alloc( void )
349{
Paul Bakker6e339b52013-07-03 13:37:05 +0200350 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351}
352
353static void camellia_ctx_free( void *ctx )
354{
Paul Bakker6e339b52013-07-03 13:37:05 +0200355 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000356}
357
Paul Bakker343a8702011-06-09 14:27:58 +0000358const cipher_base_t camellia_info = {
359 POLARSSL_CIPHER_ID_CAMELLIA,
360 camellia_crypt_cbc_wrap,
361 camellia_crypt_cfb128_wrap,
362 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200363 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000364 camellia_setkey_enc_wrap,
365 camellia_setkey_dec_wrap,
366 camellia_ctx_alloc,
367 camellia_ctx_free
368};
369
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000371 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000372 POLARSSL_MODE_CBC,
373 128,
374 "CAMELLIA-128-CBC",
375 16,
376 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000377 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378};
379
380const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000381 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000382 POLARSSL_MODE_CBC,
383 192,
384 "CAMELLIA-192-CBC",
385 16,
386 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000387 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388};
389
390const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000391 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000392 POLARSSL_MODE_CBC,
393 256,
394 "CAMELLIA-256-CBC",
395 16,
396 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000397 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398};
Paul Bakker343a8702011-06-09 14:27:58 +0000399
400#if defined(POLARSSL_CIPHER_MODE_CFB)
401const cipher_info_t camellia_128_cfb128_info = {
402 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000403 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000404 128,
405 "CAMELLIA-128-CFB128",
406 16,
407 16,
408 &camellia_info
409};
410
411const cipher_info_t camellia_192_cfb128_info = {
412 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000413 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000414 192,
415 "CAMELLIA-192-CFB128",
416 16,
417 16,
418 &camellia_info
419};
420
421const cipher_info_t camellia_256_cfb128_info = {
422 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000423 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000424 256,
425 "CAMELLIA-256-CFB128",
426 16,
427 16,
428 &camellia_info
429};
430#endif /* POLARSSL_CIPHER_MODE_CFB */
431
432#if defined(POLARSSL_CIPHER_MODE_CTR)
433const cipher_info_t camellia_128_ctr_info = {
434 POLARSSL_CIPHER_CAMELLIA_128_CTR,
435 POLARSSL_MODE_CTR,
436 128,
437 "CAMELLIA-128-CTR",
438 16,
439 16,
440 &camellia_info
441};
442
443const cipher_info_t camellia_192_ctr_info = {
444 POLARSSL_CIPHER_CAMELLIA_192_CTR,
445 POLARSSL_MODE_CTR,
446 192,
447 "CAMELLIA-192-CTR",
448 16,
449 16,
450 &camellia_info
451};
452
453const cipher_info_t camellia_256_ctr_info = {
454 POLARSSL_CIPHER_CAMELLIA_256_CTR,
455 POLARSSL_MODE_CTR,
456 256,
457 "CAMELLIA-256-CTR",
458 16,
459 16,
460 &camellia_info
461};
462#endif /* POLARSSL_CIPHER_MODE_CTR */
463
Paul Bakker8123e9d2011-01-06 15:37:30 +0000464#endif
465
466#if defined(POLARSSL_DES_C)
467
Paul Bakkerfae35f02013-03-13 10:33:51 +0100468static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 unsigned char *iv, const unsigned char *input, unsigned char *output )
470{
471 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
472}
473
Paul Bakkerfae35f02013-03-13 10:33:51 +0100474static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475 unsigned char *iv, const unsigned char *input, unsigned char *output )
476{
477 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
478}
479
Paul Bakkerfae35f02013-03-13 10:33:51 +0100480static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000481 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
482{
483 ((void) ctx);
484 ((void) operation);
485 ((void) length);
486 ((void) iv_off);
487 ((void) iv);
488 ((void) input);
489 ((void) output);
490
491 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
492}
493
Paul Bakkerfae35f02013-03-13 10:33:51 +0100494static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000495 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
496 const unsigned char *input, unsigned char *output )
497{
498 ((void) ctx);
499 ((void) length);
500 ((void) nc_off);
501 ((void) nonce_counter);
502 ((void) stream_block);
503 ((void) input);
504 ((void) output);
505
506 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
507}
508
Paul Bakkerfae35f02013-03-13 10:33:51 +0100509static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000511 ((void) key_length);
512
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513 return des_setkey_dec( (des_context *) ctx, key );
514}
515
Paul Bakkerfae35f02013-03-13 10:33:51 +0100516static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000518 ((void) key_length);
519
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520 return des_setkey_enc( (des_context *) ctx, key );
521}
522
Paul Bakkerfae35f02013-03-13 10:33:51 +0100523static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000525 ((void) key_length);
526
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527 return des3_set2key_dec( (des3_context *) ctx, key );
528}
529
Paul Bakkerfae35f02013-03-13 10:33:51 +0100530static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000532 ((void) key_length);
533
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534 return des3_set2key_enc( (des3_context *) ctx, key );
535}
536
Paul Bakkerfae35f02013-03-13 10:33:51 +0100537static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000539 ((void) key_length);
540
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541 return des3_set3key_dec( (des3_context *) ctx, key );
542}
543
Paul Bakkerfae35f02013-03-13 10:33:51 +0100544static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000545{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000546 ((void) key_length);
547
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548 return des3_set3key_enc( (des3_context *) ctx, key );
549}
550
551static void * des_ctx_alloc( void )
552{
Paul Bakker6e339b52013-07-03 13:37:05 +0200553 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554}
555
556static void * des3_ctx_alloc( void )
557{
Paul Bakker6e339b52013-07-03 13:37:05 +0200558 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559}
560
561static void des_ctx_free( void *ctx )
562{
Paul Bakker6e339b52013-07-03 13:37:05 +0200563 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564}
565
Paul Bakker343a8702011-06-09 14:27:58 +0000566const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000567 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000568 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000569 des_crypt_cfb128_wrap,
570 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200571 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000572 des_setkey_enc_wrap,
573 des_setkey_dec_wrap,
574 des_ctx_alloc,
575 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576};
577
Paul Bakker343a8702011-06-09 14:27:58 +0000578const cipher_info_t des_cbc_info = {
579 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000580 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000581 POLARSSL_KEY_LENGTH_DES,
582 "DES-CBC",
583 8,
584 8,
585 &des_info
586};
587
588const cipher_base_t des_ede_info = {
589 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000590 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000591 des_crypt_cfb128_wrap,
592 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200593 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000594 des3_set2key_enc_wrap,
595 des3_set2key_dec_wrap,
596 des3_ctx_alloc,
597 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598};
599
Paul Bakker343a8702011-06-09 14:27:58 +0000600const cipher_info_t des_ede_cbc_info = {
601 POLARSSL_CIPHER_DES_EDE_CBC,
602 POLARSSL_MODE_CBC,
603 POLARSSL_KEY_LENGTH_DES_EDE,
604 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200605 8,
606 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000607 &des_ede_info
608};
609
610const cipher_base_t des_ede3_info = {
611 POLARSSL_CIPHER_ID_DES,
612 des3_crypt_cbc_wrap,
613 des_crypt_cfb128_wrap,
614 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200615 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000616 des3_set3key_enc_wrap,
617 des3_set3key_dec_wrap,
618 des3_ctx_alloc,
619 des_ctx_free
620};
621
Paul Bakker8123e9d2011-01-06 15:37:30 +0000622const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000623 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000624 POLARSSL_MODE_CBC,
625 POLARSSL_KEY_LENGTH_DES_EDE3,
626 "DES-EDE3-CBC",
627 8,
628 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000629 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630};
631#endif
632
Paul Bakker6132d0a2012-07-04 17:10:40 +0000633#if defined(POLARSSL_BLOWFISH_C)
634
Paul Bakkerfae35f02013-03-13 10:33:51 +0100635static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000636 unsigned char *iv, const unsigned char *input, unsigned char *output )
637{
638 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
639}
640
Paul Bakkerfae35f02013-03-13 10:33:51 +0100641static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000642 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
643{
644#if defined(POLARSSL_CIPHER_MODE_CFB)
645 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
646#else
647 ((void) ctx);
648 ((void) operation);
649 ((void) length);
650 ((void) iv_off);
651 ((void) iv);
652 ((void) input);
653 ((void) output);
654
655 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
656#endif
657}
658
Paul Bakkerfae35f02013-03-13 10:33:51 +0100659static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000660 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
661 const unsigned char *input, unsigned char *output )
662{
663#if defined(POLARSSL_CIPHER_MODE_CTR)
664 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
665 stream_block, input, output );
666#else
667 ((void) ctx);
668 ((void) length);
669 ((void) nc_off);
670 ((void) nonce_counter);
671 ((void) stream_block);
672 ((void) input);
673 ((void) output);
674
675 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
676#endif
677}
678
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200679static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000680{
681 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
682}
683
684static void * blowfish_ctx_alloc( void )
685{
Paul Bakker6e339b52013-07-03 13:37:05 +0200686 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000687}
688
689static void blowfish_ctx_free( void *ctx )
690{
Paul Bakker6e339b52013-07-03 13:37:05 +0200691 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000692}
693
694const cipher_base_t blowfish_info = {
695 POLARSSL_CIPHER_ID_BLOWFISH,
696 blowfish_crypt_cbc_wrap,
697 blowfish_crypt_cfb64_wrap,
698 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200699 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200700 blowfish_setkey_wrap,
701 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000702 blowfish_ctx_alloc,
703 blowfish_ctx_free
704};
705
706const cipher_info_t blowfish_cbc_info = {
707 POLARSSL_CIPHER_BLOWFISH_CBC,
708 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200709 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000710 "BLOWFISH-CBC",
711 8,
712 8,
713 &blowfish_info
714};
715
716#if defined(POLARSSL_CIPHER_MODE_CFB)
717const cipher_info_t blowfish_cfb64_info = {
718 POLARSSL_CIPHER_BLOWFISH_CFB64,
719 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200720 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000721 "BLOWFISH-CFB64",
722 8,
723 8,
724 &blowfish_info
725};
726#endif /* POLARSSL_CIPHER_MODE_CFB */
727
728#if defined(POLARSSL_CIPHER_MODE_CTR)
729const cipher_info_t blowfish_ctr_info = {
730 POLARSSL_CIPHER_BLOWFISH_CTR,
731 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200732 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000733 "BLOWFISH-CTR",
734 8,
735 8,
736 &blowfish_info
737};
738#endif /* POLARSSL_CIPHER_MODE_CTR */
739#endif /* POLARSSL_BLOWFISH_C */
740
Paul Bakker68884e32013-01-07 18:20:04 +0100741#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200742static int arc4_crypt_stream_wrap( void *ctx, size_t length,
743 const unsigned char *input,
744 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +0100745{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200746 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100747}
748
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200749static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
750 unsigned int key_length )
751{
752 arc4_setup( (arc4_context *) ctx, key, key_length );
753 return( 0 );
754}
755
756static void * arc4_ctx_alloc( void )
757{
758 return polarssl_malloc( sizeof( arc4_context ) );
759}
Paul Bakker68884e32013-01-07 18:20:04 +0100760
761static void arc4_ctx_free( void *ctx )
762{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200763 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +0100764}
765
766const cipher_base_t arc4_base_info = {
767 POLARSSL_CIPHER_ID_ARC4,
768 NULL,
769 NULL,
770 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200771 arc4_crypt_stream_wrap,
772 arc4_setkey_wrap,
773 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +0100774 arc4_ctx_alloc,
775 arc4_ctx_free
776};
777
778const cipher_info_t arc4_128_info = {
779 POLARSSL_CIPHER_ARC4_128,
780 POLARSSL_MODE_STREAM,
781 128,
782 "ARC4-128",
783 0,
784 1,
785 &arc4_base_info
786};
787#endif /* POLARSSL_ARC4_C */
788
Paul Bakkerfab5c822012-02-06 16:45:10 +0000789#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200790static int null_crypt_stream( void *ctx, size_t length,
791 const unsigned char *input,
792 unsigned char *output )
793{
794 ((void) ctx);
795 memmove( output, input, length );
796 return( 0 );
797}
798
799static int null_setkey( void *ctx, const unsigned char *key,
800 unsigned int key_length )
801{
802 ((void) ctx);
803 ((void) key);
804 ((void) key_length);
805
806 return( 0 );
807}
808
Paul Bakkerfab5c822012-02-06 16:45:10 +0000809static void * null_ctx_alloc( void )
810{
811 return (void *) 1;
812}
813
Paul Bakkerfab5c822012-02-06 16:45:10 +0000814static void null_ctx_free( void *ctx )
815{
816 ((void) ctx);
817}
818
819const cipher_base_t null_base_info = {
820 POLARSSL_CIPHER_ID_NULL,
821 NULL,
822 NULL,
823 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200824 null_crypt_stream,
825 null_setkey,
826 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000827 null_ctx_alloc,
828 null_ctx_free
829};
830
831const cipher_info_t null_cipher_info = {
832 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200833 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000834 0,
835 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +0100836 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000837 1,
838 &null_base_info
839};
840#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
841
Paul Bakker8123e9d2011-01-06 15:37:30 +0000842#endif