4 * \brief Generic cipher wrapper for PolarSSL
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2012, Brainspark B.V.
10 * This file is part of PolarSSL (http://www.polarssl.org)
11 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
13 * All rights reserved.
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.
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.
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.
32 #if defined(POLARSSL_CIPHER_C)
34 #include "cipher_wrap.h"
36 #if defined(POLARSSL_AES_C)
40 #if defined(POLARSSL_CAMELLIA_C)
44 #if defined(POLARSSL_DES_C)
48 #if defined(POLARSSL_BLOWFISH_C)
54 #if defined(POLARSSL_AES_C)
56 int aes_crypt_cbc_wrap( void *ctx
, operation_t operation
, size_t length
,
57 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
59 return aes_crypt_cbc( (aes_context
*) ctx
, operation
, length
, iv
, input
, output
);
62 int aes_crypt_cfb128_wrap( void *ctx
, operation_t operation
, size_t length
,
63 size_t *iv_off
, unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
65 #if defined(POLARSSL_CIPHER_MODE_CFB)
66 return aes_crypt_cfb128( (aes_context
*) ctx
, operation
, length
, iv_off
, iv
, input
, output
);
76 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
80 int aes_crypt_ctr_wrap( void *ctx
, size_t length
,
81 size_t *nc_off
, unsigned char *nonce_counter
, unsigned char *stream_block
,
82 const unsigned char *input
, unsigned char *output
)
84 #if defined(POLARSSL_CIPHER_MODE_CTR)
85 return aes_crypt_ctr( (aes_context
*) ctx
, length
, nc_off
, nonce_counter
,
86 stream_block
, input
, output
);
91 ((void) nonce_counter
);
92 ((void) stream_block
);
96 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
100 int aes_setkey_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
102 return aes_setkey_dec( (aes_context
*) ctx
, key
, key_length
);
105 int aes_setkey_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
107 return aes_setkey_enc( (aes_context
*) ctx
, key
, key_length
);
110 static void * aes_ctx_alloc( void )
112 return malloc( sizeof( aes_context
) );
115 static void aes_ctx_free( void *ctx
)
120 const cipher_base_t aes_info
= {
121 POLARSSL_CIPHER_ID_AES
,
123 aes_crypt_cfb128_wrap
,
131 const cipher_info_t aes_128_cbc_info
= {
132 POLARSSL_CIPHER_AES_128_CBC
,
141 const cipher_info_t aes_192_cbc_info
= {
142 POLARSSL_CIPHER_AES_192_CBC
,
151 const cipher_info_t aes_256_cbc_info
= {
152 POLARSSL_CIPHER_AES_256_CBC
,
161 #if defined(POLARSSL_CIPHER_MODE_CFB)
162 const cipher_info_t aes_128_cfb128_info
= {
163 POLARSSL_CIPHER_AES_128_CFB128
,
172 const cipher_info_t aes_192_cfb128_info
= {
173 POLARSSL_CIPHER_AES_192_CFB128
,
182 const cipher_info_t aes_256_cfb128_info
= {
183 POLARSSL_CIPHER_AES_256_CFB128
,
191 #endif /* POLARSSL_CIPHER_MODE_CFB */
193 #if defined(POLARSSL_CIPHER_MODE_CTR)
194 const cipher_info_t aes_128_ctr_info
= {
195 POLARSSL_CIPHER_AES_128_CTR
,
204 const cipher_info_t aes_192_ctr_info
= {
205 POLARSSL_CIPHER_AES_192_CTR
,
214 const cipher_info_t aes_256_ctr_info
= {
215 POLARSSL_CIPHER_AES_256_CTR
,
223 #endif /* POLARSSL_CIPHER_MODE_CTR */
227 #if defined(POLARSSL_CAMELLIA_C)
229 int camellia_crypt_cbc_wrap( void *ctx
, operation_t operation
, size_t length
,
230 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
232 return camellia_crypt_cbc( (camellia_context
*) ctx
, operation
, length
, iv
, input
, output
);
235 int camellia_crypt_cfb128_wrap( void *ctx
, operation_t operation
, size_t length
,
236 size_t *iv_off
, unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
238 #if defined(POLARSSL_CIPHER_MODE_CFB)
239 return camellia_crypt_cfb128( (camellia_context
*) ctx
, operation
, length
, iv_off
, iv
, input
, output
);
249 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
253 int camellia_crypt_ctr_wrap( void *ctx
, size_t length
,
254 size_t *nc_off
, unsigned char *nonce_counter
, unsigned char *stream_block
,
255 const unsigned char *input
, unsigned char *output
)
257 #if defined(POLARSSL_CIPHER_MODE_CTR)
258 return camellia_crypt_ctr( (camellia_context
*) ctx
, length
, nc_off
, nonce_counter
,
259 stream_block
, input
, output
);
264 ((void) nonce_counter
);
265 ((void) stream_block
);
269 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
273 int camellia_setkey_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
275 return camellia_setkey_dec( (camellia_context
*) ctx
, key
, key_length
);
278 int camellia_setkey_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
280 return camellia_setkey_enc( (camellia_context
*) ctx
, key
, key_length
);
283 static void * camellia_ctx_alloc( void )
285 return malloc( sizeof( camellia_context
) );
288 static void camellia_ctx_free( void *ctx
)
293 const cipher_base_t camellia_info
= {
294 POLARSSL_CIPHER_ID_CAMELLIA
,
295 camellia_crypt_cbc_wrap
,
296 camellia_crypt_cfb128_wrap
,
297 camellia_crypt_ctr_wrap
,
298 camellia_setkey_enc_wrap
,
299 camellia_setkey_dec_wrap
,
304 const cipher_info_t camellia_128_cbc_info
= {
305 POLARSSL_CIPHER_CAMELLIA_128_CBC
,
314 const cipher_info_t camellia_192_cbc_info
= {
315 POLARSSL_CIPHER_CAMELLIA_192_CBC
,
324 const cipher_info_t camellia_256_cbc_info
= {
325 POLARSSL_CIPHER_CAMELLIA_256_CBC
,
334 #if defined(POLARSSL_CIPHER_MODE_CFB)
335 const cipher_info_t camellia_128_cfb128_info
= {
336 POLARSSL_CIPHER_CAMELLIA_128_CFB128
,
339 "CAMELLIA-128-CFB128",
345 const cipher_info_t camellia_192_cfb128_info
= {
346 POLARSSL_CIPHER_CAMELLIA_192_CFB128
,
349 "CAMELLIA-192-CFB128",
355 const cipher_info_t camellia_256_cfb128_info
= {
356 POLARSSL_CIPHER_CAMELLIA_256_CFB128
,
359 "CAMELLIA-256-CFB128",
364 #endif /* POLARSSL_CIPHER_MODE_CFB */
366 #if defined(POLARSSL_CIPHER_MODE_CTR)
367 const cipher_info_t camellia_128_ctr_info
= {
368 POLARSSL_CIPHER_CAMELLIA_128_CTR
,
377 const cipher_info_t camellia_192_ctr_info
= {
378 POLARSSL_CIPHER_CAMELLIA_192_CTR
,
387 const cipher_info_t camellia_256_ctr_info
= {
388 POLARSSL_CIPHER_CAMELLIA_256_CTR
,
396 #endif /* POLARSSL_CIPHER_MODE_CTR */
400 #if defined(POLARSSL_DES_C)
402 int des_crypt_cbc_wrap( void *ctx
, operation_t operation
, size_t length
,
403 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
405 return des_crypt_cbc( (des_context
*) ctx
, operation
, length
, iv
, input
, output
);
408 int des3_crypt_cbc_wrap( void *ctx
, operation_t operation
, size_t length
,
409 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
411 return des3_crypt_cbc( (des3_context
*) ctx
, operation
, length
, iv
, input
, output
);
414 int des_crypt_cfb128_wrap( void *ctx
, operation_t operation
, size_t length
,
415 size_t *iv_off
, unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
425 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
428 int des_crypt_ctr_wrap( void *ctx
, size_t length
,
429 size_t *nc_off
, unsigned char *nonce_counter
, unsigned char *stream_block
,
430 const unsigned char *input
, unsigned char *output
)
435 ((void) nonce_counter
);
436 ((void) stream_block
);
440 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
444 int des_setkey_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
448 return des_setkey_dec( (des_context
*) ctx
, key
);
451 int des_setkey_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
455 return des_setkey_enc( (des_context
*) ctx
, key
);
458 int des3_set2key_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
462 return des3_set2key_dec( (des3_context
*) ctx
, key
);
465 int des3_set2key_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
469 return des3_set2key_enc( (des3_context
*) ctx
, key
);
472 int des3_set3key_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
476 return des3_set3key_dec( (des3_context
*) ctx
, key
);
479 int des3_set3key_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
483 return des3_set3key_enc( (des3_context
*) ctx
, key
);
486 static void * des_ctx_alloc( void )
488 return malloc( sizeof( des_context
) );
491 static void * des3_ctx_alloc( void )
493 return malloc( sizeof( des3_context
) );
496 static void des_ctx_free( void *ctx
)
501 const cipher_base_t des_info
= {
502 POLARSSL_CIPHER_ID_DES
,
504 des_crypt_cfb128_wrap
,
512 const cipher_info_t des_cbc_info
= {
513 POLARSSL_CIPHER_DES_CBC
,
515 POLARSSL_KEY_LENGTH_DES
,
522 const cipher_base_t des_ede_info
= {
523 POLARSSL_CIPHER_ID_DES
,
525 des_crypt_cfb128_wrap
,
527 des3_set2key_enc_wrap
,
528 des3_set2key_dec_wrap
,
533 const cipher_info_t des_ede_cbc_info
= {
534 POLARSSL_CIPHER_DES_EDE_CBC
,
536 POLARSSL_KEY_LENGTH_DES_EDE
,
543 const cipher_base_t des_ede3_info
= {
544 POLARSSL_CIPHER_ID_DES
,
546 des_crypt_cfb128_wrap
,
548 des3_set3key_enc_wrap
,
549 des3_set3key_dec_wrap
,
554 const cipher_info_t des_ede3_cbc_info
= {
555 POLARSSL_CIPHER_DES_EDE3_CBC
,
557 POLARSSL_KEY_LENGTH_DES_EDE3
,
565 #if defined(POLARSSL_BLOWFISH_C)
567 int blowfish_crypt_cbc_wrap( void *ctx
, operation_t operation
, size_t length
,
568 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
570 return blowfish_crypt_cbc( (blowfish_context
*) ctx
, operation
, length
, iv
, input
, output
);
573 int blowfish_crypt_cfb64_wrap( void *ctx
, operation_t operation
, size_t length
,
574 size_t *iv_off
, unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
576 #if defined(POLARSSL_CIPHER_MODE_CFB)
577 return blowfish_crypt_cfb64( (blowfish_context
*) ctx
, operation
, length
, iv_off
, iv
, input
, output
);
587 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
591 int blowfish_crypt_ctr_wrap( void *ctx
, size_t length
,
592 size_t *nc_off
, unsigned char *nonce_counter
, unsigned char *stream_block
,
593 const unsigned char *input
, unsigned char *output
)
595 #if defined(POLARSSL_CIPHER_MODE_CTR)
596 return blowfish_crypt_ctr( (blowfish_context
*) ctx
, length
, nc_off
, nonce_counter
,
597 stream_block
, input
, output
);
602 ((void) nonce_counter
);
603 ((void) stream_block
);
607 return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE
;
611 int blowfish_setkey_dec_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
613 return blowfish_setkey( (blowfish_context
*) ctx
, key
, key_length
);
616 int blowfish_setkey_enc_wrap( void *ctx
, const unsigned char *key
, unsigned int key_length
)
618 return blowfish_setkey( (blowfish_context
*) ctx
, key
, key_length
);
621 static void * blowfish_ctx_alloc( void )
623 return malloc( sizeof( blowfish_context
) );
626 static void blowfish_ctx_free( void *ctx
)
631 const cipher_base_t blowfish_info
= {
632 POLARSSL_CIPHER_ID_BLOWFISH
,
633 blowfish_crypt_cbc_wrap
,
634 blowfish_crypt_cfb64_wrap
,
635 blowfish_crypt_ctr_wrap
,
636 blowfish_setkey_enc_wrap
,
637 blowfish_setkey_dec_wrap
,
642 const cipher_info_t blowfish_cbc_info
= {
643 POLARSSL_CIPHER_BLOWFISH_CBC
,
652 #if defined(POLARSSL_CIPHER_MODE_CFB)
653 const cipher_info_t blowfish_cfb64_info
= {
654 POLARSSL_CIPHER_BLOWFISH_CFB64
,
662 #endif /* POLARSSL_CIPHER_MODE_CFB */
664 #if defined(POLARSSL_CIPHER_MODE_CTR)
665 const cipher_info_t blowfish_ctr_info
= {
666 POLARSSL_CIPHER_BLOWFISH_CTR
,
674 #endif /* POLARSSL_CIPHER_MODE_CTR */
675 #endif /* POLARSSL_BLOWFISH_C */
677 #if defined(POLARSSL_CIPHER_NULL_CIPHER)
678 static void * null_ctx_alloc( void )
684 static void null_ctx_free( void *ctx
)
689 const cipher_base_t null_base_info
= {
690 POLARSSL_CIPHER_ID_NULL
,
700 const cipher_info_t null_cipher_info
= {
701 POLARSSL_CIPHER_NULL
,
709 #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */