Struan Clark 2 лет назад
Родитель
Сommit
ce8afc6c94
5 измененных файлов с 144 добавлено и 13 удалено
  1. 81 5
      helpers/flipbip_file.c
  2. 4 1
      helpers/flipbip_file.h
  3. 45 2
      helpers/flipbip_string.c
  4. 4 1
      helpers/flipbip_string.h
  5. 10 4
      views/flipbip_scene_1.c

+ 81 - 5
helpers/flipbip_file.c

@@ -1,7 +1,8 @@
 #include "flipbip_file.h"
 #include "flipbip_file.h"
-#include "../flipbip.h"
+#include "../helpers/flipbip_string.h"
 
 
 #include "../crypto/memzero.h"
 #include "../crypto/memzero.h"
+#include "../crypto/rand.h"
 
 
 #include <storage/storage.h>
 #include <storage/storage.h>
 
 
@@ -22,8 +23,9 @@ bool flipbip_load_settings(char* settings) {
             i++;
             i++;
         }
         }
     } else {
     } else {
-        memzero(settings, strlen(settings));
-        settings[0] = '\0';
+        strcpy(settings, "uhoh");
+        //memzero(settings, strlen(settings));
+        //settings[0] = '\0';
     }
     }
     storage_file_close(settings_file);
     storage_file_close(settings_file);
     storage_file_free(settings_file);
     storage_file_free(settings_file);
@@ -49,11 +51,15 @@ bool flipbip_load_settings(char* settings) {
     return true;
     return true;
 }
 }
 
 
-bool flipbip_save_settings(const char* settings) {
+bool flipbip_save_settings(const char* settings, bool append) {
     Storage* fs_api = furi_record_open(RECORD_STORAGE);
     Storage* fs_api = furi_record_open(RECORD_STORAGE);
     File* settings_file = storage_file_alloc(fs_api);
     File* settings_file = storage_file_alloc(fs_api);
     storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
     storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
-    if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
+    int open_mode = FSOM_OPEN_ALWAYS;
+    if(append) {
+        open_mode = FSOM_OPEN_APPEND;
+    }
+    if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_WRITE, open_mode)) {
         storage_file_write(
         storage_file_write(
             settings_file,
             settings_file,
             settings,
             settings,
@@ -64,5 +70,75 @@ bool flipbip_save_settings(const char* settings) {
     storage_file_free(settings_file);
     storage_file_free(settings_file);
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_STORAGE);
 
 
+    return true;
+}
+
+bool flipbip_load_settings_secure(char* settings) {
+    const size_t hlen = 4;
+    const size_t klen = 128;
+    const size_t slen = 512;
+    const size_t dlen = hlen + klen + slen;
+    
+    char *data = malloc(dlen+1);
+    memzero(data, dlen+1);
+
+    if (!flipbip_load_settings(data)) return false;
+
+    // if (strncmp(data, "fb01", hlen) != 0) {
+    //     memzero(data, dlen);
+    //     free(data);
+    //     return true;
+    // }
+    data += hlen;
+
+    uint8_t key[64];
+    flipbip_xtob(data, key, 64);
+    data += klen;
+
+    flipbip_cipher(key, data, data);
+    flipbip_xtob(data, (unsigned char*)settings, 256);
+
+    data = data - klen - hlen;
+    memzero(data, dlen);
+    free(data);
+
+    return true;
+}
+
+bool flipbip_save_settings_secure(const char* settings) {
+    const size_t hlen = 4;
+    const size_t klen = 128;
+    const size_t slen = 512;
+    const size_t dlen = hlen + klen + slen;
+
+    size_t len = strlen(settings);
+    if (len > 256) len = 256;
+    
+    char *data = malloc(dlen + 1);
+    memzero(data, dlen + 1);
+    
+    memcpy(data, "fb01", hlen);
+    data += hlen - 1;
+
+    uint8_t key[64];
+    random_buffer(key, 64);
+    for (size_t i = 0; i < 64; i++) {
+        flipbip_btox(key[i], data + (i * 2));
+    }
+    data += klen;
+
+    for (size_t i = 0; i < len; i++) {
+        flipbip_btox(settings[i], data + (i * 2));
+    }
+    flipbip_cipher(key, data, data);
+
+    data = data - klen - hlen;
+    data[dlen] = '\0';
+
+    flipbip_save_settings(data, false);
+
+    memzero(data, dlen);
+    free(data);
+
     return true;
     return true;
 }
 }

+ 4 - 1
helpers/flipbip_file.h

@@ -1,4 +1,7 @@
 #include <stdbool.h>
 #include <stdbool.h>
 
 
 bool flipbip_load_settings(char* settings);
 bool flipbip_load_settings(char* settings);
-bool flipbip_save_settings(const char* settings);
+bool flipbip_save_settings(const char* settings, bool append);
+
+bool flipbip_load_settings_secure(char* settings);
+bool flipbip_save_settings_secure(const char* settings);

+ 45 - 2
helpers/flipbip_string.c

@@ -28,6 +28,12 @@
  */
  */
 #include "flipbip_string.h"
 #include "flipbip_string.h"
 #include <ctype.h>
 #include <ctype.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "../crypto/memzero.h"
+#include "../crypto/rc4.h"
+
 char *
 char *
 flipbip_strtok(char *s, const char *delim)
 flipbip_strtok(char *s, const char *delim)
 {
 {
@@ -77,11 +83,11 @@ cont:
 	/* NOTREACHED */
 	/* NOTREACHED */
 }
 }
 
 
-
 void 
 void 
-flipbip_btox(unsigned char i, char *str)
+flipbip_btox(const unsigned char in, char *str)
 {
 {
     unsigned char n;
     unsigned char n;
+    unsigned char i = in;
  
  
     str += 2;
     str += 2;
     *str = '\0';
     *str = '\0';
@@ -90,4 +96,41 @@ flipbip_btox(unsigned char i, char *str)
         *--str = "0123456789abcdef"[i & 0x0F];
         *--str = "0123456789abcdef"[i & 0x0F];
         i >>= 4;
         i >>= 4;
     }
     }
+}
+void 
+flipbip_xtob(const char *str, unsigned char *out, int out_len) 
+{
+    int len = strlen(str) / 2;
+    if (len > out_len) len = out_len;
+    for (int i = 0; i < len; i++) {
+        char c = 0;
+        if (str[i * 2] >= '0' && str[i * 2] <= '9') 
+            c += (str[i * 2] - '0') << 4;
+        if ((str[i * 2] & ~0x20) >= 'A' && (str[i * 2] & ~0x20) <= 'F')
+            c += (10 + (str[i * 2] & ~0x20) - 'A') << 4;
+        if (str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9')
+            c += (str[i * 2 + 1] - '0');
+        if ((str[i * 2 + 1] & ~0x20) >= 'A' && (str[i * 2 + 1] & ~0x20) <= 'F')
+            c += (10 + (str[i * 2 + 1] & ~0x20) - 'A');
+        out[i] = c;
+    }
+}
+
+void 
+flipbip_cipher(const unsigned char* key_in, const char* in, char* out)
+{
+    RC4_CTX ctx;
+    uint8_t buf[256];
+
+    memzero(buf, 256);
+    flipbip_xtob(in, buf, 256);
+
+    rc4_init(&ctx, key_in, 64);
+    rc4_encrypt(&ctx, buf, 256);
+
+    for (size_t i = 0; i < 256; i++) {
+        flipbip_btox(buf[i], out + i * 2);
+    }
+
+    memzero(buf, 256);
 }
 }

+ 4 - 1
helpers/flipbip_string.h

@@ -1,4 +1,7 @@
 char * flipbip_strtok(char *s, const char *delim);
 char * flipbip_strtok(char *s, const char *delim);
 char * flipbip_strtok_r(char *s, const char *delim, char **last);
 char * flipbip_strtok_r(char *s, const char *delim, char **last);
 
 
-void flipbip_btox(unsigned char i, char *str);
+void flipbip_btox(const unsigned char i, char *str);
+void flipbip_xtob(const char *str, unsigned char *out, int out_len);
+
+void flipbip_cipher(const unsigned char* key_in, const char* in, char* out);

+ 10 - 4
views/flipbip_scene_1.c

@@ -12,6 +12,7 @@
 #include "../helpers/flipbip_file.h"
 #include "../helpers/flipbip_file.h"
 
 
 #include <string.h>
 #include <string.h>
+#include "../crypto/rand.h"
 #include "../crypto/bip32.h"
 #include "../crypto/bip32.h"
 #include "../crypto/bip39.h"
 #include "../crypto/bip39.h"
 #include "../crypto/curves.h"
 #include "../crypto/curves.h"
@@ -269,10 +270,15 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
 
 
     // Generate a random mnemonic using trezor-crypto
     // Generate a random mnemonic using trezor-crypto
     model->strength = strength;
     model->strength = strength;
-    model->mnemonic = mnemonic_generate(model->strength);
-
-    flipbip_save_settings("123456beep");
-    // flipbip_load_file(EXT_PATH("flipbip.dat"));
+    
+    const char* mnemonic = mnemonic_generate(strength);
+    if (!flipbip_save_settings_secure(mnemonic)) return;
+    
+    char* mnemonic2 = malloc(256+1);
+    memzero((void*)mnemonic2, 256+1);
+    if (!flipbip_load_settings_secure(mnemonic2)) return;
+    
+    model->mnemonic = mnemonic2;
     
     
     // test mnemonic
     // test mnemonic
     //model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";
     //model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";