Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TranslationCertificate.cc

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: TranslationCertificate.cc,v 1.3 2004/05/19 15:56:47 mmarsh Exp $
00008 //
00009 // $Log: TranslationCertificate.cc,v $
00010 // Revision 1.3  2004/05/19 15:56:47  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.2  2003/11/04 22:31:48  mmarsh
00014 // *** empty log message ***
00015 //
00016 //
00017 
00018 #include "TranslationCertificate.h"
00019 #include "CODEX_Exceptions/BignumExceptions.h"
00020 
00021 using namespace CODEX_Ciphers;
00022 using namespace CODEX_Exceptions;
00023 
00024 TranslationCertificate::TranslationCertificate() :
00025    CODEX_ASN1::Base( false )
00026 {
00027 }
00028 
00030 TranslationCertificate::TranslationCertificate(
00031    const ElGamalPublicKey& pubKeyRecipient,
00032    const ElGamalPrivateKey& privKeyCreator,
00033    const CODEX_ASN1::BigNumber& h,
00034    const BIGNUM * k,
00035    const ElGamalCipherText& cipherTextCreator,
00036    const HashFunction& hashFunc ) :
00037    CODEX_ASN1::Base( true )
00038 {
00039    int length;
00040    unsigned char* buff = NULL;
00041    unsigned char* pBuff;
00042    CODEX_ASN1::ustring* tempStr = NULL;
00043 
00044    const BIGNUM * p = pubKeyRecipient.p().value();
00045    // Jakobsson uses q where he should use p-1.  We will duplicate this
00046    // error in notation, though not in computation.
00047    BIGNUM * q = (BIGNUM*)pubKeyRecipient.p1();
00048    const BIGNUM * g = pubKeyRecipient.g().value();
00049    const BIGNUM * y = pubKeyRecipient.y().value();
00050    const BIGNUM * x = privKeyCreator.value();
00051    const BIGNUM * c1 = cipherTextCreator.c1().value();
00052    const BIGNUM * hbn = h.value();
00053    BIGNUM * b1 = NULL;
00054    BIGNUM * b2 = NULL;
00055    BIGNUM * temp = NULL;
00056    BIGNUM * temp2 = NULL;
00057    BIGNUM * temp3 = NULL;
00058    BIGNUM * temp4 = NULL;
00059    BN_CTX * ctx = NULL;
00060 
00061    try
00062    {
00063       ctx = BN_CTX_new();
00064       if ( NULL == ctx )
00065       {
00066          throw BignumContextException( __FILE__ , __LINE__ );
00067       }
00068       // We will use these BIGNUMs as much as possible, to reduce the need
00069       // to allocate memory.
00070       temp = BN_new();
00071       if ( NULL == temp )
00072       {
00073          throw BignumNullException( __FILE__ , __LINE__ );
00074       }
00075       temp2 = BN_new();
00076       if ( NULL == temp2 )
00077       {
00078          throw BignumNullException( __FILE__ , __LINE__ );
00079       }
00080       temp3 = BN_new();
00081       if ( NULL == temp3 )
00082       {
00083          throw BignumNullException( __FILE__ , __LINE__ );
00084       }
00085       temp4 = BN_new();
00086       if ( NULL == temp4 )
00087       {
00088          throw BignumNullException( __FILE__ , __LINE__ );
00089       }
00090 
00091       // h^x mod p
00092       if ( ! BN_mod_exp( temp, hbn, x, p, ctx ) )
00093       {
00094          throw BignumModExpException( __FILE__ , __LINE__ );
00095       }
00096       m_z1bar = CODEX_ASN1::BigNumber( BN_dup(temp) );
00097 
00098       // h^(-k) mod p
00099       if ( ! BN_mod_exp( temp, hbn, k, p, ctx ) )
00100       {
00101          throw BignumModExpException( __FILE__ , __LINE__ );
00102       }
00103       if ( ! BN_mod_inverse( temp, temp, p, ctx ) )
00104       {
00105          throw BignumModInverseException( __FILE__ , __LINE__ );
00106       }
00107       m_z2bar = CODEX_ASN1::BigNumber( BN_dup(temp) );
00108 
00109       // select temp random in [2,q)
00110       do
00111       {
00112          if ( ! BN_rand_range( temp, q ) )
00113          {
00114             throw BignumRandRangeException( __FILE__ , __LINE__ );
00115          }
00116       } while ( 0 >= BN_cmp( temp, BN_value_one() ) );
00117 
00118       // G = hash( g^temp mod p )
00119       if ( ! BN_mod_exp( temp2, g, temp, p, ctx ) )
00120       {
00121          throw BignumModExpException( __FILE__ , __LINE__ );
00122       }
00123       CODEX_ASN1::BigNumber galpha( BN_dup(temp2) );
00124       length = galpha.marshal(0);
00125       buff = new unsigned char[length];
00126       pBuff = buff;
00127       galpha.marshal(&pBuff);
00128       tempStr = hashFunc( CODEX_ASN1::ustring(buff, length) );
00129       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), temp2 ) )
00130       {
00131          throw BignumBin2BNException( __FILE__ , __LINE__ );
00132       }    
00133       if ( ! BN_mod( temp2, temp2, q, ctx ) )
00134       {
00135          throw BignumModException( __FILE__ , __LINE__ );
00136       }
00137       m_G = CODEX_ASN1::BigNumber( BN_dup(temp2) );
00138       delete tempStr;
00139       tempStr = NULL;
00140       delete [] buff;
00141       buff = NULL;
00142 
00143       // H = hash( h^temp mod p )
00144       if ( ! BN_mod_exp( temp2, hbn, temp, p, ctx ) )
00145       {
00146          throw BignumModExpException( __FILE__ , __LINE__ );
00147       }
00148       CODEX_ASN1::BigNumber halpha( BN_dup(temp2) );
00149       length = halpha.marshal(0);
00150       buff = new unsigned char[length];
00151       pBuff = buff;
00152       halpha.marshal(&pBuff);
00153       tempStr = hashFunc( CODEX_ASN1::ustring(buff, length) );
00154       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), temp2 ) )
00155       {
00156          throw BignumBin2BNException( __FILE__ , __LINE__ );
00157       }    
00158       if ( ! BN_mod( temp2, temp2, q, ctx ) )
00159       {
00160          throw BignumModException( __FILE__ , __LINE__ );
00161       }
00162       m_H = CODEX_ASN1::BigNumber( BN_dup(temp2) );
00163       delete tempStr;
00164       tempStr = NULL;
00165       delete [] buff;
00166       buff = NULL;
00167 
00168       // delta = temp + hash(G,H,2)*k - hash(G,H,1)*x mod q
00169       length = 1; // we're going to add a byte at the end
00170       length += m_G.marshal(0);
00171       length += m_H.marshal(0);
00172       buff = new unsigned char[length];
00173       pBuff = buff;
00174       m_G.marshal(&pBuff);
00175       m_H.marshal(&pBuff);
00176       buff[length-1] = 1;
00177       tempStr = hashFunc( CODEX_ASN1::ustring(buff, length) );
00178       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), temp2 ) )
00179       {
00180          throw BignumBin2BNException( __FILE__ , __LINE__ );
00181       }    
00182       if ( ! BN_mod( temp2, temp2, q, ctx ) )
00183       {
00184          throw BignumModException( __FILE__ , __LINE__ );
00185       }
00186       delete tempStr;
00187       tempStr = NULL;
00188       buff[length-1] = 2;
00189       tempStr = hashFunc( CODEX_ASN1::ustring(buff, length) );
00190       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), temp3 ) )
00191       {
00192          throw BignumBin2BNException( __FILE__ , __LINE__ );
00193       }    
00194       if ( ! BN_mod( temp3, temp3, q, ctx ) )
00195       {
00196          throw BignumModException( __FILE__ , __LINE__ );
00197       }
00198       delete tempStr;
00199       tempStr = NULL;
00200       delete [] buff;
00201       buff = NULL;
00202       if ( ! BN_mod_mul( temp2, temp2, x, q, ctx ) )
00203       {
00204          throw BignumModMulException( __FILE__ , __LINE__ );
00205       }
00206       if ( ! BN_mod_mul( temp3, temp3, k, q, ctx ) )
00207       {
00208          throw BignumModMulException( __FILE__ , __LINE__ );
00209       }
00210       if ( ! BN_add( temp3, temp, temp3 ) )
00211       {
00212          throw BignumAddException( __FILE__ , __LINE__ );
00213       }
00214       if ( ! BN_sub( temp4, temp3, temp2 ) )
00215       {
00216          throw BignumSubException( __FILE__ , __LINE__ );
00217       }
00218       if ( ! BN_add( temp4, temp4, q ) )
00219       {
00220          throw BignumAddException( __FILE__ , __LINE__ );
00221       }
00222       // We can re-use temp now -- we're done with the random number.
00223       if ( ! BN_mod( temp, temp4, q, ctx ) )
00224       {
00225          throw BignumModException( __FILE__ , __LINE__ );
00226       }
00227       m_delta = CODEX_ASN1::BigNumber( BN_dup(temp) );
00228 
00229       // select temp random in [2,q)
00230       do
00231       {
00232          if ( ! BN_rand_range( temp, q ) )
00233          {
00234             throw BignumRandRangeException( __FILE__ , __LINE__ );
00235          }
00236       } while ( 0 >= BN_cmp( temp, BN_value_one() ) );
00237       // select temp2 random in [2,q)
00238       do
00239       {
00240          if ( ! BN_rand_range( temp2, q ) )
00241          {
00242             throw BignumRandRangeException( __FILE__ , __LINE__ );
00243          }
00244       } while ( 0 >= BN_cmp( temp2, BN_value_one() ) );
00245 
00246       // temp3 = g^temp mod p
00247       if ( ! BN_mod_exp( temp3, g, temp, p, ctx ) )
00248       {
00249          throw BignumModExpException( __FILE__ , __LINE__ );
00250       }
00251       // temp4 = h^temp2 mod p
00252       if ( ! BN_mod_exp( temp4, hbn, temp2, p, ctx ) )
00253       {
00254          throw BignumModExpException( __FILE__ , __LINE__ );
00255       }
00256       // temp3 = temp3 * temp4 mod p = g^temp h^temp2 mod p
00257       if ( ! BN_mod_mul( temp3, temp3, temp4, p, ctx ) )
00258       {
00259          throw BignumModMulException( __FILE__ , __LINE__ );
00260       }
00261       CODEX_ASN1::BigNumber fhash( BN_dup(temp3) );
00262       length = fhash.marshal(0);
00263       buff = new unsigned char[length];
00264       pBuff = buff;
00265       fhash.marshal(&pBuff);
00266       tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00267       b1 = BN_new();
00268       if ( NULL == b1 )
00269       {
00270          throw BignumNullException( __FILE__ , __LINE__ );
00271       }
00272       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00273       {
00274          throw BignumBin2BNException( __FILE__ , __LINE__ );
00275       }
00276       if ( ! BN_mod( b1, b1, q, ctx ) )
00277       {
00278          throw BignumModException( __FILE__ , __LINE__ );
00279       }
00280       m_F = CODEX_ASN1::BigNumber( b1 );
00281       b1 = NULL;
00282       delete tempStr;
00283       tempStr = NULL;
00284       delete [] buff;
00285       buff = NULL;
00286 
00287       // temp3 = b_1^temp mod p
00288       if ( ! BN_mod_exp( temp3, c1, temp, p, ctx ) )
00289       {
00290          throw BignumModExpException( __FILE__ , __LINE__ );
00291       }
00292       // temp4 = y_2^temp2 mod p
00293       if ( ! BN_mod_exp( temp4, y, temp2, p, ctx ) )
00294       {
00295          throw BignumModExpException( __FILE__ , __LINE__ );
00296       }
00297       // temp3 = temp3 * temp4 mod p
00298       if ( ! BN_mod_mul( temp3, temp3, temp4, p, ctx ) )
00299       {
00300          throw BignumModMulException( __FILE__ , __LINE__ );
00301       }
00302       CODEX_ASN1::BigNumber mhash( BN_dup(temp3) );
00303       length = mhash.marshal(0);
00304       buff = new unsigned char[length];
00305       pBuff = buff;
00306       mhash.marshal(&pBuff);
00307       tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00308       b1 = BN_new();
00309       if ( NULL == b1 )
00310       {
00311          throw BignumNullException( __FILE__ , __LINE__ );
00312       }
00313       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00314       {
00315          throw BignumBin2BNException( __FILE__ , __LINE__ );
00316       }
00317       if ( ! BN_mod( b1, b1, q, ctx ) )
00318       {
00319          throw BignumModException( __FILE__ , __LINE__ );
00320       }
00321       m_M = CODEX_ASN1::BigNumber( b1 );
00322       b1 = NULL;
00323       delete tempStr;
00324       tempStr = NULL;
00325       delete [] buff;
00326       buff = NULL;
00327 
00328       length = 0;
00329       length += m_F.marshal(0);
00330       length += m_M.marshal(0);
00331       buff = new unsigned char[length];
00332       pBuff = buff;
00333       m_F.marshal(&pBuff);
00334       m_M.marshal(&pBuff);
00335       tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00336       b1 = BN_new();
00337       if ( NULL == b1 )
00338       {
00339          throw BignumNullException( __FILE__ , __LINE__ );
00340       }
00341       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00342       {
00343          throw BignumBin2BNException( __FILE__ , __LINE__ );
00344       }
00345       if ( ! BN_mod( b1, b1, q, ctx ) )
00346       {
00347          throw BignumModException( __FILE__ , __LINE__ );
00348       }
00349       delete tempStr;
00350       tempStr = NULL;
00351       delete [] buff;
00352       buff = NULL;
00353 
00354       if ( ! BN_mod_mul( temp3, b1, x, q, ctx ) )
00355       {
00356          throw BignumModMulException( __FILE__ , __LINE__ );
00357       }
00358       if ( ! BN_sub( temp4, temp, temp3 ) )
00359       {
00360          throw BignumSubException( __FILE__ , __LINE__ );
00361       }
00362       if ( ! BN_add( temp4, temp4, q ) ) // make sure it's positive
00363       {
00364          throw BignumAddException( __FILE__ , __LINE__ );
00365       }
00366       if ( ! BN_mod( temp4, temp4, q, ctx ) )
00367       {
00368          throw BignumModException( __FILE__ , __LINE__ );
00369       }
00370       m_d1 = CODEX_ASN1::BigNumber( BN_dup(temp4) );
00371 
00372       if ( ! BN_mod_mul( temp3, b1, k, q, ctx ) )
00373       {
00374          throw BignumModMulException( __FILE__ , __LINE__ );
00375       }
00376       if ( ! BN_add( temp3, temp2, temp3 ) )
00377       {
00378          throw BignumAddException( __FILE__ , __LINE__ );
00379       }
00380       if ( ! BN_mod( temp3, temp3, q, ctx ) )
00381       {
00382          throw BignumModException( __FILE__ , __LINE__ );
00383       }
00384       m_d2 = CODEX_ASN1::BigNumber( BN_dup(temp3) );
00385 
00386       BN_free( b1 );
00387       b1 = NULL;
00388 
00389       // Final cleanup.
00390       if ( NULL != b1 )
00391       {
00392          BN_free( b1 );
00393       }
00394       if ( NULL != b2 )
00395       {
00396          BN_free( b2 );
00397       }
00398       if ( NULL != temp )
00399       {
00400          BN_free( temp );
00401       }
00402       if ( NULL != temp2 )
00403       {
00404          BN_free( temp2 );
00405       }
00406       if ( NULL != temp3 )
00407       {
00408          BN_free( temp3 );
00409       }
00410       if ( NULL != temp4 )
00411       {
00412          BN_free( temp4 );
00413       }
00414       if ( NULL != ctx )
00415       {
00416          BN_CTX_free( ctx );
00417       }
00418       if ( NULL != buff )
00419       {
00420          delete [] buff;
00421       }
00422       if ( NULL != tempStr )
00423       {
00424          delete tempStr;
00425       }
00426    }
00427    catch( ... )
00428    {
00429       // All memory cleanup from exceptions is done here.
00430       if ( NULL != b1 ) BN_free( b1 );
00431       if ( NULL != b2 ) BN_free( b2 );
00432       if ( NULL != temp ) BN_free( temp );
00433       if ( NULL != temp2 ) BN_free( temp2 );
00434       if ( NULL != temp3 ) BN_free( temp3 );
00435       if ( NULL != temp4 ) BN_free( temp4 );
00436       if ( NULL != ctx ) BN_CTX_free( ctx );
00437       if ( NULL != buff ) delete [] buff;
00438       if ( NULL != tempStr ) delete tempStr;
00439       throw;
00440    }
00441 }
00442 
00443 TranslationCertificate::TranslationCertificate(
00444    const CODEX_ASN1::BigNumber& z1bar,
00445    const CODEX_ASN1::BigNumber& z2bar,
00446    const CODEX_ASN1::BigNumber& G,
00447    const CODEX_ASN1::BigNumber& H,
00448    const CODEX_ASN1::BigNumber& delta,
00449    const CODEX_ASN1::BigNumber& F,
00450    const CODEX_ASN1::BigNumber& M,
00451    const CODEX_ASN1::BigNumber& d1,
00452    const CODEX_ASN1::BigNumber& d2 ) :
00453    CODEX_ASN1::Base( true ),
00454    m_z1bar( z1bar ),
00455    m_z2bar( z2bar ),
00456    m_G( G ),
00457    m_H( H ),
00458    m_delta( delta ),
00459    m_F( F ),
00460    m_M( M ),
00461    m_d1( d1 ),
00462    m_d2( d2 )
00463 {
00464 }
00465 
00466 TranslationCertificate::TranslationCertificate(
00467    const TranslationCertificate& aCert ) :
00468    CODEX_ASN1::Base( aCert.m_initialized ),
00469    m_z1bar( aCert.m_z1bar ),
00470    m_z2bar( aCert.m_z2bar ),
00471    m_G( aCert.m_G ),
00472    m_H( aCert.m_H ),
00473    m_delta( aCert.m_delta ),
00474    m_F( aCert.m_F ),
00475    m_M( aCert.m_M ),
00476    m_d1( aCert.m_d1 ),
00477    m_d2( aCert.m_d2 )
00478 {
00479 }
00480 
00481 void
00482 TranslationCertificate::operator=( const TranslationCertificate& aCert )
00483 {
00484    m_initialized = aCert.m_initialized;
00485    m_z1bar       = aCert.m_z1bar;
00486    m_z2bar       = aCert.m_z2bar;
00487    m_G           = aCert.m_G;
00488    m_H           = aCert.m_H;
00489    m_delta       = aCert.m_delta;
00490    m_F           = aCert.m_F;
00491    m_M           = aCert.m_M;
00492    m_d1          = aCert.m_d1;
00493    m_d2          = aCert.m_d2;
00494 }
00495 
00496 bool
00497 TranslationCertificate::verify( const CODEX_ASN1::BigNumber& yCreator,
00498                                 const ElGamalPublicKey& pubKeyRecipient,
00499                                 const ElGamalCipherText& cipherTextCreator,
00500                                 const ElGamalCipherText& cipherTextRecipient,
00501                                 const CODEX_ASN1::BigNumber& h,
00502                                 const HashFunction& hashFunc ) const
00503 {
00504    bool retVal = true;
00505    int length;
00506    unsigned char* buff = NULL;
00507    unsigned char* pBuff;
00508    CODEX_ASN1::ustring* tempStr = NULL;
00509 
00510    // We're going to use Jakobsson's notation here, so that it's easier
00511    // to match the operations to the math in the paper.
00512    const BIGNUM * p     = pubKeyRecipient.p().value();
00513    const BIGNUM * g     = pubKeyRecipient.g().value();
00514    const BIGNUM * q     = NULL;
00515    const BIGNUM * hbn   = h.value();
00516    const BIGNUM * z1    = yCreator.value();
00517    BIGNUM       * z2    = NULL;
00518    const BIGNUM * mu1   = cipherTextCreator.c1().value();
00519    const BIGNUM * mu2   = pubKeyRecipient.y().value();
00520    BIGNUM       * sigma = NULL;
00521    const BIGNUM * z1Bar = m_z1bar.value();
00522    const BIGNUM * z2Bar = m_z2bar.value();
00523    const BIGNUM * Gbn   = m_G.value();
00524    const BIGNUM * Hbn   = m_H.value();
00525    const BIGNUM * Delta = m_delta.value();
00526    const BIGNUM * Fbn   = m_F.value();
00527    const BIGNUM * Mbn   = m_M.value();
00528    const BIGNUM * d1bn  = m_d1.value();
00529    const BIGNUM * d2bn  = m_d2.value();
00530 
00531    BIGNUM * e1    = NULL;
00532    BIGNUM * e2    = NULL;
00533    BIGNUM * temp1 = NULL;
00534    BIGNUM * temp2 = NULL;
00535    BIGNUM * temp3 = NULL;
00536    BIGNUM * temp4 = NULL;
00537    BIGNUM * b1    = NULL;
00538    BIGNUM * b2    = NULL;
00539    BN_CTX * ctx   = NULL;
00540 
00541    try
00542    {
00543       // Jakobsson uses q where he should use p-1.  We will duplicate
00544       // this error in notation, though not in computation.
00545       q = pubKeyRecipient.p1();
00546 
00547       ctx = BN_CTX_new();
00548       if ( NULL == ctx )
00549       {
00550          throw BignumContextException( __FILE__ , __LINE__ );
00551       }
00552 
00553       z2 = BN_new();
00554       if ( NULL == z2 )
00555       {
00556          throw BignumNullException( __FILE__ , __LINE__ );
00557       }
00558       if ( ! BN_mod_inverse( z2,
00559                              cipherTextRecipient.c1().value(),
00560                              p,
00561                              ctx ) )
00562       {
00563          throw BignumModInverseException( __FILE__ , __LINE__ );
00564       }
00565 
00566       sigma = BN_new();
00567       if ( NULL == sigma )
00568       {
00569          throw BignumNullException( __FILE__ , __LINE__ );
00570       }
00571       if ( ! BN_mod_inverse( sigma,
00572                              cipherTextRecipient.c2().value(),
00573                              p,
00574                              ctx ) )
00575       {
00576          throw BignumModInverseException( __FILE__ , __LINE__ );
00577       }
00578       if ( ! BN_mod_mul( sigma,
00579                          sigma,
00580                          cipherTextCreator.c2().value(),
00581                          p,
00582                          ctx ) )
00583       {
00584          throw BignumModMulException( __FILE__ , __LINE__ );
00585       }
00586 
00587       // Create temporary variables
00588       temp1 = BN_new();
00589       if ( NULL == temp1 )
00590       {
00591          throw BignumNullException( __FILE__ , __LINE__ );
00592       }
00593       temp2 = BN_new();
00594       if ( NULL == temp2 )
00595       {
00596          throw BignumNullException( __FILE__ , __LINE__ );
00597       }
00598       temp3 = BN_new();
00599       if ( NULL == temp3 )
00600       {
00601          throw BignumNullException( __FILE__ , __LINE__ );
00602       }
00603       temp4 = BN_new();
00604       if ( NULL == temp4 )
00605       {
00606          throw BignumNullException( __FILE__ , __LINE__ );
00607       }
00608 
00609       // compute e1,e2
00610       length = 1; // we're going to add a byte at the end
00611       length += m_G.marshal(0);
00612       length += m_H.marshal(0);
00613       buff = new unsigned char[length];
00614       pBuff = buff;
00615       m_G.marshal(&pBuff);
00616       m_H.marshal(&pBuff);
00617       buff[length-1] = 1;
00618       tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00619       e1 = BN_new();
00620       if ( NULL == e1 )
00621       {
00622          throw BignumNullException( __FILE__ , __LINE__ );
00623       }
00624       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), e1 ) )
00625       {
00626          throw BignumBin2BNException( __FILE__ , __LINE__ );
00627       }
00628       if ( ! BN_mod( e1, e1, q, ctx ) )
00629       {
00630          throw BignumModException( __FILE__ , __LINE__ );
00631       }
00632       delete tempStr;
00633       tempStr = NULL;
00634       buff[length-1] = 2;
00635       tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00636       e2 = BN_new();
00637       if ( NULL == e2 )
00638       {
00639          throw BignumNullException( __FILE__ , __LINE__ );
00640       }
00641       if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), e2 ) )
00642       {
00643          throw BignumBin2BNException( __FILE__ , __LINE__ );
00644       }
00645       if ( ! BN_mod( e2, e2, q, ctx ) )
00646       {
00647          throw BignumModException( __FILE__ , __LINE__ );
00648       }
00649       delete tempStr;
00650       tempStr = NULL;
00651       delete [] buff;
00652       buff = NULL;
00653       if ( retVal )
00654       {
00655          // test G
00656          if ( ! BN_mod_exp( temp1, g, Delta, p, ctx ) )
00657          {
00658             throw BignumModExpException( __FILE__ , __LINE__ );
00659          }
00660          if ( ! BN_mod_exp( temp2, z1, e1, p, ctx ) )
00661          {
00662             throw BignumModExpException( __FILE__ , __LINE__ );
00663          }
00664          if ( ! BN_mod_exp( temp3, z2, e2, p, ctx ) )
00665          {
00666             throw BignumModExpException( __FILE__ , __LINE__ );
00667          }
00668          if ( ! BN_mod_mul( temp1, temp1, temp2, p, ctx ) )
00669          {
00670             throw BignumModMulException( __FILE__ , __LINE__ );
00671          }
00672          if ( ! BN_mod_mul( temp1, temp1, temp3, p, ctx ) )
00673          {
00674             throw BignumModMulException( __FILE__ , __LINE__ );
00675          }
00676          CODEX_ASN1::BigNumber thing( BN_dup(temp1) );
00677          length = thing.marshal(0);
00678          buff = new unsigned char[length];
00679          pBuff = buff;
00680          thing.marshal(&pBuff);
00681          tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00682          b1 = BN_new();
00683          if ( NULL == b1 )
00684          {
00685             throw BignumNullException( __FILE__ , __LINE__ );
00686          }
00687          if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00688          {
00689             throw BignumBin2BNException( __FILE__ , __LINE__ );
00690          }
00691          if ( ! BN_mod( b1, b1, q, ctx ) )
00692          {
00693             throw BignumModException( __FILE__ , __LINE__ );
00694          }
00695          delete tempStr;
00696          tempStr = NULL;
00697          delete [] buff;
00698          buff = NULL;
00699          if ( 0 != BN_cmp( Gbn, b1 ) )
00700          {
00701             retVal = false;
00702          }
00703          BN_free( b1 );
00704          b1 = NULL;
00705       }
00706       if ( retVal )
00707       {
00708          // test H
00709          if ( ! BN_mod_exp( temp1, hbn, Delta, p, ctx ) )
00710          {
00711             throw BignumModExpException( __FILE__ , __LINE__ );
00712          }
00713          if ( ! BN_mod_exp( temp2, z1Bar, e1, p, ctx ) )
00714          {
00715             throw BignumModExpException( __FILE__ , __LINE__ );
00716          }
00717          if ( ! BN_mod_exp( temp3, z2Bar, e2, p, ctx ) )
00718          {
00719             throw BignumModExpException( __FILE__ , __LINE__ );
00720          }
00721          if ( ! BN_mod_mul( temp1, temp1, temp2, p, ctx ) )
00722          {
00723             throw BignumModMulException( __FILE__ , __LINE__ );
00724          }
00725          if ( ! BN_mod_mul( temp1, temp1, temp3, p, ctx ) )
00726          {
00727             throw BignumModMulException( __FILE__ , __LINE__ );
00728          }
00729          CODEX_ASN1::BigNumber thing( BN_dup(temp1) );
00730          length = thing.marshal(0);
00731          buff = new unsigned char[length];
00732          pBuff = buff;
00733          thing.marshal(&pBuff);
00734          tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00735          b1 = BN_new();
00736          if ( NULL == b1 )
00737          {
00738             throw BignumNullException( __FILE__ , __LINE__ );
00739          }
00740          if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00741          {
00742             throw BignumBin2BNException( __FILE__ , __LINE__ );
00743          }
00744          if ( ! BN_mod( b1, b1, q, ctx ) )
00745          {
00746             throw BignumModException( __FILE__ , __LINE__ );
00747          }
00748          delete tempStr;
00749          tempStr = NULL;
00750          delete [] buff;
00751          buff = NULL;
00752          if ( 0 != BN_cmp( Hbn, b1 ) )
00753          {
00754             retVal = false;
00755          }
00756          BN_free( b1 );
00757          b1 = NULL;
00758       }
00759       BN_free( e1 );
00760       e1 = NULL;
00761       BN_free( e2 );
00762       e2 = NULL;
00763 
00764       if ( retVal )
00765       {
00766          // compute e1
00767          length = 0;
00768          length += m_F.marshal(0);
00769          length += m_M.marshal(0);
00770          buff = new unsigned char[length];
00771          pBuff = buff;
00772          m_F.marshal(&pBuff);
00773          m_M.marshal(&pBuff);
00774          tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00775          e1 = BN_new();
00776          if ( NULL == e1 )
00777          {
00778             throw BignumNullException( __FILE__ , __LINE__ );
00779          }
00780          if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), e1 ) )
00781          {
00782             throw BignumBin2BNException( __FILE__ , __LINE__ );
00783          }
00784          if ( ! BN_mod( e1, e1, q, ctx ) )
00785          {
00786             throw BignumModException( __FILE__ , __LINE__ );
00787          }
00788          delete tempStr;
00789          tempStr = NULL;
00790          delete [] buff;
00791          buff = NULL;
00792 
00793          // test F
00794          if ( ! BN_mod_exp( temp1, g, d1bn, p, ctx ) )
00795          {
00796             throw BignumModExpException( __FILE__ , __LINE__ );
00797          }
00798          if ( ! BN_mod_exp( temp2, hbn, d2bn, p, ctx ) )
00799          {
00800             throw BignumModExpException( __FILE__ , __LINE__ );
00801          }
00802          if ( ! BN_mod_mul( temp3, z1, z2Bar, p, ctx ) )
00803          {
00804             throw BignumModMulException( __FILE__ , __LINE__ );
00805          }
00806          if ( ! BN_mod_exp( temp4, temp3, e1, p, ctx ) )
00807          {
00808             throw BignumModExpException( __FILE__ , __LINE__ );
00809          }
00810          if ( ! BN_mod_mul( temp1, temp1, temp2, p, ctx ) )
00811          {
00812             throw BignumModMulException( __FILE__ , __LINE__ );
00813          }
00814          if ( ! BN_mod_mul( temp1, temp1, temp4, p, ctx ) )
00815          {
00816             throw BignumModMulException( __FILE__ , __LINE__ );
00817          }
00818          CODEX_ASN1::BigNumber thing( BN_dup(temp1) );
00819          length = thing.marshal(0);
00820          buff = new unsigned char[length];
00821          pBuff = buff;
00822          thing.marshal(&pBuff);
00823          tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00824          b1 = BN_new();
00825          if ( NULL == b1 )
00826          {
00827             throw BignumNullException( __FILE__ , __LINE__ );
00828          }
00829          if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00830          {
00831             throw BignumBin2BNException( __FILE__ , __LINE__ );
00832          }
00833          if ( ! BN_mod( b1, b1, q, ctx ) )
00834          {
00835             throw BignumModException( __FILE__ , __LINE__ );
00836          }
00837          delete tempStr;
00838          tempStr = NULL;
00839          delete [] buff;
00840          buff = NULL;
00841          if ( 0 != BN_cmp( Fbn, b1 ) )
00842          {
00843             retVal = false;
00844          }
00845          BN_free( b1 );
00846          b1 = NULL;
00847          if ( retVal )
00848          {
00849             // test M
00850             if ( ! BN_mod_exp( temp1, mu1, d1bn, p, ctx ) )
00851             {
00852                throw BignumModExpException( __FILE__ , __LINE__ );
00853             }
00854             if ( ! BN_mod_exp( temp2, mu2, d2bn, p, ctx ) )
00855             {
00856                throw BignumModExpException( __FILE__ , __LINE__ );
00857             }
00858             if ( ! BN_mod_exp( temp3, sigma, e1, p, ctx ) )
00859             {
00860                throw BignumModExpException( __FILE__ , __LINE__ );
00861             }
00862             if ( ! BN_mod_mul( temp1, temp1, temp2, p, ctx ) )
00863             {
00864                throw BignumModMulException( __FILE__ , __LINE__ );
00865             }
00866             if ( ! BN_mod_mul( temp1, temp1, temp3, p, ctx ) )
00867             {
00868                throw BignumModMulException( __FILE__ , __LINE__ );
00869             }
00870             CODEX_ASN1::BigNumber thing2( BN_dup(temp1) );
00871             length = thing2.marshal(0);
00872             buff = new unsigned char[length];
00873             pBuff = buff;
00874             thing2.marshal(&pBuff);
00875             tempStr = hashFunc( CODEX_ASN1::ustring( buff, length ) );
00876             b1 = BN_new();
00877             if ( NULL == b1 )
00878             {
00879                throw BignumNullException( __FILE__ , __LINE__ );
00880             }
00881             if ( NULL == BN_bin2bn( tempStr->data(), tempStr->length(), b1 ) )
00882             {
00883                throw BignumBin2BNException( __FILE__ , __LINE__ );
00884             }
00885             if ( ! BN_mod( b1, b1, q, ctx ) )
00886             {
00887                throw BignumModException( __FILE__ , __LINE__ );
00888             }
00889             delete tempStr;
00890             tempStr = NULL;
00891             delete [] buff;
00892             buff = NULL;
00893             if ( 0 != BN_cmp( Mbn, b1 ) )
00894             {
00895                retVal = false;
00896             }
00897             BN_free( b1 );
00898             b1 = NULL;
00899          }
00900          BN_free( e1 );
00901          e1 = NULL;
00902       }
00903 
00904       // Final cleanup
00905       if ( NULL != buff ) delete [] buff;
00906       if ( NULL != tempStr ) delete tempStr;
00907 
00908       if ( NULL != z2    ) BN_free( z2    );
00909       if ( NULL != sigma ) BN_free( sigma );
00910       if ( NULL != e1    ) BN_free( e1    );
00911       if ( NULL != e2    ) BN_free( e2    );
00912       if ( NULL != temp1 ) BN_free( temp1 );
00913       if ( NULL != temp2 ) BN_free( temp2 );
00914       if ( NULL != temp3 ) BN_free( temp3 );
00915       if ( NULL != temp4 ) BN_free( temp4 );
00916       if ( NULL != b1    ) BN_free( b1    );
00917       if ( NULL != b2    ) BN_free( b2    );
00918       if ( NULL != ctx   ) BN_CTX_free( ctx );
00919 
00920       return retVal;
00921    }
00922    catch ( ... )
00923    {
00924       // All memory cleanup from exceptions is done here.
00925       if ( NULL != buff ) delete [] buff;
00926       if ( NULL != tempStr ) delete tempStr;
00927 
00928       if ( NULL != z2    ) BN_free( z2    );
00929       if ( NULL != sigma ) BN_free( sigma );
00930       if ( NULL != e1    ) BN_free( e1    );
00931       if ( NULL != e2    ) BN_free( e2    );
00932       if ( NULL != temp1 ) BN_free( temp1 );
00933       if ( NULL != temp2 ) BN_free( temp2 );
00934       if ( NULL != temp3 ) BN_free( temp3 );
00935       if ( NULL != temp4 ) BN_free( temp4 );
00936       if ( NULL != b1    ) BN_free( b1    );
00937       if ( NULL != b2    ) BN_free( b2    );
00938       if ( NULL != ctx   ) BN_CTX_free( ctx );
00939       throw;
00940    }
00941 }
00942 
00943 int
00944 TranslationCertificate::marshal( unsigned char ** pp ) const
00945 {
00946    int r=0;
00947    int ret=0;
00948    unsigned char * p;
00949 
00950    ret += m_z1bar.marshal(0);
00951    ret += m_z2bar.marshal(0);
00952    ret += m_G.marshal(0);
00953    ret += m_H.marshal(0);
00954    ret += m_delta.marshal(0);
00955    ret += m_F.marshal(0);
00956    ret += m_M.marshal(0);
00957    ret += m_d1.marshal(0);
00958    ret += m_d2.marshal(0);
00959    M_ASN1_I2D_seq_total();
00960    m_z1bar.marshal(&p);
00961    m_z2bar.marshal(&p);
00962    m_G.marshal(&p);
00963    m_H.marshal(&p);
00964    m_delta.marshal(&p);
00965    m_F.marshal(&p);
00966    m_M.marshal(&p);
00967    m_d1.marshal(&p);
00968    m_d2.marshal(&p);
00969    M_ASN1_I2D_finish();
00970 }
00971 
00972 void*
00973 TranslationCertificate::unmarshal( void* bogus,
00974                                    unsigned char ** pp,
00975                                    long length )
00976 {
00977    if ( m_initialized )
00978    {
00979       return NULL;
00980    }
00981    if ( (NULL == pp) || (NULL == *pp) )
00982    {
00983       return NULL;
00984    }
00985 
00986    ASN1_CTX c;
00987    c.pp = pp;
00988    c.q = *pp;
00989    c.error = ERR_R_NESTED_ASN1_ERROR;
00990    int i;
00991 
00992    M_ASN1_D2I_Init();
00993    M_ASN1_D2I_start_sequence();
00994    M_ASN1_D2I_get(i, m_z1bar.unmarshal);
00995    M_ASN1_D2I_get(i, m_z2bar.unmarshal);
00996    M_ASN1_D2I_get(i, m_G.unmarshal);
00997    M_ASN1_D2I_get(i, m_H.unmarshal);
00998    M_ASN1_D2I_get(i, m_delta.unmarshal);
00999    M_ASN1_D2I_get(i, m_F.unmarshal);
01000    M_ASN1_D2I_get(i, m_M.unmarshal);
01001    M_ASN1_D2I_get(i, m_d1.unmarshal);
01002    M_ASN1_D2I_get(i, m_d2.unmarshal);
01003    if ( !asn1_Finish(&c) )
01004    {
01005       return NULL;
01006    }
01007    *pp=c.p;
01008    m_initialized = true;
01009    return this;
01010   err: // needed by ASN.1 macros
01011    return NULL;
01012 }

Generated on Fri May 6 17:41:27 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1