13 #define USER_ID_SIZE 64
\r
14 #define ROOM_ID_SIZE 128
\r
15 #define SERVER_SIZE 20
\r
16 #define ACCESS_TOKEN_SIZE 40
\r
17 #define DEVICE_ID_SIZE 20
\r
18 #define EXPIRE_MS_SIZE 20
\r
19 #define REFRESH_TOKEN_SIZE 20
\r
20 #define MAX_URL_LEN 1024
\r
22 #define OLM_IDENTITY_KEYS_JSON_SIZE 128
\r
23 #define DEVICE_KEY_SIZE 44
\r
24 #define SIGNING_KEY_SIZE 44
\r
25 #define ONETIME_KEY_SIZE 44
\r
26 #define MASTER_KEY_SIZE 44
\r
28 #define KEY_SHARE_EVENT_LEN 1024
\r
30 #define OLM_ACCOUNT_MEMORY_SIZE 7528
\r
31 #define OLM_ACCOUNT_RANDOM_SIZE (32+32)
\r
33 #define OLM_SESSION_MEMORY_SIZE 3352
\r
34 #define OLM_ENCRYPT_RANDOM_SIZE 32
\r
35 #define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)
\r
37 #define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)
\r
38 #define OLM_KEY_ID_SIZE 32
\r
40 #define OLM_SIGNATURE_SIZE 128
\r
42 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232
\r
43 #define MEGOLM_SESSION_ID_SIZE 44
\r
44 #define MEGOLM_SESSION_KEY_SIZE 306
\r
45 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)
\r
47 #define JSON_ONETIME_KEY_SIZE 128
\r
48 #define JSON_ONETIME_KEY_SIGNED_SIZE 256
\r
49 #define JSON_SIGNATURE_SIZE 256
\r
51 #define NUM_MEGOLM_SESSIONS 2
\r
52 #define NUM_OLM_SESSIONS 2
\r
53 #define NUM_DEVICES 10
\r
57 typedef struct MatrixHttpConnection MatrixHttpConnection;
\r
61 MatrixHttpConnection ** hc,
\r
66 MatrixHttpConnection ** hc);
\r
69 MatrixHttpSetAccessToken(
\r
70 MatrixHttpConnection * hc,
\r
71 const char * accessToken);
\r
75 MatrixHttpConnection * hc,
\r
77 char * outResponseBuffer, int outResponseCap,
\r
78 bool authenticated);
\r
82 MatrixHttpConnection * hc,
\r
84 const char * requestBuffer,
\r
85 char * outResponseBuffer, int outResponseCap,
\r
86 bool authenticated);
\r
90 MatrixHttpConnection * hc,
\r
92 const char * requestBuffer,
\r
93 char * outResponseBuffer, int outResponseCap,
\r
94 bool authenticated);
\r
100 typedef struct MatrixDevice {
\r
101 char deviceId[DEVICE_ID_SIZE];
\r
102 char deviceKey[DEVICE_KEY_SIZE];
\r
103 char signingKey[SIGNING_KEY_SIZE];
\r
107 // Matrix Olm Account
\r
109 typedef struct MatrixOlmAccount {
\r
110 OlmAccount * account;
\r
111 char memory[OLM_ACCOUNT_MEMORY_SIZE];
\r
112 } MatrixOlmAccount;
\r
115 MatrixOlmAccountInit(
\r
116 MatrixOlmAccount * account);
\r
119 MatrixOlmAccountUnpickle(
\r
120 MatrixOlmAccount * account,
\r
121 void * pickled, int pickledLen,
\r
122 const void * key, int keyLen);
\r
125 MatrixOlmAccountGetDeviceKey(
\r
126 MatrixOlmAccount * account,
\r
127 char * key, int keyCap);
\r
130 MatrixOlmAccountGetSigningKey(
\r
131 MatrixOlmAccount * account,
\r
132 char * key, int keyCap);
\r
135 // Matrix Olm Session
\r
137 typedef struct MatrixOlmSession {
\r
138 const char * deviceId; // TODO: char[]
\r
141 OlmSession * session;
\r
142 char memory[OLM_SESSION_MEMORY_SIZE];
\r
143 } MatrixOlmSession;
\r
146 MatrixOlmSessionUnpickle(
\r
147 MatrixOlmSession * session,
\r
148 const char * deviceId,
\r
149 void * pickled, int pickledLen,
\r
150 const void * key, int keyLen);
\r
153 MatrixOlmSessionFrom(
\r
154 MatrixOlmSession * session,
\r
155 OlmAccount * olmAccount,
\r
156 const char * deviceId,
\r
157 const char * deviceKey,
\r
158 const char * encrypted);
\r
161 MatrixOlmSessionTo(
\r
162 MatrixOlmSession * session,
\r
163 OlmAccount * olmAccount,
\r
164 const char * deviceId,
\r
165 const char * deviceKey,
\r
166 const char * deviceOnetimeKey);
\r
169 MatrixOlmSessionEncrypt(
\r
170 MatrixOlmSession * session,
\r
171 const char * plaintext,
\r
172 char * outBuffer, int outBufferCap);
\r
175 MatrixOlmSessionDecrypt(
\r
176 MatrixOlmSession * session,
\r
177 size_t messageType,
\r
179 char * outBuffer, int outBufferCap);
\r
182 // Matrix Megolm Session
\r
184 typedef struct MatrixMegolmInSession {
\r
185 char roomId[ROOM_ID_SIZE];
\r
186 char id[MEGOLM_SESSION_ID_SIZE];
\r
187 char key[MEGOLM_SESSION_KEY_SIZE];
\r
189 OlmInboundGroupSession * session;
\r
190 char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
\r
192 } MatrixMegolmInSession;
\r
195 MatrixMegolmInSessionInit(
\r
196 MatrixMegolmInSession * session,
\r
197 const char * roomId,
\r
198 const char * sessionId,
\r
199 const char * sessionKey, int sessionKeyLen);
\r
202 MatrixMegolmInSessionDecrypt(
\r
203 MatrixMegolmInSession * session,
\r
204 const char * encrypted, int encryptedLen,
\r
205 char * outDecrypted, int outDecryptedCap);
\r
207 typedef struct MatrixMegolmOutSession {
\r
208 char roomId[ROOM_ID_SIZE];
\r
209 char id[MEGOLM_SESSION_ID_SIZE];
\r
210 char key[MEGOLM_SESSION_KEY_SIZE];
\r
212 OlmOutboundGroupSession * session;
\r
213 char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];
\r
214 } MatrixMegolmOutSession;
\r
217 MatrixMegolmOutSessionInit(
\r
218 MatrixMegolmOutSession * session,
\r
219 const char * roomId);
\r
222 MatrixMegolmOutSessionEncrypt(
\r
223 MatrixMegolmOutSession * session,
\r
224 const char * plaintext,
\r
225 char * outBuffer, int outBufferCap);
\r
230 typedef struct MatrixClient {
\r
231 MatrixOlmAccount olmAccount;
\r
233 MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];
\r
234 int numMegolmInSessions;
\r
235 MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];
\r
236 int numMegolmOutSessions;
\r
237 MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];
\r
238 int numOlmSessions;
\r
240 MatrixDevice devices[NUM_DEVICES];
\r
243 // char deviceKey[DEVICE_KEY_SIZE];
\r
244 // char signingKey[DEVICE_KEY_SIZE];
\r
246 char userId[USER_ID_SIZE];
\r
247 char accessToken[ACCESS_TOKEN_SIZE];
\r
248 char deviceId[DEVICE_ID_SIZE];
\r
249 char expireMs[EXPIRE_MS_SIZE];
\r
250 char refreshToken[REFRESH_TOKEN_SIZE];
\r
251 char masterKey[MASTER_KEY_SIZE];
\r
255 MatrixHttpConnection * hc;
\r
260 MatrixClient * client);
\r
263 MatrixClientSetAccessToken(
\r
264 MatrixClient * client,
\r
265 const char * accessToken);
\r
268 MatrixClientSetDeviceId(
\r
269 MatrixClient * client,
\r
270 const char * deviceId);
\r
273 MatrixClientSetUserId(
\r
274 MatrixClient * client,
\r
275 const char * userId);
\r
278 MatrixClientGenerateOnetimeKeys(
\r
279 MatrixClient * client,
\r
283 MatrixClientUploadOnetimeKeys(
\r
284 MatrixClient * client);
\r
287 MatrixClientUploadDeviceKeys(
\r
288 MatrixClient * client);
\r
291 MatrixClientClaimOnetimeKey(
\r
292 MatrixClient * client,
\r
293 const char * userId,
\r
294 const char * deviceId,
\r
295 char * outOnetimeKey, int outOnetimeKeyCap);
\r
298 MatrixClientLoginPassword(
\r
299 MatrixClient * client,
\r
300 const char * username,
\r
301 const char * password,
\r
302 const char * displayName);
\r
305 MatrixClientSendEvent(
\r
306 MatrixClient * client,
\r
307 const char * roomId,
\r
308 const char * msgType,
\r
309 const char * msgBody);
\r
312 MatrixClientSendEventEncrypted(
\r
313 MatrixClient * client,
\r
314 const char * roomId,
\r
315 const char * msgType,
\r
316 const char * msgBody);
\r
320 MatrixClient * client,
\r
321 const char * event, int eventLen);
\r
325 MatrixClient * client,
\r
326 const char * room, int roomLen,
\r
327 const char * event, int eventLen);
\r
331 MatrixClient * client,
\r
332 char * outSyncBuffer, int outSyncCap,
\r
333 char * nextBatch, int nextBatchCap);
\r
336 MatrixClientGetRoomEvent(
\r
337 MatrixClient * client,
\r
338 const char * roomId,
\r
339 const char * eventId,
\r
340 char * outEvent, int outEventCap);
\r
343 MatrixClientShareMegolmOutSession(
\r
344 MatrixClient * client,
\r
345 const char * userId,
\r
346 const char * deviceId,
\r
347 MatrixMegolmOutSession * session);
\r
350 MatrixClientShareMegolmOutSessionTest(
\r
351 MatrixClient * client,
\r
352 const char * userId,
\r
353 const char * deviceId,
\r
354 MatrixMegolmOutSession * session);
\r
357 MatrixClientGetMegolmOutSession(
\r
358 MatrixClient * client,
\r
359 const char * roomId,
\r
360 MatrixMegolmOutSession ** outSession);
\r
363 MatrixClientNewMegolmOutSession(
\r
364 MatrixClient * client,
\r
365 const char * roomId,
\r
366 MatrixMegolmOutSession ** outSession);
\r
369 MatrixClientGetMegolmInSession(
\r
370 MatrixClient * client,
\r
371 const char * roomId, int roomIdLen,
\r
372 const char * sessionId, int sessionIdLen,
\r
373 MatrixMegolmInSession ** outSession);
\r
376 MatrixClientNewMegolmInSession(
\r
377 MatrixClient * client,
\r
378 const char * roomId,
\r
379 const char * sessionId,
\r
380 const char * sessionKey,
\r
381 MatrixMegolmInSession ** outSession);
\r
384 MatrixClientRequestMegolmInSession(
\r
385 MatrixClient * client,
\r
386 const char * roomId,
\r
387 const char * sessionId,
\r
388 const char * senderKey,
\r
389 const char * userId,
\r
390 const char * deviceId); // TODO: remove deviceId (query all devices)
\r
393 MatrixClientGetOlmSession(
\r
394 MatrixClient * client,
\r
395 const char * userId,
\r
396 const char * deviceId,
\r
397 MatrixOlmSession ** outSession);
\r
400 MatrixClientNewOlmSessionIn(
\r
401 MatrixClient * client,
\r
402 const char * userId,
\r
403 const char * deviceId,
\r
404 const char * encrypted,
\r
405 MatrixOlmSession ** outSession);
\r
408 MatrixClientNewOlmSessionOut(
\r
409 MatrixClient * client,
\r
410 const char * userId,
\r
411 const char * deviceId,
\r
412 MatrixOlmSession ** outSession);
\r
415 MatrixClientSendToDevice(
\r
416 MatrixClient * client,
\r
417 const char * userId,
\r
418 const char * deviceId,
\r
419 const char * message,
\r
420 const char * msgType);
\r
423 MatrixClientSendToDeviceEncrypted(
\r
424 MatrixClient * client,
\r
425 const char * userId,
\r
426 const char * deviceId,
\r
427 const char * message,
\r
428 const char * msgType);
\r
431 MatrixClientSendDummy(
\r
432 MatrixClient * client,
\r
433 const char * userId,
\r
434 const char * deviceId);
\r
437 MatrixClientRequestDeviceKey(
\r
438 MatrixClient * client,
\r
439 const char * deviceId,
\r
440 char * outDeviceKey, int outDeviceKeyCap);
\r
443 MatrixClientRequestSigningKey(
\r
444 MatrixClient * client,
\r
445 const char * deviceId,
\r
446 char * outSigningKey, int outSigningKeyCap);
\r
449 MatrixClientRequestMasterKey(
\r
450 MatrixClient * client,
\r
451 char * outMasterKey, int outMasterKeyCap);
\r
454 MatrixClientRequestDeviceKeys(
\r
455 MatrixClient * client);
\r
458 MatrixClientDeleteDevice(
\r
459 MatrixClient * client);
\r
465 Randomize(uint8_t * random, int randomLen);
\r
469 const char * sIn, int sInLen,
\r
470 char * sOut, int sOutCap);
\r
474 const char * sIn, int sInLen,
\r
475 char * sOut, int sOutCap);
\r
479 MatrixClient * client,
\r
480 const char * sIn, int sInLen,
\r
481 char * sOut, int sOutCap);
\r