Skip to content

Commit

Permalink
XS: protocol magic check
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Oct 4, 2023
1 parent be3cf1d commit 8c58015
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ DEFINES += APP_FEATURE_TOKEN_MINTING
DEFINES += APP_FEATURE_POOL_REGISTRATION
DEFINES += APP_FEATURE_POOL_RETIREMENT
DEFINES += APP_FEATURE_BYRON_ADDRESS_DERIVATION
DEFINES += APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
endif

##############
Expand Down
8 changes: 7 additions & 1 deletion src/addressUtilsByron.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#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 {
Expand Down Expand Up @@ -186,6 +190,8 @@ size_t deriveAddress_byron(

#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 @@ -216,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 @@ -301,3 +306,4 @@ uint32_t extractProtocolMagic(
return protocolMagic;
}

#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
4 changes: 4 additions & 0 deletions src/addressUtilsByron.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ size_t deriveAddress_byron(

#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


#if defined(DEVEL) && !defined(APP_XS)
void run_addressUtilsByron_test();
Expand Down
4 changes: 3 additions & 1 deletion src/securityPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ security_policy_t policyForSignTxInput(sign_tx_signingmode_t txSigningMode)

static bool is_addressBytes_suitable_for_tx_output(
const uint8_t* addressBuffer, size_t addressSize,
const uint8_t networkId, const uint32_t protocolMagic
const uint8_t networkId, const uint32_t protocolMagic __attribute__((unused))
)
{
ASSERT(addressSize < BUFFER_SIZE_PARANOIA);
Expand All @@ -482,7 +482,9 @@ static bool is_addressBytes_suitable_for_tx_output(
return false;

case BYRON:
#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
CHECK(extractProtocolMagic(addressBuffer, addressSize) == protocolMagic);
#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
break;

default: {
Expand Down
8 changes: 7 additions & 1 deletion src/signTx_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ static const char* _newTxLine1(sign_tx_signingmode_t txSigningMode)
#ifdef HAVE_NBGL
static void signTx_handleInit_ui_runStep_cb(void)
{
// if the protocol magic check is not enabled,
// displaying the protocol magic might be misleading,
// so we must not show it
#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
char networkParams[100] = {0};
ui_getNetworkParamsScreen_2(
networkParams,
SIZEOF(networkParams),
ctx->commonTxData.protocolMagic);
fill_and_display_if_required("Protocol magic", networkParams, signTx_handleInit_ui_runStep, respond_with_user_reject);
#endif
}
#endif // HAVE_NBGL

Expand Down Expand Up @@ -118,7 +123,8 @@ void signTx_handleInit_ui_runStep()
#ifdef HAVE_BAGL
ui_displayNetworkParamsScreen(
"Network details",
ctx->commonTxData.networkId, ctx->commonTxData.protocolMagic,
ctx->commonTxData.networkId,
ctx->commonTxData.protocolMagic,
this_fn
);
#elif defined(HAVE_NBGL)
Expand Down
12 changes: 12 additions & 0 deletions src/uiScreens_bagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,23 @@ void ui_displayNetworkParamsScreen(
STATIC_ASSERT(!IS_SIGNED(networkId), "signed type for %u");
STATIC_ASSERT(sizeof(protocolMagic) <= sizeof(unsigned), "oversized type for %u");
STATIC_ASSERT(!IS_SIGNED(protocolMagic), "signed type for %u");

#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
snprintf(
networkParams, SIZEOF(networkParams),
"network id %u / protocol magic %u",
networkId, protocolMagic
);
#else
// if the protocol magic check is not enabled,
// displaying the protocol magic might be misleading,
// so we must not show it
snprintf(
networkParams, SIZEOF(networkParams),
"network id %u",
networkId
);
#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
ASSERT(strlen(networkParams) + 1 < SIZEOF(networkParams));

ui_displayPaginatedText(
Expand Down

0 comments on commit 8c58015

Please sign in to comment.