blob: 19bc79bbc6e4ba74a19701aa1570d70a97c34de1 [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;
61}
62
63/*
64 * Free (the components of) a pk_context
65 */
66void pk_free( pk_context *ctx )
67{
68 if( ctx == NULL )
69 return;
70
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020071#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020072 if( ctx->type == POLARSSL_PK_RSA )
73 rsa_free( ctx->data );
74 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020075#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020076#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020077 if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
78 ecp_keypair_free( ctx->data );
79 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020080#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020081#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020082 if( ctx->type == POLARSSL_PK_ECDSA )
83 ecdsa_free( ctx->data );
84 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020085#endif
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020086 {
87 ; /* guard for the else's above */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020088 }
89
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +020090 polarssl_free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020091
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020092 ctx->info = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093 ctx->type = POLARSSL_PK_NONE;
94 ctx->data = NULL;
95}
96
97/*
98 * Set a pk_context to a given type
99 */
100int pk_set_type( pk_context *ctx, pk_type_t type )
101{
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200102 size_t size;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200103 const pk_info_t *info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200104
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200105 if( ctx->type == type )
106 return( 0 );
107
108 if( ctx->type != POLARSSL_PK_NONE )
109 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
110
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200111#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200112 if( type == POLARSSL_PK_RSA )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200113 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200114 size = sizeof( rsa_context );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200115 info = &rsa_info;
116 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200117 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200118#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200119#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200120 if( type == POLARSSL_PK_ECKEY )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200121 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200122 size = sizeof( ecp_keypair );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200123 info = &eckey_info;
124 }
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200125 else if( type == POLARSSL_PK_ECKEY_DH )
126 {
127 size = sizeof( ecp_keypair );
128 info = &eckeydh_info;
129 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200130 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200131#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200132#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200133 if( type == POLARSSL_PK_ECDSA )
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200134 {
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200135 size = sizeof( ecdsa_context );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200136 info = &ecdsa_info;
137 }
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200138 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200139#endif
Manuel Pégourié-Gonnardc2c90032013-07-15 11:04:58 +0200140 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141
Paul Bakkerd9ca94a2013-07-25 11:25:09 +0200142 if( ( ctx->data = polarssl_malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200143 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144
145 memset( ctx->data, 0, size );
146 ctx->type = type;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200147 ctx->info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
149 return( 0 );
150}