Skip to content

Commit

Permalink
file operations cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 7, 2023
1 parent 473cdf7 commit aa7e286
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 48 deletions.
143 changes: 99 additions & 44 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
#include <storage/storage.h>

#define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
// #define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.dat"
#define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.txt"
#define FLIPBIP_SETTINGS_FILE_NAME_BAK ".flipbip.bak"
#define FLIPBIP_SETTINGS_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_SETTINGS_FILE_NAME
#define FLIPBIP_SETTINGS_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_SETTINGS_FILE_NAME_BAK
// #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat"
#define FLIPBIP_DAT_FILE_NAME ".flipbip.dat.txt"
#define FLIPBIP_DAT_FILE_NAME_BAK ".flipbip.dat.bak"
// #define FLIPBIP_KEY_FILE_NAME ".flipbip.key"
#define FLIPBIP_KEY_FILE_NAME ".flipbip.key.txt"
#define FLIPBIP_KEY_FILE_NAME_BAK ".flipbip.key.bak"
#define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME
#define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME_BAK
#define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME
#define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME_BAK

const size_t FILE_HLEN = 4;
const size_t FILE_KLEN = 256;
Expand All @@ -20,10 +25,16 @@ const char* FILE_HSTR = "fb01";
const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
"baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";

bool flipbip_load_settings(char* settings) {
bool flipbip_load_settings(char* settings, bool key_file) {
Storage *fs_api = furi_record_open(RECORD_STORAGE);
File* settings_file = storage_file_alloc(fs_api);
if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
const char* path;
if(key_file) {
path = FLIPBIP_KEY_PATH;
} else {
path = FLIPBIP_DAT_PATH;
}
if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
char chr;
int i = 0;
while((storage_file_read(settings_file, &chr, 1) == 1) &&
Expand All @@ -33,6 +44,7 @@ bool flipbip_load_settings(char* settings) {
}
} else {
memzero(settings, strlen(settings));
settings[0] = '\0';
}
storage_file_close(settings_file);
storage_file_free(settings_file);
Expand All @@ -42,7 +54,7 @@ bool flipbip_load_settings(char* settings) {
Storage* fs_api = furi_record_open(RECORD_STORAGE);
FileInfo layout_file_info;
FS_Error file_check_err = storage_common_stat(
fs_api, FLIPBIP_SETTINGS_PATH, &layout_file_info);
fs_api, path, &layout_file_info);
furi_record_close(RECORD_STORAGE);
if(file_check_err != FSE_OK) {
memzero(settings, strlen(settings));
Expand All @@ -58,7 +70,7 @@ bool flipbip_load_settings(char* settings) {
return true;
}

bool flipbip_save_settings(const char* settings, bool append) {
bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
Storage* fs_api = furi_record_open(RECORD_STORAGE);

storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
Expand All @@ -68,7 +80,17 @@ bool flipbip_save_settings(const char* settings, bool append) {
}

File* settings_file = storage_file_alloc(fs_api);
if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_WRITE, open_mode)) {
const char* path;
const char* path_bak;
if(key_file) {
path = FLIPBIP_KEY_PATH;
path_bak = FLIPBIP_KEY_PATH_BAK;
} else {
path = FLIPBIP_DAT_PATH;
path_bak = FLIPBIP_DAT_PATH_BAK;
}

if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
storage_file_write(
settings_file,
settings,
Expand All @@ -79,7 +101,7 @@ bool flipbip_save_settings(const char* settings, bool append) {
storage_file_free(settings_file);

File* settings_file_bak = storage_file_alloc(fs_api);
if(storage_file_open(settings_file_bak, FLIPBIP_SETTINGS_PATH_BAK, FSAM_WRITE, open_mode)) {
if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
storage_file_write(
settings_file_bak,
settings,
Expand All @@ -95,36 +117,58 @@ bool flipbip_save_settings(const char* settings, bool append) {
}

bool flipbip_load_settings_secure(char* settings) {
const size_t dlen = FILE_HLEN + FILE_KLEN + FILE_SLEN + 1;
const size_t dlen = FILE_HLEN + FILE_SLEN + 1;

// allocate memory for data
// allocate memory for key/data
char *data = malloc(dlen);
memzero(data, dlen);

// load data from file
if (!flipbip_load_settings(data)) return false;
// load k2 from file
if (!flipbip_load_settings(data, true)) return false;

// check header
if (data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || data[3] != FILE_HSTR[3]) {
memzero(data, dlen);
free(data);
return false;
}
// seek --> header
data += FILE_HLEN;

// load k2 from file using k1
//uint8_t k1[16] = {0};
//flipbip_xtob(FILE_K1, k1, 64);
// prepare k1
uint8_t k1[64];
flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);

// load k2 from file buffer (secured by k1)
flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);
uint8_t k2[128];
//flipbip_cipher(k1, 16, data, data, FILE_KLEN);
flipbip_xtob(data, k2, 128);
data += FILE_KLEN;
flipbip_xtob(data, k2, FILE_KLEN / 2);
// zero k2 buffer
memzero(data, FILE_KLEN);
// seek <-- header
data -= FILE_HLEN;

// load settings from file using k2
flipbip_cipher(k2, 128, data, data, FILE_SLEN);
flipbip_xtob(data, (unsigned char*)settings, 256);
// load data from file
if (!flipbip_load_settings(data, false)) return false;

data = data - FILE_KLEN - FILE_HLEN;
// check header
if (data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || data[3] != FILE_HSTR[3]) {
memzero(data, dlen);
free(data);
return false;
}
// seek --> header
data += FILE_HLEN;

// load settings from file buffer (secured by k2)
flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);
flipbip_xtob(data, (unsigned char*)data, FILE_SLEN / 2);

// copy to output
strcpy(settings, data);

// seek <-- header
data -= FILE_HLEN;

// clear memory
memzero(data, dlen);
Expand All @@ -134,43 +178,54 @@ bool flipbip_load_settings_secure(char* settings) {
}

bool flipbip_save_settings_secure(const char* settings) {
const size_t dlen = FILE_HLEN + FILE_KLEN + FILE_SLEN + 1;
const size_t dlen = FILE_HLEN + FILE_SLEN + 1;

// cap settings to 256 bytes
size_t len = strlen(settings);
if (len > 256) len = 256;
if (len > (FILE_SLEN / 2)) len = FILE_SLEN / 2;

// allocate memory for data
char *data = malloc(dlen + 1);
// allocate memory for key/data
char *data = malloc(dlen);
memzero(data, dlen);

// write header
strncpy(data, FILE_HSTR, FILE_HLEN);
// seek --> header
data += FILE_HLEN;

// generate key
//uint8_t k1[16] = {0};
//flipbip_xtob(FILE_K1, k1, 64);
// prepare k1
uint8_t k1[64];
flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);

// generate k2
uint8_t k2[128];
random_buffer(k2, 128);
random_buffer(k2, FILE_KLEN / 2);

// write k2 to file (secured by k1)
for (size_t i = 0; i < 128; i++) {
// write k2 to file buffer (secured by k1)
for (size_t i = 0; i < (FILE_KLEN / 2); i++) {
flipbip_btox(k2[i], data + (i * 2));
}
//flipbip_cipher(k1, 16, data, data, FILE_KLEN);
data += FILE_KLEN;
flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);

// write settings to file (secured by k2)
// seek <-- header
data -= FILE_HLEN;
// save k2 to file
flipbip_save_settings(data, true, false);
// seek --> header
data += FILE_HLEN;
// zero k2 memory
memzero(data, FILE_KLEN);

// write settings to file buffer (secured by k2)
for (size_t i = 0; i < len; i++) {
flipbip_btox(settings[i], data + (i * 2));
flipbip_btox((uint8_t)settings[i], data + (i * 2));
}
flipbip_cipher(k2, 128, data, data, FILE_SLEN);

data = data - FILE_KLEN - FILE_HLEN;
flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);

// save data
flipbip_save_settings(data, false);
// seek <-- header
data -= FILE_HLEN;
// save data to file
flipbip_save_settings(data, false, false);

// clear memory
memzero(data, dlen);
Expand Down
4 changes: 2 additions & 2 deletions helpers/flipbip_file.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdbool.h>

bool flipbip_load_settings(char* settings);
bool flipbip_save_settings(const char* settings, bool append);
bool flipbip_load_settings(char* settings, bool key_file);
bool flipbip_save_settings(const char* settings, bool key_file , bool append);

bool flipbip_load_settings_secure(char* settings);
bool flipbip_save_settings_secure(const char* settings);
4 changes: 2 additions & 2 deletions views/flipbip_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
const char* mnemonic = mnemonic_generate(strength);
if (!flipbip_save_settings_secure(mnemonic)) return;

char* mnemonic2 = malloc(256+1);
memzero((void*)mnemonic2, 256+1);
char* mnemonic2 = malloc(256);
memzero((void*)mnemonic2, 256);
if (!flipbip_load_settings_secure(mnemonic2)) return;

model->mnemonic = mnemonic2;
Expand Down

0 comments on commit aa7e286

Please sign in to comment.