]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
b51d0e99fc3469c612d21e09fd6c5312f7be0324
[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 \r
123 // Matrix Megolm Session\r
124 \r
125 typedef struct MatrixMegolmInSession {\r
126     OlmInboundGroupSession * session;\r
127 } MatrixMegolmInSession;\r
128 \r
129 typedef struct MatrixMegolmOutSession {\r
130     char roomId[ROOM_ID_SIZE];\r
131 \r
132     OlmOutboundGroupSession * session;\r
133     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
134 \r
135     char id[MEGOLM_SESSION_ID_SIZE];\r
136     char key[MEGOLM_SESSION_KEY_SIZE];\r
137 } MatrixMegolmOutSession;\r
138 \r
139 bool\r
140 MatrixMegolmOutSessionInit(\r
141     MatrixMegolmOutSession * session,\r
142     const char * roomId);\r
143 \r
144 bool\r
145 MatrixMegolmOutSessionEncrypt(\r
146     MatrixMegolmOutSession * session,\r
147     const char * plaintext,\r
148     char * outBuffer, int outBufferCap);\r
149 \r
150 bool\r
151 MatrixMegolmOutSessionSave(\r
152     MatrixMegolmOutSession * session,\r
153     const char * filename,\r
154     const char * key);\r
155     \r
156 bool\r
157 MatrixMegolmOutSessionLoad(\r
158     MatrixMegolmOutSession * session,\r
159     const char * filename,\r
160     const char * key);\r
161 \r
162 \r
163 // Matrix Client\r
164 \r
165 typedef struct MatrixClient {\r
166     MatrixOlmAccount olmAccount;\r
167 \r
168     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
169     int numMegolmInSessions;\r
170     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
171     int numMegolmOutSessions;\r
172     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
173     int numOlmSessions;\r
174     \r
175     MatrixDevice devices[NUM_DEVICES];\r
176     int numDevices;\r
177     \r
178     // char deviceKey[DEVICE_KEY_SIZE];\r
179     // char signingKey[DEVICE_KEY_SIZE];\r
180 \r
181     char userId[USER_ID_SIZE];\r
182     char server[SERVER_SIZE];\r
183     char accessToken[ACCESS_TOKEN_SIZE];\r
184     char deviceId[DEVICE_ID_SIZE];\r
185     char expireMs[EXPIRE_MS_SIZE];\r
186     char refreshToken[REFRESH_TOKEN_SIZE];\r
187 \r
188     void * httpUserData;\r
189 } MatrixClient;\r
190 \r
191 bool\r
192 MatrixClientInit(\r
193     MatrixClient * client,\r
194     const char * server);\r
195 \r
196 bool\r
197 MatrixClientSave(\r
198     MatrixClient * client,\r
199     const char * filename);\r
200 \r
201 bool\r
202 MatrixClientLoad(\r
203     MatrixClient * client,\r
204     const char * filename);\r
205 \r
206 bool\r
207 MatrixClientSetAccessToken(\r
208     MatrixClient * client,\r
209     const char * accessToken);\r
210 \r
211 bool\r
212 MatrixClientSetDeviceId(\r
213     MatrixClient * client,\r
214     const char * deviceId);\r
215 \r
216 bool\r
217 MatrixClientSetUserId(\r
218     MatrixClient * client,\r
219     const char * userId);\r
220 \r
221 bool\r
222 MatrixClientGenerateOnetimeKeys(\r
223     MatrixClient * client,\r
224     int numberOfKeys);\r
225 \r
226 bool\r
227 MatrixClientUploadOnetimeKeys(\r
228     MatrixClient * client);\r
229 \r
230 bool\r
231 MatrixClientUploadDeviceKey(\r
232     MatrixClient * client);\r
233 \r
234 bool\r
235 MatrixClientClaimOnetimeKey(\r
236     MatrixClient * client,\r
237     const char * userId,\r
238     const char * deviceId,\r
239     char * outOnetimeKey, int outOnetimeKeyCap);\r
240 \r
241 bool\r
242 MatrixClientLoginPassword(\r
243     MatrixClient * client,\r
244     const char * username,\r
245     const char * password,\r
246     const char * displayName);\r
247     \r
248 bool\r
249 MatrixClientSendEvent(\r
250     MatrixClient * client,\r
251     const char * roomId,\r
252     const char * msgType,\r
253     const char * msgBody);\r
254     \r
255 bool\r
256 MatrixClientSendEventEncrypted(\r
257     MatrixClient * client,\r
258     const char * roomId,\r
259     const char * msgType,\r
260     const char * msgBody);\r
261 \r
262 bool\r
263 MatrixClientSync(\r
264     MatrixClient * client,\r
265     char * outSync, int outSyncCap);\r
266 \r
267 bool\r
268 MatrixClientShareMegolmOutSession(\r
269     MatrixClient * client,\r
270     const char * userId,\r
271     const char * deviceId,\r
272     MatrixMegolmOutSession * session);\r
273 \r
274 bool\r
275 MatrixClientShareMegolmOutSessionTest(\r
276     MatrixClient * client,\r
277     const char * userId,\r
278     const char * deviceId,\r
279     MatrixMegolmOutSession * session);\r
280 \r
281 bool\r
282 MatrixClientGetMegolmOutSession(\r
283     MatrixClient * client,\r
284     const char * roomId,\r
285     MatrixMegolmOutSession ** outSession);\r
286 \r
287 bool\r
288 MatrixClientSetMegolmOutSession(\r
289     MatrixClient * client,\r
290     const char * roomId,\r
291     MatrixMegolmOutSession session);\r
292 \r
293 bool\r
294 MatrixClientInitMegolmOutSession(\r
295     MatrixClient * client,\r
296     const char * roomId);\r
297 \r
298 bool\r
299 MatrixClientGetOlmSession(\r
300     MatrixClient * client,\r
301     const char * userId,\r
302     const char * deviceId,\r
303     MatrixOlmSession ** outSession);\r
304 \r
305 bool\r
306 MatrixClientSendToDevice(\r
307     MatrixClient * client,\r
308     const char * userId,\r
309     const char * deviceId,\r
310     const char * message,\r
311     const char * msgType);\r
312 \r
313 bool\r
314 MatrixClientSendToDeviceEncrypted(\r
315     MatrixClient * client,\r
316     const char * userId,\r
317     const char * deviceId,\r
318     const char * message,\r
319     const char * msgType);\r
320 \r
321 bool\r
322 MatrixClientRequestDeviceKey(\r
323     MatrixClient * client,\r
324     const char * deviceId,\r
325     char * outDeviceKey, int outDeviceKeyCap);\r
326     \r
327 bool\r
328 MatrixClientRequestSigningKey(\r
329     MatrixClient * client,\r
330     const char * deviceId,\r
331     char * outSigningKey, int outSigningKeyCap);\r
332 \r
333 bool\r
334 MatrixClientRequestDeviceKeys(\r
335     MatrixClient * client);\r
336 \r
337 bool\r
338 MatrixClientDeleteDevice(\r
339     MatrixClient * client);\r
340 \r
341 \r
342 \r
343 \r
344 bool\r
345 MatrixHttpInit(\r
346     MatrixClient * client);\r
347 \r
348 bool\r
349 MatrixHttpConnect(\r
350     MatrixClient * client);\r
351 \r
352 bool\r
353 MatrixHttpDeinit(\r
354     MatrixClient * client);\r
355 \r
356 bool\r
357 MatrixHttpGet(\r
358     MatrixClient * client,\r
359     const char * url,\r
360     char * outResponseBuffer, int outResponseCap,\r
361     bool authenticated);\r
362 \r
363 bool\r
364 MatrixHttpPost(\r
365     MatrixClient * client,\r
366     const char * url,\r
367     const char * requestBuffer,\r
368     char * outResponseBuffer, int outResponseCap,\r
369     bool authenticated);\r
370 \r
371 bool\r
372 MatrixHttpPut(\r
373     MatrixClient * client,\r
374     const char * url,\r
375     const char * requestBuffer,\r
376     char * outResponseBuffer, int outResponseCap,\r
377     bool authenticated);\r
378 \r
379 // util\r
380 \r
381 void\r
382 Randomize(uint8_t * random, int randomLen);\r
383 \r
384 bool\r
385 JsonEscape(\r
386     const char * sIn, int sInLen,\r
387     char * sOut, int sOutCap);\r
388     \r
389 bool\r
390 JsonSign(\r
391     MatrixClient * client,\r
392     const char * sIn, int sInLen,\r
393     char * sOut, int sOutCap);\r
394 \r
395 #endif\r