PK: use alloc and free function pointers
diff --git a/include/polarssl/pk.h b/include/polarssl/pk.h
index 8626b60..83dc2d0 100644
--- a/include/polarssl/pk.h
+++ b/include/polarssl/pk.h
@@ -102,6 +102,13 @@
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 19bc79b..9f33641 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -65,33 +65,39 @@
*/
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 @@
*/
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 @@
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 f898591..50e8db5 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 <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 @@
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 @@
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 @@
#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 @@
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 */