blob: 994320f94970d0751352ca08a5cd1221f89122cb [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk_wrap.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020032/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020039#endif
40
41#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020043#endif
44
Paul Bakker7dc4c442014-02-01 22:50:26 +010045#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020047#else
48#include <stdlib.h>
49#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +020053#if defined(POLARSSL_PK_RSA_ALT_SUPPORT)
Paul Bakker34617722014-06-13 17:20:13 +020054/* Implementation that should never be optimized out by the compiler */
55static void polarssl_zeroize( void *v, size_t n ) {
56 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
57}
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +020058#endif
Paul Bakker34617722014-06-13 17:20:13 +020059
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020060#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020061static int rsa_can_do( pk_type_t type )
62{
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +020063 return( type == POLARSSL_PK_RSA ||
64 type == POLARSSL_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020065}
66
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020067static size_t rsa_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020068{
Paul Bakker8fc30b12013-11-25 13:29:43 +010069 return( 8 * ((const rsa_context *) ctx)->len );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020070}
71
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020072static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
73 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020074 const unsigned char *sig, size_t sig_len )
75{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020076 int ret;
77
78 if( sig_len < ((rsa_context *) ctx)->len )
Manuel Pégourié-Gonnardac4cd362013-08-14 20:20:41 +020079 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020080
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +020081 if( ( ret = rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL,
82 RSA_PUBLIC, md_alg,
83 (unsigned int) hash_len, hash, sig ) ) != 0 )
84 return( ret );
85
86 if( sig_len > ((rsa_context *) ctx)->len )
87 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
88
89 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020090}
91
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +020092static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
93 const unsigned char *hash, size_t hash_len,
94 unsigned char *sig, size_t *sig_len,
95 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
96{
97 *sig_len = ((rsa_context *) ctx)->len;
98
99 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200100 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200101}
102
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200103static int rsa_decrypt_wrap( void *ctx,
104 const unsigned char *input, size_t ilen,
105 unsigned char *output, size_t *olen, size_t osize,
106 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
107{
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200108 if( ilen != ((rsa_context *) ctx)->len )
109 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
110
Paul Bakker548957d2013-08-30 10:30:02 +0200111 return( rsa_pkcs1_decrypt( (rsa_context *) ctx, f_rng, p_rng,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200112 RSA_PRIVATE, olen, input, output, osize ) );
113}
114
115static int rsa_encrypt_wrap( void *ctx,
116 const unsigned char *input, size_t ilen,
117 unsigned char *output, size_t *olen, size_t osize,
118 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
119{
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200120 *olen = ((rsa_context *) ctx)->len;
121
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100122 if( *olen > osize )
123 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
124
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200125 return( rsa_pkcs1_encrypt( (rsa_context *) ctx,
126 f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) );
127}
128
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100129static int rsa_check_pair_wrap( const void *pub, const void *prv )
130{
131 return( rsa_check_pub_priv( (const rsa_context *) pub,
132 (const rsa_context *) prv ) );
133}
134
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200135static void *rsa_alloc_wrap( void )
136{
137 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
138
139 if( ctx != NULL )
140 rsa_init( (rsa_context *) ctx, 0, 0 );
141
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200142 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200143}
144
145static void rsa_free_wrap( void *ctx )
146{
147 rsa_free( (rsa_context *) ctx );
148 polarssl_free( ctx );
149}
150
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200151static void rsa_debug( const void *ctx, pk_debug_item *items )
152{
153 items->type = POLARSSL_PK_DEBUG_MPI;
154 items->name = "rsa.N";
155 items->value = &( ((rsa_context *) ctx)->N );
156
157 items++;
158
159 items->type = POLARSSL_PK_DEBUG_MPI;
160 items->name = "rsa.E";
161 items->value = &( ((rsa_context *) ctx)->E );
162}
163
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200164const pk_info_t rsa_info = {
165 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200166 "RSA",
167 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200168 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200169 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200170 rsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200171 rsa_decrypt_wrap,
172 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100173 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200174 rsa_alloc_wrap,
175 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200176 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200177};
178#endif /* POLARSSL_RSA_C */
179
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200180#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200181/*
182 * Generic EC key
183 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200184static int eckey_can_do( pk_type_t type )
185{
186 return( type == POLARSSL_PK_ECKEY ||
187 type == POLARSSL_PK_ECKEY_DH ||
188 type == POLARSSL_PK_ECDSA );
189}
190
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200191static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200192{
193 return( ((ecp_keypair *) ctx)->grp.pbits );
194}
195
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200196#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200197/* Forward declarations */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200198static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
199 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200200 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200201
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200202static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
203 const unsigned char *hash, size_t hash_len,
204 unsigned char *sig, size_t *sig_len,
205 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
206
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200207static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
208 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200209 const unsigned char *sig, size_t sig_len )
210{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200211 int ret;
212 ecdsa_context ecdsa;
213
214 ecdsa_init( &ecdsa );
215
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200216 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
217 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200218
219 ecdsa_free( &ecdsa );
220
221 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200222}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200223
224static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
225 const unsigned char *hash, size_t hash_len,
226 unsigned char *sig, size_t *sig_len,
227 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
228{
229 int ret;
230 ecdsa_context ecdsa;
231
232 ecdsa_init( &ecdsa );
233
234 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
235 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
236 f_rng, p_rng );
237
238 ecdsa_free( &ecdsa );
239
240 return( ret );
241}
242
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200243#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100245static int eckey_check_pair( const void *pub, const void *prv )
246{
247 return( ecp_check_pub_priv( (const ecp_keypair *) pub,
248 (const ecp_keypair *) prv ) );
249}
250
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200251static void *eckey_alloc_wrap( void )
252{
253 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
254
255 if( ctx != NULL )
256 ecp_keypair_init( ctx );
257
258 return( ctx );
259}
260
261static void eckey_free_wrap( void *ctx )
262{
263 ecp_keypair_free( (ecp_keypair *) ctx );
264 polarssl_free( ctx );
265}
266
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200267static void eckey_debug( const void *ctx, pk_debug_item *items )
268{
269 items->type = POLARSSL_PK_DEBUG_ECP;
270 items->name = "eckey.Q";
271 items->value = &( ((ecp_keypair *) ctx)->Q );
272}
273
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200274const pk_info_t eckey_info = {
275 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200276 "EC",
277 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200278 eckey_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200279#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200280 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200281 eckey_sign_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200282#else
283 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200284 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200285#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200286 NULL,
287 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100288 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200289 eckey_alloc_wrap,
290 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200292};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200293
294/*
Paul Bakker75342a62014-04-08 17:35:40 +0200295 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200296 */
297static int eckeydh_can_do( pk_type_t type )
298{
299 return( type == POLARSSL_PK_ECKEY ||
300 type == POLARSSL_PK_ECKEY_DH );
301}
302
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200303const pk_info_t eckeydh_info = {
304 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200305 "EC_DH",
306 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200307 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200308 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200309 NULL,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200310 NULL,
311 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100312 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200313 eckey_alloc_wrap, /* Same underlying key structure */
314 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200315 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200316};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200317#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200318
319#if defined(POLARSSL_ECDSA_C)
320static int ecdsa_can_do( pk_type_t type )
321{
322 return( type == POLARSSL_PK_ECDSA );
323}
324
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200325static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
326 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200327 const unsigned char *sig, size_t sig_len )
328{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200329 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200330 ((void) md_alg);
331
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200332 ret = ecdsa_read_signature( (ecdsa_context *) ctx,
333 hash, hash_len, sig, sig_len );
334
335 if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH )
336 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
337
338 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200339}
340
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200341static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
342 const unsigned char *hash, size_t hash_len,
343 unsigned char *sig, size_t *sig_len,
344 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
345{
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200346 return( ecdsa_write_signature( (ecdsa_context *) ctx,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200347 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200348}
349
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200350static void *ecdsa_alloc_wrap( void )
351{
352 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
353
354 if( ctx != NULL )
355 ecdsa_init( (ecdsa_context *) ctx );
356
357 return( ctx );
358}
359
360static void ecdsa_free_wrap( void *ctx )
361{
362 ecdsa_free( (ecdsa_context *) ctx );
363 polarssl_free( ctx );
364}
365
366const pk_info_t ecdsa_info = {
367 POLARSSL_PK_ECDSA,
368 "ECDSA",
369 eckey_get_size, /* Compatible key structures */
370 ecdsa_can_do,
371 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200372 ecdsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200373 NULL,
374 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100375 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200376 ecdsa_alloc_wrap,
377 ecdsa_free_wrap,
378 eckey_debug, /* Compatible key structures */
379};
380#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200381
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200382#if defined(POLARSSL_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200383/*
384 * Support for alternative RSA-private implementations
385 */
386
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200387static int rsa_alt_can_do( pk_type_t type )
388{
389 return( type == POLARSSL_PK_RSA );
390}
391
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200392static size_t rsa_alt_get_size( const void *ctx )
393{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100394 const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200395
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200396 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200397}
398
399static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
400 const unsigned char *hash, size_t hash_len,
401 unsigned char *sig, size_t *sig_len,
402 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
403{
404 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
405
406 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
407
408 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200409 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200410}
411
412static int rsa_alt_decrypt_wrap( void *ctx,
413 const unsigned char *input, size_t ilen,
414 unsigned char *output, size_t *olen, size_t osize,
415 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
416{
417 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
418
419 ((void) f_rng);
420 ((void) p_rng);
421
422 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
423 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
424
425 return( rsa_alt->decrypt_func( rsa_alt->key,
426 RSA_PRIVATE, olen, input, output, osize ) );
427}
428
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100429#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100430static int rsa_alt_check_pair( const void *pub, const void *prv )
431{
432 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
433 unsigned char hash[32];
434 size_t sig_len = 0;
435 int ret;
436
437 if( rsa_alt_get_size( prv ) != rsa_get_size( pub ) )
438 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
439
440 memset( hash, 0x2a, sizeof( hash ) );
441
442 if( ( ret = rsa_alt_sign_wrap( (void *) prv, POLARSSL_MD_NONE,
443 hash, sizeof( hash ),
444 sig, &sig_len, NULL, NULL ) ) != 0 )
445 {
446 return( ret );
447 }
448
449 if( rsa_verify_wrap( (void *) pub, POLARSSL_MD_NONE,
450 hash, sizeof( hash ), sig, sig_len ) != 0 )
451 {
452 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
453 }
454
455 return( 0 );
456}
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100457#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100458
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200459static void *rsa_alt_alloc_wrap( void )
460{
461 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
462
463 if( ctx != NULL )
464 memset( ctx, 0, sizeof( rsa_alt_context ) );
465
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200466 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200467}
468
469static void rsa_alt_free_wrap( void *ctx )
470{
Paul Bakker34617722014-06-13 17:20:13 +0200471 polarssl_zeroize( ctx, sizeof( rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200472 polarssl_free( ctx );
473}
474
475const pk_info_t rsa_alt_info = {
476 POLARSSL_PK_RSA_ALT,
477 "RSA-alt",
478 rsa_alt_get_size,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200479 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200480 NULL,
481 rsa_alt_sign_wrap,
482 rsa_alt_decrypt_wrap,
483 NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100484#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100485 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +0100486#else
487 NULL,
488#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200489 rsa_alt_alloc_wrap,
490 rsa_alt_free_wrap,
491 NULL,
492};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200493
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200494#endif /* POLARSSL_PK_RSA_ALT_SUPPORT */
495
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200496#endif /* POLARSSL_PK_C */