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 | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 72 | static int rsa_sign_wrap( void *ctx, md_type_t md_alg, |
| 73 | const unsigned char *hash, size_t hash_len, |
| 74 | unsigned char *sig, size_t *sig_len, |
| 75 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 76 | { |
| 77 | *sig_len = ((rsa_context *) ctx)->len; |
| 78 | |
| 79 | return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE, |
| 80 | md_alg, hash_len, hash, sig ) ); |
| 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 83 | static void *rsa_alloc_wrap( void ) |
| 84 | { |
| 85 | void *ctx = polarssl_malloc( sizeof( rsa_context ) ); |
| 86 | |
| 87 | if( ctx != NULL ) |
| 88 | rsa_init( (rsa_context *) ctx, 0, 0 ); |
| 89 | |
| 90 | return ctx; |
| 91 | } |
| 92 | |
| 93 | static void rsa_free_wrap( void *ctx ) |
| 94 | { |
| 95 | rsa_free( (rsa_context *) ctx ); |
| 96 | polarssl_free( ctx ); |
| 97 | } |
| 98 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 99 | static void rsa_debug( const void *ctx, pk_debug_item *items ) |
| 100 | { |
| 101 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 102 | items->name = "rsa.N"; |
| 103 | items->value = &( ((rsa_context *) ctx)->N ); |
| 104 | |
| 105 | items++; |
| 106 | |
| 107 | items->type = POLARSSL_PK_DEBUG_MPI; |
| 108 | items->name = "rsa.E"; |
| 109 | items->value = &( ((rsa_context *) ctx)->E ); |
| 110 | } |
| 111 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 112 | const pk_info_t rsa_info = { |
| 113 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 114 | "RSA", |
| 115 | rsa_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 116 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 117 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 118 | rsa_sign_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 119 | rsa_alloc_wrap, |
| 120 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 121 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 122 | }; |
| 123 | #endif /* POLARSSL_RSA_C */ |
| 124 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 125 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Generic EC key |
| 128 | */ |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 129 | static int eckey_can_do( pk_type_t type ) |
| 130 | { |
| 131 | return( type == POLARSSL_PK_ECKEY || |
| 132 | type == POLARSSL_PK_ECKEY_DH || |
| 133 | type == POLARSSL_PK_ECDSA ); |
| 134 | } |
| 135 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 136 | static size_t eckey_get_size( const void *ctx ) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 137 | { |
| 138 | return( ((ecp_keypair *) ctx)->grp.pbits ); |
| 139 | } |
| 140 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 141 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 142 | /* Forward declarations */ |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 143 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 144 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 145 | const unsigned char *sig, size_t sig_len ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 147 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
| 148 | const unsigned char *hash, size_t hash_len, |
| 149 | unsigned char *sig, size_t *sig_len, |
| 150 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 151 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 152 | static int eckey_verify_wrap( void *ctx, md_type_t md_alg, |
| 153 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 154 | const unsigned char *sig, size_t sig_len ) |
| 155 | { |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 156 | int ret; |
| 157 | ecdsa_context ecdsa; |
| 158 | |
| 159 | ecdsa_init( &ecdsa ); |
| 160 | |
Manuel Pégourié-Gonnard | 583b608 | 2013-08-20 16:58:13 +0200 | [diff] [blame] | 161 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 162 | ret = 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] | 163 | |
| 164 | ecdsa_free( &ecdsa ); |
| 165 | |
| 166 | return( ret ); |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 167 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 168 | |
| 169 | static int eckey_sign_wrap( void *ctx, md_type_t md_alg, |
| 170 | const unsigned char *hash, size_t hash_len, |
| 171 | unsigned char *sig, size_t *sig_len, |
| 172 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 173 | { |
| 174 | int ret; |
| 175 | ecdsa_context ecdsa; |
| 176 | |
| 177 | ecdsa_init( &ecdsa ); |
| 178 | |
| 179 | if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 ) |
| 180 | ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len, |
| 181 | f_rng, p_rng ); |
| 182 | |
| 183 | ecdsa_free( &ecdsa ); |
| 184 | |
| 185 | return( ret ); |
| 186 | } |
| 187 | |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 188 | #endif /* POLARSSL_ECDSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 189 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 190 | static void *eckey_alloc_wrap( void ) |
| 191 | { |
| 192 | void *ctx = polarssl_malloc( sizeof( ecp_keypair ) ); |
| 193 | |
| 194 | if( ctx != NULL ) |
| 195 | ecp_keypair_init( ctx ); |
| 196 | |
| 197 | return( ctx ); |
| 198 | } |
| 199 | |
| 200 | static void eckey_free_wrap( void *ctx ) |
| 201 | { |
| 202 | ecp_keypair_free( (ecp_keypair *) ctx ); |
| 203 | polarssl_free( ctx ); |
| 204 | } |
| 205 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 206 | static void eckey_debug( const void *ctx, pk_debug_item *items ) |
| 207 | { |
| 208 | items->type = POLARSSL_PK_DEBUG_ECP; |
| 209 | items->name = "eckey.Q"; |
| 210 | items->value = &( ((ecp_keypair *) ctx)->Q ); |
| 211 | } |
| 212 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 213 | const pk_info_t eckey_info = { |
| 214 | POLARSSL_PK_ECKEY, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 215 | "EC", |
| 216 | eckey_get_size, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 217 | eckey_can_do, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 218 | #if defined(POLARSSL_ECDSA_C) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 219 | eckey_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 220 | eckey_sign_wrap, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 221 | #else |
| 222 | NULL, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 223 | NULL, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 224 | #endif |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 225 | eckey_alloc_wrap, |
| 226 | eckey_free_wrap, |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 227 | eckey_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 228 | }; |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * EC key resticted to ECDH |
| 232 | */ |
| 233 | static int eckeydh_can_do( pk_type_t type ) |
| 234 | { |
| 235 | return( type == POLARSSL_PK_ECKEY || |
| 236 | type == POLARSSL_PK_ECKEY_DH ); |
| 237 | } |
| 238 | |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 239 | const pk_info_t eckeydh_info = { |
| 240 | POLARSSL_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 241 | "EC_DH", |
| 242 | eckey_get_size, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 243 | eckeydh_can_do, |
Manuel Pégourié-Gonnard | fff80f8 | 2013-08-17 15:20:06 +0200 | [diff] [blame] | 244 | NULL, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 245 | NULL, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 246 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 247 | eckey_free_wrap, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 248 | eckey_debug, /* Same underlying key structure */ |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 249 | }; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 250 | #endif /* POLARSSL_ECP_C */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 251 | |
| 252 | #if defined(POLARSSL_ECDSA_C) |
| 253 | static int ecdsa_can_do( pk_type_t type ) |
| 254 | { |
| 255 | return( type == POLARSSL_PK_ECDSA ); |
| 256 | } |
| 257 | |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 258 | static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg, |
| 259 | const unsigned char *hash, size_t hash_len, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 260 | const unsigned char *sig, size_t sig_len ) |
| 261 | { |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 262 | ((void) md_alg); |
| 263 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 264 | return( ecdsa_read_signature( (ecdsa_context *) ctx, |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 265 | hash, hash_len, sig, sig_len ) ); |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 268 | static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg, |
| 269 | const unsigned char *hash, size_t hash_len, |
| 270 | unsigned char *sig, size_t *sig_len, |
| 271 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 272 | { |
| 273 | ((void) md_alg); |
| 274 | |
| 275 | return( ecdsa_write_signature( (ecdsa_context *) ctx, |
| 276 | hash, hash_len, sig, sig_len, f_rng, p_rng ) ); |
| 277 | } |
| 278 | |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 279 | static void *ecdsa_alloc_wrap( void ) |
| 280 | { |
| 281 | void *ctx = polarssl_malloc( sizeof( ecdsa_context ) ); |
| 282 | |
| 283 | if( ctx != NULL ) |
| 284 | ecdsa_init( (ecdsa_context *) ctx ); |
| 285 | |
| 286 | return( ctx ); |
| 287 | } |
| 288 | |
| 289 | static void ecdsa_free_wrap( void *ctx ) |
| 290 | { |
| 291 | ecdsa_free( (ecdsa_context *) ctx ); |
| 292 | polarssl_free( ctx ); |
| 293 | } |
| 294 | |
| 295 | const pk_info_t ecdsa_info = { |
| 296 | POLARSSL_PK_ECDSA, |
| 297 | "ECDSA", |
| 298 | eckey_get_size, /* Compatible key structures */ |
| 299 | ecdsa_can_do, |
| 300 | ecdsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame^] | 301 | ecdsa_sign_wrap, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 302 | ecdsa_alloc_wrap, |
| 303 | ecdsa_free_wrap, |
| 304 | eckey_debug, /* Compatible key structures */ |
| 305 | }; |
| 306 | #endif /* POLARSSL_ECDSA_C */ |