00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_CIPHERS_VARRSA_H__
00022 #define __CODEX_CIPHERS_VARRSA_H__
00023
00024 #include <openssl/bn.h>
00025 #include "RSA.h"
00026 #include "HashFunction.h"
00027
00028
00029
00030
00031
00032 namespace CODEX_Ciphers
00033 {
00034
00035 class VarRSABlindCipherText;
00036
00040 class VarRSACipherText : public CODEX_ASN1::Base
00041 {
00042 public:
00044 VarRSACipherText() :
00045 CODEX_ASN1::Base( false )
00046 {}
00048 VarRSACipherText( BIGNUM * c1, BIGNUM * c2 ) :
00049 CODEX_ASN1::Base( true ),
00050 m_c1( c1 ),
00051 m_c2( c2 )
00052 {}
00054 VarRSACipherText( const CODEX_ASN1::BigNumber& c1,
00055 const CODEX_ASN1::BigNumber& c2 ) :
00056 CODEX_ASN1::Base( true ),
00057 m_c1( c1 ),
00058 m_c2( c2 )
00059 {}
00061 VarRSACipherText( const VarRSACipherText& aOther ) :
00062 CODEX_ASN1::Base( aOther.m_initialized ),
00063 m_c1( aOther.m_c1 ),
00064 m_c2( aOther.m_c2 )
00065 {
00066 }
00068 virtual ~VarRSACipherText() {}
00069
00071 void operator=( const VarRSACipherText& aOther );
00072
00074 bool operator==( const VarRSACipherText& aBN ) const;
00075
00077 bool operator!=( const VarRSACipherText& aBN ) const;
00078
00083 const CODEX_ASN1::BigNumber& c1() const { return m_c1; }
00088 const CODEX_ASN1::BigNumber& c2() const { return m_c2; }
00089
00097 VarRSABlindCipherText* blind(
00098 const RSACipherText& aOther,
00099 const CODEX_ASN1::BigNumber& modulus ) const;
00100
00102 int marshal( unsigned char ** pp ) const;
00104 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00105
00106 private :
00107 CODEX_ASN1::BigNumber m_c1;
00108 CODEX_ASN1::BigNumber m_c2;
00109 };
00110
00117 class VarRSABlindCipherText : public CODEX_ASN1::Base
00118 {
00119 public:
00121 VarRSABlindCipherText() :
00122 CODEX_ASN1::Base( false )
00123 {}
00125 VarRSABlindCipherText( BIGNUM * c1, BIGNUM * c2 ) :
00126 CODEX_ASN1::Base( true ),
00127 m_c1( c1 ),
00128 m_c2( c2 )
00129 {}
00131 VarRSABlindCipherText( const CODEX_ASN1::BigNumber& c1,
00132 const CODEX_ASN1::BigNumber& c2 ) :
00133 CODEX_ASN1::Base( true ),
00134 m_c1( c1 ),
00135 m_c2( c2 )
00136 {}
00138 VarRSABlindCipherText( const VarRSABlindCipherText& aOther ) :
00139 CODEX_ASN1::Base( aOther.m_initialized ),
00140 m_c1( aOther.m_c1 ),
00141 m_c2( aOther.m_c2 )
00142 {
00143 }
00145 virtual ~VarRSABlindCipherText() {}
00146
00148 void operator=( const VarRSABlindCipherText& aOther );
00149
00151 bool operator<( const VarRSABlindCipherText& aOther ) const;
00152
00157 const CODEX_ASN1::BigNumber& c1() const { return m_c1; }
00162 const CODEX_ASN1::BigNumber& c2() const { return m_c2; }
00163
00165 int marshal( unsigned char ** pp ) const;
00167 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00168
00169 private :
00170 CODEX_ASN1::BigNumber m_c1;
00171 CODEX_ASN1::BigNumber m_c2;
00172 };
00173
00181 class VarRSABlindPlainText : public CODEX_ASN1::Base
00182 {
00183 public:
00185 VarRSABlindPlainText() :
00186 CODEX_ASN1::Base( false )
00187 {}
00189 VarRSABlindPlainText( BIGNUM * b1, BIGNUM * b2 ) :
00190 CODEX_ASN1::Base( true ),
00191 m_b1( b1 ),
00192 m_b2( b2 )
00193 {}
00195 VarRSABlindPlainText( const CODEX_ASN1::BigNumber& b1,
00196 const CODEX_ASN1::BigNumber& b2 ) :
00197 CODEX_ASN1::Base( true ),
00198 m_b1( b1 ),
00199 m_b2( b2 )
00200 {}
00202 VarRSABlindPlainText( const VarRSABlindPlainText& aOther ) :
00203 CODEX_ASN1::Base( aOther.m_initialized ),
00204 m_b1( aOther.m_b1 ),
00205 m_b2( aOther.m_b2 )
00206 {
00207 }
00209 virtual ~VarRSABlindPlainText() {}
00210
00212 void operator=( const VarRSABlindPlainText& aOther );
00213
00218 const CODEX_ASN1::BigNumber& b1() const { return m_b1; }
00223 const CODEX_ASN1::BigNumber& b2() const { return m_b2; }
00224
00233 BIGNUM * unblind( const BIGNUM * b,
00234 const CODEX_ASN1::BigNumber& modulus,
00235 const HashFunction& hashFunc ) const;
00236
00238 int marshal( unsigned char ** pp ) const;
00240 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00241
00242 private :
00243 CODEX_ASN1::BigNumber m_b1;
00244 CODEX_ASN1::BigNumber m_b2;
00245 };
00246
00247 typedef RSASignature VarRSASignature;
00248
00252 class VarRSAPublicKey : public RSAPublicKey
00253 {
00254 public:
00256 VarRSAPublicKey( const RSAPublicKey& aKey );
00257
00259 virtual ~VarRSAPublicKey() {}
00260
00262 const CODEX_ASN1::BigNumber& n() const { return m_key.n(); }
00264 const CODEX_ASN1::BigNumber& e() const { return m_key.e(); }
00265
00283 VarRSACipherText* encrypt( const BIGNUM * message,
00284 const HashFunction& hashFunc,
00285 BIGNUM * r=0 ) const;
00286
00303 bool verifySignature( const VarRSASignature& signature,
00304 const BIGNUM * message ) const
00305 {
00306 return m_key.verifySignature( signature, message );
00307 }
00308
00309 private:
00310 const RSAPublicKey& m_key;
00311 };
00312
00316 class VarRSAPrivateKey : public RSAPrivateKey
00317 {
00318 public:
00320 VarRSAPrivateKey( const RSAPrivateKey& aKey );
00321
00323 virtual ~VarRSAPrivateKey();
00324
00326 const CODEX_ASN1::SecureBigNumber& p() const { return m_key.p(); }
00328 const CODEX_ASN1::SecureBigNumber& q() const { return m_key.q(); }
00330 const CODEX_ASN1::SecureBigNumber& d() const { return m_key.d(); }
00332 const CODEX_ASN1::SecureBigNumber& n() const { return m_key.n(); }
00334 const CODEX_ASN1::SecureBigNumber& phi() const { return m_key.phi(); }
00335
00349 BIGNUM * decrypt( const VarRSACipherText& cipherText,
00350 const HashFunction& hashFunc ) const;
00364 VarRSABlindPlainText* decryptBlind(
00365 const VarRSABlindCipherText& cipherText ) const;
00380 VarRSASignature* sign( const BIGNUM * message ) const
00381 {
00382 return m_key.sign( message );
00383 }
00384
00385 private:
00386 const RSAPrivateKey& m_key;
00387 };
00388
00389 }
00390
00391 #endif