Skip to content

Commit 8adb2da

Browse files
Merge pull request #513 from LedgerHQ/fbe/further_reduce_stack
Further reduce stack usage
2 parents 794d266 + c353982 commit 8adb2da

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ else
266266
APP_SOURCE_PATH += src_bagl
267267
endif
268268

269+
# Allow usage of function from lib_standard_app/crypto_helpers.c
270+
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c
271+
269272
### initialize plugin SDK submodule if needed, rebuild it, and warn if a difference is noticed
270273
ifeq ($(CHAIN),ethereum)
271274
ifneq ($(shell git submodule status | grep '^[-+]'),)

src_features/signTx/logic_signTx.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,14 @@ static int strcasecmp_workaround(const char *str1, const char *str2) {
319319
return 0;
320320
}
321321

322-
void finalizeParsing(bool direct) {
322+
__attribute__((noinline)) static void finalize_parsing_helper(bool direct, bool *use_standard_UI) {
323323
char displayBuffer[50];
324324
uint8_t decimals = WEI_TO_ETHER;
325325
uint64_t chain_id = get_tx_chain_id();
326326
const char *ticker = get_displayable_ticker(&chain_id);
327327
ethPluginFinalize_t pluginFinalize;
328-
bool use_standard_UI = true;
328+
329+
*use_standard_UI = true;
329330

330331
// Verify the chain
331332
if (chainConfig->chainId != ETHEREUM_MAINNET_CHAINID) {
@@ -396,7 +397,7 @@ void finalizeParsing(bool direct) {
396397
switch (pluginFinalize.uiType) {
397398
case ETH_UI_TYPE_GENERIC:
398399
// Use the dedicated ETH plugin UI
399-
use_standard_UI = false;
400+
*use_standard_UI = false;
400401
tmpContent.txContent.dataPresent = false;
401402
// Add the number of screens + the number of additional screens to get the total
402403
// number of screens needed.
@@ -405,7 +406,7 @@ void finalizeParsing(bool direct) {
405406
break;
406407
case ETH_UI_TYPE_AMOUNT_ADDRESS:
407408
// Use the standard ETH UI as this plugin uses the amount/address UI
408-
use_standard_UI = true;
409+
*use_standard_UI = true;
409410
tmpContent.txContent.dataPresent = false;
410411
if ((pluginFinalize.amount == NULL) || (pluginFinalize.address == NULL)) {
411412
PRINTF("Incorrect amount/address set by plugin\n");
@@ -443,7 +444,7 @@ void finalizeParsing(bool direct) {
443444
}
444445

445446
// User has just validated a swap but ETH received apdus about a non standard plugin / contract
446-
if (G_called_from_swap && !use_standard_UI) {
447+
if (G_called_from_swap && !*use_standard_UI) {
447448
PRINTF("ERR_SILENT_MODE_CHECK_FAILED, G_called_from_swap\n");
448449
THROW(ERR_SILENT_MODE_CHECK_FAILED);
449450
}
@@ -457,7 +458,7 @@ void finalizeParsing(bool direct) {
457458
}
458459

459460
// Prepare destination address and amount to display
460-
if (use_standard_UI) {
461+
if (*use_standard_UI) {
461462
// Format the address in a temporary buffer, if in swap case compare it with validated
462463
// address, else commit it
463464
address_to_string(tmpContent.txContent.destination,
@@ -532,11 +533,14 @@ void finalizeParsing(bool direct) {
532533
// Prepare network field
533534
get_network_as_string(strings.common.network_name, sizeof(strings.common.network_name));
534535
PRINTF("Network: %s\n", strings.common.network_name);
536+
}
535537

538+
void finalizeParsing(bool direct) {
539+
bool use_standard_UI;
536540
bool no_consent_check;
537-
538-
// If called from swap, the user as already validated a standard transaction
539-
// We have already checked the fields of this transaction above
541+
finalize_parsing_helper(direct, &use_standard_UI);
542+
// If called from swap, the user has already validated a standard transaction
543+
// And we have already checked the fields of this transaction above
540544
no_consent_check = G_called_from_swap && use_standard_UI;
541545

542546
#ifdef NO_CONSENT

src_features/signTx/ui_common_signTx.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1+
#include "lib_standard_app/crypto_helpers.h"
12
#include "os_io_seproxyhal.h"
23
#include "shared_context.h"
34
#include "utils.h"
45
#include "common_ui.h"
56
#include "handle_swap_sign_transaction.h"
67

78
unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_element_t *e) {
8-
uint8_t privateKeyData[INT256_LENGTH];
9-
uint8_t signature[100];
10-
cx_ecfp_private_key_t privateKey;
11-
uint32_t tx = 0;
9+
uint32_t info = 0;
1210
int err;
13-
io_seproxyhal_io_heartbeat();
14-
os_perso_derive_node_bip32(CX_CURVE_256K1,
15-
tmpCtx.transactionContext.bip32.path,
16-
tmpCtx.transactionContext.bip32.length,
17-
privateKeyData,
18-
NULL);
19-
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &privateKey);
20-
explicit_bzero(privateKeyData, sizeof(privateKeyData));
21-
unsigned int info = 0;
22-
io_seproxyhal_io_heartbeat();
23-
cx_ecdsa_sign(&privateKey,
24-
CX_RND_RFC6979 | CX_LAST,
25-
CX_SHA256,
26-
tmpCtx.transactionContext.hash,
27-
sizeof(tmpCtx.transactionContext.hash),
28-
signature,
29-
sizeof(signature),
30-
&info);
31-
explicit_bzero(&privateKey, sizeof(privateKey));
11+
if (bip32_derive_ecdsa_sign_rs_hash_256(CX_CURVE_256K1,
12+
tmpCtx.transactionContext.bip32.path,
13+
tmpCtx.transactionContext.bip32.length,
14+
CX_RND_RFC6979 | CX_LAST,
15+
CX_SHA256,
16+
tmpCtx.transactionContext.hash,
17+
sizeof(tmpCtx.transactionContext.hash),
18+
G_io_apdu_buffer + 1,
19+
G_io_apdu_buffer + 1 + 32,
20+
&info) != CX_OK) {
21+
THROW(0x6F00);
22+
}
23+
3224
if (txContext.txType == EIP1559 || txContext.txType == EIP2930) {
3325
if (info & CX_ECCINFO_PARITY_ODD) {
3426
G_io_apdu_buffer[0] = 1;
@@ -57,13 +49,13 @@ unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_elemen
5749
G_io_apdu_buffer[0] += 2;
5850
}
5951
}
60-
format_signature_out(signature);
61-
tx = 65;
62-
G_io_apdu_buffer[tx++] = 0x90;
63-
G_io_apdu_buffer[tx++] = 0x00;
52+
53+
// Write status code at parity_byte + r + s
54+
G_io_apdu_buffer[1 + 64] = 0x90;
55+
G_io_apdu_buffer[1 + 64 + 1] = 0x00;
6456

6557
// Send back the response, do not restart the event loop
66-
err = io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, tx);
58+
err = io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 1 + 64 + 2);
6759
if (G_called_from_swap) {
6860
PRINTF("G_called_from_swap\n");
6961

0 commit comments

Comments
 (0)