blob: ab21e50a5277536b53a80d808b434af0e06aec2b [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,
Paul Bakkered27a042013-04-18 22:46:23 +020085} pk_type_t;
86
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020087/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020088 * \brief Types for interfacing with the debug module
89 */
90typedef enum
91{
92 POLARSSL_PK_DEBUG_NONE = 0,
93 POLARSSL_PK_DEBUG_MPI,
94 POLARSSL_PK_DEBUG_ECP,
95} pk_debug_type;
96
97/**
98 * \brief Item to send to the debug module
99 */
100typedef struct
101{
102 pk_debug_type type;
103 char *name;
104 void *value;
105} pk_debug_item;
106
107/** Maximum number of item send for debugging, plus 1 */
108#define POLARSSL_PK_DEBUG_MAX_ITEMS 3
109
110/**
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200111 * \brief Public key information and operations
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200112 */
113typedef struct
114{
115 /** Public key type */
116 pk_type_t type;
117
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200118 /** Type name */
119 const char *name;
120
121 /** Get key size in bits */
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200122 size_t (*get_size)( const void * );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200123
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200124 /** Tell if the context implements this type (eg ECKEY can do ECDSA) */
125 int (*can_do)( pk_type_t type );
126
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200127 /** Verify signature */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200128 int (*verify_func)( void *ctx, md_type_t md_alg,
129 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200130 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200131
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200132 /** Make signature */
133 int (*sign_func)( void *ctx, md_type_t md_alg,
134 const unsigned char *hash, size_t hash_len,
135 unsigned char *sig, size_t *sig_len,
136 int (*f_rng)(void *, unsigned char *, size_t),
137 void *p_rng );
138
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200139 /** Decrypt message */
140 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
141 unsigned char *output, size_t *olen, size_t osize,
142 int (*f_rng)(void *, unsigned char *, size_t),
143 void *p_rng );
144
145 /** Encrypt message */
146 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
147 unsigned char *output, size_t *olen, size_t osize,
148 int (*f_rng)(void *, unsigned char *, size_t),
149 void *p_rng );
150
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200151 /** Allocate a new context */
152 void * (*ctx_alloc_func)( void );
153
154 /** Free the given context */
155 void (*ctx_free_func)( void *ctx );
156
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200157 /** Interface with the debug module */
158 void (*debug_func)( const void *ctx, pk_debug_item *items );
159
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200160} pk_info_t;
161
162/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200163 * \brief Public key container
164 */
165typedef struct
166{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200167 const pk_info_t * pk_info; /**< Public key informations */
168 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200169} pk_context;
170
171/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200172 * \brief Return information associated with the given PK type
173 *
174 * \param type PK type to search for.
175 *
176 * \return The PK info associated with the type or NULL if not found.
177 */
178const pk_info_t *pk_info_from_type( pk_type_t pk_type );
179
180/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200181 * \brief Initialize a pk_context (as NONE)
182 */
183void pk_init( pk_context *ctx );
184
185/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200186 * \brief Initialize a PK context with the information given
187 * and allocates the type-specific PK subcontext.
188 *
189 * \param ctx Context to initialize. Must be empty (type NONE).
190 * \param info Information to use
191 *
192 * \return 0 on success,
193 * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
194 * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
195 */
196int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
197
198/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200199 * \brief Free a pk_context
200 */
201void pk_free( pk_context *ctx );
202
203/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200204 * \brief Get the size in bits of the underlying key
205 *
206 * \param ctx Context to use
207 *
208 * \return Key size in bits, or 0 on error
209 */
210size_t pk_get_size( const pk_context *ctx );
211
212/**
213 * \brief Tell if a context can do the operation given by type
214 *
215 * \param ctx Context to test
216 * \param type Target type
217 *
218 * \return 0 if context can't do the operations,
219 * 1 otherwise.
220 */
221int pk_can_do( pk_context *ctx, pk_type_t type );
222
223/**
224 * \brief Verify signature
225 *
226 * \param ctx PK context to use
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200227 * \param md_alg Hash algorithm used
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200228 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200229 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200230 * \param sig Signature to verify
231 * \param sig_len Signature length
232 *
233 * \return 0 on success (signature is valid),
234 * or a specific error code.
235 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200236int pk_verify( pk_context *ctx, md_type_t md_alg,
237 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200238 const unsigned char *sig, size_t sig_len );
239
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200240/**
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200241 * \brief Make signature
242 *
243 * \param ctx PK context to use
244 * \param md_alg Hash algorithm used
245 * \param hash Hash of the message to sign
246 * \param hash_len Hash length
247 * \param sig Place to write the signature
248 * \param sig_len Number of bytes written
249 * \param f_rng RNG function
250 * \param p_rng RNG parameter
251 *
252 * \return 0 on success, or a specific error code.
253 */
254int pk_sign( pk_context *ctx, md_type_t md_alg,
255 const unsigned char *hash, size_t hash_len,
256 unsigned char *sig, size_t *sig_len,
257 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
258
259/**
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200260 * \brief Decrypt message
261 *
262 * \param ctx PK context to use
263 * \param input Input to decrypt
264 * \param ilen Input size
265 * \param output Decrypted output
266 * \param olen Decrypted message lenght
267 * \param osize Size of the output buffer
268 *
269 * \return 0 on success, or a specific error code.
270 */
271int pk_decrypt( pk_context *ctx,
272 const unsigned char *input, size_t ilen,
273 unsigned char *output, size_t *olen, size_t osize,
274 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
275
276/**
277 * \brief Encrypt message
278 *
279 * \param ctx PK context to use
280 * \param input Message to encrypt
281 * \param ilen Message size
282 * \param output Encrypted output
283 * \param olen Encrypted output length
284 * \param osize Size of the output buffer
285 *
286 * \return 0 on success, or a specific error code.
287 */
288int pk_encrypt( pk_context *ctx,
289 const unsigned char *input, size_t ilen,
290 unsigned char *output, size_t *olen, size_t osize,
291 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
292
293/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200294 * \brief Export debug information
295 *
296 * \param ctx Context to use
297 * \param items Place to write debug items
298 *
299 * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA
300 */
301int pk_debug( const pk_context *ctx, pk_debug_item *items );
302
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200303/**
304 * \brief Access the type name
305 *
306 * \param ctx Context to use
307 *
308 * \return Type name on success, or "invalid PK"
309 */
310const char * pk_get_name( const pk_context *ctx );
311
Paul Bakkered27a042013-04-18 22:46:23 +0200312#ifdef __cplusplus
313}
314#endif
315
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200316#endif /* POLARSSL_PK_H */