blob: eb91d895fd68f453288f69b4e23e1a329bcd15bb [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
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
26#include "polarssl/config.h"
27
28#include "polarssl/pk_wrap.h"
29
30#if defined(POLARSSL_RSA_C)
31#include "polarssl/rsa.h"
32#endif
33
34#if defined(POLARSSL_ECP_C)
35#include "polarssl/ecp.h"
36#endif
37
38#if defined(POLARSSL_ECDSA_C)
39#include "polarssl/ecdsa.h"
40#endif
41
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020042#if defined(POLARSSL_MEMORY_C)
43#include "polarssl/memory.h"
44#else
45#include <stdlib.h>
46#define polarssl_malloc malloc
47#define polarssl_free free
48#endif
49
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020050#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020051static int rsa_can_do( pk_type_t type )
52{
53 return( type == POLARSSL_PK_RSA );
54}
55
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +020056static size_t rsa_get_size( const void * ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020057{
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +020058 return( 8 * ((rsa_context *) ctx)->len );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020059}
60
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020061static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
62 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020063 const unsigned char *sig, size_t sig_len )
64{
Manuel Pégourié-Gonnardac4cd362013-08-14 20:20:41 +020065 if( sig_len != ((rsa_context *) ctx)->len )
66 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020067
68 return( rsa_pkcs1_verify( (rsa_context *) ctx,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +020069 RSA_PUBLIC, md_alg, hash_len, hash, sig ) );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020070}
71
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +020072static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
73 const unsigned char *hash, size_t hash_len,
74 unsigned char *sig, size_t *sig_len,
75 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
76{
77 *sig_len = ((rsa_context *) ctx)->len;
78
79 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
80 md_alg, hash_len, hash, sig ) );
81}
82
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020083static void *rsa_alloc_wrap( void )
84{
85 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
86
87 if( ctx != NULL )
88 rsa_init( (rsa_context *) ctx, 0, 0 );
89
90 return ctx;
91}
92
93static void rsa_free_wrap( void *ctx )
94{
95 rsa_free( (rsa_context *) ctx );
96 polarssl_free( ctx );
97}
98
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020099static void rsa_debug( const void *ctx, pk_debug_item *items )
100{
101 items->type = POLARSSL_PK_DEBUG_MPI;
102 items->name = "rsa.N";
103 items->value = &( ((rsa_context *) ctx)->N );
104
105 items++;
106
107 items->type = POLARSSL_PK_DEBUG_MPI;
108 items->name = "rsa.E";
109 items->value = &( ((rsa_context *) ctx)->E );
110}
111
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200112const pk_info_t rsa_info = {
113 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200114 "RSA",
115 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200116 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200117 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200118 rsa_sign_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119 rsa_alloc_wrap,
120 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200121 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200122};
123#endif /* POLARSSL_RSA_C */
124
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200125#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200126/*
127 * Generic EC key
128 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200129static int eckey_can_do( pk_type_t type )
130{
131 return( type == POLARSSL_PK_ECKEY ||
132 type == POLARSSL_PK_ECKEY_DH ||
133 type == POLARSSL_PK_ECDSA );
134}
135
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200136static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200137{
138 return( ((ecp_keypair *) ctx)->grp.pbits );
139}
140
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200141#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200142/* Forward declarations */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200143static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
144 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200145 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200146
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200147static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
148 const unsigned char *hash, size_t hash_len,
149 unsigned char *sig, size_t *sig_len,
150 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
151
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200152static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
153 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200154 const unsigned char *sig, size_t sig_len )
155{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200156 int ret;
157 ecdsa_context ecdsa;
158
159 ecdsa_init( &ecdsa );
160
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200161 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
162 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200163
164 ecdsa_free( &ecdsa );
165
166 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200167}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200168
169static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
170 const unsigned char *hash, size_t hash_len,
171 unsigned char *sig, size_t *sig_len,
172 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
173{
174 int ret;
175 ecdsa_context ecdsa;
176
177 ecdsa_init( &ecdsa );
178
179 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
180 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
181 f_rng, p_rng );
182
183 ecdsa_free( &ecdsa );
184
185 return( ret );
186}
187
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200188#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200189
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200190static void *eckey_alloc_wrap( void )
191{
192 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
193
194 if( ctx != NULL )
195 ecp_keypair_init( ctx );
196
197 return( ctx );
198}
199
200static void eckey_free_wrap( void *ctx )
201{
202 ecp_keypair_free( (ecp_keypair *) ctx );
203 polarssl_free( ctx );
204}
205
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200206static void eckey_debug( const void *ctx, pk_debug_item *items )
207{
208 items->type = POLARSSL_PK_DEBUG_ECP;
209 items->name = "eckey.Q";
210 items->value = &( ((ecp_keypair *) ctx)->Q );
211}
212
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200213const pk_info_t eckey_info = {
214 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200215 "EC",
216 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200217 eckey_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200218#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200219 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200220 eckey_sign_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200221#else
222 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200223 NULL,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200224#endif
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200225 eckey_alloc_wrap,
226 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200227 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200228};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200229
230/*
231 * EC key resticted to ECDH
232 */
233static int eckeydh_can_do( pk_type_t type )
234{
235 return( type == POLARSSL_PK_ECKEY ||
236 type == POLARSSL_PK_ECKEY_DH );
237}
238
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200239const pk_info_t eckeydh_info = {
240 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200241 "EC_DH",
242 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200243 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200244 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200245 NULL,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200246 eckey_alloc_wrap, /* Same underlying key structure */
247 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200248 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200249};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200250#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200251
252#if defined(POLARSSL_ECDSA_C)
253static int ecdsa_can_do( pk_type_t type )
254{
255 return( type == POLARSSL_PK_ECDSA );
256}
257
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200258static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
259 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200260 const unsigned char *sig, size_t sig_len )
261{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200262 ((void) md_alg);
263
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200264 return( ecdsa_read_signature( (ecdsa_context *) ctx,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200265 hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200266}
267
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200268static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
269 const unsigned char *hash, size_t hash_len,
270 unsigned char *sig, size_t *sig_len,
271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
272{
273 ((void) md_alg);
274
275 return( ecdsa_write_signature( (ecdsa_context *) ctx,
276 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
277}
278
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200279static void *ecdsa_alloc_wrap( void )
280{
281 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
282
283 if( ctx != NULL )
284 ecdsa_init( (ecdsa_context *) ctx );
285
286 return( ctx );
287}
288
289static void ecdsa_free_wrap( void *ctx )
290{
291 ecdsa_free( (ecdsa_context *) ctx );
292 polarssl_free( ctx );
293}
294
295const pk_info_t ecdsa_info = {
296 POLARSSL_PK_ECDSA,
297 "ECDSA",
298 eckey_get_size, /* Compatible key structures */
299 ecdsa_can_do,
300 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200301 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200302 ecdsa_alloc_wrap,
303 ecdsa_free_wrap,
304 eckey_debug, /* Compatible key structures */
305};
306#endif /* POLARSSL_ECDSA_C */