Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:08

0001 // rabin.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file rabin.h

0004 /// \brief Classes for Rabin encryption and signature schemes

0005 
0006 #ifndef CRYPTOPP_RABIN_H
0007 #define CRYPTOPP_RABIN_H
0008 
0009 #include "cryptlib.h"
0010 #include "oaep.h"
0011 #include "pssr.h"
0012 #include "integer.h"
0013 
0014 NAMESPACE_BEGIN(CryptoPP)
0015 
0016 /// \brief Rabin trapdoor function using the public key

0017 /// \since Crypto++ 2.0

0018 class RabinFunction : public TrapdoorFunction, public PublicKey
0019 {
0020     typedef RabinFunction ThisClass;
0021 
0022 public:
0023 
0024     /// \brief Initialize a Rabin public key

0025     /// \param n the modulus

0026     /// \param r element r

0027     /// \param s element s

0028     void Initialize(const Integer &n, const Integer &r, const Integer &s)
0029         {m_n = n; m_r = r; m_s = s;}
0030 
0031     void BERDecode(BufferedTransformation &bt);
0032     void DEREncode(BufferedTransformation &bt) const;
0033 
0034     Integer ApplyFunction(const Integer &x) const;
0035     Integer PreimageBound() const {return m_n;}
0036     Integer ImageBound() const {return m_n;}
0037 
0038     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0039     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0040     void AssignFrom(const NameValuePairs &source);
0041 
0042     const Integer& GetModulus() const {return m_n;}
0043     const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
0044     const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
0045 
0046     void SetModulus(const Integer &n) {m_n = n;}
0047     void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
0048     void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
0049 
0050 protected:
0051     Integer m_n, m_r, m_s;
0052 };
0053 
0054 /// \brief Rabin trapdoor function using the private key

0055 /// \since Crypto++ 2.0

0056 class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
0057 {
0058     typedef InvertibleRabinFunction ThisClass;
0059 
0060 public:
0061 
0062     /// \brief Initialize a Rabin private key

0063     /// \param n modulus

0064     /// \param r element r

0065     /// \param s element s

0066     /// \param p first prime factor

0067     /// \param q second prime factor

0068     /// \param u q<sup>-1</sup> mod p

0069     /// \details This Initialize() function overload initializes a private key from existing parameters.

0070     void Initialize(const Integer &n, const Integer &r, const Integer &s, const Integer &p, const Integer &q, const Integer &u)
0071         {m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
0072 
0073     /// \brief Create a Rabin private key

0074     /// \param rng a RandomNumberGenerator derived class

0075     /// \param keybits the size of the key, in bits

0076     /// \details This function overload of Initialize() creates a new private key because it

0077     ///   takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0078     ///   then use one of the other Initialize() overloads.

0079     void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
0080         {GenerateRandomWithKeySize(rng, keybits);}
0081 
0082     void BERDecode(BufferedTransformation &bt);
0083     void DEREncode(BufferedTransformation &bt) const;
0084 
0085     Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
0086 
0087     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0088     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0089     void AssignFrom(const NameValuePairs &source);
0090     /*! parameters: (ModulusSize) */
0091     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0092 
0093     const Integer& GetPrime1() const {return m_p;}
0094     const Integer& GetPrime2() const {return m_q;}
0095     const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
0096 
0097     void SetPrime1(const Integer &p) {m_p = p;}
0098     void SetPrime2(const Integer &q) {m_q = q;}
0099     void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
0100 
0101 protected:
0102     Integer m_p, m_q, m_u;
0103 };
0104 
0105 /// \brief Rabin keys

0106 struct Rabin
0107 {
0108     static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
0109     typedef RabinFunction PublicKey;
0110     typedef InvertibleRabinFunction PrivateKey;
0111 };
0112 
0113 /// \brief Rabin encryption scheme

0114 /// \tparam STANDARD encryption standard

0115 template <class STANDARD>
0116 struct RabinES : public TF_ES<Rabin, STANDARD>
0117 {
0118 };
0119 
0120 /// \brief Rabin signature scheme

0121 /// \tparam STANDARD signature standard

0122 /// \tparam H hash transformation

0123 template <class STANDARD, class H>
0124 struct RabinSS : public TF_SS<Rabin, STANDARD, H>
0125 {
0126 };
0127 
0128 // More typedefs for backwards compatibility

0129 class SHA1;
0130 typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
0131 typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
0132 
0133 NAMESPACE_END
0134 
0135 #endif