blob: 8bcba58744f236148dc2cc7fa0e54b31a5654798 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02007 *
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020028
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +020029#if defined(POLARSSL_PK_C)
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
Andres AGc71b7eb2017-01-19 11:24:33 +000043#include <limits.h>
Andres Amaya Garciae13b54e2017-08-04 13:49:29 +010044#include <stdint.h>
Andres AGc71b7eb2017-01-19 11:24:33 +000045
Paul Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
47static void polarssl_zeroize( void *v, size_t n ) {
48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051/*
52 * Initialise a pk_context
53 */
54void pk_init( pk_context *ctx )
55{
56 if( ctx == NULL )
57 return;
58
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020059 ctx->pk_info = NULL;
60 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061}
62
63/*
64 * Free (the components of) a pk_context
65 */
66void pk_free( pk_context *ctx )
67{
Paul Bakker66d5d072014-06-17 16:39:18 +020068 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 return;
70
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020071 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Paul Bakker34617722014-06-13 17:20:13 +020073 polarssl_zeroize( ctx, sizeof( pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020074}
75
76/*
77 * Get pk_info structure from type
78 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020079const pk_info_t * pk_info_from_type( pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020080{
81 switch( pk_type ) {
82#if defined(POLARSSL_RSA_C)
83 case POLARSSL_PK_RSA:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020084 return( &rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020085#endif
86#if defined(POLARSSL_ECP_C)
87 case POLARSSL_PK_ECKEY:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020088 return( &eckey_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020089 case POLARSSL_PK_ECKEY_DH:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020090 return( &eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020091#endif
92#if defined(POLARSSL_ECDSA_C)
93 case POLARSSL_PK_ECDSA:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020094 return( &ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020095#endif
Paul Bakker75342a62014-04-08 17:35:40 +020096 /* POLARSSL_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020097 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020098 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020099 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200100}
101
102/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200103 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200104 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200105int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200106{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200107 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200108 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200109
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200110 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200111 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200112
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200113 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200114
115 return( 0 );
116}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200117
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100118/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200119 * Initialize an RSA-alt context
120 */
121int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
122 pk_rsa_alt_decrypt_func decrypt_func,
123 pk_rsa_alt_sign_func sign_func,
124 pk_rsa_alt_key_len_func key_len_func )
125{
126 rsa_alt_context *rsa_alt;
127 const pk_info_t *info = &rsa_alt_info;
128
129 if( ctx == NULL || ctx->pk_info != NULL )
130 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
131
132 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
133 return( POLARSSL_ERR_PK_MALLOC_FAILED );
134
135 ctx->pk_info = info;
136
137 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
138
139 rsa_alt->key = key;
140 rsa_alt->decrypt_func = decrypt_func;
141 rsa_alt->sign_func = sign_func;
142 rsa_alt->key_len_func = key_len_func;
143
144 return( 0 );
145}
146
147/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200148 * Tell if a PK can do the operations of the given type
149 */
150int pk_can_do( pk_context *ctx, pk_type_t type )
151{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200152 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200153 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200154 return( 0 );
155
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200156 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200157}
158
159/*
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200160 * Helper for pk_sign and pk_verify
161 */
162static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
163{
164 const md_info_t *md_info;
165
166 if( *hash_len != 0 )
167 return( 0 );
168
169 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
170 return( -1 );
171
172 *hash_len = md_info->size;
173 return( 0 );
174}
175
176/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200177 * Verify a signature
178 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200179int pk_verify( pk_context *ctx, md_type_t md_alg,
180 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200181 const unsigned char *sig, size_t sig_len )
182{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200183 if( ctx == NULL || ctx->pk_info == NULL ||
184 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200185 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200186
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200187 if( ctx->pk_info->verify_func == NULL )
188 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
189
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200190 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200191 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200192}
193
194/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200195 * Verify a signature with options
196 */
197int pk_verify_ext( pk_type_t type, const void *options,
198 pk_context *ctx, md_type_t md_alg,
199 const unsigned char *hash, size_t hash_len,
200 const unsigned char *sig, size_t sig_len )
201{
202 if( ctx == NULL || ctx->pk_info == NULL )
203 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
204
205 if( ! pk_can_do( ctx, type ) )
206 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
207
208 if( type == POLARSSL_PK_RSASSA_PSS )
209 {
210#if defined(POLARSSL_RSA_C) && defined(POLARSSL_PKCS1_V21)
211 int ret;
212 const pk_rsassa_pss_options *pss_opts;
213
Andres Amaya Garciae13b54e2017-08-04 13:49:29 +0100214#if SIZE_MAX > UINT_MAX
Andres AGc71b7eb2017-01-19 11:24:33 +0000215 if( md_alg == POLARSSL_MD_NONE && UINT_MAX < hash_len )
216 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garciae13b54e2017-08-04 13:49:29 +0100217#endif /* SIZE_MAX > UINT_MAX */
Andres AGc71b7eb2017-01-19 11:24:33 +0000218
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200219 if( options == NULL )
220 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
221
222 pss_opts = (const pk_rsassa_pss_options *) options;
223
224 if( sig_len < pk_get_len( ctx ) )
225 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
226
227 ret = rsa_rsassa_pss_verify_ext( pk_rsa( *ctx ),
228 NULL, NULL, RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200229 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200230 pss_opts->mgf1_hash_id,
231 pss_opts->expected_salt_len,
232 sig );
233 if( ret != 0 )
234 return( ret );
235
236 if( sig_len > pk_get_len( ctx ) )
237 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
238
239 return( 0 );
240#else
241 return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
Andres AGc71b7eb2017-01-19 11:24:33 +0000242#endif /* POLARSSL_RSA_C && POLARSSL_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200243 }
244
245 /* General case: no options */
246 if( options != NULL )
247 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
248
249 return( pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
250}
251
252/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200253 * Make a signature
254 */
255int pk_sign( pk_context *ctx, md_type_t md_alg,
256 const unsigned char *hash, size_t hash_len,
257 unsigned char *sig, size_t *sig_len,
258 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
259{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200260 if( ctx == NULL || ctx->pk_info == NULL ||
261 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200262 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
263
264 if( ctx->pk_info->sign_func == NULL )
265 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
266
267 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
268 sig, sig_len, f_rng, p_rng ) );
269}
270
271/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200272 * Decrypt message
273 */
274int pk_decrypt( pk_context *ctx,
275 const unsigned char *input, size_t ilen,
276 unsigned char *output, size_t *olen, size_t osize,
277 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
278{
279 if( ctx == NULL || ctx->pk_info == NULL )
280 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
281
282 if( ctx->pk_info->decrypt_func == NULL )
283 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
284
285 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
286 output, olen, osize, f_rng, p_rng ) );
287}
288
289/*
290 * Encrypt message
291 */
292int pk_encrypt( pk_context *ctx,
293 const unsigned char *input, size_t ilen,
294 unsigned char *output, size_t *olen, size_t osize,
295 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
296{
297 if( ctx == NULL || ctx->pk_info == NULL )
298 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
299
300 if( ctx->pk_info->encrypt_func == NULL )
301 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
302
303 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
304 output, olen, osize, f_rng, p_rng ) );
305}
306
307/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100308 * Check public-private key pair
309 */
310int pk_check_pair( const pk_context *pub, const pk_context *prv )
311{
312 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100313 prv == NULL || prv->pk_info == NULL ||
314 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100315 {
316 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
317 }
318
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100319 if( prv->pk_info->type == POLARSSL_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100320 {
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100321 if( pub->pk_info->type != POLARSSL_PK_RSA )
322 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
323 }
324 else
325 {
326 if( pub->pk_info != prv->pk_info )
327 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100328 }
329
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100330 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100331}
332
333/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200334 * Get key size in bits
335 */
336size_t pk_get_size( const pk_context *ctx )
337{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200338 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200339 return( 0 );
340
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200341 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200342}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200343
344/*
345 * Export debug information
346 */
347int pk_debug( const pk_context *ctx, pk_debug_item *items )
348{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200349 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200350 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200351
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200352 if( ctx->pk_info->debug_func == NULL )
353 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
354
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200355 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200356 return( 0 );
357}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200358
359/*
360 * Access the PK type name
361 */
362const char * pk_get_name( const pk_context *ctx )
363{
364 if( ctx == NULL || ctx->pk_info == NULL )
365 return( "invalid PK" );
366
367 return( ctx->pk_info->name );
368}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200369
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200370/*
371 * Access the PK type
372 */
373pk_type_t pk_get_type( const pk_context *ctx )
374{
375 if( ctx == NULL || ctx->pk_info == NULL )
376 return( POLARSSL_PK_NONE );
377
378 return( ctx->pk_info->type );
379}
380
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200381#endif /* POLARSSL_PK_C */