void fsm_msgPing(Ping *msg) { RESP_INIT(Success); if(msg->has_button_protection && msg->button_protection) if(!confirm(ButtonRequestType_ButtonRequest_Ping, "Ping", msg->message)) { fsm_sendFailure(FailureType_Failure_ActionCancelled, "Ping cancelled"); go_home(); return; } if(msg->has_pin_protection && msg->pin_protection) { if(!pin_protect_cached()) { go_home(); return; } } if(msg->has_passphrase_protection && msg->passphrase_protection) { if(!passphrase_protect()) { fsm_sendFailure(FailureType_Failure_ActionCancelled, "Ping cancelled"); go_home(); return; } } if(msg->has_message) { resp->has_message = true; memcpy(&(resp->message), &(msg->message), sizeof(resp->message)); } msg_write(MessageType_MessageType_Success, resp); go_home(); }
/* * storage_get_root_node() - Returns root node of device * * INPUT * - node: where to put the node that is found * OUTPUT * true/false whether root node was found */ bool storage_get_root_node(HDNode *node) { // root node is properly cached if(sessionRootNodeCached) { memcpy(node, &sessionRootNode, sizeof(HDNode)); return true; } // if storage has node, decrypt and use it if(shadow_config.storage.has_node) { if(!passphrase_protect()) { return false; } layout_loading(); if(hdnode_from_xprv(shadow_config.storage.node.depth, shadow_config.storage.node.fingerprint, shadow_config.storage.node.child_num, shadow_config.storage.node.chain_code.bytes, shadow_config.storage.node.private_key.bytes, &sessionRootNode) == 0) { return false; } if(shadow_config.storage.has_passphrase_protection && shadow_config.storage.passphrase_protection && strlen(sessionPassphrase)) { // decrypt hd node uint8_t secret[64]; /* Length of salt + 4 bytes are needed as workspace by pbkdf2_hmac_sha512 */ uint8_t salt[strlen(PBKDF2_HMAC_SHA512_SALT) + 4]; memcpy((char *)salt, PBKDF2_HMAC_SHA512_SALT, strlen(PBKDF2_HMAC_SHA512_SALT)); animating_progress_handler(); pbkdf2_hmac_sha512((const uint8_t *)sessionPassphrase, strlen(sessionPassphrase), salt, strlen(PBKDF2_HMAC_SHA512_SALT), BIP39_PBKDF2_ROUNDS, secret, 64, get_root_node_callback); aes_decrypt_ctx ctx; aes_decrypt_key256(secret, &ctx); aes_cbc_decrypt(sessionRootNode.chain_code, sessionRootNode.chain_code, 32, secret + 32, &ctx); aes_cbc_decrypt(sessionRootNode.private_key, sessionRootNode.private_key, 32, secret + 32, &ctx); } memcpy(node, &sessionRootNode, sizeof(HDNode)); sessionRootNodeCached = true; return true; } // if storage has mnemonic, convert it to node and use it if(shadow_config.storage.has_mnemonic) { if(!passphrase_protect()) { return false; } if(storage_get_root_node_cache(node)) { return true; } layout_loading(); uint8_t seed[64]; animating_progress_handler(); mnemonic_to_seed(shadow_config.storage.mnemonic, sessionPassphrase, seed, get_root_node_callback); // BIP-0039 if(hdnode_from_seed(seed, sizeof(seed), &sessionRootNode) == 0) { return false; } storage_set_root_node_cache(&sessionRootNode); memcpy(node, &sessionRootNode, sizeof(HDNode)); sessionRootNodeCached = true; return true; } return false; }