blob: c5583c3638899f6cc5a56295dd9d329e1539b7de [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"
29
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020030#if defined(POLARSSL_RSA_C)
31#include "polarssl/rsa.h"
32#endif
33#if defined(POLARSSL_ECP_C)
34#include "polarssl/ecp.h"
35#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020036#if defined(POLARSSL_ECDSA_C)
37#include "polarssl/ecdsa.h"
38#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020039
Paul Bakkerd9ca94a2013-07-25 11:25:09 +020040#if defined(POLARSSL_MEMORY_C)
41#include "polarssl/memory.h"
42#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020047#include <stdlib.h>
48
49/*
50 * Initialise a pk_context
51 */
52void pk_init( pk_context *ctx )
53{
54 if( ctx == NULL )
55 return;
56
57 ctx->type = POLARSSL_PK_NONE;
58 ctx->data = NULL;
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020059 ctx->dont_free = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060}
61
62/*
63 * Free (the components of) a pk_context
64 */
65void pk_free( pk_context *ctx )
66{
67 if( ctx == NULL )
68 return;
69
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020070#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020071 if( ctx->type == POLARSSL_PK_RSA )
72 rsa_free( ctx->data );
73 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020074#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020075#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020076 if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
77 ecp_keypair_free( ctx->data );
78 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020079#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020080#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020081 if( ctx->type == POLARSSL_PK_ECDSA )
82 ecdsa_free( ctx->data );
83 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020084#endif
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020085 {
86 ; /* guard for the else's above */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020087 }
88
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020089 if( ! ctx->dont_free )
Paul Bakkerd9ca94a2013-07-25 11:25:09 +020090 polarssl_free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020091
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020092 ctx->type = POLARSSL_PK_NONE;
93 ctx->data = NULL;
94}
95
96/*
97 * Set a pk_context to a given type
98 */
99int pk_set_type( pk_context *ctx, pk_type_t type )
100{
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200101 size_t size;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200102
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200103 if( ctx->type == type )
104 return( 0 );
105
106 if( ctx->type != POLARSSL_PK_NONE )
107 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
108
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200109#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200110 if( type == POLARSSL_PK_RSA )
111 size = sizeof( rsa_context );
112 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200113#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200114#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200115 if( type == POLARSSL_PK_ECKEY || type == POLARSSL_PK_ECKEY_DH )
116 size = sizeof( ecp_keypair );
117 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200118#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200119#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200120 if( type == POLARSSL_PK_ECDSA )
121 size = sizeof( ecdsa_context );
122 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200123#endif
Manuel Pégourié-Gonnardc2c90032013-07-15 11:04:58 +0200124 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200125
Paul Bakkerd9ca94a2013-07-25 11:25:09 +0200126 if( ( ctx->data = polarssl_malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200127 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128
129 memset( ctx->data, 0, size );
130 ctx->type = type;
131
132 return( 0 );
133}
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200134
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +0200135#if defined(POLARSSL_ECDSA_C)
136/*
137 * Convert generic EC context to ECDSA
138 */
139int pk_ec_to_ecdsa( pk_context *ctx )
140{
141 ecp_keypair *eckey;
142 ecdsa_context *ecdsa;
143
144 if( ctx->type == POLARSSL_PK_ECDSA )
145 return( 0 );
146
147 if( ctx->type != POLARSSL_PK_ECKEY )
148 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
149
150 eckey = (ecp_keypair *) ctx->data;
151
152 if( ( ecdsa = polarssl_malloc( sizeof( ecdsa_context ) ) ) == NULL )
153 return( POLARSSL_ERR_PK_MALLOC_FAILED );
154
155 ecdsa_init( ecdsa );
156
157 /* struct ecdsa_context begins the same as struct ecp_keypair */
158 memcpy( ecdsa, eckey, sizeof( ecp_keypair ) );
159
160 if( ! ctx->dont_free )
161 polarssl_free( eckey );
162
163 ctx->dont_free = 0;
164 ctx->type = POLARSSL_PK_ECDSA;
165 ctx->data = ecdsa;
166
167 return( 0 );
168}
169#endif /* POLARSSL_ECDSA_C */
170
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200171#if defined(POLARSSL_RSA_C)
172/*
173 * Wrap an RSA context in a PK context
174 */
175int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
176{
177 if( ctx->type != POLARSSL_PK_NONE )
178 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
179
180 ctx->type = POLARSSL_PK_RSA;
181 ctx->data = (rsa_context *) rsa;
182 ctx->dont_free = 1;
183
184 return( 0 );
185}
186#endif