Crypto++  5.6.3
Free C++ class library of cryptographic schemes
rw.h
Go to the documentation of this file.
1 // rw.h - written and placed in the public domain by Wei Dai
2 
3 //! \file rw.h
4 //! \brief Classes for Rabin-Williams signature scheme
5 //! \details The implementation provides Rabin-Williams signature schemes as defined in
6 //! IEEE P1363. It uses Bernstein's tweaked square roots in place of square roots to
7 //! speedup calculations.
8 //! \sa <A HREF="http://cr.yp.to/sigs/rwsota-20080131.pdf">RSA signatures and Rabin–Williams
9 //! signatures: the state of the art (20080131)</A>, Section 6, <em>The tweaks e and f</em>.
10 
11 #ifndef CRYPTOPP_RW_H
12 #define CRYPTOPP_RW_H
13 
14 #include "cryptlib.h"
15 #include "pubkey.h"
16 #include "integer.h"
17 
18 NAMESPACE_BEGIN(CryptoPP)
19 
20 //! \class RWFunction
21 //! \brief Rabin-Williams trapdoor function using the public key
22 class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
23 {
24  typedef RWFunction ThisClass;
25 
26 public:
27  void Initialize(const Integer &n)
28  {m_n = n;}
29 
30  void BERDecode(BufferedTransformation &bt);
31  void DEREncode(BufferedTransformation &bt) const;
32 
33  void Save(BufferedTransformation &bt) const
34  {DEREncode(bt);}
36  {BERDecode(bt);}
37 
38  Integer ApplyFunction(const Integer &x) const;
39  Integer PreimageBound() const {return ++(m_n>>1);}
40  Integer ImageBound() const {return m_n;}
41 
42  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
43  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
44  void AssignFrom(const NameValuePairs &source);
45 
46  const Integer& GetModulus() const {return m_n;}
47  void SetModulus(const Integer &n) {m_n = n;}
48 
49 protected:
50  Integer m_n;
51 };
52 
53 //! \class InvertibleRWFunction
54 //! \brief Rabin-Williams trapdoor function using the private key
55 class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
56 {
58 
59 public:
60  InvertibleRWFunction() : m_precompute(false) {}
61 
62  void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
63  // generate a random private key
64  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
65  {GenerateRandomWithKeySize(rng, modulusBits);}
66 
67  void BERDecode(BufferedTransformation &bt);
68  void DEREncode(BufferedTransformation &bt) const;
69 
70  void Save(BufferedTransformation &bt) const
71  {DEREncode(bt);}
73  {BERDecode(bt);}
74 
76 
77  // GeneratibleCryptoMaterial
78  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
79  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
80  void AssignFrom(const NameValuePairs &source);
81  /*! parameters: (ModulusSize) */
83 
84  const Integer& GetPrime1() const {return m_p;}
85  const Integer& GetPrime2() const {return m_q;}
86  const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
87 
88  void SetPrime1(const Integer &p) {m_p = p;}
89  void SetPrime2(const Integer &q) {m_q = q;}
90  void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
91 
92  virtual bool SupportsPrecomputation() const {return true;}
93  virtual void Precompute(unsigned int unused = 0) {PrecomputeTweakedRoots();}
94  virtual void Precompute(unsigned int unused = 0) const {PrecomputeTweakedRoots();}
95 
96  virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation);
97  virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const;
98 
99 protected:
100  void PrecomputeTweakedRoots() const;
101 
102 protected:
103  Integer m_p, m_q, m_u;
104 
105  mutable Integer m_pre_2_9p, m_pre_2_3q, m_pre_q_p;
106  mutable bool m_precompute;
107 };
108 
109 //! \class RW
110 //! \brief Rabin-Williams algorithm
111 struct RW
112 {
113  static std::string StaticAlgorithmName() {return "RW";}
114  typedef RWFunction PublicKey;
116 };
117 
118 //! \class RWSS
119 //! \brief Rabin-Williams signature scheme
120 template <class STANDARD, class H>
121 struct RWSS : public TF_SS<STANDARD, H, RW>
122 {
123 };
124 
125 NAMESPACE_END
126 
127 #endif
virtual void AssignFrom(const NameValuePairs &source)=0
Assign values to this object.
virtual void Precompute(unsigned int precomputationStorage)
Perform precomputation.
Definition: cryptlib.h:2128
Rabin-Williams trapdoor function using the private key.
Definition: rw.h:55
virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
Definition: cryptlib.h:2143
This file contains helper classes/functions for implementing public key algorithms.
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: rw.h:35
Abstract base classes that provide a uniform interface to this library.
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
Interface for random number generators.
Definition: cryptlib.h:1186
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: rw.h:40
Interface for buffered transformations.
Definition: cryptlib.h:1352
virtual Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const =0
Calculates the inverse of an element.
Interface for private keys.
Definition: cryptlib.h:2191
Applies the inverse of the trapdoor function.
Definition: pubkey.h:184
virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
Definition: cryptlib.h:2137
Rabin-Williams trapdoor function using the public key.
Definition: rw.h:22
virtual bool SupportsPrecomputation() const
Determines whether the object supports precomputation.
Definition: rw.h:92
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
Applies the trapdoor function.
Definition: pubkey.h:127
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: rw.h:72
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: rw.h:70
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
Rabin-Williams signature scheme.
Definition: rw.h:121
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random key or crypto parameters.
Definition: cryptlib.h:2171
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Definition: cryptlib.cpp:772
Interface for public keys.
Definition: cryptlib.h:2186
Crypto++ library namespace.
virtual void Precompute(unsigned int unused=0)
Perform precomputation.
Definition: rw.h:93
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rw.h:39
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: rw.h:33
Rabin-Williams algorithm.
Definition: rw.h:111
Interface for retrieving values given their names.
Definition: cryptlib.h:277
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2180