oaep.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // oaep.h - originally written and placed in the public domain by Wei Dai
  2. /// \file oaep.h
  3. /// \brief Classes for optimal asymmetric encryption padding
  4. /// \since Crypto++ 2.1
  5. #ifndef CRYPTOPP_OAEP_H
  6. #define CRYPTOPP_OAEP_H
  7. #include "cryptlib.h"
  8. #include "pubkey.h"
  9. #include "sha.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief OAEP padding base class
  12. /// \since Crypto++ 2.1
  13. class CRYPTOPP_DLL OAEP_Base : public PK_EncryptionMessageEncodingMethod
  14. {
  15. public:
  16. bool ParameterSupported(const char *name) const {return strcmp(name, Name::EncodingParameters()) == 0;}
  17. size_t MaxUnpaddedLength(size_t paddedLength) const;
  18. void Pad(RandomNumberGenerator &rng, const byte *raw, size_t inputLength, byte *padded, size_t paddedLength, const NameValuePairs &parameters) const;
  19. DecodingResult Unpad(const byte *padded, size_t paddedLength, byte *raw, const NameValuePairs &parameters) const;
  20. protected:
  21. virtual unsigned int DigestSize() const =0;
  22. virtual HashTransformation * NewHash() const =0;
  23. virtual MaskGeneratingFunction * NewMGF() const =0;
  24. };
  25. /// \brief OAEP padding
  26. /// \tparam H HashTransformation derived class
  27. /// \tparam MGF MaskGeneratingFunction derived class
  28. /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#cem_OAEP-MGF1">EME-OAEP</a>, for use with classes derived from TF_ES
  29. /// \since Crypto++ 2.1
  30. template <class H, class MGF=P1363_MGF1>
  31. class OAEP : public OAEP_Base, public EncryptionStandard
  32. {
  33. public:
  34. static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("OAEP-") + MGF::StaticAlgorithmName() + "(" + H::StaticAlgorithmName() + ")";}
  35. typedef OAEP<H, MGF> EncryptionMessageEncodingMethod;
  36. protected:
  37. unsigned int DigestSize() const {return H::DIGESTSIZE;}
  38. HashTransformation * NewHash() const {return new H;}
  39. MaskGeneratingFunction * NewMGF() const {return new MGF;}
  40. };
  41. CRYPTOPP_DLL_TEMPLATE_CLASS OAEP<SHA1>;
  42. NAMESPACE_END
  43. #endif