blob: e2c9bb1262e64c3c4d37ef7c2dbf1396eecfd086 [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é-Gonnardd73b3c12013-08-12 17:06:05 +020061static int rsa_verify_wrap( void *ctx,
62 const unsigned char *hash, const md_info_t *md_info,
63 const unsigned char *sig, size_t sig_len )
64{
65 ((void) sig_len);
66
67 return( rsa_pkcs1_verify( (rsa_context *) ctx,
68 RSA_PUBLIC, md_info->type, 0, hash, sig ) );
69}
70
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020071static void *rsa_alloc_wrap( void )
72{
73 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
74
75 if( ctx != NULL )
76 rsa_init( (rsa_context *) ctx, 0, 0 );
77
78 return ctx;
79}
80
81static void rsa_free_wrap( void *ctx )
82{
83 rsa_free( (rsa_context *) ctx );
84 polarssl_free( ctx );
85}
86
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +020087static void rsa_debug( const void *ctx, pk_debug_item *items )
88{
89 items->type = POLARSSL_PK_DEBUG_MPI;
90 items->name = "rsa.N";
91 items->value = &( ((rsa_context *) ctx)->N );
92
93 items++;
94
95 items->type = POLARSSL_PK_DEBUG_MPI;
96 items->name = "rsa.E";
97 items->value = &( ((rsa_context *) ctx)->E );
98}
99
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200100const pk_info_t rsa_info = {
101 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200102 "RSA",
103 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200104 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200105 rsa_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200106 rsa_alloc_wrap,
107 rsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200108 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200109};
110#endif /* POLARSSL_RSA_C */
111
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200112#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200113/*
114 * Generic EC key
115 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200116static int eckey_can_do( pk_type_t type )
117{
118 return( type == POLARSSL_PK_ECKEY ||
119 type == POLARSSL_PK_ECKEY_DH ||
120 type == POLARSSL_PK_ECDSA );
121}
122
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200123static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200124{
125 return( ((ecp_keypair *) ctx)->grp.pbits );
126}
127
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200128#if defined(POLARSSL_ECDSA_C)
129/* Forward declaration */
130static int ecdsa_verify_wrap( void *ctx,
131 const unsigned char *hash, const md_info_t *md_info,
132 const unsigned char *sig, size_t sig_len );
133#endif
134
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200135static int eckey_verify_wrap( void *ctx,
136 const unsigned char *hash, const md_info_t *md_info,
137 const unsigned char *sig, size_t sig_len )
138{
139#if !defined(POLARSSL_ECDSA_C)
140 ((void) ctx);
141 ((void) hash);
142 ((void) md_info);
143 ((void) sig);
144 ((void) sig_len);
145
146 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
147#else
148 int ret;
149 ecdsa_context ecdsa;
150
151 ecdsa_init( &ecdsa );
152
153 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
154 ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len );
155
156 ecdsa_free( &ecdsa );
157
158 return( ret );
159#endif /* POLARSSL_ECDSA_C */
160}
161
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200162static void *eckey_alloc_wrap( void )
163{
164 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
165
166 if( ctx != NULL )
167 ecp_keypair_init( ctx );
168
169 return( ctx );
170}
171
172static void eckey_free_wrap( void *ctx )
173{
174 ecp_keypair_free( (ecp_keypair *) ctx );
175 polarssl_free( ctx );
176}
177
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200178static void eckey_debug( const void *ctx, pk_debug_item *items )
179{
180 items->type = POLARSSL_PK_DEBUG_ECP;
181 items->name = "eckey.Q";
182 items->value = &( ((ecp_keypair *) ctx)->Q );
183}
184
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200185const pk_info_t eckey_info = {
186 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200187 "EC",
188 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200189 eckey_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200190 eckey_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200191 eckey_alloc_wrap,
192 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200193 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200194};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200195
196/*
197 * EC key resticted to ECDH
198 */
199static int eckeydh_can_do( pk_type_t type )
200{
201 return( type == POLARSSL_PK_ECKEY ||
202 type == POLARSSL_PK_ECKEY_DH );
203}
204
205static int eckeydh_verify_wrap( void *ctx,
206 const unsigned char *hash, const md_info_t *md_info,
207 const unsigned char *sig, size_t sig_len )
208{
209 ((void) ctx);
210 ((void) hash);
211 ((void) md_info);
212 ((void) sig);
213 ((void) sig_len);
214
215 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
216}
217
218const pk_info_t eckeydh_info = {
219 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200220 "EC_DH",
221 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200222 eckeydh_can_do,
223 eckeydh_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200224 eckey_alloc_wrap, /* Same underlying key structure */
225 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200226 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200227};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200228#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200229
230#if defined(POLARSSL_ECDSA_C)
231static int ecdsa_can_do( pk_type_t type )
232{
233 return( type == POLARSSL_PK_ECDSA );
234}
235
236static int ecdsa_verify_wrap( void *ctx,
237 const unsigned char *hash, const md_info_t *md_info,
238 const unsigned char *sig, size_t sig_len )
239{
240 return( ecdsa_read_signature( (ecdsa_context *) ctx,
241 hash, md_info->size, sig, sig_len ) );
242}
243
244static void *ecdsa_alloc_wrap( void )
245{
246 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
247
248 if( ctx != NULL )
249 ecdsa_init( (ecdsa_context *) ctx );
250
251 return( ctx );
252}
253
254static void ecdsa_free_wrap( void *ctx )
255{
256 ecdsa_free( (ecdsa_context *) ctx );
257 polarssl_free( ctx );
258}
259
260const pk_info_t ecdsa_info = {
261 POLARSSL_PK_ECDSA,
262 "ECDSA",
263 eckey_get_size, /* Compatible key structures */
264 ecdsa_can_do,
265 ecdsa_verify_wrap,
266 ecdsa_alloc_wrap,
267 ecdsa_free_wrap,
268 eckey_debug, /* Compatible key structures */
269};
270#endif /* POLARSSL_ECDSA_C */