]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
a06eab9e0edb7c0a510819f3bf4f101ab5019caf
[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 SERVER_SIZE 20\r
15 #define ACCESS_TOKEN_SIZE 40\r
16 #define DEVICE_ID_SIZE 20\r
17 #define EXPIRE_MS_SIZE 20\r
18 #define REFRESH_TOKEN_SIZE 20\r
19 #define MAX_URL_LEN 128\r
20 \r
21 #define OLM_IDENTITY_KEYS_JSON_SIZE 128\r
22 #define DEVICE_KEY_SIZE 44\r
23 #define SIGNING_KEY_SIZE 44\r
24 #define ONETIME_KEY_SIZE 44\r
25 \r
26 #define KEY_SHARE_EVENT_LEN 1024\r
27 \r
28 #define OLM_ACCOUNT_MEMORY_SIZE 7528\r
29 #define OLM_ACCOUNT_RANDOM_SIZE (32+32)\r
30 \r
31 #define OLM_SESSION_MEMORY_SIZE 3352\r
32 #define OLM_ENCRYPT_RANDOM_SIZE 32\r
33 #define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)\r
34 \r
35 #define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)\r
36 #define OLM_KEY_ID_SIZE 32\r
37 \r
38 #define OLM_SIGNATURE_SIZE 128\r
39 \r
40 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232\r
41 #define MEGOLM_SESSION_ID_SIZE 44\r
42 #define MEGOLM_SESSION_KEY_SIZE 306\r
43 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)\r
44 \r
45 #define JSON_ONETIME_KEY_SIZE 128\r
46 #define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
47 #define JSON_SIGNATURE_SIZE 256\r
48 \r
49 #define NUM_MEGOLM_SESSIONS 10\r
50 #define NUM_OLM_SESSIONS 10\r
51 #define NUM_DEVICES 10\r
52 \r
53 void\r
54 Randomize(uint8_t * random, int randomLen);\r
55 \r
56 bool\r
57 JsonEscape(\r
58     const char * sIn, int sInLen,\r
59     char * sOut, int sOutCap);\r
60     \r
61 bool JsonSign(\r
62     const char * sIn, int sInLen,\r
63     char * sOut, int sOutCap);\r
64 \r
65 // Matrix Device\r
66 \r
67 typedef struct MatrixDevice {\r
68     char deviceId[DEVICE_ID_SIZE];\r
69     char deviceKey[DEVICE_KEY_SIZE];\r
70 } MatrixDevice;\r
71 \r
72 \r
73 // Matrix Olm Account\r
74 \r
75 typedef struct MatrixOlmAccount {\r
76     OlmAccount * account;\r
77     char memory[OLM_ACCOUNT_MEMORY_SIZE];\r
78 } MatrixOlmAccount;\r
79 \r
80 bool\r
81 MatrixOlmAccountInit(\r
82     MatrixOlmAccount * account);\r
83 \r
84 \r
85 // Matrix Olm Session\r
86 \r
87 typedef struct MatrixOlmSession {\r
88     const char * deviceId;\r
89 \r
90     int type;\r
91     OlmSession * session;\r
92     char memory[OLM_SESSION_MEMORY_SIZE];\r
93 } MatrixOlmSession;\r
94 \r
95 bool\r
96 MatrixOlmSessionTo(\r
97     MatrixOlmSession * session,\r
98     OlmAccount * olmAccount,\r
99     const char * deviceId,\r
100     const char * deviceKey,\r
101     const char * deviceOnetimeKey);\r
102 \r
103 bool\r
104 MatrixOlmSessionEncrypt(\r
105     MatrixOlmSession * session,\r
106     const char * plaintext,\r
107     char * outBuffer, int outBufferCap);\r
108 \r
109 \r
110 // Matrix Megolm Session\r
111 \r
112 typedef struct MatrixMegolmInSession {\r
113     OlmInboundGroupSession * session;\r
114 } MatrixMegolmInSession;\r
115 \r
116 typedef struct MatrixMegolmOutSession {\r
117     const char * roomId;\r
118 \r
119     OlmOutboundGroupSession * session;\r
120     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
121 \r
122     char id[MEGOLM_SESSION_ID_SIZE];\r
123     char key[MEGOLM_SESSION_KEY_SIZE];\r
124 } MatrixMegolmOutSession;\r
125 \r
126 bool\r
127 MatrixMegolmOutSessionInit(\r
128     MatrixMegolmOutSession * session,\r
129     const char * roomId);\r
130     \r
131 bool\r
132 MatrixMegolmOutSessionEncrypt(\r
133     MatrixMegolmOutSession * session,\r
134     const char * plaintext,\r
135     char * outBuffer, int outBufferCap);\r
136 \r
137 \r
138 // Matrix Client\r
139 \r
140 typedef struct MatrixClient {\r
141     MatrixOlmAccount olmAccount;\r
142 \r
143     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
144     int numMegolmInSessions;\r
145     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
146     int numMegolmOutSessions;\r
147     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
148     int numOlmSessions;\r
149     \r
150     MatrixDevice devices[NUM_DEVICES];\r
151     int numDevices;\r
152     \r
153     char deviceKey[DEVICE_KEY_SIZE];\r
154     char signingKey[DEVICE_KEY_SIZE];\r
155 \r
156     char userId[USER_ID_SIZE];\r
157     char server[SERVER_SIZE];\r
158     char accessToken[ACCESS_TOKEN_SIZE];\r
159     char deviceId[DEVICE_ID_SIZE];\r
160     char expireMs[EXPIRE_MS_SIZE];\r
161     char refreshToken[REFRESH_TOKEN_SIZE];\r
162 \r
163     void * httpUserData;\r
164 } MatrixClient;\r
165 \r
166 bool\r
167 MatrixClientInit(\r
168     MatrixClient * client,\r
169     const char * server);\r
170 \r
171 bool\r
172 MatrixClientSetAccessToken(\r
173     MatrixClient * client,\r
174     const char * accessToken);\r
175 \r
176 bool\r
177 MatrixClientSetDeviceId(\r
178     MatrixClient * client,\r
179     const char * deviceId);\r
180 \r
181 bool\r
182 MatrixClientSetUserId(\r
183     MatrixClient * client,\r
184     const char * userId);\r
185 \r
186 bool\r
187 MatrixClientGenerateOnetimeKeys(\r
188     MatrixClient * client,\r
189     int numberOfKeys);\r
190 \r
191 bool\r
192 MatrixClientUploadOnetimeKeys(\r
193     MatrixClient * client);\r
194 \r
195 bool\r
196 MatrixClientUploadDeviceKeys(\r
197     MatrixClient * client);\r
198 \r
199 bool\r
200 MatrixClientClaimOnetimeKey(\r
201     MatrixClient * client,\r
202     const char * userId,\r
203     const char * deviceId,\r
204     char * outOnetimeKey, int outOnetimeKeyCap);\r
205 \r
206 bool\r
207 MatrixClientLoginPassword(\r
208     MatrixClient * client,\r
209     const char * username,\r
210     const char * password,\r
211     const char * displayName);\r
212     \r
213 bool\r
214 MatrixClientSendEvent(\r
215     MatrixClient * client,\r
216     const char * roomId,\r
217     const char * msgType,\r
218     const char * msgBody);\r
219     \r
220 bool\r
221 MatrixClientSendEventEncrypted(\r
222     MatrixClient * client,\r
223     const char * roomId,\r
224     const char * msgType,\r
225     const char * msgBody);\r
226 \r
227 bool\r
228 MatrixClientSync(\r
229     MatrixClient * client,\r
230     char * outSync, int outSyncCap);\r
231 \r
232 bool\r
233 MatrixClientShareMegolmOutSession(\r
234     MatrixClient * client,\r
235     const char * userId,\r
236     const char * deviceId,\r
237     MatrixMegolmOutSession * session);\r
238 \r
239 bool\r
240 MatrixClientShareMegolmOutSessionTest(\r
241     MatrixClient * client,\r
242     const char * deviceId,\r
243     MatrixMegolmOutSession * session);\r
244 \r
245 bool\r
246 MatrixClientGetMegolmOutSession(\r
247     MatrixClient * client,\r
248     const char * roomId,\r
249     MatrixMegolmOutSession ** outSession);\r
250 \r
251 bool\r
252 MatrixClientSetMegolmOutSession(\r
253     MatrixClient * client,\r
254     const char * roomId,\r
255     MatrixMegolmOutSession session);\r
256 \r
257 bool\r
258 MatrixClientGetOlmSession(\r
259     MatrixClient * client,\r
260     const char * userId,\r
261     const char * deviceId,\r
262     MatrixOlmSession ** outSession);\r
263 \r
264 bool\r
265 MatrixClientSendToDevice(\r
266     MatrixClient * client,\r
267     const char * userId,\r
268     const char * deviceId,\r
269     const char * message,\r
270     const char * msgType);\r
271 \r
272 bool\r
273 MatrixClientSendToDeviceEncrypted(\r
274     MatrixClient * client,\r
275     const char * userId,\r
276     const char * deviceId,\r
277     const char * message,\r
278     const char * msgType);\r
279 \r
280 bool\r
281 MatrixClientGetDeviceKey(\r
282     MatrixClient * client,\r
283     const char * deviceId,\r
284     char * outDeviceKey, int outDeviceKeyCap);\r
285 \r
286 bool\r
287 MatrixClientGetDeviceKey(\r
288     MatrixClient * client,\r
289     const char * deviceId,\r
290     char * outDeviceKey, int outDeviceKeyCap);\r
291 \r
292 bool\r
293 MatrixClientRequestDeviceKeys(\r
294     MatrixClient * client);\r
295 \r
296 \r
297 \r
298 bool\r
299 MatrixHttpInit(\r
300     MatrixClient * client);\r
301 \r
302 bool\r
303 MatrixHttpConnect(\r
304     MatrixClient * client);\r
305 \r
306 bool\r
307 MatrixHttpDeinit(\r
308     MatrixClient * client);\r
309 \r
310 bool\r
311 MatrixHttpGet(\r
312     MatrixClient * client,\r
313     const char * url,\r
314     char * outResponseBuffer, int outResponseCap,\r
315     bool authenticated);\r
316 \r
317 bool\r
318 MatrixHttpPost(\r
319     MatrixClient * client,\r
320     const char * url,\r
321     const char * requestBuffer,\r
322     char * outResponseBuffer, int outResponseCap,\r
323     bool authenticated);\r
324 \r
325 bool\r
326 MatrixHttpPut(\r
327     MatrixClient * client,\r
328     const char * url,\r
329     const char * requestBuffer,\r
330     char * outResponseBuffer, int outResponseCap,\r
331     bool authenticated);\r
332 \r
333 #endif\r