blob: c2a4c7fce0e61919b57d3af03255f1b55e2dc2d4 [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é-Gonnard765db072013-08-14 15:00:27 +020072static void *rsa_alloc_wrap( void )
73{
74 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
75
76 if( ctx != NULL )
77 rsa_init( (rsa_context *) ctx, 0, 0 );
78
79 return ctx;
80}
81
82static void rsa_free_wrap( void *ctx )
83{
84 rsa_free( (rsa_context *) ctx );
85 polarssl_free( ctx );
86}
87
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020088static void rsa_debug( const void *ctx, pk_debug_item *items )
89{
90 items->type = POLARSSL_PK_DEBUG_MPI;
91 items->name = "rsa.N";
92 items->value = &( ((rsa_context *) ctx)->N );
93
94 items++;
95
96 items->type = POLARSSL_PK_DEBUG_MPI;
97 items->name = "rsa.E";
98 items->value = &( ((rsa_context *) ctx)->E );
99}
100
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200101const pk_info_t rsa_info = {
102 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200103 "RSA",
104 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200105 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200106 rsa_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200107 rsa_alloc_wrap,
108 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200109 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200110};
111#endif /* POLARSSL_RSA_C */
112
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200113#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200114/*
115 * Generic EC key
116 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200117static int eckey_can_do( pk_type_t type )
118{
119 return( type == POLARSSL_PK_ECKEY ||
120 type == POLARSSL_PK_ECKEY_DH ||
121 type == POLARSSL_PK_ECDSA );
122}
123
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200124static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200125{
126 return( ((ecp_keypair *) ctx)->grp.pbits );
127}
128
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200129#if defined(POLARSSL_ECDSA_C)
130/* Forward declaration */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200131static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
132 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200133 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200134
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200135static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
136 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200137 const unsigned char *sig, size_t sig_len )
138{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200139 int ret;
140 ecdsa_context ecdsa;
141
142 ecdsa_init( &ecdsa );
143
144 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200145 ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200146
147 ecdsa_free( &ecdsa );
148
149 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200150}
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200151#endif /* POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200152
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200153static void *eckey_alloc_wrap( void )
154{
155 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
156
157 if( ctx != NULL )
158 ecp_keypair_init( ctx );
159
160 return( ctx );
161}
162
163static void eckey_free_wrap( void *ctx )
164{
165 ecp_keypair_free( (ecp_keypair *) ctx );
166 polarssl_free( ctx );
167}
168
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200169static void eckey_debug( const void *ctx, pk_debug_item *items )
170{
171 items->type = POLARSSL_PK_DEBUG_ECP;
172 items->name = "eckey.Q";
173 items->value = &( ((ecp_keypair *) ctx)->Q );
174}
175
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200176const pk_info_t eckey_info = {
177 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200178 "EC",
179 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200180 eckey_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200181#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200182 eckey_verify_wrap,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200183#else
184 NULL,
185#endif
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200186 eckey_alloc_wrap,
187 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200188 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200189};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200190
191/*
192 * EC key resticted to ECDH
193 */
194static int eckeydh_can_do( pk_type_t type )
195{
196 return( type == POLARSSL_PK_ECKEY ||
197 type == POLARSSL_PK_ECKEY_DH );
198}
199
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200200const pk_info_t eckeydh_info = {
201 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200202 "EC_DH",
203 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200204 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200205 NULL,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200206 eckey_alloc_wrap, /* Same underlying key structure */
207 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200208 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200209};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200210#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200211
212#if defined(POLARSSL_ECDSA_C)
213static int ecdsa_can_do( pk_type_t type )
214{
215 return( type == POLARSSL_PK_ECDSA );
216}
217
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200218static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
219 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200220 const unsigned char *sig, size_t sig_len )
221{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200222 ((void) md_alg);
223
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200224 return( ecdsa_read_signature( (ecdsa_context *) ctx,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200225 hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200226}
227
228static void *ecdsa_alloc_wrap( void )
229{
230 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
231
232 if( ctx != NULL )
233 ecdsa_init( (ecdsa_context *) ctx );
234
235 return( ctx );
236}
237
238static void ecdsa_free_wrap( void *ctx )
239{
240 ecdsa_free( (ecdsa_context *) ctx );
241 polarssl_free( ctx );
242}
243
244const pk_info_t ecdsa_info = {
245 POLARSSL_PK_ECDSA,
246 "ECDSA",
247 eckey_get_size, /* Compatible key structures */
248 ecdsa_can_do,
249 ecdsa_verify_wrap,
250 ecdsa_alloc_wrap,
251 ecdsa_free_wrap,
252 eckey_debug, /* Compatible key structures */
253};
254#endif /* POLARSSL_ECDSA_C */