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

Generated on Thu Mar 30 22:11:47 2006 for Crypto++ by  doxygen 1.4.6