Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version v0.3.4: Long TXs support, NVM storage, Settings submenu, fixes. #5

Closed
wants to merge 10 commits into from
8 changes: 4 additions & 4 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
artifact: aptos-app-nanosp
runs-on: ubuntu-latest
container:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:bd1db4121a88260b9b6866d3c05b7d20f928743b

steps:
- name: Clone
Expand All @@ -46,7 +46,7 @@ jobs:
name: Clang Static Analyzer
runs-on: ubuntu-latest
container:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:bd1db4121a88260b9b6866d3c05b7d20f928743b

steps:
- name: Clone
Expand All @@ -68,7 +68,7 @@ jobs:
name: Unit test
runs-on: ubuntu-latest
container:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:bd1db4121a88260b9b6866d3c05b7d20f928743b

steps:
- name: Install curl
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
image: ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:bd1db4121a88260b9b6866d3c05b7d20f928743b

steps:
- name: Clone
Expand Down
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ****************************************************************************
# Ledger App Boilerplate
# Ledger App Aptos
# (c) 2020 Ledger SAS.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -32,8 +32,8 @@ APP_LOAD_PARAMS += $(COMMON_LOAD_PARAMS)

APPNAME = "Aptos"
APPVERSION_M = 0
APPVERSION_N = 1
APPVERSION_P = 0
APPVERSION_N = 3
APPVERSION_P = 4
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

ifeq ($(TARGET_NAME),TARGET_NANOS)
Expand All @@ -56,6 +56,16 @@ DEFINES += BLE_SEGMENT_SIZE=32
DEFINES += HAVE_WEBUSB WEBUSB_URL_SIZE_B=0 WEBUSB_URL=""
DEFINES += UNUSED\(x\)=\(void\)x

ifeq ($(TARGET_NAME),TARGET_NANOS)
DEFINES += MAX_TRANSACTION_PACKETS=6
endif
ifeq ($(TARGET_NAME),TARGET_NANOS2)
DEFINES += MAX_TRANSACTION_PACKETS=106
endif
ifeq ($(TARGET_NAME),TARGET_NANOX)
DEFINES += MAX_TRANSACTION_PACKETS=104
endif

ifeq ($(TARGET_NAME),TARGET_NANOX)
DEFINES += HAVE_BLE BLE_COMMAND_TIMEOUT_MS=2000 HAVE_BLE_APDU
endif
Expand Down
4 changes: 3 additions & 1 deletion fuzzing/fuzz_tx_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
transaction_init(&tx);
status = transaction_deserialize(&buf, &tx);

if (status == PARSING_OK && tx.tx_variant == TX_RAW) {
if (status == PARSING_OK && tx.tx_variant == TX_RAW &&
tx.payload_variant == PAYLOAD_ENTRY_FUNCTION) {
printf("\nTransaction size: %lu\n", size);
printf("chain_id: %d\n", tx.chain_id);
printf("sequence: %lu\n", tx.sequence);
Expand All @@ -31,6 +32,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
format_hex(tx.sender, ADDRESS_LEN, sender, sizeof(sender));
printf("sender: %s\n", sender);
printf("payload_variant: %d\n", tx.payload_variant);
printf("entry_function.known_type: %d\n", tx.payload.entry_function.known_type);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/address.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/apdu/dispatcher.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/apdu/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Parameter 1 for maximum APDU number.
*/
#define P1_MAX 0x03
#define P1_MAX (MAX_TRANSACTION_PACKETS + 1)

/**
* Dispatch APDU command received to the right handler.
Expand Down
2 changes: 1 addition & 1 deletion src/apdu/parser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
9 changes: 8 additions & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
*/
#define MAX_APPNAME_LEN 64

/**
* Maximum transaction packets.
*/
#ifndef MAX_TRANSACTION_PACKETS
#define MAX_TRANSACTION_PACKETS 2
#endif

/**
* Maximum transaction length (bytes).
*/
#define MAX_TRANSACTION_LEN 510
#define MAX_TRANSACTION_LEN (MAX_TRANSACTION_PACKETS * 255)

/**
* Signature length (bytes).
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
6 changes: 6 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ extern bolos_ux_params_t G_ux_params;
* Global context for user requests.
*/
extern global_ctx_t G_context;

/**
* Global NVM app storage.
*/
extern const app_storage_t N_app_storage;
#define N_storage (*(volatile app_storage_t*) PIC(&N_app_storage))
2 changes: 1 addition & 1 deletion src/handler/get_app_name.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/handler/get_public_key.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/handler/get_version.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/handler/sign_tx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/helper/send_reponse.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion src/io.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
13 changes: 12 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -33,6 +33,7 @@ uint8_t G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B];
ux_state_t G_ux;
bolos_ux_params_t G_ux_params;
global_ctx_t G_context;
const app_storage_t N_app_storage;

/**
* Handle APDU command received and send back APDU response using handlers.
Expand Down Expand Up @@ -110,6 +111,15 @@ void app_exit() {
END_TRY_L(exit);
}

void nvm_app_storage_init() {
if (N_storage.initialized != 0x01) {
app_storage_t storage;
storage.settings.show_full_message = 0x00;
storage.initialized = 0x01;
nvm_write((void *) &N_storage, (void *) &storage, sizeof(app_storage_t));
}
}

/**
* Main loop to setup USB, Bluetooth, UI and launch app_main().
*/
Expand All @@ -125,6 +135,7 @@ __attribute__((section(".boot"))) int main() {
BEGIN_TRY {
TRY {
io_seproxyhal_init();
nvm_app_storage_init();

#ifdef TARGET_NANOX
G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0);
Expand Down
35 changes: 17 additions & 18 deletions src/transaction/deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ parser_status_e tx_raw_deserialize(buffer_t *buf, transaction_t *tx) {
}

parser_status_e tx_variant_deserialize(buffer_t *buf, transaction_t *tx) {
parser_status_e status = TX_VARIANT_UNDEFINED_ERROR;
if (buf->offset != 0) {
return TX_VARIANT_READ_ERROR;
}
Expand All @@ -103,18 +104,18 @@ parser_status_e tx_variant_deserialize(buffer_t *buf, transaction_t *tx) {

uint8_t *prefix;
// read hashed prefix bytes
if (!bcs_read_ptr_to_fixed_bytes(buf, &prefix, TX_HASHED_PREFIX_LEN)) {
return HASHED_PREFIX_READ_ERROR;
}

if (memcmp(prefix, PREFIX_RAW_TX_WITH_DATA_HASHED, TX_HASHED_PREFIX_LEN) == 0) {
tx->tx_variant = TX_RAW_WITH_DATA;
return PARSING_OK;
}
if (bcs_read_ptr_to_fixed_bytes(buf, &prefix, TX_HASHED_PREFIX_LEN)) {
if (memcmp(prefix, PREFIX_RAW_TX_WITH_DATA_HASHED, TX_HASHED_PREFIX_LEN) == 0) {
tx->tx_variant = TX_RAW_WITH_DATA;
return PARSING_OK;
}

if (memcmp(prefix, PREFIX_RAW_TX_HASHED, TX_HASHED_PREFIX_LEN) == 0) {
tx->tx_variant = TX_RAW;
return PARSING_OK;
if (memcmp(prefix, PREFIX_RAW_TX_HASHED, TX_HASHED_PREFIX_LEN) == 0) {
tx->tx_variant = TX_RAW;
return PARSING_OK;
}
} else {
status = HASHED_PREFIX_READ_ERROR;
}

if (transaction_utils_check_encoding(buf->ptr, buf->size)) {
Expand All @@ -123,7 +124,7 @@ parser_status_e tx_variant_deserialize(buffer_t *buf, transaction_t *tx) {
return PARSING_OK;
}

return TX_VARIANT_UNDEFINED_ERROR;
return status;
}

parser_status_e entry_function_payload_deserialize(buffer_t *buf, transaction_t *tx) {
Expand Down Expand Up @@ -323,17 +324,15 @@ entry_function_known_type_t determine_function_type(transaction_t *tx) {
return FUNC_UNKNOWN;
}

// TODO: Add string length check before comparison
if (tx->payload.entry_function.module_id.address[ADDRESS_LEN - 1] == 0x01 &&
memcmp(tx->payload.entry_function.module_id.name.bytes, "aptos_account", 13) == 0 &&
memcmp(tx->payload.entry_function.function_name.bytes, "transfer", 8) == 0) {
bcs_cmp_bytes(&tx->payload.entry_function.module_id.name, "aptos_account", 13) &&
bcs_cmp_bytes(&tx->payload.entry_function.function_name, "transfer", 8)) {
return FUNC_APTOS_ACCOUNT_TRANSFER;
}

// TODO: Add string length check before comparison
if (tx->payload.entry_function.module_id.address[ADDRESS_LEN - 1] == 0x01 &&
memcmp(tx->payload.entry_function.module_id.name.bytes, "coin", 4) == 0 &&
memcmp(tx->payload.entry_function.function_name.bytes, "transfer", 8) == 0) {
bcs_cmp_bytes(&tx->payload.entry_function.module_id.name, "coin", 4) &&
bcs_cmp_bytes(&tx->payload.entry_function.function_name, "transfer", 8)) {
return FUNC_COIN_TRANSFER;
}

Expand Down
8 changes: 6 additions & 2 deletions src/transaction/utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,7 +17,7 @@

#include <stdint.h> // uint*_t
#include <stdbool.h> // bool
#include <string.h> // memmove
#include <string.h> // memcmp

#include "types.h"

Expand All @@ -30,3 +30,7 @@ bool transaction_utils_check_encoding(const uint8_t *msg, uint64_t msg_len) {

return true;
}

bool bcs_cmp_bytes(const fixed_bytes_t *bcs_bytes, const void *value, size_t len) {
return bcs_bytes->len == len && memcmp(bcs_bytes->bytes, value, len) == 0;
}
15 changes: 15 additions & 0 deletions src/transaction/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@
*
*/
bool transaction_utils_check_encoding(const uint8_t *msg, uint64_t msg_len);

/**
* Compares the fixed_bytes_t bcs_bytes to the memory pointed by value.
*
* @param[in] bcs_bytes
* Pointer to fixed_bytes_t struct.
* @param[in] value
* Pointer to input block of memory.
* @param[in] len
* Length of input bytes to compare.
*
* @return true if success, false otherwise.
*
*/
bool bcs_cmp_bytes(const fixed_bytes_t *bcs_bytes, const void *value, size_t len);
9 changes: 9 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ typedef struct {
uint32_t bip32_path[MAX_BIP32_PATH]; /// BIP32 path
uint8_t bip32_path_len; /// length of BIP32 path
} global_ctx_t;

typedef struct {
uint8_t show_full_message;
} app_settings;

typedef struct {
app_settings settings;
uint8_t initialized;
} app_storage_t;
2 changes: 1 addition & 1 deletion src/ui/action/validate.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Ledger App Boilerplate.
* Ledger App Aptos.
* (c) 2020 Ledger SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Loading