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