Skip to content

Commit ac7b6e1

Browse files
Merge pull request #501 from LedgerHQ/fbe/decrease_ram_usage_in_lib_mode
Decrease ram usage in lib mode
2 parents f1859ac + 1723386 commit ac7b6e1

10 files changed

+126
-110
lines changed

makefile_conf/chain/ethereum.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
1111
DEFINES += HAVE_ETH2
1212
APPNAME = "Ethereum"
1313
DEFINES_LIB=
14-
APP_LOAD_FLAGS=--appFlags 0xa40
14+
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
15+
APP_LOAD_FLAGS=--appFlags 0xa40

makefile_conf/chain/goerli.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
1212
DEFINES += HAVE_ETH2
1313
APPNAME = "Eth Goerli"
1414
DEFINES_LIB=
15-
APP_LOAD_FLAGS=--appFlags 0xa40
15+
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
16+
APP_LOAD_FLAGS=--appFlags 0xa40

makefile_conf/chain/ropsten.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
1212
DEFINES += HAVE_ETH2
1313
APPNAME = "Eth Ropsten"
1414
DEFINES_LIB=
15-
APP_LOAD_FLAGS=--appFlags 0xa40
15+
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
16+
APP_LOAD_FLAGS=--appFlags 0xa40

src/handle_check_address.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#include "ethUtils.h"
55
#include "string.h"
66

7-
#define ZERO(x) memset(&x, 0, sizeof(x))
7+
#define ZERO(x) explicit_bzero(&x, sizeof(x))
88

9-
int handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
9+
void handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
10+
params->result = 0;
1011
PRINTF("Params on the address %d\n", (unsigned int) params);
1112
PRINTF("Address to check %s\n", params->address_to_check);
1213
PRINTF("Inside handle_check_address\n");
1314
if (params->address_to_check == 0) {
1415
PRINTF("Address to check == 0\n");
15-
return 0;
16+
return;
1617
}
1718

18-
uint8_t i;
1919
const uint8_t* bip32_path_ptr = params->address_parameters;
2020
uint8_t bip32PathLength = *(bip32_path_ptr++);
2121
cx_sha3_t local_sha3;
@@ -27,37 +27,55 @@ int handle_check_address(check_address_parameters_t* params, chain_config_t* cha
2727
char address[51];
2828
} locals_union1;
2929
union group2 {
30-
uint8_t privateKeyData[32];
30+
uint8_t privateKeyData[64];
3131
cx_ecfp_public_key_t publicKey;
3232
} locals_union2;
3333

3434
if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH) ||
3535
(bip32PathLength * 4 != params->address_parameters_length - 1)) {
3636
PRINTF("Invalid path\n");
37-
return 0;
37+
return;
3838
}
39-
for (i = 0; i < bip32PathLength; i++) {
39+
for (uint8_t i = 0; i < bip32PathLength; i++) {
4040
locals_union1.bip32Path[i] = U4BE(bip32_path_ptr, 0);
4141
bip32_path_ptr += 4;
4242
}
43-
os_perso_derive_node_bip32(CX_CURVE_256K1,
44-
locals_union1.bip32Path,
45-
bip32PathLength,
46-
locals_union2.privateKeyData,
47-
NULL);
43+
if (os_derive_bip32_no_throw(CX_CURVE_256K1,
44+
locals_union1.bip32Path,
45+
bip32PathLength,
46+
locals_union2.privateKeyData,
47+
NULL) != CX_OK) {
48+
ZERO(locals_union1);
49+
ZERO(locals_union2);
50+
return;
51+
}
52+
4853
ZERO(locals_union1);
49-
cx_ecfp_init_private_key(CX_CURVE_256K1,
50-
locals_union2.privateKeyData,
51-
32,
52-
&locals_union1.privateKey);
54+
if (cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1,
55+
locals_union2.privateKeyData,
56+
32,
57+
&locals_union1.privateKey) != CX_OK) {
58+
ZERO(locals_union1);
59+
ZERO(locals_union2);
60+
return;
61+
}
5362
ZERO(locals_union2);
54-
cx_ecfp_generate_pair(CX_CURVE_256K1, &locals_union2.publicKey, &locals_union1.privateKey, 1);
63+
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1,
64+
&locals_union2.publicKey,
65+
&locals_union1.privateKey,
66+
1) != CX_OK) {
67+
ZERO(locals_union1);
68+
ZERO(locals_union2);
69+
return;
70+
}
5571
ZERO(locals_union1);
5672
if (!getEthAddressStringFromKey(&locals_union2.publicKey,
5773
locals_union1.address,
5874
&local_sha3,
5975
chain_config->chainId)) {
60-
THROW(CX_INVALID_PARAMETER);
76+
ZERO(locals_union1);
77+
ZERO(locals_union2);
78+
return;
6179
}
6280
ZERO(locals_union2);
6381

@@ -68,8 +86,9 @@ int handle_check_address(check_address_parameters_t* params, chain_config_t* cha
6886

6987
if (strcmp(locals_union1.address, params->address_to_check + offset_0x) != 0) {
7088
PRINTF("Addresses don't match\n");
71-
return 0;
89+
} else {
90+
PRINTF("Addresses match\n");
91+
params->result = 1;
7292
}
73-
PRINTF("Addresses match\n");
74-
return 1;
93+
ZERO(locals_union1);
7594
}

src/handle_check_address.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "swap_lib_calls.h"
55
#include "chainConfig.h"
66

7-
int handle_check_address(check_address_parameters_t* check_address_params,
8-
chain_config_t* chain_config);
7+
void handle_check_address(check_address_parameters_t* check_address_params,
8+
chain_config_t* chain_config);
99

10-
#endif // _HANDLE_CHECK_ADDRESS_H_
10+
#endif // _HANDLE_CHECK_ADDRESS_H_

src/handle_get_printable_amount.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#include <stdint.h>
88
#include <os.h>
99

10-
int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain_config_t* config) {
10+
void handle_get_printable_amount(get_printable_amount_parameters_t* params,
11+
chain_config_t* config) {
1112
uint8_t decimals;
1213
char ticker[MAX_TICKER_LEN];
1314
memset(params->printable_amount, 0, sizeof(params->printable_amount));
1415
if (params->amount_length > 32) {
1516
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
16-
return 0;
17+
return;
1718
}
1819

1920
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
@@ -29,7 +30,7 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain
2930
ticker,
3031
&decimals)) {
3132
PRINTF("Error while parsing config\n");
32-
return 0;
33+
return;
3334
}
3435
}
3536

@@ -39,7 +40,7 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain
3940
ticker,
4041
params->printable_amount,
4142
sizeof(params->printable_amount))) {
42-
THROW(EXCEPTION_OVERFLOW);
43+
memset(params->printable_amount, 0, sizeof(params->printable_amount));
4344
}
44-
return 1;
45+
return;
4546
}

src/handle_get_printable_amount.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "swap_lib_calls.h"
55
#include "chainConfig.h"
66

7-
int handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
8-
chain_config_t* config);
7+
void handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
8+
chain_config_t* config);
99

10-
#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_
10+
#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_

src/handle_swap_sign_transaction.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
4141
ticker,
4242
stack_data.fullAmount,
4343
sizeof(stack_data.fullAmount))) {
44-
THROW(EXCEPTION_OVERFLOW);
44+
return false;
4545
}
4646

4747
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
@@ -53,7 +53,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
5353
ticker,
5454
stack_data.maxFee,
5555
sizeof(stack_data.maxFee))) {
56-
THROW(EXCEPTION_OVERFLOW);
56+
return false;
5757
}
5858

5959
// Full reset the global variables
@@ -71,9 +71,10 @@ void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_succes
7171
os_lib_end();
7272
}
7373

74-
void handle_swap_sign_transaction(chain_config_t* config) {
75-
UX_INIT();
74+
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config) {
7675
#ifdef HAVE_NBGL
76+
// On Stax, display a spinner at startup
77+
UX_INIT();
7778
nbgl_useCaseSpinner("Signing");
7879
#endif // HAVE_NBGL
7980

@@ -93,10 +94,9 @@ void handle_swap_sign_transaction(chain_config_t* config) {
9394
nvm_write((void*) &N_storage, (void*) &storage, sizeof(internalStorage_t));
9495
}
9596

97+
PRINTF("USB power ON/OFF\n");
9698
USB_power(0);
9799
USB_power(1);
98-
// ui_idle();
99-
PRINTF("USB power ON/OFF\n");
100100
#ifdef HAVE_BLE
101101
// grab the current plane mode setting
102102
G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0);

src/handle_swap_sign_transaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params,
77
chain_config_t* config);
88

9-
void handle_swap_sign_transaction(chain_config_t* config);
9+
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config);
1010

1111
void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);

0 commit comments

Comments
 (0)