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