2017-10-17 17:18:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, Jan Pochyla, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "../../trezorhal/flash.h"
|
|
|
|
|
|
|
|
// Byte-length of flash sector containing fail counters.
|
2017-10-18 16:37:22 +00:00
|
|
|
#define PIN_SECTOR_SIZE 0x4000
|
2017-10-17 17:18:16 +00:00
|
|
|
|
|
|
|
// Maximum number of failed unlock attempts.
|
|
|
|
#define PIN_MAX_TRIES 15
|
|
|
|
|
|
|
|
// 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-10-27 15:49:30 +00:00
|
|
|
static secbool initialized = secfalse;
|
|
|
|
static secbool unlocked = secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_init(void)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-11-08 17:08:19 +00:00
|
|
|
initialized = secfalse;
|
|
|
|
unlocked = secfalse;
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != flash_init()) {
|
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != norcow_init()) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-10-27 15:49:30 +00:00
|
|
|
initialized = sectrue;
|
|
|
|
return sectrue;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pin_fails_reset(uint32_t ofs)
|
|
|
|
{
|
2017-10-18 16:37:22 +00:00
|
|
|
if (ofs + sizeof(uint32_t) >= PIN_SECTOR_SIZE) {
|
2017-10-17 17:18:16 +00:00
|
|
|
// ofs points to the last word of the PIN fails area. Because there is
|
|
|
|
// no space left, we recycle the sector (set all words to 0xffffffff).
|
|
|
|
// On next unlock attempt, we start counting from the the first word.
|
|
|
|
flash_erase_sectors((uint8_t[]) { FLASH_SECTOR_PIN_AREA }, 1, NULL);
|
|
|
|
} else {
|
|
|
|
// Mark this counter as exhausted. On next unlock attempt, pinfails_get
|
|
|
|
// seeks to the next word.
|
|
|
|
flash_unlock();
|
|
|
|
flash_write_word_rel(FLASH_SECTOR_PIN_AREA, ofs, 0);
|
|
|
|
flash_lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
static secbool pin_fails_increase(uint32_t ofs)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
|
|
|
uint32_t ctr = ~PIN_MAX_TRIES;
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != flash_read_word_rel(FLASH_SECTOR_PIN_AREA, ofs, &ctr)) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
ctr = ctr << 1;
|
|
|
|
|
|
|
|
flash_unlock();
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != flash_write_word_rel(FLASH_SECTOR_PIN_AREA, ofs, ctr)) {
|
2017-10-17 17:18:16 +00:00
|
|
|
flash_lock();
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
flash_lock();
|
|
|
|
|
|
|
|
uint32_t check = 0;
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != flash_read_word_rel(FLASH_SECTOR_PIN_AREA, ofs, &check)) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
if (~ctr >= 1 << PIN_MAX_TRIES) {
|
|
|
|
for (;;) {
|
|
|
|
if (norcow_wipe()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 14:31:23 +00:00
|
|
|
ensure(secfalse, "pin_fails_check_max");
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
static secbool pin_fails_read(uint32_t *ofs, uint32_t *ctr)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
2017-10-27 15:49:30 +00:00
|
|
|
if (NULL == ofs || NULL == ctr) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-10-18 16:37:22 +00:00
|
|
|
for (uint32_t o = 0; o < PIN_SECTOR_SIZE; o += sizeof(uint32_t)) {
|
2017-10-17 17:18:16 +00:00
|
|
|
uint32_t c = 0;
|
|
|
|
if (!flash_read_word_rel(FLASH_SECTOR_PIN_AREA, o, &c)) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
if (c != 0) {
|
|
|
|
*ofs = o;
|
|
|
|
*ctr = c;
|
2017-10-27 15:49:30 +00:00
|
|
|
return sectrue;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
static secbool const_cmp(const uint8_t *pub, size_t publen, const uint8_t *sec, size_t seclen)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
|
|
|
size_t diff = seclen ^ publen;
|
|
|
|
for (size_t i = 0; i < publen; i++) {
|
|
|
|
diff |= pub[i] ^ sec[i];
|
|
|
|
}
|
2017-11-08 17:08:19 +00:00
|
|
|
return sectrue * (0 == diff);
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
static secbool pin_cmp(const uint8_t *pin, size_t pinlen)
|
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-11-08 17:08:19 +00:00
|
|
|
if (NULL != spin) {
|
|
|
|
return const_cmp(pin, pinlen, spin, spinlen);
|
|
|
|
} else {
|
|
|
|
return sectrue * (0 == pinlen);
|
|
|
|
}
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
static secbool pin_check(const uint8_t *pin, size_t len)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
|
|
|
uint32_t ofs;
|
|
|
|
uint32_t ctr;
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != pin_fails_read(&ofs, &ctr)) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
|
|
|
pin_fails_check_max(ctr);
|
|
|
|
|
|
|
|
// Sleep for ~ctr seconds before checking the PIN.
|
|
|
|
for (uint32_t wait = ~ctr; wait > 0; wait--) {
|
2017-11-08 17:08:19 +00:00
|
|
|
hal_delay(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-10-27 15:49:30 +00:00
|
|
|
if (sectrue != pin_fails_increase(ofs)) {
|
|
|
|
return secfalse;
|
2017-10-17 17:18:16 +00:00
|
|
|
}
|
2017-11-08 17:08:19 +00:00
|
|
|
if (sectrue != pin_cmp(pin, len)) {
|
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
|
|
|
}
|
|
|
|
pin_fails_reset(ofs);
|
2017-11-06 16:26:13 +00:00
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
return sectrue;
|
|
|
|
}
|
2017-11-06 16:26:13 +00:00
|
|
|
|
2017-11-08 17:08:19 +00:00
|
|
|
secbool storage_unlock(const uint8_t *pin, size_t len)
|
|
|
|
{
|
|
|
|
unlocked = secfalse;
|
|
|
|
if (sectrue == initialized && sectrue == pin_check(pin, len)) {
|
|
|
|
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
|
|
|
{
|
2017-11-08 17:08:19 +00:00
|
|
|
if (sectrue != initialized || sectrue != unlocked || PIN_KEY == key) {
|
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
|
|
|
{
|
2017-11-08 17:08:19 +00:00
|
|
|
if (sectrue != initialized || sectrue != unlocked || PIN_KEY == key) {
|
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
|
|
|
}
|
|
|
|
const void *spin = NULL;
|
|
|
|
uint16_t spinlen = 0;
|
|
|
|
norcow_get(PIN_KEY, &spin, &spinlen);
|
2017-11-08 17:08:19 +00:00
|
|
|
return sectrue * (0 != spinlen);
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_change_pin(const uint8_t *pin, size_t len, const uint8_t *newpin, size_t newlen)
|
2017-10-24 11:55:29 +00:00
|
|
|
{
|
2017-11-08 17:08:19 +00:00
|
|
|
if (sectrue != initialized || sectrue != unlocked || newlen > PIN_MAXLEN) {
|
2017-10-27 15:49:30 +00:00
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sectrue != pin_check(pin, len)) {
|
|
|
|
return secfalse;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
2017-11-08 17:08:19 +00:00
|
|
|
return norcow_set(PIN_KEY, newpin, newlen);
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:49:30 +00:00
|
|
|
secbool storage_wipe(void)
|
2017-10-17 17:18:16 +00:00
|
|
|
{
|
|
|
|
return norcow_wipe();
|
|
|
|
}
|