From d9cabcb74518cf025de2bbc4b5d23ee94c3bc74b Mon Sep 17 00:00:00 2001 From: lulu Date: Thu, 11 May 2023 10:45:22 +0800 Subject: [PATCH] fix(legacy): fix cardano derive when session is reused (#393) --- legacy/firmware/config.c | 9 ++++++++- legacy/firmware/fsm_msg_common.h | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/legacy/firmware/config.c b/legacy/firmware/config.c index 8e6e1085fb..1d59e27630 100644 --- a/legacy/firmware/config.c +++ b/legacy/firmware/config.c @@ -1770,6 +1770,13 @@ bool config_getTrezorCompMode(bool *trezor_comp_mode) { return sectrue == config_get_bool(KEY_TREZOR_COMP_MODE, trezor_comp_mode); } -bool config_getDeriveCardano(void) { return derive_cardano; } +bool config_getDeriveCardano(void) { + if ((activeSessionCache->cardanoSecretCached != sectrue) && + (activeSessionCache->seedCached == sectrue)) { + return false; // only BIP-0039 + } + + return true; +} void config_setDeriveCardano(bool on) { derive_cardano = on; } diff --git a/legacy/firmware/fsm_msg_common.h b/legacy/firmware/fsm_msg_common.h index a419b55dc3..a7ac913144 100644 --- a/legacy/firmware/fsm_msg_common.h +++ b/legacy/firmware/fsm_msg_common.h @@ -209,11 +209,6 @@ bool get_features(Features *resp) { void fsm_msgInitialize(const Initialize *msg) { fsm_abortWorkflows(); - if (msg && msg->has_derive_cardano && msg->derive_cardano) { - config_setDeriveCardano(true); - } else { - config_setDeriveCardano(false); - } uint8_t *session_id; if (msg && msg->has_session_id) { @@ -222,6 +217,19 @@ void fsm_msgInitialize(const Initialize *msg) { session_id = session_startSession(NULL); } + if (msg && msg->has_derive_cardano && msg->derive_cardano) { + if (!config_getDeriveCardano()) { + // seed is already derived, and host wants to change derive_cardano + // setting + // => create a new session + session_endCurrentSession(); + session_id = session_startSession(NULL); + } + config_setDeriveCardano(true); + } else { + config_setDeriveCardano(false); + } + RESP_INIT(Features); get_features(resp);