Skip to content

Commit

Permalink
upgrading zxlib (#45)
Browse files Browse the repository at this point in the history
* upgrading zxlib

* adding missing files

* remove webusb popups

* upgrade zxlib

* upgrade zxlib + UI

* bump version number

* bump version numbers

* add missing file

* update zemu tests
  • Loading branch information
jleni authored Jul 4, 2020
1 parent 69c7549 commit 07f9598
Show file tree
Hide file tree
Showing 55 changed files with 1,745 additions and 485 deletions.
88 changes: 48 additions & 40 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,33 @@ endif

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

ifeq ($(APP_TESTING),1)
DEFINES += APP_TESTING
DEFINES += ZEMU_LOGGING
endif

all: bin/app.elf
@echo "#!/usr/bin/env bash" > $(CURDIR)/pkg/zxtool.sh
@echo "APPNAME=\"${APPNAME}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPVERSION=\"${APPVERSION}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPPATH=\""${APPPATH}"\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "LOAD_PARAMS=\"${COMMON_LOAD_PARAMS}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "DELETE_PARAMS=\"${COMMON_DELETE_PARAMS}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPHEX=\"" >> $(CURDIR)/pkg/zxtool.sh
@cat $(CURDIR)/bin/app.hex >> $(CURDIR)/pkg/zxtool.sh
@echo "\"" >> $(CURDIR)/pkg/zxtool.sh
@cat $(CURDIR)/../deps/ledger-zxlib/scripts/template.sh >> $(CURDIR)/pkg/zxtool.sh
@chmod +x $(CURDIR)/pkg/zxtool.sh

include $(BOLOS_SDK)/Makefile.defines

ifndef COIN
COIN=FIL
endif

APPVERSION_M=0
APPVERSION_N=15
APPVERSION_P=2
APPVERSION_N=16
APPVERSION_P=0

$(info COIN = [$(COIN)])
ifeq ($(COIN),FIL)
Expand All @@ -51,7 +69,7 @@ endef
$(error "$(error_message)")
endif

APP_LOAD_PARAMS = --appFlags 0x200 --delete $(COMMON_LOAD_PARAMS) --path $(APPPATH) --path "44'/1'"
APP_LOAD_PARAMS = --appFlags 0x200 --delete $(COMMON_LOAD_PARAMS) --path ${APPPATH} --path "44'/1'"

ifeq ($(TARGET_NAME),TARGET_NANOS)
SCRIPT_LD:=$(CURDIR)/script.ld
Expand All @@ -66,42 +84,25 @@ ifndef ICONNAME
$(error ICONNAME is not set)
endif

all: default
@echo "#!/usr/bin/env bash" > $(CURDIR)/pkg/zxtool.sh
@echo "APPNAME=\"${APPNAME}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPVERSION=\"${APPVERSION}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPPATH=\""${APPPATH}"\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "LOAD_PARAMS=\"${COMMON_LOAD_PARAMS}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "DELETE_PARAMS=\"${COMMON_DELETE_PARAMS}\"" >> $(CURDIR)/pkg/zxtool.sh
@echo "APPHEX=\"" >> $(CURDIR)/pkg/zxtool.sh
@cat $(CURDIR)/bin/app.hex >> $(CURDIR)/pkg/zxtool.sh
@echo "\"" >> $(CURDIR)/pkg/zxtool.sh
@cat $(CURDIR)/../deps/ledger-zxlib/scripts/template.sh >> $(CURDIR)/pkg/zxtool.sh
@chmod +x $(CURDIR)/pkg/zxtool.sh

############
# Platform

DEFINES += UNUSED\(x\)=\(void\)x
DEFINES += PRINTF\(...\)=
DEFINES += UNUSED\(x\)=\(void\)x
DEFINES += PRINTF\(...\)=

APPVERSION=$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)
DEFINES += APPVERSION=\"$(APPVERSION)\"
DEFINES += APPVERSION=\"$(APPVERSION)\"

DEFINES += OS_IO_SEPROXYHAL
DEFINES += HAVE_BAGL HAVE_SPRINTF
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=7 IO_HID_EP_LENGTH=64 HAVE_USB_APDU
DEFINES += OS_IO_SEPROXYHAL
DEFINES += HAVE_BAGL HAVE_SPRINTF
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=7 IO_HID_EP_LENGTH=64 HAVE_USB_APDU

DEFINES += LEDGER_MAJOR_VERSION=$(APPVERSION_M) LEDGER_MINOR_VERSION=$(APPVERSION_N) LEDGER_PATCH_VERSION=$(APPVERSION_P)
DEFINES += LEDGER_MAJOR_VERSION=$(APPVERSION_M) LEDGER_MINOR_VERSION=$(APPVERSION_N) LEDGER_PATCH_VERSION=$(APPVERSION_P)

DEFINES += HAVE_U2F HAVE_IO_U2F
DEFINES += U2F_PROXY_MAGIC=\"FIL\"
DEFINES += USB_SEGMENT_SIZE=64
DEFINES += U2F_MAX_MESSAGE_SIZE=264 #257+5+2
DEFINES += HAVE_BOLOS_APP_STACK_CANARY
DEFINES += USB_SEGMENT_SIZE=64
DEFINES += HAVE_BOLOS_APP_STACK_CANARY

WEBUSB_URL = www.ledgerwallet.com
DEFINES += HAVE_WEBUSB WEBUSB_URL_SIZE_B=$(shell echo -n $(WEBUSB_URL) | wc -c) WEBUSB_URL=$(shell echo -n $(WEBUSB_URL) | sed -e "s/./\\\'\0\\\',/g")
DEFINES += HAVE_WEBUSB WEBUSB_URL_SIZE_B=0 WEBUSB_URL=""

ifeq ($(TARGET_NAME),TARGET_NANOX)
DEFINES += IO_SEPROXYHAL_BUFFER_SIZE_B=300
Expand All @@ -115,12 +116,14 @@ DEFINES += HAVE_BAGL_FONT_OPEN_SANS_LIGHT_16PX

DEFINES += HAVE_UX_FLOW

#SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
SDK_SOURCE_PATH += lib_ux
DEFINES += HAVE_BLE
DEFINES += HAVE_BLE_APDU BLE_COMMAND_TIMEOUT_MS=2000

SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
else
# Assume Nano S
DEFINES += IO_SEPROXYHAL_BUFFER_SIZE_B=128
DEFINES += HAVE_BOLOS_UX COMPLIANCE_UX_160 HAVE_UX_LEGACY HAVE_UX_FLOW
DEFINES += HAVE_BOLOS_UX COMPLIANCE_UX_160 HAVE_UX_LEGACY
endif

# X specific
Expand Down Expand Up @@ -167,39 +170,44 @@ APP_SOURCE_PATH += $(MY_DIR)/../deps/ledger-zxlib/include
APP_SOURCE_PATH += $(MY_DIR)/../deps/ledger-zxlib/src
APP_SOURCE_PATH += $(MY_DIR)/../deps/ledger-zxlib/app/common

SDK_SOURCE_PATH += lib_stusb lib_u2f lib_stusb_impl
SDK_SOURCE_PATH += lib_stusb lib_stusb_impl
SDK_SOURCE_PATH += lib_ux

# NanoX SDK does not allow += in APP_SOURCE_PATH or INCLUDES_PATH
CFLAGS += -I$(MY_DIR)/../deps/tinycbor/src
APP_SOURCE_PATH += $(MY_DIR)/../deps/tinycbor-ledger

#SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
SDK_SOURCE_PATH += lib_ux

.PHONY: rust
rust:
@echo "No rust code"
# cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release

# Before linking, we need to be sure rust lib is there
bin/app.elf: rust

.PHONY: rust_clean
rust_clean:
@echo "No rust code"
# cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo clean

clean: rust_clean

# load, delete and listvariants are provided to comply with Ledger requirements
.PHONY: load
load:
python -m ledgerblue.loadApp $(APP_LOAD_PARAMS)

.PHONY: delete
delete:
python -m ledgerblue.deleteApp $(COMMON_DELETE_PARAMS)

.PHONY: rust

all: default rust

# Import generic rules from the SDK
include $(BOLOS_SDK)/Makefile.rules

#add dependency on custom makefile filename
dep/%.d: %.c Makefile

.PHONY: listvariants
listvariants:
@echo VARIANTS COIN FIL
2 changes: 1 addition & 1 deletion app/script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ MEMORY
}

PAGE_SIZE = 64;
STACK_SIZE = 848;
STACK_SIZE = 2008;
END_STACK = ORIGIN(SRAM) + LENGTH(SRAM);

SECTIONS
Expand Down
57 changes: 57 additions & 0 deletions app/src/addr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* (c) 2020 Zondax GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#include <stdio.h>
#include "coin.h"
#include "zxerror.h"
#include "zxmacros.h"
#include "app_mode.h"
#include "crypto.h"

zxerr_t addr_getNumItems(uint8_t *num_items) {
zemu_log_stack("addr_getNumItems");
*num_items = 1;
if (app_mode_expert()) {
*num_items = 2;
}
return zxerr_ok;
}

zxerr_t addr_getItem(int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
zemu_log_stack("addr_getItem");
switch (displayIdx) {
case 0:
snprintf(outKey, outKeyLen, "Your Address");
pageString(outVal, outValLen, (char *) (G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SECP256K1), pageIdx, pageCount);
return zxerr_ok;
case 1: {
if (!app_mode_expert()) {
return zxerr_no_data;
}

snprintf(outKey, outKeyLen, "Your Path");
char buffer[300];
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
return zxerr_ok;
}
default:
return zxerr_no_data;
}
}
34 changes: 34 additions & 0 deletions app/src/addr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* (c) 2020 Zondax GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/// Return the number of items in the address view
zxerr_t addr_getNumItems(uint8_t *num_items);

/// Gets an specific item from the address view (including paging)
zxerr_t addr_getItem(int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outValue, uint16_t outValueLen,
uint8_t pageIdx, uint8_t *pageCount);

#ifdef __cplusplus
}
#endif
6 changes: 3 additions & 3 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
extern "C" {
#endif

#define CLA 0x06

#include <stdint.h>
#include <stddef.h>

Expand All @@ -40,8 +42,6 @@ typedef enum {
} address_kind_e;

#define VIEW_ADDRESS_OFFSET_SECP256K1 (SECP256K1_PK_LEN + ADDRESS_PROTOCOL_SECP256K1_PAYLOAD_LEN + ADDRESS_PROTOCOL_LEN + 2)
#define VIEW_ADDRESS_ITEM_COUNT 2
#define VIEW_ADDRESS_LAST_PAGE_DEFAULT 0

#define CRYPTO_BLOB_SKIP_BYTES 1

Expand All @@ -50,7 +50,7 @@ typedef enum {
#define COIN_SUPPORTED_TX_VERSION 0

#define MENU_MAIN_APP_LINE1 "Filecoin"
#define MENU_MAIN_APP_LINE2 "Network"
#define MENU_MAIN_APP_LINE2 "Ready"
#define APPVERSION_LINE1 "Version"
#define APPVERSION_LINE2 "v"APPVERSION

Expand Down
38 changes: 1 addition & 37 deletions app/src/common/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,5 @@
********************************************************************************/

#include "actions.h"
#include "crypto.h"
#include "tx.h"
#include "apdu_codes.h"
#include <os_io_seproxyhal.h>
#include "coin.h"

void app_sign() {
uint8_t *signature = G_io_apdu_buffer;

const uint8_t *message = tx_get_buffer() + CRYPTO_BLOB_SKIP_BYTES;
const uint16_t messageLength = tx_get_buffer_length() - CRYPTO_BLOB_SKIP_BYTES;

const uint8_t replyLen = crypto_sign(signature, IO_APDU_BUFFER_SIZE - 2, message, messageLength);
if (replyLen > 0) {
set_code(G_io_apdu_buffer, replyLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, replyLen + 2);
} else {
set_code(G_io_apdu_buffer, 0, APDU_CODE_SIGN_VERIFY_ERROR);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}
}

uint8_t app_fill_address() {
// Put data directly in the apdu buffer
MEMZERO(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE);
return crypto_fillAddress(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 2);
}

void app_reply_address() {
const uint8_t replyLen = app_fill_address();
set_code(G_io_apdu_buffer, replyLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, replyLen + 2);
}

void app_reply_error() {
set_code(G_io_apdu_buffer, 0, APDU_CODE_DATA_INVALID);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}
uint8_t action_addr_len;
40 changes: 36 additions & 4 deletions app/src/common/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,43 @@
#pragma once

#include <stdint.h>
#include "crypto.h"
#include "tx.h"
#include "apdu_codes.h"
#include <os_io_seproxyhal.h>
#include "coin.h"

void app_sign();
extern uint8_t action_addr_len;

uint8_t app_fill_address();
__Z_INLINE void app_sign() {
uint8_t *signature = G_io_apdu_buffer;

void app_reply_address();
const uint8_t *message = tx_get_buffer() + CRYPTO_BLOB_SKIP_BYTES;
const uint16_t messageLength = tx_get_buffer_length() - CRYPTO_BLOB_SKIP_BYTES;

void app_reply_error();
const uint8_t replyLen = crypto_sign(signature, IO_APDU_BUFFER_SIZE - 3, message, messageLength);
if (replyLen > 0) {
set_code(G_io_apdu_buffer, replyLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, replyLen + 2);
} else {
set_code(G_io_apdu_buffer, 0, APDU_CODE_SIGN_VERIFY_ERROR);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}
}

__Z_INLINE uint8_t app_fill_address() {
// Put data directly in the apdu buffer
MEMZERO(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE);
action_addr_len = crypto_fillAddress(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE - 2);
return action_addr_len;
}

__Z_INLINE void app_reply_address() {
set_code(G_io_apdu_buffer, action_addr_len, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, action_addr_len + 2);
}

__Z_INLINE void app_reply_error() {
set_code(G_io_apdu_buffer, 0, APDU_CODE_DATA_INVALID);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}
Loading

0 comments on commit 07f9598

Please sign in to comment.