Skip to content

Commit

Permalink
storage proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 6, 2023
1 parent 5aa6379 commit ce8afc6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 13 deletions.
86 changes: 81 additions & 5 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "flipbip_file.h"
#include "../flipbip.h"
#include "../helpers/flipbip_string.h"

#include "../crypto/memzero.h"
#include "../crypto/rand.h"

#include <storage/storage.h>

Expand All @@ -22,8 +23,9 @@ bool flipbip_load_settings(char* settings) {
i++;
}
} 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_free(settings_file);
Expand All @@ -49,11 +51,15 @@ bool flipbip_load_settings(char* settings) {
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);
File* settings_file = storage_file_alloc(fs_api);
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(
settings_file,
settings,
Expand All @@ -64,5 +70,75 @@ bool flipbip_save_settings(const char* settings) {
storage_file_free(settings_file);
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;
}
5 changes: 4 additions & 1 deletion helpers/flipbip_file.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdbool.h>

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);
47 changes: 45 additions & 2 deletions helpers/flipbip_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
*/
#include "flipbip_string.h"
#include <ctype.h>
#include <stdint.h>
#include <string.h>

#include "../crypto/memzero.h"
#include "../crypto/rc4.h"

char *
flipbip_strtok(char *s, const char *delim)
{
Expand Down Expand Up @@ -77,11 +83,11 @@ flipbip_strtok_r(char *s, const char *delim, char **last)
/* NOTREACHED */
}


void
flipbip_btox(unsigned char i, char *str)
flipbip_btox(const unsigned char in, char *str)
{
unsigned char n;
unsigned char i = in;

str += 2;
*str = '\0';
Expand All @@ -90,4 +96,41 @@ flipbip_btox(unsigned char i, char *str)
*--str = "0123456789abcdef"[i & 0x0F];
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);
}
5 changes: 4 additions & 1 deletion helpers/flipbip_string.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
char * flipbip_strtok(char *s, const char *delim);
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);
14 changes: 10 additions & 4 deletions views/flipbip_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../helpers/flipbip_file.h"

#include <string.h>
#include "../crypto/rand.h"
#include "../crypto/bip32.h"
#include "../crypto/bip39.h"
#include "../crypto/curves.h"
Expand Down Expand Up @@ -269,10 +270,15 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in

// Generate a random mnemonic using trezor-crypto
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
//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";
Expand Down

0 comments on commit ce8afc6

Please sign in to comment.