00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __CODEX_CIPHERS_RSA_H__
00022 #define __CODEX_CIPHERS_RSA_H__
00023
00024 #include <openssl/bn.h>
00025 #include "CODEX_ASN1/Base.h"
00026 #include "CODEX_ASN1/BigNumber.h"
00027 #include "CODEX_ASN1/SecureBigNumber.h"
00028 #include "CODEX_ASN1/Certificate.h"
00029
00030 namespace CODEX_Ciphers
00031 {
00036 class RSACipherText : public CODEX_ASN1::BigNumber
00037 {
00038 public:
00040 RSACipherText() :
00041 CODEX_ASN1::BigNumber()
00042 {}
00044 RSACipherText( BIGNUM * c ) :
00045 CODEX_ASN1::BigNumber( c )
00046 {}
00048 RSACipherText( const CODEX_ASN1::BigNumber& c ) :
00049 CODEX_ASN1::BigNumber( c )
00050 {}
00052 virtual ~RSACipherText() {}
00053
00061 RSACipherText* blind( const RSACipherText& aOther,
00062 const CODEX_ASN1::BigNumber& modulus ) const;
00063 };
00064
00069 class RSASignature : public CODEX_ASN1::BigNumber
00070 {
00071 public:
00073 RSASignature() :
00074 CODEX_ASN1::BigNumber()
00075 {}
00077 RSASignature( BIGNUM * s ) :
00078 CODEX_ASN1::BigNumber( s )
00079 {}
00081 RSASignature( const CODEX_ASN1::BigNumber& s ) :
00082 CODEX_ASN1::BigNumber( s )
00083 {}
00085 virtual ~RSASignature() {}
00086 };
00087
00091 class RSAPublicKey : public CODEX_ASN1::Base
00092 {
00093 public:
00095 RSAPublicKey();
00097 RSAPublicKey( BIGNUM * n, BIGNUM * e );
00099 RSAPublicKey( const CODEX_ASN1::BigNumber& n,
00100 const CODEX_ASN1::BigNumber& e );
00102 RSAPublicKey( const X509 * cert );
00104 RSAPublicKey( const RSAPublicKey& aKey );
00106 virtual ~RSAPublicKey() {}
00107
00109 void operator=( const RSAPublicKey& aKey );
00110
00112 virtual const CODEX_ASN1::BigNumber& n() const { return m_n; }
00114 virtual const CODEX_ASN1::BigNumber& e() const { return m_e; }
00115
00130 RSACipherText* encrypt( const BIGNUM * message ) const;
00131
00148 bool verifySignature( const RSASignature& signature,
00149 const BIGNUM * message ) const;
00150
00152 int marshal( unsigned char ** pp ) const;
00154 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00155
00162 void toFile(const char* fname) const;
00163
00170 void* fromFile(const char* fname);
00171
00172 protected:
00173 BIGNUM * exponentiate( const BIGNUM * aBN ) const;
00174
00175 private :
00176 CODEX_ASN1::BigNumber m_n;
00177 CODEX_ASN1::BigNumber m_e;
00178 };
00179
00190 class RSAPrivateKey : public CODEX_ASN1::Base
00191 {
00192 public:
00194 RSAPrivateKey();
00200 RSAPrivateKey( BIGNUM * p,
00201 BIGNUM * q,
00202 BIGNUM * d,
00203 BIGNUM * n=0,
00204 BIGNUM * phi=0 );
00210 RSAPrivateKey( const CODEX_ASN1::BigNumber& p,
00211 const CODEX_ASN1::BigNumber& q,
00212 const CODEX_ASN1::BigNumber& d );
00214 RSAPrivateKey( const RSAPrivateKey& aKey );
00216 virtual ~RSAPrivateKey();
00217
00219 void operator=( const RSAPrivateKey& aKey );
00220
00222 virtual const CODEX_ASN1::SecureBigNumber& p() const { return m_p; }
00224 virtual const CODEX_ASN1::SecureBigNumber& q() const { return m_q; }
00226 virtual const CODEX_ASN1::SecureBigNumber& d() const { return m_d; }
00228 virtual const CODEX_ASN1::SecureBigNumber& n() const { return m_n; }
00230 virtual const CODEX_ASN1::SecureBigNumber& phi() const { return m_phi; }
00231
00244 BIGNUM * decrypt( const RSACipherText& cipherText ) const;
00259 RSASignature* sign( const BIGNUM * message ) const;
00260
00262 int marshal( unsigned char ** pp ) const;
00264 void* unmarshal( void* bogus, unsigned char ** pp, long length );
00265
00272 void toFile(const char* fname) const;
00273
00280 void* fromFile(const char* fname);
00281
00294 void fromPEMFile(const char* fname, const char* phrase=0);
00295
00296 protected:
00297 BIGNUM * exponentiate( const BIGNUM * aBN ) const;
00298
00299 private:
00300 CODEX_ASN1::SecureBigNumber m_p;
00301 CODEX_ASN1::SecureBigNumber m_q;
00302 CODEX_ASN1::SecureBigNumber m_d;
00303 CODEX_ASN1::SecureBigNumber m_n;
00304 CODEX_ASN1::SecureBigNumber m_phi;
00305 };
00306
00310 class RSAKeyPairGenerator
00311 {
00312 public:
00314 RSAKeyPairGenerator( long numBits ) : m_numBits( numBits ) {}
00315
00334 void operator()( RSAPublicKey*& pubKey,
00335 RSAPrivateKey*& privKey );
00336
00337 private:
00338 long m_numBits;
00339 };
00340
00341 }
00342
00343 #endif