blob: 80eccc911b107c48643e28e37d76031b414962f5 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
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
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +020028#if defined(POLARSSL_PK_C)
29
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020030#include "polarssl/pk.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031#include "polarssl/pk_wrap.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020032
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020033#if defined(POLARSSL_RSA_C)
34#include "polarssl/rsa.h"
35#endif
36#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020039#if defined(POLARSSL_ECDSA_C)
40#include "polarssl/ecdsa.h"
41#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020042
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020043/*
44 * Initialise a pk_context
45 */
46void pk_init( pk_context *ctx )
47{
48 if( ctx == NULL )
49 return;
50
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020051 ctx->pk_info = NULL;
52 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053}
54
55/*
56 * Free (the components of) a pk_context
57 */
58void pk_free( pk_context *ctx )
59{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020060 if( ctx == NULL || ctx->pk_info == NULL)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061 return;
62
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020063 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
64 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020065
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020066 ctx->pk_info = NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020067}
68
69/*
70 * Get pk_info structure from type
71 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020072const pk_info_t * pk_info_from_type( pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020073{
74 switch( pk_type ) {
75#if defined(POLARSSL_RSA_C)
76 case POLARSSL_PK_RSA:
77 return &rsa_info;
78#endif
79#if defined(POLARSSL_ECP_C)
80 case POLARSSL_PK_ECKEY:
81 return &eckey_info;
82 case POLARSSL_PK_ECKEY_DH:
83 return &eckeydh_info;
84#endif
85#if defined(POLARSSL_ECDSA_C)
86 case POLARSSL_PK_ECDSA:
87 return &ecdsa_info;
88#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020089 /* POLARSSL_PK_RSA_ALT ommited on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020090 default:
91 return NULL;
92 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093}
94
95/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020096 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020097 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020098int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020099{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200100 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200101 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200102
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200103 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200104 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200105
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200106 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200110
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100111/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200112 * Initialize an RSA-alt context
113 */
114int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
115 pk_rsa_alt_decrypt_func decrypt_func,
116 pk_rsa_alt_sign_func sign_func,
117 pk_rsa_alt_key_len_func key_len_func )
118{
119 rsa_alt_context *rsa_alt;
120 const pk_info_t *info = &rsa_alt_info;
121
122 if( ctx == NULL || ctx->pk_info != NULL )
123 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
124
125 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
126 return( POLARSSL_ERR_PK_MALLOC_FAILED );
127
128 ctx->pk_info = info;
129
130 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
131
132 rsa_alt->key = key;
133 rsa_alt->decrypt_func = decrypt_func;
134 rsa_alt->sign_func = sign_func;
135 rsa_alt->key_len_func = key_len_func;
136
137 return( 0 );
138}
139
140/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200141 * Tell if a PK can do the operations of the given type
142 */
143int pk_can_do( pk_context *ctx, pk_type_t type )
144{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200145 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200146 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200147 return( 0 );
148
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200149 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200150}
151
152/*
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200153 * Helper for pk_sign and pk_verify
154 */
155static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
156{
157 const md_info_t *md_info;
158
159 if( *hash_len != 0 )
160 return( 0 );
161
162 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
163 return( -1 );
164
165 *hash_len = md_info->size;
166 return( 0 );
167}
168
169/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200170 * Verify a signature
171 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200172int pk_verify( pk_context *ctx, md_type_t md_alg,
173 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200174 const unsigned char *sig, size_t sig_len )
175{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200176 if( ctx == NULL || ctx->pk_info == NULL ||
177 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200178 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200179
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200180 if( ctx->pk_info->verify_func == NULL )
181 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
182
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200183 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200184 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200185}
186
187/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200188 * Make a signature
189 */
190int pk_sign( pk_context *ctx, md_type_t md_alg,
191 const unsigned char *hash, size_t hash_len,
192 unsigned char *sig, size_t *sig_len,
193 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
194{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200195 if( ctx == NULL || ctx->pk_info == NULL ||
196 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200197 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
198
199 if( ctx->pk_info->sign_func == NULL )
200 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
201
202 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
203 sig, sig_len, f_rng, p_rng ) );
204}
205
206/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200207 * Decrypt message
208 */
209int pk_decrypt( pk_context *ctx,
210 const unsigned char *input, size_t ilen,
211 unsigned char *output, size_t *olen, size_t osize,
212 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
213{
214 if( ctx == NULL || ctx->pk_info == NULL )
215 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
216
217 if( ctx->pk_info->decrypt_func == NULL )
218 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
219
220 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
221 output, olen, osize, f_rng, p_rng ) );
222}
223
224/*
225 * Encrypt message
226 */
227int pk_encrypt( pk_context *ctx,
228 const unsigned char *input, size_t ilen,
229 unsigned char *output, size_t *olen, size_t osize,
230 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
231{
232 if( ctx == NULL || ctx->pk_info == NULL )
233 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
234
235 if( ctx->pk_info->encrypt_func == NULL )
236 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
237
238 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
239 output, olen, osize, f_rng, p_rng ) );
240}
241
242/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200243 * Get key size in bits
244 */
245size_t pk_get_size( const pk_context *ctx )
246{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200247 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200248 return( 0 );
249
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200250 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200251}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200252
253/*
254 * Export debug information
255 */
256int pk_debug( const pk_context *ctx, pk_debug_item *items )
257{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200258 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200259 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200260
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200261 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200262 return( 0 );
263}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200264
265/*
266 * Access the PK type name
267 */
268const char * pk_get_name( const pk_context *ctx )
269{
270 if( ctx == NULL || ctx->pk_info == NULL )
271 return( "invalid PK" );
272
273 return( ctx->pk_info->name );
274}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200275
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200276/*
277 * Access the PK type
278 */
279pk_type_t pk_get_type( const pk_context *ctx )
280{
281 if( ctx == NULL || ctx->pk_info == NULL )
282 return( POLARSSL_PK_NONE );
283
284 return( ctx->pk_info->type );
285}
286
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200287#endif /* POLARSSL_PK_C */