storage: copy only required bytes from old storage, bump storage version

+ backup_device: ask for pin, always use correct number of words
This commit is contained in:
Pavol Rusnak 2017-08-07 16:34:07 +02:00
parent 9c25e03d98
commit a01ba51a2a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 32 additions and 7 deletions

View File

@ -452,6 +452,8 @@ void fsm_msgBackupDevice(BackupDevice *msg)
{
CHECK_INITIALIZED
CHECK_PIN_UNCACHED
(void)msg;
reset_backup(true);
}

View File

@ -121,15 +121,18 @@ void reset_backup(bool separated)
storage_commit();
for (int pass = 0; pass < 2; pass++) {
int i = 0;
for (int word_pos = 1; word_pos <= (int)strength/32*3; word_pos++) {
int i = 0, word_pos = 1;
while (storage.mnemonic[i] != 0) {
// copy current_word
int j = 0;
while (storage.mnemonic[i] != ' ' && storage.mnemonic[i] != 0 && j + 1 < (int)sizeof(current_word)) {
current_word[j] = storage.mnemonic[i];
i++; j++;
}
current_word[j] = 0; if (storage.mnemonic[i] != 0) i++;
current_word[j] = 0;
if (storage.mnemonic[i] != 0) {
i++;
}
char desc[] = "##th word is:";
if (word_pos < 10) {
desc[0] = ' ';
@ -151,7 +154,7 @@ void reset_backup(bool separated)
current_word_display[j + 1] = current_word[j] + 'A' - 'a';
}
current_word_display[j + 1] = 0;
if (word_pos == (int)strength/32*3) { // last word
if (storage.mnemonic[i] == 0) { // last word
if (pass == 1) {
layoutDialogSwipe(&bmp_icon_info, NULL, _("Finish"), NULL, _("Please check the seed"), NULL, (word_pos < 10 ? desc + 1 : desc), current_word_display, NULL, NULL);
} else {
@ -172,6 +175,7 @@ void reset_backup(bool separated)
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
return;
}
word_pos++;
}
}

View File

@ -102,7 +102,7 @@ static bool sessionPinCached;
static bool sessionPassphraseCached;
static char sessionPassphrase[51];
#define STORAGE_VERSION 7
#define STORAGE_VERSION 8
void storage_show_error(void)
{
@ -133,6 +133,7 @@ bool storage_from_flash(void)
// version 5: since 1.3.3
// version 6: since 1.3.6
// version 7: since 1.5.1
// version 8: since 1.5.2
if (version > STORAGE_VERSION) {
// downgrade -> clear storage
return false;
@ -141,8 +142,26 @@ bool storage_from_flash(void)
// load uuid
memcpy(storage_uuid, (void *)(FLASH_STORAGE_START + 4), sizeof(storage_uuid));
data2hex(storage_uuid, sizeof(storage_uuid), storage_uuid_str);
// copy storage
memcpy(&storage, (void *)(FLASH_STORAGE_START + 4 + sizeof(storage_uuid)), sizeof(Storage));
size_t old_storage_size = 0;
if (version == 1 || version == 2) {
old_storage_size = 460;
} else
if (version == 3 || version == 4 || version == 5) {
old_storage_size = 1488;
} else
if (version == 6 || version == 7) {
old_storage_size = 1496;
} else
if (version == 8) {
old_storage_size = 1504;
}
memset(&storage, 0, sizeof(Storage));
memcpy(&storage, (void *)(FLASH_STORAGE_START + 4 + sizeof(storage_uuid)), old_storage_size);
if (version <= 5) {
// convert PIN failure counter from version 5 format
uint32_t pinctr = storage.has_pin_failed_attempts

View File

@ -22,7 +22,7 @@
#define VERSION_MAJOR 1
#define VERSION_MINOR 5
#define VERSION_PATCH 1
#define VERSION_PATCH 2
#define STR(X) #X
#define VERSTR(X) STR(X)