00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_THRESHOLDCRYPTO_COMBINATORICTHRESHOLDRSA_H__
00019 #define __CODEX_THRESHOLDCRYPTO_COMBINATORICTHRESHOLDRSA_H__
00020
00021 #include <fstream>
00022
00023 #include "ThresholdRSA.h"
00024 #include "CODEX_VSS/Combinatoric.h"
00025
00026 #include "timing.h"
00027
00028 namespace CODEX_ThresholdCrypto
00029 {
00031 template< unsigned int NumT , unsigned int ThreshT >
00032 class ThresholdRSACrypto< CODEX_VSS::Combinatoric< NumT , ThreshT > >
00033 {
00034 public :
00036 typedef CODEX_VSS::Combinatoric< NumT , ThreshT > ShareType;
00037
00039 typedef CODEX_VSS::ShareSet< ShareType > SetType;
00040
00042 ThresholdRSACrypto( const CODEX_ASN1::BigNumber& modulus ) :
00043 m_modulus( modulus )
00044 {
00045 }
00046
00050 void sign( const ShareType& shares,
00051 const BIGNUM * message,
00052 ShareType& partials ) const
00053 {
00054 #ifdef TIMING
00055 PartialSigTimer.start();
00056 #endif
00057 CODEX_VSS::ModExpFunctional f( BN_dup(message), m_modulus );
00058 shares.apply( f, partials );
00059 #ifdef TIMING
00060 PartialSigTimer.stop();
00061 #endif
00062 }
00063
00067 void decrypt( const ShareType& shares,
00068 const CODEX_Ciphers::RSACipherText& ciphertext,
00069 ShareType& partials ) const
00070 {
00071 #ifdef TIMING
00072
00073 PartialSigTimer.start();
00074 #endif
00075 CODEX_VSS::ModExpFunctional f( ciphertext, m_modulus );
00076 shares.apply( f, partials );
00077 #ifdef TIMING
00078 PartialSigTimer.stop();
00079 #endif
00080 }
00081
00092 BIGNUM* threshold( const SetType& partials ) const
00093 {
00094 #ifdef TIMING
00095 ThresholdSigTimer.start();
00096 #endif
00097 for ( unsigned int i = 0 ; i < ShareType::NumShares ; ++i )
00098 {
00099 if ( ! partials(i).initialized() )
00100 {
00101 #ifdef TIMING
00102 ThresholdSigTimer.stop();
00103 #endif
00104 return 0;
00105 }
00106 }
00107 const BIGNUM * n = m_modulus.value();
00108 BIGNUM * s = 0;
00109 BN_CTX * ctx = 0;
00110 try
00111 {
00112 ctx = BN_CTX_new();
00113 if ( 0 == ctx )
00114 {
00115 throw CODEX_Exceptions::BignumContextException( __FILE__ ,
00116 __LINE__ );
00117 }
00118 s = BN_new();
00119 if ( 0 == s )
00120 {
00121 throw CODEX_Exceptions::BignumNullException( __FILE__ ,
00122 __LINE__ );
00123 }
00124 BN_one(s);
00125 for ( unsigned int i = 0 ; i < ShareType::NumShares ; ++i )
00126 {
00127 const BIGNUM * share = partials(i).value();
00128 if ( ! BN_mod_mul( s, s, share, n, ctx ) )
00129 {
00130 throw CODEX_Exceptions::BignumModMulException( __FILE__ ,
00131 __LINE__ );
00132 }
00133 }
00134 }
00135 catch ( ... )
00136 {
00137 if ( 0 != s ) BN_free(s);
00138 if ( 0 != ctx ) BN_CTX_free(ctx);
00139 #ifdef TIMING
00140 ThresholdSigTimer.stop();
00141 #endif
00142 throw;
00143 }
00144 BN_CTX_free(ctx);
00145 #ifdef TIMING
00146 ThresholdSigTimer.stop();
00147 #endif
00148 return s;
00149 }
00150
00151 private :
00152 CODEX_ASN1::BigNumber m_modulus;
00153 };
00154
00155 }
00156
00157 #endif