Skip to content

Commit

Permalink
Factorize getEthPublicKey to be used in other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed Feb 23, 2024
1 parent edb54a5 commit 706e44d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/apdu_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 23 additions & 21 deletions src_features/getPublicKey/cmd_getPublicKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@
#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,
const uint8_t *dataBuffer,
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();
Expand All @@ -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)) {
Expand Down

0 comments on commit 706e44d

Please sign in to comment.