2020-06-27 13:24:04 +02:00
|
|
|
#include "shared_context.h"
|
|
|
|
|
#include "apdu_constants.h"
|
2023-08-04 18:17:09 +02:00
|
|
|
#include "utils.h"
|
2020-06-27 13:24:04 +02:00
|
|
|
#include "feature_getPublicKey.h"
|
2022-02-15 14:09:43 +01:00
|
|
|
#include "ethUtils.h"
|
2022-08-24 09:25:01 +02:00
|
|
|
#include "common_ui.h"
|
|
|
|
|
#include "os_io_seproxyhal.h"
|
2020-06-27 13:24:04 +02:00
|
|
|
|
2020-12-01 16:20:13 +01:00
|
|
|
void handleGetPublicKey(uint8_t p1,
|
|
|
|
|
uint8_t p2,
|
2022-07-15 12:33:19 +02:00
|
|
|
const uint8_t *dataBuffer,
|
2022-07-19 11:42:25 +02:00
|
|
|
uint8_t dataLength,
|
2020-12-01 16:20:13 +01:00
|
|
|
unsigned int *flags,
|
|
|
|
|
unsigned int *tx) {
|
2021-04-30 18:22:15 +02:00
|
|
|
uint8_t privateKeyData[INT256_LENGTH];
|
2022-07-08 11:12:50 +02:00
|
|
|
bip32_path_t bip32;
|
2020-12-01 16:20:13 +01:00
|
|
|
cx_ecfp_private_key_t privateKey;
|
2022-07-08 11:12:50 +02:00
|
|
|
|
2023-06-30 14:05:46 +02:00
|
|
|
if (!G_called_from_swap) {
|
2020-12-01 16:20:13 +01:00
|
|
|
reset_app_context();
|
|
|
|
|
}
|
2022-07-08 11:12:50 +02:00
|
|
|
|
2020-12-01 16:20:13 +01:00
|
|
|
if ((p1 != P1_CONFIRM) && (p1 != P1_NON_CONFIRM)) {
|
2023-08-09 11:38:17 +02:00
|
|
|
PRINTF("Error: Unexpected P1 (%u)!\n", p1);
|
2023-08-04 18:17:09 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_P1_P2);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
|
|
|
|
if ((p2 != P2_CHAINCODE) && (p2 != P2_NO_CHAINCODE)) {
|
2023-08-09 11:38:17 +02:00
|
|
|
PRINTF("Error: Unexpected P2 (%u)!\n", p2);
|
2023-08-04 18:17:09 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_P1_P2);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
2022-07-08 11:12:50 +02:00
|
|
|
|
|
|
|
|
dataBuffer = parseBip32(dataBuffer, &dataLength, &bip32);
|
|
|
|
|
|
|
|
|
|
if (dataBuffer == NULL) {
|
2023-08-04 18:17:09 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
2022-07-08 11:12:50 +02:00
|
|
|
|
2020-12-01 16:20:13 +01:00
|
|
|
tmpCtx.publicKeyContext.getChaincode = (p2 == P2_CHAINCODE);
|
|
|
|
|
io_seproxyhal_io_heartbeat();
|
|
|
|
|
os_perso_derive_node_bip32(
|
|
|
|
|
CX_CURVE_256K1,
|
2022-07-08 11:12:50 +02:00
|
|
|
bip32.path,
|
|
|
|
|
bip32.length,
|
2020-12-01 16:20:13 +01:00
|
|
|
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();
|
2023-10-05 15:01:58 +02:00
|
|
|
if (!getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey,
|
|
|
|
|
tmpCtx.publicKeyContext.address,
|
|
|
|
|
&global_sha3,
|
|
|
|
|
chainConfig->chainId)) {
|
|
|
|
|
THROW(CX_INVALID_PARAMETER);
|
|
|
|
|
}
|
2023-08-04 18:17:09 +02:00
|
|
|
|
|
|
|
|
uint64_t chain_id = chainConfig->chainId;
|
|
|
|
|
if (dataLength >= sizeof(chain_id)) {
|
|
|
|
|
chain_id = u64_from_BE(dataBuffer, sizeof(chain_id));
|
|
|
|
|
dataLength -= sizeof(chain_id);
|
|
|
|
|
dataBuffer += sizeof(chain_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void) dataBuffer; // to prevent dead increment warning
|
|
|
|
|
if (dataLength > 0) {
|
2023-08-09 11:38:17 +02:00
|
|
|
PRINTF("Error: Leftover unwanted data (%u bytes long)!\n", dataLength);
|
2023-08-04 18:17:09 +02:00
|
|
|
THROW(APDU_RESPONSE_INVALID_DATA);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 13:24:04 +02:00
|
|
|
#ifndef NO_CONSENT
|
2020-12-01 16:20:13 +01:00
|
|
|
if (p1 == P1_NON_CONFIRM)
|
|
|
|
|
#endif // NO_CONSENT
|
|
|
|
|
{
|
|
|
|
|
*tx = set_result_get_publicKey();
|
2023-08-04 18:17:09 +02:00
|
|
|
THROW(APDU_RESPONSE_OK);
|
2020-12-01 16:20:13 +01:00
|
|
|
}
|
2020-06-27 13:24:04 +02:00
|
|
|
#ifndef NO_CONSENT
|
2020-12-01 16:20:13 +01:00
|
|
|
else {
|
|
|
|
|
snprintf(strings.common.fullAddress,
|
|
|
|
|
sizeof(strings.common.fullAddress),
|
|
|
|
|
"0x%.*s",
|
|
|
|
|
40,
|
|
|
|
|
tmpCtx.publicKeyContext.address);
|
2023-08-09 11:38:17 +02:00
|
|
|
// don't unnecessarily pass the current app's chain ID
|
|
|
|
|
ui_display_public_key(chainConfig->chainId == chain_id ? NULL : &chain_id);
|
2020-06-27 13:24:04 +02:00
|
|
|
|
2020-12-01 16:20:13 +01:00
|
|
|
*flags |= IO_ASYNCH_REPLY;
|
|
|
|
|
}
|
|
|
|
|
#endif // NO_CONSENT
|
2020-06-27 13:24:04 +02:00
|
|
|
}
|