00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "polarssl/config.h"
00027
00028 #if defined(POLARSSL_PK_C)
00029
00030 #include "polarssl/pk_wrap.h"
00031
00032
00033 #include "polarssl/rsa.h"
00034
00035 #if defined(POLARSSL_ECP_C)
00036 #include "polarssl/ecp.h"
00037 #endif
00038
00039 #if defined(POLARSSL_ECDSA_C)
00040 #include "polarssl/ecdsa.h"
00041 #endif
00042
00043 #if defined(POLARSSL_MEMORY_C)
00044 #include "polarssl/memory.h"
00045 #else
00046 #include <stdlib.h>
00047 #define polarssl_malloc malloc
00048 #define polarssl_free free
00049 #endif
00050
00051
00052 static int rsa_can_do( pk_type_t type )
00053 {
00054 return( type == POLARSSL_PK_RSA );
00055 }
00056
00057 #if defined(POLARSSL_RSA_C)
00058 static size_t rsa_get_size( const void *ctx )
00059 {
00060 return( 8 * ((rsa_context *) ctx)->len );
00061 }
00062
00063 static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
00064 const unsigned char *hash, size_t hash_len,
00065 const unsigned char *sig, size_t sig_len )
00066 {
00067 if( sig_len != ((rsa_context *) ctx)->len )
00068 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
00069
00070 return( rsa_pkcs1_verify( (rsa_context *) ctx, NULL, NULL,
00071 RSA_PUBLIC, md_alg, (unsigned int) hash_len, hash, sig ) );
00072 }
00073
00074 static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
00075 const unsigned char *hash, size_t hash_len,
00076 unsigned char *sig, size_t *sig_len,
00077 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00078 {
00079 *sig_len = ((rsa_context *) ctx)->len;
00080
00081 return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
00082 md_alg, (unsigned int) hash_len, hash, sig ) );
00083 }
00084
00085 static int rsa_decrypt_wrap( void *ctx,
00086 const unsigned char *input, size_t ilen,
00087 unsigned char *output, size_t *olen, size_t osize,
00088 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00089 {
00090 if( ilen != ((rsa_context *) ctx)->len )
00091 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
00092
00093 return( rsa_pkcs1_decrypt( (rsa_context *) ctx, f_rng, p_rng,
00094 RSA_PRIVATE, olen, input, output, osize ) );
00095 }
00096
00097 static int rsa_encrypt_wrap( void *ctx,
00098 const unsigned char *input, size_t ilen,
00099 unsigned char *output, size_t *olen, size_t osize,
00100 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00101 {
00102 ((void) osize);
00103
00104 *olen = ((rsa_context *) ctx)->len;
00105
00106 return( rsa_pkcs1_encrypt( (rsa_context *) ctx,
00107 f_rng, p_rng, RSA_PUBLIC, ilen, input, output ) );
00108 }
00109
00110 static void *rsa_alloc_wrap( void )
00111 {
00112 void *ctx = polarssl_malloc( sizeof( rsa_context ) );
00113
00114 if( ctx != NULL )
00115 rsa_init( (rsa_context *) ctx, 0, 0 );
00116
00117 return ctx;
00118 }
00119
00120 static void rsa_free_wrap( void *ctx )
00121 {
00122 rsa_free( (rsa_context *) ctx );
00123 polarssl_free( ctx );
00124 }
00125
00126 static void rsa_debug( const void *ctx, pk_debug_item *items )
00127 {
00128 items->type = POLARSSL_PK_DEBUG_MPI;
00129 items->name = "rsa.N";
00130 items->value = &( ((rsa_context *) ctx)->N );
00131
00132 items++;
00133
00134 items->type = POLARSSL_PK_DEBUG_MPI;
00135 items->name = "rsa.E";
00136 items->value = &( ((rsa_context *) ctx)->E );
00137 }
00138
00139 const pk_info_t rsa_info = {
00140 POLARSSL_PK_RSA,
00141 "RSA",
00142 rsa_get_size,
00143 rsa_can_do,
00144 rsa_verify_wrap,
00145 rsa_sign_wrap,
00146 rsa_decrypt_wrap,
00147 rsa_encrypt_wrap,
00148 rsa_alloc_wrap,
00149 rsa_free_wrap,
00150 rsa_debug,
00151 };
00152 #endif
00153
00154 #if defined(POLARSSL_ECP_C)
00155
00156
00157
00158 static int eckey_can_do( pk_type_t type )
00159 {
00160 return( type == POLARSSL_PK_ECKEY ||
00161 type == POLARSSL_PK_ECKEY_DH ||
00162 type == POLARSSL_PK_ECDSA );
00163 }
00164
00165 static size_t eckey_get_size( const void *ctx )
00166 {
00167 return( ((ecp_keypair *) ctx)->grp.pbits );
00168 }
00169
00170 #if defined(POLARSSL_ECDSA_C)
00171
00172 static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
00173 const unsigned char *hash, size_t hash_len,
00174 const unsigned char *sig, size_t sig_len );
00175
00176 static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
00177 const unsigned char *hash, size_t hash_len,
00178 unsigned char *sig, size_t *sig_len,
00179 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
00180
00181 static int eckey_verify_wrap( void *ctx, md_type_t md_alg,
00182 const unsigned char *hash, size_t hash_len,
00183 const unsigned char *sig, size_t sig_len )
00184 {
00185 int ret;
00186 ecdsa_context ecdsa;
00187
00188 ecdsa_init( &ecdsa );
00189
00190 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
00191 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
00192
00193 ecdsa_free( &ecdsa );
00194
00195 return( ret );
00196 }
00197
00198 static int eckey_sign_wrap( void *ctx, md_type_t md_alg,
00199 const unsigned char *hash, size_t hash_len,
00200 unsigned char *sig, size_t *sig_len,
00201 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00202 {
00203 int ret;
00204 ecdsa_context ecdsa;
00205
00206 ecdsa_init( &ecdsa );
00207
00208 if( ( ret = ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
00209 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
00210 f_rng, p_rng );
00211
00212 ecdsa_free( &ecdsa );
00213
00214 return( ret );
00215 }
00216
00217 #endif
00218
00219 static void *eckey_alloc_wrap( void )
00220 {
00221 void *ctx = polarssl_malloc( sizeof( ecp_keypair ) );
00222
00223 if( ctx != NULL )
00224 ecp_keypair_init( ctx );
00225
00226 return( ctx );
00227 }
00228
00229 static void eckey_free_wrap( void *ctx )
00230 {
00231 ecp_keypair_free( (ecp_keypair *) ctx );
00232 polarssl_free( ctx );
00233 }
00234
00235 static void eckey_debug( const void *ctx, pk_debug_item *items )
00236 {
00237 items->type = POLARSSL_PK_DEBUG_ECP;
00238 items->name = "eckey.Q";
00239 items->value = &( ((ecp_keypair *) ctx)->Q );
00240 }
00241
00242 const pk_info_t eckey_info = {
00243 POLARSSL_PK_ECKEY,
00244 "EC",
00245 eckey_get_size,
00246 eckey_can_do,
00247 #if defined(POLARSSL_ECDSA_C)
00248 eckey_verify_wrap,
00249 eckey_sign_wrap,
00250 #else
00251 NULL,
00252 NULL,
00253 #endif
00254 NULL,
00255 NULL,
00256 eckey_alloc_wrap,
00257 eckey_free_wrap,
00258 eckey_debug,
00259 };
00260
00261
00262
00263
00264 static int eckeydh_can_do( pk_type_t type )
00265 {
00266 return( type == POLARSSL_PK_ECKEY ||
00267 type == POLARSSL_PK_ECKEY_DH );
00268 }
00269
00270 const pk_info_t eckeydh_info = {
00271 POLARSSL_PK_ECKEY_DH,
00272 "EC_DH",
00273 eckey_get_size,
00274 eckeydh_can_do,
00275 NULL,
00276 NULL,
00277 NULL,
00278 NULL,
00279 eckey_alloc_wrap,
00280 eckey_free_wrap,
00281 eckey_debug,
00282 };
00283 #endif
00284
00285 #if defined(POLARSSL_ECDSA_C)
00286 static int ecdsa_can_do( pk_type_t type )
00287 {
00288 return( type == POLARSSL_PK_ECDSA );
00289 }
00290
00291 static int ecdsa_verify_wrap( void *ctx, md_type_t md_alg,
00292 const unsigned char *hash, size_t hash_len,
00293 const unsigned char *sig, size_t sig_len )
00294 {
00295 ((void) md_alg);
00296
00297 return( ecdsa_read_signature( (ecdsa_context *) ctx,
00298 hash, hash_len, sig, sig_len ) );
00299 }
00300
00301 static int ecdsa_sign_wrap( void *ctx, md_type_t md_alg,
00302 const unsigned char *hash, size_t hash_len,
00303 unsigned char *sig, size_t *sig_len,
00304 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00305 {
00306 ((void) md_alg);
00307
00308 return( ecdsa_write_signature( (ecdsa_context *) ctx,
00309 hash, hash_len, sig, sig_len, f_rng, p_rng ) );
00310 }
00311
00312 static void *ecdsa_alloc_wrap( void )
00313 {
00314 void *ctx = polarssl_malloc( sizeof( ecdsa_context ) );
00315
00316 if( ctx != NULL )
00317 ecdsa_init( (ecdsa_context *) ctx );
00318
00319 return( ctx );
00320 }
00321
00322 static void ecdsa_free_wrap( void *ctx )
00323 {
00324 ecdsa_free( (ecdsa_context *) ctx );
00325 polarssl_free( ctx );
00326 }
00327
00328 const pk_info_t ecdsa_info = {
00329 POLARSSL_PK_ECDSA,
00330 "ECDSA",
00331 eckey_get_size,
00332 ecdsa_can_do,
00333 ecdsa_verify_wrap,
00334 ecdsa_sign_wrap,
00335 NULL,
00336 NULL,
00337 ecdsa_alloc_wrap,
00338 ecdsa_free_wrap,
00339 eckey_debug,
00340 };
00341 #endif
00342
00343
00344
00345
00346
00347 static size_t rsa_alt_get_size( const void *ctx )
00348 {
00349 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
00350
00351 return( rsa_alt->key_len_func( rsa_alt->key ) );
00352 }
00353
00354 static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
00355 const unsigned char *hash, size_t hash_len,
00356 unsigned char *sig, size_t *sig_len,
00357 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00358 {
00359 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
00360
00361 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
00362
00363 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
00364 md_alg, (unsigned int) hash_len, hash, sig ) );
00365 }
00366
00367 static int rsa_alt_decrypt_wrap( void *ctx,
00368 const unsigned char *input, size_t ilen,
00369 unsigned char *output, size_t *olen, size_t osize,
00370 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00371 {
00372 rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
00373
00374 ((void) f_rng);
00375 ((void) p_rng);
00376
00377 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
00378 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
00379
00380 return( rsa_alt->decrypt_func( rsa_alt->key,
00381 RSA_PRIVATE, olen, input, output, osize ) );
00382 }
00383
00384 static void *rsa_alt_alloc_wrap( void )
00385 {
00386 void *ctx = polarssl_malloc( sizeof( rsa_alt_context ) );
00387
00388 if( ctx != NULL )
00389 memset( ctx, 0, sizeof( rsa_alt_context ) );
00390
00391 return ctx;
00392 }
00393
00394 static void rsa_alt_free_wrap( void *ctx )
00395 {
00396 polarssl_free( ctx );
00397 }
00398
00399 const pk_info_t rsa_alt_info = {
00400 POLARSSL_PK_RSA_ALT,
00401 "RSA-alt",
00402 rsa_alt_get_size,
00403 rsa_can_do,
00404 NULL,
00405 rsa_alt_sign_wrap,
00406 rsa_alt_decrypt_wrap,
00407 NULL,
00408 rsa_alt_alloc_wrap,
00409 rsa_alt_free_wrap,
00410 NULL,
00411 };
00412
00413 #endif