]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix.h
add libolm.a to Makefile
[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 MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232\r
34 #define MEGOLM_SESSION_ID_SIZE 44\r
35 #define MEGOLM_SESSION_KEY_SIZE 306\r
36 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)\r
37 \r
38 #define NUM_MEGOLM_SESSIONS 10\r
39 #define NUM_OLM_SESSIONS 10\r
40 #define NUM_DEVICES 10\r
41 \r
42 void\r
43 Randomize(uint8_t * random, int randomLen);\r
44 \r
45 bool\r
46 JsonEscape(\r
47     char * sIn, int sInLen,\r
48     char * sOut, int sOutCap);\r
49 \r
50 typedef struct MatrixDevice {\r
51     char deviceId[DEVICE_ID_SIZE];\r
52     char deviceKey[DEVICE_KEY_SIZE];\r
53 } MatrixDevice;\r
54 \r
55 typedef struct MatrixOlmSession {\r
56     const char * deviceId;\r
57 \r
58     int type;\r
59     OlmSession * session;\r
60     char memory[OLM_SESSION_MEMORY_SIZE];\r
61 } MatrixOlmSession;\r
62 \r
63 bool\r
64 MatrixOlmSessionInit(\r
65     MatrixOlmSession * session,\r
66     const char * deviceId);\r
67 \r
68 bool\r
69 MatrixOlmSessionEncrypt(\r
70     MatrixOlmSession * session,\r
71     const char * plaintext,\r
72     char * outBuffer, int outBufferCap);\r
73 \r
74 \r
75 \r
76 typedef struct MatrixMegolmInSession {\r
77     OlmInboundGroupSession * session;\r
78 } MatrixMegolmInSession;\r
79 \r
80 typedef struct MatrixMegolmOutSession {\r
81     const char * roomId;\r
82 \r
83     OlmOutboundGroupSession * session;\r
84     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
85 \r
86     char id[MEGOLM_SESSION_ID_SIZE];\r
87     char key[MEGOLM_SESSION_KEY_SIZE];\r
88 } MatrixMegolmOutSession;\r
89 \r
90 bool\r
91 MatrixMegolmOutSessionInit(\r
92     MatrixMegolmOutSession * session,\r
93     const char * roomId);\r
94     \r
95 bool\r
96 MatrixMegolmOutSessionEncrypt(\r
97     MatrixMegolmOutSession * session,\r
98     const char * plaintext,\r
99     char * outBuffer, int outBufferCap);\r
100 \r
101 \r
102 \r
103 typedef struct MatrixClient {\r
104     OlmAccount * olmAccount;\r
105     char olmAccountMemory[OLM_ACCOUNT_MEMORY_SIZE];\r
106 \r
107     MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
108     int numMegolmInSessions;\r
109     MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
110     int numMegolmOutSessions;\r
111     MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
112     int numOlmSessions;\r
113     \r
114     MatrixDevice devices[NUM_DEVICES];\r
115     int numDevices;\r
116     \r
117     char deviceKey[DEVICE_KEY_SIZE];\r
118     char signingKey[DEVICE_KEY_SIZE];\r
119 \r
120     char userId[USER_ID_SIZE];\r
121     char server[SERVER_SIZE];\r
122     char accessToken[ACCESS_TOKEN_SIZE];\r
123     char deviceId[DEVICE_ID_SIZE];\r
124     char expireMs[EXPIRE_MS_SIZE];\r
125     char refreshToken[REFRESH_TOKEN_SIZE];\r
126 \r
127     void * httpUserData;\r
128 } MatrixClient;\r
129 \r
130 bool\r
131 MatrixClientInit(\r
132     MatrixClient * client,\r
133     const char * server);\r
134 \r
135 bool\r
136 MatrixClientSetAccessToken(\r
137     MatrixClient * client,\r
138     const char * accessToken);\r
139 \r
140 bool\r
141 MatrixClientSetDeviceId(\r
142     MatrixClient * client,\r
143     const char * deviceId);\r
144 \r
145 bool\r
146 MatrixClientLoginPassword(\r
147     MatrixClient * client,\r
148     const char * username,\r
149     const char * password,\r
150     const char * displayName);\r
151     \r
152 bool\r
153 MatrixClientSendEvent(\r
154     MatrixClient * client,\r
155     const char * roomId,\r
156     const char * msgType,\r
157     const char * msgBody);\r
158     \r
159 bool\r
160 MatrixClientSendEventEncrypted(\r
161     MatrixClient * client,\r
162     const char * roomId,\r
163     const char * msgType,\r
164     const char * msgBody);\r
165 \r
166 bool\r
167 MatrixClientSync(\r
168     MatrixClient * client,\r
169     char * outSyncBuffer, int outSyncCap);\r
170 \r
171 bool\r
172 MatrixClientShareMegolmOutSession(\r
173     MatrixClient * client,\r
174     const char * deviceId,\r
175     MatrixMegolmOutSession * session);\r
176 \r
177 bool\r
178 MatrixClientGetMegolmOutSession(\r
179     MatrixClient * client,\r
180     const char * roomId,\r
181     MatrixMegolmOutSession ** outSession);\r
182 \r
183 bool\r
184 MatrixClientSetMegolmOutSession(\r
185     MatrixClient * client,\r
186     const char * roomId,\r
187     MatrixMegolmOutSession session);\r
188 \r
189 bool\r
190 MatrixClientGetOlmSession(\r
191     MatrixClient * client,\r
192     const char * deviceId,\r
193     MatrixOlmSession ** outSession);\r
194 \r
195 bool\r
196 MatrixClientSendToDevice(\r
197     MatrixClient * client,\r
198     const char * userId,\r
199     const char * deviceId,\r
200     const char * message,\r
201     const char * msgType);\r
202 \r
203 bool\r
204 MatrixClientSendToDeviceEncrypted(\r
205     MatrixClient * client,\r
206     const char * userId,\r
207     const char * deviceId,\r
208     const char * message,\r
209     const char * msgType);\r
210 \r
211 bool\r
212 MatrixClientGetDeviceKey(\r
213     MatrixClient * client,\r
214     const char * deviceId,\r
215     char * outDeviceKey, int outDeviceKeyCap);\r
216 \r
217 bool\r
218 MatrixClientGetDeviceKey(\r
219     MatrixClient * client,\r
220     const char * deviceId,\r
221     char * outDeviceKey, int outDeviceKeyCap);\r
222 \r
223 bool\r
224 MatrixClientRequestDeviceKeys(\r
225     MatrixClient * client);\r
226 \r
227 \r
228 \r
229 bool\r
230 MatrixHttpInit(\r
231     MatrixClient * client);\r
232 \r
233 bool\r
234 MatrixHttpDeinit(\r
235     MatrixClient * client);\r
236 \r
237 bool\r
238 MatrixHttpGet(\r
239     MatrixClient * client,\r
240     const char * url,\r
241     char * outResponseBuffer, int outResponseCap,\r
242     bool authenticated);\r
243 \r
244 bool\r
245 MatrixHttpPost(\r
246     MatrixClient * client,\r
247     const char * url,\r
248     const char * requestBuffer,\r
249     char * outResponseBuffer, int outResponseCap,\r
250     bool authenticated);\r
251 \r
252 bool\r
253 MatrixHttpPut(\r
254     MatrixClient * client,\r
255     const char * url,\r
256     const char * requestBuffer,\r
257     char * outResponseBuffer, int outResponseCap,\r
258     bool authenticated);\r
259 \r
260 #endif\r