Skip to content

Commit

Permalink
Merge pull request #501 from LedgerHQ/fbe/decrease_ram_usage_in_lib_mode
Browse files Browse the repository at this point in the history
Decrease ram usage in lib mode
  • Loading branch information
fbeutin-ledger authored Dec 4, 2023
2 parents f1859ac + 1723386 commit ac7b6e1
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 110 deletions.
3 changes: 2 additions & 1 deletion makefile_conf/chain/ethereum.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
DEFINES += HAVE_ETH2
APPNAME = "Ethereum"
DEFINES_LIB=
APP_LOAD_FLAGS=--appFlags 0xa40
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
APP_LOAD_FLAGS=--appFlags 0xa40
3 changes: 2 additions & 1 deletion makefile_conf/chain/goerli.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
DEFINES += HAVE_ETH2
APPNAME = "Eth Goerli"
DEFINES_LIB=
APP_LOAD_FLAGS=--appFlags 0xa40
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
APP_LOAD_FLAGS=--appFlags 0xa40
3 changes: 2 additions & 1 deletion makefile_conf/chain/ropsten.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ APP_LOAD_PARAMS += --path "12381/3600" --curve bls12381g1
DEFINES += HAVE_ETH2
APPNAME = "Eth Ropsten"
DEFINES_LIB=
APP_LOAD_FLAGS=--appFlags 0xa40
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
APP_LOAD_FLAGS=--appFlags 0xa40
61 changes: 40 additions & 21 deletions src/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include "ethUtils.h"
#include "string.h"

#define ZERO(x) memset(&x, 0, sizeof(x))
#define ZERO(x) explicit_bzero(&x, sizeof(x))

int handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
void handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
params->result = 0;
PRINTF("Params on the address %d\n", (unsigned int) params);
PRINTF("Address to check %s\n", params->address_to_check);
PRINTF("Inside handle_check_address\n");
if (params->address_to_check == 0) {
PRINTF("Address to check == 0\n");
return 0;
return;
}

uint8_t i;
const uint8_t* bip32_path_ptr = params->address_parameters;
uint8_t bip32PathLength = *(bip32_path_ptr++);
cx_sha3_t local_sha3;
Expand All @@ -27,37 +27,55 @@ int handle_check_address(check_address_parameters_t* params, chain_config_t* cha
char address[51];
} locals_union1;
union group2 {
uint8_t privateKeyData[32];
uint8_t privateKeyData[64];
cx_ecfp_public_key_t publicKey;
} locals_union2;

if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH) ||
(bip32PathLength * 4 != params->address_parameters_length - 1)) {
PRINTF("Invalid path\n");
return 0;
return;
}
for (i = 0; i < bip32PathLength; i++) {
for (uint8_t i = 0; i < bip32PathLength; i++) {
locals_union1.bip32Path[i] = U4BE(bip32_path_ptr, 0);
bip32_path_ptr += 4;
}
os_perso_derive_node_bip32(CX_CURVE_256K1,
locals_union1.bip32Path,
bip32PathLength,
locals_union2.privateKeyData,
NULL);
if (os_derive_bip32_no_throw(CX_CURVE_256K1,
locals_union1.bip32Path,
bip32PathLength,
locals_union2.privateKeyData,
NULL) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}

ZERO(locals_union1);
cx_ecfp_init_private_key(CX_CURVE_256K1,
locals_union2.privateKeyData,
32,
&locals_union1.privateKey);
if (cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1,
locals_union2.privateKeyData,
32,
&locals_union1.privateKey) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}
ZERO(locals_union2);
cx_ecfp_generate_pair(CX_CURVE_256K1, &locals_union2.publicKey, &locals_union1.privateKey, 1);
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1,
&locals_union2.publicKey,
&locals_union1.privateKey,
1) != CX_OK) {
ZERO(locals_union1);
ZERO(locals_union2);
return;
}
ZERO(locals_union1);
if (!getEthAddressStringFromKey(&locals_union2.publicKey,
locals_union1.address,
&local_sha3,
chain_config->chainId)) {
THROW(CX_INVALID_PARAMETER);
ZERO(locals_union1);
ZERO(locals_union2);
return;
}
ZERO(locals_union2);

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

if (strcmp(locals_union1.address, params->address_to_check + offset_0x) != 0) {
PRINTF("Addresses don't match\n");
return 0;
} else {
PRINTF("Addresses match\n");
params->result = 1;
}
PRINTF("Addresses match\n");
return 1;
ZERO(locals_union1);
}
6 changes: 3 additions & 3 deletions src/handle_check_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "swap_lib_calls.h"
#include "chainConfig.h"

int handle_check_address(check_address_parameters_t* check_address_params,
chain_config_t* chain_config);
void handle_check_address(check_address_parameters_t* check_address_params,
chain_config_t* chain_config);

#endif // _HANDLE_CHECK_ADDRESS_H_
#endif // _HANDLE_CHECK_ADDRESS_H_
11 changes: 6 additions & 5 deletions src/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include <stdint.h>
#include <os.h>

int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain_config_t* config) {
void handle_get_printable_amount(get_printable_amount_parameters_t* params,
chain_config_t* config) {
uint8_t decimals;
char ticker[MAX_TICKER_LEN];
memset(params->printable_amount, 0, sizeof(params->printable_amount));
if (params->amount_length > 32) {
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
return 0;
return;
}

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

Expand All @@ -39,7 +40,7 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain
ticker,
params->printable_amount,
sizeof(params->printable_amount))) {
THROW(EXCEPTION_OVERFLOW);
memset(params->printable_amount, 0, sizeof(params->printable_amount));
}
return 1;
return;
}
6 changes: 3 additions & 3 deletions src/handle_get_printable_amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "swap_lib_calls.h"
#include "chainConfig.h"

int handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
chain_config_t* config);
void handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
chain_config_t* config);

#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_
#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_
12 changes: 6 additions & 6 deletions src/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
ticker,
stack_data.fullAmount,
sizeof(stack_data.fullAmount))) {
THROW(EXCEPTION_OVERFLOW);
return false;
}

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

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

void handle_swap_sign_transaction(chain_config_t* config) {
UX_INIT();
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config) {
#ifdef HAVE_NBGL
// On Stax, display a spinner at startup
UX_INIT();
nbgl_useCaseSpinner("Signing");
#endif // HAVE_NBGL

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

PRINTF("USB power ON/OFF\n");
USB_power(0);
USB_power(1);
// ui_idle();
PRINTF("USB power ON/OFF\n");
#ifdef HAVE_BLE
// grab the current plane mode setting
G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/handle_swap_sign_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params,
chain_config_t* config);

void handle_swap_sign_transaction(chain_config_t* config);
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config);

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

0 comments on commit ac7b6e1

Please sign in to comment.