2017-10-17 17:18:16 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2017-10-17 17:18:16 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-10-17 17:18:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
#include "common.h"
|
2017-10-17 17:18:16 +00:00
|
|
|
#include "norcow.h"
|
2017-12-14 23:06:09 +00:00
|
|
|
#include "flash.h"
|
|
|
|
|
2017-12-14 16:14:15 +00:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/obj.h"
|
2017-10-17 17:18:16 +00:00
|
|
|
|
|
|
|
// Norcow storage key of configured PIN.
|
|
|
|
#define PIN_KEY 0x0000
|
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
// Maximum PIN length.
|
|
|
|
#define PIN_MAXLEN 32
|
|
|
|
|
2017-12-12 22:47:42 +00:00
|
|
|
// Byte-length of flash section containing fail counters.
|
|
|
|
#define PIN_FAIL_KEY 0x0001
|
|
|
|
#define PIN_FAIL_SECTOR_SIZE 32
|
|
|
|
|
|
|
|
// Maximum number of failed unlock attempts.
|
|
|
|
#define PIN_MAX_TRIES 15
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
static secbool initialized = secfalse;
|
|
|
|
static secbool unlocked = secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
|
2017-12-09 13:48:49 +00:00
|
|
|
void storage_init(void)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-11-08 17:08:19 +00:00
|
|
|
initialized = secfalse;
|
|
|
|
unlocked = secfalse;
|
2017-12-09 13:48:49 +00:00
|
|
|
flash_init();
|
|
|
|
norcow_init();
|
2017-10-27 15:49:30 +00:00
|
|
|
initialized = sectrue;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 16:24:19 +00:00
|
|
|
static secbool pin_fails_reset(uint16_t ofs)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-12-16 16:24:19 +00:00
|
|
|
return norcow_update(PIN_FAIL_KEY, ofs, 0);
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 22:47:42 +00:00
|
|
|
static secbool pin_fails_increase(const uint32_t *ptr, uint16_t ofs)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-12-12 22:47:42 +00:00
|
|
|
uint32_t ctr = *ptr;
|
2017-10-17 17:18:16 +00:00
|
|
|
ctr = ctr << 1;
|
|
|
|
|
2017-12-12 22:47:42 +00:00
|
|
|
if (sectrue != norcow_update(PIN_FAIL_KEY, ofs, ctr)) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 22:47:42 +00:00
|
|
|
uint32_t check = *ptr;
|
2017-10-27 15:49:30 +00:00
|
|
|
if (ctr != check) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
return sectrue;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pin_fails_check_max(uint32_t ctr)
|
|
|
|
{
|
2017-12-11 22:11:43 +00:00
|
|
|
if (~ctr >= (1 << PIN_MAX_TRIES)) {
|
2017-12-09 13:48:49 +00:00
|
|
|
norcow_wipe();
|
2017-12-07 14:31:23 +00:00
|
|
|
ensure(secfalse, "pin_fails_check_max");
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 09:27:36 +00:00
|
|
|
static secbool pin_cmp(const uint32_t pin)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-10-24 11:55:29 +00:00
|
|
|
const void *spin = NULL;
|
|
|
|
uint16_t spinlen = 0;
|
|
|
|
norcow_get(PIN_KEY, &spin, &spinlen);
|
2017-12-15 09:27:36 +00:00
|
|
|
if (NULL != spin && spinlen == sizeof(uint32_t)) {
|
|
|
|
return sectrue * (pin == *(const uint32_t*)spin);
|
2017-11-08 17:08:19 +00:00
|
|
|
} else {
|
2017-12-15 09:27:36 +00:00
|
|
|
return sectrue * (1 == pin);
|
2017-11-08 17:08:19 +00:00
|
|
|
}
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:14:15 +00:00
|
|
|
static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-12-12 22:47:42 +00:00
|
|
|
const void *vpinfail;
|
|
|
|
uint16_t pinfaillen;
|
2017-12-14 16:14:15 +00:00
|
|
|
unsigned int ofs;
|
2017-12-12 22:47:42 +00:00
|
|
|
// The PIN_FAIL_KEY points to an area of words, initialized to
|
|
|
|
// 0xffffffff (meaning no pin failures). The first non-zero word
|
|
|
|
// in this area is the current pin failure counter. If PIN_FAIL_KEY
|
|
|
|
// has no configuration or is empty, the pin failure counter is 0.
|
|
|
|
// We rely on the fact that flash allows to clear bits and we clear one
|
|
|
|
// bit to indicate pin failure. On success, the word is set to 0,
|
|
|
|
// indicating that the next word is the pin failure counter.
|
|
|
|
|
|
|
|
// Find the current pin failure counter
|
|
|
|
if (secfalse != norcow_get(PIN_FAIL_KEY, &vpinfail, &pinfaillen)) {
|
2017-12-14 16:14:15 +00:00
|
|
|
*pinfail = vpinfail;
|
2017-12-12 22:47:42 +00:00
|
|
|
for (ofs = 0; ofs < pinfaillen / sizeof(uint32_t); ofs++) {
|
2017-12-14 16:14:15 +00:00
|
|
|
if (((const uint32_t *) vpinfail)[ofs]) {
|
|
|
|
*pinfail = vpinfail;
|
|
|
|
*pofs = ofs;
|
|
|
|
return sectrue;
|
2017-12-12 22:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 16:14:15 +00:00
|
|
|
|
|
|
|
// No pin failure section, or all entries used -> create a new one.
|
|
|
|
uint32_t pinarea[PIN_FAIL_SECTOR_SIZE];
|
|
|
|
memset(pinarea, 0xff, sizeof(pinarea));
|
|
|
|
if (sectrue != norcow_set(PIN_FAIL_KEY, pinarea, sizeof(pinarea))) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
if (sectrue != norcow_get(PIN_FAIL_KEY, &vpinfail, &pinfaillen)) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
*pinfail = vpinfail;
|
|
|
|
*pofs = 0;
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
2018-02-26 22:02:44 +00:00
|
|
|
secbool storage_check_pin(uint32_t pin, mp_obj_t callback)
|
2017-12-14 16:14:15 +00:00
|
|
|
{
|
|
|
|
const uint32_t *pinfail = NULL;
|
|
|
|
uint32_t ofs;
|
|
|
|
uint32_t ctr;
|
|
|
|
|
|
|
|
// Get the pin failure counter
|
|
|
|
if (pin_get_fails(&pinfail, &ofs) != sectrue) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-12-12 22:47:42 +00:00
|
|
|
|
|
|
|
// Read current failure counter
|
|
|
|
ctr = pinfail[ofs];
|
2017-12-14 16:14:15 +00:00
|
|
|
// Wipe storage if too many failures
|
2017-10-17 17:18:16 +00:00
|
|
|
pin_fails_check_max(ctr);
|
|
|
|
|
|
|
|
// Sleep for ~ctr seconds before checking the PIN.
|
2018-02-24 20:02:14 +00:00
|
|
|
uint32_t progress;
|
2017-10-17 17:18:16 +00:00
|
|
|
for (uint32_t wait = ~ctr; wait > 0; wait--) {
|
2018-02-24 20:02:14 +00:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
if (mp_obj_is_callable(callback)) {
|
|
|
|
if ((~ctr) > 1000000) { // precise enough
|
|
|
|
progress = (~ctr - wait) / ((~ctr) / 1000);
|
|
|
|
} else {
|
|
|
|
progress = ((~ctr - wait) * 10 + i) * 100 / (~ctr);
|
|
|
|
}
|
|
|
|
mp_call_function_2(callback, mp_obj_new_int(wait), mp_obj_new_int(progress));
|
|
|
|
}
|
|
|
|
hal_delay(100);
|
2017-12-14 18:01:41 +00:00
|
|
|
}
|
2018-02-24 20:02:14 +00:00
|
|
|
}
|
|
|
|
// Show last frame if we were waiting
|
|
|
|
if ((~ctr > 0) && mp_obj_is_callable(callback)) {
|
|
|
|
mp_call_function_2(callback, mp_obj_new_int(0), mp_obj_new_int(1000));
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// First, we increase PIN fail counter in storage, even before checking the
|
|
|
|
// PIN. If the PIN is correct, we reset the counter afterwards. If not, we
|
|
|
|
// check if this is the last allowed attempt.
|
2017-12-12 22:47:42 +00:00
|
|
|
if (sectrue != pin_fails_increase(pinfail + ofs, ofs * sizeof(uint32_t))) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-12-15 09:27:36 +00:00
|
|
|
if (sectrue != pin_cmp(pin)) {
|
2017-12-14 16:14:15 +00:00
|
|
|
// Wipe storage if too many failures
|
2017-10-17 17:18:16 +00:00
|
|
|
pin_fails_check_max(ctr << 1);
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-12-12 22:47:42 +00:00
|
|
|
// Finally set the counter to 0 to indicate success.
|
2017-12-16 16:24:19 +00:00
|
|
|
return pin_fails_reset(ofs * sizeof(uint32_t));
|
2017-11-08 17:08:19 +00:00
|
|
|
}
|
2017-11-06 16:26:13 +00:00
|
|
|
|
2017-12-15 09:27:36 +00:00
|
|
|
secbool storage_unlock(const uint32_t pin, mp_obj_t callback)
|
2017-11-08 17:08:19 +00:00
|
|
|
{
|
|
|
|
unlocked = secfalse;
|
2018-02-26 22:02:44 +00:00
|
|
|
if (sectrue == initialized && sectrue == storage_check_pin(pin, callback)) {
|
2017-11-08 17:08:19 +00:00
|
|
|
unlocked = sectrue;
|
|
|
|
}
|
2017-11-06 16:26:13 +00:00
|
|
|
return unlocked;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_get(uint16_t key, const void **val, uint16_t *len)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2018-01-13 14:21:40 +00:00
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
// APP == 0 is reserved for PIN related values
|
|
|
|
if (sectrue != initialized || app == 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
// top bit of APP set indicates the value can be read from unlocked device
|
|
|
|
if (sectrue != unlocked && ((app & 0x80) == 0)) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
return norcow_get(key, val, len);
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_set(uint16_t key, const void *val, uint16_t len)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2018-01-13 14:21:40 +00:00
|
|
|
const uint8_t app = key >> 8;
|
|
|
|
// APP == 0 is reserved for PIN related values
|
|
|
|
if (sectrue != initialized || sectrue != unlocked || app == 0) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
return norcow_set(key, val, len);
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_has_pin(void)
|
2017-10-24 11:55:29 +00:00
|
|
|
{
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != initialized) {
|
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2018-02-05 14:36:59 +00:00
|
|
|
return sectrue == pin_cmp(1) ? secfalse : sectrue;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 09:27:36 +00:00
|
|
|
secbool storage_change_pin(const uint32_t pin, const uint32_t newpin, mp_obj_t callback)
|
2017-10-24 11:55:29 +00:00
|
|
|
{
|
2017-12-15 09:27:36 +00:00
|
|
|
if (sectrue != initialized || sectrue != unlocked) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2018-02-26 22:02:44 +00:00
|
|
|
if (sectrue != storage_check_pin(pin, callback)) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2017-12-15 09:27:36 +00:00
|
|
|
return norcow_set(PIN_KEY, &newpin, sizeof(uint32_t));
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 13:48:49 +00:00
|
|
|
void storage_wipe(void)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-12-09 13:48:49 +00:00
|
|
|
norcow_wipe();
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|