Crypto++  5.6.5
Free C++ class library of cryptographic schemes
rabin.h
Go to the documentation of this file.
1 // rabin.h - written and placed in the public domain by Wei Dai
2 
3 //! \file rabin.h
4 //! \brief Classes for Rabin encryption and signature schemes
5 
6 #ifndef CRYPTOPP_RABIN_H
7 #define CRYPTOPP_RABIN_H
8 
9 #include "cryptlib.h"
10 #include "oaep.h"
11 #include "pssr.h"
12 #include "integer.h"
13 
14 NAMESPACE_BEGIN(CryptoPP)
15 
16 //! \class RabinFunction
17 //! \brief Rabin trapdoor function using the public key
18 //! \since Crypto++ 2.0
20 {
21  typedef RabinFunction ThisClass;
22 
23 public:
24 
25  //! \brief Initialize a Rabin public key
26  //! \param n the modulus
27  //! \param r element r
28  //! \param s element s
29  void Initialize(const Integer &n, const Integer &r, const Integer &s)
30  {m_n = n; m_r = r; m_s = s;}
31 
32  void BERDecode(BufferedTransformation &bt);
33  void DEREncode(BufferedTransformation &bt) const;
34 
35  Integer ApplyFunction(const Integer &x) const;
36  Integer PreimageBound() const {return m_n;}
37  Integer ImageBound() const {return m_n;}
38 
39  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
40  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
41  void AssignFrom(const NameValuePairs &source);
42 
43  const Integer& GetModulus() const {return m_n;}
44  const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
45  const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
46 
47  void SetModulus(const Integer &n) {m_n = n;}
48  void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
49  void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
50 
51 protected:
52  Integer m_n, m_r, m_s;
53 };
54 
55 //! \class InvertibleRabinFunction
56 //! \brief Rabin trapdoor function using the private key
57 //! \since Crypto++ 2.0
59 {
61 
62 public:
63 
64  //! \brief Initialize a Rabin private key
65  //! \param n modulus
66  //! \param r element r
67  //! \param s element s
68  //! \param p first prime factor
69  //! \param q second prime factor
70  //! \param u q<sup>-1</sup> mod p
71  //! \details This Initialize() function overload initializes a private key from existing parameters.
72  void Initialize(const Integer &n, const Integer &r, const Integer &s, const Integer &p, const Integer &q, const Integer &u)
73  {m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
74 
75  //! \brief Create a Rabin private key
76  //! \param rng a RandomNumberGenerator derived class
77  //! \param keybits the size of the key, in bits
78  //! \details This function overload of Initialize() creates a new private key because it
79  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
80  //! then use one of the other Initialize() overloads.
81  void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
82  {GenerateRandomWithKeySize(rng, keybits);}
83 
84  void BERDecode(BufferedTransformation &bt);
85  void DEREncode(BufferedTransformation &bt) const;
86 
88 
89  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
90  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
91  void AssignFrom(const NameValuePairs &source);
92  /*! parameters: (ModulusSize) */
94 
95  const Integer& GetPrime1() const {return m_p;}
96  const Integer& GetPrime2() const {return m_q;}
97  const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
98 
99  void SetPrime1(const Integer &p) {m_p = p;}
100  void SetPrime2(const Integer &q) {m_q = q;}
101  void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
102 
103 protected:
104  Integer m_p, m_q, m_u;
105 };
106 
107 //! \brief Rabin keys
108 struct Rabin
109 {
110  static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
111  typedef RabinFunction PublicKey;
113 };
114 
115 //! \brief Rabin encryption scheme
116 //! \tparam STANDARD encryption standard
117 template <class STANDARD>
118 struct RabinES : public TF_ES<Rabin, STANDARD>
119 {
120 };
121 
122 //! \brief Rabin signature scheme
123 //! \tparam STANDARD signature standard
124 //! \tparam H hash transformation
125 template <class STANDARD, class H>
126 struct RabinSS : public TF_SS<Rabin, STANDARD, H>
127 {
128 };
129 
130 // More typedefs for backwards compatibility
131 class SHA1;
132 typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
133 typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
134 
135 NAMESPACE_END
136 
137 #endif
Trapdoor Function (TF) encryption scheme.
Definition: pubkey.h:2043
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rabin.cpp:141
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rabin.h:36
Abstract base classes that provide a uniform interface to this library.
Interface for random number generators.
Definition: cryptlib.h:1188
Interface for buffered transformations.
Definition: cryptlib.h:1352
Interface for private keys.
Definition: cryptlib.h:2186
Rabin signature scheme.
Definition: rabin.h:126
Rabin trapdoor function using the public key.
Definition: rabin.h:19
Rabin encryption scheme.
Definition: rabin.h:118
Applies the inverse of the trapdoor function.
Definition: pubkey.h:183
Rabin keys.
Definition: rabin.h:108
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
SHA-1 message digest.
Definition: sha.h:25
Applies the trapdoor function.
Definition: pubkey.h:128
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: rabin.h:37
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: rabin.cpp:76
void Initialize(const Integer &n, const Integer &r, const Integer &s, const Integer &p, const Integer &q, const Integer &u)
Initialize a Rabin private key.
Definition: rabin.h:72
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rabin.cpp:213
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Definition: cryptlib.cpp:780
Multiple precision integer with arithmetic operations.
Interface for public keys.
Definition: cryptlib.h:2181
Crypto++ library namespace.
Rabin trapdoor function using the private key.
Definition: rabin.h:58
Classes for probablistic signature schemes.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rabin.cpp:184
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rabin.cpp:204
void Initialize(const Integer &n, const Integer &r, const Integer &s)
Initialize a Rabin public key.
Definition: rabin.h:29
void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
Create a Rabin private key.
Definition: rabin.h:81
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:1954
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2070