blob: fb0e92ec5308df5a0ce7272bbdc9435ee5c7cab7 [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
132 /** Allocate a new context */
133 void * (*ctx_alloc_func)( void );
134
135 /** Free the given context */
136 void (*ctx_free_func)( void *ctx );
137
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200138 /** Interface with the debug module */
139 void (*debug_func)( const void *ctx, pk_debug_item *items );
140
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200141} pk_info_t;
142
143/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144 * \brief Public key container
145 */
146typedef struct
147{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200148 const pk_info_t * pk_info; /**< Public key informations */
149 void * pk_ctx; /**< Underlying public key context */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150} pk_context;
151
152/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200153 * \brief Return information associated with the given PK type
154 *
155 * \param type PK type to search for.
156 *
157 * \return The PK info associated with the type or NULL if not found.
158 */
159const pk_info_t *pk_info_from_type( pk_type_t pk_type );
160
161/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200162 * \brief Initialize a pk_context (as NONE)
163 */
164void pk_init( pk_context *ctx );
165
166/**
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200167 * \brief Initialize a PK context with the information given
168 * and allocates the type-specific PK subcontext.
169 *
170 * \param ctx Context to initialize. Must be empty (type NONE).
171 * \param info Information to use
172 *
173 * \return 0 on success,
174 * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
175 * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
176 */
177int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
178
179/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200180 * \brief Free a pk_context
181 */
182void pk_free( pk_context *ctx );
183
184/**
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200185 * \brief Get the size in bits of the underlying key
186 *
187 * \param ctx Context to use
188 *
189 * \return Key size in bits, or 0 on error
190 */
191size_t pk_get_size( const pk_context *ctx );
192
193/**
194 * \brief Tell if a context can do the operation given by type
195 *
196 * \param ctx Context to test
197 * \param type Target type
198 *
199 * \return 0 if context can't do the operations,
200 * 1 otherwise.
201 */
202int pk_can_do( pk_context *ctx, pk_type_t type );
203
204/**
205 * \brief Verify signature
206 *
207 * \param ctx PK context to use
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200208 * \param md_alg Hash algorithm used
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200209 * \param hash Hash of the message to sign
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200210 * \param hash_len Hash length
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200211 * \param sig Signature to verify
212 * \param sig_len Signature length
213 *
214 * \return 0 on success (signature is valid),
215 * or a specific error code.
216 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200217int pk_verify( pk_context *ctx, md_type_t md_alg,
218 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200219 const unsigned char *sig, size_t sig_len );
220
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200221/**
222 * \brief Export debug information
223 *
224 * \param ctx Context to use
225 * \param items Place to write debug items
226 *
227 * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA
228 */
229int pk_debug( const pk_context *ctx, pk_debug_item *items );
230
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200231/**
232 * \brief Access the type name
233 *
234 * \param ctx Context to use
235 *
236 * \return Type name on success, or "invalid PK"
237 */
238const char * pk_get_name( const pk_context *ctx );
239
Paul Bakkered27a042013-04-18 22:46:23 +0200240#ifdef __cplusplus
241}
242#endif
243
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244#endif /* POLARSSL_PK_H */