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