00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "ThresholdRSA.h"
00019
00020 using namespace CODEX_ThresholdCrypto;
00021
00022 ThresholdRSARange::ThresholdRSARange( const BIGNUM * modulus,
00023 unsigned int n ) :
00024 m_min( 0 ),
00025 m_max( 0 )
00026 {
00027 BN_CTX * ctx = 0;
00028 BIGNUM * bnz = 0;
00029 try
00030 {
00031 ctx = BN_CTX_new();
00032 if ( 0 == ctx )
00033 {
00034 throw CODEX_Exceptions::BignumContextException( __FILE__ , __LINE__ );
00035 }
00036 m_max = BN_new();
00037 if ( 0 == m_max )
00038 {
00039 throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00040 }
00041 CODEX_ASN1::Integer iNum( n );
00042 CODEX_ASN1::BigNumber bnNum( iNum.asn1() );
00043 if ( ! BN_sqr( m_max, modulus, ctx ) )
00044 {
00045 throw CODEX_Exceptions::BignumMulException( __FILE__ , __LINE__ );
00046 }
00047 if ( ! BN_mul( m_max, m_max, bnNum.value(), ctx ) )
00048 {
00049 throw CODEX_Exceptions::BignumMulException( __FILE__ , __LINE__ );
00050 }
00051
00052 bnz = BN_new();
00053 if ( 0 == bnz )
00054 {
00055 throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00056 }
00057 if ( ! BN_zero(bnz) )
00058 {
00059 throw CODEX_Exceptions::BignumSetWordException( __FILE__ , __LINE__ );
00060 }
00061 m_min = BN_new();
00062 if ( 0 == m_min )
00063 {
00064 throw CODEX_Exceptions::BignumNullException( __FILE__ , __LINE__ );
00065 }
00066 if ( ! BN_sub( m_min, bnz, m_max ) )
00067 {
00068 throw CODEX_Exceptions::BignumSubException( __FILE__ , __LINE__ );
00069 }
00070 BN_free(bnz);
00071 BN_CTX_free(ctx);
00072 }
00073 catch ( ... )
00074 {
00075 if ( 0 != ctx ) BN_CTX_free( ctx );
00076 if ( 0 != m_min ) BN_free( m_min );
00077 if ( 0 != m_max ) BN_free( m_max );
00078 if ( 0 != bnz ) BN_free( bnz );
00079 throw;
00080 }
00081 }
00082
00083 ThresholdRSARange::~ThresholdRSARange()
00084 {
00085 if ( 0 != m_min ) BN_free( m_min );
00086 if ( 0 != m_max ) BN_free( m_max );
00087 }
00088
00089 ThresholdRSASubRange::ThresholdRSASubRange( const BIGNUM * modulus ) :
00090 ThresholdRSARange( modulus, 1 )
00091 {
00092 }
00093
00094 ThresholdRSASubRange::~ThresholdRSASubRange()
00095 {
00096 }