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