File indexing completed on 2025-01-18 09:55:08
0001
0002
0003
0004
0005
0006
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
0022
0023 class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
0024 {
0025 typedef RSAFunction ThisClass;
0026
0027 public:
0028
0029
0030
0031 void Initialize(const Integer &n, const Integer &e)
0032 {m_n = n; m_e = e;}
0033
0034
0035 OID GetAlgorithmID() const;
0036 void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
0037 void DEREncodePublicKey(BufferedTransformation &bt) const;
0038
0039
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
0045 Integer ApplyFunction(const Integer &x) const;
0046 Integer PreimageBound() const {return m_n;}
0047 Integer ImageBound() const {return m_n;}
0048
0049
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
0061
0062 class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
0063 {
0064 typedef InvertibleRSAFunction ThisClass;
0065
0066 public:
0067
0068
0069
0070
0071
0072
0073
0074
0075 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
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
0091
0092
0093
0094
0095
0096 void Initialize(const Integer &n, const Integer &e, const Integer &d);
0097
0098
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
0112 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0113
0114
0115 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0116
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
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
0141
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
0150
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
0159
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
0168
0169
0170
0171 template <class STANDARD>
0172 struct RSAES : public TF_ES<RSA, STANDARD>
0173 {
0174 };
0175
0176
0177
0178
0179
0180
0181
0182 template <class STANDARD, class H>
0183 struct RSASS : public TF_SS<RSA, STANDARD, H>
0184 {
0185 };
0186
0187
0188
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
0197
0198
0199 template <class H>
0200 struct RSASS_ISO : public TF_SS<RSA_ISO, P1363_EMSA2, H>
0201 {
0202 };
0203
0204
0205
0206
0207 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Decryptor, RSAES_PKCS1v15_Decryptor);
0208
0209
0210
0211 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Encryptor, RSAES_PKCS1v15_Encryptor);
0212
0213
0214
0215
0216 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Decryptor, RSAES_OAEP_SHA_Decryptor);
0217
0218
0219
0220 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Encryptor, RSAES_OAEP_SHA_Encryptor);
0221
0222
0223
0224
0225 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA256> >::Decryptor, RSAES_OAEP_SHA256_Decryptor);
0226
0227
0228
0229 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA256> >::Encryptor, RSAES_OAEP_SHA256_Encryptor);
0230
0231 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
0232
0233
0234
0235 class RSASSA_PKCS1v15_SHA_Signer : public RSASS<PKCS1v15,SHA1>::Signer {};
0236
0237
0238
0239 class RSASSA_PKCS1v15_SHA_Verifier : public RSASS<PKCS1v15,SHA1>::Verifier {};
0240
0241
0242
0243
0244 class RSASSA_PKCS1v15_SHA256_Signer : public RSASS<PKCS1v15,SHA256>::Signer {};
0245
0246
0247
0248 class RSASSA_PKCS1v15_SHA256_Verifier : public RSASS<PKCS1v15,SHA256>::Verifier {};
0249
0250 namespace Weak {
0251
0252
0253
0254
0255 class RSASSA_PKCS1v15_MD2_Signer : public RSASS<PKCS1v15, Weak1::MD2>::Signer {};
0256
0257
0258
0259 class RSASSA_PKCS1v15_MD2_Verifier : public RSASS<PKCS1v15, Weak1::MD2>::Verifier {};
0260
0261
0262
0263
0264 class RSASSA_PKCS1v15_MD5_Signer : public RSASS<PKCS1v15, Weak1::MD5>::Signer {};
0265
0266
0267
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
0285
0286 NAMESPACE_END
0287
0288 #endif