Files
app-ethereum/src_features/getPublicKey/cmd_getPublicKey.c

84 lines
2.6 KiB
C
Raw Normal View History

2020-06-27 13:24:04 +02:00
#include "shared_context.h"
#include "apdu_constants.h"
2024-01-16 13:51:36 +01:00
#include "common_utils.h"
2020-06-27 13:24:04 +02:00
#include "feature_getPublicKey.h"
2022-08-24 09:25:01 +02:00
#include "common_ui.h"
#include "os_io_seproxyhal.h"
2024-04-15 11:33:42 +02:00
#include "crypto_helpers.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,
const uint8_t *dataBuffer,
uint8_t dataLength,
2020-12-01 16:20:13 +01:00
unsigned int *flags,
unsigned int *tx) {
2022-07-08 11:12:50 +02:00
bip32_path_t bip32;
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)) {
PRINTF("Error: Unexpected P1 (%u)!\n", p1);
THROW(APDU_RESPONSE_INVALID_P1_P2);
2020-12-01 16:20:13 +01:00
}
if ((p2 != P2_CHAINCODE) && (p2 != P2_NO_CHAINCODE)) {
PRINTF("Error: Unexpected P2 (%u)!\n", p2);
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) {
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);
2024-03-18 08:58:05 +01:00
if (bip32_derive_get_pubkey_256(
CX_CURVE_256K1,
bip32.path,
bip32.length,
tmpCtx.publicKeyContext.publicKey.W,
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL),
CX_SHA512) != CX_OK) {
THROW(APDU_RESPONSE_UNKNOWN);
}
2024-03-18 08:58:05 +01:00
getEthAddressStringFromRawKey(tmpCtx.publicKeyContext.publicKey.W,
tmpCtx.publicKeyContext.address,
chainConfig->chainId);
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) {
PRINTF("Error: Leftover unwanted data (%u bytes long)!\n", dataLength);
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();
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);
// 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
}