safer.h 3.0 KB

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