- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# rsa.h

Go to the documentation of this file.

    00001 #ifndef CRYPTOPP_RSA_H
    00002 #define CRYPTOPP_RSA_H
    00003
    00004 /** \file
    00005         This file contains classes that implement the RSA
    00006         ciphers and signature schemes as defined in PKCS #1 v2.0.
    00007 */
    00008
    00009 #include "pubkey.h"
    00010 #include "asn.h"
    00011 #include "pkcspad.h"
    00012 #include "oaep.h"
    00013 #include "emsa2.h"
    00014
    00015 NAMESPACE_BEGIN(CryptoPP)
    00016
    00017 //! _
    00018 class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
    00019 {
    00020         typedef RSAFunction ThisClass;
    00021
    00022 public:
    00023         void Initialize(const Integer &n, const Integer &e)
    00024                 {m_n = n; m_e = e;}
    00025
    00026         // X509PublicKey
    00027         OID GetAlgorithmID() const;
    00028         void BERDecodeKey(BufferedTransformation &bt);
    00029         void DEREncodeKey(BufferedTransformation &bt) const;
    00030
    00031         // CryptoMaterial
    00032         bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
    00033         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
    00034         void AssignFrom(const NameValuePairs &source);
    00035
    00036         // TrapdoorFunction
    00037         Integer ApplyFunction(const Integer &x) const;
    00038         Integer PreimageBound() const {return m_n;}
    00039         Integer ImageBound() const {return m_n;}
    00040
    00041         // non-derived
    00042         const Integer & GetModulus() const {return m_n;}
    00043         const Integer & GetPublicExponent() const {return m_e;}
    00044
    00045         void SetModulus(const Integer &n) {m_n = n;}
    00046         void SetPublicExponent(const Integer &e) {m_e = e;}
    00047
    00048 protected:
    00049         Integer m_n, m_e;
    00050 };
    00051
    00052 //! _
    00053 class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
    00054 {
    00055         typedef InvertibleRSAFunction ThisClass;
    00056
    00057 public:
    00058         void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
    00059         void Initialize(const Integer &n, const Integer &e, const Integer &d, const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u)
    00060                 {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
    00061         //! factor n given private exponent
    00062         void Initialize(const Integer &n, const Integer &e, const Integer &d);
    00063
    00064         // PKCS8PrivateKey
    00065         void BERDecode(BufferedTransformation &bt)
    00066                 {PKCS8PrivateKey::BERDecode(bt);}
    00067         void DEREncode(BufferedTransformation &bt) const
    00068                 {PKCS8PrivateKey::DEREncode(bt);}
    00069         void BERDecodeKey(BufferedTransformation &bt);
    00070         void DEREncodeKey(BufferedTransformation &bt) const;
    00071
    00072         // TrapdoorFunctionInverse
    00073         Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
    00074
    00075         // GeneratableCryptoMaterial
    00076         bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
    00077         /*! parameters: (ModulusSize, PublicExponent (default 17)) */
    00078         void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
    00079         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
    00080         void AssignFrom(const NameValuePairs &source);
    00081
    00082         // non-derived interface
    00083         const Integer& GetPrime1() const {return m_p;}
    00084         const Integer& GetPrime2() const {return m_q;}
    00085         const Integer& GetPrivateExponent() const {return m_d;}
    00086         const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
    00087         const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
    00088         const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
    00089
    00090         void SetPrime1(const Integer &p) {m_p = p;}
    00091         void SetPrime2(const Integer &q) {m_q = q;}
    00092         void SetPrivateExponent(const Integer &d) {m_d = d;}
    00093         void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
    00094         void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
    00095         void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
    00096
    00097 protected:
    00098         Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
    00099 };
    00100
    00101 class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
    00102 {
    00103 public:
    00104         Integer ApplyFunction(const Integer &x) const;
    00105         Integer PreimageBound() const {return ++(m_n>>1);}
    00106 };
    00107
    00108 class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
    00109 {
    00110 public:
    00111         Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
    00112         Integer PreimageBound() const {return ++(m_n>>1);}
    00113 };
    00114
    00115 //! RSA
    00116 struct CRYPTOPP_DLL RSA
    00117 {
    00118         static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
    00119         typedef RSAFunction PublicKey;
    00120         typedef InvertibleRSAFunction PrivateKey;
    00121 };
    00122
    00123 //! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
    00124 template <class STANDARD>
    00125 struct RSAES : public TF_ES<STANDARD, RSA>
    00126 {
    00127 };
    00128
    00129 //! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
    00130 /*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
    00131 template <class STANDARD, class H>
    00132 struct RSASS : public TF_SS<STANDARD, H, RSA>
    00133 {
    00134 };
    00135
    00136 struct CRYPTOPP_DLL RSA_ISO
    00137 {
    00138         static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
    00139         typedef RSAFunction_ISO PublicKey;
    00140         typedef InvertibleRSAFunction_ISO PrivateKey;
    00141 };
    00142
    00143 template <class H>
    00144 struct RSASS_ISO : public TF_SS<P1363_EMSA2, H, RSA_ISO>
    00145 {
    00146 };
    00147
    00148 // The two RSA encryption schemes defined in PKCS #1 v2.0
    00149 typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
    00150 typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
    00151
    00152 typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
    00153 typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
    00154
    00155 // The three RSA signature schemes defined in PKCS #1 v2.0
    00156 typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
    00157 typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
    00158
    00159 typedef RSASS<PKCS1v15, MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
    00160 typedef RSASS<PKCS1v15, MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
    00161
    00162 typedef RSASS<PKCS1v15, MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
    00163 typedef RSASS<PKCS1v15, MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
    00164
    00165 NAMESPACE_END
    00166
    00167 #endif