Skip to content

Commit

Permalink
Feat/bls poc (#244)
Browse files Browse the repository at this point in the history
* verify bls signatures
Fix return type and add comment
fix(bls): Fixed wrong data size in constants
ping to ic_bls12_381 to 0.9.1

* Remove unnecessary cargo patch holder code that was commented out

* Initial parser for icp certificates

* add some useful methods and lookup funtion

* Implement the reconstruct function and enhance a bit the lookup function which needs to be rewritten

* Add the lookup_path_list and fix simple lookup function

* Fix delegation parser

* Update error definitions

* Complete Certificate parsing and verification

* Add more test and ensure parser/root_hash/lookup are working

* New testing dependencies

* enhance verification

* Improve tests and error handling, add C interface meant to parse certificates

* some improvements

* fix typo

* Initial candid parser for irc21-consent-message-response type

* Add license note

* Implement candid parser for ConsentMessageResponse and add unit test

* Many improvements and add UI for MessageResponse type and full parser along with some unit tests

* Add helper lines function

* Document code better

* new function to compute message

* shore: Re-structure the project better by moving certificate related parts into a separate folder

* Add parser for call_request argument in apdu instruction

* Add parser for consent_msg_request argument in apdu instruction

* Update value() method and add canister ranges parsing

* Add method to check if a canister is within certain ranges, add test

* Implement UI functions and export entry points

* add some new files and placeholders for bls parsing

* add apdu js interface

* adding new nvdata functions to save bls info

* update js package and add simple zemu test

* add rust ffi for parsing

* improve root_key handling and cleaning

* root_key as global value

* save call hash to be signed

* improve sign apdu

* add placeholder for certificate verifying function

* type fix

* Restructure files

* prefix state names

* Implement checks for payload and consent request plus minor fixes

* Fill handler path with processing functions in tx module and parser

* use config.toml

* Separate icrc21 types into their own module, and implement request_id computation

* Add utility functions to compute hashes

* Minor updates, and checks

* Add type table parsing and use it to parse candid values

* Fix icrc21 consentmessage request parsing and update other parts to use type_table

* use little endiannes and add extra checks in test

* Nonce is optional

* Add request_id method and tests along with conversion methods

* Use request_id to check also for certificate validation and add simple test

* Use inner arg and method name to set ffi struct for certificate verification

* Add signing method for bls certificate and update actions

* Use proper constants to define type lengths in ffi

* Remove unused method to compute request_id

* Some improvements and fixing warnings

* Add compilation flag to enable/disable bls certificate verification depending on ledger target

* Conditional compilation and fix tests and js library

* Remove decode trait implementation reducing stack usage

* Use ledger-rust bindings to create static resources for call/consent request and update C api and handlers

* Update build command in cargo for ledger-rust

* Add bls_message to build digest used in certificate signature verification and update from_bytes_into implementation

* Use certificate resource and improve certificate handler

* Fix bls test key

* Fix signing and resource cleaning

* Some improvements

* Some cleanups

* Fix test and add snapshot

* Add logs

* Add prefix to exported rust functions

* Print to std-out in tests

* Many clean-ups and fix js instruction to verify certificate

* Use constant and implement FromBytes

* Remove from_bytes impl in delegation and fix certificate::from_bytes_into

* Add timestamp check

* Add consent message formatting

* Add utilities for testing with cargo insta later

* Update test snapshots

* Use TryFrom impl and add pubkey test

* Ensure resources are freed and state is reset after signing

* Define a state at creation level

* Enable certificate verification using official key for testing

* Add test with/without custom root key

* Improve parsing by using from_bytes_into which reduces stack usage

* Fix ingress_expiry and method_name checks

* Add new constants

* use from_bytes_into

* Remove table logging

* Remove check on root_key as it could be an optional parameters

* Update testing data

* export C function to rust to handle principal request and use it to verify certificate

* Import rslib if BLS feature is enable

* Improve CI for rust parser

* fix clippy and test

* fix clippy and zemu tests

* Improve zemu testing

* fix clippy

* fix zemu

* fix flex compilation

* filter out nanos for bls testing in zemu

* some CI fixes

* Re-structuring code to reduce stack usage relying on lazy parsing and ensuring table lenght is sized to a reasonable size

* Reduce stack usage

* Add test to verify ffi verification flow

* fix ffi test

* Reduce stack usage and more logging

* update dep

* Add more logging

* update cargo.lock

* move from stack to nvm

* Disable nanox for now on zemu tests

* Remove BLS support for nanoX

* Fix hashing computation, now we hash name and value separately and update the global hasher

* Ensure jest reporter module gets installed

* Ensure jest reporter module gets installed

* Fix warnings and compile c api on bls feature only

* Add try script for bls verification

* Adjust time expiryti offset as requested to 10 minutes and not 12

* Update testing data and snapshots

* Update submodules

* Fix compilation error with protobuf

* Update testing data

* remove flow which is not compatible with our hybrid app

* remove dead code

* Update error type in C code to match Rust new errors

* Removed commemted code and add more notes to clarify design desitions

* Rename test for bls to avoid snapshots overlapping

Ensure state gets restarted in case of errors

Remove unnecessary SDK modules

Document and more comments

consice header

Fix typo

* Fix canister ranges comparisson and update testing data in ffi-test

* Bum app version and update snapshots

* Add a new set of tests, and increase time offset to 12 minutes

* Minor fix and use call request for sender validation

* Add new zemu test to ensure principal verification agains certificate data

* Use default device principal in our unit tests

* Increate time offset to 12 minutes, so a call request is valid if made within those 12 minutes window since certificate creation

* Update sender verification to follow what they suggested

---------

Co-authored-by: Carlos Medeiros <[email protected]>
  • Loading branch information
neithanmo and chcmedeiros authored Sep 20, 2024
1 parent 8e8a2ce commit 23a4a9a
Show file tree
Hide file tree
Showing 166 changed files with 8,378 additions and 208 deletions.
37 changes: 0 additions & 37 deletions .github/workflows/codeql.yml

This file was deleted.

94 changes: 91 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,46 @@ jobs:
make
echo "size=$(python3 deps/ledger-zxlib/scripts/getSize.py s)" >> $GITHUB_OUTPUT
clippy:
runs-on: ubuntu-latest
container:
image: zondax/rust-ci:latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Cache/restore Cargo dependencies
uses: actions/cache@v3
with:
path: ./app/rust/.cargo
key: ${{ runner.os }}-${{ hashFiles('./Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ github.sha }}
- name: clippy
run: |
cd ./app/rust
cargo clippy --all-targets --features "clippy"
rust_tests:
runs-on: ubuntu-latest
container:
image: zondax/rust-ci:latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Cache/restore Cargo dependencies
uses: actions/cache@v3
with:
path: ./app/rust/.cargo
key: ${{ runner.os }}-${{ hashFiles('./Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ github.sha }}
- name: run rust tests
run: make rust_test

size_nano_s:
needs: build_ledger
runs-on: ubuntu-latest
Expand All @@ -61,9 +101,45 @@ jobs:
echo "LNS app size: ${{needs.build_ledger.outputs.size}} KiB"
[ ${{needs.build_ledger.outputs.size}} -le $NANOS_LIMIT_SIZE ]
tests_zemu_setup:
if: ${{ !contains(toJSON(github.event.commits[*].message), '[skip-zemu]') }}
runs-on: ubuntu-latest
outputs:
tests: ${{ steps.get-tests.outputs.tests }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true

- name: Install node
uses: actions/setup-node@v3

- name: Install yarn
run: npm install -g yarn

- name: Build/Install build js deps
run: |
export PATH=~/.cargo/bin:$PATH
make zemu_install
- name: Get test files
id: get-tests
run: |
cd tests_zemu
TESTS=`yarn test --listTests --json | head -n 3 | tail -n 1 | jq -cM 'to_entries | map({id: .key, file: .value})'`
echo "tests=$TESTS" >> "$GITHUB_OUTPUT"
test_zemu:
needs: [configure, tests_zemu_setup]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
test: ${{ fromJson(needs.tests_zemu_setup.outputs['tests']) }}
steps:
- name: Debug needs context
run: echo "${{ toJson(needs) }}"
- name: Test
run: |
id
Expand All @@ -72,15 +148,27 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
submodules: recursive
- run: sudo apt-get update -y && sudo apt-get install -y libusb-1.0.0 libudev-dev
- name: Install node
uses: actions/setup-node@v3
- name: Install yarn
run: |
npm install -g yarn
- name: Build Ledger app and run tests
run: make test_all
- name: Build Ledger app
run: make PRODUCTION_BUILD=1

- name: Build/Install build js deps
run: make zemu_install

- name: Run zemu tests
run: |
cd tests_zemu
yarn jest ${{ matrix.test.file }} --maxConcurrency 2 \
--reporters='@matteoh2o1999/github-actions-jest-reporter'
env:
FORCE_COLOR: 1

- name: Upload Snapshots (only failure)
if: ${{ failure() }}
uses: actions/upload-artifact@v3
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ scan-build/
tests_zemu/yarn.lock

js/yarn.lock
app/rust/.cargo/registry/
app/rust/target/
app/rust/.cargo/.package-cache
app/rust/.cargo/.package-cache-mutate
app/rust/.cargo/git/
deps/nanopb
15 changes: 3 additions & 12 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
[submodule "deps/nanos-secure-sdk"]
path = deps/nanos-secure-sdk
url = https://github.com/LedgerHQ/ledger-secure-sdk.git
[submodule "deps/picohash"]
path = deps/picohash
url = https://github.com/kazuho/picohash
[submodule "deps/nanopb"]
path = deps/nanopb
url = https://github.com/nanopb/nanopb
[submodule "deps/nanox-secure-sdk"]
path = deps/nanox-secure-sdk
url = https://github.com/LedgerHQ/ledger-secure-sdk.git
[submodule "deps/nanosplus-secure-sdk"]
path = deps/nanosplus-secure-sdk
url = https://github.com/LedgerHQ/ledger-secure-sdk.git
[submodule "deps/ledger-zxlib"]
path = deps/ledger-zxlib
url = https://github.com/Zondax/ledger-zxlib
[submodule "deps/stax-secure-sdk"]
path = deps/stax-secure-sdk
url = https://github.com/LedgerHQ/ledger-secure-sdk.git
[submodule "deps/ledger-rust"]
path = deps/ledger-rust
url = https://github.com/Zondax/ledger-rust
9 changes: 8 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
cmake-build-debug/
\.idea/workspace\.xml
\.idea/

src/ledger/bin/
src/ledger/debug/
src/ledger/obj/
obj/app\.elf
debug/app\.map
debug/app\.asm
bin/app\.hex
bin/app\.elf
glyphs/glyphs\.h
glyphs/glyphs\.c
obj/
Expand Down
59 changes: 53 additions & 6 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,26 @@ APP_LOAD_PARAMS = --curve secp256k1 $(COMMON_LOAD_PARAMS) --path $(APPPATH)

APP_STACK_MIN_SIZE := 2050
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices
RUST_TARGET := thumbv6m-none-eabi

$(info TARGET_NAME = [$(TARGET_NAME)])
$(info ICONNAME = [$(ICONNAME)])

# Enable bls signature verification for all targets except TARGET_NANOS and TARGET_NANOX
ifneq ($(TARGET_NAME),TARGET_NANOS)
ifneq ($(TARGET_NAME),TARGET_NANOX)
BLS_SIGNATURE = 1
DEFINES += BLS_SIGNATURE=$(BLS_SIGNATURE)
endif
endif

RUST_TARGET_NAME := $(TARGET_NAME)

NBGL_TARGETS := TARGET_STAX TARGET_FLEX
ifeq ($(TARGET_NAME),$(filter $(TARGET_NAME), $(NBGL_TARGETS)))
RUST_TARGET_NAME := TARGET_STAX
endif

ifndef ICONNAME
$(error ICONNAME is not set)
endif
Expand All @@ -82,24 +98,55 @@ include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
CFLAGS += -Wvla -Wno-implicit-fallthrough
CFLAGS += -I$(MY_DIR)/../deps/tinycbor/src
APP_SOURCE_PATH += $(MY_DIR)/../deps/tinycbor-ledger
LDFLAGS += -z muldefs

ifdef BLS_SIGNATURE
LDLIBS += -Lrust/target/$(RUST_TARGET)/release -lrslib
endif

APP_SOURCE_PATH += $(MY_DIR)/rust/include

CFLAGS += -I$(MY_DIR)/../deps/nanopb/
APP_SOURCE_PATH += $(MY_DIR)/../deps/nanopb_tiny/


# Check if BLS_SIGNATURE is set
ifdef BLS_SIGNATURE

APP_CUSTOM_LINK_DEPENDENCIES = rust

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

# Before linking, we need to be sure rust lib is there
bin/app.elf: rust
.PHONY: rust_clean
rust_clean:
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo clean

clean: rust_clean

.PHONY: rust_tests
rust_tests:
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo test
else
# Empty rules when BLS_SIGNATURE is not set
.PHONY: rust
rust:
@echo "Skipping Rust build (BLS_SIGNATURE not set)"

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

.PHONY: rust_tests
rust_tests:
@echo "Skipping Rust tests (BLS_SIGNATURE not set)"

endif

ifdef BLS_SIGNATURE
clean: rust_clean
endif

include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.side_loading

Expand Down
4 changes: 2 additions & 2 deletions app/Makefile.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the major version of this release
APPVERSION_M=3
# This is the minor version of this release
APPVERSION_N=1
APPVERSION_N=2
# This is the patch version of this release
APPVERSION_P=1
APPVERSION_P=0
23 changes: 23 additions & 0 deletions app/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

[unstable]
build-std = ["core"]
build-std-features = ["panic_immediate_abort"]
target-applies-to-host = true
host-config = true

[host]
rustflags = ["-Ctarget-feature=-crt-static"]

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = [
"--emit",
"asm",
"-C",
"relocation-model=ropi",
"-C",
"link-arg=-nostartfiles",
"-C",
"link-arg=-Tlink.ld",
"-C",
"inline-threshold=0",
]
Loading

0 comments on commit 23a4a9a

Please sign in to comment.