From 2a4a298d586c06a0c6dbd4c6e2ed9af6fc9bff86 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 7 Mar 2018 14:04:37 +0100 Subject: [PATCH] firmware: refactor fsm_getDerivedNode to include fingerprint --- firmware/fsm.c | 54 ++++++++++++++++++++-------------------------- firmware/layout2.c | 11 +++++++--- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/firmware/fsm.c b/firmware/fsm.c index c36fb7452a..1a508c637a 100644 --- a/firmware/fsm.c +++ b/firmware/fsm.c @@ -194,9 +194,12 @@ static const CoinInfo *fsm_getCoin(bool has_name, const char *name) return coin; } -static HDNode *fsm_getDerivedNode(const char *curve, const uint32_t *address_n, size_t address_n_count) +static HDNode *fsm_getDerivedNode(const char *curve, const uint32_t *address_n, size_t address_n_count, uint32_t *fingerprint) { static CONFIDENTIAL HDNode node; + if (fingerprint) { + *fingerprint = 0; + } if (!storage_getRootNode(&node, curve, true)) { fsm_sendFailure(FailureType_Failure_NotInitialized, _("Device not initialized or passphrase request cancelled or unsupported curve")); layoutHome(); @@ -205,7 +208,7 @@ static HDNode *fsm_getDerivedNode(const char *curve, const uint32_t *address_n, if (!address_n || address_n_count == 0) { return &node; } - if (hdnode_private_ckd_cached(&node, address_n, address_n_count, NULL) == 0) { + if (hdnode_private_ckd_cached(&node, address_n, address_n_count, fingerprint) == 0) { fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to derive private key")); layoutHome(); return 0; @@ -439,19 +442,8 @@ void fsm_msgGetPublicKey(GetPublicKey *msg) curve = msg->ecdsa_curve_name; } uint32_t fingerprint; - HDNode *node; - if (msg->address_n_count == 0) { - /* get master node */ - fingerprint = 0; - node = fsm_getDerivedNode(curve, msg->address_n, 0); - } else { - /* get parent node */ - node = fsm_getDerivedNode(curve, msg->address_n, msg->address_n_count - 1); - if (!node) return; - fingerprint = hdnode_fingerprint(node); - /* get child */ - hdnode_private_ckd(node, msg->address_n[msg->address_n_count - 1]); - } + HDNode *node = node = fsm_getDerivedNode(curve, msg->address_n, msg->address_n_count, &fingerprint); + if (!node) return; hdnode_fill_public_key(node); if (msg->has_show_display && msg->show_display) { @@ -545,7 +537,7 @@ void fsm_msgSignTx(SignTx *msg) const CoinInfo *coin = fsm_getCoin(msg->has_coin_name, msg->coin_name); if (!coin) return; - const HDNode *node = fsm_getDerivedNode(coin->curve_name, 0, 0); + const HDNode *node = fsm_getDerivedNode(coin->curve_name, NULL, 0, NULL); if (!node) return; signing_init(msg->inputs_count, msg->outputs_count, coin, node, msg->version, msg->lock_time); @@ -573,7 +565,7 @@ void fsm_msgEthereumSignTx(EthereumSignTx *msg) CHECK_PIN - const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; ethereum_signing_init(msg, node); @@ -594,7 +586,7 @@ void fsm_msgCipherKeyValue(CipherKeyValue *msg) CHECK_PIN - const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; bool encrypt = msg->has_encrypt && msg->encrypt; @@ -789,7 +781,7 @@ void fsm_msgGetAddress(GetAddress *msg) const CoinInfo *coin = fsm_getCoin(msg->has_coin_name, msg->coin_name); if (!coin) return; - HDNode *node = fsm_getDerivedNode(coin->curve_name, msg->address_n, msg->address_n_count); + HDNode *node = fsm_getDerivedNode(coin->curve_name, msg->address_n, msg->address_n_count, NULL); if (!node) return; hdnode_fill_public_key(node); @@ -844,7 +836,7 @@ void fsm_msgEthereumGetAddress(EthereumGetAddress *msg) CHECK_PIN - const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; resp->address.size = 20; @@ -883,7 +875,7 @@ void fsm_msgEthereumSignMessage(EthereumSignMessage *msg) CHECK_PIN - const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; ethereum_message_sign(msg, node, resp); @@ -945,7 +937,7 @@ void fsm_msgSignMessage(SignMessage *msg) const CoinInfo *coin = fsm_getCoin(msg->has_coin_name, msg->coin_name); if (!coin) return; - HDNode *node = fsm_getDerivedNode(coin->curve_name, msg->address_n, msg->address_n_count); + HDNode *node = fsm_getDerivedNode(coin->curve_name, msg->address_n, msg->address_n_count, NULL); if (!node) return; layoutProgressSwipe(_("Signing"), 0); @@ -1027,7 +1019,7 @@ void fsm_msgSignIdentity(SignIdentity *msg) if (msg->has_ecdsa_curve_name) { curve = msg->ecdsa_curve_name; } - HDNode *node = fsm_getDerivedNode(curve, address_n, 5); + HDNode *node = fsm_getDerivedNode(curve, address_n, 5, NULL); if (!node) return; bool sign_ssh = msg->identity.has_proto && (strcmp(msg->identity.proto, "ssh") == 0); @@ -1104,7 +1096,7 @@ void fsm_msgGetECDHSessionKey(GetECDHSessionKey *msg) curve = msg->ecdsa_curve_name; } - const HDNode *node = fsm_getDerivedNode(curve, address_n, 5); + const HDNode *node = fsm_getDerivedNode(curve, address_n, 5, NULL); if (!node) return; int result_size = 0; @@ -1140,7 +1132,7 @@ void fsm_msgEncryptMessage(EncryptMessage *msg) CHECK_PIN - node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; hdnode_get_address_raw(node, coin->address_type, address_raw); } @@ -1177,7 +1169,7 @@ void fsm_msgDecryptMessage(DecryptMessage *msg) CHECK_PIN - const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(SECP256K1_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; layoutProgressSwipe(_("Decrypting"), 0); @@ -1266,7 +1258,7 @@ void fsm_msgNEMGetAddress(NEMGetAddress *msg) RESP_INIT(NEMAddress); - HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->address_n, msg->address_n_count); + HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; if (!hdnode_get_nem_address(node, msg->network, resp->address)) @@ -1339,7 +1331,7 @@ void fsm_msgNEMSignTx(NEMSignTx *msg) { RESP_INIT(NEMSignedTx); - HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->transaction.address_n, msg->transaction.address_n_count); + HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->transaction.address_n, msg->transaction.address_n_count, NULL); if (!node) return; hdnode_fill_public_key(node); @@ -1503,7 +1495,7 @@ void fsm_msgNEMDecryptMessage(NEMDecryptMessage *msg) CHECK_PIN - HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; const uint8_t *salt = msg->payload.bytes; @@ -1557,7 +1549,7 @@ void fsm_msgCosiCommit(CosiCommit *msg) CHECK_PIN - HDNode *node = fsm_getDerivedNode(ED25519_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(ED25519_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; uint8_t nonce[32]; @@ -1597,7 +1589,7 @@ void fsm_msgCosiSign(CosiSign *msg) CHECK_PIN - HDNode *node = fsm_getDerivedNode(ED25519_NAME, msg->address_n, msg->address_n_count); + const HDNode *node = fsm_getDerivedNode(ED25519_NAME, msg->address_n, msg->address_n_count, NULL); if (!node) return; uint8_t nonce[32]; diff --git a/firmware/layout2.c b/firmware/layout2.c index e65234f4f5..b56f787db9 100644 --- a/firmware/layout2.c +++ b/firmware/layout2.c @@ -511,11 +511,16 @@ void layoutAddress(const char *address, const char *desc, bool qrcode, bool igno void layoutPublicKey(const uint8_t *pubkey) { - char hex[32*2+1], desc[16]; + char hex[32 * 2 + 1], desc[16]; strlcpy(desc, "Public Key: 00", sizeof(desc)); - data2hex(pubkey, 1, desc + 12); + if (pubkey[0] == 1) { + /* ed25519 public key */ + // pass - leave 00 + } else { + data2hex(pubkey, 1, desc + 12); + } data2hex(pubkey + 1, 32, hex); - const char **str = split_message((const uint8_t *)hex, 32*2, 16); + const char **str = split_message((const uint8_t *)hex, 32 * 2, 16); layoutDialogSwipe(&bmp_icon_question, NULL, _("Continue"), NULL, desc, str[0], str[1], str[2], str[3], NULL); }