storage_encryption.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/bytes.h"
  9. namespace Storage {
  10. constexpr auto kSaltSize = size_type(64);
  11. class CtrState {
  12. public:
  13. static constexpr auto kBlockSize = size_type(16);
  14. static constexpr auto kKeySize = size_type(32);
  15. static constexpr auto kIvSize = kBlockSize;
  16. CtrState(bytes::const_span key, bytes::const_span iv);
  17. void encrypt(bytes::span data, int64 offset);
  18. void decrypt(bytes::span data, int64 offset);
  19. private:
  20. template <typename Method>
  21. void process(bytes::span data, int64 offset, Method method);
  22. bytes::array<kIvSize> incrementedIv(int64 blockIndex);
  23. static constexpr auto EcountSize = kBlockSize;
  24. bytes::array<kKeySize> _key;
  25. bytes::array<kIvSize> _iv;
  26. };
  27. class EncryptionKey {
  28. public:
  29. static constexpr auto kSize = size_type(256);
  30. static constexpr auto kSize_v2 = size_type(64);
  31. EncryptionKey() = default;
  32. explicit EncryptionKey(bytes::vector &&data);
  33. bool empty() const;
  34. explicit operator bool() const;
  35. const bytes::vector &data() const;
  36. CtrState prepareCtrState(bytes::const_span salt) const;
  37. private:
  38. bytes::vector _data;
  39. };
  40. } // namespace Storage