blob: 284bd1dbd05d6283ffb7b7d525296c802c7a2d37 [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
112#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200113int ecdsa_can_do( pk_type_t type )
114{
115 return( type == POLARSSL_PK_ECDSA );
116}
117
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200118static size_t ecdsa_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200119{
120 return( ((ecdsa_context *) ctx)->grp.pbits );
121}
122
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200123int ecdsa_verify_wrap( void *ctx,
124 const unsigned char *hash, const md_info_t *md_info,
125 const unsigned char *sig, size_t sig_len )
126{
127 return( ecdsa_read_signature( (ecdsa_context *) ctx,
128 hash, md_info->size, sig, sig_len ) );
129}
130
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200131static void *ecdsa_alloc_wrap( void )
132{
133 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
134
135 if( ctx != NULL )
136 ecdsa_init( (ecdsa_context *) ctx );
137
138 return( ctx );
139}
140
141static void ecdsa_free_wrap( void *ctx )
142{
143 ecdsa_free( (ecdsa_context *) ctx );
144 polarssl_free( ctx );
145}
146
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200147const pk_info_t ecdsa_info = {
148 POLARSSL_PK_ECDSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200149 "ECDSA",
150 ecdsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200151 ecdsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200152 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200153 ecdsa_alloc_wrap,
154 ecdsa_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200155 NULL,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200156};
157#endif /* POLARSSL_ECDSA_C */
158
159#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200160/*
161 * Generic EC key
162 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200163static int eckey_can_do( pk_type_t type )
164{
165 return( type == POLARSSL_PK_ECKEY ||
166 type == POLARSSL_PK_ECKEY_DH ||
167 type == POLARSSL_PK_ECDSA );
168}
169
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200170static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200171{
172 return( ((ecp_keypair *) ctx)->grp.pbits );
173}
174
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200175static int eckey_verify_wrap( void *ctx,
176 const unsigned char *hash, const md_info_t *md_info,
177 const unsigned char *sig, size_t sig_len )
178{
179#if !defined(POLARSSL_ECDSA_C)
180 ((void) ctx);
181 ((void) hash);
182 ((void) md_info);
183 ((void) sig);
184 ((void) sig_len);
185
186 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
187#else
188 int ret;
189 ecdsa_context ecdsa;
190
191 ecdsa_init( &ecdsa );
192
193 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
194 ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len );
195
196 ecdsa_free( &ecdsa );
197
198 return( ret );
199#endif /* POLARSSL_ECDSA_C */
200}
201
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200202static void *eckey_alloc_wrap( void )
203{
204 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
205
206 if( ctx != NULL )
207 ecp_keypair_init( ctx );
208
209 return( ctx );
210}
211
212static void eckey_free_wrap( void *ctx )
213{
214 ecp_keypair_free( (ecp_keypair *) ctx );
215 polarssl_free( ctx );
216}
217
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200218static void eckey_debug( const void *ctx, pk_debug_item *items )
219{
220 items->type = POLARSSL_PK_DEBUG_ECP;
221 items->name = "eckey.Q";
222 items->value = &( ((ecp_keypair *) ctx)->Q );
223}
224
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200225const pk_info_t eckey_info = {
226 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200227 "EC",
228 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200229 eckey_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200230 eckey_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200231 eckey_alloc_wrap,
232 eckey_free_wrap,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200233 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200234};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200235
236/*
237 * EC key resticted to ECDH
238 */
239static int eckeydh_can_do( pk_type_t type )
240{
241 return( type == POLARSSL_PK_ECKEY ||
242 type == POLARSSL_PK_ECKEY_DH );
243}
244
245static int eckeydh_verify_wrap( void *ctx,
246 const unsigned char *hash, const md_info_t *md_info,
247 const unsigned char *sig, size_t sig_len )
248{
249 ((void) ctx);
250 ((void) hash);
251 ((void) md_info);
252 ((void) sig);
253 ((void) sig_len);
254
255 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
256}
257
258const pk_info_t eckeydh_info = {
259 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200260 "EC_DH",
261 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200262 eckeydh_can_do,
263 eckeydh_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200264 eckey_alloc_wrap, /* Same underlying key structure */
265 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266 NULL,
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200267};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200268#endif /* POLARSSL_ECP_C */