blob: a43b94955c2de87da76b2bb8712c16ef7a3e29b3 [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
184/* Helper symbol to state that the PK module has support for EC keys. This
185 * can either be provided through the legacy ECP solution or through the
186 * PSA friendly MBEDTLS_PK_USE_PSA_EC_DATA. */
187#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_ECP_C)
188#define MBEDTLS_PK_HAVE_ECC_KEYS
189#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_ECP_C */
190
Valerio Setticf084ae2023-01-26 14:30:12 +0100191/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200192 * \brief Types for interfacing with the debug module
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 MBEDTLS_PK_DEBUG_NONE = 0,
196 MBEDTLS_PK_DEBUG_MPI,
197 MBEDTLS_PK_DEBUG_ECP,
Valerio Setti92c3f362023-05-17 15:36:44 +0200198 MBEDTLS_PK_DEBUG_PSA_EC,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200200
201/**
202 * \brief Item to send to the debug module
203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204typedef struct mbedtls_pk_debug_item {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200205 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
206 const char *MBEDTLS_PRIVATE(name);
207 void *MBEDTLS_PRIVATE(value);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200209
210/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200212
213/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200214 * \brief Public key information and operations
Gilles Peskine2e9d65f2021-08-31 23:05:19 +0200215 *
216 * \note The library does not support custom pk info structures,
217 * only built-in structures returned by
218 * mbedtls_cipher_info_from_type().
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200221
Valerio Setti722f8f72023-05-17 15:31:21 +0200222#define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
223 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200224/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200225 * \brief Public key container
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227typedef struct mbedtls_pk_context {
228 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
229 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200230 /* The following field is used to store the ID of a private key in the
231 * following cases:
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200232 * - opaque key when MBEDTLS_USE_PSA_CRYPTO is defined
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200233 * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case:
234 * - the pk_ctx above is not not used to store the private key anymore.
235 * Actually that field not populated at all in this case because also
236 * the public key will be stored in raw format as explained below
237 * - this ID is used for all private key operations (ex: sign, check
238 * key pair, key write, etc) using PSA functions
239 *
240 * Note: this private key storing solution only affects EC keys, not the
241 * other ones. The latters still use the pk_ctx to store their own
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200242 * context. */
243#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +0200244 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200245#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +0200246 /* The following fields are meant for storing the public key in raw format
247 * which is handy for:
248 * - easily importing it into the PSA context
249 * - reducing the ECP module dependencies in the PK one.
250 *
251 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled:
252 * - the pk_ctx above is not used anymore for storing the public key
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200253 * inside the ecp_keypair structure
Valerio Setti722f8f72023-05-17 15:31:21 +0200254 * - the following fields are used for all public key operations: signature
255 * verify, key pair check and key write.
256 * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy
Valerio Setti016264b2023-05-22 18:40:35 +0200257 * ecp_keypair structure is used for storing the public key and performing
Valerio Setti722f8f72023-05-17 15:31:21 +0200258 * all the operations.
259 *
260 * Note: This new public key storing solution only works for EC keys, not
Valerio Settif57007d2023-05-19 13:54:39 +0200261 * other ones. The latters still use pk_ctx to store their own
Valerio Setti722f8f72023-05-17 15:31:21 +0200262 * context.
263 */
264#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
265 uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */
266 size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */
267 psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */
268 size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */
Valerio Settic1541cb2023-05-17 15:49:55 +0200269#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200271
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200272#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200273/**
274 * \brief Context for resuming operations
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276typedef struct {
277 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
278 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200279} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200280#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200281/* Now we can declare functions that take a pointer to that */
282typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200283#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200286/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200287 * \brief Types for RSA-alt abstraction
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen,
290 const unsigned char *input, unsigned char *output,
291 size_t output_max_len);
292typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng,
295 mbedtls_md_type_t md_alg, unsigned int hashlen,
296 const unsigned char *hash, unsigned char *sig);
297typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200299
300/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200301 * \brief Return information associated with the given PK type
302 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200303 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200304 *
305 * \return The PK info associated with the type or NULL if not found.
306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200308
309/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310 * \brief Initialize a #mbedtls_pk_context (as NONE).
311 *
312 * \param ctx The context to initialize.
313 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200316
317/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500318 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100319 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 * \param ctx The context to clear. It must have been initialized.
321 * If this is \c NULL, this function does nothing.
322 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100323 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100324 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200325 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100326 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200327 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200329
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200330#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200331/**
332 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 *
334 * \param ctx The context to initialize.
335 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200338
339/**
340 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 *
342 * \param ctx The context to clear. It must have been initialized.
343 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200346#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200347
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200348/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200349 * \brief Initialize a PK context with the information given
350 * and allocates the type-specific PK subcontext.
351 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352 * \param ctx Context to initialize. It must not have been set
353 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200354 * \param info Information to use
355 *
356 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200358 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200359 *
Paul Bakker42099c32014-01-27 11:45:49 +0100360 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200361 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200364
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200365#if defined(MBEDTLS_USE_PSA_CRYPTO)
366/**
Gilles Peskine11392492019-05-27 14:53:19 +0200367 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200368 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100369 * \note This function replaces mbedtls_pk_setup() for contexts
Gilles Peskine11392492019-05-27 14:53:19 +0200370 * that wrap a (possibly opaque) PSA key instead of
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100371 * storing and manipulating the key material directly.
372 *
373 * \param ctx The context to initialize. It must be empty (type NONE).
Neil Armstrongb3547422022-03-22 10:22:28 +0100374 * \param key The PSA key to wrap, which must hold an ECC or RSA key
375 * pair (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200376 *
Gilles Peskine11392492019-05-27 14:53:19 +0200377 * \note The wrapped key must remain valid as long as the
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100378 * wrapping PK context is in use, that is at least between
379 * the point this function is called and the point
380 * mbedtls_pk_free() is called on this context. The wrapped
Gilles Peskine11392492019-05-27 14:53:19 +0200381 * key might then be independently used or destroyed.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100382 *
Neil Armstrongb3547422022-03-22 10:22:28 +0100383 * \note This function is currently only available for ECC or RSA
384 * key pairs (that is, keys containing private key material).
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100385 * Support for other key types may be added later.
386 *
387 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100388 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200389 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100390 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100391 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100392 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
395 const mbedtls_svc_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200396#endif /* MBEDTLS_USE_PSA_CRYPTO */
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200399/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200400 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200401 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500402 * \param ctx Context to initialize. It must not have been set
403 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200404 * \param key RSA key pointer
405 * \param decrypt_func Decryption function
406 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200407 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200408 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200410 * context wasn't already initialized as RSA_ALT.
411 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200412 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200413 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
415 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
416 mbedtls_pk_rsa_alt_sign_func sign_func,
417 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200419
420/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200421 * \brief Get the size in bits of the underlying key
422 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200424 *
425 * \return Key size in bits, or 0 on error
426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200428
429/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200430 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500431 *
432 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200433 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200434 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200437{
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200439}
440
441/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200442 * \brief Tell if a context can do the operation given by type
443 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444 * \param ctx The context to query. It must have been initialized.
445 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200446 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500447 * \return 1 if the context can do operations on the given type.
448 * \return 0 if the context cannot do the operations on the given
449 * type. This is always the case for a context that has
450 * been initialized but not set up, or that has been
451 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200454
Neil Armstrong0b529582022-05-11 10:10:20 +0200455#if defined(MBEDTLS_USE_PSA_CRYPTO)
456/**
Neil Armstrongcec133a2022-05-17 11:56:01 +0200457 * \brief Tell if context can do the operation given by PSA algorithm
Neil Armstrong0b529582022-05-11 10:10:20 +0200458 *
459 * \param ctx The context to query. It must have been initialized.
460 * \param alg PSA algorithm to check against, the following are allowed:
461 * PSA_ALG_RSA_PKCS1V15_SIGN(hash),
462 * PSA_ALG_RSA_PSS(hash),
463 * PSA_ALG_RSA_PKCS1V15_CRYPT,
464 * PSA_ALG_ECDSA(hash),
465 * PSA_ALG_ECDH, where hash is a specific hash.
Neil Armstrong408f6a62022-05-17 14:23:20 +0200466 * \param usage PSA usage flag to check against, must be composed of:
467 * PSA_KEY_USAGE_SIGN_HASH
468 * PSA_KEY_USAGE_DECRYPT
469 * PSA_KEY_USAGE_DERIVE.
470 * Context key must match all passed usage flags.
Neil Armstrong0b529582022-05-11 10:10:20 +0200471 *
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200472 * \warning Since the set of allowed algorithms and usage flags may be
473 * expanded in the future, the return value \c 0 should not
474 * be taken in account for non-allowed algorithms and usage
475 * flags.
476 *
Neil Armstrong0b529582022-05-11 10:10:20 +0200477 * \return 1 if the context can do operations on the given type.
478 * \return 0 if the context cannot do the operations on the given
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200479 * type, for non-allowed algorithms and usage flags, or
480 * for a context that has been initialized but not set up
481 * or that has been cleared with mbedtls_pk_free().
Neil Armstrong0b529582022-05-11 10:10:20 +0200482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
484 psa_key_usage_t usage);
Neil Armstrong0b529582022-05-11 10:10:20 +0200485#endif /* MBEDTLS_USE_PSA_CRYPTO */
486
Gilles Peskine0b172552024-01-18 14:11:26 +0100487#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
488/**
489 * \brief Determine valid PSA attributes that can be used to
490 * import a key into PSA.
491 *
492 * The attributes determined by this function are suitable
493 * for calling mbedtls_pk_import_into_psa() to create
494 * a PSA key with the same key material.
495 *
496 * The typical flow of operations involving this function is
497 * ```
498 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
499 * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes);
500 * if (ret != 0) ...; // error handling omitted
501 * // Tweak attributes if desired
502 * psa_key_id_t key_id = 0;
503 * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id);
504 * if (ret != 0) ...; // error handling omitted
505 * ```
506 *
507 * \note This function does not support RSA-alt contexts
508 * (set up with mbedtls_pk_setup_rsa_alt()).
509 *
510 * \param[in] pk The PK context to use. It must have been set up.
511 * It can either contain a key pair or just a public key.
512 * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following:
513 * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a
514 * key pair. The output \p attributes will contain a
515 * key pair type, and the usage policy will allow
516 * #PSA_KEY_USAGE_ENCRYPT as well as
517 * #PSA_KEY_USAGE_DECRYPT.
518 * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a
519 * key pair. The output \p attributes will contain a
520 * key pair type.
521 * - #PSA_KEY_USAGE_ENCRYPT: The output
522 * \p attributes will contain a public key type.
523 * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a
524 * key pair. The output \p attributes will contain a
525 * key pair type, and the usage policy will allow
526 * #PSA_KEY_USAGE_VERIFY_HASH as well as
527 * #PSA_KEY_USAGE_SIGN_HASH.
528 * - #PSA_KEY_USAGE_SIGN_MESSAGE: \p pk must contain a
529 * key pair. The output \p attributes will contain a
530 * key pair type, and the usage policy will allow
531 * #PSA_KEY_USAGE_VERIFY_MESSAGE as well as
532 * #PSA_KEY_USAGE_SIGN_MESSAGE.
533 * - #PSA_KEY_USAGE_VERIFY_HASH: The output
534 * \p attributes will contain a public key type.
535 * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output
536 * \p attributes will contain a public key type.
537 * \param[out] attributes
538 * On success, valid attributes to import the key into PSA.
539 * - The lifetime and key identifier are unchanged. If the
540 * attribute structure was initialized or reset before
541 * calling this function, this will result in a volatile
542 * key. Call psa_set_key_identifier() before or after this
543 * function if you wish to create a persistent key. Call
544 * psa_set_key_lifetime() before or after this function if
545 * you wish to import the key in a secure element.
546 * - The key type and bit-size are determined by the contents
547 * of the PK context. If the PK context contains a key
548 * pair, the key type can be either a key pair type or
549 * the corresponding public key type, depending on
550 * \p usage. If the PK context contains a public key,
551 * the key type is a public key type.
552 * - The key's policy is determined by the key type and
553 * the \p usage parameter. The usage always allows
554 * \p usage, exporting and copying the key, and
555 * possibly other permissions as documented for the
556 * \p usage parameter.
557 * The permitted algorithm is determined as follows
558 * based on the #mbedtls_pk_type_t type of \p pk,
559 * the chosen \p usage and other factors:
560 * - #MBEDTLS_PK_RSA with whose underlying
561 * #mbedtls_rsa_context has the padding mode
562 * #MBEDTLS_RSA_PKCS_V15:
563 * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH)
564 * if \p usage is SIGN/VERIFY, and
565 * #PSA_ALG_RSA_PKCS1V15_CRYPT
566 * if \p usage is ENCRYPT/DECRYPT.
567 * - #MBEDTLS_PK_RSA with whose underlying
568 * #mbedtls_rsa_context has the padding mode
569 * #MBEDTLS_RSA_PKCS_V21 and the digest type
570 * corresponding to the PSA algorithm \c hash:
571 * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH)
572 * if \p usage is SIGN/VERIFY, and
573 * #PSA_ALG_RSA_OAEP(\c hash)
574 * if \p usage is ENCRYPT/DECRYPT.
575 * - #MBEDTLS_PK_RSA_ALT: not supported.
576 * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY
577 * if \p usage is SIGN/VERIFY:
578 * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH)
579 * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled,
580 * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH).
581 * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY
582 * if \p usage is DERIVE:
583 * #PSA_ALG_ECDH.
584 * - #MBEDTLS_PK_OPAQUE: same as the algorithm policy
585 * set for the underlying PSA key, except that
586 * sign/decrypt flags are removed if the type is
587 * set to a public key type.
588 * Note that the enrollment algorithm set with
589 * psa_set_key_enrollment_algorithm() is not copied.
590 *
591 * \return 0 on success.
592 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
593 * a key of the type identified in \p attributes.
594 * Another error code on other failures.
595 */
596int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
597 psa_key_usage_t usage,
598 psa_key_attributes_t *attributes);
599#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
600
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200601/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200602 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200603 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500604 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200605 * \param md_alg Hash algorithm used.
606 * This can be #MBEDTLS_MD_NONE if the signature algorithm
607 * does not rely on a hash algorithm (non-deterministic
608 * ECDSA, RSA PKCS#1 v1.5).
609 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
610 * \p hash is the DigestInfo structure used by RFC 8017
611 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
612 * algorithm then \p hash is the digest itself, and this
613 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200614 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200615 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200616 * \param sig Signature to verify
617 * \param sig_len Signature length
618 *
619 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200620 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500621 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200622 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200623 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200624 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200626 * to verify RSASSA_PSS signatures.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
629 const unsigned char *hash, size_t hash_len,
630 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200631
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200632/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200633 * \brief Restartable version of \c mbedtls_pk_verify()
634 *
635 * \note Performs the same job as \c mbedtls_pk_verify(), but can
636 * return early and restart according to the limit set with
637 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
638 * operations. For RSA, same as \c mbedtls_pk_verify().
639 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500640 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200641 * \param md_alg Hash algorithm used (see notes)
642 * \param hash Hash of the message to sign
643 * \param hash_len Hash length or 0 (see notes)
644 * \param sig Signature to verify
645 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200646 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200647 *
648 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200649 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200650 * operations was reached: see \c mbedtls_ecp_set_max_ops().
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
653 mbedtls_md_type_t md_alg,
654 const unsigned char *hash, size_t hash_len,
655 const unsigned char *sig, size_t sig_len,
656 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200657
658/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200659 * \brief Verify signature, with options.
660 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200661 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200662 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200663 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500664 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200665 * \param md_alg Hash algorithm used (see notes)
666 * \param hash Hash of the message to sign
667 * \param hash_len Hash length or 0 (see notes)
668 * \param sig Signature to verify
669 * \param sig_len Signature length
670 *
671 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200672 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200673 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200674 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500675 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200676 * or a specific error code.
677 *
678 * \note If hash_len is 0, then the length associated with md_alg
679 * is used instead, or an error returned if it is invalid.
680 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200682 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
684 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnarda6e02912022-12-21 09:59:33 +0100685 * otherwise it must be NULL. Note that if
686 * #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not
687 * verified as PSA_ALG_RSA_PSS_ANY_SALT is used.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200688 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
690 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
691 const unsigned char *hash, size_t hash_len,
692 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200693
694/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200695 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200696 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500697 * \param ctx The PK context to use. It must have been set up
698 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200699 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200700 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200701 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100702 * \param sig Place to write the signature.
703 * It must have enough room for the signature.
704 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
705 * You may use a smaller buffer if it is large enough
706 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200707 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100708 * \param sig_len On successful return,
709 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200710 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200711 * \param p_rng RNG parameter
712 *
713 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200714 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200715 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
716 * There is no interface in the PK module to make RSASSA-PSS
717 * signatures yet.
718 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
720 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200721 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
723 const unsigned char *hash, size_t hash_len,
724 unsigned char *sig, size_t sig_size, size_t *sig_len,
725 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200726
727/**
Jerry Yu406cf272022-03-22 11:33:42 +0800728 * \brief Make signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800729 *
Jerry Yu8beb9e12022-03-12 16:23:53 +0800730 * \param pk_type Signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800731 * \param ctx The PK context to use. It must have been set up
732 * with a private key.
733 * \param md_alg Hash algorithm used (see notes)
734 * \param hash Hash of the message to sign
735 * \param hash_len Hash length
736 * \param sig Place to write the signature.
737 * It must have enough room for the signature.
738 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
739 * You may use a smaller buffer if it is large enough
740 * given the key type.
741 * \param sig_size The size of the \p sig buffer in bytes.
742 * \param sig_len On successful return,
743 * the number of bytes written to \p sig.
744 * \param f_rng RNG function, must not be \c NULL.
745 * \param p_rng RNG parameter
746 *
747 * \return 0 on success, or a specific error code.
748 *
Jerry Yue6e73d62022-03-24 13:05:08 +0800749 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
750 * see #PSA_ALG_RSA_PSS for a description of PSS options used.
Jerry Yud69439a2022-02-24 15:52:15 +0800751 *
752 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
753 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
754 *
Jerry Yud69439a2022-02-24 15:52:15 +0800755 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
757 mbedtls_pk_context *ctx,
758 mbedtls_md_type_t md_alg,
759 const unsigned char *hash, size_t hash_len,
760 unsigned char *sig, size_t sig_size, size_t *sig_len,
761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800763
764/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200765 * \brief Restartable version of \c mbedtls_pk_sign()
766 *
767 * \note Performs the same job as \c mbedtls_pk_sign(), but can
768 * return early and restart according to the limit set with
769 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
770 * operations. For RSA, same as \c mbedtls_pk_sign().
771 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500772 * \param ctx The PK context to use. It must have been set up
773 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100774 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200775 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200776 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100777 * \param sig Place to write the signature.
778 * It must have enough room for the signature.
779 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
780 * You may use a smaller buffer if it is large enough
781 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200782 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100783 * \param sig_len On successful return,
784 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200785 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200786 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200787 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200788 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100789 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200790 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200791 * operations was reached: see \c mbedtls_ecp_set_max_ops().
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
794 mbedtls_md_type_t md_alg,
795 const unsigned char *hash, size_t hash_len,
796 unsigned char *sig, size_t sig_size, size_t *sig_len,
797 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
798 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200799
800/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200801 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200802 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500803 * \param ctx The PK context to use. It must have been set up
804 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200805 * \param input Input to decrypt
806 * \param ilen Input size
807 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200808 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200809 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200810 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200811 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200812 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200813 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
814 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200815 * \return 0 on success, or a specific error code.
816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
818 const unsigned char *input, size_t ilen,
819 unsigned char *output, size_t *olen, size_t osize,
820 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200821
822/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200823 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200824 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500825 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200826 * \param input Message to encrypt
827 * \param ilen Message size
828 * \param output Encrypted output
829 * \param olen Encrypted output length
830 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200831 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200832 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200833 *
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200834 * \note \p f_rng is used for padding generation.
835 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200836 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
837 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200838 * \return 0 on success, or a specific error code.
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
841 const unsigned char *input, size_t ilen,
842 unsigned char *output, size_t *olen, size_t osize,
843 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200844
845/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100846 * \brief Check if a public-private pair of keys matches.
847 *
848 * \param pub Context holding a public key.
849 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200850 * \param f_rng RNG function, must not be \c NULL.
851 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100852 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200853 * \return \c 0 on success (keys were checked and match each other).
854 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
855 * be checked - in that case they may or may not match.
856 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
857 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
860 const mbedtls_pk_context *prv,
861 int (*f_rng)(void *, unsigned char *, size_t),
862 void *p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100863
864/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200865 * \brief Export debug information
866 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500867 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200868 * \param items Place to write debug items
869 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200871 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200873
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200874/**
875 * \brief Access the type name
876 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500877 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200878 *
879 * \return Type name on success, or "invalid PK"
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200882
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200883/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200884 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200885 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500886 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200887 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500888 * \return Type on success.
889 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200890 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200892
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200893#if defined(MBEDTLS_RSA_C)
894/**
895 * Quick access to an RSA context inside a PK context.
896 *
897 * \warning This function can only be used when the type of the context, as
898 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
899 * Ensuring that is the caller's responsibility.
900 * Alternatively, you can check whether this function returns NULL.
901 *
902 * \return The internal RSA context held by the PK context, or NULL.
903 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200905{
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200907 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200909 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200911 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200912}
913#endif /* MBEDTLS_RSA_C */
914
Valerio Setti229bf102023-05-15 11:13:55 +0200915#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200916/**
917 * Quick access to an EC context inside a PK context.
918 *
919 * \warning This function can only be used when the type of the context, as
920 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
921 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
922 * Ensuring that is the caller's responsibility.
923 * Alternatively, you can check whether this function returns NULL.
924 *
925 * \return The internal EC context held by the PK context, or NULL.
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200928{
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200930 case MBEDTLS_PK_ECKEY:
931 case MBEDTLS_PK_ECKEY_DH:
932 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200934 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200936 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200937}
Valerio Setti229bf102023-05-15 11:13:55 +0200938#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200941/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200943 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200945 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
946 * subsystem must have been initialized by calling
947 * psa_crypto_init() before calling this function.
948 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500949 * \param ctx The PK context to fill. It must have been initialized
950 * but not set up.
951 * \param key Input buffer to parse.
952 * The buffer must contain the input exactly, with no
953 * extra trailing material. For PEM, the buffer must
954 * contain a null-terminated string.
955 * \param keylen Size of \b key in bytes.
956 * For PEM data, this includes the terminating null byte,
957 * so \p keylen must be equal to `strlen(key) + 1`.
958 * \param pwd Optional password for decryption.
959 * Pass \c NULL if expecting a non-encrypted key.
960 * Pass a string of \p pwdlen bytes if expecting an encrypted
961 * key; a non-encrypted key will also be accepted.
962 * The empty password is not supported.
963 * \param pwdlen Size of the password in bytes.
964 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200965 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
966 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100968 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
970 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100971 *
972 * \note The key is also checked for correctness.
973 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974 * \return 0 if successful, or a specific PK or PEM error code
975 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
977 const unsigned char *key, size_t keylen,
978 const unsigned char *pwd, size_t pwdlen,
979 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980
Paul Bakkerff3a5182013-09-16 22:42:19 +0200981/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200983 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200985 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
986 * subsystem must have been initialized by calling
987 * psa_crypto_init() before calling this function.
988 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500989 * \param ctx The PK context to fill. It must have been initialized
990 * but not set up.
991 * \param key Input buffer to parse.
992 * The buffer must contain the input exactly, with no
993 * extra trailing material. For PEM, the buffer must
994 * contain a null-terminated string.
995 * \param keylen Size of \b key in bytes.
996 * For PEM data, this includes the terminating null byte,
997 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100999 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1001 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001002 *
Valerio Setti78f79d32023-02-07 08:33:26 +01001003 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
1004 * limitations.
1005 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001006 * \note The key is also checked for correctness.
1007 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 * \return 0 if successful, or a specific PK or PEM error code
1009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1011 const unsigned char *key, size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001014/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001016 * \brief Load and parse a private key
1017 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001018 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
1019 * subsystem must have been initialized by calling
1020 * psa_crypto_init() before calling this function.
1021 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001022 * \param ctx The PK context to fill. It must have been initialized
1023 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001024 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001025 * \param password Optional password to decrypt the file.
1026 * Pass \c NULL if expecting a non-encrypted key.
1027 * Pass a null-terminated string if expecting an encrypted
1028 * key; a non-encrypted key will also be accepted.
1029 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001030 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1031 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001032 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001033 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1035 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001036 *
1037 * \note The key is also checked for correctness.
1038 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001039 * \return 0 if successful, or a specific PK or PEM error code
1040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001041int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1042 const char *path, const char *password,
1043 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001044
Paul Bakkerff3a5182013-09-16 22:42:19 +02001045/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001046/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 * \brief Load and parse a public key
1048 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001049 * \param ctx The PK context to fill. It must have been initialized
1050 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +01001051 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001053 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +01001054 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
1055 * you need a specific key type, check the result with
1056 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001057 *
1058 * \note The key is also checked for correctness.
1059 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 * \return 0 if successful, or a specific PK or PEM error code
1061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_FS_IO */
1064#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001068 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
1069 * Note: data is written at the end of the buffer! Use the
1070 * return value to determine where you should start
1071 * using the buffer
1072 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001073 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001074 * \param buf buffer to write to
1075 * \param size size of the buffer
1076 *
1077 * \return length of data written if successful, or a specific
1078 * error code
1079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001081
1082/**
1083 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
1084 * Note: data is written at the end of the buffer! Use the
1085 * return value to determine where you should start
1086 * using the buffer
1087 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001088 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001089 * \param buf buffer to write to
1090 * \param size size of the buffer
1091 *
1092 * \return length of data written if successful, or a specific
1093 * error code
1094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001098/**
1099 * \brief Write a public key to a PEM string
1100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001101 * \param ctx PK context which must contain a valid public or private key.
1102 * \param buf Buffer to write to. The output includes a
1103 * terminating null byte.
1104 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001105 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001106 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001109
1110/**
1111 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
1112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001113 * \param ctx PK context which must contain a valid private key.
1114 * \param buf Buffer to write to. The output includes a
1115 * terminating null byte.
1116 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001117 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001118 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120int 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 +02001121#endif /* MBEDTLS_PEM_WRITE_C */
1122#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001123
1124/*
1125 * WARNING: Low-level functions. You probably do not want to use these unless
1126 * you are certain you do ;)
1127 */
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001130/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131 * \brief Parse a SubjectPublicKeyInfo DER structure
1132 *
1133 * \param p the position in the ASN.1 data
1134 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001135 * \param pk The PK context to fill. It must have been initialized
1136 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 *
1138 * \return 0 if successful, or a specific PK error code
1139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
1141 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001145/**
1146 * \brief Write a subjectPublicKey to ASN.1 data
1147 * Note: function works backwards in data buffer
1148 *
1149 * \param p reference to current position pointer
1150 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001151 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001152 *
1153 * \return the length written or a negative error code
1154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
1156 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001158
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +01001159/*
1160 * Internal module functions. You probably do not want to use these unless you
1161 * know you do.
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +01001165#endif
1166
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001167#if defined(MBEDTLS_USE_PSA_CRYPTO)
1168/**
Neil Armstrong23143dc2022-04-21 11:33:54 +02001169 * \brief Turn an EC or RSA key into an opaque one.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001170 *
1171 * \warning This is a temporary utility function for tests. It might
1172 * change or be removed at any time without notice.
1173 *
Neil Armstrong23143dc2022-04-21 11:33:54 +02001174 * \param pk Input: the EC or RSA key to import to a PSA key.
Gilles Peskine11392492019-05-27 14:53:19 +02001175 * Output: a PK context wrapping that PSA key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001176 * \param key Output: a PSA key identifier.
Gilles Peskine11392492019-05-27 14:53:19 +02001177 * It's the caller's responsibility to call
Ronald Croncf56a0a2020-08-04 09:51:30 +02001178 * psa_destroy_key() on that key identifier after calling
Gilles Peskine11392492019-05-27 14:53:19 +02001179 * mbedtls_pk_free() on the PK context.
Neil Armstronga1fc18f2022-04-22 13:57:14 +02001180 * \param alg The algorithm to allow for use with that key.
1181 * \param usage The usage to allow for use with that key.
1182 * \param alg2 The secondary algorithm to allow for use with that key.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001183 *
1184 * \return \c 0 if successful.
1185 * \return An Mbed TLS error code otherwise.
1186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001187int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
1188 mbedtls_svc_key_id_t *key,
1189 psa_algorithm_t alg,
1190 psa_key_usage_t usage,
1191 psa_algorithm_t alg2);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001192#endif /* MBEDTLS_USE_PSA_CRYPTO */
1193
Paul Bakkered27a042013-04-18 22:46:23 +02001194#ifdef __cplusplus
1195}
1196#endif
1197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#endif /* MBEDTLS_PK_H */