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
NAMESPACE_BEGIN(CryptoPP)14 NAMESPACE_BEGIN(CryptoPP)
15
16 //! \class RabinFunction
17 //! \brief Rabin trapdoor function using the public key
18 //! \since Crypto++ 2.0
19 class RabinFunction : public TrapdoorFunction, public PublicKey
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
58 class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
59 {
60 typedef InvertibleRabinFunction ThisClass;
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.
Initialize(const Integer & n,const Integer & r,const Integer & s,const Integer & p,const Integer & q,const Integer & u)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.
Initialize(RandomNumberGenerator & rng,unsigned int keybits)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
87 Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
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) */
93 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
94
GetPrime1()95 const Integer& GetPrime1() const {return m_p;}
GetPrime2()96 const Integer& GetPrime2() const {return m_q;}
GetMultiplicativeInverseOfPrime2ModPrime1()97 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
98
SetPrime1(const Integer & p)99 void SetPrime1(const Integer &p) {m_p = p;}
SetPrime2(const Integer & q)100 void SetPrime2(const Integer &q) {m_q = q;}
SetMultiplicativeInverseOfPrime2ModPrime1(const Integer & u)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 {
StaticAlgorithmNameRabin110 static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
111 typedef RabinFunction PublicKey;
112 typedef InvertibleRabinFunction PrivateKey;
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
138