emsa2.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // emsa2.h - originally written and placed in the public domain by Wei Dai
  2. /// \file emsa2.h
  3. /// \brief Classes and functions for various padding schemes used in public key algorithms
  4. #ifndef CRYPTOPP_EMSA2_H
  5. #define CRYPTOPP_EMSA2_H
  6. #include "cryptlib.h"
  7. #include "pubkey.h"
  8. #include "hashfwd.h"
  9. #include "misc.h"
  10. #ifdef CRYPTOPP_IS_DLL
  11. # include "sha.h"
  12. #endif
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief EMSA2 hash identifier
  15. /// \tparam H HashTransformation derived class
  16. /// \since Crypto++ 5.0
  17. template <class H> class EMSA2HashId
  18. {
  19. public:
  20. static const byte id;
  21. };
  22. /// \brief EMSA2 padding method
  23. /// \tparam BASE Message encoding method
  24. /// \since Crypto++ 5.0
  25. template <class BASE>
  26. class EMSA2HashIdLookup : public BASE
  27. {
  28. public:
  29. struct HashIdentifierLookup
  30. {
  31. template <class H> struct HashIdentifierLookup2
  32. {
  33. static HashIdentifier Lookup()
  34. {
  35. return HashIdentifier(&EMSA2HashId<H>::id, 1);
  36. }
  37. };
  38. };
  39. };
  40. // EMSA2HashId can be instantiated with the following classes.
  41. // SHA1, SHA224, SHA256, SHA384, SHA512, RIPEMD128, RIPEMD160, Whirlpool
  42. #ifdef CRYPTOPP_IS_DLL
  43. CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId<SHA1>;
  44. CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId<SHA224>;
  45. CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId<SHA256>;
  46. CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId<SHA384>;
  47. CRYPTOPP_DLL_TEMPLATE_CLASS EMSA2HashId<SHA512>;
  48. #endif
  49. // https://github.com/weidai11/cryptopp/issues/300 and
  50. // https://github.com/weidai11/cryptopp/issues/533
  51. #if defined(__clang__)
  52. template<> const byte EMSA2HashId<SHA1>::id;
  53. template<> const byte EMSA2HashId<SHA224>::id;
  54. template<> const byte EMSA2HashId<SHA256>::id;
  55. template<> const byte EMSA2HashId<SHA384>::id;
  56. template<> const byte EMSA2HashId<SHA512>::id;
  57. #endif
  58. /// \brief EMSA2 padding method
  59. /// \since Crypto++ 5.0
  60. class CRYPTOPP_DLL EMSA2Pad : public EMSA2HashIdLookup<PK_DeterministicSignatureMessageEncodingMethod>
  61. {
  62. public:
  63. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "EMSA2";}
  64. size_t MinRepresentativeBitLength(size_t hashIdentifierLength, size_t digestLength) const
  65. {CRYPTOPP_UNUSED(hashIdentifierLength); return 8*digestLength + 31;}
  66. void ComputeMessageRepresentative(RandomNumberGenerator &rng,
  67. const byte *recoverableMessage, size_t recoverableMessageLength,
  68. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  69. byte *representative, size_t representativeBitLength) const;
  70. };
  71. // EMSA2, for use with RWSS and RSA_ISO
  72. // Only the following hash functions are supported by this signature standard:
  73. // \dontinclude emsa2.h
  74. // \skip EMSA2HashId can be instantiated
  75. // \until end of list
  76. /// \brief EMSA2/P1363 padding method
  77. /// \details Use with RWSS and RSA_ISO
  78. /// \since Crypto++ 5.0
  79. struct P1363_EMSA2 : public SignatureStandard
  80. {
  81. typedef EMSA2Pad SignatureMessageEncodingMethod;
  82. };
  83. NAMESPACE_END
  84. #endif