Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 28 | #include "polarssl/pk.h" |
| 29 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 30 | #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é-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 36 | #if defined(POLARSSL_ECDSA_C) |
| 37 | #include "polarssl/ecdsa.h" |
| 38 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 39 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 40 | #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é-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 47 | #include <stdlib.h> |
| 48 | |
| 49 | /* |
| 50 | * Initialise a pk_context |
| 51 | */ |
| 52 | void 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é-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 59 | ctx->dont_free = 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Free (the components of) a pk_context |
| 64 | */ |
| 65 | void pk_free( pk_context *ctx ) |
| 66 | { |
| 67 | if( ctx == NULL ) |
| 68 | return; |
| 69 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 70 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 71 | if( ctx->type == POLARSSL_PK_RSA ) |
| 72 | rsa_free( ctx->data ); |
| 73 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 74 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 75 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 76 | if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH ) |
| 77 | ecp_keypair_free( ctx->data ); |
| 78 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 79 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 80 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 81 | if( ctx->type == POLARSSL_PK_ECDSA ) |
| 82 | ecdsa_free( ctx->data ); |
| 83 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 84 | #endif |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 85 | { |
| 86 | ; /* guard for the else's above */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 89 | if( ! ctx->dont_free ) |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 90 | polarssl_free( ctx->data ); |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 92 | ctx->type = POLARSSL_PK_NONE; |
| 93 | ctx->data = NULL; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Set a pk_context to a given type |
| 98 | */ |
| 99 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 100 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 101 | size_t size; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 102 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 103 | 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é-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 109 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 110 | if( type == POLARSSL_PK_RSA ) |
| 111 | size = sizeof( rsa_context ); |
| 112 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 113 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 114 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 115 | if( type == POLARSSL_PK_ECKEY || type == POLARSSL_PK_ECKEY_DH ) |
| 116 | size = sizeof( ecp_keypair ); |
| 117 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 118 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 119 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 120 | if( type == POLARSSL_PK_ECDSA ) |
| 121 | size = sizeof( ecdsa_context ); |
| 122 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 123 | #endif |
Manuel Pégourié-Gonnard | c2c9003 | 2013-07-15 11:04:58 +0200 | [diff] [blame] | 124 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 125 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 126 | if( ( ctx->data = polarssl_malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 127 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 128 | |
| 129 | memset( ctx->data, 0, size ); |
| 130 | ctx->type = type; |
| 131 | |
| 132 | return( 0 ); |
| 133 | } |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 134 | |
Manuel Pégourié-Gonnard | 211a64c | 2013-08-09 15:04:26 +0200 | [diff] [blame] | 135 | #if defined(POLARSSL_ECDSA_C) |
| 136 | /* |
| 137 | * Convert generic EC context to ECDSA |
| 138 | */ |
| 139 | int 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é-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 171 | #if defined(POLARSSL_RSA_C) |
| 172 | /* |
| 173 | * Wrap an RSA context in a PK context |
| 174 | */ |
| 175 | int 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 |