123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- /**
- * \file md_wrap.c
- *
- * \brief Generic message digest wrapper for PolarSSL
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include "config.h"
- #if defined(POLARSSL_CIPHER_C)
- #include "polarssl/cipher_wrap.h"
- #include "polarssl/aes.h"
- #include "polarssl/camellia.h"
- #include "polarssl/des.h"
- #include <stdlib.h>
- #if defined(POLARSSL_AES_C)
- int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
- unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
- }
- int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
- size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- #if defined(POLARSSL_CIPHER_MODE_CFB)
- return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
- #else
- ((void) ctx);
- ((void) operation);
- ((void) length);
- ((void) iv_off);
- ((void) iv);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- #endif
- }
- int aes_crypt_ctr_wrap( void *ctx, size_t length,
- size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
- const unsigned char *input, unsigned char *output )
- {
- #if defined(POLARSSL_CIPHER_MODE_CTR)
- return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
- stream_block, input, output );
- #else
- ((void) ctx);
- ((void) length);
- ((void) nc_off);
- ((void) nonce_counter);
- ((void) stream_block);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- #endif
- }
- int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- return aes_setkey_dec( (aes_context *) ctx, key, key_length );
- }
- int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- return aes_setkey_enc( (aes_context *) ctx, key, key_length );
- }
- static void * aes_ctx_alloc( void )
- {
- return malloc( sizeof( aes_context ) );
- }
- static void aes_ctx_free( void *ctx )
- {
- free( ctx );
- }
- const cipher_base_t aes_info = {
- POLARSSL_CIPHER_ID_AES,
- aes_crypt_cbc_wrap,
- aes_crypt_cfb128_wrap,
- aes_crypt_ctr_wrap,
- aes_setkey_enc_wrap,
- aes_setkey_dec_wrap,
- aes_ctx_alloc,
- aes_ctx_free
- };
- const cipher_info_t aes_128_cbc_info = {
- POLARSSL_CIPHER_AES_128_CBC,
- POLARSSL_MODE_CBC,
- 128,
- "AES-128-CBC",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_192_cbc_info = {
- POLARSSL_CIPHER_AES_192_CBC,
- POLARSSL_MODE_CBC,
- 192,
- "AES-192-CBC",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_256_cbc_info = {
- POLARSSL_CIPHER_AES_256_CBC,
- POLARSSL_MODE_CBC,
- 256,
- "AES-256-CBC",
- 16,
- 16,
- &aes_info
- };
- #if defined(POLARSSL_CIPHER_MODE_CFB)
- const cipher_info_t aes_128_cfb128_info = {
- POLARSSL_CIPHER_AES_128_CFB128,
- POLARSSL_MODE_CFB128,
- 128,
- "AES-128-CFB128",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_192_cfb128_info = {
- POLARSSL_CIPHER_AES_192_CFB128,
- POLARSSL_MODE_CFB128,
- 192,
- "AES-192-CFB128",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_256_cfb128_info = {
- POLARSSL_CIPHER_AES_256_CFB128,
- POLARSSL_MODE_CFB128,
- 256,
- "AES-256-CFB128",
- 16,
- 16,
- &aes_info
- };
- #endif /* POLARSSL_CIPHER_MODE_CFB */
- #if defined(POLARSSL_CIPHER_MODE_CTR)
- const cipher_info_t aes_128_ctr_info = {
- POLARSSL_CIPHER_AES_128_CTR,
- POLARSSL_MODE_CTR,
- 128,
- "AES-128-CTR",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_192_ctr_info = {
- POLARSSL_CIPHER_AES_192_CTR,
- POLARSSL_MODE_CTR,
- 192,
- "AES-192-CTR",
- 16,
- 16,
- &aes_info
- };
- const cipher_info_t aes_256_ctr_info = {
- POLARSSL_CIPHER_AES_256_CTR,
- POLARSSL_MODE_CTR,
- 256,
- "AES-256-CTR",
- 16,
- 16,
- &aes_info
- };
- #endif /* POLARSSL_CIPHER_MODE_CTR */
- #endif
- #if defined(POLARSSL_CAMELLIA_C)
- int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
- unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
- }
- int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
- size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- #if defined(POLARSSL_CIPHER_MODE_CFB)
- return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
- #else
- ((void) ctx);
- ((void) operation);
- ((void) length);
- ((void) iv_off);
- ((void) iv);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- #endif
- }
- int camellia_crypt_ctr_wrap( void *ctx, size_t length,
- size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
- const unsigned char *input, unsigned char *output )
- {
- #if defined(POLARSSL_CIPHER_MODE_CTR)
- return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
- stream_block, input, output );
- #else
- ((void) ctx);
- ((void) length);
- ((void) nc_off);
- ((void) nonce_counter);
- ((void) stream_block);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- #endif
- }
- int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
- }
- int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
- }
- static void * camellia_ctx_alloc( void )
- {
- return malloc( sizeof( camellia_context ) );
- }
- static void camellia_ctx_free( void *ctx )
- {
- free( ctx );
- }
- const cipher_base_t camellia_info = {
- POLARSSL_CIPHER_ID_CAMELLIA,
- camellia_crypt_cbc_wrap,
- camellia_crypt_cfb128_wrap,
- camellia_crypt_ctr_wrap,
- camellia_setkey_enc_wrap,
- camellia_setkey_dec_wrap,
- camellia_ctx_alloc,
- camellia_ctx_free
- };
- const cipher_info_t camellia_128_cbc_info = {
- POLARSSL_CIPHER_CAMELLIA_128_CBC,
- POLARSSL_MODE_CBC,
- 128,
- "CAMELLIA-128-CBC",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_192_cbc_info = {
- POLARSSL_CIPHER_CAMELLIA_192_CBC,
- POLARSSL_MODE_CBC,
- 192,
- "CAMELLIA-192-CBC",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_256_cbc_info = {
- POLARSSL_CIPHER_CAMELLIA_256_CBC,
- POLARSSL_MODE_CBC,
- 256,
- "CAMELLIA-256-CBC",
- 16,
- 16,
- &camellia_info
- };
- #if defined(POLARSSL_CIPHER_MODE_CFB)
- const cipher_info_t camellia_128_cfb128_info = {
- POLARSSL_CIPHER_CAMELLIA_128_CFB128,
- POLARSSL_MODE_CFB128,
- 128,
- "CAMELLIA-128-CFB128",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_192_cfb128_info = {
- POLARSSL_CIPHER_CAMELLIA_192_CFB128,
- POLARSSL_MODE_CFB128,
- 192,
- "CAMELLIA-192-CFB128",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_256_cfb128_info = {
- POLARSSL_CIPHER_CAMELLIA_256_CFB128,
- POLARSSL_MODE_CFB128,
- 256,
- "CAMELLIA-256-CFB128",
- 16,
- 16,
- &camellia_info
- };
- #endif /* POLARSSL_CIPHER_MODE_CFB */
- #if defined(POLARSSL_CIPHER_MODE_CTR)
- const cipher_info_t camellia_128_ctr_info = {
- POLARSSL_CIPHER_CAMELLIA_128_CTR,
- POLARSSL_MODE_CTR,
- 128,
- "CAMELLIA-128-CTR",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_192_ctr_info = {
- POLARSSL_CIPHER_CAMELLIA_192_CTR,
- POLARSSL_MODE_CTR,
- 192,
- "CAMELLIA-192-CTR",
- 16,
- 16,
- &camellia_info
- };
- const cipher_info_t camellia_256_ctr_info = {
- POLARSSL_CIPHER_CAMELLIA_256_CTR,
- POLARSSL_MODE_CTR,
- 256,
- "CAMELLIA-256-CTR",
- 16,
- 16,
- &camellia_info
- };
- #endif /* POLARSSL_CIPHER_MODE_CTR */
- #endif
- #if defined(POLARSSL_DES_C)
- int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
- unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
- }
- int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
- unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
- }
- int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
- size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
- {
- ((void) ctx);
- ((void) operation);
- ((void) length);
- ((void) iv_off);
- ((void) iv);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- }
- int des_crypt_ctr_wrap( void *ctx, size_t length,
- size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
- const unsigned char *input, unsigned char *output )
- {
- ((void) ctx);
- ((void) length);
- ((void) nc_off);
- ((void) nonce_counter);
- ((void) stream_block);
- ((void) input);
- ((void) output);
- return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
- }
- int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des_setkey_dec( (des_context *) ctx, key );
- }
- int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des_setkey_enc( (des_context *) ctx, key );
- }
- int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des3_set2key_dec( (des3_context *) ctx, key );
- }
- int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des3_set2key_enc( (des3_context *) ctx, key );
- }
- int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des3_set3key_dec( (des3_context *) ctx, key );
- }
- int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
- {
- ((void) key_length);
- return des3_set3key_enc( (des3_context *) ctx, key );
- }
- static void * des_ctx_alloc( void )
- {
- return malloc( sizeof( des_context ) );
- }
- static void * des3_ctx_alloc( void )
- {
- return malloc( sizeof( des3_context ) );
- }
- static void des_ctx_free( void *ctx )
- {
- free( ctx );
- }
- const cipher_base_t des_info = {
- POLARSSL_CIPHER_ID_DES,
- des_crypt_cbc_wrap,
- des_crypt_cfb128_wrap,
- des_crypt_ctr_wrap,
- des_setkey_enc_wrap,
- des_setkey_dec_wrap,
- des_ctx_alloc,
- des_ctx_free
- };
- const cipher_info_t des_cbc_info = {
- POLARSSL_CIPHER_DES_CBC,
- POLARSSL_MODE_CBC,
- POLARSSL_KEY_LENGTH_DES,
- "DES-CBC",
- 8,
- 8,
- &des_info
- };
- const cipher_base_t des_ede_info = {
- POLARSSL_CIPHER_ID_DES,
- des3_crypt_cbc_wrap,
- des_crypt_cfb128_wrap,
- des_crypt_ctr_wrap,
- des3_set2key_enc_wrap,
- des3_set2key_dec_wrap,
- des3_ctx_alloc,
- des_ctx_free
- };
- const cipher_info_t des_ede_cbc_info = {
- POLARSSL_CIPHER_DES_EDE_CBC,
- POLARSSL_MODE_CBC,
- POLARSSL_KEY_LENGTH_DES_EDE,
- "DES-EDE-CBC",
- 16,
- 16,
- &des_ede_info
- };
- const cipher_base_t des_ede3_info = {
- POLARSSL_CIPHER_ID_DES,
- des3_crypt_cbc_wrap,
- des_crypt_cfb128_wrap,
- des_crypt_ctr_wrap,
- des3_set3key_enc_wrap,
- des3_set3key_dec_wrap,
- des3_ctx_alloc,
- des_ctx_free
- };
- const cipher_info_t des_ede3_cbc_info = {
- POLARSSL_CIPHER_DES_EDE3_CBC,
- POLARSSL_MODE_CBC,
- POLARSSL_KEY_LENGTH_DES_EDE3,
- "DES-EDE3-CBC",
- 8,
- 8,
- &des_ede3_info
- };
- #endif
- #endif
|