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; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Free (the components of) a pk_context |
| 65 | */ |
| 66 | void pk_free( pk_context *ctx ) |
| 67 | { |
| 68 | if( ctx == NULL ) |
| 69 | return; |
| 70 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 71 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 72 | if( ctx->type == POLARSSL_PK_RSA ) |
| 73 | rsa_free( ctx->data ); |
| 74 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 75 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 76 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 77 | if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH ) |
| 78 | ecp_keypair_free( ctx->data ); |
| 79 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 80 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 81 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 82 | if( ctx->type == POLARSSL_PK_ECDSA ) |
| 83 | ecdsa_free( ctx->data ); |
| 84 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 85 | #endif |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 86 | { |
| 87 | ; /* guard for the else's above */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +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 | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 92 | ctx->info = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 93 | ctx->type = POLARSSL_PK_NONE; |
| 94 | ctx->data = NULL; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Set a pk_context to a given type |
| 99 | */ |
| 100 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 101 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 102 | size_t size; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 103 | const pk_info_t *info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 105 | if( ctx->type == type ) |
| 106 | return( 0 ); |
| 107 | |
| 108 | if( ctx->type != POLARSSL_PK_NONE ) |
| 109 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 110 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 111 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 112 | if( type == POLARSSL_PK_RSA ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 113 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 114 | size = sizeof( rsa_context ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 115 | info = &rsa_info; |
| 116 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 117 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 118 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 119 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 120 | if( type == POLARSSL_PK_ECKEY ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 121 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 122 | size = sizeof( ecp_keypair ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 123 | info = &eckey_info; |
| 124 | } |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 125 | else if( type == POLARSSL_PK_ECKEY_DH ) |
| 126 | { |
| 127 | size = sizeof( ecp_keypair ); |
| 128 | info = &eckeydh_info; |
| 129 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 130 | else |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 131 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 132 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 133 | if( type == POLARSSL_PK_ECDSA ) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 134 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 135 | size = sizeof( ecdsa_context ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 136 | info = &ecdsa_info; |
| 137 | } |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 138 | else |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 139 | #endif |
Manuel Pégourié-Gonnard | c2c9003 | 2013-07-15 11:04:58 +0200 | [diff] [blame] | 140 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 141 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 142 | if( ( ctx->data = polarssl_malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 143 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 144 | |
| 145 | memset( ctx->data, 0, size ); |
| 146 | ctx->type = type; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 147 | ctx->info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 148 | |
| 149 | return( 0 ); |
| 150 | } |