blob: 0d2a368ad1f211a2209fff59e2fcbf590b3bfe3e [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{
120 ((void) osize);
121
122 *olen = ((rsa_context *) ctx)->len;
123
124 return( rsa_pkcs1_encrypt( (rsa_context *) ctx,
125 f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) );
126}
127
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100128static int rsa_check_pair_wrap( const void *pub, const void *prv )
129{
130 return( rsa_check_pub_priv( (const rsa_context *) pub,
131 (const rsa_context *) prv ) );
132}
133
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134static void *rsa_alloc_wrap( void )
135{
136 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
137
138 if( ctx != NULL )
139 rsa_init( (rsa_context *) ctx, 0, 0 );
140
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200141 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200142}
143
144static void rsa_free_wrap( void *ctx )
145{
146 rsa_free( (rsa_context *) ctx );
147 polarssl_free( ctx );
148}
149
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200150static void rsa_debug( const void *ctx, pk_debug_item *items )
151{
152 items->type = POLARSSL_PK_DEBUG_MPI;
153 items->name = "rsa.N";
154 items->value = &( ((rsa_context *) ctx)->N );
155
156 items++;
157
158 items->type = POLARSSL_PK_DEBUG_MPI;
159 items->name = "rsa.E";
160 items->value = &( ((rsa_context *) ctx)->E );
161}
162
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200163const pk_info_t rsa_info = {
164 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200165 "RSA",
166 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200167 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200168 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200169 rsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200170 rsa_decrypt_wrap,
171 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100172 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200173 rsa_alloc_wrap,
174 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200175 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200176};
177#endif /* POLARSSL_RSA_C */
178
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200179#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200180/*
181 * Generic EC key
182 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200183static int eckey_can_do( pk_type_t type )
184{
185 return( type == POLARSSL_PK_ECKEY ||
186 type == POLARSSL_PK_ECKEY_DH ||
187 type == POLARSSL_PK_ECDSA );
188}
189
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200190static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200191{
192 return( ((ecp_keypair *) ctx)->grp.pbits );
193}
194
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200195#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200196/* Forward declarations */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200197static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
198 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200199 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200200
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200201static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
202 const unsigned char *hash, size_t hash_len,
203 unsigned char *sig, size_t *sig_len,
204 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
205
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200206static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
207 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200208 const unsigned char *sig, size_t sig_len )
209{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200210 int ret;
211 ecdsa_context ecdsa;
212
213 ecdsa_init( &ecdsa );
214
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200215 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
216 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200217
218 ecdsa_free( &ecdsa );
219
220 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200221}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200222
223static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
224 const unsigned char *hash, size_t hash_len,
225 unsigned char *sig, size_t *sig_len,
226 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
227{
228 int ret;
229 ecdsa_context ecdsa;
230
231 ecdsa_init( &ecdsa );
232
233 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
234 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
235 f_rng, p_rng );
236
237 ecdsa_free( &ecdsa );
238
239 return( ret );
240}
241
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200242#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200243
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100244static int eckey_check_pair( const void *pub, const void *prv )
245{
246 return( ecp_check_pub_priv( (const ecp_keypair *) pub,
247 (const ecp_keypair *) prv ) );
248}
249
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200250static void *eckey_alloc_wrap( void )
251{
252 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
253
254 if( ctx != NULL )
255 ecp_keypair_init( ctx );
256
257 return( ctx );
258}
259
260static void eckey_free_wrap( void *ctx )
261{
262 ecp_keypair_free( (ecp_keypair *) ctx );
263 polarssl_free( ctx );
264}
265
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266static void eckey_debug( const void *ctx, pk_debug_item *items )
267{
268 items->type = POLARSSL_PK_DEBUG_ECP;
269 items->name = "eckey.Q";
270 items->value = &( ((ecp_keypair *) ctx)->Q );
271}
272
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200273const pk_info_t eckey_info = {
274 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200275 "EC",
276 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200277 eckey_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200278#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200279 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200280 eckey_sign_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200281#else
282 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200283 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200284#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200285 NULL,
286 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100287 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200288 eckey_alloc_wrap,
289 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200291};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200292
293/*
Paul Bakker75342a62014-04-08 17:35:40 +0200294 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200295 */
296static int eckeydh_can_do( pk_type_t type )
297{
298 return( type == POLARSSL_PK_ECKEY ||
299 type == POLARSSL_PK_ECKEY_DH );
300}
301
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200302const pk_info_t eckeydh_info = {
303 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200304 "EC_DH",
305 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200306 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200307 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200308 NULL,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200309 NULL,
310 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100311 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200312 eckey_alloc_wrap, /* Same underlying key structure */
313 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200314 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200315};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200316#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200317
318#if defined(POLARSSL_ECDSA_C)
319static int ecdsa_can_do( pk_type_t type )
320{
321 return( type == POLARSSL_PK_ECDSA );
322}
323
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200324static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
325 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200326 const unsigned char *sig, size_t sig_len )
327{
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200328 int ret;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200329 ((void) md_alg);
330
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200331 ret = ecdsa_read_signature( (ecdsa_context *) ctx,
332 hash, hash_len, sig, sig_len );
333
334 if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH )
335 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
336
337 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200338}
339
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200340static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
341 const unsigned char *hash, size_t hash_len,
342 unsigned char *sig, size_t *sig_len,
343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
344{
Manuel Pégourié-Gonnard65ad3e42014-01-06 16:57:24 +0100345 /* Use deterministic ECDSA by default if available */
346#if defined(POLARSSL_ECDSA_DETERMINISTIC)
347 ((void) f_rng);
348 ((void) p_rng);
349
350 return( ecdsa_write_signature_det( (ecdsa_context *) ctx,
351 hash, hash_len, sig, sig_len, md_alg ) );
352#else
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200353 ((void) md_alg);
354
355 return( ecdsa_write_signature( (ecdsa_context *) ctx,
356 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Paul Bakker9af723c2014-05-01 13:03:14 +0200357#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200358}
359
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200360static void *ecdsa_alloc_wrap( void )
361{
362 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
363
364 if( ctx != NULL )
365 ecdsa_init( (ecdsa_context *) ctx );
366
367 return( ctx );
368}
369
370static void ecdsa_free_wrap( void *ctx )
371{
372 ecdsa_free( (ecdsa_context *) ctx );
373 polarssl_free( ctx );
374}
375
376const pk_info_t ecdsa_info = {
377 POLARSSL_PK_ECDSA,
378 "ECDSA",
379 eckey_get_size, /* Compatible key structures */
380 ecdsa_can_do,
381 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200382 ecdsa_sign_wrap,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200383 NULL,
384 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100385 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200386 ecdsa_alloc_wrap,
387 ecdsa_free_wrap,
388 eckey_debug, /* Compatible key structures */
389};
390#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200391
392/*
393 * Support for alternative RSA-private implementations
394 */
395
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200396static int rsa_alt_can_do( pk_type_t type )
397{
398 return( type == POLARSSL_PK_RSA );
399}
400
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200401static size_t rsa_alt_get_size( const void *ctx )
402{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100403 const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200404
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200405 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200406}
407
408static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
409 const unsigned char *hash, size_t hash_len,
410 unsigned char *sig, size_t *sig_len,
411 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
412{
413 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
414
415 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
416
417 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200419}
420
421static int rsa_alt_decrypt_wrap( void *ctx,
422 const unsigned char *input, size_t ilen,
423 unsigned char *output, size_t *olen, size_t osize,
424 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
425{
426 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
427
428 ((void) f_rng);
429 ((void) p_rng);
430
431 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
432 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
433
434 return( rsa_alt->decrypt_func( rsa_alt->key,
435 RSA_PRIVATE, olen, input, output, osize ) );
436}
437
438static void *rsa_alt_alloc_wrap( void )
439{
440 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
441
442 if( ctx != NULL )
443 memset( ctx, 0, sizeof( rsa_alt_context ) );
444
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200445 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200446}
447
448static void rsa_alt_free_wrap( void *ctx )
449{
Paul Bakker34617722014-06-13 17:20:13 +0200450 polarssl_zeroize( ctx, sizeof( rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200451 polarssl_free( ctx );
452}
453
454const pk_info_t rsa_alt_info = {
455 POLARSSL_PK_RSA_ALT,
456 "RSA-alt",
457 rsa_alt_get_size,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200458 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200459 NULL,
460 rsa_alt_sign_wrap,
461 rsa_alt_decrypt_wrap,
462 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100463 NULL, /* No public key */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200464 rsa_alt_alloc_wrap,
465 rsa_alt_free_wrap,
466 NULL,
467};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200468
469#endif /* POLARSSL_PK_C */