blob: 346ad7af1fc7e5316c19fa4d4f78f1084f8e4435 [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkered27a042013-04-18 22:46:23 +02007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkered27a042013-04-18 22:46:23 +02009 *
Paul Bakkered27a042013-04-18 22:46:23 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_PK_H
26#define MBEDTLS_PK_H
Paul Bakkered27a042013-04-18 22:46:23 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020029#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020033
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020034#include "md.h"
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020037#include "rsa.h"
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#include "ecp.h"
42#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020045#include "ecdsa.h"
46#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
49#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
50#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x2E80 /**< Bad input parameters to function. */
51#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x2E00 /**< Read/write of file failed. */
52#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x2D80 /**< Unsupported key version */
53#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x2D00 /**< Invalid key tag or value. */
54#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x2C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
55#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x2C00 /**< Private key password can't be empty. */
56#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x2B80 /**< Given private key password does not allow for correct decryption. */
57#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x2B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
58#define MBEDTLS_ERR_PK_INVALID_ALG -0x2A80 /**< The algorithm tag or value is invalid. */
59#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x2A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
60#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x2980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
61#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x2000 /**< The signature is valid but its length is less than expected. */
Paul Bakker1a7550a2013-09-15 13:01:22 +020062
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020063
Paul Bakkered27a042013-04-18 22:46:23 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
68/**
69 * \brief Public key types
70 */
71typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 MBEDTLS_PK_NONE=0,
73 MBEDTLS_PK_RSA,
74 MBEDTLS_PK_ECKEY,
75 MBEDTLS_PK_ECKEY_DH,
76 MBEDTLS_PK_ECDSA,
77 MBEDTLS_PK_RSA_ALT,
78 MBEDTLS_PK_RSASSA_PSS,
79} mbedtls_pk_type_t;
Paul Bakkered27a042013-04-18 22:46:23 +020080
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020081/**
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020082 * \brief Options for RSASSA-PSS signature verification.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020084 */
85typedef struct
86{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_md_type_t mgf1_hash_id;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020088 int expected_salt_len;
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090} mbedtls_pk_rsassa_pss_options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020091
92/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020093 * \brief Types for interfacing with the debug module
94 */
95typedef enum
96{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 MBEDTLS_PK_DEBUG_NONE = 0,
98 MBEDTLS_PK_DEBUG_MPI,
99 MBEDTLS_PK_DEBUG_ECP,
100} mbedtls_pk_debug_type;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200101
102/**
103 * \brief Item to send to the debug module
104 */
105typedef struct
106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_pk_debug_type type;
Paul Bakkerfcc17212013-10-11 09:36:52 +0200108 const char *name;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200109 void *value;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110} mbedtls_pk_debug_item;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200111
112/** Maximum number of item send for debugging, plus 1 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200114
115/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200116 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200119
120/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200121 * \brief Public key container
122 */
123typedef struct
124{
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200125 const mbedtls_pk_info_t * pk_info; /**< Public key informations */
126 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127} mbedtls_pk_context;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128
Manuel Pégourié-Gonnarda79efde2015-04-09 10:58:56 +0200129#if defined(MBEDTLS_RSA_C)
130/**
131 * Quick access to an RSA context inside a PK context.
132 *
133 * \warning You must make sure the PK context actually holds an RSA context
134 * before using this function!
135 */
136static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
137{
138 return( (mbedtls_rsa_context *) (pk).pk_ctx );
139}
140#endif /* MBEDTLS_RSA_C */
141
142#if defined(MBEDTLS_ECP_C)
143/**
144 * Quick access to an EC context inside a PK context.
145 *
146 * \warning You must make sure the PK context actually holds an EC context
147 * before using this function!
148 */
149static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
150{
151 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
152}
153#endif /* MBEDTLS_ECP_C */
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200156/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200157 * \brief Types for RSA-alt abstraction
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200160 const unsigned char *input, unsigned char *output,
161 size_t output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200163 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200165 const unsigned char *hash, unsigned char *sig );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
167#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200168
169/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200170 * \brief Return information associated with the given PK type
171 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200172 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200173 *
174 * \return The PK info associated with the type or NULL if not found.
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200177
178/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 * \brief Initialize a mbedtls_pk_context (as NONE)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181void mbedtls_pk_init( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200182
183/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 * \brief Free a mbedtls_pk_context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_pk_free( mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200187
188/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200189 * \brief Initialize a PK context with the information given
190 * and allocates the type-specific PK subcontext.
191 *
192 * \param ctx Context to initialize. Must be empty (type NONE).
193 * \param info Information to use
194 *
195 * \return 0 on success,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
197 * MBEDTLS_ERR_PK_MALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200198 *
Paul Bakker42099c32014-01-27 11:45:49 +0100199 * \note For contexts holding an RSA-alt key, use
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * \c mbedtls_pk_init_ctx_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_pk_init_ctx( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200205/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200206 * \brief Initialize an RSA-alt context
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207 *
208 * \param ctx Context to initialize. Must be empty (type NONE).
209 * \param key RSA key pointer
210 * \param decrypt_func Decryption function
211 * \param sign_func Signing function
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200212 * \param key_len_func Function returning key length in bytes
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200213 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200215 * context wasn't already initialized as RSA_ALT.
216 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 * \note This function replaces \c mbedtls_pk_init_ctx() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_pk_init_ctx_rsa_alt( mbedtls_pk_context *ctx, void * key,
220 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
221 mbedtls_pk_rsa_alt_sign_func sign_func,
222 mbedtls_pk_rsa_alt_key_len_func key_len_func );
223#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200224
225/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200226 * \brief Get the size in bits of the underlying key
227 *
228 * \param ctx Context to use
229 *
230 * \return Key size in bits, or 0 on error
231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232size_t mbedtls_pk_get_size( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200233
234/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200235 * \brief Get the length in bytes of the underlying key
236 * \param ctx Context to use
237 *
Paul Bakker4fc090a2013-09-18 15:43:25 +0200238 * \return Key length in bytes, or 0 on error
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( ( mbedtls_pk_get_size( ctx ) + 7 ) / 8 );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200243}
244
245/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200246 * \brief Tell if a context can do the operation given by type
247 *
248 * \param ctx Context to test
249 * \param type Target type
250 *
251 * \return 0 if context can't do the operations,
252 * 1 otherwise.
253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200255
256/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200257 * \brief Verify signature (including padding if relevant).
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200258 *
259 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200260 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200261 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200262 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200263 * \param sig Signature to verify
264 * \param sig_len Signature length
265 *
266 * \return 0 on success (signature is valid),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200268 * valid but its actual length is less than sig_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200269 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200270 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200271 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200273 * to verify RSASSA_PSS signatures.
274 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200275 * \note If hash_len is 0, then the length associated with md_alg
276 * is used instead, or an error returned if it is invalid.
277 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200281 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200282 const unsigned char *sig, size_t sig_len );
283
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200285 * \brief Verify signature, with options.
286 * (Includes verification of the padding depending on type.)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200287 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200288 * \param type Signature type (inc. possible padding type) to verify
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200289 * \param options Pointer to type-specific options, or NULL
290 * \param ctx PK context to use
291 * \param md_alg Hash algorithm used (see notes)
292 * \param hash Hash of the message to sign
293 * \param hash_len Hash length or 0 (see notes)
294 * \param sig Signature to verify
295 * \param sig_len Signature length
296 *
297 * \return 0 on success (signature is valid),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 * MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200299 * used for this type of signatures,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 * MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200301 * valid but its actual length is less than sig_len,
302 * or a specific error code.
303 *
304 * \note If hash_len is 0, then the length associated with md_alg
305 * is used instead, or an error returned if it is invalid.
306 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200308 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
310 * to a mbedtls_pk_rsassa_pss_options structure,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200311 * otherwise it must be NULL.
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
314 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200315 const unsigned char *hash, size_t hash_len,
316 const unsigned char *sig, size_t sig_len );
317
318/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200319 * \brief Make signature, including padding if relevant.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200320 *
321 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200322 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200323 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200324 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200325 * \param sig Place to write the signature
326 * \param sig_len Number of bytes written
327 * \param f_rng RNG function
328 * \param p_rng RNG parameter
329 *
330 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200331 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200332 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
333 * There is no interface in the PK module to make RSASSA-PSS
334 * signatures yet.
335 *
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200336 * \note If hash_len is 0, then the length associated with md_alg
337 * is used instead, or an error returned if it is invalid.
338 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
340 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200343 const unsigned char *hash, size_t hash_len,
344 unsigned char *sig, size_t *sig_len,
345 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
346
347/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200348 * \brief Decrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200349 *
350 * \param ctx PK context to use
351 * \param input Input to decrypt
352 * \param ilen Input size
353 * \param output Decrypted output
Paul Bakker4fc090a2013-09-18 15:43:25 +0200354 * \param olen Decrypted message length
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200355 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200356 * \param f_rng RNG function
357 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200358 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200359 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
360 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200361 * \return 0 on success, or a specific error code.
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200364 const unsigned char *input, size_t ilen,
365 unsigned char *output, size_t *olen, size_t osize,
366 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
367
368/**
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200369 * \brief Encrypt message (including padding if relevant).
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200370 *
371 * \param ctx PK context to use
372 * \param input Message to encrypt
373 * \param ilen Message size
374 * \param output Encrypted output
375 * \param olen Encrypted output length
376 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200377 * \param f_rng RNG function
378 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200379 *
Manuel Pégourié-Gonnardd543a582014-06-25 14:04:36 +0200380 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
381 *
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200382 * \return 0 on success, or a specific error code.
383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200385 const unsigned char *input, size_t ilen,
386 unsigned char *output, size_t *olen, size_t osize,
387 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
388
389/**
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100390 * \brief Check if a public-private pair of keys matches.
391 *
392 * \param pub Context holding a public key.
393 * \param prv Context holding a private (and public) key.
394 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100398
399/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200400 * \brief Export debug information
401 *
402 * \param ctx Context to use
403 * \param items Place to write debug items
404 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200408
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200409/**
410 * \brief Access the type name
411 *
412 * \param ctx Context to use
413 *
414 * \return Type name on success, or "invalid PK"
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200417
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200418/**
Paul Bakker4fc090a2013-09-18 15:43:25 +0200419 * \brief Get the key type
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200420 *
421 * \param ctx Context to use
422 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 * \return Type on success, or MBEDTLS_PK_NONE
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200428/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200429/**
430 * \brief Parse a private key
431 *
432 * \param ctx key to be initialized
433 * \param key input buffer
434 * \param keylen size of the buffer
435 * \param pwd password for decryption (optional)
436 * \param pwdlen size of the password
437 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100438 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
440 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100441 *
442 * \note The key is also checked for correctness.
443 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200444 * \return 0 if successful, or a specific PK or PEM error code
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200447 const unsigned char *key, size_t keylen,
448 const unsigned char *pwd, size_t pwdlen );
449
Paul Bakkerff3a5182013-09-16 22:42:19 +0200450/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200451/**
452 * \brief Parse a public key
453 *
454 * \param ctx key to be initialized
455 * \param key input buffer
456 * \param keylen size of the buffer
457 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100458 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
460 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100461 *
462 * \note The key is also checked for correctness.
463 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200464 * \return 0 if successful, or a specific PK or PEM error code
465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467 const unsigned char *key, size_t keylen );
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_FS_IO)
Paul Bakkerff3a5182013-09-16 22:42:19 +0200470/** \ingroup pk_module */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200472 * \brief Load and parse a private key
473 *
474 * \param ctx key to be initialized
475 * \param path filename to read the private key from
476 * \param password password to decrypt the file (can be NULL)
477 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100478 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
480 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100481 *
482 * \note The key is also checked for correctness.
483 *
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200484 * \return 0 if successful, or a specific PK or PEM error code
485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200487 const char *path, const char *password );
488
Paul Bakkerff3a5182013-09-16 22:42:19 +0200489/** \ingroup pk_module */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200490/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 * \brief Load and parse a public key
492 *
493 * \param ctx key to be initialized
494 * \param path filename to read the private key from
495 *
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100496 * \note On entry, ctx must be empty, either freshly initialised
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
498 * specific key type, check the result with mbedtls_pk_can_do().
Manuel Pégourié-Gonnarde3b3d192014-03-13 19:21:49 +0100499 *
500 * \note The key is also checked for correctness.
501 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502 * \return 0 if successful, or a specific PK or PEM error code
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
505#endif /* MBEDTLS_FS_IO */
506#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509/**
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200510 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
511 * Note: data is written at the end of the buffer! Use the
512 * return value to determine where you should start
513 * using the buffer
514 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100515 * \param ctx private to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200516 * \param buf buffer to write to
517 * \param size size of the buffer
518 *
519 * \return length of data written if successful, or a specific
520 * error code
521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200523
524/**
525 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
526 * Note: data is written at the end of the buffer! Use the
527 * return value to determine where you should start
528 * using the buffer
529 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100530 * \param ctx public key to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200531 * \param buf buffer to write to
532 * \param size size of the buffer
533 *
534 * \return length of data written if successful, or a specific
535 * error code
536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200540/**
541 * \brief Write a public key to a PEM string
542 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100543 * \param ctx public key to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200544 * \param buf buffer to write to
545 * \param size size of the buffer
546 *
547 * \return 0 successful, or a specific error code
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200550
551/**
552 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
553 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100554 * \param ctx private to write away
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200555 * \param buf buffer to write to
556 * \param size size of the buffer
557 *
558 * \return 0 successful, or a specific error code
559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
561#endif /* MBEDTLS_PEM_WRITE_C */
562#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200563
564/*
565 * WARNING: Low-level functions. You probably do not want to use these unless
566 * you are certain you do ;)
567 */
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200570/**
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571 * \brief Parse a SubjectPublicKeyInfo DER structure
572 *
573 * \param p the position in the ASN.1 data
574 * \param end end of the buffer
575 * \param pk the key to fill
576 *
577 * \return 0 if successful, or a specific PK error code
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
580 mbedtls_pk_context *pk );
581#endif /* MBEDTLS_PK_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200584/**
585 * \brief Write a subjectPublicKey to ASN.1 data
586 * Note: function works backwards in data buffer
587 *
588 * \param p reference to current position pointer
589 * \param start start of the buffer (for bounds-checking)
590 * \param key public key to write away
591 *
592 * \return the length written or a negative error code
593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
595 const mbedtls_pk_context *key );
596#endif /* MBEDTLS_PK_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200597
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100598/*
599 * Internal module functions. You probably do not want to use these unless you
600 * know you do.
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_FS_IO)
603int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100604#endif
605
Paul Bakkered27a042013-04-18 22:46:23 +0200606#ifdef __cplusplus
607}
608#endif
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#endif /* MBEDTLS_PK_H */