Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support test HWMs #46

Merged
merged 25 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e9b9442
Add nix/env.sh and show what app is being built in Makefile
3noch Feb 1, 2019
29c012e
Add watch script
3noch Feb 1, 2019
0474685
Improve install scripts
3noch Feb 1, 2019
8f9eb5d
Automatically detect NVRAM data size during install
3noch Feb 1, 2019
bd7f8b2
Clean up test/apdu.sh
3noch Feb 1, 2019
2a650cb
Change copy_string for reasons
3noch Feb 1, 2019
ebe6592
Clean up headers
3noch Feb 1, 2019
c7a3bb5
Centralize APDU instruction table
3noch Feb 1, 2019
1091eb2
Move throw_stack_size to globals.h
3noch Feb 1, 2019
160d6f3
Add some helpful debug macros
3noch Feb 1, 2019
5f3d29d
Refactor to use bip32_path struct
3noch Feb 1, 2019
7da20af
Clean up headers
3noch Feb 1, 2019
ac036ba
Add setup APDU
3noch Feb 1, 2019
544523e
Use G for globals in apdu_pubkey.c
3noch Feb 1, 2019
516c2a2
Use G for globals in apdu_baking.c
3noch Feb 1, 2019
665a3f2
Factor out provide_pubkey
3noch Feb 1, 2019
e1f875b
Use safer abstraction for pubkey generation
3noch Feb 1, 2019
f11b103
Add new APDU for querying both HWMs and chain ID
3noch Feb 1, 2019
ee3639e
Fix HWM updates on baking idle screens
3noch Feb 1, 2019
971970e
Use G for globals in ui.c
3noch Feb 1, 2019
6e90ed4
Show chain ID with alias on baking idle screens
3noch Feb 1, 2019
a1051eb
Move baking idle screen globals to better place
3noch Feb 1, 2019
4a40251
Show chain ID aliases in setup prompts
3noch Feb 1, 2019
9d53371
Add clarifications to some string size constants
3noch Feb 1, 2019
cc7e789
Update baking idle screens any time NVRAM changes
3noch Feb 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ ifeq ($(COMMIT),)
endif

ICONNAME=icon.gif

################
# Default rule #
################
all: default
all: show-app default


.PHONY: show-app
show-app:
@echo ">>>>> Building $(APP) at commit $(COMMIT)"


############
# Platform #
Expand Down
11 changes: 10 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let
export APP='${if bakingApp then "tezos_baking" else "tezos_wallet"}'
export COMMIT='${commit}'
make clean
make
make all
EOF

mkdir -p "$out"
Expand All @@ -45,4 +45,13 @@ in rec {
cp '${baking}/bin/app.hex' baking.hex
tar czf "$out" wallet.hex baking.hex
'';

# Script that places you in the environment to run `make`, etc.
env-shell = pkgs.writeScriptBin "env-shell" ''
#!${pkgs.stdenv.shell}
export BOLOS_SDK='${bolosSdk}'
export BOLOS_ENV='${bolosEnv}'
export COMMIT='${commit}'
exec '${fhs}/bin/enter-fhs'
'';
}
19 changes: 10 additions & 9 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
#!/bin/sh
set -eux
#!/usr/bin/env bash
set -Eeuo pipefail

rootdir="$(cd "$(dirname "$0")"/; pwd)"
root="$(git rev-parse --show-toplevel)"

app_name=Tezos
if [ "${1:-}X" != X ]; then
app_name="$1"
fi

app_file=$rootdir/bin/app.hex
app_dir="$root"
if [ "${2:-}X" != X ]; then
app_file="$2"
app_dir="$2"
fi

if [ "${3:-}X" = X ]; then
version="$(git -C "$rootdir" describe --tags | cut -f1 -d- | cut -f2 -dv)"
version="$(git -C "$root" describe --tags | cut -f1 -d- | cut -f2 -dv)"
else
version="$3"
fi


set -x
python -m ledgerblue.loadApp \
--appFlags 0x00 \
--dataSize 0x80 \
--dataSize "$(grep _nvram_data_size "$app_dir/debug/app.map" | tr -s ' ' | cut -f2 -d' ')" \
--tlv \
--curve ed25519 \
--curve secp256k1 \
--curve prime256r1 \
--targetId "${TARGET_ID:-0x31100004}" \
--delete \
--path 44"'"/1729"'" \
--fileName "$app_file" \
--fileName "$app_dir/bin/app.hex" \
--appName "$app_name" \
--appVersion "$version" \
--icon "$(cat "$rootdir/dist/icon.hex")" \
--icon "$(cat "$root/dist/icon.hex")" \
--targetVersion ""
15 changes: 15 additions & 0 deletions nix/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

commit=$(git describe --abbrev=8 --always 2>/dev/null)
echo >&2 "Git commit: $commit"
shell_dir="$(nix-build -A env-shell --no-out-link --argstr commit "$commit" "${NIX_BUILD_ARGS:-}")"
shell="$shell_dir/bin/env-shell"

if [ $# -eq 0 ]; then
echo >&2 "Entering via $shell"
exec "$shell"
else
exec "$shell" <<EOF
$@
EOF
fi
15 changes: 8 additions & 7 deletions nix/install.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#!/usr/bin/env bash
set -Eeuxo pipefail
set -Eeuo pipefail

rootdir=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
root="$(git rev-parse --show-toplevel)"

: "${VERSION:=${2:-"$(git -C "$rootdir" describe --tags | cut -f1 -d- | cut -f2 -dv)"}}"
: "${VERSION:=${2:-"$(git -C "$root" describe --tags | cut -f1 -d- | cut -f2 -dv)"}}"

install-wallet() {
"$rootdir/install.sh" 'Tezos Wallet' "$("$rootdir/nix/build.sh" -A wallet)/bin/app.hex" "$VERSION"
"$root/install.sh" 'Tezos Wallet' "$("$root/nix/build.sh" -A wallet)" "$VERSION"
}
install-baking() {
"$rootdir/install.sh" 'Tezos Baking' "$("$rootdir/nix/build.sh" -A baking)/bin/app.hex" "$VERSION"
"$root/install.sh" 'Tezos Baking' "$("$root/nix/build.sh" -A baking)" "$VERSION"
}

export rootdir
export root
export VERSION
export -f install-wallet
export -f install-baking

nix-shell "$rootdir/nix/ledgerblue.nix" -A shell --run "$(cat <<EOF
nix-shell "$root/nix/ledgerblue.nix" -A shell --run "$(cat <<EOF
set -Eeuo pipefail
if [ "${1:-}" = "wallet" ]; then
install-wallet
elif [ "${1:-}" = "baking" ]; then
Expand Down
23 changes: 23 additions & 0 deletions nix/watch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -uo pipefail

root="$(git rev-parse --show-toplevel)"

fail() { unset ___empty; : "${___empty:?$1}"; }

[ -z "${1:-}" ] && fail "No command given; try running $0 make"

watchdirs=("$root/default.nix" "$root/nix" "$root/Makefile" "$root/src")

inotifywait="$(nix-build '<nixpkgs>' -A inotify-tools --no-out-link)/bin/inotifywait"
while true; do
"$root/nix/env.sh" <<EOF
$@
EOF
if ! "$inotifywait" -qre close_write "${watchdirs[@]}"; then
fail "inotifywait failed"
fi
echo "----------------------"
echo
done
12 changes: 11 additions & 1 deletion src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#include <stdint.h>
#include <string.h>

size_t provide_pubkey(uint8_t *const io_buffer, cx_ecfp_public_key_t const *const pubkey) {
size_t tx = 0;
io_buffer[tx++] = pubkey->W_len;
memmove(io_buffer + tx, pubkey->W, pubkey->W_len);
tx += pubkey->W_len;
io_buffer[tx++] = 0x90;
io_buffer[tx++] = 0x00;
return tx;
}

unsigned int handle_apdu_error(uint8_t __attribute__((unused)) instruction) {
THROW(EXC_INVALID_INS);
}
Expand Down Expand Up @@ -55,7 +65,7 @@ void main_loop(apdu_handler handlers[INS_MAX]) {
THROW(EXC_WRONG_LENGTH);
}

uint8_t instruction = G_io_apdu_buffer[OFFSET_INS];
const uint8_t instruction = G_io_apdu_buffer[OFFSET_INS];
const apdu_handler cb = (instruction >= INS_MAX)
? handle_apdu_error
: handlers[instruction];
Expand Down
31 changes: 18 additions & 13 deletions src/apdu.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include "exception.h"
#include "globals.h"
#include "keys.h"
#include "types.h"
#include "ui.h"

// Order matters
#include "os.h"

#include <stdbool.h>
Expand All @@ -16,14 +15,25 @@
#endif

#define OFFSET_CLA 0
#define OFFSET_INS 1
#define OFFSET_P1 2
#define OFFSET_INS 1 // instruction code
#define OFFSET_P1 2 // user-defined 1-byte parameter
#define OFFSET_CURVE 3
#define OFFSET_LC 4
#define OFFSET_CDATA 5
#define OFFSET_LC 4 // length of CDATA
#define OFFSET_CDATA 5 // payload

// Instruction codes
#define INS_VERSION 0x00
#define INS_AUTHORIZE_BAKING 0x01
#define INS_GET_PUBLIC_KEY 0x02
#define INS_PROMPT_PUBLIC_KEY 0x03
#define INS_SIGN 0x04
#define INS_SIGN_UNSAFE 0x05 // Data that is already hashed.
#define INS_RESET 0x06
#define INS_QUERY_AUTH_KEY 0x07
#define INS_QUERY_MAIN_HWM 0x08
#define INS_GIT 0x09
#define INS_SETUP 0x0A
#define INS_QUERY_ALL_HWM 0x0B

__attribute__((noreturn))
void main_loop(apdu_handler handlers[INS_MAX]);
Expand All @@ -50,13 +60,8 @@ static inline void require_hid(void) {
}
}

size_t provide_pubkey(uint8_t *const io_buffer, cx_ecfp_public_key_t const *const pubkey);

uint32_t handle_apdu_error(uint8_t instruction);
uint32_t handle_apdu_version(uint8_t instruction);
uint32_t handle_apdu_git(uint8_t instruction);

static inline void throw_stack_size() {
uint8_t st;
// uint32_t tmp1 = (uint32_t)&st - (uint32_t)&app_stack_canary;
uint32_t tmp2 = (uint32_t)global.stack_root - (uint32_t)&st;
THROW(0x9000 + tmp2);
}
47 changes: 28 additions & 19 deletions src/apdu_baking.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#include "apdu.h"
#include "baking_auth.h"
#include "globals.h"
#include "os_cx.h"
#include "protocol.h"
#include "to_string.h"
#include "ui_prompt.h"

// Order matters
#include "cx.h"
#include "os.h"

#include <string.h>

#define G global.u.baking
3noch marked this conversation as resolved.
Show resolved Hide resolved

static bool reset_ok(void);

unsigned int handle_apdu_reset(__attribute__((unused)) uint8_t instruction) {
Expand All @@ -21,15 +20,12 @@ unsigned int handle_apdu_reset(__attribute__((unused)) uint8_t instruction) {
if (dataLength != sizeof(int)) {
THROW(EXC_WRONG_LENGTH_FOR_INS);
}
level_t lvl = READ_UNALIGNED_BIG_ENDIAN(level_t, dataBuffer);

if (!is_valid_level(lvl)) {
THROW(EXC_PARSE_ERROR);
}
level_t const lvl = READ_UNALIGNED_BIG_ENDIAN(level_t, dataBuffer);
if (!is_valid_level(lvl)) THROW(EXC_PARSE_ERROR);

global.u.baking.reset_level = lvl;
G.reset_level = lvl;

register_ui_callback(0, number_to_string_indirect32, &global.u.baking.reset_level);
register_ui_callback(0, number_to_string_indirect32, &G.reset_level);

static const char *const reset_prompts[] = {
PROMPT("Reset HWM"),
Expand All @@ -39,7 +35,12 @@ unsigned int handle_apdu_reset(__attribute__((unused)) uint8_t instruction) {
}

bool reset_ok(void) {
write_highest_level(global.u.baking.reset_level, false); // We have not yet had an endorsement at this level
UPDATE_NVRAM(ram, {
ram->hwm.main.highest_level = G.reset_level;
ram->hwm.main.had_endorsement = false;
ram->hwm.test.highest_level = G.reset_level;
ram->hwm.test.had_endorsement = false;
});

uint32_t tx = 0;
G_io_apdu_buffer[tx++] = 0x90;
Expand All @@ -64,25 +65,33 @@ uint32_t send_word_big_endian(uint32_t tx, uint32_t word) {
return tx + i;
}

unsigned int handle_apdu_hwm(__attribute__((unused)) uint8_t instruction) {
unsigned int handle_apdu_all_hwm(__attribute__((unused)) uint8_t instruction) {
uint32_t tx = 0;
tx = send_word_big_endian(tx, N_data.hwm.main.highest_level);
tx = send_word_big_endian(tx, N_data.hwm.test.highest_level);
tx = send_word_big_endian(tx, N_data.main_chain_id.v);
G_io_apdu_buffer[tx++] = 0x90;
G_io_apdu_buffer[tx++] = 0x00;
return tx;
}

level_t level = N_data.highest_level;
tx = send_word_big_endian(tx, level);

unsigned int handle_apdu_main_hwm(__attribute__((unused)) uint8_t instruction) {
uint32_t tx = 0;
tx = send_word_big_endian(tx, N_data.hwm.main.highest_level);
G_io_apdu_buffer[tx++] = 0x90;
G_io_apdu_buffer[tx++] = 0x00;
return tx;
}


unsigned int handle_apdu_query_auth_key(__attribute__((unused)) uint8_t instruction) {
uint32_t tx = 0;
uint8_t const length = N_data.bip32_path.length;

uint8_t length = N_data.path_length;
uint32_t tx = 0;
G_io_apdu_buffer[tx++] = length;

for (uint8_t i = 0; i < length; ++i) {
tx = send_word_big_endian(tx, N_data.bip32_path[i]);
tx = send_word_big_endian(tx, N_data.bip32_path.components[i]);
}

G_io_apdu_buffer[tx++] = 0x90;
Expand Down
7 changes: 2 additions & 5 deletions src/apdu_baking.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

#include <stdint.h>

#define INS_RESET 0x06
#define INS_QUERY_AUTH_KEY 0x07
#define INS_QUERY_HWM 0x08

unsigned int handle_apdu_reset(uint8_t instruction);
unsigned int handle_apdu_query_auth_key(uint8_t instruction);
unsigned int handle_apdu_hwm(uint8_t instruction);
unsigned int handle_apdu_main_hwm(uint8_t instruction);
unsigned int handle_apdu_all_hwm(uint8_t instruction);
Loading