mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-25 13:43:31 +00:00
PK: use alloc and free function pointers
This commit is contained in:
parent
3053f5bcb4
commit
765db07dfb
@ -102,6 +102,13 @@ typedef struct
|
|||||||
int (*verify_func)( void *ctx,
|
int (*verify_func)( void *ctx,
|
||||||
const unsigned char *hash, const md_info_t *md_info,
|
const unsigned char *hash, const md_info_t *md_info,
|
||||||
const unsigned char *sig, size_t sig_len );
|
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;
|
} pk_info_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
84
library/pk.c
84
library/pk.c
@ -65,33 +65,39 @@ void pk_init( pk_context *ctx )
|
|||||||
*/
|
*/
|
||||||
void pk_free( pk_context *ctx )
|
void pk_free( pk_context *ctx )
|
||||||
{
|
{
|
||||||
if( ctx == NULL )
|
if( ctx == NULL || ctx->info == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
ctx->info->ctx_free_func( ctx->data );
|
||||||
if( ctx->type == POLARSSL_PK_RSA )
|
ctx->data = NULL;
|
||||||
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 = NULL;
|
ctx->info = NULL;
|
||||||
ctx->type = POLARSSL_PK_NONE;
|
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 )
|
int pk_set_type( pk_context *ctx, pk_type_t type )
|
||||||
{
|
{
|
||||||
size_t size;
|
|
||||||
const pk_info_t *info;
|
const pk_info_t *info;
|
||||||
|
|
||||||
if( ctx->type == type )
|
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 )
|
if( ctx->type != POLARSSL_PK_NONE )
|
||||||
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
|
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
if( ( info = pk_info_from_type( type ) ) == NULL )
|
||||||
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
|
|
||||||
return( POLARSSL_ERR_PK_TYPE_MISMATCH );
|
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 );
|
return( POLARSSL_ERR_PK_MALLOC_FAILED );
|
||||||
|
|
||||||
memset( ctx->data, 0, size );
|
|
||||||
ctx->type = type;
|
ctx->type = type;
|
||||||
ctx->info = info;
|
ctx->info = info;
|
||||||
|
|
||||||
|
@ -39,6 +39,14 @@
|
|||||||
#include "polarssl/ecdsa.h"
|
#include "polarssl/ecdsa.h"
|
||||||
#endif
|
#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)
|
#if defined(POLARSSL_RSA_C)
|
||||||
static int rsa_can_do( pk_type_t type )
|
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 ) );
|
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 = {
|
const pk_info_t rsa_info = {
|
||||||
POLARSSL_PK_RSA,
|
POLARSSL_PK_RSA,
|
||||||
"RSA",
|
"RSA",
|
||||||
rsa_get_size,
|
rsa_get_size,
|
||||||
rsa_can_do,
|
rsa_can_do,
|
||||||
rsa_verify_wrap,
|
rsa_verify_wrap,
|
||||||
|
rsa_alloc_wrap,
|
||||||
|
rsa_free_wrap,
|
||||||
};
|
};
|
||||||
#endif /* POLARSSL_RSA_C */
|
#endif /* POLARSSL_RSA_C */
|
||||||
|
|
||||||
@ -88,12 +114,30 @@ int ecdsa_verify_wrap( void *ctx,
|
|||||||
hash, md_info->size, sig, sig_len ) );
|
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 = {
|
const pk_info_t ecdsa_info = {
|
||||||
POLARSSL_PK_ECDSA,
|
POLARSSL_PK_ECDSA,
|
||||||
"ECDSA",
|
"ECDSA",
|
||||||
ecdsa_get_size,
|
ecdsa_get_size,
|
||||||
ecdsa_can_do,
|
ecdsa_can_do,
|
||||||
ecdsa_verify_wrap,
|
ecdsa_verify_wrap,
|
||||||
|
ecdsa_alloc_wrap,
|
||||||
|
ecdsa_free_wrap,
|
||||||
};
|
};
|
||||||
#endif /* POLARSSL_ECDSA_C */
|
#endif /* POLARSSL_ECDSA_C */
|
||||||
|
|
||||||
@ -140,12 +184,30 @@ static int eckey_verify_wrap( void *ctx,
|
|||||||
#endif /* POLARSSL_ECDSA_C */
|
#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 = {
|
const pk_info_t eckey_info = {
|
||||||
POLARSSL_PK_ECKEY,
|
POLARSSL_PK_ECKEY,
|
||||||
"EC",
|
"EC",
|
||||||
eckey_get_size,
|
eckey_get_size,
|
||||||
eckey_can_do,
|
eckey_can_do,
|
||||||
eckey_verify_wrap,
|
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 */
|
eckey_get_size, /* Same underlying key structure */
|
||||||
eckeydh_can_do,
|
eckeydh_can_do,
|
||||||
eckeydh_verify_wrap,
|
eckeydh_verify_wrap,
|
||||||
|
eckey_alloc_wrap, /* Same underlying key structure */
|
||||||
|
eckey_free_wrap, /* Same underlying key structure */
|
||||||
};
|
};
|
||||||
#endif /* POLARSSL_ECP_C */
|
#endif /* POLARSSL_ECP_C */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user