blob: 562f8b34b4875bcdcad40340ad5953f6bddfedeb [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
Paul Bakker6e339b52013-07-03 13:37:05 +020056#if defined(POLARSSL_MEMORY_C)
57#include "polarssl/memory.h"
58#else
59#define polarssl_malloc malloc
60#define polarssl_free free
61#endif
62
Paul Bakker8123e9d2011-01-06 15:37:30 +000063#include <stdlib.h>
64
65#if defined(POLARSSL_AES_C)
66
Paul Bakkerfae35f02013-03-13 10:33:51 +010067static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000068 unsigned char *iv, const unsigned char *input, unsigned char *output )
69{
70 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
71}
72
Paul Bakkerfae35f02013-03-13 10:33:51 +010073static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000074 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
75{
76#if defined(POLARSSL_CIPHER_MODE_CFB)
77 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
78#else
79 ((void) ctx);
80 ((void) operation);
81 ((void) length);
82 ((void) iv_off);
83 ((void) iv);
84 ((void) input);
85 ((void) output);
86
87 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
88#endif
89}
90
Paul Bakkerfae35f02013-03-13 10:33:51 +010091static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000092 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
93 const unsigned char *input, unsigned char *output )
94{
95#if defined(POLARSSL_CIPHER_MODE_CTR)
96 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
97 stream_block, input, output );
98#else
99 ((void) ctx);
100 ((void) length);
101 ((void) nc_off);
102 ((void) nonce_counter);
103 ((void) stream_block);
104 ((void) input);
105 ((void) output);
106
107 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
108#endif
109}
110
Paul Bakkerfae35f02013-03-13 10:33:51 +0100111static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000112{
113 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
114}
115
Paul Bakkerfae35f02013-03-13 10:33:51 +0100116static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000117{
118 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
119}
120
121static void * aes_ctx_alloc( void )
122{
Paul Bakker6e339b52013-07-03 13:37:05 +0200123 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000124}
125
126static void aes_ctx_free( void *ctx )
127{
Paul Bakker6e339b52013-07-03 13:37:05 +0200128 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000129}
130
Paul Bakker343a8702011-06-09 14:27:58 +0000131const cipher_base_t aes_info = {
132 POLARSSL_CIPHER_ID_AES,
133 aes_crypt_cbc_wrap,
134 aes_crypt_cfb128_wrap,
135 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200136 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000137 aes_setkey_enc_wrap,
138 aes_setkey_dec_wrap,
139 aes_ctx_alloc,
140 aes_ctx_free
141};
142
Paul Bakker8123e9d2011-01-06 15:37:30 +0000143const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000144 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000145 POLARSSL_MODE_CBC,
146 128,
147 "AES-128-CBC",
148 16,
149 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000150 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000151};
152
153const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000154 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000155 POLARSSL_MODE_CBC,
156 192,
157 "AES-192-CBC",
158 16,
159 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000160 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000161};
162
163const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000164 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000165 POLARSSL_MODE_CBC,
166 256,
167 "AES-256-CBC",
168 16,
169 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000170 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000171};
Paul Bakker343a8702011-06-09 14:27:58 +0000172
173#if defined(POLARSSL_CIPHER_MODE_CFB)
174const cipher_info_t aes_128_cfb128_info = {
175 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000176 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000177 128,
178 "AES-128-CFB128",
179 16,
180 16,
181 &aes_info
182};
183
184const cipher_info_t aes_192_cfb128_info = {
185 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000186 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000187 192,
188 "AES-192-CFB128",
189 16,
190 16,
191 &aes_info
192};
193
194const cipher_info_t aes_256_cfb128_info = {
195 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000196 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000197 256,
198 "AES-256-CFB128",
199 16,
200 16,
201 &aes_info
202};
203#endif /* POLARSSL_CIPHER_MODE_CFB */
204
205#if defined(POLARSSL_CIPHER_MODE_CTR)
206const cipher_info_t aes_128_ctr_info = {
207 POLARSSL_CIPHER_AES_128_CTR,
208 POLARSSL_MODE_CTR,
209 128,
210 "AES-128-CTR",
211 16,
212 16,
213 &aes_info
214};
215
216const cipher_info_t aes_192_ctr_info = {
217 POLARSSL_CIPHER_AES_192_CTR,
218 POLARSSL_MODE_CTR,
219 192,
220 "AES-192-CTR",
221 16,
222 16,
223 &aes_info
224};
225
226const cipher_info_t aes_256_ctr_info = {
227 POLARSSL_CIPHER_AES_256_CTR,
228 POLARSSL_MODE_CTR,
229 256,
230 "AES-256-CTR",
231 16,
232 16,
233 &aes_info
234};
235#endif /* POLARSSL_CIPHER_MODE_CTR */
236
Paul Bakker68884e32013-01-07 18:20:04 +0100237#if defined(POLARSSL_GCM_C)
238const cipher_info_t aes_128_gcm_info = {
239 POLARSSL_CIPHER_AES_128_GCM,
240 POLARSSL_MODE_GCM,
241 128,
242 "AES-128-GCM",
243 16,
244 16,
245 &aes_info
246};
247
248const cipher_info_t aes_256_gcm_info = {
249 POLARSSL_CIPHER_AES_256_GCM,
250 POLARSSL_MODE_GCM,
251 256,
252 "AES-256-GCM",
253 16,
254 16,
255 &aes_info
256};
257#endif /* POLARSSL_GCM_C */
258
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259#endif
260
261#if defined(POLARSSL_CAMELLIA_C)
262
Paul Bakkerfae35f02013-03-13 10:33:51 +0100263static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000264 unsigned char *iv, const unsigned char *input, unsigned char *output )
265{
266 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
267}
268
Paul Bakkerfae35f02013-03-13 10:33:51 +0100269static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000270 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
271{
272#if defined(POLARSSL_CIPHER_MODE_CFB)
273 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
274#else
275 ((void) ctx);
276 ((void) operation);
277 ((void) length);
278 ((void) iv_off);
279 ((void) iv);
280 ((void) input);
281 ((void) output);
282
283 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
284#endif
285}
286
Paul Bakkerfae35f02013-03-13 10:33:51 +0100287static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000288 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
289 const unsigned char *input, unsigned char *output )
290{
291#if defined(POLARSSL_CIPHER_MODE_CTR)
292 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
293 stream_block, input, output );
294#else
295 ((void) ctx);
296 ((void) length);
297 ((void) nc_off);
298 ((void) nonce_counter);
299 ((void) stream_block);
300 ((void) input);
301 ((void) output);
302
303 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
304#endif
305}
306
Paul Bakkerfae35f02013-03-13 10:33:51 +0100307static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308{
309 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
310}
311
Paul Bakkerfae35f02013-03-13 10:33:51 +0100312static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000313{
314 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
315}
316
317static void * camellia_ctx_alloc( void )
318{
Paul Bakker6e339b52013-07-03 13:37:05 +0200319 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320}
321
322static void camellia_ctx_free( void *ctx )
323{
Paul Bakker6e339b52013-07-03 13:37:05 +0200324 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000325}
326
Paul Bakker343a8702011-06-09 14:27:58 +0000327const cipher_base_t camellia_info = {
328 POLARSSL_CIPHER_ID_CAMELLIA,
329 camellia_crypt_cbc_wrap,
330 camellia_crypt_cfb128_wrap,
331 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200332 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000333 camellia_setkey_enc_wrap,
334 camellia_setkey_dec_wrap,
335 camellia_ctx_alloc,
336 camellia_ctx_free
337};
338
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000340 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000341 POLARSSL_MODE_CBC,
342 128,
343 "CAMELLIA-128-CBC",
344 16,
345 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000346 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347};
348
349const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000350 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000351 POLARSSL_MODE_CBC,
352 192,
353 "CAMELLIA-192-CBC",
354 16,
355 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000356 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357};
358
359const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000360 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000361 POLARSSL_MODE_CBC,
362 256,
363 "CAMELLIA-256-CBC",
364 16,
365 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000366 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367};
Paul Bakker343a8702011-06-09 14:27:58 +0000368
369#if defined(POLARSSL_CIPHER_MODE_CFB)
370const cipher_info_t camellia_128_cfb128_info = {
371 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000372 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000373 128,
374 "CAMELLIA-128-CFB128",
375 16,
376 16,
377 &camellia_info
378};
379
380const cipher_info_t camellia_192_cfb128_info = {
381 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000382 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000383 192,
384 "CAMELLIA-192-CFB128",
385 16,
386 16,
387 &camellia_info
388};
389
390const cipher_info_t camellia_256_cfb128_info = {
391 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000392 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000393 256,
394 "CAMELLIA-256-CFB128",
395 16,
396 16,
397 &camellia_info
398};
399#endif /* POLARSSL_CIPHER_MODE_CFB */
400
401#if defined(POLARSSL_CIPHER_MODE_CTR)
402const cipher_info_t camellia_128_ctr_info = {
403 POLARSSL_CIPHER_CAMELLIA_128_CTR,
404 POLARSSL_MODE_CTR,
405 128,
406 "CAMELLIA-128-CTR",
407 16,
408 16,
409 &camellia_info
410};
411
412const cipher_info_t camellia_192_ctr_info = {
413 POLARSSL_CIPHER_CAMELLIA_192_CTR,
414 POLARSSL_MODE_CTR,
415 192,
416 "CAMELLIA-192-CTR",
417 16,
418 16,
419 &camellia_info
420};
421
422const cipher_info_t camellia_256_ctr_info = {
423 POLARSSL_CIPHER_CAMELLIA_256_CTR,
424 POLARSSL_MODE_CTR,
425 256,
426 "CAMELLIA-256-CTR",
427 16,
428 16,
429 &camellia_info
430};
431#endif /* POLARSSL_CIPHER_MODE_CTR */
432
Paul Bakker8123e9d2011-01-06 15:37:30 +0000433#endif
434
435#if defined(POLARSSL_DES_C)
436
Paul Bakkerfae35f02013-03-13 10:33:51 +0100437static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438 unsigned char *iv, const unsigned char *input, unsigned char *output )
439{
440 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
441}
442
Paul Bakkerfae35f02013-03-13 10:33:51 +0100443static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 unsigned char *iv, const unsigned char *input, unsigned char *output )
445{
446 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
447}
448
Paul Bakkerfae35f02013-03-13 10:33:51 +0100449static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000450 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
451{
452 ((void) ctx);
453 ((void) operation);
454 ((void) length);
455 ((void) iv_off);
456 ((void) iv);
457 ((void) input);
458 ((void) output);
459
460 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
461}
462
Paul Bakkerfae35f02013-03-13 10:33:51 +0100463static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000464 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
465 const unsigned char *input, unsigned char *output )
466{
467 ((void) ctx);
468 ((void) length);
469 ((void) nc_off);
470 ((void) nonce_counter);
471 ((void) stream_block);
472 ((void) input);
473 ((void) output);
474
475 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
476}
477
Paul Bakkerfae35f02013-03-13 10:33:51 +0100478static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000480 ((void) key_length);
481
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482 return des_setkey_dec( (des_context *) ctx, key );
483}
484
Paul Bakkerfae35f02013-03-13 10:33:51 +0100485static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000487 ((void) key_length);
488
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489 return des_setkey_enc( (des_context *) ctx, key );
490}
491
Paul Bakkerfae35f02013-03-13 10:33:51 +0100492static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000494 ((void) key_length);
495
Paul Bakker8123e9d2011-01-06 15:37:30 +0000496 return des3_set2key_dec( (des3_context *) ctx, key );
497}
498
Paul Bakkerfae35f02013-03-13 10:33:51 +0100499static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000501 ((void) key_length);
502
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503 return des3_set2key_enc( (des3_context *) ctx, key );
504}
505
Paul Bakkerfae35f02013-03-13 10:33:51 +0100506static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000507{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000508 ((void) key_length);
509
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510 return des3_set3key_dec( (des3_context *) ctx, key );
511}
512
Paul Bakkerfae35f02013-03-13 10:33:51 +0100513static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000514{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000515 ((void) key_length);
516
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517 return des3_set3key_enc( (des3_context *) ctx, key );
518}
519
520static void * des_ctx_alloc( void )
521{
Paul Bakker6e339b52013-07-03 13:37:05 +0200522 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000523}
524
525static void * des3_ctx_alloc( void )
526{
Paul Bakker6e339b52013-07-03 13:37:05 +0200527 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000528}
529
530static void des_ctx_free( void *ctx )
531{
Paul Bakker6e339b52013-07-03 13:37:05 +0200532 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000533}
534
Paul Bakker343a8702011-06-09 14:27:58 +0000535const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000536 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000537 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000538 des_crypt_cfb128_wrap,
539 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200540 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000541 des_setkey_enc_wrap,
542 des_setkey_dec_wrap,
543 des_ctx_alloc,
544 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000545};
546
Paul Bakker343a8702011-06-09 14:27:58 +0000547const cipher_info_t des_cbc_info = {
548 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000549 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000550 POLARSSL_KEY_LENGTH_DES,
551 "DES-CBC",
552 8,
553 8,
554 &des_info
555};
556
557const cipher_base_t des_ede_info = {
558 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000559 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000560 des_crypt_cfb128_wrap,
561 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200562 NULL,
Paul Bakker23986e52011-04-24 08:57:21 +0000563 des3_set2key_enc_wrap,
564 des3_set2key_dec_wrap,
565 des3_ctx_alloc,
566 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567};
568
Paul Bakker343a8702011-06-09 14:27:58 +0000569const cipher_info_t des_ede_cbc_info = {
570 POLARSSL_CIPHER_DES_EDE_CBC,
571 POLARSSL_MODE_CBC,
572 POLARSSL_KEY_LENGTH_DES_EDE,
573 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200574 8,
575 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000576 &des_ede_info
577};
578
579const cipher_base_t des_ede3_info = {
580 POLARSSL_CIPHER_ID_DES,
581 des3_crypt_cbc_wrap,
582 des_crypt_cfb128_wrap,
583 des_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200584 NULL,
Paul Bakker343a8702011-06-09 14:27:58 +0000585 des3_set3key_enc_wrap,
586 des3_set3key_dec_wrap,
587 des3_ctx_alloc,
588 des_ctx_free
589};
590
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000592 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000593 POLARSSL_MODE_CBC,
594 POLARSSL_KEY_LENGTH_DES_EDE3,
595 "DES-EDE3-CBC",
596 8,
597 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000598 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599};
600#endif
601
Paul Bakker6132d0a2012-07-04 17:10:40 +0000602#if defined(POLARSSL_BLOWFISH_C)
603
Paul Bakkerfae35f02013-03-13 10:33:51 +0100604static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000605 unsigned char *iv, const unsigned char *input, unsigned char *output )
606{
607 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
608}
609
Paul Bakkerfae35f02013-03-13 10:33:51 +0100610static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000611 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
612{
613#if defined(POLARSSL_CIPHER_MODE_CFB)
614 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
615#else
616 ((void) ctx);
617 ((void) operation);
618 ((void) length);
619 ((void) iv_off);
620 ((void) iv);
621 ((void) input);
622 ((void) output);
623
624 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
625#endif
626}
627
Paul Bakkerfae35f02013-03-13 10:33:51 +0100628static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000629 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
630 const unsigned char *input, unsigned char *output )
631{
632#if defined(POLARSSL_CIPHER_MODE_CTR)
633 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
634 stream_block, input, output );
635#else
636 ((void) ctx);
637 ((void) length);
638 ((void) nc_off);
639 ((void) nonce_counter);
640 ((void) stream_block);
641 ((void) input);
642 ((void) output);
643
644 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
645#endif
646}
647
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200648static int blowfish_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000649{
650 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
651}
652
653static void * blowfish_ctx_alloc( void )
654{
Paul Bakker6e339b52013-07-03 13:37:05 +0200655 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000656}
657
658static void blowfish_ctx_free( void *ctx )
659{
Paul Bakker6e339b52013-07-03 13:37:05 +0200660 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000661}
662
663const cipher_base_t blowfish_info = {
664 POLARSSL_CIPHER_ID_BLOWFISH,
665 blowfish_crypt_cbc_wrap,
666 blowfish_crypt_cfb64_wrap,
667 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200668 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200669 blowfish_setkey_wrap,
670 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000671 blowfish_ctx_alloc,
672 blowfish_ctx_free
673};
674
675const cipher_info_t blowfish_cbc_info = {
676 POLARSSL_CIPHER_BLOWFISH_CBC,
677 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200678 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000679 "BLOWFISH-CBC",
680 8,
681 8,
682 &blowfish_info
683};
684
685#if defined(POLARSSL_CIPHER_MODE_CFB)
686const cipher_info_t blowfish_cfb64_info = {
687 POLARSSL_CIPHER_BLOWFISH_CFB64,
688 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200689 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000690 "BLOWFISH-CFB64",
691 8,
692 8,
693 &blowfish_info
694};
695#endif /* POLARSSL_CIPHER_MODE_CFB */
696
697#if defined(POLARSSL_CIPHER_MODE_CTR)
698const cipher_info_t blowfish_ctr_info = {
699 POLARSSL_CIPHER_BLOWFISH_CTR,
700 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200701 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000702 "BLOWFISH-CTR",
703 8,
704 8,
705 &blowfish_info
706};
707#endif /* POLARSSL_CIPHER_MODE_CTR */
708#endif /* POLARSSL_BLOWFISH_C */
709
Paul Bakker68884e32013-01-07 18:20:04 +0100710#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200711static int arc4_crypt_stream_wrap( void *ctx, size_t length,
712 const unsigned char *input,
713 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +0100714{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200715 return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +0100716}
717
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200718static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
719 unsigned int key_length )
720{
721 arc4_setup( (arc4_context *) ctx, key, key_length );
722 return( 0 );
723}
724
725static void * arc4_ctx_alloc( void )
726{
727 return polarssl_malloc( sizeof( arc4_context ) );
728}
Paul Bakker68884e32013-01-07 18:20:04 +0100729
730static void arc4_ctx_free( void *ctx )
731{
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200732 polarssl_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +0100733}
734
735const cipher_base_t arc4_base_info = {
736 POLARSSL_CIPHER_ID_ARC4,
737 NULL,
738 NULL,
739 NULL,
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740 arc4_crypt_stream_wrap,
741 arc4_setkey_wrap,
742 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +0100743 arc4_ctx_alloc,
744 arc4_ctx_free
745};
746
747const cipher_info_t arc4_128_info = {
748 POLARSSL_CIPHER_ARC4_128,
749 POLARSSL_MODE_STREAM,
750 128,
751 "ARC4-128",
752 0,
753 1,
754 &arc4_base_info
755};
756#endif /* POLARSSL_ARC4_C */
757
Paul Bakkerfab5c822012-02-06 16:45:10 +0000758#if defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200759static int null_crypt_stream( void *ctx, size_t length,
760 const unsigned char *input,
761 unsigned char *output )
762{
763 ((void) ctx);
764 memmove( output, input, length );
765 return( 0 );
766}
767
768static int null_setkey( void *ctx, const unsigned char *key,
769 unsigned int key_length )
770{
771 ((void) ctx);
772 ((void) key);
773 ((void) key_length);
774
775 return( 0 );
776}
777
Paul Bakkerfab5c822012-02-06 16:45:10 +0000778static void * null_ctx_alloc( void )
779{
780 return (void *) 1;
781}
782
Paul Bakkerfab5c822012-02-06 16:45:10 +0000783static void null_ctx_free( void *ctx )
784{
785 ((void) ctx);
786}
787
788const cipher_base_t null_base_info = {
789 POLARSSL_CIPHER_ID_NULL,
790 NULL,
791 NULL,
792 NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200793 null_crypt_stream,
794 null_setkey,
795 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000796 null_ctx_alloc,
797 null_ctx_free
798};
799
800const cipher_info_t null_cipher_info = {
801 POLARSSL_CIPHER_NULL,
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +0200802 POLARSSL_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000803 0,
804 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +0100805 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000806 1,
807 &null_base_info
808};
809#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
810
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811#endif