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

VarRSABlindPlainText.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: VarRSABlindPlainText.cc,v 1.3 2004/05/19 15:56:48 mmarsh Exp $
00008 //
00009 // $Log: VarRSABlindPlainText.cc,v $
00010 // Revision 1.3  2004/05/19 15:56:48  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 "VarRSA.h"
00019 #include "BIGNUM_xor.h"
00020 #include "CODEX_Exceptions/BignumExceptions.h"
00021 
00022 using namespace CODEX_Ciphers;
00023 
00024 void
00025 VarRSABlindPlainText::operator=( const VarRSABlindPlainText& aOther )
00026 {
00027    m_initialized = aOther.m_initialized;
00028    m_b1          = aOther.m_b1;
00029    m_b2          = aOther.m_b2;
00030 }
00031 
00032 BIGNUM *
00033 VarRSABlindPlainText::unblind( const BIGNUM * b,
00034                                const CODEX_ASN1::BigNumber& modulus,
00035                                const HashFunction& hashFunc ) const
00036 {
00037    if ( 0 == b )
00038    {
00039       throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00040    }
00041    BIGNUM * retVal = 0;
00042    const BIGNUM * n = modulus.value();
00043    BIGNUM * db = 0;
00044    BIGNUM * binv = 0;
00045    BIGNUM * temp = 0;
00046    BN_CTX * ctx = 0;
00047    unsigned char* buff = 0;
00048    CODEX_ASN1::ustring* tempStr = 0;
00049    try
00050    {
00051       retVal = BN_new();
00052       if ( 0 == retVal )
00053       {
00054          throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00055       }
00056 
00057       ctx = BN_CTX_new();
00058       if ( 0 == ctx )
00059       {
00060          throw CODEX_Exceptions::BignumContextException( __FILE__ , __LINE__ );
00061       }
00062 
00063       binv = BN_new();
00064       if ( 0 == binv )
00065       {
00066          throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00067       }
00068       if ( ! BN_mod_inverse( binv, b, n, ctx ) )
00069       {
00070          throw CODEX_Exceptions::BignumModInverseException( __FILE__ ,
00071                                                             __LINE__ );
00072       }
00073 
00074       db = BN_new();
00075       if ( 0 == db )
00076       {
00077          throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00078       }
00079       if ( ! BN_mod_mul( db, m_b1.value(), binv, n, ctx ) )
00080       {
00081          throw CODEX_Exceptions::BignumModMulException( __FILE__ , __LINE__ );
00082       }
00083       CODEX_ASN1::SecureBigNumber dbbn( db );
00084       db = 0;
00085       int length = dbbn.marshal(0);
00086       buff = new unsigned char[length];
00087       unsigned char* pBuff = buff;
00088       dbbn.marshal(&pBuff);
00089       tempStr = hashFunc( CODEX_ASN1::ustring(buff,length),
00090                           BN_num_bits(n) );
00091       temp = BN_new();
00092       if ( 0 == temp )
00093       {
00094          throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00095       }
00096       if ( 0 == BN_bin2bn( tempStr->data(), tempStr->length(), retVal ) )
00097       {
00098          throw CODEX_Exceptions::BignumBin2BNException( __FILE__ , __LINE__ );
00099       }
00100       if ( ! BN_mod( temp, retVal, n, ctx ) )
00101       {
00102          throw CODEX_Exceptions::BignumModException( __FILE__ , __LINE__ );
00103       }
00104       delete tempStr;
00105       tempStr = 0;
00106       delete [] buff;
00107       buff = 0;
00108 
00109       BIGNUM_xor( retVal, temp, m_b2.value() );
00110 
00111       BN_clear_free( temp );
00112       temp = 0;
00113       BN_CTX_free( ctx );
00114       ctx = 0;
00115       return retVal;
00116    }
00117    catch ( ... )
00118    {
00119       if ( 0 != temp ) BN_clear_free( temp );
00120       if ( 0 != retVal ) BN_clear_free( retVal );
00121       if ( 0 != ctx ) BN_CTX_free( ctx );
00122       if ( 0 != binv ) BN_clear_free( binv );
00123       if ( 0 != db ) BN_free( db );
00124       if ( 0 != tempStr ) delete tempStr;
00125       if ( 0 != buff ) delete [] buff;
00126       throw;
00127    }
00128 }
00129 
00130 int
00131 VarRSABlindPlainText::marshal( unsigned char ** pp ) const
00132 {
00133    int r=0;
00134    int ret=0;
00135    unsigned char * p;
00136 
00137    ret += m_b1.marshal(0);
00138    ret += m_b2.marshal(0);
00139    M_ASN1_I2D_seq_total();
00140    m_b1.marshal(&p);
00141    m_b2.marshal(&p);
00142    M_ASN1_I2D_finish();
00143 }
00144 
00145 void*
00146 VarRSABlindPlainText::unmarshal( void* bogus,
00147                                  unsigned char ** pp,
00148                                  long length )
00149 {
00150    if ( m_initialized )
00151    {
00152       return NULL;
00153    }
00154    if ( (NULL == pp) || (NULL == *pp) )
00155    {
00156       return NULL;
00157    }
00158    ASN1_CTX c;
00159    c.pp = pp;
00160    c.q = *pp;
00161    c.error = ERR_R_NESTED_ASN1_ERROR;
00162    int i;
00163 
00164    M_ASN1_D2I_Init();
00165    M_ASN1_D2I_start_sequence();
00166    M_ASN1_D2I_get(i, m_b1.unmarshal);
00167    M_ASN1_D2I_get(i, m_b2.unmarshal);
00168    if ( !asn1_Finish(&c) )
00169    {
00170       return NULL;
00171    }
00172    *pp=c.p;
00173    m_initialized = true;
00174    return this;
00175   err: // needed by ASN.1 macros
00176    return NULL;
00177 }

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