Crypto++  5.6.5 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 //! \since Tweaked roots using <em>e</em> and <em>f</em> since Crypto++ 5.6.4
56 class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
57 {
59
60 public:
61  InvertibleRWFunction() : m_precompute(false) {}
62
63  void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
64  // generate a random private key
65  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
66  {GenerateRandomWithKeySize(rng, modulusBits);}
67
68  void BERDecode(BufferedTransformation &bt);
69  void DEREncode(BufferedTransformation &bt) const;
70
71  void Save(BufferedTransformation &bt) const
72  {DEREncode(bt);}
74  {BERDecode(bt);}
75
77
78  // GeneratibleCryptoMaterial
79  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
80  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
81  void AssignFrom(const NameValuePairs &source);
82  /*! parameters: (ModulusSize) */
84
85  const Integer& GetPrime1() const {return m_p;}
86  const Integer& GetPrime2() const {return m_q;}
87  const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
88
89  void SetPrime1(const Integer &p) {m_p = p;}
90  void SetPrime2(const Integer &q) {m_q = q;}
91  void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
92
93  virtual bool SupportsPrecomputation() const {return true;}
94  virtual void Precompute(unsigned int unused = 0) {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
95  virtual void Precompute(unsigned int unused = 0) const {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
96
97  virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation);
98  virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const;
99
100 protected:
101  void PrecomputeTweakedRoots() const;
102
103 protected:
104  Integer m_p, m_q, m_u;
105
106  mutable Integer m_pre_2_9p, m_pre_2_3q, m_pre_q_p;
107  mutable bool m_precompute;
108 };
109
110 //! \class RW
111 //! \brief Rabin-Williams algorithm
112 struct RW
113 {
114  static std::string StaticAlgorithmName() {return "RW";}
115  typedef RWFunction PublicKey;
117 };
118
119 //! \class RWSS
120 //! \brief Rabin-Williams signature scheme
121 template <class STANDARD, class H>
122 struct RWSS : public TF_SS<STANDARD, H, RW>
123 {
124 };
125
126 NAMESPACE_END
127
128 #endif
virtual void AssignFrom(const NameValuePairs &source)=0
Assign values to this object.
virtual void Precompute(unsigned int precomputationStorage)
Perform precomputation.
Definition: cryptlib.h:2144
Rabin-Williams trapdoor function using the private key.
Definition: rw.h:56
virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
Definition: cryptlib.h:2159
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:1201
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:1367
virtual Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const =0
Calculates the inverse of an element.
Interface for private keys.
Definition: cryptlib.h:2207
Applies the inverse of the trapdoor function.
Definition: pubkey.h:190
virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
Definition: cryptlib.h:2153
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:93
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
Applies the trapdoor function.
Definition: pubkey.h:133
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: rw.h:73
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: rw.h:71
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
Rabin-Williams signature scheme.
Definition: rw.h:122
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random key or crypto parameters.
Definition: cryptlib.h:2187
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Definition: cryptlib.cpp:768
Multiple precision integer with arithmetic operations.
Interface for public keys.
Definition: cryptlib.h:2202
Crypto++ library namespace.
virtual void Precompute(unsigned int unused=0)
Perform precomputation.
Definition: rw.h:94
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:112
Interface for retrieving values given their names.
Definition: cryptlib.h:282
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2188