X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/9826729ea9eb492b0b25c52b934d9f1283bb70dd..504241758d7b832af61939beaf61b0e0574174c4:/src/matrix.h?ds=sidebyside diff --git a/src/matrix.h b/src/matrix.h index 60561aa..da0e859 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -11,49 +11,87 @@ #define USER_ID_SIZE 64 +#define ROOM_ID_SIZE 128 #define SERVER_SIZE 20 #define ACCESS_TOKEN_SIZE 40 #define DEVICE_ID_SIZE 20 #define EXPIRE_MS_SIZE 20 #define REFRESH_TOKEN_SIZE 20 -#define MAX_URL_LEN 128 +#define MAX_URL_LEN 1024 #define OLM_IDENTITY_KEYS_JSON_SIZE 128 #define DEVICE_KEY_SIZE 44 #define SIGNING_KEY_SIZE 44 +#define ONETIME_KEY_SIZE 44 #define KEY_SHARE_EVENT_LEN 1024 #define OLM_ACCOUNT_MEMORY_SIZE 7528 -#define OLM_ACCOUNT_RANDOM_SIZE 32+32 +#define OLM_ACCOUNT_RANDOM_SIZE (32+32) #define OLM_SESSION_MEMORY_SIZE 3352 #define OLM_ENCRYPT_RANDOM_SIZE 32 +#define OLM_OUTBOUND_SESSION_RANDOM_SIZE (32*2) + +#define OLM_ONETIME_KEYS_RANDOM_SIZE (32*10) +#define OLM_KEY_ID_SIZE 32 + +#define OLM_SIGNATURE_SIZE 128 #define MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE 232 #define MEGOLM_SESSION_ID_SIZE 44 #define MEGOLM_SESSION_KEY_SIZE 306 #define MEGOLM_INIT_RANDOM_SIZE (4*32 + 32) +#define JSON_ONETIME_KEY_SIZE 128 +#define JSON_ONETIME_KEY_SIGNED_SIZE 256 +#define JSON_SIGNATURE_SIZE 256 + #define NUM_MEGOLM_SESSIONS 10 #define NUM_OLM_SESSIONS 10 #define NUM_DEVICES 10 -void -Randomize(uint8_t * random, int randomLen); - -bool -JsonEscape( - char * sIn, int sInLen, - char * sOut, int sOutCap); +// Matrix Device typedef struct MatrixDevice { char deviceId[DEVICE_ID_SIZE]; char deviceKey[DEVICE_KEY_SIZE]; + char signingKey[SIGNING_KEY_SIZE]; } MatrixDevice; + +// Matrix Olm Account + +typedef struct MatrixOlmAccount { + OlmAccount * account; + char memory[OLM_ACCOUNT_MEMORY_SIZE]; +} MatrixOlmAccount; + +bool +MatrixOlmAccountInit( + MatrixOlmAccount * account); + +bool +MatrixOlmAccountUnpickle( + MatrixOlmAccount * account, + void * pickled, int pickledLen, + const void * key, int keyLen); + +bool +MatrixOlmAccountGetDeviceKey( + MatrixOlmAccount * account, + char * key, int keyCap); + +bool +MatrixOlmAccountGetSigningKey( + MatrixOlmAccount * account, + char * key, int keyCap); + + +// Matrix Olm Session + typedef struct MatrixOlmSession { - const char * deviceId; + const char * deviceId; // TODO: char[] int type; OlmSession * session; @@ -61,9 +99,19 @@ typedef struct MatrixOlmSession { } MatrixOlmSession; bool -MatrixOlmSessionInit( +MatrixOlmSessionUnpickle( MatrixOlmSession * session, - const char * deviceId); + const char * deviceId, + void * pickled, int pickledLen, + const void * key, int keyLen); + +bool +MatrixOlmSessionTo( + MatrixOlmSession * session, + OlmAccount * olmAccount, + const char * deviceId, + const char * deviceKey, + const char * deviceOnetimeKey); bool MatrixOlmSessionEncrypt( @@ -71,14 +119,28 @@ MatrixOlmSessionEncrypt( const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixOlmSessionDecrypt( + MatrixOlmSession * session, + size_t messageType, + char * encrypted, + char * outBuffer, int outBufferCap); +// Matrix Megolm Session + typedef struct MatrixMegolmInSession { OlmInboundGroupSession * session; } MatrixMegolmInSession; +bool +MatrixMegolmInSessionDecrypt( + MatrixMegolmInSession * megolmInSession, + const char * encrypted, + char * outDecrypted, int outDecryptedCap); + typedef struct MatrixMegolmOutSession { - const char * roomId; + char roomId[ROOM_ID_SIZE]; OlmOutboundGroupSession * session; char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; @@ -91,18 +153,30 @@ bool MatrixMegolmOutSessionInit( MatrixMegolmOutSession * session, const char * roomId); - + bool MatrixMegolmOutSessionEncrypt( MatrixMegolmOutSession * session, const char * plaintext, char * outBuffer, int outBufferCap); +bool +MatrixMegolmOutSessionSave( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + +bool +MatrixMegolmOutSessionLoad( + MatrixMegolmOutSession * session, + const char * filename, + const char * key); + +// Matrix Client typedef struct MatrixClient { - OlmAccount * olmAccount; - char olmAccountMemory[OLM_ACCOUNT_MEMORY_SIZE]; + MatrixOlmAccount olmAccount; MatrixMegolmInSession megolmInSessions[NUM_MEGOLM_SESSIONS]; int numMegolmInSessions; @@ -114,8 +188,8 @@ typedef struct MatrixClient { MatrixDevice devices[NUM_DEVICES]; int numDevices; - char deviceKey[DEVICE_KEY_SIZE]; - char signingKey[DEVICE_KEY_SIZE]; + // char deviceKey[DEVICE_KEY_SIZE]; + // char signingKey[DEVICE_KEY_SIZE]; char userId[USER_ID_SIZE]; char server[SERVER_SIZE]; @@ -132,6 +206,16 @@ MatrixClientInit( MatrixClient * client, const char * server); +bool +MatrixClientSave( + MatrixClient * client, + const char * filename); + +bool +MatrixClientLoad( + MatrixClient * client, + const char * filename); + bool MatrixClientSetAccessToken( MatrixClient * client, @@ -142,6 +226,31 @@ MatrixClientSetDeviceId( MatrixClient * client, const char * deviceId); +bool +MatrixClientSetUserId( + MatrixClient * client, + const char * userId); + +bool +MatrixClientGenerateOnetimeKeys( + MatrixClient * client, + int numberOfKeys); + +bool +MatrixClientUploadOnetimeKeys( + MatrixClient * client); + +bool +MatrixClientUploadDeviceKey( + MatrixClient * client); + +bool +MatrixClientClaimOnetimeKey( + MatrixClient * client, + const char * userId, + const char * deviceId, + char * outOnetimeKey, int outOnetimeKeyCap); + bool MatrixClientLoginPassword( MatrixClient * client, @@ -166,11 +275,27 @@ MatrixClientSendEventEncrypted( bool MatrixClientSync( MatrixClient * client, - char * outSyncBuffer, int outSyncCap); + char * outSync, int outSyncCap, + const char * nextBatch); + +bool +MatrixClientGetRoomEvent( + MatrixClient * client, + const char * roomId, + const char * eventId, + char * outEvent, int outEventCap); bool MatrixClientShareMegolmOutSession( MatrixClient * client, + const char * userId, + const char * deviceId, + MatrixMegolmOutSession * session); + +bool +MatrixClientShareMegolmOutSessionTest( + MatrixClient * client, + const char * userId, const char * deviceId, MatrixMegolmOutSession * session); @@ -186,9 +311,25 @@ MatrixClientSetMegolmOutSession( const char * roomId, MatrixMegolmOutSession session); +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId); + +bool +MatrixClientRequestMegolmInSession( + MatrixClient * client, + const char * roomId, + const char * sessionId, + const char * senderKey, + const char * userId, + const char * deviceId, // TODO: remove deviceId (query all devices) + MatrixMegolmInSession * outMegolmInSession); + bool MatrixClientGetOlmSession( MatrixClient * client, + const char * userId, const char * deviceId, MatrixOlmSession ** outSession); @@ -209,27 +350,42 @@ MatrixClientSendToDeviceEncrypted( const char * msgType); bool -MatrixClientGetDeviceKey( +MatrixClientSendDummy( MatrixClient * client, - const char * deviceId, - char * outDeviceKey, int outDeviceKeyCap); + const char * userId, + const char * deviceId); bool -MatrixClientGetDeviceKey( +MatrixClientRequestDeviceKey( MatrixClient * client, const char * deviceId, char * outDeviceKey, int outDeviceKeyCap); + +bool +MatrixClientRequestSigningKey( + MatrixClient * client, + const char * deviceId, + char * outSigningKey, int outSigningKeyCap); bool MatrixClientRequestDeviceKeys( MatrixClient * client); +bool +MatrixClientDeleteDevice( + MatrixClient * client); + + bool MatrixHttpInit( MatrixClient * client); +bool +MatrixHttpConnect( + MatrixClient * client); + bool MatrixHttpDeinit( MatrixClient * client); @@ -257,4 +413,20 @@ MatrixHttpPut( char * outResponseBuffer, int outResponseCap, bool authenticated); +// util + +void +Randomize(uint8_t * random, int randomLen); + +bool +JsonEscape( + const char * sIn, int sInLen, + char * sOut, int sOutCap); + +bool +JsonSign( + MatrixClient * client, + const char * sIn, int sInLen, + char * sOut, int sOutCap); + #endif