Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# rw.h

Go to the documentation of this file.

    00001 #ifndef CRYPTOPP_RW_H
    00002 #define CRYPTOPP_RW_H
    00003
    00004 /** \file
    00005         This file contains classes that implement the
    00006         Rabin-Williams signature schemes as defined in IEEE P1363.
    00007 */
    00008
    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
    00017 //! .
    00018 template <word r>
    00019 class RWFunction : virtual public TrapdoorFunction, public PublicKey
    00020 {
    00021         typedef RWFunction ThisClass;
    00022
    00023 public:
    00024         void Initialize(const Integer &n)
    00025                 {m_n = n;}
    00026
    00027         void BERDecode(BufferedTransformation &bt);
    00028         void DEREncode(BufferedTransformation &bt) const;
    00029
    00030         Integer ApplyFunction(const Integer &x) const;
    00031         Integer PreimageBound() const {return m_n;}
    00032         Integer ImageBound() const {return ++(m_n>>1);}
    00033
    00034         bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
    00035         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
    00036         void AssignFrom(const NameValuePairs &source);
    00037
    00038         const Integer& GetModulus() const {return m_n;}
    00039         void SetModulus(const Integer &n) {m_n = n;}
    00040
    00041 protected:
    00042         Integer m_n;
    00043 };
    00044
    00045 //! .
    00046 template <word r>
    00047 class InvertibleRWFunction : public RWFunction<r>, public TrapdoorFunctionInverse, public PrivateKey
    00048 {
    00049         typedef InvertibleRWFunction ThisClass;
    00050
    00051 public:
    00052         void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u)
    00053                 {m_n = n; m_p = p; m_q = q; m_u = u;}
    00054         // generate a random private key
    00055         void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
    00056                 {GenerateRandomWithKeySize(rng, modulusBits);}
    00057
    00058         void BERDecode(BufferedTransformation &bt);
    00059         void DEREncode(BufferedTransformation &bt) const;
    00060
    00061         Integer CalculateInverse(const Integer &x) const;
    00062
    00063         // GeneratibleCryptoMaterial
    00064         bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
    00065         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
    00066         void AssignFrom(const NameValuePairs &source);
    00067         /*! parameters: (ModulusSize) */
    00068         void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
    00069
    00070         const Integer& GetPrime1() const {return m_p;}
    00071         const Integer& GetPrime2() const {return m_q;}
    00072         const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
    00073
    00074         void SetPrime1(const Integer &p) {m_p = p;}
    00075         void SetPrime2(const Integer &q) {m_q = q;}
    00076         void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
    00077
    00078 protected:
    00079         Integer m_p, m_q, m_u;
    00080 };
    00081
    00082 //! .
    00083 class EMSA2Pad : public PK_PaddingAlgorithm
    00084 {
    00085 public:
    00086         static const char *StaticAlgorithmName() {return "EMSA2";}
    00087
    00088         unsigned int MaxUnpaddedLength(unsigned int paddedLength) const {return (paddedLength+1)/8-2;}
    00089
    00090         void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
    00091         DecodingResult Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
    00092 };
    00093
    00094 //! .
    00095 template <class H>
    00096 class EMSA2DecoratedHashModule : public HashTransformationWithDefaultTruncation
    00097 {
    00098 public:
    00099         EMSA2DecoratedHashModule() : empty(true) {}
    00100         void Update(const byte *input, unsigned int length)
    00101                 {h.Update(input, length); empty = empty && length==0;}
    00102         unsigned int DigestSize() const;
    00103         void Final(byte *digest);
    00104         void Restart() {h.Restart(); empty=true;}
    00105
    00106 private:
    00107         H h;
    00108         bool empty;
    00109 };
    00110
    00111 template <class H> struct EMSA2DigestDecoration
    00112 {
    00113         static const byte decoration;
    00114 };
    00115
    00116 //! EMSA2, for use with RW
    00117 /*! The following hash functions are supported: SHA, RIPEMD160. */
    00118 struct P1363_EMSA2 : public SignatureStandard
    00119 {
    00120         template <class H> struct SignaturePaddingAlgorithm {typedef EMSA2Pad type;};
    00121         template <class H> struct DecoratedHashingAlgorithm {typedef EMSA2DecoratedHashModule<H> type;};
    00122 };
    00123
    00124 template<> struct CryptoStandardTraits<P1363_EMSA2> : public P1363_EMSA2 {};
    00125
    00126 // EMSA2DecoratedHashModule can be instantiated with the following two classes.
    00127 class SHA;
    00128 class RIPEMD160;
    00129
    00130 template <class H>
    00131 void EMSA2DecoratedHashModule<H>::Final(byte *digest)
    00132 {
    00133         digest[0] = empty ? 0x4b : 0x6b;
    00134         h.Final(digest+1);
    00135         digest[DigestSize()-1] = EMSA2DigestDecoration<H>::decoration;
    00136         empty=true;
    00137 }
    00138
    00139 template <class H>
    00140 unsigned int EMSA2DecoratedHashModule<H>::DigestSize() const
    00141 {
    00142         return h.DigestSize() + 2;
    00143 }
    00144
    00145 //! .
    00146 template <word r>
    00147 struct RW
    00148 {
    00149         static std::string StaticAlgorithmName() {return "RW";}
    00150         typedef RWFunction<r> PublicKey;
    00151         typedef InvertibleRWFunction<r> PrivateKey;
    00152 };
    00153
    00154 //! RW
    00155 template <class H, class STANDARD = P1363_EMSA2>
    00156 struct RWSSA : public TF_SSA<STANDARD, H, RW<IFSSA_R> >
    00157 {
    00158 };
    00159
    00160 NAMESPACE_END
    00161
    00162 #endif