Skip to content

Cleanup #789

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions src/handle_check_address.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "handle_check_address.h"
#include "apdu_constants.h"
#include "crypto_helpers.h"
#include "getPublicKey.h"

#define ZERO(x) explicit_bzero(&x, sizeof(x))

Expand All @@ -26,14 +27,7 @@ uint16_t handle_check_address(check_address_parameters_t* params, chain_config_t
PRINTF("Invalid path\n");
return APDU_RESPONSE_INVALID_DATA;
}
CX_CHECK(bip32_derive_get_pubkey_256(CX_CURVE_256K1,
bip32.path,
bip32.length,
raw_pubkey,
NULL,
CX_SHA512));

getEthAddressStringFromRawKey((const uint8_t*) raw_pubkey, address, chain_config->chainId);
CX_CHECK(get_public_key_string(&bip32, raw_pubkey, address, NULL, chain_config->chainId));

uint8_t offset_0x = 0;
if (memcmp(params->address_to_check, "0x", 2) == 0) {
Expand Down
17 changes: 17 additions & 0 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "network_info.h"
#include "shared_context.h"
#include "common_utils.h"
#include "apdu_constants.h"

const char g_unknown_ticker[] = "???";

Expand Down Expand Up @@ -156,6 +157,22 @@ const char *get_network_name_from_chain_id(const uint64_t *chain_id) {
return PIC(net->name);
}

uint16_t get_network_as_string(char *out, size_t out_size) {
uint64_t chain_id = get_tx_chain_id();
const char *name = get_network_name_from_chain_id(&chain_id);

if (name == NULL) {
// No network name found so simply copy the chain ID as the network name.
if (!u64_to_string(chain_id, out, out_size)) {
return APDU_RESPONSE_CHAINID_OUT_BUF_SMALL;
}
} else {
// Network name found, simply copy it.
strlcpy(out, name, out_size);
}
return APDU_RESPONSE_OK;
}

const char *get_network_ticker_from_chain_id(const uint64_t *chain_id) {
const network_info_t *net = get_network_from_chain_id(chain_id);

Expand Down
1 change: 1 addition & 0 deletions src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern network_info_t DYNAMIC_NETWORK_INFO[];
extern const char g_unknown_ticker[];

const char *get_network_name_from_chain_id(const uint64_t *chain_id);
uint16_t get_network_as_string(char *out, size_t out_size);
const char *get_network_ticker_from_chain_id(const uint64_t *chain_id);

bool chain_is_ethereum_compatible(const uint64_t *chain_id);
Expand Down
2 changes: 1 addition & 1 deletion src_features/generic_tx_parser/gtp_param_trusted_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "gtp_field_table.h"
#include "utils.h"
#include "shared_context.h"
#include "feature_signTx.h" // get_public_key
#include "getPublicKey.h"
#include "apdu_constants.h"

enum {
Expand Down
2 changes: 1 addition & 1 deletion src_features/generic_tx_parser/gtp_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "gtp_data_path.h"
#include "shared_context.h" // txContext
#include "apdu_constants.h" // APDU_RESPONSE_OK
#include "feature_signTx.h" // get_public_key
#include "getPublicKey.h"
#include "gtp_parsed_value.h"

enum {
Expand Down
14 changes: 5 additions & 9 deletions src_features/getPublicKey/cmd_getPublicKey.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "shared_context.h"
#include "apdu_constants.h"
#include "common_utils.h"
#include "feature_getPublicKey.h"
#include "getPublicKey.h"
#include "common_ui.h"
#include "os_io_seproxyhal.h"
#include "crypto_helpers.h"
Expand Down Expand Up @@ -34,16 +34,12 @@ uint16_t handleGetPublicKey(uint8_t p1,
}

tmpCtx.publicKeyContext.getChaincode = (p2 == P2_CHAINCODE);
CX_CHECK(bip32_derive_get_pubkey_256(
CX_CURVE_256K1,
bip32.path,
bip32.length,
CX_CHECK(get_public_key_string(
&bip32,
tmpCtx.publicKeyContext.publicKey.W,
tmpCtx.publicKeyContext.address,
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL),
CX_SHA512));
getEthAddressStringFromRawKey(tmpCtx.publicKeyContext.publicKey.W,
tmpCtx.publicKeyContext.address,
chainConfig->chainId);
chainConfig->chainId));

uint64_t chain_id = chainConfig->chainId;
if (dataLength >= sizeof(chain_id)) {
Expand Down
8 changes: 0 additions & 8 deletions src_features/getPublicKey/feature_getPublicKey.h

This file was deleted.

58 changes: 58 additions & 0 deletions src_features/getPublicKey/getPublicKey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <string.h>
#include "crypto_helpers.h"
#include "shared_context.h"
#include "apdu_constants.h"

uint16_t get_public_key_string(bip32_path_t *bip32,
uint8_t *pubKey,
char *address,
uint8_t *chainCode,
uint64_t chainId) {
cx_err_t error = CX_INTERNAL_ERROR;

CX_CHECK(bip32_derive_get_pubkey_256(CX_CURVE_256K1,
bip32->path,
bip32->length,
pubKey,
chainCode,
CX_SHA512));
getEthAddressStringFromRawKey(pubKey, address, chainId);
error = CX_OK;
end:
return error;
}

uint16_t get_public_key(uint8_t *out, uint8_t outLength) {
uint8_t raw_pubkey[65];
cx_err_t error = CX_INTERNAL_ERROR;

if (outLength < ADDRESS_LENGTH) {
return APDU_RESPONSE_WRONG_DATA_LENGTH;
}
CX_CHECK(bip32_derive_get_pubkey_256(CX_CURVE_256K1,
tmpCtx.transactionContext.bip32.path,
tmpCtx.transactionContext.bip32.length,
raw_pubkey,
NULL,
CX_SHA512));

getEthAddressFromRawKey(raw_pubkey, out);
error = APDU_RESPONSE_OK;
end:
return error;
}

uint32_t set_result_get_publicKey() {
uint32_t tx = 0;
G_io_apdu_buffer[tx++] = 65;
memmove(G_io_apdu_buffer + tx, tmpCtx.publicKeyContext.publicKey.W, 65);
tx += 65;
G_io_apdu_buffer[tx++] = 40;
memmove(G_io_apdu_buffer + tx, tmpCtx.publicKeyContext.address, 40);
tx += 40;
if (tmpCtx.publicKeyContext.getChaincode) {
memmove(G_io_apdu_buffer + tx, tmpCtx.publicKeyContext.chainCode, 32);
tx += 32;
}
return tx;
}
14 changes: 14 additions & 0 deletions src_features/getPublicKey/getPublicKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _GET_PUB_KEY_H_
#define _GET_PUB_KEY_H_

#include "shared_context.h"

uint16_t get_public_key(uint8_t *out, uint8_t outLength);
uint16_t get_public_key_string(bip32_path_t *bip32,
uint8_t *pubKey,
char *address,
uint8_t *chainCode,
uint64_t chainId);
uint32_t set_result_get_publicKey(void);

#endif // _GET_PUB_KEY_H_
17 changes: 0 additions & 17 deletions src_features/getPublicKey/logic_getPublicKey.c

This file was deleted.

2 changes: 1 addition & 1 deletion src_features/getPublicKey/ui_common_getPublicKey.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "apdu_constants.h"
#include "feature_getPublicKey.h"
#include "getPublicKey.h"
#include "ui_callbacks.h"

unsigned int io_seproxyhal_touch_address_ok(void) {
Expand Down
2 changes: 1 addition & 1 deletion src_features/provide_tx_simulation/cmd_get_tx_simulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "apdu_constants.h"
#include "hash_bytes.h"
#include "public_keys.h"
#include "feature_signTx.h"
#include "getPublicKey.h"
#include "tlv.h"
#include "tlv_apdu.h"
#include "mem.h"
Expand Down
13 changes: 6 additions & 7 deletions src_features/signAuthorizationEIP7702/commands_7702.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "rlp_encode.h"
#include "whitelist_7702.h"
#include "auth_7702.h"
#include "getPublicKey.h"

// Avoid saving the full structure when parsing
// Alternative option : add a callback to f_tlv_payload_handler
Expand Down Expand Up @@ -126,15 +127,13 @@ static bool handleAuth7702TLV(const uint8_t *payload, uint16_t size, bool to_fre
sizeof(tmpCtx.authSigningContext7702.authHash)));
// Prepare information to be displayed
// * Address to be delegated
CX_CHECK(bip32_derive_get_pubkey_256(CX_CURVE_256K1,
tmpCtx.authSigningContext7702.bip32.path,
tmpCtx.authSigningContext7702.bip32.length,
publicKey.W,
NULL,
CX_SHA512));
strings.common.fromAddress[0] = '0';
strings.common.fromAddress[1] = 'x';
getEthAddressStringFromRawKey(publicKey.W, strings.common.fromAddress + 2, auth7702->chainId);
CX_CHECK(get_public_key_string(&tmpCtx.authSigningContext7702.bip32,
publicKey.W,
strings.common.fromAddress + 2,
NULL,
auth7702->chainId));
// * Delegate
if (!allzeroes(auth7702->delegate, sizeof(auth7702->delegate))) {
#ifdef HAVE_EIP7702_WHITELIST
Expand Down
2 changes: 0 additions & 2 deletions src_features/signTx/feature_signTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ void start_signature_flow(void);

uint16_t handle_parsing_status(parserStatus_e status);

uint16_t get_public_key(uint8_t *out, uint8_t outLength);
bool max_transaction_fee_to_string(const txInt256_t *BEGasPrice,
const txInt256_t *BEGasLimit,
char *displayBuffer,
uint32_t displayBufferSize);
uint16_t get_network_as_string(char *out, size_t out_size);

#endif // _SIGN_TX_H_
55 changes: 2 additions & 53 deletions src_features/signTx/logic_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "common_ui.h"
#include "ui_callbacks.h"
#include "apdu_constants.h"
#include "crypto_helpers.h"
#include "format.h"
#include "manage_asset_info.h"
#include "handle_swap_sign_transaction.h"
#include "os_math.h"
#include "calldata.h"
#include "swap_error_code_helpers.h"
#include "getPublicKey.h"

static bool g_use_standard_ui;

Expand Down Expand Up @@ -292,57 +292,6 @@ static void nonce_to_string(const txInt256_t *nonce, char *out, size_t out_size)
tostring256(&nonce_uint256, 10, out, out_size);
}

uint16_t get_network_as_string(char *out, size_t out_size) {
uint64_t chain_id = get_tx_chain_id();
const char *name = get_network_name_from_chain_id(&chain_id);

if (name == NULL) {
// No network name found so simply copy the chain ID as the network name.
if (!u64_to_string(chain_id, out, out_size)) {
return APDU_RESPONSE_CHAINID_OUT_BUF_SMALL;
}
} else {
// Network name found, simply copy it.
strlcpy(out, name, out_size);
}
return APDU_RESPONSE_OK;
}

uint16_t get_public_key(uint8_t *out, uint8_t outLength) {
uint8_t raw_pubkey[65];
cx_err_t error = CX_INTERNAL_ERROR;

if (outLength < ADDRESS_LENGTH) {
return APDU_RESPONSE_WRONG_DATA_LENGTH;
}
CX_CHECK(bip32_derive_get_pubkey_256(CX_CURVE_256K1,
tmpCtx.transactionContext.bip32.path,
tmpCtx.transactionContext.bip32.length,
raw_pubkey,
NULL,
CX_SHA512));

getEthAddressFromRawKey(raw_pubkey, out);
error = APDU_RESPONSE_OK;
end:
return error;
}

/* Local implementation of strncasecmp, workaround of the segfaulting base implem
* Remove once strncasecmp is fixed
*/
static int strcasecmp_workaround(const char *str1, const char *str2) {
unsigned char c1, c2;
do {
c1 = *str1++;
c2 = *str2++;
if (toupper(c1) != toupper(c2)) {
return toupper(c1) - toupper(c2);
}
} while (c1 != '\0');
return 0;
}

__attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContext_t *context) {
char displayBuffer[50];
uint8_t decimals = WEI_TO_ETHER;
Expand Down Expand Up @@ -523,7 +472,7 @@ __attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContex
}
if (G_called_from_swap) {
// Ensure the values are the same that the ones that have been previously validated
if (strcasecmp_workaround(strings.common.toAddress, displayBuffer) != 0) {
if (strcmp(strings.common.toAddress, displayBuffer) != 0) {
PRINTF("Error comparing destination addresses\n");
send_swap_error_with_string(APDU_RESPONSE_MODE_CHECK_FAILED,
SWAP_EC_ERROR_WRONG_DESTINATION,
Expand Down
3 changes: 2 additions & 1 deletion tests/fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (NOT DEFINED ENV{LIB_FUZZING_ENGINE})
elseif (SANITIZER MATCHES "memory")
set(COMPILATION_FLAGS ${COMPILATION_FLAGS} -fsanitize=fuzzer,memory,undefined -fsanitize-memory-track-origins -fsanitize=fuzzer-no-link)
else()
message(FATAL_ERROR "Unkown sanitizer type. It must be set to `address` or `memory`.")
message(FATAL_ERROR "Unknown sanitizer type. It must be set to `address` or `memory`.")
endif()
else()
set(COMPILATION_FLAGS "$ENV{LIB_FUZZING_ENGINE} $ENV{CFLAGS} ${CUSTOM_C_FLAGS}")
Expand Down Expand Up @@ -182,6 +182,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/../../ethereum-plugin-sdk/src/
${CMAKE_SOURCE_DIR}/../../src
${CMAKE_SOURCE_DIR}/../../src_features/generic_tx_parser/
${CMAKE_SOURCE_DIR}/../../src_features/getPublicKey/
${CMAKE_SOURCE_DIR}/../../src_features/provide_enum_value/
${CMAKE_SOURCE_DIR}/../../src_features/provide_network_info/
${CMAKE_SOURCE_DIR}/../../src_features/signTx/
Expand Down
Loading