From 993358afd4900cdd1cb2769bbf2e7f5095d81e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Mon, 4 Mar 2024 09:32:06 +0100 Subject: [PATCH 1/3] Makefile: use standard app files --- Makefile | 2 -- src/boot.c | 72 --------------------------------------------------- src/globals.c | 11 -------- src/globals.h | 5 ---- src/main.c | 3 +++ 5 files changed, 3 insertions(+), 90 deletions(-) delete mode 100644 src/boot.c diff --git a/Makefile b/Makefile index 41dac256..71c47171 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,6 @@ VARIANT_VALUES = tezos_baking # OPTION DISABLE_STANDARD_APP_SYNC_RAPDU = 1 -DISABLE_STANDARD_APP_FILES = 1 -DEFINES += HAVE_LEGACY_PID ENABLE_BLUETOOTH = 1 ENABLE_NBGL_QRCODE = 1 diff --git a/src/boot.c b/src/boot.c deleted file mode 100644 index 4f705663..00000000 --- a/src/boot.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "ui.h" - -// Order matters -#include "os.h" -#include "cx.h" -#include "globals.h" - -__attribute__((noreturn)) void app_main(void); - -__attribute__((section(".boot"))) int main(arg0) { - // exit critical section - __asm volatile("cpsie i"); - - // ensure exception will work as planned - os_boot(); - - if (arg0 != 0) { - // Called as library from another app - exit_app(); - } else { - uint8_t tag; - init_globals(); - global.stack_root = &tag; - - for (;;) { - BEGIN_TRY { - TRY { - ui_init(); - - io_seproxyhal_init(); - -#ifdef HAVE_BLE - // grab the current plane mode setting - // requires "--appFlag 0x240" to be set in makefile - G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0); -#endif // HAVE_BLE - - USB_power(0); - USB_power(1); - -#ifdef HAVE_BLE - BLE_power(0, NULL); - BLE_power(1, NULL); -#endif // HAVE_BLE - - ui_initial_screen(); - - app_main(); - } - CATCH(EXCEPTION_IO_RESET) { - // reset IO and UX - CLOSE_TRY; - continue; - } - CATCH_OTHER(e) { - CLOSE_TRY; - break; - } - FINALLY { - } - } - END_TRY; - } - } - - // Only reached in case of uncaught exception - io_seproxyhal_power_off( -#if defined API_LEVEL && (API_LEVEL == 0 || API_LEVEL > 10) - false -#endif - ); // Should not be allowed dashboard access -} diff --git a/src/globals.c b/src/globals.c index 116c7776..eca1af72 100644 --- a/src/globals.c +++ b/src/globals.c @@ -16,23 +16,12 @@ globals_t global; -// These are strange variables that the SDK relies on us to define but uses directly itself. -ux_state_t G_ux; -bolos_ux_params_t G_ux_params; - -unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B]; - void clear_apdu_globals(void) { memset(&global.apdu, 0, sizeof(global.apdu)); } void init_globals(void) { memset(&global, 0, sizeof(global)); - - memset(&G_ux, 0, sizeof(G_ux)); - memset(&G_ux_params, 0, sizeof(G_ux_params)); - - memset(G_io_seproxyhal_spi_buffer, 0, sizeof(G_io_seproxyhal_spi_buffer)); } // DO NOT TRY TO INIT THIS. This can only be written via an system call. diff --git a/src/globals.h b/src/globals.h index ad52a020..3a48e4ea 100644 --- a/src/globals.h +++ b/src/globals.h @@ -99,7 +99,6 @@ typedef struct { bool is_blank_screen; } dynamic_display; - void *stack_root; apdu_handler handlers[INS_MAX + 1]; bip32_path_with_curve_t path_with_curve; @@ -130,10 +129,6 @@ typedef struct { extern globals_t global; -extern unsigned int app_stack_canary; // From SDK - -extern unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B]; - extern nvram_data const N_data_real; #define N_data (*(volatile nvram_data *) PIC(&N_data_real)) diff --git a/src/main.c b/src/main.c index 25b90012..8e87819c 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,9 @@ #include "memory.h" __attribute__((noreturn)) void app_main(void) { + init_globals(); + ui_initial_screen(); + // TODO: Consider using static initialization of a const, instead of this for (size_t i = 0; i < NUM_ELEMENTS(global.handlers); i++) { global.handlers[i] = handle_apdu_error; From bba3c8a5f11af18ff20713d0d5fa1c4b20291e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Mon, 4 Mar 2024 09:32:54 +0100 Subject: [PATCH 2/3] base58: use base58 encoding from ledger SDK --- src/apdu_sign.c | 1 - src/base58_encoding.c | 65 ------------------------------------------- src/base58_encoding.h | 5 ---- src/to_string.c | 9 +++--- src/ui_sign_bagl.c | 1 - src/ui_sign_nbgl.c | 1 - 6 files changed, 4 insertions(+), 78 deletions(-) delete mode 100644 src/base58_encoding.c delete mode 100644 src/base58_encoding.h diff --git a/src/apdu_sign.c b/src/apdu_sign.c index 323d3d1a..e0bcfa7a 100644 --- a/src/apdu_sign.c +++ b/src/apdu_sign.c @@ -2,7 +2,6 @@ #include "apdu.h" #include "baking_auth.h" -#include "base58_encoding.h" #include "globals.h" #include "keys.h" #include "memory.h" diff --git a/src/base58_encoding.c b/src/base58_encoding.c deleted file mode 100644 index 939aab24..00000000 --- a/src/base58_encoding.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2012-2014 Luke Dashjr - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the standard MIT license. - */ - -#include -#include -#include -#include -#include - -#include "base58_encoding.h" - -#define MAX_ENC_INPUT_SIZE 120 - -static const char b58digits_ordered[] = - "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - -bool b58enc(/* out */ char *b58, /* in/out */ size_t *b58sz, const void *data, size_t binsz) { - const uint8_t *bin = data; - int carry; - size_t i, j, high, zcount = 0; - size_t size; - uint8_t buf[MAX_ENC_INPUT_SIZE * 138 / 100 + 1] = {0}; - - while (zcount < binsz && !bin[zcount]) { - ++zcount; - } - - size = (binsz - zcount) * 138 / 100 + 1; - - if (size > MAX_ENC_INPUT_SIZE) { - return false; - } - - for (i = zcount, high = size - 1; i < binsz; ++i, high = j) { - for (carry = bin[i], j = size - 1; ((int) j >= 0) && ((j > high) || carry); --j) { - carry += 256 * buf[j]; - buf[j] = carry % 58; - carry /= 58; - } - } - - for (j = 0; (j < size) && !buf[j]; ++j) { - // Find the last index of buf regarding the size - } - - if (*b58sz <= zcount + size - j) { - *b58sz = zcount + size - j + 1; - return false; - } - - if (zcount) { - memset(b58, '1', zcount); - } - for (i = zcount; j < size; ++i, ++j) { - b58[i] = b58digits_ordered[buf[j]]; - } - b58[i] = '\0'; - *b58sz = i + 1; - - return true; -} diff --git a/src/base58_encoding.h b/src/base58_encoding.h deleted file mode 100644 index 8386690b..00000000 --- a/src/base58_encoding.h +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include - -/* Return true IFF successful, false otherwise. */ -bool b58enc(/* out */ char *b58, /* in/out */ size_t *b58sz, const void *bin, size_t binsz); diff --git a/src/to_string.c b/src/to_string.c index 447fed75..0568077e 100644 --- a/src/to_string.c +++ b/src/to_string.c @@ -1,7 +1,8 @@ #include "to_string.h" +#include + #include "apdu.h" -#include "base58_encoding.h" #include "keys.h" #include @@ -107,8 +108,7 @@ void pkh_to_string(char *const buff, memcpy(data.hash, hash, sizeof(data.hash)); compute_hash_checksum(data.checksum, &data, sizeof(data) - sizeof(data.checksum)); - size_t out_size = buff_size; - if (!b58enc(buff, &out_size, &data, sizeof(data))) { + if (base58_encode((const uint8_t *) &data, sizeof(data), buff, buff_size) == -1) { THROW(EXC_WRONG_LENGTH); } } @@ -131,8 +131,7 @@ void chain_id_to_string(char *const buff, size_t const buff_size, chain_id_t con compute_hash_checksum(data.checksum, &data, sizeof(data) - sizeof(data.checksum)); - size_t out_size = buff_size; - if (!b58enc(buff, &out_size, &data, sizeof(data))) { + if (base58_encode((const uint8_t *) &data, sizeof(data), buff, buff_size) == -1) { THROW(EXC_WRONG_LENGTH); } } diff --git a/src/ui_sign_bagl.c b/src/ui_sign_bagl.c index 8d94eda1..94ec7336 100644 --- a/src/ui_sign_bagl.c +++ b/src/ui_sign_bagl.c @@ -3,7 +3,6 @@ #include "apdu.h" #include "baking_auth.h" -#include "base58_encoding.h" #include "globals.h" #include "keys.h" #include "memory.h" diff --git a/src/ui_sign_nbgl.c b/src/ui_sign_nbgl.c index 41617d26..af3a96ad 100644 --- a/src/ui_sign_nbgl.c +++ b/src/ui_sign_nbgl.c @@ -4,7 +4,6 @@ #include "apdu.h" #include "baking_auth.h" -#include "base58_encoding.h" #include "globals.h" #include "keys.h" #include "memory.h" From 9f524a4c5efbeb87176fe3d3bab0dbf54b5675d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Mon, 4 Mar 2024 09:36:05 +0100 Subject: [PATCH 3/3] misra: enable the new checked misra-rules --- misra.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misra.json b/misra.json index 15f2e229..ee080658 100644 --- a/misra.json +++ b/misra.json @@ -2,6 +2,6 @@ "script": "misra.py", "args": [ "--rule-texts=misra.md", - "--suppress-rules 2.7,3.1,7.2,7.4,8.2,8.4,8.9,8.14,9.2,10.1,10.3,10.4,10.5,10.6,11.1,11.3,11.5,11.8,12.1,12.3,13.3,13.4,14.2,14.4,15.5,16.3,16.4,16.5,16.6,17.7,17.8,18.4,18.8,19.2,20.5,20.7,21.15,21.16" + "--suppress-rules 2.7,3.1,7.2,7.4,8.2,8.4,8.9,8.14,9.2,10.1,10.3,10.4,10.5,10.6,11.1,11.3,11.5,11.8,12.1,13.3,13.4,14.2,14.4,15.5,16.3,16.4,16.5,16.6,17.7,17.8,18.4,18.8,19.2,20.5,20.7,21.15,21.16" ] }