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