blob: d1bd2158b9a56e36bcfd3968f0805ce6cd1fe3b8 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +020028#if defined(POLARSSL_PK_C)
29
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020030#include "polarssl/pk.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031#include "polarssl/pk_wrap.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020032
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020033#if defined(POLARSSL_RSA_C)
34#include "polarssl/rsa.h"
35#endif
36#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020039#if defined(POLARSSL_ECDSA_C)
40#include "polarssl/ecdsa.h"
41#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020042
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020043/*
44 * Initialise a pk_context
45 */
46void pk_init( pk_context *ctx )
47{
48 if( ctx == NULL )
49 return;
50
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020051 ctx->pk_info = NULL;
52 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053}
54
55/*
56 * Free (the components of) a pk_context
57 */
58void pk_free( pk_context *ctx )
59{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020060 if( ctx == NULL || ctx->pk_info == NULL)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061 return;
62
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020063 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
64 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020065
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020066 ctx->pk_info = NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020067}
68
69/*
70 * Get pk_info structure from type
71 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020072const pk_info_t * pk_info_from_type( pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020073{
74 switch( pk_type ) {
75#if defined(POLARSSL_RSA_C)
76 case POLARSSL_PK_RSA:
77 return &rsa_info;
78#endif
79#if defined(POLARSSL_ECP_C)
80 case POLARSSL_PK_ECKEY:
81 return &eckey_info;
82 case POLARSSL_PK_ECKEY_DH:
83 return &eckeydh_info;
84#endif
85#if defined(POLARSSL_ECDSA_C)
86 case POLARSSL_PK_ECDSA:
87 return &ecdsa_info;
88#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020089 /* POLARSSL_PK_RSA_ALT ommited on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020090 default:
91 return NULL;
92 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093}
94
95/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020096 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020097 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020098int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020099{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200100 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200101 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200102
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200103 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200104 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200105
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200106 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200110
111/*
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100112 * Set RSA padding
113 */
114int pk_rsa_set_padding( pk_context *ctx, int padding, int hash_id )
115{
116 if( ctx == NULL || ctx->pk_info == NULL ||
117 ctx->pk_info->type != POLARSSL_PK_RSA )
118 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
119
120 rsa_set_padding( pk_rsa( *ctx ), padding, hash_id );
121
122 return( 0 );
123}
124
125/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200126 * Initialize an RSA-alt context
127 */
128int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
129 pk_rsa_alt_decrypt_func decrypt_func,
130 pk_rsa_alt_sign_func sign_func,
131 pk_rsa_alt_key_len_func key_len_func )
132{
133 rsa_alt_context *rsa_alt;
134 const pk_info_t *info = &rsa_alt_info;
135
136 if( ctx == NULL || ctx->pk_info != NULL )
137 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
138
139 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
140 return( POLARSSL_ERR_PK_MALLOC_FAILED );
141
142 ctx->pk_info = info;
143
144 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
145
146 rsa_alt->key = key;
147 rsa_alt->decrypt_func = decrypt_func;
148 rsa_alt->sign_func = sign_func;
149 rsa_alt->key_len_func = key_len_func;
150
151 return( 0 );
152}
153
154/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155 * Tell if a PK can do the operations of the given type
156 */
157int pk_can_do( pk_context *ctx, pk_type_t type )
158{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200159 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200160 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200161 return( 0 );
162
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200163 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200164}
165
166/*
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200167 * Helper for pk_sign and pk_verify
168 */
169static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
170{
171 const md_info_t *md_info;
172
173 if( *hash_len != 0 )
174 return( 0 );
175
176 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
177 return( -1 );
178
179 *hash_len = md_info->size;
180 return( 0 );
181}
182
183/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200184 * Verify a signature
185 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200186int pk_verify( pk_context *ctx, md_type_t md_alg,
187 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200188 const unsigned char *sig, size_t sig_len )
189{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200190 if( ctx == NULL || ctx->pk_info == NULL ||
191 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200192 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200193
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200194 if( ctx->pk_info->verify_func == NULL )
195 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
196
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200197 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200198 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200199}
200
201/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200202 * Make a signature
203 */
204int pk_sign( pk_context *ctx, md_type_t md_alg,
205 const unsigned char *hash, size_t hash_len,
206 unsigned char *sig, size_t *sig_len,
207 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
208{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200209 if( ctx == NULL || ctx->pk_info == NULL ||
210 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200211 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
212
213 if( ctx->pk_info->sign_func == NULL )
214 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
215
216 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
217 sig, sig_len, f_rng, p_rng ) );
218}
219
220/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200221 * Decrypt message
222 */
223int pk_decrypt( pk_context *ctx,
224 const unsigned char *input, size_t ilen,
225 unsigned char *output, size_t *olen, size_t osize,
226 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
227{
228 if( ctx == NULL || ctx->pk_info == NULL )
229 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
230
231 if( ctx->pk_info->decrypt_func == NULL )
232 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
233
234 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
235 output, olen, osize, f_rng, p_rng ) );
236}
237
238/*
239 * Encrypt message
240 */
241int pk_encrypt( pk_context *ctx,
242 const unsigned char *input, size_t ilen,
243 unsigned char *output, size_t *olen, size_t osize,
244 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
245{
246 if( ctx == NULL || ctx->pk_info == NULL )
247 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
248
249 if( ctx->pk_info->encrypt_func == NULL )
250 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
251
252 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
253 output, olen, osize, f_rng, p_rng ) );
254}
255
256/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200257 * Get key size in bits
258 */
259size_t pk_get_size( const pk_context *ctx )
260{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200261 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200262 return( 0 );
263
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200264 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200265}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266
267/*
268 * Export debug information
269 */
270int pk_debug( const pk_context *ctx, pk_debug_item *items )
271{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200272 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200273 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200274
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200275 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276 return( 0 );
277}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200278
279/*
280 * Access the PK type name
281 */
282const char * pk_get_name( const pk_context *ctx )
283{
284 if( ctx == NULL || ctx->pk_info == NULL )
285 return( "invalid PK" );
286
287 return( ctx->pk_info->name );
288}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200289
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200290/*
291 * Access the PK type
292 */
293pk_type_t pk_get_type( const pk_context *ctx )
294{
295 if( ctx == NULL || ctx->pk_info == NULL )
296 return( POLARSSL_PK_NONE );
297
298 return( ctx->pk_info->type );
299}
300
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200301#endif /* POLARSSL_PK_C */