esign.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // esign.h - originally written and placed in the public domain by Wei Dai
  2. /// \file esign.h
  3. /// \brief Classes providing ESIGN signature schemes as defined in IEEE P1363a
  4. /// \since Crypto++ 5.0
  5. #ifndef CRYPTOPP_ESIGN_H
  6. #define CRYPTOPP_ESIGN_H
  7. #include "cryptlib.h"
  8. #include "pubkey.h"
  9. #include "integer.h"
  10. #include "asn.h"
  11. #include "misc.h"
  12. NAMESPACE_BEGIN(CryptoPP)
  13. /// \brief ESIGN trapdoor function using the public key
  14. /// \since Crypto++ 5.0
  15. class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
  16. {
  17. typedef ESIGNFunction ThisClass;
  18. public:
  19. /// \brief Initialize a ESIGN public key with {n,e}
  20. /// \param n the modulus
  21. /// \param e the public exponent
  22. void Initialize(const Integer &n, const Integer &e)
  23. {m_n = n; m_e = e;}
  24. // PublicKey
  25. void BERDecode(BufferedTransformation &bt);
  26. void DEREncode(BufferedTransformation &bt) const;
  27. // CryptoMaterial
  28. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  29. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  30. void AssignFrom(const NameValuePairs &source);
  31. // TrapdoorFunction
  32. Integer ApplyFunction(const Integer &x) const;
  33. Integer PreimageBound() const {return m_n;}
  34. Integer ImageBound() const {return Integer::Power2(GetK());}
  35. // non-derived
  36. const Integer & GetModulus() const {return m_n;}
  37. const Integer & GetPublicExponent() const {return m_e;}
  38. void SetModulus(const Integer &n) {m_n = n;}
  39. void SetPublicExponent(const Integer &e) {m_e = e;}
  40. protected:
  41. // Covertiy finding on overflow. The library allows small values for research purposes.
  42. unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
  43. Integer m_n, m_e;
  44. };
  45. /// \brief ESIGN trapdoor function using the private key
  46. /// \since Crypto++ 5.0
  47. class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
  48. {
  49. typedef InvertibleESIGNFunction ThisClass;
  50. public:
  51. /// \brief Initialize a ESIGN private key with {n,e,p,q}
  52. /// \param n modulus
  53. /// \param e public exponent
  54. /// \param p first prime factor
  55. /// \param q second prime factor
  56. /// \details This Initialize() function overload initializes a private key from existing parameters.
  57. void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
  58. {m_n = n; m_e = e; m_p = p; m_q = q;}
  59. /// \brief Create a ESIGN private key
  60. /// \param rng a RandomNumberGenerator derived class
  61. /// \param modulusBits the size of the modulud, in bits
  62. /// \details This function overload of Initialize() creates a new private key because it
  63. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  64. /// then use one of the other Initialize() overloads.
  65. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
  66. {GenerateRandomWithKeySize(rng, modulusBits);}
  67. // Squash Visual Studio C4250 warning
  68. void Save(BufferedTransformation &bt) const
  69. {BEREncode(bt);}
  70. // Squash Visual Studio C4250 warning
  71. void Load(BufferedTransformation &bt)
  72. {BERDecode(bt);}
  73. void BERDecode(BufferedTransformation &bt);
  74. void DEREncode(BufferedTransformation &bt) const;
  75. Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
  76. // GeneratibleCryptoMaterial
  77. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  78. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  79. void AssignFrom(const NameValuePairs &source);
  80. /*! parameters: (ModulusSize) */
  81. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  82. const Integer& GetPrime1() const {return m_p;}
  83. const Integer& GetPrime2() const {return m_q;}
  84. void SetPrime1(const Integer &p) {m_p = p;}
  85. void SetPrime2(const Integer &q) {m_q = q;}
  86. protected:
  87. Integer m_p, m_q;
  88. };
  89. /// \brief EMSA5 padding method
  90. /// \tparam T Mask Generation Function
  91. /// \since Crypto++ 5.0
  92. template <class T>
  93. class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
  94. {
  95. public:
  96. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
  97. void ComputeMessageRepresentative(RandomNumberGenerator &rng,
  98. const byte *recoverableMessage, size_t recoverableMessageLength,
  99. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  100. byte *representative, size_t representativeBitLength) const
  101. {
  102. CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
  103. CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
  104. SecByteBlock digest(hash.DigestSize());
  105. hash.Final(digest);
  106. size_t representativeByteLength = BitsToBytes(representativeBitLength);
  107. T mgf;
  108. mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
  109. if (representativeBitLength % 8 != 0)
  110. representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
  111. }
  112. };
  113. /// \brief EMSA5 padding method, for use with ESIGN
  114. /// \since Crypto++ 5.0
  115. struct P1363_EMSA5 : public SignatureStandard
  116. {
  117. typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
  118. };
  119. /// \brief ESIGN keys
  120. /// \since Crypto++ 5.0
  121. struct ESIGN_Keys
  122. {
  123. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
  124. typedef ESIGNFunction PublicKey;
  125. typedef InvertibleESIGNFunction PrivateKey;
  126. };
  127. /// \brief ESIGN signature scheme, IEEE P1363a
  128. /// \tparam H HashTransformation derived class
  129. /// \tparam STANDARD Signature encoding method
  130. /// \since Crypto++ 5.0
  131. template <class H, class STANDARD = P1363_EMSA5>
  132. struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
  133. {
  134. };
  135. NAMESPACE_END
  136. #endif