PK: support for opaque keys
Add a new key pair object type: MBEDTLS_PK_OPAQUE, intended for
implementations of asymmetric cryptography operations that call an
external cryptographic module.
External cryptographic module engines must implement the API described
by a mbedtls_pk_info_t structure and, usually, a custom setup function.
Document the fields of the mbedtls_pk_info_t structure and the
requirements on a PK engine. Also document non-obvious aspects of the
behavior of the pk interface functions on opaque keys.
Change the interface of check_pair_func to take a pointer to a full
mbedtls_pk_context as its pub argument, and not just the data part of
the context. This is necessary because when prv is opaque, pub may
legitimately be of a different type (typically prv would be opaque and
pub would be transparent).
diff --git a/library/pk.c b/library/pk.c
index d080c75..d8801b5 100644
--- a/library/pk.c
+++ b/library/pk.c
@@ -94,6 +94,7 @@
return( &mbedtls_ecdsa_info );
#endif
/* MBEDTLS_PK_RSA_ALT omitted on purpose */
+ /* MBEDTLS_PK_OPAQUE omitted on purpose: they can't be built by parsing */
default:
return( NULL );
}
@@ -107,8 +108,11 @@
if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
- if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
- return( MBEDTLS_ERR_PK_ALLOC_FAILED );
+ if( info->ctx_alloc_func != NULL )
+ {
+ if( ( ctx->pk_ctx = info->ctx_alloc_func( ) ) == NULL )
+ return( MBEDTLS_ERR_PK_ALLOC_FAILED );
+ }
ctx->pk_info = info;
@@ -312,24 +316,31 @@
int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
{
if( pub == NULL || pub->pk_info == NULL ||
- prv == NULL || prv->pk_info == NULL ||
- prv->pk_info->check_pair_func == NULL )
+ prv == NULL || prv->pk_info == NULL )
{
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
}
+ if( pub->pk_info == prv->pk_info && pub->pk_ctx == prv->pk_ctx )
+ return( 0 );
+
+ if( prv->pk_info->check_pair_func == NULL )
+ {
+ return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
+ }
+
if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
{
if( pub->pk_info->type != MBEDTLS_PK_RSA )
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
}
- else
+ else if( prv->pk_info->type != MBEDTLS_PK_OPAQUE )
{
if( pub->pk_info != prv->pk_info )
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
}
- return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
+ return( prv->pk_info->check_pair_func( pub, prv->pk_ctx ) );
}
/*
@@ -384,7 +395,9 @@
}
/*
- * Access the PK type
+ * Access the PK type.
+ * For an opaque key pair object, this does not give any information on the
+ * underlying cryptographic material.
*/
mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
{