1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

- Added generic cipher wrapper for integration with OpenVPN (donated by Fox-IT)

This commit is contained in:
Paul Bakker
2011-01-06 15:37:30 +00:00
parent 6d46812123
commit 8123e9d8f1
14 changed files with 2008 additions and 14 deletions

View File

@ -7,6 +7,8 @@ set(src
bignum.c
camellia.c
certs.c
cipher.c
cipher_wrap.c
debug.c
des.c
dhm.c

View File

@ -26,7 +26,8 @@ OBJS= aes.o arc4.o base64.o \
ssl_cli.o ssl_srv.o ssl_tls.o \
timing.o x509parse.o xtea.o \
camellia.o version.o md.o \
md_wrap.o
md_wrap.o cipher.o cipher_wrap.o
.SILENT:

346
library/cipher.c Normal file
View File

@ -0,0 +1,346 @@
/**
* \file cipher.c
*
* \brief Generic cipher 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 "polarssl/config.h"
#if defined(POLARSSL_CIPHER_C)
#include "polarssl/cipher.h"
#include "polarssl/cipher_wrap.h"
#include <string.h>
#include <stdlib.h>
const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
{
/* Find static cipher information */
switch ( cipher_type )
{
#if defined(POLARSSL_AES_C)
case POLARSSL_CIPHER_AES_128_CBC:
return &aes_128_cbc_info;
case POLARSSL_CIPHER_AES_192_CBC:
return &aes_192_cbc_info;
case POLARSSL_CIPHER_AES_256_CBC:
return &aes_256_cbc_info;
#endif
#if defined(POLARSSL_CAMELLIA_C)
case POLARSSL_CIPHER_CAMELLIA_128_CBC:
return &camellia_128_cbc_info;
case POLARSSL_CIPHER_CAMELLIA_192_CBC:
return &camellia_192_cbc_info;
case POLARSSL_CIPHER_CAMELLIA_256_CBC:
return &camellia_256_cbc_info;
#endif
#if defined(POLARSSL_DES_C)
case POLARSSL_CIPHER_DES_CBC:
return &des_cbc_info;
case POLARSSL_CIPHER_DES_EDE_CBC:
return &des_ede_cbc_info;
case POLARSSL_CIPHER_DES_EDE3_CBC:
return &des_ede3_cbc_info;
#endif
default:
return NULL;
}
}
const cipher_info_t *cipher_info_from_string( const char *cipher_name )
{
if( NULL == cipher_name )
return NULL;
/* Get the appropriate digest information */
#if defined(POLARSSL_CAMELLIA_C)
if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
#endif
#if defined(POLARSSL_AES_C)
if( !strcasecmp( "AES-128-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
if( !strcasecmp( "AES-192-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
if( !strcasecmp( "AES-256-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
#endif
#if defined(POLARSSL_DES_C)
if( !strcasecmp( "DES-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
#endif
return NULL;
}
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
{
if( NULL == cipher_info || NULL == ctx )
return 1;
memset( ctx, 0, sizeof( ctx ) );
if( NULL == ( ctx->cipher_ctx = cipher_info->ctx_alloc_func() ) )
return 2;
ctx->cipher_info = cipher_info;
return 0;
}
int cipher_free_ctx( cipher_context_t *ctx )
{
if( ctx == NULL || ctx->cipher_info == NULL )
return 1;
ctx->cipher_info->ctx_free_func( ctx->cipher_ctx );
return 0;
}
int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
int key_length, const operation_t operation )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return 1;
ctx->key_length = key_length;
ctx->operation = operation;
if (POLARSSL_ENCRYPT == operation)
return ctx->cipher_info->setkey_enc_func( ctx->cipher_ctx, key,
ctx->key_length );
if (POLARSSL_DECRYPT == operation)
return ctx->cipher_info->setkey_dec_func( ctx->cipher_ctx, key,
ctx->key_length );
return 1;
}
int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
return 1;
ctx->unprocessed_len = 0;
memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
return 0;
}
int cipher_update( cipher_context_t *ctx, const unsigned char *input, int ilen,
unsigned char *output, int *olen )
{
int copy_len = 0;
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
return 1;
*olen = 0;
if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
{
/*
* If there is not enough data for a full block, cache it.
*/
if( ( ctx->operation == POLARSSL_DECRYPT &&
ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
( ctx->operation == POLARSSL_ENCRYPT &&
ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
{
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
ilen );
ctx->unprocessed_len += ilen;
return 0;
}
/*
* Process cached data first
*/
if( ctx->unprocessed_len != 0 )
{
copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
copy_len );
if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
ctx->unprocessed_data, output) )
{
return 1;
}
*olen += cipher_get_block_size( ctx );
output += cipher_get_block_size( ctx );
ctx->unprocessed_len = 0;
input += copy_len;
ilen -= copy_len;
}
/*
* Cache final, incomplete block
*/
if( 0 != ilen )
{
copy_len = ilen % cipher_get_block_size( ctx );
if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
copy_len = cipher_get_block_size(ctx);
memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
copy_len );
ctx->unprocessed_len += copy_len;
ilen -= copy_len;
}
/*
* Process remaining full blocks
*/
if( ilen )
{
if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
ctx->operation, ilen, ctx->iv, input, output ) )
{
return 1;
}
*olen += ilen;
}
return 0;
}
return 1;
}
static void add_pkcs_padding( unsigned char *output, unsigned char output_len,
int data_len )
{
unsigned char padding_len = output_len - data_len;
unsigned char i = 0;
for( i = 0; i < padding_len; i++ )
output[data_len + i] = padding_len;
}
static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
int *data_len)
{
int i = 0;
unsigned char padding_len = 0;
if ( NULL == input || NULL == data_len )
return 1;
padding_len = input[input_len - 1];
if ( padding_len > input_len )
return 2;
for ( i = input_len - padding_len; i < input_len; i++ )
if ( input[i] != padding_len )
return 2;
*data_len = input_len - padding_len;
return 0;
}
int cipher_finish( cipher_context_t *ctx, unsigned char *output, int *olen)
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
return 1;
*olen = 0;
if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
{
if( POLARSSL_ENCRYPT == ctx->operation )
{
add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
ctx->unprocessed_len );
}
else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
{
/* For decrypt operations, expect a full block */
return 1;
}
/* cipher block */
if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, ctx->operation,
cipher_get_block_size( ctx ), ctx->iv, ctx->unprocessed_data,
output ) )
{
return 1;
}
/* Set output size for decryption */
if( POLARSSL_DECRYPT == ctx->operation )
return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
/* Set output size for encryption */
*olen = cipher_get_block_size( ctx );
return 0;
}
return 1;
}
#if defined(POLARSSL_SELF_TEST)
#include <stdio.h>
#define ASSERT(x) if (!(x)) { \
printf( "failed with %i at %s\n", value, (#x) ); \
return( 1 ); \
}
/*
* Checkup routine
*/
int cipher_self_test( int verbose )
{
return( 0 );
}
#endif
#endif

295
library/cipher_wrap.c Normal file
View File

@ -0,0 +1,295 @@
/**
* \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 "polarssl/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 <string.h>
#include <stdlib.h>
#if defined(POLARSSL_AES_C)
int aes_crypt_cbc_wrap( void *ctx, operation_t operation, int 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_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
{
return aes_setkey_dec( (aes_context *) ctx, key, key_length );
}
int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, 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_info_t aes_128_cbc_info = {
.type = POLARSSL_CIPHER_AES_128_CBC,
.cipher = POLARSSL_CIPHER_ID_AES,
.mode = POLARSSL_MODE_CBC,
.key_length = 128,
.name = "AES-128-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = aes_crypt_cbc_wrap,
.setkey_enc_func = aes_setkey_enc_wrap,
.setkey_dec_func = aes_setkey_dec_wrap,
.ctx_alloc_func = aes_ctx_alloc,
.ctx_free_func = aes_ctx_free
};
const cipher_info_t aes_192_cbc_info = {
.type = POLARSSL_CIPHER_AES_192_CBC,
.cipher = POLARSSL_CIPHER_ID_AES,
.mode = POLARSSL_MODE_CBC,
.key_length = 192,
.name = "AES-192-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = aes_crypt_cbc_wrap,
.setkey_enc_func = aes_setkey_enc_wrap,
.setkey_dec_func = aes_setkey_dec_wrap,
.ctx_alloc_func = aes_ctx_alloc,
.ctx_free_func = aes_ctx_free
};
const cipher_info_t aes_256_cbc_info = {
.type = POLARSSL_CIPHER_AES_256_CBC,
.cipher = POLARSSL_CIPHER_ID_AES,
.mode = POLARSSL_MODE_CBC,
.key_length = 256,
.name = "AES-256-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = aes_crypt_cbc_wrap,
.setkey_enc_func = aes_setkey_enc_wrap,
.setkey_dec_func = aes_setkey_dec_wrap,
.ctx_alloc_func = aes_ctx_alloc,
.ctx_free_func = aes_ctx_free
};
#endif
#if defined(POLARSSL_CAMELLIA_C)
int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, int 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_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
{
return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
}
int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, 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_info_t camellia_128_cbc_info = {
.type = POLARSSL_CIPHER_CAMELLIA_128_CBC,
.cipher = POLARSSL_CIPHER_ID_CAMELLIA,
.mode = POLARSSL_MODE_CBC,
.key_length = 128,
.name = "CAMELLIA-128-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = camellia_crypt_cbc_wrap,
.setkey_enc_func = camellia_setkey_enc_wrap,
.setkey_dec_func = camellia_setkey_dec_wrap,
.ctx_alloc_func = camellia_ctx_alloc,
.ctx_free_func = camellia_ctx_free
};
const cipher_info_t camellia_192_cbc_info = {
.type = POLARSSL_CIPHER_CAMELLIA_192_CBC,
.cipher = POLARSSL_CIPHER_ID_CAMELLIA,
.mode = POLARSSL_MODE_CBC,
.key_length = 192,
.name = "CAMELLIA-192-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = camellia_crypt_cbc_wrap,
.setkey_enc_func = camellia_setkey_enc_wrap,
.setkey_dec_func = camellia_setkey_dec_wrap,
.ctx_alloc_func = camellia_ctx_alloc,
.ctx_free_func = camellia_ctx_free
};
const cipher_info_t camellia_256_cbc_info = {
.type = POLARSSL_CIPHER_CAMELLIA_256_CBC,
.cipher = POLARSSL_CIPHER_ID_CAMELLIA,
.mode = POLARSSL_MODE_CBC,
.key_length = 256,
.name = "CAMELLIA-256-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = camellia_crypt_cbc_wrap,
.setkey_enc_func = camellia_setkey_enc_wrap,
.setkey_dec_func = camellia_setkey_dec_wrap,
.ctx_alloc_func = camellia_ctx_alloc,
.ctx_free_func = camellia_ctx_free
};
#endif
#if defined(POLARSSL_DES_C)
int des_crypt_cbc_wrap( void *ctx, operation_t operation, int 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, int 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_setkey_dec_wrap( void *ctx, const unsigned char *key, int key_length )
{
return des_setkey_dec( (des_context *) ctx, key );
}
int des_setkey_enc_wrap( void *ctx, const unsigned char *key, int key_length )
{
return des_setkey_enc( (des_context *) ctx, key );
}
int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, int key_length )
{
return des3_set2key_dec( (des3_context *) ctx, key );
}
int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, int key_length )
{
return des3_set2key_enc( (des3_context *) ctx, key );
}
int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, int key_length )
{
return des3_set3key_dec( (des3_context *) ctx, key );
}
int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, int 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_info_t des_cbc_info = {
.type = POLARSSL_CIPHER_DES_CBC,
.cipher = POLARSSL_CIPHER_ID_DES,
.mode = POLARSSL_MODE_CBC,
.key_length = POLARSSL_KEY_LENGTH_DES,
.name = "DES-CBC",
.iv_size = 8,
.block_size = 8,
.cbc_func = des_crypt_cbc_wrap,
.setkey_enc_func = des_setkey_enc_wrap,
.setkey_dec_func = des_setkey_dec_wrap,
.ctx_alloc_func = des_ctx_alloc,
.ctx_free_func = des_ctx_free
};
const cipher_info_t des_ede_cbc_info = {
.type = POLARSSL_CIPHER_DES_EDE_CBC,
.cipher = POLARSSL_CIPHER_ID_DES,
.mode = POLARSSL_MODE_CBC,
.key_length = POLARSSL_KEY_LENGTH_DES_EDE,
.name = "DES-EDE-CBC",
.iv_size = 16,
.block_size = 16,
.cbc_func = des3_crypt_cbc_wrap,
.setkey_enc_func = des3_set2key_enc_wrap,
.setkey_dec_func = des3_set2key_dec_wrap,
.ctx_alloc_func = des3_ctx_alloc,
.ctx_free_func = des_ctx_free
};
const cipher_info_t des_ede3_cbc_info = {
.type = POLARSSL_CIPHER_DES_EDE3_CBC,
.cipher = POLARSSL_CIPHER_ID_DES,
.mode = POLARSSL_MODE_CBC,
.key_length = POLARSSL_KEY_LENGTH_DES_EDE3,
.name = "DES-EDE3-CBC",
.iv_size = 8,
.block_size = 8,
.cbc_func = des3_crypt_cbc_wrap,
.setkey_enc_func = des3_set3key_enc_wrap,
.setkey_dec_func = des3_set3key_dec_wrap,
.ctx_alloc_func = des3_ctx_alloc,
.ctx_free_func = des_ctx_free
};
#endif
#endif

View File

@ -362,15 +362,17 @@ static void des_setkey( unsigned long SK[32], const unsigned char key[8] )
/*
* DES key schedule (56-bit, encryption)
*/
void des_setkey_enc( des_context *ctx, const unsigned char key[8] )
int des_setkey_enc( des_context *ctx, const unsigned char key[8] )
{
des_setkey( ctx->sk, key );
return( 0 );
}
/*
* DES key schedule (56-bit, decryption)
*/
void des_setkey_dec( des_context *ctx, const unsigned char key[8] )
int des_setkey_dec( des_context *ctx, const unsigned char key[8] )
{
int i;
@ -381,6 +383,8 @@ void des_setkey_dec( des_context *ctx, const unsigned char key[8] )
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
}
return( 0 );
}
static void des3_set2key( unsigned long esk[96],
@ -411,23 +415,27 @@ static void des3_set2key( unsigned long esk[96],
/*
* Triple-DES key schedule (112-bit, encryption)
*/
void des3_set2key_enc( des3_context *ctx, const unsigned char key[16] )
int des3_set2key_enc( des3_context *ctx, const unsigned char key[16] )
{
unsigned long sk[96];
des3_set2key( ctx->sk, sk, key );
memset( sk, 0, sizeof( sk ) );
return( 0 );
}
/*
* Triple-DES key schedule (112-bit, decryption)
*/
void des3_set2key_dec( des3_context *ctx, const unsigned char key[16] )
int des3_set2key_dec( des3_context *ctx, const unsigned char key[16] )
{
unsigned long sk[96];
des3_set2key( sk, ctx->sk, key );
memset( sk, 0, sizeof( sk ) );
return( 0 );
}
static void des3_set3key( unsigned long esk[96],
@ -456,23 +464,27 @@ static void des3_set3key( unsigned long esk[96],
/*
* Triple-DES key schedule (168-bit, encryption)
*/
void des3_set3key_enc( des3_context *ctx, const unsigned char key[24] )
int des3_set3key_enc( des3_context *ctx, const unsigned char key[24] )
{
unsigned long sk[96];
des3_set3key( ctx->sk, sk, key );
memset( sk, 0, sizeof( sk ) );
return( 0 );
}
/*
* Triple-DES key schedule (168-bit, decryption)
*/
void des3_set3key_dec( des3_context *ctx, const unsigned char key[24] )
int des3_set3key_dec( des3_context *ctx, const unsigned char key[24] )
{
unsigned long sk[96];
des3_set3key( sk, ctx->sk, key );
memset( sk, 0, sizeof( sk ) );
return( 0 );
}
/*