source: git/src-cryptopp/rw.h

Last change on this file was e230cb0, checked in by David Stainton <dstainton415@…>, at 2016-10-12T13:27:29Z

Add cryptopp from tag CRYPTOPP_5_6_5

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
18NAMESPACE_BEGIN(CryptoPP)
19
20//! \class RWFunction
21//! \brief Rabin-Williams trapdoor function using the public key
22class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
23{
24        typedef RWFunction ThisClass;
25
26public:
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);}
35        void Load(BufferedTransformation &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
49protected:
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
56class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
57{
58        typedef InvertibleRWFunction ThisClass;
59
60public:
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);}
73        void Load(BufferedTransformation &bt)
74                {BERDecode(bt);}
75
76        Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
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) */
83        void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
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
100protected:
101        void PrecomputeTweakedRoots() const;
102
103protected:
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
112struct RW
113{
114        static std::string StaticAlgorithmName() {return "RW";}
115        typedef RWFunction PublicKey;
116        typedef InvertibleRWFunction PrivateKey;
117};
118
119//! \class RWSS
120//! \brief Rabin-Williams signature scheme
121template <class STANDARD, class H>
122struct RWSS : public TF_SS<STANDARD, H, RW>
123{
124};
125
126NAMESPACE_END
127
128#endif
Note: See TracBrowser for help on using the repository browser.