blob: 09a851391510061e5f9ce2e8def64252a9922270 [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é-Gonnard2ac9c602015-10-05 16:18:23 +010052#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
53 !defined(inline) && !defined(__cplusplus)
54#define inline __inline
55#endif
56
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020057#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
Manuel Pégourié-Gonnardeed55a42015-04-09 17:31:59 +020058#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
59#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
60#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
61#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
62#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
63#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
64#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
65#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
66#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
67#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
68#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
69#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
Gilles Peskine5114d3e2018-03-30 07:12:15 +020070#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 +030071
72/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010073#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020074
Paul Bakkered27a042013-04-18 22:46:23 +020075#ifdef __cplusplus
76extern "C" {
77#endif
78
79/**
80 * \brief Public key types
81 */
82typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 MBEDTLS_PK_NONE=0,
84 MBEDTLS_PK_RSA,
85 MBEDTLS_PK_ECKEY,
86 MBEDTLS_PK_ECKEY_DH,
87 MBEDTLS_PK_ECDSA,
88 MBEDTLS_PK_RSA_ALT,
89 MBEDTLS_PK_RSASSA_PSS,
90} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020091
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020092/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020093 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020095 */
Dawid Drozd428cc522018-07-24 10:02:47 +020096typedef struct mbedtls_pk_rsassa_pss_options
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020099 int expected_salt_len;
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200102
103/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200104 * \brief Types for interfacing with the debug module
105 */
106typedef enum
107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 MBEDTLS_PK_DEBUG_NONE = 0,
109 MBEDTLS_PK_DEBUG_MPI,
110 MBEDTLS_PK_DEBUG_ECP,
111} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200112
113/**
114 * \brief Item to send to the debug module
115 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200116typedef struct mbedtls_pk_debug_item
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200119 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200120 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200122
123/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200125
126/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200127 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200128 */
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200129#if defined(MBEDTLS_PK_SINGLE_TYPE)
130typedef enum {
131 MBEDTLS_PK_INVALID_HANDLE,
132 MBEDTLS_PK_UNIQUE_VALID_HANDLE,
133} mbedtls_pk_handle_t;
134#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200136typedef const mbedtls_pk_info_t *mbedtls_pk_handle_t;
137#define MBEDTLS_PK_INVALID_HANDLE ( (mbedtls_pk_handle_t) NULL )
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200138#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200139
140/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141 * \brief Public key container
142 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200143typedef struct mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144{
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +0200145#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200146 mbedtls_pk_handle_t pk_info; /**< Public key information */
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +0200147#endif
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200148 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Jarno Lamsab1760922019-04-18 15:58:34 +0300151#if defined(MBEDTLS_USE_TINYCRYPT)
152typedef struct
153{
154 uint8_t private_key[NUM_ECC_BYTES];
155 uint8_t public_key[2*NUM_ECC_BYTES];
156} mbedtls_uecc_keypair;
157#endif
158
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200159#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200160/**
161 * \brief Context for resuming operations
162 */
163typedef struct
164{
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200165 mbedtls_pk_handle_t pk_info; /**< Public key information */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200166 void * rs_ctx; /**< Underlying restart context */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200167} mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200168#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200169/* Now we can declare functions that take a pointer to that */
170typedef void mbedtls_pk_restart_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200171#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200172
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200173#if defined(MBEDTLS_RSA_C)
174/**
175 * Quick access to an RSA context inside a PK context.
176 *
177 * \warning You must make sure the PK context actually holds an RSA context
178 * before using this function!
179 */
180static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
181{
182 return( (mbedtls_rsa_context *) (pk).pk_ctx );
183}
184#endif /* MBEDTLS_RSA_C */
185
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300186#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckerda779712019-08-21 13:22:59 +0100187static inline mbedtls_uecc_keypair *mbedtls_pk_uecc( const mbedtls_pk_context pk )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +0300188{
189 return( (mbedtls_uecc_keypair *) (pk).pk_ctx );
190}
191#endif
192
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200193#if defined(MBEDTLS_ECP_C)
194/**
195 * Quick access to an EC context inside a PK context.
196 *
197 * \warning You must make sure the PK context actually holds an EC context
198 * before using this function!
199 */
200static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
201{
202 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
203}
204#endif /* MBEDTLS_ECP_C */
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200207/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200208 * \brief Types for RSA-alt abstraction
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211 const unsigned char *input, unsigned char *output,
212 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200216 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
218#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200219
220/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200221 * \brief Return information associated with the given PK type
222 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200223 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200224 *
225 * \return The PK info associated with the type or NULL if not found.
226 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200227mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200228
229/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100230 * \brief Initialize a #mbedtls_pk_context (as NONE).
231 *
232 * \param ctx The context to initialize.
233 * This must not be \c NULL.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200236
237/**
Gilles Peskinea310b412018-12-19 00:51:21 +0100238 * \brief Free the components of a #mbedtls_pk_context.
239 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100240 * \param ctx The context to clear. It must have been initialized.
Gilles Peskinea310b412018-12-19 00:51:21 +0100241 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200244
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200245#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200246/**
247 * \brief Initialize a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100248 *
249 * \param ctx The context to initialize.
250 * This must not be \c NULL.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200251 */
252void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
253
254/**
255 * \brief Free the components of a restart context
Gilles Peskinea310b412018-12-19 00:51:21 +0100256 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100257 * \param ctx The context to clear. It must have been initialized.
Gilles Peskinea310b412018-12-19 00:51:21 +0100258 * If this is \c NULL, this function does nothing.
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200259 */
260void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200261#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200262
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200263/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200264 * \brief Initialize a PK context with the information given
265 * and allocates the type-specific PK subcontext.
266 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100267 * \param ctx Context to initialize. It must not have been set
268 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200269 * \param info Information to use
270 *
271 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200273 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200274 *
Paul Bakker42099c32014-01-27 11:45:49 +0100275 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200276 * \c mbedtls_pk_setup_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200277 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200278int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200281/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200282 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200283 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100284 * \param ctx Context to initialize. It must not have been set
285 * up yet (type #MBEDTLS_PK_NONE).
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200286 * \param key RSA key pointer
287 * \param decrypt_func Decryption function
288 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200289 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200290 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200292 * context wasn't already initialized as RSA_ALT.
293 *
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200294 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200295 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200296int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
298 mbedtls_pk_rsa_alt_sign_func sign_func,
299 mbedtls_pk_rsa_alt_key_len_func key_len_func );
300#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200301
302/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200303 * \brief Get the size in bits of the underlying key
304 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100305 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200306 *
307 * \return Key size in bits, or 0 on error
308 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200309size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200310
311/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200312 * \brief Get the length in bytes of the underlying key
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100313 *
314 * \param ctx The context to query. It must have been initialized.
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200315 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200316 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200319{
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200320 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200321}
322
323/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200324 * \brief Tell if a context can do the operation given by type
325 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100326 * \param ctx The context to query. It must have been initialized.
327 * \param type The desired type.
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200328 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100329 * \return 1 if the context can do operations on the given type.
330 * \return 0 if the context cannot do the operations on the given
331 * type. This is always the case for a context that has
332 * been initialized but not set up, or that has been
333 * cleared with mbedtls_pk_free().
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200336
337/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200338 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200339 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100340 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200341 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200342 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200343 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200344 * \param sig Signature to verify
345 * \param sig_len Signature length
346 *
347 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200348 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
349 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200350 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200351 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200352 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200354 * to verify RSASSA_PSS signatures.
355 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200356 * \note If hash_len is 0, then the length associated with md_alg
357 * is used instead, or an error returned if it is invalid.
358 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200362 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200363 const unsigned char *sig, size_t sig_len );
364
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200365/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200366 * \brief Restartable version of \c mbedtls_pk_verify()
367 *
368 * \note Performs the same job as \c mbedtls_pk_verify(), but can
369 * return early and restart according to the limit set with
370 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
371 * operations. For RSA, same as \c mbedtls_pk_verify().
372 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100373 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200374 * \param md_alg Hash algorithm used (see notes)
375 * \param hash Hash of the message to sign
376 * \param hash_len Hash length or 0 (see notes)
377 * \param sig Signature to verify
378 * \param sig_len Signature length
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200379 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200380 *
381 * \return See \c mbedtls_pk_verify(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200382 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200383 * operations was reached: see \c mbedtls_ecp_set_max_ops().
384 */
385int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
386 mbedtls_md_type_t md_alg,
387 const unsigned char *hash, size_t hash_len,
388 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200389 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200390
391/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200392 * \brief Verify signature, with options.
393 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200394 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200395 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200396 * \param options Pointer to type-specific options, or NULL
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100397 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200398 * \param md_alg Hash algorithm used (see notes)
399 * \param hash Hash of the message to sign
400 * \param hash_len Hash length or 0 (see notes)
401 * \param sig Signature to verify
402 * \param sig_len Signature length
403 *
404 * \return 0 on success (signature is valid),
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200405 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200406 * used for this type of signatures,
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200407 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
408 * signature in sig but its length is less than \p siglen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200409 * or a specific error code.
410 *
411 * \note If hash_len is 0, then the length associated with md_alg
412 * is used instead, or an error returned if it is invalid.
413 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200415 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
417 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200418 * otherwise it must be NULL.
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
421 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200422 const unsigned char *hash, size_t hash_len,
423 const unsigned char *sig, size_t sig_len );
424
425/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200426 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200427 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100428 * \param ctx The PK context to use. It must have been set up
429 * with a private key.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200430 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200431 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200432 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200433 * \param sig Place to write the signature
434 * \param sig_len Number of bytes written
435 * \param f_rng RNG function
436 * \param p_rng RNG parameter
437 *
438 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200439 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200440 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
441 * There is no interface in the PK module to make RSASSA-PSS
442 * signatures yet.
443 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200444 * \note If hash_len is 0, then the length associated with md_alg
445 * is used instead, or an error returned if it is invalid.
446 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
448 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200451 const unsigned char *hash, size_t hash_len,
452 unsigned char *sig, size_t *sig_len,
453 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
454
455/**
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200456 * \brief Restartable version of \c mbedtls_pk_sign()
457 *
458 * \note Performs the same job as \c mbedtls_pk_sign(), but can
459 * return early and restart according to the limit set with
460 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
461 * operations. For RSA, same as \c mbedtls_pk_sign().
462 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100463 * \param ctx The PK context to use. It must have been set up
464 * with a private key.
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200465 * \param md_alg Hash algorithm used (see notes)
466 * \param hash Hash of the message to sign
467 * \param hash_len Hash length or 0 (see notes)
468 * \param sig Place to write the signature
469 * \param sig_len Number of bytes written
470 * \param f_rng RNG function
471 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200472 * \param rs_ctx Restart context (NULL to disable restart)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200473 *
474 * \return See \c mbedtls_pk_sign(), or
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200475 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200476 * operations was reached: see \c mbedtls_ecp_set_max_ops().
477 */
478int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
479 mbedtls_md_type_t md_alg,
480 const unsigned char *hash, size_t hash_len,
481 unsigned char *sig, size_t *sig_len,
482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200483 mbedtls_pk_restart_ctx *rs_ctx );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200484
485/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200486 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200487 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100488 * \param ctx The PK context to use. It must have been set up
489 * with a private key.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200490 * \param input Input to decrypt
491 * \param ilen Input size
492 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200493 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200494 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200495 * \param f_rng RNG function
496 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200497 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200498 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
499 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200500 * \return 0 on success, or a specific error code.
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200503 const unsigned char *input, size_t ilen,
504 unsigned char *output, size_t *olen, size_t osize,
505 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
506
507/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200508 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200509 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100510 * \param ctx The PK context to use. It must have been set up.
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200511 * \param input Message to encrypt
512 * \param ilen Message size
513 * \param output Encrypted output
514 * \param olen Encrypted output length
515 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200516 * \param f_rng RNG function
517 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200518 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200519 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
520 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521 * \return 0 on success, or a specific error code.
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200524 const unsigned char *input, size_t ilen,
525 unsigned char *output, size_t *olen, size_t osize,
526 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
527
528/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100529 * \brief Check if a public-private pair of keys matches.
530 *
531 * \param pub Context holding a public key.
532 * \param prv Context holding a private (and public) key.
533 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100537
538/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200539 * \brief Export debug information
540 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100541 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200542 * \param items Place to write debug items
543 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200547
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200548/**
549 * \brief Access the type name
550 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100551 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200552 *
553 * \return Type name on success, or "invalid PK"
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200556
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200557/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200558 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200559 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100560 * \param ctx The PK context to use. It must have been initialized.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200561 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100562 * \return Type on success.
563 * \return #MBEDTLS_PK_NONE for a context that has not been set up.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200568/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200569/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200570 * \brief Parse a private key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100572 * \param ctx The PK context to fill. It must have been initialized
573 * but not set up.
Gilles Peskine159171b2018-12-19 17:03:28 +0100574 * \param key Input buffer to parse.
575 * The buffer must contain the input exactly, with no
576 * extra trailing material. For PEM, the buffer must
577 * contain a null-terminated string.
578 * \param keylen Size of \b key in bytes.
579 * For PEM data, this includes the terminating null byte,
580 * so \p keylen must be equal to `strlen(key) + 1`.
Gilles Peskined54b9752018-12-19 17:12:01 +0100581 * \param pwd Optional password for decryption.
582 * Pass \c NULL if expecting a non-encrypted key.
583 * Pass a string of \p pwdlen bytes if expecting an encrypted
584 * key; a non-encrypted key will also be accepted.
585 * The empty password is not supported.
586 * \param pwdlen Size of the password in bytes.
587 * Ignored if \p pwd is \c NULL.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200588 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100589 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
591 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100592 *
593 * \note The key is also checked for correctness.
594 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595 * \return 0 if successful, or a specific PK or PEM error code
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598 const unsigned char *key, size_t keylen,
599 const unsigned char *pwd, size_t pwdlen );
600
Paul Bakkerff3a5182013-09-16 22:42:19 +0200601/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200603 * \brief Parse a public key in PEM or DER format
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100605 * \param ctx The PK context to fill. It must have been initialized
606 * but not set up.
Gilles Peskine159171b2018-12-19 17:03:28 +0100607 * \param key Input buffer to parse.
608 * The buffer must contain the input exactly, with no
609 * extra trailing material. For PEM, the buffer must
610 * contain a null-terminated string.
611 * \param keylen Size of \b key in bytes.
612 * For PEM data, this includes the terminating null byte,
613 * so \p keylen must be equal to `strlen(key) + 1`.
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
617 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100618 *
619 * \note The key is also checked for correctness.
620 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200621 * \return 0 if successful, or a specific PK or PEM error code
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200624 const unsigned char *key, size_t keylen );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200627/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200629 * \brief Load and parse a private key
630 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100631 * \param ctx The PK context to fill. It must have been initialized
632 * but not set up.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200633 * \param path filename to read the private key from
Gilles Peskined54b9752018-12-19 17:12:01 +0100634 * \param password Optional password to decrypt the file.
635 * Pass \c NULL if expecting a non-encrypted key.
636 * Pass a null-terminated string if expecting an encrypted
637 * key; a non-encrypted key will also be accepted.
638 * The empty password is not supported.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200639 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100640 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
642 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100643 *
644 * \note The key is also checked for correctness.
645 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200646 * \return 0 if successful, or a specific PK or PEM error code
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200649 const char *path, const char *password );
650
Paul Bakkerff3a5182013-09-16 22:42:19 +0200651/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200652/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653 * \brief Load and parse a public 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.
Simon Butcher295639b2016-05-10 19:39:36 +0100657 * \param path filename to read the public key from
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100659 * \note On entry, ctx must be empty, either freshly initialised
Simon Butcher295639b2016-05-10 19:39:36 +0100660 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
661 * you need a specific key type, check the result with
662 * mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100663 *
664 * \note The key is also checked for correctness.
665 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 * \return 0 if successful, or a specific PK or PEM error code
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
669#endif /* MBEDTLS_FS_IO */
670#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200674 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
675 * Note: data is written at the end of the buffer! Use the
676 * return value to determine where you should start
677 * using the buffer
678 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100679 * \param ctx PK context which must contain a valid private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200680 * \param buf buffer to write to
681 * \param size size of the buffer
682 *
683 * \return length of data written if successful, or a specific
684 * error code
685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200687
688/**
689 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
690 * Note: data is written at the end of the buffer! Use the
691 * return value to determine where you should start
692 * using the buffer
693 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100694 * \param ctx PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200695 * \param buf buffer to write to
696 * \param size size of the buffer
697 *
698 * \return length of data written if successful, or a specific
699 * error code
700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200704/**
705 * \brief Write a public key to a PEM string
706 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100707 * \param ctx PK context which must contain a valid public or private key.
Gilles Peskine159171b2018-12-19 17:03:28 +0100708 * \param buf Buffer to write to. The output includes a
709 * terminating null byte.
710 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200711 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200712 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200715
716/**
717 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
718 *
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100719 * \param ctx PK context which must contain a valid private key.
Gilles Peskine159171b2018-12-19 17:03:28 +0100720 * \param buf Buffer to write to. The output includes a
721 * terminating null byte.
722 * \param size Size of the buffer in bytes.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200723 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200724 * \return 0 if successful, or a specific error code
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
727#endif /* MBEDTLS_PEM_WRITE_C */
728#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200729
730/*
731 * WARNING: Low-level functions. You probably do not want to use these unless
732 * you are certain you do ;)
733 */
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200736/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 * \brief Parse a SubjectPublicKeyInfo DER structure
738 *
739 * \param p the position in the ASN.1 data
740 * \param end end of the buffer
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100741 * \param pk The PK context to fill. It must have been initialized
742 * but not set up.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743 *
744 * \return 0 if successful, or a specific PK error code
745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
747 mbedtls_pk_context *pk );
748#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200751/**
752 * \brief Write a subjectPublicKey to ASN.1 data
753 * Note: function works backwards in data buffer
754 *
755 * \param p reference to current position pointer
756 * \param start start of the buffer (for bounds-checking)
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100757 * \param key PK context which must contain a valid public or private key.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200758 *
759 * \return the length written or a negative error code
760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
762 const mbedtls_pk_context *key );
763#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200764
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100765/*
766 * Internal module functions. You probably do not want to use these unless you
767 * know you do.
768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#if defined(MBEDTLS_FS_IO)
770int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100771#endif
772
Paul Bakkered27a042013-04-18 22:46:23 +0200773#ifdef __cplusplus
774}
775#endif
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#endif /* MBEDTLS_PK_H */