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