00001 #ifndef CRYPTOPP_RSA_H
00002 #define CRYPTOPP_RSA_H
00003
00010 #include "pkcspad.h"
00011 #include "oaep.h"
00012 #include "integer.h"
00013
00014 NAMESPACE_BEGIN(CryptoPP)
00015
00017 class RSAFunction : virtual public TrapdoorFunction
00018 {
00019 public:
00020 RSAFunction(const Integer &n, const Integer &e) : n(n), e(e) {}
00021 RSAFunction(BufferedTransformation &bt);
00022 void DEREncode(BufferedTransformation &bt) const;
00023
00024 Integer ApplyFunction(const Integer &x) const;
00025 Integer PreimageBound() const {return n;}
00026 Integer ImageBound() const {return n;}
00027
00028 const Integer& GetModulus() const {return n;}
00029 const Integer& GetExponent() const {return e;}
00030
00031 protected:
00032 RSAFunction() {}
00033 Integer n, e;
00034 };
00035
00037 class InvertibleRSAFunction : public RSAFunction, public InvertibleTrapdoorFunction
00038 {
00039 public:
00040 InvertibleRSAFunction(const Integer &n, const Integer &e, const Integer &d,
00041 const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u);
00042
00043 InvertibleRSAFunction(RandomNumberGenerator &rng, unsigned int keybits, const Integer &eStart=17);
00044 InvertibleRSAFunction(BufferedTransformation &bt);
00045 void DEREncode(BufferedTransformation &bt) const;
00046
00047 Integer CalculateInverse(const Integer &x) const;
00048
00049 const Integer& GetPrime1() const {return p;}
00050 const Integer& GetPrime2() const {return q;}
00051 const Integer& GetDecryptionExponent() const {return d;}
00052
00053 protected:
00054 Integer d, p, q, dp, dq, u;
00055 };
00056
00058 template <class B>
00059 class RSAPrivateKeyTemplate : public B
00060 {
00061 public:
00062 RSAPrivateKeyTemplate(const Integer &n, const Integer &e, const Integer &d,
00063 const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u)
00064 : PublicKeyBaseTemplate<InvertibleRSAFunction>(
00065 InvertibleRSAFunction(n, e, d, p, q, dp, dq, u)) {}
00066
00067 RSAPrivateKeyTemplate(RandomNumberGenerator &rng, unsigned int keybits, const Integer &eStart=17)
00068 : PublicKeyBaseTemplate<InvertibleRSAFunction>(
00069 InvertibleRSAFunction(rng, keybits, eStart)) {}
00070
00071 RSAPrivateKeyTemplate(const InvertibleRSAFunction &priv)
00072 : PublicKeyBaseTemplate<InvertibleRSAFunction>(priv) {}
00073
00074 RSAPrivateKeyTemplate(BufferedTransformation &bt)
00075 : PublicKeyBaseTemplate<InvertibleRSAFunction>(bt) {}
00076 };
00077
00079 template <class B, class V>
00080 class RSAPublicKeyTemplate : public B
00081 {
00082 public:
00083 RSAPublicKeyTemplate(const Integer &n, const Integer &e)
00084 : PublicKeyBaseTemplate<RSAFunction>(RSAFunction(n, e)) {}
00085
00086 RSAPublicKeyTemplate(const V &priv)
00087 : PublicKeyBaseTemplate<RSAFunction>(priv.GetTrapdoorFunction()) {}
00088
00089 RSAPublicKeyTemplate(const RSAFunction &pub)
00090 : PublicKeyBaseTemplate<RSAFunction>(pub) {}
00091
00092 RSAPublicKeyTemplate(BufferedTransformation &bt)
00093 : PublicKeyBaseTemplate<RSAFunction>(bt) {}
00094 };
00095
00096
00098
00099 RSAES_PKCS1v15_Decryptor;
00101 typedef RSAPublicKeyTemplate<EncryptorTemplate<PKCS_EncryptionPaddingScheme, RSAFunction>, RSAES_PKCS1v15_Decryptor>
00102 RSAES_PKCS1v15_Encryptor;
00103
00105 typedef RSAPrivateKeyTemplate<DecryptorTemplate<OAEP<SHA>, InvertibleRSAFunction> >
00106 RSAES_OAEP_SHA_Decryptor;
00108 typedef RSAPublicKeyTemplate<EncryptorTemplate<OAEP<SHA>, RSAFunction>, RSAES_OAEP_SHA_Decryptor>
00109 RSAES_OAEP_SHA_Encryptor;
00110
00111
00113
00114 RSASSA_PKCS1v15_SHA_Signer;
00116 typedef RSAPublicKeyTemplate<VerifierTemplate<DigestVerifierTemplate<PKCS_SignaturePaddingScheme, RSAFunction>, PKCS_DecoratedHashModule<SHA> >, RSASSA_PKCS1v15_SHA_Signer>
00117 RSASSA_PKCS1v15_SHA_Verifier;
00118
00120 typedef RSAPrivateKeyTemplate<SignerTemplate<DigestSignerTemplate<PKCS_SignaturePaddingScheme, InvertibleRSAFunction>, PKCS_DecoratedHashModule<MD2> > >
00121 RSASSA_PKCS1v15_MD2_Signer;
00123 typedef RSAPublicKeyTemplate<VerifierTemplate<DigestVerifierTemplate<PKCS_SignaturePaddingScheme, RSAFunction>, PKCS_DecoratedHashModule<MD2> >, RSASSA_PKCS1v15_MD2_Signer>
00124 RSASSA_PKCS1v15_MD2_Verifier;
00125
00127 typedef RSAPrivateKeyTemplate<SignerTemplate<DigestSignerTemplate<PKCS_SignaturePaddingScheme, InvertibleRSAFunction>, PKCS_DecoratedHashModule<MD5> > >
00128 RSASSA_PKCS1v15_MD5_Signer;
00130 typedef RSAPublicKeyTemplate<VerifierTemplate<DigestVerifierTemplate<PKCS_SignaturePaddingScheme, RSAFunction>, PKCS_DecoratedHashModule<MD5> >, RSASSA_PKCS1v15_MD5_Signer>
00131 RSASSA_PKCS1v15_MD5_Verifier;
00132
00133 NAMESPACE_END
00134
00135 #endif