00001 #ifndef CRYPTOPP_RSA_H
00002 #define CRYPTOPP_RSA_H
00003
00004
00005
00006
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
00026 OID GetAlgorithmID() const;
00027 void BERDecodeKey(BufferedTransformation &bt);
00028 void DEREncodeKey(BufferedTransformation &bt) const;
00029
00030
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
00036 Integer ApplyFunction(const Integer &x) const;
00037 Integer PreimageBound() const {return m_n;}
00038 Integer ImageBound() const {return m_n;}
00039
00040
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
00061
00062 void BERDecode(BufferedTransformation &bt)
00063 {PKCS8PrivateKey::BERDecode(bt);}
00064 void DEREncode(BufferedTransformation &bt) const
00065 {PKCS8PrivateKey::DEREncode(bt);}
00066 void BERDecodeKey(BufferedTransformation &bt);
00067 void DEREncodeKey(BufferedTransformation &bt) const;
00068
00069
00070 Integer CalculateInverse(const Integer &x) const;
00071
00072
00073 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00074
00075 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00076 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00077 void AssignFrom(const NameValuePairs &source);
00078
00079
00080 const Integer& GetPrime1() const {return m_p;}
00081 const Integer& GetPrime2() const {return m_q;}
00082 const Integer& GetPrivateExponent() const {return m_d;}
00083 const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
00084 const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
00085 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
00086
00087 void SetPrime1(const Integer &p) {m_p = p;}
00088 void SetPrime2(const Integer &q) {m_q = q;}
00089 void SetPrivateExponent(const Integer &d) {m_d = d;}
00090 void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
00091 void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
00092 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
00093
00094 protected:
00095 virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const {}
00096 virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt) {}
00097
00098 Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
00099 };
00100
00101
00102
00103 class RSAFunctionInverse_NonCRT : public TrapdoorFunctionBounds, public TrapdoorFunctionInverse, public PrivateKey
00104 {
00105 public:
00106 Integer CalculateInverse(const Integer &x) const
00107 {return a_exp_b_mod_c(x, m_d, m_n);}
00108 Integer PreimageBound() const {return m_n;}
00109 Integer ImageBound() const {return m_n;}
00110
00111 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00112
00113 const Integer& GetModulus() const {return m_n;}
00114 const Integer& GetDecryptionExponent() const {return m_d;}
00115
00116 void SetModulus(const Integer &n) {m_n = n;}
00117 void SetDecryptionExponent(const Integer &d) {m_d = d;}
00118
00119 void SetPrivateValues(const Integer &n, const Integer &d)
00120 {m_n = n; m_d = d;}
00121
00122 private:
00123 Integer m_n, m_d;
00124 };
00125 */
00126
00127
00128 struct CRYPTOPP_DLL RSA
00129 {
00130 static std::string StaticAlgorithmName() {return "RSA";}
00131 typedef RSAFunction PublicKey;
00132 typedef InvertibleRSAFunction PrivateKey;
00133 };
00134
00135
00136 template <class STANDARD>
00137 struct RSAES : public TF_ES<STANDARD, RSA>
00138 {
00139 };
00140
00141
00142
00143 template <class STANDARD, class H>
00144 struct RSASSA : public TF_SSA<STANDARD, H, RSA>
00145 {
00146 };
00147
00148
00149 typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
00150 typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
00151
00152 typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
00153 typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
00154
00155
00156 typedef RSASSA<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
00157 typedef RSASSA<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
00158
00159 typedef RSASSA<PKCS1v15, MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
00160 typedef RSASSA<PKCS1v15, MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
00161
00162 typedef RSASSA<PKCS1v15, MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
00163 typedef RSASSA<PKCS1v15, MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
00164
00165 NAMESPACE_END
00166
00167 #endif