Skip to content

Commit

Permalink
improve printing code mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Sep 30, 2024
1 parent 98acd43 commit 0d96616
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 118 deletions.
172 changes: 88 additions & 84 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#*******************************************************************************
#* (c) 2018-2024 Zondax AG
#*
#* 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.
#********************************************************************************
# *******************************************************************************
# * (c) 2018-2024 Zondax AG
# *
# * 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.
# ********************************************************************************
cmake_minimum_required(VERSION 3.28)
include("cmake/HunterGate.cmake")
HunterGate(
Expand All @@ -22,17 +22,17 @@ HunterGate(
)

if(CMAKE_GENERATOR MATCHES "Ninja")
message(FATAL_ERROR "This project does not support the Ninja generator. "
"Please use Unix Makefiles or another supported generator. "
"This error is typical in CLion. In this case, switch to generator Unix Makefiles.")
message(FATAL_ERROR "This project does not support the Ninja generator. "
"Please use Unix Makefiles or another supported generator. "
"This error is typical in CLion. In this case, switch to generator Unix Makefiles.")
endif()

########################################################
# #######################################################
project(ledger-ironfish VERSION 0.0.0)
set(CMAKE_CXX_STANDARD 20)
cmake_policy(SET CMP0025 NEW)
cmake_policy(SET CMP0144 NEW)

# cmake_policy(SET CMP0144 NEW)
set(HUNTER_STATUS_DEBUG ON)
set(HUNTER_TLS_VERIFY OFF)

Expand Down Expand Up @@ -60,22 +60,22 @@ if(ENABLE_FUZZING)
SET(ENABLE_SANITIZERS ON CACHE BOOL "Sanitizer automatically enabled" FORCE)
SET(CMAKE_BUILD_TYPE Debug)

if (DEFINED ENV{FUZZ_LOGGING})
if(DEFINED ENV{FUZZ_LOGGING})
add_definitions(-DFUZZING_LOGGING)
message(FATAL_ERROR "Fuzz logging enabled")
endif()

set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,bugprone-*,cert-*,clang-analyzer-*,-cert-err58-cpp,misc-*)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
message(FATAL_ERROR "Clang version must be at least 10.0!")
endif()
else()
message(FATAL_ERROR
"You are using an unsupported compiler! Fuzzing only works with Clang 10.\n"
"1. Install clang-10 \n"
"2. Pass -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_COMPILER=clang++-10")
"You are using an unsupported compiler! Fuzzing only works with Clang 10.\n"
"1. Install clang-10 \n"
"2. Pass -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_COMPILER=clang++-10")
endif()

string(APPEND CMAKE_C_FLAGS " -fsanitize=fuzzer-no-link")
Expand All @@ -95,67 +95,69 @@ if(ENABLE_SANITIZERS)
string(APPEND CMAKE_LINKER_FLAGS " -fsanitize=address,undefined -fsanitize-recover=address,undefined")
endif()

set (RETRIEVE_MAJOR_CMD
"cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_M | cut -b 14- | tr -d '\n'"
set(RETRIEVE_MAJOR_CMD
"cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_M | cut -b 14- | tr -d '\n'"
)
set (RETRIEVE_MINOR_CMD
"cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_N | cut -b 14- | tr -d '\n'"
set(RETRIEVE_MINOR_CMD
"cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_N | cut -b 14- | tr -d '\n'"
)
execute_process(
COMMAND bash "-c" ${RETRIEVE_MAJOR_CMD}
RESULT_VARIABLE MAJOR_RESULT
OUTPUT_VARIABLE MAJOR_VERSION
COMMAND bash "-c" ${RETRIEVE_MAJOR_CMD}
RESULT_VARIABLE MAJOR_RESULT
OUTPUT_VARIABLE MAJOR_VERSION
)
execute_process(
COMMAND bash "-c" ${RETRIEVE_MINOR_CMD}
RESULT_VARIABLE MINOR_RESULT
OUTPUT_VARIABLE MINOR_VERSION
COMMAND bash "-c" ${RETRIEVE_MINOR_CMD}
RESULT_VARIABLE MINOR_RESULT
OUTPUT_VARIABLE MINOR_VERSION
)

message(STATUS "LEDGER_MAJOR_VERSION [${MAJOR_RESULT}]: ${MAJOR_VERSION}" )
message(STATUS "LEDGER_MINOR_VERSION [${MINOR_RESULT}]: ${MINOR_VERSION}" )
message(STATUS "LEDGER_MAJOR_VERSION [${MAJOR_RESULT}]: ${MAJOR_VERSION}")
message(STATUS "LEDGER_MINOR_VERSION [${MINOR_RESULT}]: ${MINOR_VERSION}")

add_definitions(
-DLEDGER_MAJOR_VERSION=${MAJOR_VERSION}
-DLEDGER_MINOR_VERSION=${MINOR_VERSION}
)


# #############################################################
# #############################################################
# Static Libraries
file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/app_mode.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/base58.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/bech32.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/bignum.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/hexutils.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/zxmacros.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/zxformat.c
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
####
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/blake2s/blake2s-ref.c
)
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/app_mode.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/base58.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/bech32.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/bignum.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/hexutils.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/zxmacros.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/src/zxformat.c

# ###
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper/chacha.c

# ###
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/blake2s/blake2s-ref.c
)

add_library(app_lib STATIC ${LIB_SRC})

target_include_directories(app_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/include
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref

)
${CMAKE_CURRENT_SOURCE_DIR}/deps/ledger-zxlib/include
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
)

##############################################################
## Rust library for CPP tests
# #############################################################
# # Rust library for CPP tests
set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust")

# Determine the Rust target triple based on the host system
Expand Down Expand Up @@ -204,39 +206,41 @@ add_dependencies(rslib RustLibBuild)
# Ensure your C++ targets depend on the Rust library being built first
# For example, for your app_lib static library:
add_dependencies(app_lib rslib)
##############################################################
# Tests

# #############################################################
# Tests
file(GLOB_RECURSE TESTS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp)

add_executable(unittests ${TESTS_SRC})
target_include_directories(unittests PRIVATE
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
)
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib

# ##
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref
)

target_link_libraries(unittests PRIVATE
app_lib
rslib
GTest::gtest_main
fmt::fmt
JsonCpp::JsonCpp)
app_lib
rslib
GTest::gtest_main
fmt::fmt
JsonCpp::JsonCpp)

add_compile_definitions(TESTVECTORS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/")
add_test(NAME unittests COMMAND unittests)
set_tests_properties(unittests PROPERTIES WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)

##############################################################
##############################################################
# Fuzz Targets
# #############################################################
# #############################################################
# Fuzz Targets
if(ENABLE_FUZZING)
set(FUZZ_TARGETS
parser_parse
)
)

foreach(target ${FUZZ_TARGETS})
add_executable(fuzz-${target} ${CMAKE_CURRENT_SOURCE_DIR}/fuzz/${target}.cpp)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ifeq ($(BOLOS_SDK),)
# When not using the SDK, we override and build the XL complete app

ZXLIB_COMPILE_STAX ?= 1
PRODUCTION_BUILD ?= 0
PRODUCTION_BUILD ?= 1
include $(CURDIR)/deps/ledger-zxlib/dockerized_build.mk

else
Expand All @@ -38,6 +38,6 @@ default:
endif

test_all:
PRODUCTION_BUILD=1 make
PRODUCTION_BUILD=1 APP_TESTING=1 make
make zemu_install
make zemu_test
2 changes: 1 addition & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.installer_script
include $(BOLOS_SDK)/Makefile.defines

$(info ************ TARGET_NAME = [$(TARGET_NAME)])
PRODUCTION_BUILD ?= 0
PRODUCTION_BUILD ?= 1
$(info ************ PRODUCTION_BUILD = [$(if $(filter 1,$(PRODUCTION_BUILD)),PRODUCTION BUILD,INTERNAL USE)])
DEFINES += PRODUCTION_BUILD=$(PRODUCTION_BUILD)

Expand Down
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=12
APPVERSION_P=13
13 changes: 0 additions & 13 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile
THROW(APDU_CODE_OK);
}

#if defined(APP_TESTING)
void handleTest(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
THROW(APDU_CODE_OK);
}
#endif

void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
volatile uint16_t sw = 0;

Expand Down Expand Up @@ -201,13 +195,6 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

#if defined(APP_TESTING)
case INS_TEST: {
handleTest(flags, tx, rx);
THROW(APDU_CODE_OK);
break;
}
#endif
default:
THROW(APDU_CODE_INS_NOT_SUPPORTED);
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ parser_error_t crypto_calculate_key_for_encryption_keys(const uint8_t *note, con

#if defined(LEDGER_SPECIFIC)
cx_blake2b_t ctx = {0};
ASSERT_CX_OK(cx_blake2b_init2_no_throw(&ctx, 32, NULL, 0, (uint8_t *)SHARED_KEY_PERSONALIZATION,
ASSERT_CX_OK(cx_blake2b_init2_no_throw(&ctx, 256, NULL, 0, (uint8_t *)SHARED_KEY_PERSONALIZATION,
sizeof(SHARED_KEY_PERSONALIZATION)));
ASSERT_CX_OK(cx_blake2b_update(&ctx, ovk, 32));
ASSERT_CX_OK(cx_blake2b_update(&ctx, note, VALUE_COMMITMENT_SIZE + NOTE_COMMITMENT_SIZE + EPHEMERAL_PUBLIC_KEY_SIZE));
Expand Down Expand Up @@ -274,6 +274,9 @@ parser_error_t crypto_decrypt_merkle_note(parser_tx_t *txObj, const uint8_t *m_n
uint8_t cc_nonce[CHACHA_NONCE_SIZE] = {0};
CHECK_ERROR(chacha(note_encryption_key, sizeof(note_encryption_key), m_note + NOTE_ENCRYPTION_KEYS_OFFSET,
ENCRYPTED_SHARED_KEY_SIZE, encryption_key, cc_nonce, 1));
#if defined(LEDGER_SPECIFIC)
io_seproxyhal_io_heartbeat();
#endif
CHECK_APP_CANARY()

// Extract public address and secret key from the note encryption key
Expand All @@ -286,14 +289,19 @@ parser_error_t crypto_decrypt_merkle_note(parser_tx_t *txObj, const uint8_t *m_n
uint8_t shared_key[32] = {0};
const uint8_t *ephemeral_public_key = m_note + VALUE_COMMITMENT_SIZE + NOTE_COMMITMENT_SIZE;
CHECK_ERROR(shared_secret(secret_key, public_address, ephemeral_public_key, shared_key));
#if defined(LEDGER_SPECIFIC)
io_seproxyhal_io_heartbeat();
#endif
CHECK_APP_CANARY()

// Finally decrypt the note
uint8_t plain_text[ENCRYPTED_NOTE_SIZE] = {0};
CHECK_ERROR(chacha(plain_text, sizeof(plain_text), m_note + ENCRYPTED_NOTE_OFFSET, ENCRYPTED_NOTE_SIZE, shared_key,
cc_nonce, 1));
#if defined(LEDGER_SPECIFIC)
io_seproxyhal_io_heartbeat();
#endif
CHECK_APP_CANARY()

// Fill the txObj with the decrypted note
txObj->outputs.decrypted_note.value = *(uint64_t *)(plain_text + SCALAR_SIZE);
MEMCPY(txObj->outputs.decrypted_note.asset_id, plain_text + SCALAR_SIZE + AMOUNT_VALUE_SIZE + MEMO_SIZE,
Expand Down
3 changes: 3 additions & 0 deletions app/src/crypto_helper/chacha.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ parser_error_t chacha(uint8_t *out, size_t out_len, const uint8_t *in, size_t in
input[14] = U8TO32_LITTLE(nonce + 4);
input[15] = U8TO32_LITTLE(nonce + 8);
while (in_len > 0) {
#if defined(LEDGER_SPECIFIC)
io_seproxyhal_io_heartbeat();
#endif
todo = sizeof(buf);
if (in_len < todo) {
todo = in_len;
Expand Down
Loading

0 comments on commit 0d96616

Please sign in to comment.