blob: 2ed9c07b6d4cc1f7b020fdb41a46c4c01d1a0c7d [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
Jarno Lamsab1760922019-04-18 15:58:34 +030048#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker496b83f2019-08-20 13:33:49 +010049#include "tinycrypt/ecc.h"
Jarno Lamsab1760922019-04-18 15:58:34 +030050#endif
51
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +020052#if defined(MBEDTLS_PK_SINGLE_TYPE)
53#include "pk_internal.h"
54#endif
55
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010056#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
57 !defined(inline) && !defined(__cplusplus)
58#define inline __inline
59#endif
60
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020061#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnardeed55a42015-04-09 17:31:59 +020062#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
63#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
64#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
65#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
66#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
67#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
68#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
69#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
70#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
71#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
72#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
73#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020074#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 +030075
76/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010077#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
Paul Bakkered27a042013-04-18 22:46:23 +020079#ifdef __cplusplus
80extern "C" {
81#endif
82
83/**
84 * \brief Public key types
85 */
86typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 MBEDTLS_PK_NONE=0,
88 MBEDTLS_PK_RSA,
89 MBEDTLS_PK_ECKEY,
90 MBEDTLS_PK_ECKEY_DH,
91 MBEDTLS_PK_ECDSA,
92 MBEDTLS_PK_RSA_ALT,
93 MBEDTLS_PK_RSASSA_PSS,
94} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020095
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020096/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020097 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020099 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200100typedef struct mbedtls_pk_rsassa_pss_options
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200103 int expected_salt_len;
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200106
107/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200108 * \brief Types for interfacing with the debug module
109 */
110typedef enum
111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_PK_DEBUG_NONE = 0,
113 MBEDTLS_PK_DEBUG_MPI,
114 MBEDTLS_PK_DEBUG_ECP,
115} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200116
117/**
118 * \brief Item to send to the debug module
119 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200120typedef struct mbedtls_pk_debug_item
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200123 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200124 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200126
127/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200129
130/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200131 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200132 */
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200133#if defined(MBEDTLS_PK_SINGLE_TYPE)
134typedef enum {
135 MBEDTLS_PK_INVALID_HANDLE,
136 MBEDTLS_PK_UNIQUE_VALID_HANDLE,
137} mbedtls_pk_handle_t;
138#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200140typedef const mbedtls_pk_info_t *mbedtls_pk_handle_t;
141#define MBEDTLS_PK_INVALID_HANDLE ( (mbedtls_pk_handle_t) NULL )
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200142#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143
Jarno Lamsab1760922019-04-18 15:58:34 +0300144#if defined(MBEDTLS_USE_TINYCRYPT)
145typedef struct
146{
147 uint8_t private_key[NUM_ECC_BYTES];
148 uint8_t public_key[2*NUM_ECC_BYTES];
149} mbedtls_uecc_keypair;
150#endif
151
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200152/**
153 * \brief Public key container
154 */
155typedef struct mbedtls_pk_context
156{
157#if defined(MBEDTLS_PK_SINGLE_TYPE)
158 /* This is an array to make access to it more uniform with the case where
159 * it's a pointer to void - either way it needs casting before use. */
160 unsigned char pk_ctx[sizeof(
161 MBEDTLS_PK_INFO_CONTEXT( MBEDTLS_PK_SINGLE_TYPE ) )];
162#else
163 mbedtls_pk_handle_t pk_info; /**< Public key information */
164 void * pk_ctx; /**< Underlying public key context */
165#endif
166} mbedtls_pk_context;
167
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200168#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200169/**
170 * \brief Context for resuming operations
171 */
172typedef struct
173{
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200174 mbedtls_pk_handle_t pk_info; /**< Public key information */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200175 void * rs_ctx; /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200176} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200177#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200178/* Now we can declare functions that take a pointer to that */
179typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200180#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200181
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200182#if defined(MBEDTLS_RSA_C)
183/**
184 * Quick access to an RSA context inside a PK context.
185 *
186 * \warning You must make sure the PK context actually holds an RSA context
187 * before using this function!
188 */
189static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
190{
191 return( (mbedtls_rsa_context *) (pk).pk_ctx );
192}
193#endif /* MBEDTLS_RSA_C */
194
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300195#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200196#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Hanno Beckerda779712019-08-21 13:22:59 +0100197static inline mbedtls_uecc_keypair *mbedtls_pk_uecc( const mbedtls_pk_context pk )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300198{
199 return( (mbedtls_uecc_keypair *) (pk).pk_ctx );
200}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200201#else
202/* Go with a macro in order to avoid making a copy of the struct (the argument
203 * is not a pointer so it's passed by value) and then returning an address
204 * inside that copy, which would be undefined behaviour. */
205#define mbedtls_pk_uecc( pk ) ( (mbedtls_uecc_keypair *) (pk).pk_ctx )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300206#endif
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200207#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300208
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200209#if defined(MBEDTLS_ECP_C)
210/**
211 * Quick access to an EC context inside a PK context.
212 *
213 * \warning You must make sure the PK context actually holds an EC context
214 * before using this function!
215 */
216static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
217{
218 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
219}
220#endif /* MBEDTLS_ECP_C */
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200223/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200224 * \brief Types for RSA-alt abstraction
225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200227 const unsigned char *input, unsigned char *output,
228 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200230 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200232 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
234#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200235
236/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200237 * \brief Return information associated with the given PK type
238 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200239 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200240 *
241 * \return The PK info associated with the type or NULL if not found.
242 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200243mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200244
245/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100246 * \brief Initialize a #mbedtls_pk_context (as NONE).
247 *
248 * \param ctx The context to initialize.
249 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200252
253/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100254 * \brief Free the components of a #mbedtls_pk_context.
255 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100256 * \param ctx The context to clear. It must have been initialized.
Gilles Peskinea310b412018-12-19 00:51:21 +0100257 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200260
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200261#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200262/**
263 * \brief Initialize a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100264 *
265 * \param ctx The context to initialize.
266 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200267 */
268void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
269
270/**
271 * \brief Free the components of a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100272 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100273 * \param ctx The context to clear. It must have been initialized.
Gilles Peskinea310b412018-12-19 00:51:21 +0100274 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200275 */
276void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200277#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200278
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200279/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200280 * \brief Initialize a PK context with the information given
281 * and allocates the type-specific PK subcontext.
282 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100283 * \param ctx Context to initialize. It must not have been set
284 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200285 * \param info Information to use
286 *
287 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200290 *
Paul Bakker42099c32014-01-27 11:45:49 +0100291 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200292 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200293 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200294int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200297/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200298 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200299 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100300 * \param ctx Context to initialize. It must not have been set
301 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200302 * \param key RSA key pointer
303 * \param decrypt_func Decryption function
304 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200305 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200306 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200308 * context wasn't already initialized as RSA_ALT.
309 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200310 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200311 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200312int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
314 mbedtls_pk_rsa_alt_sign_func sign_func,
315 mbedtls_pk_rsa_alt_key_len_func key_len_func );
316#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200317
318/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200319 * \brief Get the size in bits of the underlying key
320 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100321 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200322 *
323 * \return Key size in bits, or 0 on error
324 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200325size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200326
327/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200328 * \brief Get the length in bytes of the underlying key
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100329 *
330 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200331 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200332 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200335{
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200336 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200337}
338
339/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200340 * \brief Tell if a context can do the operation given by type
341 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100342 * \param ctx The context to query. It must have been initialized.
343 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200344 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100345 * \return 1 if the context can do operations on the given type.
346 * \return 0 if the context cannot do the operations on the given
347 * type. This is always the case for a context that has
348 * been initialized but not set up, or that has been
349 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200352
353/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200354 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200355 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100356 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200357 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200358 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200359 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200360 * \param sig Signature to verify
361 * \param sig_len Signature length
362 *
363 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200364 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
365 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200366 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200367 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200368 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200370 * to verify RSASSA_PSS signatures.
371 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200372 * \note If hash_len is 0, then the length associated with md_alg
373 * is used instead, or an error returned if it is invalid.
374 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200378 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200379 const unsigned char *sig, size_t sig_len );
380
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200381/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200382 * \brief Restartable version of \c mbedtls_pk_verify()
383 *
384 * \note Performs the same job as \c mbedtls_pk_verify(), but can
385 * return early and restart according to the limit set with
386 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
387 * operations. For RSA, same as \c mbedtls_pk_verify().
388 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100389 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200390 * \param md_alg Hash algorithm used (see notes)
391 * \param hash Hash of the message to sign
392 * \param hash_len Hash length or 0 (see notes)
393 * \param sig Signature to verify
394 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200395 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200396 *
397 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200398 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200399 * operations was reached: see \c mbedtls_ecp_set_max_ops().
400 */
401int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
402 mbedtls_md_type_t md_alg,
403 const unsigned char *hash, size_t hash_len,
404 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200405 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200406
407/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200408 * \brief Verify signature, with options.
409 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200410 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200411 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200412 * \param options Pointer to type-specific options, or NULL
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100413 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200414 * \param md_alg Hash algorithm used (see notes)
415 * \param hash Hash of the message to sign
416 * \param hash_len Hash length or 0 (see notes)
417 * \param sig Signature to verify
418 * \param sig_len Signature length
419 *
420 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200421 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200422 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200423 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
424 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200425 * or a specific error code.
426 *
427 * \note If hash_len is 0, then the length associated with md_alg
428 * is used instead, or an error returned if it is invalid.
429 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200431 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
433 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200434 * otherwise it must be NULL.
435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
437 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200438 const unsigned char *hash, size_t hash_len,
439 const unsigned char *sig, size_t sig_len );
440
441/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200442 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200443 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100444 * \param ctx The PK context to use. It must have been set up
445 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200446 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200447 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200448 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200449 * \param sig Place to write the signature
450 * \param sig_len Number of bytes written
451 * \param f_rng RNG function
452 * \param p_rng RNG parameter
453 *
454 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200455 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200456 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
457 * There is no interface in the PK module to make RSASSA-PSS
458 * signatures yet.
459 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200460 * \note If hash_len is 0, then the length associated with md_alg
461 * is used instead, or an error returned if it is invalid.
462 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
464 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
k-stachowiak4e36da32019-05-31 20:16:50 +0200465 *
466 * \note In order to ensure enough space for the signature, the
467 * \p sig buffer size must be of at least
468 * `max(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)` bytes.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200471 const unsigned char *hash, size_t hash_len,
472 unsigned char *sig, size_t *sig_len,
473 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
474
475/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200476 * \brief Restartable version of \c mbedtls_pk_sign()
477 *
478 * \note Performs the same job as \c mbedtls_pk_sign(), but can
479 * return early and restart according to the limit set with
480 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
481 * operations. For RSA, same as \c mbedtls_pk_sign().
482 *
k-stachowiak4e36da32019-05-31 20:16:50 +0200483 * \note In order to ensure enough space for the signature, the
484 * \p sig buffer size must be of at least
485 * `max(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)` bytes.
486 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100487 * \param ctx The PK context to use. It must have been set up
488 * with a private key.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200489 * \param md_alg Hash algorithm used (see notes)
490 * \param hash Hash of the message to sign
491 * \param hash_len Hash length or 0 (see notes)
492 * \param sig Place to write the signature
493 * \param sig_len Number of bytes written
494 * \param f_rng RNG function
495 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200496 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200497 *
498 * \return See \c mbedtls_pk_sign(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200499 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200500 * operations was reached: see \c mbedtls_ecp_set_max_ops().
501 */
502int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
503 mbedtls_md_type_t md_alg,
504 const unsigned char *hash, size_t hash_len,
505 unsigned char *sig, size_t *sig_len,
506 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200507 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200508
509/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200510 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200511 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100512 * \param ctx The PK context to use. It must have been set up
513 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200514 * \param input Input to decrypt
515 * \param ilen Input size
516 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200517 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200518 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200519 * \param f_rng RNG function
520 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200522 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
523 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200524 * \return 0 on success, or a specific error code.
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527 const unsigned char *input, size_t ilen,
528 unsigned char *output, size_t *olen, size_t osize,
529 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
530
531/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200532 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200533 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100534 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200535 * \param input Message to encrypt
536 * \param ilen Message size
537 * \param output Encrypted output
538 * \param olen Encrypted output length
539 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200540 * \param f_rng RNG function
541 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200542 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200543 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
544 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200545 * \return 0 on success, or a specific error code.
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200548 const unsigned char *input, size_t ilen,
549 unsigned char *output, size_t *olen, size_t osize,
550 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
551
552/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100553 * \brief Check if a public-private pair of keys matches.
554 *
555 * \param pub Context holding a public key.
556 * \param prv Context holding a private (and public) key.
557 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100561
562/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200563 * \brief Export debug information
564 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100565 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200566 * \param items Place to write debug items
567 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200571
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200572/**
573 * \brief Access the type name
574 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100575 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200576 *
577 * \return Type name on success, or "invalid PK"
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200580
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200581/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200582 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200583 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100584 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200585 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100586 * \return Type on success.
587 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200592/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200594 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100596 * \param ctx The PK context to fill. It must have been initialized
597 * but not set up.
Gilles Peskine159171b2018-12-19 17:03:28 +0100598 * \param key Input buffer to parse.
599 * The buffer must contain the input exactly, with no
600 * extra trailing material. For PEM, the buffer must
601 * contain a null-terminated string.
602 * \param keylen Size of \b key in bytes.
603 * For PEM data, this includes the terminating null byte,
604 * so \p keylen must be equal to `strlen(key) + 1`.
Gilles Peskined54b9752018-12-19 17:12:01 +0100605 * \param pwd Optional password for decryption.
606 * Pass \c NULL if expecting a non-encrypted key.
607 * Pass a string of \p pwdlen bytes if expecting an encrypted
608 * key; a non-encrypted key will also be accepted.
609 * The empty password is not supported.
610 * \param pwdlen Size of the password in bytes.
611 * Ignored if \p pwd is \c NULL.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100613 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
615 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100616 *
617 * \note The key is also checked for correctness.
618 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619 * \return 0 if successful, or a specific PK or PEM error code
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622 const unsigned char *key, size_t keylen,
623 const unsigned char *pwd, size_t pwdlen );
624
Paul Bakkerff3a5182013-09-16 22:42:19 +0200625/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200626/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200627 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100629 * \param ctx The PK context to fill. It must have been initialized
630 * but not set up.
Gilles Peskine159171b2018-12-19 17:03:28 +0100631 * \param key Input buffer to parse.
632 * The buffer must contain the input exactly, with no
633 * extra trailing material. For PEM, the buffer must
634 * contain a null-terminated string.
635 * \param keylen Size of \b key in bytes.
636 * For PEM data, this includes the terminating null byte,
637 * so \p keylen must be equal to `strlen(key) + 1`.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100639 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
641 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100642 *
643 * \note The key is also checked for correctness.
644 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645 * \return 0 if successful, or a specific PK or PEM error code
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648 const unsigned char *key, size_t keylen );
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200651/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200653 * \brief Load and parse a private key
654 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100655 * \param ctx The PK context to fill. It must have been initialized
656 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200657 * \param path filename to read the private key from
Gilles Peskined54b9752018-12-19 17:12:01 +0100658 * \param password Optional password to decrypt the file.
659 * Pass \c NULL if expecting a non-encrypted key.
660 * Pass a null-terminated string if expecting an encrypted
661 * key; a non-encrypted key will also be accepted.
662 * The empty password is not supported.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200663 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100664 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
666 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100667 *
668 * \note The key is also checked for correctness.
669 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200670 * \return 0 if successful, or a specific PK or PEM error code
671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200673 const char *path, const char *password );
674
Paul Bakkerff3a5182013-09-16 22:42:19 +0200675/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200676/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200677 * \brief Load and parse a public key
678 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100679 * \param ctx The PK context to fill. It must have been initialized
680 * but not set up.
Simon Butcher295639b2016-05-10 19:39:36 +0100681 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100683 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100684 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
685 * you need a specific key type, check the result with
686 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100687 *
688 * \note The key is also checked for correctness.
689 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690 * \return 0 if successful, or a specific PK or PEM error code
691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
693#endif /* MBEDTLS_FS_IO */
694#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200698 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
699 * Note: data is written at the end of the buffer! Use the
700 * return value to determine where you should start
701 * using the buffer
702 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100703 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200704 * \param buf buffer to write to
705 * \param size size of the buffer
706 *
707 * \return length of data written if successful, or a specific
708 * error code
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200711
712/**
713 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
714 * Note: data is written at the end of the buffer! Use the
715 * return value to determine where you should start
716 * using the buffer
717 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100718 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200719 * \param buf buffer to write to
720 * \param size size of the buffer
721 *
722 * \return length of data written if successful, or a specific
723 * error code
724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200728/**
729 * \brief Write a public key to a PEM string
730 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100731 * \param ctx PK context which must contain a valid public or private key.
Gilles Peskine159171b2018-12-19 17:03:28 +0100732 * \param buf Buffer to write to. The output includes a
733 * terminating null byte.
734 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200735 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200736 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200739
740/**
741 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
742 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100743 * \param ctx PK context which must contain a valid private key.
Gilles Peskine159171b2018-12-19 17:03:28 +0100744 * \param buf Buffer to write to. The output includes a
745 * terminating null byte.
746 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200747 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200748 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
751#endif /* MBEDTLS_PEM_WRITE_C */
752#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200753
754/*
755 * WARNING: Low-level functions. You probably do not want to use these unless
756 * you are certain you do ;)
757 */
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200760/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 * \brief Parse a SubjectPublicKeyInfo DER structure
762 *
763 * \param p the position in the ASN.1 data
764 * \param end end of the buffer
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100765 * \param pk The PK context to fill. It must have been initialized
766 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200767 *
768 * \return 0 if successful, or a specific PK error code
769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
771 mbedtls_pk_context *pk );
772#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200775/**
776 * \brief Write a subjectPublicKey to ASN.1 data
777 * Note: function works backwards in data buffer
778 *
779 * \param p reference to current position pointer
780 * \param start start of the buffer (for bounds-checking)
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100781 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200782 *
783 * \return the length written or a negative error code
784 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
786 const mbedtls_pk_context *key );
787#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200788
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100789/*
790 * Internal module functions. You probably do not want to use these unless you
791 * know you do.
792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_FS_IO)
794int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100795#endif
796
Paul Bakkered27a042013-04-18 22:46:23 +0200797#ifdef __cplusplus
798}
799#endif
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_PK_H */