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
00027 #ifndef _CRT_SECURE_NO_DEPRECATE
00028 #define _CRT_SECURE_NO_DEPRECATE 1
00029 #endif
00030
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <stdio.h>
00034
00035 #include "xyssl/rsa.h"
00036
00037 #if !defined(NO_GENPRIME)
00038
00039
00040
00041 int rsa_gen_key( rsa_context *ctx, int nbits, int exponent,
00042 int (*rng_f)(void *), void *rng_d )
00043 {
00044 int ret;
00045 mpi P1, Q1, H, G;
00046
00047 if( nbits < 128 || exponent < 3 || rng_f == NULL )
00048 return( ERR_RSA_BAD_INPUT_DATA );
00049
00050 mpi_init( &P1, &Q1, &H, &G, NULL );
00051
00052 memset( ctx, 0, sizeof( rsa_context ) );
00053
00054
00055
00056
00057
00058 CHK( mpi_lset( &ctx->E, exponent ) );
00059
00060 nbits >>= 1;
00061
00062 do
00063 {
00064 CHK( mpi_gen_prime( &ctx->P, nbits, 0, rng_f, rng_d ) );
00065 CHK( mpi_gen_prime( &ctx->Q, nbits, 0, rng_f, rng_d ) );
00066
00067 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
00068 mpi_swap( &ctx->P, &ctx->Q );
00069
00070 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
00071 continue;
00072
00073 CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
00074 CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
00075 CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
00076 CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
00077 CHK( mpi_gcd( &G, &ctx->E, &H ) );
00078 }
00079 while( mpi_cmp_int( &G, 1 ) != 0 );
00080
00081
00082
00083
00084
00085
00086
00087 CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
00088 CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
00089 CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
00090 CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
00091
00092 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
00093
00094 cleanup:
00095
00096 mpi_free( &P1, &Q1, &H, &G, NULL );
00097
00098 if( ret != 0 )
00099 {
00100 rsa_free( ctx );
00101 return( ERR_RSA_KEY_GEN_FAILED | ret );
00102 }
00103
00104 return( 0 );
00105 }
00106 #endif
00107
00108
00109
00110
00111 int rsa_read_public( rsa_context *ctx, FILE *f )
00112 {
00113 int ret;
00114
00115 memset( ctx, 0, sizeof( rsa_context ) );
00116
00117 CHK( mpi_read_file( &ctx->N, 16, f ) );
00118 CHK( mpi_read_file( &ctx->E, 16, f ) );
00119
00120 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
00121
00122 cleanup:
00123
00124 if( ret != 0 )
00125 {
00126 rsa_free( ctx );
00127 return( ERR_RSA_KEY_RD_FAILED | ret );
00128 }
00129
00130 return( 0 );
00131 }
00132
00133
00134
00135
00136 int rsa_read_private( rsa_context *ctx, FILE *f )
00137 {
00138 int ret;
00139
00140 memset( ctx, 0, sizeof( rsa_context ) );
00141
00142 CHK( mpi_read_file( &ctx->N , 16, f ) );
00143 CHK( mpi_read_file( &ctx->E , 16, f ) );
00144 CHK( mpi_read_file( &ctx->D , 16, f ) );
00145 CHK( mpi_read_file( &ctx->P , 16, f ) );
00146 CHK( mpi_read_file( &ctx->Q , 16, f ) );
00147 CHK( mpi_read_file( &ctx->DP, 16, f ) );
00148 CHK( mpi_read_file( &ctx->DQ, 16, f ) );
00149 CHK( mpi_read_file( &ctx->QP, 16, f ) );
00150
00151 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
00152
00153 cleanup:
00154
00155 if( ret != 0 )
00156 {
00157 rsa_free( ctx );
00158 return( ERR_RSA_KEY_RD_FAILED | ret );
00159 }
00160
00161 return( 0 );
00162 }
00163
00164
00165
00166
00167 int rsa_write_public( rsa_context *ctx, FILE *f )
00168 {
00169 int ret;
00170
00171 CHK( mpi_write_file( "N = ", &ctx->N, 16, f ) );
00172 CHK( mpi_write_file( "E = ", &ctx->E, 16, f ) );
00173
00174 cleanup:
00175
00176 if( ret != 0 )
00177 return( ERR_RSA_KEY_WR_FAILED | ret );
00178
00179 return( 0 );
00180 }
00181
00182
00183
00184
00185 int rsa_write_private( rsa_context *ctx, FILE *f )
00186 {
00187 int ret;
00188
00189 CHK( mpi_write_file( "N = " , &ctx->N , 16, f ) );
00190 CHK( mpi_write_file( "E = " , &ctx->E , 16, f ) );
00191 CHK( mpi_write_file( "D = " , &ctx->D , 16, f ) );
00192 CHK( mpi_write_file( "P = " , &ctx->P , 16, f ) );
00193 CHK( mpi_write_file( "Q = " , &ctx->Q , 16, f ) );
00194 CHK( mpi_write_file( "DP = ", &ctx->DP, 16, f ) );
00195 CHK( mpi_write_file( "DQ = ", &ctx->DQ, 16, f ) );
00196 CHK( mpi_write_file( "QP = ", &ctx->QP, 16, f ) );
00197
00198 cleanup:
00199
00200 if( ret != 0 )
00201 return( ERR_RSA_KEY_WR_FAILED | ret );
00202
00203 return( 0 );
00204 }
00205
00206
00207
00208
00209 int rsa_public( rsa_context *ctx,
00210 unsigned char *input, int ilen,
00211 unsigned char *output, int olen )
00212 {
00213 int ret;
00214 mpi T;
00215
00216 if( ilen != ctx->len || olen != ctx->len )
00217 return( ERR_RSA_BAD_INPUT_DATA );
00218
00219 mpi_init( &T, NULL );
00220
00221 CHK( mpi_read_binary( &T, input, ilen ) );
00222
00223 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
00224 {
00225 mpi_free( &T, NULL );
00226 return( ERR_RSA_BAD_INPUT_DATA );
00227 }
00228
00229 CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
00230 CHK( mpi_write_binary( &T, output, &olen ) );
00231
00232 cleanup:
00233
00234 mpi_free( &T, NULL );
00235
00236 if( ret != 0 )
00237 return( ERR_RSA_PUBLIC_FAILED | ret );
00238
00239 return( 0 );
00240 }
00241
00242
00243
00244
00245 int rsa_private( rsa_context *ctx,
00246 unsigned char *input, int ilen,
00247 unsigned char *output, int olen )
00248 {
00249 int ret;
00250 mpi T, T1, T2;
00251
00252 if( ilen != ctx->len || olen != ctx->len )
00253 return( ERR_RSA_BAD_INPUT_DATA );
00254
00255 mpi_init( &T, &T1, &T2, NULL );
00256
00257 CHK( mpi_read_binary( &T, input, ilen ) );
00258
00259 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
00260 {
00261 mpi_free( &T, NULL );
00262 return( ERR_RSA_BAD_INPUT_DATA );
00263 }
00264
00265 #if 0
00266 CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
00267 #else
00268
00269
00270
00271
00272
00273
00274 CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
00275 CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
00276
00277
00278
00279
00280 CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
00281 CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
00282 CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
00283
00284
00285
00286
00287 CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
00288 CHK( mpi_add_mpi( &T, &T2, &T1 ) );
00289 #endif
00290
00291 CHK( mpi_write_binary( &T, output, &olen ) );
00292
00293 cleanup:
00294
00295 mpi_free( &T, &T1, &T2, NULL );
00296
00297 if( ret != 0 )
00298 return( ERR_RSA_PRIVATE_FAILED | ret );
00299
00300 return( 0 );
00301 }
00302
00303
00304
00305
00306 int rsa_check_pubkey( rsa_context *ctx )
00307 {
00308 if( ( ctx->N.p[0] & 1 ) == 0 ||
00309 ( ctx->E.p[0] & 1 ) == 0 )
00310 return( ERR_RSA_KEY_CHK_FAILED );
00311
00312 if( mpi_msb( &ctx->N ) < 128 ||
00313 mpi_msb( &ctx->N ) > 4096 )
00314 return( ERR_RSA_KEY_CHK_FAILED );
00315
00316 if( mpi_msb( &ctx->E ) < 2 ||
00317 mpi_msb( &ctx->E ) > 64 )
00318 return( ERR_RSA_KEY_CHK_FAILED );
00319
00320 return( 0 );
00321 }
00322
00323
00324
00325
00326 int rsa_check_privkey( rsa_context *ctx )
00327 {
00328 int ret = 0;
00329 mpi TN, P1, Q1, H, G;
00330
00331 mpi_init( &TN, &P1, &Q1, &H, &G, NULL );
00332
00333 CHK( mpi_mul_mpi( &TN, &ctx->P, &ctx->Q ) );
00334 CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
00335 CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
00336 CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
00337 CHK( mpi_gcd( &G, &ctx->E, &H ) );
00338
00339 if( mpi_cmp_mpi( &TN, &ctx->N ) == 0 &&
00340 mpi_cmp_int( &G, 1 ) == 0 )
00341 {
00342 mpi_free( &TN, &P1, &Q1, &H, &G, NULL );
00343 return( 0 );
00344 }
00345
00346 cleanup:
00347
00348 mpi_free( &TN, &P1, &Q1, &H, &G, NULL );
00349 return( ERR_RSA_KEY_CHK_FAILED | ret );
00350 }
00351
00352
00353
00354
00355 int rsa_pkcs1_encrypt( rsa_context *ctx,
00356 unsigned char *input, int ilen,
00357 unsigned char *output, int olen )
00358 {
00359 int nb_pad;
00360 unsigned char *p = output;
00361
00362 if( olen != ctx->len || olen < ilen + 11 )
00363 return( ERR_RSA_BAD_INPUT_DATA );
00364
00365 nb_pad = olen - 3 - ilen;
00366
00367 *p++ = 0;
00368 *p++ = RSA_CRYPT;
00369
00370 while( nb_pad-- > 0 )
00371 {
00372 do { *p = rand(); } while( *p == 0 );
00373 p++;
00374 }
00375
00376 *p++ = 0;
00377 memcpy( p, input, ilen );
00378
00379 return( rsa_public( ctx, output, olen, output, olen ) );
00380 }
00381
00382
00383
00384
00385 int rsa_pkcs1_decrypt( rsa_context *ctx,
00386 unsigned char *input, int ilen,
00387 unsigned char *output, int *olen )
00388 {
00389 int ret;
00390 unsigned char *p, buf[512];
00391
00392 if( ilen != ctx->len || ilen < 16 || ilen > 512 )
00393 return( ERR_RSA_BAD_INPUT_DATA );
00394
00395 if( ( ret = rsa_private( ctx, input, ilen, buf, ilen ) ) != 0 )
00396 return( ret );
00397
00398 p = buf;
00399
00400 if( *p++ != 0 || *p++ != RSA_CRYPT )
00401 return( ERR_RSA_INVALID_PADDING );
00402
00403 while( *p != 0 )
00404 {
00405 if( p >= buf + ilen - 1 )
00406 return( ERR_RSA_INVALID_PADDING );
00407 p++;
00408 }
00409 p++;
00410
00411 if( *olen < ilen - (int)(p - buf) )
00412 return( ERR_RSA_INVALID_PADDING );
00413
00414 *olen = ilen - (int)(p - buf);
00415 memcpy( output, p, *olen );
00416
00417 return( 0 );
00418 }
00419
00420
00421
00422
00423 int rsa_pkcs1_sign( rsa_context *ctx, int alg_id,
00424 unsigned char *hash, int hashlen,
00425 unsigned char *sig, int siglen )
00426 {
00427 int nb_pad;
00428 unsigned char *p = sig;
00429
00430 if( siglen != ctx->len || siglen < 16 )
00431 return( ERR_RSA_BAD_INPUT_DATA );
00432
00433 switch( alg_id )
00434 {
00435 case RSA_RAW:
00436 nb_pad = siglen - 3 - hashlen;
00437 break;
00438
00439 case RSA_MD2:
00440 case RSA_MD4:
00441 case RSA_MD5:
00442 nb_pad = siglen - 3 - 34;
00443 break;
00444
00445 case RSA_SHA1:
00446 nb_pad = siglen - 3 - 35;
00447 break;
00448
00449 default:
00450 return( ERR_RSA_BAD_INPUT_DATA );
00451 }
00452
00453 if( nb_pad < 8 )
00454 return( ERR_RSA_BAD_INPUT_DATA );
00455
00456 *p++ = 0;
00457 *p++ = RSA_SIGN;
00458
00459 memset( p, 0xFF, nb_pad );
00460 p += nb_pad;
00461 *p++ = 0;
00462
00463 switch( alg_id )
00464 {
00465 case RSA_RAW:
00466 memcpy( p, hash, hashlen );
00467 break;
00468
00469 case RSA_MD2:
00470 memcpy( p, ASN1_HASH_MDX, 18 );
00471 memcpy( p + 18, hash, 16 );
00472 p[13] = 2; break;
00473
00474 case RSA_MD4:
00475 memcpy( p, ASN1_HASH_MDX, 18 );
00476 memcpy( p + 18, hash, 16 );
00477 p[13] = 4; break;
00478
00479 case RSA_MD5:
00480 memcpy( p, ASN1_HASH_MDX, 18 );
00481 memcpy( p + 18, hash, 16 );
00482 p[13] = 5; break;
00483
00484 case RSA_SHA1:
00485 memcpy( p, ASN1_HASH_SHA1, 15 );
00486 memcpy( p + 15, hash, 20 );
00487 break;
00488
00489 default:
00490 return( ERR_RSA_BAD_INPUT_DATA );
00491 }
00492
00493 return( rsa_private( ctx, sig, siglen, sig, siglen ) );
00494 }
00495
00496
00497
00498
00499 int rsa_pkcs1_verify( rsa_context *ctx, int alg_id,
00500 unsigned char *hash, int hashlen,
00501 unsigned char *sig, int siglen )
00502 {
00503 int ret, len;
00504 unsigned char *p, c, buf[512];
00505
00506 if( siglen != ctx->len || siglen < 16 || siglen > 512 )
00507 return( ERR_RSA_BAD_INPUT_DATA );
00508
00509 if( ( ret = rsa_public( ctx, sig, siglen, buf, siglen ) ) != 0 )
00510 return( ret );
00511
00512 p = buf;
00513
00514 if( *p++ != 0 || *p++ != RSA_SIGN )
00515 return( ERR_RSA_INVALID_PADDING );
00516
00517 while( *p != 0 )
00518 {
00519 if( p >= buf + siglen - 1 || *p != 0xFF )
00520 return( ERR_RSA_INVALID_PADDING );
00521 p++;
00522 }
00523 p++;
00524
00525 len = siglen - (int)( p - buf );
00526
00527 if( len == 34 )
00528 {
00529 c = p[13];
00530 p[13] = 0;
00531
00532 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
00533 return( ERR_RSA_VERIFY_FAILED );
00534
00535 if( ( c == 2 && alg_id == RSA_MD2 ) ||
00536 ( c == 4 && alg_id == RSA_MD4 ) ||
00537 ( c == 5 && alg_id == RSA_MD5 ) )
00538 {
00539 if( memcmp( p + 18, hash, 16 ) == 0 )
00540 return( 0 );
00541 else
00542 return( ERR_RSA_VERIFY_FAILED );
00543 }
00544 }
00545
00546 if( len == 35 && alg_id == RSA_SHA1 )
00547 {
00548 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
00549 memcmp( p + 15, hash, 20 ) == 0 )
00550 return( 0 );
00551 else
00552 return( ERR_RSA_VERIFY_FAILED );
00553 }
00554
00555 if( len == hashlen && alg_id == RSA_RAW )
00556 {
00557 if( memcmp( p, hash, hashlen ) == 0 )
00558 return( 0 );
00559 else
00560 return( ERR_RSA_VERIFY_FAILED );
00561 }
00562
00563 return( ERR_RSA_INVALID_PADDING );
00564 }
00565
00566
00567
00568
00569 void rsa_free( rsa_context *ctx )
00570 {
00571 mpi_free( &ctx->N, &ctx->E, &ctx->D,
00572 &ctx->P, &ctx->Q, &ctx->DP,
00573 &ctx->DQ, &ctx->QP, &ctx->RN,
00574 &ctx->RP, &ctx->RQ, NULL );
00575 }
00576
00577 #if defined(SELF_TEST)
00578
00579 #include "xyssl/sha1.h"
00580
00581 #define PT_LEN 24
00582 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
00583 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
00584
00585
00586
00587
00588 int rsa_self_test( int verbose )
00589 {
00590 int len;
00591 rsa_context rsa;
00592 unsigned char sha1sum[20];
00593 unsigned char rsa_plaintext[PT_LEN];
00594 unsigned char rsa_decrypted[PT_LEN];
00595 unsigned char rsa_ciphertext[KEY_LEN];
00596
00597 memset( &rsa, 0, sizeof( rsa_context ) );
00598
00599 rsa.len = KEY_LEN;
00600 mpi_read_string( &rsa.N , 16, RSA_N );
00601 mpi_read_string( &rsa.E , 16, RSA_E );
00602 mpi_read_string( &rsa.D , 16, RSA_D );
00603 mpi_read_string( &rsa.P , 16, RSA_P );
00604 mpi_read_string( &rsa.Q , 16, RSA_Q );
00605 mpi_read_string( &rsa.DP, 16, RSA_DP );
00606 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
00607 mpi_read_string( &rsa.QP, 16, RSA_QP );
00608
00609 if( verbose != 0 )
00610 printf( " RSA key validation: " );
00611
00612 if( rsa_check_pubkey( &rsa ) != 0 ||
00613 rsa_check_privkey( &rsa ) != 0 )
00614 {
00615 if( verbose != 0 )
00616 printf( "failed\n" );
00617
00618 return( 1 );
00619 }
00620
00621 if( verbose != 0 )
00622 printf( "passed\n PKCS#1 encryption : " );
00623
00624 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
00625
00626 if( rsa_pkcs1_encrypt( &rsa, rsa_plaintext, PT_LEN,
00627 rsa_ciphertext, KEY_LEN ) != 0 )
00628 {
00629 if( verbose != 0 )
00630 printf( "failed\n" );
00631
00632 return( 1 );
00633 }
00634
00635 if( verbose != 0 )
00636 printf( "passed\n PKCS#1 decryption : " );
00637
00638 len = sizeof( rsa_decrypted );
00639
00640 if( rsa_pkcs1_decrypt( &rsa, rsa_ciphertext, KEY_LEN,
00641 rsa_decrypted, &len ) != 0 ||
00642 memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
00643 {
00644 if( verbose != 0 )
00645 printf( "failed\n" );
00646
00647 return( 1 );
00648 }
00649
00650 if( verbose != 0 )
00651 printf( "passed\n PKCS#1 data sign : " );
00652
00653 sha1( rsa_plaintext, PT_LEN, sha1sum );
00654
00655 if( rsa_pkcs1_sign( &rsa, RSA_SHA1, sha1sum, 20,
00656 rsa_ciphertext, KEY_LEN ) != 0 )
00657 {
00658 if( verbose != 0 )
00659 printf( "failed\n" );
00660
00661 return( 1 );
00662 }
00663
00664 if( verbose != 0 )
00665 printf( "passed\n PKCS#1 sig. verify: " );
00666
00667 if( rsa_pkcs1_verify( &rsa, RSA_SHA1, sha1sum, 20,
00668 rsa_ciphertext, KEY_LEN ) != 0 )
00669 {
00670 if( verbose != 0 )
00671 printf( "failed\n" );
00672
00673 return( 1 );
00674 }
00675
00676 if( verbose != 0 )
00677 printf( "passed\n\n" );
00678
00679 rsa_free( &rsa );
00680
00681 return( 0 );
00682 }
00683 #else
00684 int rsa_self_test( int verbose )
00685 {
00686 return( 0 );
00687 }
00688 #endif