blob: 71505ed2e52bb91057032ef30620732c73a0753a [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
36
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020037#include <stdlib.h>
38
39/*
40 * Initialise a pk_context
41 */
42void pk_init( pk_context *ctx )
43{
44 if( ctx == NULL )
45 return;
46
47 ctx->type = POLARSSL_PK_NONE;
48 ctx->data = NULL;
49}
50
51/*
52 * Free (the components of) a pk_context
53 */
54void pk_free( pk_context *ctx )
55{
56 if( ctx == NULL )
57 return;
58
59 switch( ctx->type )
60 {
61 case POLARSSL_PK_NONE:
62 break;
63
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020064#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065 case POLARSSL_PK_RSA:
66 rsa_free( ctx->data );
67 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020070#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071 case POLARSSL_PK_ECKEY:
72 case POLARSSL_PK_ECKEY_DH:
73 ecp_keypair_free( ctx->data );
74 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020075#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020076 }
77
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020078 free( ctx-> data );
79
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020080 ctx->type = POLARSSL_PK_NONE;
81 ctx->data = NULL;
82}
83
84/*
85 * Set a pk_context to a given type
86 */
87int pk_set_type( pk_context *ctx, pk_type_t type )
88{
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020089 size_t size = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020090
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020091 switch( type )
92 {
93#if defined(POLARSSL_RSA_C)
94 case POLARSSL_PK_RSA:
95 size = sizeof( rsa_context );
96 break;
97#endif
98
99#if defined(POLARSSL_ECP_C)
100 case POLARSSL_PK_ECKEY:
101 case POLARSSL_PK_ECKEY_DH:
102 size = sizeof( ecp_keypair );
103 break;
104#endif
105
106 case POLARSSL_PK_NONE:
107 ; /* Should not happen */
108 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200109
110 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200111 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200112
113 memset( ctx->data, 0, size );
114 ctx->type = type;
115
116 return( 0 );
117}