blob: a1c7c1d2e0ed12724e2f7ac4d215ed6d882e59b2 [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é-Gonnarddfab4c12014-01-22 15:45:04 +0100111#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200112/*
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100113 * Set RSA padding
114 */
115int pk_rsa_set_padding( pk_context *ctx, int padding, int hash_id )
116{
117 if( ctx == NULL || ctx->pk_info == NULL ||
118 ctx->pk_info->type != POLARSSL_PK_RSA )
119 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
120
121 rsa_set_padding( pk_rsa( *ctx ), padding, hash_id );
122
123 return( 0 );
124}
Manuel Pégourié-Gonnarddfab4c12014-01-22 15:45:04 +0100125#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100126
127/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200128 * Initialize an RSA-alt context
129 */
130int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
131 pk_rsa_alt_decrypt_func decrypt_func,
132 pk_rsa_alt_sign_func sign_func,
133 pk_rsa_alt_key_len_func key_len_func )
134{
135 rsa_alt_context *rsa_alt;
136 const pk_info_t *info = &rsa_alt_info;
137
138 if( ctx == NULL || ctx->pk_info != NULL )
139 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
140
141 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
142 return( POLARSSL_ERR_PK_MALLOC_FAILED );
143
144 ctx->pk_info = info;
145
146 rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
147
148 rsa_alt->key = key;
149 rsa_alt->decrypt_func = decrypt_func;
150 rsa_alt->sign_func = sign_func;
151 rsa_alt->key_len_func = key_len_func;
152
153 return( 0 );
154}
155
156/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200157 * Tell if a PK can do the operations of the given type
158 */
159int pk_can_do( pk_context *ctx, pk_type_t type )
160{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200161 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200162 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200163 return( 0 );
164
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200165 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200166}
167
168/*
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200169 * Helper for pk_sign and pk_verify
170 */
171static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
172{
173 const md_info_t *md_info;
174
175 if( *hash_len != 0 )
176 return( 0 );
177
178 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
179 return( -1 );
180
181 *hash_len = md_info->size;
182 return( 0 );
183}
184
185/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200186 * Verify a signature
187 */
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200188int pk_verify( pk_context *ctx, md_type_t md_alg,
189 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200190 const unsigned char *sig, size_t sig_len )
191{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200192 if( ctx == NULL || ctx->pk_info == NULL ||
193 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200194 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200195
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200196 if( ctx->pk_info->verify_func == NULL )
197 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
198
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200199 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200200 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200201}
202
203/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200204 * Make a signature
205 */
206int pk_sign( pk_context *ctx, md_type_t md_alg,
207 const unsigned char *hash, size_t hash_len,
208 unsigned char *sig, size_t *sig_len,
209 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
210{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200211 if( ctx == NULL || ctx->pk_info == NULL ||
212 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200213 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
214
215 if( ctx->pk_info->sign_func == NULL )
216 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
217
218 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
219 sig, sig_len, f_rng, p_rng ) );
220}
221
222/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200223 * Decrypt message
224 */
225int pk_decrypt( pk_context *ctx,
226 const unsigned char *input, size_t ilen,
227 unsigned char *output, size_t *olen, size_t osize,
228 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
229{
230 if( ctx == NULL || ctx->pk_info == NULL )
231 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
232
233 if( ctx->pk_info->decrypt_func == NULL )
234 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
235
236 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
237 output, olen, osize, f_rng, p_rng ) );
238}
239
240/*
241 * Encrypt message
242 */
243int pk_encrypt( pk_context *ctx,
244 const unsigned char *input, size_t ilen,
245 unsigned char *output, size_t *olen, size_t osize,
246 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
247{
248 if( ctx == NULL || ctx->pk_info == NULL )
249 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
250
251 if( ctx->pk_info->encrypt_func == NULL )
252 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
253
254 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
255 output, olen, osize, f_rng, p_rng ) );
256}
257
258/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200259 * Get key size in bits
260 */
261size_t pk_get_size( const pk_context *ctx )
262{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200263 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200264 return( 0 );
265
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200266 return( ctx->pk_info->get_size( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200267}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200268
269/*
270 * Export debug information
271 */
272int pk_debug( const pk_context *ctx, pk_debug_item *items )
273{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200274 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard15699382013-08-14 19:22:48 +0200275 return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200277 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 return( 0 );
279}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200280
281/*
282 * Access the PK type name
283 */
284const char * pk_get_name( const pk_context *ctx )
285{
286 if( ctx == NULL || ctx->pk_info == NULL )
287 return( "invalid PK" );
288
289 return( ctx->pk_info->name );
290}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200291
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200292/*
293 * Access the PK type
294 */
295pk_type_t pk_get_type( const pk_context *ctx )
296{
297 if( ctx == NULL || ctx->pk_info == NULL )
298 return( POLARSSL_PK_NONE );
299
300 return( ctx->pk_info->type );
301}
302
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200303#endif /* POLARSSL_PK_C */