Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

Commit

Permalink
halt the device when encountered an invalid mnemonic (skip if device …
Browse files Browse the repository at this point in the history
…loaded or recovered without enforced wordlist)
  • Loading branch information
prusnak committed Nov 9, 2016
1 parent 810d478 commit 4ce4cc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
43 changes: 26 additions & 17 deletions firmware/recovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,36 @@ void recovery_word(const char *word)
strlcpy(words[word_pos - 1], word, sizeof(words[word_pos - 1]));
}

if (word_index + 1 == 24) { // last one
uint32_t i;
strlcpy(storage.mnemonic, words[0], sizeof(storage.mnemonic));
for (i = 1; i < word_count; i++) {
strlcat(storage.mnemonic, " ", sizeof(storage.mnemonic));
strlcat(storage.mnemonic, words[i], sizeof(storage.mnemonic));
}
if (!enforce_wordlist || mnemonic_check(storage.mnemonic)) {
storage.has_mnemonic = true;
storage_commit();
fsm_sendSuccess("Device recovered");
} else {
if (word_index + 1 < 24) { // not the last one
word_index++;
next_word();
return;
}

// the last one
strlcpy(storage.mnemonic, words[0], sizeof(storage.mnemonic));
for (uint32_t i = 1; i < word_count; i++) {
strlcat(storage.mnemonic, " ", sizeof(storage.mnemonic));
strlcat(storage.mnemonic, words[i], sizeof(storage.mnemonic));
}

awaiting_word = false;
layoutHome();

if (!mnemonic_check(storage.mnemonic)) {
if (enforce_wordlist) {
storage_reset();
fsm_sendFailure(FailureType_Failure_SyntaxError, "Invalid mnemonic, are words in correct order?");
return;
} else { // not enforcing => mark storage as imported
storage.has_imported = true;
storage.imported = true;
}
awaiting_word = false;
layoutHome();
} else {
word_index++;
next_word();
}

storage.has_mnemonic = true;
storage_commit();
fsm_sendSuccess("Device recovered");
}

void recovery_abort(void)
Expand Down
10 changes: 9 additions & 1 deletion firmware/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static char sessionPassphrase[51];
void storage_show_error(void)
{
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Storage failure", "detected.", NULL, "Please unplug", "the device.", NULL);
for (;;) { }
system_halt();
}

void storage_check_flash_errors(void)
Expand Down Expand Up @@ -353,6 +353,14 @@ const uint8_t *storage_getSeed(bool usePassphrase)
if (usePassphrase && !protectPassphrase()) {
return NULL;
}
// if storage was not imported (i.e. it was properly generated or recovered)
if (!storage.has_imported || !storage.imported) {
// test whether mnemonic is a valid BIP-0039 mnemonic
if (!mnemonic_check(storage.mnemonic)) {
// and if not then halt the device
storage_show_error();
}
}
mnemonic_to_seed(storage.mnemonic, usePassphrase ? sessionPassphrase : "", sessionSeed, get_root_node_callback); // BIP-0039
sessionSeedCached = true;
sessionSeedUsesPassphrase = usePassphrase;
Expand Down

0 comments on commit 4ce4cc5

Please sign in to comment.