]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
remove newline
[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 // create an olm sesseion from a type 0 message\r
153 bool\r
154 MatrixOlmSessionFrom(\r
155     MatrixOlmSession * session,\r
156     OlmAccount * olmAccount,\r
157     const char * deviceId,\r
158     const char * deviceKey,\r
159     const char * encrypted);\r
160 \r
161 // create a new olm session from a claimed onetime key\r
162 bool\r
163 MatrixOlmSessionTo(\r
164     MatrixOlmSession * session,\r
165     OlmAccount * olmAccount,\r
166     const char * deviceId,\r
167     const char * deviceKey,\r
168     const char * deviceOnetimeKey);\r
169 \r
170 bool\r
171 MatrixOlmSessionEncrypt(\r
172     MatrixOlmSession * session,\r
173     const char * plaintext,\r
174     char * outBuffer, int outBufferCap);\r
175 \r
176 bool\r
177 MatrixOlmSessionDecrypt(\r
178     MatrixOlmSession * session,\r
179     size_t messageType,\r
180     char * encrypted,\r
181     char * outBuffer, int outBufferCap);\r
182 \r
183 \r
184 // Matrix Megolm Session\r
185 \r
186 typedef struct MatrixMegolmInSession {\r
187     char roomId[ROOM_ID_SIZE];\r
188     char id[MEGOLM_SESSION_ID_SIZE];\r
189     char key[MEGOLM_SESSION_KEY_SIZE];\r
190 \r
191     OlmInboundGroupSession * session;\r
192     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
193 \r
194 } MatrixMegolmInSession;\r
195 \r
196 bool\r
197 MatrixMegolmInSessionInit(\r
198     MatrixMegolmInSession * session,\r
199     const char * roomId,\r
200     const char * sessionId,\r
201     const char * sessionKey, int sessionKeyLen);\r
202 \r
203 bool\r
204 MatrixMegolmInSessionDecrypt(\r
205     MatrixMegolmInSession * session,\r
206     const char * encrypted, int encryptedLen,\r
207     char * outDecrypted, int outDecryptedCap);\r
208 \r
209 typedef struct MatrixMegolmOutSession {\r
210     char roomId[ROOM_ID_SIZE];\r
211     char id[MEGOLM_SESSION_ID_SIZE];\r
212     char key[MEGOLM_SESSION_KEY_SIZE];\r
213 \r
214     OlmOutboundGroupSession * session;\r
215     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
216 } MatrixMegolmOutSession;\r
217 \r
218 bool\r
219 MatrixMegolmOutSessionInit(\r
220     MatrixMegolmOutSession * session,\r
221     const char * roomId);\r
222 \r
223 bool\r
224 MatrixMegolmOutSessionEncrypt(\r
225     MatrixMegolmOutSession * session,\r
226     const char * plaintext,\r
227     char * outBuffer, int outBufferCap);\r
228 \r
229 \r
230 // Matrix Client\r
231 \r
232 typedef struct MatrixClient {\r
233     MatrixOlmAccount olmAccount;\r
234 \r
235     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
236     int numMegolmInSessions;\r
237     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
238     int numMegolmOutSessions;\r
239     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
240     int numOlmSessions;\r
241     \r
242     MatrixDevice devices[NUM_DEVICES];\r
243     int numDevices;\r
244 \r
245     char userId[USER_ID_SIZE];\r
246     char accessToken[ACCESS_TOKEN_SIZE];\r
247     char deviceId[DEVICE_ID_SIZE];\r
248     char expireMs[EXPIRE_MS_SIZE];\r
249     char refreshToken[REFRESH_TOKEN_SIZE];\r
250     char masterKey[MASTER_KEY_SIZE];\r
251 \r
252     bool verified;\r
253 \r
254     MatrixHttpConnection * hc;\r
255 } MatrixClient;\r
256 \r
257 bool\r
258 MatrixClientInit(\r
259     MatrixClient * client);\r
260 \r
261 bool\r
262 MatrixClientSetAccessToken(\r
263     MatrixClient * client,\r
264     const char * accessToken);\r
265 \r
266 bool\r
267 MatrixClientSetDeviceId(\r
268     MatrixClient * client,\r
269     const char * deviceId);\r
270 \r
271 bool\r
272 MatrixClientSetUserId(\r
273     MatrixClient * client,\r
274     const char * userId);\r
275 \r
276 bool\r
277 MatrixClientGenerateOnetimeKeys(\r
278     MatrixClient * client,\r
279     int numberOfKeys);\r
280 \r
281 bool\r
282 MatrixClientUploadOnetimeKeys(\r
283     MatrixClient * client);\r
284 \r
285 bool\r
286 MatrixClientUploadDeviceKeys(\r
287     MatrixClient * client);\r
288 \r
289 bool\r
290 MatrixClientClaimOnetimeKey(\r
291     MatrixClient * client,\r
292     const char * userId,\r
293     const char * deviceId,\r
294     char * outOnetimeKey, int outOnetimeKeyCap);\r
295 \r
296 bool\r
297 MatrixClientLoginPassword(\r
298     MatrixClient * client,\r
299     const char * username,\r
300     const char * password,\r
301     const char * displayName);\r
302     \r
303 bool\r
304 MatrixClientSendEvent(\r
305     MatrixClient * client,\r
306     const char * roomId,\r
307     const char * msgType,\r
308     const char * msgBody);\r
309     \r
310 bool\r
311 MatrixClientSendEventEncrypted(\r
312     MatrixClient * client,\r
313     const char * roomId,\r
314     const char * msgType,\r
315     const char * msgBody);\r
316 \r
317 bool\r
318 MatrixClientSync(\r
319     MatrixClient * client,\r
320     char * outSyncBuffer, int outSyncCap,\r
321     char * nextBatch, int nextBatchCap);\r
322 \r
323 bool\r
324 MatrixClientGetRoomEvent(\r
325     MatrixClient * client,\r
326     const char * roomId,\r
327     const char * eventId,\r
328     char * outEvent, int outEventCap);\r
329 \r
330 bool\r
331 MatrixClientShareMegolmOutSession(\r
332     MatrixClient * client,\r
333     const char * userId,\r
334     const char * deviceId,\r
335     MatrixMegolmOutSession * session);\r
336 \r
337 // try to lookup outgoing megolm session, return true if found\r
338 bool\r
339 MatrixClientGetMegolmOutSession(\r
340     MatrixClient * client,\r
341     const char * roomId,\r
342     MatrixMegolmOutSession ** outSession);\r
343 \r
344 // create a new outgoing megolm session and store it locally\r
345 bool\r
346 MatrixClientNewMegolmOutSession(\r
347     MatrixClient * client,\r
348     const char * roomId,\r
349     MatrixMegolmOutSession ** outSession);\r
350 \r
351 // try to lookup incoming megolm session, return true if found\r
352 bool\r
353 MatrixClientGetMegolmInSession(\r
354     MatrixClient * client,\r
355     const char * roomId, int roomIdLen,\r
356     const char * sessionId, int sessionIdLen,\r
357     MatrixMegolmInSession ** outSession);\r
358 \r
359 // create a new incoming megolm session and store it locally\r
360 bool\r
361 MatrixClientNewMegolmInSession(\r
362     MatrixClient * client,\r
363     const char * roomId,\r
364     const char * sessionId,\r
365     const char * sessionKey,\r
366     MatrixMegolmInSession ** outSession);\r
367 \r
368 // send a m.room_key_request to the device identified by userId/devideId\r
369 bool\r
370 MatrixClientRequestMegolmInSession(\r
371     MatrixClient * client,\r
372     const char * roomId,\r
373     const char * sessionId,\r
374     const char * senderKey,\r
375     const char * userId,\r
376     const char * deviceId);\r
377 \r
378 // try to lookup olm session, return true if found\r
379 bool\r
380 MatrixClientGetOlmSession(\r
381     MatrixClient * client,\r
382     const char * userId,\r
383     const char * deviceId,\r
384     MatrixOlmSession ** outSession);\r
385 \r
386 // create a new olm session from a type 0 message and store it locally\r
387 bool\r
388 MatrixClientNewOlmSessionIn(\r
389     MatrixClient * client,\r
390     const char * userId,\r
391     const char * deviceId,\r
392     const char * encrypted,\r
393     MatrixOlmSession ** outSession);\r
394 \r
395 // create a new olm session with device userId/deviceId and store it locally\r
396 // this automatically claims the onetime key\r
397 bool\r
398 MatrixClientNewOlmSessionOut(\r
399     MatrixClient * client,\r
400     const char * userId,\r
401     const char * deviceId,\r
402     MatrixOlmSession ** outSession);\r
403 \r
404 bool\r
405 MatrixClientSendToDevice(\r
406     MatrixClient * client,\r
407     const char * userId,\r
408     const char * deviceId,\r
409     const char * message,\r
410     const char * msgType);\r
411 \r
412 bool\r
413 MatrixClientSendToDeviceEncrypted(\r
414     MatrixClient * client,\r
415     const char * userId,\r
416     const char * deviceId,\r
417     const char * message,\r
418     const char * msgType);\r
419 \r
420 bool\r
421 MatrixClientSendDummy(\r
422     MatrixClient * client,\r
423     const char * userId,\r
424     const char * deviceId);\r
425 \r
426 // lookup device key locally and if not present get it from server\r
427 bool\r
428 MatrixClientRequestDeviceKey(\r
429     MatrixClient * client,\r
430     const char * deviceId,\r
431     char * outDeviceKey, int outDeviceKeyCap);\r
432     \r
433 // lookup signing key locally and if not present get it from server\r
434 bool\r
435 MatrixClientRequestSigningKey(\r
436     MatrixClient * client,\r
437     const char * deviceId,\r
438     char * outSigningKey, int outSigningKeyCap);\r
439 \r
440 // lookup the master key for this user and if not present get it from server\r
441 bool\r
442 MatrixClientRequestMasterKey(\r
443     MatrixClient * client,\r
444     char * outMasterKey, int outMasterKeyCap);\r
445 \r
446 // call keys/query and store retrieved information\r
447 // this is called by the other Request* functions\r
448 bool\r
449 MatrixClientRequestDeviceKeys(\r
450     MatrixClient * client);\r
451 \r
452 // delete this device on the server\r
453 bool\r
454 MatrixClientDeleteDevice(\r
455     MatrixClient * client);\r
456 \r
457 \r
458 // util\r
459 \r
460 void\r
461 Randomize(uint8_t * random, int randomLen);\r
462 \r
463 bool\r
464 JsonEscape(\r
465     const char * sIn, int sInLen,\r
466     char * sOut, int sOutCap);\r
467     \r
468 bool\r
469 JsonCanonicalize(\r
470     const char * sIn, int sInLen,\r
471     char * sOut, int sOutCap);\r
472     \r
473 bool\r
474 JsonSign(\r
475     MatrixClient * client,\r
476     const char * sIn, int sInLen,\r
477     char * sOut, int sOutCap);\r
478 \r
479 #endif\r