pkcspad.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // pkcspad.h - originally written and placed in the public domain by Wei Dai
  2. /// \file pkcspad.h
  3. /// \brief Classes for PKCS padding schemes
  4. /// \details PKCS #1 v1.5, v2.0 and P1363a allow MD2, MD5, SHA1, SHA224, SHA256, SHA384,
  5. /// SHA512, Tiger and RipeMd-160 to be instantiated.
  6. #ifndef CRYPTOPP_PKCSPAD_H
  7. #define CRYPTOPP_PKCSPAD_H
  8. #include "cryptlib.h"
  9. #include "pubkey.h"
  10. #include "hashfwd.h"
  11. #ifdef CRYPTOPP_IS_DLL
  12. #include "sha.h"
  13. #endif
  14. NAMESPACE_BEGIN(CryptoPP)
  15. /// \brief PKCS #1 v1.5 Encryption Padding Scheme
  16. /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#cem_PKCS1-1.5">EME-PKCS1-v1_5</a>
  17. class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod
  18. {
  19. public:
  20. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EME-PKCS1-v1_5";}
  21. size_t MaxUnpaddedLength(size_t paddedLength) const;
  22. void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs &parameters) const;
  23. DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs &parameters) const;
  24. };
  25. /// \brief PKCS #1 decoration data structure
  26. template <class H> class PKCS_DigestDecoration
  27. {
  28. public:
  29. static const byte decoration[];
  30. static const unsigned int length;
  31. };
  32. // PKCS_DigestDecoration can be instantiated with the following
  33. // classes as specified in PKCS #1 v2.0 and P1363a
  34. // SHA1, SHA224, SHA256, SHA384, SHA512, Tiger, RIPEMD160, MD2, MD5
  35. #if defined(CRYPTOPP_IS_DLL)
  36. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA1>;
  37. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA224>;
  38. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA256>;
  39. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA384>;
  40. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA512>;
  41. // http://github.com/weidai11/cryptopp/issues/517
  42. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_256>;
  43. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_384>;
  44. CRYPTOPP_DLL_TEMPLATE_CLASS PKCS_DigestDecoration<SHA3_512>;
  45. #endif
  46. // https://github.com/weidai11/cryptopp/issues/300 and
  47. // https://github.com/weidai11/cryptopp/issues/533
  48. #if defined(__clang__)
  49. template<> const byte PKCS_DigestDecoration<SHA1>::decoration[];
  50. template<> const unsigned int PKCS_DigestDecoration<SHA1>::length;
  51. template<> const byte PKCS_DigestDecoration<SHA224>::decoration[];
  52. template<> const unsigned int PKCS_DigestDecoration<SHA224>::length;
  53. template<> const byte PKCS_DigestDecoration<SHA256>::decoration[];
  54. template<> const unsigned int PKCS_DigestDecoration<SHA256>::length;
  55. template<> const byte PKCS_DigestDecoration<SHA384>::decoration[];
  56. template<> const unsigned int PKCS_DigestDecoration<SHA384>::length;
  57. template<> const byte PKCS_DigestDecoration<SHA512>::decoration[];
  58. template<> const unsigned int PKCS_DigestDecoration<SHA512>::length;
  59. // http://github.com/weidai11/cryptopp/issues/517
  60. template<> const byte PKCS_DigestDecoration<SHA3_256>::decoration[];
  61. template<> const unsigned int PKCS_DigestDecoration<SHA3_256>::length;
  62. template<> const byte PKCS_DigestDecoration<SHA3_384>::decoration[];
  63. template<> const unsigned int PKCS_DigestDecoration<SHA3_384>::length;
  64. template<> const byte PKCS_DigestDecoration<SHA3_512>::decoration[];
  65. template<> const unsigned int PKCS_DigestDecoration<SHA3_512>::length;
  66. template<> const byte PKCS_DigestDecoration<Weak1::MD2>::decoration[];
  67. template<> const unsigned int PKCS_DigestDecoration<Weak1::MD2>::length;
  68. template<> const byte PKCS_DigestDecoration<Weak1::MD5>::decoration[];
  69. template<> const unsigned int PKCS_DigestDecoration<Weak1::MD5>::length;
  70. #endif
  71. /// \brief PKCS #1 v1.5 Signature Encoding Scheme
  72. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PKCS1-1.5">EMSA-PKCS1-v1_5</a>
  73. class CRYPTOPP_DLL PKCS1v15_SignatureMessageEncodingMethod : public PK_DeterministicSignatureMessageEncodingMethod
  74. {
  75. public:
  76. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "EMSA-PKCS1-v1_5";}
  77. size_t MinRepresentativeBitLength(size_t hashIdentifierSize, size_t digestSize) const
  78. {return 8 * (digestSize + hashIdentifierSize + 10);}
  79. void ComputeMessageRepresentative(RandomNumberGenerator &rng,
  80. const byte *recoverableMessage, size_t recoverableMessageLength,
  81. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  82. byte *representative, size_t representativeBitLength) const;
  83. struct HashIdentifierLookup
  84. {
  85. template <class H> struct HashIdentifierLookup2
  86. {
  87. static HashIdentifier Lookup()
  88. {
  89. return HashIdentifier(PKCS_DigestDecoration<H>::decoration, PKCS_DigestDecoration<H>::length);
  90. }
  91. };
  92. };
  93. };
  94. /// \brief PKCS #1 version 1.5, for use with RSAES and RSASS
  95. /// \dontinclude pkcspad.h
  96. struct PKCS1v15 : public SignatureStandard, public EncryptionStandard
  97. {
  98. typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod;
  99. typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod;
  100. };
  101. NAMESPACE_END
  102. #endif