/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/library/dsa.c

Go to the documentation of this file.
00001 /*
00002  *  The RSA Public-Key cryptosystem
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License, version 2.1 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *  MA  02110-1301  USA
00019  */
00020 /*
00021  *  RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
00022  *
00023  *  http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
00024  *  http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
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  * Generate an RSA keypair
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      * find primes P and Q with Q < P so that:
00056      * GCD( E, (P-1)*(Q-1) ) == 1
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      * D  = E^-1 mod ((P-1)*(Q-1))
00083      * DP = D mod (P - 1)
00084      * DQ = D mod (Q - 1)
00085      * QP = Q^-1 mod P
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  * Perform an RSA public key operation
00110  */
00111 int rsa_public( rsa_context   *ctx,
00112                 unsigned char *input,  int ilen,
00113                 unsigned char *output, int olen )
00114 {
00115     int ret;
00116     mpi T;
00117 
00118     if( ilen != ctx->len || olen != ctx->len )
00119         return( ERR_RSA_BAD_INPUT_DATA );
00120 
00121     mpi_init( &T, NULL );
00122 
00123     CHK( mpi_read_binary( &T, input, ilen ) );
00124 
00125     if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
00126     {
00127         mpi_free( &T, NULL );
00128         return( ERR_RSA_BAD_INPUT_DATA );
00129     }
00130 
00131     CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
00132     CHK( mpi_write_binary( &T, output, &olen ) );
00133 
00134 cleanup:
00135 
00136     mpi_free( &T, NULL );
00137 
00138     if( ret != 0 )
00139         return( ERR_RSA_PUBLIC_FAILED | ret );
00140 
00141     return( 0 );
00142 }
00143 
00144 /*
00145  * Perform an RSA private key operation
00146  */
00147 int rsa_private( rsa_context   *ctx,
00148                  unsigned char *input,  int ilen,
00149                  unsigned char *output, int olen )
00150 {
00151     int ret;
00152     mpi T, T1, T2;
00153 
00154     if( ilen != ctx->len || olen != ctx->len )
00155         return( ERR_RSA_BAD_INPUT_DATA );
00156 
00157     mpi_init( &T, &T1, &T2, NULL );
00158 
00159     CHK( mpi_read_binary( &T, input, ilen ) );
00160 
00161     if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
00162     {
00163         mpi_free( &T, NULL );
00164         return( ERR_RSA_BAD_INPUT_DATA );
00165     }
00166 
00167 #if 0
00168     CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
00169 #else
00170     /*
00171      * faster decryption using the CRT
00172      *
00173      * T1 = input ^ dP mod P
00174      * T2 = input ^ dQ mod Q
00175      */
00176     CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
00177     CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
00178 
00179     /*
00180      * T = (T1 - T2) * (Q^-1 mod P) mod P
00181      */
00182     CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
00183     CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
00184     CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
00185 
00186     /*
00187      * output = T2 + T * Q
00188      */
00189     CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
00190     CHK( mpi_add_mpi( &T, &T2, &T1 ) );
00191 #endif
00192 
00193     CHK( mpi_write_binary( &T, output, &olen ) );
00194 
00195 cleanup:
00196 
00197     mpi_free( &T, &T1, &T2, NULL );
00198 
00199     if( ret != 0 )
00200         return( ERR_RSA_PRIVATE_FAILED | ret );
00201 
00202     return( 0 );
00203 }
00204 
00205 /*
00206  * Check if the public key is valid
00207  */
00208 int rsa_check_pubkey( rsa_context *ctx )
00209 {
00210     if( ( ctx->N.p[0] & 1 ) == 0 || 
00211         ( ctx->E.p[0] & 1 ) == 0 )
00212         return( ERR_RSA_KEY_CHK_FAILED );
00213 
00214     if( mpi_msb( &ctx->N ) < 128 ||
00215         mpi_msb( &ctx->N ) > 4096 )
00216         return( ERR_RSA_KEY_CHK_FAILED );
00217 
00218     if( mpi_msb( &ctx->E ) < 2 ||
00219         mpi_msb( &ctx->E ) > 64 )
00220         return( ERR_RSA_KEY_CHK_FAILED );
00221 
00222     return( 0 );
00223 }
00224 
00225 /*
00226  * Check if the private key is valid
00227  */
00228 int rsa_check_privkey( rsa_context *ctx )
00229 {
00230     int ret = 0;
00231     mpi TN, P1, Q1, H, G;
00232 
00233     mpi_init( &TN, &P1, &Q1, &H, &G, NULL );
00234 
00235     CHK( mpi_mul_mpi( &TN, &ctx->P, &ctx->Q ) );
00236     CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
00237     CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
00238     CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
00239     CHK( mpi_gcd( &G, &ctx->E, &H  ) );
00240 
00241     if( mpi_cmp_mpi( &TN, &ctx->N ) == 0 &&
00242         mpi_cmp_int( &G, 1 ) == 0 )
00243     {
00244         mpi_free( &TN, &P1, &Q1, &H, &G, NULL );
00245         return( 0 );
00246     }
00247 
00248 cleanup:
00249 
00250     mpi_free( &TN, &P1, &Q1, &H, &G, NULL );
00251     return( ERR_RSA_KEY_CHK_FAILED | ret );
00252 }
00253 
00254 /*
00255  * Add the PKCS#1 v1.5 padding and do a public RSA
00256  */
00257 int rsa_pkcs1_encrypt( rsa_context   *ctx,
00258                        unsigned char *input,  int ilen,
00259                        unsigned char *output, int olen )
00260 {
00261     int nb_pad;
00262     unsigned char *p = output;
00263 
00264     if( olen != ctx->len || olen < ilen + 11 )
00265         return( ERR_RSA_BAD_INPUT_DATA );
00266 
00267     nb_pad = olen - 3 - ilen;
00268 
00269     *p++ = 0;
00270     *p++ = RSA_CRYPT;
00271 
00272     while( nb_pad-- > 0 )
00273     {
00274         do { *p = rand(); } while( *p == 0 );
00275         p++;
00276     }
00277 
00278     *p++ = 0;
00279     memcpy( p, input, ilen );
00280 
00281     return( rsa_public( ctx, output, olen, output, olen ) );
00282 }
00283 
00284 /*
00285  * Do a private RSA, removes the PKCS#1 v1.5 padding
00286  */
00287 int rsa_pkcs1_decrypt( rsa_context   *ctx,
00288                        unsigned char *input,  int  ilen,
00289                        unsigned char *output, int *olen )
00290 {
00291     int ret;
00292     unsigned char *p, buf[512];
00293 
00294     if( ilen != ctx->len || ilen < 16 || ilen > 512 )
00295         return( ERR_RSA_BAD_INPUT_DATA );
00296 
00297     if( ( ret = rsa_private( ctx, input, ilen, buf, ilen ) ) != 0 )
00298         return( ret );
00299 
00300     p = buf;
00301 
00302     if( *p++ != 0 || *p++ != RSA_CRYPT )
00303         return( ERR_RSA_INVALID_PADDING );
00304 
00305     while( *p != 0 )
00306     {
00307         if( p >= buf + ilen - 1 )
00308             return( ERR_RSA_INVALID_PADDING );
00309         p++;
00310     }
00311     p++;
00312 
00313     if( *olen < ilen - (int)(p - buf) )
00314         return( ERR_RSA_INVALID_PADDING );
00315 
00316     *olen = ilen - (int)(p - buf);
00317     memcpy( output, p, *olen );
00318 
00319     return( 0 );
00320 }
00321 
00322 /*
00323  * Perform a private RSA to sign a message digest
00324  */
00325 int rsa_pkcs1_sign( rsa_context   *ctx,  int alg_id,
00326                     unsigned char *hash, int hashlen,
00327                     unsigned char *sig,  int siglen )
00328 {
00329     int nb_pad;
00330     unsigned char *p = sig;
00331 
00332     if( siglen != ctx->len || siglen < 16 )
00333         return( ERR_RSA_BAD_INPUT_DATA );
00334 
00335     switch( alg_id )
00336     {
00337         case RSA_RAW:
00338             nb_pad = siglen - 3 - hashlen;
00339             break;
00340 
00341         case RSA_MD2:
00342         case RSA_MD4:
00343         case RSA_MD5:
00344             nb_pad = siglen - 3 - 34;
00345             break;
00346 
00347         case RSA_SHA1:
00348             nb_pad = siglen - 3 - 35;
00349             break;
00350 
00351         default:
00352             return( ERR_RSA_BAD_INPUT_DATA );
00353     }
00354 
00355     if( nb_pad < 8 )
00356         return( ERR_RSA_BAD_INPUT_DATA );
00357 
00358     *p++ = 0;
00359     *p++ = RSA_SIGN;
00360 
00361     memset( p, 0xFF, nb_pad );
00362     p += nb_pad;
00363     *p++ = 0;
00364 
00365     switch( alg_id )
00366     {
00367         case RSA_RAW:
00368             memcpy( p, hash, hashlen );
00369             break;
00370 
00371         case RSA_MD2:
00372             memcpy( p, ASN1_HASH_MDX, 18 );
00373             memcpy( p + 18, hash, 16 );
00374             p[13] = 2; break;
00375 
00376         case RSA_MD4:
00377             memcpy( p, ASN1_HASH_MDX, 18 );
00378             memcpy( p + 18, hash, 16 );
00379             p[13] = 4; break;
00380 
00381         case RSA_MD5:
00382             memcpy( p, ASN1_HASH_MDX, 18 );
00383             memcpy( p + 18, hash, 16 );
00384             p[13] = 5; break;
00385 
00386         case RSA_SHA1:
00387             memcpy( p, ASN1_HASH_SHA1, 15 );
00388             memcpy( p + 15, hash, 20 );
00389             break;
00390 
00391         default:
00392             return( ERR_RSA_BAD_INPUT_DATA );
00393     }
00394 
00395     return( rsa_private( ctx, sig, siglen, sig, siglen ) );
00396 }
00397 
00398 /*
00399  * Perform a public RSA and check the message digest
00400  */
00401 int rsa_pkcs1_verify( rsa_context   *ctx,  int alg_id,
00402                       unsigned char *hash, int hashlen,
00403                       unsigned char *sig,  int siglen )
00404 {
00405     int ret, len;
00406     unsigned char *p, c, buf[512];
00407 
00408     if( siglen != ctx->len || siglen < 16 || siglen > 512 )
00409         return( ERR_RSA_BAD_INPUT_DATA );
00410 
00411     if( ( ret = rsa_public( ctx, sig, siglen, buf, siglen ) ) != 0 )
00412         return( ret );
00413 
00414     p = buf;
00415 
00416     if( *p++ != 0 || *p++ != RSA_SIGN )
00417         return( ERR_RSA_INVALID_PADDING );
00418 
00419     while( *p != 0 )
00420     {
00421         if( p >= buf + siglen - 1 || *p != 0xFF )
00422             return( ERR_RSA_INVALID_PADDING );
00423         p++;
00424     }
00425     p++;
00426 
00427     len = siglen - (int)( p - buf );
00428 
00429     if( len == 34 )
00430     {
00431         c = p[13];
00432         p[13] = 0;
00433 
00434         if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
00435             return( ERR_RSA_VERIFY_FAILED );
00436 
00437         if( ( c == 2 && alg_id == RSA_MD2 ) ||
00438             ( c == 4 && alg_id == RSA_MD4 ) ||
00439             ( c == 5 && alg_id == RSA_MD5 ) )
00440         {
00441             if( memcmp( p + 18, hash, 16 ) == 0 ) 
00442                 return( 0 );
00443             else
00444                 return( ERR_RSA_VERIFY_FAILED );
00445         }
00446     }
00447 
00448     if( len == 35 && alg_id == RSA_SHA1 )
00449     {
00450         if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
00451             memcmp( p + 15, hash, 20 ) == 0 )
00452             return( 0 );
00453         else
00454             return( ERR_RSA_VERIFY_FAILED );
00455     }
00456 
00457     if( len == hashlen && alg_id == RSA_RAW )
00458     {
00459         if( memcmp( p, hash, hashlen ) == 0 )
00460             return( 0 );
00461         else
00462             return( ERR_RSA_VERIFY_FAILED );
00463     }
00464 
00465     return( ERR_RSA_INVALID_PADDING );
00466 }
00467 
00468 /*
00469  * Free the components of an RSA key
00470  */
00471 void rsa_free( rsa_context *ctx )
00472 {
00473     mpi_free( &ctx->N,  &ctx->E,  &ctx->D,
00474               &ctx->P,  &ctx->Q,  &ctx->DP,
00475               &ctx->DQ, &ctx->QP, &ctx->RN,
00476               &ctx->RP, &ctx->RQ, NULL );
00477 }
00478 
00479 #if defined(SELF_TEST)
00480 
00481 #include "xyssl/sha1.h"
00482 
00483 #define PT_LEN  24
00484 #define RSA_PT  "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
00485                 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
00486 
00487 /*
00488  * Checkup routine
00489  */
00490 int rsa_self_test( int verbose )
00491 {
00492     int len;
00493     rsa_context rsa;
00494     unsigned char sha1sum[20];
00495     unsigned char rsa_plaintext[PT_LEN];
00496     unsigned char rsa_decrypted[PT_LEN];
00497     unsigned char rsa_ciphertext[KEY_LEN];
00498 
00499     memset( &rsa, 0, sizeof( rsa ) );
00500 
00501     rsa.len = KEY_LEN;
00502     mpi_read_string( &rsa.N , 16, RSA_N  );
00503     mpi_read_string( &rsa.E , 16, RSA_E  );
00504     mpi_read_string( &rsa.D , 16, RSA_D  );
00505     mpi_read_string( &rsa.P , 16, RSA_P  );
00506     mpi_read_string( &rsa.Q , 16, RSA_Q  );
00507     mpi_read_string( &rsa.DP, 16, RSA_DP );
00508     mpi_read_string( &rsa.DQ, 16, RSA_DQ );
00509     mpi_read_string( &rsa.QP, 16, RSA_QP );
00510 
00511     if( verbose != 0 )
00512         printf( "  RSA key validation: " );
00513 
00514     if( rsa_check_pubkey(  &rsa ) != 0 ||
00515         rsa_check_privkey( &rsa ) != 0 )
00516     {
00517         if( verbose != 0 )
00518             printf( "failed\n" );
00519 
00520         return( 1 );
00521     }
00522 
00523     if( verbose != 0 )
00524         printf( "passed\n  PKCS#1 encryption : " );
00525 
00526     memcpy( rsa_plaintext, RSA_PT, PT_LEN );
00527 
00528     if( rsa_pkcs1_encrypt( &rsa, rsa_plaintext,  PT_LEN,
00529                                  rsa_ciphertext, KEY_LEN ) != 0 )
00530     {
00531         if( verbose != 0 )
00532             printf( "failed\n" );
00533 
00534         return( 1 );
00535     }
00536 
00537     if( verbose != 0 )
00538         printf( "passed\n  PKCS#1 decryption : " );
00539 
00540     len = sizeof( rsa_decrypted );
00541 
00542     if( rsa_pkcs1_decrypt( &rsa, rsa_ciphertext, KEY_LEN,
00543                                  rsa_decrypted,  &len ) != 0 ||
00544         memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
00545     {
00546         if( verbose != 0 )
00547             printf( "failed\n" );
00548 
00549         return( 1 );
00550     }
00551 
00552     if( verbose != 0 )
00553         printf( "passed\n  PKCS#1 data sign  : " );
00554 
00555     sha1( rsa_plaintext, PT_LEN, sha1sum );
00556 
00557     if( rsa_pkcs1_sign( &rsa, RSA_SHA1, sha1sum, 20,
00558                         rsa_ciphertext, KEY_LEN ) != 0 )
00559     {
00560         if( verbose != 0 )
00561             printf( "failed\n" );
00562 
00563         return( 1 );
00564     }
00565 
00566     if( verbose != 0 )
00567         printf( "passed\n  PKCS#1 sig. verify: " );
00568 
00569     if( rsa_pkcs1_verify( &rsa, RSA_SHA1, sha1sum, 20,
00570                           rsa_ciphertext, KEY_LEN ) != 0 )
00571     {
00572         if( verbose != 0 )
00573             printf( "failed\n" );
00574 
00575         return( 1 );
00576     }
00577 
00578     if( verbose != 0 )
00579         printf( "passed\n\n" );
00580 
00581     rsa_free( &rsa );
00582 
00583     return( 0 );
00584 }
00585 #else
00586 int rsa_self_test( int verbose )
00587 {
00588     return( 0 );
00589 }
00590 #endif

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4