|
45 | 45 | #include "ecdsa.h"
|
46 | 46 | #endif
|
47 | 47 |
|
| 48 | +#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 49 | +#include "psa/crypto.h" |
| 50 | +#endif |
| 51 | + |
48 | 52 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
49 | 53 | !defined(inline) && !defined(__cplusplus)
|
50 | 54 | #define inline __inline
|
@@ -83,6 +87,7 @@ typedef enum {
|
83 | 87 | MBEDTLS_PK_ECDSA,
|
84 | 88 | MBEDTLS_PK_RSA_ALT,
|
85 | 89 | MBEDTLS_PK_RSASSA_PSS,
|
| 90 | + MBEDTLS_PK_OPAQUE, |
86 | 91 | } mbedtls_pk_type_t;
|
87 | 92 |
|
88 | 93 | /**
|
@@ -203,6 +208,11 @@ void mbedtls_pk_init( mbedtls_pk_context *ctx );
|
203 | 208 |
|
204 | 209 | /**
|
205 | 210 | * \brief Free a mbedtls_pk_context
|
| 211 | + * |
| 212 | + * \note For contexts that have been set up with |
| 213 | + * mbedtls_pk_setup_opaque(), this does not free the underlying |
| 214 | + * key slot and you still need to call psa_destroy_key() |
| 215 | + * independently if you want to destroy that key. |
206 | 216 | */
|
207 | 217 | void mbedtls_pk_free( mbedtls_pk_context *ctx );
|
208 | 218 |
|
@@ -234,6 +244,38 @@ void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
|
234 | 244 | */
|
235 | 245 | int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
|
236 | 246 |
|
| 247 | +#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 248 | +/** |
| 249 | + * \brief Initialize a PK context to wrap a PSA key slot. |
| 250 | + * |
| 251 | + * \note This function replaces mbedtls_pk_setup() for contexts |
| 252 | + * that wrap a (possibly opaque) PSA key slot instead of |
| 253 | + * storing and manipulating the key material directly. |
| 254 | + * |
| 255 | + * \param ctx The context to initialize. It must be empty (type NONE). |
| 256 | + * \param key The PSA key slot to wrap, which must hold an ECC key pair |
| 257 | + * (see notes below). |
| 258 | + * |
| 259 | + * \note The wrapped key slot must remain valid as long as the |
| 260 | + * wrapping PK context is in use, that is at least between |
| 261 | + * the point this function is called and the point |
| 262 | + * mbedtls_pk_free() is called on this context. The wrapped |
| 263 | + * key slot might then be independently used or destroyed. |
| 264 | + * |
| 265 | + * \note This function is currently only available for ECC key |
| 266 | + * pairs (that is, ECC keys containing private key material). |
| 267 | + * Support for other key types may be added later. |
| 268 | + * |
| 269 | + * \return \c 0 on success. |
| 270 | + * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input |
| 271 | + * (context already used, invalid key slot). |
| 272 | + * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an |
| 273 | + * ECC key pair. |
| 274 | + * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure. |
| 275 | + */ |
| 276 | +int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_slot_t key ); |
| 277 | +#endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 278 | + |
237 | 279 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
238 | 280 | /**
|
239 | 281 | * \brief Initialize an RSA-alt context
|
@@ -480,7 +522,11 @@ int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
|
480 | 522 | * \param pub Context holding a public key.
|
481 | 523 | * \param prv Context holding a private (and public) key.
|
482 | 524 | *
|
483 |
| - * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA |
| 525 | + * \return \c 0 on success (keys were checked and match each other). |
| 526 | + * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not |
| 527 | + * be checked - in that case they may or may not match. |
| 528 | + * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid. |
| 529 | + * \return Another non-zero value if the keys do not match. |
484 | 530 | */
|
485 | 531 | int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
|
486 | 532 |
|
@@ -694,6 +740,31 @@ int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
|
694 | 740 | int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
|
695 | 741 | #endif
|
696 | 742 |
|
| 743 | +#if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 744 | +/** |
| 745 | + * \brief Turn an EC key into an Opaque one |
| 746 | + * |
| 747 | + * \warning This is a temporary utility function for tests. It might |
| 748 | + * change or be removed at any time without notice. |
| 749 | + * |
| 750 | + * \note Only ECDSA keys are supported so far. Signing with the |
| 751 | + * specified hash is the only allowed use of that key. |
| 752 | + * |
| 753 | + * \param pk Input: the EC key to transfer to a PSA key slot. |
| 754 | + * Output: a PK context wrapping that PSA key slot. |
| 755 | + * \param slot Output: the chosen slot for storing the key. |
| 756 | + * It's the caller's responsibility to destroy that slot |
| 757 | + * after calling mbedtls_pk_free() on the PK context. |
| 758 | + * \param hash_alg The hash algorithm to allow for use with that key. |
| 759 | + * |
| 760 | + * \return \c 0 if successful. |
| 761 | + * \return An Mbed TLS error code otherwise. |
| 762 | + */ |
| 763 | +int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk, |
| 764 | + psa_key_slot_t *slot, |
| 765 | + psa_algorithm_t hash_alg ); |
| 766 | +#endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 767 | + |
697 | 768 | #ifdef __cplusplus
|
698 | 769 | }
|
699 | 770 | #endif
|
|
0 commit comments