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

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

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4