Skip to content

Commit ccd39ae

Browse files
authored
Merge branch 'Zondax:main' into nam-fork
2 parents 0209e63 + 10f6248 commit ccd39ae

File tree

123 files changed

+559234
-51670
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+559234
-51670
lines changed

app/Makefile

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@ endif
2222

2323
MY_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
2424

25-
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.installer_script
26-
27-
include $(BOLOS_SDK)/Makefile.defines
28-
2925
# Set the default value for PRODUCTION_BUILD to 1 if not already defined
3026
PRODUCTION_BUILD ?= 1
3127

32-
$(info ************ TARGET_NAME = [$(TARGET_NAME)])
3328
# Display whether this is a production build or for internal use
3429
ifeq ($(PRODUCTION_BUILD), 1)
3530
$(info ************ PRODUCTION_BUILD = [PRODUCTION BUILD])
@@ -39,19 +34,22 @@ endif
3934

4035
# Add the PRODUCTION_BUILD definition to the compiler flags
4136
DEFINES += PRODUCTION_BUILD=$(PRODUCTION_BUILD)
37+
4238
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.app_testing
4339

4440
ifndef COIN
4541
COIN=NAM
4642
endif
4743

44+
VARIANT_PARAM=COIN
45+
VARIANT_VALUES=$(COIN)
46+
4847
include $(CURDIR)/Makefile.version
4948

5049
$(info COIN = [$(COIN)])
5150

5251
ifeq ($(COIN),NAM)
5352
# Main app configuration
54-
DEFINES += APP_STANDARD
5553
APPNAME = "Namada"
5654
APPPATH = "44'/877'" --path "44'/1'"
5755

@@ -66,33 +64,44 @@ endif
6664

6765
APP_LOAD_PARAMS = --curve ed25519 $(COMMON_LOAD_PARAMS) --path $(APPPATH)
6866

69-
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices
67+
# Enable QR code display for all devices
68+
ENABLE_NBGL_QRCODE ?= 1
7069

71-
$(info TARGET_NAME = [$(TARGET_NAME)])
72-
$(info ICONNAME = [$(ICONNAME)])
70+
RUST_TARGET:=thumbv6m-none-eabi
7371

74-
ifndef ICONNAME
75-
$(error ICONNAME is not set)
76-
endif
72+
$(info ************ RUST_TARGET = [$(RUST_TARGET)])
73+
74+
include $(BOLOS_SDK)/Makefile.target
75+
76+
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices
77+
78+
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
7779

7880
# Compile MASP mode for all devices excetpt Nano S,
7981
ifneq ($(TARGET_NAME),TARGET_NANOS)
8082
DEFINES += COMPILE_MASP
8183
endif
8284

83-
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
8485
# Add SDK BLAKE2b
8586
DEFINES += HAVE_HASH HAVE_BLAKE2
8687
INCLUDES_PATH += $(BOLOS_SDK)/lib_cxng/src
8788

88-
8989
# Building Rust
9090
LDFLAGS += -z muldefs
9191
LDLIBS += -L$(MY_DIR)rust/target/$(RUST_TARGET)/release -lrslib
92-
9392
APP_SOURCE_PATH += $(CURDIR)/rust/include
94-
APP_CUSTOM_LINK_DEPENDENCIES = rust
95-
RUST_TARGET:=thumbv6m-none-eabi
93+
94+
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.installer_script
95+
96+
$(info TARGET_NAME = [$(TARGET_NAME)])
97+
$(info ICONNAME = [$(ICONNAME)])
98+
99+
ifndef ICONNAME
100+
$(error ICONNAME is not set)
101+
endif
102+
103+
# make rust a prerequisite for all object files
104+
$(OBJECT_FILES): | rust
96105

97106
.PHONY: rust
98107
rust:
@@ -104,17 +113,9 @@ rust_clean:
104113

105114
clean: rust_clean
106115

107-
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.side_loading
108-
109-
# Import generic rules from the SDK
110-
include $(BOLOS_SDK)/Makefile.rules
111-
112116
#add dependency on custom makefile filename
113117
dep/%.d: %.c Makefile
114118

115-
listvariants:
116-
@echo VARIANTS COIN NAM
117-
118119
.PHONY: version
119120
version:
120121
@echo "v$(APPVERSION)" > app.version

app/Makefile.version

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This is the `transaction_version` field of `Runtime`
2-
APPVERSION_M=2
2+
APPVERSION_M=3
33
# This is the `spec_version` field of `Runtime`
44
APPVERSION_N=0
55
# This is the patch version of this release
6-
APPVERSION_P=5
6+
APPVERSION_P=4

app/src/apdu_handler.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile
239239
G_io_apdu_buffer[0] = 0x01;
240240
#endif
241241

242-
G_io_apdu_buffer[1] = (LEDGER_MAJOR_VERSION >> 8) & 0xFF;
243-
G_io_apdu_buffer[2] = (LEDGER_MAJOR_VERSION >> 0) & 0xFF;
242+
G_io_apdu_buffer[1] = (MAJOR_VERSION >> 8) & 0xFF;
243+
G_io_apdu_buffer[2] = (MAJOR_VERSION >> 0) & 0xFF;
244244

245-
G_io_apdu_buffer[3] = (LEDGER_MINOR_VERSION >> 8) & 0xFF;
246-
G_io_apdu_buffer[4] = (LEDGER_MINOR_VERSION >> 0) & 0xFF;
245+
G_io_apdu_buffer[3] = (MINOR_VERSION >> 8) & 0xFF;
246+
G_io_apdu_buffer[4] = (MINOR_VERSION >> 0) & 0xFF;
247247

248-
G_io_apdu_buffer[5] = (LEDGER_PATCH_VERSION >> 8) & 0xFF;
249-
G_io_apdu_buffer[6] = (LEDGER_PATCH_VERSION >> 0) & 0xFF;
248+
G_io_apdu_buffer[5] = (PATCH_VERSION >> 8) & 0xFF;
249+
G_io_apdu_buffer[6] = (PATCH_VERSION >> 0) & 0xFF;
250250

251251
// SDK won't reply if device is blocked ---> Always false
252252
G_io_apdu_buffer[7] = 0;

app/src/coin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
#define HDPATH_4_DEFAULT (0u)
3535

3636
#define MASK_HARDENED 0x80000000
37+
#define MASK_HARDENED_ZIP32 (0x80000000u | 0x7FFFFFFF) // 2147483647
3738

3839
#define SECP256K1_PK_LEN 65u
3940
#define COMPRESSED_SECP256K1_PK_LEN 33u

app/src/common/tx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#include "tx.h"
1818
#include "apdu_codes.h"
1919
#include "buffering.h"
20-
#include "parser.h"
20+
#include "common/parser.h"
2121
#include <string.h>
2222
#include "zxmacros.h"
2323

2424
#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) || defined(TARGET_STAX) || defined(TARGET_FLEX)
2525
#define RAM_BUFFER_SIZE 8192
2626
#define FLASH_BUFFER_SIZE 16384
2727
#elif defined(TARGET_NANOS)
28-
#define RAM_BUFFER_SIZE 256
28+
#define RAM_BUFFER_SIZE 0
2929
#define FLASH_BUFFER_SIZE 8192
3030
#endif
3131

app/src/crypto.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed) {
566566

567567
// Generate randomness using a fixed path related to the device mnemonic
568568
const uint32_t path[HDPATH_LEN_DEFAULT] = {
569-
HDPATH_0_DEFAULT, HDPATH_1_DEFAULT, MASK_HARDENED, MASK_HARDENED, MASK_HARDENED,
569+
HDPATH_0_DEFAULT, HDPATH_1_DEFAULT, MASK_HARDENED, MASK_HARDENED, MASK_HARDENED_ZIP32,
570570
};
571571

572572
MEMZERO(device_seed, KEY_LENGTH);
@@ -576,6 +576,7 @@ zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed) {
576576
io_seproxyhal_io_heartbeat();
577577
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_ED25519_SLIP10, CX_CURVE_Ed25519, path, HDPATH_LEN_DEFAULT, raw_privkey, NULL,
578578
NULL, 0));
579+
io_seproxyhal_io_heartbeat();
579580
error = zxerr_ok;
580581
MEMCPY(device_seed, raw_privkey, KEY_LENGTH);
581582

@@ -696,7 +697,9 @@ zxerr_t crypto_sign_spends_sapling(const parser_tx_t *txObj, keys_t *keys) {
696697
spend += spendLen;
697698
spend_item_t *item = spendlist_retrieve_rand_item(i);
698699

700+
io_seproxyhal_io_heartbeat();
699701
CHECK_ZXERR(sign_sapling_spend(keys, item->alpha, sign_hash, signature));
702+
io_seproxyhal_io_heartbeat();
700703

701704
// Save signature in flash
702705
CHECK_ZXERR(spend_signatures_append(signature));
@@ -766,11 +769,6 @@ parser_error_t checkSpends(const parser_tx_t *txObj, keys_t *keys, parser_contex
766769
CHECK_ERROR(computeRk(keys, item->alpha, rk));
767770

768771
CTX_CHECK_AND_ADVANCE(tx_spends_ctx, CV_LEN + NULLIFIER_LEN);
769-
#ifndef APP_TESTING
770-
if (MEMCMP(rk, tx_spends_ctx->buffer + tx_spends_ctx->offset, RK_LEN) != 0) {
771-
return parser_invalid_rk;
772-
}
773-
#endif
774772

775773
builder_spends_ctx->offset = 0;
776774
tx_spends_ctx->offset = 0;
@@ -885,6 +883,7 @@ zxerr_t crypto_check_masp(const parser_tx_t *txObj, keys_t *keys) {
885883
.bufferLen = txObj->transaction.sections.maspBuilder.metadata.spends_indices.len,
886884
.offset = 0,
887885
.tx_obj = NULL};
886+
io_seproxyhal_io_heartbeat();
888887
CHECK_PARSER_OK(checkSpends(txObj, keys, &builder_spends_ctx, &tx_spends_ctx, &spends_indices_ctx));
889888

890889
// Check outputs
@@ -900,6 +899,7 @@ zxerr_t crypto_check_masp(const parser_tx_t *txObj, keys_t *keys) {
900899
.bufferLen = txObj->transaction.sections.maspBuilder.metadata.outputs_indices.len,
901900
.offset = 0,
902901
.tx_obj = NULL};
902+
io_seproxyhal_io_heartbeat();
903903
CHECK_PARSER_OK(checkOutputs(txObj, &builder_outputs_ctx, &tx_outputs_ctx, &output_indices_ctx));
904904

905905
// Check converts
@@ -915,6 +915,7 @@ zxerr_t crypto_check_masp(const parser_tx_t *txObj, keys_t *keys) {
915915
.bufferLen = txObj->transaction.sections.maspBuilder.metadata.converts_indices.len,
916916
.offset = 0,
917917
.tx_obj = NULL};
918+
io_seproxyhal_io_heartbeat();
918919
CHECK_PARSER_OK(checkConverts(txObj, &builder_converts_ctx, &tx_converts_ctx, &converts_indices_ctx));
919920
return zxerr_ok;
920921
}
@@ -933,9 +934,14 @@ zxerr_t crypto_sign_masp_spends(parser_tx_t *txObj, uint8_t *output, uint16_t ou
933934
return zxerr_unknown;
934935
}
935936

936-
if (get_state() != STATE_PROCESSED_RANDOMNESS) {
937-
return zxerr_unknown;
938-
}
937+
// If a MASP signing has happened before, then device must be in either of two states
938+
bool signed_before = get_state() == STATE_SIGNED_SPENDS || get_state() == STATE_EXTRACT_SPENDS;
939+
// A state where signing has happened before and all signatures thereof have been extracted
940+
bool completed_signing = signed_before && !spend_signatures_more_extract();
941+
// We must either be signing for the first time, or fully completed the previous signing
942+
if (!(get_state() == STATE_PROCESSED_RANDOMNESS || completed_signing)) {
943+
return zxerr_unknown;
944+
}
939945

940946
// Get keys
941947
keys_t keys = {0};

app/src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "parser_common.h"
2323
#include "parser_impl.h"
24-
#include "parser.h"
24+
#include "common/parser.h"
2525

2626
#include "crypto.h"
2727
#include "crypto_helper.h"

app/src/parser_impl.c

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "zxformat.h"
1818
#include "leb128.h"
1919
#include "app_mode.h"
20-
20+
#include "crypto_helper.h"
2121
#include "parser_impl_common.h"
2222

2323
parser_error_t _read(parser_context_t *ctx, parser_tx_t *v) {
@@ -52,6 +52,80 @@ bool hasMemoToPrint(const parser_context_t *ctx) {
5252
return false; // No memo to print
5353
}
5454

55+
__attribute__((noinline)) parser_error_t getSpendfromIndex(uint32_t index, bytes_t *spend) {
56+
57+
for (uint32_t i = 0; i < index; i++) {
58+
spend->ptr += EXTENDED_FVK_LEN + DIVERSIFIER_LEN + NOTE_LEN;
59+
uint8_t tmp_len = spend->ptr[0];
60+
spend->ptr++;
61+
spend->ptr += (tmp_len * (32 + 1)) + sizeof(uint64_t);
62+
}
63+
64+
return parser_ok;
65+
}
66+
67+
__attribute__((noinline)) parser_error_t getOutputfromIndex(uint32_t index, bytes_t *out) {
68+
69+
for (uint32_t i = 0; i < index; i++) {
70+
uint8_t has_ovk = out->ptr[0];
71+
if(has_ovk) {
72+
out->ptr += OVK_PLUS_CHECK_BYTE;
73+
} else {
74+
out->ptr++;
75+
}
76+
out->ptr += PAYMENT_ADDR_LEN + OUT_NOTE_LEN + MEMO_LEN;
77+
}
78+
79+
return parser_ok;
80+
}
81+
82+
__attribute__((noinline)) parser_error_t findAssetData(const masp_builder_section_t *maspBuilder, const uint8_t *stoken, masp_asset_data_t *asset_data, uint32_t *index) {
83+
parser_context_t asset_data_ctx = {.buffer = maspBuilder->asset_data.ptr, .bufferLen = maspBuilder->asset_data.len, .offset = 0, .tx_obj = NULL};
84+
for (*index = 0; *index < maspBuilder->n_asset_type; (*index)++) {
85+
CHECK_ERROR(readAssetData(&asset_data_ctx, asset_data))
86+
uint8_t identifier[32];
87+
uint8_t nonce;
88+
CHECK_ERROR(readToken(&asset_data->token, &asset_data->symbol));
89+
CHECK_ERROR(derive_asset_type(asset_data, identifier, &nonce))
90+
if(MEMCMP(identifier, stoken, ASSET_ID_LEN) == 0) {
91+
return parser_ok;
92+
}
93+
}
94+
return parser_ok;
95+
}
96+
97+
parser_error_t checkMaspSpendsSymbols (const parser_context_t *ctx) {
98+
bytes_t spend = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.spends;
99+
masp_asset_data_t asset_data = {0};
100+
uint32_t asset_idx = 0;
101+
102+
for (uint32_t i = 0; i < ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends; i++) {
103+
getSpendfromIndex(i, &spend);
104+
const uint8_t *spend_token = spend.ptr + EXTENDED_FVK_LEN + DIVERSIFIER_LEN;
105+
CHECK_ERROR(findAssetData(&ctx->tx_obj->transaction.sections.maspBuilder, spend_token, &asset_data, &asset_idx))
106+
if(asset_data.symbol == NULL) {
107+
ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_spends++;
108+
}
109+
}
110+
return parser_ok;
111+
}
112+
113+
parser_error_t checkMaspOutputsSymbols (const parser_context_t *ctx) {
114+
bytes_t output = ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.outputs;
115+
masp_asset_data_t asset_data = {0};
116+
uint32_t asset_idx = 0;
117+
118+
for (uint32_t i = 0; i < ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; i++) {
119+
getOutputfromIndex(i, &output);
120+
const uint8_t *output_token = output.ptr + (output.ptr[0] ? OVK_PLUS_CHECK_BYTE : 1) + PAYMENT_ADDR_LEN;
121+
CHECK_ERROR(findAssetData(&ctx->tx_obj->transaction.sections.maspBuilder, output_token, &asset_data, &asset_idx))
122+
if(asset_data.symbol == NULL) {
123+
ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_outputs++;
124+
}
125+
}
126+
return parser_ok;
127+
}
128+
55129
parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
56130
*numItems = 0;
57131
switch (ctx->tx_obj->typeTx) {
@@ -67,10 +141,10 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
67141
case Transfer:
68142
if(ctx->tx_obj->transaction.isMasp) {
69143
uint8_t items = 1;
70-
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; // print from outputs
71-
items += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends; // print from spends
144+
items += 2 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs + ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_outputs; // print from outputs
145+
items += 2 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends + ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_spends; // print from spends
72146

73-
*numItems = (app_mode_expert() ? items + 4 : items + 2);
147+
*numItems = (app_mode_expert() ? items + TRANSFER_EXPERT_MASP_PARAMS : items + TRANSFER_NORMAL_MASP_PARAMS);
74148
} else {
75149
*numItems = (app_mode_expert() ? TRANSFER_EXPERT_PARAMS : TRANSFER_NORMAL_PARAMS);
76150
}
@@ -145,8 +219,8 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
145219
case IBC:
146220
*numItems = (app_mode_expert() ? IBC_EXPERT_PARAMS : IBC_NORMAL_PARAMS);
147221
if(ctx->tx_obj->transaction.isMasp) {
148-
*numItems += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs; // print from outputs
149-
*numItems += 3 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends; // print from spends
222+
*numItems += 2 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_outputs + ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_outputs; // print from outputs
223+
*numItems += 2 * ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.n_spends + ctx->tx_obj->transaction.sections.maspBuilder.builder.sapling_builder.no_symbol_spends; // print from spends
150224
}
151225
*numItems += ctx->tx_obj->ibc.transfer.non_masp_sources_len*2 + ctx->tx_obj->ibc.transfer.non_masp_targets_len*2 + ctx->tx_obj->ibc.transfer.no_symbol_sources + ctx->tx_obj->ibc.transfer.no_symbol_targets;
152226
*numItems += ctx->tx_obj->ibc.memo.len > 0 && app_mode_expert();
@@ -215,7 +289,7 @@ parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems) {
215289
(*numItems)++;
216290
}
217291

218-
if(app_mode_expert() && ctx->tx_obj->transaction.header.fees.symbol == NULL) {
292+
if(ctx->tx_obj->transaction.header.fees.symbol == NULL) {
219293
(*numItems)++;
220294
}
221295

app/src/parser_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ extern "C" {
2727
parser_error_t _read(parser_context_t *c, parser_tx_t *v);
2828
parser_error_t getNumItems(const parser_context_t *ctx, uint8_t *numItems);
2929
bool hasMemoToPrint(const parser_context_t *ctx);
30+
parser_error_t checkMaspSpendsSymbols (const parser_context_t *ctx);
31+
parser_error_t checkMaspOutputsSymbols (const parser_context_t *ctx);
32+
parser_error_t findAssetData(const masp_builder_section_t *maspBuilder, const uint8_t *stoken, masp_asset_data_t *asset_data, uint32_t *index);
33+
parser_error_t getSpendfromIndex(uint32_t index, bytes_t *spend);
34+
parser_error_t getOutputfromIndex(uint32_t index, bytes_t *out);
35+
3036
#ifdef __cplusplus
3137
}
3238
#endif

0 commit comments

Comments
 (0)