]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - esp32/esp_project/main/Verify.c
update esp_project
[matrix_esp_thesis] / esp32 / esp_project / main / Verify.c
index 124ddd22b1cfb5b059a0ce5caf17257d1d139956..989b151a0ac2d2cfaa1fbab782e51d8362662ff1 100644 (file)
@@ -2,6 +2,13 @@
 #include <mjson.h>
 #include <olm/sas.h>
 
+#if !CONFIG_IDF_TARGET_LINUX
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "esp_system.h"
+#include "driver/gpio.h"
+#endif
+
 #include <stdio.h>
 
 #define SERVER       "https://matrix.org"
@@ -62,9 +69,9 @@ HandleEvent(
         OlmUtility * olmUtil = olm_utility(malloc(olm_utility_size()));
         
         STATIC char publicKey[64];
-        STATIC char keyStartJsonCanonical[128];
-        STATIC char concat[128+64];
-        STATIC char commitment[256];
+        STATIC char keyStartJsonCanonical[512];
+        STATIC char concat[512+64];
+        STATIC char commitment[1024];
         olm_sas_get_pubkey(olmSas,
             publicKey,
             64);
@@ -73,15 +80,15 @@ HandleEvent(
         const char * keyStartJson;
         int keyStartJsonLen;
         mjson_find(event, eventLen, "$.content", &keyStartJson, &keyStartJsonLen);
-        JsonCanonicalize(keyStartJson, keyStartJsonLen, keyStartJsonCanonical, 128);
+        JsonCanonicalize(keyStartJson, keyStartJsonLen, keyStartJsonCanonical, 512);
 
         printf("json:\n%.*s\ncanonical json:\n%s\n", keyStartJsonLen, keyStartJson, keyStartJsonCanonical);
 
         int concatLen =
-            snprintf(concat, 128+64, "%.*s%s", olm_sas_pubkey_length(olmSas), publicKey, keyStartJsonCanonical);
+            snprintf(concat, 512+64, "%.*s%s", olm_sas_pubkey_length(olmSas), publicKey, keyStartJsonCanonical);
 
         int commitmentLen =
-            olm_sha256(olmUtil, concat, concatLen, commitment, 256);
+            olm_sha256(olmUtil, concat, concatLen, commitment, 1024);
         
         STATIC char verificationAcceptBuffer[512];
         snprintf(verificationAcceptBuffer, 512,
@@ -293,16 +300,22 @@ HandleEvent(
             mjson_get_string(event, eventLen, jp, encrypted, 2048);
 
             MatrixOlmSession * olmSession;
-            if (messageTypeInt == 0) {
-                MatrixClientGetOlmSessionIn(client,
-                    USER_ID,
-                    DEVICE_ID,
-                    &olmSession);
-            } else {
-                MatrixClientGetOlmSessionOut(client,
-                    USER_ID,
-                    DEVICE_ID,
-                    &olmSession);
+            
+            if (! MatrixClientGetOlmSession(client, USER_ID, DEVICE_ID, &olmSession))
+            {
+                if (messageTypeInt == 0) {
+                    MatrixClientNewOlmSessionIn(client,
+                        USER_ID,
+                        DEVICE_ID,
+                        encrypted,
+                        &olmSession);
+                }
+                else {
+                    MatrixClientNewOlmSessionOut(client,
+                        USER_ID,
+                        DEVICE_ID,
+                        &olmSession);
+                }
             }
 
             printf("event: %.*s\n", eventLen, event);
@@ -380,17 +393,19 @@ HandleRoomEvent(
 void
 Sync(
     MatrixClient * client,
-    char * syncBuffer
-{
+    char * syncBuffer, int syncBufferLen)
+{
     STATIC char nextBatch[1024] = {0};
 
-    MatrixClientSync(client, syncBuffer, 1024, nextBatch);
+    MatrixClientSync(client, syncBuffer, syncBufferLen, nextBatch);
     
     int res;
 
     const char * s = syncBuffer;
     int slen = strlen(syncBuffer);
     
+    printf("sync:\n\n%s\n\n", syncBuffer);
+    
     // {
     // int koff, klen, voff, vlen, vtype, off = 0;
     // for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
@@ -465,9 +480,15 @@ Sync(
 int
 main(void)
 {
-    STATIC MatrixClient _client;
-    MatrixClient * client = &_client;
-    // MatrixClient * client = (MatrixClient*)malloc(sizeof(MatrixClient));
+    // sizeof(MatrixOlmAccount);
+    // sizeof(MatrixMegolmInSession);
+    // sizeof(MatrixMegolmOutSession);
+    // sizeof(MatrixOlmSession);    
+    // sizeof(MatrixDevice);
+
+    // STATIC MatrixClient _client;
+    // MatrixClient * client = &_client;
+    MatrixClient * client = (MatrixClient*)malloc(sizeof(MatrixClient));
     MatrixClientInit(client);
 
     MatrixHttpInit(&client->hc, SERVER);
@@ -488,40 +509,88 @@ main(void)
         EVENT_ID,
         eventBuffer, 1024);
     printf("event: %s\n", eventBuffer);
-    
-    char * syncBuffer = (char*)malloc(1024*40);
-    // STATIC char syncBuffer[1024];
 
-    while (! verified)
-        Sync(client, syncBuffer);
-    
-    // while (getchar() != 'q')
-    //     Sync(client, syncBuffer);
-    
-    MatrixClientRequestMegolmInSession(client,
+    #define SYNC_BUFFER_SIZE 1024*10
+
+    // char * syncBuffer = (char*)malloc(SYNC_BUFFER_SIZE);
+    STATIC char syncBuffer[SYNC_BUFFER_SIZE];
+
+    while (! verified) {
+        Sync(client, syncBuffer, SYNC_BUFFER_SIZE);
+    }
+
+    printf("verified!\n");
+
+    // create and share megolm out session
+    MatrixMegolmOutSession * megolmOutSession;
+    MatrixClientNewMegolmOutSession(client,
         ROOM_ID,
-        SESSION_ID,
-        SENDER_KEY,
+        &megolmOutSession);
+    printf("megolm session id: %.10s... key: %.10s...\n", megolmOutSession->id, megolmOutSession->key);
+    MatrixClientShareMegolmOutSession(client,
         USER_ID,
-        DEVICE_ID);
+        "ULZZOKJBYN",
+        megolmOutSession);
 
-    MatrixMegolmInSession * megolmInSession;
-    while (! MatrixClientGetMegolmInSession(client,
-        ROOM_ID, strlen(ROOM_ID),
-        SESSION_ID, strlen(SESSION_ID),
-        &megolmInSession))
-        Sync(client, syncBuffer);
+    
+    // int c;
+    // while ((c=getchar()) != 'q') {
+    //     vTaskDelay(1000/portTICK_PERIOD_MS);
+
+    //     if (c == 's') {
+    //         Sync(client, syncBuffer, SYNC_BUFFER_SIZE);
+    //     }
+    //     else if (c == 'm') {
+    //         static const char * msgs[] = { "A", "B", "C" };
+    //         static char msg[128];
+    //         snprintf(msg, 128, "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", msgs[rand()%(sizeof(msgs)/sizeof(msgs[0]))]);
+
+    //         MatrixClientSendEventEncrypted(client,
+    //             ROOM_ID,
+    //             "m.room.message",
+    //             msg);
+    //         printf("Message sent. Message index: %d\n", (int)olm_outbound_group_session_message_index(megolmOutSession->session));
+    //     }
+    // }
+
+    for (int i = 0; i < 10; i++) {
+
+        static const char * msgs[] = { "A", "B", "C" };
+        static char msg[128];
+        snprintf(msg, 128, "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", msgs[rand()%(sizeof(msgs)/sizeof(msgs[0]))]);
 
-    int encryptedLen =
-        mjson_get_string(eventBuffer, strlen(eventBuffer), "$.content.ciphertext", encrypted, 1024);
+        MatrixClientSendEventEncrypted(client,
+            ROOM_ID,
+            "m.room.message",
+            msg);
+
+        vTaskDelay(5000/portTICK_PERIOD_MS);
+    }
+    
+    // MatrixClientRequestMegolmInSession(client,
+    //     ROOM_ID,
+    //     SESSION_ID,
+    //     SENDER_KEY,
+    //     USER_ID,
+    //     DEVICE_ID);
+
+    // MatrixMegolmInSession * megolmInSession;
+    // while (! MatrixClientGetMegolmInSession(client,
+    //     ROOM_ID, strlen(ROOM_ID),
+    //     SESSION_ID, strlen(SESSION_ID),
+    //     &megolmInSession))
+    //     Sync(client, syncBuffer, SYNC_BUFFER_SIZE);
+
+    // int encryptedLen =
+    //     mjson_get_string(eventBuffer, strlen(eventBuffer), "$.content.ciphertext", encrypted, 1024);
     
-    printf("encrypted: [%.*s]\n", encryptedLen, encrypted);
+    // printf("encrypted: [%.*s]\n", encryptedLen, encrypted);
 
-    MatrixMegolmInSessionDecrypt(megolmInSession,
-        encrypted, encryptedLen,
-        decrypted, 1024);
+    // MatrixMegolmInSessionDecrypt(megolmInSession,
+    //     encrypted, encryptedLen,
+    //     decrypted, 1024);
 
-    printf("decrypted: %s\n", decrypted);
+    // printf("decrypted: %s\n", decrypted);
 
     MatrixClientDeleteDevice(client);
         
@@ -531,11 +600,26 @@ main(void)
 }
 
 #include "wifi.h"
+#include <esp_netif.h>
 
 void
 app_main(void)
 {
-    wifi_init("Hundehuette", "Affensicherespw55");
+    // wifi_init("Pixel_7762", "affeaffe");
+    // wifi_init("Hundehuette", "Affensicherespw55");
+    wifi_init("test", "/O801i25");
+
+    esp_netif_ip_info_t ip_info;
+    esp_netif_get_ip_info(IP_EVENT_STA_GOT_IP,&ip_info);
+    printf("My IP: " IPSTR "\n", IP2STR(&ip_info.ip));
+    printf("My GW: " IPSTR "\n", IP2STR(&ip_info.gw));
+    printf("My NETMASK: " IPSTR "\n", IP2STR(&ip_info.netmask));
+
+    // uint64_t bitmask = 0xffffffffffffffff;
+    // bitmask = bitmask & SOC_GPIO_VALID_GPIO_MASK;
+    // gpio_dump_io_configuration(stdout, bitmask);
+    gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
+    // gpio_dump_io_configuration(stdout, bitmask);
 
     main();
 }