blob: f8985912c8fdd3124b3dddd110250f5ac91b6636 [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
42#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020043static int rsa_can_do( pk_type_t type )
44{
45 return( type == POLARSSL_PK_RSA );
46}
47
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020048static size_t rsa_get_size( void * ctx )
49{
50 return( mpi_size( &((rsa_context *) ctx)->N ) * 8 );
51}
52
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020053static int rsa_verify_wrap( void *ctx,
54 const unsigned char *hash, const md_info_t *md_info,
55 const unsigned char *sig, size_t sig_len )
56{
57 ((void) sig_len);
58
59 return( rsa_pkcs1_verify( (rsa_context *) ctx,
60 RSA_PUBLIC, md_info->type, 0, hash, sig ) );
61}
62
63const pk_info_t rsa_info = {
64 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020065 "RSA",
66 rsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020067 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020068 rsa_verify_wrap,
69};
70#endif /* POLARSSL_RSA_C */
71
72#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020073int ecdsa_can_do( pk_type_t type )
74{
75 return( type == POLARSSL_PK_ECDSA );
76}
77
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020078static size_t ecdsa_get_size( void *ctx )
79{
80 return( ((ecdsa_context *) ctx)->grp.pbits );
81}
82
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020083int ecdsa_verify_wrap( void *ctx,
84 const unsigned char *hash, const md_info_t *md_info,
85 const unsigned char *sig, size_t sig_len )
86{
87 return( ecdsa_read_signature( (ecdsa_context *) ctx,
88 hash, md_info->size, sig, sig_len ) );
89}
90
91const pk_info_t ecdsa_info = {
92 POLARSSL_PK_ECDSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020093 "ECDSA",
94 ecdsa_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020095 ecdsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020096 ecdsa_verify_wrap,
97};
98#endif /* POLARSSL_ECDSA_C */
99
100#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200101/*
102 * Generic EC key
103 */
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200104static int eckey_can_do( pk_type_t type )
105{
106 return( type == POLARSSL_PK_ECKEY ||
107 type == POLARSSL_PK_ECKEY_DH ||
108 type == POLARSSL_PK_ECDSA );
109}
110
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200111static size_t eckey_get_size( void *ctx )
112{
113 return( ((ecp_keypair *) ctx)->grp.pbits );
114}
115
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200116static int eckey_verify_wrap( void *ctx,
117 const unsigned char *hash, const md_info_t *md_info,
118 const unsigned char *sig, size_t sig_len )
119{
120#if !defined(POLARSSL_ECDSA_C)
121 ((void) ctx);
122 ((void) hash);
123 ((void) md_info);
124 ((void) sig);
125 ((void) sig_len);
126
127 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
128#else
129 int ret;
130 ecdsa_context ecdsa;
131
132 ecdsa_init( &ecdsa );
133
134 ret = ecdsa_from_keypair( &ecdsa, ctx ) ||
135 ecdsa_verify_wrap( &ecdsa, hash, md_info, sig, sig_len );
136
137 ecdsa_free( &ecdsa );
138
139 return( ret );
140#endif /* POLARSSL_ECDSA_C */
141}
142
143const pk_info_t eckey_info = {
144 POLARSSL_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200145 "EC",
146 eckey_get_size,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200147 eckey_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200148 eckey_verify_wrap,
149};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200150
151/*
152 * EC key resticted to ECDH
153 */
154static int eckeydh_can_do( pk_type_t type )
155{
156 return( type == POLARSSL_PK_ECKEY ||
157 type == POLARSSL_PK_ECKEY_DH );
158}
159
160static int eckeydh_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 ((void) ctx);
165 ((void) hash);
166 ((void) md_info);
167 ((void) sig);
168 ((void) sig_len);
169
170 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
171}
172
173const pk_info_t eckeydh_info = {
174 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200175 "EC_DH",
176 eckey_get_size, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200177 eckeydh_can_do,
178 eckeydh_verify_wrap,
179};
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200180#endif /* POLARSSL_ECP_C */