blob: a78ba23a7059fd7ae8439ed5f0c034a777f5457f [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkered27a042013-04-18 22:46:23 +020021 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_PK_H
24#define MBEDTLS_PK_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020025#include "mbedtls/private_access.h"
Paul Bakkered27a042013-04-18 22:46:23 +020026
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020028
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/md.h"
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_RSA_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036# include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020041#endif
42
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +020043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# include "psa/crypto.h"
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +020045#endif
46
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && !defined(inline) && \
48 !defined(__cplusplus)
49# define inline __inline
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010050#endif
51
Gilles Peskined2971572021-07-26 18:48:10 +020052/** Memory allocation failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
Gilles Peskined2971572021-07-26 18:48:10 +020054/** Type mismatch, eg attempt to encrypt with an ECDSA key */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
Gilles Peskined2971572021-07-26 18:48:10 +020056/** Bad input parameters to function. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
Gilles Peskined2971572021-07-26 18:48:10 +020058/** Read/write of file failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
Gilles Peskined2971572021-07-26 18:48:10 +020060/** Unsupported key version */
61#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
62/** Invalid key tag or value. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
Gilles Peskined2971572021-07-26 18:48:10 +020064/** Key algorithm is unsupported (only RSA and EC are supported). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
Gilles Peskined2971572021-07-26 18:48:10 +020066/** Private key password can't be empty. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
Gilles Peskined2971572021-07-26 18:48:10 +020068/** Given private key password does not allow for correct decryption. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
Gilles Peskined2971572021-07-26 18:48:10 +020070/** The pubkey tag or value is invalid (only RSA and EC are supported). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
Gilles Peskined2971572021-07-26 18:48:10 +020072/** The algorithm tag or value is invalid. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
Gilles Peskined2971572021-07-26 18:48:10 +020074/** Elliptic curve is unsupported (only NIST curves are supported). */
75#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
76/** Unavailable feature, e.g. RSA disabled for RSA key. */
77#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
78/** The buffer contains a valid signature followed by more data. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
Gilles Peskined2971572021-07-26 18:48:10 +020080/** The output buffer is too small. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081#define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880
Ron Eldor9924bdc2018-10-04 10:59:13 +030082
Paul Bakkered27a042013-04-18 22:46:23 +020083#ifdef __cplusplus
84extern "C" {
85#endif
86
87/**
88 * \brief Public key types
89 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090typedef enum
91{
92 MBEDTLS_PK_NONE = 0,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 MBEDTLS_PK_RSA,
94 MBEDTLS_PK_ECKEY,
95 MBEDTLS_PK_ECKEY_DH,
96 MBEDTLS_PK_ECDSA,
97 MBEDTLS_PK_RSA_ALT,
98 MBEDTLS_PK_RSASSA_PSS,
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +010099 MBEDTLS_PK_OPAQUE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +0200101
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200102/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200103 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200105 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106typedef struct mbedtls_pk_rsassa_pss_options {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 mbedtls_md_type_t MBEDTLS_PRIVATE(mgf1_hash_id);
108 int MBEDTLS_PRIVATE(expected_salt_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200111
112/**
Gilles Peskineda252be2019-11-05 16:23:49 +0100113 * \brief Maximum size of a signature made by mbedtls_pk_sign().
114 */
Gilles Peskine54605652019-11-08 16:24:16 +0100115/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
116 * size among the supported signature types. Do it by starting at 0,
117 * then incrementally increasing to be large enough for each supported
118 * signature mechanism.
119 *
120 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
121 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
122 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
123 */
124#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
125
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200126#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100127 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100128/* For RSA, the signature can be as large as the bignum module allows.
129 * For RSA_ALT, the signature size is not necessarily tied to what the
130 * bignum module can do, but in the absence of any specific setting,
Chris Jones3848e312021-03-11 16:17:59 +0000131 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200132# undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
133# define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100134#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100135
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136#if defined(MBEDTLS_ECDSA_C) && \
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100137 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100138/* For ECDSA, the ecdsa module exports a constant for the maximum
139 * signature size. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140# undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
141# define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100142#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100143
144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145# if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
Gilles Peskine54605652019-11-08 16:24:16 +0100147 * through the PSA API in the PSA representation. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148# undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
149# define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
150# endif
Gilles Peskine54605652019-11-08 16:24:16 +0100151
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200152# if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100153/* The Mbed TLS representation is different for ECDSA signatures:
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100154 * PSA uses the raw concatenation of r and s,
155 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
156 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
157 * types, lengths (represented by up to 2 bytes), and potential leading
158 * zeros of the INTEGERs and the SEQUENCE. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159# undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
160# define MBEDTLS_PK_SIGNATURE_MAX_SIZE \
161 (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
162# endif
Gilles Peskine54605652019-11-08 16:24:16 +0100163#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100164
165/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200166 * \brief Types for interfacing with the debug module
167 */
168typedef enum
169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 MBEDTLS_PK_DEBUG_NONE = 0,
171 MBEDTLS_PK_DEBUG_MPI,
172 MBEDTLS_PK_DEBUG_ECP,
173} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200174
175/**
176 * \brief Item to send to the debug module
177 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178typedef struct mbedtls_pk_debug_item {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200179 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type);
180 const char *MBEDTLS_PRIVATE(name);
181 void *MBEDTLS_PRIVATE(value);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200183
184/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200186
187/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200188 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200191
192/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200193 * \brief Public key container
194 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195typedef struct mbedtls_pk_context {
196 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key
197 information */
198 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200200
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200201#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200202/**
203 * \brief Context for resuming operations
204 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205typedef struct {
206 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key
207 information */
208 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200209} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200210#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200211/* Now we can declare functions that take a pointer to that */
212typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200213#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200214
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200215#if defined(MBEDTLS_RSA_C)
216/**
217 * Quick access to an RSA context inside a PK context.
218 *
219 * \warning You must make sure the PK context actually holds an RSA context
220 * before using this function!
221 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200223{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224 return ((mbedtls_rsa_context *)(pk).MBEDTLS_PRIVATE(pk_ctx));
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200225}
226#endif /* MBEDTLS_RSA_C */
227
228#if defined(MBEDTLS_ECP_C)
229/**
230 * Quick access to an EC context inside a PK context.
231 *
232 * \warning You must make sure the PK context actually holds an EC context
233 * before using this function!
234 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200236{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 return ((mbedtls_ecp_keypair *)(pk).MBEDTLS_PRIVATE(pk_ctx));
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200238}
239#endif /* MBEDTLS_ECP_C */
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200242/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200243 * \brief Types for RSA-alt abstraction
244 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx,
246 size_t *olen,
247 const unsigned char *input,
248 unsigned char *output,
249 size_t output_max_len);
250typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
251 int (*f_rng)(void *,
252 unsigned char *,
253 size_t),
254 void *p_rng,
255 mbedtls_md_type_t md_alg,
256 unsigned int hashlen,
257 const unsigned char *hash,
258 unsigned char *sig);
259typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200261
262/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200263 * \brief Return information associated with the given PK type
264 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200265 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200266 *
267 * \return The PK info associated with the type or NULL if not found.
268 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200270
271/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * \brief Initialize a #mbedtls_pk_context (as NONE).
273 *
274 * \param ctx The context to initialize.
275 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200276 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200277void mbedtls_pk_init(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200278
279/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500280 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100281 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * \param ctx The context to clear. It must have been initialized.
283 * If this is \c NULL, this function does nothing.
284 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100285 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100286 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200287 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100288 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200289 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290void mbedtls_pk_free(mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200291
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200292#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200293/**
294 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 *
296 * \param ctx The context to initialize.
297 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200298 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200300
301/**
302 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 *
304 * \param ctx The context to clear. It must have been initialized.
305 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200306 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200307void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200308#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200309
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200310/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200311 * \brief Initialize a PK context with the information given
312 * and allocates the type-specific PK subcontext.
313 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500314 * \param ctx Context to initialize. It must not have been set
315 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200316 * \param info Information to use
317 *
318 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200320 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200321 *
Paul Bakker42099c32014-01-27 11:45:49 +0100322 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200323 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200324 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200326
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200327#if defined(MBEDTLS_USE_PSA_CRYPTO)
328/**
Gilles Peskine11392492019-05-27 14:53:19 +0200329 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200330 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100331 * \note This function replaces mbedtls_pk_setup() for contexts
Gilles Peskine11392492019-05-27 14:53:19 +0200332 * that wrap a (possibly opaque) PSA key instead of
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100333 * storing and manipulating the key material directly.
334 *
335 * \param ctx The context to initialize. It must be empty (type NONE).
Gilles Peskine11392492019-05-27 14:53:19 +0200336 * \param key The PSA key to wrap, which must hold an ECC key pair
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100337 * (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200338 *
Gilles Peskine11392492019-05-27 14:53:19 +0200339 * \note The wrapped key must remain valid as long as the
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100340 * wrapping PK context is in use, that is at least between
341 * the point this function is called and the point
342 * mbedtls_pk_free() is called on this context. The wrapped
Gilles Peskine11392492019-05-27 14:53:19 +0200343 * key might then be independently used or destroyed.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100344 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100345 * \note This function is currently only available for ECC key
346 * pairs (that is, ECC keys containing private key material).
347 * Support for other key types may be added later.
348 *
349 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100350 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200351 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100352 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100353 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100354 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200355 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200356int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, const psa_key_id_t key);
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200357#endif /* MBEDTLS_USE_PSA_CRYPTO */
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200360/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200361 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200362 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 * \param ctx Context to initialize. It must not have been set
364 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200365 * \param key RSA key pointer
366 * \param decrypt_func Decryption function
367 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200368 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200369 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200371 * context wasn't already initialized as RSA_ALT.
372 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200373 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200374 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200375int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx,
376 void *key,
377 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
378 mbedtls_pk_rsa_alt_sign_func sign_func,
379 mbedtls_pk_rsa_alt_key_len_func key_len_func);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200381
382/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200383 * \brief Get the size in bits of the underlying key
384 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200386 *
387 * \return Key size in bits, or 0 on error
388 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200389size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200390
391/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200392 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 *
394 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200395 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200396 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200397 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200398static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200399{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200401}
402
403/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200404 * \brief Tell if a context can do the operation given by type
405 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406 * \param ctx The context to query. It must have been initialized.
407 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200408 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 * \return 1 if the context can do operations on the given type.
410 * \return 0 if the context cannot do the operations on the given
411 * type. This is always the case for a context that has
412 * been initialized but not set up, or that has been
413 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200414 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200415int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200416
417/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200418 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200419 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200421 * \param md_alg Hash algorithm used.
422 * This can be #MBEDTLS_MD_NONE if the signature algorithm
423 * does not rely on a hash algorithm (non-deterministic
424 * ECDSA, RSA PKCS#1 v1.5).
425 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
426 * \p hash is the DigestInfo structure used by RFC 8017
427 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
428 * algorithm then \p hash is the digest itself, and this
429 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200430 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200431 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200432 * \param sig Signature to verify
433 * \param sig_len Signature length
434 *
435 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200436 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
437 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200438 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200439 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200440 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200442 * to verify RSASSA_PSS signatures.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200443 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200444int mbedtls_pk_verify(mbedtls_pk_context *ctx,
445 mbedtls_md_type_t md_alg,
446 const unsigned char *hash,
447 size_t hash_len,
448 const unsigned char *sig,
449 size_t sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200450
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200451/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200452 * \brief Restartable version of \c mbedtls_pk_verify()
453 *
454 * \note Performs the same job as \c mbedtls_pk_verify(), but can
455 * return early and restart according to the limit set with
456 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
457 * operations. For RSA, same as \c mbedtls_pk_verify().
458 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500459 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200460 * \param md_alg Hash algorithm used (see notes)
461 * \param hash Hash of the message to sign
462 * \param hash_len Hash length or 0 (see notes)
463 * \param sig Signature to verify
464 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200465 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200466 *
467 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200468 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200469 * operations was reached: see \c mbedtls_ecp_set_max_ops().
470 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
472 mbedtls_md_type_t md_alg,
473 const unsigned char *hash,
474 size_t hash_len,
475 const unsigned char *sig,
476 size_t sig_len,
477 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200478
479/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200480 * \brief Verify signature, with options.
481 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200482 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200483 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200484 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200486 * \param md_alg Hash algorithm used (see notes)
487 * \param hash Hash of the message to sign
488 * \param hash_len Hash length or 0 (see notes)
489 * \param sig Signature to verify
490 * \param sig_len Signature length
491 *
492 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200493 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200494 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200495 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
496 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200497 * or a specific error code.
498 *
499 * \note If hash_len is 0, then the length associated with md_alg
500 * is used instead, or an error returned if it is invalid.
501 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200503 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
505 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200506 * otherwise it must be NULL.
507 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508int mbedtls_pk_verify_ext(mbedtls_pk_type_t type,
509 const void *options,
510 mbedtls_pk_context *ctx,
511 mbedtls_md_type_t md_alg,
512 const unsigned char *hash,
513 size_t hash_len,
514 const unsigned char *sig,
515 size_t sig_len);
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200516
517/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200518 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200519 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500520 * \param ctx The PK context to use. It must have been set up
521 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200522 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200523 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200524 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100525 * \param sig Place to write the signature.
526 * It must have enough room for the signature.
527 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
528 * You may use a smaller buffer if it is large enough
529 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200530 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100531 * \param sig_len On successful return,
532 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200533 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200534 * \param p_rng RNG parameter
535 *
536 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200537 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200538 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
539 * There is no interface in the PK module to make RSASSA-PSS
540 * signatures yet.
541 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
543 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200544 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200545int mbedtls_pk_sign(mbedtls_pk_context *ctx,
546 mbedtls_md_type_t md_alg,
547 const unsigned char *hash,
548 size_t hash_len,
549 unsigned char *sig,
550 size_t sig_size,
551 size_t *sig_len,
552 int (*f_rng)(void *, unsigned char *, size_t),
553 void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200554
555/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200556 * \brief Restartable version of \c mbedtls_pk_sign()
557 *
558 * \note Performs the same job as \c mbedtls_pk_sign(), but can
559 * return early and restart according to the limit set with
560 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
561 * operations. For RSA, same as \c mbedtls_pk_sign().
562 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * \param ctx The PK context to use. It must have been set up
564 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100565 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200566 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200567 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100568 * \param sig Place to write the signature.
569 * It must have enough room for the signature.
570 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
571 * You may use a smaller buffer if it is large enough
572 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200573 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100574 * \param sig_len On successful return,
575 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200576 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200577 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200578 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200579 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100580 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200581 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200582 * operations was reached: see \c mbedtls_ecp_set_max_ops().
583 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200584int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
585 mbedtls_md_type_t md_alg,
586 const unsigned char *hash,
587 size_t hash_len,
588 unsigned char *sig,
589 size_t sig_size,
590 size_t *sig_len,
591 int (*f_rng)(void *, unsigned char *, size_t),
592 void *p_rng,
593 mbedtls_pk_restart_ctx *rs_ctx);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200594
595/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200596 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200597 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500598 * \param ctx The PK context to use. It must have been set up
599 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200600 * \param input Input to decrypt
601 * \param ilen Input size
602 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200603 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200604 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200605 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200606 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200607 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200608 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
609 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200610 * \return 0 on success, or a specific error code.
611 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200612int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
613 const unsigned char *input,
614 size_t ilen,
615 unsigned char *output,
616 size_t *olen,
617 size_t osize,
618 int (*f_rng)(void *, unsigned char *, size_t),
619 void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200620
621/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200622 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200623 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500624 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200625 * \param input Message to encrypt
626 * \param ilen Message size
627 * \param output Encrypted output
628 * \param olen Encrypted output length
629 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200630 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200631 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200632 *
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200633 * \note \p f_rng is used for padding generation.
634 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200635 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
636 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200637 * \return 0 on success, or a specific error code.
638 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200639int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
640 const unsigned char *input,
641 size_t ilen,
642 unsigned char *output,
643 size_t *olen,
644 size_t osize,
645 int (*f_rng)(void *, unsigned char *, size_t),
646 void *p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200647
648/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100649 * \brief Check if a public-private pair of keys matches.
650 *
651 * \param pub Context holding a public key.
652 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200653 * \param f_rng RNG function, must not be \c NULL.
654 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100655 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200656 * \return \c 0 on success (keys were checked and match each other).
657 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
658 * be checked - in that case they may or may not match.
659 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
660 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100661 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200662int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
663 const mbedtls_pk_context *prv,
664 int (*f_rng)(void *, unsigned char *, size_t),
665 void *p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100666
667/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200668 * \brief Export debug information
669 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500670 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200671 * \param items Place to write debug items
672 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200674 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200675int mbedtls_pk_debug(const mbedtls_pk_context *ctx,
676 mbedtls_pk_debug_item *items);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200677
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200678/**
679 * \brief Access the type name
680 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500681 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200682 *
683 * \return Type name on success, or "invalid PK"
684 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200685const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200686
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200687/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200688 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200689 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500690 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200691 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500692 * \return Type on success.
693 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200694 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200695mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200698/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200700 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500702 * \param ctx The PK context to fill. It must have been initialized
703 * but not set up.
704 * \param key Input buffer to parse.
705 * The buffer must contain the input exactly, with no
706 * extra trailing material. For PEM, the buffer must
707 * contain a null-terminated string.
708 * \param keylen Size of \b key in bytes.
709 * For PEM data, this includes the terminating null byte,
710 * so \p keylen must be equal to `strlen(key) + 1`.
711 * \param pwd Optional password for decryption.
712 * Pass \c NULL if expecting a non-encrypted key.
713 * Pass a string of \p pwdlen bytes if expecting an encrypted
714 * key; a non-encrypted key will also be accepted.
715 * The empty password is not supported.
716 * \param pwdlen Size of the password in bytes.
717 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200718 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
719 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100721 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
723 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100724 *
725 * \note The key is also checked for correctness.
726 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 * \return 0 if successful, or a specific PK or PEM error code
728 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200729int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
730 const unsigned char *key,
731 size_t keylen,
732 const unsigned char *pwd,
733 size_t pwdlen,
734 int (*f_rng)(void *, unsigned char *, size_t),
735 void *p_rng);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736
Paul Bakkerff3a5182013-09-16 22:42:19 +0200737/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200739 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500741 * \param ctx The PK context to fill. It must have been initialized
742 * but not set up.
743 * \param key Input buffer to parse.
744 * The buffer must contain the input exactly, with no
745 * extra trailing material. For PEM, the buffer must
746 * contain a null-terminated string.
747 * \param keylen Size of \b key in bytes.
748 * For PEM data, this includes the terminating null byte,
749 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100751 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
753 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100754 *
755 * \note The key is also checked for correctness.
756 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757 * \return 0 if successful, or a specific PK or PEM error code
758 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200759int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
760 const unsigned char *key,
761 size_t keylen);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200763# if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200764/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200766 * \brief Load and parse a private key
767 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500768 * \param ctx The PK context to fill. It must have been initialized
769 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200770 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500771 * \param password Optional password to decrypt the file.
772 * Pass \c NULL if expecting a non-encrypted key.
773 * Pass a null-terminated string if expecting an encrypted
774 * key; a non-encrypted key will also be accepted.
775 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200776 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
777 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200778 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100779 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
781 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100782 *
783 * \note The key is also checked for correctness.
784 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200785 * \return 0 if successful, or a specific PK or PEM error code
786 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200787int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
788 const char *path,
789 const char *password,
790 int (*f_rng)(void *, unsigned char *, size_t),
791 void *p_rng);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200792
Paul Bakkerff3a5182013-09-16 22:42:19 +0200793/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200794/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795 * \brief Load and parse a public key
796 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500797 * \param ctx The PK context to fill. It must have been initialized
798 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +0100799 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100801 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100802 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
803 * you need a specific key type, check the result with
804 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100805 *
806 * \note The key is also checked for correctness.
807 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808 * \return 0 if successful, or a specific PK or PEM error code
809 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200810int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
811# endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200816 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
817 * Note: data is written at the end of the buffer! Use the
818 * return value to determine where you should start
819 * using the buffer
820 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500821 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200822 * \param buf buffer to write to
823 * \param size size of the buffer
824 *
825 * \return length of data written if successful, or a specific
826 * error code
827 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200828int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx,
829 unsigned char *buf,
830 size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200831
832/**
833 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
834 * Note: data is written at the end of the buffer! Use the
835 * return value to determine where you should start
836 * using the buffer
837 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500838 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200839 * \param buf buffer to write to
840 * \param size size of the buffer
841 *
842 * \return length of data written if successful, or a specific
843 * error code
844 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200845int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx,
846 unsigned char *buf,
847 size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200848
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200849# if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200850/**
851 * \brief Write a public key to a PEM string
852 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500853 * \param ctx PK context which must contain a valid public or private key.
854 * \param buf Buffer to write to. The output includes a
855 * terminating null byte.
856 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200857 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200858 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200859 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200860int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx,
861 unsigned char *buf,
862 size_t size);
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200863
864/**
865 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
866 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500867 * \param ctx PK context which must contain a valid private key.
868 * \param buf Buffer to write to. The output includes a
869 * terminating null byte.
870 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200871 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200872 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200873 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200874int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx,
875 unsigned char *buf,
876 size_t size);
877# endif /* MBEDTLS_PEM_WRITE_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200879
880/*
881 * WARNING: Low-level functions. You probably do not want to use these unless
882 * you are certain you do ;)
883 */
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200886/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 * \brief Parse a SubjectPublicKeyInfo DER structure
888 *
889 * \param p the position in the ASN.1 data
890 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500891 * \param pk The PK context to fill. It must have been initialized
892 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893 *
894 * \return 0 if successful, or a specific PK error code
895 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200896int mbedtls_pk_parse_subpubkey(unsigned char **p,
897 const unsigned char *end,
898 mbedtls_pk_context *pk);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200902/**
903 * \brief Write a subjectPublicKey to ASN.1 data
904 * Note: function works backwards in data buffer
905 *
906 * \param p reference to current position pointer
907 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500908 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200909 *
910 * \return the length written or a negative error code
911 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200912int mbedtls_pk_write_pubkey(unsigned char **p,
913 unsigned char *start,
914 const mbedtls_pk_context *key);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200916
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100917/*
918 * Internal module functions. You probably do not want to use these unless you
919 * know you do.
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_FS_IO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200922int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100923#endif
924
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100925#if defined(MBEDTLS_USE_PSA_CRYPTO)
926/**
Gilles Peskine11392492019-05-27 14:53:19 +0200927 * \brief Turn an EC key into an opaque one.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100928 *
929 * \warning This is a temporary utility function for tests. It might
930 * change or be removed at any time without notice.
931 *
932 * \note Only ECDSA keys are supported so far. Signing with the
933 * specified hash is the only allowed use of that key.
934 *
Gilles Peskine11392492019-05-27 14:53:19 +0200935 * \param pk Input: the EC key to import to a PSA key.
936 * Output: a PK context wrapping that PSA key.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200937 * \param key Output: a PSA key identifier.
Gilles Peskine11392492019-05-27 14:53:19 +0200938 * It's the caller's responsibility to call
Ronald Croncf56a0a2020-08-04 09:51:30 +0200939 * psa_destroy_key() on that key identifier after calling
Gilles Peskine11392492019-05-27 14:53:19 +0200940 * mbedtls_pk_free() on the PK context.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100941 * \param hash_alg The hash algorithm to allow for use with that key.
942 *
943 * \return \c 0 if successful.
944 * \return An Mbed TLS error code otherwise.
945 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200946int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
947 psa_key_id_t *key,
948 psa_algorithm_t hash_alg);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100949#endif /* MBEDTLS_USE_PSA_CRYPTO */
950
Paul Bakkered27a042013-04-18 22:46:23 +0200951#ifdef __cplusplus
952}
953#endif
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_PK_H */