blob: e0a252f2be422077c711e26c62cfbf43e7f684b0 [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é-Gonnard12e0ed92013-07-04 13:31:32 +020028#include "polarssl/pk.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020029#include "polarssl/pk_wrap.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020030
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020031#if defined(POLARSSL_RSA_C)
32#include "polarssl/rsa.h"
33#endif
34#if defined(POLARSSL_ECP_C)
35#include "polarssl/ecp.h"
36#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020037#if defined(POLARSSL_ECDSA_C)
38#include "polarssl/ecdsa.h"
39#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020040
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020041/*
42 * Initialise a pk_context
43 */
44void pk_init( pk_context *ctx )
45{
46 if( ctx == NULL )
47 return;
48
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020049 ctx->pk_info = NULL;
50 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051}
52
53/*
54 * Free (the components of) a pk_context
55 */
56void pk_free( pk_context *ctx )
57{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020058 if( ctx == NULL || ctx->pk_info == NULL)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059 return;
60
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020061 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
62 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020063
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020064 ctx->pk_info = NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020065}
66
67/*
68 * Get pk_info structure from type
69 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020070const pk_info_t * pk_info_from_type( pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020071{
72 switch( pk_type ) {
73#if defined(POLARSSL_RSA_C)
74 case POLARSSL_PK_RSA:
75 return &rsa_info;
76#endif
77#if defined(POLARSSL_ECP_C)
78 case POLARSSL_PK_ECKEY:
79 return &eckey_info;
80 case POLARSSL_PK_ECKEY_DH:
81 return &eckeydh_info;
82#endif
83#if defined(POLARSSL_ECDSA_C)
84 case POLARSSL_PK_ECDSA:
85 return &ecdsa_info;
86#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +020087 /* POLARSSL_PK_RSA_ALT ommited on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020088 default:
89 return NULL;
90 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020091}
92
93/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020094 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095 */
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020096int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020097{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +020098 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +020099 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200100
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200101 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200102 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200103
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200104 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200105
106 return( 0 );
107}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200108
109/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200110 * Initialize an RSA-alt context
111 */
112int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
113 pk_rsa_alt_decrypt_func decrypt_func,
114 pk_rsa_alt_sign_func sign_func,
115 pk_rsa_alt_key_len_func key_len_func )
116{
117 rsa_alt_context *rsa_alt;
118 const pk_info_t *info = &rsa_alt_info;
119
120 if( ctx == NULL || ctx->pk_info != NULL )
121 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
122
123 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
124 return( POLARSSL_ERR_PK_MALLOC_FAILED );
125
126 ctx->pk_info = info;
127
128 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
129
130 rsa_alt->key = key;
131 rsa_alt->decrypt_func = decrypt_func;
132 rsa_alt->sign_func = sign_func;
133 rsa_alt->key_len_func = key_len_func;
134
135 return( 0 );
136}
137
138/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200139 * Tell if a PK can do the operations of the given type
140 */
141int pk_can_do( pk_context *ctx, pk_type_t type )
142{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200143 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200144 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200145 return( 0 );
146
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200147 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200148}
149
150/*
151 * Verify a signature
152 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200153int pk_verify( pk_context *ctx, md_type_t md_alg,
154 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155 const unsigned char *sig, size_t sig_len )
156{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200157 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200158 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200159
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200160 if( ctx->pk_info->verify_func == NULL )
161 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
162
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200163 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200164 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200165}
166
167/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200168 * Make a signature
169 */
170int pk_sign( pk_context *ctx, md_type_t md_alg,
171 const unsigned char *hash, size_t hash_len,
172 unsigned char *sig, size_t *sig_len,
173 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
174{
175 if( ctx == NULL || ctx->pk_info == NULL )
176 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
177
178 if( ctx->pk_info->sign_func == NULL )
179 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
180
181 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
182 sig, sig_len, f_rng, p_rng ) );
183}
184
185/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200186 * Decrypt message
187 */
188int pk_decrypt( pk_context *ctx,
189 const unsigned char *input, size_t ilen,
190 unsigned char *output, size_t *olen, size_t osize,
191 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
192{
193 if( ctx == NULL || ctx->pk_info == NULL )
194 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
195
196 if( ctx->pk_info->decrypt_func == NULL )
197 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
198
199 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
200 output, olen, osize, f_rng, p_rng ) );
201}
202
203/*
204 * Encrypt message
205 */
206int pk_encrypt( pk_context *ctx,
207 const unsigned char *input, size_t ilen,
208 unsigned char *output, size_t *olen, size_t osize,
209 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
210{
211 if( ctx == NULL || ctx->pk_info == NULL )
212 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
213
214 if( ctx->pk_info->encrypt_func == NULL )
215 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
216
217 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
218 output, olen, osize, f_rng, p_rng ) );
219}
220
221/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200222 * Get key size in bits
223 */
224size_t pk_get_size( const pk_context *ctx )
225{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200226 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200227 return( 0 );
228
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200229 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200230}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200231
232/*
233 * Export debug information
234 */
235int pk_debug( const pk_context *ctx, pk_debug_item *items )
236{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200237 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200238 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200239
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200240 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200241 return( 0 );
242}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200243
244/*
245 * Access the PK type name
246 */
247const char * pk_get_name( const pk_context *ctx )
248{
249 if( ctx == NULL || ctx->pk_info == NULL )
250 return( "invalid PK" );
251
252 return( ctx->pk_info->name );
253}