1#ifndef CRYPTOPP_RW_H
2#define CRYPTOPP_RW_H
3
4/** \file
5	This file contains classes that implement the
6	Rabin-Williams signature schemes as defined in IEEE P1363.
7*/
8
9#include "pubkey.h"
10
11NAMESPACE_BEGIN(CryptoPP)
12
13//! _
14class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
15{
16	typedef RWFunction ThisClass;
17
18public:
19	void Initialize(const Integer &n)
20		{m_n = n;}
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>>1);}
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	void SetModulus(const Integer &n) {m_n = n;}
35
36protected:
37	Integer m_n;
38};
39
40//! _
41class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
42{
43	typedef InvertibleRWFunction ThisClass;
44
45public:
46	void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u)
47		{m_n = n; m_p = p; m_q = q; m_u = u;}
48	// generate a random private key
49	void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
50		{GenerateRandomWithKeySize(rng, modulusBits);}
51
52	void BERDecode(BufferedTransformation &bt);
53	void DEREncode(BufferedTransformation &bt) const;
54
55	Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
56
57	// GeneratibleCryptoMaterial
58	bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
59	bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
60	void AssignFrom(const NameValuePairs &source);
61	/*! parameters: (ModulusSize) */
62	void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
63
64	const Integer& GetPrime1() const {return m_p;}
65	const Integer& GetPrime2() const {return m_q;}
66	const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
67
68	void SetPrime1(const Integer &p) {m_p = p;}
69	void SetPrime2(const Integer &q) {m_q = q;}
70	void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
71
72protected:
73	Integer m_p, m_q, m_u;
74};
75
76//! RW
77struct RW
78{
79	static std::string StaticAlgorithmName() {return "RW";}
80	typedef RWFunction PublicKey;
81	typedef InvertibleRWFunction PrivateKey;
82};
83
84//! RWSS
85template <class STANDARD, class H>
86struct RWSS : public TF_SS<STANDARD, H, RW>
87{
88};
89
90NAMESPACE_END
91
92#endif
93