]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
verify device
[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 128\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 \r
280 bool\r
281 MatrixClientGetRoomEvent(\r
282     MatrixClient * client,\r
283     const char * roomId,\r
284     const char * eventId,\r
285     char * outEvent, int outEventCap);\r
286 \r
287 bool\r
288 MatrixClientShareMegolmOutSession(\r
289     MatrixClient * client,\r
290     const char * userId,\r
291     const char * deviceId,\r
292     MatrixMegolmOutSession * session);\r
293 \r
294 bool\r
295 MatrixClientShareMegolmOutSessionTest(\r
296     MatrixClient * client,\r
297     const char * userId,\r
298     const char * deviceId,\r
299     MatrixMegolmOutSession * session);\r
300 \r
301 bool\r
302 MatrixClientGetMegolmOutSession(\r
303     MatrixClient * client,\r
304     const char * roomId,\r
305     MatrixMegolmOutSession ** outSession);\r
306 \r
307 bool\r
308 MatrixClientSetMegolmOutSession(\r
309     MatrixClient * client,\r
310     const char * roomId,\r
311     MatrixMegolmOutSession session);\r
312 \r
313 bool\r
314 MatrixClientInitMegolmOutSession(\r
315     MatrixClient * client,\r
316     const char * roomId);\r
317     \r
318 bool\r
319 MatrixClientRequestMegolmInSession(\r
320     MatrixClient * client,\r
321     const char * roomId,\r
322     const char * sessionId,\r
323     const char * senderKey,\r
324     const char * userId,\r
325     const char * deviceId, // TODO: remove deviceId (query all devices)\r
326     MatrixMegolmInSession * outMegolmInSession);\r
327 \r
328 bool\r
329 MatrixClientGetOlmSession(\r
330     MatrixClient * client,\r
331     const char * userId,\r
332     const char * deviceId,\r
333     MatrixOlmSession ** outSession);\r
334 \r
335 bool\r
336 MatrixClientSendToDevice(\r
337     MatrixClient * client,\r
338     const char * userId,\r
339     const char * deviceId,\r
340     const char * message,\r
341     const char * msgType);\r
342 \r
343 bool\r
344 MatrixClientSendToDeviceEncrypted(\r
345     MatrixClient * client,\r
346     const char * userId,\r
347     const char * deviceId,\r
348     const char * message,\r
349     const char * msgType);\r
350 \r
351 bool\r
352 MatrixClientSendDummy(\r
353     MatrixClient * client,\r
354     const char * userId,\r
355     const char * deviceId);\r
356 \r
357 bool\r
358 MatrixClientRequestDeviceKey(\r
359     MatrixClient * client,\r
360     const char * deviceId,\r
361     char * outDeviceKey, int outDeviceKeyCap);\r
362     \r
363 bool\r
364 MatrixClientRequestSigningKey(\r
365     MatrixClient * client,\r
366     const char * deviceId,\r
367     char * outSigningKey, int outSigningKeyCap);\r
368 \r
369 bool\r
370 MatrixClientRequestDeviceKeys(\r
371     MatrixClient * client);\r
372 \r
373 bool\r
374 MatrixClientDeleteDevice(\r
375     MatrixClient * client);\r
376 \r
377 \r
378 \r
379 \r
380 bool\r
381 MatrixHttpInit(\r
382     MatrixClient * client);\r
383 \r
384 bool\r
385 MatrixHttpConnect(\r
386     MatrixClient * client);\r
387 \r
388 bool\r
389 MatrixHttpDeinit(\r
390     MatrixClient * client);\r
391 \r
392 bool\r
393 MatrixHttpGet(\r
394     MatrixClient * client,\r
395     const char * url,\r
396     char * outResponseBuffer, int outResponseCap,\r
397     bool authenticated);\r
398 \r
399 bool\r
400 MatrixHttpPost(\r
401     MatrixClient * client,\r
402     const char * url,\r
403     const char * requestBuffer,\r
404     char * outResponseBuffer, int outResponseCap,\r
405     bool authenticated);\r
406 \r
407 bool\r
408 MatrixHttpPut(\r
409     MatrixClient * client,\r
410     const char * url,\r
411     const char * requestBuffer,\r
412     char * outResponseBuffer, int outResponseCap,\r
413     bool authenticated);\r
414 \r
415 // util\r
416 \r
417 void\r
418 Randomize(uint8_t * random, int randomLen);\r
419 \r
420 bool\r
421 JsonEscape(\r
422     const char * sIn, int sInLen,\r
423     char * sOut, int sOutCap);\r
424     \r
425 bool\r
426 JsonSign(\r
427     MatrixClient * client,\r
428     const char * sIn, int sInLen,\r
429     char * sOut, int sOutCap);\r
430 \r
431 #endif\r