Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer: wrapper functions |
| 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/pk_wrap.h" |
| 29 | |
| 30 | #if defined(POLARSSL_RSA_C) |
| 31 | #include "polarssl/rsa.h" |
| 32 | #endif |
| 33 | |
| 34 | #if defined(POLARSSL_ECP_C) |
| 35 | #include "polarssl/ecp.h" |
| 36 | #endif |
| 37 | |
| 38 | #if defined(POLARSSL_ECDSA_C) |
| 39 | #include "polarssl/ecdsa.h" |
| 40 | #endif |
| 41 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 42 | #if defined(POLARSSL_MEMORY_C) |
| 43 | #include "polarssl/memory.h" |
| 44 | #else |
| 45 | #include <stdlib.h> |
| 46 | #define polarssl_malloc malloc |
| 47 | #define polarssl_free free |
| 48 | #endif |
| 49 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 50 | #if defined(POLARSSL_RSA_C) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 51 | static int rsa_can_do( pk_type_t type ) |
| 52 | { |
| 53 | return( type == POLARSSL_PK_RSA ); |
| 54 | } |
| 55 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 56 | static size_t rsa_get_size( const void * ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 57 | { |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 58 | return( 8 * ((rsa_context *) ctx)->len ); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 61 | static int rsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 62 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 63 | const unsigned char *sig, size_t sig_len ) |
| 64 | { |
Manuel Pégourié-Gonnard | ac4cd36 | 2013-08-14 20:20:41 +0200 | [diff] [blame] | 65 | if( sig_len != ((rsa_context *) ctx)->len ) |
| 66 | return( POLARSSL_ERR_RSA_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 67 | |
| 68 | return( rsa_pkcs1_verify( (rsa_context *) ctx, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 69 | RSA_PUBLIC, md_alg, hash_len, hash, sig ) ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 72 | static void *rsa_alloc_wrap( void ) |
| 73 | { |
| 74 | void *ctx = polarssl_malloc( sizeof( rsa_context ) ); |
| 75 | |
| 76 | if( ctx != NULL ) |
| 77 | rsa_init( (rsa_context *) ctx, 0, 0 ); |
| 78 | |
| 79 | return ctx; |
| 80 | } |
| 81 | |
| 82 | static void rsa_free_wrap( void *ctx ) |
| 83 | { |
| 84 | rsa_free( (rsa_context *) ctx ); |
| 85 | polarssl_free( ctx ); |
| 86 | } |
| 87 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 88 | static void rsa_debug( const void *ctx, pk_debug_item *items ) |
| 89 | { |
| 90 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 91 | items->name = "rsa.N"; |
| 92 | items->value = &( ((rsa_context *) ctx)->N ); |
| 93 | |
| 94 | items++; |
| 95 | |
| 96 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 97 | items->name = "rsa.E"; |
| 98 | items->value = &( ((rsa_context *) ctx)->E ); |
| 99 | } |
| 100 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 101 | const pk_info_t rsa_info = { |
| 102 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 103 | "RSA", |
| 104 | rsa_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 105 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 106 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 107 | rsa_alloc_wrap, |
| 108 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 109 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 110 | }; |
| 111 | #endif /* POLARSSL_RSA_C */ |
| 112 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 113 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Generic EC key |
| 116 | */ |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 117 | static int eckey_can_do( pk_type_t type ) |
| 118 | { |
| 119 | return( type == POLARSSL_PK_ECKEY || |
| 120 | type == POLARSSL_PK_ECKEY_DH || |
| 121 | type == POLARSSL_PK_ECDSA ); |
| 122 | } |
| 123 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 124 | static size_t eckey_get_size( const void *ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 125 | { |
| 126 | return( ((ecp_keypair *) ctx)->grp.pbits ); |
| 127 | } |
| 128 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 129 | #if defined(POLARSSL_ECDSA_C) |
| 130 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 131 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 132 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 133 | const unsigned char *sig, size_t sig_len ); |
| 134 | #endif |
| 135 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 136 | static int eckey_verify_wrap( void *ctx, md_type_t md_alg, |
| 137 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 138 | const unsigned char *sig, size_t sig_len ) |
| 139 | { |
| 140 | #if !defined(POLARSSL_ECDSA_C) |
| 141 | ((void) ctx); |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 142 | ((void) md_alg); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 143 | ((void) hash); |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 144 | ((void) hash_len); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 145 | ((void) sig); |
| 146 | ((void) sig_len); |
| 147 | |
| 148 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 149 | #else |
| 150 | int ret; |
| 151 | ecdsa_context ecdsa; |
| 152 | |
| 153 | ecdsa_init( &ecdsa ); |
| 154 | |
| 155 | ret = ecdsa_from_keypair( &ecdsa, ctx ) || |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 156 | ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 157 | |
| 158 | ecdsa_free( &ecdsa ); |
| 159 | |
| 160 | return( ret ); |
| 161 | #endif /* POLARSSL_ECDSA_C */ |
| 162 | } |
| 163 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 164 | static void *eckey_alloc_wrap( void ) |
| 165 | { |
| 166 | void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); |
| 167 | |
| 168 | if( ctx != NULL ) |
| 169 | ecp_keypair_init( ctx ); |
| 170 | |
| 171 | return( ctx ); |
| 172 | } |
| 173 | |
| 174 | static void eckey_free_wrap( void *ctx ) |
| 175 | { |
| 176 | ecp_keypair_free( (ecp_keypair *) ctx ); |
| 177 | polarssl_free( ctx ); |
| 178 | } |
| 179 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 180 | static void eckey_debug( const void *ctx, pk_debug_item *items ) |
| 181 | { |
| 182 | items->type = POLARSSL_PK_DEBUG_ECP; |
| 183 | items->name = "eckey.Q"; |
| 184 | items->value = &( ((ecp_keypair *) ctx)->Q ); |
| 185 | } |
| 186 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 187 | const pk_info_t eckey_info = { |
| 188 | POLARSSL_PK_ECKEY, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 189 | "EC", |
| 190 | eckey_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 191 | eckey_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 192 | eckey_verify_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 193 | eckey_alloc_wrap, |
| 194 | eckey_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 195 | eckey_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 196 | }; |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * EC key resticted to ECDH |
| 200 | */ |
| 201 | static int eckeydh_can_do( pk_type_t type ) |
| 202 | { |
| 203 | return( type == POLARSSL_PK_ECKEY || |
| 204 | type == POLARSSL_PK_ECKEY_DH ); |
| 205 | } |
| 206 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 207 | static int eckeydh_verify_wrap( void *ctx, md_type_t md_alg, |
| 208 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 209 | const unsigned char *sig, size_t sig_len ) |
| 210 | { |
| 211 | ((void) ctx); |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 212 | ((void) md_alg); |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 213 | ((void) hash); |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 214 | ((void) hash_len); |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 215 | ((void) sig); |
| 216 | ((void) sig_len); |
| 217 | |
| 218 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 219 | } |
| 220 | |
| 221 | const pk_info_t eckeydh_info = { |
| 222 | POLARSSL_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 223 | "EC_DH", |
| 224 | eckey_get_size, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 225 | eckeydh_can_do, |
| 226 | eckeydh_verify_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 227 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 228 | eckey_free_wrap, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 229 | eckey_debug, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 230 | }; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 231 | #endif /* POLARSSL_ECP_C */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 232 | |
| 233 | #if defined(POLARSSL_ECDSA_C) |
| 234 | static int ecdsa_can_do( pk_type_t type ) |
| 235 | { |
| 236 | return( type == POLARSSL_PK_ECDSA ); |
| 237 | } |
| 238 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 239 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 240 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 241 | const unsigned char *sig, size_t sig_len ) |
| 242 | { |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 243 | ((void) md_alg); |
| 244 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 245 | return( ecdsa_read_signature( (ecdsa_context *) ctx, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame^] | 246 | hash, hash_len, sig, sig_len ) ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void *ecdsa_alloc_wrap( void ) |
| 250 | { |
| 251 | void *ctx = polarssl_malloc( sizeof( ecdsa_context ) ); |
| 252 | |
| 253 | if( ctx != NULL ) |
| 254 | ecdsa_init( (ecdsa_context *) ctx ); |
| 255 | |
| 256 | return( ctx ); |
| 257 | } |
| 258 | |
| 259 | static void ecdsa_free_wrap( void *ctx ) |
| 260 | { |
| 261 | ecdsa_free( (ecdsa_context *) ctx ); |
| 262 | polarssl_free( ctx ); |
| 263 | } |
| 264 | |
| 265 | const pk_info_t ecdsa_info = { |
| 266 | POLARSSL_PK_ECDSA, |
| 267 | "ECDSA", |
| 268 | eckey_get_size, /* Compatible key structures */ |
| 269 | ecdsa_can_do, |
| 270 | ecdsa_verify_wrap, |
| 271 | ecdsa_alloc_wrap, |
| 272 | ecdsa_free_wrap, |
| 273 | eckey_debug, /* Compatible key structures */ |
| 274 | }; |
| 275 | #endif /* POLARSSL_ECDSA_C */ |