1 /**
2  * \file pk.h
3  *
4  * \brief Public Key abstraction layer
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #ifndef MBEDTLS_PK_H
12 #define MBEDTLS_PK_H
13 #include "mbedtls/private_access.h"
14 
15 #include "mbedtls/build_info.h"
16 
17 #include "mbedtls/md.h"
18 
19 #if defined(MBEDTLS_RSA_C)
20 #include "mbedtls/rsa.h"
21 #endif
22 
23 #if defined(MBEDTLS_ECP_C)
24 #include "mbedtls/ecp.h"
25 #endif
26 
27 #if defined(MBEDTLS_ECDSA_C)
28 #include "mbedtls/ecdsa.h"
29 #endif
30 
31 #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_PSA_CRYPTO_C)
32 #include "psa/crypto.h"
33 #endif
34 
35 /** Memory allocation failed. */
36 #define MBEDTLS_ERR_PK_ALLOC_FAILED        -0x3F80
37 /** Type mismatch, eg attempt to encrypt with an ECDSA key */
38 #define MBEDTLS_ERR_PK_TYPE_MISMATCH       -0x3F00
39 /** Bad input parameters to function. */
40 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA      -0x3E80
41 /** Read/write of file failed. */
42 #define MBEDTLS_ERR_PK_FILE_IO_ERROR       -0x3E00
43 /** Unsupported key version */
44 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
45 /** Invalid key tag or value. */
46 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT  -0x3D00
47 /** Key algorithm is unsupported (only RSA and EC are supported). */
48 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG      -0x3C80
49 /** Private key password can't be empty. */
50 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED   -0x3C00
51 /** Given private key password does not allow for correct decryption. */
52 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH   -0x3B80
53 /** The pubkey tag or value is invalid (only RSA and EC are supported). */
54 #define MBEDTLS_ERR_PK_INVALID_PUBKEY      -0x3B00
55 /** The algorithm tag or value is invalid. */
56 #define MBEDTLS_ERR_PK_INVALID_ALG         -0x3A80
57 /** Elliptic curve is unsupported (only NIST curves are supported). */
58 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
59 /** Unavailable feature, e.g. RSA disabled for RSA key. */
60 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
61 /** The buffer contains a valid signature followed by more data. */
62 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH    -0x3900
63 /** The output buffer is too small. */
64 #define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL    -0x3880
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /**
71  * \brief          Public key types
72  */
73 typedef enum {
74     MBEDTLS_PK_NONE=0,
75     MBEDTLS_PK_RSA,
76     MBEDTLS_PK_ECKEY,
77     MBEDTLS_PK_ECKEY_DH,
78     MBEDTLS_PK_ECDSA,
79     MBEDTLS_PK_RSA_ALT,
80     MBEDTLS_PK_RSASSA_PSS,
81     MBEDTLS_PK_OPAQUE,
82 } mbedtls_pk_type_t;
83 
84 /**
85  * \brief           Options for RSASSA-PSS signature verification.
86  *                  See \c mbedtls_rsa_rsassa_pss_verify_ext()
87  */
88 typedef struct mbedtls_pk_rsassa_pss_options {
89     /** The digest to use for MGF1 in PSS.
90      *
91      * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled and #MBEDTLS_RSA_C is
92      *       disabled, this must be equal to the \c md_alg argument passed
93      *       to mbedtls_pk_verify_ext(). In a future version of the library,
94      *       this constraint may apply whenever #MBEDTLS_USE_PSA_CRYPTO is
95      *       enabled regardless of the status of #MBEDTLS_RSA_C.
96      */
97     mbedtls_md_type_t mgf1_hash_id;
98 
99     /** The expected length of the salt, in bytes. This may be
100      * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
101      *
102      * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled, only
103      *       #MBEDTLS_RSA_SALT_LEN_ANY is valid. Any other value may be
104      *       ignored (allowing any salt length).
105      */
106     int expected_salt_len;
107 
108 } mbedtls_pk_rsassa_pss_options;
109 
110 /**
111  * \brief           Maximum size of a signature made by mbedtls_pk_sign().
112  */
113 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
114  * size among the supported signature types. Do it by starting at 0,
115  * then incrementally increasing to be large enough for each supported
116  * signature mechanism.
117  *
118  * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
119  * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
120  * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
121  */
122 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
123 
124 #if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
125     MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
126 /* For RSA, the signature can be as large as the bignum module allows.
127  * For RSA_ALT, the signature size is not necessarily tied to what the
128  * bignum module can do, but in the absence of any specific setting,
129  * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
130 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
131 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
132 #endif
133 
134 #if defined(MBEDTLS_ECDSA_C) &&                                 \
135     MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
136 /* For ECDSA, the ecdsa module exports a constant for the maximum
137  * signature size. */
138 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
139 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
140 #endif
141 
142 #if defined(MBEDTLS_USE_PSA_CRYPTO)
143 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
144 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
145  * through the PSA API in the PSA representation. */
146 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
147 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
148 #endif
149 
150 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
151 /* The Mbed TLS representation is different for ECDSA signatures:
152  * PSA uses the raw concatenation of r and s,
153  * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
154  * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
155  * types, lengths (represented by up to 2 bytes), and potential leading
156  * zeros of the INTEGERs and the SEQUENCE. */
157 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
158 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
159 #endif
160 #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
161 
162 /* Internal helper to define which fields in the pk_context structure below
163  * should be used for EC keys: legacy ecp_keypair or the raw (PSA friendly)
164  * format. It should be noted that this only affects how data is stored, not
165  * which functions are used for various operations. The overall picture looks
166  * like this:
167  * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data
168  *   structure and legacy functions
169  * - if USE_PSA is defined and
170  *     - if ECP_C then use ecp_keypair structure, convert data to a PSA friendly
171  *       format and use PSA functions
172  *     - if !ECP_C then use new raw data and PSA functions directly.
173  *
174  * The main reason for the "intermediate" (USE_PSA + ECP_C) above is that as long
175  * as ECP_C is defined mbedtls_pk_ec() gives the user a read/write access to the
176  * ecp_keypair structure inside the pk_context so they can modify it using
177  * ECP functions which are not under PK module's control.
178  */
179 #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \
180     !defined(MBEDTLS_ECP_C)
181 #define MBEDTLS_PK_USE_PSA_EC_DATA
182 #endif
183 
184 /* Helper symbol to state that the PK module has support for EC keys. This
185  * can either be provided through the legacy ECP solution or through the
186  * PSA friendly MBEDTLS_PK_USE_PSA_EC_DATA. */
187 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_ECP_C)
188 #define MBEDTLS_PK_HAVE_ECC_KEYS
189 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_ECP_C */
190 
191 /**
192  * \brief           Types for interfacing with the debug module
193  */
194 typedef enum {
195     MBEDTLS_PK_DEBUG_NONE = 0,
196     MBEDTLS_PK_DEBUG_MPI,
197     MBEDTLS_PK_DEBUG_ECP,
198     MBEDTLS_PK_DEBUG_PSA_EC,
199 } mbedtls_pk_debug_type;
200 
201 /**
202  * \brief           Item to send to the debug module
203  */
204 typedef struct mbedtls_pk_debug_item {
205     mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
206     const char *MBEDTLS_PRIVATE(name);
207     void *MBEDTLS_PRIVATE(value);
208 } mbedtls_pk_debug_item;
209 
210 /** Maximum number of item send for debugging, plus 1 */
211 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
212 
213 /**
214  * \brief           Public key information and operations
215  *
216  * \note        The library does not support custom pk info structures,
217  *              only built-in structures returned by
218  *              mbedtls_cipher_info_from_type().
219  */
220 typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
221 
222 #define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
223     PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
224 /**
225  * \brief           Public key container
226  */
227 typedef struct mbedtls_pk_context {
228     const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info);    /**< Public key information         */
229     void *MBEDTLS_PRIVATE(pk_ctx);                        /**< Underlying public key context  */
230     /* The following field is used to store the ID of a private key in the
231      * following cases:
232      * - opaque key when MBEDTLS_PSA_CRYPTO_C is defined
233      * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case:
234      *    - the pk_ctx above is not not used to store the private key anymore.
235      *      Actually that field not populated at all in this case because also
236      *      the public key will be stored in raw format as explained below
237      *    - this ID is used for all private key operations (ex: sign, check
238      *      key pair, key write, etc) using PSA functions
239      *
240      * Note: this private key storing solution only affects EC keys, not the
241      *       other ones. The latters still use the pk_ctx to store their own
242      *       context.
243      *
244      * Note: this priv_id is guarded by MBEDTLS_PSA_CRYPTO_C and not by
245      *       MBEDTLS_PK_USE_PSA_EC_DATA (as the public counterpart below) because,
246      *       when working with opaque keys, it can be used also in
247      *       mbedtls_pk_sign_ext for RSA keys. */
248 #if defined(MBEDTLS_PSA_CRYPTO_C)
249     mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id);      /**< Key ID for opaque keys */
250 #endif /* MBEDTLS_PSA_CRYPTO_C */
251     /* The following fields are meant for storing the public key in raw format
252      * which is handy for:
253      * - easily importing it into the PSA context
254      * - reducing the ECP module dependencies in the PK one.
255      *
256      * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled:
257      * - the pk_ctx above is not used anymore for storing the public key
258      *   inside the ecp_keypair structure
259      * - the following fields are used for all public key operations: signature
260      *   verify, key pair check and key write.
261      * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy
262      * ecp_keypair structure is used for storing the public key and performing
263      * all the operations.
264      *
265      * Note: This new public key storing solution only works for EC keys, not
266      *       other ones. The latters still use pk_ctx to store their own
267      *       context.
268      */
269 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
270     uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key   */
271     size_t MBEDTLS_PRIVATE(pub_raw_len);            /**< Valid bytes in "pub_raw" */
272     psa_ecc_family_t MBEDTLS_PRIVATE(ec_family);    /**< EC family of pk */
273     size_t MBEDTLS_PRIVATE(ec_bits);                /**< Curve's bits of pk */
274 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
275 } mbedtls_pk_context;
276 
277 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
278 /**
279  * \brief           Context for resuming operations
280  */
281 typedef struct {
282     const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info);    /**< Public key information         */
283     void *MBEDTLS_PRIVATE(rs_ctx);                        /**< Underlying restart context     */
284 } mbedtls_pk_restart_ctx;
285 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
286 /* Now we can declare functions that take a pointer to that */
287 typedef void mbedtls_pk_restart_ctx;
288 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
289 
290 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
291 /**
292  * \brief           Types for RSA-alt abstraction
293  */
294 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen,
295                                                const unsigned char *input, unsigned char *output,
296                                                size_t output_max_len);
297 typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
298                                             int (*f_rng)(void *, unsigned char *, size_t),
299                                             void *p_rng,
300                                             mbedtls_md_type_t md_alg, unsigned int hashlen,
301                                             const unsigned char *hash, unsigned char *sig);
302 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
303 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
304 
305 /**
306  * \brief           Return information associated with the given PK type
307  *
308  * \param pk_type   PK type to search for.
309  *
310  * \return          The PK info associated with the type or NULL if not found.
311  */
312 const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
313 
314 /**
315  * \brief           Initialize a #mbedtls_pk_context (as NONE).
316  *
317  * \param ctx       The context to initialize.
318  *                  This must not be \c NULL.
319  */
320 void mbedtls_pk_init(mbedtls_pk_context *ctx);
321 
322 /**
323  * \brief           Free the components of a #mbedtls_pk_context.
324  *
325  * \param ctx       The context to clear. It must have been initialized.
326  *                  If this is \c NULL, this function does nothing.
327  *
328  * \note            For contexts that have been set up with
329  *                  mbedtls_pk_setup_opaque(), this does not free the underlying
330  *                  PSA key and you still need to call psa_destroy_key()
331  *                  independently if you want to destroy that key.
332  */
333 void mbedtls_pk_free(mbedtls_pk_context *ctx);
334 
335 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
336 /**
337  * \brief           Initialize a restart context
338  *
339  * \param ctx       The context to initialize.
340  *                  This must not be \c NULL.
341  */
342 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
343 
344 /**
345  * \brief           Free the components of a restart context
346  *
347  * \param ctx       The context to clear. It must have been initialized.
348  *                  If this is \c NULL, this function does nothing.
349  */
350 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
351 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
352 
353 /**
354  * \brief           Initialize a PK context with the information given
355  *                  and allocates the type-specific PK subcontext.
356  *
357  * \param ctx       Context to initialize. It must not have been set
358  *                  up yet (type #MBEDTLS_PK_NONE).
359  * \param info      Information to use
360  *
361  * \return          0 on success,
362  *                  MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
363  *                  MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
364  *
365  * \note            For contexts holding an RSA-alt key, use
366  *                  \c mbedtls_pk_setup_rsa_alt() instead.
367  */
368 int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
369 
370 #if defined(MBEDTLS_USE_PSA_CRYPTO)
371 /**
372  * \brief           Initialize a PK context to wrap a PSA key.
373  *
374  * \note            This function replaces mbedtls_pk_setup() for contexts
375  *                  that wrap a (possibly opaque) PSA key instead of
376  *                  storing and manipulating the key material directly.
377  *
378  * \param ctx       The context to initialize. It must be empty (type NONE).
379  * \param key       The PSA key to wrap, which must hold an ECC or RSA key
380  *                  pair (see notes below).
381  *
382  * \note            The wrapped key must remain valid as long as the
383  *                  wrapping PK context is in use, that is at least between
384  *                  the point this function is called and the point
385  *                  mbedtls_pk_free() is called on this context. The wrapped
386  *                  key might then be independently used or destroyed.
387  *
388  * \note            This function is currently only available for ECC or RSA
389  *                  key pairs (that is, keys containing private key material).
390  *                  Support for other key types may be added later.
391  *
392  * \return          \c 0 on success.
393  * \return          #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
394  *                  (context already used, invalid key identifier).
395  * \return          #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
396  *                  ECC key pair.
397  * \return          #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
398  */
399 int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
400                             const mbedtls_svc_key_id_t key);
401 #endif /* MBEDTLS_USE_PSA_CRYPTO */
402 
403 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
404 /**
405  * \brief           Initialize an RSA-alt context
406  *
407  * \param ctx       Context to initialize. It must not have been set
408  *                  up yet (type #MBEDTLS_PK_NONE).
409  * \param key       RSA key pointer
410  * \param decrypt_func  Decryption function
411  * \param sign_func     Signing function
412  * \param key_len_func  Function returning key length in bytes
413  *
414  * \return          0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
415  *                  context wasn't already initialized as RSA_ALT.
416  *
417  * \note            This function replaces \c mbedtls_pk_setup() for RSA-alt.
418  */
419 int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
420                              mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
421                              mbedtls_pk_rsa_alt_sign_func sign_func,
422                              mbedtls_pk_rsa_alt_key_len_func key_len_func);
423 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
424 
425 /**
426  * \brief           Get the size in bits of the underlying key
427  *
428  * \param ctx       The context to query. It must have been initialized.
429  *
430  * \return          Key size in bits, or 0 on error
431  */
432 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
433 
434 /**
435  * \brief           Get the length in bytes of the underlying key
436  *
437  * \param ctx       The context to query. It must have been initialized.
438  *
439  * \return          Key length in bytes, or 0 on error
440  */
mbedtls_pk_get_len(const mbedtls_pk_context * ctx)441 static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
442 {
443     return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
444 }
445 
446 /**
447  * \brief           Tell if a context can do the operation given by type
448  *
449  * \param ctx       The context to query. It must have been initialized.
450  * \param type      The desired type.
451  *
452  * \return          1 if the context can do operations on the given type.
453  * \return          0 if the context cannot do the operations on the given
454  *                  type. This is always the case for a context that has
455  *                  been initialized but not set up, or that has been
456  *                  cleared with mbedtls_pk_free().
457  */
458 int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
459 
460 #if defined(MBEDTLS_USE_PSA_CRYPTO)
461 /**
462  * \brief           Tell if context can do the operation given by PSA algorithm
463  *
464  * \param ctx       The context to query. It must have been initialized.
465  * \param alg       PSA algorithm to check against, the following are allowed:
466  *                  PSA_ALG_RSA_PKCS1V15_SIGN(hash),
467  *                  PSA_ALG_RSA_PSS(hash),
468  *                  PSA_ALG_RSA_PKCS1V15_CRYPT,
469  *                  PSA_ALG_ECDSA(hash),
470  *                  PSA_ALG_ECDH, where hash is a specific hash.
471  * \param usage  PSA usage flag to check against, must be composed of:
472  *                  PSA_KEY_USAGE_SIGN_HASH
473  *                  PSA_KEY_USAGE_DECRYPT
474  *                  PSA_KEY_USAGE_DERIVE.
475  *                  Context key must match all passed usage flags.
476  *
477  * \warning         Since the set of allowed algorithms and usage flags may be
478  *                  expanded in the future, the return value \c 0 should not
479  *                  be taken in account for non-allowed algorithms and usage
480  *                  flags.
481  *
482  * \return          1 if the context can do operations on the given type.
483  * \return          0 if the context cannot do the operations on the given
484  *                  type, for non-allowed algorithms and usage flags, or
485  *                  for a context that has been initialized but not set up
486  *                  or that has been cleared with mbedtls_pk_free().
487  */
488 int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
489                           psa_key_usage_t usage);
490 #endif /* MBEDTLS_USE_PSA_CRYPTO */
491 
492 /**
493  * \brief           Verify signature (including padding if relevant).
494  *
495  * \param ctx       The PK context to use. It must have been set up.
496  * \param md_alg    Hash algorithm used.
497  *                  This can be #MBEDTLS_MD_NONE if the signature algorithm
498  *                  does not rely on a hash algorithm (non-deterministic
499  *                  ECDSA, RSA PKCS#1 v1.5).
500  *                  For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
501  *                  \p hash is the DigestInfo structure used by RFC 8017
502  *                  &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
503  *                  algorithm then \p hash is the digest itself, and this
504  *                  function calculates the DigestInfo encoding internally.
505  * \param hash      Hash of the message to sign
506  * \param hash_len  Hash length
507  * \param sig       Signature to verify
508  * \param sig_len   Signature length
509  *
510  * \return          0 on success (signature is valid),
511  *                  #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
512  *                  signature in \p sig but its length is less than \p sig_len,
513  *                  or a specific error code.
514  *
515  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
516  *                  Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
517  *                  to verify RSASSA_PSS signatures.
518  */
519 int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
520                       const unsigned char *hash, size_t hash_len,
521                       const unsigned char *sig, size_t sig_len);
522 
523 /**
524  * \brief           Restartable version of \c mbedtls_pk_verify()
525  *
526  * \note            Performs the same job as \c mbedtls_pk_verify(), but can
527  *                  return early and restart according to the limit set with
528  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
529  *                  operations. For RSA, same as \c mbedtls_pk_verify().
530  *
531  * \param ctx       The PK context to use. It must have been set up.
532  * \param md_alg    Hash algorithm used (see notes)
533  * \param hash      Hash of the message to sign
534  * \param hash_len  Hash length or 0 (see notes)
535  * \param sig       Signature to verify
536  * \param sig_len   Signature length
537  * \param rs_ctx    Restart context (NULL to disable restart)
538  *
539  * \return          See \c mbedtls_pk_verify(), or
540  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
541  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
542  */
543 int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
544                                   mbedtls_md_type_t md_alg,
545                                   const unsigned char *hash, size_t hash_len,
546                                   const unsigned char *sig, size_t sig_len,
547                                   mbedtls_pk_restart_ctx *rs_ctx);
548 
549 /**
550  * \brief           Verify signature, with options.
551  *                  (Includes verification of the padding depending on type.)
552  *
553  * \param type      Signature type (inc. possible padding type) to verify
554  * \param options   Pointer to type-specific options, or NULL
555  * \param ctx       The PK context to use. It must have been set up.
556  * \param md_alg    Hash algorithm used (see notes)
557  * \param hash      Hash of the message to sign
558  * \param hash_len  Hash length or 0 (see notes)
559  * \param sig       Signature to verify
560  * \param sig_len   Signature length
561  *
562  * \return          0 on success (signature is valid),
563  *                  #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
564  *                  used for this type of signatures,
565  *                  #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
566  *                  signature in \p sig but its length is less than \p sig_len,
567  *                  or a specific error code.
568  *
569  * \note            If hash_len is 0, then the length associated with md_alg
570  *                  is used instead, or an error returned if it is invalid.
571  *
572  * \note            md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
573  *
574  * \note            If type is MBEDTLS_PK_RSASSA_PSS, then options must point
575  *                  to a mbedtls_pk_rsassa_pss_options structure,
576  *                  otherwise it must be NULL. Note that if
577  *                  #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not
578  *                  verified as PSA_ALG_RSA_PSS_ANY_SALT is used.
579  */
580 int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
581                           mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
582                           const unsigned char *hash, size_t hash_len,
583                           const unsigned char *sig, size_t sig_len);
584 
585 /**
586  * \brief           Make signature, including padding if relevant.
587  *
588  * \param ctx       The PK context to use. It must have been set up
589  *                  with a private key.
590  * \param md_alg    Hash algorithm used (see notes)
591  * \param hash      Hash of the message to sign
592  * \param hash_len  Hash length
593  * \param sig       Place to write the signature.
594  *                  It must have enough room for the signature.
595  *                  #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
596  *                  You may use a smaller buffer if it is large enough
597  *                  given the key type.
598  * \param sig_size  The size of the \p sig buffer in bytes.
599  * \param sig_len   On successful return,
600  *                  the number of bytes written to \p sig.
601  * \param f_rng     RNG function, must not be \c NULL.
602  * \param p_rng     RNG parameter
603  *
604  * \return          0 on success, or a specific error code.
605  *
606  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
607  *                  There is no interface in the PK module to make RSASSA-PSS
608  *                  signatures yet.
609  *
610  * \note            For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
611  *                  For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
612  */
613 int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
614                     const unsigned char *hash, size_t hash_len,
615                     unsigned char *sig, size_t sig_size, size_t *sig_len,
616                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
617 
618 #if defined(MBEDTLS_PSA_CRYPTO_C)
619 /**
620  * \brief           Make signature given a signature type.
621  *
622  * \param pk_type   Signature type.
623  * \param ctx       The PK context to use. It must have been set up
624  *                  with a private key.
625  * \param md_alg    Hash algorithm used (see notes)
626  * \param hash      Hash of the message to sign
627  * \param hash_len  Hash length
628  * \param sig       Place to write the signature.
629  *                  It must have enough room for the signature.
630  *                  #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
631  *                  You may use a smaller buffer if it is large enough
632  *                  given the key type.
633  * \param sig_size  The size of the \p sig buffer in bytes.
634  * \param sig_len   On successful return,
635  *                  the number of bytes written to \p sig.
636  * \param f_rng     RNG function, must not be \c NULL.
637  * \param p_rng     RNG parameter
638  *
639  * \return          0 on success, or a specific error code.
640  *
641  * \note            When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
642  *                  see #PSA_ALG_RSA_PSS for a description of PSS options used.
643  *
644  * \note            For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
645  *                  For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
646  *
647  */
648 int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
649                         mbedtls_pk_context *ctx,
650                         mbedtls_md_type_t md_alg,
651                         const unsigned char *hash, size_t hash_len,
652                         unsigned char *sig, size_t sig_size, size_t *sig_len,
653                         int (*f_rng)(void *, unsigned char *, size_t),
654                         void *p_rng);
655 #endif /* MBEDTLS_PSA_CRYPTO_C */
656 
657 /**
658  * \brief           Restartable version of \c mbedtls_pk_sign()
659  *
660  * \note            Performs the same job as \c mbedtls_pk_sign(), but can
661  *                  return early and restart according to the limit set with
662  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
663  *                  operations. For RSA, same as \c mbedtls_pk_sign().
664  *
665  * \param ctx       The PK context to use. It must have been set up
666  *                  with a private key.
667  * \param md_alg    Hash algorithm used (see notes for mbedtls_pk_sign())
668  * \param hash      Hash of the message to sign
669  * \param hash_len  Hash length
670  * \param sig       Place to write the signature.
671  *                  It must have enough room for the signature.
672  *                  #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
673  *                  You may use a smaller buffer if it is large enough
674  *                  given the key type.
675  * \param sig_size  The size of the \p sig buffer in bytes.
676  * \param sig_len   On successful return,
677  *                  the number of bytes written to \p sig.
678  * \param f_rng     RNG function, must not be \c NULL.
679  * \param p_rng     RNG parameter
680  * \param rs_ctx    Restart context (NULL to disable restart)
681  *
682  * \return          See \c mbedtls_pk_sign().
683  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
684  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
685  */
686 int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
687                                 mbedtls_md_type_t md_alg,
688                                 const unsigned char *hash, size_t hash_len,
689                                 unsigned char *sig, size_t sig_size, size_t *sig_len,
690                                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
691                                 mbedtls_pk_restart_ctx *rs_ctx);
692 
693 /**
694  * \brief           Decrypt message (including padding if relevant).
695  *
696  * \param ctx       The PK context to use. It must have been set up
697  *                  with a private key.
698  * \param input     Input to decrypt
699  * \param ilen      Input size
700  * \param output    Decrypted output
701  * \param olen      Decrypted message length
702  * \param osize     Size of the output buffer
703  * \param f_rng     RNG function, must not be \c NULL.
704  * \param p_rng     RNG parameter
705  *
706  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
707  *
708  * \return          0 on success, or a specific error code.
709  */
710 int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
711                        const unsigned char *input, size_t ilen,
712                        unsigned char *output, size_t *olen, size_t osize,
713                        int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
714 
715 /**
716  * \brief           Encrypt message (including padding if relevant).
717  *
718  * \param ctx       The PK context to use. It must have been set up.
719  * \param input     Message to encrypt
720  * \param ilen      Message size
721  * \param output    Encrypted output
722  * \param olen      Encrypted output length
723  * \param osize     Size of the output buffer
724  * \param f_rng     RNG function, must not be \c NULL.
725  * \param p_rng     RNG parameter
726  *
727  * \note            \p f_rng is used for padding generation.
728  *
729  * \note            For RSA keys, the default padding type is PKCS#1 v1.5.
730  *
731  * \return          0 on success, or a specific error code.
732  */
733 int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
734                        const unsigned char *input, size_t ilen,
735                        unsigned char *output, size_t *olen, size_t osize,
736                        int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
737 
738 /**
739  * \brief           Check if a public-private pair of keys matches.
740  *
741  * \param pub       Context holding a public key.
742  * \param prv       Context holding a private (and public) key.
743  * \param f_rng     RNG function, must not be \c NULL.
744  * \param p_rng     RNG parameter
745  *
746  * \return          \c 0 on success (keys were checked and match each other).
747  * \return          #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
748  *                  be checked - in that case they may or may not match.
749  * \return          #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
750  * \return          Another non-zero value if the keys do not match.
751  */
752 int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
753                           const mbedtls_pk_context *prv,
754                           int (*f_rng)(void *, unsigned char *, size_t),
755                           void *p_rng);
756 
757 /**
758  * \brief           Export debug information
759  *
760  * \param ctx       The PK context to use. It must have been initialized.
761  * \param items     Place to write debug items
762  *
763  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
764  */
765 int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
766 
767 /**
768  * \brief           Access the type name
769  *
770  * \param ctx       The PK context to use. It must have been initialized.
771  *
772  * \return          Type name on success, or "invalid PK"
773  */
774 const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
775 
776 /**
777  * \brief           Get the key type
778  *
779  * \param ctx       The PK context to use. It must have been initialized.
780  *
781  * \return          Type on success.
782  * \return          #MBEDTLS_PK_NONE for a context that has not been set up.
783  */
784 mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
785 
786 #if defined(MBEDTLS_RSA_C)
787 /**
788  * Quick access to an RSA context inside a PK context.
789  *
790  * \warning This function can only be used when the type of the context, as
791  * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
792  * Ensuring that is the caller's responsibility.
793  * Alternatively, you can check whether this function returns NULL.
794  *
795  * \return The internal RSA context held by the PK context, or NULL.
796  */
mbedtls_pk_rsa(const mbedtls_pk_context pk)797 static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
798 {
799     switch (mbedtls_pk_get_type(&pk)) {
800         case MBEDTLS_PK_RSA:
801             return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx);
802         default:
803             return NULL;
804     }
805 }
806 #endif /* MBEDTLS_RSA_C */
807 
808 #if defined(MBEDTLS_ECP_C)
809 /**
810  * Quick access to an EC context inside a PK context.
811  *
812  * \warning This function can only be used when the type of the context, as
813  * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
814  * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
815  * Ensuring that is the caller's responsibility.
816  * Alternatively, you can check whether this function returns NULL.
817  *
818  * \return The internal EC context held by the PK context, or NULL.
819  */
mbedtls_pk_ec(const mbedtls_pk_context pk)820 static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
821 {
822     switch (mbedtls_pk_get_type(&pk)) {
823         case MBEDTLS_PK_ECKEY:
824         case MBEDTLS_PK_ECKEY_DH:
825         case MBEDTLS_PK_ECDSA:
826             return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
827         default:
828             return NULL;
829     }
830 }
831 #endif /* MBEDTLS_ECP_C */
832 
833 #if defined(MBEDTLS_PK_PARSE_C)
834 /** \ingroup pk_module */
835 /**
836  * \brief           Parse a private key in PEM or DER format
837  *
838  * \note            If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
839  *                  subsystem must have been initialized by calling
840  *                  psa_crypto_init() before calling this function.
841  *
842  * \param ctx       The PK context to fill. It must have been initialized
843  *                  but not set up.
844  * \param key       Input buffer to parse.
845  *                  The buffer must contain the input exactly, with no
846  *                  extra trailing material. For PEM, the buffer must
847  *                  contain a null-terminated string.
848  * \param keylen    Size of \b key in bytes.
849  *                  For PEM data, this includes the terminating null byte,
850  *                  so \p keylen must be equal to `strlen(key) + 1`.
851  * \param pwd       Optional password for decryption.
852  *                  Pass \c NULL if expecting a non-encrypted key.
853  *                  Pass a string of \p pwdlen bytes if expecting an encrypted
854  *                  key; a non-encrypted key will also be accepted.
855  *                  The empty password is not supported.
856  * \param pwdlen    Size of the password in bytes.
857  *                  Ignored if \p pwd is \c NULL.
858  * \param f_rng     RNG function, must not be \c NULL. Used for blinding.
859  * \param p_rng     RNG parameter
860  *
861  * \note            On entry, ctx must be empty, either freshly initialised
862  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
863  *                  specific key type, check the result with mbedtls_pk_can_do().
864  *
865  * \note            The key is also checked for correctness.
866  *
867  * \return          0 if successful, or a specific PK or PEM error code
868  */
869 int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
870                          const unsigned char *key, size_t keylen,
871                          const unsigned char *pwd, size_t pwdlen,
872                          int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
873 
874 /** \ingroup pk_module */
875 /**
876  * \brief           Parse a public key in PEM or DER format
877  *
878  * \note            If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
879  *                  subsystem must have been initialized by calling
880  *                  psa_crypto_init() before calling this function.
881  *
882  * \param ctx       The PK context to fill. It must have been initialized
883  *                  but not set up.
884  * \param key       Input buffer to parse.
885  *                  The buffer must contain the input exactly, with no
886  *                  extra trailing material. For PEM, the buffer must
887  *                  contain a null-terminated string.
888  * \param keylen    Size of \b key in bytes.
889  *                  For PEM data, this includes the terminating null byte,
890  *                  so \p keylen must be equal to `strlen(key) + 1`.
891  *
892  * \note            On entry, ctx must be empty, either freshly initialised
893  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
894  *                  specific key type, check the result with mbedtls_pk_can_do().
895  *
896  * \note            For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
897  *                  limitations.
898  *
899  * \note            The key is also checked for correctness.
900  *
901  * \return          0 if successful, or a specific PK or PEM error code
902  */
903 int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
904                                 const unsigned char *key, size_t keylen);
905 
906 #if defined(MBEDTLS_FS_IO)
907 /** \ingroup pk_module */
908 /**
909  * \brief           Load and parse a private key
910  *
911  * \note            If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
912  *                  subsystem must have been initialized by calling
913  *                  psa_crypto_init() before calling this function.
914  *
915  * \param ctx       The PK context to fill. It must have been initialized
916  *                  but not set up.
917  * \param path      filename to read the private key from
918  * \param password  Optional password to decrypt the file.
919  *                  Pass \c NULL if expecting a non-encrypted key.
920  *                  Pass a null-terminated string if expecting an encrypted
921  *                  key; a non-encrypted key will also be accepted.
922  *                  The empty password is not supported.
923  * \param f_rng     RNG function, must not be \c NULL. Used for blinding.
924  * \param p_rng     RNG parameter
925  *
926  * \note            On entry, ctx must be empty, either freshly initialised
927  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
928  *                  specific key type, check the result with mbedtls_pk_can_do().
929  *
930  * \note            The key is also checked for correctness.
931  *
932  * \return          0 if successful, or a specific PK or PEM error code
933  */
934 int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
935                              const char *path, const char *password,
936                              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
937 
938 /** \ingroup pk_module */
939 /**
940  * \brief           Load and parse a public key
941  *
942  * \param ctx       The PK context to fill. It must have been initialized
943  *                  but not set up.
944  * \param path      filename to read the public key from
945  *
946  * \note            On entry, ctx must be empty, either freshly initialised
947  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
948  *                  you need a specific key type, check the result with
949  *                  mbedtls_pk_can_do().
950  *
951  * \note            The key is also checked for correctness.
952  *
953  * \return          0 if successful, or a specific PK or PEM error code
954  */
955 int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
956 #endif /* MBEDTLS_FS_IO */
957 #endif /* MBEDTLS_PK_PARSE_C */
958 
959 #if defined(MBEDTLS_PK_WRITE_C)
960 /**
961  * \brief           Write a private key to a PKCS#1 or SEC1 DER structure
962  *                  Note: data is written at the end of the buffer! Use the
963  *                        return value to determine where you should start
964  *                        using the buffer
965  *
966  * \param ctx       PK context which must contain a valid private key.
967  * \param buf       buffer to write to
968  * \param size      size of the buffer
969  *
970  * \return          length of data written if successful, or a specific
971  *                  error code
972  */
973 int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
974 
975 /**
976  * \brief           Write a public key to a SubjectPublicKeyInfo DER structure
977  *                  Note: data is written at the end of the buffer! Use the
978  *                        return value to determine where you should start
979  *                        using the buffer
980  *
981  * \param ctx       PK context which must contain a valid public or private key.
982  * \param buf       buffer to write to
983  * \param size      size of the buffer
984  *
985  * \return          length of data written if successful, or a specific
986  *                  error code
987  */
988 int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
989 
990 #if defined(MBEDTLS_PEM_WRITE_C)
991 /**
992  * \brief           Write a public key to a PEM string
993  *
994  * \param ctx       PK context which must contain a valid public or private key.
995  * \param buf       Buffer to write to. The output includes a
996  *                  terminating null byte.
997  * \param size      Size of the buffer in bytes.
998  *
999  * \return          0 if successful, or a specific error code
1000  */
1001 int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
1002 
1003 /**
1004  * \brief           Write a private key to a PKCS#1 or SEC1 PEM string
1005  *
1006  * \param ctx       PK context which must contain a valid private key.
1007  * \param buf       Buffer to write to. The output includes a
1008  *                  terminating null byte.
1009  * \param size      Size of the buffer in bytes.
1010  *
1011  * \return          0 if successful, or a specific error code
1012  */
1013 int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
1014 #endif /* MBEDTLS_PEM_WRITE_C */
1015 #endif /* MBEDTLS_PK_WRITE_C */
1016 
1017 /*
1018  * WARNING: Low-level functions. You probably do not want to use these unless
1019  *          you are certain you do ;)
1020  */
1021 
1022 #if defined(MBEDTLS_PK_PARSE_C)
1023 /**
1024  * \brief           Parse a SubjectPublicKeyInfo DER structure
1025  *
1026  * \param p         the position in the ASN.1 data
1027  * \param end       end of the buffer
1028  * \param pk        The PK context to fill. It must have been initialized
1029  *                  but not set up.
1030  *
1031  * \return          0 if successful, or a specific PK error code
1032  */
1033 int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
1034                                mbedtls_pk_context *pk);
1035 #endif /* MBEDTLS_PK_PARSE_C */
1036 
1037 #if defined(MBEDTLS_PK_WRITE_C)
1038 /**
1039  * \brief           Write a subjectPublicKey to ASN.1 data
1040  *                  Note: function works backwards in data buffer
1041  *
1042  * \param p         reference to current position pointer
1043  * \param start     start of the buffer (for bounds-checking)
1044  * \param key       PK context which must contain a valid public or private key.
1045  *
1046  * \return          the length written or a negative error code
1047  */
1048 int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
1049                             const mbedtls_pk_context *key);
1050 #endif /* MBEDTLS_PK_WRITE_C */
1051 
1052 /*
1053  * Internal module functions. You probably do not want to use these unless you
1054  * know you do.
1055  */
1056 #if defined(MBEDTLS_FS_IO)
1057 int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
1058 #endif
1059 
1060 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1061 /**
1062  * \brief           Turn an EC or RSA key into an opaque one.
1063  *
1064  * \warning         This is a temporary utility function for tests. It might
1065  *                  change or be removed at any time without notice.
1066  *
1067  * \param pk        Input: the EC or RSA key to import to a PSA key.
1068  *                  Output: a PK context wrapping that PSA key.
1069  * \param key       Output: a PSA key identifier.
1070  *                  It's the caller's responsibility to call
1071  *                  psa_destroy_key() on that key identifier after calling
1072  *                  mbedtls_pk_free() on the PK context.
1073  * \param alg       The algorithm to allow for use with that key.
1074  * \param usage     The usage to allow for use with that key.
1075  * \param alg2      The secondary algorithm to allow for use with that key.
1076  *
1077  * \return          \c 0 if successful.
1078  * \return          An Mbed TLS error code otherwise.
1079  */
1080 int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
1081                               mbedtls_svc_key_id_t *key,
1082                               psa_algorithm_t alg,
1083                               psa_key_usage_t usage,
1084                               psa_algorithm_t alg2);
1085 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1086 
1087 #ifdef __cplusplus
1088 }
1089 #endif
1090 
1091 #endif /* MBEDTLS_PK_H */
1092