From 373deea06df630293f2110e0ff195393af91c0d4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 26 Oct 2017 12:03:35 +0200 Subject: [PATCH] pk_internal: pass context to can_do In the mbedtls_pk_info_t method can_do, pass the context data. This will be needed for opaque keys, where the info structure depends on the method to access the opaque key and not on the key type. --- include/mbedtls/pk_internal.h | 4 ++-- library/pk.c | 2 +- library/pk_wrap.c | 15 ++++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/mbedtls/pk_internal.h b/include/mbedtls/pk_internal.h index aab3db704..592eb4b06 100644 --- a/include/mbedtls/pk_internal.h +++ b/include/mbedtls/pk_internal.h @@ -41,10 +41,10 @@ struct mbedtls_pk_info_t const char *name; /** Get key size in bits */ - size_t (*get_bitlen)( const void * ); + size_t (*get_bitlen)( const void *ctx ); /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ - int (*can_do)( mbedtls_pk_type_t type ); + int (*can_do)( const void * ctx, mbedtls_pk_type_t type ); /** Verify signature */ int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg, diff --git a/library/pk.c b/library/pk.c index b52c73fbc..9037646de 100644 --- a/library/pk.c +++ b/library/pk.c @@ -154,7 +154,7 @@ int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ) if( ctx == NULL || ctx->pk_info == NULL ) return( 0 ); - return( ctx->pk_info->can_do( type ) ); + return( ctx->pk_info->can_do( ctx->pk_ctx, type ) ); } /* diff --git a/library/pk_wrap.c b/library/pk_wrap.c index a4bb35fc8..55be5954e 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -60,8 +60,9 @@ static void mbedtls_zeroize( void *v, size_t n ) { #endif #if defined(MBEDTLS_RSA_C) -static int rsa_can_do( mbedtls_pk_type_t type ) +static int rsa_can_do( const void *ctx, mbedtls_pk_type_t type ) { + (void) ctx; return( type == MBEDTLS_PK_RSA || type == MBEDTLS_PK_RSASSA_PSS ); } @@ -201,8 +202,9 @@ const mbedtls_pk_info_t mbedtls_rsa_info = { /* * Generic EC key */ -static int eckey_can_do( mbedtls_pk_type_t type ) +static int eckey_can_do( const void *ctx, mbedtls_pk_type_t type ) { + (void) ctx; return( type == MBEDTLS_PK_ECKEY || type == MBEDTLS_PK_ECKEY_DH || type == MBEDTLS_PK_ECDSA ); @@ -314,8 +316,9 @@ const mbedtls_pk_info_t mbedtls_eckey_info = { /* * EC key restricted to ECDH */ -static int eckeydh_can_do( mbedtls_pk_type_t type ) +static int eckeydh_can_do( const void *ctx, mbedtls_pk_type_t type ) { + (void) ctx; return( type == MBEDTLS_PK_ECKEY || type == MBEDTLS_PK_ECKEY_DH ); } @@ -337,8 +340,9 @@ const mbedtls_pk_info_t mbedtls_eckeydh_info = { #endif /* MBEDTLS_ECP_C */ #if defined(MBEDTLS_ECDSA_C) -static int ecdsa_can_do( mbedtls_pk_type_t type ) +static int ecdsa_can_do( const void *ctx, mbedtls_pk_type_t type ) { + (void) ctx; return( type == MBEDTLS_PK_ECDSA ); } @@ -404,8 +408,9 @@ const mbedtls_pk_info_t mbedtls_ecdsa_info = { * Support for alternative RSA-private implementations */ -static int rsa_alt_can_do( mbedtls_pk_type_t type ) +static int rsa_alt_can_do( const void *ctx, mbedtls_pk_type_t type ) { + (void) ctx; return( type == MBEDTLS_PK_RSA ); }