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