Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/cryptopp/rw.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // rw.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file rw.h

0004 /// \brief Classes for Rabin-Williams signature scheme

0005 /// \details The implementation provides Rabin-Williams signature schemes as defined in

0006 ///   IEEE P1363. It uses Bernstein's tweaked square roots in place of square roots to

0007 ///   speedup calculations.

0008 /// \sa <A HREF="http://cr.yp.to/sigs/rwsota-20080131.pdf">RSA signatures and Rabin–Williams

0009 ///   signatures: the state of the art (20080131)</A>, Section 6, <em>The tweaks e and f</em>.

0010 /// \since Crypto++ 3.0

0011 
0012 #ifndef CRYPTOPP_RW_H
0013 #define CRYPTOPP_RW_H
0014 
0015 #include "cryptlib.h"
0016 #include "pubkey.h"
0017 #include "integer.h"
0018 
0019 NAMESPACE_BEGIN(CryptoPP)
0020 
0021 /// \brief Rabin-Williams trapdoor function using the public key

0022 /// \since Crypto++ 3.0, Tweaked roots using <em>e</em> and <em>f</em> since Crypto++ 5.6.4

0023 class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
0024 {
0025     typedef RWFunction ThisClass;
0026 
0027 public:
0028 
0029     /// \brief Initialize a Rabin-Williams public key

0030     /// \param n the modulus

0031     void Initialize(const Integer &n)
0032         {m_n = n;}
0033 
0034     void BERDecode(BufferedTransformation &bt);
0035     void DEREncode(BufferedTransformation &bt) const;
0036 
0037     void Save(BufferedTransformation &bt) const
0038         {DEREncode(bt);}
0039     void Load(BufferedTransformation &bt)
0040         {BERDecode(bt);}
0041 
0042     Integer ApplyFunction(const Integer &x) const;
0043     Integer PreimageBound() const {return ++(m_n>>1);}
0044     Integer ImageBound() const {return m_n;}
0045 
0046     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0047     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0048     void AssignFrom(const NameValuePairs &source);
0049 
0050     const Integer& GetModulus() const {return m_n;}
0051     void SetModulus(const Integer &n) {m_n = n;}
0052 
0053 protected:
0054     Integer m_n;
0055 };
0056 
0057 /// \brief Rabin-Williams trapdoor function using the private key

0058 /// \since Crypto++ 3.0, Tweaked roots using <em>e</em> and <em>f</em> since Crypto++ 5.6.4

0059 class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
0060 {
0061     typedef InvertibleRWFunction ThisClass;
0062 
0063 public:
0064     /// \brief Construct an InvertibleRWFunction

0065     InvertibleRWFunction() : m_precompute(false) {}
0066 
0067     /// \brief Initialize a Rabin-Williams private key

0068     /// \param n modulus

0069     /// \param p first prime factor

0070     /// \param q second prime factor

0071     /// \param u q<sup>-1</sup> mod p

0072     /// \details This Initialize() function overload initializes a private key from existing parameters.

0073     void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
0074 
0075     /// \brief Create a Rabin-Williams private key

0076     /// \param rng a RandomNumberGenerator derived class

0077     /// \param modulusBits the size of the modulus, in bits

0078     /// \details This function overload of Initialize() creates a new private key because it

0079     ///   takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0080     ///   then use one of the other Initialize() overloads.

0081     void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
0082         {GenerateRandomWithKeySize(rng, modulusBits);}
0083 
0084     void BERDecode(BufferedTransformation &bt);
0085     void DEREncode(BufferedTransformation &bt) const;
0086 
0087     void Save(BufferedTransformation &bt) const
0088         {DEREncode(bt);}
0089     void Load(BufferedTransformation &bt)
0090         {BERDecode(bt);}
0091 
0092     Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0093 
0094     // GeneratibleCryptoMaterial

0095     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0096     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0097     void AssignFrom(const NameValuePairs &source);
0098     /*! parameters: (ModulusSize) */
0099     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0100 
0101     const Integer& GetPrime1() const {return m_p;}
0102     const Integer& GetPrime2() const {return m_q;}
0103     const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
0104 
0105     void SetPrime1(const Integer &p) {m_p = p;}
0106     void SetPrime2(const Integer &q) {m_q = q;}
0107     void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
0108 
0109     virtual bool SupportsPrecomputation() const {return true;}
0110     virtual void Precompute(unsigned int unused = 0) {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
0111     virtual void Precompute(unsigned int unused = 0) const {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
0112 
0113     virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation);
0114     virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const;
0115 
0116 protected:
0117     void PrecomputeTweakedRoots() const;
0118 
0119 protected:
0120     Integer m_p, m_q, m_u;
0121 
0122     mutable Integer m_pre_2_9p, m_pre_2_3q, m_pre_q_p;
0123     mutable bool m_precompute;
0124 };
0125 
0126 /// \brief Rabin-Williams keys

0127 /// \since Crypto++ 3.0

0128 struct RW
0129 {
0130     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "RW";}
0131     typedef RWFunction PublicKey;
0132     typedef InvertibleRWFunction PrivateKey;
0133 };
0134 
0135 /// \brief Rabin-Williams signature scheme

0136 /// \tparam STANDARD signature standard

0137 /// \tparam H hash transformation

0138 /// \since Crypto++ 3.0

0139 template <class STANDARD, class H>
0140 struct RWSS : public TF_SS<RW, STANDARD, H>
0141 {
0142 };
0143 
0144 NAMESPACE_END
0145 
0146 #endif