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" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 29 | #include "polarssl/pk_wrap.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 31 | #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é-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_ECDSA_C) |
| 38 | #include "polarssl/ecdsa.h" |
| 39 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 40 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 41 | #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é-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | |
| 50 | /* |
| 51 | * Initialise a pk_context |
| 52 | */ |
| 53 | void pk_init( pk_context *ctx ) |
| 54 | { |
| 55 | if( ctx == NULL ) |
| 56 | return; |
| 57 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 58 | ctx->info = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 59 | ctx->type = POLARSSL_PK_NONE; |
| 60 | ctx->data = NULL; |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 61 | ctx->dont_free = 0; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Free (the components of) a pk_context |
| 66 | */ |
| 67 | void pk_free( pk_context *ctx ) |
| 68 | { |
| 69 | if( ctx == NULL ) |
| 70 | return; |
| 71 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 72 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 73 | if( ctx->type == POLARSSL_PK_RSA ) |
| 74 | rsa_free( ctx->data ); |
| 75 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 76 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 77 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 78 | if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH ) |
| 79 | ecp_keypair_free( ctx->data ); |
| 80 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 81 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 82 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 83 | if( ctx->type == POLARSSL_PK_ECDSA ) |
| 84 | ecdsa_free( ctx->data ); |
| 85 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 86 | #endif |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 87 | { |
| 88 | ; /* guard for the else's above */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 91 | if( ! ctx->dont_free ) |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 92 | polarssl_free( ctx->data ); |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 93 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 94 | ctx->info = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 95 | ctx->type = POLARSSL_PK_NONE; |
| 96 | ctx->data = NULL; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Set a pk_context to a given type |
| 101 | */ |
| 102 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 103 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 104 | size_t size; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 105 | const pk_info_t *info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 106 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 107 | if( ctx->type == type ) |
| 108 | return( 0 ); |
| 109 | |
| 110 | if( ctx->type != POLARSSL_PK_NONE ) |
| 111 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 112 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 113 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 114 | if( type == POLARSSL_PK_RSA ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 115 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 116 | size = sizeof( rsa_context ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 117 | info = &rsa_info; |
| 118 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 119 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 120 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 121 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame^] | 122 | if( type == POLARSSL_PK_ECKEY ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 123 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 124 | size = sizeof( ecp_keypair ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 125 | info = &eckey_info; |
| 126 | } |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame^] | 127 | else if( type == POLARSSL_PK_ECKEY_DH ) |
| 128 | { |
| 129 | size = sizeof( ecp_keypair ); |
| 130 | info = &eckeydh_info; |
| 131 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 132 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 133 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 134 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 135 | if( type == POLARSSL_PK_ECDSA ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 136 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 137 | size = sizeof( ecdsa_context ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 138 | info = &ecdsa_info; |
| 139 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 140 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 141 | #endif |
Manuel Pégourié-Gonnard | c2c9003 | 2013-07-15 11:04:58 +0200 | [diff] [blame] | 142 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 143 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 144 | if( ( ctx->data = polarssl_malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 145 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 146 | |
| 147 | memset( ctx->data, 0, size ); |
| 148 | ctx->type = type; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 149 | ctx->info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 150 | |
| 151 | return( 0 ); |
| 152 | } |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 153 | |
| 154 | #if defined(POLARSSL_RSA_C) |
| 155 | /* |
| 156 | * Wrap an RSA context in a PK context |
| 157 | */ |
| 158 | int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa) |
| 159 | { |
| 160 | if( ctx->type != POLARSSL_PK_NONE ) |
| 161 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 162 | |
| 163 | ctx->type = POLARSSL_PK_RSA; |
| 164 | ctx->data = (rsa_context *) rsa; |
| 165 | ctx->dont_free = 1; |
| 166 | |
| 167 | return( 0 ); |
| 168 | } |
| 169 | #endif |