mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * Public Key abstraction layer: wrapper functions
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22
ansond 0:137634ff4186 23 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 24 #include "polarssl/config.h"
ansond 0:137634ff4186 25 #else
ansond 0:137634ff4186 26 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 27 #endif
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 #if defined(POLARSSL_PK_C)
ansond 0:137634ff4186 30 #include "polarssl/pk_wrap.h"
ansond 0:137634ff4186 31
ansond 0:137634ff4186 32 /* Even if RSA not activated, for the sake of RSA-alt */
ansond 0:137634ff4186 33 #include "polarssl/rsa.h"
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #include <string.h>
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #if defined(POLARSSL_ECP_C)
ansond 0:137634ff4186 38 #include "polarssl/ecp.h"
ansond 0:137634ff4186 39 #endif
ansond 0:137634ff4186 40
ansond 0:137634ff4186 41 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 42 #include "polarssl/ecdsa.h"
ansond 0:137634ff4186 43 #endif
ansond 0:137634ff4186 44
ansond 0:137634ff4186 45 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 46 #include "polarssl/platform.h"
ansond 0:137634ff4186 47 #else
ansond 0:137634ff4186 48 #include <stdlib.h>
ansond 0:137634ff4186 49 #define polarssl_malloc malloc
ansond 0:137634ff4186 50 #define polarssl_free free
ansond 0:137634ff4186 51 #endif
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 54 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 56 }
ansond 0:137634ff4186 57
ansond 0:137634ff4186 58 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 59 static int rsa_can_do( pk_type_t type )
ansond 0:137634ff4186 60 {
ansond 0:137634ff4186 61 return( type == POLARSSL_PK_RSA ||
ansond 0:137634ff4186 62 type == POLARSSL_PK_RSASSA_PSS );
ansond 0:137634ff4186 63 }
ansond 0:137634ff4186 64
ansond 0:137634ff4186 65 static size_t rsa_get_size( const void *ctx )
ansond 0:137634ff4186 66 {
ansond 0:137634ff4186 67 return( 8 * ((const rsa_context *) ctx)->len );
ansond 0:137634ff4186 68 }
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 71 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 72 const unsigned char *sig, size_t sig_len )
ansond 0:137634ff4186 73 {
ansond 0:137634ff4186 74 int ret;
ansond 0:137634ff4186 75
ansond 0:137634ff4186 76 if( sig_len < ((rsa_context *) ctx)->len )
ansond 0:137634ff4186 77 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
ansond 0:137634ff4186 78
ansond 0:137634ff4186 79 if( ( ret = rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL,
ansond 0:137634ff4186 80 RSA_PUBLIC, md_alg,
ansond 0:137634ff4186 81 (unsigned int) hash_len, hash, sig ) ) != 0 )
ansond 0:137634ff4186 82 return( ret );
ansond 0:137634ff4186 83
ansond 0:137634ff4186 84 if( sig_len > ((rsa_context *) ctx)->len )
ansond 0:137634ff4186 85 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 return( 0 );
ansond 0:137634ff4186 88 }
ansond 0:137634ff4186 89
ansond 0:137634ff4186 90 static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 91 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 92 unsigned char *sig, size_t *sig_len,
ansond 0:137634ff4186 93 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 94 {
ansond 0:137634ff4186 95 *sig_len = ((rsa_context *) ctx)->len;
ansond 0:137634ff4186 96
ansond 0:137634ff4186 97 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
ansond 0:137634ff4186 98 md_alg, (unsigned int) hash_len, hash, sig ) );
ansond 0:137634ff4186 99 }
ansond 0:137634ff4186 100
ansond 0:137634ff4186 101 static int rsa_decrypt_wrap( void *ctx,
ansond 0:137634ff4186 102 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 103 unsigned char *output, size_t *olen, size_t osize,
ansond 0:137634ff4186 104 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 105 {
ansond 0:137634ff4186 106 if( ilen != ((rsa_context *) ctx)->len )
ansond 0:137634ff4186 107 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ansond 0:137634ff4186 108
ansond 0:137634ff4186 109 return( rsa_pkcs1_decrypt( (rsa_context *) ctx, f_rng, p_rng,
ansond 0:137634ff4186 110 RSA_PRIVATE, olen, input, output, osize ) );
ansond 0:137634ff4186 111 }
ansond 0:137634ff4186 112
ansond 0:137634ff4186 113 static int rsa_encrypt_wrap( void *ctx,
ansond 0:137634ff4186 114 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 115 unsigned char *output, size_t *olen, size_t osize,
ansond 0:137634ff4186 116 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 117 {
ansond 0:137634ff4186 118 *olen = ((rsa_context *) ctx)->len;
ansond 0:137634ff4186 119
ansond 0:137634ff4186 120 if( *olen > osize )
ansond 0:137634ff4186 121 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
ansond 0:137634ff4186 122
ansond 0:137634ff4186 123 return( rsa_pkcs1_encrypt( (rsa_context *) ctx,
ansond 0:137634ff4186 124 f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) );
ansond 0:137634ff4186 125 }
ansond 0:137634ff4186 126
ansond 0:137634ff4186 127 static int rsa_check_pair_wrap( const void *pub, const void *prv )
ansond 0:137634ff4186 128 {
ansond 0:137634ff4186 129 return( rsa_check_pub_priv( (const rsa_context *) pub,
ansond 0:137634ff4186 130 (const rsa_context *) prv ) );
ansond 0:137634ff4186 131 }
ansond 0:137634ff4186 132
ansond 0:137634ff4186 133 static void *rsa_alloc_wrap( void )
ansond 0:137634ff4186 134 {
ansond 0:137634ff4186 135 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
ansond 0:137634ff4186 136
ansond 0:137634ff4186 137 if( ctx != NULL )
ansond 0:137634ff4186 138 rsa_init( (rsa_context *) ctx, 0, 0 );
ansond 0:137634ff4186 139
ansond 0:137634ff4186 140 return( ctx );
ansond 0:137634ff4186 141 }
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 static void rsa_free_wrap( void *ctx )
ansond 0:137634ff4186 144 {
ansond 0:137634ff4186 145 rsa_free( (rsa_context *) ctx );
ansond 0:137634ff4186 146 polarssl_free( ctx );
ansond 0:137634ff4186 147 }
ansond 0:137634ff4186 148
ansond 0:137634ff4186 149 static void rsa_debug( const void *ctx, pk_debug_item *items )
ansond 0:137634ff4186 150 {
ansond 0:137634ff4186 151 items->type = POLARSSL_PK_DEBUG_MPI;
ansond 0:137634ff4186 152 items->name = "rsa.N";
ansond 0:137634ff4186 153 items->value = &( ((rsa_context *) ctx)->N );
ansond 0:137634ff4186 154
ansond 0:137634ff4186 155 items++;
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 items->type = POLARSSL_PK_DEBUG_MPI;
ansond 0:137634ff4186 158 items->name = "rsa.E";
ansond 0:137634ff4186 159 items->value = &( ((rsa_context *) ctx)->E );
ansond 0:137634ff4186 160 }
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 const pk_info_t rsa_info = {
ansond 0:137634ff4186 163 POLARSSL_PK_RSA,
ansond 0:137634ff4186 164 "RSA",
ansond 0:137634ff4186 165 rsa_get_size,
ansond 0:137634ff4186 166 rsa_can_do,
ansond 0:137634ff4186 167 rsa_verify_wrap,
ansond 0:137634ff4186 168 rsa_sign_wrap,
ansond 0:137634ff4186 169 rsa_decrypt_wrap,
ansond 0:137634ff4186 170 rsa_encrypt_wrap,
ansond 0:137634ff4186 171 rsa_check_pair_wrap,
ansond 0:137634ff4186 172 rsa_alloc_wrap,
ansond 0:137634ff4186 173 rsa_free_wrap,
ansond 0:137634ff4186 174 rsa_debug,
ansond 0:137634ff4186 175 };
ansond 0:137634ff4186 176 #endif /* POLARSSL_RSA_C */
ansond 0:137634ff4186 177
ansond 0:137634ff4186 178 #if defined(POLARSSL_ECP_C)
ansond 0:137634ff4186 179 /*
ansond 0:137634ff4186 180 * Generic EC key
ansond 0:137634ff4186 181 */
ansond 0:137634ff4186 182 static int eckey_can_do( pk_type_t type )
ansond 0:137634ff4186 183 {
ansond 0:137634ff4186 184 return( type == POLARSSL_PK_ECKEY ||
ansond 0:137634ff4186 185 type == POLARSSL_PK_ECKEY_DH ||
ansond 0:137634ff4186 186 type == POLARSSL_PK_ECDSA );
ansond 0:137634ff4186 187 }
ansond 0:137634ff4186 188
ansond 0:137634ff4186 189 static size_t eckey_get_size( const void *ctx )
ansond 0:137634ff4186 190 {
ansond 0:137634ff4186 191 return( ((ecp_keypair *) ctx)->grp.pbits );
ansond 0:137634ff4186 192 }
ansond 0:137634ff4186 193
ansond 0:137634ff4186 194 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 195 /* Forward declarations */
ansond 0:137634ff4186 196 static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 197 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 198 const unsigned char *sig, size_t sig_len );
ansond 0:137634ff4186 199
ansond 0:137634ff4186 200 static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 201 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 202 unsigned char *sig, size_t *sig_len,
ansond 0:137634ff4186 203 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
ansond 0:137634ff4186 204
ansond 0:137634ff4186 205 static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 206 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 207 const unsigned char *sig, size_t sig_len )
ansond 0:137634ff4186 208 {
ansond 0:137634ff4186 209 int ret;
ansond 0:137634ff4186 210 ecdsa_context ecdsa;
ansond 0:137634ff4186 211
ansond 0:137634ff4186 212 ecdsa_init( &ecdsa );
ansond 0:137634ff4186 213
ansond 0:137634ff4186 214 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
ansond 0:137634ff4186 215 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
ansond 0:137634ff4186 216
ansond 0:137634ff4186 217 ecdsa_free( &ecdsa );
ansond 0:137634ff4186 218
ansond 0:137634ff4186 219 return( ret );
ansond 0:137634ff4186 220 }
ansond 0:137634ff4186 221
ansond 0:137634ff4186 222 static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 223 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 224 unsigned char *sig, size_t *sig_len,
ansond 0:137634ff4186 225 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 226 {
ansond 0:137634ff4186 227 int ret;
ansond 0:137634ff4186 228 ecdsa_context ecdsa;
ansond 0:137634ff4186 229
ansond 0:137634ff4186 230 ecdsa_init( &ecdsa );
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
ansond 0:137634ff4186 233 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
ansond 0:137634ff4186 234 f_rng, p_rng );
ansond 0:137634ff4186 235
ansond 0:137634ff4186 236 ecdsa_free( &ecdsa );
ansond 0:137634ff4186 237
ansond 0:137634ff4186 238 return( ret );
ansond 0:137634ff4186 239 }
ansond 0:137634ff4186 240
ansond 0:137634ff4186 241 #endif /* POLARSSL_ECDSA_C */
ansond 0:137634ff4186 242
ansond 0:137634ff4186 243 static int eckey_check_pair( const void *pub, const void *prv )
ansond 0:137634ff4186 244 {
ansond 0:137634ff4186 245 return( ecp_check_pub_priv( (const ecp_keypair *) pub,
ansond 0:137634ff4186 246 (const ecp_keypair *) prv ) );
ansond 0:137634ff4186 247 }
ansond 0:137634ff4186 248
ansond 0:137634ff4186 249 static void *eckey_alloc_wrap( void )
ansond 0:137634ff4186 250 {
ansond 0:137634ff4186 251 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 if( ctx != NULL )
ansond 0:137634ff4186 254 ecp_keypair_init( ctx );
ansond 0:137634ff4186 255
ansond 0:137634ff4186 256 return( ctx );
ansond 0:137634ff4186 257 }
ansond 0:137634ff4186 258
ansond 0:137634ff4186 259 static void eckey_free_wrap( void *ctx )
ansond 0:137634ff4186 260 {
ansond 0:137634ff4186 261 ecp_keypair_free( (ecp_keypair *) ctx );
ansond 0:137634ff4186 262 polarssl_free( ctx );
ansond 0:137634ff4186 263 }
ansond 0:137634ff4186 264
ansond 0:137634ff4186 265 static void eckey_debug( const void *ctx, pk_debug_item *items )
ansond 0:137634ff4186 266 {
ansond 0:137634ff4186 267 items->type = POLARSSL_PK_DEBUG_ECP;
ansond 0:137634ff4186 268 items->name = "eckey.Q";
ansond 0:137634ff4186 269 items->value = &( ((ecp_keypair *) ctx)->Q );
ansond 0:137634ff4186 270 }
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 const pk_info_t eckey_info = {
ansond 0:137634ff4186 273 POLARSSL_PK_ECKEY,
ansond 0:137634ff4186 274 "EC",
ansond 0:137634ff4186 275 eckey_get_size,
ansond 0:137634ff4186 276 eckey_can_do,
ansond 0:137634ff4186 277 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 278 eckey_verify_wrap,
ansond 0:137634ff4186 279 eckey_sign_wrap,
ansond 0:137634ff4186 280 #else
ansond 0:137634ff4186 281 NULL,
ansond 0:137634ff4186 282 NULL,
ansond 0:137634ff4186 283 #endif
ansond 0:137634ff4186 284 NULL,
ansond 0:137634ff4186 285 NULL,
ansond 0:137634ff4186 286 eckey_check_pair,
ansond 0:137634ff4186 287 eckey_alloc_wrap,
ansond 0:137634ff4186 288 eckey_free_wrap,
ansond 0:137634ff4186 289 eckey_debug,
ansond 0:137634ff4186 290 };
ansond 0:137634ff4186 291
ansond 0:137634ff4186 292 /*
ansond 0:137634ff4186 293 * EC key restricted to ECDH
ansond 0:137634ff4186 294 */
ansond 0:137634ff4186 295 static int eckeydh_can_do( pk_type_t type )
ansond 0:137634ff4186 296 {
ansond 0:137634ff4186 297 return( type == POLARSSL_PK_ECKEY ||
ansond 0:137634ff4186 298 type == POLARSSL_PK_ECKEY_DH );
ansond 0:137634ff4186 299 }
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 const pk_info_t eckeydh_info = {
ansond 0:137634ff4186 302 POLARSSL_PK_ECKEY_DH,
ansond 0:137634ff4186 303 "EC_DH",
ansond 0:137634ff4186 304 eckey_get_size, /* Same underlying key structure */
ansond 0:137634ff4186 305 eckeydh_can_do,
ansond 0:137634ff4186 306 NULL,
ansond 0:137634ff4186 307 NULL,
ansond 0:137634ff4186 308 NULL,
ansond 0:137634ff4186 309 NULL,
ansond 0:137634ff4186 310 eckey_check_pair,
ansond 0:137634ff4186 311 eckey_alloc_wrap, /* Same underlying key structure */
ansond 0:137634ff4186 312 eckey_free_wrap, /* Same underlying key structure */
ansond 0:137634ff4186 313 eckey_debug, /* Same underlying key structure */
ansond 0:137634ff4186 314 };
ansond 0:137634ff4186 315 #endif /* POLARSSL_ECP_C */
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 #if defined(POLARSSL_ECDSA_C)
ansond 0:137634ff4186 318 static int ecdsa_can_do( pk_type_t type )
ansond 0:137634ff4186 319 {
ansond 0:137634ff4186 320 return( type == POLARSSL_PK_ECDSA );
ansond 0:137634ff4186 321 }
ansond 0:137634ff4186 322
ansond 0:137634ff4186 323 static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 324 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 325 const unsigned char *sig, size_t sig_len )
ansond 0:137634ff4186 326 {
ansond 0:137634ff4186 327 int ret;
ansond 0:137634ff4186 328 ((void) md_alg);
ansond 0:137634ff4186 329
ansond 0:137634ff4186 330 ret = ecdsa_read_signature( (ecdsa_context *) ctx,
ansond 0:137634ff4186 331 hash, hash_len, sig, sig_len );
ansond 0:137634ff4186 332
ansond 0:137634ff4186 333 if( ret == POLARSSL_ERR_ECP_SIG_LEN_MISMATCH )
ansond 0:137634ff4186 334 return( POLARSSL_ERR_PK_SIG_LEN_MISMATCH );
ansond 0:137634ff4186 335
ansond 0:137634ff4186 336 return( ret );
ansond 0:137634ff4186 337 }
ansond 0:137634ff4186 338
ansond 0:137634ff4186 339 static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 340 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 341 unsigned char *sig, size_t *sig_len,
ansond 0:137634ff4186 342 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 343 {
ansond 0:137634ff4186 344 /* Use deterministic ECDSA by default if available */
ansond 0:137634ff4186 345 #if defined(POLARSSL_ECDSA_DETERMINISTIC)
ansond 0:137634ff4186 346 ((void) f_rng);
ansond 0:137634ff4186 347 ((void) p_rng);
ansond 0:137634ff4186 348
ansond 0:137634ff4186 349 return( ecdsa_write_signature_det( (ecdsa_context *) ctx,
ansond 0:137634ff4186 350 hash, hash_len, sig, sig_len, md_alg ) );
ansond 0:137634ff4186 351 #else
ansond 0:137634ff4186 352 ((void) md_alg);
ansond 0:137634ff4186 353
ansond 0:137634ff4186 354 return( ecdsa_write_signature( (ecdsa_context *) ctx,
ansond 0:137634ff4186 355 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
ansond 0:137634ff4186 356 #endif /* POLARSSL_ECDSA_DETERMINISTIC */
ansond 0:137634ff4186 357 }
ansond 0:137634ff4186 358
ansond 0:137634ff4186 359 static void *ecdsa_alloc_wrap( void )
ansond 0:137634ff4186 360 {
ansond 0:137634ff4186 361 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
ansond 0:137634ff4186 362
ansond 0:137634ff4186 363 if( ctx != NULL )
ansond 0:137634ff4186 364 ecdsa_init( (ecdsa_context *) ctx );
ansond 0:137634ff4186 365
ansond 0:137634ff4186 366 return( ctx );
ansond 0:137634ff4186 367 }
ansond 0:137634ff4186 368
ansond 0:137634ff4186 369 static void ecdsa_free_wrap( void *ctx )
ansond 0:137634ff4186 370 {
ansond 0:137634ff4186 371 ecdsa_free( (ecdsa_context *) ctx );
ansond 0:137634ff4186 372 polarssl_free( ctx );
ansond 0:137634ff4186 373 }
ansond 0:137634ff4186 374
ansond 0:137634ff4186 375 const pk_info_t ecdsa_info = {
ansond 0:137634ff4186 376 POLARSSL_PK_ECDSA,
ansond 0:137634ff4186 377 "ECDSA",
ansond 0:137634ff4186 378 eckey_get_size, /* Compatible key structures */
ansond 0:137634ff4186 379 ecdsa_can_do,
ansond 0:137634ff4186 380 ecdsa_verify_wrap,
ansond 0:137634ff4186 381 ecdsa_sign_wrap,
ansond 0:137634ff4186 382 NULL,
ansond 0:137634ff4186 383 NULL,
ansond 0:137634ff4186 384 eckey_check_pair, /* Compatible key structures */
ansond 0:137634ff4186 385 ecdsa_alloc_wrap,
ansond 0:137634ff4186 386 ecdsa_free_wrap,
ansond 0:137634ff4186 387 eckey_debug, /* Compatible key structures */
ansond 0:137634ff4186 388 };
ansond 0:137634ff4186 389 #endif /* POLARSSL_ECDSA_C */
ansond 0:137634ff4186 390
ansond 0:137634ff4186 391 /*
ansond 0:137634ff4186 392 * Support for alternative RSA-private implementations
ansond 0:137634ff4186 393 */
ansond 0:137634ff4186 394
ansond 0:137634ff4186 395 static int rsa_alt_can_do( pk_type_t type )
ansond 0:137634ff4186 396 {
ansond 0:137634ff4186 397 return( type == POLARSSL_PK_RSA );
ansond 0:137634ff4186 398 }
ansond 0:137634ff4186 399
ansond 0:137634ff4186 400 static size_t rsa_alt_get_size( const void *ctx )
ansond 0:137634ff4186 401 {
ansond 0:137634ff4186 402 const rsa_alt_context *rsa_alt = (const rsa_alt_context *) ctx;
ansond 0:137634ff4186 403
ansond 0:137634ff4186 404 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
ansond 0:137634ff4186 405 }
ansond 0:137634ff4186 406
ansond 0:137634ff4186 407 static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
ansond 0:137634ff4186 408 const unsigned char *hash, size_t hash_len,
ansond 0:137634ff4186 409 unsigned char *sig, size_t *sig_len,
ansond 0:137634ff4186 410 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 411 {
ansond 0:137634ff4186 412 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
ansond 0:137634ff4186 413
ansond 0:137634ff4186 414 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
ansond 0:137634ff4186 415
ansond 0:137634ff4186 416 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
ansond 0:137634ff4186 417 md_alg, (unsigned int) hash_len, hash, sig ) );
ansond 0:137634ff4186 418 }
ansond 0:137634ff4186 419
ansond 0:137634ff4186 420 static int rsa_alt_decrypt_wrap( void *ctx,
ansond 0:137634ff4186 421 const unsigned char *input, size_t ilen,
ansond 0:137634ff4186 422 unsigned char *output, size_t *olen, size_t osize,
ansond 0:137634ff4186 423 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 424 {
ansond 0:137634ff4186 425 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
ansond 0:137634ff4186 426
ansond 0:137634ff4186 427 ((void) f_rng);
ansond 0:137634ff4186 428 ((void) p_rng);
ansond 0:137634ff4186 429
ansond 0:137634ff4186 430 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
ansond 0:137634ff4186 431 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ansond 0:137634ff4186 432
ansond 0:137634ff4186 433 return( rsa_alt->decrypt_func( rsa_alt->key,
ansond 0:137634ff4186 434 RSA_PRIVATE, olen, input, output, osize ) );
ansond 0:137634ff4186 435 }
ansond 0:137634ff4186 436
ansond 0:137634ff4186 437 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 438 static int rsa_alt_check_pair( const void *pub, const void *prv )
ansond 0:137634ff4186 439 {
ansond 0:137634ff4186 440 unsigned char sig[POLARSSL_MPI_MAX_SIZE];
ansond 0:137634ff4186 441 unsigned char hash[32];
ansond 0:137634ff4186 442 size_t sig_len = 0;
ansond 0:137634ff4186 443 int ret;
ansond 0:137634ff4186 444
ansond 0:137634ff4186 445 if( rsa_alt_get_size( prv ) != rsa_get_size( pub ) )
ansond 0:137634ff4186 446 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
ansond 0:137634ff4186 447
ansond 0:137634ff4186 448 memset( hash, 0x2a, sizeof( hash ) );
ansond 0:137634ff4186 449
ansond 0:137634ff4186 450 if( ( ret = rsa_alt_sign_wrap( (void *) prv, POLARSSL_MD_NONE,
ansond 0:137634ff4186 451 hash, sizeof( hash ),
ansond 0:137634ff4186 452 sig, &sig_len, NULL, NULL ) ) != 0 )
ansond 0:137634ff4186 453 {
ansond 0:137634ff4186 454 return( ret );
ansond 0:137634ff4186 455 }
ansond 0:137634ff4186 456
ansond 0:137634ff4186 457 if( rsa_verify_wrap( (void *) pub, POLARSSL_MD_NONE,
ansond 0:137634ff4186 458 hash, sizeof( hash ), sig, sig_len ) != 0 )
ansond 0:137634ff4186 459 {
ansond 0:137634ff4186 460 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
ansond 0:137634ff4186 461 }
ansond 0:137634ff4186 462
ansond 0:137634ff4186 463 return( 0 );
ansond 0:137634ff4186 464 }
ansond 0:137634ff4186 465 #endif /* POLARSSL_RSA_C */
ansond 0:137634ff4186 466
ansond 0:137634ff4186 467 static void *rsa_alt_alloc_wrap( void )
ansond 0:137634ff4186 468 {
ansond 0:137634ff4186 469 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
ansond 0:137634ff4186 470
ansond 0:137634ff4186 471 if( ctx != NULL )
ansond 0:137634ff4186 472 memset( ctx, 0, sizeof( rsa_alt_context ) );
ansond 0:137634ff4186 473
ansond 0:137634ff4186 474 return( ctx );
ansond 0:137634ff4186 475 }
ansond 0:137634ff4186 476
ansond 0:137634ff4186 477 static void rsa_alt_free_wrap( void *ctx )
ansond 0:137634ff4186 478 {
ansond 0:137634ff4186 479 polarssl_zeroize( ctx, sizeof( rsa_alt_context ) );
ansond 0:137634ff4186 480 polarssl_free( ctx );
ansond 0:137634ff4186 481 }
ansond 0:137634ff4186 482
ansond 0:137634ff4186 483 const pk_info_t rsa_alt_info = {
ansond 0:137634ff4186 484 POLARSSL_PK_RSA_ALT,
ansond 0:137634ff4186 485 "RSA-alt",
ansond 0:137634ff4186 486 rsa_alt_get_size,
ansond 0:137634ff4186 487 rsa_alt_can_do,
ansond 0:137634ff4186 488 NULL,
ansond 0:137634ff4186 489 rsa_alt_sign_wrap,
ansond 0:137634ff4186 490 rsa_alt_decrypt_wrap,
ansond 0:137634ff4186 491 NULL,
ansond 0:137634ff4186 492 #if defined(POLARSSL_RSA_C)
ansond 0:137634ff4186 493 rsa_alt_check_pair,
ansond 0:137634ff4186 494 #else
ansond 0:137634ff4186 495 NULL,
ansond 0:137634ff4186 496 #endif
ansond 0:137634ff4186 497 rsa_alt_alloc_wrap,
ansond 0:137634ff4186 498 rsa_alt_free_wrap,
ansond 0:137634ff4186 499 NULL,
ansond 0:137634ff4186 500 };
ansond 0:137634ff4186 501
ansond 0:137634ff4186 502 #endif /* POLARSSL_PK_C */
ansond 0:137634ff4186 503