Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:08

0001 // rsa.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file rsa.h

0004 /// \brief Classes for the RSA cryptosystem

0005 /// \details This file contains classes that implement the RSA

0006 ///   ciphers and signature schemes as defined in PKCS #1 v2.0.

0007 
0008 #ifndef CRYPTOPP_RSA_H
0009 #define CRYPTOPP_RSA_H
0010 
0011 #include "cryptlib.h"
0012 #include "pubkey.h"
0013 #include "integer.h"
0014 #include "pkcspad.h"
0015 #include "oaep.h"
0016 #include "emsa2.h"
0017 #include "asn.h"
0018 
0019 NAMESPACE_BEGIN(CryptoPP)
0020 
0021 /// \brief RSA trapdoor function using the public key

0022 /// \since Crypto++ 1.0

0023 class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
0024 {
0025     typedef RSAFunction ThisClass;
0026 
0027 public:
0028     /// \brief Initialize a RSA public key

0029     /// \param n the modulus

0030     /// \param e the public exponent

0031     void Initialize(const Integer &n, const Integer &e)
0032         {m_n = n; m_e = e;}
0033 
0034     // X509PublicKey

0035     OID GetAlgorithmID() const;
0036     void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
0037     void DEREncodePublicKey(BufferedTransformation &bt) const;
0038 
0039     // CryptoMaterial

0040     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0041     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0042     void AssignFrom(const NameValuePairs &source);
0043 
0044     // TrapdoorFunction

0045     Integer ApplyFunction(const Integer &x) const;
0046     Integer PreimageBound() const {return m_n;}
0047     Integer ImageBound() const {return m_n;}
0048 
0049     // non-derived

0050     const Integer & GetModulus() const {return m_n;}
0051     const Integer & GetPublicExponent() const {return m_e;}
0052 
0053     void SetModulus(const Integer &n) {m_n = n;}
0054     void SetPublicExponent(const Integer &e) {m_e = e;}
0055 
0056 protected:
0057     Integer m_n, m_e;
0058 };
0059 
0060 /// \brief RSA trapdoor function using the private key

0061 /// \since Crypto++ 1.0

0062 class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
0063 {
0064     typedef InvertibleRSAFunction ThisClass;
0065 
0066 public:
0067     /// \brief Create a RSA private key

0068     /// \param rng a RandomNumberGenerator derived class

0069     /// \param modulusBits the size of the modulus, in bits

0070     /// \param e the desired public exponent

0071     /// \details Initialize() creates a new keypair using a public exponent of 17.

0072     /// \details This function overload of Initialize() creates a new private key because it

0073     ///   takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0074     ///   then use one of the other Initialize() overloads.

0075     void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
0076 
0077     /// \brief Initialize a RSA private key

0078     /// \param n modulus

0079     /// \param e public exponent

0080     /// \param d private exponent

0081     /// \param p first prime factor

0082     /// \param q second prime factor

0083     /// \param dp d mod p

0084     /// \param dq d mod q

0085     /// \param u q<sup>-1</sup> mod p

0086     /// \details This Initialize() function overload initializes a private key from existing parameters.

0087     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)
0088         {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
0089 
0090     /// \brief Initialize a RSA private key

0091     /// \param n modulus

0092     /// \param e public exponent

0093     /// \param d private exponent

0094     /// \details This Initialize() function overload initializes a private key from existing parameters.

0095     ///   Initialize() will factor n using d and populate {p,q,dp,dq,u}.

0096     void Initialize(const Integer &n, const Integer &e, const Integer &d);
0097 
0098     // PKCS8PrivateKey

0099     void BERDecode(BufferedTransformation &bt)
0100         {PKCS8PrivateKey::BERDecode(bt);}
0101     void DEREncode(BufferedTransformation &bt) const
0102         {PKCS8PrivateKey::DEREncode(bt);}
0103     void Load(BufferedTransformation &bt)
0104         {PKCS8PrivateKey::BERDecode(bt);}
0105     void Save(BufferedTransformation &bt) const
0106         {PKCS8PrivateKey::DEREncode(bt);}
0107     OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
0108     void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
0109     void DEREncodePrivateKey(BufferedTransformation &bt) const;
0110 
0111     // TrapdoorFunctionInverse

0112     Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0113 
0114     // GeneratableCryptoMaterial

0115     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0116     // parameters: (ModulusSize, PublicExponent (default 17))

0117     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0118     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0119     void AssignFrom(const NameValuePairs &source);
0120 
0121     // non-derived interface

0122     const Integer& GetPrime1() const {return m_p;}
0123     const Integer& GetPrime2() const {return m_q;}
0124     const Integer& GetPrivateExponent() const {return m_d;}
0125     const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
0126     const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
0127     const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
0128 
0129     void SetPrime1(const Integer &p) {m_p = p;}
0130     void SetPrime2(const Integer &q) {m_q = q;}
0131     void SetPrivateExponent(const Integer &d) {m_d = d;}
0132     void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
0133     void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
0134     void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
0135 
0136 protected:
0137     Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
0138 };
0139 
0140 /// \brief RSA trapdoor function using the public key

0141 /// \since Crypto++ 1.0

0142 class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
0143 {
0144 public:
0145     Integer ApplyFunction(const Integer &x) const;
0146     Integer PreimageBound() const {return ++(m_n>>1);}
0147 };
0148 
0149 /// \brief RSA trapdoor function using the private key

0150 /// \since Crypto++ 1.0

0151 class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
0152 {
0153 public:
0154     Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0155     Integer PreimageBound() const {return ++(m_n>>1);}
0156 };
0157 
0158 /// \brief RSA algorithm

0159 /// \since Crypto++ 1.0

0160 struct CRYPTOPP_DLL RSA
0161 {
0162     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
0163     typedef RSAFunction PublicKey;
0164     typedef InvertibleRSAFunction PrivateKey;
0165 };
0166 
0167 /// \brief RSA encryption algorithm

0168 /// \tparam STANDARD signature standard

0169 /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>

0170 /// \since Crypto++ 1.0

0171 template <class STANDARD>
0172 struct RSAES : public TF_ES<RSA, STANDARD>
0173 {
0174 };
0175 
0176 /// \brief RSA signature algorithm

0177 /// \tparam STANDARD signature standard

0178 /// \tparam H hash transformation

0179 /// \details See documentation of PKCS1v15 for a list of hash functions that can be used with it.

0180 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>

0181 /// \since Crypto++ 1.0

0182 template <class STANDARD, class H>
0183 struct RSASS : public TF_SS<RSA, STANDARD, H>
0184 {
0185 };
0186 
0187 /// \brief RSA algorithm

0188 /// \since Crypto++ 1.0

0189 struct CRYPTOPP_DLL RSA_ISO
0190 {
0191     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
0192     typedef RSAFunction_ISO PublicKey;
0193     typedef InvertibleRSAFunction_ISO PrivateKey;
0194 };
0195 
0196 /// \brief RSA signature algorithm

0197 /// \tparam H hash transformation

0198 /// \since Crypto++ 1.0

0199 template <class H>
0200 struct RSASS_ISO : public TF_SS<RSA_ISO, P1363_EMSA2, H>
0201 {
0202 };
0203 
0204 /// \brief \ref RSAES<STANDARD> "RSAES<PKCS1v15>::Decryptor" typedef

0205 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0206 /// \since Crypto++ 1.0

0207 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Decryptor, RSAES_PKCS1v15_Decryptor);
0208 /// \brief \ref RSAES<STANDARD> "RSAES<PKCS1v15>::Encryptor" typedef

0209 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0210 /// \since Crypto++ 1.0

0211 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Encryptor, RSAES_PKCS1v15_Encryptor);
0212 
0213 /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA1>>::Decryptor" typedef

0214 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0215 /// \since Crypto++ 1.0

0216 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Decryptor, RSAES_OAEP_SHA_Decryptor);
0217 /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA1>>::Encryptor" typedef

0218 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0219 /// \since Crypto++ 1.0

0220 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Encryptor, RSAES_OAEP_SHA_Encryptor);
0221 
0222 /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA256>>::Decryptor" typedef

0223 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0224 /// \since Crypto++ 8.8

0225 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA256> >::Decryptor, RSAES_OAEP_SHA256_Decryptor);
0226 /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA256>>::Encryptor" typedef

0227 /// \details RSA encryption scheme defined in PKCS #1 v2.0

0228 /// \since Crypto++ 8.8

0229 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA256> >::Encryptor, RSAES_OAEP_SHA256_Encryptor);
0230 
0231 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
0232 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA1>::Signer" typedef

0233 /// \details RSA signature schemes defined in PKCS #1 v2.0

0234 /// \since Crypto++ 1.0

0235 class RSASSA_PKCS1v15_SHA_Signer : public RSASS<PKCS1v15,SHA1>::Signer {};
0236 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA1>::Verifier" typedef

0237 /// \details RSA signature schemes defined in PKCS #1 v2.0

0238 /// \since Crypto++ 1.0

0239 class RSASSA_PKCS1v15_SHA_Verifier : public RSASS<PKCS1v15,SHA1>::Verifier {};
0240 
0241 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA256>::Signer" typedef

0242 /// \details RSA signature schemes defined in PKCS #1 v2.0

0243 /// \since Crypto++ 8.8

0244 class RSASSA_PKCS1v15_SHA256_Signer : public RSASS<PKCS1v15,SHA256>::Signer {};
0245 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA256>::Verifier" typedef

0246 /// \details RSA signature schemes defined in PKCS #1 v2.0

0247 /// \since Crypto++ 8.8

0248 class RSASSA_PKCS1v15_SHA256_Verifier : public RSASS<PKCS1v15,SHA256>::Verifier {};
0249 
0250 namespace Weak {
0251 
0252 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD2>::Signer" typedef

0253 /// \details RSA signature schemes defined in PKCS #1 v2.0

0254 /// \since Crypto++ 1.0

0255 class RSASSA_PKCS1v15_MD2_Signer : public RSASS<PKCS1v15, Weak1::MD2>::Signer {};
0256 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD2>::Verifier" typedef

0257 /// \details RSA signature schemes defined in PKCS #1 v2.0

0258 /// \since Crypto++ 1.0

0259 class RSASSA_PKCS1v15_MD2_Verifier : public RSASS<PKCS1v15, Weak1::MD2>::Verifier {};
0260 
0261 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD5>::Signer" typedef

0262 /// \details RSA signature schemes defined in PKCS #1 v2.0

0263 /// \since Crypto++ 1.0

0264 class RSASSA_PKCS1v15_MD5_Signer : public RSASS<PKCS1v15, Weak1::MD5>::Signer {};
0265 /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD5>::Verifier" typedef

0266 /// \details RSA signature schemes defined in PKCS #1 v2.0

0267 /// \since Crypto++ 1.0

0268 class RSASSA_PKCS1v15_MD5_Verifier : public RSASS<PKCS1v15, Weak1::MD5>::Verifier {};
0269 }
0270 
0271 #else
0272 typedef RSASS<PKCS1v15,SHA1>::Signer RSASSA_PKCS1v15_SHA_Signer;
0273 typedef RSASS<PKCS1v15,SHA1>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
0274 
0275 typedef RSASS<PKCS1v15,SHA256>::Signer RSASSA_PKCS1v15_SHA256_Signer;
0276 typedef RSASS<PKCS1v15,SHA256>::Verifier RSASSA_PKCS1v15_SHA256_Verifier;
0277 
0278 namespace Weak {
0279     typedef RSASS<PKCS1v15, Weak1::MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
0280     typedef RSASS<PKCS1v15, Weak1::MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
0281     typedef RSASS<PKCS1v15, Weak1::MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
0282     typedef RSASS<PKCS1v15, Weak1::MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
0283 }
0284 #endif // CRYPTOPP_DOXYGEN_PROCESSING

0285 
0286 NAMESPACE_END
0287 
0288 #endif