blob: f3c64cb4265884954f94eeb2b299e03619fa1f12 [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->data = NULL;
60}
61
62/*
63 * Free (the components of) a pk_context
64 */
65void pk_free( pk_context *ctx )
66{
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020067 if( ctx == NULL || ctx->info == NULL)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068 return;
69
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020070 ctx->info->ctx_free_func( ctx->data );
71 ctx->data = NULL;
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020073 ctx->info = NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020074}
75
76/*
77 * Get pk_info structure from type
78 */
79static const pk_info_t * pk_info_from_type( pk_type_t pk_type )
80{
81 switch( pk_type ) {
82#if defined(POLARSSL_RSA_C)
83 case POLARSSL_PK_RSA:
84 return &rsa_info;
85#endif
86#if defined(POLARSSL_ECP_C)
87 case POLARSSL_PK_ECKEY:
88 return &eckey_info;
89 case POLARSSL_PK_ECKEY_DH:
90 return &eckeydh_info;
91#endif
92#if defined(POLARSSL_ECDSA_C)
93 case POLARSSL_PK_ECDSA:
94 return &ecdsa_info;
95#endif
96 default:
97 return NULL;
98 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020099}
100
101/*
102 * Set a pk_context to a given type
103 */
104int pk_set_type( pk_context *ctx, pk_type_t type )
105{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200106 const pk_info_t *info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200108 if( ctx->info != NULL )
109 {
110 if( ctx->info->type == type )
111 return 0;
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200112
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200113 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200114 }
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200115
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200116 if( ( info = pk_info_from_type( type ) ) == NULL )
Manuel Pégourié-Gonnardc2c90032013-07-15 11:04:58 +0200117 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200118
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119 if( ( ctx->data = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200120 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200121
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200122 ctx->info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200123
124 return( 0 );
125}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200126
127/*
128 * Tell if a PK can do the operations of the given type
129 */
130int pk_can_do( pk_context *ctx, pk_type_t type )
131{
132 /* null of NONE context can't do anything */
133 if( ctx == NULL || ctx->info == NULL )
134 return( 0 );
135
136 return( ctx->info->can_do( type ) );
137}
138
139/*
140 * Verify a signature
141 */
142int pk_verify( pk_context *ctx,
143 const unsigned char *hash, const md_info_t *md_info,
144 const unsigned char *sig, size_t sig_len )
145{
146 if( ctx == NULL || ctx->info == NULL )
147 return( POLARSSL_ERR_PK_TYPE_MISMATCH ); // TODO
148
149 return( ctx->info->verify_func( ctx->data, hash, md_info, sig, sig_len ) );
150}
151
152/*
153 * Get key size in bits
154 */
155size_t pk_get_size( const pk_context *ctx )
156{
157 if( ctx == NULL || ctx->info == NULL )
158 return( 0 );
159
160 return( ctx->info->get_size( ctx->data ) );
161}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200162
163/*
164 * Export debug information
165 */
166int pk_debug( const pk_context *ctx, pk_debug_item *items )
167{
168 if( ctx == NULL || ctx->info == NULL )
169 return( POLARSSL_ERR_PK_TYPE_MISMATCH ); // TODO
170
171 ctx->info->debug_func( ctx->data, items );
172 return( 0 );
173}