Shannon.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef SHANNON_H
  2. #define SHANNON_H
  3. #include <cstdint> // for uint32_t, uint8_t
  4. #include <vector> // for vector
  5. class Shannon {
  6. public:
  7. static constexpr unsigned int N = 16;
  8. void key(const std::vector<uint8_t>& key); /* set key */
  9. void nonce(const std::vector<uint8_t>& nonce); /* set Init Vector */
  10. void stream(std::vector<uint8_t>& buf); /* stream cipher */
  11. void maconly(std::vector<uint8_t>& buf); /* accumulate MAC */
  12. void encrypt(std::vector<uint8_t>& buf); /* encrypt + MAC */
  13. void decrypt(std::vector<uint8_t>& buf); /* finalize + MAC */
  14. void finish(std::vector<uint8_t>& buf); /* finalise MAC */
  15. private:
  16. static constexpr unsigned int FOLD = Shannon::N;
  17. static constexpr unsigned int INITKONST = 0x6996c53a;
  18. static constexpr unsigned int KEYP = 13;
  19. uint32_t R[Shannon::N];
  20. uint32_t CRC[Shannon::N];
  21. uint32_t initR[Shannon::N];
  22. uint32_t konst;
  23. uint32_t sbuf;
  24. uint32_t mbuf;
  25. int nbuf;
  26. static uint32_t sbox1(uint32_t w);
  27. static uint32_t sbox2(uint32_t w);
  28. void cycle();
  29. void crcfunc(uint32_t i);
  30. void macfunc(uint32_t i);
  31. void initState();
  32. void saveState();
  33. void reloadState();
  34. void genkonst();
  35. void diffuse();
  36. void loadKey(const std::vector<uint8_t>& key);
  37. };
  38. #endif