blob: a75ab32481d91b30e67a8a16deeee2de8b9f0baf [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02005 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +020032#if defined(POLARSSL_PK_C)
33
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020034#include "polarssl/pk_wrap.h"
35
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020036/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#include "polarssl/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020038
39#if defined(POLARSSL_ECP_C)
40#include "polarssl/ecp.h"
41#endif
42
43#if defined(POLARSSL_ECDSA_C)
44#include "polarssl/ecdsa.h"
45#endif
46
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020049#else
50#include <stdlib.h>
51#define polarssl_malloc malloc
52#define polarssl_free free
53#endif
54
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
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é-Gonnard65ad3e42014-01-06 16:57:24 +0100346 /* Use deterministic ECDSA by default if available */
347#if defined(POLARSSL_ECDSA_DETERMINISTIC)
348 ((void) f_rng);
349 ((void) p_rng);
350
351 return( ecdsa_write_signature_det( (ecdsa_context *) ctx,
352 hash, hash_len, sig, sig_len, md_alg ) );
353#else
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200354 ((void) md_alg);
355
356 return( ecdsa_write_signature( (ecdsa_context *) ctx,
357 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Paul Bakker9af723c2014-05-01 13:03:14 +0200358#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200359}
360
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200361static void *ecdsa_alloc_wrap( void )
362{
363 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
364
365 if( ctx != NULL )
366 ecdsa_init( (ecdsa_context *) ctx );
367
368 return( ctx );
369}
370
371static void ecdsa_free_wrap( void *ctx )
372{
373 ecdsa_free( (ecdsa_context *) ctx );
374 polarssl_free( ctx );
375}
376
377const pk_info_t ecdsa_info = {
378 POLARSSL_PK_ECDSA,
379 "ECDSA",
380 eckey_get_size, /* Compatible key structures */
381 ecdsa_can_do,
382 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200383 ecdsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200384 NULL,
385 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100386 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200387 ecdsa_alloc_wrap,
388 ecdsa_free_wrap,
389 eckey_debug, /* Compatible key structures */
390};
391#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200392
393/*
394 * Support for alternative RSA-private implementations
395 */
396
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200397static int rsa_alt_can_do( pk_type_t type )
398{
399 return( type == POLARSSL_PK_RSA );
400}
401
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200402static size_t rsa_alt_get_size( const void *ctx )
403{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100404 const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200405
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200406 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200407}
408
409static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
410 const unsigned char *hash, size_t hash_len,
411 unsigned char *sig, size_t *sig_len,
412 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
413{
414 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
415
416 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
417
418 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200420}
421
422static int rsa_alt_decrypt_wrap( void *ctx,
423 const unsigned char *input, size_t ilen,
424 unsigned char *output, size_t *olen, size_t osize,
425 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
426{
427 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
428
429 ((void) f_rng);
430 ((void) p_rng);
431
432 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
433 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
434
435 return( rsa_alt->decrypt_func( rsa_alt->key,
436 RSA_PRIVATE, olen, input, output, osize ) );
437}
438
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100439static int rsa_alt_check_pair( const void *pub, const void *prv )
440{
441 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
442 unsigned char hash[32];
443 size_t sig_len = 0;
444 int ret;
445
446 if( rsa_alt_get_size( prv ) != rsa_get_size( pub ) )
447 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
448
449 memset( hash, 0x2a, sizeof( hash ) );
450
451 if( ( ret = rsa_alt_sign_wrap( (void *) prv, POLARSSL_MD_NONE,
452 hash, sizeof( hash ),
453 sig, &sig_len, NULL, NULL ) ) != 0 )
454 {
455 return( ret );
456 }
457
458 if( rsa_verify_wrap( (void *) pub, POLARSSL_MD_NONE,
459 hash, sizeof( hash ), sig, sig_len ) != 0 )
460 {
461 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
462 }
463
464 return( 0 );
465}
466
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200467static void *rsa_alt_alloc_wrap( void )
468{
469 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
470
471 if( ctx != NULL )
472 memset( ctx, 0, sizeof( rsa_alt_context ) );
473
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200474 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200475}
476
477static void rsa_alt_free_wrap( void *ctx )
478{
Paul Bakker34617722014-06-13 17:20:13 +0200479 polarssl_zeroize( ctx, sizeof( rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200480 polarssl_free( ctx );
481}
482
483const pk_info_t rsa_alt_info = {
484 POLARSSL_PK_RSA_ALT,
485 "RSA-alt",
486 rsa_alt_get_size,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200487 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200488 NULL,
489 rsa_alt_sign_wrap,
490 rsa_alt_decrypt_wrap,
491 NULL,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100492 rsa_alt_check_pair,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200493 rsa_alt_alloc_wrap,
494 rsa_alt_free_wrap,
495 NULL,
496};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200497
498#endif /* POLARSSL_PK_C */