blob: a8c0c377e9bf7b68f83d2cb3303b9b5755d0e9d3 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Paul Bakkered27a042013-04-18 22:46:23 +020013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010015#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020018#endif
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020019
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010020#include "mbedtls/md.h"
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_RSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010023#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020024#endif
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_ECP_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028#endif
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_ECDSA_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020032#endif
33
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +020034#if defined(MBEDTLS_USE_PSA_CRYPTO)
35#include "psa/crypto.h"
36#endif
37
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010039 !defined(inline) && !defined(__cplusplus)
40#define inline __inline
41#endif
42
Gilles Peskinea3974432021-07-26 18:48:10 +020043/** Memory allocation failed. */
44#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
45/** Type mismatch, eg attempt to encrypt with an ECDSA key */
46#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
47/** Bad input parameters to function. */
48#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
49/** Read/write of file failed. */
50#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
51/** Unsupported key version */
52#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
53/** Invalid key tag or value. */
54#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
55/** Key algorithm is unsupported (only RSA and EC are supported). */
56#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
57/** Private key password can't be empty. */
58#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
59/** Given private key password does not allow for correct decryption. */
60#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
61/** The pubkey tag or value is invalid (only RSA and EC are supported). */
62#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
63/** The algorithm tag or value is invalid. */
64#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
65/** Elliptic curve is unsupported (only NIST curves are supported). */
66#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
67/** Unavailable feature, e.g. RSA disabled for RSA key. */
68#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
69/** The buffer contains a valid signature followed by more data. */
70#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
Ron Eldor9924bdc2018-10-04 10:59:13 +030071
72/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020073/** PK hardware accelerator failed. */
74#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880
Paul Bakker1a7550a2013-09-15 13:01:22 +020075
Paul Bakkered27a042013-04-18 22:46:23 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
80/**
81 * \brief Public key types
82 */
83typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 MBEDTLS_PK_NONE=0,
85 MBEDTLS_PK_RSA,
86 MBEDTLS_PK_ECKEY,
87 MBEDTLS_PK_ECKEY_DH,
88 MBEDTLS_PK_ECDSA,
89 MBEDTLS_PK_RSA_ALT,
90 MBEDTLS_PK_RSASSA_PSS,
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +010091 MBEDTLS_PK_OPAQUE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020093
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020094/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020095 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020097 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098typedef struct mbedtls_pk_rsassa_pss_options {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200100 int expected_salt_len;
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200103
104/**
Gilles Peskineda252be2019-11-05 16:23:49 +0100105 * \brief Maximum size of a signature made by mbedtls_pk_sign().
106 */
Gilles Peskine54605652019-11-08 16:24:16 +0100107/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
108 * size among the supported signature types. Do it by starting at 0,
109 * then incrementally increasing to be large enough for each supported
110 * signature mechanism.
111 *
112 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
113 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
114 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
115 */
116#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100119 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100120/* For RSA, the signature can be as large as the bignum module allows.
121 * For RSA_ALT, the signature size is not necessarily tied to what the
122 * bignum module can do, but in the absence of any specific setting,
123 * we use that (rsa_alt_sign_wrap in pk_wrap will check). */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100124#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
125#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
126#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100127
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100128#if defined(MBEDTLS_ECDSA_C) && \
129 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100130/* For ECDSA, the ecdsa module exports a constant for the maximum
131 * signature size. */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100132#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
133#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
134#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100135
136#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100137#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
138/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
Gilles Peskine54605652019-11-08 16:24:16 +0100139 * through the PSA API in the PSA representation. */
140#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100142#endif
143
144#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
145/* The Mbed TLS representation is different for ECDSA signatures:
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100146 * PSA uses the raw concatenation of r and s,
147 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
148 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
149 * types, lengths (represented by up to 2 bytes), and potential leading
150 * zeros of the INTEGERs and the SEQUENCE. */
151#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152#define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100153#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100154#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100155
156/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200157 * \brief Types for interfacing with the debug module
158 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_PK_DEBUG_NONE = 0,
161 MBEDTLS_PK_DEBUG_MPI,
162 MBEDTLS_PK_DEBUG_ECP,
163} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200164
165/**
166 * \brief Item to send to the debug module
167 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168typedef struct mbedtls_pk_debug_item {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200170 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200171 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200173
174/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200176
177/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200178 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200181
182/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200183 * \brief Public key container
184 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185typedef struct mbedtls_pk_context {
186 const mbedtls_pk_info_t *pk_info; /**< Public key information */
187 void *pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200189
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200190#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200191/**
192 * \brief Context for resuming operations
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194typedef struct {
195 const mbedtls_pk_info_t *pk_info; /**< Public key information */
196 void *rs_ctx; /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200197} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200198#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200199/* Now we can declare functions that take a pointer to that */
200typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200201#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200204/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200205 * \brief Types for RSA-alt abstraction
206 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen,
208 const unsigned char *input, unsigned char *output,
209 size_t output_max_len);
210typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
211 int (*f_rng)(void *, unsigned char *, size_t),
212 void *p_rng,
213 int mode, mbedtls_md_type_t md_alg,
214 unsigned int hashlen,
215 const unsigned char *hash, unsigned char *sig);
216typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200218
219/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200220 * \brief Return information associated with the given PK type
221 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200222 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200223 *
224 * \return The PK info associated with the type or NULL if not found.
225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200227
228/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 * \brief Initialize a #mbedtls_pk_context (as NONE).
230 *
231 * \param ctx The context to initialize.
232 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200235
236/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500237 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100238 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239 * \param ctx The context to clear. It must have been initialized.
240 * If this is \c NULL, this function does nothing.
241 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100242 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100243 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200244 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100245 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200248
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200249#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200250/**
251 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 *
253 * \param ctx The context to initialize.
254 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200257
258/**
259 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500260 *
261 * \param ctx The context to clear. It must have been initialized.
262 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200263 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200265#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200266
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200267/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200268 * \brief Initialize a PK context with the information given
269 * and allocates the type-specific PK subcontext.
270 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \param ctx Context to initialize. It must not have been set
272 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200273 * \param info Information to use
274 *
275 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200277 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200278 *
Paul Bakker42099c32014-01-27 11:45:49 +0100279 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200280 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200283
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200284#if defined(MBEDTLS_USE_PSA_CRYPTO)
285/**
Gilles Peskine11392492019-05-27 14:53:19 +0200286 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200287 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100288 * \note This function replaces mbedtls_pk_setup() for contexts
Gilles Peskine11392492019-05-27 14:53:19 +0200289 * that wrap a (possibly opaque) PSA key instead of
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100290 * storing and manipulating the key material directly.
291 *
292 * \param ctx The context to initialize. It must be empty (type NONE).
Gilles Peskine11392492019-05-27 14:53:19 +0200293 * \param key The PSA key to wrap, which must hold an ECC key pair
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100294 * (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200295 *
Gilles Peskine11392492019-05-27 14:53:19 +0200296 * \note The wrapped key must remain valid as long as the
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100297 * wrapping PK context is in use, that is at least between
298 * the point this function is called and the point
299 * mbedtls_pk_free() is called on this context. The wrapped
Gilles Peskine11392492019-05-27 14:53:19 +0200300 * key might then be independently used or destroyed.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100301 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100302 * \note This function is currently only available for ECC key
303 * pairs (that is, ECC keys containing private key material).
304 * Support for other key types may be added later.
305 *
306 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100307 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200308 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100309 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100310 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100311 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200312 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
314 const psa_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200315#endif /* MBEDTLS_USE_PSA_CRYPTO */
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200318/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200319 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200320 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 * \param ctx Context to initialize. It must not have been set
322 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200323 * \param key RSA key pointer
324 * \param decrypt_func Decryption function
325 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200326 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200327 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200329 * context wasn't already initialized as RSA_ALT.
330 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200331 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200332 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
334 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
335 mbedtls_pk_rsa_alt_sign_func sign_func,
336 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200338
339/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200340 * \brief Get the size in bits of the underlying key
341 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500342 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200343 *
344 * \return Key size in bits, or 0 on error
345 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200347
348/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200349 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500350 *
351 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200352 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200353 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200354 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200356{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200358}
359
360/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200361 * \brief Tell if a context can do the operation given by type
362 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 * \param ctx The context to query. It must have been initialized.
364 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200365 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500366 * \return 1 if the context can do operations on the given type.
367 * \return 0 if the context cannot do the operations on the given
368 * type. This is always the case for a context that has
369 * been initialized but not set up, or that has been
370 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200373
374/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200375 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200376 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200378 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200379 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200380 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200381 * \param sig Signature to verify
382 * \param sig_len Signature length
383 *
384 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200385 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek96ce1b02023-07-14 05:22:42 -0400386 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200387 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200388 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200389 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200391 * to verify RSASSA_PSS signatures.
392 *
Gilles Peskine07ae2082023-03-07 20:22:51 +0100393 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto
394 * subsystem must have been initialized by calling
395 * psa_crypto_init() before calling this function,
396 * if the key might be an ECC (ECDSA) key.
397 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200398 * \note If hash_len is 0, then the length associated with md_alg
399 * is used instead, or an error returned if it is invalid.
400 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
404 const unsigned char *hash, size_t hash_len,
405 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200406
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200407/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200408 * \brief Restartable version of \c mbedtls_pk_verify()
409 *
410 * \note Performs the same job as \c mbedtls_pk_verify(), but can
411 * return early and restart according to the limit set with
412 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
413 * operations. For RSA, same as \c mbedtls_pk_verify().
414 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200416 * \param md_alg Hash algorithm used (see notes)
417 * \param hash Hash of the message to sign
418 * \param hash_len Hash length or 0 (see notes)
419 * \param sig Signature to verify
420 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200421 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200422 *
423 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200424 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200425 * operations was reached: see \c mbedtls_ecp_set_max_ops().
426 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
428 mbedtls_md_type_t md_alg,
429 const unsigned char *hash, size_t hash_len,
430 const unsigned char *sig, size_t sig_len,
431 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200432
433/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200434 * \brief Verify signature, with options.
435 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200436 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200437 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200438 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500439 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200440 * \param md_alg Hash algorithm used (see notes)
441 * \param hash Hash of the message to sign
442 * \param hash_len Hash length or 0 (see notes)
443 * \param sig Signature to verify
444 * \param sig_len Signature length
445 *
446 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200447 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200448 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200449 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
Andrzej Kurek96ce1b02023-07-14 05:22:42 -0400450 * signature in \p sig but its length is less than \p sig_len,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200451 * or a specific error code.
452 *
453 * \note If hash_len is 0, then the length associated with md_alg
454 * is used instead, or an error returned if it is invalid.
455 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200457 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
459 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200460 * otherwise it must be NULL.
461 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
463 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
464 const unsigned char *hash, size_t hash_len,
465 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200466
467/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200468 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200469 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470 * \param ctx The PK context to use. It must have been set up
471 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200472 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200473 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200474 * \param hash_len Hash length or 0 (see notes)
Gilles Peskineda252be2019-11-05 16:23:49 +0100475 * \param sig Place to write the signature.
476 * It must have enough room for the signature.
477 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
478 * You may use a smaller buffer if it is large enough
479 * given the key type.
480 * \param sig_len On successful return,
481 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200482 * \param f_rng RNG function
483 * \param p_rng RNG parameter
484 *
485 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200486 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200487 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
488 * There is no interface in the PK module to make RSASSA-PSS
489 * signatures yet.
490 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200491 * \note If hash_len is 0, then the length associated with md_alg
492 * is used instead, or an error returned if it is invalid.
493 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
495 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200496 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
498 const unsigned char *hash, size_t hash_len,
499 unsigned char *sig, size_t *sig_len,
500 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200501
502/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200503 * \brief Restartable version of \c mbedtls_pk_sign()
504 *
505 * \note Performs the same job as \c mbedtls_pk_sign(), but can
506 * return early and restart according to the limit set with
507 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
508 * operations. For RSA, same as \c mbedtls_pk_sign().
509 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500510 * \param ctx The PK context to use. It must have been set up
511 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100512 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200513 * \param hash Hash of the message to sign
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100514 * \param hash_len Hash length or 0 (see notes for mbedtls_pk_sign())
515 * \param sig Place to write the signature.
516 * It must have enough room for the signature.
517 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
518 * You may use a smaller buffer if it is large enough
519 * given the key type.
520 * \param sig_len On successful return,
521 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200522 * \param f_rng RNG function
523 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200524 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200525 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100526 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200527 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200528 * operations was reached: see \c mbedtls_ecp_set_max_ops().
529 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
531 mbedtls_md_type_t md_alg,
532 const unsigned char *hash, size_t hash_len,
533 unsigned char *sig, size_t *sig_len,
534 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
535 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200536
537/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200538 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200539 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500540 * \param ctx The PK context to use. It must have been set up
541 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200542 * \param input Input to decrypt
543 * \param ilen Input size
544 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200545 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200546 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200547 * \param f_rng RNG function
548 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200549 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200550 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
551 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200552 * \return 0 on success, or a specific error code.
553 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
555 const unsigned char *input, size_t ilen,
556 unsigned char *output, size_t *olen, size_t osize,
557 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200558
559/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200560 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200561 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500562 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563 * \param input Message to encrypt
564 * \param ilen Message size
565 * \param output Encrypted output
566 * \param olen Encrypted output length
567 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200568 * \param f_rng RNG function
569 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200570 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200571 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
572 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200573 * \return 0 on success, or a specific error code.
574 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
576 const unsigned char *input, size_t ilen,
577 unsigned char *output, size_t *olen, size_t osize,
578 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200579
580/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100581 * \brief Check if a public-private pair of keys matches.
582 *
583 * \param pub Context holding a public key.
584 * \param prv Context holding a private (and public) key.
585 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200586 * \return \c 0 on success (keys were checked and match each other).
587 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
588 * be checked - in that case they may or may not match.
589 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
590 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100591 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100593
594/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200595 * \brief Export debug information
596 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500597 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200598 * \param items Place to write debug items
599 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200601 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200603
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200604/**
605 * \brief Access the type name
606 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500607 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200608 *
609 * \return Type name on success, or "invalid PK"
610 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200612
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200613/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200614 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200615 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500616 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200617 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500618 * \return Type on success.
619 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200620 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100621mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200622
Manuel Pégourié-Gonnarda4a4aab2022-06-10 09:48:38 +0200623#if defined(MBEDTLS_RSA_C)
624/**
625 * Quick access to an RSA context inside a PK context.
626 *
627 * \warning This function can only be used when the type of the context, as
628 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
629 * Ensuring that is the caller's responsibility.
630 * Alternatively, you can check whether this function returns NULL.
631 *
632 * \return The internal RSA context held by the PK context, or NULL.
633 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnarda4a4aab2022-06-10 09:48:38 +0200635{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200637 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 return (mbedtls_rsa_context *) (pk).pk_ctx;
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200639 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return NULL;
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200641 }
Manuel Pégourié-Gonnarda4a4aab2022-06-10 09:48:38 +0200642}
643#endif /* MBEDTLS_RSA_C */
644
645#if defined(MBEDTLS_ECP_C)
646/**
647 * Quick access to an EC context inside a PK context.
648 *
649 * \warning This function can only be used when the type of the context, as
650 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
651 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
652 * Ensuring that is the caller's responsibility.
653 * Alternatively, you can check whether this function returns NULL.
654 *
655 * \return The internal EC context held by the PK context, or NULL.
656 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnarda4a4aab2022-06-10 09:48:38 +0200658{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 switch (mbedtls_pk_get_type(&pk)) {
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200660 case MBEDTLS_PK_ECKEY:
661 case MBEDTLS_PK_ECKEY_DH:
662 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 return (mbedtls_ecp_keypair *) (pk).pk_ctx;
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200664 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 return NULL;
Manuel Pégourié-Gonnarded36d202022-06-23 09:43:39 +0200666 }
Manuel Pégourié-Gonnarda4a4aab2022-06-10 09:48:38 +0200667}
668#endif /* MBEDTLS_ECP_C */
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200671/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200673 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500675 * \param ctx The PK context to fill. It must have been initialized
676 * but not set up.
677 * \param key Input buffer to parse.
678 * The buffer must contain the input exactly, with no
679 * extra trailing material. For PEM, the buffer must
680 * contain a null-terminated string.
681 * \param keylen Size of \b key in bytes.
682 * For PEM data, this includes the terminating null byte,
683 * so \p keylen must be equal to `strlen(key) + 1`.
684 * \param pwd Optional password for decryption.
685 * Pass \c NULL if expecting a non-encrypted key.
686 * Pass a string of \p pwdlen bytes if expecting an encrypted
687 * key; a non-encrypted key will also be accepted.
688 * The empty password is not supported.
689 * \param pwdlen Size of the password in bytes.
690 * Ignored if \p pwd is \c NULL.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100692 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
694 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100695 *
696 * \note The key is also checked for correctness.
697 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698 * \return 0 if successful, or a specific PK or PEM error code
699 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
701 const unsigned char *key, size_t keylen,
702 const unsigned char *pwd, size_t pwdlen);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703
Paul Bakkerff3a5182013-09-16 22:42:19 +0200704/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200705/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200706 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500708 * \param ctx The PK context to fill. It must have been initialized
709 * but not set up.
710 * \param key Input buffer to parse.
711 * The buffer must contain the input exactly, with no
712 * extra trailing material. For PEM, the buffer must
713 * contain a null-terminated string.
714 * \param keylen Size of \b key in bytes.
715 * For PEM data, this includes the terminating null byte,
716 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100718 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
720 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100721 *
722 * \note The key is also checked for correctness.
723 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724 * \return 0 if successful, or a specific PK or PEM error code
725 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
727 const unsigned char *key, size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200730/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200732 * \brief Load and parse a private key
733 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500734 * \param ctx The PK context to fill. It must have been initialized
735 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200736 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500737 * \param password Optional password to decrypt the file.
738 * Pass \c NULL if expecting a non-encrypted key.
739 * Pass a null-terminated string if expecting an encrypted
740 * key; a non-encrypted key will also be accepted.
741 * The empty password is not supported.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200742 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100743 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
745 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100746 *
747 * \note The key is also checked for correctness.
748 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200749 * \return 0 if successful, or a specific PK or PEM error code
750 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
752 const char *path, const char *password);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200753
Paul Bakkerff3a5182013-09-16 22:42:19 +0200754/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200755/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756 * \brief Load and parse a public key
757 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500758 * \param ctx The PK context to fill. It must have been initialized
759 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +0100760 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100762 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100763 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
764 * you need a specific key type, check the result with
765 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100766 *
767 * \note The key is also checked for correctness.
768 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769 * \return 0 if successful, or a specific PK or PEM error code
770 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#endif /* MBEDTLS_FS_IO */
773#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200777 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
778 * Note: data is written at the end of the buffer! Use the
779 * return value to determine where you should start
780 * using the buffer
781 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500782 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200783 * \param buf buffer to write to
784 * \param size size of the buffer
785 *
786 * \return length of data written if successful, or a specific
787 * error code
788 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200790
791/**
792 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
793 * Note: data is written at the end of the buffer! Use the
794 * return value to determine where you should start
795 * using the buffer
796 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500797 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200798 * \param buf buffer to write to
799 * \param size size of the buffer
800 *
801 * \return length of data written if successful, or a specific
802 * error code
803 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200807/**
808 * \brief Write a public key to a PEM string
809 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500810 * \param ctx PK context which must contain a valid public or private key.
811 * \param buf Buffer to write to. The output includes a
812 * terminating null byte.
813 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200814 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200815 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200816 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200818
819/**
820 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
821 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500822 * \param ctx PK context which must contain a valid private key.
823 * \param buf Buffer to write to. The output includes a
824 * terminating null byte.
825 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200826 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200827 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200828 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_PEM_WRITE_C */
831#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200832
833/*
834 * WARNING: Low-level functions. You probably do not want to use these unless
835 * you are certain you do ;)
836 */
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200839/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840 * \brief Parse a SubjectPublicKeyInfo DER structure
841 *
842 * \param p the position in the ASN.1 data
843 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500844 * \param pk The PK context to fill. It must have been initialized
845 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846 *
847 * \return 0 if successful, or a specific PK error code
848 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
850 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200854/**
855 * \brief Write a subjectPublicKey to ASN.1 data
856 * Note: function works backwards in data buffer
857 *
858 * \param p reference to current position pointer
859 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500860 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200861 *
862 * \return the length written or a negative error code
863 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
865 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200867
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100868/*
869 * Internal module functions. You probably do not want to use these unless you
870 * know you do.
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100874#endif
875
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100876#if defined(MBEDTLS_USE_PSA_CRYPTO)
877/**
Gilles Peskine11392492019-05-27 14:53:19 +0200878 * \brief Turn an EC key into an opaque one.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100879 *
880 * \warning This is a temporary utility function for tests. It might
881 * change or be removed at any time without notice.
882 *
883 * \note Only ECDSA keys are supported so far. Signing with the
884 * specified hash is the only allowed use of that key.
885 *
Gilles Peskine11392492019-05-27 14:53:19 +0200886 * \param pk Input: the EC key to import to a PSA key.
887 * Output: a PK context wrapping that PSA key.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200888 * \param key Output: a PSA key identifier.
Gilles Peskine11392492019-05-27 14:53:19 +0200889 * It's the caller's responsibility to call
Ronald Croncf56a0a2020-08-04 09:51:30 +0200890 * psa_destroy_key() on that key identifier after calling
Gilles Peskine11392492019-05-27 14:53:19 +0200891 * mbedtls_pk_free() on the PK context.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100892 * \param hash_alg The hash algorithm to allow for use with that key.
893 *
894 * \return \c 0 if successful.
895 * \return An Mbed TLS error code otherwise.
896 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
898 psa_key_id_t *key,
899 psa_algorithm_t hash_alg);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100900#endif /* MBEDTLS_USE_PSA_CRYPTO */
901
Paul Bakkered27a042013-04-18 22:46:23 +0200902#ifdef __cplusplus
903}
904#endif
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#endif /* MBEDTLS_PK_H */