00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_VSS_SHARELABEL_H__
00019 #define __CODEX_VSS_SHARELABEL_H__
00020
00021 #include <fstream>
00022
00023 #include "VRecon.h"
00024 #include "ShareSet.h"
00025 #include "CODEX_ASN1/Base.h"
00026 #include "CODEX_ASN1/Integer.h"
00027 #include "CODEX_Exceptions/FileExceptions.h"
00028
00029 namespace CODEX_VSS
00030 {
00043 template< class _ShareType , class _OneWay >
00044 class ShareLabel : public CODEX_ASN1::Base
00045 {
00046 public :
00048 typedef _ShareType ShareType;
00053 typedef _OneWay OneWay;
00059 typedef VRecon< ShareType , OneWay > VType;
00060
00062 const static unsigned int NumShares = ShareType::NumShares;
00063
00065 ShareLabel() :
00066 CODEX_ASN1::Base( false )
00067 {
00068 }
00069
00071 ShareLabel( int num,
00072 int version,
00073 int id,
00074 const ShareSet< ShareType >& shareSet,
00075 const OneWay& func ) :
00076 CODEX_ASN1::Base( true ),
00077 m_num( num ),
00078 m_version( version ),
00079 m_id( id )
00080 {
00081 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00082 {
00083 func( shareSet(i), m_vc[i] );
00084 }
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00096 ShareLabel( int num,
00097 int version,
00098 int id,
00099 const typename VType::ValueType vc[NumShares] ) :
00100 CODEX_ASN1::Base( true ),
00101 m_num( num ),
00102 m_version( version ),
00103 m_id( id )
00104 {
00105 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00106 {
00107 m_vc[i] = vc[i];
00108 }
00109 }
00110
00112 ShareLabel( const ShareLabel< _ShareType, _OneWay >& aOther ) :
00113 CODEX_ASN1::Base( aOther.m_initialized ),
00114 m_num( aOther.m_num ),
00115 m_version( aOther.m_version ),
00116 m_id( aOther.m_id )
00117 {
00118 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00119 {
00120 m_vc[i] = aOther.m_vc[i];
00121 }
00122 }
00123
00125 virtual ~ShareLabel()
00126 {
00127 }
00128
00130 void operator=( const ShareLabel< _ShareType, _OneWay >& aOther )
00131 {
00132 m_initialized = aOther.m_initialized;
00133 m_num = aOther.m_num;
00134 m_version = aOther.m_version;
00135 m_id = aOther.m_id;
00136 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00137 {
00138 m_vc[i] = aOther.m_vc[i];
00139 }
00140 }
00141
00156 int cmp( const ShareLabel< _ShareType, _OneWay >& aOther ) const
00157 {
00158 int n1 = m_num.value();
00159 int n2 = aOther.m_num.value();
00160 if ( n1 < n2 ) return -1;
00161 if ( n1 > n2 ) return 1;
00162
00163 int v1 = m_version.value();
00164 int v2 = aOther.m_version.value();
00165 if ( v1 < v2 ) return -1;
00166 if ( v1 > v2 ) return 1;
00167
00168 int i1 = m_id.value();
00169 int i2 = aOther.m_id.value();
00170 if ( i1 < i2 ) return -1;
00171 if ( i1 > i2 ) return 1;
00172
00173 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00174 {
00175 if ( m_vc[i] < aOther.m_vc[i] ) return -1;
00176 if ( m_vc[i] > aOther.m_vc[i] ) return 1;
00177 }
00178
00179
00180 return 0;
00181 }
00182
00184 bool operator<( const ShareLabel< _ShareType, _OneWay >& aOther )
00185 const
00186 {
00187 return ( cmp(aOther) < 0 );
00188 }
00189
00191 bool operator>( const ShareLabel< _ShareType, _OneWay >& aOther )
00192 const
00193 {
00194 return ( cmp(aOther) > 0 );
00195 }
00196
00198 bool operator==( const ShareLabel< _ShareType, _OneWay >& aOther )
00199 const
00200 {
00201 return ( cmp(aOther) == 0 );
00202 }
00203
00205 bool operator!=( const ShareLabel< _ShareType, _OneWay >& aOther )
00206 const
00207 {
00208 return ( cmp(aOther) != 0 );
00209 }
00210
00212 bool operator<=( const ShareLabel< _ShareType, _OneWay >& aOther )
00213 const
00214 {
00215 return ( cmp(aOther) <= 0 );
00216 }
00217
00219 bool operator>=( const ShareLabel< _ShareType, _OneWay >& aOther )
00220 const
00221 {
00222 return ( cmp(aOther) >= 0 );
00223 }
00224
00226 const CODEX_ASN1::Integer& num() const { return m_num; }
00227
00229 const CODEX_ASN1::Integer& version() const { return m_version; }
00230
00232 const CODEX_ASN1::Integer& id() const { return m_id; }
00233
00235 const typename VType::ValueType* vc() const { return m_vc; }
00236
00238 const typename VType::ValueType& vc( unsigned int i ) const
00239 {
00240 if ( i >= NumShares )
00241 {
00242 throw CODEX_Exceptions::IllegalIndexException( __FILE__ ,
00243 __LINE__ );
00244 }
00245 return m_vc[i];
00246 }
00247
00258 bool verify( const typename VType::ValueType& vcs,
00259 const OneWay& func ) const
00260 {
00261 VType vr( func );
00262 typename VType::ValueType res;
00263 vr( m_vc, res );
00264 if ( res != vcs ) return false;
00265 return true;
00266 }
00267
00274 bool check( const ShareType& share, const OneWay& func ) const
00275 {
00276 VType vr( func );
00277 return vr.checkShare( m_vc, share, func );
00278 }
00279
00280 int marshal( unsigned char ** pp ) const
00281 {
00282
00283 if ( ! m_num.initialized() ) return 0;
00284 if ( ! m_version.initialized() ) return 0;
00285 if ( ! m_id.initialized() ) return 0;
00286 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00287 {
00288 if ( ! m_vc[i].initialized() ) return 0;
00289 }
00290 int r=0;
00291 int ret=0;
00292 unsigned char * p;
00293
00294 ret += m_num.marshal(0);
00295 ret += m_version.marshal(0);
00296 ret += m_id.marshal(0);
00297 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00298 {
00299 ret += m_vc[i].marshal(0);
00300 }
00301 M_ASN1_I2D_seq_total();
00302 m_num.marshal(&p);
00303 m_version.marshal(&p);
00304 m_id.marshal(&p);
00305 for ( unsigned int i = 0 ; i < NumShares ; ++i )
00306 {
00307 m_vc[i].marshal(&p);
00308 }
00309 M_ASN1_I2D_finish();
00310 }
00311
00312 void* unmarshal( void* bogus, unsigned char** pp, long length )
00313 {
00314 if ( m_initialized )
00315 {
00316 return 0;
00317 }
00318 if ( (0 == pp) || (0 == *pp) )
00319 {
00320 return 0;
00321 }
00322
00323 ASN1_CTX c;
00324 c.pp = pp;
00325 c.q = *pp;
00326 c.error = ERR_R_NESTED_ASN1_ERROR;
00327 int i;
00328
00329 M_ASN1_D2I_Init();
00330 M_ASN1_D2I_start_sequence();
00331 M_ASN1_D2I_get(i, m_num.unmarshal);
00332 M_ASN1_D2I_get(i, m_version.unmarshal);
00333 M_ASN1_D2I_get(i, m_id.unmarshal);
00334 for ( unsigned int count = 0 ; count < NumShares ; ++count )
00335 {
00336 M_ASN1_D2I_get(i, m_vc[count].unmarshal);
00337 }
00338 if ( !asn1_Finish(&c) )
00339 {
00340 return 0;
00341 }
00342 *pp=c.p;
00343 m_initialized = true;
00344 return this;
00345 err:
00346 return 0;
00347 }
00348
00349 private :
00350 CODEX_ASN1::Integer m_num;
00351 CODEX_ASN1::Integer m_version;
00352 CODEX_ASN1::Integer m_id;
00353 typename VType::ValueType m_vc[ NumShares ];
00354 };
00355
00357 template< class _ShareType , class _OneWay >
00358 class LabeledShare : public CODEX_ASN1::Base
00359 {
00360 public :
00362 typedef _ShareType ShareType;
00363
00365 typedef _OneWay OneWay;
00366
00368 typedef ShareLabel< _ShareType , _OneWay > LabelType;
00369
00371 typedef CODEX_ASN1::Base BaseType;
00372
00374 LabeledShare() : BaseType( false ) {}
00375
00377 LabeledShare( const ShareType& share, const LabelType& label ) :
00378 BaseType( true ),
00379 m_share( share ),
00380 m_label( label )
00381 {}
00382
00384 LabeledShare( const LabeledShare< ShareType , OneWay >& aOther ) :
00385 BaseType( aOther.m_initialized ),
00386 m_share( aOther.m_share ),
00387 m_label( aOther.m_label )
00388 {}
00389
00391 ~LabeledShare() {}
00392
00394 void operator=( const LabeledShare< ShareType , OneWay >& aOther )
00395 {
00396 m_initialized = aOther.m_initialized;
00397 m_share = aOther.m_share;
00398 m_label = aOther.m_label;
00399 }
00400
00402 const ShareType& share() const { return m_share; }
00403
00405 const LabelType& label() const { return m_label; }
00406
00407 int marshal( unsigned char ** pp ) const
00408 {
00409 if ( ! m_share.initialized() ) return 0;
00410 if ( ! m_label.initialized() ) return 0;
00411 int r=0;
00412 int ret=0;
00413 unsigned char * p;
00414
00415 ret += m_share.marshal(0);
00416 ret += m_label.marshal(0);
00417 M_ASN1_I2D_seq_total();
00418 m_share.marshal(&p);
00419 m_label.marshal(&p);
00420 M_ASN1_I2D_finish();
00421 }
00422
00423 void* unmarshal( void* bogus, unsigned char** pp, long length )
00424 {
00425 if ( m_initialized )
00426 {
00427 return 0;
00428 }
00429 if ( (0 == pp) || (0 == *pp) )
00430 {
00431 return 0;
00432 }
00433
00434 ASN1_CTX c;
00435 c.pp = pp;
00436 c.q = *pp;
00437 c.error = ERR_R_NESTED_ASN1_ERROR;
00438 int i;
00439
00440 M_ASN1_D2I_Init();
00441 M_ASN1_D2I_start_sequence();
00442 M_ASN1_D2I_get(i, m_share.unmarshal);
00443 M_ASN1_D2I_get(i, m_label.unmarshal);
00444 if ( !asn1_Finish(&c) )
00445 {
00446 return 0;
00447 }
00448 *pp=c.p;
00449 m_initialized = true;
00450 return this;
00451 err:
00452 return 0;
00453 }
00454
00456 void toFile( const char* fname ) const
00457 {
00458 int length = marshal(0);
00459 if ( 0 == length ) return;
00460 unsigned char* buff = new unsigned char[length];
00461 unsigned char* p = buff;
00462 marshal(&p);
00463 ofstream os(fname);
00464 if ( ! os.is_open() )
00465 {
00466 delete [] buff;
00467 throw CODEX_Exceptions::FileCannotCreateException( __FILE__ ,
00468 __LINE__ ,
00469 fname );
00470 }
00471 for ( int i = 0 ; i < length ; ++i )
00472 {
00473 os << buff[i];
00474 }
00475 os.close();
00476 delete [] buff;
00477 }
00478
00480 void* fromFile( const char* fname )
00481 {
00482 ifstream is(fname);
00483 if ( ! is.is_open() )
00484 {
00485 throw CODEX_Exceptions::FileCannotOpenException( __FILE__ ,
00486 __LINE__ ,
00487 fname );
00488 }
00489 string s;
00490 char ch;
00491 while ( is.get(ch) )
00492 {
00493 s.push_back(ch);
00494 }
00495
00496
00497 unsigned int length = s.length();
00498 unsigned char* p = new unsigned char[length];
00499 unsigned char* pOrig = p;
00500
00501 for ( unsigned int i = 0 ; i < length ; ++i )
00502 {
00503 p[i] = s.data()[i];
00504 }
00505 void* retVal = unmarshal(0,&p,length);
00506 delete [] pOrig;
00507 return retVal;
00508 }
00509
00510 private :
00511 ShareType m_share;
00512 LabelType m_label;
00513 };
00514
00515
00521 template< class _ShareType , class _OneWay >
00522 class SecretWitness : public CODEX_ASN1::Base
00523 {
00524 public :
00526 typedef CODEX_ASN1::Base BaseType;
00527
00529 typedef _ShareType ShareType;
00530
00532 typedef _OneWay OneWay;
00533
00535 typedef typename VRecon< ShareType , OneWay >::ValueType WitnessType;
00536
00538 typedef typename ShareType::ValueType ValueType;
00539
00541 typedef typename OneWay::CtorArgs ArgsType;
00542
00544 SecretWitness() : BaseType( false ) {}
00545
00547 SecretWitness( const WitnessType& witness , const ArgsType& args ) :
00548 BaseType( true ),
00549 m_witness( witness ),
00550 m_args( args )
00551 {
00552 }
00553
00555 SecretWitness( const SecretWitness< ShareType , OneWay >& aOther ) :
00556 BaseType( aOther.m_initialized ),
00557 m_witness( aOther.m_witness ),
00558 m_args( aOther.m_args )
00559 {
00560 }
00561
00563 ~SecretWitness() {}
00564
00566 void operator=( const SecretWitness< ShareType , OneWay >& aOther )
00567 {
00568 m_initialized = aOther.m_initialized;
00569 m_witness = aOther.m_witness;
00570 m_args = aOther.m_args;
00571 }
00572
00574 const WitnessType& witness() const { return m_witness; }
00575
00577 const ArgsType& args() const { return m_args; }
00578
00579 int marshal( unsigned char ** pp ) const
00580 {
00581 if ( ! m_witness.initialized() ) return 0;
00582 if ( ! m_args.initialized() ) return 0;
00583 int r=0;
00584 int ret=0;
00585 unsigned char * p;
00586
00587 ret += m_witness.marshal(0);
00588 ret += m_args.marshal(0);
00589 M_ASN1_I2D_seq_total();
00590 m_witness.marshal(&p);
00591 m_args.marshal(&p);
00592 M_ASN1_I2D_finish();
00593 }
00594
00595 void* unmarshal( void* bogus, unsigned char** pp, long length )
00596 {
00597 if ( m_initialized )
00598 {
00599 return 0;
00600 }
00601 if ( (0 == pp) || (0 == *pp) )
00602 {
00603 return 0;
00604 }
00605
00606 ASN1_CTX c;
00607 c.pp = pp;
00608 c.q = *pp;
00609 c.error = ERR_R_NESTED_ASN1_ERROR;
00610 int i;
00611
00612 M_ASN1_D2I_Init();
00613 M_ASN1_D2I_start_sequence();
00614 M_ASN1_D2I_get(i, m_witness.unmarshal);
00615 M_ASN1_D2I_get(i, m_args.unmarshal);
00616 if ( !asn1_Finish(&c) )
00617 {
00618 return 0;
00619 }
00620 *pp=c.p;
00621 m_initialized = true;
00622 return this;
00623 err:
00624 return 0;
00625 }
00626
00628 void toFile( const char* fname ) const
00629 {
00630 int length = marshal(0);
00631 if ( 0 == length ) return;
00632 unsigned char* buff = new unsigned char[length];
00633 unsigned char* p = buff;
00634 marshal(&p);
00635 ofstream os(fname);
00636 if ( ! os.is_open() )
00637 {
00638 delete [] buff;
00639 throw CODEX_Exceptions::FileCannotCreateException( __FILE__ ,
00640 __LINE__ ,
00641 fname );
00642 }
00643 for ( int i = 0 ; i < length ; ++i )
00644 {
00645 os << buff[i];
00646 }
00647 os.close();
00648 delete [] buff;
00649 }
00650
00652 void* fromFile( const char* fname )
00653 {
00654 ifstream is(fname);
00655 if ( ! is.is_open() )
00656 {
00657 throw CODEX_Exceptions::FileCannotOpenException( __FILE__ ,
00658 __LINE__ ,
00659 fname );
00660 }
00661 string s;
00662 char ch;
00663 while ( is.get(ch) )
00664 {
00665 s.push_back(ch);
00666 }
00667
00668
00669 unsigned int length = s.length();
00670
00671 unsigned char* p = new unsigned char[length];
00672 unsigned char* pOrig = p;
00673 for ( unsigned int i = 0 ; i < length ; ++i )
00674 {
00675 p[i] = s.data()[i];
00676 }
00677 void* retVal = unmarshal(0,&p,length);
00678 delete [] pOrig;
00679 return retVal;
00680 }
00681
00682 private :
00683 WitnessType m_witness;
00684 ArgsType m_args;
00685 };
00686
00687 }
00688
00689 #endif