blob: b71a90cecebab1b27ec1b470ab72255f2a3b64ca [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 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200187 * \param pk_type PK type to search for.
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200188 *
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/**
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200248 * \brief Get the length in bytes of the underlying key
249 * \param ctx Context to use
250 *
251 * \return Key lenght in bytes, or 0 on error
252 */
Manuel Pégourié-Gonnardc6b68032013-08-22 17:36:45 +0200253static inline size_t pk_get_len( const pk_context *ctx )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +0200254{
255 return( ( pk_get_size( ctx ) + 7 ) / 8 );
256}
257
258/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200259 * \brief Tell if a context can do the operation given by type
260 *
261 * \param ctx Context to test
262 * \param type Target type
263 *
264 * \return 0 if context can't do the operations,
265 * 1 otherwise.
266 */
267int pk_can_do( pk_context *ctx, pk_type_t type );
268
269/**
270 * \brief Verify signature
271 *
272 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200273 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200274 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200275 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200276 * \param sig Signature to verify
277 * \param sig_len Signature length
278 *
279 * \return 0 on success (signature is valid),
280 * or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200281 *
282 * \note If hash_len is 0, then the length associated with md_alg
283 * is used instead, or an error returned if it is invalid.
284 *
285 * \note md_alg may be POLARSSL_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200286 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200287int pk_verify( pk_context *ctx, md_type_t md_alg,
288 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200289 const unsigned char *sig, size_t sig_len );
290
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291/**
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200292 * \brief Make signature
293 *
294 * \param ctx PK context to use
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200295 * \param md_alg Hash algorithm used (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200296 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200297 * \param hash_len Hash length or 0 (see notes)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200298 * \param sig Place to write the signature
299 * \param sig_len Number of bytes written
300 * \param f_rng RNG function
301 * \param p_rng RNG parameter
302 *
303 * \return 0 on success, or a specific error code.
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200304 *
305 * \note If hash_len is 0, then the length associated with md_alg
306 * is used instead, or an error returned if it is invalid.
307 *
308 * \note md_alg may be POLARSSL_MD_NONE, only if hash_len != 0
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200309 */
310int pk_sign( pk_context *ctx, md_type_t md_alg,
311 const unsigned char *hash, size_t hash_len,
312 unsigned char *sig, size_t *sig_len,
313 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
314
315/**
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200316 * \brief Decrypt message
317 *
318 * \param ctx PK context to use
319 * \param input Input to decrypt
320 * \param ilen Input size
321 * \param output Decrypted output
322 * \param olen Decrypted message lenght
323 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200324 * \param f_rng RNG function
325 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200326 *
327 * \return 0 on success, or a specific error code.
328 */
329int pk_decrypt( pk_context *ctx,
330 const unsigned char *input, size_t ilen,
331 unsigned char *output, size_t *olen, size_t osize,
332 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
333
334/**
335 * \brief Encrypt message
336 *
337 * \param ctx PK context to use
338 * \param input Message to encrypt
339 * \param ilen Message size
340 * \param output Encrypted output
341 * \param olen Encrypted output length
342 * \param osize Size of the output buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200343 * \param f_rng RNG function
344 * \param p_rng RNG parameter
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200345 *
346 * \return 0 on success, or a specific error code.
347 */
348int pk_encrypt( pk_context *ctx,
349 const unsigned char *input, size_t ilen,
350 unsigned char *output, size_t *olen, size_t osize,
351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
352
353/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200354 * \brief Export debug information
355 *
356 * \param ctx Context to use
357 * \param items Place to write debug items
358 *
359 * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA
360 */
361int pk_debug( const pk_context *ctx, pk_debug_item *items );
362
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200363/**
364 * \brief Access the type name
365 *
366 * \param ctx Context to use
367 *
368 * \return Type name on success, or "invalid PK"
369 */
370const char * pk_get_name( const pk_context *ctx );
371
Paul Bakkered27a042013-04-18 22:46:23 +0200372#ifdef __cplusplus
373}
374#endif
375
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200376#endif /* POLARSSL_PK_H */