00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "VarRSA.h"
00019 #include "CODEX_Exceptions/BignumExceptions.h"
00020
00021 using namespace CODEX_Ciphers;
00022
00023 void
00024 VarRSACipherText::operator=( const VarRSACipherText& aOther )
00025 {
00026 m_initialized = aOther.m_initialized;
00027 m_c1 = aOther.m_c1;
00028 m_c2 = aOther.m_c2;
00029 }
00030
00031 bool
00032 VarRSACipherText::operator==( const VarRSACipherText& aOther ) const
00033 {
00034 if ( m_c1 != aOther.m_c1 ) return false;
00035 if ( m_c2 != aOther.m_c2 ) return false;
00036 return true;
00037 }
00038
00039 bool
00040 VarRSACipherText::operator!=( const VarRSACipherText& aOther ) const
00041 {
00042 return ! ( *this == aOther );
00043 }
00044
00045 VarRSABlindCipherText*
00046 VarRSACipherText::blind( const RSACipherText& aOther,
00047 const CODEX_ASN1::BigNumber& modulus ) const
00048 {
00049 VarRSABlindCipherText* retVal = 0;
00050
00051 const BIGNUM * kbn = m_c1.value();
00052 const BIGNUM * b = aOther.value();
00053 const BIGNUM * n = modulus.value();
00054 BIGNUM * bk = 0;
00055 BN_CTX * ctx = 0;
00056 try
00057 {
00058 ctx = BN_CTX_new();
00059 if ( 0 == ctx )
00060 {
00061 throw CODEX_Exceptions::BignumContextException( __FILE__ , __LINE__ );
00062 }
00063 bk = BN_new();
00064 if ( 0 == bk )
00065 {
00066 throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00067 }
00068 if ( ! BN_mod_mul( bk, b, kbn, n, ctx ) )
00069 {
00070 throw CODEX_Exceptions::BignumModMulException( __FILE__ , __LINE__ );
00071 }
00072 retVal = new VarRSABlindCipherText( bk, m_c2 );
00073 bk = 0;
00074 BN_CTX_free( ctx );
00075 ctx = 0;
00076 return retVal;
00077 }
00078 catch ( ... )
00079 {
00080 if ( 0 != ctx ) BN_CTX_free( ctx );
00081 if ( 0 != bk ) BN_free( bk );
00082 throw;
00083 }
00084 }
00085
00086 int
00087 VarRSACipherText::marshal( unsigned char ** pp ) const
00088 {
00089 int r=0;
00090 int ret=0;
00091 unsigned char * p;
00092
00093 ret += m_c1.marshal(0);
00094 ret += m_c2.marshal(0);
00095 M_ASN1_I2D_seq_total();
00096 m_c1.marshal(&p);
00097 m_c2.marshal(&p);
00098 M_ASN1_I2D_finish();
00099 }
00100
00101 void*
00102 VarRSACipherText::unmarshal( void* bogus, unsigned char ** pp, long length )
00103 {
00104 if ( m_initialized )
00105 {
00106 return NULL;
00107 }
00108 if ( (NULL == pp) || (NULL == *pp) )
00109 {
00110 return NULL;
00111 }
00112 ASN1_CTX c;
00113 c.pp = pp;
00114 c.q = *pp;
00115 c.error = ERR_R_NESTED_ASN1_ERROR;
00116 int i;
00117
00118 M_ASN1_D2I_Init();
00119 M_ASN1_D2I_start_sequence();
00120 M_ASN1_D2I_get(i, m_c1.unmarshal);
00121 M_ASN1_D2I_get(i, m_c2.unmarshal);
00122 if ( !asn1_Finish(&c) )
00123 {
00124 return NULL;
00125 }
00126 *pp=c.p;
00127 m_initialized = true;
00128 return this;
00129 err:
00130 return NULL;
00131 }