blob: cc8a2fcfb89061ae5291373793d9120b6aba81ae [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é-Gonnard765db072013-08-14 15:00:27 +0200139 /** Allocate a new context */
140 void * (*ctx_alloc_func)( void );
141
142 /** Free the given context */
143 void (*ctx_free_func)( void *ctx );
144
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200145 /** Interface with the debug module */
146 void (*debug_func)( const void *ctx, pk_debug_item *items );
147
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200148} pk_info_t;
149
150/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200151 * \brief Public key container
152 */
153typedef struct
154{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200155 const pk_info_t * pk_info; /**< Public key informations */
156 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200157} pk_context;
158
159/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200160 * \brief Return information associated with the given PK type
161 *
162 * \param type PK type to search for.
163 *
164 * \return The PK info associated with the type or NULL if not found.
165 */
166const pk_info_t *pk_info_from_type( pk_type_t pk_type );
167
168/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200169 * \brief Initialize a pk_context (as NONE)
170 */
171void pk_init( pk_context *ctx );
172
173/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200174 * \brief Initialize a PK context with the information given
175 * and allocates the type-specific PK subcontext.
176 *
177 * \param ctx Context to initialize. Must be empty (type NONE).
178 * \param info Information to use
179 *
180 * \return 0 on success,
181 * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
182 * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
183 */
184int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
185
186/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200187 * \brief Free a pk_context
188 */
189void pk_free( pk_context *ctx );
190
191/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200192 * \brief Get the size in bits of the underlying key
193 *
194 * \param ctx Context to use
195 *
196 * \return Key size in bits, or 0 on error
197 */
198size_t pk_get_size( const pk_context *ctx );
199
200/**
201 * \brief Tell if a context can do the operation given by type
202 *
203 * \param ctx Context to test
204 * \param type Target type
205 *
206 * \return 0 if context can't do the operations,
207 * 1 otherwise.
208 */
209int pk_can_do( pk_context *ctx, pk_type_t type );
210
211/**
212 * \brief Verify signature
213 *
214 * \param ctx PK context to use
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200215 * \param md_alg Hash algorithm used
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200216 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200217 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200218 * \param sig Signature to verify
219 * \param sig_len Signature length
220 *
221 * \return 0 on success (signature is valid),
222 * or a specific error code.
223 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200224int pk_verify( pk_context *ctx, md_type_t md_alg,
225 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200226 const unsigned char *sig, size_t sig_len );
227
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200228/**
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200229 * \brief Make signature
230 *
231 * \param ctx PK context to use
232 * \param md_alg Hash algorithm used
233 * \param hash Hash of the message to sign
234 * \param hash_len Hash length
235 * \param sig Place to write the signature
236 * \param sig_len Number of bytes written
237 * \param f_rng RNG function
238 * \param p_rng RNG parameter
239 *
240 * \return 0 on success, or a specific error code.
241 */
242int pk_sign( pk_context *ctx, md_type_t md_alg,
243 const unsigned char *hash, size_t hash_len,
244 unsigned char *sig, size_t *sig_len,
245 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
246
247/**
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200248 * \brief Export debug information
249 *
250 * \param ctx Context to use
251 * \param items Place to write debug items
252 *
253 * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA
254 */
255int pk_debug( const pk_context *ctx, pk_debug_item *items );
256
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200257/**
258 * \brief Access the type name
259 *
260 * \param ctx Context to use
261 *
262 * \return Type name on success, or "invalid PK"
263 */
264const char * pk_get_name( const pk_context *ctx );
265
Paul Bakkered27a042013-04-18 22:46:23 +0200266#ifdef __cplusplus
267}
268#endif
269
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200270#endif /* POLARSSL_PK_H */