]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix.h
added esp project
[matrix_esp_thesis] / src / matrix.h
index 2e7f6e0e9a6861fa70839a18b2c8dd8ca27b19b4..5e5eabdd6e7d5c90acf652fb451dcbb72feb7da0 100644 (file)
@@ -2,13 +2,15 @@
 #define MATRIX__H\r
 \r
 #include <stdbool.h>\r
+#include <stdlib.h>\r
 #include <string.h>\r
+#include <time.h>\r
 \r
 #include <olm/olm.h>\r
 \r
 \r
 \r
-// TODO: fix\r
+#define USER_ID_SIZE 64\r
 #define SERVER_SIZE 20\r
 #define ACCESS_TOKEN_SIZE 40\r
 #define DEVICE_ID_SIZE 20\r
 #define REFRESH_TOKEN_SIZE 20\r
 #define MAX_URL_LEN 128\r
 \r
+#define OLM_IDENTITY_KEYS_JSON_SIZE 128\r
+#define DEVICE_KEY_SIZE 44\r
+#define SIGNING_KEY_SIZE 44\r
+#define ONETIME_KEY_SIZE 44\r
+\r
+#define KEY_SHARE_EVENT_LEN 1024\r
+\r
+#define OLM_ACCOUNT_MEMORY_SIZE 7528\r
+#define OLM_ACCOUNT_RANDOM_SIZE (32+32)\r
+\r
+#define OLM_SESSION_MEMORY_SIZE 3352\r
+#define OLM_ENCRYPT_RANDOM_SIZE 32\r
+#define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2)\r
+\r
+#define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10)\r
+#define OLM_KEY_ID_SIZE 32\r
+\r
+#define OLM_SIGNATURE_SIZE 128\r
+\r
+#define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232\r
+#define MEGOLM_SESSION_ID_SIZE 44\r
+#define MEGOLM_SESSION_KEY_SIZE 306\r
+#define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32)\r
+\r
+#define JSON_ONETIME_KEY_SIZE 128\r
+#define JSON_ONETIME_KEY_SIGNED_SIZE 256\r
+#define JSON_SIGNATURE_SIZE 256\r
+\r
+#define NUM_MEGOLM_SESSIONS 10\r
+#define NUM_OLM_SESSIONS 10\r
+#define NUM_DEVICES 10\r
+\r
+// Matrix Device\r
+\r
+typedef struct MatrixDevice {\r
+    char deviceId[DEVICE_ID_SIZE];\r
+    char deviceKey[DEVICE_KEY_SIZE];\r
+} MatrixDevice;\r
+\r
+\r
+// Matrix Olm Account\r
+\r
+typedef struct MatrixOlmAccount {\r
+    OlmAccount * account;\r
+    char memory[OLM_ACCOUNT_MEMORY_SIZE];\r
+} MatrixOlmAccount;\r
+\r
+bool\r
+MatrixOlmAccountInit(\r
+    MatrixOlmAccount * account);\r
+\r
+\r
+// Matrix Olm Session\r
+\r
+typedef struct MatrixOlmSession {\r
+    const char * deviceId;\r
+\r
+    int type;\r
+    OlmSession * session;\r
+    char memory[OLM_SESSION_MEMORY_SIZE];\r
+} MatrixOlmSession;\r
+\r
+bool\r
+MatrixOlmSessionTo(\r
+    MatrixOlmSession * session,\r
+    OlmAccount * olmAccount,\r
+    const char * deviceId,\r
+    const char * deviceKey,\r
+    const char * deviceOnetimeKey);\r
+\r
+bool\r
+MatrixOlmSessionEncrypt(\r
+    MatrixOlmSession * session,\r
+    const char * plaintext,\r
+    char * outBuffer, int outBufferCap);\r
+\r
+\r
+// Matrix Megolm Session\r
+\r
+typedef struct MatrixMegolmInSession {\r
+    OlmInboundGroupSession * session;\r
+} MatrixMegolmInSession;\r
+\r
+typedef struct MatrixMegolmOutSession {\r
+    const char * roomId;\r
+\r
+    OlmOutboundGroupSession * session;\r
+    char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
+\r
+    char id[MEGOLM_SESSION_ID_SIZE];\r
+    char key[MEGOLM_SESSION_KEY_SIZE];\r
+} MatrixMegolmOutSession;\r
+\r
+bool\r
+MatrixMegolmOutSessionInit(\r
+    MatrixMegolmOutSession * session,\r
+    const char * roomId);\r
+    \r
+bool\r
+MatrixMegolmOutSessionEncrypt(\r
+    MatrixMegolmOutSession * session,\r
+    const char * plaintext,\r
+    char * outBuffer, int outBufferCap);\r
+\r
+\r
+// Matrix Client\r
 \r
 typedef struct MatrixClient {\r
-    OlmAccount * olmAccount;\r
-    OlmSession * olmSession;\r
+    MatrixOlmAccount olmAccount;\r
+\r
+    MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS];\r
+    int numMegolmInSessions;\r
+    MatrixMegolmOutSession megolmOutSessions[NUM_MEGOLM_SESSIONS];\r
+    int numMegolmOutSessions;\r
+    MatrixOlmSession olmSessions[NUM_OLM_SESSIONS];\r
+    int numOlmSessions;\r
     \r
-    char server[SERVER_SIZE+1];\r
-    char accessTokenBuffer[ACCESS_TOKEN_SIZE];\r
-    char deviceIdBuffer[DEVICE_ID_SIZE];\r
-    char expireMsBuffer[EXPIRE_MS_SIZE];\r
-    char refreshTokenBuffer[REFRESH_TOKEN_SIZE];\r
+    MatrixDevice devices[NUM_DEVICES];\r
+    int numDevices;\r
+    \r
+    char deviceKey[DEVICE_KEY_SIZE];\r
+    char signingKey[DEVICE_KEY_SIZE];\r
+\r
+    char userId[USER_ID_SIZE];\r
+    char server[SERVER_SIZE];\r
+    char accessToken[ACCESS_TOKEN_SIZE];\r
+    char deviceId[DEVICE_ID_SIZE];\r
+    char expireMs[EXPIRE_MS_SIZE];\r
+    char refreshToken[REFRESH_TOKEN_SIZE];\r
 \r
     void * httpUserData;\r
 } MatrixClient;\r
@@ -40,6 +161,36 @@ MatrixClientSetAccessToken(
     MatrixClient * client,\r
     const char * accessToken);\r
 \r
+bool\r
+MatrixClientSetDeviceId(\r
+    MatrixClient * client,\r
+    const char * deviceId);\r
+\r
+bool\r
+MatrixClientSetUserId(\r
+    MatrixClient * client,\r
+    const char * userId);\r
+\r
+bool\r
+MatrixClientGenerateOnetimeKeys(\r
+    MatrixClient * client,\r
+    int numberOfKeys);\r
+\r
+bool\r
+MatrixClientUploadOnetimeKeys(\r
+    MatrixClient * client);\r
+\r
+bool\r
+MatrixClientUploadDeviceKeys(\r
+    MatrixClient * client);\r
+\r
+bool\r
+MatrixClientClaimOnetimeKey(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    char * outOnetimeKey, int outOnetimeKeyCap);\r
+\r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
@@ -53,11 +204,93 @@ MatrixClientSendEvent(
     const char * roomId,\r
     const char * msgType,\r
     const char * msgBody);\r
+    \r
+bool\r
+MatrixClientSendEventEncrypted(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    const char * msgType,\r
+    const char * msgBody);\r
+\r
+bool\r
+MatrixClientSync(\r
+    MatrixClient * client,\r
+    char * outSync, int outSyncCap);\r
+\r
+bool\r
+MatrixClientShareMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    MatrixMegolmOutSession * session);\r
+\r
+bool\r
+MatrixClientShareMegolmOutSessionTest(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    MatrixMegolmOutSession * session);\r
+\r
+bool\r
+MatrixClientGetMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    MatrixMegolmOutSession ** outSession);\r
+\r
+bool\r
+MatrixClientSetMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId,\r
+    MatrixMegolmOutSession session);\r
+\r
+bool\r
+MatrixClientGetOlmSession(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    MatrixOlmSession ** outSession);\r
+\r
+bool\r
+MatrixClientSendToDevice(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * message,\r
+    const char * msgType);\r
+\r
+bool\r
+MatrixClientSendToDeviceEncrypted(\r
+    MatrixClient * client,\r
+    const char * userId,\r
+    const char * deviceId,\r
+    const char * message,\r
+    const char * msgType);\r
+\r
+bool\r
+MatrixClientGetDeviceKey(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    char * outDeviceKey, int outDeviceKeyCap);\r
+\r
+bool\r
+MatrixClientGetDeviceKey(\r
+    MatrixClient * client,\r
+    const char * deviceId,\r
+    char * outDeviceKey, int outDeviceKeyCap);\r
+\r
+bool\r
+MatrixClientRequestDeviceKeys(\r
+    MatrixClient * client);\r
+\r
+\r
 \r
 bool\r
 MatrixHttpInit(\r
     MatrixClient * client);\r
 \r
+bool\r
+MatrixHttpConnect(\r
+    MatrixClient * client);\r
+\r
 bool\r
 MatrixHttpDeinit(\r
     MatrixClient * client);\r
@@ -85,4 +318,19 @@ MatrixHttpPut(
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated);\r
 \r
+// util\r
+\r
+void\r
+Randomize(uint8_t * random, int randomLen);\r
+\r
+bool\r
+JsonEscape(\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+    \r
+bool JsonSign(\r
+    MatrixClient * client,\r
+    const char * sIn, int sInLen,\r
+    char * sOut, int sOutCap);\r
+\r
 #endif\r