Skip to content

Commit

Permalink
remove features from Nano S
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Nov 29, 2023
1 parent b42825b commit 7f80101
Show file tree
Hide file tree
Showing 47 changed files with 429 additions and 73 deletions.
42 changes: 40 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#*******************************************************************************

APPNAME = "Cardano ADA"

APPVERSION_M = 6
APPVERSION_N = 1
APPVERSION_P = 2
Expand Down Expand Up @@ -63,8 +64,6 @@ DEFINES += HAVE_SPRINTF HAVE_SNPRINTF_FORMAT_U
DEFINES += APPVERSION=\"$(APPVERSION)\"
DEFINES += MAJOR_VERSION=$(APPVERSION_M) MINOR_VERSION=$(APPVERSION_N) PATCH_VERSION=$(APPVERSION_P)

DEFINES += UNUSED\(x\)=\(void\)x

## USB HID?
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=4 IO_HID_EP_LENGTH=64 HAVE_USB_APDU

Expand Down Expand Up @@ -122,6 +121,29 @@ else
DEFINES += PRINTF\(...\)=
endif

# restricted features for Nano S
# but not in DEVEL mode where we usually want to test all features with HEADLESS
ifeq ($(TARGET_NAME), TARGET_NANOS)
ifneq ($(DEVEL), 1)
APP_XS = 1
endif
else
APP_XS = 0
endif

ifeq ($(APP_XS), 1)
DEFINES += APP_XS
else
# features not included in the Nano S app
DEFINES += APP_FEATURE_OPCERT
DEFINES += APP_FEATURE_NATIVE_SCRIPT_HASH
DEFINES += APP_FEATURE_POOL_REGISTRATION
DEFINES += APP_FEATURE_POOL_RETIREMENT
DEFINES += APP_FEATURE_BYRON_ADDRESS_DERIVATION
DEFINES += APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
endif
# always include this, it's important for Plutus users
DEFINES += APP_FEATURE_TOKEN_MINTING

##################
# Dependencies #
Expand Down Expand Up @@ -203,3 +225,19 @@ format:

size: all
$(GCCPATH)arm-none-eabi-size --format=gnu bin/app.elf

##############
# Device-specific builds
##############

nanos: clean
BOLOS_SDK=$(NANOS_SDK) make

nanosp: clean
BOLOS_SDK=$(NANOSP_SDK) make

nanox: clean
BOLOS_SDK=$(NANOX_SDK) make

stax: clean
BOLOS_SDK=$(STAX_SDK) make
2 changes: 1 addition & 1 deletion doc/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Install Docker
- Pull the required containers as discussed in https://github.com/LedgerHQ/ledger-app-builder/ (lite container is sufficient for a C build):

`sudo docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder-lite:latest`
`docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder-lite:latest`

## Compiling the app

Expand Down
11 changes: 11 additions & 0 deletions doc/features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Features (not) available on specific Ledger devices

Nano S has a very limited space for storing applications. It is not enough to fit all Cardano features there, so some of them are only available on Nano S+ and other more spacious Ledger devices (e.g. Nano X and Stax).

The features not supported on Nano S, Cardano app version 7 and above:
* pool registration and retirement
* signing of operational certificates
* computation of native script hashes
* details in Byron change outputs (only the address is shown)

Details can be found in [Makefile](../Makefile) and in the code (search for compilation flags beginning with `APP_FEATURE_`).
9 changes: 9 additions & 0 deletions fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ add_compile_definitions(
HAVE_HASH
HAVE_SHA256
HAVE_SHA3

# include all app features, incl. those removed from Nano S
APP_FEATURE_OPCERT
APP_FEATURE_NATIVE_SCRIPT_HASH
APP_FEATURE_POOL_REGISTRATION
APP_FEATURE_POOL_RETIREMENT
APP_FEATURE_BYRON_ADDRESS_DERIVATION
APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
APP_FEATURE_TOKEN_MINTING
)

set(SOURCE
Expand Down
109 changes: 60 additions & 49 deletions src/addressUtilsByron.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
#include "crc32.h"
#include "bufView.h"

#if defined(APP_FEATURE_BYRON_ADDRESS_DERIVATION) || defined(APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK)

static const size_t ADDRESS_ROOT_SIZE = 28;
static const size_t PROTOCOL_MAGIC_ADDRESS_ATTRIBUTE_KEY = 2;

#endif

#ifdef APP_FEATURE_BYRON_ADDRESS_DERIVATION

enum {
CARDANO_ADDRESS_TYPE_PUBKEY = 0,
/*
Expand All @@ -15,9 +24,6 @@ enum {
*/
};

static const size_t ADDRESS_ROOT_SIZE = 28;
static const size_t PROTOCOL_MAGIC_ADDRESS_ATTRIBUTE_KEY = 2;

void addressRootFromExtPubKey(
const extendedPublicKey_t* extPubKey,
uint8_t* outBuffer, size_t outSize
Expand Down Expand Up @@ -136,6 +142,56 @@ size_t cborPackRawAddressWithChecksum(
return view_processedSize(&output);
}

size_t deriveRawAddress(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t addressRoot[28] = {0};
{
extendedPublicKey_t extPubKey;

deriveExtendedPublicKey(pathSpec, &extPubKey);

addressRootFromExtPubKey(
&extPubKey,
addressRoot, SIZEOF(addressRoot)
);
}

return cborEncodePubkeyAddressInner(
addressRoot, SIZEOF(addressRoot),
protocolMagic,
outBuffer, outSize
);
}

size_t deriveAddress_byron(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t rawAddressBuffer[40] = {0};
size_t rawAddressSize = deriveRawAddress(
pathSpec, protocolMagic,
rawAddressBuffer, SIZEOF(rawAddressBuffer)
);

return cborPackRawAddressWithChecksum(
rawAddressBuffer, rawAddressSize,
outBuffer, outSize
);

}

#endif // APP_FEATURE_BYRON_ADDRESS_DERIVATION

#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK

static uint64_t parseToken(read_view_t* view, uint8_t type)
{
const cbor_token_t token = view_parseToken(view);
Expand Down Expand Up @@ -166,7 +222,6 @@ static size_t parseBytesSizeToken(read_view_t* view)
return parsedSizeDowncasted;
}


uint32_t extractProtocolMagic(
const uint8_t* addressBuffer, size_t addressSize
)
Expand Down Expand Up @@ -252,48 +307,4 @@ uint32_t extractProtocolMagic(
return protocolMagic;
}

size_t deriveRawAddress(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t addressRoot[28] = {0};
{
extendedPublicKey_t extPubKey;

deriveExtendedPublicKey(pathSpec, &extPubKey);

addressRootFromExtPubKey(
&extPubKey,
addressRoot, SIZEOF(addressRoot)
);
}

return cborEncodePubkeyAddressInner(
addressRoot, SIZEOF(addressRoot),
protocolMagic,
outBuffer, outSize
);
}

size_t deriveAddress_byron(
const bip44_path_t* pathSpec, uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

uint8_t rawAddressBuffer[40] = {0};
size_t rawAddressSize = deriveRawAddress(
pathSpec, protocolMagic,
rawAddressBuffer, SIZEOF(rawAddressBuffer)
);

return cborPackRawAddressWithChecksum(
rawAddressBuffer, rawAddressSize,
outBuffer, outSize
);

}
#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
10 changes: 9 additions & 1 deletion src/addressUtilsByron.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
#include "common.h"
#include "bip44.h"

#ifdef APP_FEATURE_BYRON_ADDRESS_DERIVATION

size_t deriveAddress_byron(
const bip44_path_t* pathSpec,
uint32_t protocolMagic,
uint8_t* outBuffer, size_t outSize
);

#endif // APP_FEATURE_BYRON_ADDRESS_DERIVATION

#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK

// Note: validates the overall address structure at the same time
uint32_t extractProtocolMagic(
const uint8_t* addressBuffer, size_t addressSize
);

#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK


#ifdef DEVEL
#if defined(DEVEL) && !defined(APP_XS)
void run_addressUtilsByron_test();
#endif // DEVEL

Expand Down
2 changes: 1 addition & 1 deletion src/addressUtilsByron_test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef DEVEL
#if defined(DEVEL) && !defined(APP_XS)

#include "addressUtilsByron.h"
#include "cardano.h"
Expand Down
18 changes: 15 additions & 3 deletions src/addressUtilsShelley.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,6 @@ size_t deriveAddress(const addressParams_t* addressParams, uint8_t* outBuffer, s
ASSERT(outSize < BUFFER_SIZE_PARANOIA);
ASSERT(isValidAddressParams(addressParams));

const bip44_path_t* spendingPath = &addressParams->spendingKeyPath;

// shelley
switch (addressParams->type) {
case BASE_PAYMENT_KEY_STAKE_KEY:
Expand All @@ -458,8 +456,16 @@ size_t deriveAddress(const addressParams_t* addressParams, uint8_t* outBuffer, s
case REWARD_KEY:
case REWARD_SCRIPT:
return deriveAddress_reward(addressParams, outBuffer, outSize);

#ifdef APP_FEATURE_BYRON_ADDRESS_DERIVATION
case BYRON:
return deriveAddress_byron(spendingPath, addressParams->protocolMagic, outBuffer, outSize);
return deriveAddress_byron(
&addressParams->spendingKeyPath,
addressParams->protocolMagic,
outBuffer, outSize
);
#endif // APP_FEATURE_BYRON_ADDRESS_DERIVATION

default:
ASSERT(false);
}
Expand Down Expand Up @@ -698,6 +704,12 @@ bool isValidAddressParams(const addressParams_t* params)
#define CHECK(cond) if (!(cond)) return false
if (params->type != BYRON) {
CHECK(isValidNetworkId(params->networkId));
} else {
// code for Byron address derivation not available in XS app
// thus we cannot process address params
#ifndef APP_FEATURE_BYRON_ADDRESS_DERIVATION
return false;
#endif
}

CHECK(isValidStakingInfo(params));
Expand Down
4 changes: 4 additions & 0 deletions src/cardano.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ typedef struct {

// ============================== NATIVE SCRIPTS ==============================

#ifdef APP_FEATURE_NATIVE_SCRIPT_HASH

// depth of n means it can handle up to n-1 levels of nesting
#define MAX_SCRIPT_DEPTH 11

Expand All @@ -172,4 +174,6 @@ typedef enum {
NATIVE_SCRIPT_INVALID_HEREAFTER = 5,
} native_script_type;

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH

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

#include "deriveNativeScriptHash.h"
#include "deriveNativeScriptHash_ui.h"
#include "state.h"
Expand Down Expand Up @@ -304,3 +306,5 @@ void deriveNativeScriptHash_handleAPDU(
}

#undef TRACE_WITH_CTX

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH
4 changes: 4 additions & 0 deletions src/deriveNativeScriptHash.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH
#define H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH

#ifdef APP_FEATURE_NATIVE_SCRIPT_HASH

#include "bip44.h"
#include "cardano.h"
#include "common.h"
Expand Down Expand Up @@ -48,4 +50,6 @@ typedef struct {
ui_native_script_type ui_scriptType;
} ins_derive_native_script_hash_context_t;

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH

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

#include "deriveNativeScriptHash.h"
#include "deriveNativeScriptHash_ui.h"
#include "state.h"
Expand Down Expand Up @@ -338,3 +340,5 @@ void deriveNativeScriptHash_displayNativeScriptHash_policyId()
fill_and_display_if_required("Policy ID", bufferHex, deriveNativeScriptHash_displayNativeScriptHash_finish, respond_with_user_reject);
#endif // HAVE_BAGL
}

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH
6 changes: 6 additions & 0 deletions src/deriveNativeScriptHash_ui.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH_UI
#define H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH_UI

#ifdef APP_FEATURE_NATIVE_SCRIPT_HASH

enum {
DISPLAY_UI_STEP_POSITION = 200,
#ifdef HAVE_NBGL
Expand All @@ -17,4 +20,7 @@ void deriveNativeScriptHash_displayNativeScriptHash_callback();
void deriveNativeScriptHash_displayNativeScriptHash_bech32();

void deriveNativeScriptHash_displayNativeScriptHash_policyId();

#endif // APP_FEATURE_NATIVE_SCRIPT_HASH

#endif // H_CARDANO_APP_DERIVE_NATIVE_SCRIPT_HASH_UI
Loading

0 comments on commit 7f80101

Please sign in to comment.