Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Upgrading zxlib (#13)
Browse files Browse the repository at this point in the history
* updating tests
* upgrading zxlib
* add default app variant to cpp tests
* Adjust test delay
  • Loading branch information
jleni authored May 28, 2020
1 parent fb938c8 commit 6b04421
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 73 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_subdirectory(cmake/gtest)

string(APPEND CMAKE_CXX_FLAGS " -fsanitize=address -fno-omit-frame-pointer")
string(APPEND CMAKE_LINKER_FLAGS " -fsanitize=address -fno-omit-frame-pointer")
add_definitions(-DAPP_STANDARD)

set (RETRIEVE_LEDGER_MINOR_CMD
"cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_N | cut -b 14-"
Expand Down
31 changes: 26 additions & 5 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,32 @@ MY_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

include $(BOLOS_SDK)/Makefile.defines

# Main app configuration
APPNAME = "Kusama"
ifndef COIN
COIN=kusama
endif

include $(CURDIR)/Makefile.version

$(info COIN = [$(COIN)])
ifeq ($(COIN),kusama)
# Main app configuration
DEFINES += APP_STANDARD
APPNAME = "Kusama"
APPPATH = "44'/434'"
APP_LOAD_PARAMS = --appFlags 0x200 --delete $(COMMON_LOAD_PARAMS) --path ${APPPATH}

else ifeq ($(COIN),kusama_variant1)
else
define error_message


COIN value not supported: [$(COIN)]


endef
$(error "$(error_message)")
endif

APP_LOAD_PARAMS = --appFlags 0x200 --delete $(COMMON_LOAD_PARAMS) --path $(APPPATH)

ifeq ($(TARGET_NAME),TARGET_NANOS)
SCRIPT_LD:=$(CURDIR)/script.ld
Expand Down Expand Up @@ -91,7 +111,7 @@ DEFINES += HAVE_BAGL_FONT_OPEN_SANS_REGULAR_11PX
DEFINES += HAVE_BAGL_FONT_OPEN_SANS_EXTRABOLD_11PX
DEFINES += HAVE_BAGL_FONT_OPEN_SANS_LIGHT_16PX

DEFINES += HAVE_UX_FLOW
DEFINES += HAVE_UX_FLOW

#SDK_SOURCE_PATH += lib_blewbxx lib_blewbxx_impl
SDK_SOURCE_PATH += lib_ux
Expand Down Expand Up @@ -134,7 +154,8 @@ AFLAGS +=

LD := $(GCCPATH)arm-none-eabi-gcc
LDFLAGS += -O3 -Os
LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib -lm -lgcc -lc
LDLIBS += -lm -lgcc -lc
LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib

##########################
include $(BOLOS_SDK)/Makefile.glyphs
Expand Down
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
APPVERSION_M=0
APPVERSION_N=1062
APPVERSION_P=1
APPVERSION_P=3
47 changes: 7 additions & 40 deletions app/src/coin.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) 2019 Zondax GmbH
* (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.
Expand All @@ -15,43 +15,10 @@
********************************************************************************/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

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

#define HDPATH_LEN_DEFAULT 5

#define HDPATH_0_DEFAULT (0x80000000 | 0x2c)
#define HDPATH_1_DEFAULT (0x80000000 | 0x1b2) // 434
#define HDPATH_2_DEFAULT (0x80000000u | 0u)
#define HDPATH_3_DEFAULT (0u)
#define HDPATH_4_DEFAULT (0u)

#define PK_LEN_ED25519 32u

typedef enum {
addr_ed22519 = 0,
addr_sr25519 = 1
} address_kind_e;

#define VIEW_ADDRESS_OFFSET_ED25519 (PK_LEN_ED25519)
#define VIEW_ADDRESS_ITEM_COUNT 2
#define VIEW_ADDRESS_LAST_PAGE_DEFAULT 0

#define MENU_MAIN_APP_LINE1 "Kusama"
#define MENU_MAIN_APP_LINE2 "Web3"
#define APPVERSION_LINE1 "Kusama"
#define APPVERSION_LINE2 "v"APPVERSION

// Specific to Kusama
#define PK_ADDRESS_TYPE 02
#define SUPPORTED_SPEC_VERSION LEDGER_MINOR_VERSION

#define COIN_AMOUNT_DECIMAL_PLACES 12

#ifdef __cplusplus
}
#if defined(APP_STANDARD)
#include "coin_standard.h"
#elif defined(APP_VARIANT1)
#include "coin_variant1.h"
#else
#error "APP MODE IS NOT SUPPORTED"
#endif
58 changes: 58 additions & 0 deletions app/src/coin_standard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* (c) 2019 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

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

#define HDPATH_LEN_DEFAULT 5

#define HDPATH_0_DEFAULT (0x80000000 | 0x2c)
#define HDPATH_1_DEFAULT (0x80000000 | 0x1b2) // 434
#define HDPATH_2_DEFAULT (0x80000000u | 0u)
#define HDPATH_3_DEFAULT (0u)
#define HDPATH_4_DEFAULT (0u)

#define PK_LEN_ED25519 32u

typedef enum {
addr_ed22519 = 0,
addr_sr25519 = 1
} address_kind_e;

#define VIEW_ADDRESS_OFFSET_ED25519 (PK_LEN_ED25519)
#define VIEW_ADDRESS_ITEM_COUNT 2
#define VIEW_ADDRESS_LAST_PAGE_DEFAULT 0

#define MENU_MAIN_APP_LINE1 "Kusama"
#define MENU_MAIN_APP_LINE2 "Web3"
#define APPVERSION_LINE1 "Kusama"
#define APPVERSION_LINE2 "v"APPVERSION

// Specific to Kusama
#define PK_ADDRESS_TYPE 02
#define SUPPORTED_SPEC_VERSION LEDGER_MINOR_VERSION

#define COIN_AMOUNT_DECIMAL_PLACES 12
#define CRYPTO_BLOB_SKIP_BYTES 0

#ifdef __cplusplus
}
#endif
26 changes: 26 additions & 0 deletions app/src/coin_variant1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* (c) 2019 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

#error "APP MODE IS NOT SUPPORTED"

#ifdef __cplusplus
}
#endif
15 changes: 11 additions & 4 deletions app/src/common/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@
#include <os_io_seproxyhal.h>
#include "coin.h"

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

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

return crypto_sign(signature, IO_APDU_BUFFER_SIZE - 3, message, messageLength);
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);
}
}

uint8_t app_fill_address() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/common/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <stdint.h>

uint8_t app_sign();
void app_sign();

uint8_t app_fill_address();

Expand Down
8 changes: 8 additions & 0 deletions app/src/common/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
if (!process_chunk(tx, rx))
THROW(APDU_CODE_OK);

CHECK_APP_CANARY()

const char *error_msg = tx_parse();
CHECK_APP_CANARY()

if (error_msg != NULL) {
int error_msg_length = strlen(error_msg);
Expand All @@ -213,6 +216,7 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
THROW(APDU_CODE_DATA_INVALID);
}

CHECK_APP_CANARY()
view_sign_show();
*flags |= IO_ASYNCH_REPLY;
break;
Expand Down Expand Up @@ -295,15 +299,19 @@ void app_main() {
{
rx = tx;
tx = 0;

rx = io_exchange(CHANNEL_APDU | flags, rx);
flags = 0;
CHECK_APP_CANARY()

if (rx == 0)
THROW(APDU_CODE_EMPTY_BUFFER);

handle_generic_apdu(&flags, &tx, rx);
CHECK_APP_CANARY()

handleApdu(&flags, &tx, rx);
CHECK_APP_CANARY()
}
CATCH_OTHER(e);
{
Expand Down
12 changes: 1 addition & 11 deletions deps/ledger-zxlib/app/common/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,9 @@ void h_error_accept(unsigned int _) {

void h_sign_accept(unsigned int _) {
UNUSED(_);

const uint8_t replyLen = app_sign();

app_sign();
view_idle_show(0);
UX_WAIT();

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

void h_sign_reject(unsigned int _) {
Expand Down
2 changes: 1 addition & 1 deletion deps/ledger-zxlib/app/common/view_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void h_review_loop_start() {
if (flow_inside_loop) {
// coming from right
h_paging_decrease();
if (viewdata.idx<0) {
if (viewdata.itemIdx<0) {
// exit to the left
flow_inside_loop = 0;
ux_flow_prev();
Expand Down
10 changes: 9 additions & 1 deletion deps/ledger-zxlib/dockerized_build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ define run_docker
-u $(USERID) \
-v $(shell pwd):/project \
$(DOCKER_IMAGE) \
"$(2)"
"COIN=$(COIN) $(2)"
endef

all: build
Expand Down Expand Up @@ -99,6 +99,10 @@ buildX: build_rust
clean:
$(call run_docker,$(DOCKER_BOLOS_SDK),make -C $(DOCKER_APP_SRC) clean)

.PHONY: listvariants
listvariants:
$(call run_docker,$(DOCKER_BOLOS_SDK),make -C $(DOCKER_APP_SRC) listvariants)

.PHONY: shell
shell:
$(call run_docker,$(DOCKER_BOLOS_SDK) -t,bash)
Expand Down Expand Up @@ -175,6 +179,10 @@ zemu_install: zemu_install_js_link
zemu:
cd $(TESTS_ZEMU_DIR)/tools && node debug.mjs

.PHONY: zemu_val
zemu_val:
cd $(TESTS_ZEMU_DIR)/tools && node debug_val.mjs

.PHONY: zemu_debug
zemu_debug:
cd $(TESTS_ZEMU_DIR)/tools && node debug.mjs debug
Expand Down
4 changes: 2 additions & 2 deletions deps/ledger-zxlib/include/zxversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
********************************************************************************/
#pragma once

#define ZXLIB_MAJOR 1
#define ZXLIB_MAJOR 2
#define ZXLIB_MINOR 0
#define ZXLIB_PATCH 3
#define ZXLIB_PATCH 0
Binary file modified tests_zemu/snapshots/show-address/0.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 modified tests_zemu/snapshots/show-address/1.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 modified tests_zemu/snapshots/show-address/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 6 additions & 7 deletions tests_zemu/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe('Basic checks', function () {
expect(resp.return_code).toEqual(0x9000);
expect(resp.error_message).toEqual("No errors");

const expected_address = "CaKWz5omakTK7ovp4m3koXrHyHb7NG3Nt7GENHbviByZpKp";
const expected_pk = "0000000000000000000000000000000000000000000000000000000000000000";
const expected_address = "JMdbWK5cy3Bm4oCyhWNLQJoC4cczNgJsyk7nLZHMqFT7z7R";
const expected_pk = "ffbc10f71d63e0da1b9e7ee2eb4037466551dc32b9d4641aafd73a65970fae42";

expect(resp.address).toEqual(expected_address);
expect(resp.pubKey).toEqual(expected_pk);
Expand All @@ -93,7 +93,7 @@ describe('Basic checks', function () {
const respRequest = app.getAddress(0x80000000, 0x80000000, 0x80000000, true);

// We need to wait until the app responds to the APDU
await Zemu.sleep(2000);
await Zemu.sleep(3500);

// Now navigate the address / path
await sim.snapshot(`${snapshotPrefixTmp}${snapshotCount++}.png`);
Expand All @@ -112,8 +112,8 @@ describe('Basic checks', function () {
expect(resp.return_code).toEqual(0x9000);
expect(resp.error_message).toEqual("No errors");

const expected_address = "CaKWz5omakTK7ovp4m3koXrHyHb7NG3Nt7GENHbviByZpKp";
const expected_pk = "0000000000000000000000000000000000000000000000000000000000000000";
const expected_address = "JMdbWK5cy3Bm4oCyhWNLQJoC4cczNgJsyk7nLZHMqFT7z7R";
const expected_pk = "ffbc10f71d63e0da1b9e7ee2eb4037466551dc32b9d4641aafd73a65970fae42";

expect(resp.address).toEqual(expected_address);
expect(resp.pubKey).toEqual(expected_pk);
Expand Down Expand Up @@ -225,8 +225,7 @@ describe('Basic checks', function () {
prehash = Buffer.from(blake2bFinal(context));
}
const valid = ed25519.verify(signatureResponse.signature.slice(1), prehash, pubKey);
// FIXME: Zemu/Speculos cannot yet emulate Ed25519 HD
// expect(valid).toEqual(true);
expect(valid).toEqual(true);
} finally {
await sim.close();
}
Expand Down

0 comments on commit 6b04421

Please sign in to comment.