megolm.hpp (1657B)
1 #pragma once 2 #include <array> 3 #include <botan/rng.h> 4 5 /** 6 * number of bytes in each part of the ratchet; this should be the same as 7 * the length of the hash function used in the HMAC (32 bytes for us, as we 8 * use HMAC-SHA-256) 9 */ 10 #define MEGOLM_RATCHET_PART_LENGTH 32 /* SHA256_OUTPUT_LENGTH */ 11 12 /** 13 * number of parts in the ratchet; the advance() implementations rely on 14 * this being 4. 15 */ 16 #define MEGOLM_RATCHET_PARTS 4 17 18 #define MEGOLM_RATCHET_LENGTH (MEGOLM_RATCHET_PARTS * MEGOLM_RATCHET_PART_LENGTH) 19 20 namespace spank_olm 21 { 22 struct Megolm 23 { 24 std::array<std::array<std::uint8_t, MEGOLM_RATCHET_PART_LENGTH>, MEGOLM_RATCHET_PARTS> data; 25 std::uint32_t counter; 26 27 28 /** 29 * \brief Initialize the megolm ratchet. 30 */ 31 void init(Botan::RandomNumberGenerator &rng, unsigned int counter); 32 33 /** 34 * \brief Returns the number of bytes needed to store a megolm 35 */ 36 [[nodiscard]] size_t pickle_length() const; 37 38 /** 39 * \brief Pickle the megolm. 40 */ 41 std::uint8_t *pickle(std::uint8_t *pos) const; 42 43 /** 44 * \brief Unpickle the megolm. 45 */ 46 std::uint8_t const *unpickle(std::uint8_t const *pos, const std::uint8_t *end); 47 48 /** 49 * \brief Advance the megolm ratchet by one step. 50 */ 51 void advance(); 52 53 /** 54 * \brief Advance the megolm ratchet by a given number of steps. 55 */ 56 void advance(unsigned int advance_to); 57 58 [[nodiscard]] const uint8_t *get_data() const { return reinterpret_cast<const uint8_t *>(data.data()); } 59 }; 60 } // namespace spank_olm