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

Catalyst voting support #65

Merged
merged 33 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6ebfc7c
decrease headless delay
janmazak Feb 19, 2021
69133ba
fix include guards
janmazak Feb 15, 2021
2bf73f1
add #endif DEVEL
janmazak Feb 15, 2021
55de114
add #endif HEADLESS
janmazak Feb 15, 2021
255743b
introduce certificate_type_t
janmazak Nov 25, 2020
f2c116f
refactor security policy helpers
janmazak Nov 25, 2020
ed0830b
use REWARD_ACCOUNT_SIZE
janmazak Dec 11, 2020
51f1c65
minor fixes and refactors
janmazak Jan 9, 2021
a2ccb7d
trace stack and save memory
janmazak Apr 9, 2021
4e43640
Add Catalyst voting registration support
refi93 Mar 16, 2021
8c3ca44
Add tx part context initialization
refi93 Apr 12, 2021
43544f8
Remove redundant comments
refi93 Apr 14, 2021
4e7fd7a
Rename test_utils.h -> testUtils.h
refi93 Apr 14, 2021
d42a089
Fix voting rewards address size in tests
refi93 Apr 14, 2021
ba6d978
Add noinline directive to append_cbor serialization helpers
refi93 Apr 14, 2021
e3ed0e5
Introduce hash context selection into aux data hash builder
refi93 Apr 14, 2021
3a36f66
shorten aux data builder enums
refi93 Apr 15, 2021
d5c49b1
CR fix to hash context updater macro in aux data hash builder
refi93 Apr 15, 2021
5e43f29
drop MAX_SHELLEY_ADDRESS_SIZE
refi93 Apr 15, 2021
c5d28e1
fix comment
refi93 Apr 16, 2021
83ecb3b
Remove redundant tracing
refi93 Apr 16, 2021
3d72f9a
remove empty lines
refi93 Apr 16, 2021
e19bd94
Add explanatory comment around staking key path in Catalyst registrat…
refi93 Apr 16, 2021
aee11af
Remove static specifier from enum, align declarations
refi93 Apr 16, 2021
eacf7b2
Assign derived address size directly to addressSize
refi93 Apr 16, 2021
dc60bea
Enhance aux data policy, improve memory handling in catalyst registra…
refi93 Apr 16, 2021
63bf1d5
fix references to hash builder, minor CR fixes
refi93 Apr 16, 2021
81408d1
add policies for all catalyst entries
refi93 Apr 16, 2021
c06626e
rename witnesses context to witness context
refi93 Apr 16, 2021
0cb4a88
CR fix
refi93 Apr 16, 2021
5b542af
Refactor subctx initialization for submachines
refi93 Apr 16, 2021
457f0ff
rename auxData.h to signTxAuxData.h
refi93 Apr 16, 2021
234ccb6
Inverse control over computed auxiliary data hash in Catalyst submachine
refi93 Apr 16, 2021
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
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ PIN = 5555

APPNAME = "Cardano ADA"
APPVERSION_M = 2
APPVERSION_N = 2
APPVERSION_P = 1
APPVERSION_N = 3
APPVERSION_P = 0
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

APP_LOAD_PARAMS =--appFlags 0x240 --curve ed25519 --path "44'/1815'" --path "1852'/1815'"
Expand Down Expand Up @@ -122,10 +122,12 @@ ifeq ($(GCCPATH),)
$(info GCCPATH is not set: arm-none-eabi-* will be used from PATH)
endif

WERROR := -Werror=incompatible-pointer-types -Werror=return-type

CC := $(CLANGPATH)clang
CFLAGS += -O3 -Os -Wall -Wextra -Wuninitialized
CFLAGS += -std=gnu11 -O3 -Os -Wall -Wextra -Wuninitialized $(WERROR)

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

LDFLAGS += -O3 -Os -Wall
Expand Down
2 changes: 1 addition & 1 deletion src/addressUtilsByron.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ uint32_t extractProtocolMagic(

#ifdef DEVEL
void run_addressUtilsByron_test();
#endif
#endif // DEVEL

#endif // H_CARDANO_APP_ADDRESS_UTILS_BYRON
4 changes: 2 additions & 2 deletions src/addressUtilsByron_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "addressUtilsByron.h"
#include "cardano.h"
#include "hexUtils.h"
#include "test_utils.h"
#include "testUtils.h"

#define HD HARDENED_BIP32

Expand Down Expand Up @@ -171,4 +171,4 @@ void run_addressUtilsByron_test()
testProtocolMagicExtractionThrows();
}

#endif
#endif // DEVEL
81 changes: 77 additions & 4 deletions src/addressUtilsShelley.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ bool isSupportedAddressType(uint8_t addressType)
}
}

bool isShelleyAddressType(uint8_t addressType)
{
switch (addressType) {
case BASE:
case POINTER:
case ENTERPRISE:
case REWARD:
return true;
default:
return false;
}
}

uint8_t constructShelleyAddressHeader(address_type_t type, uint8_t networkId)
{
ASSERT(isSupportedAddressType(type));
Expand Down Expand Up @@ -97,10 +110,13 @@ bool isStakingInfoConsistentWithAddressType(const addressParams_t* addressParams

size_t view_appendPublicKeyHash(write_view_t* view, const bip44_path_t* keyDerivationPath)
{
TRACE_STACK_USAGE();

extendedPublicKey_t extPubKey;
deriveExtendedPublicKey(keyDerivationPath, &extPubKey);

uint8_t hashedPubKey[ADDRESS_KEY_HASH_LENGTH];
STATIC_ASSERT(ADDRESS_KEY_HASH_LENGTH * 8 == 224, "wrong address key hash length");
blake2b_224_hash(
extPubKey.pubKey, SIZEOF(extPubKey.pubKey),
hashedPubKey, SIZEOF(hashedPubKey)
Expand Down Expand Up @@ -286,6 +302,8 @@ static size_t deriveAddress_reward(
uint8_t* outBuffer, size_t outSize
)
{
TRACE_STACK_USAGE();

ASSERT(getAddressType(addressHeader) == REWARD);
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

Expand All @@ -295,20 +313,37 @@ static size_t deriveAddress_reward(
}
{
// staking key path expected (corresponds to reward account)
ASSERT(bip44_isValidStakingKeyPath(spendingKeyPath)); // TODO check for unusual account?
ASSERT(bip44_isValidStakingKeyPath(spendingKeyPath));

view_appendPublicKeyHash(&out, spendingKeyPath);
}
{
// no staking data
}

const int ADDRESS_LENGTH = 1 + ADDRESS_KEY_HASH_LENGTH;
const int ADDRESS_LENGTH = REWARD_ACCOUNT_SIZE;
ASSERT(view_processedSize(&out) == ADDRESS_LENGTH);

return ADDRESS_LENGTH;
}

size_t constructRewardAddress(
size_t constructRewardAddressFromKeyPath(
const bip44_path_t* path, uint8_t networkId, uint8_t* outBuffer, size_t outSize
)
{
ASSERT(outSize == REWARD_ACCOUNT_SIZE);
ASSERT(bip44_isValidStakingKeyPath(path));

TRACE_STACK_USAGE();

const uint8_t header = constructShelleyAddressHeader(REWARD, networkId);
return deriveAddress_reward(
header, path,
outBuffer, outSize
);
}

size_t constructRewardAddressFromKeyHash(
uint8_t networkId,
const uint8_t* stakingKeyHashBuffer, size_t stakingKeyHashSize,
uint8_t* outBuffer, size_t outSize
Expand All @@ -327,7 +362,7 @@ size_t constructRewardAddress(
view_appendData(&out, stakingKeyHashBuffer, stakingKeyHashSize);
}

const int ADDRESS_LENGTH = 1 + ADDRESS_KEY_HASH_LENGTH;
const int ADDRESS_LENGTH = REWARD_ACCOUNT_SIZE;
ASSERT(view_processedSize(&out) == ADDRESS_LENGTH);

return ADDRESS_LENGTH;
Expand Down Expand Up @@ -503,3 +538,41 @@ void view_parseAddressParams(read_view_t* view, addressParams_t* params)
ASSERT(false);
}
}

static inline bool isSpendingPathConsistentWithAddressType(const address_type_t addressType, const bip44_path_t* spendingPath)
refi93 marked this conversation as resolved.
Show resolved Hide resolved
{
#define CHECK(cond) if (!(cond)) return false
// Byron derivation path is only valid for a Byron address
// the rest should be Shelley derivation scheme
if (addressType == BYRON) {
CHECK(bip44_hasByronPrefix(spendingPath));
} else {
CHECK(bip44_hasShelleyPrefix(spendingPath));
}

if (addressType == REWARD) {
CHECK(bip44_isValidStakingKeyPath(spendingPath));
} else {
CHECK(bip44_isValidAddressPath(spendingPath));
}

return true;
#undef CHECK
}

static inline bool isValidStakingInfo(const addressParams_t* addressParams)
{
#define CHECK(cond) if (!(cond)) return false
CHECK(isStakingInfoConsistentWithAddressType(addressParams));
if (addressParams->stakingChoice == STAKING_KEY_PATH) {
CHECK(bip44_isValidStakingKeyPath(&addressParams->stakingKeyPath));
}
return true;
#undef CHECK
}

bool isValidAddressParams(const addressParams_t* addressParams)
{
return isSpendingPathConsistentWithAddressType(addressParams->type, &addressParams->spendingKeyPath) &&
isValidStakingInfo(addressParams);
}
13 changes: 11 additions & 2 deletions src/addressUtilsShelley.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ uint8_t getAddressHeader(uint8_t* addressBuffer, size_t addressSize);

address_type_t getAddressType(uint8_t addressHeader);
bool isSupportedAddressType(uint8_t addressHeader);
bool isShelleyAddressType(uint8_t addressType);
uint8_t constructShelleyAddressHeader(address_type_t type, uint8_t networkId);

uint8_t getNetworkId(uint8_t addressHeader);
bool isValidNetworkId(uint8_t networkId);


// describes which staking info should be incorporated into address
// (see stakingChoice in addressParams_t)
typedef enum {
Expand Down Expand Up @@ -62,11 +64,17 @@ typedef struct {

bool isStakingInfoConsistentWithAddressType(const addressParams_t* addressParams);

__noinline_due_to_stack__
size_t view_appendPublicKeyHash(write_view_t* view, const bip44_path_t* keyDerivationPath);

size_t deriveAddress(const addressParams_t* addressParams, uint8_t* outBuffer, size_t outSize);

size_t constructRewardAddress(
__noinline_due_to_stack__
size_t constructRewardAddressFromKeyPath(
const bip44_path_t* path, uint8_t networkId, uint8_t* outBuffer, size_t outSize
);
__noinline_due_to_stack__
size_t constructRewardAddressFromKeyHash(
uint8_t networkId,
const uint8_t* stakingKeyHashBuffer, size_t stakingKeyHashSize,
uint8_t* outBuffer, size_t outSize
Expand All @@ -78,9 +86,10 @@ size_t humanReadableAddress(const uint8_t* address, size_t addressSize, char* ou

void view_parseAddressParams(read_view_t* view, addressParams_t* params);

bool isValidAddressParams(const addressParams_t* addressParams);

#ifdef DEVEL
void run_addressUtilsShelley_test();
#endif
#endif // DEVEL

#endif // H_CARDANO_APP_ADDRESS_UTILS_SHELLEY
4 changes: 2 additions & 2 deletions src/addressUtilsShelley_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "cardano.h"
#include "bip44.h"
#include "hexUtils.h"
#include "test_utils.h"
#include "testUtils.h"

#define HD HARDENED_BIP32
#define MAX_ADDRESS_LENGTH 128
Expand Down Expand Up @@ -200,4 +200,4 @@ void run_addressUtilsShelley_test()
testAddressDerivation();
}

#endif
#endif // DEVEL
2 changes: 1 addition & 1 deletion src/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void assert(
}
#else
# error "RESET_ON_CRASH should be enabled in non-devel mode!"
#endif
#endif // DEVEL
}
#endif
}
6 changes: 3 additions & 3 deletions src/assert.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef H_CARDANO_APP_UTIL
#define H_CARDANO_APP_UTIL
#ifndef H_CARDANO_APP_ASSERT
#define H_CARDANO_APP_ASSERT

#include "common.h"

Expand Down Expand Up @@ -28,4 +28,4 @@ extern void assert(int cond, const char* msgStr);

#define ASSERT(cond) assert((cond), _SHORTEN_( _FILE_LINE_, _MAX_ASSERT_LENGTH_))

#endif
#endif // H_CARDANO_APP_ASSERT
Loading