From 765db07dfbb68f9eb0bfdd7d0a5a02386580d3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 14 Aug 2013 15:00:27 +0200 Subject: [PATCH] PK: use alloc and free function pointers --- include/polarssl/pk.h | 7 ++++ library/pk.c | 84 ++++++++++++++++--------------------------- library/pk_wrap.c | 64 +++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 54 deletions(-) diff --git a/include/polarssl/pk.h b/include/polarssl/pk.h index 8626b604e8..83dc2d07ba 100644 --- a/include/polarssl/pk.h +++ b/include/polarssl/pk.h @@ -102,6 +102,13 @@ typedef struct int (*verify_func)( void *ctx, const unsigned char *hash, const md_info_t *md_info, const unsigned char *sig, size_t sig_len ); + + /** Allocate a new context */ + void * (*ctx_alloc_func)( void ); + + /** Free the given context */ + void (*ctx_free_func)( void *ctx ); + } pk_info_t; /** diff --git a/library/pk.c b/library/pk.c index 19bc79bbc6..9f3364172f 100644 --- a/library/pk.c +++ b/library/pk.c @@ -65,33 +65,39 @@ void pk_init( pk_context *ctx ) */ void pk_free( pk_context *ctx ) { - if( ctx == NULL ) + if( ctx == NULL || ctx->info == NULL) return; -#if defined(POLARSSL_RSA_C) - if( ctx->type == POLARSSL_PK_RSA ) - rsa_free( ctx->data ); - else -#endif -#if defined(POLARSSL_ECP_C) - if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH ) - ecp_keypair_free( ctx->data ); - else -#endif -#if defined(POLARSSL_ECDSA_C) - if( ctx->type == POLARSSL_PK_ECDSA ) - ecdsa_free( ctx->data ); - else -#endif - { - ; /* guard for the else's above */ - } - - polarssl_free( ctx->data ); + ctx->info->ctx_free_func( ctx->data ); + ctx->data = NULL; ctx->info = NULL; ctx->type = POLARSSL_PK_NONE; - ctx->data = NULL; +} + +/* + * Get pk_info structure from type + */ +static const pk_info_t * pk_info_from_type( pk_type_t pk_type ) +{ + switch( pk_type ) { +#if defined(POLARSSL_RSA_C) + case POLARSSL_PK_RSA: + return &rsa_info; +#endif +#if defined(POLARSSL_ECP_C) + case POLARSSL_PK_ECKEY: + return &eckey_info; + case POLARSSL_PK_ECKEY_DH: + return &eckeydh_info; +#endif +#if defined(POLARSSL_ECDSA_C) + case POLARSSL_PK_ECDSA: + return &ecdsa_info; +#endif + default: + return NULL; + } } /* @@ -99,7 +105,6 @@ void pk_free( pk_context *ctx ) */ int pk_set_type( pk_context *ctx, pk_type_t type ) { - size_t size; const pk_info_t *info; if( ctx->type == type ) @@ -108,41 +113,12 @@ int pk_set_type( pk_context *ctx, pk_type_t type ) if( ctx->type != POLARSSL_PK_NONE ) return( POLARSSL_ERR_PK_TYPE_MISMATCH ); -#if defined(POLARSSL_RSA_C) - if( type == POLARSSL_PK_RSA ) - { - size = sizeof( rsa_context ); - info = &rsa_info; - } - else -#endif -#if defined(POLARSSL_ECP_C) - if( type == POLARSSL_PK_ECKEY ) - { - size = sizeof( ecp_keypair ); - info = &eckey_info; - } - else if( type == POLARSSL_PK_ECKEY_DH ) - { - size = sizeof( ecp_keypair ); - info = &eckeydh_info; - } - else -#endif -#if defined(POLARSSL_ECDSA_C) - if( type == POLARSSL_PK_ECDSA ) - { - size = sizeof( ecdsa_context ); - info = &ecdsa_info; - } - else -#endif + if( ( info = pk_info_from_type( type ) ) == NULL ) return( POLARSSL_ERR_PK_TYPE_MISMATCH ); - if( ( ctx->data = polarssl_malloc( size ) ) == NULL ) + if( ( ctx->data = info->ctx_alloc_func() ) == NULL ) return( POLARSSL_ERR_PK_MALLOC_FAILED ); - memset( ctx->data, 0, size ); ctx->type = type; ctx->info = info; diff --git a/library/pk_wrap.c b/library/pk_wrap.c index f8985912c8..50e8db52ec 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -39,6 +39,14 @@ #include "polarssl/ecdsa.h" #endif +#if defined(POLARSSL_MEMORY_C) +#include "polarssl/memory.h" +#else +#include +#define polarssl_malloc malloc +#define polarssl_free free +#endif + #if defined(POLARSSL_RSA_C) static int rsa_can_do( pk_type_t type ) { @@ -60,12 +68,30 @@ static int rsa_verify_wrap( void *ctx, RSA_PUBLIC, md_info->type, 0, hash, sig ) ); } +static void *rsa_alloc_wrap( void ) +{ + void *ctx = polarssl_malloc( sizeof( rsa_context ) ); + + if( ctx != NULL ) + rsa_init( (rsa_context *) ctx, 0, 0 ); + + return ctx; +} + +static void rsa_free_wrap( void *ctx ) +{ + rsa_free( (rsa_context *) ctx ); + polarssl_free( ctx ); +} + const pk_info_t rsa_info = { POLARSSL_PK_RSA, "RSA", rsa_get_size, rsa_can_do, rsa_verify_wrap, + rsa_alloc_wrap, + rsa_free_wrap, }; #endif /* POLARSSL_RSA_C */ @@ -88,12 +114,30 @@ int ecdsa_verify_wrap( void *ctx, hash, md_info->size, sig, sig_len ) ); } +static void *ecdsa_alloc_wrap( void ) +{ + void *ctx = polarssl_malloc( sizeof( ecdsa_context ) ); + + if( ctx != NULL ) + ecdsa_init( (ecdsa_context *) ctx ); + + return( ctx ); +} + +static void ecdsa_free_wrap( void *ctx ) +{ + ecdsa_free( (ecdsa_context *) ctx ); + polarssl_free( ctx ); +} + const pk_info_t ecdsa_info = { POLARSSL_PK_ECDSA, "ECDSA", ecdsa_get_size, ecdsa_can_do, ecdsa_verify_wrap, + ecdsa_alloc_wrap, + ecdsa_free_wrap, }; #endif /* POLARSSL_ECDSA_C */ @@ -140,12 +184,30 @@ static int eckey_verify_wrap( void *ctx, #endif /* POLARSSL_ECDSA_C */ } +static void *eckey_alloc_wrap( void ) +{ + void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); + + if( ctx != NULL ) + ecp_keypair_init( ctx ); + + return( ctx ); +} + +static void eckey_free_wrap( void *ctx ) +{ + ecp_keypair_free( (ecp_keypair *) ctx ); + polarssl_free( ctx ); +} + const pk_info_t eckey_info = { POLARSSL_PK_ECKEY, "EC", eckey_get_size, eckey_can_do, eckey_verify_wrap, + eckey_alloc_wrap, + eckey_free_wrap, }; /* @@ -176,5 +238,7 @@ const pk_info_t eckeydh_info = { eckey_get_size, /* Same underlying key structure */ eckeydh_can_do, eckeydh_verify_wrap, + eckey_alloc_wrap, /* Same underlying key structure */ + eckey_free_wrap, /* Same underlying key structure */ }; #endif /* POLARSSL_ECP_C */