2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-06-18 22:12:31 +00:00
|
|
|
#include <libopencm3/stm32/flash.h>
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
#include "messages.pb.h"
|
|
|
|
#include "storage.pb.h"
|
|
|
|
|
|
|
|
#include "trezor.h"
|
2014-06-07 12:21:59 +00:00
|
|
|
#include "sha2.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "aes.h"
|
2014-06-07 12:21:59 +00:00
|
|
|
#include "pbkdf2.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "bip32.h"
|
|
|
|
#include "bip39.h"
|
2016-04-22 15:49:00 +00:00
|
|
|
#include "curves.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "rng.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "protect.h"
|
|
|
|
#include "layout2.h"
|
2016-07-07 16:37:37 +00:00
|
|
|
#include "usb.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2017-12-13 09:53:27 +00:00
|
|
|
#include "u2f.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
/* magic constant to check validity of storage block */
|
|
|
|
static const uint32_t storage_magic = 0x726f7473; // 'stor' as uint32_t
|
|
|
|
|
|
|
|
static uint32_t storage_uuid[12 / sizeof(uint32_t)];
|
|
|
|
_Static_assert(((uint32_t)storage_uuid & 3) == 0, "uuid unaligned");
|
|
|
|
_Static_assert((sizeof(storage_uuid) & 3) == 0, "uuid unaligned");
|
|
|
|
|
|
|
|
Storage CONFIDENTIAL storageUpdate;
|
|
|
|
_Static_assert(((uint32_t)&storageUpdate & 3) == 0, "storage unaligned");
|
|
|
|
_Static_assert((sizeof(storageUpdate) & 3) == 0, "storage unaligned");
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-12-12 18:36:34 +00:00
|
|
|
#define STORAGE_ROM ((const Storage *)(FLASH_STORAGE_START + sizeof(storage_magic) + sizeof(storage_uuid)))
|
2017-12-13 18:38:51 +00:00
|
|
|
|
|
|
|
#if EMULATOR
|
|
|
|
// TODO: Fix this for emulator
|
|
|
|
#define storageRom STORAGE_ROM
|
|
|
|
#else
|
2017-12-12 18:36:34 +00:00
|
|
|
const Storage *storageRom = STORAGE_ROM;
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2017-08-22 17:00:25 +00:00
|
|
|
|
|
|
|
char storage_uuid_str[25];
|
2017-08-22 07:44:18 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
/*
|
|
|
|
storage layout:
|
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
offset | type/length | description
|
|
|
|
--------+--------------+-------------------------------
|
|
|
|
0x0000 | 4 bytes | magic = 'stor'
|
|
|
|
0x0004 | 12 bytes | uuid
|
|
|
|
0x0010 | ? bytes | Storage structure
|
|
|
|
--------+--------------+-------------------------------
|
|
|
|
0x4000 | 4 kbytes | area for pin failures
|
|
|
|
0x5000 | 256 bytes | area for u2f counter updates
|
|
|
|
0x5100 | 11.75 kbytes | reserved
|
2016-04-27 16:12:03 +00:00
|
|
|
|
|
|
|
The area for pin failures looks like this:
|
|
|
|
0 ... 0 pinfail 0xffffffff .. 0xffffffff
|
|
|
|
The pinfail is a binary number of the form 1...10...0,
|
|
|
|
the number of zeros is the number of pin failures.
|
|
|
|
This layout is used because we can only clear bits without
|
|
|
|
erasing the flash.
|
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
The area for u2f counter updates is just a sequence of zero-bits
|
|
|
|
followed by a sequence of one-bits. The bits in a byte are numbered
|
|
|
|
from LSB to MSB. The number of zero bits is the offset that should
|
|
|
|
be added to the storage u2f_counter to get the real counter value.
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
#define FLASH_STORAGE_PINAREA (FLASH_META_START + 0x4000)
|
|
|
|
#define FLASH_STORAGE_PINAREA_LEN (0x1000)
|
2016-05-24 21:44:40 +00:00
|
|
|
#define FLASH_STORAGE_U2FAREA (FLASH_STORAGE_PINAREA + FLASH_STORAGE_PINAREA_LEN)
|
|
|
|
#define FLASH_STORAGE_U2FAREA_LEN (0x100)
|
2017-08-22 17:00:25 +00:00
|
|
|
#define FLASH_STORAGE_REALLEN (sizeof(storage_magic) + sizeof(storage_uuid) + sizeof(Storage))
|
2016-08-29 21:22:49 +00:00
|
|
|
|
2017-12-13 18:38:51 +00:00
|
|
|
#if !EMULATOR
|
|
|
|
// TODO: Fix this for emulator
|
2016-04-27 16:12:03 +00:00
|
|
|
_Static_assert(FLASH_STORAGE_START + FLASH_STORAGE_REALLEN <= FLASH_STORAGE_PINAREA, "Storage struct is too large for TREZOR flash");
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
/* Current u2f offset, i.e. u2f counter is
|
|
|
|
* storage.u2f_counter + storage_u2f_offset.
|
|
|
|
* This corresponds to the number of cleared bits in the U2FAREA.
|
|
|
|
*/
|
|
|
|
static uint32_t storage_u2f_offset;
|
|
|
|
|
2016-05-19 23:49:20 +00:00
|
|
|
static bool sessionSeedCached, sessionSeedUsesPassphrase;
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2017-08-08 10:59:39 +00:00
|
|
|
static uint8_t CONFIDENTIAL sessionSeed[64];
|
2016-04-27 16:12:03 +00:00
|
|
|
|
|
|
|
static bool sessionPinCached;
|
|
|
|
|
|
|
|
static bool sessionPassphraseCached;
|
2017-08-08 10:59:39 +00:00
|
|
|
static char CONFIDENTIAL sessionPassphrase[51];
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2017-12-13 09:53:27 +00:00
|
|
|
#define STORAGE_VERSION 9
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2016-05-27 12:03:20 +00:00
|
|
|
void storage_show_error(void)
|
|
|
|
{
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, _("Storage failure"), _("detected."), NULL, _("Please unplug"), _("the device."), NULL);
|
2016-11-09 15:06:50 +00:00
|
|
|
system_halt();
|
2016-05-27 12:03:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
void storage_check_flash_errors(void)
|
|
|
|
{
|
2017-12-13 18:38:51 +00:00
|
|
|
#if !EMULATOR
|
2016-04-27 16:12:03 +00:00
|
|
|
// flash operation failed
|
|
|
|
if (FLASH_SR & (FLASH_SR_PGAERR | FLASH_SR_PGPERR | FLASH_SR_PGSERR | FLASH_SR_WRPERR)) {
|
2016-05-27 12:03:20 +00:00
|
|
|
storage_show_error();
|
2016-04-27 16:12:03 +00:00
|
|
|
}
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2016-04-27 16:12:03 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2016-05-20 18:06:14 +00:00
|
|
|
bool storage_from_flash(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2017-12-13 09:53:27 +00:00
|
|
|
storage_clear_update();
|
2017-08-22 17:00:25 +00:00
|
|
|
if (memcmp((void *)FLASH_STORAGE_START, &storage_magic, sizeof(storage_magic)) != 0) {
|
2016-05-20 18:06:14 +00:00
|
|
|
// wrong magic
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
const uint32_t version = storageRom->version;
|
2016-04-27 16:12:03 +00:00
|
|
|
// version 1: since 1.0.0
|
|
|
|
// version 2: since 1.2.1
|
|
|
|
// version 3: since 1.3.1
|
|
|
|
// version 4: since 1.3.2
|
|
|
|
// version 5: since 1.3.3
|
|
|
|
// version 6: since 1.3.6
|
2017-07-31 20:43:28 +00:00
|
|
|
// version 7: since 1.5.1
|
2017-08-07 14:34:07 +00:00
|
|
|
// version 8: since 1.5.2
|
2017-12-13 09:53:27 +00:00
|
|
|
// version 9: since 1.6.1
|
2016-05-20 18:06:14 +00:00
|
|
|
if (version > STORAGE_VERSION) {
|
|
|
|
// downgrade -> clear storage
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load uuid
|
2017-08-22 17:00:25 +00:00
|
|
|
memcpy(storage_uuid, (void *)(FLASH_STORAGE_START + sizeof(storage_magic)), sizeof(storage_uuid));
|
2016-05-20 18:06:14 +00:00
|
|
|
data2hex(storage_uuid, sizeof(storage_uuid), storage_uuid_str);
|
2017-08-07 14:34:07 +00:00
|
|
|
|
2017-12-15 11:28:54 +00:00
|
|
|
#define OLD_STORAGE_SIZE(last_member) (((offsetof(Storage, last_member) + pb_membersize(Storage, last_member)) + 3) & ~3)
|
2017-08-22 16:06:04 +00:00
|
|
|
|
2016-05-20 18:06:14 +00:00
|
|
|
// copy storage
|
2017-08-07 14:34:07 +00:00
|
|
|
size_t old_storage_size = 0;
|
|
|
|
|
2017-08-22 16:09:10 +00:00
|
|
|
if (version == 0) {
|
|
|
|
} else if (version <= 2) {
|
2017-08-22 16:06:04 +00:00
|
|
|
old_storage_size = OLD_STORAGE_SIZE(imported);
|
2017-08-22 16:09:10 +00:00
|
|
|
} else if (version <= 5) {
|
2017-12-13 09:53:27 +00:00
|
|
|
// added homescreen
|
2017-08-22 16:06:04 +00:00
|
|
|
old_storage_size = OLD_STORAGE_SIZE(homescreen);
|
2017-08-22 16:09:10 +00:00
|
|
|
} else if (version <= 7) {
|
2017-12-13 09:53:27 +00:00
|
|
|
// added u2fcounter
|
2017-08-22 16:06:04 +00:00
|
|
|
old_storage_size = OLD_STORAGE_SIZE(u2f_counter);
|
2017-08-22 16:09:10 +00:00
|
|
|
} else if (version <= 8) {
|
2017-12-13 09:53:27 +00:00
|
|
|
// added flags and needsBackup
|
2017-08-22 16:06:04 +00:00
|
|
|
old_storage_size = OLD_STORAGE_SIZE(flags);
|
2017-08-22 16:09:10 +00:00
|
|
|
} else if (version <= 9) {
|
2017-12-13 09:53:27 +00:00
|
|
|
// added u2froot
|
2017-08-22 16:06:04 +00:00
|
|
|
old_storage_size = OLD_STORAGE_SIZE(u2froot);
|
2017-08-07 14:34:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
// erase newly added fields
|
|
|
|
if (old_storage_size != sizeof(Storage)) {
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
for (uint32_t offset = old_storage_size; offset < sizeof(Storage); offset += sizeof(uint32_t)) {
|
|
|
|
flash_program_word(FLASH_STORAGE_START + sizeof(storage_magic) + sizeof(storage_uuid) + offset, 0);
|
|
|
|
}
|
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
|
|
|
}
|
2017-08-07 14:34:07 +00:00
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
if (version <= 5) {
|
|
|
|
// convert PIN failure counter from version 5 format
|
2017-08-22 07:44:18 +00:00
|
|
|
uint32_t pinctr = storageRom->has_pin_failed_attempts ? storageRom->pin_failed_attempts : 0;
|
|
|
|
if (pinctr > 31) {
|
2016-04-27 16:12:03 +00:00
|
|
|
pinctr = 31;
|
2017-08-22 07:44:18 +00:00
|
|
|
}
|
2016-04-27 16:12:03 +00:00
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
// erase extra storage sector
|
|
|
|
flash_erase_sector(FLASH_META_SECTOR_LAST, FLASH_CR_PROGRAM_X32);
|
|
|
|
flash_program_word(FLASH_STORAGE_PINAREA, 0xffffffff << pinctr);
|
2017-08-22 17:00:25 +00:00
|
|
|
// erase storageRom.has_pin_failed_attempts and storageRom.pin_failed_attempts
|
2017-12-13 18:38:51 +00:00
|
|
|
#if !EMULATOR
|
|
|
|
// TODO: Fix this for emulator
|
2017-12-12 18:36:34 +00:00
|
|
|
_Static_assert(((uint32_t)&STORAGE_ROM->pin_failed_attempts & 3) == 0, "storage.pin_failed_attempts unaligned");
|
2017-12-13 18:38:51 +00:00
|
|
|
#endif
|
2017-12-12 18:36:34 +00:00
|
|
|
flash_program_byte((uint32_t)&storageRom->has_pin_failed_attempts, 0);
|
2017-08-22 17:00:25 +00:00
|
|
|
flash_program_word((uint32_t)&storageRom->pin_failed_attempts, 0);
|
2016-04-27 16:12:03 +00:00
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2016-05-24 21:44:40 +00:00
|
|
|
uint32_t *u2fptr = (uint32_t*) FLASH_STORAGE_U2FAREA;
|
2017-08-22 07:44:18 +00:00
|
|
|
while (*u2fptr == 0) {
|
2016-05-24 21:44:40 +00:00
|
|
|
u2fptr++;
|
2017-08-22 07:44:18 +00:00
|
|
|
}
|
2016-05-24 21:44:40 +00:00
|
|
|
storage_u2f_offset = 32 * (u2fptr - (uint32_t*) FLASH_STORAGE_U2FAREA);
|
|
|
|
uint32_t u2fword = *u2fptr;
|
|
|
|
while ((u2fword & 1) == 0) {
|
|
|
|
storage_u2f_offset++;
|
|
|
|
u2fword >>= 1;
|
|
|
|
}
|
2017-12-13 09:53:27 +00:00
|
|
|
// force recomputing u2f root for storage version < 9.
|
2017-12-13 16:56:46 +00:00
|
|
|
// this is done by re-setting the mnemonic, which triggers the computation
|
2017-12-13 09:53:27 +00:00
|
|
|
if (version < 9) {
|
|
|
|
storageUpdate.has_mnemonic = storageRom->has_mnemonic;
|
|
|
|
strlcpy(storageUpdate.mnemonic, storageRom->mnemonic, sizeof(storageUpdate.mnemonic));
|
|
|
|
}
|
|
|
|
// update storage version on flash
|
|
|
|
if (version != STORAGE_VERSION) {
|
|
|
|
storage_update();
|
|
|
|
}
|
2016-05-20 18:06:14 +00:00
|
|
|
return true;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_init(void)
|
|
|
|
{
|
2016-05-20 18:06:14 +00:00
|
|
|
if (!storage_from_flash()) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storage_wipe();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
void storage_generate_uuid(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
// set random uuid
|
2016-08-29 21:22:49 +00:00
|
|
|
random_buffer((uint8_t *)storage_uuid, sizeof(storage_uuid));
|
2014-04-29 12:26:51 +00:00
|
|
|
data2hex(storage_uuid, sizeof(storage_uuid), storage_uuid_str);
|
|
|
|
}
|
|
|
|
|
2015-03-31 14:31:29 +00:00
|
|
|
void session_clear(bool clear_pin)
|
2014-06-17 14:03:07 +00:00
|
|
|
{
|
2016-04-19 16:23:12 +00:00
|
|
|
sessionSeedCached = false;
|
|
|
|
memset(&sessionSeed, 0, sizeof(sessionSeed));
|
|
|
|
sessionPassphraseCached = false;
|
|
|
|
memset(&sessionPassphrase, 0, sizeof(sessionPassphrase));
|
2015-03-31 14:31:29 +00:00
|
|
|
if (clear_pin) {
|
|
|
|
sessionPinCached = false;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
static uint32_t storage_flash_words(uint32_t addr, const uint32_t *src, int nwords) {
|
2017-07-23 20:20:51 +00:00
|
|
|
for (int i = 0; i < nwords; i++) {
|
2016-04-27 17:23:02 +00:00
|
|
|
flash_program_word(addr, *src++);
|
2017-08-22 17:00:25 +00:00
|
|
|
addr += sizeof(uint32_t);
|
2016-04-27 17:23:02 +00:00
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-12-13 09:53:27 +00:00
|
|
|
static void get_u2froot_callback(uint32_t iter, uint32_t total)
|
|
|
|
{
|
|
|
|
layoutProgress(_("Updating"), 1000 * iter / total);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void storage_compute_u2froot(const char* mnemonic, HDNodeType *u2froot) {
|
|
|
|
static CONFIDENTIAL HDNode node;
|
|
|
|
char oldTiny = usbTiny(1);
|
|
|
|
mnemonic_to_seed(mnemonic, "", sessionSeed, get_u2froot_callback); // BIP-0039
|
|
|
|
usbTiny(oldTiny);
|
|
|
|
hdnode_from_seed(sessionSeed, 64, NIST256P1_NAME, &node);
|
|
|
|
hdnode_private_ckd(&node, U2F_KEY_PATH);
|
|
|
|
u2froot->depth = node.depth;
|
|
|
|
u2froot->child_num = U2F_KEY_PATH;
|
|
|
|
u2froot->chain_code.size = sizeof(node.chain_code);
|
|
|
|
memcpy(u2froot->chain_code.bytes, node.chain_code, sizeof(node.chain_code));
|
|
|
|
u2froot->has_private_key = true;
|
|
|
|
u2froot->private_key.size = sizeof(node.private_key);
|
|
|
|
memcpy(u2froot->private_key.bytes, node.private_key, sizeof(node.private_key));
|
|
|
|
memset(&node, 0, sizeof(node));
|
|
|
|
session_clear(false); // invalidate seed cache
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
// if storage is filled in - update fields that has has_field set to true
|
|
|
|
// if storage is NULL - do not backup original content - essentialy a wipe
|
|
|
|
static void storage_commit_locked(bool update)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
if (update) {
|
|
|
|
if (storageUpdate.has_passphrase_protection) {
|
|
|
|
sessionSeedCached = false;
|
|
|
|
sessionPassphraseCached = false;
|
|
|
|
}
|
|
|
|
if (storageUpdate.has_pin) {
|
|
|
|
sessionPinCached = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
storageUpdate.version = STORAGE_VERSION;
|
|
|
|
if (!storageUpdate.has_node && !storageUpdate.has_mnemonic) {
|
|
|
|
storageUpdate.has_node = storageRom->has_node;
|
|
|
|
memcpy(&storageUpdate.node, &storageRom->node, sizeof(HDNodeType));
|
|
|
|
storageUpdate.has_mnemonic = storageRom->has_mnemonic;
|
|
|
|
strlcpy(storageUpdate.mnemonic, storageRom->mnemonic, sizeof(storageUpdate.mnemonic));
|
2017-12-13 09:53:27 +00:00
|
|
|
storageUpdate.has_u2froot = storageRom->has_u2froot;
|
|
|
|
memcpy(&storageUpdate.u2froot, &storageRom->u2froot, sizeof(HDNodeType));
|
|
|
|
} else if (storageUpdate.has_mnemonic) {
|
|
|
|
storageUpdate.has_u2froot = true;
|
|
|
|
storage_compute_u2froot(storageUpdate.mnemonic, &storageUpdate.u2froot);
|
2017-08-22 17:00:25 +00:00
|
|
|
}
|
|
|
|
if (!storageUpdate.has_passphrase_protection) {
|
|
|
|
storageUpdate.has_passphrase_protection = storageRom->has_passphrase_protection;
|
|
|
|
storageUpdate.passphrase_protection = storageRom->passphrase_protection;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_pin) {
|
|
|
|
storageUpdate.has_pin = storageRom->has_pin;
|
|
|
|
strlcpy(storageUpdate.pin, storageRom->pin, sizeof(storageUpdate.pin));
|
|
|
|
} else if (!storageUpdate.pin[0]) {
|
|
|
|
storageUpdate.has_pin = false;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_language) {
|
|
|
|
storageUpdate.has_language = storageRom->has_language;
|
|
|
|
strlcpy(storageUpdate.language, storageRom->language, sizeof(storageUpdate.language));
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_label) {
|
|
|
|
storageUpdate.has_label = storageRom->has_label;
|
|
|
|
strlcpy(storageUpdate.label, storageRom->label, sizeof(storageUpdate.label));
|
|
|
|
} else if (!storageUpdate.label[0]) {
|
|
|
|
storageUpdate.has_label = false;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_imported) {
|
|
|
|
storageUpdate.has_imported = storageRom->has_imported;
|
|
|
|
storageUpdate.imported = storageRom->imported;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_homescreen) {
|
|
|
|
storageUpdate.has_homescreen = storageRom->has_homescreen;
|
|
|
|
memcpy(&storageUpdate.homescreen, &storageRom->homescreen, sizeof(storageUpdate.homescreen));
|
|
|
|
} else if (storageUpdate.homescreen.size == 0) {
|
|
|
|
storageUpdate.has_homescreen = false;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_u2f_counter) {
|
|
|
|
storageUpdate.has_u2f_counter = storageRom->has_u2f_counter;
|
|
|
|
storageUpdate.u2f_counter = storageRom->u2f_counter;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_needs_backup) {
|
|
|
|
storageUpdate.has_needs_backup = storageRom->has_needs_backup;
|
|
|
|
storageUpdate.needs_backup = storageRom->needs_backup;
|
|
|
|
}
|
|
|
|
if (!storageUpdate.has_flags) {
|
|
|
|
storageUpdate.has_flags = storageRom->has_flags;
|
|
|
|
storageUpdate.flags = storageRom->flags;
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
// backup meta
|
2017-08-22 17:00:25 +00:00
|
|
|
uint32_t meta_backup[FLASH_META_DESC_LEN / sizeof(uint32_t)];
|
2016-04-27 17:23:02 +00:00
|
|
|
memcpy(meta_backup, (uint8_t*)FLASH_META_START, FLASH_META_DESC_LEN);
|
2016-04-27 16:12:03 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
// erase storage
|
2016-04-27 16:12:03 +00:00
|
|
|
flash_erase_sector(FLASH_META_SECTOR_FIRST, FLASH_CR_PROGRAM_X32);
|
2017-08-22 17:00:25 +00:00
|
|
|
|
|
|
|
// copy meta back
|
2016-04-27 17:23:02 +00:00
|
|
|
uint32_t flash = FLASH_META_START;
|
2017-08-22 17:00:25 +00:00
|
|
|
flash = storage_flash_words(flash, meta_backup, FLASH_META_DESC_LEN / sizeof(uint32_t));
|
|
|
|
|
2016-04-27 17:23:02 +00:00
|
|
|
// copy storage
|
2017-08-22 17:00:25 +00:00
|
|
|
flash = storage_flash_words(flash, &storage_magic, sizeof(storage_magic) / sizeof(uint32_t));
|
|
|
|
flash = storage_flash_words(flash, storage_uuid, sizeof(storage_uuid) / sizeof(uint32_t));
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
flash = storage_flash_words(flash, (const uint32_t *)&storageUpdate, sizeof(storageUpdate) / sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
storage_clear_update();
|
|
|
|
|
2016-04-27 17:23:02 +00:00
|
|
|
// fill remainder with zero for future extensions
|
|
|
|
while (flash < FLASH_STORAGE_PINAREA) {
|
|
|
|
flash_program_word(flash, 0);
|
2017-08-22 17:00:25 +00:00
|
|
|
flash += sizeof(uint32_t);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2016-05-24 21:44:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
void storage_clear_update(void)
|
|
|
|
{
|
|
|
|
memset(&storageUpdate, 0, sizeof(storageUpdate));
|
|
|
|
}
|
|
|
|
|
|
|
|
void storage_update(void)
|
2016-05-24 21:44:40 +00:00
|
|
|
{
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
2017-08-22 17:00:25 +00:00
|
|
|
storage_commit_locked(true);
|
2014-04-29 12:26:51 +00:00
|
|
|
flash_lock();
|
2016-04-27 16:12:03 +00:00
|
|
|
storage_check_flash_errors();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_loadDevice(LoadDevice *msg)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
session_clear(true);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_imported = true;
|
|
|
|
storageUpdate.imported = true;
|
2014-05-25 22:58:17 +00:00
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
storage_setPin(msg->has_pin ? msg->pin : "");
|
|
|
|
storage_setPassphraseProtection(msg->has_passphrase_protection && msg->passphrase_protection);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
if (msg->has_node) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_node = true;
|
|
|
|
storageUpdate.has_mnemonic = false;
|
|
|
|
memcpy(&storageUpdate.node, &(msg->node), sizeof(HDNodeType));
|
2016-04-19 16:23:12 +00:00
|
|
|
sessionSeedCached = false;
|
|
|
|
memset(&sessionSeed, 0, sizeof(sessionSeed));
|
2014-04-29 12:26:51 +00:00
|
|
|
} else if (msg->has_mnemonic) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_mnemonic = true;
|
|
|
|
storageUpdate.has_node = false;
|
|
|
|
strlcpy(storageUpdate.mnemonic, msg->mnemonic, sizeof(storageUpdate.mnemonic));
|
2016-04-19 16:23:12 +00:00
|
|
|
sessionSeedCached = false;
|
|
|
|
memset(&sessionSeed, 0, sizeof(sessionSeed));
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
if (msg->has_language && msg->language) {
|
|
|
|
storageUpdate.has_language = true;
|
|
|
|
strlcpy(storageUpdate.language, msg->language, sizeof(storageUpdate.language));
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
storage_setLabel(msg->has_label ? msg->label : "");
|
2016-10-31 15:20:15 +00:00
|
|
|
|
|
|
|
if (msg->has_u2f_counter) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_u2f_counter = true;
|
|
|
|
storageUpdate.u2f_counter = msg->u2f_counter - storage_u2f_offset;
|
2016-10-31 15:20:15 +00:00
|
|
|
}
|
2017-08-22 17:00:25 +00:00
|
|
|
|
|
|
|
storage_update();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_setLabel(const char *label)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_label = true;
|
2014-04-29 12:26:51 +00:00
|
|
|
if (!label) return;
|
2017-08-22 17:00:25 +00:00
|
|
|
strlcpy(storageUpdate.label, label, sizeof(storageUpdate.label));
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_setLanguage(const char *lang)
|
|
|
|
{
|
|
|
|
if (!lang) return;
|
|
|
|
// sanity check
|
|
|
|
if (strcmp(lang, "english") == 0) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_language = true;
|
|
|
|
strlcpy(storageUpdate.language, lang, sizeof(storageUpdate.language));
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 18:29:27 +00:00
|
|
|
void storage_setPassphraseProtection(bool passphrase_protection)
|
|
|
|
{
|
2016-04-19 16:23:12 +00:00
|
|
|
sessionSeedCached = false;
|
2014-12-13 18:29:27 +00:00
|
|
|
sessionPassphraseCached = false;
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_passphrase_protection = true;
|
|
|
|
storageUpdate.passphrase_protection = passphrase_protection;
|
2014-12-13 18:29:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:17:05 +00:00
|
|
|
bool storage_hasPassphraseProtection(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_passphrase_protection && storageRom->passphrase_protection;
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:27:07 +00:00
|
|
|
void storage_setHomescreen(const uint8_t *data, uint32_t size)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_homescreen = true;
|
2015-02-04 20:27:07 +00:00
|
|
|
if (data && size == 1024) {
|
2017-08-22 17:00:25 +00:00
|
|
|
memcpy(storageUpdate.homescreen.bytes, data, size);
|
|
|
|
storageUpdate.homescreen.size = size;
|
2015-02-04 20:27:07 +00:00
|
|
|
} else {
|
2017-08-22 17:00:25 +00:00
|
|
|
memset(storageUpdate.homescreen.bytes, 0, sizeof(storageUpdate.homescreen.bytes));
|
|
|
|
storageUpdate.homescreen.size = 0;
|
2015-02-04 20:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:00:25 +00:00
|
|
|
static void get_root_node_callback(uint32_t iter, uint32_t total)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2016-11-17 19:07:41 +00:00
|
|
|
usbSleep(1);
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Waking up"), 1000 * iter / total);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 23:49:20 +00:00
|
|
|
const uint8_t *storage_getSeed(bool usePassphrase)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
// root node is properly cached
|
2016-05-19 23:49:20 +00:00
|
|
|
if (usePassphrase == sessionSeedUsesPassphrase
|
|
|
|
&& sessionSeedCached) {
|
2016-04-19 16:23:12 +00:00
|
|
|
return sessionSeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if storage has mnemonic, convert it to node and use it
|
2017-08-22 07:44:18 +00:00
|
|
|
if (storageRom->has_mnemonic) {
|
2016-05-19 23:49:20 +00:00
|
|
|
if (usePassphrase && !protectPassphrase()) {
|
2016-04-19 16:23:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-09 15:06:50 +00:00
|
|
|
// if storage was not imported (i.e. it was properly generated or recovered)
|
2017-08-22 07:44:18 +00:00
|
|
|
if (!storageRom->has_imported || !storageRom->imported) {
|
2016-11-09 15:06:50 +00:00
|
|
|
// test whether mnemonic is a valid BIP-0039 mnemonic
|
2017-08-22 07:44:18 +00:00
|
|
|
if (!mnemonic_check(storageRom->mnemonic)) {
|
2016-11-09 15:06:50 +00:00
|
|
|
// and if not then halt the device
|
|
|
|
storage_show_error();
|
|
|
|
}
|
|
|
|
}
|
2016-07-07 16:37:37 +00:00
|
|
|
char oldTiny = usbTiny(1);
|
2017-08-22 07:44:18 +00:00
|
|
|
mnemonic_to_seed(storageRom->mnemonic, usePassphrase ? sessionPassphrase : "", sessionSeed, get_root_node_callback); // BIP-0039
|
2016-07-07 16:37:37 +00:00
|
|
|
usbTiny(oldTiny);
|
2016-04-19 16:23:12 +00:00
|
|
|
sessionSeedCached = true;
|
2016-05-19 23:49:20 +00:00
|
|
|
sessionSeedUsesPassphrase = usePassphrase;
|
2016-04-19 16:23:12 +00:00
|
|
|
return sessionSeed;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 16:23:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-13 09:53:27 +00:00
|
|
|
bool storage_getU2FRoot(HDNode *node)
|
|
|
|
{
|
|
|
|
return storageRom->has_u2froot
|
|
|
|
&& hdnode_from_xprv(storageRom->u2froot.depth, storageRom->u2froot.child_num, storageRom->u2froot.chain_code.bytes, storageRom->u2froot.private_key.bytes, NIST256P1_NAME, node);
|
|
|
|
}
|
|
|
|
|
2016-05-19 23:49:20 +00:00
|
|
|
bool storage_getRootNode(HDNode *node, const char *curve, bool usePassphrase)
|
2016-04-19 16:23:12 +00:00
|
|
|
{
|
2014-04-29 12:26:51 +00:00
|
|
|
// if storage has node, decrypt and use it
|
2017-08-22 07:44:18 +00:00
|
|
|
if (storageRom->has_node && strcmp(curve, SECP256K1_NAME) == 0) {
|
2014-04-29 12:26:51 +00:00
|
|
|
if (!protectPassphrase()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-22 07:44:18 +00:00
|
|
|
if (hdnode_from_xprv(storageRom->node.depth, storageRom->node.child_num, storageRom->node.chain_code.bytes, storageRom->node.private_key.bytes, curve, node) == 0) {
|
2014-12-16 17:28:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-22 07:44:18 +00:00
|
|
|
if (storageRom->has_passphrase_protection && storageRom->passphrase_protection && sessionPassphraseCached && strlen(sessionPassphrase) > 0) {
|
2014-04-29 12:26:51 +00:00
|
|
|
// decrypt hd node
|
2014-06-07 12:21:59 +00:00
|
|
|
uint8_t secret[64];
|
2016-05-12 19:05:17 +00:00
|
|
|
PBKDF2_HMAC_SHA512_CTX pctx;
|
|
|
|
pbkdf2_hmac_sha512_Init(&pctx, (const uint8_t *)sessionPassphrase, strlen(sessionPassphrase), (const uint8_t *)"TREZORHD", 8);
|
|
|
|
get_root_node_callback(0, BIP39_PBKDF2_ROUNDS);
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
pbkdf2_hmac_sha512_Update(&pctx, BIP39_PBKDF2_ROUNDS / 8);
|
|
|
|
get_root_node_callback((i + 1) * BIP39_PBKDF2_ROUNDS / 8, BIP39_PBKDF2_ROUNDS);
|
|
|
|
}
|
|
|
|
pbkdf2_hmac_sha512_Final(&pctx, secret);
|
2014-06-07 12:21:59 +00:00
|
|
|
aes_decrypt_ctx ctx;
|
|
|
|
aes_decrypt_key256(secret, &ctx);
|
2016-04-19 16:23:12 +00:00
|
|
|
aes_cbc_decrypt(node->chain_code, node->chain_code, 32, secret + 32, &ctx);
|
|
|
|
aes_cbc_decrypt(node->private_key, node->private_key, 32, secret + 32, &ctx);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-19 23:49:20 +00:00
|
|
|
const uint8_t *seed = storage_getSeed(usePassphrase);
|
2016-04-19 16:23:12 +00:00
|
|
|
if (seed == NULL) {
|
|
|
|
return false;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2016-04-19 16:23:12 +00:00
|
|
|
|
|
|
|
return hdnode_from_seed(seed, 64, curve, node);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *storage_getLabel(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_label ? storageRom->label : 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *storage_getLanguage(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_language ? storageRom->language : 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 20:27:07 +00:00
|
|
|
const uint8_t *storage_getHomescreen(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return (storageRom->has_homescreen && storageRom->homescreen.size == 1024) ? storageRom->homescreen.bytes : 0;
|
2015-02-04 20:27:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:17:05 +00:00
|
|
|
void storage_setMnemonic(const char *mnemonic)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_mnemonic = true;
|
|
|
|
strlcpy(storageUpdate.mnemonic, mnemonic, sizeof(storageUpdate.mnemonic));
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_hasNode(void)
|
|
|
|
{
|
|
|
|
return storageRom->has_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HDNode *storage_getNode(void)
|
|
|
|
{
|
|
|
|
return storageRom->has_node ? (const HDNode *)&storageRom->node : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_hasMnemonic(void)
|
|
|
|
{
|
|
|
|
return storageRom->has_mnemonic;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *storage_getMnemonic(void)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
return storageUpdate.has_mnemonic ? storageUpdate.mnemonic
|
|
|
|
: storageRom->has_mnemonic ? storageRom->mnemonic : 0;
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 13:44:01 +00:00
|
|
|
/* Check whether mnemonic matches storage. The mnemonic must be
|
|
|
|
* a null-terminated string.
|
2015-02-22 14:19:13 +00:00
|
|
|
*/
|
2017-06-26 13:44:01 +00:00
|
|
|
bool storage_containsMnemonic(const char *mnemonic) {
|
|
|
|
/* The execution time of the following code only depends on the
|
|
|
|
* (public) input. This avoids timing attacks.
|
|
|
|
*/
|
|
|
|
char diff = 0;
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (; mnemonic[i]; i++) {
|
2017-08-22 07:44:18 +00:00
|
|
|
diff |= (storageRom->mnemonic[i] - mnemonic[i]);
|
2017-06-26 13:44:01 +00:00
|
|
|
}
|
2017-08-22 07:44:18 +00:00
|
|
|
diff |= storageRom->mnemonic[i];
|
2017-06-26 13:44:01 +00:00
|
|
|
return diff == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether pin matches storage. The pin must be
|
|
|
|
* a null-terminated string with at most 9 characters.
|
|
|
|
*/
|
|
|
|
bool storage_containsPin(const char *pin)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2015-02-22 14:19:13 +00:00
|
|
|
/* The execution time of the following code only depends on the
|
|
|
|
* (public) input. This avoids timing attacks.
|
|
|
|
*/
|
|
|
|
char diff = 0;
|
|
|
|
uint32_t i = 0;
|
|
|
|
while (pin[i]) {
|
2017-08-22 07:44:18 +00:00
|
|
|
diff |= storageRom->pin[i] - pin[i];
|
2015-02-22 14:19:13 +00:00
|
|
|
i++;
|
|
|
|
}
|
2017-08-22 07:44:18 +00:00
|
|
|
diff |= storageRom->pin[i];
|
2015-02-22 14:19:13 +00:00
|
|
|
return diff == 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_hasPin(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_pin && storageRom->pin[0] != 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_setPin(const char *pin)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_pin = true;
|
|
|
|
strlcpy(storageUpdate.pin, pin, sizeof(storageUpdate.pin));
|
2014-04-29 12:26:51 +00:00
|
|
|
sessionPinCached = false;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:17:05 +00:00
|
|
|
const char *storage_getPin(void)
|
|
|
|
{
|
|
|
|
return storageRom->has_pin ? storageRom->pin : 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
void session_cachePassphrase(const char *passphrase)
|
|
|
|
{
|
|
|
|
strlcpy(sessionPassphrase, passphrase, sizeof(sessionPassphrase));
|
|
|
|
sessionPassphraseCached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool session_isPassphraseCached(void)
|
|
|
|
{
|
|
|
|
return sessionPassphraseCached;
|
|
|
|
}
|
|
|
|
|
2015-02-22 14:19:13 +00:00
|
|
|
void session_cachePin(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
sessionPinCached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool session_isPinCached(void)
|
|
|
|
{
|
2015-02-22 14:19:13 +00:00
|
|
|
return sessionPinCached;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
void storage_clearPinArea(void)
|
2016-05-20 15:00:10 +00:00
|
|
|
{
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
flash_erase_sector(FLASH_META_SECTOR_LAST, FLASH_CR_PROGRAM_X32);
|
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
2016-05-24 21:44:40 +00:00
|
|
|
storage_u2f_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called when u2f area or pin area overflows
|
|
|
|
static void storage_area_recycle(uint32_t new_pinfails)
|
|
|
|
{
|
2016-05-27 12:03:20 +00:00
|
|
|
// first clear storage marker. In case of a failure below it is better
|
|
|
|
// to clear the storage than to allow restarting with zero PIN failures
|
|
|
|
flash_program_word(FLASH_STORAGE_START, 0);
|
|
|
|
if (*(uint32_t *)FLASH_STORAGE_START != 0) {
|
|
|
|
storage_show_error();
|
|
|
|
}
|
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
// erase storage sector
|
|
|
|
flash_erase_sector(FLASH_META_SECTOR_LAST, FLASH_CR_PROGRAM_X32);
|
|
|
|
flash_program_word(FLASH_STORAGE_PINAREA, new_pinfails);
|
2016-05-27 12:03:20 +00:00
|
|
|
if (*(uint32_t *)FLASH_STORAGE_PINAREA != new_pinfails) {
|
|
|
|
storage_show_error();
|
|
|
|
}
|
|
|
|
|
2016-05-24 21:44:40 +00:00
|
|
|
if (storage_u2f_offset > 0) {
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_u2f_counter = true;
|
|
|
|
storageUpdate.u2f_counter += storage_u2f_offset;
|
2016-05-24 21:44:40 +00:00
|
|
|
storage_u2f_offset = 0;
|
2017-08-22 17:00:25 +00:00
|
|
|
storage_commit_locked(true);
|
2016-05-24 21:44:40 +00:00
|
|
|
}
|
2016-05-20 15:00:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
void storage_resetPinFails(uint32_t *pinfailsptr)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2016-04-27 16:12:03 +00:00
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
2016-05-24 21:44:40 +00:00
|
|
|
if ((uint32_t) (pinfailsptr + 1)
|
|
|
|
>= FLASH_STORAGE_PINAREA + FLASH_STORAGE_PINAREA_LEN) {
|
|
|
|
// recycle extra storage sector
|
|
|
|
storage_area_recycle(0xffffffff);
|
2016-04-27 16:12:03 +00:00
|
|
|
} else {
|
|
|
|
flash_program_word((uint32_t) pinfailsptr, 0);
|
|
|
|
}
|
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
bool storage_increasePinFails(uint32_t *pinfailsptr)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2016-04-27 16:12:03 +00:00
|
|
|
uint32_t newctr = *pinfailsptr << 1;
|
|
|
|
// counter already at maximum, we do not increase it any more
|
|
|
|
// return success so that a good pin is accepted
|
|
|
|
if (!newctr)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
flash_program_word((uint32_t) pinfailsptr, newctr);
|
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
|
|
|
|
|
|
|
return *pinfailsptr == newctr;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:12:03 +00:00
|
|
|
uint32_t *storage_getPinFailsPtr(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2016-04-27 16:12:03 +00:00
|
|
|
uint32_t *pinfailsptr = (uint32_t *) FLASH_STORAGE_PINAREA;
|
|
|
|
while (*pinfailsptr == 0)
|
|
|
|
pinfailsptr++;
|
|
|
|
return pinfailsptr;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool storage_isInitialized(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_node || storageRom->has_mnemonic;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2015-11-02 23:08:18 +00:00
|
|
|
|
2017-08-22 07:17:05 +00:00
|
|
|
bool storage_isImported(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_imported && storageRom->imported;
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storage_setImported(bool imported)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_imported = true;
|
|
|
|
storageUpdate.imported = imported;
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 17:45:12 +00:00
|
|
|
bool storage_needsBackup(void)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
return storageUpdate.has_needs_backup ? storageUpdate.needs_backup
|
|
|
|
: storageRom->has_needs_backup && storageRom->needs_backup;
|
2017-06-26 17:45:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 07:17:05 +00:00
|
|
|
void storage_setNeedsBackup(bool needs_backup)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_needs_backup = true;
|
|
|
|
storageUpdate.needs_backup = needs_backup;
|
2017-08-22 07:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 16:36:09 +00:00
|
|
|
void storage_applyFlags(uint32_t flags)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
if ((storageRom->flags | flags) == storageRom->flags) {
|
2017-07-17 16:36:09 +00:00
|
|
|
return; // no new flags
|
|
|
|
}
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_flags = true;
|
|
|
|
storageUpdate.flags |= flags;
|
2017-07-17 16:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t storage_getFlags(void)
|
|
|
|
{
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->has_flags ? storageRom->flags : 0;
|
2017-07-17 16:36:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 23:08:18 +00:00
|
|
|
uint32_t storage_nextU2FCounter(void)
|
|
|
|
{
|
2016-05-24 21:44:40 +00:00
|
|
|
uint32_t *ptr = ((uint32_t *) FLASH_STORAGE_U2FAREA) + (storage_u2f_offset / 32);
|
|
|
|
uint32_t newval = 0xfffffffe << (storage_u2f_offset & 31);
|
|
|
|
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
flash_program_word((uint32_t) ptr, newval);
|
|
|
|
storage_u2f_offset++;
|
|
|
|
if (storage_u2f_offset >= 8 * FLASH_STORAGE_U2FAREA_LEN) {
|
|
|
|
storage_area_recycle(*storage_getPinFailsPtr());
|
2015-11-02 23:08:18 +00:00
|
|
|
}
|
2016-05-24 21:44:40 +00:00
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
2017-08-22 07:44:18 +00:00
|
|
|
return storageRom->u2f_counter + storage_u2f_offset;
|
2015-11-02 23:08:18 +00:00
|
|
|
}
|
2016-06-12 21:39:28 +00:00
|
|
|
|
|
|
|
void storage_setU2FCounter(uint32_t u2fcounter)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
storageUpdate.has_u2f_counter = true;
|
|
|
|
storageUpdate.u2f_counter = u2fcounter - storage_u2f_offset;
|
2016-06-12 21:39:28 +00:00
|
|
|
}
|
2017-07-31 00:26:28 +00:00
|
|
|
|
|
|
|
void storage_wipe(void)
|
|
|
|
{
|
2017-08-22 17:00:25 +00:00
|
|
|
session_clear(true);
|
|
|
|
storage_generate_uuid();
|
|
|
|
|
|
|
|
flash_clear_status_flags();
|
|
|
|
flash_unlock();
|
|
|
|
storage_commit_locked(false);
|
|
|
|
flash_lock();
|
|
|
|
storage_check_flash_errors();
|
|
|
|
|
2017-07-31 00:26:28 +00:00
|
|
|
storage_clearPinArea();
|
|
|
|
}
|