00001 #ifndef CRYPTOPP_RW_H
00002 #define CRYPTOPP_RW_H
00003
00009 #include "pubkey.h"
00010 #include "integer.h"
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014 const word IFSSR_R = 6;
00015 const word IFSSA_R = 12;
00016
00018 class EMSA2Pad
00019 {
00020 public:
00021 unsigned int MaxUnpaddedLength(unsigned int paddedLength) const {return (paddedLength+1)/8-2;}
00022
00023 void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
00024 unsigned int Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
00025 };
00026
00028 template <class H>
00029 class EMSA2DecoratedHashModule : public HashModule
00030 {
00031 public:
00032 EMSA2DecoratedHashModule() : empty(true) {}
00033 void Update(const byte *input, unsigned int length)
00034 {h.Update(input, length); empty = empty && length==0;}
00035 unsigned int DigestSize() const;
00036 void Final(byte *digest);
00037
00038 private:
00039 H h;
00040 bool empty;
00041 };
00042
00043 template <class H> struct EMSA2DigestDecoration
00044 {
00045 static const byte decoration;
00046 };
00047
00048
00049 class SHA;
00050 class RIPEMD160;
00051
00052 template <class H>
00053 void EMSA2DecoratedHashModule<H>::Final(byte *digest)
00054 {
00055 digest[0] = empty ? 0x4b : 0x6b;
00056 h.Final(digest+1);
00057 digest[DigestSize()-1] = EMSA2DigestDecoration<H>::decoration;
00058 }
00059
00060 template <class H>
00061 unsigned int EMSA2DecoratedHashModule<H>::DigestSize() const
00062 {
00063 return h.DigestSize() + 2;
00064 }
00065
00066
00067
00069 template <word r>
00070 class RWFunction : virtual public TrapdoorFunction
00071 {
00072 public:
00073 RWFunction(const Integer &n);
00074 RWFunction(BufferedTransformation &bt);
00075 void DEREncode(BufferedTransformation &bt) const;
00076 ~RWFunction();
00077
00078 Integer ApplyFunction(const Integer &x) const;
00079 Integer PreimageBound() const {return n;}
00080 Integer ImageBound() const {return ++(n>>1);}
00081
00082 const Integer& GetModulus() const {return n;}
00083
00084 protected:
00085 RWFunction() {}
00086 Integer n;
00087 };
00088
00090 template <word r>
00091 class InvertibleRWFunction : public RWFunction<r>, public InvertibleTrapdoorFunction
00092 {
00093 public:
00094 InvertibleRWFunction(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
00095
00096 InvertibleRWFunction(RandomNumberGenerator &rng, unsigned int keybits);
00097 InvertibleRWFunction(BufferedTransformation &bt);
00098 ~InvertibleRWFunction();
00099 void DEREncode(BufferedTransformation &bt) const;
00100
00101 Integer CalculateInverse(const Integer &x) const;
00102
00103 const Integer& GetPrime1() const {return p;}
00104 const Integer& GetPrime2() const {return q;}
00105
00106 protected:
00107 Integer p, q, u;
00108 };
00109
00111 template <class H>
00112 class RWSigner : public SignerTemplate<DigestSignerTemplate<EMSA2Pad, InvertibleRWFunction<IFSSA_R> >, EMSA2DecoratedHashModule<H> >
00113 {
00114 public:
00115 RWSigner(const Integer &n, const Integer &p, const Integer &q, const Integer &u)
00116 : PublicKeyBaseTemplate<InvertibleRWFunction<IFSSA_R> >(
00117 InvertibleRWFunction<IFSSA_R>(n, p, q, u)) {}
00118
00119 RWSigner(RandomNumberGenerator &rng, unsigned int keybits)
00120 : PublicKeyBaseTemplate<InvertibleRWFunction<IFSSA_R> >(
00121 InvertibleRWFunction<IFSSA_R>(rng, keybits)) {}
00122
00123 RWSigner(BufferedTransformation &bt)
00124 : PublicKeyBaseTemplate<InvertibleRWFunction<IFSSA_R> >(bt) {}
00125 };
00126
00128 template <class H>
00129 class RWVerifier : public VerifierTemplate<DigestVerifierTemplate<EMSA2Pad, RWFunction<IFSSA_R> >, EMSA2DecoratedHashModule<H> >
00130 {
00131 public:
00132 RWVerifier(const Integer &n)
00133 : PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(RWFunction<IFSSA_R>(n)) {}
00134
00135 RWVerifier(const RWSigner<H> &priv)
00136 : PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(priv.GetTrapdoorFunction()) {}
00137
00138 RWVerifier(BufferedTransformation &bt)
00139 : PublicKeyBaseTemplate<RWFunction<IFSSA_R> >(bt) {}
00140 };
00141
00142 NAMESPACE_END
00143
00144 #endif