blob: df11de6acd7f229b25c6a488035b9cfd8832aa8c [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkered27a042013-04-18 22:46:23 +02009 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#ifndef MBEDTLS_PK_H
12#define MBEDTLS_PK_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020013#include "mbedtls/private_access.h"
Paul Bakkered27a042013-04-18 22:46:23 +020014
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020016
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010017#include "mbedtls/md.h"
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_RSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010020#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020021#endif
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if defined(MBEDTLS_ECP_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010024#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025#endif
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_ECDSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010028#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020029#endif
30
Gilles Peskine0b172552024-01-18 14:11:26 +010031#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +020032#include "psa/crypto.h"
33#endif
34
Gilles Peskined2971572021-07-26 18:48:10 +020035/** Memory allocation failed. */
36#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
37/** Type mismatch, eg attempt to encrypt with an ECDSA key */
38#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
39/** Bad input parameters to function. */
40#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
41/** Read/write of file failed. */
42#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
43/** Unsupported key version */
44#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
45/** Invalid key tag or value. */
46#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
47/** Key algorithm is unsupported (only RSA and EC are supported). */
48#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
49/** Private key password can't be empty. */
50#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
51/** Given private key password does not allow for correct decryption. */
52#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
53/** The pubkey tag or value is invalid (only RSA and EC are supported). */
54#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
55/** The algorithm tag or value is invalid. */
56#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
57/** Elliptic curve is unsupported (only NIST curves are supported). */
58#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
59/** Unavailable feature, e.g. RSA disabled for RSA key. */
60#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
61/** The buffer contains a valid signature followed by more data. */
62#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
63/** The output buffer is too small. */
64#define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880
Ron Eldor9924bdc2018-10-04 10:59:13 +030065
Paul Bakkered27a042013-04-18 22:46:23 +020066#ifdef __cplusplus
67extern "C" {
68#endif
69
70/**
71 * \brief Public key types
72 */
73typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 MBEDTLS_PK_NONE=0,
75 MBEDTLS_PK_RSA,
76 MBEDTLS_PK_ECKEY,
77 MBEDTLS_PK_ECKEY_DH,
78 MBEDTLS_PK_ECDSA,
79 MBEDTLS_PK_RSA_ALT,
80 MBEDTLS_PK_RSASSA_PSS,
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +010081 MBEDTLS_PK_OPAQUE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020083
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020084/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020085 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020087 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088typedef struct mbedtls_pk_rsassa_pss_options {
Gilles Peskine34c43a82023-02-02 23:06:37 +010089 /** The digest to use for MGF1 in PSS.
90 *
91 * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled and #MBEDTLS_RSA_C is
92 * disabled, this must be equal to the \c md_alg argument passed
93 * to mbedtls_pk_verify_ext(). In a future version of the library,
94 * this constraint may apply whenever #MBEDTLS_USE_PSA_CRYPTO is
95 * enabled regardless of the status of #MBEDTLS_RSA_C.
96 */
97 mbedtls_md_type_t mgf1_hash_id;
98
99 /** The expected length of the salt, in bytes. This may be
100 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
101 *
102 * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled, only
103 * #MBEDTLS_RSA_SALT_LEN_ANY is valid. Any other value may be
104 * ignored (allowing any salt length).
105 */
106 int expected_salt_len;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200109
110/**
Gilles Peskineda252be2019-11-05 16:23:49 +0100111 * \brief Maximum size of a signature made by mbedtls_pk_sign().
112 */
Gilles Peskine54605652019-11-08 16:24:16 +0100113/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
114 * size among the supported signature types. Do it by starting at 0,
115 * then incrementally increasing to be large enough for each supported
116 * signature mechanism.
117 *
118 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
119 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
120 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
121 */
122#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100125 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100126/* For RSA, the signature can be as large as the bignum module allows.
127 * For RSA_ALT, the signature size is not necessarily tied to what the
128 * bignum module can do, but in the absence of any specific setting,
Chris Jones3848e312021-03-11 16:17:59 +0000129 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100130#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
131#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
132#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100133
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100134#if defined(MBEDTLS_ECDSA_C) && \
135 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100136/* For ECDSA, the ecdsa module exports a constant for the maximum
137 * signature size. */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100138#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
139#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
140#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100141
142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100143#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
144/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
Gilles Peskine54605652019-11-08 16:24:16 +0100145 * through the PSA API in the PSA representation. */
146#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100147#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100148#endif
149
150#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
151/* The Mbed TLS representation is different for ECDSA signatures:
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100152 * PSA uses the raw concatenation of r and s,
153 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
154 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
155 * types, lengths (represented by up to 2 bytes), and potential leading
156 * zeros of the INTEGERs and the SEQUENCE. */
157#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine449bd832023-01-11 14:50:10 +0100158#define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100159#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100160#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100161
Valerio Settia9aab1a2023-06-19 13:39:54 +0200162/* Internal helper to define which fields in the pk_context structure below
163 * should be used for EC keys: legacy ecp_keypair or the raw (PSA friendly)
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200164 * format. It should be noted that this only affects how data is stored, not
Valerio Settia9aab1a2023-06-19 13:39:54 +0200165 * which functions are used for various operations. The overall picture looks
166 * like this:
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200167 * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data
168 * structure and legacy functions
Valerio Settia9aab1a2023-06-19 13:39:54 +0200169 * - if USE_PSA is defined and
170 * - if ECP_C then use ecp_keypair structure, convert data to a PSA friendly
171 * format and use PSA functions
172 * - if !ECP_C then use new raw data and PSA functions directly.
173 *
174 * The main reason for the "intermediate" (USE_PSA + ECP_C) above is that as long
175 * as ECP_C is defined mbedtls_pk_ec() gives the user a read/write access to the
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200176 * ecp_keypair structure inside the pk_context so they can modify it using
Valerio Settia9aab1a2023-06-19 13:39:54 +0200177 * ECP functions which are not under PK module's control.
178 */
179#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \
180 !defined(MBEDTLS_ECP_C)
181#define MBEDTLS_PK_USE_PSA_EC_DATA
Manuel Pégourié-Gonnardf7298cd2023-09-18 09:55:24 +0200182#endif
Valerio Settia9aab1a2023-06-19 13:39:54 +0200183
Valerio Setticf084ae2023-01-26 14:30:12 +0100184/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200185 * \brief Types for interfacing with the debug module
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 MBEDTLS_PK_DEBUG_NONE = 0,
189 MBEDTLS_PK_DEBUG_MPI,
190 MBEDTLS_PK_DEBUG_ECP,
Valerio Setti92c3f362023-05-17 15:36:44 +0200191 MBEDTLS_PK_DEBUG_PSA_EC,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200193
194/**
195 * \brief Item to send to the debug module
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197typedef struct mbedtls_pk_debug_item {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200198 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
199 const char *MBEDTLS_PRIVATE(name);
200 void *MBEDTLS_PRIVATE(value);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200202
203/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200205
206/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200207 * \brief Public key information and operations
Gilles Peskine2e9d65f2021-08-31 23:05:19 +0200208 *
209 * \note The library does not support custom pk info structures,
210 * only built-in structures returned by
211 * mbedtls_cipher_info_from_type().
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200214
Valerio Setti722f8f72023-05-17 15:31:21 +0200215#define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
216 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200217/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200218 * \brief Public key container
219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220typedef struct mbedtls_pk_context {
221 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
222 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200223 /* The following field is used to store the ID of a private key in the
224 * following cases:
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200225 * - opaque key when MBEDTLS_USE_PSA_CRYPTO is defined
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200226 * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case:
227 * - the pk_ctx above is not not used to store the private key anymore.
228 * Actually that field not populated at all in this case because also
229 * the public key will be stored in raw format as explained below
230 * - this ID is used for all private key operations (ex: sign, check
231 * key pair, key write, etc) using PSA functions
232 *
233 * Note: this private key storing solution only affects EC keys, not the
234 * other ones. The latters still use the pk_ctx to store their own
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200235 * context. */
236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +0200237 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */
Tomi Fontanillesbad170e2023-12-14 22:03:12 +0200238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +0200239 /* The following fields are meant for storing the public key in raw format
240 * which is handy for:
241 * - easily importing it into the PSA context
242 * - reducing the ECP module dependencies in the PK one.
243 *
244 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled:
245 * - the pk_ctx above is not used anymore for storing the public key
Valerio Setti7ef8a8d2023-05-23 18:39:54 +0200246 * inside the ecp_keypair structure
Valerio Setti722f8f72023-05-17 15:31:21 +0200247 * - the following fields are used for all public key operations: signature
248 * verify, key pair check and key write.
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100249 * - For a key pair, priv_id contains the private key. For a public key,
250 * priv_id is null.
Valerio Setti722f8f72023-05-17 15:31:21 +0200251 * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy
Valerio Setti016264b2023-05-22 18:40:35 +0200252 * ecp_keypair structure is used for storing the public key and performing
Valerio Setti722f8f72023-05-17 15:31:21 +0200253 * all the operations.
254 *
255 * Note: This new public key storing solution only works for EC keys, not
Valerio Settif57007d2023-05-19 13:54:39 +0200256 * other ones. The latters still use pk_ctx to store their own
Valerio Setti722f8f72023-05-17 15:31:21 +0200257 * context.
258 */
259#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
260 uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */
261 size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */
262 psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */
263 size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */
Valerio Settic1541cb2023-05-17 15:49:55 +0200264#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200266
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200267#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200268/**
269 * \brief Context for resuming operations
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271typedef struct {
272 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */
273 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200274} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200275#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200276/* Now we can declare functions that take a pointer to that */
277typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200278#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200281/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200282 * \brief Types for RSA-alt abstraction
283 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen,
285 const unsigned char *input, unsigned char *output,
286 size_t output_max_len);
287typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
288 int (*f_rng)(void *, unsigned char *, size_t),
289 void *p_rng,
290 mbedtls_md_type_t md_alg, unsigned int hashlen,
291 const unsigned char *hash, unsigned char *sig);
292typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200294
295/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200296 * \brief Return information associated with the given PK type
297 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200298 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200299 *
300 * \return The PK info associated with the type or NULL if not found.
301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200303
304/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 * \brief Initialize a #mbedtls_pk_context (as NONE).
306 *
307 * \param ctx The context to initialize.
308 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200311
312/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500313 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100314 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 * \param ctx The context to clear. It must have been initialized.
316 * If this is \c NULL, this function does nothing.
317 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100318 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100319 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200320 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100321 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200324
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200325#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200326/**
327 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 *
329 * \param ctx The context to initialize.
330 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200333
334/**
335 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500336 *
337 * \param ctx The context to clear. It must have been initialized.
338 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200341#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200342
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200343/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200344 * \brief Initialize a PK context with the information given
345 * and allocates the type-specific PK subcontext.
346 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500347 * \param ctx Context to initialize. It must not have been set
348 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200349 * \param info Information to use
350 *
351 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200353 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200354 *
Paul Bakker42099c32014-01-27 11:45:49 +0100355 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200356 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200359
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200360#if defined(MBEDTLS_USE_PSA_CRYPTO)
361/**
Gilles Peskine11392492019-05-27 14:53:19 +0200362 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200363 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100364 * \note This function replaces mbedtls_pk_setup() for contexts
Gilles Peskine11392492019-05-27 14:53:19 +0200365 * that wrap a (possibly opaque) PSA key instead of
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100366 * storing and manipulating the key material directly.
367 *
368 * \param ctx The context to initialize. It must be empty (type NONE).
Neil Armstrongb3547422022-03-22 10:22:28 +0100369 * \param key The PSA key to wrap, which must hold an ECC or RSA key
370 * pair (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200371 *
Gilles Peskine11392492019-05-27 14:53:19 +0200372 * \note The wrapped key must remain valid as long as the
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100373 * wrapping PK context is in use, that is at least between
374 * the point this function is called and the point
375 * mbedtls_pk_free() is called on this context. The wrapped
Gilles Peskine11392492019-05-27 14:53:19 +0200376 * key might then be independently used or destroyed.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100377 *
Neil Armstrongb3547422022-03-22 10:22:28 +0100378 * \note This function is currently only available for ECC or RSA
379 * key pairs (that is, keys containing private key material).
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100380 * Support for other key types may be added later.
381 *
382 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100383 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200384 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100385 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100386 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100387 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200388 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
390 const mbedtls_svc_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200391#endif /* MBEDTLS_USE_PSA_CRYPTO */
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200394/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200395 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200396 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500397 * \param ctx Context to initialize. It must not have been set
398 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200399 * \param key RSA key pointer
400 * \param decrypt_func Decryption function
401 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200402 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200403 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200405 * context wasn't already initialized as RSA_ALT.
406 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200407 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
410 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
411 mbedtls_pk_rsa_alt_sign_func sign_func,
412 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200414
415/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200416 * \brief Get the size in bits of the underlying key
417 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500418 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200419 *
420 * \return Key size in bits, or 0 on error
421 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200423
424/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200425 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 *
427 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200428 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200429 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200432{
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200434}
435
436/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200437 * \brief Tell if a context can do the operation given by type
438 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500439 * \param ctx The context to query. It must have been initialized.
440 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200441 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500442 * \return 1 if the context can do operations on the given type.
443 * \return 0 if the context cannot do the operations on the given
444 * type. This is always the case for a context that has
445 * been initialized but not set up, or that has been
446 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200449
Neil Armstrong0b529582022-05-11 10:10:20 +0200450#if defined(MBEDTLS_USE_PSA_CRYPTO)
451/**
Neil Armstrongcec133a2022-05-17 11:56:01 +0200452 * \brief Tell if context can do the operation given by PSA algorithm
Neil Armstrong0b529582022-05-11 10:10:20 +0200453 *
454 * \param ctx The context to query. It must have been initialized.
455 * \param alg PSA algorithm to check against, the following are allowed:
456 * PSA_ALG_RSA_PKCS1V15_SIGN(hash),
457 * PSA_ALG_RSA_PSS(hash),
458 * PSA_ALG_RSA_PKCS1V15_CRYPT,
459 * PSA_ALG_ECDSA(hash),
460 * PSA_ALG_ECDH, where hash is a specific hash.
Neil Armstrong408f6a62022-05-17 14:23:20 +0200461 * \param usage PSA usage flag to check against, must be composed of:
462 * PSA_KEY_USAGE_SIGN_HASH
463 * PSA_KEY_USAGE_DECRYPT
464 * PSA_KEY_USAGE_DERIVE.
465 * Context key must match all passed usage flags.
Neil Armstrong0b529582022-05-11 10:10:20 +0200466 *
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200467 * \warning Since the set of allowed algorithms and usage flags may be
468 * expanded in the future, the return value \c 0 should not
469 * be taken in account for non-allowed algorithms and usage
470 * flags.
471 *
Neil Armstrong0b529582022-05-11 10:10:20 +0200472 * \return 1 if the context can do operations on the given type.
473 * \return 0 if the context cannot do the operations on the given
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200474 * type, for non-allowed algorithms and usage flags, or
475 * for a context that has been initialized but not set up
476 * or that has been cleared with mbedtls_pk_free().
Neil Armstrong0b529582022-05-11 10:10:20 +0200477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
479 psa_key_usage_t usage);
Neil Armstrong0b529582022-05-11 10:10:20 +0200480#endif /* MBEDTLS_USE_PSA_CRYPTO */
481
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100482#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine0b172552024-01-18 14:11:26 +0100483/**
484 * \brief Determine valid PSA attributes that can be used to
485 * import a key into PSA.
486 *
487 * The attributes determined by this function are suitable
488 * for calling mbedtls_pk_import_into_psa() to create
489 * a PSA key with the same key material.
490 *
491 * The typical flow of operations involving this function is
492 * ```
493 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
494 * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes);
495 * if (ret != 0) ...; // error handling omitted
496 * // Tweak attributes if desired
497 * psa_key_id_t key_id = 0;
498 * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id);
499 * if (ret != 0) ...; // error handling omitted
500 * ```
501 *
502 * \note This function does not support RSA-alt contexts
503 * (set up with mbedtls_pk_setup_rsa_alt()).
504 *
505 * \param[in] pk The PK context to use. It must have been set up.
506 * It can either contain a key pair or just a public key.
507 * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following:
508 * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a
509 * key pair. The output \p attributes will contain a
510 * key pair type, and the usage policy will allow
511 * #PSA_KEY_USAGE_ENCRYPT as well as
512 * #PSA_KEY_USAGE_DECRYPT.
513 * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a
514 * key pair. The output \p attributes will contain a
515 * key pair type.
516 * - #PSA_KEY_USAGE_ENCRYPT: The output
517 * \p attributes will contain a public key type.
518 * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a
519 * key pair. The output \p attributes will contain a
520 * key pair type, and the usage policy will allow
521 * #PSA_KEY_USAGE_VERIFY_HASH as well as
522 * #PSA_KEY_USAGE_SIGN_HASH.
523 * - #PSA_KEY_USAGE_SIGN_MESSAGE: \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_MESSAGE as well as
527 * #PSA_KEY_USAGE_SIGN_MESSAGE.
528 * - #PSA_KEY_USAGE_VERIFY_HASH: The output
529 * \p attributes will contain a public key type.
530 * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output
531 * \p attributes will contain a public key type.
532 * \param[out] attributes
533 * On success, valid attributes to import the key into PSA.
534 * - The lifetime and key identifier are unchanged. If the
535 * attribute structure was initialized or reset before
536 * calling this function, this will result in a volatile
537 * key. Call psa_set_key_identifier() before or after this
538 * function if you wish to create a persistent key. Call
539 * psa_set_key_lifetime() before or after this function if
540 * you wish to import the key in a secure element.
541 * - The key type and bit-size are determined by the contents
542 * of the PK context. If the PK context contains a key
543 * pair, the key type can be either a key pair type or
544 * the corresponding public key type, depending on
545 * \p usage. If the PK context contains a public key,
546 * the key type is a public key type.
547 * - The key's policy is determined by the key type and
548 * the \p usage parameter. The usage always allows
549 * \p usage, exporting and copying the key, and
550 * possibly other permissions as documented for the
551 * \p usage parameter.
Gilles Peskinee208b252024-02-01 20:42:21 +0100552 * The permitted algorithm policy is determined as follows
Gilles Peskine0b172552024-01-18 14:11:26 +0100553 * based on the #mbedtls_pk_type_t type of \p pk,
554 * the chosen \p usage and other factors:
Gilles Peskinee208b252024-02-01 20:42:21 +0100555 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100556 * #mbedtls_rsa_context has the padding mode
557 * #MBEDTLS_RSA_PKCS_V15:
558 * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH)
559 * if \p usage is SIGN/VERIFY, and
560 * #PSA_ALG_RSA_PKCS1V15_CRYPT
561 * if \p usage is ENCRYPT/DECRYPT.
Gilles Peskinee208b252024-02-01 20:42:21 +0100562 * - #MBEDTLS_PK_RSA whose underlying
Gilles Peskine0b172552024-01-18 14:11:26 +0100563 * #mbedtls_rsa_context has the padding mode
564 * #MBEDTLS_RSA_PKCS_V21 and the digest type
565 * corresponding to the PSA algorithm \c hash:
566 * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH)
567 * if \p usage is SIGN/VERIFY, and
568 * #PSA_ALG_RSA_OAEP(\c hash)
569 * if \p usage is ENCRYPT/DECRYPT.
570 * - #MBEDTLS_PK_RSA_ALT: not supported.
571 * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY
572 * if \p usage is SIGN/VERIFY:
573 * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH)
574 * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled,
575 * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH).
576 * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY
577 * if \p usage is DERIVE:
578 * #PSA_ALG_ECDH.
Gilles Peskinee208b252024-02-01 20:42:21 +0100579 * - #MBEDTLS_PK_OPAQUE: same as the primary algorithm
Gilles Peskine0b172552024-01-18 14:11:26 +0100580 * set for the underlying PSA key, except that
581 * sign/decrypt flags are removed if the type is
582 * set to a public key type.
Gilles Peskine793920c2024-02-01 21:26:54 +0100583 * The underlying key must allow \p usage.
Gilles Peskine0b172552024-01-18 14:11:26 +0100584 * Note that the enrollment algorithm set with
585 * psa_set_key_enrollment_algorithm() is not copied.
586 *
587 * \return 0 on success.
588 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain
589 * a key of the type identified in \p attributes.
590 * Another error code on other failures.
591 */
592int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
593 psa_key_usage_t usage,
594 psa_key_attributes_t *attributes);
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100595#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskine0b172552024-01-18 14:11:26 +0100596
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200597/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200598 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200599 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500600 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200601 * \param md_alg Hash algorithm used.
602 * This can be #MBEDTLS_MD_NONE if the signature algorithm
603 * does not rely on a hash algorithm (non-deterministic
604 * ECDSA, RSA PKCS#1 v1.5).
605 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
606 * \p hash is the DigestInfo structure used by RFC 8017
607 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
608 * algorithm then \p hash is the digest itself, and this
609 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200610 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200611 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200612 * \param sig Signature to verify
613 * \param sig_len Signature length
614 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100615 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
616 * either PKCS#1 v1.5 or PSS (accepting any salt length),
617 * depending on the padding mode in the underlying RSA context.
618 * For a pk object constructed by parsing, this is PKCS#1 v1.5
619 * by default. Use mbedtls_pk_verify_ext() to explicitly select
620 * a different algorithm.
621 *
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200622 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200623 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500624 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200625 * or a specific error code.
626 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
628 const unsigned char *hash, size_t hash_len,
629 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200630
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200631/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200632 * \brief Restartable version of \c mbedtls_pk_verify()
633 *
634 * \note Performs the same job as \c mbedtls_pk_verify(), but can
635 * return early and restart according to the limit set with
636 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
637 * operations. For RSA, same as \c mbedtls_pk_verify().
638 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500639 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200640 * \param md_alg Hash algorithm used (see notes)
641 * \param hash Hash of the message to sign
642 * \param hash_len Hash length or 0 (see notes)
643 * \param sig Signature to verify
644 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200645 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200646 *
647 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200648 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200649 * operations was reached: see \c mbedtls_ecp_set_max_ops().
650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
652 mbedtls_md_type_t md_alg,
653 const unsigned char *hash, size_t hash_len,
654 const unsigned char *sig, size_t sig_len,
655 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200656
657/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200658 * \brief Verify signature, with options.
659 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200660 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200661 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200662 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500663 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200664 * \param md_alg Hash algorithm used (see notes)
665 * \param hash Hash of the message to sign
666 * \param hash_len Hash length or 0 (see notes)
667 * \param sig Signature to verify
668 * \param sig_len Signature length
669 *
670 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200671 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200672 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200673 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek3bedb5b2022-02-17 14:39:00 -0500674 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200675 * or a specific error code.
676 *
677 * \note If hash_len is 0, then the length associated with md_alg
678 * is used instead, or an error returned if it is invalid.
679 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200681 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
683 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnarda6e02912022-12-21 09:59:33 +0100684 * otherwise it must be NULL. Note that if
685 * #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not
686 * verified as PSA_ALG_RSA_PSS_ANY_SALT is used.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200687 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
689 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
690 const unsigned char *hash, size_t hash_len,
691 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200692
693/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200694 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200695 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500696 * \param ctx The PK context to use. It must have been set up
697 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200698 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200699 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200700 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100701 * \param sig Place to write the signature.
702 * It must have enough room for the signature.
703 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
704 * You may use a smaller buffer if it is large enough
705 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200706 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100707 * \param sig_len On successful return,
708 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200709 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200710 * \param p_rng RNG parameter
711 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100712 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
713 * either PKCS#1 v1.5 or PSS (using the largest possible salt
714 * length up to the hash length), depending on the padding mode
715 * in the underlying RSA context. For a pk object constructed
716 * by parsing, this is PKCS#1 v1.5 by default. Use
717 * mbedtls_pk_verify_ext() to explicitly select a different
718 * algorithm.
719 *
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200720 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200721 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
723 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200724 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
726 const unsigned char *hash, size_t hash_len,
727 unsigned char *sig, size_t sig_size, size_t *sig_len,
728 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200729
730/**
Jerry Yu406cf272022-03-22 11:33:42 +0800731 * \brief Make signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800732 *
Jerry Yu8beb9e12022-03-12 16:23:53 +0800733 * \param pk_type Signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800734 * \param ctx The PK context to use. It must have been set up
735 * with a private key.
736 * \param md_alg Hash algorithm used (see notes)
737 * \param hash Hash of the message to sign
738 * \param hash_len Hash length
739 * \param sig Place to write the signature.
740 * It must have enough room for the signature.
741 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
742 * You may use a smaller buffer if it is large enough
743 * given the key type.
744 * \param sig_size The size of the \p sig buffer in bytes.
745 * \param sig_len On successful return,
746 * the number of bytes written to \p sig.
747 * \param f_rng RNG function, must not be \c NULL.
748 * \param p_rng RNG parameter
749 *
750 * \return 0 on success, or a specific error code.
751 *
Jerry Yue6e73d62022-03-24 13:05:08 +0800752 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
753 * see #PSA_ALG_RSA_PSS for a description of PSS options used.
Jerry Yud69439a2022-02-24 15:52:15 +0800754 *
755 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
756 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
757 *
Jerry Yud69439a2022-02-24 15:52:15 +0800758 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
760 mbedtls_pk_context *ctx,
761 mbedtls_md_type_t md_alg,
762 const unsigned char *hash, size_t hash_len,
763 unsigned char *sig, size_t sig_size, size_t *sig_len,
764 int (*f_rng)(void *, unsigned char *, size_t),
765 void *p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800766
767/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200768 * \brief Restartable version of \c mbedtls_pk_sign()
769 *
770 * \note Performs the same job as \c mbedtls_pk_sign(), but can
771 * return early and restart according to the limit set with
772 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
773 * operations. For RSA, same as \c mbedtls_pk_sign().
774 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500775 * \param ctx The PK context to use. It must have been set up
776 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100777 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200778 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200779 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100780 * \param sig Place to write the signature.
781 * It must have enough room for the signature.
782 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
783 * You may use a smaller buffer if it is large enough
784 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200785 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100786 * \param sig_len On successful return,
787 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200788 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200789 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200790 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200791 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100792 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200793 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200794 * operations was reached: see \c mbedtls_ecp_set_max_ops().
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
797 mbedtls_md_type_t md_alg,
798 const unsigned char *hash, size_t hash_len,
799 unsigned char *sig, size_t sig_size, size_t *sig_len,
800 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
801 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200802
803/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200804 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200805 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500806 * \param ctx The PK context to use. It must have been set up
807 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200808 * \param input Input to decrypt
809 * \param ilen Input size
810 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200811 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200812 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200813 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200814 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200815 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100816 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
817 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
818 * the underlying RSA context. For a pk object constructed by
819 * parsing, this is PKCS#1 v1.5 by default.
820 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200821 * \return 0 on success, or a specific error code.
822 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
824 const unsigned char *input, size_t ilen,
825 unsigned char *output, size_t *olen, size_t osize,
826 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200827
828/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200829 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200830 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500831 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200832 * \param input Message to encrypt
833 * \param ilen Message size
834 * \param output Encrypted output
835 * \param olen Encrypted output length
836 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200837 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200838 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200839 *
Valerio Setti85e568c2024-02-19 15:45:00 +0100840 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is
841 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in
842 * the underlying RSA context. For a pk object constructed by
843 * parsing, this is PKCS#1 v1.5 by default.
844 *
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200845 * \note \p f_rng is used for padding generation.
846 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200847 * \return 0 on success, or a specific error code.
848 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
850 const unsigned char *input, size_t ilen,
851 unsigned char *output, size_t *olen, size_t osize,
852 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200853
854/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100855 * \brief Check if a public-private pair of keys matches.
856 *
857 * \param pub Context holding a public key.
858 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200859 * \param f_rng RNG function, must not be \c NULL.
860 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100861 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200862 * \return \c 0 on success (keys were checked and match each other).
863 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
864 * be checked - in that case they may or may not match.
865 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
866 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100867 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100868int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
869 const mbedtls_pk_context *prv,
870 int (*f_rng)(void *, unsigned char *, size_t),
871 void *p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100872
873/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200874 * \brief Export debug information
875 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500876 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200877 * \param items Place to write debug items
878 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200882
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200883/**
884 * \brief Access the type name
885 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500886 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200887 *
888 * \return Type name on success, or "invalid PK"
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200891
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200892/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200893 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200894 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500895 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200896 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500897 * \return Type on success.
898 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200901
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200902#if defined(MBEDTLS_RSA_C)
903/**
904 * Quick access to an RSA context inside a PK context.
905 *
906 * \warning This function can only be used when the type of the context, as
907 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
908 * Ensuring that is the caller's responsibility.
909 * Alternatively, you can check whether this function returns NULL.
910 *
911 * \return The internal RSA context held by the PK context, or NULL.
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200914{
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200916 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200918 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200920 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200921}
922#endif /* MBEDTLS_RSA_C */
923
Valerio Setti229bf102023-05-15 11:13:55 +0200924#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200925/**
926 * Quick access to an EC context inside a PK context.
927 *
928 * \warning This function can only be used when the type of the context, as
929 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
930 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
931 * Ensuring that is the caller's responsibility.
932 * Alternatively, you can check whether this function returns NULL.
933 *
934 * \return The internal EC context held by the PK context, or NULL.
935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200937{
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200939 case MBEDTLS_PK_ECKEY:
940 case MBEDTLS_PK_ECKEY_DH:
941 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx);
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200943 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 return NULL;
Manuel Pégourié-Gonnard4cfaae52022-06-23 09:43:39 +0200945 }
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200946}
Valerio Setti229bf102023-05-15 11:13:55 +0200947#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200950/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200951/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200952 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200953 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200954 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
955 * subsystem must have been initialized by calling
956 * psa_crypto_init() before calling this function.
957 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500958 * \param ctx The PK context to fill. It must have been initialized
959 * but not set up.
960 * \param key Input buffer to parse.
961 * The buffer must contain the input exactly, with no
962 * extra trailing material. For PEM, the buffer must
963 * contain a null-terminated string.
964 * \param keylen Size of \b key in bytes.
965 * For PEM data, this includes the terminating null byte,
966 * so \p keylen must be equal to `strlen(key) + 1`.
967 * \param pwd Optional password for decryption.
968 * Pass \c NULL if expecting a non-encrypted key.
969 * Pass a string of \p pwdlen bytes if expecting an encrypted
970 * key; a non-encrypted key will also be accepted.
971 * The empty password is not supported.
972 * \param pwdlen Size of the password in bytes.
973 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200974 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
975 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100977 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
979 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100980 *
981 * \note The key is also checked for correctness.
982 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983 * \return 0 if successful, or a specific PK or PEM error code
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
986 const unsigned char *key, size_t keylen,
987 const unsigned char *pwd, size_t pwdlen,
988 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200989
Paul Bakkerff3a5182013-09-16 22:42:19 +0200990/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200992 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +0200994 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
995 * subsystem must have been initialized by calling
996 * psa_crypto_init() before calling this function.
997 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500998 * \param ctx The PK context to fill. It must have been initialized
999 * but not set up.
1000 * \param key Input buffer to parse.
1001 * The buffer must contain the input exactly, with no
1002 * extra trailing material. For PEM, the buffer must
1003 * contain a null-terminated string.
1004 * \param keylen Size of \b key in bytes.
1005 * For PEM data, this includes the terminating null byte,
1006 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001008 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1010 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001011 *
Valerio Setti78f79d32023-02-07 08:33:26 +01001012 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
1013 * limitations.
1014 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001015 * \note The key is also checked for correctness.
1016 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 * \return 0 if successful, or a specific PK or PEM error code
1018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
1020 const unsigned char *key, size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +02001023/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001025 * \brief Load and parse a private key
1026 *
Gilles Peskine5b7e1642022-08-04 23:44:59 +02001027 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
1028 * subsystem must have been initialized by calling
1029 * psa_crypto_init() before calling this function.
1030 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001031 * \param ctx The PK context to fill. It must have been initialized
1032 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001033 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001034 * \param password Optional password to decrypt the file.
1035 * Pass \c NULL if expecting a non-encrypted key.
1036 * Pass a null-terminated string if expecting an encrypted
1037 * key; a non-encrypted key will also be accepted.
1038 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001039 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
1040 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001041 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001042 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
1044 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001045 *
1046 * \note The key is also checked for correctness.
1047 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001048 * \return 0 if successful, or a specific PK or PEM error code
1049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001050int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
1051 const char *path, const char *password,
1052 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001053
Paul Bakkerff3a5182013-09-16 22:42:19 +02001054/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001055/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 * \brief Load and parse a public key
1057 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001058 * \param ctx The PK context to fill. It must have been initialized
1059 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +01001060 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001062 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +01001063 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
1064 * you need a specific key type, check the result with
1065 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +01001066 *
1067 * \note The key is also checked for correctness.
1068 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069 * \return 0 if successful, or a specific PK or PEM error code
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_FS_IO */
1073#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001077 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
1078 * Note: data is written at the end of the buffer! Use the
1079 * return value to determine where you should start
1080 * using the buffer
1081 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001082 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001083 * \param buf buffer to write to
1084 * \param size size of the buffer
1085 *
1086 * \return length of data written if successful, or a specific
1087 * error code
1088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001090
1091/**
1092 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
1093 * Note: data is written at the end of the buffer! Use the
1094 * return value to determine where you should start
1095 * using the buffer
1096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001097 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001098 * \param buf buffer to write to
1099 * \param size size of the buffer
1100 *
1101 * \return length of data written if successful, or a specific
1102 * error code
1103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001107/**
1108 * \brief Write a public key to a PEM string
1109 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001110 * \param ctx PK context which must contain a valid public or private key.
1111 * \param buf Buffer to write to. The output includes a
1112 * terminating null byte.
1113 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001114 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001115 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001118
1119/**
1120 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
1121 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001122 * \param ctx PK context which must contain a valid private key.
1123 * \param buf Buffer to write to. The output includes a
1124 * terminating null byte.
1125 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001126 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +02001127 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129int 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 +02001130#endif /* MBEDTLS_PEM_WRITE_C */
1131#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001132
1133/*
1134 * WARNING: Low-level functions. You probably do not want to use these unless
1135 * you are certain you do ;)
1136 */
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001139/**
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 * \brief Parse a SubjectPublicKeyInfo DER structure
1141 *
1142 * \param p the position in the ASN.1 data
1143 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001144 * \param pk The PK context to fill. It must have been initialized
1145 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146 *
1147 * \return 0 if successful, or a specific PK error code
1148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
1150 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001154/**
1155 * \brief Write a subjectPublicKey to ASN.1 data
1156 * Note: function works backwards in data buffer
1157 *
1158 * \param p reference to current position pointer
1159 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001160 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001161 *
1162 * \return the length written or a negative error code
1163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
1165 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001167
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
1169/**
Neil Armstrong23143dc2022-04-21 11:33:54 +02001170 * \brief Turn an EC or RSA key into an opaque one.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001171 *
1172 * \warning This is a temporary utility function for tests. It might
1173 * change or be removed at any time without notice.
1174 *
Neil Armstrong23143dc2022-04-21 11:33:54 +02001175 * \param pk Input: the EC or RSA key to import to a PSA key.
Gilles Peskine11392492019-05-27 14:53:19 +02001176 * Output: a PK context wrapping that PSA key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001177 * \param key Output: a PSA key identifier.
Gilles Peskine11392492019-05-27 14:53:19 +02001178 * It's the caller's responsibility to call
Ronald Croncf56a0a2020-08-04 09:51:30 +02001179 * psa_destroy_key() on that key identifier after calling
Gilles Peskine11392492019-05-27 14:53:19 +02001180 * mbedtls_pk_free() on the PK context.
Neil Armstronga1fc18f2022-04-22 13:57:14 +02001181 * \param alg The algorithm to allow for use with that key.
1182 * \param usage The usage to allow for use with that key.
1183 * \param alg2 The secondary algorithm to allow for use with that key.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001184 *
1185 * \return \c 0 if successful.
1186 * \return An Mbed TLS error code otherwise.
1187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001188int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
1189 mbedtls_svc_key_id_t *key,
1190 psa_algorithm_t alg,
1191 psa_key_usage_t usage,
1192 psa_algorithm_t alg2);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001193#endif /* MBEDTLS_USE_PSA_CRYPTO */
1194
Paul Bakkered27a042013-04-18 22:46:23 +02001195#ifdef __cplusplus
1196}
1197#endif
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_PK_H */