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.

88 lines
2.4 KiB

  1. // safer.h - written and placed in the public domain by Wei Dai
  2. //! \file safer.h
  3. //! \brief Classes for the SAFER block cipher
  4. #ifndef CRYPTOPP_SAFER_H
  5. #define CRYPTOPP_SAFER_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. /// base class, do not use directly
  10. class SAFER
  11. {
  12. public:
  13. class CRYPTOPP_NO_VTABLE Base : public BlockCipher
  14. {
  15. public:
  16. unsigned int OptimalDataAlignment() const {return 1;}
  17. void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs &params);
  18. protected:
  19. virtual bool Strengthened() const =0;
  20. SecByteBlock keySchedule;
  21. static const byte exp_tab[256];
  22. static const byte log_tab[256];
  23. };
  24. class CRYPTOPP_NO_VTABLE Enc : public Base
  25. {
  26. public:
  27. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  28. };
  29. class CRYPTOPP_NO_VTABLE Dec : public Base
  30. {
  31. public:
  32. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  33. };
  34. };
  35. template <class BASE, class INFO, bool STR>
  36. class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE>
  37. {
  38. protected:
  39. bool Strengthened() const {return STR;}
  40. };
  41. //! _
  42. struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
  43. {
  44. static const char *StaticAlgorithmName() {return "SAFER-K";}
  45. };
  46. /// <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-K">SAFER-K</a>
  47. class SAFER_K : public SAFER_K_Info, public SAFER, public BlockCipherDocumentation
  48. {
  49. public:
  50. typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_K_Info, false> > Encryption;
  51. typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_K_Info, false> > Decryption;
  52. };
  53. //! _
  54. struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
  55. {
  56. static const char *StaticAlgorithmName() {return "SAFER-SK";}
  57. };
  58. /// <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-SK">SAFER-SK</a>
  59. class SAFER_SK : public SAFER_SK_Info, public SAFER, public BlockCipherDocumentation
  60. {
  61. public:
  62. typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_SK_Info, true> > Encryption;
  63. typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_SK_Info, true> > Decryption;
  64. };
  65. typedef SAFER_K::Encryption SAFER_K_Encryption;
  66. typedef SAFER_K::Decryption SAFER_K_Decryption;
  67. typedef SAFER_SK::Encryption SAFER_SK_Encryption;
  68. typedef SAFER_SK::Decryption SAFER_SK_Decryption;
  69. NAMESPACE_END
  70. #endif