Team Fortress 2 Source Code as on 22/4/2020
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.

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