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