rabbit.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // rabbit.h - written and placed in the public domain by Jeffrey Walton
  2. // based on public domain code by Martin Boesgaard, Mette Vesterager,
  3. // Thomas Pedersen, Jesper Christiansen and Ove Scavenius.
  4. //
  5. // The reference materials and source files are available at
  6. // The eSTREAM Project, http://www.ecrypt.eu.org/stream/e2-rabbit.html.
  7. /// \file rabbit.h
  8. /// \brief Classes for Rabbit stream cipher
  9. /// \sa <A HREF="http://www.ecrypt.eu.org/stream/e2-rabbit.html">The
  10. /// eSTREAM Project | Rabbit</A> and
  11. /// <A HREF="https://www.cryptopp.com/wiki/Rabbit">Crypto++ Wiki | Rabbit</A>.
  12. /// \since Crypto++ 8.0
  13. #ifndef CRYPTOPP_RABBIT_H
  14. #define CRYPTOPP_RABBIT_H
  15. #include "strciphr.h"
  16. #include "secblock.h"
  17. // The library does not have a way to describe an optional IV. Rabbit takes
  18. // an optional IV so two classes are offered to bridge the gap. One provides
  19. // Rabbit without an IV and the second provides Rabbit with an IV.
  20. NAMESPACE_BEGIN(CryptoPP)
  21. /// \brief Rabbit stream cipher information
  22. /// \since Crypto++ 8.0
  23. struct RabbitInfo : public FixedKeyLength<16, SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
  24. {
  25. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "Rabbit"; }
  26. };
  27. /// \brief Rabbit stream cipher information
  28. /// \since Crypto++ 8.0
  29. struct RabbitWithIVInfo : public FixedKeyLength<16, SimpleKeyingInterface::UNIQUE_IV, 8>
  30. {
  31. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "RabbitWithIV"; }
  32. };
  33. /// \brief Rabbit stream cipher implementation
  34. /// \since Crypto++ 8.0
  35. class RabbitPolicy : public AdditiveCipherConcretePolicy<word32, 4>, public RabbitInfo
  36. {
  37. protected:
  38. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  39. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  40. bool CanOperateKeystream() const { return true; }
  41. bool CipherIsRandomAccess() const { return false; }
  42. private:
  43. // Master and working states
  44. FixedSizeSecBlock<word32, 8> m_mx, m_mc, m_wx, m_wc;
  45. // Workspace
  46. FixedSizeSecBlock<word32, 12> m_t;
  47. word32 m_mcy, m_wcy; // carry
  48. };
  49. /// \brief Rabbit stream cipher implementation
  50. /// \since Crypto++ 8.0
  51. class RabbitWithIVPolicy : public AdditiveCipherConcretePolicy<word32, 4>, public RabbitWithIVInfo
  52. {
  53. protected:
  54. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  55. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  56. void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
  57. bool CanOperateKeystream() const { return true; }
  58. bool CipherIsRandomAccess() const { return false; }
  59. private:
  60. // Master and working states
  61. FixedSizeSecBlock<word32, 8> m_mx, m_mc, m_wx, m_wc;
  62. // Workspace
  63. FixedSizeSecBlock<word32, 12> m_t;
  64. word32 m_mcy, m_wcy; // carry
  65. };
  66. /// \brief Rabbit stream cipher
  67. /// \details Rabbit is a stream cipher developed by Martin Boesgaard, Mette Vesterager,
  68. /// Thomas Pedersen, Jesper Christiansen and Ove Scavenius. Rabbit is one of the final four
  69. /// Profile 1 (software) ciphers selected for the eSTREAM portfolio.
  70. /// \details Crypto++ provides Rabbit and RabbitWithIV classes. Two classes are necessary
  71. /// because the library lacks the means to describe and manage optional IVs.
  72. /// \sa RabbitWithIV, <A HREF="http://www.ecrypt.eu.org/stream/e2-rabbit.html">The
  73. /// eSTREAM Project | Rabbit</A> and
  74. /// <A HREF="https://www.cryptopp.com/wiki/Rabbit">Crypto++ Wiki | Rabbit</A>.
  75. /// \since Crypto++ 8.0
  76. struct Rabbit : public RabbitInfo, public SymmetricCipherDocumentation
  77. {
  78. typedef SymmetricCipherFinal<ConcretePolicyHolder<RabbitPolicy, AdditiveCipherTemplate<> >, RabbitInfo> Encryption;
  79. typedef Encryption Decryption;
  80. };
  81. /// \brief Rabbit stream cipher
  82. /// \details Rabbit is a stream cipher developed by Martin Boesgaard, Mette Vesterager,
  83. /// Thomas Pedersen, Jesper Christiansen and Ove Scavenius. Rabbit is one of the final four
  84. /// Profile 1 (software) ciphers selected for the eSTREAM portfolio.
  85. /// \details Crypto++ provides Rabbit and RabbitWithIV classes. Two classes are necessary
  86. /// because the library lacks the means to describe and manage optional IVs.
  87. /// \sa Rabbit, <A HREF="http://www.ecrypt.eu.org/stream/e2-rabbit.html">The
  88. /// eSTREAM Project | Rabbit</A> and
  89. /// <A HREF="https://www.cryptopp.com/wiki/Rabbit">Crypto++ Wiki | Rabbit</A>.
  90. /// \since Crypto++ 8.0
  91. struct RabbitWithIV : public RabbitWithIVInfo, public SymmetricCipherDocumentation
  92. {
  93. typedef SymmetricCipherFinal<ConcretePolicyHolder<RabbitWithIVPolicy, AdditiveCipherTemplate<> >, RabbitWithIVInfo> Encryption;
  94. typedef Encryption Decryption;
  95. };
  96. NAMESPACE_END
  97. #endif // CRYPTOPP_RABBIT_H