blob: 60942d7efa5d8cdc6137352c67a2deb9cace2506 [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkered27a042013-04-18 22:46:23 +02009 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#ifndef MBEDTLS_PK_H
12#define MBEDTLS_PK_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020013#include "mbedtls/private_access.h"
Paul Bakkered27a042013-04-18 22:46:23 +020014
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020016
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010017#include "mbedtls/md.h"
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_RSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010020#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020021#endif
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_ECP_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010024#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025#endif
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_ECDSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010028#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020029#endif
30
Gilles Peskine0b172552024-01-18 14:11:26 +010031#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +020032#include "psa/crypto.h"
33#endif
34
Gilles Peskined2971572021-07-26 18:48:10 +020035/** 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
Ron Eldor9924bdc2018-10-04 10:59:13 +030065
Paul Bakkered27a042013-04-18 22:46:23 +020066#ifdef __cplusplus
67extern "C" {
68#endif
69
70/**
71 * \brief Public key types
72 */
73typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 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,
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +010081 MBEDTLS_PK_OPAQUE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020083
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020084/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020085 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020087 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088typedef struct mbedtls_pk_rsassa_pss_options {
Gilles Peskine34c43a82023-02-02 23:06:37 +010089 /** 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;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200109
110/**
Gilles Peskineda252be2019-11-05 16:23:49 +0100111 * \brief Maximum size of a signature made by mbedtls_pk_sign().
112 */
Gilles Peskine54605652019-11-08 16:24:16 +0100113/* 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100124#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100125 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100126/* 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,
Chris Jones3848e312021-03-11 16:17:59 +0000129 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100130#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
131#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
132#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100133
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100134#if defined(MBEDTLS_ECDSA_C) && \
135 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100136/* For ECDSA, the ecdsa module exports a constant for the maximum
137 * signature size. */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100138#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
139#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
140#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100141
142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100143#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
144/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
Gilles Peskine54605652019-11-08 16:24:16 +0100145 * through the PSA API in the PSA representation. */
146#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100147#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100148#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:
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100152 * 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100158#define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100159#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100160#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100161
Valerio Settia9aab1a2023-06-19 13:39:54 +0200162/* 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)
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200164 * format. It should be noted that this only affects how data is stored, not
Valerio Settia9aab1a2023-06-19 13:39:54 +0200165 * which functions are used for various operations. The overall picture looks
166 * like this:
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200167 * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data
168 * structure and legacy functions
Valerio Settia9aab1a2023-06-19 13:39:54 +0200169 * - 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
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200176 * ecp_keypair structure inside the pk_context so they can modify it using
Valerio Settia9aab1a2023-06-19 13:39:54 +0200177 * 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
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200182#endif
Valerio Settia9aab1a2023-06-19 13:39:54 +0200183
Valerio Setticf084ae2023-01-26 14:30:12 +0100184/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200185 * \brief Types for interfacing with the debug module
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 MBEDTLS_PK_DEBUG_NONE = 0,
189 MBEDTLS_PK_DEBUG_MPI,
190 MBEDTLS_PK_DEBUG_ECP,
Valerio Setti92c3f362023-05-17 15:36:44 +0200191 MBEDTLS_PK_DEBUG_PSA_EC,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200193
194/**
195 * \brief Item to send to the debug module
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197typedef struct mbedtls_pk_debug_item {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200198 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
199 const char *MBEDTLS_PRIVATE(name);
200 void *MBEDTLS_PRIVATE(value);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200202
203/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200205
206/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200207 * \brief Public key information and operations
Gilles Peskine2e9d65f2021-08-31 23:05:19 +0200208 *
209 * \note The library does not support custom pk info structures,
210 * only built-in structures returned by
211 * mbedtls_cipher_info_from_type().
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200214
Valerio Setti722f8f72023-05-17 15:31:21 +0200215#define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
216 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200217/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200218 * \brief Public key container
219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220typedef struct mbedtls_pk_context {
221 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
222 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200223 /* The following field is used to store the ID of a private key in the
224 * following cases:
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200225 * - opaque key when MBEDTLS_USE_PSA_CRYPTO is defined
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200226 * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case:
227 * - the pk_ctx above is not not used to store the private key anymore.
228 * Actually that field not populated at all in this case because also
229 * the public key will be stored in raw format as explained below
230 * - this ID is used for all private key operations (ex: sign, check
231 * key pair, key write, etc) using PSA functions
232 *
233 * Note: this private key storing solution only affects EC keys, not the
234 * other ones. The latters still use the pk_ctx to store their own
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200235 * context. */
236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +0200237 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +0200239 /* The following fields are meant for storing the public key in raw format
240 * which is handy for:
241 * - easily importing it into the PSA context
242 * - reducing the ECP module dependencies in the PK one.
243 *
244 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled:
245 * - the pk_ctx above is not used anymore for storing the public key
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200246 * inside the ecp_keypair structure
Valerio Setti722f8f72023-05-17 15:31:21 +0200247 * - the following fields are used for all public key operations: signature
248 * verify, key pair check and key write.
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100249 * - For a key pair, priv_id contains the private key. For a public key,
250 * priv_id is null.
Valerio Setti722f8f72023-05-17 15:31:21 +0200251 * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy
Valerio Setti016264b2023-05-22 18:40:35 +0200252 * ecp_keypair structure is used for storing the public key and performing
Valerio Setti722f8f72023-05-17 15:31:21 +0200253 * all the operations.
254 *
255 * Note: This new public key storing solution only works for EC keys, not
Valerio Settif57007d2023-05-19 13:54:39 +0200256 * other ones. The latters still use pk_ctx to store their own
Valerio Setti722f8f72023-05-17 15:31:21 +0200257 * context.
258 */
259#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
260 uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */
261 size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */
262 psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */
263 size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */
Valerio Settic1541cb2023-05-17 15:49:55 +0200264#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200266
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200267#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200268/**
269 * \brief Context for resuming operations
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271typedef struct {
272 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
273 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200274} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200275#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200276/* Now we can declare functions that take a pointer to that */
277typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200278#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200281/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200282 * \brief Types for RSA-alt abstraction
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen,
285 const unsigned char *input, unsigned char *output,
286 size_t output_max_len);
287typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
288 int (*f_rng)(void *, unsigned char *, size_t),
289 void *p_rng,
290 mbedtls_md_type_t md_alg, unsigned int hashlen,
291 const unsigned char *hash, unsigned char *sig);
292typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200294
295/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200296 * \brief Return information associated with the given PK type
297 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200298 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200299 *
300 * \return The PK info associated with the type or NULL if not found.
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200303
304/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 * \brief Initialize a #mbedtls_pk_context (as NONE).
306 *
307 * \param ctx The context to initialize.
308 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200311
312/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500313 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100314 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 * \param ctx The context to clear. It must have been initialized.
316 * If this is \c NULL, this function does nothing.
317 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100318 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100319 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200320 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100321 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200324
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200325#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200326/**
327 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 *
329 * \param ctx The context to initialize.
330 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200333
334/**
335 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500336 *
337 * \param ctx The context to clear. It must have been initialized.
338 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200341#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200342
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200343/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200344 * \brief Initialize a PK context with the information given
345 * and allocates the type-specific PK subcontext.
346 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 * \param ctx Context to initialize. It must not have been set
348 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200349 * \param info Information to use
350 *
351 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200353 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200354 *
Paul Bakker42099c32014-01-27 11:45:49 +0100355 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200356 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200359
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200360#if defined(MBEDTLS_USE_PSA_CRYPTO)
361/**
Gilles Peskine11392492019-05-27 14:53:19 +0200362 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200363 *
Valerio Settif5a6e222024-03-18 11:06:44 +0100364 * This function helps creating a PK context which wraps a
365 * PSA key. The PSA wrapped key must:
366 * * remain valid as long as the wrapping PK context is in use,
367 * that is at least between the point this function is
368 * called and the point mbedtls_pk_free() is called on this
369 * context;
370 * * be a key pair;
371 * * be an EC or RSA type (DH is not suported in PK module).
372 *
373 * Under the hood PSA functions are used to perform the required
374 * operations and, based on the key type, used algorithms will be:
375 * * EC:
Valerio Setti55ed91e2024-03-19 11:32:51 +0100376 * * verify, verify_ext: #PSA_ALG_ECDSA_ANY;
377 * * sign, sign_ext: try #PSA_ALG_DETERMINISTIC_ECDSA()
378 * first and, in case it fails, try with #PSA_ALG_ECDSA().
Valerio Settif5a6e222024-03-18 11:06:44 +0100379 * * RSA:
380 * * sign: #PSA_ALG_RSA_PKCS1V15_SIGN();
Valerio Setti622f9052024-03-18 17:12:49 +0100381 * * sign_ext: use the algorithm associated with the wrapped
382 * PSA key;
383 * * verify: not supported;
384 * * verify_ext: not supported;
385 * * decrypt: #PSA_ALG_RSA_PKCS1V15_CRYPT;
386 * * encrypt: not supported.
Valerio Setti55ed91e2024-03-19 11:32:51 +0100387 * In order for the above operations to succeed, the policy of
388 * the wrapped PSA key must allow the specified algorithm.
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100389 *
390 * \param ctx The context to initialize. It must be empty (type NONE).
Neil Armstrongb3547422022-03-22 10:22:28 +0100391 * \param key The PSA key to wrap, which must hold an ECC or RSA key
392 * pair (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200393 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100394 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100395 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200396 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100397 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100398 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100399 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200400 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
402 const mbedtls_svc_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200403#endif /* MBEDTLS_USE_PSA_CRYPTO */
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200406/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200407 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200408 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 * \param ctx Context to initialize. It must not have been set
410 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200411 * \param key RSA key pointer
412 * \param decrypt_func Decryption function
413 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200414 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200415 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200417 * context wasn't already initialized as RSA_ALT.
418 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200419 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200420 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
422 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
423 mbedtls_pk_rsa_alt_sign_func sign_func,
424 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200426
427/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200428 * \brief Get the size in bits of the underlying key
429 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200431 *
432 * \return Key size in bits, or 0 on error
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200435
436/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200437 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500438 *
439 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200440 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200441 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200446}
447
448/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200449 * \brief Tell if a context can do the operation given by type
450 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500451 * \param ctx The context to query. It must have been initialized.
452 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200453 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500454 * \return 1 if the context can do operations on the given type.
455 * \return 0 if the context cannot do the operations on the given
456 * type. This is always the case for a context that has
457 * been initialized but not set up, or that has been
458 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200461
Neil Armstrong0b529582022-05-11 10:10:20 +0200462#if defined(MBEDTLS_USE_PSA_CRYPTO)
463/**
Neil Armstrongcec133a2022-05-17 11:56:01 +0200464 * \brief Tell if context can do the operation given by PSA algorithm
Neil Armstrong0b529582022-05-11 10:10:20 +0200465 *
466 * \param ctx The context to query. It must have been initialized.
467 * \param alg PSA algorithm to check against, the following are allowed:
468 * PSA_ALG_RSA_PKCS1V15_SIGN(hash),
469 * PSA_ALG_RSA_PSS(hash),
470 * PSA_ALG_RSA_PKCS1V15_CRYPT,
471 * PSA_ALG_ECDSA(hash),
472 * PSA_ALG_ECDH, where hash is a specific hash.
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100473 * \param usage PSA usage flag to check against, must be composed of:
Neil Armstrong408f6a62022-05-17 14:23:20 +0200474 * PSA_KEY_USAGE_SIGN_HASH
475 * PSA_KEY_USAGE_DECRYPT
476 * PSA_KEY_USAGE_DERIVE.
477 * Context key must match all passed usage flags.
Neil Armstrong0b529582022-05-11 10:10:20 +0200478 *
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200479 * \warning Since the set of allowed algorithms and usage flags may be
480 * expanded in the future, the return value \c 0 should not
481 * be taken in account for non-allowed algorithms and usage
482 * flags.
483 *
Neil Armstrong0b529582022-05-11 10:10:20 +0200484 * \return 1 if the context can do operations on the given type.
485 * \return 0 if the context cannot do the operations on the given
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200486 * type, for non-allowed algorithms and usage flags, or
487 * for a context that has been initialized but not set up
488 * or that has been cleared with mbedtls_pk_free().
Neil Armstrong0b529582022-05-11 10:10:20 +0200489 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
491 psa_key_usage_t usage);
Neil Armstrong0b529582022-05-11 10:10:20 +0200492#endif /* MBEDTLS_USE_PSA_CRYPTO */
493
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100494#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine0b172552024-01-18 14:11:26 +0100495/**
496 * \brief Determine valid PSA attributes that can be used to
497 * import a key into PSA.
498 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100499 * The attributes determined by this function are suitable
500 * for calling mbedtls_pk_import_into_psa() to create
501 * a PSA key with the same key material.
Gilles Peskine0b172552024-01-18 14:11:26 +0100502 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100503 * The typical flow of operations involving this function is
504 * ```
505 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
506 * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes);
507 * if (ret != 0) ...; // error handling omitted
508 * // Tweak attributes if desired
509 * psa_key_id_t key_id = 0;
510 * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id);
511 * if (ret != 0) ...; // error handling omitted
512 * ```
Gilles Peskine0b172552024-01-18 14:11:26 +0100513 *
514 * \note This function does not support RSA-alt contexts
515 * (set up with mbedtls_pk_setup_rsa_alt()).
516 *
517 * \param[in] pk The PK context to use. It must have been set up.
518 * It can either contain a key pair or just a public key.
519 * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following:
520 * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a
521 * key pair. The output \p attributes will contain a
522 * key pair type, and the usage policy will allow
523 * #PSA_KEY_USAGE_ENCRYPT as well as
524 * #PSA_KEY_USAGE_DECRYPT.
525 * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a
526 * key pair. The output \p attributes will contain a
527 * key pair type.
528 * - #PSA_KEY_USAGE_ENCRYPT: The output
529 * \p attributes will contain a public key type.
530 * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a
531 * key pair. The output \p attributes will contain a
532 * key pair type, and the usage policy will allow
533 * #PSA_KEY_USAGE_VERIFY_HASH as well as
534 * #PSA_KEY_USAGE_SIGN_HASH.
535 * - #PSA_KEY_USAGE_SIGN_MESSAGE: \p pk must contain a
536 * key pair. The output \p attributes will contain a
537 * key pair type, and the usage policy will allow
538 * #PSA_KEY_USAGE_VERIFY_MESSAGE as well as
539 * #PSA_KEY_USAGE_SIGN_MESSAGE.
540 * - #PSA_KEY_USAGE_VERIFY_HASH: The output
541 * \p attributes will contain a public key type.
542 * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output
543 * \p attributes will contain a public key type.
544 * \param[out] attributes
545 * On success, valid attributes to import the key into PSA.
546 * - The lifetime and key identifier are unchanged. If the
547 * attribute structure was initialized or reset before
548 * calling this function, this will result in a volatile
549 * key. Call psa_set_key_identifier() before or after this
550 * function if you wish to create a persistent key. Call
551 * psa_set_key_lifetime() before or after this function if
552 * you wish to import the key in a secure element.
553 * - The key type and bit-size are determined by the contents
554 * of the PK context. If the PK context contains a key
555 * pair, the key type can be either a key pair type or
556 * the corresponding public key type, depending on
557 * \p usage. If the PK context contains a public key,
558 * the key type is a public key type.
559 * - The key's policy is determined by the key type and
560 * the \p usage parameter. The usage always allows
561 * \p usage, exporting and copying the key, and
562 * possibly other permissions as documented for the
563 * \p usage parameter.
Gilles Peskinee208b252024-02-01 20:42:21 +0100564 * The permitted algorithm policy is determined as follows
Gilles Peskine0b172552024-01-18 14:11:26 +0100565 * based on the #mbedtls_pk_type_t type of \p pk,
566 * the chosen \p usage and other factors:
Gilles Peskinee208b252024-02-01 20:42:21 +0100567 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100568 * #mbedtls_rsa_context has the padding mode
569 * #MBEDTLS_RSA_PKCS_V15:
570 * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH)
571 * if \p usage is SIGN/VERIFY, and
572 * #PSA_ALG_RSA_PKCS1V15_CRYPT
573 * if \p usage is ENCRYPT/DECRYPT.
Gilles Peskinee208b252024-02-01 20:42:21 +0100574 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100575 * #mbedtls_rsa_context has the padding mode
576 * #MBEDTLS_RSA_PKCS_V21 and the digest type
577 * corresponding to the PSA algorithm \c hash:
578 * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH)
579 * if \p usage is SIGN/VERIFY, and
580 * #PSA_ALG_RSA_OAEP(\c hash)
581 * if \p usage is ENCRYPT/DECRYPT.
582 * - #MBEDTLS_PK_RSA_ALT: not supported.
583 * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY
584 * if \p usage is SIGN/VERIFY:
585 * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH)
586 * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled,
587 * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH).
588 * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY
589 * if \p usage is DERIVE:
590 * #PSA_ALG_ECDH.
Gilles Peskinee208b252024-02-01 20:42:21 +0100591 * - #MBEDTLS_PK_OPAQUE: same as the primary algorithm
Gilles Peskine0b172552024-01-18 14:11:26 +0100592 * set for the underlying PSA key, except that
593 * sign/decrypt flags are removed if the type is
594 * set to a public key type.
Gilles Peskine793920c2024-02-01 21:26:54 +0100595 * The underlying key must allow \p usage.
Gilles Peskine0b172552024-01-18 14:11:26 +0100596 * Note that the enrollment algorithm set with
597 * psa_set_key_enrollment_algorithm() is not copied.
598 *
599 * \return 0 on success.
600 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
601 * a key of the type identified in \p attributes.
602 * Another error code on other failures.
603 */
604int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
605 psa_key_usage_t usage,
606 psa_key_attributes_t *attributes);
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100607
608/**
609 * \brief Import a key into the PSA key store.
610 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100611 * This function is equivalent to calling psa_import_key()
612 * with the key material from \p pk.
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100613 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100614 * The typical way to use this function is:
615 * -# Call mbedtls_pk_get_psa_attributes() to obtain
616 * attributes for the given key.
617 * -# If desired, modify the attributes, for example:
618 * - To create a persistent key, call
619 * psa_set_key_identifier() and optionally
620 * psa_set_key_lifetime().
621 * - To import only the public part of a key pair:
622 *
623 * psa_set_key_type(&attributes,
624 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
625 * psa_get_key_type(&attributes)));
626 * - Restrict the key usage if desired.
627 * -# Call mbedtls_pk_import_into_psa().
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100628 *
629 * \note This function does not support RSA-alt contexts
630 * (set up with mbedtls_pk_setup_rsa_alt()).
631 *
632 * \param[in] pk The PK context to use. It must have been set up.
633 * It can either contain a key pair or just a public key.
634 * \param[in] attributes
635 * The attributes to use for the new key. They must be
636 * compatible with \p pk. In particular, the key type
637 * must match the content of \p pk.
638 * If \p pk contains a key pair, the key type in
639 * attributes can be either the key pair type or the
640 * corresponding public key type (to import only the
641 * public part).
642 * \param[out] key_id
643 * On success, the identifier of the newly created key.
644 * On error, this is #MBEDTLS_SVC_KEY_ID_INIT.
645 *
646 * \return 0 on success.
647 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
648 * a key of the type identified in \p attributes.
649 * Another error code on other failures.
650 */
651int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
652 const psa_key_attributes_t *attributes,
653 mbedtls_svc_key_id_t *key_id);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100654
655/**
656 * \brief Create a PK context starting from a key stored in PSA.
657 * This key:
658 * - must be exportable and
659 * - must be an RSA or EC key pair or public key (FFDH is not supported in PK).
660 *
661 * The resulting PK object will be a transparent type:
662 * - #MBEDTLS_PK_RSA for RSA keys or
663 * - #MBEDTLS_PK_ECKEY for EC keys.
664 *
665 * Once this functions returns the PK object will be completely
666 * independent from the original PSA key that it was generated
667 * from.
668 * Calling mbedtls_pk_sign(), mbedtls_pk_verify(),
669 * mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting
670 * PK context will perform the corresponding algorithm for that
671 * PK context type.
672 * * For ECDSA, the choice of deterministic vs randomized will
673 * be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC.
674 * * For an RSA key, the output PK context will allow both
675 * encrypt/decrypt and sign/verify regardless of the original
676 * key's policy.
677 * The original key's policy determines the output key's padding
678 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
679 * otherwise PKCS1 v1.5 is set.
680 *
681 * \param key_id The key identifier of the key stored in PSA.
682 * \param pk The PK context that will be filled. It must be initialized,
683 * but not set up.
684 *
685 * \return 0 on success.
686 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
687 * parameters are not correct.
688 */
689int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
690
691/**
692 * \brief Create a PK context for the public key of a PSA key.
693 *
694 * The key must be an RSA or ECC key. It can be either a
695 * public key or a key pair, and only the public key is copied.
696 * The resulting PK object will be a transparent type:
697 * - #MBEDTLS_PK_RSA for RSA keys or
698 * - #MBEDTLS_PK_ECKEY for EC keys.
699 *
700 * Once this functions returns the PK object will be completely
701 * independent from the original PSA key that it was generated
702 * from.
703 * Calling mbedtls_pk_verify() or
704 * mbedtls_pk_encrypt() on the resulting
705 * PK context will perform the corresponding algorithm for that
706 * PK context type.
707 *
708 * For an RSA key, the output PK context will allow both
709 * encrypt and verify regardless of the original key's policy.
710 * The original key's policy determines the output key's padding
711 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
712 * otherwise PKCS1 v1.5 is set.
713 *
714 * \param key_id The key identifier of the key stored in PSA.
715 * \param pk The PK context that will be filled. It must be initialized,
716 * but not set up.
717 *
718 * \return 0 on success.
719 * \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
720 * parameters are not correct.
721 */
722int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
723#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine0b172552024-01-18 14:11:26 +0100724
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200725/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200726 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200727 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500728 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200729 * \param md_alg Hash algorithm used.
730 * This can be #MBEDTLS_MD_NONE if the signature algorithm
731 * does not rely on a hash algorithm (non-deterministic
732 * ECDSA, RSA PKCS#1 v1.5).
733 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
734 * \p hash is the DigestInfo structure used by RFC 8017
735 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
736 * algorithm then \p hash is the digest itself, and this
737 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200738 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200739 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200740 * \param sig Signature to verify
741 * \param sig_len Signature length
742 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100743 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
744 * either PKCS#1 v1.5 or PSS (accepting any salt length),
745 * depending on the padding mode in the underlying RSA context.
746 * For a pk object constructed by parsing, this is PKCS#1 v1.5
747 * by default. Use mbedtls_pk_verify_ext() to explicitly select
748 * a different algorithm.
749 *
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200750 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200751 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500752 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200753 * or a specific error code.
754 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
756 const unsigned char *hash, size_t hash_len,
757 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200758
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200759/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200760 * \brief Restartable version of \c mbedtls_pk_verify()
761 *
762 * \note Performs the same job as \c mbedtls_pk_verify(), but can
763 * return early and restart according to the limit set with
764 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
765 * operations. For RSA, same as \c mbedtls_pk_verify().
766 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500767 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200768 * \param md_alg Hash algorithm used (see notes)
769 * \param hash Hash of the message to sign
770 * \param hash_len Hash length or 0 (see notes)
771 * \param sig Signature to verify
772 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200773 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200774 *
775 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200776 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200777 * operations was reached: see \c mbedtls_ecp_set_max_ops().
778 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
780 mbedtls_md_type_t md_alg,
781 const unsigned char *hash, size_t hash_len,
782 const unsigned char *sig, size_t sig_len,
783 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200784
785/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200786 * \brief Verify signature, with options.
787 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200788 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200789 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200790 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500791 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200792 * \param md_alg Hash algorithm used (see notes)
793 * \param hash Hash of the message to sign
794 * \param hash_len Hash length or 0 (see notes)
795 * \param sig Signature to verify
796 * \param sig_len Signature length
797 *
798 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200799 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200800 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200801 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500802 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200803 * or a specific error code.
804 *
805 * \note If hash_len is 0, then the length associated with md_alg
806 * is used instead, or an error returned if it is invalid.
807 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200809 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
811 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnarda6e02912022-12-21 09:59:33 +0100812 * otherwise it must be NULL. Note that if
813 * #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not
814 * verified as PSA_ALG_RSA_PSS_ANY_SALT is used.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200815 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
817 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
818 const unsigned char *hash, size_t hash_len,
819 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200820
821/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200822 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200823 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500824 * \param ctx The PK context to use. It must have been set up
825 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200826 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200827 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200828 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100829 * \param sig Place to write the signature.
830 * It must have enough room for the signature.
831 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
832 * You may use a smaller buffer if it is large enough
833 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200834 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100835 * \param sig_len On successful return,
836 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200837 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200838 * \param p_rng RNG parameter
839 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100840 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
841 * either PKCS#1 v1.5 or PSS (using the largest possible salt
842 * length up to the hash length), depending on the padding mode
843 * in the underlying RSA context. For a pk object constructed
844 * by parsing, this is PKCS#1 v1.5 by default. Use
845 * mbedtls_pk_verify_ext() to explicitly select a different
846 * algorithm.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200847 *
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200848 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200849 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
851 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200852 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
854 const unsigned char *hash, size_t hash_len,
855 unsigned char *sig, size_t sig_size, size_t *sig_len,
856 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200857
858/**
Jerry Yu406cf272022-03-22 11:33:42 +0800859 * \brief Make signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800860 *
Jerry Yu8beb9e12022-03-12 16:23:53 +0800861 * \param pk_type Signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800862 * \param ctx The PK context to use. It must have been set up
863 * with a private key.
864 * \param md_alg Hash algorithm used (see notes)
865 * \param hash Hash of the message to sign
866 * \param hash_len Hash length
867 * \param sig Place to write the signature.
868 * It must have enough room for the signature.
869 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
870 * You may use a smaller buffer if it is large enough
871 * given the key type.
872 * \param sig_size The size of the \p sig buffer in bytes.
873 * \param sig_len On successful return,
874 * the number of bytes written to \p sig.
875 * \param f_rng RNG function, must not be \c NULL.
876 * \param p_rng RNG parameter
877 *
878 * \return 0 on success, or a specific error code.
879 *
Jerry Yue6e73d62022-03-24 13:05:08 +0800880 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
881 * see #PSA_ALG_RSA_PSS for a description of PSS options used.
Jerry Yud69439a2022-02-24 15:52:15 +0800882 *
883 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
884 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
885 *
Jerry Yud69439a2022-02-24 15:52:15 +0800886 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
888 mbedtls_pk_context *ctx,
889 mbedtls_md_type_t md_alg,
890 const unsigned char *hash, size_t hash_len,
891 unsigned char *sig, size_t sig_size, size_t *sig_len,
892 int (*f_rng)(void *, unsigned char *, size_t),
893 void *p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800894
895/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200896 * \brief Restartable version of \c mbedtls_pk_sign()
897 *
898 * \note Performs the same job as \c mbedtls_pk_sign(), but can
899 * return early and restart according to the limit set with
900 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
901 * operations. For RSA, same as \c mbedtls_pk_sign().
902 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500903 * \param ctx The PK context to use. It must have been set up
904 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100905 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200906 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200907 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100908 * \param sig Place to write the signature.
909 * It must have enough room for the signature.
910 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
911 * You may use a smaller buffer if it is large enough
912 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200913 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100914 * \param sig_len On successful return,
915 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200916 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200917 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200918 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200919 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100920 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200921 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200922 * operations was reached: see \c mbedtls_ecp_set_max_ops().
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
925 mbedtls_md_type_t md_alg,
926 const unsigned char *hash, size_t hash_len,
927 unsigned char *sig, size_t sig_size, size_t *sig_len,
928 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
929 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200930
931/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200932 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200933 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500934 * \param ctx The PK context to use. It must have been set up
935 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200936 * \param input Input to decrypt
937 * \param ilen Input size
938 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200939 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200940 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200941 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200942 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200943 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100944 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
945 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
946 * the underlying RSA context. For a pk object constructed by
947 * parsing, this is PKCS#1 v1.5 by default.
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200948 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200949 * \return 0 on success, or a specific error code.
950 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
952 const unsigned char *input, size_t ilen,
953 unsigned char *output, size_t *olen, size_t osize,
954 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200955
956/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200957 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200958 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500959 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200960 * \param input Message to encrypt
961 * \param ilen Message size
962 * \param output Encrypted output
963 * \param olen Encrypted output length
964 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200965 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200966 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200967 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100968 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
969 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
970 * the underlying RSA context. For a pk object constructed by
971 * parsing, this is PKCS#1 v1.5 by default.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200972 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200973 * \note \p f_rng is used for padding generation.
974 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200975 * \return 0 on success, or a specific error code.
976 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
978 const unsigned char *input, size_t ilen,
979 unsigned char *output, size_t *olen, size_t osize,
980 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200981
982/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100983 * \brief Check if a public-private pair of keys matches.
984 *
985 * \param pub Context holding a public key.
986 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200987 * \param f_rng RNG function, must not be \c NULL.
988 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100989 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200990 * \return \c 0 on success (keys were checked and match each other).
991 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
992 * be checked - in that case they may or may not match.
993 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
994 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
997 const mbedtls_pk_context *prv,
998 int (*f_rng)(void *, unsigned char *, size_t),
999 void *p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001000
1001/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001002 * \brief Export debug information
1003 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001004 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001005 * \param items Place to write debug items
1006 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001010
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001011/**
1012 * \brief Access the type name
1013 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001014 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001015 *
1016 * \return Type name on success, or "invalid PK"
1017 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001019
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001020/**
Paul Bakker4fc090a2013-09-18 15:43:25 +02001021 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001022 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001023 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001024 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001025 * \return Type on success.
1026 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001029
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001030#if defined(MBEDTLS_RSA_C)
1031/**
1032 * Quick access to an RSA context inside a PK context.
1033 *
1034 * \warning This function can only be used when the type of the context, as
1035 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
1036 * Ensuring that is the caller's responsibility.
1037 * Alternatively, you can check whether this function returns NULL.
1038 *
1039 * \return The internal RSA context held by the PK context, or NULL.
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001042{
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001044 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001046 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001048 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001049}
1050#endif /* MBEDTLS_RSA_C */
1051
Valerio Setti229bf102023-05-15 11:13:55 +02001052#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001053/**
1054 * Quick access to an EC context inside a PK context.
1055 *
1056 * \warning This function can only be used when the type of the context, as
1057 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
1058 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
1059 * Ensuring that is the caller's responsibility.
1060 * Alternatively, you can check whether this function returns NULL.
1061 *
1062 * \return The internal EC context held by the PK context, or NULL.
1063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001065{
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001067 case MBEDTLS_PK_ECKEY:
1068 case MBEDTLS_PK_ECKEY_DH:
1069 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001071 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001073 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001074}
Valerio Setti229bf102023-05-15 11:13:55 +02001075#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001078/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001079/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001080 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001082 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
1083 * subsystem must have been initialized by calling
1084 * psa_crypto_init() before calling this function.
1085 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001086 * \param ctx The PK context to fill. It must have been initialized
1087 * but not set up.
1088 * \param key Input buffer to parse.
1089 * The buffer must contain the input exactly, with no
1090 * extra trailing material. For PEM, the buffer must
1091 * contain a null-terminated string.
1092 * \param keylen Size of \b key in bytes.
1093 * For PEM data, this includes the terminating null byte,
1094 * so \p keylen must be equal to `strlen(key) + 1`.
1095 * \param pwd Optional password for decryption.
1096 * Pass \c NULL if expecting a non-encrypted key.
1097 * Pass a string of \p pwdlen bytes if expecting an encrypted
1098 * key; a non-encrypted key will also be accepted.
1099 * The empty password is not supported.
1100 * \param pwdlen Size of the password in bytes.
1101 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001102 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1103 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001105 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1107 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001108 *
1109 * \note The key is also checked for correctness.
1110 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111 * \return 0 if successful, or a specific PK or PEM error code
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
1114 const unsigned char *key, size_t keylen,
1115 const unsigned char *pwd, size_t pwdlen,
1116 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117
Paul Bakkerff3a5182013-09-16 22:42:19 +02001118/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001120 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +02001121 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001122 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
1123 * subsystem must have been initialized by calling
1124 * psa_crypto_init() before calling this function.
1125 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001126 * \param ctx The PK context to fill. It must have been initialized
1127 * but not set up.
1128 * \param key Input buffer to parse.
1129 * The buffer must contain the input exactly, with no
1130 * extra trailing material. For PEM, the buffer must
1131 * contain a null-terminated string.
1132 * \param keylen Size of \b key in bytes.
1133 * For PEM data, this includes the terminating null byte,
1134 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001136 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1138 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001139 *
Valerio Setti78f79d32023-02-07 08:33:26 +01001140 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
1141 * limitations.
1142 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001143 * \note The key is also checked for correctness.
1144 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 * \return 0 if successful, or a specific PK or PEM error code
1146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1148 const unsigned char *key, size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001151/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001153 * \brief Load and parse a private key
1154 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001155 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
1156 * subsystem must have been initialized by calling
1157 * psa_crypto_init() before calling this function.
1158 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001159 * \param ctx The PK context to fill. It must have been initialized
1160 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001161 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001162 * \param password Optional password to decrypt the file.
1163 * Pass \c NULL if expecting a non-encrypted key.
1164 * Pass a null-terminated string if expecting an encrypted
1165 * key; a non-encrypted key will also be accepted.
1166 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001167 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1168 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001169 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001170 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1172 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001173 *
1174 * \note The key is also checked for correctness.
1175 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001176 * \return 0 if successful, or a specific PK or PEM error code
1177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1179 const char *path, const char *password,
1180 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001181
Paul Bakkerff3a5182013-09-16 22:42:19 +02001182/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001183/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 * \brief Load and parse a public key
1185 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001186 * \param ctx The PK context to fill. It must have been initialized
1187 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +01001188 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001190 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +01001191 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
1192 * you need a specific key type, check the result with
1193 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001194 *
1195 * \note The key is also checked for correctness.
1196 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 * \return 0 if successful, or a specific PK or PEM error code
1198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#endif /* MBEDTLS_FS_IO */
1201#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001205 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
1206 * Note: data is written at the end of the buffer! Use the
1207 * return value to determine where you should start
1208 * using the buffer
1209 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001210 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001211 * \param buf buffer to write to
1212 * \param size size of the buffer
1213 *
1214 * \return length of data written if successful, or a specific
1215 * error code
1216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001218
1219/**
1220 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
1221 * Note: data is written at the end of the buffer! Use the
1222 * return value to determine where you should start
1223 * using the buffer
1224 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001225 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001226 * \param buf buffer to write to
1227 * \param size size of the buffer
1228 *
1229 * \return length of data written if successful, or a specific
1230 * error code
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001235/**
1236 * \brief Write a public key to a PEM string
1237 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001238 * \param ctx PK context which must contain a valid public or private key.
1239 * \param buf Buffer to write to. The output includes a
1240 * terminating null byte.
1241 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001242 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001243 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001246
1247/**
1248 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
1249 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001250 * \param ctx PK context which must contain a valid private key.
1251 * \param buf Buffer to write to. The output includes a
1252 * terminating null byte.
1253 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001254 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001255 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_PEM_WRITE_C */
1259#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001260
1261/*
1262 * WARNING: Low-level functions. You probably do not want to use these unless
1263 * you are certain you do ;)
1264 */
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001267/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268 * \brief Parse a SubjectPublicKeyInfo DER structure
1269 *
1270 * \param p the position in the ASN.1 data
1271 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001272 * \param pk The PK context to fill. It must have been initialized
1273 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 *
1275 * \return 0 if successful, or a specific PK error code
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
1278 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001282/**
1283 * \brief Write a subjectPublicKey to ASN.1 data
1284 * Note: function works backwards in data buffer
1285 *
1286 * \param p reference to current position pointer
1287 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001288 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001289 *
1290 * \return the length written or a negative error code
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
1293 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001295
Paul Bakkered27a042013-04-18 22:46:23 +02001296#ifdef __cplusplus
1297}
1298#endif
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#endif /* MBEDTLS_PK_H */