]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
start working on Sync example (reply to to_device messages)
[matrix_esp_thesis] / src / matrix.h
1 #ifndef MATRIX__H\r
2 #define MATRIX__H\r
3 \r
4 #include <stdbool.h>\r
5 #include <stdlib.h>\r
6 #include <string.h>\r
7 #include <time.h>\r
8 \r
9 #include <olm/olm.h>\r
10 \r
11 \r
12 \r
13 #define USER_ID_SIZE 64\r
14 #define ROOM_ID_SIZE 128\r
15 #define SERVER_SIZE 20\r
16 #define ACCESS_TOKEN_SIZE 40\r
17 #define DEVICE_ID_SIZE 20\r
18 #define EXPIRE_MS_SIZE 20\r
19 #define REFRESH_TOKEN_SIZE 20\r
20 #define MAX_URL_LEN 1024\r
21 \r
22 #define OLM_IDENTITY_KEYS_JSON_SIZE 128\r
23 #define DEVICE_KEY_SIZE 44\r
24 #define SIGNING_KEY_SIZE 44\r
25 #define ONETIME_KEY_SIZE 44\r
26 \r
27 #define KEY_SHARE_EVENT_LEN 1024\r
28 \r
29 #define OLM_ACCOUNT_MEMORY_SIZE 7528\r
30 #define OLM_ACCOUNT_RANDOM_SIZE (32+32)\r
31 \r
32 #define OLM_SESSION_MEMORY_SIZE 3352\r
33 #define OLM_ENCRYPT_RANDOM_SIZE 32\r
34 #define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)\r
35 \r
36 #define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)\r
37 #define OLM_KEY_ID_SIZE 32\r
38 \r
39 #define OLM_SIGNATURE_SIZE 128\r
40 \r
41 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232\r
42 #define MEGOLM_SESSION_ID_SIZE 44\r
43 #define MEGOLM_SESSION_KEY_SIZE 306\r
44 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)\r
45 \r
46 #define JSON_ONETIME_KEY_SIZE 128\r
47 #define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
48 #define JSON_SIGNATURE_SIZE 256\r
49 \r
50 #define NUM_MEGOLM_SESSIONS 10\r
51 #define NUM_OLM_SESSIONS 10\r
52 #define NUM_DEVICES 10\r
53 \r
54 // Matrix Device\r
55 \r
56 typedef struct MatrixDevice {\r
57     char deviceId[DEVICE_ID_SIZE];\r
58     char deviceKey[DEVICE_KEY_SIZE];\r
59     char signingKey[SIGNING_KEY_SIZE];\r
60 } MatrixDevice;\r
61 \r
62 \r
63 // Matrix Olm Account\r
64 \r
65 typedef struct MatrixOlmAccount {\r
66     OlmAccount * account;\r
67     char memory[OLM_ACCOUNT_MEMORY_SIZE];\r
68 } MatrixOlmAccount;\r
69 \r
70 bool\r
71 MatrixOlmAccountInit(\r
72     MatrixOlmAccount * account);\r
73 \r
74 bool\r
75 MatrixOlmAccountUnpickle(\r
76     MatrixOlmAccount * account,\r
77     void * pickled, int pickledLen,\r
78     const void * key, int keyLen);\r
79 \r
80 bool\r
81 MatrixOlmAccountGetDeviceKey(\r
82     MatrixOlmAccount * account,\r
83     char * key, int keyCap);\r
84     \r
85 bool\r
86 MatrixOlmAccountGetSigningKey(\r
87     MatrixOlmAccount * account,\r
88     char * key, int keyCap);\r
89 \r
90 \r
91 // Matrix Olm Session\r
92 \r
93 typedef struct MatrixOlmSession {\r
94     const char * deviceId; // TODO: char[]\r
95 \r
96     int type;\r
97     OlmSession * session;\r
98     char memory[OLM_SESSION_MEMORY_SIZE];\r
99 } MatrixOlmSession;\r
100 \r
101 bool\r
102 MatrixOlmSessionUnpickle(\r
103     MatrixOlmSession * session,\r
104     const char * deviceId,\r
105     void * pickled, int pickledLen,\r
106     const void * key, int keyLen);\r
107 \r
108 bool\r
109 MatrixOlmSessionTo(\r
110     MatrixOlmSession * session,\r
111     OlmAccount * olmAccount,\r
112     const char * deviceId,\r
113     const char * deviceKey,\r
114     const char * deviceOnetimeKey);\r
115 \r
116 bool\r
117 MatrixOlmSessionEncrypt(\r
118     MatrixOlmSession * session,\r
119     const char * plaintext,\r
120     char * outBuffer, int outBufferCap);\r
121 \r
122 bool\r
123 MatrixOlmSessionDecrypt(\r
124     MatrixOlmSession * session,\r
125     size_t messageType,\r
126     char * encrypted,\r
127     char * outBuffer, int outBufferCap);\r
128 \r
129 \r
130 // Matrix Megolm Session\r
131 \r
132 typedef struct MatrixMegolmInSession {\r
133     OlmInboundGroupSession * session;\r
134 } MatrixMegolmInSession;\r
135 \r
136 bool\r
137 MatrixMegolmInSessionDecrypt(\r
138     MatrixMegolmInSession * megolmInSession,\r
139     const char * encrypted,\r
140     char * outDecrypted, int outDecryptedCap);\r
141 \r
142 typedef struct MatrixMegolmOutSession {\r
143     char roomId[ROOM_ID_SIZE];\r
144 \r
145     OlmOutboundGroupSession * session;\r
146     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
147 \r
148     char id[MEGOLM_SESSION_ID_SIZE];\r
149     char key[MEGOLM_SESSION_KEY_SIZE];\r
150 } MatrixMegolmOutSession;\r
151 \r
152 bool\r
153 MatrixMegolmOutSessionInit(\r
154     MatrixMegolmOutSession * session,\r
155     const char * roomId);\r
156 \r
157 bool\r
158 MatrixMegolmOutSessionEncrypt(\r
159     MatrixMegolmOutSession * session,\r
160     const char * plaintext,\r
161     char * outBuffer, int outBufferCap);\r
162 \r
163 bool\r
164 MatrixMegolmOutSessionSave(\r
165     MatrixMegolmOutSession * session,\r
166     const char * filename,\r
167     const char * key);\r
168     \r
169 bool\r
170 MatrixMegolmOutSessionLoad(\r
171     MatrixMegolmOutSession * session,\r
172     const char * filename,\r
173     const char * key);\r
174 \r
175 \r
176 // Matrix Client\r
177 \r
178 typedef struct MatrixClient {\r
179     MatrixOlmAccount olmAccount;\r
180 \r
181     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
182     int numMegolmInSessions;\r
183     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
184     int numMegolmOutSessions;\r
185     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
186     int numOlmSessions;\r
187     \r
188     MatrixDevice devices[NUM_DEVICES];\r
189     int numDevices;\r
190     \r
191     // char deviceKey[DEVICE_KEY_SIZE];\r
192     // char signingKey[DEVICE_KEY_SIZE];\r
193 \r
194     char userId[USER_ID_SIZE];\r
195     char server[SERVER_SIZE];\r
196     char accessToken[ACCESS_TOKEN_SIZE];\r
197     char deviceId[DEVICE_ID_SIZE];\r
198     char expireMs[EXPIRE_MS_SIZE];\r
199     char refreshToken[REFRESH_TOKEN_SIZE];\r
200 \r
201     void * httpUserData;\r
202 } MatrixClient;\r
203 \r
204 bool\r
205 MatrixClientInit(\r
206     MatrixClient * client,\r
207     const char * server);\r
208 \r
209 bool\r
210 MatrixClientSave(\r
211     MatrixClient * client,\r
212     const char * filename);\r
213 \r
214 bool\r
215 MatrixClientLoad(\r
216     MatrixClient * client,\r
217     const char * filename);\r
218 \r
219 bool\r
220 MatrixClientSetAccessToken(\r
221     MatrixClient * client,\r
222     const char * accessToken);\r
223 \r
224 bool\r
225 MatrixClientSetDeviceId(\r
226     MatrixClient * client,\r
227     const char * deviceId);\r
228 \r
229 bool\r
230 MatrixClientSetUserId(\r
231     MatrixClient * client,\r
232     const char * userId);\r
233 \r
234 bool\r
235 MatrixClientGenerateOnetimeKeys(\r
236     MatrixClient * client,\r
237     int numberOfKeys);\r
238 \r
239 bool\r
240 MatrixClientUploadOnetimeKeys(\r
241     MatrixClient * client);\r
242 \r
243 bool\r
244 MatrixClientUploadDeviceKey(\r
245     MatrixClient * client);\r
246 \r
247 bool\r
248 MatrixClientClaimOnetimeKey(\r
249     MatrixClient * client,\r
250     const char * userId,\r
251     const char * deviceId,\r
252     char * outOnetimeKey, int outOnetimeKeyCap);\r
253 \r
254 bool\r
255 MatrixClientLoginPassword(\r
256     MatrixClient * client,\r
257     const char * username,\r
258     const char * password,\r
259     const char * displayName);\r
260     \r
261 bool\r
262 MatrixClientSendEvent(\r
263     MatrixClient * client,\r
264     const char * roomId,\r
265     const char * msgType,\r
266     const char * msgBody);\r
267     \r
268 bool\r
269 MatrixClientSendEventEncrypted(\r
270     MatrixClient * client,\r
271     const char * roomId,\r
272     const char * msgType,\r
273     const char * msgBody);\r
274 \r
275 bool\r
276 MatrixClientSync(\r
277     MatrixClient * client,\r
278     char * outSync, int outSyncCap,\r
279     const char * nextBatch);\r
280 \r
281 bool\r
282 MatrixClientGetRoomEvent(\r
283     MatrixClient * client,\r
284     const char * roomId,\r
285     const char * eventId,\r
286     char * outEvent, int outEventCap);\r
287 \r
288 bool\r
289 MatrixClientShareMegolmOutSession(\r
290     MatrixClient * client,\r
291     const char * userId,\r
292     const char * deviceId,\r
293     MatrixMegolmOutSession * session);\r
294 \r
295 bool\r
296 MatrixClientShareMegolmOutSessionTest(\r
297     MatrixClient * client,\r
298     const char * userId,\r
299     const char * deviceId,\r
300     MatrixMegolmOutSession * session);\r
301 \r
302 bool\r
303 MatrixClientGetMegolmOutSession(\r
304     MatrixClient * client,\r
305     const char * roomId,\r
306     MatrixMegolmOutSession ** outSession);\r
307 \r
308 bool\r
309 MatrixClientSetMegolmOutSession(\r
310     MatrixClient * client,\r
311     const char * roomId,\r
312     MatrixMegolmOutSession session);\r
313 \r
314 bool\r
315 MatrixClientInitMegolmOutSession(\r
316     MatrixClient * client,\r
317     const char * roomId);\r
318     \r
319 bool\r
320 MatrixClientRequestMegolmInSession(\r
321     MatrixClient * client,\r
322     const char * roomId,\r
323     const char * sessionId,\r
324     const char * senderKey,\r
325     const char * userId,\r
326     const char * deviceId, // TODO: remove deviceId (query all devices)\r
327     MatrixMegolmInSession * outMegolmInSession);\r
328 \r
329 bool\r
330 MatrixClientGetOlmSession(\r
331     MatrixClient * client,\r
332     const char * userId,\r
333     const char * deviceId,\r
334     MatrixOlmSession ** outSession);\r
335 \r
336 bool\r
337 MatrixClientSendToDevice(\r
338     MatrixClient * client,\r
339     const char * userId,\r
340     const char * deviceId,\r
341     const char * message,\r
342     const char * msgType);\r
343 \r
344 bool\r
345 MatrixClientSendToDeviceEncrypted(\r
346     MatrixClient * client,\r
347     const char * userId,\r
348     const char * deviceId,\r
349     const char * message,\r
350     const char * msgType);\r
351 \r
352 bool\r
353 MatrixClientSendDummy(\r
354     MatrixClient * client,\r
355     const char * userId,\r
356     const char * deviceId);\r
357 \r
358 bool\r
359 MatrixClientRequestDeviceKey(\r
360     MatrixClient * client,\r
361     const char * deviceId,\r
362     char * outDeviceKey, int outDeviceKeyCap);\r
363     \r
364 bool\r
365 MatrixClientRequestSigningKey(\r
366     MatrixClient * client,\r
367     const char * deviceId,\r
368     char * outSigningKey, int outSigningKeyCap);\r
369 \r
370 bool\r
371 MatrixClientRequestDeviceKeys(\r
372     MatrixClient * client);\r
373 \r
374 bool\r
375 MatrixClientDeleteDevice(\r
376     MatrixClient * client);\r
377 \r
378 \r
379 \r
380 \r
381 bool\r
382 MatrixHttpInit(\r
383     MatrixClient * client);\r
384 \r
385 bool\r
386 MatrixHttpConnect(\r
387     MatrixClient * client);\r
388 \r
389 bool\r
390 MatrixHttpDeinit(\r
391     MatrixClient * client);\r
392 \r
393 bool\r
394 MatrixHttpGet(\r
395     MatrixClient * client,\r
396     const char * url,\r
397     char * outResponseBuffer, int outResponseCap,\r
398     bool authenticated);\r
399 \r
400 bool\r
401 MatrixHttpPost(\r
402     MatrixClient * client,\r
403     const char * url,\r
404     const char * requestBuffer,\r
405     char * outResponseBuffer, int outResponseCap,\r
406     bool authenticated);\r
407 \r
408 bool\r
409 MatrixHttpPut(\r
410     MatrixClient * client,\r
411     const char * url,\r
412     const char * requestBuffer,\r
413     char * outResponseBuffer, int outResponseCap,\r
414     bool authenticated);\r
415 \r
416 // util\r
417 \r
418 void\r
419 Randomize(uint8_t * random, int randomLen);\r
420 \r
421 bool\r
422 JsonEscape(\r
423     const char * sIn, int sInLen,\r
424     char * sOut, int sOutCap);\r
425     \r
426 bool\r
427 JsonSign(\r
428     MatrixClient * client,\r
429     const char * sIn, int sInLen,\r
430     char * sOut, int sOutCap);\r
431 \r
432 #endif\r