4
* \brief Generic cipher wrapper for PolarSSL
6
* \author Adriaan de Jong <dejong@fox-it.com>
8
* Copyright (C) 2006-2011, 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.
30
#include "polarssl/config.h"
32
#if defined(POLARSSL_CIPHER_C)
34
#include "polarssl/cipher_wrap.h"
35
#include "polarssl/aes.h"
36
#include "polarssl/camellia.h"
37
#include "polarssl/des.h"
41
#if defined(POLARSSL_AES_C)
43
int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
44
unsigned char *iv, const unsigned char *input, unsigned char *output )
46
return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
49
int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
50
size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
52
#if defined(POLARSSL_CIPHER_MODE_CFB)
53
return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
63
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
67
int aes_crypt_ctr_wrap( void *ctx, size_t length,
68
size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
69
const unsigned char *input, unsigned char *output )
71
#if defined(POLARSSL_CIPHER_MODE_CTR)
72
return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
73
stream_block, input, output );
78
((void) nonce_counter);
79
((void) stream_block);
83
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
87
int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
89
return aes_setkey_dec( (aes_context *) ctx, key, key_length );
92
int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
94
return aes_setkey_enc( (aes_context *) ctx, key, key_length );
97
static void * aes_ctx_alloc( void )
99
return malloc( sizeof( aes_context ) );
102
static void aes_ctx_free( void *ctx )
107
const cipher_base_t aes_info = {
108
POLARSSL_CIPHER_ID_AES,
110
aes_crypt_cfb128_wrap,
118
const cipher_info_t aes_128_cbc_info = {
119
POLARSSL_CIPHER_AES_128_CBC,
128
const cipher_info_t aes_192_cbc_info = {
129
POLARSSL_CIPHER_AES_192_CBC,
138
const cipher_info_t aes_256_cbc_info = {
139
POLARSSL_CIPHER_AES_256_CBC,
148
#if defined(POLARSSL_CIPHER_MODE_CFB)
149
const cipher_info_t aes_128_cfb128_info = {
150
POLARSSL_CIPHER_AES_128_CFB128,
151
POLARSSL_MODE_CFB128,
159
const cipher_info_t aes_192_cfb128_info = {
160
POLARSSL_CIPHER_AES_192_CFB128,
161
POLARSSL_MODE_CFB128,
169
const cipher_info_t aes_256_cfb128_info = {
170
POLARSSL_CIPHER_AES_256_CFB128,
171
POLARSSL_MODE_CFB128,
178
#endif /* POLARSSL_CIPHER_MODE_CFB */
180
#if defined(POLARSSL_CIPHER_MODE_CTR)
181
const cipher_info_t aes_128_ctr_info = {
182
POLARSSL_CIPHER_AES_128_CTR,
191
const cipher_info_t aes_192_ctr_info = {
192
POLARSSL_CIPHER_AES_192_CTR,
201
const cipher_info_t aes_256_ctr_info = {
202
POLARSSL_CIPHER_AES_256_CTR,
210
#endif /* POLARSSL_CIPHER_MODE_CTR */
214
#if defined(POLARSSL_CAMELLIA_C)
216
int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
217
unsigned char *iv, const unsigned char *input, unsigned char *output )
219
return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
222
int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
223
size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
225
#if defined(POLARSSL_CIPHER_MODE_CFB)
226
return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
236
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
240
int camellia_crypt_ctr_wrap( void *ctx, size_t length,
241
size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
242
const unsigned char *input, unsigned char *output )
244
#if defined(POLARSSL_CIPHER_MODE_CTR)
245
return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
246
stream_block, input, output );
251
((void) nonce_counter);
252
((void) stream_block);
256
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
260
int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
262
return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
265
int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
267
return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
270
static void * camellia_ctx_alloc( void )
272
return malloc( sizeof( camellia_context ) );
275
static void camellia_ctx_free( void *ctx )
280
const cipher_base_t camellia_info = {
281
POLARSSL_CIPHER_ID_CAMELLIA,
282
camellia_crypt_cbc_wrap,
283
camellia_crypt_cfb128_wrap,
284
camellia_crypt_ctr_wrap,
285
camellia_setkey_enc_wrap,
286
camellia_setkey_dec_wrap,
291
const cipher_info_t camellia_128_cbc_info = {
292
POLARSSL_CIPHER_CAMELLIA_128_CBC,
301
const cipher_info_t camellia_192_cbc_info = {
302
POLARSSL_CIPHER_CAMELLIA_192_CBC,
311
const cipher_info_t camellia_256_cbc_info = {
312
POLARSSL_CIPHER_CAMELLIA_256_CBC,
321
#if defined(POLARSSL_CIPHER_MODE_CFB)
322
const cipher_info_t camellia_128_cfb128_info = {
323
POLARSSL_CIPHER_CAMELLIA_128_CFB128,
324
POLARSSL_MODE_CFB128,
326
"CAMELLIA-128-CFB128",
332
const cipher_info_t camellia_192_cfb128_info = {
333
POLARSSL_CIPHER_CAMELLIA_192_CFB128,
334
POLARSSL_MODE_CFB128,
336
"CAMELLIA-192-CFB128",
342
const cipher_info_t camellia_256_cfb128_info = {
343
POLARSSL_CIPHER_CAMELLIA_256_CFB128,
344
POLARSSL_MODE_CFB128,
346
"CAMELLIA-256-CFB128",
351
#endif /* POLARSSL_CIPHER_MODE_CFB */
353
#if defined(POLARSSL_CIPHER_MODE_CTR)
354
const cipher_info_t camellia_128_ctr_info = {
355
POLARSSL_CIPHER_CAMELLIA_128_CTR,
364
const cipher_info_t camellia_192_ctr_info = {
365
POLARSSL_CIPHER_CAMELLIA_192_CTR,
374
const cipher_info_t camellia_256_ctr_info = {
375
POLARSSL_CIPHER_CAMELLIA_256_CTR,
383
#endif /* POLARSSL_CIPHER_MODE_CTR */
387
#if defined(POLARSSL_DES_C)
389
int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
390
unsigned char *iv, const unsigned char *input, unsigned char *output )
392
return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
395
int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
396
unsigned char *iv, const unsigned char *input, unsigned char *output )
398
return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
401
int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
402
size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
412
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
415
int des_crypt_ctr_wrap( void *ctx, size_t length,
416
size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
417
const unsigned char *input, unsigned char *output )
422
((void) nonce_counter);
423
((void) stream_block);
427
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
431
int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
435
return des_setkey_dec( (des_context *) ctx, key );
438
int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
442
return des_setkey_enc( (des_context *) ctx, key );
445
int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
449
return des3_set2key_dec( (des3_context *) ctx, key );
452
int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
456
return des3_set2key_enc( (des3_context *) ctx, key );
459
int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
463
return des3_set3key_dec( (des3_context *) ctx, key );
466
int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
470
return des3_set3key_enc( (des3_context *) ctx, key );
473
static void * des_ctx_alloc( void )
475
return malloc( sizeof( des_context ) );
478
static void * des3_ctx_alloc( void )
480
return malloc( sizeof( des3_context ) );
483
static void des_ctx_free( void *ctx )
488
const cipher_base_t des_info = {
489
POLARSSL_CIPHER_ID_DES,
491
des_crypt_cfb128_wrap,
499
const cipher_info_t des_cbc_info = {
500
POLARSSL_CIPHER_DES_CBC,
502
POLARSSL_KEY_LENGTH_DES,
509
const cipher_base_t des_ede_info = {
510
POLARSSL_CIPHER_ID_DES,
512
des_crypt_cfb128_wrap,
514
des3_set2key_enc_wrap,
515
des3_set2key_dec_wrap,
520
const cipher_info_t des_ede_cbc_info = {
521
POLARSSL_CIPHER_DES_EDE_CBC,
523
POLARSSL_KEY_LENGTH_DES_EDE,
530
const cipher_base_t des_ede3_info = {
531
POLARSSL_CIPHER_ID_DES,
533
des_crypt_cfb128_wrap,
535
des3_set3key_enc_wrap,
536
des3_set3key_dec_wrap,
541
const cipher_info_t des_ede3_cbc_info = {
542
POLARSSL_CIPHER_DES_EDE3_CBC,
544
POLARSSL_KEY_LENGTH_DES_EDE3,