pk: store opaque key ID directly in the pk_context structure
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
parent
71f88ecc52
commit
e00954d0ed
3 changed files with 19 additions and 31 deletions
|
@ -40,7 +40,7 @@
|
||||||
#include "mbedtls/ecdsa.h"
|
#include "mbedtls/ecdsa.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
#include "psa/crypto.h"
|
#include "psa/crypto.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -234,10 +234,17 @@ typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Public key container
|
* \brief Public key container
|
||||||
|
*
|
||||||
|
* \note The opaque_id is guarded by MBEDTLS_PSA_CRYPTO_C and not
|
||||||
|
* only by MBEDTLS_USE_PSA_CRYPTO because it can be used also
|
||||||
|
* in mbedtls_pk_sign_ext for RSA keys.
|
||||||
*/
|
*/
|
||||||
typedef struct mbedtls_pk_context {
|
typedef struct mbedtls_pk_context {
|
||||||
const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
|
const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
|
||||||
void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
|
void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
|
||||||
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
mbedtls_svc_key_id_t MBEDTLS_PRIVATE(opaque_id); /**< Key ID for opaque keys */
|
||||||
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||||
} mbedtls_pk_context;
|
} mbedtls_pk_context;
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
||||||
|
|
17
library/pk.c
17
library/pk.c
|
@ -60,6 +60,9 @@ void mbedtls_pk_init(mbedtls_pk_context *ctx)
|
||||||
{
|
{
|
||||||
ctx->pk_info = NULL;
|
ctx->pk_info = NULL;
|
||||||
ctx->pk_ctx = NULL;
|
ctx->pk_ctx = NULL;
|
||||||
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
ctx->opaque_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||||
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -71,7 +74,7 @@ void mbedtls_pk_free(mbedtls_pk_context *ctx)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->pk_info != NULL) {
|
if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
|
||||||
ctx->pk_info->ctx_free_func(ctx->pk_ctx);
|
ctx->pk_info->ctx_free_func(ctx->pk_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +143,8 @@ int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
|
||||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
|
if ((info->ctx_alloc_func == NULL) ||
|
||||||
|
((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
|
||||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +162,6 @@ int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
|
||||||
{
|
{
|
||||||
const mbedtls_pk_info_t *info = NULL;
|
const mbedtls_pk_info_t *info = NULL;
|
||||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||||
mbedtls_svc_key_id_t *pk_ctx;
|
|
||||||
psa_key_type_t type;
|
psa_key_type_t type;
|
||||||
|
|
||||||
if (ctx == NULL || ctx->pk_info != NULL) {
|
if (ctx == NULL || ctx->pk_info != NULL) {
|
||||||
|
@ -179,14 +182,8 @@ int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
|
||||||
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
|
|
||||||
return MBEDTLS_ERR_PK_ALLOC_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->pk_info = info;
|
ctx->pk_info = info;
|
||||||
|
ctx->opaque_id = key;
|
||||||
pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
|
|
||||||
*pk_ctx = key;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1503,22 +1503,6 @@ const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
|
||||||
#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
|
#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
|
||||||
|
|
||||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
|
|
||||||
static void *pk_opaque_alloc_wrap(void)
|
|
||||||
{
|
|
||||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
|
|
||||||
|
|
||||||
/* no _init() function to call, as calloc() already zeroized */
|
|
||||||
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void pk_opaque_free_wrap(void *ctx)
|
|
||||||
{
|
|
||||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
|
|
||||||
mbedtls_free(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
|
static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
|
||||||
{
|
{
|
||||||
const mbedtls_svc_key_id_t *key = pk->pk_ctx;
|
const mbedtls_svc_key_id_t *key = pk->pk_ctx;
|
||||||
|
@ -1635,8 +1619,8 @@ const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
|
||||||
NULL, /* decrypt - not relevant */
|
NULL, /* decrypt - not relevant */
|
||||||
NULL, /* encrypt - not relevant */
|
NULL, /* encrypt - not relevant */
|
||||||
NULL, /* check_pair - could be done later or left NULL */
|
NULL, /* check_pair - could be done later or left NULL */
|
||||||
pk_opaque_alloc_wrap,
|
NULL, /* alloc - no need to allocate new data dynamically */
|
||||||
pk_opaque_free_wrap,
|
NULL, /* free - as for the alloc, there is no data to free */
|
||||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
||||||
NULL, /* restart alloc - not relevant */
|
NULL, /* restart alloc - not relevant */
|
||||||
NULL, /* restart free - not relevant */
|
NULL, /* restart free - not relevant */
|
||||||
|
@ -1687,8 +1671,8 @@ const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
|
||||||
#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
|
#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
|
||||||
NULL, /* encrypt - will be done later */
|
NULL, /* encrypt - will be done later */
|
||||||
NULL, /* check_pair - could be done later or left NULL */
|
NULL, /* check_pair - could be done later or left NULL */
|
||||||
pk_opaque_alloc_wrap,
|
NULL, /* alloc - no need to allocate new data dynamically */
|
||||||
pk_opaque_free_wrap,
|
NULL, /* free - as for the alloc, there is no data to free */
|
||||||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
|
||||||
NULL, /* restart alloc - not relevant */
|
NULL, /* restart alloc - not relevant */
|
||||||
NULL, /* restart free - not relevant */
|
NULL, /* restart free - not relevant */
|
||||||
|
|
Loading…
Reference in a new issue