pk.h
mbedtls/private_access.h
mbedtls/build_info.h
mbedtls/md.h
mbedtls/rsa.h
mbedtls/ecp.h
mbedtls/ecdsa.h
psa/crypto.h
oid.h
ssl_ciphersuites.h
x509.h
mbedtls/bignum.h
mbedtls/build_info.h
mbedtls/ccm.h
mbedtls/chacha20.h
mbedtls/chachapoly.h
mbedtls/check_config.h
mbedtls/cipher.h
mbedtls/cmac.h
mbedtls/config_adjust_legacy_crypto.h
mbedtls/config_adjust_psa_from_legacy.h
mbedtls/config_adjust_psa_superset_legacy.h
mbedtls/config_adjust_ssl.h
mbedtls/config_adjust_x509.h
mbedtls/config_psa.h
mbedtls/ecdsa.h
mbedtls/ecjpake.h
mbedtls/ecp.h
mbedtls/gcm.h
mbedtls/mbedtls_config.h
mbedtls/md.h
mbedtls/md5.h
pk.h
mbedtls/platform_time.h
mbedtls/platform_util.h
mbedtls/poly1305.h
mbedtls/private_access.h
mbedtls/ripemd160.h
mbedtls/rsa.h
mbedtls/sha1.h
mbedtls/sha256.h
mbedtls/sha3.h
mbedtls/sha512.h
mbedtls/threading.h
psa/build_info.h
psa/crypto.h
psa/crypto_adjust_auto_enabled.h
psa/crypto_adjust_config_dependencies.h
psa/crypto_adjust_config_key_pair_types.h
psa/crypto_adjust_config_synonyms.h
psa/crypto_builtin_composites.h
psa/crypto_builtin_key_derivation.h
psa/crypto_builtin_primitives.h
crypto_compat.h
psa/crypto_driver_common.h
psa/crypto_driver_contexts_composites.h
psa/crypto_driver_contexts_key_derivation.h
psa/crypto_driver_contexts_primitives.h
crypto_extra.h
psa/crypto_legacy.h
crypto_platform.h
crypto_sizes.h
crypto_struct.h
crypto_types.h
crypto_values.h
inttypes.h
limits.h
pthread.h
stddef.h
stdint.h
stdio.h
stdlib.h
time.h
debug.h
net_sockets.h
oid.h
pk.h
pkcs7.h
ssl.h
ssl_cache.h
ssl_ciphersuites.h
ssl_cookie.h
ssl_ticket.h
x509.h
x509_crl.h
x509_crt.h
x509_csr.h
mbedtls_pk_rsassa_pss_options
mbedtls_pk_debug_item
mbedtls_pk_context
MBEDTLS_ERR_PK_ALLOC_FAILED
-0x3F80

Memory allocation failed.

MBEDTLS_ERR_PK_TYPE_MISMATCH
-0x3F00

Type mismatch, eg attempt to encrypt with an ECDSA key

MBEDTLS_ERR_PK_BAD_INPUT_DATA
-0x3E80

Bad input parameters to function.

MBEDTLS_ERR_PK_FILE_IO_ERROR
-0x3E00

Read/write of file failed.

MBEDTLS_ERR_PK_KEY_INVALID_VERSION
-0x3D80

Unsupported key version

MBEDTLS_ERR_PK_KEY_INVALID_FORMAT
-0x3D00

Invalid key tag or value.

MBEDTLS_ERR_PK_UNKNOWN_PK_ALG
-0x3C80

Key algorithm is unsupported (only RSA and EC are supported).

MBEDTLS_ERR_PK_PASSWORD_REQUIRED
-0x3C00

Private key password can't be empty.

MBEDTLS_ERR_PK_PASSWORD_MISMATCH
-0x3B80

Given private key password does not allow for correct decryption.

MBEDTLS_ERR_PK_INVALID_PUBKEY
-0x3B00

The pubkey tag or value is invalid (only RSA and EC are supported).

MBEDTLS_ERR_PK_INVALID_ALG
-0x3A80

The algorithm tag or value is invalid.

MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE
-0x3A00

Elliptic curve is unsupported (only NIST curves are supported).

MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE
-0x3980

Unavailable feature, e.g. RSA disabled for RSA key.

MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
-0x3900

The buffer contains a valid signature followed by more data.

MBEDTLS_ERR_PK_BUFFER_TOO_SMALL
-0x3880

The output buffer is too small.

MBEDTLS_PK_SIGNATURE_MAX_SIZE
0

Maximum size of a signature made by mbedtls_pk_sign().

MBEDTLS_PK_DEBUG_MAX_ITEMS
3

Maximum number of item send for debugging, plus 1

MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN
PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE
(PSA_VENDOR_ECC_MAX_CURVE_BITS)
mbedtls_pk_type_t
MBEDTLS_PK_NONE
=0
MBEDTLS_PK_RSA
MBEDTLS_PK_ECKEY
MBEDTLS_PK_ECKEY_DH
MBEDTLS_PK_ECDSA
MBEDTLS_PK_RSA_ALT
MBEDTLS_PK_RSASSA_PSS
MBEDTLS_PK_OPAQUE

Public key types.

mbedtls_pk_debug_type
MBEDTLS_PK_DEBUG_NONE
= 0
MBEDTLS_PK_DEBUG_MPI
MBEDTLS_PK_DEBUG_ECP
MBEDTLS_PK_DEBUG_PSA_EC

Types for interfacing with the debug module.

struct mbedtls_pk_rsassa_pss_options typedef struct mbedtls_pk_rsassa_pss_options mbedtls_pk_rsassa_pss_options mbedtls_pk_rsassa_pss_options

Options for RSASSA-PSS signature verification. See `mbedtls_rsa_rsassa_pss_verify_ext()`

struct mbedtls_pk_debug_item typedef struct mbedtls_pk_debug_item mbedtls_pk_debug_item mbedtls_pk_debug_item

Item to send to the debug module.

struct mbedtls_pk_info_t typedef struct mbedtls_pk_info_t mbedtls_pk_info_t mbedtls_pk_info_t

Public key information and operations.

The library does not support custom pk info structures, only built-in structures returned by mbedtls_cipher_info_from_type().

struct mbedtls_pk_context typedef struct mbedtls_pk_context mbedtls_pk_context mbedtls_pk_context

Public key container.

void typedef void mbedtls_pk_restart_ctx mbedtls_pk_restart_ctx
int(* typedef int(* mbedtls_pk_rsa_alt_decrypt_func) (void *ctx, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len) )(void *ctx, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len) mbedtls_pk_rsa_alt_decrypt_func

Types for RSA-alt abstraction.

int(* typedef int(* mbedtls_pk_rsa_alt_sign_func) (void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig) )(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig) mbedtls_pk_rsa_alt_sign_func
size_t(* typedef size_t(* mbedtls_pk_rsa_alt_key_len_func) (void *ctx) )(void *ctx) mbedtls_pk_rsa_alt_key_len_func
const mbedtls_pk_info_t * const mbedtls_pk_info_t * mbedtls_pk_info_from_type (mbedtls_pk_type_t pk_type) mbedtls_pk_info_from_type mbedtls_pk_type_t pk_type

Return information associated with the given PK type.

pk_type

PK type to search for.

The PK info associated with the type or NULL if not found.

void void mbedtls_pk_init (mbedtls_pk_context *ctx) mbedtls_pk_init mbedtls_pk_context * ctx

Initialize a mbedtls_pk_context (as NONE).

ctx

The context to initialize. This must not be `NULL`.

void void mbedtls_pk_free (mbedtls_pk_context *ctx) mbedtls_pk_free mbedtls_pk_context * ctx

Free the components of a mbedtls_pk_context.

ctx

The context to clear. It must have been initialized. If this is `NULL`, this function does nothing.

For contexts that have been set up with mbedtls_pk_setup_opaque(), this does not free the underlying PSA key and you still need to call psa_destroy_key() independently if you want to destroy that key.

int int mbedtls_pk_setup (mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) mbedtls_pk_setup mbedtls_pk_context * ctx const mbedtls_pk_info_t * info

Initialize a PK context with the information given and allocates the type-specific PK subcontext.

ctx

Context to initialize. It must not have been set up yet (type MBEDTLS_PK_NONE).

info

Information to use

0 on success, MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input, MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.

For contexts holding an RSA-alt key, use `mbedtls_pk_setup_rsa_alt()` instead.

int int mbedtls_pk_setup_rsa_alt (mbedtls_pk_context *ctx, void *key, mbedtls_pk_rsa_alt_decrypt_func decrypt_func, mbedtls_pk_rsa_alt_sign_func sign_func, mbedtls_pk_rsa_alt_key_len_func key_len_func) mbedtls_pk_setup_rsa_alt mbedtls_pk_context * ctx void * key mbedtls_pk_rsa_alt_decrypt_func decrypt_func mbedtls_pk_rsa_alt_sign_func sign_func mbedtls_pk_rsa_alt_key_len_func key_len_func

Initialize an RSA-alt context.

ctx

Context to initialize. It must not have been set up yet (type MBEDTLS_PK_NONE).

key

RSA key pointer

decrypt_func

Decryption function

sign_func

Signing function

key_len_func

Function returning key length in bytes

0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the context wasn't already initialized as RSA_ALT.

This function replaces `mbedtls_pk_setup()` for RSA-alt.

size_t size_t mbedtls_pk_get_bitlen (const mbedtls_pk_context *ctx) mbedtls_pk_get_bitlen const mbedtls_pk_context * ctx

Get the size in bits of the underlying key.

ctx

The context to query. It must have been initialized.

Key size in bits, or 0 on error

mbedtls_pk_get_len
size_t static size_t mbedtls_pk_get_len (const mbedtls_pk_context *ctx) mbedtls_pk_get_len const mbedtls_pk_context * ctx

Get the length in bytes of the underlying key.

ctx

The context to query. It must have been initialized.

Key length in bytes, or 0 on error

mbedtls_pk_get_bitlen
int int mbedtls_pk_can_do (const mbedtls_pk_context *ctx, mbedtls_pk_type_t type) mbedtls_pk_can_do const mbedtls_pk_context * ctx mbedtls_pk_type_t type

Tell if a context can do the operation given by type.

ctx

The context to query. It must have been initialized.

type

The desired type.

1 if the context can do operations on the given type.

0 if the context cannot do the operations on the given type. This is always the case for a context that has been initialized but not set up, or that has been cleared with mbedtls_pk_free().

int int mbedtls_pk_get_psa_attributes (const mbedtls_pk_context *pk, psa_key_usage_t usage, psa_key_attributes_t *attributes) mbedtls_pk_get_psa_attributes const mbedtls_pk_context * pk psa_key_usage_t usage psa_key_attributes_t * attributes

Determine valid PSA attributes that can be used to import a key into PSA.

The attributes determined by this function are suitable for calling mbedtls_pk_import_into_psa() to create a PSA key with the same key material.

The typical flow of operations involving this function is

    psa_key_attributes_tattributes=PSA_KEY_ATTRIBUTES_INIT;
    intret=mbedtls_pk_get_psa_attributes(pk,&attributes);
    if(ret!=0)...;//errorhandlingomitted
    //Tweakattributesifdesired
    psa_key_id_tkey_id=0;
    ret=mbedtls_pk_import_into_psa(pk,&attributes,&key_id);
    if(ret!=0)...;//errorhandlingomitted

This function does not support RSA-alt contexts (set up with mbedtls_pk_setup_rsa_alt()).

pk

The PK context to use. It must have been set up. It can either contain a key pair or just a public key.

usage

A single `PSA_KEY_USAGE_xxx` flag among the following:

- PSA_KEY_USAGE_DECRYPT: `pk` must contain a key pair. The output `attributes` will contain a key pair type, and the usage policy will allow PSA_KEY_USAGE_ENCRYPT as well as PSA_KEY_USAGE_DECRYPT.

- PSA_KEY_USAGE_DERIVE: `pk` must contain a key pair. The output `attributes` will contain a key pair type.

- PSA_KEY_USAGE_ENCRYPT: The output `attributes` will contain a public key type.

- PSA_KEY_USAGE_SIGN_HASH: `pk` must contain a key pair. The output `attributes` will contain a key pair type, and the usage policy will allow PSA_KEY_USAGE_VERIFY_HASH as well as PSA_KEY_USAGE_SIGN_HASH.

- PSA_KEY_USAGE_SIGN_MESSAGE: `pk` must contain a key pair. The output `attributes` will contain a key pair type, and the usage policy will allow PSA_KEY_USAGE_VERIFY_MESSAGE as well as PSA_KEY_USAGE_SIGN_MESSAGE.

- PSA_KEY_USAGE_VERIFY_HASH: The output `attributes` will contain a public key type.

- PSA_KEY_USAGE_VERIFY_MESSAGE: The output `attributes` will contain a public key type.

attributes

On success, valid attributes to import the key into PSA.

- The lifetime and key identifier are unchanged. If the attribute structure was initialized or reset before calling this function, this will result in a volatile key. Call psa_set_key_identifier() before or after this function if you wish to create a persistent key. Call psa_set_key_lifetime() before or after this function if you wish to import the key in a secure element.

- The key type and bit-size are determined by the contents of the PK context. If the PK context contains a key pair, the key type can be either a key pair type or the corresponding public key type, depending on `usage`. If the PK context contains a public key, the key type is a public key type.

- The key's policy is determined by the key type and the `usage` parameter. The usage always allows `usage`, exporting and copying the key, and possibly other permissions as documented for the `usage` parameter. The permitted algorithm policy is determined as follows based on the mbedtls_pk_type_t type of `pk`, the chosen `usage` and other factors:

  - MBEDTLS_PK_RSA whose underlying mbedtls_rsa_context has the padding mode MBEDTLS_RSA_PKCS_V15: PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH) if `usage` is SIGN/VERIFY, and PSA_ALG_RSA_PKCS1V15_CRYPT if `usage` is ENCRYPT/DECRYPT.

  - MBEDTLS_PK_RSA whose underlying mbedtls_rsa_context has the padding mode MBEDTLS_RSA_PKCS_V21 and the digest type corresponding to the PSA algorithm `hash:` PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH) if `usage` is SIGN/VERIFY, and PSA_ALG_RSA_OAEP(`hash`) if `usage` is ENCRYPT/DECRYPT.

  - MBEDTLS_PK_RSA_ALT: not supported.

  - MBEDTLS_PK_ECDSA or MBEDTLS_PK_ECKEY if `usage` is SIGN/VERIFY: PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH) if MBEDTLS_ECDSA_DETERMINISTIC is enabled, otherwise PSA_ALG_ECDSA(PSA_ALG_ANY_HASH).

  - MBEDTLS_PK_ECKEY_DH or MBEDTLS_PK_ECKEY if `usage` is DERIVE: PSA_ALG_ECDH.

  - MBEDTLS_PK_OPAQUE: same as the primary algorithm set for the underlying PSA key, except that sign/decrypt flags are removed if the type is set to a public key type. The underlying key must allow `usage`. Note that the enrollment algorithm set with psa_set_key_enrollment_algorithm() is not copied.

0 on success. MBEDTLS_ERR_PK_TYPE_MISMATCH if `pk` does not contain a key of the type identified in `attributes`. Another error code on other failures.

int int mbedtls_pk_import_into_psa (const mbedtls_pk_context *pk, const psa_key_attributes_t *attributes, mbedtls_svc_key_id_t *key_id) mbedtls_pk_import_into_psa const mbedtls_pk_context * pk const psa_key_attributes_t * attributes mbedtls_svc_key_id_t * key_id

Import a key into the PSA key store.

This function is equivalent to calling psa_import_key() with the key material from `pk`.

The typical way to use this function is:

1. Call mbedtls_pk_get_psa_attributes() to obtain attributes for the given key.

2. If desired, modify the attributes, for example:

   - To create a persistent key, call psa_set_key_identifier() and optionally psa_set_key_lifetime().

   - To import only the public part of a key pair: psa_set_key_type(&attributes, PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( psa_get_key_type(&attributes)));

   - Restrict the key usage if desired.

3. Call mbedtls_pk_import_into_psa().

This function does not support RSA-alt contexts (set up with mbedtls_pk_setup_rsa_alt()).

pk

The PK context to use. It must have been set up. It can either contain a key pair or just a public key.

attributes

The attributes to use for the new key. They must be compatible with `pk`. In particular, the key type must match the content of `pk`. If `pk` contains a key pair, the key type in attributes can be either the key pair type or the corresponding public key type (to import only the public part).

key_id

On success, the identifier of the newly created key. On error, this is MBEDTLS_SVC_KEY_ID_INIT.

0 on success. MBEDTLS_ERR_PK_TYPE_MISMATCH if `pk` does not contain a key of the type identified in `attributes`. Another error code on other failures.

int int mbedtls_pk_copy_from_psa (mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk) mbedtls_pk_copy_from_psa mbedtls_svc_key_id_t key_id mbedtls_pk_context * pk

Create a PK context starting from a key stored in PSA. This key:

- must be exportable and

- must be an RSA or EC key pair or public key (FFDH is not supported in PK).

The resulting PK object will be a transparent type:

- MBEDTLS_PK_RSA for RSA keys or

- MBEDTLS_PK_ECKEY for EC keys.

Once this functions returns the PK object will be completely independent from the original PSA key that it was generated from. Calling mbedtls_pk_sign(), mbedtls_pk_verify(), mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting PK context will perform the corresponding algorithm for that PK context type.

- For ECDSA, the choice of deterministic vs randomized will be based on the compile-time setting MBEDTLS_ECDSA_DETERMINISTIC.

- For an RSA key, the output PK context will allow both encrypt/decrypt and sign/verify regardless of the original key's policy. The original key's policy determines the output key's padding mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, otherwise PKCS1 v1.5 is set.

key_id

The key identifier of the key stored in PSA.

pk

The PK context that will be filled. It must be initialized, but not set up.

0 on success.

MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input parameters are not correct.

int int mbedtls_pk_copy_public_from_psa (mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk) mbedtls_pk_copy_public_from_psa mbedtls_svc_key_id_t key_id mbedtls_pk_context * pk

Create a PK context for the public key of a PSA key.

The key must be an RSA or ECC key. It can be either a public key or a key pair, and only the public key is copied. The resulting PK object will be a transparent type: - #MBEDTLS_PK_RSA for RSA keys or - #MBEDTLS_PK_ECKEY for EC keys. Once this functions returns the PK object will be completely independent from the original PSA key that it was generated from. Calling mbedtls_pk_verify() or mbedtls_pk_encrypt() on the resulting PK context will perform the corresponding algorithm for that PK context type. For an RSA key, the output PK context will allow both encrypt and verify regardless of the original key's policy. The original key's policy determines the output key's padding mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, otherwise PKCS1 v1.5 is set. key_id

The key identifier of the key stored in PSA.

pk

The PK context that will be filled. It must be initialized, but not set up.

0 on success.

MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input parameters are not correct.

int int mbedtls_pk_verify (mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len) mbedtls_pk_verify mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len const unsigned char * sig size_t sig_len

Verify signature (including padding if relevant).

ctx

The PK context to use. It must have been set up.

md_alg

Hash algorithm used. This can be MBEDTLS_MD_NONE if the signature algorithm does not rely on a hash algorithm (non-deterministic ECDSA, RSA PKCS#1 v1.5). For PKCS#1 v1.5, if `md_alg` is MBEDTLS_MD_NONE, then `hash` is the DigestInfo structure used by RFC 8017 9.2 steps 36. If `md_alg` is a valid hash algorithm then `hash` is the digest itself, and this function calculates the DigestInfo encoding internally.

hash

Hash of the message to sign

hash_len

Hash length

sig

Signature to verify

sig_len

Signature length

For keys of type MBEDTLS_PK_RSA, the signature algorithm is either PKCS#1 v1.5 or PSS (accepting any salt length), depending on the padding mode in the underlying RSA context. For a pk object constructed by parsing, this is PKCS#1 v1.5 by default. Use mbedtls_pk_verify_ext() to explicitly select a different algorithm.

0 on success (signature is valid), MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid signature in `sig` but its length is less than `sig_len`, or a specific error code.

int int mbedtls_pk_verify_restartable (mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len, mbedtls_pk_restart_ctx *rs_ctx) mbedtls_pk_verify_restartable mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len const unsigned char * sig size_t sig_len mbedtls_pk_restart_ctx * rs_ctx

Restartable version of `mbedtls_pk_verify()`

Performs the same job as `mbedtls_pk_verify()`, but can return early and restart according to the limit set with `mbedtls_ecp_set_max_ops()` to reduce blocking for ECC operations. For RSA, same as `mbedtls_pk_verify()`.

ctx

The PK context to use. It must have been set up.

md_alg

Hash algorithm used (see notes)

hash

Hash of the message to sign

hash_len

Hash length or 0 (see notes)

sig

Signature to verify

sig_len

Signature length

rs_ctx

Restart context (NULL to disable restart)

See `mbedtls_pk_verify()`, or

MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of operations was reached: see `mbedtls_ecp_set_max_ops()`.

int int mbedtls_pk_verify_ext (mbedtls_pk_type_t type, const void *options, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len) mbedtls_pk_verify_ext mbedtls_pk_type_t type const void * options mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len const unsigned char * sig size_t sig_len

Verify signature, with options. (Includes verification of the padding depending on type.)

type

Signature type (inc. possible padding type) to verify

options

Pointer to type-specific options, or NULL

ctx

The PK context to use. It must have been set up.

md_alg

Hash algorithm used (see notes)

hash

Hash of the message to sign

hash_len

Hash length or 0 (see notes)

sig

Signature to verify

sig_len

Signature length

0 on success (signature is valid), MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be used for this type of signatures, MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid signature in `sig` but its length is less than `sig_len`, or a specific error code.

If hash_len is 0, then the length associated with md_alg is used instead, or an error returned if it is invalid.

md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0

If type is MBEDTLS_PK_RSASSA_PSS, then options must point to a mbedtls_pk_rsassa_pss_options structure, otherwise it must be NULL. Note that if #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not verified as PSA_ALG_RSA_PSS_ANY_SALT is used.

int int mbedtls_pk_sign (mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t sig_size, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_sign mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len unsigned char * sig size_t sig_size size_t * sig_len int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Make signature, including padding if relevant.

ctx

The PK context to use. It must have been set up with a private key.

md_alg

Hash algorithm used (see notes)

hash

Hash of the message to sign

hash_len

Hash length

sig

Place to write the signature. It must have enough room for the signature. MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. You may use a smaller buffer if it is large enough given the key type.

sig_size

The size of the `sig` buffer in bytes.

sig_len

On successful return, the number of bytes written to `sig`.

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

For keys of type MBEDTLS_PK_RSA, the signature algorithm is either PKCS#1 v1.5 or PSS (using the largest possible salt length up to the hash length), depending on the padding mode in the underlying RSA context. For a pk object constructed by parsing, this is PKCS#1 v1.5 by default. Use mbedtls_pk_verify_ext() to explicitly select a different algorithm.

0 on success, or a specific error code.

For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. For ECDSA, md_alg may never be MBEDTLS_MD_NONE.

int int mbedtls_pk_sign_ext (mbedtls_pk_type_t pk_type, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t sig_size, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_sign_ext mbedtls_pk_type_t pk_type mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len unsigned char * sig size_t sig_size size_t * sig_len int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Make signature given a signature type.

pk_type

Signature type.

ctx

The PK context to use. It must have been set up with a private key.

md_alg

Hash algorithm used (see notes)

hash

Hash of the message to sign

hash_len

Hash length

sig

Place to write the signature. It must have enough room for the signature. MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. You may use a smaller buffer if it is large enough given the key type.

sig_size

The size of the `sig` buffer in bytes.

sig_len

On successful return, the number of bytes written to `sig`.

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

0 on success, or a specific error code.

When `pk_type` is MBEDTLS_PK_RSASSA_PSS, see PSA_ALG_RSA_PSS for a description of PSS options used.

For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. For ECDSA, md_alg may never be MBEDTLS_MD_NONE.

int int mbedtls_pk_sign_restartable (mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t sig_size, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_pk_restart_ctx *rs_ctx) mbedtls_pk_sign_restartable mbedtls_pk_context * ctx mbedtls_md_type_t md_alg const unsigned char * hash size_t hash_len unsigned char * sig size_t sig_size size_t * sig_len int(*)(void *, unsigned char *, size_t) f_rng void * p_rng mbedtls_pk_restart_ctx * rs_ctx

Restartable version of `mbedtls_pk_sign()`

Performs the same job as `mbedtls_pk_sign()`, but can return early and restart according to the limit set with `mbedtls_ecp_set_max_ops()` to reduce blocking for ECC operations. For RSA, same as `mbedtls_pk_sign()`.

ctx

The PK context to use. It must have been set up with a private key.

md_alg

Hash algorithm used (see notes for mbedtls_pk_sign())

hash

Hash of the message to sign

hash_len

Hash length

sig

Place to write the signature. It must have enough room for the signature. MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. You may use a smaller buffer if it is large enough given the key type.

sig_size

The size of the `sig` buffer in bytes.

sig_len

On successful return, the number of bytes written to `sig`.

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

rs_ctx

Restart context (NULL to disable restart)

See `mbedtls_pk_sign()`.

MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of operations was reached: see `mbedtls_ecp_set_max_ops()`.

int int mbedtls_pk_decrypt (mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_decrypt mbedtls_pk_context * ctx const unsigned char * input size_t ilen unsigned char * output size_t * olen size_t osize int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Decrypt message (including padding if relevant).

ctx

The PK context to use. It must have been set up with a private key.

input

Input to decrypt

ilen

Input size

output

Decrypted output

olen

Decrypted message length

osize

Size of the output buffer

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

For keys of type MBEDTLS_PK_RSA, the signature algorithm is either PKCS#1 v1.5 or OAEP, depending on the padding mode in the underlying RSA context. For a pk object constructed by parsing, this is PKCS#1 v1.5 by default.

0 on success, or a specific error code.

int int mbedtls_pk_encrypt (mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_encrypt mbedtls_pk_context * ctx const unsigned char * input size_t ilen unsigned char * output size_t * olen size_t osize int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Encrypt message (including padding if relevant).

ctx

The PK context to use. It must have been set up.

input

Message to encrypt

ilen

Message size

output

Encrypted output

olen

Encrypted output length

osize

Size of the output buffer

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

For keys of type MBEDTLS_PK_RSA, the signature algorithm is either PKCS#1 v1.5 or OAEP, depending on the padding mode in the underlying RSA context. For a pk object constructed by parsing, this is PKCS#1 v1.5 by default.

`f_rng` is used for padding generation.

0 on success, or a specific error code.

int int mbedtls_pk_check_pair (const mbedtls_pk_context *pub, const mbedtls_pk_context *prv, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_check_pair const mbedtls_pk_context * pub const mbedtls_pk_context * prv int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Check if a public-private pair of keys matches.

pub

Context holding a public key.

prv

Context holding a private (and public) key.

f_rng

RNG function, must not be `NULL`.

p_rng

RNG parameter

`0` on success (keys were checked and match each other).

MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not be checked - in that case they may or may not match.

MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.

Another non-zero value if the keys do not match.

int int mbedtls_pk_debug (const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items) mbedtls_pk_debug const mbedtls_pk_context * ctx mbedtls_pk_debug_item * items

Export debug information.

ctx

The PK context to use. It must have been initialized.

items

Place to write debug items

0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA

const char * const char * mbedtls_pk_get_name (const mbedtls_pk_context *ctx) mbedtls_pk_get_name const mbedtls_pk_context * ctx

Access the type name.

ctx

The PK context to use. It must have been initialized.

Type name on success, or "invalid PK"

mbedtls_pk_type_t mbedtls_pk_type_t mbedtls_pk_get_type (const mbedtls_pk_context *ctx) mbedtls_pk_get_type const mbedtls_pk_context * ctx

Get the key type.

ctx

The PK context to use. It must have been initialized.

Type on success.

MBEDTLS_PK_NONE for a context that has not been set up.

mbedtls_pk_ec
mbedtls_pk_rsa
mbedtls_rsa_context * static mbedtls_rsa_context * mbedtls_pk_rsa (const mbedtls_pk_context pk) mbedtls_pk_rsa const mbedtls_pk_context pk

Quick access to an RSA context inside a PK context.

This function can only be used when the type of the context, as returned by mbedtls_pk_get_type(), is MBEDTLS_PK_RSA. Ensuring that is the caller's responsibility. Alternatively, you can check whether this function returns NULL.

The internal RSA context held by the PK context, or NULL.

mbedtls_pk_get_type
MBEDTLS_PK_RSA
MBEDTLS_PRIVATE
mbedtls_ecp_keypair * static mbedtls_ecp_keypair * mbedtls_pk_ec (const mbedtls_pk_context pk) mbedtls_pk_ec const mbedtls_pk_context pk

Quick access to an EC context inside a PK context.

This function can only be used when the type of the context, as returned by mbedtls_pk_get_type(), is MBEDTLS_PK_ECKEY, MBEDTLS_PK_ECKEY_DH, or MBEDTLS_PK_ECDSA. Ensuring that is the caller's responsibility. Alternatively, you can check whether this function returns NULL.

The internal EC context held by the PK context, or NULL.

MBEDTLS_PK_ECDSA
MBEDTLS_PK_ECKEY
MBEDTLS_PK_ECKEY_DH
mbedtls_pk_get_type
MBEDTLS_PRIVATE
int int mbedtls_pk_parse_key (mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_parse_key mbedtls_pk_context * ctx const unsigned char * key size_t keylen const unsigned char * pwd size_t pwdlen int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Parse a private key in PEM or DER format.

If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto subsystem must have been initialized by calling psa_crypto_init() before calling this function.

ctx

The PK context to fill. It must have been initialized but not set up.

key

Input buffer to parse. The buffer must contain the input exactly, with no extra trailing material. For PEM, the buffer must contain a null-terminated string.

keylen

Size of key in bytes. For PEM data, this includes the terminating null byte, so `keylen` must be equal to `strlen(key) + 1`.

pwd

Optional password for decryption. Pass `NULL` if expecting a non-encrypted key. Pass a string of `pwdlen` bytes if expecting an encrypted key; a non-encrypted key will also be accepted. The empty password is not supported.

pwdlen

Size of the password in bytes. Ignored if `pwd` is `NULL`.

f_rng

RNG function, must not be `NULL`. Used for blinding.

p_rng

RNG parameter

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a specific key type, check the result with mbedtls_pk_can_do().

The key is also checked for correctness.

0 if successful, or a specific PK or PEM error code

int int mbedtls_pk_parse_public_key (mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen) mbedtls_pk_parse_public_key mbedtls_pk_context * ctx const unsigned char * key size_t keylen

Parse a public key in PEM or DER format.

If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto subsystem must have been initialized by calling psa_crypto_init() before calling this function.

ctx

The PK context to fill. It must have been initialized but not set up.

key

Input buffer to parse. The buffer must contain the input exactly, with no extra trailing material. For PEM, the buffer must contain a null-terminated string.

keylen

Size of key in bytes. For PEM data, this includes the terminating null byte, so `keylen` must be equal to `strlen(key) + 1`.

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a specific key type, check the result with mbedtls_pk_can_do().

For compressed points, see MBEDTLS_ECP_PF_COMPRESSED for limitations.

The key is also checked for correctness.

0 if successful, or a specific PK or PEM error code

int int mbedtls_pk_parse_keyfile (mbedtls_pk_context *ctx, const char *path, const char *password, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) mbedtls_pk_parse_keyfile mbedtls_pk_context * ctx const char * path const char * password int(*)(void *, unsigned char *, size_t) f_rng void * p_rng

Load and parse a private key.

If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto subsystem must have been initialized by calling psa_crypto_init() before calling this function.

ctx

The PK context to fill. It must have been initialized but not set up.

path

filename to read the private key from

password

Optional password to decrypt the file. Pass `NULL` if expecting a non-encrypted key. Pass a null-terminated string if expecting an encrypted key; a non-encrypted key will also be accepted. The empty password is not supported.

f_rng

RNG function, must not be `NULL`. Used for blinding.

p_rng

RNG parameter

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a specific key type, check the result with mbedtls_pk_can_do().

The key is also checked for correctness.

0 if successful, or a specific PK or PEM error code

int int mbedtls_pk_parse_public_keyfile (mbedtls_pk_context *ctx, const char *path) mbedtls_pk_parse_public_keyfile mbedtls_pk_context * ctx const char * path

Load and parse a public key.

ctx

The PK context to fill. It must have been initialized but not set up.

path

filename to read the public key from

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a specific key type, check the result with mbedtls_pk_can_do().

The key is also checked for correctness.

0 if successful, or a specific PK or PEM error code

int int mbedtls_pk_write_key_der (const mbedtls_pk_context *ctx, unsigned char *buf, size_t size) mbedtls_pk_write_key_der const mbedtls_pk_context * ctx unsigned char * buf size_t size

Write a private key to a PKCS#1 or SEC1 DER structure Note: data is written at the end of the buffer! Use the return value to determine where you should start using the buffer.

ctx

PK context which must contain a valid private key.

buf

buffer to write to

size

size of the buffer

length of data written if successful, or a specific error code

int int mbedtls_pk_write_pubkey_der (const mbedtls_pk_context *ctx, unsigned char *buf, size_t size) mbedtls_pk_write_pubkey_der const mbedtls_pk_context * ctx unsigned char * buf size_t size

Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the buffer! Use the return value to determine where you should start using the buffer.

ctx

PK context which must contain a valid public or private key.

buf

buffer to write to

size

size of the buffer

length of data written if successful, or a specific error code

int int mbedtls_pk_write_pubkey_pem (const mbedtls_pk_context *ctx, unsigned char *buf, size_t size) mbedtls_pk_write_pubkey_pem const mbedtls_pk_context * ctx unsigned char * buf size_t size

Write a public key to a PEM string.

ctx

PK context which must contain a valid public or private key.

buf

Buffer to write to. The output includes a terminating null byte.

size

Size of the buffer in bytes.

0 if successful, or a specific error code

int int mbedtls_pk_write_key_pem (const mbedtls_pk_context *ctx, unsigned char *buf, size_t size) mbedtls_pk_write_key_pem const mbedtls_pk_context * ctx unsigned char * buf size_t size

Write a private key to a PKCS#1 or SEC1 PEM string.

ctx

PK context which must contain a valid private key.

buf

Buffer to write to. The output includes a terminating null byte.

size

Size of the buffer in bytes.

0 if successful, or a specific error code

int int mbedtls_pk_parse_subpubkey (unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk) mbedtls_pk_parse_subpubkey unsigned char ** p const unsigned char * end mbedtls_pk_context * pk

Parse a SubjectPublicKeyInfo DER structure.

p

the position in the ASN.1 data

end

end of the buffer

pk

The PK context to fill. It must have been initialized but not set up.

0 if successful, or a specific PK error code

int int mbedtls_pk_write_pubkey (unsigned char **p, unsigned char *start, const mbedtls_pk_context *key) mbedtls_pk_write_pubkey unsigned char ** p unsigned char * start const mbedtls_pk_context * key

Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.

p

reference to current position pointer

start

start of the buffer (for bounds-checking)

key

PK context which must contain a valid public or private key.

the length written or a negative error code

Public Key abstraction layer.

    /*
    *CopyrightTheMbedTLSContributors
    *SPDX-License-Identifier:Apache-2.0ORGPL-2.0-or-later
    */

    #ifndefMBEDTLS_PK_H
    #defineMBEDTLS_PK_H
    #include"mbedtls/private_access.h"

    #include"mbedtls/build_info.h"

    #include"mbedtls/md.h"

    #ifdefined(MBEDTLS_RSA_C)
    #include"mbedtls/rsa.h"
    #endif

    #ifdefined(MBEDTLS_ECP_C)
    #include"mbedtls/ecp.h"
    #endif

    #ifdefined(MBEDTLS_ECDSA_C)
    #include"mbedtls/ecdsa.h"
    #endif

    #ifdefined(MBEDTLS_PSA_CRYPTO_CLIENT)
    #include"psa/crypto.h"
    #endif

    #defineMBEDTLS_ERR_PK_ALLOC_FAILED-0x3F80
    #defineMBEDTLS_ERR_PK_TYPE_MISMATCH-0x3F00
    #defineMBEDTLS_ERR_PK_BAD_INPUT_DATA-0x3E80
    #defineMBEDTLS_ERR_PK_FILE_IO_ERROR-0x3E00
    #defineMBEDTLS_ERR_PK_KEY_INVALID_VERSION-0x3D80
    #defineMBEDTLS_ERR_PK_KEY_INVALID_FORMAT-0x3D00
    #defineMBEDTLS_ERR_PK_UNKNOWN_PK_ALG-0x3C80
    #defineMBEDTLS_ERR_PK_PASSWORD_REQUIRED-0x3C00
    #defineMBEDTLS_ERR_PK_PASSWORD_MISMATCH-0x3B80
    #defineMBEDTLS_ERR_PK_INVALID_PUBKEY-0x3B00
    #defineMBEDTLS_ERR_PK_INVALID_ALG-0x3A80
    #defineMBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE-0x3A00
    #defineMBEDTLS_ERR_PK_FEATURE_UNAVAILABLE-0x3980
    #defineMBEDTLS_ERR_PK_SIG_LEN_MISMATCH-0x3900
    #defineMBEDTLS_ERR_PK_BUFFER_TOO_SMALL-0x3880

    #ifdef__cplusplus
    extern"C"{
    #endif

    typedefenum{
    MBEDTLS_PK_NONE=0,
    MBEDTLS_PK_RSA,
    MBEDTLS_PK_ECKEY,
    MBEDTLS_PK_ECKEY_DH,
    MBEDTLS_PK_ECDSA,
    MBEDTLS_PK_RSA_ALT,
    MBEDTLS_PK_RSASSA_PSS,
    MBEDTLS_PK_OPAQUE,
    }mbedtls_pk_type_t;

    typedefstructmbedtls_pk_rsassa_pss_options{
    mbedtls_md_type_tmgf1_hash_id;

    intexpected_salt_len;

    }mbedtls_pk_rsassa_pss_options;

    /*WeneedtosetMBEDTLS_PK_SIGNATURE_MAX_SIZEtothemaximumsignature
    *sizeamongthesupportedsignaturetypes.Doitbystartingat0,
    *thenincrementallyincreasingtobelargeenoughforeachsupported
    *signaturemechanism.
    *
    *Theresultingvaluecanbe0,forexampleifMBEDTLS_ECDH_Cisenabled
    *(whichallowsthepkmoduletobeincluded)butneitherMBEDTLS_ECDSA_C
    *norMBEDTLS_RSA_Cnoranyopaquesignaturemechanism(PSAorRSA_ALT).
    */
    #defineMBEDTLS_PK_SIGNATURE_MAX_SIZE0

    #if(defined(MBEDTLS_RSA_C)||defined(MBEDTLS_PK_RSA_ALT_SUPPORT))&&\
    MBEDTLS_MPI_MAX_SIZE>MBEDTLS_PK_SIGNATURE_MAX_SIZE
    /*ForRSA,thesignaturecanbeaslargeasthebignummoduleallows.
    *ForRSA_ALT,thesignaturesizeisnotnecessarilytiedtowhatthe
    *bignummodulecando,butintheabsenceofanyspecificsetting,
    *weusethat(rsa_alt_sign_wrapinlibrary/pk_wrap.hwillcheck).*/
    #undefMBEDTLS_PK_SIGNATURE_MAX_SIZE
    #defineMBEDTLS_PK_SIGNATURE_MAX_SIZEMBEDTLS_MPI_MAX_SIZE
    #endif

    #ifdefined(MBEDTLS_ECDSA_C)&&\
    MBEDTLS_ECDSA_MAX_LEN>MBEDTLS_PK_SIGNATURE_MAX_SIZE
    /*ForECDSA,theecdsamoduleexportsaconstantforthemaximum
    *signaturesize.*/
    #undefMBEDTLS_PK_SIGNATURE_MAX_SIZE
    #defineMBEDTLS_PK_SIGNATURE_MAX_SIZEMBEDTLS_ECDSA_MAX_LEN
    #endif

    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)
    #ifPSA_SIGNATURE_MAX_SIZE>MBEDTLS_PK_SIGNATURE_MAX_SIZE
    /*PSA_SIGNATURE_MAX_SIZEisthemaximumsizeofasignaturemade
    *throughthePSAAPIinthePSArepresentation.*/
    #undefMBEDTLS_PK_SIGNATURE_MAX_SIZE
    #defineMBEDTLS_PK_SIGNATURE_MAX_SIZEPSA_SIGNATURE_MAX_SIZE
    #endif

    #ifPSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE+11>MBEDTLS_PK_SIGNATURE_MAX_SIZE
    /*TheMbedTLSrepresentationisdifferentforECDSAsignatures:
    *PSAusestherawconcatenationofrands,
    *whereasMbedTLSusestheASN.1representation(SEQUENCEoftwoINTEGERs).
    *AddtheoverheadofASN.1:upto(1+2)+2*(1+2+1)forthe
    *types,lengths(representedbyupto2bytes),andpotentialleading
    *zerosoftheINTEGERsandtheSEQUENCE.*/
    #undefMBEDTLS_PK_SIGNATURE_MAX_SIZE
    #defineMBEDTLS_PK_SIGNATURE_MAX_SIZE(PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE+11)
    #endif
    #endif/*defined(MBEDTLS_USE_PSA_CRYPTO)*/

    /*Internalhelpertodefinewhichfieldsinthepk_contextstructurebelow
    *shouldbeusedforECkeys:legacyecp_keypairortheraw(PSAfriendly)
    *format.Itshouldbenotedthatthisonlyaffectshowdataisstored,not
    *whichfunctionsareusedforvariousoperations.Theoverallpicturelooks
    *likethis:
    *-ifUSE_PSAisnotdefinedandECP_Cisdefinedthenuseecp_keypairdata
    *structureandlegacyfunctions
    *-ifUSE_PSAisdefinedand
    *-ifECP_Cthenuseecp_keypairstructure,convertdatatoaPSAfriendly
    *formatandusePSAfunctions
    *-if!ECP_CthenusenewrawdataandPSAfunctionsdirectly.
    *
    *Themainreasonforthe"intermediate"(USE_PSA+ECP_C)aboveisthataslong
    *asECP_Cisdefinedmbedtls_pk_ec()givestheuseraread/writeaccesstothe
    *ecp_keypairstructureinsidethepk_contextsotheycanmodifyitusing
    *ECPfunctionswhicharenotunderPKmodule'scontrol.
    */
    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)&&defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)&&\
    !defined(MBEDTLS_ECP_C)
    #defineMBEDTLS_PK_USE_PSA_EC_DATA
    #endif

    typedefenum{
    MBEDTLS_PK_DEBUG_NONE=0,
    MBEDTLS_PK_DEBUG_MPI,
    MBEDTLS_PK_DEBUG_ECP,
    MBEDTLS_PK_DEBUG_PSA_EC,
    }mbedtls_pk_debug_type;

    typedefstructmbedtls_pk_debug_item{
    mbedtls_pk_debug_typeMBEDTLS_PRIVATE(type);
    constchar*MBEDTLS_PRIVATE(name);
    void*MBEDTLS_PRIVATE(value);
    }mbedtls_pk_debug_item;

    #defineMBEDTLS_PK_DEBUG_MAX_ITEMS3

    typedefstructmbedtls_pk_info_tmbedtls_pk_info_t;

    #defineMBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN\
    PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
    typedefstructmbedtls_pk_context{
    constmbedtls_pk_info_t*MBEDTLS_PRIVATE(pk_info);
    void*MBEDTLS_PRIVATE(pk_ctx);
    /*ThefollowingfieldisusedtostoretheIDofaprivatekeyinthe
    *followingcases:
    *-opaquekeywhenMBEDTLS_USE_PSA_CRYPTOisdefined
    *-normalkeywhenMBEDTLS_PK_USE_PSA_EC_DATAisdefined.Inthiscase:
    *-thepk_ctxaboveisnotnotusedtostoretheprivatekeyanymore.
    *Actuallythatfieldnotpopulatedatallinthiscasebecausealso
    *thepublickeywillbestoredinrawformatasexplainedbelow
    *-thisIDisusedforallprivatekeyoperations(ex:sign,check
    *keypair,keywrite,etc)usingPSAfunctions
    *
    *Note:thisprivatekeystoringsolutiononlyaffectsECkeys,notthe
    *otherones.Thelattersstillusethepk_ctxtostoretheirown
    *context.*/
    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)
    mbedtls_svc_key_id_tMBEDTLS_PRIVATE(priv_id);
    #endif/*MBEDTLS_USE_PSA_CRYPTO*/
    /*Thefollowingfieldsaremeantforstoringthepublickeyinrawformat
    *whichishandyfor:
    *-easilyimportingitintothePSAcontext
    *-reducingtheECPmoduledependenciesinthePKone.
    *
    *WhenMBEDTLS_PK_USE_PSA_EC_DATAisenabled:
    *-thepk_ctxaboveisnotusedanymoreforstoringthepublickey
    *insidetheecp_keypairstructure
    *-thefollowingfieldsareusedforallpublickeyoperations:signature
    *verify,keypaircheckandkeywrite.
    *-Forakeypair,priv_idcontainstheprivatekey.Forapublickey,
    *priv_idisnull.
    *Ofcourse,whenMBEDTLS_PK_USE_PSA_EC_DATAisnotenabled,thelegacy
    *ecp_keypairstructureisusedforstoringthepublickeyandperforming
    *alltheoperations.
    *
    *Note:ThisnewpublickeystoringsolutiononlyworksforECkeys,not
    *otherones.Thelattersstillusepk_ctxtostoretheirown
    *context.
    */
    #ifdefined(MBEDTLS_PK_USE_PSA_EC_DATA)
    uint8_tMBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
    size_tMBEDTLS_PRIVATE(pub_raw_len);
    psa_ecc_family_tMBEDTLS_PRIVATE(ec_family);
    size_tMBEDTLS_PRIVATE(ec_bits);
    #endif/*MBEDTLS_PK_USE_PSA_EC_DATA*/
    }mbedtls_pk_context;

    #ifdefined(MBEDTLS_ECDSA_C)&&defined(MBEDTLS_ECP_RESTARTABLE)
    typedefstruct{
    constmbedtls_pk_info_t*MBEDTLS_PRIVATE(pk_info);
    void*MBEDTLS_PRIVATE(rs_ctx);
    }mbedtls_pk_restart_ctx;
    #else/*MBEDTLS_ECDSA_C&&MBEDTLS_ECP_RESTARTABLE*/
    /*Nowwecandeclarefunctionsthattakeapointertothat*/
    typedefvoidmbedtls_pk_restart_ctx;
    #endif/*MBEDTLS_ECDSA_C&&MBEDTLS_ECP_RESTARTABLE*/

    #ifdefined(MBEDTLS_PK_RSA_ALT_SUPPORT)
    typedefint(*mbedtls_pk_rsa_alt_decrypt_func)(void*ctx,size_t*olen,
    constunsignedchar*input,unsignedchar*output,
    size_toutput_max_len);
    typedefint(*mbedtls_pk_rsa_alt_sign_func)(void*ctx,
    int(*f_rng)(void*,unsignedchar*,size_t),
    void*p_rng,
    mbedtls_md_type_tmd_alg,unsignedinthashlen,
    constunsignedchar*hash,unsignedchar*sig);
    typedefsize_t(*mbedtls_pk_rsa_alt_key_len_func)(void*ctx);
    #endif/*MBEDTLS_PK_RSA_ALT_SUPPORT*/

    constmbedtls_pk_info_t*mbedtls_pk_info_from_type(mbedtls_pk_type_tpk_type);

    voidmbedtls_pk_init(mbedtls_pk_context*ctx);

    voidmbedtls_pk_free(mbedtls_pk_context*ctx);

    #ifdefined(MBEDTLS_ECDSA_C)&&defined(MBEDTLS_ECP_RESTARTABLE)
    voidmbedtls_pk_restart_init(mbedtls_pk_restart_ctx*ctx);

    voidmbedtls_pk_restart_free(mbedtls_pk_restart_ctx*ctx);
    #endif/*MBEDTLS_ECDSA_C&&MBEDTLS_ECP_RESTARTABLE*/

    intmbedtls_pk_setup(mbedtls_pk_context*ctx,constmbedtls_pk_info_t*info);

    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)
    intmbedtls_pk_setup_opaque(mbedtls_pk_context*ctx,
    constmbedtls_svc_key_id_tkey);
    #endif/*MBEDTLS_USE_PSA_CRYPTO*/

    #ifdefined(MBEDTLS_PK_RSA_ALT_SUPPORT)
    intmbedtls_pk_setup_rsa_alt(mbedtls_pk_context*ctx,void*key,
    mbedtls_pk_rsa_alt_decrypt_funcdecrypt_func,
    mbedtls_pk_rsa_alt_sign_funcsign_func,
    mbedtls_pk_rsa_alt_key_len_funckey_len_func);
    #endif/*MBEDTLS_PK_RSA_ALT_SUPPORT*/

    size_tmbedtls_pk_get_bitlen(constmbedtls_pk_context*ctx);

    staticinlinesize_tmbedtls_pk_get_len(constmbedtls_pk_context*ctx)
    {
    return(mbedtls_pk_get_bitlen(ctx)+7)/8;
    }

    intmbedtls_pk_can_do(constmbedtls_pk_context*ctx,mbedtls_pk_type_ttype);

    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)
    intmbedtls_pk_can_do_ext(constmbedtls_pk_context*ctx,psa_algorithm_talg,
    psa_key_usage_tusage);
    #endif/*MBEDTLS_USE_PSA_CRYPTO*/

    #ifdefined(MBEDTLS_PSA_CRYPTO_CLIENT)
    intmbedtls_pk_get_psa_attributes(constmbedtls_pk_context*pk,
    psa_key_usage_tusage,
    psa_key_attributes_t*attributes);

    intmbedtls_pk_import_into_psa(constmbedtls_pk_context*pk,
    constpsa_key_attributes_t*attributes,
    mbedtls_svc_key_id_t*key_id);

    intmbedtls_pk_copy_from_psa(mbedtls_svc_key_id_tkey_id,mbedtls_pk_context*pk);

    intmbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_tkey_id,mbedtls_pk_context*pk);
    #endif/*MBEDTLS_PSA_CRYPTO_CLIENT*/

    intmbedtls_pk_verify(mbedtls_pk_context*ctx,mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    constunsignedchar*sig,size_tsig_len);

    intmbedtls_pk_verify_restartable(mbedtls_pk_context*ctx,
    mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    constunsignedchar*sig,size_tsig_len,
    mbedtls_pk_restart_ctx*rs_ctx);

    intmbedtls_pk_verify_ext(mbedtls_pk_type_ttype,constvoid*options,
    mbedtls_pk_context*ctx,mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    constunsignedchar*sig,size_tsig_len);

    intmbedtls_pk_sign(mbedtls_pk_context*ctx,mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    unsignedchar*sig,size_tsig_size,size_t*sig_len,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng);

    intmbedtls_pk_sign_ext(mbedtls_pk_type_tpk_type,
    mbedtls_pk_context*ctx,
    mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    unsignedchar*sig,size_tsig_size,size_t*sig_len,
    int(*f_rng)(void*,unsignedchar*,size_t),
    void*p_rng);

    intmbedtls_pk_sign_restartable(mbedtls_pk_context*ctx,
    mbedtls_md_type_tmd_alg,
    constunsignedchar*hash,size_thash_len,
    unsignedchar*sig,size_tsig_size,size_t*sig_len,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng,
    mbedtls_pk_restart_ctx*rs_ctx);

    intmbedtls_pk_decrypt(mbedtls_pk_context*ctx,
    constunsignedchar*input,size_tilen,
    unsignedchar*output,size_t*olen,size_tosize,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng);

    intmbedtls_pk_encrypt(mbedtls_pk_context*ctx,
    constunsignedchar*input,size_tilen,
    unsignedchar*output,size_t*olen,size_tosize,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng);

    intmbedtls_pk_check_pair(constmbedtls_pk_context*pub,
    constmbedtls_pk_context*prv,
    int(*f_rng)(void*,unsignedchar*,size_t),
    void*p_rng);

    intmbedtls_pk_debug(constmbedtls_pk_context*ctx,mbedtls_pk_debug_item*items);

    constchar*mbedtls_pk_get_name(constmbedtls_pk_context*ctx);

    mbedtls_pk_type_tmbedtls_pk_get_type(constmbedtls_pk_context*ctx);

    #ifdefined(MBEDTLS_RSA_C)
    staticinlinembedtls_rsa_context*mbedtls_pk_rsa(constmbedtls_pk_contextpk)
    {
    switch(mbedtls_pk_get_type(&pk)){
    caseMBEDTLS_PK_RSA:
    return(mbedtls_rsa_context*)(pk).MBEDTLS_PRIVATE(pk_ctx);
    default:
    returnNULL;
    }
    }
    #endif/*MBEDTLS_RSA_C*/

    #ifdefined(MBEDTLS_ECP_C)
    staticinlinembedtls_ecp_keypair*mbedtls_pk_ec(constmbedtls_pk_contextpk)
    {
    switch(mbedtls_pk_get_type(&pk)){
    caseMBEDTLS_PK_ECKEY:
    caseMBEDTLS_PK_ECKEY_DH:
    caseMBEDTLS_PK_ECDSA:
    return(mbedtls_ecp_keypair*)(pk).MBEDTLS_PRIVATE(pk_ctx);
    default:
    returnNULL;
    }
    }
    #endif/*MBEDTLS_ECP_C*/

    #ifdefined(MBEDTLS_PK_PARSE_C)
    intmbedtls_pk_parse_key(mbedtls_pk_context*ctx,
    constunsignedchar*key,size_tkeylen,
    constunsignedchar*pwd,size_tpwdlen,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng);

    intmbedtls_pk_parse_public_key(mbedtls_pk_context*ctx,
    constunsignedchar*key,size_tkeylen);

    #ifdefined(MBEDTLS_FS_IO)
    intmbedtls_pk_parse_keyfile(mbedtls_pk_context*ctx,
    constchar*path,constchar*password,
    int(*f_rng)(void*,unsignedchar*,size_t),void*p_rng);

    intmbedtls_pk_parse_public_keyfile(mbedtls_pk_context*ctx,constchar*path);
    #endif/*MBEDTLS_FS_IO*/
    #endif/*MBEDTLS_PK_PARSE_C*/

    #ifdefined(MBEDTLS_PK_WRITE_C)
    intmbedtls_pk_write_key_der(constmbedtls_pk_context*ctx,unsignedchar*buf,size_tsize);

    intmbedtls_pk_write_pubkey_der(constmbedtls_pk_context*ctx,unsignedchar*buf,size_tsize);

    #ifdefined(MBEDTLS_PEM_WRITE_C)
    intmbedtls_pk_write_pubkey_pem(constmbedtls_pk_context*ctx,unsignedchar*buf,size_tsize);

    intmbedtls_pk_write_key_pem(constmbedtls_pk_context*ctx,unsignedchar*buf,size_tsize);
    #endif/*MBEDTLS_PEM_WRITE_C*/
    #endif/*MBEDTLS_PK_WRITE_C*/

    /*
    *WARNING:Low-levelfunctions.Youprobablydonotwanttousetheseunless
    *youarecertainyoudo;)
    */

    #ifdefined(MBEDTLS_PK_PARSE_C)
    intmbedtls_pk_parse_subpubkey(unsignedchar**p,constunsignedchar*end,
    mbedtls_pk_context*pk);
    #endif/*MBEDTLS_PK_PARSE_C*/

    #ifdefined(MBEDTLS_PK_WRITE_C)
    intmbedtls_pk_write_pubkey(unsignedchar**p,unsignedchar*start,
    constmbedtls_pk_context*key);
    #endif/*MBEDTLS_PK_WRITE_C*/

    #ifdef__cplusplus
    }
    #endif

    #endif/*MBEDTLS_PK_H*/