Skip to content

Commit 298aebe

Browse files
authored
Merge pull request #5 from xtruan/develop
Memory management, minor cleanup
2 parents c54a1a7 + 2c72c2d commit 298aebe

File tree

5 files changed

+105
-61
lines changed

5 files changed

+105
-61
lines changed

crypto/bip32.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ int hdnode_get_ethereum_pubkeyhash(const HDNode* node, uint8_t* pubkeyhash) {
526526

527527
/* get uncompressed public key */
528528
if(ecdsa_get_public_key65(node->curve->params, node->private_key, buf) != 0) {
529+
memzero(ctx, sizeof(SHA3_CTX));
530+
free(ctx);
529531
return 0;
530532
}
531533

flipbip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ int32_t flipbip_app(void* p) {
179179
UNUSED(p);
180180
FlipBip* app = flipbip_app_alloc();
181181

182-
// Disabled because causes exit on customer firmwares such as RM
182+
// Disabled because causes exit on custom firmwares such as RM
183183
/*if(!furi_hal_region_is_provisioned()) {
184184
flipbip_app_free(app);
185185
return 1;

flipbip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "views/flipbip_startscreen.h"
1616
#include "views/flipbip_scene_1.h"
1717

18-
#define FLIPBIP_VERSION "v0.0.5"
18+
#define FLIPBIP_VERSION "v0.0.6"
1919

2020
#define COIN_BTC 0
2121
#define COIN_DOGE 3

helpers/flipbip_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool flipbip_load_settings(char* settings, bool key_file) {
5555
storage_file_free(settings_file);
5656
furi_record_close(RECORD_STORAGE);
5757

58-
if(!strlen(settings) == 0) {
58+
if(strlen(settings) > 0) {
5959
Storage* fs_api = furi_record_open(RECORD_STORAGE);
6060
FileInfo layout_file_info;
6161
FS_Error file_check_err = storage_common_stat(fs_api, path, &layout_file_info);

views/flipbip_scene_1.c

Lines changed: 100 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
#define DERIV_ACCOUNT 0
2323
#define DERIV_CHANGE 0
2424

25+
#define PAGE_LOADING 0
26+
#define PAGE_INFO 1
27+
#define PAGE_MNEMONIC 2
28+
#define PAGE_SEED 3
29+
#define PAGE_XPRV_ROOT 4
30+
#define PAGE_XPRV_ACCT 5
31+
#define PAGE_XPUB_ACCT 6
32+
#define PAGE_XPRV_EXTD 7
33+
#define PAGE_XPUB_EXTD 8
34+
#define PAGE_ADDR_BEGIN 9
35+
#define PAGE_ADDR_END 14 //18
36+
2537
#define TEXT_LOADING "Loading..."
2638
#define TEXT_NEW_WALLET "New wallet"
2739
#define TEXT_DEFAULT_COIN "Coin"
@@ -68,12 +80,15 @@ typedef struct {
6880
CONFIDENTIAL const char* xpub_extended;
6981
} FlipBipScene1Model;
7082

71-
static CONFIDENTIAL char s_disp_text1[30 + 1];
72-
static CONFIDENTIAL char s_disp_text2[30 + 1];
73-
static CONFIDENTIAL char s_disp_text3[30 + 1];
74-
static CONFIDENTIAL char s_disp_text4[30 + 1];
75-
static CONFIDENTIAL char s_disp_text5[30 + 1];
76-
static CONFIDENTIAL char s_disp_text6[30 + 1];
83+
static CONFIDENTIAL HDNode* s_addr_node = NULL;
84+
85+
static CONFIDENTIAL char* s_disp_text1 = NULL;
86+
static CONFIDENTIAL char* s_disp_text2 = NULL;
87+
static CONFIDENTIAL char* s_disp_text3 = NULL;
88+
static CONFIDENTIAL char* s_disp_text4 = NULL;
89+
static CONFIDENTIAL char* s_disp_text5 = NULL;
90+
static CONFIDENTIAL char* s_disp_text6 = NULL;
91+
7792
static const char* s_derivation_text = TEXT_DEFAULT_DERIV;
7893
//static bool s_busy = false;
7994

@@ -116,7 +131,8 @@ static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
116131

117132
static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
118133
// Delineate sections of the mnemonic every 4 words
119-
char* mnemonic_working = malloc(strlen(mnemonic) + 1);
134+
const size_t mnemonic_working_len = strlen(mnemonic) + 1;
135+
char* mnemonic_working = malloc(mnemonic_working_len);
120136
strcpy(mnemonic_working, mnemonic);
121137
int word = 0;
122138
for(size_t i = 0; i < strlen(mnemonic_working); i++) {
@@ -159,35 +175,36 @@ static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
159175
}
160176

161177
// Free the working mnemonic memory
162-
memzero(mnemonic_working, strlen(mnemonic_working));
178+
memzero(mnemonic_working, mnemonic_working_len);
163179
free(mnemonic_working);
164180
}
165181

166182
static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
167-
char* seed_working = malloc(64 * 2 + 1);
183+
const size_t seed_working_len = 64 * 2 + 1;
184+
char* seed_working = malloc(seed_working_len);
168185
// Convert the seed to a hex string
169186
flipbip_btox(model->seed, 64, seed_working);
170187

171188
flipbip_scene_1_draw_generic(seed_working, 22);
172189

173190
// Free the working seed memory
174-
memzero(seed_working, sizeof(seed_working));
191+
memzero(seed_working, seed_working_len);
175192
free(seed_working);
176193
}
177194

178195
static void
179196
flipbip_scene_1_draw_address(const HDNode* node, uint32_t addr_type, uint32_t addr_index) {
180197
//s_busy = true;
181198

182-
// buffer for key serialization
183-
const size_t buflen = 128;
184-
char buf[128 + 1];
199+
// buffer for address serialization
200+
const size_t buflen = 40;
201+
char buf[40 + 1] = {0};
185202

186-
HDNode* addr_node = malloc(sizeof(HDNode));
187-
memcpy(addr_node, node, sizeof(HDNode));
203+
// use static node for address generation
204+
memcpy(s_addr_node, node, sizeof(HDNode));
188205

189-
hdnode_private_ckd(addr_node, addr_index);
190-
hdnode_fill_public_key(addr_node);
206+
hdnode_private_ckd(s_addr_node, addr_index);
207+
hdnode_fill_public_key(s_addr_node);
191208

192209
// coin info
193210
// bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
@@ -199,32 +216,23 @@ static void
199216
if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
200217
// BTC / DOGE style address
201218
ecdsa_get_address(
202-
addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
219+
s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
203220

204-
char* address = malloc(buflen + 1);
205-
strncpy(address, buf, buflen);
206-
flipbip_scene_1_draw_generic(address, 12);
207-
memzero(address, buflen + 1);
208-
free(address);
221+
flipbip_scene_1_draw_generic(buf, 12);
209222

210223
//ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
211-
//char *wif = malloc(buflen + 1);
212-
//strncpy(wif, buf, buflen);
224+
213225
} else if(coin_info[5] == FlipBipCoinETH60) { // ETH
214226
// ETH style address
215-
hdnode_get_ethereum_pubkeyhash(addr_node, (uint8_t*)buf);
216-
char* address = malloc(42 + 1);
217-
memcpy(address, "0x", 2);
227+
hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
228+
char address[42 + 1] = {0};
229+
address[0] = '0';
230+
address[1] = 'x';
218231
// Convert the hash to a hex string
219232
flipbip_btox((uint8_t*)buf, 20, address + 2);
220233
flipbip_scene_1_draw_generic(address, 12);
221-
memzero(address, 42 + 1);
222-
free(address);
223234
}
224235

225-
memzero(addr_node, sizeof(HDNode));
226-
free(addr_node);
227-
228236
//s_busy = false;
229237
}
230238

@@ -243,32 +251,33 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
243251
canvas_set_color(canvas, ColorBlack);
244252

245253
flipbip_scene_1_clear_text();
246-
if(model->page == 1) {
254+
if(model->page == PAGE_INFO) {
247255
flipbip_scene_1_draw_generic(TEXT_INFO, 27);
248-
} else if(model->page == 2) {
256+
} else if(model->page == PAGE_MNEMONIC) {
249257
flipbip_scene_1_draw_mnemonic(model->mnemonic);
250-
} else if(model->page == 3) {
258+
} else if(model->page == PAGE_SEED) {
251259
flipbip_scene_1_draw_seed(model);
252-
} else if(model->page == 4) {
260+
} else if(model->page == PAGE_XPRV_ROOT) {
253261
flipbip_scene_1_draw_generic(model->xprv_root, 20);
254-
} else if(model->page == 5) {
262+
} else if(model->page == PAGE_XPRV_ACCT) {
255263
flipbip_scene_1_draw_generic(model->xprv_account, 20);
256-
} else if(model->page == 6) {
264+
} else if(model->page == PAGE_XPUB_ACCT) {
257265
flipbip_scene_1_draw_generic(model->xpub_account, 20);
258-
} else if(model->page == 7) {
266+
} else if(model->page == PAGE_XPRV_EXTD) {
259267
flipbip_scene_1_draw_generic(model->xprv_extended, 20);
260-
} else if(model->page == 8) {
268+
} else if(model->page == PAGE_XPUB_EXTD) {
261269
flipbip_scene_1_draw_generic(model->xpub_extended, 20);
262-
} else if(model->page >= 9 && model->page <= 13) {
263-
flipbip_scene_1_draw_address(model->node, model->coin, model->page - 9);
270+
} else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
271+
flipbip_scene_1_draw_address(model->node, model->coin, model->page - PAGE_ADDR_BEGIN);
264272
}
265273

266-
if(model->page == 0) {
274+
if(model->page == PAGE_LOADING) {
267275
canvas_set_font(canvas, FontPrimary);
268276
canvas_draw_str(canvas, 1, 10, TEXT_LOADING);
269277
canvas_draw_str(canvas, 6, 30, s_derivation_text);
270278
canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
271-
} else if(model->page >= 9 && model->page <= 13) {
279+
} else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
280+
// draw address header
272281
canvas_set_font(canvas, FontSecondary);
273282
// coin_name, derivation_path
274283
const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
@@ -278,6 +287,15 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
278287
const size_t receive_len = strlen(receive_text) * 7;
279288
canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, receive_text);
280289
canvas_draw_str_aligned(canvas, receive_len, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS);
290+
291+
// draw address number
292+
const unsigned char addr_num[1] = {(unsigned char)(model->page - PAGE_ADDR_BEGIN)};
293+
char addr_num_text[3];
294+
flipbip_btox(addr_num, 1, addr_num_text);
295+
addr_num_text[0] = '/';
296+
canvas_draw_str_aligned(canvas, 110, 2, AlignLeft, AlignTop, addr_num_text);
297+
298+
// draw address
281299
canvas_set_font(canvas, FontPrimary);
282300
canvas_draw_str(canvas, 6, 22, s_disp_text1);
283301
canvas_draw_str(canvas, 6, 34, s_disp_text2);
@@ -300,7 +318,7 @@ static int flipbip_scene_1_model_init(
300318
const uint32_t coin,
301319
const bool overwrite,
302320
const char* passphrase_text) {
303-
model->page = 0;
321+
model->page = PAGE_LOADING;
304322
model->mnemonic_only = false;
305323
model->strength = strength;
306324
model->coin = coin;
@@ -414,7 +432,7 @@ static int flipbip_scene_1_model_init(
414432

415433
model->node = node;
416434

417-
model->page = 1;
435+
model->page = PAGE_INFO;
418436

419437
#if USE_BIP39_CACHE
420438
// Clear the BIP39 cache
@@ -454,10 +472,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
454472
FlipBipScene1Model * model,
455473
{
456474
//UNUSED(model);
457-
model->page = (model->page + 1) % 14;
458-
if(model->page == 0) {
459-
model->page = 1;
475+
int page = (model->page + 1) % (PAGE_ADDR_END + 1);
476+
if(page == 0) {
477+
page = PAGE_INFO;
460478
}
479+
model->page = page;
461480
},
462481
true);
463482
break;
@@ -468,10 +487,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
468487
FlipBipScene1Model * model,
469488
{
470489
//UNUSED(model);
471-
model->page = (model->page - 1) % 14;
472-
if(model->page == 0) {
473-
model->page = 13;
490+
int page = (model->page - 1) % (PAGE_ADDR_END + 1);
491+
if(page == 0) {
492+
page = PAGE_ADDR_END;
474493
}
494+
model->page = page;
475495
},
476496
true);
477497
break;
@@ -490,8 +510,8 @@ void flipbip_scene_1_exit(void* context) {
490510
instance->view,
491511
FlipBipScene1Model * model,
492512
{
493-
model->page = 0;
494-
model->strength = 0;
513+
model->page = PAGE_LOADING;
514+
model->strength = FlipBipStrength256;
495515
model->coin = FlipBipCoinBTC0;
496516
memzero(model->seed, 64);
497517
// if mnemonic_only is true, then we don't need to free the data here
@@ -570,15 +590,15 @@ void flipbip_scene_1_enter(void* context) {
570590
// if error, set the error message
571591
if(status == FlipBipStatusSaveError) {
572592
model->mnemonic = "ERROR:,Save error";
573-
model->page = 2;
593+
model->page = PAGE_MNEMONIC;
574594
flipbip_play_long_bump(app);
575595
} else if(status == FlipBipStatusLoadError) {
576596
model->mnemonic = "ERROR:,Load error";
577-
model->page = 2;
597+
model->page = PAGE_MNEMONIC;
578598
flipbip_play_long_bump(app);
579599
} else if(status == FlipBipStatusMnemonicCheckError) {
580600
model->mnemonic = "ERROR:,Mnemonic check error";
581-
model->page = 2;
601+
model->page = PAGE_MNEMONIC;
582602
flipbip_play_long_bump(app);
583603
}
584604

@@ -602,6 +622,17 @@ FlipBipScene1* flipbip_scene_1_alloc() {
602622
view_set_enter_callback(instance->view, flipbip_scene_1_enter);
603623
view_set_exit_callback(instance->view, flipbip_scene_1_exit);
604624

625+
// allocate the address node
626+
s_addr_node = (HDNode*)malloc(sizeof(HDNode));
627+
628+
// allocate the display text
629+
s_disp_text1 = (char*)malloc(30 + 1);
630+
s_disp_text2 = (char*)malloc(30 + 1);
631+
s_disp_text3 = (char*)malloc(30 + 1);
632+
s_disp_text4 = (char*)malloc(30 + 1);
633+
s_disp_text5 = (char*)malloc(30 + 1);
634+
s_disp_text6 = (char*)malloc(30 + 1);
635+
605636
return instance;
606637
}
607638

@@ -611,7 +642,18 @@ void flipbip_scene_1_free(FlipBipScene1* instance) {
611642
with_view_model(
612643
instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
613644

645+
// free the address node
646+
memzero(s_addr_node, sizeof(HDNode));
647+
free(s_addr_node);
648+
649+
// free the display text
614650
flipbip_scene_1_clear_text();
651+
free(s_disp_text1);
652+
free(s_disp_text2);
653+
free(s_disp_text3);
654+
free(s_disp_text4);
655+
free(s_disp_text5);
656+
free(s_disp_text6);
615657

616658
view_free(instance->view);
617659
free(instance);

0 commit comments

Comments
 (0)