Skip to content

Commit d6c5c44

Browse files
Factorize getEthPublicKey to be used in other modules
1 parent edb54a5 commit d6c5c44

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ endif
268268

269269
# Allow usage of function from lib_standard_app/crypto_helpers.c
270270
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c
271+
INCLUDES_PATH += ${BOLOS_SDK}/lib_standard_app
271272

272273
### initialize plugin SDK submodule if needed, rebuild it, and warn if a difference is noticed
273274
ifeq ($(CHAIN),ethereum)

src/apdu_constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ void handlePerformPrivacyOperation(uint8_t p1,
112112
unsigned int *flags,
113113
unsigned int *tx);
114114

115+
void getEthPublicKey(uint32_t *bip32Path, uint8_t bip32PathLength);
116+
115117
#ifdef HAVE_ETH2
116118

117119
void handleGetEth2PublicKey(uint8_t p1,

src_features/getPublicKey/cmd_getPublicKey.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@
44
#include "feature_getPublicKey.h"
55
#include "common_ui.h"
66
#include "os_io_seproxyhal.h"
7+
#include "crypto_helpers.h"
8+
9+
10+
void getEthPublicKey(uint32_t *bip32Path, uint8_t bip32PathLength) {
11+
tmpCtx.publicKeyContext.publicKey.curve = CX_CURVE_256K1;
12+
tmpCtx.publicKeyContext.publicKey.W_len = 65;
13+
if (bip32_derive_get_pubkey_256(CX_CURVE_256K1,
14+
bip32Path,
15+
bip32PathLength,
16+
tmpCtx.publicKeyContext.publicKey.W,
17+
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL),
18+
CX_SHA512) != CX_OK) {
19+
THROW(CX_INVALID_PARAMETER);
20+
}
21+
22+
if (!getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey,
23+
tmpCtx.publicKeyContext.address,
24+
&global_sha3,
25+
chainConfig->chainId)) {
26+
THROW(CX_INVALID_PARAMETER);
27+
}
28+
}
729

830
void handleGetPublicKey(uint8_t p1,
931
uint8_t p2,
1032
const uint8_t *dataBuffer,
1133
uint8_t dataLength,
1234
unsigned int *flags,
1335
unsigned int *tx) {
14-
uint8_t privateKeyData[INT256_LENGTH];
1536
bip32_path_t bip32;
16-
cx_ecfp_private_key_t privateKey;
1737

1838
if (!G_called_from_swap) {
1939
reset_app_context();
@@ -35,25 +55,7 @@ void handleGetPublicKey(uint8_t p1,
3555
}
3656

3757
tmpCtx.publicKeyContext.getChaincode = (p2 == P2_CHAINCODE);
38-
io_seproxyhal_io_heartbeat();
39-
os_perso_derive_node_bip32(
40-
CX_CURVE_256K1,
41-
bip32.path,
42-
bip32.length,
43-
privateKeyData,
44-
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL));
45-
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &privateKey);
46-
io_seproxyhal_io_heartbeat();
47-
cx_ecfp_generate_pair(CX_CURVE_256K1, &tmpCtx.publicKeyContext.publicKey, &privateKey, 1);
48-
explicit_bzero(&privateKey, sizeof(privateKey));
49-
explicit_bzero(privateKeyData, sizeof(privateKeyData));
50-
io_seproxyhal_io_heartbeat();
51-
if (!getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey,
52-
tmpCtx.publicKeyContext.address,
53-
&global_sha3,
54-
chainConfig->chainId)) {
55-
THROW(CX_INVALID_PARAMETER);
56-
}
58+
getEthPublicKey(bip32.path, bip32.length);
5759

5860
uint64_t chain_id = chainConfig->chainId;
5961
if (dataLength >= sizeof(chain_id)) {

0 commit comments

Comments
 (0)