Commit 765db07d authored by Manuel Pégourié-Gonnard's avatar Manuel Pégourié-Gonnard
Browse files

PK: use alloc and free function pointers

parent 3053f5bc
......@@ -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;
/**
......
......@@ -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;
ctx->info->ctx_free_func( ctx->data );
ctx->data = NULL;
ctx->info = NULL;
ctx->type = POLARSSL_PK_NONE;
}
/*
* 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)
if( ctx->type == POLARSSL_PK_RSA )
rsa_free( ctx->data );
else
case POLARSSL_PK_RSA:
return &rsa_info;
#endif
#if defined(POLARSSL_ECP_C)
if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
ecp_keypair_free( ctx->data );
else
case POLARSSL_PK_ECKEY:
return &eckey_info;
case POLARSSL_PK_ECKEY_DH:
return &eckeydh_info;
#endif
#if defined(POLARSSL_ECDSA_C)
if( ctx->type == POLARSSL_PK_ECDSA )
ecdsa_free( ctx->data );
else
case POLARSSL_PK_ECDSA:
return &ecdsa_info;
#endif
{
; /* guard for the else's above */
default:
return NULL;
}
polarssl_free( ctx->data );
ctx->info = NULL;
ctx->type = POLARSSL_PK_NONE;
ctx->data = 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;
......
......@@ -39,6 +39,14 @@
#include "polarssl/ecdsa.h"
#endif
#if defined(POLARSSL_MEMORY_C)
#include "polarssl/memory.h"
#else
#include <stdlib.h>
#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 */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment