diff --git a/Makefile b/Makefile index b192be047..fdb9cc23d 100644 --- a/Makefile +++ b/Makefile @@ -268,6 +268,7 @@ endif # Allow usage of function from lib_standard_app/crypto_helpers.c APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c +INCLUDES_PATH += ${BOLOS_SDK}/lib_standard_app ### initialize plugin SDK submodule if needed, rebuild it, and warn if a difference is noticed ifeq ($(CHAIN),ethereum) diff --git a/src/apdu_constants.h b/src/apdu_constants.h index 07e41a300..599e5878c 100644 --- a/src/apdu_constants.h +++ b/src/apdu_constants.h @@ -112,6 +112,8 @@ void handlePerformPrivacyOperation(uint8_t p1, unsigned int *flags, unsigned int *tx); +void getEthPublicKey(uint32_t *bip32Path, uint8_t bip32PathLength); + #ifdef HAVE_ETH2 void handleGetEth2PublicKey(uint8_t p1, diff --git a/src_features/getPublicKey/cmd_getPublicKey.c b/src_features/getPublicKey/cmd_getPublicKey.c index c30addcb6..4ee37bfb9 100644 --- a/src_features/getPublicKey/cmd_getPublicKey.c +++ b/src_features/getPublicKey/cmd_getPublicKey.c @@ -4,6 +4,28 @@ #include "feature_getPublicKey.h" #include "common_ui.h" #include "os_io_seproxyhal.h" +#include "crypto_helpers.h" + +void getEthPublicKey(uint32_t *bip32Path, uint8_t bip32PathLength) { + tmpCtx.publicKeyContext.publicKey.curve = CX_CURVE_256K1; + tmpCtx.publicKeyContext.publicKey.W_len = 65; + if (bip32_derive_get_pubkey_256( + CX_CURVE_256K1, + bip32Path, + bip32PathLength, + tmpCtx.publicKeyContext.publicKey.W, + (tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL), + CX_SHA512) != CX_OK) { + THROW(CX_INVALID_PARAMETER); + } + + if (!getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey, + tmpCtx.publicKeyContext.address, + &global_sha3, + chainConfig->chainId)) { + THROW(CX_INVALID_PARAMETER); + } +} void handleGetPublicKey(uint8_t p1, uint8_t p2, @@ -11,9 +33,7 @@ void handleGetPublicKey(uint8_t p1, uint8_t dataLength, unsigned int *flags, unsigned int *tx) { - uint8_t privateKeyData[INT256_LENGTH]; bip32_path_t bip32; - cx_ecfp_private_key_t privateKey; if (!G_called_from_swap) { reset_app_context(); @@ -35,25 +55,7 @@ void handleGetPublicKey(uint8_t p1, } tmpCtx.publicKeyContext.getChaincode = (p2 == P2_CHAINCODE); - io_seproxyhal_io_heartbeat(); - os_perso_derive_node_bip32( - CX_CURVE_256K1, - bip32.path, - bip32.length, - privateKeyData, - (tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL)); - cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &privateKey); - io_seproxyhal_io_heartbeat(); - cx_ecfp_generate_pair(CX_CURVE_256K1, &tmpCtx.publicKeyContext.publicKey, &privateKey, 1); - explicit_bzero(&privateKey, sizeof(privateKey)); - explicit_bzero(privateKeyData, sizeof(privateKeyData)); - io_seproxyhal_io_heartbeat(); - if (!getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey, - tmpCtx.publicKeyContext.address, - &global_sha3, - chainConfig->chainId)) { - THROW(CX_INVALID_PARAMETER); - } + getEthPublicKey(bip32.path, bip32.length); uint64_t chain_id = chainConfig->chainId; if (dataLength >= sizeof(chain_id)) {