blob: c83d02bdd63cc2e10edbc9139d3f2f155e3e9528 [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
Paul Bakkerd9ca94a2013-07-25 11:25:09 +020041#if defined(POLARSSL_MEMORY_C)
42#include "polarssl/memory.h"
43#else
44#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020048#include <stdlib.h>
49
50/*
51 * Initialise a pk_context
52 */
53void pk_init( pk_context *ctx )
54{
55 if( ctx == NULL )
56 return;
57
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020058 ctx->info = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059 ctx->type = POLARSSL_PK_NONE;
60 ctx->data = NULL;
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020061 ctx->dont_free = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062}
63
64/*
65 * Free (the components of) a pk_context
66 */
67void pk_free( pk_context *ctx )
68{
69 if( ctx == NULL )
70 return;
71
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020072#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020073 if( ctx->type == POLARSSL_PK_RSA )
74 rsa_free( ctx->data );
75 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020076#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020077#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020078 if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
79 ecp_keypair_free( ctx->data );
80 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020081#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020082#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020083 if( ctx->type == POLARSSL_PK_ECDSA )
84 ecdsa_free( ctx->data );
85 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020086#endif
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020087 {
88 ; /* guard for the else's above */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020089 }
90
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020091 if( ! ctx->dont_free )
Paul Bakkerd9ca94a2013-07-25 11:25:09 +020092 polarssl_free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020093
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020094 ctx->info = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095 ctx->type = POLARSSL_PK_NONE;
96 ctx->data = NULL;
97}
98
99/*
100 * Set a pk_context to a given type
101 */
102int pk_set_type( pk_context *ctx, pk_type_t type )
103{
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200104 size_t size;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200105 const pk_info_t *info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200106
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200107 if( ctx->type == type )
108 return( 0 );
109
110 if( ctx->type != POLARSSL_PK_NONE )
111 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
112
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200113#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200114 if( type == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200115 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200116 size = sizeof( rsa_context );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200117 info = &rsa_info;
118 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200119 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200120#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200121#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200122 if( type == POLARSSL_PK_ECKEY )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200123 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200124 size = sizeof( ecp_keypair );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200125 info = &eckey_info;
126 }
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200127 else if( type == POLARSSL_PK_ECKEY_DH )
128 {
129 size = sizeof( ecp_keypair );
130 info = &eckeydh_info;
131 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200132 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200133#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200134#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200135 if( type == POLARSSL_PK_ECDSA )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200136 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200137 size = sizeof( ecdsa_context );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200138 info = &ecdsa_info;
139 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200140 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200141#endif
Manuel Pégourié-Gonnardc2c90032013-07-15 11:04:58 +0200142 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143
Paul Bakkerd9ca94a2013-07-25 11:25:09 +0200144 if( ( ctx->data = polarssl_malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200145 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146
147 memset( ctx->data, 0, size );
148 ctx->type = type;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200149 ctx->info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
151 return( 0 );
152}
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200153
154#if defined(POLARSSL_RSA_C)
155/*
156 * Wrap an RSA context in a PK context
157 */
158int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
159{
160 if( ctx->type != POLARSSL_PK_NONE )
161 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
162
163 ctx->type = POLARSSL_PK_RSA;
164 ctx->data = (rsa_context *) rsa;
165 ctx->dont_free = 1;
166
167 return( 0 );
168}
169#endif