blob: 4866ec565e058c44008f432f799d243d1e0ecc88 [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
Ronald Cronf6d17ca2024-11-06 14:11:15 +010015#include "tf-psa-crypto/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 *
Janos Follath056cb142024-11-19 16:17:36 +000091 * \note When #MBEDTLS_RSA_C is disabled, this must be equal to the \c md_alg argument passed
92 * to mbedtls_pk_verify_ext(). In a future version of the library, this constraint may
93 * apply regardless of the status of #MBEDTLS_RSA_C.
Gilles Peskine34c43a82023-02-02 23:06:37 +010094 */
95 mbedtls_md_type_t mgf1_hash_id;
96
97 /** The expected length of the salt, in bytes. This may be
98 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
99 *
Janos Follath056cb142024-11-19 16:17:36 +0000100 * \note Only #MBEDTLS_RSA_SALT_LEN_ANY is valid. Any other value may be ignored (allowing any
101 * salt length).
Gilles Peskine34c43a82023-02-02 23:06:37 +0100102 */
103 int expected_salt_len;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200106
107/**
Gilles Peskineda252be2019-11-05 16:23:49 +0100108 * \brief Maximum size of a signature made by mbedtls_pk_sign().
109 */
Gilles Peskine54605652019-11-08 16:24:16 +0100110/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
111 * size among the supported signature types. Do it by starting at 0,
112 * then incrementally increasing to be large enough for each supported
113 * signature mechanism.
114 *
115 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
116 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
117 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
118 */
119#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100122 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100123/* For RSA, the signature can be as large as the bignum module allows.
124 * For RSA_ALT, the signature size is not necessarily tied to what the
125 * bignum module can do, but in the absence of any specific setting,
Chris Jones3848e312021-03-11 16:17:59 +0000126 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100127#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
128#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
129#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100130
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100131#if defined(MBEDTLS_ECDSA_C) && \
132 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100133/* For ECDSA, the ecdsa module exports a constant for the maximum
134 * signature size. */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100135#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
136#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
137#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100138
139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100140#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
141/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
Gilles Peskine54605652019-11-08 16:24:16 +0100142 * through the PSA API in the PSA representation. */
143#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100144#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100145#endif
146
147#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
148/* The Mbed TLS representation is different for ECDSA signatures:
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100149 * PSA uses the raw concatenation of r and s,
150 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
151 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
152 * types, lengths (represented by up to 2 bytes), and potential leading
153 * zeros of the INTEGERs and the SEQUENCE. */
154#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine449bd832023-01-11 14:50:10 +0100155#define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100156#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100157#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100158
Valerio Settia9aab1a2023-06-19 13:39:54 +0200159/* Internal helper to define which fields in the pk_context structure below
160 * should be used for EC keys: legacy ecp_keypair or the raw (PSA friendly)
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200161 * format. It should be noted that this only affects how data is stored, not
Valerio Settia9aab1a2023-06-19 13:39:54 +0200162 * which functions are used for various operations. The overall picture looks
163 * like this:
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200164 * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data
Janos Follath056cb142024-11-19 16:17:36 +0000165 * structure and legacy functions. (MBEDTLS_USE_PSA_CRYPTO is always on and
166 * although this codepath remains present, it never will be taken.)
Valerio Settia9aab1a2023-06-19 13:39:54 +0200167 * - if USE_PSA is defined and
168 * - if ECP_C then use ecp_keypair structure, convert data to a PSA friendly
169 * format and use PSA functions
170 * - if !ECP_C then use new raw data and PSA functions directly.
171 *
172 * The main reason for the "intermediate" (USE_PSA + ECP_C) above is that as long
173 * 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 +0200174 * ecp_keypair structure inside the pk_context so they can modify it using
Valerio Settia9aab1a2023-06-19 13:39:54 +0200175 * ECP functions which are not under PK module's control.
176 */
177#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \
178 !defined(MBEDTLS_ECP_C)
179#define MBEDTLS_PK_USE_PSA_EC_DATA
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200180#endif
Valerio Settia9aab1a2023-06-19 13:39:54 +0200181
Valerio Setticf084ae2023-01-26 14:30:12 +0100182/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183 * \brief Types for interfacing with the debug module
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 MBEDTLS_PK_DEBUG_NONE = 0,
187 MBEDTLS_PK_DEBUG_MPI,
188 MBEDTLS_PK_DEBUG_ECP,
Valerio Setti92c3f362023-05-17 15:36:44 +0200189 MBEDTLS_PK_DEBUG_PSA_EC,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200191
192/**
193 * \brief Item to send to the debug module
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195typedef struct mbedtls_pk_debug_item {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200196 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
197 const char *MBEDTLS_PRIVATE(name);
198 void *MBEDTLS_PRIVATE(value);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200200
201/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200203
204/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200205 * \brief Public key information and operations
Gilles Peskine2e9d65f2021-08-31 23:05:19 +0200206 *
207 * \note The library does not support custom pk info structures,
208 * only built-in structures returned by
209 * mbedtls_cipher_info_from_type().
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200212
Valerio Setti722f8f72023-05-17 15:31:21 +0200213#define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
214 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200215/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200216 * \brief Public key container
217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218typedef struct mbedtls_pk_context {
219 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
220 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200221 /* The following field is used to store the ID of a private key in the
222 * following cases:
Janos Follath056cb142024-11-19 16:17:36 +0000223 * - opaque key
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200224 * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case:
225 * - the pk_ctx above is not not used to store the private key anymore.
226 * Actually that field not populated at all in this case because also
227 * the public key will be stored in raw format as explained below
228 * - this ID is used for all private key operations (ex: sign, check
229 * key pair, key write, etc) using PSA functions
230 *
231 * Note: this private key storing solution only affects EC keys, not the
232 * other ones. The latters still use the pk_ctx to store their own
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200233 * context. */
234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +0200235 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200236#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +0200237 /* The following fields are meant for storing the public key in raw format
238 * which is handy for:
239 * - easily importing it into the PSA context
240 * - reducing the ECP module dependencies in the PK one.
241 *
242 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled:
243 * - the pk_ctx above is not used anymore for storing the public key
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200244 * inside the ecp_keypair structure
Valerio Setti722f8f72023-05-17 15:31:21 +0200245 * - the following fields are used for all public key operations: signature
246 * verify, key pair check and key write.
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100247 * - For a key pair, priv_id contains the private key. For a public key,
248 * priv_id is null.
Valerio Setti722f8f72023-05-17 15:31:21 +0200249 * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy
Valerio Setti016264b2023-05-22 18:40:35 +0200250 * ecp_keypair structure is used for storing the public key and performing
Valerio Setti722f8f72023-05-17 15:31:21 +0200251 * all the operations.
252 *
253 * Note: This new public key storing solution only works for EC keys, not
Valerio Settif57007d2023-05-19 13:54:39 +0200254 * other ones. The latters still use pk_ctx to store their own
Valerio Setti722f8f72023-05-17 15:31:21 +0200255 * context.
256 */
257#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
258 uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */
259 size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */
260 psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */
261 size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */
Valerio Settic1541cb2023-05-17 15:49:55 +0200262#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200264
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200265#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200266/**
267 * \brief Context for resuming operations
268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269typedef struct {
270 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
271 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200272} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200273#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200274/* Now we can declare functions that take a pointer to that */
275typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200276#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200279/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200280 * \brief Types for RSA-alt abstraction
281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen,
283 const unsigned char *input, unsigned char *output,
284 size_t output_max_len);
285typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
286 int (*f_rng)(void *, unsigned char *, size_t),
287 void *p_rng,
288 mbedtls_md_type_t md_alg, unsigned int hashlen,
289 const unsigned char *hash, unsigned char *sig);
290typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200292
293/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200294 * \brief Return information associated with the given PK type
295 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200296 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200297 *
298 * \return The PK info associated with the type or NULL if not found.
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200301
302/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 * \brief Initialize a #mbedtls_pk_context (as NONE).
304 *
305 * \param ctx The context to initialize.
306 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200309
310/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500311 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100312 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313 * \param ctx The context to clear. It must have been initialized.
314 * If this is \c NULL, this function does nothing.
315 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100316 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100317 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200318 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100319 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200322
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200323#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200324/**
325 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 *
327 * \param ctx The context to initialize.
328 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200331
332/**
333 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 *
335 * \param ctx The context to clear. It must have been initialized.
336 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200337 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200339#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200340
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200341/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200342 * \brief Initialize a PK context with the information given
343 * and allocates the type-specific PK subcontext.
344 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 * \param ctx Context to initialize. It must not have been set
346 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200347 * \param info Information to use
348 *
349 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200351 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200352 *
Paul Bakker42099c32014-01-27 11:45:49 +0100353 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200354 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200355 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200357
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200358#if defined(MBEDTLS_USE_PSA_CRYPTO)
359/**
Valerio Settifc6b22c2024-03-20 16:08:08 +0100360 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200361 *
Valerio Settif9f63ed2024-03-25 09:37:47 +0100362 * This function creates a PK context which wraps a PSA key. The PSA wrapped
363 * key must be an EC or RSA key pair (DH is not suported in the PK module).
Valerio Settif5a6e222024-03-18 11:06:44 +0100364 *
Valerio Settif9f63ed2024-03-25 09:37:47 +0100365 * Under the hood PSA functions will be used to perform the required
Valerio Settifc6b22c2024-03-20 16:08:08 +0100366 * operations and, based on the key type, used algorithms will be:
367 * * EC:
368 * * verify, verify_ext, sign, sign_ext: ECDSA.
369 * * RSA:
Valerio Setti42a39542024-03-21 16:22:24 +0100370 * * sign, decrypt: use the primary algorithm in the wrapped PSA key;
371 * * sign_ext: RSA PSS if the pk_type is #MBEDTLS_PK_RSASSA_PSS, otherwise
372 * it falls back to the sign() case;
Valerio Settifc6b22c2024-03-20 16:08:08 +0100373 * * verify, verify_ext, encrypt: not supported.
Valerio Setti80cd4792024-03-20 15:58:54 +0100374 *
Valerio Settifc6b22c2024-03-20 16:08:08 +0100375 * In order for the above operations to succeed, the policy of the wrapped PSA
376 * key must allow the specified algorithm.
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100377 *
Valerio Settiac81e232024-03-21 16:49:02 +0100378 * Opaque PK contexts wrapping an EC keys also support \c mbedtls_pk_check_pair(),
379 * whereas RSA ones do not.
380 *
Valerio Settifc6b22c2024-03-20 16:08:08 +0100381 * \warning The PSA wrapped key must remain valid as long as the wrapping PK
382 * context is in use, that is at least between the point this function
383 * is called and the point mbedtls_pk_free() is called on this context.
Valerio Setti80cd4792024-03-20 15:58:54 +0100384 *
Valerio Settifc6b22c2024-03-20 16:08:08 +0100385 * \param ctx The context to initialize. It must be empty (type NONE).
386 * \param key The PSA key to wrap, which must hold an ECC or RSA key pair.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200387 *
Valerio Settifc6b22c2024-03-20 16:08:08 +0100388 * \return \c 0 on success.
389 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input (context already
390 * used, invalid key identifier).
Valerio Setti1c7f5de2024-04-04 09:39:12 +0200391 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an ECC or
392 * RSA key pair.
Valerio Settifc6b22c2024-03-20 16:08:08 +0100393 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
396 const mbedtls_svc_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200397#endif /* MBEDTLS_USE_PSA_CRYPTO */
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200400/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200401 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200402 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500403 * \param ctx Context to initialize. It must not have been set
404 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200405 * \param key RSA key pointer
406 * \param decrypt_func Decryption function
407 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200408 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200409 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200411 * context wasn't already initialized as RSA_ALT.
412 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200413 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
416 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
417 mbedtls_pk_rsa_alt_sign_func sign_func,
418 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200420
421/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200422 * \brief Get the size in bits of the underlying key
423 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500424 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200425 *
426 * \return Key size in bits, or 0 on error
427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200429
430/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200431 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500432 *
433 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200434 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200435 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200436 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200438{
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200440}
441
442/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200443 * \brief Tell if a context can do the operation given by type
444 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 * \param ctx The context to query. It must have been initialized.
446 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200447 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500448 * \return 1 if the context can do operations on the given type.
449 * \return 0 if the context cannot do the operations on the given
450 * type. This is always the case for a context that has
451 * been initialized but not set up, or that has been
452 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200455
Neil Armstrong0b529582022-05-11 10:10:20 +0200456#if defined(MBEDTLS_USE_PSA_CRYPTO)
457/**
Neil Armstrongcec133a2022-05-17 11:56:01 +0200458 * \brief Tell if context can do the operation given by PSA algorithm
Neil Armstrong0b529582022-05-11 10:10:20 +0200459 *
460 * \param ctx The context to query. It must have been initialized.
461 * \param alg PSA algorithm to check against, the following are allowed:
462 * PSA_ALG_RSA_PKCS1V15_SIGN(hash),
463 * PSA_ALG_RSA_PSS(hash),
464 * PSA_ALG_RSA_PKCS1V15_CRYPT,
465 * PSA_ALG_ECDSA(hash),
466 * PSA_ALG_ECDH, where hash is a specific hash.
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100467 * \param usage PSA usage flag to check against, must be composed of:
Neil Armstrong408f6a62022-05-17 14:23:20 +0200468 * PSA_KEY_USAGE_SIGN_HASH
469 * PSA_KEY_USAGE_DECRYPT
470 * PSA_KEY_USAGE_DERIVE.
471 * Context key must match all passed usage flags.
Neil Armstrong0b529582022-05-11 10:10:20 +0200472 *
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200473 * \warning Since the set of allowed algorithms and usage flags may be
474 * expanded in the future, the return value \c 0 should not
475 * be taken in account for non-allowed algorithms and usage
476 * flags.
477 *
Neil Armstrong0b529582022-05-11 10:10:20 +0200478 * \return 1 if the context can do operations on the given type.
479 * \return 0 if the context cannot do the operations on the given
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200480 * type, for non-allowed algorithms and usage flags, or
481 * for a context that has been initialized but not set up
482 * or that has been cleared with mbedtls_pk_free().
Neil Armstrong0b529582022-05-11 10:10:20 +0200483 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
485 psa_key_usage_t usage);
Neil Armstrong0b529582022-05-11 10:10:20 +0200486#endif /* MBEDTLS_USE_PSA_CRYPTO */
487
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100488#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine0b172552024-01-18 14:11:26 +0100489/**
490 * \brief Determine valid PSA attributes that can be used to
491 * import a key into PSA.
492 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100493 * The attributes determined by this function are suitable
494 * for calling mbedtls_pk_import_into_psa() to create
495 * a PSA key with the same key material.
Gilles Peskine0b172552024-01-18 14:11:26 +0100496 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100497 * The typical flow of operations involving this function is
498 * ```
499 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
500 * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes);
501 * if (ret != 0) ...; // error handling omitted
502 * // Tweak attributes if desired
503 * psa_key_id_t key_id = 0;
504 * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id);
505 * if (ret != 0) ...; // error handling omitted
506 * ```
Gilles Peskine0b172552024-01-18 14:11:26 +0100507 *
508 * \note This function does not support RSA-alt contexts
509 * (set up with mbedtls_pk_setup_rsa_alt()).
510 *
511 * \param[in] pk The PK context to use. It must have been set up.
512 * It can either contain a key pair or just a public key.
513 * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following:
514 * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a
515 * key pair. The output \p attributes will contain a
516 * key pair type, and the usage policy will allow
517 * #PSA_KEY_USAGE_ENCRYPT as well as
518 * #PSA_KEY_USAGE_DECRYPT.
519 * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a
520 * key pair. The output \p attributes will contain a
521 * key pair type.
522 * - #PSA_KEY_USAGE_ENCRYPT: The output
523 * \p attributes will contain a public key type.
524 * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a
525 * key pair. The output \p attributes will contain a
526 * key pair type, and the usage policy will allow
527 * #PSA_KEY_USAGE_VERIFY_HASH as well as
528 * #PSA_KEY_USAGE_SIGN_HASH.
529 * - #PSA_KEY_USAGE_SIGN_MESSAGE: \p pk must contain a
530 * key pair. The output \p attributes will contain a
531 * key pair type, and the usage policy will allow
532 * #PSA_KEY_USAGE_VERIFY_MESSAGE as well as
533 * #PSA_KEY_USAGE_SIGN_MESSAGE.
534 * - #PSA_KEY_USAGE_VERIFY_HASH: The output
535 * \p attributes will contain a public key type.
536 * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output
537 * \p attributes will contain a public key type.
538 * \param[out] attributes
539 * On success, valid attributes to import the key into PSA.
540 * - The lifetime and key identifier are unchanged. If the
541 * attribute structure was initialized or reset before
542 * calling this function, this will result in a volatile
543 * key. Call psa_set_key_identifier() before or after this
544 * function if you wish to create a persistent key. Call
545 * psa_set_key_lifetime() before or after this function if
546 * you wish to import the key in a secure element.
547 * - The key type and bit-size are determined by the contents
548 * of the PK context. If the PK context contains a key
549 * pair, the key type can be either a key pair type or
550 * the corresponding public key type, depending on
551 * \p usage. If the PK context contains a public key,
552 * the key type is a public key type.
553 * - The key's policy is determined by the key type and
554 * the \p usage parameter. The usage always allows
555 * \p usage, exporting and copying the key, and
556 * possibly other permissions as documented for the
557 * \p usage parameter.
Gilles Peskinee208b252024-02-01 20:42:21 +0100558 * The permitted algorithm policy is determined as follows
Gilles Peskine0b172552024-01-18 14:11:26 +0100559 * based on the #mbedtls_pk_type_t type of \p pk,
560 * the chosen \p usage and other factors:
Gilles Peskinee208b252024-02-01 20:42:21 +0100561 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100562 * #mbedtls_rsa_context has the padding mode
563 * #MBEDTLS_RSA_PKCS_V15:
564 * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH)
565 * if \p usage is SIGN/VERIFY, and
566 * #PSA_ALG_RSA_PKCS1V15_CRYPT
567 * if \p usage is ENCRYPT/DECRYPT.
Gilles Peskinee208b252024-02-01 20:42:21 +0100568 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100569 * #mbedtls_rsa_context has the padding mode
570 * #MBEDTLS_RSA_PKCS_V21 and the digest type
571 * corresponding to the PSA algorithm \c hash:
572 * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH)
573 * if \p usage is SIGN/VERIFY, and
574 * #PSA_ALG_RSA_OAEP(\c hash)
575 * if \p usage is ENCRYPT/DECRYPT.
576 * - #MBEDTLS_PK_RSA_ALT: not supported.
577 * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY
578 * if \p usage is SIGN/VERIFY:
579 * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH)
580 * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled,
581 * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH).
582 * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY
583 * if \p usage is DERIVE:
584 * #PSA_ALG_ECDH.
Gilles Peskinee208b252024-02-01 20:42:21 +0100585 * - #MBEDTLS_PK_OPAQUE: same as the primary algorithm
Gilles Peskine0b172552024-01-18 14:11:26 +0100586 * set for the underlying PSA key, except that
587 * sign/decrypt flags are removed if the type is
588 * set to a public key type.
Gilles Peskine793920c2024-02-01 21:26:54 +0100589 * The underlying key must allow \p usage.
Gilles Peskine0b172552024-01-18 14:11:26 +0100590 * Note that the enrollment algorithm set with
591 * psa_set_key_enrollment_algorithm() is not copied.
592 *
593 * \return 0 on success.
594 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
595 * a key of the type identified in \p attributes.
596 * Another error code on other failures.
597 */
598int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
599 psa_key_usage_t usage,
600 psa_key_attributes_t *attributes);
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100601
602/**
603 * \brief Import a key into the PSA key store.
604 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100605 * This function is equivalent to calling psa_import_key()
606 * with the key material from \p pk.
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100607 *
Gilles Peskineddbe4ae2024-03-04 18:30:09 +0100608 * The typical way to use this function is:
609 * -# Call mbedtls_pk_get_psa_attributes() to obtain
610 * attributes for the given key.
611 * -# If desired, modify the attributes, for example:
612 * - To create a persistent key, call
613 * psa_set_key_identifier() and optionally
614 * psa_set_key_lifetime().
615 * - To import only the public part of a key pair:
616 *
617 * psa_set_key_type(&attributes,
618 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
619 * psa_get_key_type(&attributes)));
620 * - Restrict the key usage if desired.
621 * -# Call mbedtls_pk_import_into_psa().
Gilles Peskine05ee3fb2024-02-07 18:58:10 +0100622 *
623 * \note This function does not support RSA-alt contexts
624 * (set up with mbedtls_pk_setup_rsa_alt()).
625 *
626 * \param[in] pk The PK context to use. It must have been set up.
627 * It can either contain a key pair or just a public key.
628 * \param[in] attributes
629 * The attributes to use for the new key. They must be
630 * compatible with \p pk. In particular, the key type
631 * must match the content of \p pk.
632 * If \p pk contains a key pair, the key type in
633 * attributes can be either the key pair type or the
634 * corresponding public key type (to import only the
635 * public part).
636 * \param[out] key_id
637 * On success, the identifier of the newly created key.
638 * On error, this is #MBEDTLS_SVC_KEY_ID_INIT.
639 *
640 * \return 0 on success.
641 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
642 * a key of the type identified in \p attributes.
643 * Another error code on other failures.
644 */
645int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
646 const psa_key_attributes_t *attributes,
647 mbedtls_svc_key_id_t *key_id);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100648
649/**
650 * \brief Create a PK context starting from a key stored in PSA.
651 * This key:
652 * - must be exportable and
653 * - must be an RSA or EC key pair or public key (FFDH is not supported in PK).
654 *
655 * The resulting PK object will be a transparent type:
656 * - #MBEDTLS_PK_RSA for RSA keys or
657 * - #MBEDTLS_PK_ECKEY for EC keys.
658 *
659 * Once this functions returns the PK object will be completely
660 * independent from the original PSA key that it was generated
661 * from.
662 * Calling mbedtls_pk_sign(), mbedtls_pk_verify(),
663 * mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting
664 * PK context will perform the corresponding algorithm for that
665 * PK context type.
666 * * For ECDSA, the choice of deterministic vs randomized will
667 * be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC.
668 * * For an RSA key, the output PK context will allow both
669 * encrypt/decrypt and sign/verify regardless of the original
670 * key's policy.
671 * The original key's policy determines the output key's padding
672 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
673 * otherwise PKCS1 v1.5 is set.
674 *
675 * \param key_id The key identifier of the key stored in PSA.
676 * \param pk The PK context that will be filled. It must be initialized,
677 * but not set up.
678 *
679 * \return 0 on success.
680 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
681 * parameters are not correct.
682 */
683int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
684
685/**
686 * \brief Create a PK context for the public key of a PSA key.
687 *
688 * The key must be an RSA or ECC key. It can be either a
689 * public key or a key pair, and only the public key is copied.
690 * The resulting PK object will be a transparent type:
691 * - #MBEDTLS_PK_RSA for RSA keys or
692 * - #MBEDTLS_PK_ECKEY for EC keys.
693 *
694 * Once this functions returns the PK object will be completely
695 * independent from the original PSA key that it was generated
696 * from.
697 * Calling mbedtls_pk_verify() or
698 * mbedtls_pk_encrypt() on the resulting
699 * PK context will perform the corresponding algorithm for that
700 * PK context type.
701 *
702 * For an RSA key, the output PK context will allow both
703 * encrypt and verify regardless of the original key's policy.
704 * The original key's policy determines the output key's padding
705 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
706 * otherwise PKCS1 v1.5 is set.
707 *
708 * \param key_id The key identifier of the key stored in PSA.
709 * \param pk The PK context that will be filled. It must be initialized,
710 * but not set up.
711 *
712 * \return 0 on success.
713 * \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
714 * parameters are not correct.
715 */
716int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
717#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine0b172552024-01-18 14:11:26 +0100718
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200719/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200720 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200721 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200723 * \param md_alg Hash algorithm used.
724 * This can be #MBEDTLS_MD_NONE if the signature algorithm
725 * does not rely on a hash algorithm (non-deterministic
726 * ECDSA, RSA PKCS#1 v1.5).
727 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
728 * \p hash is the DigestInfo structure used by RFC 8017
729 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
730 * algorithm then \p hash is the digest itself, and this
731 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200732 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200733 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200734 * \param sig Signature to verify
735 * \param sig_len Signature length
736 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100737 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
738 * either PKCS#1 v1.5 or PSS (accepting any salt length),
739 * depending on the padding mode in the underlying RSA context.
740 * For a pk object constructed by parsing, this is PKCS#1 v1.5
741 * by default. Use mbedtls_pk_verify_ext() to explicitly select
742 * a different algorithm.
743 *
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200744 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200745 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500746 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200747 * or a specific error code.
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
750 const unsigned char *hash, size_t hash_len,
751 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200752
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200753/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200754 * \brief Restartable version of \c mbedtls_pk_verify()
755 *
756 * \note Performs the same job as \c mbedtls_pk_verify(), but can
757 * return early and restart according to the limit set with
758 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
759 * operations. For RSA, same as \c mbedtls_pk_verify().
760 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500761 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200762 * \param md_alg Hash algorithm used (see notes)
763 * \param hash Hash of the message to sign
764 * \param hash_len Hash length or 0 (see notes)
765 * \param sig Signature to verify
766 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200767 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200768 *
769 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200770 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200771 * operations was reached: see \c mbedtls_ecp_set_max_ops().
772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
774 mbedtls_md_type_t md_alg,
775 const unsigned char *hash, size_t hash_len,
776 const unsigned char *sig, size_t sig_len,
777 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200778
779/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200780 * \brief Verify signature, with options.
781 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200782 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200783 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200784 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500785 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200786 * \param md_alg Hash algorithm used (see notes)
787 * \param hash Hash of the message to sign
788 * \param hash_len Hash length or 0 (see notes)
789 * \param sig Signature to verify
790 * \param sig_len Signature length
791 *
792 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200793 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200794 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200795 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500796 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200797 * or a specific error code.
798 *
799 * \note If hash_len is 0, then the length associated with md_alg
800 * is used instead, or an error returned if it is invalid.
801 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200803 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
805 * to a mbedtls_pk_rsassa_pss_options structure,
Janos Follath056cb142024-11-19 16:17:36 +0000806 * otherwise it must be NULL. Note that the salt length is not
807 * verified as contexes have PSA_ALG_RSA_PSS_ANY_SALT as default
808 * and that is the only valid value.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200809 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
811 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
812 const unsigned char *hash, size_t hash_len,
813 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200814
815/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200816 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200817 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500818 * \param ctx The PK context to use. It must have been set up
819 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200820 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200821 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200822 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100823 * \param sig Place to write the signature.
824 * It must have enough room for the signature.
825 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
826 * You may use a smaller buffer if it is large enough
827 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200828 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100829 * \param sig_len On successful return,
830 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200831 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200832 * \param p_rng RNG parameter
833 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100834 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
835 * either PKCS#1 v1.5 or PSS (using the largest possible salt
836 * length up to the hash length), depending on the padding mode
837 * in the underlying RSA context. For a pk object constructed
838 * by parsing, this is PKCS#1 v1.5 by default. Use
839 * mbedtls_pk_verify_ext() to explicitly select a different
840 * algorithm.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200841 *
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200842 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200843 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
845 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
848 const unsigned char *hash, size_t hash_len,
849 unsigned char *sig, size_t sig_size, size_t *sig_len,
850 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200851
852/**
Jerry Yu406cf272022-03-22 11:33:42 +0800853 * \brief Make signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800854 *
Jerry Yu8beb9e12022-03-12 16:23:53 +0800855 * \param pk_type Signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800856 * \param ctx The PK context to use. It must have been set up
857 * with a private key.
858 * \param md_alg Hash algorithm used (see notes)
859 * \param hash Hash of the message to sign
860 * \param hash_len Hash length
861 * \param sig Place to write the signature.
862 * It must have enough room for the signature.
863 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
864 * You may use a smaller buffer if it is large enough
865 * given the key type.
866 * \param sig_size The size of the \p sig buffer in bytes.
867 * \param sig_len On successful return,
868 * the number of bytes written to \p sig.
869 * \param f_rng RNG function, must not be \c NULL.
870 * \param p_rng RNG parameter
871 *
872 * \return 0 on success, or a specific error code.
873 *
Jerry Yue6e73d62022-03-24 13:05:08 +0800874 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
875 * see #PSA_ALG_RSA_PSS for a description of PSS options used.
Jerry Yud69439a2022-02-24 15:52:15 +0800876 *
877 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
878 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
879 *
Jerry Yud69439a2022-02-24 15:52:15 +0800880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
882 mbedtls_pk_context *ctx,
883 mbedtls_md_type_t md_alg,
884 const unsigned char *hash, size_t hash_len,
885 unsigned char *sig, size_t sig_size, size_t *sig_len,
886 int (*f_rng)(void *, unsigned char *, size_t),
887 void *p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800888
889/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200890 * \brief Restartable version of \c mbedtls_pk_sign()
891 *
892 * \note Performs the same job as \c mbedtls_pk_sign(), but can
893 * return early and restart according to the limit set with
894 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
895 * operations. For RSA, same as \c mbedtls_pk_sign().
896 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500897 * \param ctx The PK context to use. It must have been set up
898 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100899 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200900 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200901 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100902 * \param sig Place to write the signature.
903 * It must have enough room for the signature.
904 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
905 * You may use a smaller buffer if it is large enough
906 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200907 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100908 * \param sig_len On successful return,
909 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200910 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200911 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200912 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200913 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100914 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200915 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200916 * operations was reached: see \c mbedtls_ecp_set_max_ops().
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
919 mbedtls_md_type_t md_alg,
920 const unsigned char *hash, size_t hash_len,
921 unsigned char *sig, size_t sig_size, size_t *sig_len,
922 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
923 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200924
925/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200926 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200927 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500928 * \param ctx The PK context to use. It must have been set up
929 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200930 * \param input Input to decrypt
931 * \param ilen Input size
932 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200933 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200934 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200935 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200936 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200937 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100938 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
939 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
940 * the underlying RSA context. For a pk object constructed by
941 * parsing, this is PKCS#1 v1.5 by default.
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200942 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200943 * \return 0 on success, or a specific error code.
944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
946 const unsigned char *input, size_t ilen,
947 unsigned char *output, size_t *olen, size_t osize,
948 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200949
950/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200951 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200952 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500953 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200954 * \param input Message to encrypt
955 * \param ilen Message size
956 * \param output Encrypted output
957 * \param olen Encrypted output length
958 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200959 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200960 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200961 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100962 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
963 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
964 * the underlying RSA context. For a pk object constructed by
965 * parsing, this is PKCS#1 v1.5 by default.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200966 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200967 * \note \p f_rng is used for padding generation.
968 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200969 * \return 0 on success, or a specific error code.
970 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
972 const unsigned char *input, size_t ilen,
973 unsigned char *output, size_t *olen, size_t osize,
974 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200975
976/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100977 * \brief Check if a public-private pair of keys matches.
978 *
979 * \param pub Context holding a public key.
980 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200981 * \param f_rng RNG function, must not be \c NULL.
982 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100983 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200984 * \return \c 0 on success (keys were checked and match each other).
985 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
986 * be checked - in that case they may or may not match.
987 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
988 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
991 const mbedtls_pk_context *prv,
992 int (*f_rng)(void *, unsigned char *, size_t),
993 void *p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100994
995/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200996 * \brief Export debug information
997 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500998 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200999 * \param items Place to write debug items
1000 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001004
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001005/**
1006 * \brief Access the type name
1007 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001008 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001009 *
1010 * \return Type name on success, or "invalid PK"
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001013
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001014/**
Paul Bakker4fc090a2013-09-18 15:43:25 +02001015 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001016 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001017 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001018 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001019 * \return Type on success.
1020 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001023
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001024#if defined(MBEDTLS_RSA_C)
1025/**
1026 * Quick access to an RSA context inside a PK context.
1027 *
1028 * \warning This function can only be used when the type of the context, as
1029 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
1030 * Ensuring that is the caller's responsibility.
1031 * Alternatively, you can check whether this function returns NULL.
1032 *
1033 * \return The internal RSA context held by the PK context, or NULL.
1034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001036{
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001038 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001040 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001042 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001043}
1044#endif /* MBEDTLS_RSA_C */
1045
Valerio Setti229bf102023-05-15 11:13:55 +02001046#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001047/**
1048 * Quick access to an EC context inside a PK context.
1049 *
1050 * \warning This function can only be used when the type of the context, as
1051 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
1052 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
1053 * Ensuring that is the caller's responsibility.
1054 * Alternatively, you can check whether this function returns NULL.
1055 *
1056 * \return The internal EC context held by the PK context, or NULL.
1057 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001059{
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001061 case MBEDTLS_PK_ECKEY:
1062 case MBEDTLS_PK_ECKEY_DH:
1063 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001065 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +02001067 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001068}
Valerio Setti229bf102023-05-15 11:13:55 +02001069#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +02001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001072/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001074 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 *
Janos Follath056cb142024-11-19 16:17:36 +00001076 * \note The PSA crypto subsystem must have been initialized by
1077 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001078 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001079 * \param ctx The PK context to fill. It must have been initialized
1080 * but not set up.
1081 * \param key Input buffer to parse.
1082 * The buffer must contain the input exactly, with no
1083 * extra trailing material. For PEM, the buffer must
1084 * contain a null-terminated string.
1085 * \param keylen Size of \b key in bytes.
1086 * For PEM data, this includes the terminating null byte,
1087 * so \p keylen must be equal to `strlen(key) + 1`.
1088 * \param pwd Optional password for decryption.
1089 * Pass \c NULL if expecting a non-encrypted key.
1090 * Pass a string of \p pwdlen bytes if expecting an encrypted
1091 * key; a non-encrypted key will also be accepted.
1092 * The empty password is not supported.
1093 * \param pwdlen Size of the password in bytes.
1094 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001095 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1096 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001098 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1100 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001101 *
1102 * \note The key is also checked for correctness.
1103 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 * \return 0 if successful, or a specific PK or PEM error code
1105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
1107 const unsigned char *key, size_t keylen,
1108 const unsigned char *pwd, size_t pwdlen,
1109 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110
Paul Bakkerff3a5182013-09-16 22:42:19 +02001111/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001113 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 *
Janos Follath056cb142024-11-19 16:17:36 +00001115 * \note The PSA crypto subsystem must have been initialized by
1116 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001117 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001118 * \param ctx The PK context to fill. It must have been initialized
1119 * but not set up.
1120 * \param key Input buffer to parse.
1121 * The buffer must contain the input exactly, with no
1122 * extra trailing material. For PEM, the buffer must
1123 * contain a null-terminated string.
1124 * \param keylen Size of \b key in bytes.
1125 * For PEM data, this includes the terminating null byte,
1126 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001128 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1130 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001131 *
Valerio Setti78f79d32023-02-07 08:33:26 +01001132 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
1133 * limitations.
1134 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001135 * \note The key is also checked for correctness.
1136 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 * \return 0 if successful, or a specific PK or PEM error code
1138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1140 const unsigned char *key, size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001143/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001145 * \brief Load and parse a private key
1146 *
Janos Follath056cb142024-11-19 16:17:36 +00001147 * \note The PSA crypto subsystem must have been initialized by
1148 * calling psa_crypto_init() before calling this function.
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001149 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001150 * \param ctx The PK context to fill. It must have been initialized
1151 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001152 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001153 * \param password Optional password to decrypt the file.
1154 * Pass \c NULL if expecting a non-encrypted key.
1155 * Pass a null-terminated string if expecting an encrypted
1156 * key; a non-encrypted key will also be accepted.
1157 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001158 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1159 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001160 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001161 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1163 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001164 *
1165 * \note The key is also checked for correctness.
1166 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001167 * \return 0 if successful, or a specific PK or PEM error code
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1170 const char *path, const char *password,
1171 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001172
Paul Bakkerff3a5182013-09-16 22:42:19 +02001173/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001174/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 * \brief Load and parse a public key
1176 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001177 * \param ctx The PK context to fill. It must have been initialized
1178 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +01001179 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001181 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +01001182 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
1183 * you need a specific key type, check the result with
1184 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001185 *
1186 * \note The key is also checked for correctness.
1187 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 * \return 0 if successful, or a specific PK or PEM error code
1189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_FS_IO */
1192#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001196 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
1197 * Note: data is written at the end of the buffer! Use the
1198 * return value to determine where you should start
1199 * using the buffer
1200 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001201 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001202 * \param buf buffer to write to
1203 * \param size size of the buffer
1204 *
1205 * \return length of data written if successful, or a specific
1206 * error code
1207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001209
1210/**
1211 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
1212 * Note: data is written at the end of the buffer! Use the
1213 * return value to determine where you should start
1214 * using the buffer
1215 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001216 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001217 * \param buf buffer to write to
1218 * \param size size of the buffer
1219 *
1220 * \return length of data written if successful, or a specific
1221 * error code
1222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001226/**
1227 * \brief Write a public key to a PEM string
1228 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001229 * \param ctx PK context which must contain a valid public or private key.
1230 * \param buf Buffer to write to. The output includes a
1231 * terminating null byte.
1232 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001233 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001234 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001236int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001237
1238/**
1239 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
1240 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001241 * \param ctx PK context which must contain a valid private key.
1242 * \param buf Buffer to write to. The output includes a
1243 * terminating null byte.
1244 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001245 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001246 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248int 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 +02001249#endif /* MBEDTLS_PEM_WRITE_C */
1250#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001251
1252/*
1253 * WARNING: Low-level functions. You probably do not want to use these unless
1254 * you are certain you do ;)
1255 */
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001258/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 * \brief Parse a SubjectPublicKeyInfo DER structure
1260 *
1261 * \param p the position in the ASN.1 data
1262 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001263 * \param pk The PK context to fill. It must have been initialized
1264 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 *
1266 * \return 0 if successful, or a specific PK error code
1267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
1269 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001273/**
1274 * \brief Write a subjectPublicKey to ASN.1 data
1275 * Note: function works backwards in data buffer
1276 *
1277 * \param p reference to current position pointer
1278 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001279 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001280 *
1281 * \return the length written or a negative error code
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
1284 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001286
Paul Bakkered27a042013-04-18 22:46:23 +02001287#ifdef __cplusplus
1288}
1289#endif
1290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#endif /* MBEDTLS_PK_H */