From d08ed271db85c112a588d3da7e3a490f0b0a7971 Mon Sep 17 00:00:00 2001 From: Jan Mazak Date: Wed, 21 Jun 2023 15:32:25 +0200 Subject: [PATCH] XS: Byron address derivation --- Makefile | 1 + src/addressUtilsByron.c | 106 +++++++++++++++++++---------------- src/addressUtilsByron.h | 4 ++ src/addressUtilsByron_test.c | 2 +- src/addressUtilsShelley.c | 17 +++++- 5 files changed, 77 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 5764fbbf..d5bb0ed0 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ DEFINES += APP_XS_DISABLE_TOKEN_MINTING DEFINES += APP_XS_DISABLE_POOL_REGISTRATION DEFINES += APP_XS_DISABLE_POOL_RETIREMENT DEFINES += APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK +DEFINES += APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION endif ############## diff --git a/src/addressUtilsByron.c b/src/addressUtilsByron.c index db48e87b..7385a891 100644 --- a/src/addressUtilsByron.c +++ b/src/addressUtilsByron.c @@ -7,6 +7,15 @@ #include "crc32.h" #include "bufView.h" +#if !defined(APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION) || !defined(APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK) + +static const size_t ADDRESS_ROOT_SIZE = 28; +static const size_t PROTOCOL_MAGIC_ADDRESS_ATTRIBUTE_KEY = 2; + +#endif + +#ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION + enum { CARDANO_ADDRESS_TYPE_PUBKEY = 0, /* @@ -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 @@ -136,6 +142,54 @@ 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_XS_DISABLE_BYRON_ADDRESS_DERIVATION + #ifndef APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK static uint64_t parseToken(read_view_t* view, uint8_t type) @@ -253,49 +307,3 @@ uint32_t extractProtocolMagic( } #endif // APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK - -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 - ); - -} diff --git a/src/addressUtilsByron.h b/src/addressUtilsByron.h index 70b5c391..5fdff4ab 100644 --- a/src/addressUtilsByron.h +++ b/src/addressUtilsByron.h @@ -4,12 +4,16 @@ #include "common.h" #include "bip44.h" +#ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION + size_t deriveAddress_byron( const bip44_path_t* pathSpec, uint32_t protocolMagic, uint8_t* outBuffer, size_t outSize ); +#endif // APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION + #ifndef APP_XS_DISABLE_BYRON_PROTOCOL_MAGIC_CHECK // Note: validates the overall address structure at the same time diff --git a/src/addressUtilsByron_test.c b/src/addressUtilsByron_test.c index 945418e5..444e3bb6 100644 --- a/src/addressUtilsByron_test.c +++ b/src/addressUtilsByron_test.c @@ -1,4 +1,4 @@ -#ifdef DEVEL +#if defined(DEVEL) && !defined(APP_XS) #include "addressUtilsByron.h" #include "cardano.h" diff --git a/src/addressUtilsShelley.c b/src/addressUtilsShelley.c index 7740708e..47d94790 100644 --- a/src/addressUtilsShelley.c +++ b/src/addressUtilsShelley.c @@ -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: @@ -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); + + #ifndef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION case BYRON: - return deriveAddress_byron(spendingPath, addressParams->protocolMagic, outBuffer, outSize); + return deriveAddress_byron( + &addressParams->spendingKeyPath, + addressParams->protocolMagic, + outBuffer, outSize + ); + #endif // APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION + default: ASSERT(false); } @@ -698,6 +704,11 @@ 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 + #ifdef APP_XS_DISABLE_BYRON_ADDRESS_DERIVATION + return false; + #endif } CHECK(isValidStakingInfo(params));