• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/math/RSA.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _rsa_h
00018 #define _rsa_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 #include <spl/collection/Array.h>
00023 #include <spl/BigInteger.h>
00024 #include <spl/RefCountPtr.h>
00025 #include <spl/String.h>
00026 
00034 class RSAKey : public IMemoryValidate
00035 {
00036 protected:
00037         BigInteger m_modulus;
00038         BigInteger m_exponent;
00039         int m_bitSize;
00040 
00041 public:
00042         inline RSAKey()
00043         : m_bitSize(0), m_modulus(), m_exponent()
00044         {
00045         }
00046 
00047         virtual ~RSAKey();
00048 
00049         inline RSAKey(int bitSize, const BigInteger& mondulus, const BigInteger& exponent)
00050         : m_bitSize(bitSize), m_modulus(mondulus), m_exponent(exponent)
00051         {
00052         }
00053 
00054         inline RSAKey(const RSAKey& k)
00055         : m_bitSize(k.m_bitSize), m_modulus(k.m_modulus), m_exponent(k.m_exponent)
00056         {
00057         }
00058 
00059         inline RSAKey& operator =(const RSAKey& k)
00060         {
00061                 m_bitSize = k.m_bitSize;
00062                 m_modulus = k.m_modulus;
00063                 m_exponent = k.m_exponent;
00064 
00065                 return *this;
00066         }
00067 
00068         inline BigInteger& Modulus() { return m_modulus; }
00069         inline BigInteger& Exponent() { return m_exponent; }
00070         inline int GetBitSize() const { return m_bitSize; }
00071 
00072 #if defined(DEBUG) || defined(_DEBUG)
00073         virtual void CheckMem() const;
00074         virtual void ValidateMem() const;
00075 #endif
00076 };
00077 
00080 class RSAPrivateKey
00081 {
00082 private:
00083         BigInteger m_p;
00084         BigInteger m_q;
00085         BigInteger m_dP;
00086         BigInteger m_dQ;
00087         BigInteger m_qInv;
00088 
00089         BigInteger m_modulus;
00090         BigInteger m_privateExponent;
00091         BigInteger m_publicExponent;
00092 
00093         int m_bitSize;
00094 
00095 public:
00096         RSAPrivateKey(int strength, int certainty);
00097         RSAPrivateKey
00098         (
00099                 int bitSize,
00100                 BigInteger      modulus,
00101         BigInteger      publicExponent,
00102         BigInteger      privateExponent,
00103         BigInteger      p,
00104         BigInteger      q,
00105         BigInteger      dP,
00106         BigInteger      dQ,
00107         BigInteger      qInv
00108         );
00109 
00110         virtual ~RSAPrivateKey();
00111 
00112         inline RSAKey GetPublicKey() const { return RSAKey(m_bitSize, m_modulus, m_publicExponent); }
00113 
00114         BigInteger& PublicExponent()
00115     {
00116         return m_publicExponent;
00117         }
00118 
00119         BigInteger& PrivateExponent()
00120         {
00121                 return m_privateExponent;
00122         }
00123 
00124         BigInteger& Modulus()
00125         {
00126                 return m_modulus;
00127         }
00128 
00129         BigInteger& P()
00130         {
00131                 return m_p;
00132         }
00133 
00134         BigInteger& Q()
00135         {
00136                 return m_q;
00137         }
00138 
00139         BigInteger& DP()
00140         {
00141                 return m_dP;
00142         }
00143 
00144         BigInteger& DQ()
00145         {
00146                 return m_dQ;
00147         }
00148 
00149         BigInteger& QInv()
00150         {
00151                 return m_qInv;
00152         }
00153 
00154 #if defined(DEBUG) || defined(_DEBUG)
00155         virtual void CheckMem() const;
00156         virtual void ValidateMem() const;
00157 #endif
00158 };
00159 
00162 class RSAPublic : public IMemoryValidate
00163 {
00164 protected:
00165         RSAKey m_publicKey;
00166         int m_bitSize;
00167 
00168         //void ConvertInput
00169         //(
00170         //      Array<byte>& inBuf,
00171         //      int             inOff,
00172         //      int             inLen,
00173         //      BigInteger& output
00174         //);
00175 
00176         //void ConvertOutput
00177         //(
00178         //      bool forEncryption,
00179         //      BigInteger& result,
00180         //      Array<byte>& output
00181         //);
00182 
00190         int GetInputBlockSize(bool forEncryption) const;
00191 
00199         int GetOutputBlockSize(bool forEncryption) const;
00200 
00201 public:
00202         RSAPublic(int bitSize, BigInteger& mondulus, BigInteger& exponent);
00203         RSAPublic(int bitSize);
00204         RSAPublic(const RSAPublic& key);
00205         RSAPublic(const RSAKey& key, int bitSize);
00206         virtual ~RSAPublic();
00207 
00208         inline RSAKey& GetPublicKey() { return m_publicKey; }
00209         inline int GetBitSize() const { return m_bitSize; }
00210 
00211         RefCountPtr<Array<byte> > EncryptBinary(Array<byte>& plainText);
00212         RefCountPtr<String> EncryptText(Array<byte>& plainText);
00213         RefCountPtr<String> EncryptText(const String& plainText);
00214 
00215         void PadInputBuffer(Array<byte>& plainText) const;
00216 
00217 #if defined(DEBUG) || defined(_DEBUG)
00218         virtual void CheckMem() const;
00219         virtual void ValidateMem() const;
00220 #endif
00221 };
00222 
00225 class RSA : public RSAPublic
00226 {
00227 private:
00228         RSAPrivateKey m_key;
00229 
00230 public:
00231         RSA(int bitSize, int certainty = 4);
00232 
00233         inline RSAPublic& GetPublic() const { return (RSAPublic&)*this; }
00234         inline RSAPrivateKey GetPrivateKey() { return m_key; }
00235 
00236         RefCountPtr<Array<byte> > DecryptBinary(Array<byte>& encText);
00237         StringPtr DecryptText(const String& encText);
00238 
00239 #if defined(DEBUG) || defined(_DEBUG)
00240         virtual void CheckMem() const;
00241         virtual void ValidateMem() const;
00242 #endif
00243 };
00244 
00247 #endif