blob: 833f1d82c31165132929ff544c8ec02aa6128ba9 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
2 * \file md_wrap.c
3 *
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 Bakkerfab5c822012-02-06 16:45:10 +00008 * Copyright (C) 2006-2012, 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 Bakker8123e9d2011-01-06 15:37:30 +000052#include <stdlib.h>
53
Paul Bakker312da332014-06-13 17:20:13 +020054/* Implementation that should never be optimized out by the compiler */
55static void polarssl_zeroize( void *v, size_t n ) {
56 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
57}
58
Paul Bakker8123e9d2011-01-06 15:37:30 +000059#if defined(POLARSSL_AES_C)
60
Paul Bakker1d073c52014-07-08 20:15:51 +020061static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000062 unsigned char *iv, const unsigned char *input, unsigned char *output )
63{
64 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
65}
66
Paul Bakker1d073c52014-07-08 20:15:51 +020067static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000068 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
69{
70#if defined(POLARSSL_CIPHER_MODE_CFB)
71 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
72#else
73 ((void) ctx);
74 ((void) operation);
75 ((void) length);
76 ((void) iv_off);
77 ((void) iv);
78 ((void) input);
79 ((void) output);
80
81 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
82#endif
83}
84
Paul Bakker1d073c52014-07-08 20:15:51 +020085static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000086 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
87 const unsigned char *input, unsigned char *output )
88{
89#if defined(POLARSSL_CIPHER_MODE_CTR)
90 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
91 stream_block, input, output );
92#else
93 ((void) ctx);
94 ((void) length);
95 ((void) nc_off);
96 ((void) nonce_counter);
97 ((void) stream_block);
98 ((void) input);
99 ((void) output);
100
101 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
102#endif
103}
104
Paul Bakker1d073c52014-07-08 20:15:51 +0200105static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106{
107 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
108}
109
Paul Bakker1d073c52014-07-08 20:15:51 +0200110static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000111{
112 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
113}
114
115static void * aes_ctx_alloc( void )
116{
117 return malloc( sizeof( aes_context ) );
118}
119
120static void aes_ctx_free( void *ctx )
121{
Paul Bakker312da332014-06-13 17:20:13 +0200122 polarssl_zeroize( ctx, sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123 free( ctx );
124}
125
Paul Bakker343a8702011-06-09 14:27:58 +0000126const cipher_base_t aes_info = {
127 POLARSSL_CIPHER_ID_AES,
128 aes_crypt_cbc_wrap,
129 aes_crypt_cfb128_wrap,
130 aes_crypt_ctr_wrap,
131 aes_setkey_enc_wrap,
132 aes_setkey_dec_wrap,
133 aes_ctx_alloc,
134 aes_ctx_free
135};
136
Paul Bakker8123e9d2011-01-06 15:37:30 +0000137const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000138 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000139 POLARSSL_MODE_CBC,
140 128,
141 "AES-128-CBC",
142 16,
143 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000144 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145};
146
147const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000148 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 POLARSSL_MODE_CBC,
150 192,
151 "AES-192-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_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000158 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000159 POLARSSL_MODE_CBC,
160 256,
161 "AES-256-CBC",
162 16,
163 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000164 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000165};
Paul Bakker343a8702011-06-09 14:27:58 +0000166
167#if defined(POLARSSL_CIPHER_MODE_CFB)
168const cipher_info_t aes_128_cfb128_info = {
169 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000170 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000171 128,
172 "AES-128-CFB128",
173 16,
174 16,
175 &aes_info
176};
177
178const cipher_info_t aes_192_cfb128_info = {
179 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000180 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000181 192,
182 "AES-192-CFB128",
183 16,
184 16,
185 &aes_info
186};
187
188const cipher_info_t aes_256_cfb128_info = {
189 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000190 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000191 256,
192 "AES-256-CFB128",
193 16,
194 16,
195 &aes_info
196};
197#endif /* POLARSSL_CIPHER_MODE_CFB */
198
199#if defined(POLARSSL_CIPHER_MODE_CTR)
200const cipher_info_t aes_128_ctr_info = {
201 POLARSSL_CIPHER_AES_128_CTR,
202 POLARSSL_MODE_CTR,
203 128,
204 "AES-128-CTR",
205 16,
206 16,
207 &aes_info
208};
209
210const cipher_info_t aes_192_ctr_info = {
211 POLARSSL_CIPHER_AES_192_CTR,
212 POLARSSL_MODE_CTR,
213 192,
214 "AES-192-CTR",
215 16,
216 16,
217 &aes_info
218};
219
220const cipher_info_t aes_256_ctr_info = {
221 POLARSSL_CIPHER_AES_256_CTR,
222 POLARSSL_MODE_CTR,
223 256,
224 "AES-256-CTR",
225 16,
226 16,
227 &aes_info
228};
229#endif /* POLARSSL_CIPHER_MODE_CTR */
230
Paul Bakker8123e9d2011-01-06 15:37:30 +0000231#endif
232
233#if defined(POLARSSL_CAMELLIA_C)
234
Paul Bakker1d073c52014-07-08 20:15:51 +0200235static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000236 unsigned char *iv, const unsigned char *input, unsigned char *output )
237{
238 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
239}
240
Paul Bakker1d073c52014-07-08 20:15:51 +0200241static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000242 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
243{
244#if defined(POLARSSL_CIPHER_MODE_CFB)
245 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
246#else
247 ((void) ctx);
248 ((void) operation);
249 ((void) length);
250 ((void) iv_off);
251 ((void) iv);
252 ((void) input);
253 ((void) output);
254
255 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
256#endif
257}
258
Paul Bakker1d073c52014-07-08 20:15:51 +0200259static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000260 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
261 const unsigned char *input, unsigned char *output )
262{
263#if defined(POLARSSL_CIPHER_MODE_CTR)
264 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
265 stream_block, input, output );
266#else
267 ((void) ctx);
268 ((void) length);
269 ((void) nc_off);
270 ((void) nonce_counter);
271 ((void) stream_block);
272 ((void) input);
273 ((void) output);
274
275 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
276#endif
277}
278
Paul Bakker1d073c52014-07-08 20:15:51 +0200279static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000280{
281 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
282}
283
Paul Bakker1d073c52014-07-08 20:15:51 +0200284static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000285{
286 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
287}
288
289static void * camellia_ctx_alloc( void )
290{
291 return malloc( sizeof( camellia_context ) );
292}
293
294static void camellia_ctx_free( void *ctx )
295{
Paul Bakker312da332014-06-13 17:20:13 +0200296 polarssl_zeroize( ctx, sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000297 free( ctx );
298}
299
Paul Bakker343a8702011-06-09 14:27:58 +0000300const cipher_base_t camellia_info = {
301 POLARSSL_CIPHER_ID_CAMELLIA,
302 camellia_crypt_cbc_wrap,
303 camellia_crypt_cfb128_wrap,
304 camellia_crypt_ctr_wrap,
305 camellia_setkey_enc_wrap,
306 camellia_setkey_dec_wrap,
307 camellia_ctx_alloc,
308 camellia_ctx_free
309};
310
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000312 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000313 POLARSSL_MODE_CBC,
314 128,
315 "CAMELLIA-128-CBC",
316 16,
317 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000318 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319};
320
321const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000322 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000323 POLARSSL_MODE_CBC,
324 192,
325 "CAMELLIA-192-CBC",
326 16,
327 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000328 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000329};
330
331const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000332 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000333 POLARSSL_MODE_CBC,
334 256,
335 "CAMELLIA-256-CBC",
336 16,
337 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000338 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339};
Paul Bakker343a8702011-06-09 14:27:58 +0000340
341#if defined(POLARSSL_CIPHER_MODE_CFB)
342const cipher_info_t camellia_128_cfb128_info = {
343 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000344 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000345 128,
346 "CAMELLIA-128-CFB128",
347 16,
348 16,
349 &camellia_info
350};
351
352const cipher_info_t camellia_192_cfb128_info = {
353 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000354 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000355 192,
356 "CAMELLIA-192-CFB128",
357 16,
358 16,
359 &camellia_info
360};
361
362const cipher_info_t camellia_256_cfb128_info = {
363 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000364 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000365 256,
366 "CAMELLIA-256-CFB128",
367 16,
368 16,
369 &camellia_info
370};
371#endif /* POLARSSL_CIPHER_MODE_CFB */
372
373#if defined(POLARSSL_CIPHER_MODE_CTR)
374const cipher_info_t camellia_128_ctr_info = {
375 POLARSSL_CIPHER_CAMELLIA_128_CTR,
376 POLARSSL_MODE_CTR,
377 128,
378 "CAMELLIA-128-CTR",
379 16,
380 16,
381 &camellia_info
382};
383
384const cipher_info_t camellia_192_ctr_info = {
385 POLARSSL_CIPHER_CAMELLIA_192_CTR,
386 POLARSSL_MODE_CTR,
387 192,
388 "CAMELLIA-192-CTR",
389 16,
390 16,
391 &camellia_info
392};
393
394const cipher_info_t camellia_256_ctr_info = {
395 POLARSSL_CIPHER_CAMELLIA_256_CTR,
396 POLARSSL_MODE_CTR,
397 256,
398 "CAMELLIA-256-CTR",
399 16,
400 16,
401 &camellia_info
402};
403#endif /* POLARSSL_CIPHER_MODE_CTR */
404
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405#endif
406
407#if defined(POLARSSL_DES_C)
408
Paul Bakker1d073c52014-07-08 20:15:51 +0200409static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410 unsigned char *iv, const unsigned char *input, unsigned char *output )
411{
412 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
413}
414
Paul Bakker1d073c52014-07-08 20:15:51 +0200415static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416 unsigned char *iv, const unsigned char *input, unsigned char *output )
417{
418 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
419}
420
Paul Bakker1d073c52014-07-08 20:15:51 +0200421static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000422 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
423{
424 ((void) ctx);
425 ((void) operation);
426 ((void) length);
427 ((void) iv_off);
428 ((void) iv);
429 ((void) input);
430 ((void) output);
431
432 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
433}
434
Paul Bakker1d073c52014-07-08 20:15:51 +0200435static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000436 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
437 const unsigned char *input, unsigned char *output )
438{
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;
448}
449
450
Paul Bakker1d073c52014-07-08 20:15:51 +0200451static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000453 ((void) key_length);
454
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455 return des_setkey_dec( (des_context *) ctx, key );
456}
457
Paul Bakker1d073c52014-07-08 20:15:51 +0200458static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000460 ((void) key_length);
461
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462 return des_setkey_enc( (des_context *) ctx, key );
463}
464
Paul Bakker1d073c52014-07-08 20:15:51 +0200465static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000467 ((void) key_length);
468
Paul Bakker8123e9d2011-01-06 15:37:30 +0000469 return des3_set2key_dec( (des3_context *) ctx, key );
470}
471
Paul Bakker1d073c52014-07-08 20:15:51 +0200472static int des3_set2key_enc_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 des3_set2key_enc( (des3_context *) ctx, key );
477}
478
Paul Bakker1d073c52014-07-08 20:15:51 +0200479static int des3_set3key_dec_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 des3_set3key_dec( (des3_context *) ctx, key );
484}
485
Paul Bakker1d073c52014-07-08 20:15:51 +0200486static int des3_set3key_enc_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_set3key_enc( (des3_context *) ctx, key );
491}
492
493static void * des_ctx_alloc( void )
494{
495 return malloc( sizeof( des_context ) );
496}
497
498static void * des3_ctx_alloc( void )
499{
500 return malloc( sizeof( des3_context ) );
501}
502
503static void des_ctx_free( void *ctx )
504{
Paul Bakker312da332014-06-13 17:20:13 +0200505 polarssl_zeroize( ctx, sizeof( des_context ) );
506 free( ctx );
507}
508
509static void des3_ctx_free( void *ctx )
510{
511 polarssl_zeroize( ctx, sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512 free( ctx );
513}
514
Paul Bakker343a8702011-06-09 14:27:58 +0000515const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000516 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000517 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000518 des_crypt_cfb128_wrap,
519 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000520 des_setkey_enc_wrap,
521 des_setkey_dec_wrap,
522 des_ctx_alloc,
523 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000524};
525
Paul Bakker343a8702011-06-09 14:27:58 +0000526const cipher_info_t des_cbc_info = {
527 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000528 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000529 POLARSSL_KEY_LENGTH_DES,
530 "DES-CBC",
531 8,
532 8,
533 &des_info
534};
535
536const cipher_base_t des_ede_info = {
537 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000538 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000539 des_crypt_cfb128_wrap,
540 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000541 des3_set2key_enc_wrap,
542 des3_set2key_dec_wrap,
543 des3_ctx_alloc,
Paul Bakker312da332014-06-13 17:20:13 +0200544 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000545};
546
Paul Bakker343a8702011-06-09 14:27:58 +0000547const cipher_info_t des_ede_cbc_info = {
548 POLARSSL_CIPHER_DES_EDE_CBC,
549 POLARSSL_MODE_CBC,
550 POLARSSL_KEY_LENGTH_DES_EDE,
551 "DES-EDE-CBC",
Paul Bakker2be71fa2013-06-18 16:33:27 +0200552 8,
553 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000554 &des_ede_info
555};
556
557const cipher_base_t des_ede3_info = {
558 POLARSSL_CIPHER_ID_DES,
559 des3_crypt_cbc_wrap,
560 des_crypt_cfb128_wrap,
561 des_crypt_ctr_wrap,
562 des3_set3key_enc_wrap,
563 des3_set3key_dec_wrap,
564 des3_ctx_alloc,
Paul Bakker312da332014-06-13 17:20:13 +0200565 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +0000566};
567
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000569 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000570 POLARSSL_MODE_CBC,
571 POLARSSL_KEY_LENGTH_DES_EDE3,
572 "DES-EDE3-CBC",
573 8,
574 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000575 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576};
577#endif
578
Paul Bakker6132d0a2012-07-04 17:10:40 +0000579#if defined(POLARSSL_BLOWFISH_C)
580
Paul Bakker1d073c52014-07-08 20:15:51 +0200581static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000582 unsigned char *iv, const unsigned char *input, unsigned char *output )
583{
584 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
585}
586
Paul Bakker1d073c52014-07-08 20:15:51 +0200587static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000588 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
589{
590#if defined(POLARSSL_CIPHER_MODE_CFB)
591 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
592#else
593 ((void) ctx);
594 ((void) operation);
595 ((void) length);
596 ((void) iv_off);
597 ((void) iv);
598 ((void) input);
599 ((void) output);
600
601 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
602#endif
603}
604
Paul Bakker1d073c52014-07-08 20:15:51 +0200605static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000606 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
607 const unsigned char *input, unsigned char *output )
608{
609#if defined(POLARSSL_CIPHER_MODE_CTR)
610 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
611 stream_block, input, output );
612#else
613 ((void) ctx);
614 ((void) length);
615 ((void) nc_off);
616 ((void) nonce_counter);
617 ((void) stream_block);
618 ((void) input);
619 ((void) output);
620
621 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
622#endif
623}
624
Paul Bakker1d073c52014-07-08 20:15:51 +0200625static int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000626{
627 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
628}
629
Paul Bakker1d073c52014-07-08 20:15:51 +0200630static int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000631{
632 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
633}
634
635static void * blowfish_ctx_alloc( void )
636{
637 return malloc( sizeof( blowfish_context ) );
638}
639
640static void blowfish_ctx_free( void *ctx )
641{
Paul Bakker312da332014-06-13 17:20:13 +0200642 polarssl_zeroize( ctx, sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000643 free( ctx );
644}
645
646const cipher_base_t blowfish_info = {
647 POLARSSL_CIPHER_ID_BLOWFISH,
648 blowfish_crypt_cbc_wrap,
649 blowfish_crypt_cfb64_wrap,
650 blowfish_crypt_ctr_wrap,
651 blowfish_setkey_enc_wrap,
652 blowfish_setkey_dec_wrap,
653 blowfish_ctx_alloc,
654 blowfish_ctx_free
655};
656
657const cipher_info_t blowfish_cbc_info = {
658 POLARSSL_CIPHER_BLOWFISH_CBC,
659 POLARSSL_MODE_CBC,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200660 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000661 "BLOWFISH-CBC",
662 8,
663 8,
664 &blowfish_info
665};
666
667#if defined(POLARSSL_CIPHER_MODE_CFB)
668const cipher_info_t blowfish_cfb64_info = {
669 POLARSSL_CIPHER_BLOWFISH_CFB64,
670 POLARSSL_MODE_CFB,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200671 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000672 "BLOWFISH-CFB64",
673 8,
674 8,
675 &blowfish_info
676};
677#endif /* POLARSSL_CIPHER_MODE_CFB */
678
679#if defined(POLARSSL_CIPHER_MODE_CTR)
680const cipher_info_t blowfish_ctr_info = {
681 POLARSSL_CIPHER_BLOWFISH_CTR,
682 POLARSSL_MODE_CTR,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200683 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000684 "BLOWFISH-CTR",
685 8,
686 8,
687 &blowfish_info
688};
689#endif /* POLARSSL_CIPHER_MODE_CTR */
690#endif /* POLARSSL_BLOWFISH_C */
691
Paul Bakkerfab5c822012-02-06 16:45:10 +0000692#if defined(POLARSSL_CIPHER_NULL_CIPHER)
693static void * null_ctx_alloc( void )
694{
695 return (void *) 1;
696}
697
698
699static void null_ctx_free( void *ctx )
700{
701 ((void) ctx);
702}
703
704const cipher_base_t null_base_info = {
705 POLARSSL_CIPHER_ID_NULL,
706 NULL,
707 NULL,
708 NULL,
709 NULL,
710 NULL,
711 null_ctx_alloc,
712 null_ctx_free
713};
714
715const cipher_info_t null_cipher_info = {
716 POLARSSL_CIPHER_NULL,
717 POLARSSL_MODE_NULL,
718 0,
719 "NULL",
720 1,
721 1,
722 &null_base_info
723};
724#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
725
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726#endif