]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
4037f0968705f717c8b6cbd7f17394672662be4b
[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 100\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 MatrixOlmSessionFrom(\r
110     MatrixOlmSession * session,\r
111     OlmAccount * olmAccount,\r
112     const char * deviceId,\r
113     const char * deviceKey,\r
114     const char * encrypted);\r
115 \r
116 bool\r
117 MatrixOlmSessionTo(\r
118     MatrixOlmSession * session,\r
119     OlmAccount * olmAccount,\r
120     const char * deviceId,\r
121     const char * deviceKey,\r
122     const char * deviceOnetimeKey);\r
123 \r
124 bool\r
125 MatrixOlmSessionEncrypt(\r
126     MatrixOlmSession * session,\r
127     const char * plaintext,\r
128     char * outBuffer, int outBufferCap);\r
129 \r
130 bool\r
131 MatrixOlmSessionDecrypt(\r
132     MatrixOlmSession * session,\r
133     size_t messageType,\r
134     char * encrypted,\r
135     char * outBuffer, int outBufferCap);\r
136 \r
137 \r
138 // Matrix Megolm Session\r
139 \r
140 typedef struct MatrixMegolmInSession {\r
141     char roomId[ROOM_ID_SIZE];\r
142     char id[MEGOLM_SESSION_ID_SIZE];\r
143     char key[MEGOLM_SESSION_KEY_SIZE];\r
144 \r
145     OlmInboundGroupSession * session;\r
146     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
147 \r
148 } MatrixMegolmInSession;\r
149 \r
150 bool\r
151 MatrixMegolmInSessionInit(\r
152     MatrixMegolmInSession * session,\r
153     const char * roomId,\r
154     const char * sessionId,\r
155     const char * sessionKey, int sessionKeyLen);\r
156 \r
157 bool\r
158 MatrixMegolmInSessionDecrypt(\r
159     MatrixMegolmInSession * session,\r
160     const char * encrypted, int encryptedLen,\r
161     char * outDecrypted, int outDecryptedCap);\r
162 \r
163 typedef struct MatrixMegolmOutSession {\r
164     char roomId[ROOM_ID_SIZE];\r
165     char id[MEGOLM_SESSION_ID_SIZE];\r
166     char key[MEGOLM_SESSION_KEY_SIZE];\r
167 \r
168     OlmOutboundGroupSession * session;\r
169     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
170 } MatrixMegolmOutSession;\r
171 \r
172 bool\r
173 MatrixMegolmOutSessionInit(\r
174     MatrixMegolmOutSession * session,\r
175     const char * roomId);\r
176 \r
177 bool\r
178 MatrixMegolmOutSessionEncrypt(\r
179     MatrixMegolmOutSession * session,\r
180     const char * plaintext,\r
181     char * outBuffer, int outBufferCap);\r
182 \r
183 bool\r
184 MatrixMegolmOutSessionSave(\r
185     MatrixMegolmOutSession * session,\r
186     const char * filename,\r
187     const char * key);\r
188     \r
189 bool\r
190 MatrixMegolmOutSessionLoad(\r
191     MatrixMegolmOutSession * session,\r
192     const char * filename,\r
193     const char * key);\r
194 \r
195 \r
196 // Matrix Client\r
197 \r
198 typedef struct MatrixClient {\r
199     MatrixOlmAccount olmAccount;\r
200 \r
201     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
202     int numMegolmInSessions;\r
203     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
204     int numMegolmOutSessions;\r
205     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
206     int numOlmSessions;\r
207     \r
208     MatrixDevice devices[NUM_DEVICES];\r
209     int numDevices;\r
210     \r
211     // char deviceKey[DEVICE_KEY_SIZE];\r
212     // char signingKey[DEVICE_KEY_SIZE];\r
213 \r
214     char userId[USER_ID_SIZE];\r
215     char server[SERVER_SIZE];\r
216     char accessToken[ACCESS_TOKEN_SIZE];\r
217     char deviceId[DEVICE_ID_SIZE];\r
218     char expireMs[EXPIRE_MS_SIZE];\r
219     char refreshToken[REFRESH_TOKEN_SIZE];\r
220 \r
221     void * httpUserData;\r
222 } MatrixClient;\r
223 \r
224 bool\r
225 MatrixClientInit(\r
226     MatrixClient * client,\r
227     const char * server);\r
228 \r
229 bool\r
230 MatrixClientSave(\r
231     MatrixClient * client,\r
232     const char * filename);\r
233 \r
234 bool\r
235 MatrixClientLoad(\r
236     MatrixClient * client,\r
237     const char * filename);\r
238 \r
239 bool\r
240 MatrixClientSetAccessToken(\r
241     MatrixClient * client,\r
242     const char * accessToken);\r
243 \r
244 bool\r
245 MatrixClientSetDeviceId(\r
246     MatrixClient * client,\r
247     const char * deviceId);\r
248 \r
249 bool\r
250 MatrixClientSetUserId(\r
251     MatrixClient * client,\r
252     const char * userId);\r
253 \r
254 bool\r
255 MatrixClientGenerateOnetimeKeys(\r
256     MatrixClient * client,\r
257     int numberOfKeys);\r
258 \r
259 bool\r
260 MatrixClientUploadOnetimeKeys(\r
261     MatrixClient * client);\r
262 \r
263 bool\r
264 MatrixClientUploadDeviceKey(\r
265     MatrixClient * client);\r
266 \r
267 bool\r
268 MatrixClientClaimOnetimeKey(\r
269     MatrixClient * client,\r
270     const char * userId,\r
271     const char * deviceId,\r
272     char * outOnetimeKey, int outOnetimeKeyCap);\r
273 \r
274 bool\r
275 MatrixClientLoginPassword(\r
276     MatrixClient * client,\r
277     const char * username,\r
278     const char * password,\r
279     const char * displayName);\r
280     \r
281 bool\r
282 MatrixClientSendEvent(\r
283     MatrixClient * client,\r
284     const char * roomId,\r
285     const char * msgType,\r
286     const char * msgBody);\r
287     \r
288 bool\r
289 MatrixClientSendEventEncrypted(\r
290     MatrixClient * client,\r
291     const char * roomId,\r
292     const char * msgType,\r
293     const char * msgBody);\r
294 \r
295 bool\r
296 MatrixClientSync(\r
297     MatrixClient * client,\r
298     char * outSync, int outSyncCap,\r
299     const char * nextBatch);\r
300 \r
301 bool\r
302 MatrixClientGetRoomEvent(\r
303     MatrixClient * client,\r
304     const char * roomId,\r
305     const char * eventId,\r
306     char * outEvent, int outEventCap);\r
307 \r
308 bool\r
309 MatrixClientShareMegolmOutSession(\r
310     MatrixClient * client,\r
311     const char * userId,\r
312     const char * deviceId,\r
313     MatrixMegolmOutSession * session);\r
314 \r
315 bool\r
316 MatrixClientShareMegolmOutSessionTest(\r
317     MatrixClient * client,\r
318     const char * userId,\r
319     const char * deviceId,\r
320     MatrixMegolmOutSession * session);\r
321 \r
322 bool\r
323 MatrixClientGetMegolmOutSession(\r
324     MatrixClient * client,\r
325     const char * roomId,\r
326     MatrixMegolmOutSession ** outSession);\r
327 \r
328 bool\r
329 MatrixClientNewMegolmOutSession(\r
330     MatrixClient * client,\r
331     const char * roomId,\r
332     MatrixMegolmOutSession ** outSession);\r
333 \r
334 bool\r
335 MatrixClientGetMegolmInSession(\r
336     MatrixClient * client,\r
337     const char * roomId, int roomIdLen,\r
338     const char * sessionId, int sessionIdLen,\r
339     MatrixMegolmInSession ** outSession);\r
340 \r
341 bool\r
342 MatrixClientNewMegolmInSession(\r
343     MatrixClient * client,\r
344     const char * roomId,\r
345     const char * sessionId,\r
346     const char * sessionKey,\r
347     MatrixMegolmInSession ** outSession);\r
348     \r
349 bool\r
350 MatrixClientRequestMegolmInSession(\r
351     MatrixClient * client,\r
352     const char * roomId,\r
353     const char * sessionId,\r
354     const char * senderKey,\r
355     const char * userId,\r
356     const char * deviceId); // TODO: remove deviceId (query all devices)\r
357 \r
358 bool\r
359 MatrixClientGetOlmSessionIn(\r
360     MatrixClient * client,\r
361     const char * userId,\r
362     const char * deviceId,\r
363     const char * encrypted,\r
364     MatrixOlmSession ** outSession);\r
365     \r
366 bool\r
367 MatrixClientGetOlmSessionOut(\r
368     MatrixClient * client,\r
369     const char * userId,\r
370     const char * deviceId,\r
371     MatrixOlmSession ** outSession);\r
372 \r
373 bool\r
374 MatrixClientSendToDevice(\r
375     MatrixClient * client,\r
376     const char * userId,\r
377     const char * deviceId,\r
378     const char * message,\r
379     const char * msgType);\r
380 \r
381 bool\r
382 MatrixClientSendToDeviceEncrypted(\r
383     MatrixClient * client,\r
384     const char * userId,\r
385     const char * deviceId,\r
386     const char * message,\r
387     const char * msgType);\r
388 \r
389 bool\r
390 MatrixClientSendDummy(\r
391     MatrixClient * client,\r
392     const char * userId,\r
393     const char * deviceId);\r
394 \r
395 bool\r
396 MatrixClientRequestDeviceKey(\r
397     MatrixClient * client,\r
398     const char * deviceId,\r
399     char * outDeviceKey, int outDeviceKeyCap);\r
400     \r
401 bool\r
402 MatrixClientRequestSigningKey(\r
403     MatrixClient * client,\r
404     const char * deviceId,\r
405     char * outSigningKey, int outSigningKeyCap);\r
406 \r
407 bool\r
408 MatrixClientRequestDeviceKeys(\r
409     MatrixClient * client);\r
410 \r
411 bool\r
412 MatrixClientDeleteDevice(\r
413     MatrixClient * client);\r
414 \r
415 \r
416 \r
417 \r
418 bool\r
419 MatrixHttpInit(\r
420     MatrixClient * client);\r
421 \r
422 bool\r
423 MatrixHttpConnect(\r
424     MatrixClient * client);\r
425 \r
426 bool\r
427 MatrixHttpDeinit(\r
428     MatrixClient * client);\r
429 \r
430 bool\r
431 MatrixHttpGet(\r
432     MatrixClient * client,\r
433     const char * url,\r
434     char * outResponseBuffer, int outResponseCap,\r
435     bool authenticated);\r
436 \r
437 bool\r
438 MatrixHttpPost(\r
439     MatrixClient * client,\r
440     const char * url,\r
441     const char * requestBuffer,\r
442     char * outResponseBuffer, int outResponseCap,\r
443     bool authenticated);\r
444 \r
445 bool\r
446 MatrixHttpPut(\r
447     MatrixClient * client,\r
448     const char * url,\r
449     const char * requestBuffer,\r
450     char * outResponseBuffer, int outResponseCap,\r
451     bool authenticated);\r
452 \r
453 // util\r
454 \r
455 void\r
456 Randomize(uint8_t * random, int randomLen);\r
457 \r
458 bool\r
459 JsonEscape(\r
460     const char * sIn, int sInLen,\r
461     char * sOut, int sOutCap);\r
462     \r
463 bool\r
464 JsonCanonicalize(\r
465     const char * sIn, int sInLen,\r
466     char * sOut, int sOutCap);\r
467     \r
468 bool\r
469 JsonSign(\r
470     MatrixClient * client,\r
471     const char * sIn, int sInLen,\r
472     char * sOut, int sOutCap);\r
473 \r
474 #endif\r