File indexing completed on 2025-01-18 09:55:08
0001
0002
0003
0004
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
0017
0018 class RabinFunction : public TrapdoorFunction, public PublicKey
0019 {
0020 typedef RabinFunction ThisClass;
0021
0022 public:
0023
0024
0025
0026
0027
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
0055
0056 class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
0057 {
0058 typedef InvertibleRabinFunction ThisClass;
0059
0060 public:
0061
0062
0063
0064
0065
0066
0067
0068
0069
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
0074
0075
0076
0077
0078
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
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
0106 struct Rabin
0107 {
0108 static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
0109 typedef RabinFunction PublicKey;
0110 typedef InvertibleRabinFunction PrivateKey;
0111 };
0112
0113
0114
0115 template <class STANDARD>
0116 struct RabinES : public TF_ES<Rabin, STANDARD>
0117 {
0118 };
0119
0120
0121
0122
0123 template <class STANDARD, class H>
0124 struct RabinSS : public TF_SS<Rabin, STANDARD, H>
0125 {
0126 };
0127
0128
0129 class SHA1;
0130 typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
0131 typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
0132
0133 NAMESPACE_END
0134
0135 #endif