Skip to content

Commit

Permalink
Merge pull request #7 from xtruan/develop
Browse files Browse the repository at this point in the history
Crypto as lib & QR address gen
  • Loading branch information
xtruan authored Mar 13, 2023
2 parents 8413b87 + 0150732 commit e7943e3
Show file tree
Hide file tree
Showing 151 changed files with 324 additions and 133 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build

on:
push:
branches:
- main
- develop

env:
firmware_version: '0.78.1'

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Flipper Zero Firmware
uses: actions/checkout@v3
with:
repository: 'flipperdevices/flipperzero-firmware'
ref: ${{ env.firmware_version }}
submodules: true
- name: Checkout FlipBIP
uses: actions/checkout@v3
with:
path: 'applications_user/FlipBIP'
- name: Build FAPs
run: ./fbt COMPACT=1 DEBUG=0 faps
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Release

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'

env:
firmware_version: '0.78.1'

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Flipper Zero Firmware
uses: actions/checkout@v3
with:
repository: 'flipperdevices/flipperzero-firmware'
ref: ${{ env.firmware_version }}
submodules: true
- name: Checkout FlipBIP
uses: actions/checkout@v3
with:
path: 'applications_user/FlipBIP'
- name: Build FAPs
run: ./fbt COMPACT=1 DEBUG=0 faps
- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1
with:
strip_v: false
- name: Publish FlipBIP
uses: softprops/action-gh-release@v1
with:
files: |
build/f7-firmware-C/.extapps/FlipBIP.fap
applications_user/FlipBIP/README.md
name: ${{steps.tag.outputs.tag}}
body: Built against Flipper Zero firmware v${{ env.firmware_version }}
generate_release_notes: true
fail_on_unmatched_files: true
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ The application will be compiled and copied onto your device
- Import your own mnemonic
- Lots of typing required but you can now use the wallet with an existing mnemonic you have saved
- Useful to convert paper backup to keys and receive addresses without relying on a laptop or phone
- Improved receive address generation features
- Addresses are now generated at the same time as other pieces of wallet info
- This slows down initial wallet load, but makes UI much more responsive
- QR code files are now generated for each address and stored in the `apps_data/flipbip` directory
- This app is required to view the QR code files: https://github.com/bmatcuk/flipperzero-qrcode (included in RM firmware)
- NOTE: This happens during the `View Wallet` step; you must view a wallet after generating/importing a wallet in order to ensure the address QR files are correct
- Broke out crypto functionality into its own library using `fap_private_libs` feature

### Work in Progress

Expand Down
7 changes: 6 additions & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ App(
requires=[
"gui",
],
stack_size=2 * 1024,
stack_size=3 * 1024,
order=10,
fap_icon="flipbip_10px.png",
fap_icon_assets="icons",
fap_private_libs=[
Lib(
name="crypto",
),
],
fap_category="Misc",
fap_description="Crypto toolkit for Flipper",
fap_author="Struan Clark (xtruan)",
Expand Down
7 changes: 4 additions & 3 deletions flipbip.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "flipbip.h"
#include "crypto/memzero.h"
#include "crypto/bip39.h"
#include "helpers/flipbip_file.h"
#include "helpers/flipbip_haptic.h"
// From: lib/crypto
#include <memzero.h>
#include <bip39.h>

bool flipbip_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Expand Down Expand Up @@ -49,7 +50,7 @@ static void text_input_callback(void* context) {
if(mnemonic_check(app->import_mnemonic_text) == 0)
status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
// Save the mnemonic to persistent storage
else if(!flipbip_save_settings_secure(app->import_mnemonic_text))
else if(!flipbip_save_file_secure(app->import_mnemonic_text))
status = FlipBipStatusSaveError; // 12 = save error

if(status == FlipBipStatusSuccess) {
Expand Down
4 changes: 2 additions & 2 deletions flipbip.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "views/flipbip_startscreen.h"
#include "views/flipbip_scene_1.h"

#define FLIPBIP_VERSION "v0.0.6"
#define FLIPBIP_VERSION "v0.0.7"

#define COIN_BTC 0
#define COIN_DOGE 3
Expand Down Expand Up @@ -96,4 +96,4 @@ typedef enum {
FlipBipStatusLoadError = 11,
FlipBipStatusSaveError = 12,
FlipBipStatusMnemonicCheckError = 13,
} FlipBipStatus;
} FlipBipStatus;
121 changes: 83 additions & 38 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
#include "flipbip_file.h"
#include "../helpers/flipbip_string.h"

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

#include <storage/storage.h>
#include <applications.h>
#include <loader/loader.h>
#include "../helpers/flipbip_string.h"
// From: lib/crypto
#include <memzero.h>
#include <rand.h>

// #define FLIPBIP_APP_BASE_FOLDER APP_DATA_PATH("flipbip")
#define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
#define FLIPBIP_APP_BASE_FOLDER_PATH(path) FLIPBIP_APP_BASE_FOLDER "/" path
#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;
const size_t FILE_SLEN = 512;
#define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME)
#define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_DAT_FILE_NAME_BAK)
#define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME)
#define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER_PATH(FLIPBIP_KEY_FILE_NAME_BAK)

const char* TEXT_QRFILE = "Filetype: QRCode\n"
"Version: 0\n"
"Message: "; // 37 chars + 1 null
#define FILE_HLEN 4
#define FILE_KLEN 256
#define FILE_SLEN 512
#define FILE_MAX_PATH_LEN 48
const char* FILE_HSTR = "fb01";
const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
"baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";

bool flipbip_load_settings(char* settings, bool key_file) {
bool flipbip_load_file(char* settings, const FlipBipFile file_type, const char* file_name) {
bool ret = false;
const char* path;
if(key_file) {
if(file_type == FlipBipFileKey) {
path = FLIPBIP_KEY_PATH;
} else {
} else if(file_type == FlipBipFileDat) {
path = FLIPBIP_DAT_PATH;
} else {
char path_buf[32] = {0};
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER);
strcpy(path_buf + strlen(path_buf), "/");
strcpy(path_buf + strlen(path_buf), file_name);
path = path_buf;
}

Storage* fs_api = furi_record_open(RECORD_STORAGE);
Expand Down Expand Up @@ -74,34 +87,53 @@ bool flipbip_load_settings(char* settings, bool key_file) {
return ret;
}

bool flipbip_has_settings(bool key_file) {
bool flipbip_has_file(const FlipBipFile file_type, const char* file_name, const bool remove) {
bool ret = false;
const char* path;
if(key_file) {
if(file_type == FlipBipFileKey) {
path = FLIPBIP_KEY_PATH;
} else {
} else if(file_type == FlipBipFileDat) {
path = FLIPBIP_DAT_PATH;
} else {
char path_buf[32] = {0};
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER);
strcpy(path_buf + strlen(path_buf), "/");
strcpy(path_buf + strlen(path_buf), file_name);
path = path_buf;
}

Storage* fs_api = furi_record_open(RECORD_STORAGE);
if(storage_file_exists(fs_api, path)) {
ret = true;
if(remove) {
ret = storage_simply_remove(fs_api, path);
} else {
ret = storage_file_exists(fs_api, path);
}
furi_record_close(RECORD_STORAGE);

return ret;
}

bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
bool flipbip_save_file(
const char* settings,
const FlipBipFile file_type,
const char* file_name,
const bool append) {
bool ret = false;
const char* path;
const char* path_bak;
if(key_file) {
if(file_type == FlipBipFileKey) {
path = FLIPBIP_KEY_PATH;
path_bak = FLIPBIP_KEY_PATH_BAK;
} else {
} else if(file_type == FlipBipFileDat) {
path = FLIPBIP_DAT_PATH;
path_bak = FLIPBIP_DAT_PATH_BAK;
} else {
char path_buf[FILE_MAX_PATH_LEN] = {0};
strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22
strcpy(path_buf + strlen(path_buf), "/");
strcpy(path_buf + strlen(path_buf), file_name);
path = path_buf;
path_bak = NULL;
}
int open_mode = FSOM_OPEN_ALWAYS;
if(append) {
Expand All @@ -116,7 +148,7 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
// return ret;
// }
// try to create the folder
storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
storage_simply_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);

File* settings_file = storage_file_alloc(fs_api);
if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
Expand All @@ -127,28 +159,41 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
storage_file_close(settings_file);
storage_file_free(settings_file);

File* settings_file_bak = storage_file_alloc(fs_api);
if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
storage_file_write(settings_file_bak, settings, strlen(settings));
storage_file_write(settings_file_bak, "\n", 1);
if(path_bak != NULL) {
File* settings_file_bak = storage_file_alloc(fs_api);
if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
storage_file_write(settings_file_bak, settings, strlen(settings));
storage_file_write(settings_file_bak, "\n", 1);
}
storage_file_close(settings_file_bak);
storage_file_free(settings_file_bak);
}
storage_file_close(settings_file_bak);
storage_file_free(settings_file_bak);

furi_record_close(RECORD_STORAGE);

return ret;
}

bool flipbip_load_settings_secure(char* settings) {
bool flipbip_save_qrfile(
const char* qr_msg_prefix,
const char* qr_msg_content,
const char* file_name) {
char qr_buf[90] = {0};
strcpy(qr_buf, TEXT_QRFILE);
strcpy(qr_buf + strlen(qr_buf), qr_msg_prefix);
strcpy(qr_buf + strlen(qr_buf), qr_msg_content);
return flipbip_save_file(qr_buf, FlipBipFileOther, file_name, false);
}

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

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

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

// check header
if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
Expand All @@ -174,7 +219,7 @@ bool flipbip_load_settings_secure(char* settings) {
data -= FILE_HLEN;

// load data from file
if(!flipbip_load_settings(data, false)) return false;
if(!flipbip_load_file(data, FlipBipFileDat, NULL)) return false;

// check header
if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] ||
Expand Down Expand Up @@ -207,7 +252,7 @@ bool flipbip_load_settings_secure(char* settings) {
return true;
}

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

// cap settings to 256 bytes
Expand Down Expand Up @@ -238,7 +283,7 @@ bool flipbip_save_settings_secure(const char* settings) {
// seek <-- header
data -= FILE_HLEN;
// save k2 to file
flipbip_save_settings(data, true, false);
flipbip_save_file(data, FlipBipFileKey, NULL, false);
// seek --> header
data += FILE_HLEN;
// zero k2 memory
Expand All @@ -251,7 +296,7 @@ bool flipbip_save_settings_secure(const char* settings) {
// seek <-- header
data -= FILE_HLEN;
// save data to file
flipbip_save_settings(data, false, false);
flipbip_save_file(data, FlipBipFileDat, NULL, false);

// clear memory
memzero(data, dlen);
Expand All @@ -260,4 +305,4 @@ bool flipbip_save_settings_secure(const char* settings) {
memzero(k2, FILE_KLEN / 2);

return true;
}
}
Loading

0 comments on commit e7943e3

Please sign in to comment.