1 module matrix.megolm;
2 
3 extern (C):
4 // copy&pasted from megolm.h
5 
6 /**
7  * number of bytes in each part of the ratchet; this should be the same as
8  * the length of the hash function used in the HMAC (32 bytes for us, as we
9  * use HMAC-SHA-256)
10  */
11 enum MEGOLM_RATCHET_PART_LENGTH = 32; /* SHA256_OUTPUT_LENGTH */
12 
13 /**
14  * number of parts in the ratchet; the advance() implementations rely on
15  * this being 4.
16  */
17 enum MEGOLM_RATCHET_PARTS = 4;
18 
19 enum MEGOLM_RATCHET_LENGTH = (MEGOLM_RATCHET_PARTS * MEGOLM_RATCHET_PART_LENGTH);
20 
21 struct Megolm {
22     ubyte[MEGOLM_RATCHET_PARTS][MEGOLM_RATCHET_PART_LENGTH] data;
23     uint counter;
24 };
25 
26 struct _olm_cipher;
27 
28 
29 /**
30  * The cipher used in megolm-backed conversations
31  *
32  * (AES256 + SHA256, with keys based on an HKDF with info of MEGOLM_KEYS)
33  */
34 extern const _olm_cipher *megolm_cipher;
35 
36 /**
37  * initialize the megolm ratchet. random_data should be at least
38  * MEGOLM_RATCHET_LENGTH bytes of randomness.
39  */
40 void megolm_init(Megolm *megolm, const(ubyte)* random_data, uint counter);
41 
42 /** Returns the number of bytes needed to store a megolm */
43 size_t megolm_pickle_length(const Megolm *megolm);
44 
45 /**
46  * Pickle the megolm. Returns a pointer to the next free space in the buffer.
47  */
48 ubyte * megolm_pickle(const Megolm *megolm, ubyte *pos);
49 
50 /**
51  * Unpickle the megolm. Returns a pointer to the next item in the buffer.
52  */
53 const(ubyte)* megolm_unpickle(Megolm *megolm,const(ubyte)* pos,
54                                 const(ubyte)* end);
55 
56 
57 /** advance the ratchet by one step */
58 void megolm_advance(Megolm *megolm);
59 
60 /**
61  * get the key data in the ratchet. The returned data is
62  * MEGOLM_RATCHET_LENGTH bytes long.
63  */
64 const(ubyte)* megolm_get_data(Megolm* megolm) {
65     return cast(const ubyte *) (megolm.data);
66 }
67 
68 /** advance the ratchet to a given count */
69 void megolm_advance_to(Megolm *megolm, uint advance_to);