Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.7 KiB

  1. #ifndef CRYPTOPP_ESIGN_H
  2. #define CRYPTOPP_ESIGN_H
  3. /** \file
  4. This file contains classes that implement the
  5. ESIGN signature schemes as defined in IEEE P1363a.
  6. */
  7. #include "pubkey.h"
  8. #include "integer.h"
  9. #include "asn.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. //! _
  12. class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
  13. {
  14. typedef ESIGNFunction ThisClass;
  15. public:
  16. void Initialize(const Integer &n, const Integer &e)
  17. {m_n = n; m_e = e;}
  18. // PublicKey
  19. void BERDecode(BufferedTransformation &bt);
  20. void DEREncode(BufferedTransformation &bt) const;
  21. // CryptoMaterial
  22. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  23. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  24. void AssignFrom(const NameValuePairs &source);
  25. // TrapdoorFunction
  26. Integer ApplyFunction(const Integer &x) const;
  27. Integer PreimageBound() const {return m_n;}
  28. Integer ImageBound() const {return Integer::Power2(GetK());}
  29. // non-derived
  30. const Integer & GetModulus() const {return m_n;}
  31. const Integer & GetPublicExponent() const {return m_e;}
  32. void SetModulus(const Integer &n) {m_n = n;}
  33. void SetPublicExponent(const Integer &e) {m_e = e;}
  34. protected:
  35. unsigned int GetK() const {return m_n.BitCount()/3-1;}
  36. Integer m_n, m_e;
  37. };
  38. //! _
  39. class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
  40. {
  41. typedef InvertibleESIGNFunction ThisClass;
  42. public:
  43. void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
  44. {m_n = n; m_e = e; m_p = p; m_q = q;}
  45. // generate a random private key
  46. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
  47. {GenerateRandomWithKeySize(rng, modulusBits);}
  48. void BERDecode(BufferedTransformation &bt);
  49. void DEREncode(BufferedTransformation &bt) const;
  50. Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
  51. // GeneratibleCryptoMaterial
  52. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  53. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  54. void AssignFrom(const NameValuePairs &source);
  55. /*! parameters: (ModulusSize) */
  56. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  57. const Integer& GetPrime1() const {return m_p;}
  58. const Integer& GetPrime2() const {return m_q;}
  59. void SetPrime1(const Integer &p) {m_p = p;}
  60. void SetPrime2(const Integer &q) {m_q = q;}
  61. protected:
  62. Integer m_p, m_q;
  63. };
  64. //! _
  65. template <class T>
  66. class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
  67. {
  68. public:
  69. static const char *StaticAlgorithmName() {return "EMSA5";}
  70. void ComputeMessageRepresentative(RandomNumberGenerator &rng,
  71. const byte *recoverableMessage, size_t recoverableMessageLength,
  72. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  73. byte *representative, size_t representativeBitLength) const
  74. {
  75. SecByteBlock digest(hash.DigestSize());
  76. hash.Final(digest);
  77. size_t representativeByteLength = BitsToBytes(representativeBitLength);
  78. T mgf;
  79. mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
  80. if (representativeBitLength % 8 != 0)
  81. representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
  82. }
  83. };
  84. //! EMSA5, for use with ESIGN
  85. struct P1363_EMSA5 : public SignatureStandard
  86. {
  87. typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
  88. };
  89. struct ESIGN_Keys
  90. {
  91. static std::string StaticAlgorithmName() {return "ESIGN";}
  92. typedef ESIGNFunction PublicKey;
  93. typedef InvertibleESIGNFunction PrivateKey;
  94. };
  95. //! ESIGN, as defined in IEEE P1363a
  96. template <class H, class STANDARD = P1363_EMSA5>
  97. struct ESIGN : public TF_SS<STANDARD, H, ESIGN_Keys>
  98. {
  99. };
  100. NAMESPACE_END
  101. #endif