blob: fc036d2c53a27f1ea86d95685dcb406b69bb3ee7 [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
Andres AGc71b7eb2017-01-19 11:24:33 +000033#include "polarssl/bignum.h"
34
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020035#if defined(POLARSSL_RSA_C)
36#include "polarssl/rsa.h"
37#endif
38#if defined(POLARSSL_ECP_C)
39#include "polarssl/ecp.h"
40#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020041#if defined(POLARSSL_ECDSA_C)
42#include "polarssl/ecdsa.h"
43#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020044
Andres AGc71b7eb2017-01-19 11:24:33 +000045#include <limits.h>
46
Paul Bakker34617722014-06-13 17:20:13 +020047/* Implementation that should never be optimized out by the compiler */
48static void polarssl_zeroize( void *v, size_t n ) {
49 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
50}
51
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020052/*
53 * Initialise a pk_context
54 */
55void pk_init( pk_context *ctx )
56{
57 if( ctx == NULL )
58 return;
59
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020060 ctx->pk_info = NULL;
61 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062}
63
64/*
65 * Free (the components of) a pk_context
66 */
67void pk_free( pk_context *ctx )
68{
Paul Bakker66d5d072014-06-17 16:39:18 +020069 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 return;
71
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020072 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020073
Paul Bakker34617722014-06-13 17:20:13 +020074 polarssl_zeroize( ctx, sizeof( pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020075}
76
77/*
78 * Get pk_info structure from type
79 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020080const pk_info_t * pk_info_from_type( pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020081{
82 switch( pk_type ) {
83#if defined(POLARSSL_RSA_C)
84 case POLARSSL_PK_RSA:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020085 return( &rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020086#endif
87#if defined(POLARSSL_ECP_C)
88 case POLARSSL_PK_ECKEY:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020089 return( &eckey_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020090 case POLARSSL_PK_ECKEY_DH:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020091 return( &eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020092#endif
93#if defined(POLARSSL_ECDSA_C)
94 case POLARSSL_PK_ECDSA:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020095 return( &ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020096#endif
Paul Bakker75342a62014-04-08 17:35:40 +020097 /* POLARSSL_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020098 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020099 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200100 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200101}
102
103/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200104 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200105 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200106int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200108 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200109 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200110
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200111 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200112 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200113
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200114 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200115
116 return( 0 );
117}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200118
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100119/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200120 * Initialize an RSA-alt context
121 */
122int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
123 pk_rsa_alt_decrypt_func decrypt_func,
124 pk_rsa_alt_sign_func sign_func,
125 pk_rsa_alt_key_len_func key_len_func )
126{
127 rsa_alt_context *rsa_alt;
128 const pk_info_t *info = &rsa_alt_info;
129
130 if( ctx == NULL || ctx->pk_info != NULL )
131 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
132
133 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
134 return( POLARSSL_ERR_PK_MALLOC_FAILED );
135
136 ctx->pk_info = info;
137
138 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
139
140 rsa_alt->key = key;
141 rsa_alt->decrypt_func = decrypt_func;
142 rsa_alt->sign_func = sign_func;
143 rsa_alt->key_len_func = key_len_func;
144
145 return( 0 );
146}
147
148/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200149 * Tell if a PK can do the operations of the given type
150 */
151int pk_can_do( pk_context *ctx, pk_type_t type )
152{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200153 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200154 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155 return( 0 );
156
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200157 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200158}
159
160/*
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200161 * Helper for pk_sign and pk_verify
162 */
163static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
164{
165 const md_info_t *md_info;
166
167 if( *hash_len != 0 )
168 return( 0 );
169
170 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
171 return( -1 );
172
173 *hash_len = md_info->size;
174 return( 0 );
175}
176
177/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200178 * Verify a signature
179 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200180int pk_verify( pk_context *ctx, md_type_t md_alg,
181 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200182 const unsigned char *sig, size_t sig_len )
183{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200184 if( ctx == NULL || ctx->pk_info == NULL ||
185 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200186 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200187
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200188 if( ctx->pk_info->verify_func == NULL )
189 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
190
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200191 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200192 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200193}
194
195/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200196 * Verify a signature with options
197 */
198int pk_verify_ext( pk_type_t type, const void *options,
199 pk_context *ctx, md_type_t md_alg,
200 const unsigned char *hash, size_t hash_len,
201 const unsigned char *sig, size_t sig_len )
202{
203 if( ctx == NULL || ctx->pk_info == NULL )
204 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
205
206 if( ! pk_can_do( ctx, type ) )
207 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
208
209 if( type == POLARSSL_PK_RSASSA_PSS )
210 {
211#if defined(POLARSSL_RSA_C) && defined(POLARSSL_PKCS1_V21)
212 int ret;
213 const pk_rsassa_pss_options *pss_opts;
214
Andres AGc71b7eb2017-01-19 11:24:33 +0000215#if defined(POLARSSL_HAVE_INT64)
216 if( md_alg == POLARSSL_MD_NONE && UINT_MAX < hash_len )
217 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
218#endif /* POLARSSL_HAVE_INT64 */
219
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200220 if( options == NULL )
221 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
222
223 pss_opts = (const pk_rsassa_pss_options *) options;
224
225 if( sig_len < pk_get_len( ctx ) )
226 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
227
228 ret = rsa_rsassa_pss_verify_ext( pk_rsa( *ctx ),
229 NULL, NULL, RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200230 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200231 pss_opts->mgf1_hash_id,
232 pss_opts->expected_salt_len,
233 sig );
234 if( ret != 0 )
235 return( ret );
236
237 if( sig_len > pk_get_len( ctx ) )
238 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
239
240 return( 0 );
241#else
242 return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
Andres AGc71b7eb2017-01-19 11:24:33 +0000243#endif /* POLARSSL_RSA_C && POLARSSL_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200244 }
245
246 /* General case: no options */
247 if( options != NULL )
248 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
249
250 return( pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
251}
252
253/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200254 * Make a signature
255 */
256int pk_sign( pk_context *ctx, md_type_t md_alg,
257 const unsigned char *hash, size_t hash_len,
258 unsigned char *sig, size_t *sig_len,
259 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
260{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200261 if( ctx == NULL || ctx->pk_info == NULL ||
262 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200263 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
264
265 if( ctx->pk_info->sign_func == NULL )
266 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
267
268 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
269 sig, sig_len, f_rng, p_rng ) );
270}
271
272/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200273 * Decrypt message
274 */
275int pk_decrypt( pk_context *ctx,
276 const unsigned char *input, size_t ilen,
277 unsigned char *output, size_t *olen, size_t osize,
278 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
279{
280 if( ctx == NULL || ctx->pk_info == NULL )
281 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
282
283 if( ctx->pk_info->decrypt_func == NULL )
284 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
285
286 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
287 output, olen, osize, f_rng, p_rng ) );
288}
289
290/*
291 * Encrypt message
292 */
293int pk_encrypt( pk_context *ctx,
294 const unsigned char *input, size_t ilen,
295 unsigned char *output, size_t *olen, size_t osize,
296 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
297{
298 if( ctx == NULL || ctx->pk_info == NULL )
299 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
300
301 if( ctx->pk_info->encrypt_func == NULL )
302 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
303
304 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
305 output, olen, osize, f_rng, p_rng ) );
306}
307
308/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100309 * Check public-private key pair
310 */
311int pk_check_pair( const pk_context *pub, const pk_context *prv )
312{
313 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100314 prv == NULL || prv->pk_info == NULL ||
315 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100316 {
317 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
318 }
319
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100320 if( prv->pk_info->type == POLARSSL_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100321 {
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100322 if( pub->pk_info->type != POLARSSL_PK_RSA )
323 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
324 }
325 else
326 {
327 if( pub->pk_info != prv->pk_info )
328 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100329 }
330
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100331 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100332}
333
334/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200335 * Get key size in bits
336 */
337size_t pk_get_size( const pk_context *ctx )
338{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200339 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200340 return( 0 );
341
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200342 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200343}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200344
345/*
346 * Export debug information
347 */
348int pk_debug( const pk_context *ctx, pk_debug_item *items )
349{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200350 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200351 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200352
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200353 if( ctx->pk_info->debug_func == NULL )
354 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
355
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200356 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200357 return( 0 );
358}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200359
360/*
361 * Access the PK type name
362 */
363const char * pk_get_name( const pk_context *ctx )
364{
365 if( ctx == NULL || ctx->pk_info == NULL )
366 return( "invalid PK" );
367
368 return( ctx->pk_info->name );
369}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200370
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200371/*
372 * Access the PK type
373 */
374pk_type_t pk_get_type( const pk_context *ctx )
375{
376 if( ctx == NULL || ctx->pk_info == NULL )
377 return( POLARSSL_PK_NONE );
378
379 return( ctx->pk_info->type );
380}
381
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200382#endif /* POLARSSL_PK_C */