00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_CIPHERS_RSA_H__
00019 #define __CODEX_CIPHERS_RSA_H__
00020
00021 #include <openssl/bn.h>
00022 #include "CODEX_ASN1/Base.h"
00023 #include "CODEX_ASN1/BigNumber.h"
00024 #include "CODEX_ASN1/SecureBigNumber.h"
00025 #include "CODEX_ASN1/Certificate.h"
00026
00027 namespace CODEX_Ciphers
00028 {
00033 class RSACipherText : public CODEX_ASN1::BigNumber
00034 {
00035 public:
00037 RSACipherText() :
00038 CODEX_ASN1::BigNumber()
00039 {}
00041 RSACipherText( BIGNUM * c ) :
00042 CODEX_ASN1::BigNumber( c )
00043 {}
00045 RSACipherText( const CODEX_ASN1::BigNumber& c ) :
00046 CODEX_ASN1::BigNumber( c )
00047 {}
00049 virtual ~RSACipherText() {}
00050
00058 RSACipherText* blind( const RSACipherText& aOther,
00059 const CODEX_ASN1::BigNumber& modulus ) const;
00060 };
00061
00066 class RSASignature : public CODEX_ASN1::BigNumber
00067 {
00068 public:
00070 RSASignature() :
00071 CODEX_ASN1::BigNumber()
00072 {}
00074 RSASignature( BIGNUM * s ) :
00075 CODEX_ASN1::BigNumber( s )
00076 {}
00078 RSASignature( const CODEX_ASN1::BigNumber& s ) :
00079 CODEX_ASN1::BigNumber( s )
00080 {}
00082 virtual ~RSASignature() {}
00083 };
00084
00088 class RSAPublicKey : public CODEX_ASN1::Base
00089 {
00090 public:
00092 RSAPublicKey();
00094 RSAPublicKey( BIGNUM * n, BIGNUM * e );
00096 RSAPublicKey( const CODEX_ASN1::BigNumber& n,
00097 const CODEX_ASN1::BigNumber& e );
00099 RSAPublicKey( const X509 * cert );
00101 RSAPublicKey( const RSAPublicKey& aKey );
00103 virtual ~RSAPublicKey() {}
00104
00106 void operator=( const RSAPublicKey& aKey );
00107
00109 virtual const CODEX_ASN1::BigNumber& n() const { return m_n; }
00111 virtual const CODEX_ASN1::BigNumber& e() const { return m_e; }
00112
00127 RSACipherText* encrypt( const BIGNUM * message ) const;
00128
00145 bool verifySignature( const RSASignature& signature,
00146 const BIGNUM * message ) const;
00147
00149 int marshal( unsigned char ** pp ) const;
00151 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00152
00159 void toFile(const char* fname) const;
00160
00167 void* fromFile(const char* fname);
00168
00169 protected:
00170 BIGNUM * exponentiate( const BIGNUM * aBN ) const;
00171
00172 private :
00173 CODEX_ASN1::BigNumber m_n;
00174 CODEX_ASN1::BigNumber m_e;
00175 };
00176
00187 class RSAPrivateKey : public CODEX_ASN1::Base
00188 {
00189 public:
00191 RSAPrivateKey();
00197 RSAPrivateKey( BIGNUM * p,
00198 BIGNUM * q,
00199 BIGNUM * d,
00200 BIGNUM * n=0,
00201 BIGNUM * phi=0 );
00207 RSAPrivateKey( const CODEX_ASN1::BigNumber& p,
00208 const CODEX_ASN1::BigNumber& q,
00209 const CODEX_ASN1::BigNumber& d );
00211 RSAPrivateKey( const RSAPrivateKey& aKey );
00213 virtual ~RSAPrivateKey();
00214
00216 void operator=( const RSAPrivateKey& aKey );
00217
00219 virtual const CODEX_ASN1::SecureBigNumber& p() const { return m_p; }
00221 virtual const CODEX_ASN1::SecureBigNumber& q() const { return m_q; }
00223 virtual const CODEX_ASN1::SecureBigNumber& d() const { return m_d; }
00225 virtual const CODEX_ASN1::SecureBigNumber& n() const { return m_n; }
00227 virtual const CODEX_ASN1::SecureBigNumber& phi() const { return m_phi; }
00228
00241 BIGNUM * decrypt( const RSACipherText& cipherText ) const;
00256 RSASignature* sign( const BIGNUM * message ) const;
00257
00259 int marshal( unsigned char ** pp ) const;
00261 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00262
00269 void toFile(const char* fname) const;
00270
00277 void* fromFile(const char* fname);
00278
00291 void fromPEMFile(const char* fname, const char* phrase=0);
00292
00293 protected:
00294 BIGNUM * exponentiate( const BIGNUM * aBN ) const;
00295
00296 private:
00297 CODEX_ASN1::SecureBigNumber m_p;
00298 CODEX_ASN1::SecureBigNumber m_q;
00299 CODEX_ASN1::SecureBigNumber m_d;
00300 CODEX_ASN1::SecureBigNumber m_n;
00301 CODEX_ASN1::SecureBigNumber m_phi;
00302 };
00303
00307 class RSAKeyPairGenerator
00308 {
00309 public:
00311 RSAKeyPairGenerator( long numBits ) : m_numBits( numBits ) {}
00312
00331 void operator()( RSAPublicKey*& pubKey,
00332 RSAPrivateKey*& privKey );
00333
00334 private:
00335 long m_numBits;
00336 };
00337
00338 }
00339
00340 #endif