blob: 239ff78e42314b954674a863ce95e2b2b5f51ee6 [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é-Gonnardd73b3c12013-08-12 17:06:05 +020087const pk_info_t rsa_info = {
88 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020089 "RSA",
90 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020091 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020092 rsa_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020093 rsa_alloc_wrap,
94 rsa_free_wrap,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020095};
96#endif /* POLARSSL_RSA_C */
97
98#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020099int ecdsa_can_do( pk_type_t type )
100{
101 return( type == POLARSSL_PK_ECDSA );
102}
103
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200104static size_t ecdsa_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200105{
106 return( ((ecdsa_context *) ctx)->grp.pbits );
107}
108
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200109int ecdsa_verify_wrap( void *ctx,
110 const unsigned char *hash, const md_info_t *md_info,
111 const unsigned char *sig, size_t sig_len )
112{
113 return( ecdsa_read_signature( (ecdsa_context *) ctx,
114 hash, md_info->size, sig, sig_len ) );
115}
116
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200117static void *ecdsa_alloc_wrap( void )
118{
119 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
120
121 if( ctx != NULL )
122 ecdsa_init( (ecdsa_context *) ctx );
123
124 return( ctx );
125}
126
127static void ecdsa_free_wrap( void *ctx )
128{
129 ecdsa_free( (ecdsa_context *) ctx );
130 polarssl_free( ctx );
131}
132
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200133const pk_info_t ecdsa_info = {
134 POLARSSL_PK_ECDSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200135 "ECDSA",
136 ecdsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200137 ecdsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200138 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200139 ecdsa_alloc_wrap,
140 ecdsa_free_wrap,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200141};
142#endif /* POLARSSL_ECDSA_C */
143
144#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200145/*
146 * Generic EC key
147 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200148static int eckey_can_do( pk_type_t type )
149{
150 return( type == POLARSSL_PK_ECKEY ||
151 type == POLARSSL_PK_ECKEY_DH ||
152 type == POLARSSL_PK_ECDSA );
153}
154
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155static size_t eckey_get_size( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200156{
157 return( ((ecp_keypair *) ctx)->grp.pbits );
158}
159
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200160static int eckey_verify_wrap( void *ctx,
161 const unsigned char *hash, const md_info_t *md_info,
162 const unsigned char *sig, size_t sig_len )
163{
164#if !defined(POLARSSL_ECDSA_C)
165 ((void) ctx);
166 ((void) hash);
167 ((void) md_info);
168 ((void) sig);
169 ((void) sig_len);
170
171 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
172#else
173 int ret;
174 ecdsa_context ecdsa;
175
176 ecdsa_init( &ecdsa );
177
178 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
179 ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len );
180
181 ecdsa_free( &ecdsa );
182
183 return( ret );
184#endif /* POLARSSL_ECDSA_C */
185}
186
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200187static void *eckey_alloc_wrap( void )
188{
189 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
190
191 if( ctx != NULL )
192 ecp_keypair_init( ctx );
193
194 return( ctx );
195}
196
197static void eckey_free_wrap( void *ctx )
198{
199 ecp_keypair_free( (ecp_keypair *) ctx );
200 polarssl_free( ctx );
201}
202
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200203const pk_info_t eckey_info = {
204 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200205 "EC",
206 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200207 eckey_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200208 eckey_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200209 eckey_alloc_wrap,
210 eckey_free_wrap,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200211};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200212
213/*
214 * EC key resticted to ECDH
215 */
216static int eckeydh_can_do( pk_type_t type )
217{
218 return( type == POLARSSL_PK_ECKEY ||
219 type == POLARSSL_PK_ECKEY_DH );
220}
221
222static int eckeydh_verify_wrap( void *ctx,
223 const unsigned char *hash, const md_info_t *md_info,
224 const unsigned char *sig, size_t sig_len )
225{
226 ((void) ctx);
227 ((void) hash);
228 ((void) md_info);
229 ((void) sig);
230 ((void) sig_len);
231
232 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
233}
234
235const pk_info_t eckeydh_info = {
236 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200237 "EC_DH",
238 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200239 eckeydh_can_do,
240 eckeydh_verify_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200241 eckey_alloc_wrap, /* Same underlying key structure */
242 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200243};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244#endif /* POLARSSL_ECP_C */