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 BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
00029         void DEREncodePublicKey(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 Load(BufferedTransformation &bt)
00070                 {PKCS8PrivateKey::BERDecode(bt);}
00071         void Save(BufferedTransformation &bt) const
00072                 {PKCS8PrivateKey::DEREncode(bt);}
00073         OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
00074         void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
00075         void DEREncodePrivateKey(BufferedTransformation &bt) const;
00076 
00077         // TrapdoorFunctionInverse
00078         Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
00079 
00080         // GeneratableCryptoMaterial
00081         bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00082         /*! parameters: (ModulusSize, PublicExponent (default 17)) */
00083         void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00084         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00085         void AssignFrom(const NameValuePairs &source);
00086 
00087         // non-derived interface
00088         const Integer& GetPrime1() const {return m_p;}
00089         const Integer& GetPrime2() const {return m_q;}
00090         const Integer& GetPrivateExponent() const {return m_d;}
00091         const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
00092         const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
00093         const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
00094 
00095         void SetPrime1(const Integer &p) {m_p = p;}
00096         void SetPrime2(const Integer &q) {m_q = q;}
00097         void SetPrivateExponent(const Integer &d) {m_d = d;}
00098         void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
00099         void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
00100         void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
00101 
00102 protected:
00103         Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
00104 };
00105 
00106 class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
00107 {
00108 public:
00109         Integer ApplyFunction(const Integer &x) const;
00110         Integer PreimageBound() const {return ++(m_n>>1);}
00111 };
00112 
00113 class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
00114 {
00115 public:
00116         Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
00117         Integer PreimageBound() const {return ++(m_n>>1);}
00118 };
00119 
00120 //! RSA
00121 struct CRYPTOPP_DLL RSA
00122 {
00123         static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
00124         typedef RSAFunction PublicKey;
00125         typedef InvertibleRSAFunction PrivateKey;
00126 };
00127 
00128 //! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
00129 template <class STANDARD>
00130 struct RSAES : public TF_ES<STANDARD, RSA>
00131 {
00132 };
00133 
00134 //! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
00135 /*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
00136 template <class STANDARD, class H>
00137 struct RSASS : public TF_SS<STANDARD, H, RSA>
00138 {
00139 };
00140 
00141 struct CRYPTOPP_DLL RSA_ISO
00142 {
00143         static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
00144         typedef RSAFunction_ISO PublicKey;
00145         typedef InvertibleRSAFunction_ISO PrivateKey;
00146 };
00147 
00148 template <class H>
00149 struct RSASS_ISO : public TF_SS<P1363_EMSA2, H, RSA_ISO>
00150 {
00151 };
00152 
00153 // The two RSA encryption schemes defined in PKCS #1 v2.0
00154 typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
00155 typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
00156 
00157 typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
00158 typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
00159 
00160 // The three RSA signature schemes defined in PKCS #1 v2.0
00161 typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
00162 typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
00163 
00164 typedef RSASS<PKCS1v15, MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
00165 typedef RSASS<PKCS1v15, MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
00166 
00167 typedef RSASS<PKCS1v15, MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
00168 typedef RSASS<PKCS1v15, MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
00169 
00170 NAMESPACE_END
00171 
00172 #endif

Generated on Sat Dec 23 02:07:10 2006 for Crypto++ by  doxygen 1.5.1-p1