blob: c0bc406d35c9cc1396f26e9be140abcb5c61203f [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 Bakker530927b2015-02-13 14:24:10 +01008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker8123e9d2011-01-06 15:37:30 +00009 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +000010 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27#include "polarssl/config.h"
28
29#if defined(POLARSSL_CIPHER_C)
30
31#include "polarssl/cipher_wrap.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000032
33#if defined(POLARSSL_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000034#include "polarssl/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035#endif
36
37#if defined(POLARSSL_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000038#include "polarssl/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000039#endif
40
41#if defined(POLARSSL_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000042#include "polarssl/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000043#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000044
Paul Bakker6132d0a2012-07-04 17:10:40 +000045#if defined(POLARSSL_BLOWFISH_C)
46#include "polarssl/blowfish.h"
47#endif
48
Paul Bakker8123e9d2011-01-06 15:37:30 +000049#include <stdlib.h>
50
Paul Bakker312da332014-06-13 17:20:13 +020051/* Implementation that should never be optimized out by the compiler */
52static void polarssl_zeroize( void *v, size_t n ) {
53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
54}
55
Paul Bakker8123e9d2011-01-06 15:37:30 +000056#if defined(POLARSSL_AES_C)
57
Paul Bakker1d073c52014-07-08 20:15:51 +020058static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +000059 unsigned char *iv, const unsigned char *input, unsigned char *output )
60{
61 return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
62}
63
Paul Bakker1d073c52014-07-08 20:15:51 +020064static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000065 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
66{
67#if defined(POLARSSL_CIPHER_MODE_CFB)
68 return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
69#else
70 ((void) ctx);
71 ((void) operation);
72 ((void) length);
73 ((void) iv_off);
74 ((void) iv);
75 ((void) input);
76 ((void) output);
77
78 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
79#endif
80}
81
Paul Bakker1d073c52014-07-08 20:15:51 +020082static int aes_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +000083 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
84 const unsigned char *input, unsigned char *output )
85{
86#if defined(POLARSSL_CIPHER_MODE_CTR)
87 return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
88 stream_block, input, output );
89#else
90 ((void) ctx);
91 ((void) length);
92 ((void) nc_off);
93 ((void) nonce_counter);
94 ((void) stream_block);
95 ((void) input);
96 ((void) output);
97
98 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
99#endif
100}
101
Paul Bakker1d073c52014-07-08 20:15:51 +0200102static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000103{
104 return aes_setkey_dec( (aes_context *) ctx, key, key_length );
105}
106
Paul Bakker1d073c52014-07-08 20:15:51 +0200107static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000108{
109 return aes_setkey_enc( (aes_context *) ctx, key, key_length );
110}
111
112static void * aes_ctx_alloc( void )
113{
114 return malloc( sizeof( aes_context ) );
115}
116
117static void aes_ctx_free( void *ctx )
118{
Paul Bakker312da332014-06-13 17:20:13 +0200119 polarssl_zeroize( ctx, sizeof( aes_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000120 free( ctx );
121}
122
Paul Bakker343a8702011-06-09 14:27:58 +0000123const cipher_base_t aes_info = {
124 POLARSSL_CIPHER_ID_AES,
125 aes_crypt_cbc_wrap,
126 aes_crypt_cfb128_wrap,
127 aes_crypt_ctr_wrap,
128 aes_setkey_enc_wrap,
129 aes_setkey_dec_wrap,
130 aes_ctx_alloc,
131 aes_ctx_free
132};
133
Paul Bakker8123e9d2011-01-06 15:37:30 +0000134const cipher_info_t aes_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000135 POLARSSL_CIPHER_AES_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000136 POLARSSL_MODE_CBC,
137 128,
138 "AES-128-CBC",
139 16,
140 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000141 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000142};
143
144const cipher_info_t aes_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000145 POLARSSL_CIPHER_AES_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000146 POLARSSL_MODE_CBC,
147 192,
148 "AES-192-CBC",
149 16,
150 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000151 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000152};
153
154const cipher_info_t aes_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000155 POLARSSL_CIPHER_AES_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000156 POLARSSL_MODE_CBC,
157 256,
158 "AES-256-CBC",
159 16,
160 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000161 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162};
Paul Bakker343a8702011-06-09 14:27:58 +0000163
164#if defined(POLARSSL_CIPHER_MODE_CFB)
165const cipher_info_t aes_128_cfb128_info = {
166 POLARSSL_CIPHER_AES_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000167 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000168 128,
169 "AES-128-CFB128",
170 16,
171 16,
172 &aes_info
173};
174
175const cipher_info_t aes_192_cfb128_info = {
176 POLARSSL_CIPHER_AES_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000177 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000178 192,
179 "AES-192-CFB128",
180 16,
181 16,
182 &aes_info
183};
184
185const cipher_info_t aes_256_cfb128_info = {
186 POLARSSL_CIPHER_AES_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000187 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000188 256,
189 "AES-256-CFB128",
190 16,
191 16,
192 &aes_info
193};
194#endif /* POLARSSL_CIPHER_MODE_CFB */
195
196#if defined(POLARSSL_CIPHER_MODE_CTR)
197const cipher_info_t aes_128_ctr_info = {
198 POLARSSL_CIPHER_AES_128_CTR,
199 POLARSSL_MODE_CTR,
200 128,
201 "AES-128-CTR",
202 16,
203 16,
204 &aes_info
205};
206
207const cipher_info_t aes_192_ctr_info = {
208 POLARSSL_CIPHER_AES_192_CTR,
209 POLARSSL_MODE_CTR,
210 192,
211 "AES-192-CTR",
212 16,
213 16,
214 &aes_info
215};
216
217const cipher_info_t aes_256_ctr_info = {
218 POLARSSL_CIPHER_AES_256_CTR,
219 POLARSSL_MODE_CTR,
220 256,
221 "AES-256-CTR",
222 16,
223 16,
224 &aes_info
225};
226#endif /* POLARSSL_CIPHER_MODE_CTR */
227
Paul Bakker8123e9d2011-01-06 15:37:30 +0000228#endif
229
230#if defined(POLARSSL_CAMELLIA_C)
231
Paul Bakker1d073c52014-07-08 20:15:51 +0200232static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000233 unsigned char *iv, const unsigned char *input, unsigned char *output )
234{
235 return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
236}
237
Paul Bakker1d073c52014-07-08 20:15:51 +0200238static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000239 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
240{
241#if defined(POLARSSL_CIPHER_MODE_CFB)
242 return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
243#else
244 ((void) ctx);
245 ((void) operation);
246 ((void) length);
247 ((void) iv_off);
248 ((void) iv);
249 ((void) input);
250 ((void) output);
251
252 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
253#endif
254}
255
Paul Bakker1d073c52014-07-08 20:15:51 +0200256static int camellia_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000257 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
258 const unsigned char *input, unsigned char *output )
259{
260#if defined(POLARSSL_CIPHER_MODE_CTR)
261 return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
262 stream_block, input, output );
263#else
264 ((void) ctx);
265 ((void) length);
266 ((void) nc_off);
267 ((void) nonce_counter);
268 ((void) stream_block);
269 ((void) input);
270 ((void) output);
271
272 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
273#endif
274}
275
Paul Bakker1d073c52014-07-08 20:15:51 +0200276static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000277{
278 return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
279}
280
Paul Bakker1d073c52014-07-08 20:15:51 +0200281static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000282{
283 return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
284}
285
286static void * camellia_ctx_alloc( void )
287{
288 return malloc( sizeof( camellia_context ) );
289}
290
291static void camellia_ctx_free( void *ctx )
292{
Paul Bakker312da332014-06-13 17:20:13 +0200293 polarssl_zeroize( ctx, sizeof( camellia_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000294 free( ctx );
295}
296
Paul Bakker343a8702011-06-09 14:27:58 +0000297const cipher_base_t camellia_info = {
298 POLARSSL_CIPHER_ID_CAMELLIA,
299 camellia_crypt_cbc_wrap,
300 camellia_crypt_cfb128_wrap,
301 camellia_crypt_ctr_wrap,
302 camellia_setkey_enc_wrap,
303 camellia_setkey_dec_wrap,
304 camellia_ctx_alloc,
305 camellia_ctx_free
306};
307
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308const cipher_info_t camellia_128_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000309 POLARSSL_CIPHER_CAMELLIA_128_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000310 POLARSSL_MODE_CBC,
311 128,
312 "CAMELLIA-128-CBC",
313 16,
314 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000315 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316};
317
318const cipher_info_t camellia_192_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000319 POLARSSL_CIPHER_CAMELLIA_192_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000320 POLARSSL_MODE_CBC,
321 192,
322 "CAMELLIA-192-CBC",
323 16,
324 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000325 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326};
327
328const cipher_info_t camellia_256_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000329 POLARSSL_CIPHER_CAMELLIA_256_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000330 POLARSSL_MODE_CBC,
331 256,
332 "CAMELLIA-256-CBC",
333 16,
334 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000335 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000336};
Paul Bakker343a8702011-06-09 14:27:58 +0000337
338#if defined(POLARSSL_CIPHER_MODE_CFB)
339const cipher_info_t camellia_128_cfb128_info = {
340 POLARSSL_CIPHER_CAMELLIA_128_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000341 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000342 128,
343 "CAMELLIA-128-CFB128",
344 16,
345 16,
346 &camellia_info
347};
348
349const cipher_info_t camellia_192_cfb128_info = {
350 POLARSSL_CIPHER_CAMELLIA_192_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000351 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000352 192,
353 "CAMELLIA-192-CFB128",
354 16,
355 16,
356 &camellia_info
357};
358
359const cipher_info_t camellia_256_cfb128_info = {
360 POLARSSL_CIPHER_CAMELLIA_256_CFB128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000361 POLARSSL_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000362 256,
363 "CAMELLIA-256-CFB128",
364 16,
365 16,
366 &camellia_info
367};
368#endif /* POLARSSL_CIPHER_MODE_CFB */
369
370#if defined(POLARSSL_CIPHER_MODE_CTR)
371const cipher_info_t camellia_128_ctr_info = {
372 POLARSSL_CIPHER_CAMELLIA_128_CTR,
373 POLARSSL_MODE_CTR,
374 128,
375 "CAMELLIA-128-CTR",
376 16,
377 16,
378 &camellia_info
379};
380
381const cipher_info_t camellia_192_ctr_info = {
382 POLARSSL_CIPHER_CAMELLIA_192_CTR,
383 POLARSSL_MODE_CTR,
384 192,
385 "CAMELLIA-192-CTR",
386 16,
387 16,
388 &camellia_info
389};
390
391const cipher_info_t camellia_256_ctr_info = {
392 POLARSSL_CIPHER_CAMELLIA_256_CTR,
393 POLARSSL_MODE_CTR,
394 256,
395 "CAMELLIA-256-CTR",
396 16,
397 16,
398 &camellia_info
399};
400#endif /* POLARSSL_CIPHER_MODE_CTR */
401
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402#endif
403
404#if defined(POLARSSL_DES_C)
405
Paul Bakker1d073c52014-07-08 20:15:51 +0200406static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 unsigned char *iv, const unsigned char *input, unsigned char *output )
408{
409 return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
410}
411
Paul Bakker1d073c52014-07-08 20:15:51 +0200412static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413 unsigned char *iv, const unsigned char *input, unsigned char *output )
414{
415 return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
416}
417
Paul Bakker1d073c52014-07-08 20:15:51 +0200418static int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000419 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
420{
421 ((void) ctx);
422 ((void) operation);
423 ((void) length);
424 ((void) iv_off);
425 ((void) iv);
426 ((void) input);
427 ((void) output);
428
429 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
430}
431
Paul Bakker1d073c52014-07-08 20:15:51 +0200432static int des_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker343a8702011-06-09 14:27:58 +0000433 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
434 const unsigned char *input, unsigned char *output )
435{
436 ((void) ctx);
437 ((void) length);
438 ((void) nc_off);
439 ((void) nonce_counter);
440 ((void) stream_block);
441 ((void) input);
442 ((void) output);
443
444 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
445}
446
447
Paul Bakker1d073c52014-07-08 20:15:51 +0200448static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000449{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000450 ((void) key_length);
451
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452 return des_setkey_dec( (des_context *) ctx, key );
453}
454
Paul Bakker1d073c52014-07-08 20:15:51 +0200455static int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000457 ((void) key_length);
458
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459 return des_setkey_enc( (des_context *) ctx, key );
460}
461
Paul Bakker1d073c52014-07-08 20:15:51 +0200462static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000464 ((void) key_length);
465
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466 return des3_set2key_dec( (des3_context *) ctx, key );
467}
468
Paul Bakker1d073c52014-07-08 20:15:51 +0200469static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000470{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000471 ((void) key_length);
472
Paul Bakker8123e9d2011-01-06 15:37:30 +0000473 return des3_set2key_enc( (des3_context *) ctx, key );
474}
475
Paul Bakker1d073c52014-07-08 20:15:51 +0200476static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000477{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000478 ((void) key_length);
479
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480 return des3_set3key_dec( (des3_context *) ctx, key );
481}
482
Paul Bakker1d073c52014-07-08 20:15:51 +0200483static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484{
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000485 ((void) key_length);
486
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487 return des3_set3key_enc( (des3_context *) ctx, key );
488}
489
490static void * des_ctx_alloc( void )
491{
492 return malloc( sizeof( des_context ) );
493}
494
495static void * des3_ctx_alloc( void )
496{
497 return malloc( sizeof( des3_context ) );
498}
499
500static void des_ctx_free( void *ctx )
501{
Paul Bakker312da332014-06-13 17:20:13 +0200502 polarssl_zeroize( ctx, sizeof( des_context ) );
503 free( ctx );
504}
505
506static void des3_ctx_free( void *ctx )
507{
508 polarssl_zeroize( ctx, sizeof( des3_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509 free( ctx );
510}
511
Paul Bakker343a8702011-06-09 14:27:58 +0000512const cipher_base_t des_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000513 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000514 des_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000515 des_crypt_cfb128_wrap,
516 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000517 des_setkey_enc_wrap,
518 des_setkey_dec_wrap,
519 des_ctx_alloc,
520 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521};
522
Paul Bakker343a8702011-06-09 14:27:58 +0000523const cipher_info_t des_cbc_info = {
524 POLARSSL_CIPHER_DES_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000525 POLARSSL_MODE_CBC,
Paul Bakker343a8702011-06-09 14:27:58 +0000526 POLARSSL_KEY_LENGTH_DES,
527 "DES-CBC",
528 8,
529 8,
530 &des_info
531};
532
533const cipher_base_t des_ede_info = {
534 POLARSSL_CIPHER_ID_DES,
Paul Bakker23986e52011-04-24 08:57:21 +0000535 des3_crypt_cbc_wrap,
Paul Bakker343a8702011-06-09 14:27:58 +0000536 des_crypt_cfb128_wrap,
537 des_crypt_ctr_wrap,
Paul Bakker23986e52011-04-24 08:57:21 +0000538 des3_set2key_enc_wrap,
539 des3_set2key_dec_wrap,
540 des3_ctx_alloc,
Paul Bakker312da332014-06-13 17:20:13 +0200541 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542};
543
Paul Bakker343a8702011-06-09 14:27:58 +0000544const cipher_info_t des_ede_cbc_info = {
545 POLARSSL_CIPHER_DES_EDE_CBC,
546 POLARSSL_MODE_CBC,
547 POLARSSL_KEY_LENGTH_DES_EDE,
548 "DES-EDE-CBC",
Paul Bakker2be71fa2013-06-18 16:33:27 +0200549 8,
550 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000551 &des_ede_info
552};
553
554const cipher_base_t des_ede3_info = {
555 POLARSSL_CIPHER_ID_DES,
556 des3_crypt_cbc_wrap,
557 des_crypt_cfb128_wrap,
558 des_crypt_ctr_wrap,
559 des3_set3key_enc_wrap,
560 des3_set3key_dec_wrap,
561 des3_ctx_alloc,
Paul Bakker312da332014-06-13 17:20:13 +0200562 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +0000563};
564
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565const cipher_info_t des_ede3_cbc_info = {
Paul Bakker23986e52011-04-24 08:57:21 +0000566 POLARSSL_CIPHER_DES_EDE3_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000567 POLARSSL_MODE_CBC,
568 POLARSSL_KEY_LENGTH_DES_EDE3,
569 "DES-EDE3-CBC",
570 8,
571 8,
Paul Bakker343a8702011-06-09 14:27:58 +0000572 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573};
574#endif
575
Paul Bakker6132d0a2012-07-04 17:10:40 +0000576#if defined(POLARSSL_BLOWFISH_C)
577
Paul Bakker1d073c52014-07-08 20:15:51 +0200578static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000579 unsigned char *iv, const unsigned char *input, unsigned char *output )
580{
581 return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv, input, output );
582}
583
Paul Bakker1d073c52014-07-08 20:15:51 +0200584static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000585 size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
586{
587#if defined(POLARSSL_CIPHER_MODE_CFB)
588 return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length, iv_off, iv, input, output );
589#else
590 ((void) ctx);
591 ((void) operation);
592 ((void) length);
593 ((void) iv_off);
594 ((void) iv);
595 ((void) input);
596 ((void) output);
597
598 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
599#endif
600}
601
Paul Bakker1d073c52014-07-08 20:15:51 +0200602static int blowfish_crypt_ctr_wrap( void *ctx, size_t length,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000603 size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
604 const unsigned char *input, unsigned char *output )
605{
606#if defined(POLARSSL_CIPHER_MODE_CTR)
607 return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off, nonce_counter,
608 stream_block, input, output );
609#else
610 ((void) ctx);
611 ((void) length);
612 ((void) nc_off);
613 ((void) nonce_counter);
614 ((void) stream_block);
615 ((void) input);
616 ((void) output);
617
618 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
619#endif
620}
621
Paul Bakker1d073c52014-07-08 20:15:51 +0200622static int blowfish_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000623{
624 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
625}
626
Paul Bakker1d073c52014-07-08 20:15:51 +0200627static int blowfish_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
Paul Bakker6132d0a2012-07-04 17:10:40 +0000628{
629 return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
630}
631
632static void * blowfish_ctx_alloc( void )
633{
634 return malloc( sizeof( blowfish_context ) );
635}
636
637static void blowfish_ctx_free( void *ctx )
638{
Paul Bakker312da332014-06-13 17:20:13 +0200639 polarssl_zeroize( ctx, sizeof( blowfish_context ) );
Paul Bakker6132d0a2012-07-04 17:10:40 +0000640 free( ctx );
641}
642
643const cipher_base_t blowfish_info = {
644 POLARSSL_CIPHER_ID_BLOWFISH,
645 blowfish_crypt_cbc_wrap,
646 blowfish_crypt_cfb64_wrap,
647 blowfish_crypt_ctr_wrap,
648 blowfish_setkey_enc_wrap,
649 blowfish_setkey_dec_wrap,
650 blowfish_ctx_alloc,
651 blowfish_ctx_free
652};
653
654const cipher_info_t blowfish_cbc_info = {
655 POLARSSL_CIPHER_BLOWFISH_CBC,
656 POLARSSL_MODE_CBC,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200657 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000658 "BLOWFISH-CBC",
659 8,
660 8,
661 &blowfish_info
662};
663
664#if defined(POLARSSL_CIPHER_MODE_CFB)
665const cipher_info_t blowfish_cfb64_info = {
666 POLARSSL_CIPHER_BLOWFISH_CFB64,
667 POLARSSL_MODE_CFB,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200668 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000669 "BLOWFISH-CFB64",
670 8,
671 8,
672 &blowfish_info
673};
674#endif /* POLARSSL_CIPHER_MODE_CFB */
675
676#if defined(POLARSSL_CIPHER_MODE_CTR)
677const cipher_info_t blowfish_ctr_info = {
678 POLARSSL_CIPHER_BLOWFISH_CTR,
679 POLARSSL_MODE_CTR,
Paul Bakker8a4ec442013-04-12 13:18:53 +0200680 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +0000681 "BLOWFISH-CTR",
682 8,
683 8,
684 &blowfish_info
685};
686#endif /* POLARSSL_CIPHER_MODE_CTR */
687#endif /* POLARSSL_BLOWFISH_C */
688
Paul Bakkerfab5c822012-02-06 16:45:10 +0000689#if defined(POLARSSL_CIPHER_NULL_CIPHER)
690static void * null_ctx_alloc( void )
691{
692 return (void *) 1;
693}
694
695
696static void null_ctx_free( void *ctx )
697{
698 ((void) ctx);
699}
700
701const cipher_base_t null_base_info = {
702 POLARSSL_CIPHER_ID_NULL,
703 NULL,
704 NULL,
705 NULL,
706 NULL,
707 NULL,
708 null_ctx_alloc,
709 null_ctx_free
710};
711
712const cipher_info_t null_cipher_info = {
713 POLARSSL_CIPHER_NULL,
714 POLARSSL_MODE_NULL,
715 0,
716 "NULL",
717 1,
718 1,
719 &null_base_info
720};
721#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
722
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723#endif