Commit c7ea99af authored by Paul Bakker's avatar Paul Bakker
Browse files

Add _init() and _free() for cipher modules

parent 0464dd93
......@@ -73,6 +73,20 @@ typedef struct
}
aes_context;
/**
* \brief Initialize AES context
*
* \param ctx AES context to be initialized
*/
void aes_init( aes_context *ctx );
/**
* \brief Clear AES context
*
* \param ctx AES context to be cleared
*/
void aes_free( aes_context *ctx );
/**
* \brief AES key schedule (encryption)
*
......
......@@ -55,9 +55,23 @@ typedef struct
arc4_context;
/**
* \brief ARC4 key schedule
* \brief Initialize ARC4 context
*
* \param ctx ARC4 context to be initialized
*/
void arc4_init( arc4_context *ctx );
/**
* \brief Clear ARC4 context
*
* \param ctx ARC4 context to be cleared
*/
void arc4_free( arc4_context *ctx );
/**
* \brief ARC4 key schedule
*
* \param ctx ARC4 context to be setup
* \param key the secret key
* \param keylen length of the key, in bytes
*/
......
......@@ -70,6 +70,20 @@ typedef struct
}
blowfish_context;
/**
* \brief Initialize Blowfish context
*
* \param ctx Blowfish context to be initialized
*/
void blowfish_init( blowfish_context *ctx );
/**
* \brief Clear Blowfish context
*
* \param ctx Blowfish context to be cleared
*/
void blowfish_free( blowfish_context *ctx );
/**
* \brief Blowfish key schedule
*
......
......@@ -66,6 +66,20 @@ typedef struct
}
camellia_context;
/**
* \brief Initialize CAMELLIA context
*
* \param ctx CAMELLIA context to be initialized
*/
void camellia_init( camellia_context *ctx );
/**
* \brief Clear CAMELLIA context
*
* \param ctx CAMELLIA context to be cleared
*/
void camellia_free( camellia_context *ctx );
/**
* \brief CAMELLIA key schedule (encryption)
*
......
......@@ -77,6 +77,34 @@ typedef struct
}
des3_context;
/**
* \brief Initialize DES context
*
* \param ctx DES context to be initialized
*/
void des_init( des_context *ctx );
/**
* \brief Clear DES context
*
* \param ctx DES context to be cleared
*/
void des_free( des_context *ctx );
/**
* \brief Initialize Triple-DES context
*
* \param ctx DES3 context to be initialized
*/
void des3_init( des3_context *ctx );
/**
* \brief Clear Triple-DES context
*
* \param ctx DES3 context to be cleared
*/
void des3_free( des3_context *ctx );
/**
* \brief Set key parity on the given key to odd.
*
......
......@@ -64,6 +64,20 @@ typedef struct
}
xtea_context;
/**
* \brief Initialize XTEA context
*
* \param ctx XTEA context to be initialized
*/
void xtea_init( xtea_context *ctx );
/**
* \brief Clear XTEA context
*
* \param ctx XTEA context to be cleared
*/
void xtea_free( xtea_context *ctx );
/**
* \brief XTEA key schedule
*
......
......@@ -463,6 +463,19 @@ static void aes_gen_tables( void )
#endif /* POLARSSL_AES_ROM_TABLES */
void aes_init( aes_context *ctx )
{
memset( ctx, 0, sizeof( aes_context ) );
}
void aes_free( aes_context *ctx )
{
if( ctx == NULL )
return;
polarssl_zeroize( ctx, sizeof( aes_context ) );
}
/*
* AES key schedule (encryption)
*/
......@@ -581,11 +594,12 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
unsigned int keysize )
{
int i, j;
int i, j, ret;
aes_context cty;
uint32_t *RK;
uint32_t *SK;
int ret;
aes_init( &cty );
#if defined(POLARSSL_PADLOCK_C) && defined(PADLOCK_ALIGN16)
if( aes_padlock_ace == -1 )
......@@ -599,7 +613,7 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
/* Also checks keysize */
if( ( ret = aes_setkey_enc( &cty, key, keysize ) ) != 0 )
return( ret );
goto exit;
ctx->nr = cty.nr;
......@@ -608,7 +622,7 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
{
aesni_inverse_key( (unsigned char *) ctx->rk,
(const unsigned char *) cty.rk, ctx->nr );
goto done;
goto exit;
}
#endif
......@@ -635,12 +649,10 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
*RK++ = *SK++;
*RK++ = *SK++;
#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
done:
#endif
polarssl_zeroize( &cty, sizeof( aes_context ) );
exit:
aes_free( &cty );
return( 0 );
return( ret );
}
#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
......@@ -1171,7 +1183,7 @@ static const int aes_test_ctr_len[3] =
*/
int aes_self_test( int verbose )
{
int i, j, u, v;
int ret = 0, i, j, u, v;
unsigned char key[32];
unsigned char buf[64];
unsigned char iv[16];
......@@ -1189,6 +1201,7 @@ int aes_self_test( int verbose )
aes_context ctx;
memset( key, 0, 32 );
aes_init( &ctx );
/*
* ECB mode
......@@ -1216,7 +1229,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
else
......@@ -1231,7 +1245,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
......@@ -1271,7 +1286,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
else
......@@ -1294,7 +1310,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
......@@ -1335,7 +1352,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
else
......@@ -1348,7 +1366,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
......@@ -1392,7 +1411,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
else
......@@ -1408,7 +1428,8 @@ int aes_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
}
......@@ -1420,7 +1441,12 @@ int aes_self_test( int verbose )
polarssl_printf( "\n" );
#endif /* POLARSSL_CIPHER_MODE_CTR */
return( 0 );
ret = 0;
exit:
aes_free( &ctx );
return( ret );
}
#endif /* POLARSSL_SELF_TEST */
......
......@@ -46,6 +46,24 @@
#if !defined(POLARSSL_ARC4_ALT)
/* Implementation that should never be optimized out by the compiler */
static void polarssl_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void arc4_init( arc4_context *ctx )
{
memset( ctx, 0, sizeof( arc4_context ) );
}
void arc4_free( arc4_context *ctx )
{
if( ctx == NULL )
return;
polarssl_zeroize( ctx, sizeof( arc4_context ) );
}
/*
* ARC4 key schedule
*/
......@@ -146,11 +164,13 @@ static const unsigned char arc4_test_ct[3][8] =
*/
int arc4_self_test( int verbose )
{
int i;
int i, ret = 0;
unsigned char ibuf[8];
unsigned char obuf[8];
arc4_context ctx;
arc4_init( &ctx );
for( i = 0; i < 3; i++ )
{
if( verbose != 0 )
......@@ -166,7 +186,8 @@ int arc4_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "failed\n" );
return( 1 );
ret = 1;
goto exit;
}
if( verbose != 0 )
......@@ -176,7 +197,10 @@ int arc4_self_test( int verbose )
if( verbose != 0 )
polarssl_printf( "\n" );
return( 0 );
exit:
arc4_free( &ctx );
return( ret );
}
#endif /* POLARSSL_SELF_TEST */
......
......@@ -41,6 +41,11 @@
#if !defined(POLARSSL_BLOWFISH_ALT)
/* Implementation that should never be optimized out by the compiler */
static void polarssl_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
/*
* 32-bit integer manipulation macros (big endian)
*/
......@@ -152,6 +157,19 @@ static void blowfish_dec( blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
*xr = Xr;
}
void blowfish_init( blowfish_context *ctx )
{
memset( ctx, 0, sizeof( blowfish_context ) );
}
void blowfish_free( blowfish_context *ctx )
{
if( ctx == NULL )
return;
polarssl_zeroize( ctx, sizeof( blowfish_context ) );
}
/*
* Blowfish key schedule
*/
......
......@@ -322,6 +322,19 @@ static void camellia_feistel( const uint32_t x[2], const uint32_t k[2],
z[1] ^= I0;
}
void camellia_init( camellia_context *ctx )
{
memset( ctx, 0, sizeof( camellia_context ) );
}
void camellia_free( camellia_context *ctx )
{
if( ctx == NULL )
return;
polarssl_zeroize( ctx, sizeof( camellia_context ) );
}
/*
* Camellia key schedule (encryption)
*/
......@@ -433,16 +446,17 @@ int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
unsigned int keysize )
{
int idx;
int idx, ret;
size_t i;
camellia_context cty;
uint32_t *RK;
uint32_t *SK;
int ret;
camellia_init( &cty );
/* Also checks keysize */
if( ( ret = camellia_setkey_enc( &cty, key, keysize ) ) )
return( ret );
goto exit;
ctx->nr = cty.nr;
idx = ( ctx->nr == 4 );
......@@ -468,9 +482,10 @@ int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
*RK++ = *SK++;
*RK++ = *SK++;
polarssl_zeroize( &cty, sizeof( camellia_context ) );
exit:
camellia_free( &cty );
return( 0 );
return( ret );
}
/*
......
......@@ -74,11 +74,6 @@
#include <stdlib.h>
/* Implementation that should never be optimized out by the compiler */
static void polarssl_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
#if defined(POLARSSL_GCM_C)
/* shared by all GCM ciphers */
static void *gcm_ctx_alloc( void )
......@@ -187,12 +182,19 @@ static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
static void * aes_ctx_alloc( void )
{
return polarssl_malloc( sizeof( aes_context ) );
aes_context *aes = (aes_context *) polarssl_malloc( sizeof( aes_context ) );
if( aes == NULL )
return( NULL );
aes_init( aes );
return( aes );
}
static void aes_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( aes_context ) );
aes_free( (aes_context *) ctx );
polarssl_free( ctx );
}
......@@ -541,12 +543,20 @@ static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
static void * camellia_ctx_alloc( void )
{
return polarssl_malloc( sizeof( camellia_context ) );
camellia_context *ctx;
ctx = (camellia_context *) polarssl_malloc( sizeof( camellia_context ) );
if( ctx == NULL )
return( NULL );
camellia_init( ctx );
return( ctx );
}
static void camellia_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( camellia_context ) );
camellia_free( (camellia_context *) ctx );
polarssl_free( ctx );
}
......@@ -915,23 +925,38 @@ static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
static void * des_ctx_alloc( void )
{
return polarssl_malloc( sizeof( des_context ) );
}
des_context *des = (des_context *) polarssl_malloc( sizeof( des_context ) );
static void * des3_ctx_alloc( void )
{
return polarssl_malloc( sizeof( des3_context ) );
if( des == NULL )
return( NULL );
des_init( des );
return( des );
}
static void des_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( des_context ) );
des_free( (des_context *) ctx );
polarssl_free( ctx );
}
static void * des3_ctx_alloc( void )
{
des3_context *des3;
des3 = (des3_context *) polarssl_malloc( sizeof( des3_context ) );
if( des3 == NULL )
return( NULL );
des3_init( des3 );
return( des3 );
}
static void des3_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( des3_context ) );
des3_free( (des3_context *) ctx );
polarssl_free( ctx );
}
......@@ -1122,12 +1147,20 @@ static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
static void * blowfish_ctx_alloc( void )
{
return polarssl_malloc( sizeof( blowfish_context ) );
blowfish_context *ctx;
ctx = (blowfish_context *) polarssl_malloc( sizeof( blowfish_context ) );
if( ctx == NULL )
return( NULL );
blowfish_init( ctx );
return( ctx );
}
static void blowfish_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( blowfish_context ) );
blowfish_free( (blowfish_context *) ctx );
polarssl_free( ctx );
}
......@@ -1216,12 +1249,20 @@ static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
static void * arc4_ctx_alloc( void )
{
return polarssl_malloc( sizeof( arc4_context ) );
arc4_context *ctx;
ctx = (arc4_context *) polarssl_malloc( sizeof( arc4_context ) );
if( ctx == NULL )
return( NULL );
arc4_init( ctx );
return( ctx );
}
static void arc4_ctx_free( void *ctx )
{
polarssl_zeroize( ctx, sizeof( arc4_context ) );
arc4_free( (arc4_context *) ctx );
polarssl_free( ctx );
}
......
......@@ -66,6 +66,8 @@ int ctr_drbg_init_entropy_len(
memset( ctx, 0, sizeof(ctr_drbg_context) );
memset( key, 0, CTR_DRBG_KEYSIZE );
aes_init( &ctx->aes_ctx );
ctx->f_entropy = f_entropy;
ctx->p_entropy = p_entropy;
......@@ -122,6 +124,7 @@ static int block_cipher_df( unsigned char *output,
size_t buf_len, use_len;