Skip to content

Commit

Permalink
wip - process chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Aug 28, 2024
1 parent dde7dc1 commit 105adb8
Show file tree
Hide file tree
Showing 25 changed files with 263 additions and 48 deletions.
7 changes: 7 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ __Z_INLINE void handleSignTransaction(volatile uint32_t *flags, volatile uint32_
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
}

__Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile uint32_t *tx) {
G_io_apdu_buffer[0] = 0;

Expand Down Expand Up @@ -263,6 +264,12 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

case BCOMP_SIGN_JSON_TX: {
CHECK_PIN_VALIDATED()
legacy_handleSignTransaction(flags, tx, rx);
break;
}

#if defined(APP_TESTING)
case INS_TEST: {
handleTest(flags, tx, rx);
Expand Down
160 changes: 151 additions & 9 deletions app/src/apdu_handler_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
#include "addr.h"
#include "view_internal.h"

__Z_INLINE void app_reply_address_legacy() {
#include "zxformat.h"

static bool tx_initialized = false;
static uint32_t payload_length = 0;

void legacy_app_reply_address() {
// Add the pubkey length to the beginning of the buffer
MEMMOVE(G_io_apdu_buffer + 1, G_io_apdu_buffer, action_addrResponseLen);
G_io_apdu_buffer[0] = action_addrResponseLen;
Expand All @@ -31,37 +36,47 @@ __Z_INLINE void app_reply_address_legacy() {
}


void legacy_extractHDPath(uint32_t rx, uint32_t offset) {
if (rx < LEGACY_OFFSET_HDPATH_LEN) {
uint8_t legacy_extractHDPath(uint8_t *buffer, uint32_t rx, uint32_t offset) {
if (rx < offset) {
THROW(APDU_CODE_WRONG_LENGTH);
}

uint8_t hdPathQty = G_io_apdu_buffer[LEGACY_OFFSET_HDPATH_LEN];
uint8_t hdPathQty = buffer[offset];
uint8_t hdPathLen = hdPathQty * sizeof(uint32_t);
uint32_t offset_hdpath_data = offset + 1;

ZEMU_LOGF(50, "hdPathQty: %d\n", hdPathQty);
ZEMU_LOGF(50, "hdPathLen: %d\n", hdPathLen);
ZEMU_LOGF(50, "offset_hdpath_data: %d\n", offset_hdpath_data);

if ( rx - offset != hdPathLen) {
if ( rx - offset_hdpath_data != hdPathLen) {
THROW(APDU_CODE_WRONG_LENGTH);
}

memcpy(hdPath, G_io_apdu_buffer + offset, hdPathLen);
MEMCPY(hdPath, buffer + offset_hdpath_data, hdPathLen);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT && hdPath[1] == HDPATH_1_DEFAULT;

ZEMU_LOGF(50, "mainnet: %d\n", mainnet);

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}

// return hdPathLen + 1 byte for the hdPathQty
return hdPathLen + 1;
}

void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx, const uint8_t requireConfirmation) {
legacy_extractHDPath(rx, LEGACY_OFFSET_HDPATH_DATA);
legacy_extractHDPath(G_io_apdu_buffer, rx, LEGACY_OFFSET_HDPATH_LEN);

zxerr_t zxerr = app_fill_address();
if (zxerr != zxerr_ok) {
*tx = 0;
THROW(APDU_CODE_DATA_INVALID);
}
if (requireConfirmation) {
view_review_init(addr_getItem, addr_getNumItems, app_reply_address_legacy);
view_review_init(addr_getItem, addr_getNumItems, legacy_app_reply_address);
view_review_show(REVIEW_ADDRESS);
*flags |= IO_ASYNCH_REPLY;
return;
Expand All @@ -73,4 +88,131 @@ void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint3

*tx = action_addrResponseLen + 1;
THROW(APDU_CODE_OK);
}
}

bool legacy_process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx) {

ZEMU_LOGF(50, "rx: %d\n", rx);

uint8_t chunk_length = G_io_apdu_buffer[LEGACY_OFFSET_CHUNK_LENGTH];
ZEMU_LOGF(50, "chunk_length: %d\n", chunk_length);

// TODO: check case payload = 230
uint32_t added = 0;
if (!tx_initialized) {
// read length of data
uint32_t data_length = 0;
data_length |= (uint32_t)G_io_apdu_buffer[5];
data_length |= (uint32_t)G_io_apdu_buffer[6] << 8;
data_length |= (uint32_t)G_io_apdu_buffer[7] << 16;
data_length |= (uint32_t)G_io_apdu_buffer[8] << 24;

payload_length = data_length;

uint8_t bytes[4];
for (int i = 0; i < 4; i++) {
bytes[i] = (data_length >> (i * 8)) & 0xFF;
}
for (int i = 0; i < 4; i++) {
ZEMU_LOGF(50, "payload_length %d: 0x%02X\n", i, bytes[i])
}

uint32_t offset = LEGACY_OFFSET_CHUNK_LENGTH + LEGACY_HEADER_LENGTH;
uint32_t payload_size = rx - offset;
tx_initialize();
tx_reset();
added = tx_append(&(G_io_apdu_buffer[offset]), payload_size);
if (added != payload_size) {
tx_initialized = false;
THROW(APDU_CODE_OUTPUT_BUFFER_TOO_SMALL);
}

if (rx != LEGACY_CHUNCK_SIZE + LEGACY_HEADER_LENGTH) {
return true;
}
tx_initialized = true;
} else {
uint32_t offset = LEGACY_HEADER_LENGTH;
uint32_t payload_size = rx - offset;

added = tx_append(&(G_io_apdu_buffer[offset]), payload_size);
if (added != payload_size) {
tx_initialized = false;
THROW(APDU_CODE_OUTPUT_BUFFER_TOO_SMALL);
}

if (rx != LEGACY_CHUNCK_SIZE + LEGACY_HEADER_LENGTH) {
tx_initialized = false;
return true;
}
}

return false;
}

void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignTransactionLegacy\n");
if (!legacy_process_chunk(tx, rx)) {
THROW(APDU_CODE_OK);
}
zemu_log("handleSignTransactionLegacy finish\n");
// check buffer length
uint32_t tx_buffer_length = tx_get_buffer_length();
uint8_t *tx_buffer = tx_get_buffer();
int64_t hdpath_size = tx_buffer_length - payload_length;


uint8_t bytes[4];

for (int i = 0; i < 4; i++) {
bytes[i] = (payload_length >> (i * 8)) & 0xFF;
}
for (int i = 0; i < 4; i++) {
ZEMU_LOGF(50, "payload_length %d: 0x%02X\n", i, bytes[i])
}

for (int i = 0; i < 4; i++) {
bytes[i] = (tx_buffer_length >> (i * 8)) & 0xFF;
}
for (int i = 0; i < 4; i++) {
ZEMU_LOGF(50, "tx_buffer_length %d: 0x%02X\n", i, bytes[i])
}

for (int i = 0; i < 4; i++) {
bytes[i] = (hdpath_size >> (i * 8)) & 0xFF;
}
for (int i = 0; i < 4; i++) {
ZEMU_LOGF(50, "hdpath_size %d: 0x%02X\n", i, bytes[i])
}

if (hdpath_size < 0) {
THROW(APDU_CODE_DATA_INVALID);
}

// get hdpath
uint8_t hdpath_length = legacy_extractHDPath(tx_buffer, tx_buffer_length, payload_length);

// verify sizes
if (tx_buffer_length != hdpath_length + payload_length) {
THROW(APDU_CODE_DATA_INVALID);
}

char print[200] = {0};
ZEMU_LOGF(200, "tx_buffer: %s\n", tx_buffer);


THROW(APDU_CODE_OK);

// const char *error_msg = tx_parse(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, app_sign);
// view_review_show(REVIEW_TXN);
// *flags |= IO_ASYNCH_REPLY;
}
5 changes: 4 additions & 1 deletion app/src/apdu_handler_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ extern "C" {

#include <stdint.h>

#define LEGACY_CHUNCK_SIZE 230
#define LEGACY_HEADER_LENGTH 5
#define LEGACY_OFFSET_CHUNK_LENGTH 4
#define LEGACY_OFFSET_HDPATH_LEN 5
#define LEGACY_OFFSET_HDPATH_DATA 6

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);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion tests_zemu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"hw-app-kda": "git+https://github.com/obsidiansystems/hw-app-kda"
"hw-app-kda": "link:hw-app-kda"
}
}
Binary file added tests_zemu/snapshots/x-sign_hash_1/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00015.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/x-sign_hash_1/00016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 47 additions & 3 deletions tests_zemu/tests/backward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

import Zemu, { ButtonKind, zondaxMainmenuNavigation, isTouchDevice } from '@zondax/zemu'
import Kda from "hw-app-kda";
import { PATH, defaultOptions, models, simpleTxNormal } from './common'
import { PATH, defaultOptions, models } from './common'
import { blake2bFinal, blake2bInit, blake2bUpdate } from 'blakejs'

import { JSON_TEST_CASES } from './testscases/json'
import { HASH_TEST_CASES } from './testscases/hash'
import { TRANSACTIONS_TEST_CASES } from './testscases/transactions'

// @ts-expect-error
import ed25519 from 'ed25519-supercop'

jest.setTimeout(60000)
jest.setTimeout(10000)

const expected_pk = 'de12b5e16b93fe81ca4d70656bee4334f2e40f9f28b9796e792d28f2cead74ad'

Expand Down Expand Up @@ -77,7 +78,7 @@ test.concurrent.each(models)('legacy show address', async function (m) {
}
})

test.only.each(models)('show address - reject', async function (m) {
test.concurrent.each(models)('legacy show address - reject', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({
Expand All @@ -103,6 +104,39 @@ test.only.each(models)('show address - reject', async function (m) {
}
})

describe.each(JSON_TEST_CASES)('Tx transactions', function (data) {
test.only.each(models)('sign json', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new Kda(sim.getTransport());

const txBlob = Buffer.from(data.json, "utf-8")
const { pubKey } = await app.getPublicKey(data.path);

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

// // Wait until we are not in the main menu
// await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
// await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_${data.name}`)

// const signatureResponse = await signatureRequest
// console.log(signatureResponse)

// const context = blake2bInit(32)
// blake2bUpdate(context, txBlob)
// const hash = Buffer.from(blake2bFinal(context))

// // Now verify the signature
// const valid = ed25519.verify(signatureResponse.signature, hash, pubKey)
// expect(valid).toEqual(true)
} finally {
await sim.close()
}
})
})

describe.each(HASH_TEST_CASES)('Hash transactions', function (data) {
test.concurrent.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
Expand Down Expand Up @@ -173,3 +207,13 @@ describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
}
})
})
// de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d


// c7b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d052c00008072020080000000800000000000000000
// 00030000e6de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d052c00008072020080000000800000000000000000

// 052c00008072020080000000800000000000000000

// 00030000e6de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c2273
// 00030000e6623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063
3 changes: 0 additions & 3 deletions tests_zemu/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ export const defaultOptions = {
custom: `-s "${APP_SEED}"`,
X11: false,
}

export const simpleTxNormal =
'{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)"}},"signers":[{"pubKey":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","clist":[{"args":[],"name":"coin.GAS"},{"args":["83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42",11],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1634009214,"ttl":28800,"gasLimit":600,"chainId":"0","gasPrice":1.0e-5,"sender":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790"},"nonce":"\\"2021-10-12T03:27:53.700Z\\""}'
Loading

1 comment on commit 105adb8

@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: 6 file(s) not formatted
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/common/tx.h
  • app/src/parser_impl.c
  • app/src/apdu_handler.c
  • app/src/apdu_handler_legacy.c

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.