00001
00002
00003
00004
00005
00006
00007 #ifndef CRYPTOPP_RABIN_H
00008 #define CRYPTOPP_RABIN_H
00009
00010 #include "cryptlib.h"
00011 #include "oaep.h"
00012 #include "pssr.h"
00013 #include "integer.h"
00014
00015 NAMESPACE_BEGIN(CryptoPP)
00016
00017
00018 class RabinFunction : public TrapdoorFunction, public PublicKey
00019 {
00020 typedef RabinFunction ThisClass;
00021
00022 public:
00023 void Initialize(const Integer &n, const Integer &r, const Integer &s)
00024 {m_n = n; m_r = r; m_s = s;}
00025
00026 void BERDecode(BufferedTransformation &bt);
00027 void DEREncode(BufferedTransformation &bt) const;
00028
00029 Integer ApplyFunction(const Integer &x) const;
00030 Integer PreimageBound() const {return m_n;}
00031 Integer ImageBound() const {return m_n;}
00032
00033 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00034 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00035 void AssignFrom(const NameValuePairs &source);
00036
00037 const Integer& GetModulus() const {return m_n;}
00038 const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
00039 const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
00040
00041 void SetModulus(const Integer &n) {m_n = n;}
00042 void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
00043 void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
00044
00045 protected:
00046 Integer m_n, m_r, m_s;
00047 };
00048
00049
00050 class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
00051 {
00052 typedef InvertibleRabinFunction ThisClass;
00053
00054 public:
00055 void Initialize(const Integer &n, const Integer &r, const Integer &s,
00056 const Integer &p, const Integer &q, const Integer &u)
00057 {m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
00058 void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
00059 {GenerateRandomWithKeySize(rng, keybits);}
00060
00061 void BERDecode(BufferedTransformation &bt);
00062 void DEREncode(BufferedTransformation &bt) const;
00063
00064 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
00065
00066 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
00067 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00068 void AssignFrom(const NameValuePairs &source);
00069
00070 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
00071
00072 const Integer& GetPrime1() const {return m_p;}
00073 const Integer& GetPrime2() const {return m_q;}
00074 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
00075
00076 void SetPrime1(const Integer &p) {m_p = p;}
00077 void SetPrime2(const Integer &q) {m_q = q;}
00078 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
00079
00080 protected:
00081 Integer m_p, m_q, m_u;
00082 };
00083
00084
00085 struct Rabin
00086 {
00087 static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
00088 typedef RabinFunction PublicKey;
00089 typedef InvertibleRabinFunction PrivateKey;
00090 };
00091
00092
00093 template <class STANDARD>
00094 struct RabinES : public TF_ES<STANDARD, Rabin>
00095 {
00096 };
00097
00098
00099 template <class STANDARD, class H>
00100 struct RabinSS : public TF_SS<STANDARD, H, Rabin>
00101 {
00102 };
00103
00104
00105 class SHA1;
00106 typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
00107 typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
00108
00109 NAMESPACE_END
00110
00111 #endif