Salsa20.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Based on public domain code available at: http://cr.yp.to/snuffle.html
  3. *
  4. * This therefore is public domain.
  5. */
  6. #ifndef _ZT_SALSA20_HPP
  7. #define _ZT_SALSA20_HPP
  8. #include <stdint.h>
  9. #include "Constants.hpp"
  10. namespace ZeroTier {
  11. /**
  12. * Salsa20/20 stream cipher
  13. */
  14. class Salsa20
  15. {
  16. public:
  17. Salsa20() throw() {}
  18. /**
  19. * @param key Key bits
  20. * @param kbits Number of key bits: 128 or 256 (recommended)
  21. * @param iv 64-bit initialization vector
  22. */
  23. Salsa20(const void *key,unsigned int kbits,const void *iv)
  24. throw()
  25. {
  26. init(key,kbits,iv);
  27. }
  28. /**
  29. * Initialize cipher
  30. *
  31. * @param key Key bits
  32. * @param kbits Number of key bits: 128 or 256 (recommended)
  33. * @param iv 64-bit initialization vector
  34. */
  35. void init(const void *key,unsigned int kbits,const void *iv)
  36. throw();
  37. /**
  38. * Encrypt data
  39. *
  40. * @param in Input data
  41. * @param out Output buffer
  42. * @param bytes Length of data
  43. */
  44. void encrypt(const void *in,void *out,unsigned int bytes)
  45. throw();
  46. /**
  47. * Decrypt data
  48. *
  49. * @param in Input data
  50. * @param out Output buffer
  51. * @param bytes Length of data
  52. */
  53. inline void decrypt(const void *in,void *out,unsigned int bytes)
  54. throw()
  55. {
  56. encrypt(in,out,bytes);
  57. }
  58. private:
  59. uint32_t _state[16];
  60. };
  61. } // namespace ZeroTier
  62. #endif