ChaCha20.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2013-2018, The PurpleI2P Project
  3. *
  4. * This file is part of Purple i2pd project and licensed under BSD3
  5. *
  6. * See full license text in LICENSE file at top of project tree
  7. *
  8. * Kovri go write your own code
  9. *
  10. */
  11. #ifndef LIBI2PD_CHACHA20_H
  12. #define LIBI2PD_CHACHA20_H
  13. #include <cstdint>
  14. #include <cstring>
  15. #include <inttypes.h>
  16. #include <string.h>
  17. #include "Crypto.h"
  18. #if !OPENSSL_AEAD_CHACHA20_POLY1305
  19. namespace i2p
  20. {
  21. namespace crypto
  22. {
  23. const std::size_t CHACHA20_KEY_BYTES = 32;
  24. const std::size_t CHACHA20_NOUNCE_BYTES = 12;
  25. namespace chacha
  26. {
  27. constexpr std::size_t blocksize = 64;
  28. constexpr int rounds = 20;
  29. struct Chacha20State;
  30. struct Chacha20Block
  31. {
  32. Chacha20Block () {};
  33. Chacha20Block (Chacha20Block &&) = delete;
  34. uint8_t data[blocksize];
  35. void operator << (const Chacha20State & st);
  36. };
  37. struct Chacha20State
  38. {
  39. Chacha20State (): offset (0) {};
  40. Chacha20State (Chacha20State &&) = delete;
  41. Chacha20State & operator += (const Chacha20State & other)
  42. {
  43. for(int i = 0; i < 16; i++)
  44. data[i] += other.data[i];
  45. return *this;
  46. }
  47. void Copy(const Chacha20State & other)
  48. {
  49. memcpy(data, other.data, sizeof(uint32_t) * 16);
  50. }
  51. uint32_t data[16];
  52. Chacha20Block block;
  53. size_t offset;
  54. };
  55. void Chacha20Init (Chacha20State& state, const uint8_t * nonce, const uint8_t * key, uint32_t counter);
  56. void Chacha20SetCounter (Chacha20State& state, uint32_t counter);
  57. void Chacha20Encrypt (Chacha20State& state, uint8_t * buf, size_t sz); // encrypt buf in place
  58. }
  59. }
  60. }
  61. #endif
  62. #endif