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

Remove non-baking code (behind #ifdef) #42

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion .github/workflows/build_and_functional_tests_baking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
uses: LedgerHQ/ledger-app-workflows/.github/workflows/reusable_build.yml@v1
with:
upload_app_binaries_artifact: "compiled_app_binaries"
flags: "APP=tezos_baking"
spalmer25 marked this conversation as resolved.
Show resolved Hide resolved

ragger_tests:
name: Run ragger tests using the reusable workflow
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ all: show-app default

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


############
Expand Down Expand Up @@ -131,7 +131,7 @@ endif

CC := $(CLANGPATH)clang

CFLAGS += -DBAKING_APP -O3 -Os -Wall -Wextra -Wno-incompatible-pointer-types-discards-qualifiers
CFLAGS += -O3 -Os -Wall -Wextra -Wno-incompatible-pointer-types-discards-qualifiers

AS := $(GCCPATH)arm-none-eabi-gcc

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Tezos Ledger Applications

## Investigation

### Building

Use the docker container (built by `make docker_ledger_app_builder` in the wallet app repo.)

```
docker run --rm -ti -v $(pwd):/app ledger-app-builder:latest
```

Then build the baking app:

```
BOLOS_SDK=$NANOS_SDK make
```

You can replace `NANOS` with `NANOSP`, `NANOX`, `STAX` for the other devices.

## Overview

Whether you're baking or just trading XTZ, you want to store your keys securely.
Expand Down
4 changes: 0 additions & 4 deletions src/apdu_baking.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef BAKING_APP

#include "apdu_baking.h"

#include "apdu.h"
Expand Down Expand Up @@ -115,5 +113,3 @@ size_t handle_apdu_deauthorize(__attribute__((unused)) uint8_t instruction,

return finalize_successful_send(0);
}

#endif // #ifdef BAKING_APP
4 changes: 0 additions & 4 deletions src/apdu_baking.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#ifdef BAKING_APP

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Expand All @@ -14,5 +12,3 @@ size_t handle_apdu_all_hwm(uint8_t instruction, volatile uint32_t* flags);
size_t handle_apdu_deauthorize(uint8_t instruction, volatile uint32_t* flags);
void ui_baking_reset(volatile uint32_t* flags);
bool reset_ok(void);

#endif // #ifdef BAKING_APP
4 changes: 0 additions & 4 deletions src/apdu_hmac.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef BAKING_APP

#include "apdu_hmac.h"

#include "globals.h"
Expand Down Expand Up @@ -89,5 +87,3 @@ size_t handle_apdu_hmac(__attribute__((unused)) uint8_t instruction,
tx += hmac_size;
return finalize_successful_send(tx);
}

#endif // #ifdef BAKING_APP
14 changes: 0 additions & 14 deletions src/apdu_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#include "protocol.h"
#include "to_string.h"
#include "ui.h"
#ifdef BAKING_APP
#include "baking_auth.h"
#endif // BAKING_APP

#include <string.h>

Expand All @@ -22,15 +20,12 @@ static bool pubkey_ok(void) {
return true;
}

#ifdef BAKING_APP
static bool baking_ok(void) {
authorize_baking(global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path);
pubkey_ok();
return true;
}
#endif

#ifdef BAKING_APP
char const *const *get_baking_prompts() {
static const char *const baking_prompts[] = {
PROMPT("Authorize Baking"),
Expand All @@ -39,7 +34,6 @@ char const *const *get_baking_prompts() {
};
return baking_prompts;
}
#endif

size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags) {
uint8_t *dataBuffer = G_io_apdu_buffer + OFFSET_CDATA;
Expand All @@ -53,16 +47,12 @@ size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags)

size_t const cdata_size = G_io_apdu_buffer[OFFSET_LC];

#ifdef BAKING_APP
if (cdata_size == 0 && instruction == INS_AUTHORIZE_BAKING) {
copy_bip32_path_with_curve(&global.path_with_curve, &N_data.baking_key);
} else {
#endif
read_bip32_path(&global.path_with_curve.bip32_path, dataBuffer, cdata_size);
#ifdef BAKING_APP
if (global.path_with_curve.bip32_path.length == 0) THROW(EXC_WRONG_LENGTH_FOR_INS);
}
#endif

cx_ecfp_public_key_t public_key = {0};
generate_public_key(&public_key,
Expand All @@ -75,18 +65,14 @@ size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t *flags)
// instruction == INS_PROMPT_PUBLIC_KEY || instruction == INS_AUTHORIZE_BAKING
ui_callback_t cb;
bool bake;
#ifdef BAKING_APP
if (instruction == INS_AUTHORIZE_BAKING) {
cb = baking_ok;
bake = true;
} else {
#endif
// INS_PROMPT_PUBLIC_KEY
cb = pubkey_ok;
bake = false;
#ifdef BAKING_APP
}
#endif
prompt_address(bake, cb, delay_reject);
*flags = IO_ASYNCH_REPLY;
return 0;
Expand Down
8 changes: 1 addition & 7 deletions src/apdu_pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@

size_t handle_apdu_get_public_key(uint8_t instruction, volatile uint32_t* flags);

void prompt_address(
#ifndef BAKING_APP
__attribute__((unused))
#endif
bool baking,
ui_callback_t ok_cb,
ui_callback_t cxl_cb);
void prompt_address(bool baking, ui_callback_t ok_cb, ui_callback_t cxl_cb);
4 changes: 0 additions & 4 deletions src/apdu_setup.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef BAKING_APP

#include "apdu_setup.h"

#include "apdu.h"
Expand Down Expand Up @@ -80,5 +78,3 @@ size_t handle_apdu_setup(__attribute__((unused)) uint8_t instruction, volatile u
*flags = IO_ASYNCH_REPLY;
return 0;
}

#endif // #ifdef BAKING_APP
3 changes: 0 additions & 3 deletions src/apdu_setup.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#ifdef BAKING_APP

#include "apdu.h"

#include <stddef.h>
Expand All @@ -10,4 +8,3 @@
size_t handle_apdu_setup(uint8_t instruction, volatile uint32_t* flags);

void prompt_setup(ui_callback_t const ok_cb, ui_callback_t const cxl_cb);
#endif // #ifdef BAKING_APP
83 changes: 1 addition & 82 deletions src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ static inline void clear_data(void) {
memset(&G, 0, sizeof(G));
}

#ifdef BAKING_APP
static bool sign_without_hash_ok(void) {
delayed_send(perform_signature(true, false));
return true;
Expand All @@ -89,7 +88,6 @@ static bool sign_reject(void) {
delay_reject();
return true; // Return to idle
}
#endif

static bool is_operation_allowed(enum operation_tag tag) {
switch (tag) {
Expand All @@ -101,26 +99,11 @@ static bool is_operation_allowed(enum operation_tag tag) {
return true;
case OPERATION_TAG_BABYLON_REVEAL:
return true;
#ifndef BAKING_APP
case OPERATION_TAG_PROPOSAL:
return true;
case OPERATION_TAG_BALLOT:
return true;
case OPERATION_TAG_ATHENS_ORIGINATION:
return true;
case OPERATION_TAG_ATHENS_TRANSACTION:
return true;
case OPERATION_TAG_BABYLON_ORIGINATION:
return true;
case OPERATION_TAG_BABYLON_TRANSACTION:
return true;
#endif
default:
return false;
}
}

#ifdef BAKING_APP
static bool parse_allowed_operations(struct parsed_operation_group *const out,
uint8_t const *const in,
size_t const in_size,
Expand All @@ -133,18 +116,6 @@ static bool parse_allowed_operations(struct parsed_operation_group *const out,
&is_operation_allowed);
}

#else

static bool parse_allowed_operation_packet(struct parsed_operation_group *const out,
uint8_t const *const in,
size_t const in_size) {
return parse_operations_packet(out, in, in_size, &is_operation_allowed);
}

#endif

#ifdef BAKING_APP // ----------------------------------------------------------

size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) {
switch (G.magic_byte) {
case MAGIC_BYTE_TENDERBAKE_BLOCK:
Expand Down Expand Up @@ -198,7 +169,6 @@ size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) {
PARSE_ERROR();
}
}
#endif // ifdef BAKING_APP ----------------------------------------------------

#define P1_FIRST 0x00
#define P1_NEXT 0x01
Expand All @@ -208,17 +178,12 @@ size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) {
static uint8_t get_magic_byte_or_throw(uint8_t const *const buff, size_t const buff_size) {
uint8_t const magic_byte = get_magic_byte(buff, buff_size);
switch (magic_byte) {
#ifdef BAKING_APP
case MAGIC_BYTE_TENDERBAKE_BLOCK:
case MAGIC_BYTE_TENDERBAKE_PREENDORSEMENT:
case MAGIC_BYTE_TENDERBAKE_ENDORSEMENT:
case MAGIC_BYTE_BLOCK:
case MAGIC_BYTE_BAKING_OP:
case MAGIC_BYTE_UNSAFE_OP: // Only for self-delegations
#else
case MAGIC_BYTE_UNSAFE_OP:
case MAGIC_BYTE_UNSAFE_OP3:
#endif
return magic_byte;

case MAGIC_BYTE_UNSAFE_OP2:
Expand All @@ -244,12 +209,6 @@ static size_t handle_apdu(bool const enable_hashing,
global.path_with_curve.derivation_type =
parse_derivation_type(G_io_apdu_buffer[OFFSET_CURVE]);
return finalize_successful_send(0);
#ifndef BAKING_APP
case P1_HASH_ONLY_NEXT:
// This is a debugging Easter egg
G.hash_only = true;
__attribute__((fallthrough));
#endif
case P1_NEXT:
if (global.path_with_curve.bip32_path.length == 0) THROW(EXC_WRONG_LENGTH_FOR_INS);

Expand All @@ -263,7 +222,6 @@ static size_t handle_apdu(bool const enable_hashing,
}

if (enable_parsing) {
#ifdef BAKING_APP
if (G.packet_index != 1) PARSE_ERROR(); // Only parse a single packet when baking

G.magic_byte = get_magic_byte_or_throw(buff, buff_size);
Expand All @@ -275,32 +233,6 @@ static size_t handle_apdu(bool const enable_hashing,
// This should be a baking operation so parse it.
if (!parse_baking_data(&G.parsed_baking_data, buff, buff_size)) PARSE_ERROR();
}
#else
if (G.packet_index == 1) {
G.maybe_ops.is_valid = false;
G.magic_byte = get_magic_byte_or_throw(buff, buff_size);

// If it is an "operation" (starting with the 0x03 magic byte), set up parsing
// If it is arbitrary Michelson (starting with 0x05), dont bother parsing and show
// the "Sign Hash" prompt
if (G.magic_byte == MAGIC_BYTE_UNSAFE_OP) {
parse_operations_init(&G.maybe_ops.v,
global.path_with_curve.derivation_type,
&global.path_with_curve.bip32_path,
&G.parse_state);
}
// If magic byte is not 0x03 or 0x05, fail
else if (G.magic_byte != MAGIC_BYTE_UNSAFE_OP3) {
PARSE_ERROR();
}
}

// Only parse if the message is an "Operation"
if (G.magic_byte == MAGIC_BYTE_UNSAFE_OP) {
parse_allowed_operation_packet(&G.maybe_ops.v, buff, buff_size);
}

#endif
}

if (enable_hashing) {
Expand Down Expand Up @@ -333,12 +265,7 @@ static size_t handle_apdu(bool const enable_hashing,

G.maybe_ops.is_valid = parse_operations_final(&G.parse_state, &G.maybe_ops.v);

return
#ifdef BAKING_APP
baking_sign_complete(instruction == INS_SIGN_WITH_HASH, flags);
#else
wallet_sign_complete(instruction, G.magic_byte, flags);
#endif
return baking_sign_complete(instruction == INS_SIGN_WITH_HASH, flags);
} else {
return finalize_successful_send(0);
}
Expand All @@ -357,15 +284,7 @@ size_t handle_apdu_sign_with_hash(uint8_t instruction, volatile uint32_t *flags)
}

int perform_signature(bool const on_hash, bool const send_hash) {
#ifdef BAKING_APP
write_high_water_mark(&G.parsed_baking_data);
#else
if (on_hash && G.hash_only) {
memcpy(G_io_apdu_buffer, G.final_hash, sizeof(G.final_hash));
clear_data();
return finalize_successful_send(sizeof(G.final_hash));
}
#endif

size_t tx = 0;
if (send_hash && on_hash) {
Expand Down
11 changes: 0 additions & 11 deletions src/apdu_sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
size_t handle_apdu_sign(uint8_t instruction, volatile uint32_t* flags);
size_t handle_apdu_sign_with_hash(uint8_t instruction, volatile uint32_t* flags);

#ifdef BAKING_APP // ----------------------------------------------------------

void prompt_register_delegate(ui_callback_t const ok_cb, ui_callback_t const cxl_cb);

#else // ifdef BAKING_APP -----------------------------------------------------
bool prompt_transaction(struct parsed_operation_group const* const ops,
bip32_path_with_curve_t const* const key,
ui_callback_t ok,
ui_callback_t cxl);

size_t wallet_sign_complete(uint8_t instruction, uint8_t magic_byte, volatile uint32_t* flags);
#endif // ifdef BAKING_APP ----------------------------------------------------

int perform_signature(bool const on_hash, bool const send_hash);
4 changes: 0 additions & 4 deletions src/baking_auth.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef BAKING_APP

#include "baking_auth.h"

#include "apdu.h"
Expand Down Expand Up @@ -230,5 +228,3 @@ bool parse_baking_data(parsed_baking_data_t *const out,
return false;
}
}

#endif // #ifdef BAKING_APP
Loading
Loading