Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Aug 30, 2024
1 parent c290013 commit 9f4defd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

case BCOMP_MAKE_TRANSFER_TX: {
CHECK_PIN_VALIDATED()
legacy_handleSignTransferTx(flags, tx, rx);
break;
}

#if defined(APP_TESTING)
case INS_TEST: {
handleTest(flags, tx, rx);
Expand Down
29 changes: 28 additions & 1 deletion app/src/apdu_handler_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
static bool tx_initialized = false;
static uint32_t payload_length = 0;
static uint32_t hdpath_length = 0;
static tx_type_t tx_type = tx_type_json;

__Z_INLINE void legacy_app_sign() {
const uint8_t *message = tx_get_buffer();
const uint16_t messageLength = tx_get_buffer_length() - hdpath_length;

const zxerr_t err = crypto_sign(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3, message, messageLength);
const zxerr_t err = crypto_sign(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 3, message, messageLength, tx_type);

if (err != zxerr_ok) {
set_code(G_io_apdu_buffer, 0, APDU_CODE_SIGN_VERIFY_ERROR);
Expand Down Expand Up @@ -205,6 +206,7 @@ void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *t
uint32_t buffer_length = legacy_check_request(tx);

const char *error_msg = tx_parse(buffer_length, tx_type_json);
tx_type = tx_type_transaction;
CHECK_APP_CANARY()
if (error_msg != NULL) {
tx_reset();
Expand All @@ -217,6 +219,8 @@ void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *t
*flags |= IO_ASYNCH_REPLY;
}

// bytes: | 1 | 1 | 1 | 1 | 32 | 1 | 4*hdpath_qty |
// data: | CLA | INS | P1 | P2 | hash | hdpath_qty | hdpath_data |
void legacy_handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignLegacyHash\n");
if (!legacy_process_chunk(tx, rx, false, 32)) {
Expand All @@ -226,6 +230,7 @@ void legacy_handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx, uint
uint32_t buffer_length = legacy_check_request(tx);

const char *error_msg = tx_parse(buffer_length, tx_type_hash);
tx_type = tx_type_hash;
CHECK_APP_CANARY()
if (error_msg != NULL) {
const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
Expand All @@ -239,3 +244,25 @@ void legacy_handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx, uint
*flags |= IO_ASYNCH_REPLY;
}

void legacy_handleSignTransferTx(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignLegacyTransferTx\n");
if (!legacy_process_chunk(tx, rx, false, 32)) {
THROW(APDU_CODE_OK);
}

uint32_t buffer_length = legacy_check_request(tx);

const char *error_msg = tx_parse(buffer_length, tx_type_transaction);
tx_type = tx_type_transaction;
CHECK_APP_CANARY()
if (error_msg != NULL) {
const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
memcpy(G_io_apdu_buffer, error_msg, error_msg_length);
*tx += (error_msg_length);
THROW(APDU_CODE_DATA_INVALID);
}

view_review_init(tx_getItem, tx_getNumItems, legacy_app_sign);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
}
1 change: 1 addition & 0 deletions app/src/apdu_handler_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern "C" {
void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx, const uint8_t requireConfirmation);
void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx);
void legacy_handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx);
void legacy_handleSignTransferTx(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx);

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions tests_zemu/tests/legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe.each(JSON_TEST_CASES)('Tx transactions', function (data) {
})

describe.each(HASH_TEST_CASES)('Hash transactions', function (data) {
test.only.each(models)('sign', async function (m) {
test.concurrent.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
Expand Down Expand Up @@ -183,7 +183,7 @@ describe.each(HASH_TEST_CASES)('Hash transactions', function (data) {
})

describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
test.concurrent.each(models)('sign', async function (m) {
test.only.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
Expand All @@ -194,7 +194,7 @@ describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
console.log(pubKey)

// do not wait here... we need to navigate
// const signatureRequest = app.signTransferTx(data.path, data)
const signatureRequest = app.signTransferTx(data.path, data)

// // Wait until we are not in the main menu
// await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
Expand Down

1 comment on commit 9f4defd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 11 file(s) not formatted
  • app/src/crypto.c
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/apdu_handler_legacy.c
  • app/src/parser.c
  • app/src/items_format.h
  • app/src/crypto.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/parser_impl.h
  • app/src/common/tx.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.