blob: b6b8218a049f01b5c72a05204c84f9f34ce1ef2e [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02007 *
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +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é-Gonnardd73b3c12013-08-12 17:06:05 +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é-Gonnardd73b3c12013-08-12 17:06:05 +020028
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +020029#if defined(POLARSSL_PK_C)
30
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031#include "polarssl/pk_wrap.h"
32
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020033/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020034#include "polarssl/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020035
36#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
39
40#if defined(POLARSSL_ECDSA_C)
41#include "polarssl/ecdsa.h"
42#endif
43
Paul Bakker7dc4c442014-02-01 22:50:26 +010044#if defined(POLARSSL_PLATFORM_C)
45#include "polarssl/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020046#else
47#include <stdlib.h>
48#define polarssl_malloc malloc
49#define polarssl_free free
50#endif
51
Paul Bakker34617722014-06-13 17:20:13 +020052/* Implementation that should never be optimized out by the compiler */
53static void polarssl_zeroize( void *v, size_t n ) {
54 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
55}
56
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020057#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020058static int rsa_can_do( pk_type_t type )
59{
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020060 return( type == POLARSSL_PK_RSA ||
61 type == POLARSSL_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020062}
63
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020064static size_t rsa_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020065{
Paul Bakker8fc30b12013-11-25 13:29:43 +010066 return( 8 * ((const rsa_context *) ctx)->len );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020067}
68
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020069static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
70 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020071 const unsigned char *sig, size_t sig_len )
72{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020073 int ret;
74
75 if( sig_len < ((rsa_context *) ctx)->len )
Manuel Pégourié-Gonnardac4cd362013-08-14 20:20:41 +020076 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020077
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020078 if( ( ret = rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL,
79 RSA_PUBLIC, md_alg,
80 (unsigned int) hash_len, hash, sig ) ) != 0 )
81 return( ret );
82
83 if( sig_len > ((rsa_context *) ctx)->len )
84 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
85
86 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020087}
88
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +020089static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
90 const unsigned char *hash, size_t hash_len,
91 unsigned char *sig, size_t *sig_len,
92 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
93{
94 *sig_len = ((rsa_context *) ctx)->len;
95
96 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +020098}
99
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200100static int rsa_decrypt_wrap( void *ctx,
101 const unsigned char *input, size_t ilen,
102 unsigned char *output, size_t *olen, size_t osize,
103 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
104{
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200105 if( ilen != ((rsa_context *) ctx)->len )
106 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
107
Paul Bakker548957d2013-08-30 10:30:02 +0200108 return( rsa_pkcs1_decrypt( (rsa_context *) ctx, f_rng, p_rng,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200109 RSA_PRIVATE, olen, input, output, osize ) );
110}
111
112static int rsa_encrypt_wrap( void *ctx,
113 const unsigned char *input, size_t ilen,
114 unsigned char *output, size_t *olen, size_t osize,
115 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
116{
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200117 *olen = ((rsa_context *) ctx)->len;
118
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100119 if( *olen > osize )
120 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
121
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200122 return( rsa_pkcs1_encrypt( (rsa_context *) ctx,
123 f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) );
124}
125
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100126static int rsa_check_pair_wrap( const void *pub, const void *prv )
127{
128 return( rsa_check_pub_priv( (const rsa_context *) pub,
129 (const rsa_context *) prv ) );
130}
131
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132static void *rsa_alloc_wrap( void )
133{
134 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
135
136 if( ctx != NULL )
137 rsa_init( (rsa_context *) ctx, 0, 0 );
138
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200139 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200140}
141
142static void rsa_free_wrap( void *ctx )
143{
144 rsa_free( (rsa_context *) ctx );
145 polarssl_free( ctx );
146}
147
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200148static void rsa_debug( const void *ctx, pk_debug_item *items )
149{
150 items->type = POLARSSL_PK_DEBUG_MPI;
151 items->name = "rsa.N";
152 items->value = &( ((rsa_context *) ctx)->N );
153
154 items++;
155
156 items->type = POLARSSL_PK_DEBUG_MPI;
157 items->name = "rsa.E";
158 items->value = &( ((rsa_context *) ctx)->E );
159}
160
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200161const pk_info_t rsa_info = {
162 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200163 "RSA",
164 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200165 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200166 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200167 rsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200168 rsa_decrypt_wrap,
169 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100170 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200171 rsa_alloc_wrap,
172 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200173 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200174};
175#endif /* POLARSSL_RSA_C */
176
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200177#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200178/*
179 * Generic EC key
180 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200181static int eckey_can_do( pk_type_t type )
182{
183 return( type == POLARSSL_PK_ECKEY ||
184 type == POLARSSL_PK_ECKEY_DH ||
185 type == POLARSSL_PK_ECDSA );
186}
187
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200188static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200189{
190 return( ((ecp_keypair *) ctx)->grp.pbits );
191}
192
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200193#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200194/* Forward declarations */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200195static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
196 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200197 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200198
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200199static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
200 const unsigned char *hash, size_t hash_len,
201 unsigned char *sig, size_t *sig_len,
202 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
203
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200204static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
205 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200206 const unsigned char *sig, size_t sig_len )
207{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200208 int ret;
209 ecdsa_context ecdsa;
210
211 ecdsa_init( &ecdsa );
212
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200213 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
214 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200215
216 ecdsa_free( &ecdsa );
217
218 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200219}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200220
221static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
222 const unsigned char *hash, size_t hash_len,
223 unsigned char *sig, size_t *sig_len,
224 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
225{
226 int ret;
227 ecdsa_context ecdsa;
228
229 ecdsa_init( &ecdsa );
230
231 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
232 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
233 f_rng, p_rng );
234
235 ecdsa_free( &ecdsa );
236
237 return( ret );
238}
239
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200240#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200241
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100242static int eckey_check_pair( const void *pub, const void *prv )
243{
244 return( ecp_check_pub_priv( (const ecp_keypair *) pub,
245 (const ecp_keypair *) prv ) );
246}
247
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200248static void *eckey_alloc_wrap( void )
249{
250 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
251
252 if( ctx != NULL )
253 ecp_keypair_init( ctx );
254
255 return( ctx );
256}
257
258static void eckey_free_wrap( void *ctx )
259{
260 ecp_keypair_free( (ecp_keypair *) ctx );
261 polarssl_free( ctx );
262}
263
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200264static void eckey_debug( const void *ctx, pk_debug_item *items )
265{
266 items->type = POLARSSL_PK_DEBUG_ECP;
267 items->name = "eckey.Q";
268 items->value = &( ((ecp_keypair *) ctx)->Q );
269}
270
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200271const pk_info_t eckey_info = {
272 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200273 "EC",
274 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200275 eckey_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200276#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200277 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200278 eckey_sign_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200279#else
280 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200281 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200282#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200283 NULL,
284 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100285 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200286 eckey_alloc_wrap,
287 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200288 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200289};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200290
291/*
Paul Bakker75342a62014-04-08 17:35:40 +0200292 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200293 */
294static int eckeydh_can_do( pk_type_t type )
295{
296 return( type == POLARSSL_PK_ECKEY ||
297 type == POLARSSL_PK_ECKEY_DH );
298}
299
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200300const pk_info_t eckeydh_info = {
301 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200302 "EC_DH",
303 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200304 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200305 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200306 NULL,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200307 NULL,
308 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100309 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200310 eckey_alloc_wrap, /* Same underlying key structure */
311 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200312 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200313};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200314#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200315
316#if defined(POLARSSL_ECDSA_C)
317static int ecdsa_can_do( pk_type_t type )
318{
319 return( type == POLARSSL_PK_ECDSA );
320}
321
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200322static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
323 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200324 const unsigned char *sig, size_t sig_len )
325{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200326 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200327 ((void) md_alg);
328
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200329 ret = ecdsa_read_signature( (ecdsa_context *) ctx,
330 hash, hash_len, sig, sig_len );
331
332 if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH )
333 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
334
335 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200336}
337
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200338static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
339 const unsigned char *hash, size_t hash_len,
340 unsigned char *sig, size_t *sig_len,
341 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
342{
Manuel Pégourié-Gonnard65ad3e42014-01-06 16:57:24 +0100343 /* Use deterministic ECDSA by default if available */
344#if defined(POLARSSL_ECDSA_DETERMINISTIC)
345 ((void) f_rng);
346 ((void) p_rng);
347
348 return( ecdsa_write_signature_det( (ecdsa_context *) ctx,
349 hash, hash_len, sig, sig_len, md_alg ) );
350#else
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200351 ((void) md_alg);
352
353 return( ecdsa_write_signature( (ecdsa_context *) ctx,
354 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Paul Bakker9af723c2014-05-01 13:03:14 +0200355#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200356}
357
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200358static void *ecdsa_alloc_wrap( void )
359{
360 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
361
362 if( ctx != NULL )
363 ecdsa_init( (ecdsa_context *) ctx );
364
365 return( ctx );
366}
367
368static void ecdsa_free_wrap( void *ctx )
369{
370 ecdsa_free( (ecdsa_context *) ctx );
371 polarssl_free( ctx );
372}
373
374const pk_info_t ecdsa_info = {
375 POLARSSL_PK_ECDSA,
376 "ECDSA",
377 eckey_get_size, /* Compatible key structures */
378 ecdsa_can_do,
379 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200380 ecdsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200381 NULL,
382 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100383 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200384 ecdsa_alloc_wrap,
385 ecdsa_free_wrap,
386 eckey_debug, /* Compatible key structures */
387};
388#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200389
390/*
391 * Support for alternative RSA-private implementations
392 */
393
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200394static int rsa_alt_can_do( pk_type_t type )
395{
396 return( type == POLARSSL_PK_RSA );
397}
398
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200399static size_t rsa_alt_get_size( const void *ctx )
400{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100401 const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200402
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200403 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200404}
405
406static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
407 const unsigned char *hash, size_t hash_len,
408 unsigned char *sig, size_t *sig_len,
409 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
410{
411 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
412
413 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
414
415 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200416 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200417}
418
419static int rsa_alt_decrypt_wrap( void *ctx,
420 const unsigned char *input, size_t ilen,
421 unsigned char *output, size_t *olen, size_t osize,
422 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
423{
424 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
425
426 ((void) f_rng);
427 ((void) p_rng);
428
429 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
430 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
431
432 return( rsa_alt->decrypt_func( rsa_alt->key,
433 RSA_PRIVATE, olen, input, output, osize ) );
434}
435
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100436#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100437static int rsa_alt_check_pair( const void *pub, const void *prv )
438{
439 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
440 unsigned char hash[32];
441 size_t sig_len = 0;
442 int ret;
443
444 if( rsa_alt_get_size( prv ) != rsa_get_size( pub ) )
445 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
446
447 memset( hash, 0x2a, sizeof( hash ) );
448
449 if( ( ret = rsa_alt_sign_wrap( (void *) prv, POLARSSL_MD_NONE,
450 hash, sizeof( hash ),
451 sig, &sig_len, NULL, NULL ) ) != 0 )
452 {
453 return( ret );
454 }
455
456 if( rsa_verify_wrap( (void *) pub, POLARSSL_MD_NONE,
457 hash, sizeof( hash ), sig, sig_len ) != 0 )
458 {
459 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
460 }
461
462 return( 0 );
463}
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100464#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100465
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200466static void *rsa_alt_alloc_wrap( void )
467{
468 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
469
470 if( ctx != NULL )
471 memset( ctx, 0, sizeof( rsa_alt_context ) );
472
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200473 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200474}
475
476static void rsa_alt_free_wrap( void *ctx )
477{
Paul Bakker34617722014-06-13 17:20:13 +0200478 polarssl_zeroize( ctx, sizeof( rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200479 polarssl_free( ctx );
480}
481
482const pk_info_t rsa_alt_info = {
483 POLARSSL_PK_RSA_ALT,
484 "RSA-alt",
485 rsa_alt_get_size,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200486 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200487 NULL,
488 rsa_alt_sign_wrap,
489 rsa_alt_decrypt_wrap,
490 NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100491#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100492 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100493#else
494 NULL,
495#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200496 rsa_alt_alloc_wrap,
497 rsa_alt_free_wrap,
498 NULL,
499};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200500
501#endif /* POLARSSL_PK_C */