blob: c636c3d53e60e56399c68456bd5995f647925686 [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkered27a042013-04-18 22:46:23 +020027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
Paul Bakkered27a042013-04-18 22:46:23 +020048 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#ifndef MBEDTLS_PK_H
51#define MBEDTLS_PK_H
Paul Bakkered27a042013-04-18 22:46:23 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020054#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020058
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020059#include "md.h"
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020062#include "rsa.h"
63#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020066#include "ecp.h"
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020070#include "ecdsa.h"
71#endif
72
Manuel Pégourié-Gonnard2ac9c602015-10-05 16:18:23 +010073#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
74 !defined(inline) && !defined(__cplusplus)
75#define inline __inline
76#endif
77
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020078#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnardeed55a42015-04-09 17:31:59 +020079#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
80#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
81#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
82#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
83#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
84#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
85#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
86#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
87#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
88#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
89#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
90#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020091#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The buffer contains a valid signature followed by more data. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010092#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020093
Paul Bakkered27a042013-04-18 22:46:23 +020094#ifdef __cplusplus
95extern "C" {
96#endif
97
98/**
99 * \brief Public key types
100 */
101typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 MBEDTLS_PK_NONE=0,
103 MBEDTLS_PK_RSA,
104 MBEDTLS_PK_ECKEY,
105 MBEDTLS_PK_ECKEY_DH,
106 MBEDTLS_PK_ECDSA,
107 MBEDTLS_PK_RSA_ALT,
108 MBEDTLS_PK_RSASSA_PSS,
109} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +0200110
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200111/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200112 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200114 */
115typedef struct
116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200118 int expected_salt_len;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200121
122/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200123 * \brief Types for interfacing with the debug module
124 */
125typedef enum
126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 MBEDTLS_PK_DEBUG_NONE = 0,
128 MBEDTLS_PK_DEBUG_MPI,
129 MBEDTLS_PK_DEBUG_ECP,
130} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200131
132/**
133 * \brief Item to send to the debug module
134 */
135typedef struct
136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200138 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200139 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200141
142/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200144
145/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200146 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200149
150/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200151 * \brief Public key container
152 */
153typedef struct
154{
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200155 const mbedtls_pk_info_t * pk_info; /**< Public key informations */
156 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200158
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200159#if defined(MBEDTLS_RSA_C)
160/**
161 * Quick access to an RSA context inside a PK context.
162 *
163 * \warning You must make sure the PK context actually holds an RSA context
164 * before using this function!
165 */
166static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
167{
168 return( (mbedtls_rsa_context *) (pk).pk_ctx );
169}
170#endif /* MBEDTLS_RSA_C */
171
172#if defined(MBEDTLS_ECP_C)
173/**
174 * Quick access to an EC context inside a PK context.
175 *
176 * \warning You must make sure the PK context actually holds an EC context
177 * before using this function!
178 */
179static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
180{
181 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
182}
183#endif /* MBEDTLS_ECP_C */
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200186/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200187 * \brief Types for RSA-alt abstraction
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200190 const unsigned char *input, unsigned char *output,
191 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200193 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
197#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200198
199/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200200 * \brief Return information associated with the given PK type
201 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200202 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200203 *
204 * \return The PK info associated with the type or NULL if not found.
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200207
208/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 * \brief Initialize a mbedtls_pk_context (as NONE)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200212
213/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 * \brief Free a mbedtls_pk_context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200217
218/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200219 * \brief Initialize a PK context with the information given
220 * and allocates the type-specific PK subcontext.
221 *
222 * \param ctx Context to initialize. Must be empty (type NONE).
223 * \param info Information to use
224 *
225 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200227 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200228 *
Paul Bakker42099c32014-01-27 11:45:49 +0100229 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200230 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200231 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200232int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200235/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200236 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200237 *
238 * \param ctx Context to initialize. Must be empty (type NONE).
239 * \param key RSA key pointer
240 * \param decrypt_func Decryption function
241 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200242 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200243 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200245 * context wasn't already initialized as RSA_ALT.
246 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200247 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200248 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200249int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
251 mbedtls_pk_rsa_alt_sign_func sign_func,
252 mbedtls_pk_rsa_alt_key_len_func key_len_func );
253#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200254
255/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200256 * \brief Get the size in bits of the underlying key
257 *
258 * \param ctx Context to use
259 *
260 * \return Key size in bits, or 0 on error
261 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200262size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200263
264/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200265 * \brief Get the length in bytes of the underlying key
266 * \param ctx Context to use
267 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200268 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200271{
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200272 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200273}
274
275/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200276 * \brief Tell if a context can do the operation given by type
277 *
278 * \param ctx Context to test
279 * \param type Target type
280 *
281 * \return 0 if context can't do the operations,
282 * 1 otherwise.
283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200285
286/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200287 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200288 *
289 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200290 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200291 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200292 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200293 * \param sig Signature to verify
294 * \param sig_len Signature length
295 *
296 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200297 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
298 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200299 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200300 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200301 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200303 * to verify RSASSA_PSS signatures.
304 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200305 * \note If hash_len is 0, then the length associated with md_alg
306 * is used instead, or an error returned if it is invalid.
307 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200311 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200312 const unsigned char *sig, size_t sig_len );
313
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200314/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200315 * \brief Verify signature, with options.
316 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200317 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200318 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200319 * \param options Pointer to type-specific options, or NULL
320 * \param ctx PK context to use
321 * \param md_alg Hash algorithm used (see notes)
322 * \param hash Hash of the message to sign
323 * \param hash_len Hash length or 0 (see notes)
324 * \param sig Signature to verify
325 * \param sig_len Signature length
326 *
327 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200328 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200329 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200330 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
331 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200332 * or a specific error code.
333 *
334 * \note If hash_len is 0, then the length associated with md_alg
335 * is used instead, or an error returned if it is invalid.
336 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200338 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
340 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341 * otherwise it must be NULL.
342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
344 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200345 const unsigned char *hash, size_t hash_len,
346 const unsigned char *sig, size_t sig_len );
347
348/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200349 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200350 *
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100351 * \param ctx PK context to use - must hold a private key
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200352 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200353 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200354 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355 * \param sig Place to write the signature
356 * \param sig_len Number of bytes written
357 * \param f_rng RNG function
358 * \param p_rng RNG parameter
359 *
360 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200361 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200362 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
363 * There is no interface in the PK module to make RSASSA-PSS
364 * signatures yet.
365 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200366 * \note If hash_len is 0, then the length associated with md_alg
367 * is used instead, or an error returned if it is invalid.
368 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
370 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
k-stachowiakeee98e92019-05-31 20:16:50 +0200371 *
372 * \note In order to ensure enough space for the signature, the
373 * \p sig buffer size must be of at least
374 * `max(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)` bytes.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200377 const unsigned char *hash, size_t hash_len,
378 unsigned char *sig, size_t *sig_len,
379 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
380
381/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200382 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200383 *
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100384 * \param ctx PK context to use - must hold a private key
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200385 * \param input Input to decrypt
386 * \param ilen Input size
387 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200388 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200389 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200390 * \param f_rng RNG function
391 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200392 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200393 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
394 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200395 * \return 0 on success, or a specific error code.
396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200398 const unsigned char *input, size_t ilen,
399 unsigned char *output, size_t *olen, size_t osize,
400 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
401
402/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200403 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200404 *
405 * \param ctx PK context to use
406 * \param input Message to encrypt
407 * \param ilen Message size
408 * \param output Encrypted output
409 * \param olen Encrypted output length
410 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200411 * \param f_rng RNG function
412 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200413 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200414 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
415 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200416 * \return 0 on success, or a specific error code.
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200419 const unsigned char *input, size_t ilen,
420 unsigned char *output, size_t *olen, size_t osize,
421 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
422
423/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100424 * \brief Check if a public-private pair of keys matches.
425 *
426 * \param pub Context holding a public key.
427 * \param prv Context holding a private (and public) key.
428 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100432
433/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200434 * \brief Export debug information
435 *
436 * \param ctx Context to use
437 * \param items Place to write debug items
438 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200442
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200443/**
444 * \brief Access the type name
445 *
446 * \param ctx Context to use
447 *
448 * \return Type name on success, or "invalid PK"
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200451
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200452/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200453 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200454 *
455 * \param ctx Context to use
456 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 * \return Type on success, or MBEDTLS_PK_NONE
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200462/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200463/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200464 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465 *
466 * \param ctx key to be initialized
467 * \param key input buffer
468 * \param keylen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200469 * (including the terminating null byte for PEM data)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200470 * \param pwd password for decryption (optional)
471 * \param pwdlen size of the password
472 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100473 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
475 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100476 *
477 * \note The key is also checked for correctness.
478 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 * \return 0 if successful, or a specific PK or PEM error code
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 const unsigned char *key, size_t keylen,
483 const unsigned char *pwd, size_t pwdlen );
484
Paul Bakkerff3a5182013-09-16 22:42:19 +0200485/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200487 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 *
489 * \param ctx key to be initialized
490 * \param key input buffer
491 * \param keylen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200492 * (including the terminating null byte for PEM data)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100494 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
496 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100497 *
498 * \note The key is also checked for correctness.
499 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500 * \return 0 if successful, or a specific PK or PEM error code
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200503 const unsigned char *key, size_t keylen );
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200506/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200508 * \brief Load and parse a private key
509 *
510 * \param ctx key to be initialized
511 * \param path filename to read the private key from
512 * \param password password to decrypt the file (can be NULL)
513 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100514 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
516 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100517 *
518 * \note The key is also checked for correctness.
519 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200520 * \return 0 if successful, or a specific PK or PEM error code
521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200523 const char *path, const char *password );
524
Paul Bakkerff3a5182013-09-16 22:42:19 +0200525/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527 * \brief Load and parse a public key
528 *
529 * \param ctx key to be initialized
Simon Butcher295639b2016-05-10 19:39:36 +0100530 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100532 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100533 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
534 * you need a specific key type, check the result with
535 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100536 *
537 * \note The key is also checked for correctness.
538 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539 * \return 0 if successful, or a specific PK or PEM error code
540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
542#endif /* MBEDTLS_FS_IO */
543#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200547 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
548 * Note: data is written at the end of the buffer! Use the
549 * return value to determine where you should start
550 * using the buffer
551 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100552 * \param ctx private to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200553 * \param buf buffer to write to
554 * \param size size of the buffer
555 *
556 * \return length of data written if successful, or a specific
557 * error code
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200560
561/**
562 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
563 * Note: data is written at the end of the buffer! Use the
564 * return value to determine where you should start
565 * using the buffer
566 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100567 * \param ctx public key to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200568 * \param buf buffer to write to
569 * \param size size of the buffer
570 *
571 * \return length of data written if successful, or a specific
572 * error code
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200577/**
578 * \brief Write a public key to a PEM string
579 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100580 * \param ctx public key to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200581 * \param buf buffer to write to
582 * \param size size of the buffer
583 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200584 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200587
588/**
589 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
590 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100591 * \param ctx private to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592 * \param buf buffer to write to
593 * \param size size of the buffer
594 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200595 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
598#endif /* MBEDTLS_PEM_WRITE_C */
599#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200600
601/*
602 * WARNING: Low-level functions. You probably do not want to use these unless
603 * you are certain you do ;)
604 */
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200607/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608 * \brief Parse a SubjectPublicKeyInfo DER structure
609 *
610 * \param p the position in the ASN.1 data
611 * \param end end of the buffer
612 * \param pk the key to fill
613 *
614 * \return 0 if successful, or a specific PK error code
615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
617 mbedtls_pk_context *pk );
618#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200621/**
622 * \brief Write a subjectPublicKey to ASN.1 data
623 * Note: function works backwards in data buffer
624 *
625 * \param p reference to current position pointer
626 * \param start start of the buffer (for bounds-checking)
627 * \param key public key to write away
628 *
629 * \return the length written or a negative error code
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
632 const mbedtls_pk_context *key );
633#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200634
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100635/*
636 * Internal module functions. You probably do not want to use these unless you
637 * know you do.
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_FS_IO)
640int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100641#endif
642
Paul Bakkered27a042013-04-18 22:46:23 +0200643#ifdef __cplusplus
644}
645#endif
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_PK_H */