00001
00002
00003
00004
00005
00006
00007
00008 #ifndef CRYPTOPP_RSA_H
00009 #define CRYPTOPP_RSA_H
00010
00011 #include "cryptlib.h"
00012 #include "pubkey.h"
00013 #include "integer.h"
00014 #include "pkcspad.h"
00015 #include "oaep.h"
00016 #include "emsa2.h"
00017 #include "asn.h"
00018
00019 NAMESPACE_BEGIN(CryptoPP)
00020
00021
00022 class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
00023 {
00024 typedef RSAFunction ThisClass;
00025
00026 public:
00027 void Initialize(const Integer &n, const Integer &e)
00028 {m_n = n; m_e = e;}
00029
00030
00031 OID GetAlgorithmID() const;
00032 void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
00033 void DEREncodePublicKey(BufferedTransformation &bt) const;
00034
00035
00036 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00037 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00038 void AssignFrom(const NameValuePairs &source);
00039
00040
00041 Integer ApplyFunction(const Integer &x) const;
00042 Integer PreimageBound() const {return m_n;}
00043 Integer ImageBound() const {return m_n;}
00044
00045
00046 const Integer & GetModulus() const {return m_n;}
00047 const Integer & GetPublicExponent() const {return m_e;}
00048
00049 void SetModulus(const Integer &n) {m_n = n;}
00050 void SetPublicExponent(const Integer &e) {m_e = e;}
00051
00052 protected:
00053 Integer m_n, m_e;
00054 };
00055
00056
00057 class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
00058 {
00059 typedef InvertibleRSAFunction ThisClass;
00060
00061 public:
00062 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
00063 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)
00064 {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
00065
00066 void Initialize(const Integer &n, const Integer &e, const Integer &d);
00067
00068
00069 void BERDecode(BufferedTransformation &bt)
00070 {PKCS8PrivateKey::BERDecode(bt);}
00071 void DEREncode(BufferedTransformation &bt) const
00072 {PKCS8PrivateKey::DEREncode(bt);}
00073 void Load(BufferedTransformation &bt)
00074 {PKCS8PrivateKey::BERDecode(bt);}
00075 void Save(BufferedTransformation &bt) const
00076 {PKCS8PrivateKey::DEREncode(bt);}
00077 OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
00078 void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
00079 void DEREncodePrivateKey(BufferedTransformation &bt) const;
00080
00081
00082 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
00083
00084
00085 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00086
00087 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00088 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00089 void AssignFrom(const NameValuePairs &source);
00090
00091
00092 const Integer& GetPrime1() const {return m_p;}
00093 const Integer& GetPrime2() const {return m_q;}
00094 const Integer& GetPrivateExponent() const {return m_d;}
00095 const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
00096 const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
00097 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
00098
00099 void SetPrime1(const Integer &p) {m_p = p;}
00100 void SetPrime2(const Integer &q) {m_q = q;}
00101 void SetPrivateExponent(const Integer &d) {m_d = d;}
00102 void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
00103 void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
00104 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
00105
00106 protected:
00107 Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
00108 };
00109
00110 class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
00111 {
00112 public:
00113 Integer ApplyFunction(const Integer &x) const;
00114 Integer PreimageBound() const {return ++(m_n>>1);}
00115 };
00116
00117 class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
00118 {
00119 public:
00120 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
00121 Integer PreimageBound() const {return ++(m_n>>1);}
00122 };
00123
00124
00125 struct CRYPTOPP_DLL RSA
00126 {
00127 static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
00128 typedef RSAFunction PublicKey;
00129 typedef InvertibleRSAFunction PrivateKey;
00130 };
00131
00132
00133 template <class STANDARD>
00134 struct RSAES : public TF_ES<STANDARD, RSA>
00135 {
00136 };
00137
00138
00139
00140 template <class STANDARD, class H>
00141 struct RSASS : public TF_SS<STANDARD, H, RSA>
00142 {
00143 };
00144
00145 struct CRYPTOPP_DLL RSA_ISO
00146 {
00147 static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
00148 typedef RSAFunction_ISO PublicKey;
00149 typedef InvertibleRSAFunction_ISO PrivateKey;
00150 };
00151
00152 template <class H>
00153 struct RSASS_ISO : public TF_SS<P1363_EMSA2, H, RSA_ISO>
00154 {
00155 };
00156
00157
00158 typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
00159 typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
00160
00161 typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
00162 typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
00163
00164
00165 typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
00166 typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
00167
00168 namespace Weak {
00169 typedef RSASS<PKCS1v15, Weak1::MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
00170 typedef RSASS<PKCS1v15, Weak1::MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
00171
00172 typedef RSASS<PKCS1v15, Weak1::MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
00173 typedef RSASS<PKCS1v15, Weak1::MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
00174 }
00175
00176 NAMESPACE_END
00177
00178 #endif