blob: 716070454c3d65a138ded1657dbc252d6750c5f0 [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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é-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered27a042013-04-18 22:46:23 +020023 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_PK_H
26#define MBEDTLS_PK_H
Paul Bakkered27a042013-04-18 22:46:23 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020029#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020033
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020034#include "md.h"
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020037#include "rsa.h"
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#include "ecp.h"
42#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020045#include "ecdsa.h"
46#endif
47
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010048#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
49 !defined(inline) && !defined(__cplusplus)
50#define inline __inline
51#endif
52
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020053#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnardeed55a42015-04-09 17:31:59 +020054#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
55#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
56#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
57#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
58#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
59#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
60#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
61#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
62#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
63#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
64#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
65#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020066#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The buffer contains a valid signature followed by more data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030067
68/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010069#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020070
Paul Bakkered27a042013-04-18 22:46:23 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
75/**
76 * \brief Public key types
77 */
78typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 MBEDTLS_PK_NONE=0,
80 MBEDTLS_PK_RSA,
81 MBEDTLS_PK_ECKEY,
82 MBEDTLS_PK_ECKEY_DH,
83 MBEDTLS_PK_ECDSA,
84 MBEDTLS_PK_RSA_ALT,
85 MBEDTLS_PK_RSASSA_PSS,
86} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020087
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020088/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020089 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020091 */
Dawid Drozd428cc522018-07-24 10:02:47 +020092typedef struct mbedtls_pk_rsassa_pss_options
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020095 int expected_salt_len;
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020098
99/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200100 * \brief Types for interfacing with the debug module
101 */
102typedef enum
103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_PK_DEBUG_NONE = 0,
105 MBEDTLS_PK_DEBUG_MPI,
106 MBEDTLS_PK_DEBUG_ECP,
107} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200108
109/**
110 * \brief Item to send to the debug module
111 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200112typedef struct mbedtls_pk_debug_item
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200115 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200116 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200118
119/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200121
122/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200123 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200126
127/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128 * \brief Public key container
129 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200130typedef struct mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131{
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200132 const mbedtls_pk_info_t * pk_info; /**< Public key information */
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200133 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200136#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200137/**
138 * \brief Context for resuming operations
139 */
140typedef struct
141{
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200142 const mbedtls_pk_info_t * pk_info; /**< Public key information */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200143 void * rs_ctx; /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200144} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200145#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200146/* Now we can declare functions that take a pointer to that */
147typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200148#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200149
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200150#if defined(MBEDTLS_RSA_C)
151/**
152 * Quick access to an RSA context inside a PK context.
153 *
154 * \warning You must make sure the PK context actually holds an RSA context
155 * before using this function!
156 */
157static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
158{
159 return( (mbedtls_rsa_context *) (pk).pk_ctx );
160}
161#endif /* MBEDTLS_RSA_C */
162
163#if defined(MBEDTLS_ECP_C)
164/**
165 * Quick access to an EC context inside a PK context.
166 *
167 * \warning You must make sure the PK context actually holds an EC context
168 * before using this function!
169 */
170static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
171{
172 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
173}
174#endif /* MBEDTLS_ECP_C */
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200177/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200178 * \brief Types for RSA-alt abstraction
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200181 const unsigned char *input, unsigned char *output,
182 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200184 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200186 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
188#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200189
190/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200191 * \brief Return information associated with the given PK type
192 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200193 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200194 *
195 * \return The PK info associated with the type or NULL if not found.
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200198
199/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100200 * \brief Initialize a #mbedtls_pk_context (as NONE).
201 *
202 * \param ctx The context to initialize.
203 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200206
207/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100208 * \brief Free the components of a #mbedtls_pk_context.
209 *
210 * \param ctx The context to clear.
211 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200215#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200216/**
217 * \brief Initialize a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100218 *
219 * \param ctx The context to initialize.
220 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200221 */
222void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
223
224/**
225 * \brief Free the components of a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100226 *
227 * \param ctx The context to clear.
228 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200229 */
230void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200231#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200232
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200233/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200234 * \brief Initialize a PK context with the information given
235 * and allocates the type-specific PK subcontext.
236 *
237 * \param ctx Context to initialize. Must be empty (type NONE).
238 * \param info Information to use
239 *
240 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200242 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200243 *
Paul Bakker42099c32014-01-27 11:45:49 +0100244 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200245 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200246 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200247int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200250/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200251 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200252 *
253 * \param ctx Context to initialize. Must be empty (type NONE).
254 * \param key RSA key pointer
255 * \param decrypt_func Decryption function
256 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200257 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200258 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200260 * context wasn't already initialized as RSA_ALT.
261 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200262 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200263 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200264int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
266 mbedtls_pk_rsa_alt_sign_func sign_func,
267 mbedtls_pk_rsa_alt_key_len_func key_len_func );
268#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200269
270/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200271 * \brief Get the size in bits of the underlying key
272 *
273 * \param ctx Context to use
274 *
275 * \return Key size in bits, or 0 on error
276 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200277size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200278
279/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200280 * \brief Get the length in bytes of the underlying key
281 * \param ctx Context to use
282 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200283 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200286{
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200287 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200288}
289
290/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200291 * \brief Tell if a context can do the operation given by type
292 *
293 * \param ctx Context to test
294 * \param type Target type
295 *
296 * \return 0 if context can't do the operations,
297 * 1 otherwise.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200300
301/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200302 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200303 *
304 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200305 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200306 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200307 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200308 * \param sig Signature to verify
309 * \param sig_len Signature length
310 *
311 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200312 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
313 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200314 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200315 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200316 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200318 * to verify RSASSA_PSS signatures.
319 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200320 * \note If hash_len is 0, then the length associated with md_alg
321 * is used instead, or an error returned if it is invalid.
322 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200326 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200327 const unsigned char *sig, size_t sig_len );
328
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200329/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200330 * \brief Restartable version of \c mbedtls_pk_verify()
331 *
332 * \note Performs the same job as \c mbedtls_pk_verify(), but can
333 * return early and restart according to the limit set with
334 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
335 * operations. For RSA, same as \c mbedtls_pk_verify().
336 *
337 * \param ctx PK context to use
338 * \param md_alg Hash algorithm used (see notes)
339 * \param hash Hash of the message to sign
340 * \param hash_len Hash length or 0 (see notes)
341 * \param sig Signature to verify
342 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200343 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200344 *
345 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200346 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200347 * operations was reached: see \c mbedtls_ecp_set_max_ops().
348 */
349int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
350 mbedtls_md_type_t md_alg,
351 const unsigned char *hash, size_t hash_len,
352 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200353 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200354
355/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200356 * \brief Verify signature, with options.
357 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200358 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200359 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360 * \param options Pointer to type-specific options, or NULL
361 * \param ctx PK context to use
362 * \param md_alg Hash algorithm used (see notes)
363 * \param hash Hash of the message to sign
364 * \param hash_len Hash length or 0 (see notes)
365 * \param sig Signature to verify
366 * \param sig_len Signature length
367 *
368 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200369 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200370 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200371 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
372 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200373 * or a specific error code.
374 *
375 * \note If hash_len is 0, then the length associated with md_alg
376 * is used instead, or an error returned if it is invalid.
377 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200379 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
381 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200382 * otherwise it must be NULL.
383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
385 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200386 const unsigned char *hash, size_t hash_len,
387 const unsigned char *sig, size_t sig_len );
388
389/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200390 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200391 *
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100392 * \param ctx PK context to use - must hold a private key
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200393 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200394 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200395 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200396 * \param sig Place to write the signature
397 * \param sig_len Number of bytes written
398 * \param f_rng RNG function
399 * \param p_rng RNG parameter
400 *
401 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200402 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200403 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
404 * There is no interface in the PK module to make RSASSA-PSS
405 * signatures yet.
406 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200407 * \note If hash_len is 0, then the length associated with md_alg
408 * is used instead, or an error returned if it is invalid.
409 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
411 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200412 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200414 const unsigned char *hash, size_t hash_len,
415 unsigned char *sig, size_t *sig_len,
416 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
417
418/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200419 * \brief Restartable version of \c mbedtls_pk_sign()
420 *
421 * \note Performs the same job as \c mbedtls_pk_sign(), but can
422 * return early and restart according to the limit set with
423 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
424 * operations. For RSA, same as \c mbedtls_pk_sign().
425 *
426 * \param ctx PK context to use - must hold a private key
427 * \param md_alg Hash algorithm used (see notes)
428 * \param hash Hash of the message to sign
429 * \param hash_len Hash length or 0 (see notes)
430 * \param sig Place to write the signature
431 * \param sig_len Number of bytes written
432 * \param f_rng RNG function
433 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200434 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200435 *
436 * \return See \c mbedtls_pk_sign(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200437 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200438 * operations was reached: see \c mbedtls_ecp_set_max_ops().
439 */
440int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
441 mbedtls_md_type_t md_alg,
442 const unsigned char *hash, size_t hash_len,
443 unsigned char *sig, size_t *sig_len,
444 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200445 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200446
447/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200448 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200449 *
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100450 * \param ctx PK context to use - must hold a private key
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200451 * \param input Input to decrypt
452 * \param ilen Input size
453 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200454 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200455 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200456 * \param f_rng RNG function
457 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200458 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200459 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
460 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200461 * \return 0 on success, or a specific error code.
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200464 const unsigned char *input, size_t ilen,
465 unsigned char *output, size_t *olen, size_t osize,
466 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
467
468/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200469 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200470 *
471 * \param ctx PK context to use
472 * \param input Message to encrypt
473 * \param ilen Message size
474 * \param output Encrypted output
475 * \param olen Encrypted output length
476 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200477 * \param f_rng RNG function
478 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200479 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200480 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
481 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200482 * \return 0 on success, or a specific error code.
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200485 const unsigned char *input, size_t ilen,
486 unsigned char *output, size_t *olen, size_t osize,
487 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
488
489/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100490 * \brief Check if a public-private pair of keys matches.
491 *
492 * \param pub Context holding a public key.
493 * \param prv Context holding a private (and public) key.
494 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100498
499/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200500 * \brief Export debug information
501 *
502 * \param ctx Context to use
503 * \param items Place to write debug items
504 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200508
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200509/**
510 * \brief Access the type name
511 *
512 * \param ctx Context to use
513 *
514 * \return Type name on success, or "invalid PK"
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200517
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200518/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200519 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200520 *
521 * \param ctx Context to use
522 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 * \return Type on success, or MBEDTLS_PK_NONE
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200528/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200529/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200530 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531 *
532 * \param ctx key to be initialized
Gilles Peskine159171b2018-12-19 17:03:28 +0100533 * \param key Input buffer to parse.
534 * The buffer must contain the input exactly, with no
535 * extra trailing material. For PEM, the buffer must
536 * contain a null-terminated string.
537 * \param keylen Size of \b key in bytes.
538 * For PEM data, this includes the terminating null byte,
539 * so \p keylen must be equal to `strlen(key) + 1`.
Gilles Peskined54b9752018-12-19 17:12:01 +0100540 * \param pwd Optional password for decryption.
541 * Pass \c NULL if expecting a non-encrypted key.
542 * Pass a string of \p pwdlen bytes if expecting an encrypted
543 * key; a non-encrypted key will also be accepted.
544 * The empty password is not supported.
545 * \param pwdlen Size of the password in bytes.
546 * Ignored if \p pwd is \c NULL.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200547 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100548 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
550 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100551 *
552 * \note The key is also checked for correctness.
553 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200554 * \return 0 if successful, or a specific PK or PEM error code
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557 const unsigned char *key, size_t keylen,
558 const unsigned char *pwd, size_t pwdlen );
559
Paul Bakkerff3a5182013-09-16 22:42:19 +0200560/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200561/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200562 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200563 *
564 * \param ctx key to be initialized
Gilles Peskine159171b2018-12-19 17:03:28 +0100565 * \param key Input buffer to parse.
566 * The buffer must contain the input exactly, with no
567 * extra trailing material. For PEM, the buffer must
568 * contain a null-terminated string.
569 * \param keylen Size of \b key in bytes.
570 * For PEM data, this includes the terminating null byte,
571 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100573 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
575 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100576 *
577 * \note The key is also checked for correctness.
578 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579 * \return 0 if successful, or a specific PK or PEM error code
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582 const unsigned char *key, size_t keylen );
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200585/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200587 * \brief Load and parse a private key
588 *
589 * \param ctx key to be initialized
590 * \param path filename to read the private key from
Gilles Peskined54b9752018-12-19 17:12:01 +0100591 * \param password Optional password to decrypt the file.
592 * Pass \c NULL if expecting a non-encrypted key.
593 * Pass a null-terminated string if expecting an encrypted
594 * key; a non-encrypted key will also be accepted.
595 * The empty password is not supported.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200596 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100597 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
599 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100600 *
601 * \note The key is also checked for correctness.
602 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200603 * \return 0 if successful, or a specific PK or PEM error code
604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200606 const char *path, const char *password );
607
Paul Bakkerff3a5182013-09-16 22:42:19 +0200608/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200609/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200610 * \brief Load and parse a public key
611 *
612 * \param ctx key to be initialized
Simon Butcher295639b2016-05-10 19:39:36 +0100613 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100615 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100616 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
617 * you need a specific key type, check the result with
618 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100619 *
620 * \note The key is also checked for correctness.
621 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622 * \return 0 if successful, or a specific PK or PEM error code
623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
625#endif /* MBEDTLS_FS_IO */
626#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200630 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
631 * Note: data is written at the end of the buffer! Use the
632 * return value to determine where you should start
633 * using the buffer
634 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100635 * \param ctx private to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200636 * \param buf buffer to write to
637 * \param size size of the buffer
638 *
639 * \return length of data written if successful, or a specific
640 * error code
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200643
644/**
645 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
646 * Note: data is written at the end of the buffer! Use the
647 * return value to determine where you should start
648 * using the buffer
649 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100650 * \param ctx public key to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200651 * \param buf buffer to write to
652 * \param size size of the buffer
653 *
654 * \return length of data written if successful, or a specific
655 * error code
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200660/**
661 * \brief Write a public key to a PEM string
662 *
Gilles Peskine159171b2018-12-19 17:03:28 +0100663 * \param ctx Context containing the public key to write.
664 * \param buf Buffer to write to. The output includes a
665 * terminating null byte.
666 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200667 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200668 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200671
672/**
673 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
674 *
Gilles Peskine159171b2018-12-19 17:03:28 +0100675 * \param ctx Context containing the private key to write.
676 * \param buf Buffer to write to. The output includes a
677 * terminating null byte.
678 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200679 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200680 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
683#endif /* MBEDTLS_PEM_WRITE_C */
684#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200685
686/*
687 * WARNING: Low-level functions. You probably do not want to use these unless
688 * you are certain you do ;)
689 */
690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200692/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693 * \brief Parse a SubjectPublicKeyInfo DER structure
694 *
695 * \param p the position in the ASN.1 data
696 * \param end end of the buffer
697 * \param pk the key to fill
698 *
699 * \return 0 if successful, or a specific PK error code
700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
702 mbedtls_pk_context *pk );
703#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200706/**
707 * \brief Write a subjectPublicKey to ASN.1 data
708 * Note: function works backwards in data buffer
709 *
710 * \param p reference to current position pointer
711 * \param start start of the buffer (for bounds-checking)
712 * \param key public key to write away
713 *
714 * \return the length written or a negative error code
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
717 const mbedtls_pk_context *key );
718#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200719
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100720/*
721 * Internal module functions. You probably do not want to use these unless you
722 * know you do.
723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_FS_IO)
725int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100726#endif
727
Paul Bakkered27a042013-04-18 22:46:23 +0200728#ifdef __cplusplus
729}
730#endif
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_PK_H */