cast.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // cast.h - originally written and placed in the public domain by Wei Dai
  2. /// \file cast.h
  3. /// \brief Classes for the CAST-128 and CAST-256 block ciphers
  4. /// \since Crypto++ 2.2
  5. #ifndef CRYPTOPP_CAST_H
  6. #define CRYPTOPP_CAST_H
  7. #include "seckey.h"
  8. #include "secblock.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief CAST block cipher base
  11. /// \since Crypto++ 2.2
  12. class CAST
  13. {
  14. protected:
  15. static const word32 S[8][256];
  16. };
  17. /// \brief CAST128 block cipher information
  18. /// \since Crypto++ 2.2
  19. struct CAST128_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 5, 16>
  20. {
  21. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CAST-128";}
  22. };
  23. /// \brief CAST128 block cipher
  24. /// \sa <a href="http://www.cryptopp.com/wiki/CAST-128">CAST-128</a>
  25. /// \since Crypto++ 2.2
  26. class CAST128 : public CAST128_Info, public BlockCipherDocumentation
  27. {
  28. /// \brief CAST128 block cipher default operation
  29. class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST128_Info>
  30. {
  31. public:
  32. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  33. protected:
  34. bool reduced;
  35. FixedSizeSecBlock<word32, 32> K;
  36. mutable FixedSizeSecBlock<word32, 3> m_t;
  37. };
  38. /// \brief CAST128 block cipher encryption operation
  39. class CRYPTOPP_NO_VTABLE Enc : public Base
  40. {
  41. public:
  42. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  43. };
  44. /// \brief CAST128 block cipher decryption operation
  45. class CRYPTOPP_NO_VTABLE Dec : public Base
  46. {
  47. public:
  48. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  49. };
  50. public:
  51. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  52. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  53. };
  54. /// \brief CAST256 block cipher information
  55. /// \since Crypto++ 4.0
  56. struct CAST256_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 4>
  57. {
  58. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CAST-256";}
  59. };
  60. /// \brief CAST256 block cipher
  61. /// \sa <a href="http://www.cryptopp.com/wiki/CAST-256">CAST-256</a>
  62. /// \since Crypto++ 4.0
  63. class CAST256 : public CAST256_Info, public BlockCipherDocumentation
  64. {
  65. /// \brief CAST256 block cipher default operation
  66. class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST256_Info>
  67. {
  68. public:
  69. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  70. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  71. protected:
  72. static const word32 t_m[8][24];
  73. static const unsigned int t_r[8][24];
  74. static void Omega(int i, word32 kappa[8]);
  75. FixedSizeSecBlock<word32, 8*12> K;
  76. mutable FixedSizeSecBlock<word32, 8> kappa;
  77. mutable FixedSizeSecBlock<word32, 3> m_t;
  78. };
  79. public:
  80. typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
  81. typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
  82. };
  83. typedef CAST128::Encryption CAST128Encryption;
  84. typedef CAST128::Decryption CAST128Decryption;
  85. typedef CAST256::Encryption CAST256Encryption;
  86. typedef CAST256::Decryption CAST256Decryption;
  87. NAMESPACE_END
  88. #endif