blob: 30f0492e90564e21c257f00ecc195d3206aaa39c [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)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#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)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#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)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010040#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)
44#include "psa/crypto.h"
45#endif
46
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010047#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48 !defined(inline) && !defined(__cplusplus)
49#define inline __inline
50#endif
51
Gilles Peskined2971572021-07-26 18:48:10 +020052/** Memory allocation failed. */
53#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
54/** Type mismatch, eg attempt to encrypt with an ECDSA key */
55#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
56/** Bad input parameters to function. */
57#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
58/** Read/write of file failed. */
59#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
60/** Unsupported key version */
61#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
62/** Invalid key tag or value. */
63#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
64/** Key algorithm is unsupported (only RSA and EC are supported). */
65#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
66/** Private key password can't be empty. */
67#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
68/** Given private key password does not allow for correct decryption. */
69#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
70/** The pubkey tag or value is invalid (only RSA and EC are supported). */
71#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
72/** The algorithm tag or value is invalid. */
73#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
74/** 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. */
79#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
80/** The output buffer is too small. */
81#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 */
90typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 MBEDTLS_PK_NONE=0,
92 MBEDTLS_PK_RSA,
93 MBEDTLS_PK_ECKEY,
94 MBEDTLS_PK_ECKEY_DH,
95 MBEDTLS_PK_ECDSA,
96 MBEDTLS_PK_RSA_ALT,
97 MBEDTLS_PK_RSASSA_PSS,
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +010098 MBEDTLS_PK_OPAQUE,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +0200100
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200101/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200102 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200104 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200105typedef struct mbedtls_pk_rsassa_pss_options
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200106{
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
126#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). */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100132#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
133#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
134#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100135
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100136#if defined(MBEDTLS_ECDSA_C) && \
137 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. */
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100140#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
141#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
142#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100143
144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100145#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
146/* 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. */
148#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100149#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100150#endif
151
152#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
153/* 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. */
159#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
Gilles Peskine54605652019-11-08 16:24:16 +0100160#define MBEDTLS_PK_SIGNATURE_MAX_SIZE ( PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 )
Gilles Peskineb22a24b2019-11-05 16:56:39 +0100161#endif
Gilles Peskine54605652019-11-08 16:24:16 +0100162#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
Gilles Peskineda252be2019-11-05 16:23:49 +0100163
164/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200165 * \brief Types for interfacing with the debug module
166 */
167typedef enum
168{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 MBEDTLS_PK_DEBUG_NONE = 0,
170 MBEDTLS_PK_DEBUG_MPI,
171 MBEDTLS_PK_DEBUG_ECP,
172} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200173
174/**
175 * \brief Item to send to the debug module
176 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200177typedef struct mbedtls_pk_debug_item
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200178{
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
Gilles Peskine2e9d65f2021-08-31 23:05:19 +0200189 *
190 * \note The library does not support custom pk info structures,
191 * only built-in structures returned by
192 * mbedtls_cipher_info_from_type().
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200195
196/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200197 * \brief Public key container
198 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200199typedef struct mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200200{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200201 const mbedtls_pk_info_t * MBEDTLS_PRIVATE(pk_info); /**< Public key information */
202 void * MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200204
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200205#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200206/**
207 * \brief Context for resuming operations
208 */
209typedef struct
210{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200211 const mbedtls_pk_info_t * MBEDTLS_PRIVATE(pk_info); /**< Public key information */
212 void * MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200213} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200214#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200215/* Now we can declare functions that take a pointer to that */
216typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200217#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200220/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200221 * \brief Types for RSA-alt abstraction
222 */
Thomas Daubney99914142021-05-06 15:17:03 +0100223typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200224 const unsigned char *input, unsigned char *output,
225 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200227 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Thomas Daubneyfa1581e2021-05-18 12:38:33 +0100228 mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200229 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
231#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200232
233/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200234 * \brief Return information associated with the given PK type
235 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200236 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200237 *
238 * \return The PK info associated with the type or NULL if not found.
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200241
242/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 * \brief Initialize a #mbedtls_pk_context (as NONE).
244 *
245 * \param ctx The context to initialize.
246 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200249
250/**
Andrzej Kurekb274f272019-02-05 05:06:35 -0500251 * \brief Free the components of a #mbedtls_pk_context.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100252 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 * \param ctx The context to clear. It must have been initialized.
254 * If this is \c NULL, this function does nothing.
255 *
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100256 * \note For contexts that have been set up with
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100257 * mbedtls_pk_setup_opaque(), this does not free the underlying
Gilles Peskine11392492019-05-27 14:53:19 +0200258 * PSA key and you still need to call psa_destroy_key()
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100259 * independently if you want to destroy that key.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200262
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200263#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200264/**
265 * \brief Initialize a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 *
267 * \param ctx The context to initialize.
268 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200269 */
270void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
271
272/**
273 * \brief Free the components of a restart context
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 *
275 * \param ctx The context to clear. It must have been initialized.
276 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200277 */
278void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200279#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200280
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200281/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200282 * \brief Initialize a PK context with the information given
283 * and allocates the type-specific PK subcontext.
284 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 * \param ctx Context to initialize. It must not have been set
286 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200287 * \param info Information to use
288 *
289 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200291 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200292 *
Paul Bakker42099c32014-01-27 11:45:49 +0100293 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200294 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200295 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200296int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200297
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200298#if defined(MBEDTLS_USE_PSA_CRYPTO)
299/**
Gilles Peskine11392492019-05-27 14:53:19 +0200300 * \brief Initialize a PK context to wrap a PSA key.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200301 *
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100302 * \note This function replaces mbedtls_pk_setup() for contexts
Gilles Peskine11392492019-05-27 14:53:19 +0200303 * that wrap a (possibly opaque) PSA key instead of
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100304 * storing and manipulating the key material directly.
305 *
306 * \param ctx The context to initialize. It must be empty (type NONE).
Neil Armstrongb3547422022-03-22 10:22:28 +0100307 * \param key The PSA key to wrap, which must hold an ECC or RSA key
308 * pair (see notes below).
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200309 *
Gilles Peskine11392492019-05-27 14:53:19 +0200310 * \note The wrapped key must remain valid as long as the
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100311 * wrapping PK context is in use, that is at least between
312 * the point this function is called and the point
313 * mbedtls_pk_free() is called on this context. The wrapped
Gilles Peskine11392492019-05-27 14:53:19 +0200314 * key might then be independently used or destroyed.
Manuel Pégourié-Gonnard01a12c42018-10-31 10:28:01 +0100315 *
Neil Armstrongb3547422022-03-22 10:22:28 +0100316 * \note This function is currently only available for ECC or RSA
317 * key pairs (that is, keys containing private key material).
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100318 * Support for other key types may be added later.
319 *
320 * \return \c 0 on success.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100321 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input
Ronald Croncf56a0a2020-08-04 09:51:30 +0200322 * (context already used, invalid key identifier).
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100323 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an
Manuel Pégourié-Gonnard392dc042018-11-13 10:48:23 +0100324 * ECC key pair.
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100325 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200326 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200327int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100328 const mbedtls_svc_key_id_t key );
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200329#endif /* MBEDTLS_USE_PSA_CRYPTO */
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200332/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200333 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200334 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500335 * \param ctx Context to initialize. It must not have been set
336 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200337 * \param key RSA key pointer
338 * \param decrypt_func Decryption function
339 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200340 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200341 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200343 * context wasn't already initialized as RSA_ALT.
344 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200345 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200346 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200347int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
349 mbedtls_pk_rsa_alt_sign_func sign_func,
350 mbedtls_pk_rsa_alt_key_len_func key_len_func );
351#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200352
353/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200354 * \brief Get the size in bits of the underlying key
355 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500356 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200357 *
358 * \return Key size in bits, or 0 on error
359 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200360size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200361
362/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200363 * \brief Get the length in bytes of the underlying key
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364 *
365 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200366 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200367 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200370{
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200371 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200372}
373
374/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200375 * \brief Tell if a context can do the operation given by type
376 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 * \param ctx The context to query. It must have been initialized.
378 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200379 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * \return 1 if the context can do operations on the given type.
381 * \return 0 if the context cannot do the operations on the given
382 * type. This is always the case for a context that has
383 * been initialized but not set up, or that has been
384 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200387
Neil Armstrong0b529582022-05-11 10:10:20 +0200388#if defined(MBEDTLS_USE_PSA_CRYPTO)
389/**
Neil Armstrongcec133a2022-05-17 11:56:01 +0200390 * \brief Tell if context can do the operation given by PSA algorithm
Neil Armstrong0b529582022-05-11 10:10:20 +0200391 *
392 * \param ctx The context to query. It must have been initialized.
393 * \param alg PSA algorithm to check against, the following are allowed:
394 * PSA_ALG_RSA_PKCS1V15_SIGN(hash),
395 * PSA_ALG_RSA_PSS(hash),
396 * PSA_ALG_RSA_PKCS1V15_CRYPT,
397 * PSA_ALG_ECDSA(hash),
398 * PSA_ALG_ECDH, where hash is a specific hash.
Neil Armstrong408f6a62022-05-17 14:23:20 +0200399 * \param usage PSA usage flag to check against, must be composed of:
400 * PSA_KEY_USAGE_SIGN_HASH
401 * PSA_KEY_USAGE_DECRYPT
402 * PSA_KEY_USAGE_DERIVE.
403 * Context key must match all passed usage flags.
Neil Armstrong0b529582022-05-11 10:10:20 +0200404 *
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200405 * \warning Since the set of allowed algorithms and usage flags may be
406 * expanded in the future, the return value \c 0 should not
407 * be taken in account for non-allowed algorithms and usage
408 * flags.
409 *
Neil Armstrong0b529582022-05-11 10:10:20 +0200410 * \return 1 if the context can do operations on the given type.
411 * \return 0 if the context cannot do the operations on the given
Neil Armstrongb2f2b022022-05-20 12:00:56 +0200412 * type, for non-allowed algorithms and usage flags, or
413 * for a context that has been initialized but not set up
414 * or that has been cleared with mbedtls_pk_free().
Neil Armstrong0b529582022-05-11 10:10:20 +0200415 */
Neil Armstrong408f6a62022-05-17 14:23:20 +0200416int mbedtls_pk_can_do_ext( const mbedtls_pk_context *ctx, psa_algorithm_t alg,
417 psa_key_usage_t usage );
Neil Armstrong0b529582022-05-11 10:10:20 +0200418#endif /* MBEDTLS_USE_PSA_CRYPTO */
419
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200420/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200421 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200422 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 * \param ctx The PK context to use. It must have been set up.
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200424 * \param md_alg Hash algorithm used.
425 * This can be #MBEDTLS_MD_NONE if the signature algorithm
426 * does not rely on a hash algorithm (non-deterministic
427 * ECDSA, RSA PKCS#1 v1.5).
428 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then
429 * \p hash is the DigestInfo structure used by RFC 8017
430 * &sect;9.2 steps 3&ndash;6. If \p md_alg is a valid hash
431 * algorithm then \p hash is the digest itself, and this
432 * function calculates the DigestInfo encoding internally.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200433 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200434 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200435 * \param sig Signature to verify
436 * \param sig_len Signature length
437 *
438 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200439 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
440 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200441 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200442 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200443 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200445 * to verify RSASSA_PSS signatures.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200448 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200449 const unsigned char *sig, size_t sig_len );
450
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 */
471int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
472 mbedtls_md_type_t md_alg,
473 const unsigned char *hash, size_t hash_len,
474 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200475 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200476
477/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200478 * \brief Verify signature, with options.
479 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200480 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200481 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200482 * \param options Pointer to type-specific options, or NULL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500483 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200484 * \param md_alg Hash algorithm used (see notes)
485 * \param hash Hash of the message to sign
486 * \param hash_len Hash length or 0 (see notes)
487 * \param sig Signature to verify
488 * \param sig_len Signature length
489 *
490 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200491 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200492 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200493 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
494 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200495 * or a specific error code.
496 *
497 * \note If hash_len is 0, then the length associated with md_alg
498 * is used instead, or an error returned if it is invalid.
499 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200501 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
503 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200504 * otherwise it must be NULL.
505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
507 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200508 const unsigned char *hash, size_t hash_len,
509 const unsigned char *sig, size_t sig_len );
510
511/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200512 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200513 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500514 * \param ctx The PK context to use. It must have been set up
515 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200516 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200517 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200518 * \param hash_len Hash length
Gilles Peskineda252be2019-11-05 16:23:49 +0100519 * \param sig Place to write the signature.
520 * It must have enough room for the signature.
521 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
522 * You may use a smaller buffer if it is large enough
523 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200524 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskineda252be2019-11-05 16:23:49 +0100525 * \param sig_len On successful return,
526 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200527 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200528 * \param p_rng RNG parameter
529 *
530 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200531 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200532 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
533 * There is no interface in the PK module to make RSASSA-PSS
534 * signatures yet.
535 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
537 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200540 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200541 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200542 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
543
Jerry Yubc18c232022-03-12 19:40:29 +0800544#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200545/**
Jerry Yu406cf272022-03-22 11:33:42 +0800546 * \brief Make signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800547 *
Jerry Yu8beb9e12022-03-12 16:23:53 +0800548 * \param pk_type Signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800549 * \param ctx The PK context to use. It must have been set up
550 * with a private key.
551 * \param md_alg Hash algorithm used (see notes)
552 * \param hash Hash of the message to sign
553 * \param hash_len Hash length
554 * \param sig Place to write the signature.
555 * It must have enough room for the signature.
556 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
557 * You may use a smaller buffer if it is large enough
558 * given the key type.
559 * \param sig_size The size of the \p sig buffer in bytes.
560 * \param sig_len On successful return,
561 * the number of bytes written to \p sig.
562 * \param f_rng RNG function, must not be \c NULL.
563 * \param p_rng RNG parameter
564 *
565 * \return 0 on success, or a specific error code.
566 *
Jerry Yue6e73d62022-03-24 13:05:08 +0800567 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS,
568 * see #PSA_ALG_RSA_PSS for a description of PSS options used.
Jerry Yud69439a2022-02-24 15:52:15 +0800569 *
570 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
571 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
572 *
Jerry Yud69439a2022-02-24 15:52:15 +0800573 */
Jerry Yu718a9b42022-03-12 22:43:01 +0800574int mbedtls_pk_sign_ext( mbedtls_pk_type_t pk_type,
Jerry Yu8beb9e12022-03-12 16:23:53 +0800575 mbedtls_pk_context *ctx,
576 mbedtls_md_type_t md_alg,
577 const unsigned char *hash, size_t hash_len,
578 unsigned char *sig, size_t sig_size, size_t *sig_len,
579 int (*f_rng)(void *, unsigned char *, size_t),
580 void *p_rng );
Jerry Yubc18c232022-03-12 19:40:29 +0800581#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800582
583/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200584 * \brief Restartable version of \c mbedtls_pk_sign()
585 *
586 * \note Performs the same job as \c mbedtls_pk_sign(), but can
587 * return early and restart according to the limit set with
588 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
589 * operations. For RSA, same as \c mbedtls_pk_sign().
590 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500591 * \param ctx The PK context to use. It must have been set up
592 * with a private key.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100593 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign())
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200594 * \param hash Hash of the message to sign
Gilles Peskine9dbbc292021-06-22 18:28:13 +0200595 * \param hash_len Hash length
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100596 * \param sig Place to write the signature.
597 * It must have enough room for the signature.
598 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough.
599 * You may use a smaller buffer if it is large enough
600 * given the key type.
Gilles Peskinef00f1522021-06-22 00:09:00 +0200601 * \param sig_size The size of the \p sig buffer in bytes.
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100602 * \param sig_len On successful return,
603 * the number of bytes written to \p sig.
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200604 * \param f_rng RNG function, must not be \c NULL.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200605 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200606 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200607 *
Gilles Peskine9db14fa2019-11-08 18:37:19 +0100608 * \return See \c mbedtls_pk_sign().
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200609 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200610 * operations was reached: see \c mbedtls_ecp_set_max_ops().
611 */
612int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
613 mbedtls_md_type_t md_alg,
614 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200615 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200617 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200618
619/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200620 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200621 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500622 * \param ctx The PK context to use. It must have been set up
623 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200624 * \param input Input to decrypt
625 * \param ilen Input size
626 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200627 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200628 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200629 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200630 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200631 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200632 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
633 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200634 * \return 0 on success, or a specific error code.
635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200637 const unsigned char *input, size_t ilen,
638 unsigned char *output, size_t *olen, size_t osize,
639 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
640
641/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200642 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200643 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500644 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200645 * \param input Message to encrypt
646 * \param ilen Message size
647 * \param output Encrypted output
648 * \param olen Encrypted output length
649 * \param osize Size of the output buffer
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200650 * \param f_rng RNG function, must not be \c NULL.
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200651 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200652 *
Manuel Pégourié-Gonnard34d37562021-06-15 11:29:26 +0200653 * \note \p f_rng is used for padding generation.
654 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200655 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
656 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200657 * \return 0 on success, or a specific error code.
658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200660 const unsigned char *input, size_t ilen,
661 unsigned char *output, size_t *olen, size_t osize,
662 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
663
664/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100665 * \brief Check if a public-private pair of keys matches.
666 *
667 * \param pub Context holding a public key.
668 * \param prv Context holding a private (and public) key.
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200669 * \param f_rng RNG function, must not be \c NULL.
670 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100671 *
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200672 * \return \c 0 on success (keys were checked and match each other).
673 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not
674 * be checked - in that case they may or may not match.
675 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid.
676 * \return Another non-zero value if the keys do not match.
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100677 */
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200678int mbedtls_pk_check_pair( const mbedtls_pk_context *pub,
679 const mbedtls_pk_context *prv,
680 int (*f_rng)(void *, unsigned char *, size_t),
681 void *p_rng );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100682
683/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200684 * \brief Export debug information
685 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500686 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200687 * \param items Place to write debug items
688 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200692
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200693/**
694 * \brief Access the type name
695 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500696 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200697 *
698 * \return Type name on success, or "invalid PK"
699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200701
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200702/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200703 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200704 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500705 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200706 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500707 * \return Type on success.
708 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200711
Manuel Pégourié-Gonnard22e84de2022-06-10 09:48:38 +0200712#if defined(MBEDTLS_RSA_C)
713/**
714 * Quick access to an RSA context inside a PK context.
715 *
716 * \warning This function can only be used when the type of the context, as
717 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA.
718 * Ensuring that is the caller's responsibility.
719 * Alternatively, you can check whether this function returns NULL.
720 *
721 * \return The internal RSA context held by the PK context, or NULL.
722 */
723static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
724{
725 return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ?
726 (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) :
727 NULL );
728}
729#endif /* MBEDTLS_RSA_C */
730
731#if defined(MBEDTLS_ECP_C)
732/**
733 * Quick access to an EC context inside a PK context.
734 *
735 * \warning This function can only be used when the type of the context, as
736 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY,
737 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA.
738 * Ensuring that is the caller's responsibility.
739 * Alternatively, you can check whether this function returns NULL.
740 *
741 * \return The internal EC context held by the PK context, or NULL.
742 */
743static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
744{
745 return( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
746 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH ||
747 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECDSA ?
748 (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) :
749 NULL );
750}
751#endif /* MBEDTLS_ECP_C */
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200754/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200756 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757 *
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.
760 * \param key Input buffer to parse.
761 * The buffer must contain the input exactly, with no
762 * extra trailing material. For PEM, the buffer must
763 * contain a null-terminated string.
764 * \param keylen Size of \b key in bytes.
765 * For PEM data, this includes the terminating null byte,
766 * so \p keylen must be equal to `strlen(key) + 1`.
767 * \param pwd Optional password for decryption.
768 * Pass \c NULL if expecting a non-encrypted key.
769 * Pass a string of \p pwdlen bytes if expecting an encrypted
770 * key; a non-encrypted key will also be accepted.
771 * The empty password is not supported.
772 * \param pwdlen Size of the password in bytes.
773 * Ignored if \p pwd is \c NULL.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200774 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
775 * \param p_rng RNG parameter
Paul Bakker1a7550a2013-09-15 13:01:22 +0200776 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100777 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
779 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100780 *
781 * \note The key is also checked for correctness.
782 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200783 * \return 0 if successful, or a specific PK or PEM error code
784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200786 const unsigned char *key, size_t keylen,
787 const unsigned char *pwd, size_t pwdlen,
788 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
Paul Bakkerff3a5182013-09-16 22:42:19 +0200790/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200792 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500794 * \param ctx The PK context to fill. It must have been initialized
795 * but not set up.
796 * \param key Input buffer to parse.
797 * The buffer must contain the input exactly, with no
798 * extra trailing material. For PEM, the buffer must
799 * contain a null-terminated string.
800 * \param keylen Size of \b key in bytes.
801 * For PEM data, this includes the terminating null byte,
802 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100804 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
806 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100807 *
808 * \note The key is also checked for correctness.
809 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200810 * \return 0 if successful, or a specific PK or PEM error code
811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813 const unsigned char *key, size_t keylen );
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200816/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200818 * \brief Load and parse a private key
819 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500820 * \param ctx The PK context to fill. It must have been initialized
821 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200822 * \param path filename to read the private key from
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500823 * \param password Optional password to decrypt the file.
824 * Pass \c NULL if expecting a non-encrypted key.
825 * Pass a null-terminated string if expecting an encrypted
826 * key; a non-encrypted key will also be accepted.
827 * The empty password is not supported.
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200828 * \param f_rng RNG function, must not be \c NULL. Used for blinding.
829 * \param p_rng RNG parameter
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200830 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100831 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
833 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100834 *
835 * \note The key is also checked for correctness.
836 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200837 * \return 0 if successful, or a specific PK or PEM error code
838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200840 const char *path, const char *password,
841 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200842
Paul Bakkerff3a5182013-09-16 22:42:19 +0200843/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200844/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845 * \brief Load and parse a public key
846 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500847 * \param ctx The PK context to fill. It must have been initialized
848 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +0100849 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100851 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100852 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
853 * you need a specific key type, check the result with
854 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100855 *
856 * \note The key is also checked for correctness.
857 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858 * \return 0 if successful, or a specific PK or PEM error code
859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
861#endif /* MBEDTLS_FS_IO */
862#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200866 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
867 * Note: data is written at the end of the buffer! Use the
868 * return value to determine where you should start
869 * using the buffer
870 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500871 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200872 * \param buf buffer to write to
873 * \param size size of the buffer
874 *
875 * \return length of data written if successful, or a specific
876 * error code
877 */
Mateusz Starzyk32924532021-02-02 15:22:19 +0100878int mbedtls_pk_write_key_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200879
880/**
881 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
882 * Note: data is written at the end of the buffer! Use the
883 * return value to determine where you should start
884 * using the buffer
885 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500886 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200887 * \param buf buffer to write to
888 * \param size size of the buffer
889 *
890 * \return length of data written if successful, or a specific
891 * error code
892 */
Mateusz Starzyk32924532021-02-02 15:22:19 +0100893int mbedtls_pk_write_pubkey_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200896/**
897 * \brief Write a public key to a PEM string
898 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500899 * \param ctx PK context which must contain a valid public or private key.
900 * \param buf Buffer to write to. The output includes a
901 * terminating null byte.
902 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200903 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200904 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200905 */
Mateusz Starzyk32924532021-02-02 15:22:19 +0100906int mbedtls_pk_write_pubkey_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200907
908/**
909 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
910 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500911 * \param ctx PK context which must contain a valid private key.
912 * \param buf Buffer to write to. The output includes a
913 * terminating null byte.
914 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200915 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200916 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200917 */
Mateusz Starzyk32924532021-02-02 15:22:19 +0100918int mbedtls_pk_write_key_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_PEM_WRITE_C */
920#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200921
922/*
923 * WARNING: Low-level functions. You probably do not want to use these unless
924 * you are certain you do ;)
925 */
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200928/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929 * \brief Parse a SubjectPublicKeyInfo DER structure
930 *
931 * \param p the position in the ASN.1 data
932 * \param end end of the buffer
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500933 * \param pk The PK context to fill. It must have been initialized
934 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200935 *
936 * \return 0 if successful, or a specific PK error code
937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
939 mbedtls_pk_context *pk );
940#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200943/**
944 * \brief Write a subjectPublicKey to ASN.1 data
945 * Note: function works backwards in data buffer
946 *
947 * \param p reference to current position pointer
948 * \param start start of the buffer (for bounds-checking)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500949 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200950 *
951 * \return the length written or a negative error code
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
954 const mbedtls_pk_context *key );
955#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200956
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100957/*
958 * Internal module functions. You probably do not want to use these unless you
959 * know you do.
960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_FS_IO)
962int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100963#endif
964
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100965#if defined(MBEDTLS_USE_PSA_CRYPTO)
966/**
Neil Armstrong23143dc2022-04-21 11:33:54 +0200967 * \brief Turn an EC or RSA key into an opaque one.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100968 *
969 * \warning This is a temporary utility function for tests. It might
970 * change or be removed at any time without notice.
971 *
Neil Armstrong23143dc2022-04-21 11:33:54 +0200972 * \param pk Input: the EC or RSA key to import to a PSA key.
Gilles Peskine11392492019-05-27 14:53:19 +0200973 * Output: a PK context wrapping that PSA key.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200974 * \param key Output: a PSA key identifier.
Gilles Peskine11392492019-05-27 14:53:19 +0200975 * It's the caller's responsibility to call
Ronald Croncf56a0a2020-08-04 09:51:30 +0200976 * psa_destroy_key() on that key identifier after calling
Gilles Peskine11392492019-05-27 14:53:19 +0200977 * mbedtls_pk_free() on the PK context.
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200978 * \param alg The algorithm to allow for use with that key.
979 * \param usage The usage to allow for use with that key.
980 * \param alg2 The secondary algorithm to allow for use with that key.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100981 *
982 * \return \c 0 if successful.
983 * \return An Mbed TLS error code otherwise.
984 */
985int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100986 mbedtls_svc_key_id_t *key,
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200987 psa_algorithm_t alg,
988 psa_key_usage_t usage,
989 psa_algorithm_t alg2 );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100990#endif /* MBEDTLS_USE_PSA_CRYPTO */
991
Paul Bakkered27a042013-04-18 22:46:23 +0200992#ifdef __cplusplus
993}
994#endif
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_PK_H */