Add #ifdef's on RSA and EC in PK

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-09 10:35:54 +02:00
parent 1f73a65c06
commit 81c313ccc6
2 changed files with 34 additions and 8 deletions

View File

@ -38,9 +38,13 @@ extern "C" {
*/ */
typedef enum { typedef enum {
POLARSSL_PK_NONE=0, POLARSSL_PK_NONE=0,
#if defined(POLARSSL_RSA_C)
POLARSSL_PK_RSA, POLARSSL_PK_RSA,
#endif
#if defined(POLARSSL_ECP_C)
POLARSSL_PK_ECKEY, POLARSSL_PK_ECKEY,
POLARSSL_PK_ECKEY_DH, POLARSSL_PK_ECKEY_DH,
#endif
} pk_type_t; } pk_type_t;
/** /**

View File

@ -25,10 +25,15 @@
#include "polarssl/config.h" #include "polarssl/config.h"
#include "polarssl/rsa.h"
#include "polarssl/ecp.h"
#include "polarssl/pk.h" #include "polarssl/pk.h"
#if defined(POLARSSL_RSA_C)
#include "polarssl/rsa.h"
#endif
#if defined(POLARSSL_ECP_C)
#include "polarssl/ecp.h"
#endif
#include <stdlib.h> #include <stdlib.h>
/* /*
@ -56,14 +61,18 @@ void pk_free( pk_context *ctx )
case POLARSSL_PK_NONE: case POLARSSL_PK_NONE:
break; break;
#if defined(POLARSSL_RSA_C)
case POLARSSL_PK_RSA: case POLARSSL_PK_RSA:
rsa_free( ctx->data ); rsa_free( ctx->data );
break; break;
#endif
#if defined(POLARSSL_ECP_C)
case POLARSSL_PK_ECKEY: case POLARSSL_PK_ECKEY:
case POLARSSL_PK_ECKEY_DH: case POLARSSL_PK_ECKEY_DH:
ecp_keypair_free( ctx->data ); ecp_keypair_free( ctx->data );
break; break;
#endif
} }
free( ctx-> data ); free( ctx-> data );
@ -77,13 +86,26 @@ 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 = type == POLARSSL_PK_RSA ? sizeof( rsa_context ) size_t size = 0;
: type == POLARSSL_PK_ECKEY ? sizeof( ecp_keypair )
: type == POLARSSL_PK_ECKEY_DH ? sizeof( ecp_keypair )
: 0;
if( size == 0 ) switch( type )
return( 0 ); {
#if defined(POLARSSL_RSA_C)
case POLARSSL_PK_RSA:
size = sizeof( rsa_context );
break;
#endif
#if defined(POLARSSL_ECP_C)
case POLARSSL_PK_ECKEY:
case POLARSSL_PK_ECKEY_DH:
size = sizeof( ecp_keypair );
break;
#endif
case POLARSSL_PK_NONE:
; /* Should not happen */
}
if( ( ctx->data = malloc( size ) ) == NULL ) if( ( ctx->data = malloc( size ) ) == NULL )
return( POLARSSL_ERR_PK_MALLOC_FAILED ); return( POLARSSL_ERR_PK_MALLOC_FAILED );