Skip to content

Commit

Permalink
Merge pull request #29 from trilitech/elric1@cleanups
Browse files Browse the repository at this point in the history
Random cleanups
  • Loading branch information
elric1 authored Sep 28, 2023
2 parents c5230d6 + 9d45fca commit fe8d7a3
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 125 deletions.
24 changes: 18 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
all: app_nanos.tgz app_nanosp.tgz app_nanox.tgz
debug: app_nanos_dbg.tgz app_nanosp_dbg.tgz app_nanox_dbg.tgz

.PHONY: clean all debug integration_tests unit_tests \
.PHONY: clean all debug integration_tests unit_tests scan-build% \
integration_tests_basic integration_tests_basic_%

DOCKER = docker
Expand Down Expand Up @@ -40,16 +40,28 @@ docker_images: docker_speculos \
docker_ledger_app_ocaml \
docker_ledger_app_integration_tests

scan-build-%:
SDK=$(shell echo $@ | sed 's/scan-build-\(.*\)/\U\1/')_SDK; \
$(DOCKER_RUN_APP_BUILDER) bash -c \
"BOLOS_SDK=\$$$$SDK make -C app scan-build"

scan-build: scan-build-nanos scan-build-nanosp \
scan-build-nanox scan-build-stax

app_%.tgz: app/src/*.[ch] \
app/src/parser/*.[ch] \
app/Makefile
$(DOCKER_RUN_APP_BUILDER) bash -c \
'BOLOS_SDK=$$$(shell echo $(patsubst app_%.tgz,%,$@) | tr '[:lower:]' '[:upper:]')_SDK make -C app'
SDK=$(shell echo $@ | sed 's/app_\(.*\).tgz/\U\1/')_SDK; \
$(DOCKER_RUN_APP_BUILDER) bash -c \
"BOLOS_SDK=\$$$$SDK make -C app"
$(DOCKER_RUN_APP_BUILDER) bash -c "cd app/bin/ && tar cz ." > $@

app_%_dbg.tgz: app/src/*.[ch] app/src/parser/*.[ch] app/Makefile
$(DOCKER_RUN_APP_BUILDER) bash -c \
'BOLOS_SDK=$$$(shell echo $(patsubst app_%_dbg.tgz,%,$@) | tr '[:lower:]' '[:upper:]')_SDK make -C app DEBUG=1'
app_%_dbg.tgz: app/src/*.[ch] \
app/src/parser/*.[ch] \
app/Makefile
SDK=$(shell echo $@ | sed 's/app_\(.*\)_dbg.tgz/\U\1/')_SDK; \
$(DOCKER_RUN_APP_BUILDER) bash -c \
"BOLOS_SDK=\$$$$SDK make -C app DEBUG=1"
$(DOCKER_RUN_APP_BUILDER) bash -c "cd app/bin/ && tar cz ." > $@

clean:
Expand Down
5 changes: 2 additions & 3 deletions app/src/apdu_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ provide_pubkey(void)
// Application could be PIN-locked, and pubkey->W_len would then be 0,
// so throwing an error rather than returning an empty key
TZ_ASSERT(EXC_SECURITY, os_global_pin_is_validated() == BOLOS_UX_OK);
TZ_CHECK(generate_public_key(&pubkey,
global.path_with_curve.derivation_type,
&global.path_with_curve.bip32_path));
TZ_CHECK(derive_pk(&pubkey, global.path_with_curve.derivation_type,
&global.path_with_curve.bip32_path));

byte = pubkey.W_len;
bufs[0].ptr = &byte;
Expand Down
111 changes: 63 additions & 48 deletions app/src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,63 +150,79 @@ send_continue(void)
}

static void
refill()
refill_blo_im_full(void)
{
size_t wrote = 0;
tz_parser_state *st = &global.apdu.sign.u.clear.parser_state;
size_t wrote = 0;
TZ_PREAMBLE(("void"));

skip:
while (!TZ_IS_BLOCKED(tz_operation_parser_step(st)))
;
PRINTF("[DEBUG] refill(errno: %s) \n", tz_parser_result_name(st->errno));
switch (st->errno) {
case TZ_BLO_IM_FULL:
last_screen:
if (!global.apdu.sign.u.clear.skip_to_sign) {
global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;
wrote = tz_ui_stream_push(TZ_UI_STREAM_CB_NOCB, st->field_name,
global.line_buf, TZ_UI_ICON_NONE);
} else {
global.apdu.sign.step = SIGN_ST_WAIT_DATA;
wrote = TZ_UI_STREAM_CONTENTS_SIZE;
}
tz_parser_flush_up_to(st, global.line_buf, TZ_UI_STREAM_CONTENTS_SIZE,
wrote);
TZ_POSTAMBLE;
}

tz_parser_flush_up_to(st, global.line_buf, TZ_UI_STREAM_CONTENTS_SIZE,
wrote);

// Do as much parsing as we can in one go when skipping.
// TODO: perhaps when skipping we could only skip the current
// operation, rather than the whole rest-of-batch
if (global.apdu.sign.u.clear.skip_to_sign)
goto skip;
static void
refill_blo_done(void)
{
tz_parser_state *st = &global.apdu.sign.u.clear.parser_state;
TZ_PREAMBLE(("void"));

break;
case TZ_BLO_FEED_ME:
TZ_CHECK(send_continue());
break;
case TZ_BLO_DONE:
TZ_ASSERT(EXC_UNEXPECTED_STATE,
global.apdu.sign.received_last_msg && st->regs.ilen == 0);
if (st->regs.oofs != 0)
goto last_screen;
global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;
tz_ui_stream_push_accept_reject();
tz_ui_stream_close();
break;
case TZ_ERR_INVALID_STATE:
tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, "Unknown error", "",
TZ_UI_ICON_CROSS);
tz_ui_stream_close();
break;
case TZ_ERR_INVALID_TAG:
case TZ_ERR_INVALID_OP:
case TZ_ERR_INVALID_DATA:
case TZ_ERR_UNSUPPORTED:
case TZ_ERR_TOO_LARGE:
case TZ_ERR_TOO_DEEP:
tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, "Parsing error",
tz_parser_result_name(st->errno), TZ_UI_ICON_CROSS);
tz_ui_stream_close();
break;
default:
TZ_FAIL(EXC_UNEXPECTED_STATE);
TZ_ASSERT(EXC_UNEXPECTED_STATE,
global.apdu.sign.received_last_msg && st->regs.ilen == 0);
if (st->regs.oofs != 0) {
refill_blo_im_full();
TZ_SUCCEED();
}
global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;
tz_ui_stream_push_accept_reject();
tz_ui_stream_close();

TZ_POSTAMBLE;
}

static void
refill_error(void)
{
tz_parser_state *st = &global.apdu.sign.u.clear.parser_state;
TZ_PREAMBLE(("void"));

tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, "Parsing error",
tz_parser_result_name(st->errno), TZ_UI_ICON_CROSS);
tz_ui_stream_close();
TZ_POSTAMBLE;
}

static void
refill(void)
{
tz_parser_state *st = &global.apdu.sign.u.clear.parser_state;
bool s2s = global.apdu.sign.u.clear.skip_to_sign;
TZ_PREAMBLE(("void"));

do {
while (!TZ_IS_BLOCKED(tz_operation_parser_step(st)))
;
PRINTF("[DEBUG] refill(errno: %s)\n",
tz_parser_result_name(st->errno));
// clang-format off
switch (st->errno) {
case TZ_BLO_IM_FULL: TZ_CHECK(refill_blo_im_full()); break;
case TZ_BLO_FEED_ME: TZ_CHECK(send_continue()); break;
case TZ_BLO_DONE: TZ_CHECK(refill_blo_done()); break;
default: TZ_CHECK(refill_error()); break;
}
// clang-format on
} while (s2s
&& (st->errno == TZ_BLO_IM_FULL
|| (st->errno == TZ_BLO_DONE && st->regs.oofs > 0)));

TZ_POSTAMBLE;
}
Expand Down Expand Up @@ -494,7 +510,6 @@ handle_data_apdu_blind(packet_t *pkt)
char request[80];
snprintf(request, sizeof(request), "Review request to blind\nsign %s",
type);
global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;

transaction_type = type;
STRLCPY(hash, obuf);
Expand Down
55 changes: 30 additions & 25 deletions app/src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ app_exit(void)
os_sched_exit(-1);
}

static void
print_memory_layout(void)
{
PRINTF("[PTR] stack canary: 0x%x\n", &app_stack_canary);
PRINTF("[PTR] G_io_apdu_buffer: 0x%p\n", G_io_apdu_buffer);
PRINTF("[PTR] global: 0x%p\n", &global);
PRINTF("[SIZEOF] global: %d\n", sizeof(global));
PRINTF("[SIZEOF] global.apdu.sign: %d\n", sizeof(global.apdu.sign));
PRINTF("[SIZEOF] global.apdu.hash: %d\n", sizeof(global.apdu.hash));
PRINTF("[SIZEOF] global.stream: %d\n", sizeof(global.stream));
PRINTF("[PTR] G_io_apdu_buffer: 0x%p\n", G_io_apdu_buffer);
PRINTF("[SIZEOF] G_io_apdu_buffer: %u\n", sizeof(G_io_apdu_buffer));
PRINTF("[PTR] G_io_seproxyhal_spi_buffer: 0x%p\n",
G_io_seproxyhal_spi_buffer);
PRINTF("[SIZEOF] G_io_seproxyhal_spi_buffer: %u\n",
sizeof(G_io_seproxyhal_spi_buffer));
PRINTF("[PTR] G_ux: 0x%p\n", &G_ux);
PRINTF("[SIZEOF] G_ux: %u\n", sizeof(G_ux));
PRINTF("[PTR] G_ux_os: 0x%p\n", &G_ux_os);
PRINTF("[SIZEOF] G_ux_os: %u\n", sizeof(G_ux_os));
PRINTF("[PTR] G_ux_params: 0x%p\n", &G_ux_params);
PRINTF("[SIZEOF] G_ux_params: %u\n", sizeof(G_ux_params));
PRINTF("[PTR] G_io_usb_ep_buffer: 0x%p\n", G_io_usb_ep_buffer);
PRINTF("[SIZEOF] G_io_usb_ep_buffer: %d\n", sizeof(G_io_usb_ep_buffer));
PRINTF("[PTR] G_io_app: 0x%p\n", &G_io_app);
PRINTF("[SIZEOF] G_io_app: %d\n", sizeof(G_io_app));
}

static void
dispatch(command_t *cmd)
{
Expand Down Expand Up @@ -83,41 +111,18 @@ dispatch(command_t *cmd)
}

void
app_main()
app_main(void)
{
command_t cmd;
int rx;

app_stack_canary = 0xDEADBEEF;
FUNC_ENTER(("void"));

print_memory_layout();
io_init();
init_globals();

PRINTF("[PTR] stack canary: 0x%x\n", &app_stack_canary);
PRINTF("[PTR] G_io_apdu_buffer: 0x%p\n", G_io_apdu_buffer);
PRINTF("[PTR] global: 0x%p\n", &global);
PRINTF("[SIZEOF] global: %d\n", sizeof(global));
PRINTF("[SIZEOF] global.apdu.sign: %d\n", sizeof(global.apdu.sign));
PRINTF("[SIZEOF] global.apdu.hash: %d\n", sizeof(global.apdu.hash));
PRINTF("[SIZEOF] global.stream: %d\n", sizeof(global.stream));
PRINTF("[PTR] G_io_apdu_buffer: 0x%p\n", G_io_apdu_buffer);
PRINTF("[SIZEOF] G_io_apdu_buffer: %u\n", sizeof(G_io_apdu_buffer));
PRINTF("[PTR] G_io_seproxyhal_spi_buffer: 0x%p\n",
G_io_seproxyhal_spi_buffer);
PRINTF("[SIZEOF] G_io_seproxyhal_spi_buffer: %u\n",
sizeof(G_io_seproxyhal_spi_buffer));
PRINTF("[PTR] G_ux: 0x%p\n", &G_ux);
PRINTF("[SIZEOF] G_ux: %u\n", sizeof(G_ux));
PRINTF("[PTR] G_ux_os: 0x%p\n", &G_ux_os);
PRINTF("[SIZEOF] G_ux_os: %u\n", sizeof(G_ux_os));
PRINTF("[PTR] G_ux_params: 0x%p\n", &G_ux_params);
PRINTF("[SIZEOF] G_ux_params: %u\n", sizeof(G_ux_params));
PRINTF("[PTR] G_io_usb_ep_buffer: 0x%p\n", G_io_usb_ep_buffer);
PRINTF("[SIZEOF] G_io_usb_ep_buffer: %d\n", sizeof(G_io_usb_ep_buffer));
PRINTF("[PTR] G_io_app: 0x%p\n", &G_io_app);
PRINTF("[SIZEOF] G_io_app: %d\n", sizeof(G_io_app));

/* ST_ERROR implies that we are completely unknown and need to reset */
global.step = ST_ERROR;

Expand Down
3 changes: 2 additions & 1 deletion app/src/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#define SW_OK 0x9000

// Standard APDU error codes:
// https://www.eftlab.co.uk/index.php/site-map/knowledge-base/118-apdu-response-list
// https://www.eftlab.com/knowledge-base/complete-list-of-apdu-responses
// https://docs.zondax.ch/ledger-apps/starkware/APDU

#define EXC_WRONG_PARAM 0x6B00
#define EXC_WRONG_LENGTH 0x6C00
Expand Down
7 changes: 3 additions & 4 deletions app/src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ read_bip32_path(bip32_path_t *out, const uint8_t *in, size_t in_size)
}

void
generate_public_key(cx_ecfp_public_key_t *public_key,
derivation_type_t derivation_type,
const bip32_path_t *bip32_path)
derive_pk(cx_ecfp_public_key_t *public_key, derivation_type_t derivation_type,
const bip32_path_t *bip32_path)
{
TZ_PREAMBLE(("public_key=%p, derivation_type=%d, bip32_path=%p",
public_key, derivation_type, bip32_path));
Expand Down Expand Up @@ -100,7 +99,7 @@ derive_pkh(derivation_type_t derivation_type, const bip32_path_t *bip32_path,
TZ_PREAMBLE(("buffer=%p, len=%u", buffer, len));

TZ_ASSERT_NOTNULL(buffer);
TZ_CHECK(generate_public_key(&pubkey, derivation_type, bip32_path));
TZ_CHECK(derive_pk(&pubkey, derivation_type, bip32_path));
TZ_CHECK(public_key_hash(hash + 1, 20, NULL, derivation_type, &pubkey));
// clang-format off
switch (derivation_type) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ typedef struct {
} bip32_path_with_curve_t;

void read_bip32_path(bip32_path_t *, const uint8_t *, size_t);
void generate_public_key(cx_ecfp_public_key_t *, derivation_type_t,
const bip32_path_t *);
void derive_pk(cx_ecfp_public_key_t *, derivation_type_t,
const bip32_path_t *);
void derive_pkh(derivation_type_t, const bip32_path_t *, char *, size_t);
void sign(derivation_type_t, const bip32_path_t *, const uint8_t *, size_t,
uint8_t *, size_t *);
Expand Down
16 changes: 5 additions & 11 deletions app/src/parser/num_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,21 @@ tz_parse_nat_step(tz_num_parser_buffer *buffers, tz_num_parser_regs *regs,
bool
tz_string_to_mutez(const char *str, uint64_t *res)
{
int c;

if (str == NULL || res == NULL) {
PRINTF("[ERROR] Null parameter\n");
goto error;
return false;
}

memset(res, '\0', sizeof(uint64_t));
int r = 0;
char c;

*res = 0;
while ((c = *str++) != '\0') {
if (c < '0' || c > '9') {
PRINTF("[ERROR] Non-digit character: %c\n", c);
goto error;
return false;
}
r = r * 10 + (c - '0');
*res = *res * 10 + (c - '0');
}

return true;

error:
memset(res, '\0', sizeof(uint64_t));
return false;
}
Loading

0 comments on commit fe8d7a3

Please sign in to comment.