1#ifndef CRYPTOPP_RABIN_H
2#define CRYPTOPP_RABIN_H
3
4/** \file
5*/
6
7#include "oaep.h"
8#include "pssr.h"
9#include "integer.h"
10
11NAMESPACE_BEGIN(CryptoPP)
12
13//! _
14class RabinFunction : public TrapdoorFunction, public PublicKey
15{
16	typedef RabinFunction ThisClass;
17
18public:
19	void Initialize(const Integer &n, const Integer &r, const Integer &s)
20		{m_n = n; m_r = r; m_s = s;}
21
22	void BERDecode(BufferedTransformation &bt);
23	void DEREncode(BufferedTransformation &bt) const;
24
25	Integer ApplyFunction(const Integer &x) const;
26	Integer PreimageBound() const {return m_n;}
27	Integer ImageBound() const {return m_n;}
28
29	bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
30	bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
31	void AssignFrom(const NameValuePairs &source);
32
33	const Integer& GetModulus() const {return m_n;}
34	const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
35	const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
36
37	void SetModulus(const Integer &n) {m_n = n;}
38	void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
39	void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
40
41protected:
42	Integer m_n, m_r, m_s;
43};
44
45//! _
46class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
47{
48	typedef InvertibleRabinFunction ThisClass;
49
50public:
51	void Initialize(const Integer &n, const Integer &r, const Integer &s,
52							const Integer &p, const Integer &q, const Integer &u)
53		{m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
54	void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
55		{GenerateRandomWithKeySize(rng, keybits);}
56
57	void BERDecode(BufferedTransformation &bt);
58	void DEREncode(BufferedTransformation &bt) const;
59
60	Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
61
62	bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
63	bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
64	void AssignFrom(const NameValuePairs &source);
65	/*! parameters: (ModulusSize) */
66	void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
67
68	const Integer& GetPrime1() const {return m_p;}
69	const Integer& GetPrime2() const {return m_q;}
70	const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
71
72	void SetPrime1(const Integer &p) {m_p = p;}
73	void SetPrime2(const Integer &q) {m_q = q;}
74	void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
75
76protected:
77	Integer m_p, m_q, m_u;
78};
79
80//! Rabin
81struct Rabin
82{
83	static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
84	typedef RabinFunction PublicKey;
85	typedef InvertibleRabinFunction PrivateKey;
86};
87
88//! Rabin encryption
89template <class STANDARD>
90struct RabinES : public TF_ES<STANDARD, Rabin>
91{
92};
93
94//! Rabin signature
95template <class STANDARD, class H>
96struct RabinSS : public TF_SS<STANDARD, H, Rabin>
97{
98};
99
100// More typedefs for backwards compatibility
101class SHA1;
102typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
103typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
104
105NAMESPACE_END
106
107#endif
108