blob: ca99fc633561f27717b2b594fd44e9a059fae0f0 [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
28#include "polarssl/rsa.h"
29#include "polarssl/ecp.h"
30#include "polarssl/pk.h"
31
32#include <stdlib.h>
33
34/*
35 * Initialise a pk_context
36 */
37void pk_init( pk_context *ctx )
38{
39 if( ctx == NULL )
40 return;
41
42 ctx->type = POLARSSL_PK_NONE;
43 ctx->data = NULL;
44}
45
46/*
47 * Free (the components of) a pk_context
48 */
49void pk_free( pk_context *ctx )
50{
51 if( ctx == NULL )
52 return;
53
54 switch( ctx->type )
55 {
56 case POLARSSL_PK_NONE:
57 break;
58
59 case POLARSSL_PK_RSA:
60 rsa_free( ctx->data );
61 break;
62
63 case POLARSSL_PK_ECKEY:
64 case POLARSSL_PK_ECKEY_DH:
65 ecp_keypair_free( ctx->data );
66 break;
67 }
68
69 ctx->type = POLARSSL_PK_NONE;
70 ctx->data = NULL;
71}
72
73/*
74 * Set a pk_context to a given type
75 */
76int pk_set_type( pk_context *ctx, pk_type_t type )
77{
78 size_t size = type == POLARSSL_PK_RSA ? sizeof( rsa_context )
79 : type == POLARSSL_PK_ECKEY ? sizeof( ecp_keypair )
80 : type == POLARSSL_PK_ECKEY_DH ? sizeof( ecp_keypair )
81 : 0;
82
83 if( size == 0 )
84 return( 0 );
85
86 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020087 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020088
89 memset( ctx->data, 0, size );
90 ctx->type = type;
91
92 return( 0 );
93}