blob: 6135bb0139c1c318c913b5e3b68390778cf6a3ac [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Paul Bakkered27a042013-04-18 22:46:23 +020028#ifndef POLARSSL_PK_H
29#define POLARSSL_PK_H
30
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020031#include "config.h"
32
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020033#include "md.h"
34
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020035#if defined(POLARSSL_RSA_C)
36#include "rsa.h"
37#endif
38
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020039#if defined(POLARSSL_ECP_C)
40#include "ecp.h"
41#endif
42
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020043#if defined(POLARSSL_ECDSA_C)
44#include "ecdsa.h"
45#endif
46
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020047#define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +020048#define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +020049#define POLARSSL_ERR_PK_BAD_INPUT_DATA -0x2E80 /**< Bad input parameters to function. */
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020050
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020051#if defined(POLARSSL_RSA_C)
52/**
53 * Quick access to an RSA context inside a PK context.
54 *
55 * \warning You must make sure the PK context actually holds an RSA context
56 * before using this macro!
57 */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020058#define pk_rsa( pk ) ( (rsa_context *) (pk).pk_ctx )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020059#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020060
61#if defined(POLARSSL_ECP_C)
62/**
63 * Quick access to an EC context inside a PK context.
64 *
65 * \warning You must make sure the PK context actually holds an EC context
66 * before using this macro!
67 */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020068#define pk_ec( pk ) ( (ecp_keypair *) (pk).pk_ctx )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020069#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020070
71
Paul Bakkered27a042013-04-18 22:46:23 +020072#ifdef __cplusplus
73extern "C" {
74#endif
75
76/**
77 * \brief Public key types
78 */
79typedef enum {
80 POLARSSL_PK_NONE=0,
81 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +020082 POLARSSL_PK_ECKEY,
83 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +020084 POLARSSL_PK_ECDSA,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020085 POLARSSL_PK_RSA_ALT,
Paul Bakkered27a042013-04-18 22:46:23 +020086} pk_type_t;
87
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020088/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020089 * \brief Types for interfacing with the debug module
90 */
91typedef enum
92{
93 POLARSSL_PK_DEBUG_NONE = 0,
94 POLARSSL_PK_DEBUG_MPI,
95 POLARSSL_PK_DEBUG_ECP,
96} pk_debug_type;
97
98/**
99 * \brief Item to send to the debug module
100 */
101typedef struct
102{
103 pk_debug_type type;
104 char *name;
105 void *value;
106} pk_debug_item;
107
108/** Maximum number of item send for debugging, plus 1 */
109#define POLARSSL_PK_DEBUG_MAX_ITEMS 3
110
111/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200112 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200113 */
114typedef struct
115{
116 /** Public key type */
117 pk_type_t type;
118
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200119 /** Type name */
120 const char *name;
121
122 /** Get key size in bits */
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200123 size_t (*get_size)( const void * );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200124
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200125 /** Tell if the context implements this type (eg ECKEY can do ECDSA) */
126 int (*can_do)( pk_type_t type );
127
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200128 /** Verify signature */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200129 int (*verify_func)( void *ctx, md_type_t md_alg,
130 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200131 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200133 /** Make signature */
134 int (*sign_func)( void *ctx, md_type_t md_alg,
135 const unsigned char *hash, size_t hash_len,
136 unsigned char *sig, size_t *sig_len,
137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng );
139
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200140 /** Decrypt message */
141 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
142 unsigned char *output, size_t *olen, size_t osize,
143 int (*f_rng)(void *, unsigned char *, size_t),
144 void *p_rng );
145
146 /** Encrypt message */
147 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
148 unsigned char *output, size_t *olen, size_t osize,
149 int (*f_rng)(void *, unsigned char *, size_t),
150 void *p_rng );
151
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200152 /** Allocate a new context */
153 void * (*ctx_alloc_func)( void );
154
155 /** Free the given context */
156 void (*ctx_free_func)( void *ctx );
157
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200158 /** Interface with the debug module */
159 void (*debug_func)( const void *ctx, pk_debug_item *items );
160
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200161} pk_info_t;
162
163/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200164 * \brief Public key container
165 */
166typedef struct
167{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200168 const pk_info_t * pk_info; /**< Public key informations */
169 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200170} pk_context;
171
172/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200173 * \brief Types for RSA-alt abstraction
174 */
175typedef int (*pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
176 const unsigned char *input, unsigned char *output,
177 size_t output_max_len );
178typedef int (*pk_rsa_alt_sign_func)( void *ctx,
179 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
180 int mode, int hash_id, unsigned int hashlen,
181 const unsigned char *hash, unsigned char *sig );
182typedef size_t (*pk_rsa_alt_key_len_func)( void *ctx );
183
184/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200185 * \brief Return information associated with the given PK type
186 *
187 * \param type PK type to search for.
188 *
189 * \return The PK info associated with the type or NULL if not found.
190 */
191const pk_info_t *pk_info_from_type( pk_type_t pk_type );
192
193/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200194 * \brief Initialize a pk_context (as NONE)
195 */
196void pk_init( pk_context *ctx );
197
198/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199 * \brief Free a pk_context
200 */
201void pk_free( pk_context *ctx );
202
203/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200204 * \brief Initialize a PK context with the information given
205 * and allocates the type-specific PK subcontext.
206 *
207 * \param ctx Context to initialize. Must be empty (type NONE).
208 * \param info Information to use
209 *
210 * \return 0 on success,
211 * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
212 * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200213 *
214 * \note For contexts holding an RSA-alt key, use
215 * \c pk_init_ctx_rsa_alt() instead.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200216 */
217int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
218
219/**
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200220 * \brief Initialiaze an RSA-alt context
221 *
222 * \param ctx Context to initialize. Must be empty (type NONE).
223 * \param key RSA key pointer
224 * \param decrypt_func Decryption function
225 * \param sign_func Signing function
226 * \param key_len_func Function returning key length
227 *
228 * \return 0 on success, or POLARSSL_ERR_PK_BAD_INPUT_DATA if the
229 * context wasn't already initialized as RSA_ALT.
230 *
231 * \note This function replaces \c pk_init_ctx() for RSA-alt.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200232 */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200233int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
234 pk_rsa_alt_decrypt_func decrypt_func,
235 pk_rsa_alt_sign_func sign_func,
236 pk_rsa_alt_key_len_func key_len_func );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200237
238/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200239 * \brief Get the size in bits of the underlying key
240 *
241 * \param ctx Context to use
242 *
243 * \return Key size in bits, or 0 on error
244 */
245size_t pk_get_size( const pk_context *ctx );
246
247/**
248 * \brief Tell if a context can do the operation given by type
249 *
250 * \param ctx Context to test
251 * \param type Target type
252 *
253 * \return 0 if context can't do the operations,
254 * 1 otherwise.
255 */
256int pk_can_do( pk_context *ctx, pk_type_t type );
257
258/**
259 * \brief Verify signature
260 *
261 * \param ctx PK context to use
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200262 * \param md_alg Hash algorithm used
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200263 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200264 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200265 * \param sig Signature to verify
266 * \param sig_len Signature length
267 *
268 * \return 0 on success (signature is valid),
269 * or a specific error code.
270 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200271int pk_verify( pk_context *ctx, md_type_t md_alg,
272 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200273 const unsigned char *sig, size_t sig_len );
274
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200275/**
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200276 * \brief Make signature
277 *
278 * \param ctx PK context to use
279 * \param md_alg Hash algorithm used
280 * \param hash Hash of the message to sign
281 * \param hash_len Hash length
282 * \param sig Place to write the signature
283 * \param sig_len Number of bytes written
284 * \param f_rng RNG function
285 * \param p_rng RNG parameter
286 *
287 * \return 0 on success, or a specific error code.
288 */
289int pk_sign( pk_context *ctx, md_type_t md_alg,
290 const unsigned char *hash, size_t hash_len,
291 unsigned char *sig, size_t *sig_len,
292 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
293
294/**
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200295 * \brief Decrypt message
296 *
297 * \param ctx PK context to use
298 * \param input Input to decrypt
299 * \param ilen Input size
300 * \param output Decrypted output
301 * \param olen Decrypted message lenght
302 * \param osize Size of the output buffer
303 *
304 * \return 0 on success, or a specific error code.
305 */
306int pk_decrypt( pk_context *ctx,
307 const unsigned char *input, size_t ilen,
308 unsigned char *output, size_t *olen, size_t osize,
309 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
310
311/**
312 * \brief Encrypt message
313 *
314 * \param ctx PK context to use
315 * \param input Message to encrypt
316 * \param ilen Message size
317 * \param output Encrypted output
318 * \param olen Encrypted output length
319 * \param osize Size of the output buffer
320 *
321 * \return 0 on success, or a specific error code.
322 */
323int pk_encrypt( pk_context *ctx,
324 const unsigned char *input, size_t ilen,
325 unsigned char *output, size_t *olen, size_t osize,
326 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
327
328/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200329 * \brief Export debug information
330 *
331 * \param ctx Context to use
332 * \param items Place to write debug items
333 *
334 * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA
335 */
336int pk_debug( const pk_context *ctx, pk_debug_item *items );
337
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200338/**
339 * \brief Access the type name
340 *
341 * \param ctx Context to use
342 *
343 * \return Type name on success, or "invalid PK"
344 */
345const char * pk_get_name( const pk_context *ctx );
346
Paul Bakkered27a042013-04-18 22:46:23 +0200347#ifdef __cplusplus
348}
349#endif
350
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200351#endif /* POLARSSL_PK_H */