blob: baff2aac3ef9d2f520b90d9583431e310187ace0 [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
40#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000041#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
44#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000045#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000046#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000047
Paul Bakker6132d0a2012-07-04 17:10:40 +000048#if defined(POLARSSL_BLOWFISH_C)
49#include "polarssl/blowfish.h"
50#endif
51
Paul Bakker6e339b52013-07-03 13:37:05 +020052#if defined(POLARSSL_MEMORY_C)
53#include "polarssl/memory.h"
54#else
55#define polarssl_malloc malloc
56#define polarssl_free free
57#endif
58
Paul Bakker8123e9d2011-01-06 15:37:30 +000059#include <stdlib.h>
60
61#if defined(POLARSSL_AES_C)
62
Paul Bakkerfae35f02013-03-13 10:33:51 +010063static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000064 unsigned char *iv, const unsigned char *input, unsigned char *output )
65{
66 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
67}
68
Paul Bakkerfae35f02013-03-13 10:33:51 +010069static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000070 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
71{
72#if defined(POLARSSL_CIPHER_MODE_CFB)
73 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
74#else
75 ((void) ctx);
76 ((void) operation);
77 ((void) length);
78 ((void) iv_off);
79 ((void) iv);
80 ((void) input);
81 ((void) output);
82
83 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
84#endif
85}
86
Paul Bakkerfae35f02013-03-13 10:33:51 +010087static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000088 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
89 const unsigned char *input, unsigned char *output )
90{
91#if defined(POLARSSL_CIPHER_MODE_CTR)
92 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
93 stream_block, input, output );
94#else
95 ((void) ctx);
96 ((void) length);
97 ((void) nc_off);
98 ((void) nonce_counter);
99 ((void) stream_block);
100 ((void) input);
101 ((void) output);
102
103 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
104#endif
105}
106
Paul Bakkerfae35f02013-03-13 10:33:51 +0100107static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108{
109 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
110}
111
Paul Bakkerfae35f02013-03-13 10:33:51 +0100112static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000113{
114 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
115}
116
117static void * aes_ctx_alloc( void )
118{
Paul Bakker6e339b52013-07-03 13:37:05 +0200119 return polarssl_malloc( sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000120}
121
122static void aes_ctx_free( void *ctx )
123{
Paul Bakker6e339b52013-07-03 13:37:05 +0200124 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125}
126
Paul Bakker343a8702011-06-09 14:27:58 +0000127const cipher_base_t aes_info = {
128 POLARSSL_CIPHER_ID_AES,
129 aes_crypt_cbc_wrap,
130 aes_crypt_cfb128_wrap,
131 aes_crypt_ctr_wrap,
132 aes_setkey_enc_wrap,
133 aes_setkey_dec_wrap,
134 aes_ctx_alloc,
135 aes_ctx_free
136};
137
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000139 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000140 POLARSSL_MODE_CBC,
141 128,
142 "AES-128-CBC",
143 16,
144 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000145 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000146};
147
148const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000149 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000150 POLARSSL_MODE_CBC,
151 192,
152 "AES-192-CBC",
153 16,
154 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000155 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000156};
157
158const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000160 POLARSSL_MODE_CBC,
161 256,
162 "AES-256-CBC",
163 16,
164 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000165 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000166};
Paul Bakker343a8702011-06-09 14:27:58 +0000167
168#if defined(POLARSSL_CIPHER_MODE_CFB)
169const cipher_info_t aes_128_cfb128_info = {
170 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000171 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000172 128,
173 "AES-128-CFB128",
174 16,
175 16,
176 &aes_info
177};
178
179const cipher_info_t aes_192_cfb128_info = {
180 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000181 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000182 192,
183 "AES-192-CFB128",
184 16,
185 16,
186 &aes_info
187};
188
189const cipher_info_t aes_256_cfb128_info = {
190 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000191 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000192 256,
193 "AES-256-CFB128",
194 16,
195 16,
196 &aes_info
197};
198#endif /* POLARSSL_CIPHER_MODE_CFB */
199
200#if defined(POLARSSL_CIPHER_MODE_CTR)
201const cipher_info_t aes_128_ctr_info = {
202 POLARSSL_CIPHER_AES_128_CTR,
203 POLARSSL_MODE_CTR,
204 128,
205 "AES-128-CTR",
206 16,
207 16,
208 &aes_info
209};
210
211const cipher_info_t aes_192_ctr_info = {
212 POLARSSL_CIPHER_AES_192_CTR,
213 POLARSSL_MODE_CTR,
214 192,
215 "AES-192-CTR",
216 16,
217 16,
218 &aes_info
219};
220
221const cipher_info_t aes_256_ctr_info = {
222 POLARSSL_CIPHER_AES_256_CTR,
223 POLARSSL_MODE_CTR,
224 256,
225 "AES-256-CTR",
226 16,
227 16,
228 &aes_info
229};
230#endif /* POLARSSL_CIPHER_MODE_CTR */
231
Paul Bakker68884e32013-01-07 18:20:04 +0100232#if defined(POLARSSL_GCM_C)
233const cipher_info_t aes_128_gcm_info = {
234 POLARSSL_CIPHER_AES_128_GCM,
235 POLARSSL_MODE_GCM,
236 128,
237 "AES-128-GCM",
238 16,
239 16,
240 &aes_info
241};
242
243const cipher_info_t aes_256_gcm_info = {
244 POLARSSL_CIPHER_AES_256_GCM,
245 POLARSSL_MODE_GCM,
246 256,
247 "AES-256-GCM",
248 16,
249 16,
250 &aes_info
251};
252#endif /* POLARSSL_GCM_C */
253
Paul Bakker8123e9d2011-01-06 15:37:30 +0000254#endif
255
256#if defined(POLARSSL_CAMELLIA_C)
257
Paul Bakkerfae35f02013-03-13 10:33:51 +0100258static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000259 unsigned char *iv, const unsigned char *input, unsigned char *output )
260{
261 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
262}
263
Paul Bakkerfae35f02013-03-13 10:33:51 +0100264static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000265 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
266{
267#if defined(POLARSSL_CIPHER_MODE_CFB)
268 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
269#else
270 ((void) ctx);
271 ((void) operation);
272 ((void) length);
273 ((void) iv_off);
274 ((void) iv);
275 ((void) input);
276 ((void) output);
277
278 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
279#endif
280}
281
Paul Bakkerfae35f02013-03-13 10:33:51 +0100282static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000283 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
284 const unsigned char *input, unsigned char *output )
285{
286#if defined(POLARSSL_CIPHER_MODE_CTR)
287 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
288 stream_block, input, output );
289#else
290 ((void) ctx);
291 ((void) length);
292 ((void) nc_off);
293 ((void) nonce_counter);
294 ((void) stream_block);
295 ((void) input);
296 ((void) output);
297
298 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
299#endif
300}
301
Paul Bakkerfae35f02013-03-13 10:33:51 +0100302static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000303{
304 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
305}
306
Paul Bakkerfae35f02013-03-13 10:33:51 +0100307static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308{
309 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
310}
311
312static void * camellia_ctx_alloc( void )
313{
Paul Bakker6e339b52013-07-03 13:37:05 +0200314 return polarssl_malloc( sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315}
316
317static void camellia_ctx_free( void *ctx )
318{
Paul Bakker6e339b52013-07-03 13:37:05 +0200319 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320}
321
Paul Bakker343a8702011-06-09 14:27:58 +0000322const cipher_base_t camellia_info = {
323 POLARSSL_CIPHER_ID_CAMELLIA,
324 camellia_crypt_cbc_wrap,
325 camellia_crypt_cfb128_wrap,
326 camellia_crypt_ctr_wrap,
327 camellia_setkey_enc_wrap,
328 camellia_setkey_dec_wrap,
329 camellia_ctx_alloc,
330 camellia_ctx_free
331};
332
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000334 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000335 POLARSSL_MODE_CBC,
336 128,
337 "CAMELLIA-128-CBC",
338 16,
339 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000340 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000341};
342
343const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000344 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000345 POLARSSL_MODE_CBC,
346 192,
347 "CAMELLIA-192-CBC",
348 16,
349 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000350 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351};
352
353const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000354 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000355 POLARSSL_MODE_CBC,
356 256,
357 "CAMELLIA-256-CBC",
358 16,
359 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000360 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000361};
Paul Bakker343a8702011-06-09 14:27:58 +0000362
363#if defined(POLARSSL_CIPHER_MODE_CFB)
364const cipher_info_t camellia_128_cfb128_info = {
365 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000366 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000367 128,
368 "CAMELLIA-128-CFB128",
369 16,
370 16,
371 &camellia_info
372};
373
374const cipher_info_t camellia_192_cfb128_info = {
375 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000376 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000377 192,
378 "CAMELLIA-192-CFB128",
379 16,
380 16,
381 &camellia_info
382};
383
384const cipher_info_t camellia_256_cfb128_info = {
385 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000386 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000387 256,
388 "CAMELLIA-256-CFB128",
389 16,
390 16,
391 &camellia_info
392};
393#endif /* POLARSSL_CIPHER_MODE_CFB */
394
395#if defined(POLARSSL_CIPHER_MODE_CTR)
396const cipher_info_t camellia_128_ctr_info = {
397 POLARSSL_CIPHER_CAMELLIA_128_CTR,
398 POLARSSL_MODE_CTR,
399 128,
400 "CAMELLIA-128-CTR",
401 16,
402 16,
403 &camellia_info
404};
405
406const cipher_info_t camellia_192_ctr_info = {
407 POLARSSL_CIPHER_CAMELLIA_192_CTR,
408 POLARSSL_MODE_CTR,
409 192,
410 "CAMELLIA-192-CTR",
411 16,
412 16,
413 &camellia_info
414};
415
416const cipher_info_t camellia_256_ctr_info = {
417 POLARSSL_CIPHER_CAMELLIA_256_CTR,
418 POLARSSL_MODE_CTR,
419 256,
420 "CAMELLIA-256-CTR",
421 16,
422 16,
423 &camellia_info
424};
425#endif /* POLARSSL_CIPHER_MODE_CTR */
426
Paul Bakker8123e9d2011-01-06 15:37:30 +0000427#endif
428
429#if defined(POLARSSL_DES_C)
430
Paul Bakkerfae35f02013-03-13 10:33:51 +0100431static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000432 unsigned char *iv, const unsigned char *input, unsigned char *output )
433{
434 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
435}
436
Paul Bakkerfae35f02013-03-13 10:33:51 +0100437static int des3_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 des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
441}
442
Paul Bakkerfae35f02013-03-13 10:33:51 +0100443static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000444 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
445{
446 ((void) ctx);
447 ((void) operation);
448 ((void) length);
449 ((void) iv_off);
450 ((void) iv);
451 ((void) input);
452 ((void) output);
453
454 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
455}
456
Paul Bakkerfae35f02013-03-13 10:33:51 +0100457static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000458 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
459 const unsigned char *input, unsigned char *output )
460{
461 ((void) ctx);
462 ((void) length);
463 ((void) nc_off);
464 ((void) nonce_counter);
465 ((void) stream_block);
466 ((void) input);
467 ((void) output);
468
469 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
470}
471
Paul Bakkerfae35f02013-03-13 10:33:51 +0100472static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000474 ((void) key_length);
475
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476 return des_setkey_dec( (des_context *) ctx, key );
477}
478
Paul Bakkerfae35f02013-03-13 10:33:51 +0100479static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000481 ((void) key_length);
482
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483 return des_setkey_enc( (des_context *) ctx, key );
484}
485
Paul Bakkerfae35f02013-03-13 10:33:51 +0100486static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000488 ((void) key_length);
489
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490 return des3_set2key_dec( (des3_context *) ctx, key );
491}
492
Paul Bakkerfae35f02013-03-13 10:33:51 +0100493static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000495 ((void) key_length);
496
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497 return des3_set2key_enc( (des3_context *) ctx, key );
498}
499
Paul Bakkerfae35f02013-03-13 10:33:51 +0100500static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000502 ((void) key_length);
503
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504 return des3_set3key_dec( (des3_context *) ctx, key );
505}
506
Paul Bakkerfae35f02013-03-13 10:33:51 +0100507static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000509 ((void) key_length);
510
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511 return des3_set3key_enc( (des3_context *) ctx, key );
512}
513
514static void * des_ctx_alloc( void )
515{
Paul Bakker6e339b52013-07-03 13:37:05 +0200516 return polarssl_malloc( sizeof( des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517}
518
519static void * des3_ctx_alloc( void )
520{
Paul Bakker6e339b52013-07-03 13:37:05 +0200521 return polarssl_malloc( sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000522}
523
524static void des_ctx_free( void *ctx )
525{
Paul Bakker6e339b52013-07-03 13:37:05 +0200526 polarssl_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000527}
528
Paul Bakker343a8702011-06-09 14:27:58 +0000529const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000530 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000531 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000532 des_crypt_cfb128_wrap,
533 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000534 des_setkey_enc_wrap,
535 des_setkey_dec_wrap,
536 des_ctx_alloc,
537 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000538};
539
Paul Bakker343a8702011-06-09 14:27:58 +0000540const cipher_info_t des_cbc_info = {
541 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000542 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000543 POLARSSL_KEY_LENGTH_DES,
544 "DES-CBC",
545 8,
546 8,
547 &des_info
548};
549
550const cipher_base_t des_ede_info = {
551 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000552 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000553 des_crypt_cfb128_wrap,
554 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000555 des3_set2key_enc_wrap,
556 des3_set2key_dec_wrap,
557 des3_ctx_alloc,
558 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559};
560
Paul Bakker343a8702011-06-09 14:27:58 +0000561const cipher_info_t des_ede_cbc_info = {
562 POLARSSL_CIPHER_DES_EDE_CBC,
563 POLARSSL_MODE_CBC,
564 POLARSSL_KEY_LENGTH_DES_EDE,
565 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +0200566 8,
567 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000568 &des_ede_info
569};
570
571const cipher_base_t des_ede3_info = {
572 POLARSSL_CIPHER_ID_DES,
573 des3_crypt_cbc_wrap,
574 des_crypt_cfb128_wrap,
575 des_crypt_ctr_wrap,
576 des3_set3key_enc_wrap,
577 des3_set3key_dec_wrap,
578 des3_ctx_alloc,
579 des_ctx_free
580};
581
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000583 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000584 POLARSSL_MODE_CBC,
585 POLARSSL_KEY_LENGTH_DES_EDE3,
586 "DES-EDE3-CBC",
587 8,
588 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000589 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590};
591#endif
592
Paul Bakker6132d0a2012-07-04 17:10:40 +0000593#if defined(POLARSSL_BLOWFISH_C)
594
Paul Bakkerfae35f02013-03-13 10:33:51 +0100595static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000596 unsigned char *iv, const unsigned char *input, unsigned char *output )
597{
598 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
599}
600
Paul Bakkerfae35f02013-03-13 10:33:51 +0100601static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000602 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
603{
604#if defined(POLARSSL_CIPHER_MODE_CFB)
605 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
606#else
607 ((void) ctx);
608 ((void) operation);
609 ((void) length);
610 ((void) iv_off);
611 ((void) iv);
612 ((void) input);
613 ((void) output);
614
615 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
616#endif
617}
618
Paul Bakkerfae35f02013-03-13 10:33:51 +0100619static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000620 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
621 const unsigned char *input, unsigned char *output )
622{
623#if defined(POLARSSL_CIPHER_MODE_CTR)
624 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
625 stream_block, input, output );
626#else
627 ((void) ctx);
628 ((void) length);
629 ((void) nc_off);
630 ((void) nonce_counter);
631 ((void) stream_block);
632 ((void) input);
633 ((void) output);
634
635 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
636#endif
637}
638
Paul Bakkerfae35f02013-03-13 10:33:51 +0100639static int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000640{
641 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
642}
643
Paul Bakkerfae35f02013-03-13 10:33:51 +0100644static int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000645{
646 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
647}
648
649static void * blowfish_ctx_alloc( void )
650{
Paul Bakker6e339b52013-07-03 13:37:05 +0200651 return polarssl_malloc( sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000652}
653
654static void blowfish_ctx_free( void *ctx )
655{
Paul Bakker6e339b52013-07-03 13:37:05 +0200656 polarssl_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000657}
658
659const cipher_base_t blowfish_info = {
660 POLARSSL_CIPHER_ID_BLOWFISH,
661 blowfish_crypt_cbc_wrap,
662 blowfish_crypt_cfb64_wrap,
663 blowfish_crypt_ctr_wrap,
664 blowfish_setkey_enc_wrap,
665 blowfish_setkey_dec_wrap,
666 blowfish_ctx_alloc,
667 blowfish_ctx_free
668};
669
670const cipher_info_t blowfish_cbc_info = {
671 POLARSSL_CIPHER_BLOWFISH_CBC,
672 POLARSSL_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200673 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000674 "BLOWFISH-CBC",
675 8,
676 8,
677 &blowfish_info
678};
679
680#if defined(POLARSSL_CIPHER_MODE_CFB)
681const cipher_info_t blowfish_cfb64_info = {
682 POLARSSL_CIPHER_BLOWFISH_CFB64,
683 POLARSSL_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200684 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000685 "BLOWFISH-CFB64",
686 8,
687 8,
688 &blowfish_info
689};
690#endif /* POLARSSL_CIPHER_MODE_CFB */
691
692#if defined(POLARSSL_CIPHER_MODE_CTR)
693const cipher_info_t blowfish_ctr_info = {
694 POLARSSL_CIPHER_BLOWFISH_CTR,
695 POLARSSL_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +0200696 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000697 "BLOWFISH-CTR",
698 8,
699 8,
700 &blowfish_info
701};
702#endif /* POLARSSL_CIPHER_MODE_CTR */
703#endif /* POLARSSL_BLOWFISH_C */
704
Paul Bakker68884e32013-01-07 18:20:04 +0100705#if defined(POLARSSL_ARC4_C)
706static void * arc4_ctx_alloc( void )
707{
708 return (void *) 1;
709}
710
711
712static void arc4_ctx_free( void *ctx )
713{
714 ((void) ctx);
715}
716
717const cipher_base_t arc4_base_info = {
718 POLARSSL_CIPHER_ID_ARC4,
719 NULL,
720 NULL,
721 NULL,
722 NULL,
723 NULL,
724 arc4_ctx_alloc,
725 arc4_ctx_free
726};
727
728const cipher_info_t arc4_128_info = {
729 POLARSSL_CIPHER_ARC4_128,
730 POLARSSL_MODE_STREAM,
731 128,
732 "ARC4-128",
733 0,
734 1,
735 &arc4_base_info
736};
737#endif /* POLARSSL_ARC4_C */
738
Paul Bakkerfab5c822012-02-06 16:45:10 +0000739#if defined(POLARSSL_CIPHER_NULL_CIPHER)
740static void * null_ctx_alloc( void )
741{
742 return (void *) 1;
743}
744
745
746static void null_ctx_free( void *ctx )
747{
748 ((void) ctx);
749}
750
751const cipher_base_t null_base_info = {
752 POLARSSL_CIPHER_ID_NULL,
753 NULL,
754 NULL,
755 NULL,
756 NULL,
757 NULL,
758 null_ctx_alloc,
759 null_ctx_free
760};
761
762const cipher_info_t null_cipher_info = {
763 POLARSSL_CIPHER_NULL,
764 POLARSSL_MODE_NULL,
765 0,
766 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +0100767 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +0000768 1,
769 &null_base_info
770};
771#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
772
Paul Bakker8123e9d2011-01-06 15:37:30 +0000773#endif