style: improve makefile output, format test files

pull/152/head
matejcik 5 years ago
parent 6776ad56d2
commit 9b168daa19

@ -9,22 +9,33 @@ PY_FILES = $(shell find . -type f -name '*.py' | grep -f ./tools/style.py.incl
C_FILES = $(shell find . -type f -name '*.[ch]' | grep -f ./tools/style.c.include | grep -v -f ./tools/style.c.exclude ) C_FILES = $(shell find . -type f -name '*.[ch]' | grep -f ./tools/style.c.include | grep -v -f ./tools/style.c.exclude )
style_check: ## run code style check on application sources and tests style_check: pystyle_check cstyle_check
style: pystyle cstyle
pystyle_check: ## run code style check on application sources and tests
flake8 --version flake8 --version
isort --version | awk '/VERSION/{print $$2}' isort --version | awk '/VERSION/{print $$2}'
black --version black --version
flake8 $(PY_FILES) @echo [FLAKE8]
isort --check-only $(PY_FILES) @flake8 $(PY_FILES)
black --check $(PY_FILES) @echo [ISORT]
@isort --check-only $(PY_FILES)
@echo [BLACK]
@black --check $(PY_FILES)
make -C python style_check make -C python style_check
style: ## apply code style on application sources and tests pystyle: ## apply code style on application sources and tests
isort $(PY_FILES) @echo [ISORT]
black $(PY_FILES) @isort $(PY_FILES)
@echo [BLACK]
@black $(PY_FILES)
make -C python style make -C python style
cstyle_check: ## run code style check on low-level C code cstyle_check: ## run code style check on low-level C code
./tools/clang-format-check $(C_FILES) @echo [CLANG-FORMAT]
@./tools/clang-format-check $(C_FILES)
cstyle: ## apply code style on low-level C code cstyle: ## apply code style on low-level C code
clang-format -i $(C_FILES) @echo [CLANG-FORMAT]
@clang-format -i $(C_FILES)

@ -36,9 +36,9 @@
* lookup operations, as all proper crypto code must be. * lookup operations, as all proper crypto code must be.
*/ */
#include "shamir.h"
#include <string.h> #include <string.h>
#include "memzero.h" #include "memzero.h"
#include "shamir.h"
static void bitslice(uint32_t r[8], const uint8_t *x, size_t len) { static void bitslice(uint32_t r[8], const uint8_t *x, size_t len) {
size_t bit_idx, arr_idx; size_t bit_idx, arr_idx;

@ -16,8 +16,8 @@
import logging import logging
import sys import sys
from types import SimpleNamespace
import warnings import warnings
from types import SimpleNamespace
from mnemonic import Mnemonic from mnemonic import Mnemonic

@ -23,14 +23,13 @@
#include "common.h" #include "common.h"
void __shutdown(void) void __shutdown(void) {
{
printf("SHUTDOWN\n"); printf("SHUTDOWN\n");
exit(3); exit(3);
} }
void __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func) void __fatal_error(const char *expr, const char *msg, const char *file,
{ int line, const char *func) {
printf("\nFATAL ERROR:\n"); printf("\nFATAL ERROR:\n");
if (expr) { if (expr) {
printf("expr: %s\n", expr); printf("expr: %s\n", expr);
@ -47,7 +46,8 @@ void __fatal_error(const char *expr, const char *msg, const char *file, int line
__shutdown(); __shutdown();
} }
void error_shutdown(const char *line1, const char *line2, const char *line3, const char *line4) { void error_shutdown(const char *line1, const char *line2, const char *line3,
const char *line4) {
// For testing do not treat pin_fails_check_max as a fatal error. // For testing do not treat pin_fails_check_max as a fatal error.
(void)line1; (void)line1;
(void)line2; (void)line2;

@ -22,10 +22,15 @@
#include "secbool.h" #include "secbool.h"
void __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func); void __fatal_error(const char *expr, const char *msg, const char *file,
void error_shutdown(const char *line1, const char *line2, const char *line3, const char *line4); int line, const char *func);
void error_shutdown(const char *line1, const char *line2, const char *line3,
const char *line4);
#define ensure(expr, msg) (((expr) == sectrue) ? (void)0 : __fatal_error(#expr, msg, __FILE__, __LINE__, __func__)) #define ensure(expr, msg) \
(((expr) == sectrue) \
? (void)0 \
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
#define hal_delay(ms) (void)ms; #define hal_delay(ms) (void)ms;

@ -17,8 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
@ -54,23 +54,16 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
const uint32_t FLASH_SIZE = 0x200000; const uint32_t FLASH_SIZE = 0x200000;
uint8_t *FLASH_BUFFER = NULL; uint8_t *FLASH_BUFFER = NULL;
void flash_init(void) void flash_init(void) {
{ assert(FLASH_SIZE ==
assert(FLASH_SIZE == FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]); FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
} }
secbool flash_unlock_write(void) secbool flash_unlock_write(void) { return sectrue; }
{
return sectrue;
}
secbool flash_lock_write(void) secbool flash_lock_write(void) { return sectrue; }
{
return sectrue;
}
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size) const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size) {
{
if (sector >= FLASH_SECTOR_COUNT) { if (sector >= FLASH_SECTOR_COUNT) {
return NULL; return NULL;
} }
@ -82,15 +75,16 @@ const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
return FLASH_BUFFER + addr - FLASH_SECTOR_TABLE[0]; return FLASH_BUFFER + addr - FLASH_SECTOR_TABLE[0];
} }
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)) secbool flash_erase_sectors(const uint8_t *sectors, int len,
{ void (*progress)(int pos, int len)) {
if (progress) { if (progress) {
progress(0, len); progress(0, len);
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
const uint8_t sector = sectors[i]; const uint8_t sector = sectors[i];
const uint32_t offset = FLASH_SECTOR_TABLE[sector] - FLASH_SECTOR_TABLE[0]; const uint32_t offset = FLASH_SECTOR_TABLE[sector] - FLASH_SECTOR_TABLE[0];
const uint32_t size = FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector]; const uint32_t size =
FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector];
memset(FLASH_BUFFER + offset, 0xFF, size); memset(FLASH_BUFFER + offset, 0xFF, size);
if (progress) { if (progress) {
progress(i + 1, len); progress(i + 1, len);
@ -99,8 +93,7 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
return sectrue; return sectrue;
} }
secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) {
{
uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, 1); uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, 1);
if (!flash) { if (!flash) {
return secfalse; return secfalse;
@ -112,8 +105,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data)
return sectrue; return sectrue;
} }
secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data) secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data) {
{
if (offset % 4) { // we write only at 4-byte boundary if (offset % 4) { // we write only at 4-byte boundary
return secfalse; return secfalse;
} }

@ -33,8 +33,11 @@ secbool __wur flash_lock_write(void);
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size); const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size);
secbool __wur flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)); secbool __wur flash_erase_sectors(const uint8_t *sectors, int len,
static inline secbool flash_erase(uint8_t sector) { return flash_erase_sectors(&sector, 1, NULL); } void (*progress)(int pos, int len));
static inline secbool flash_erase(uint8_t sector) {
return flash_erase_sectors(&sector, 1, NULL);
}
secbool __wur flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data); secbool __wur flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data);
secbool __wur flash_write_word(uint8_t sector, uint32_t offset, uint32_t data); secbool __wur flash_write_word(uint8_t sector, uint32_t offset, uint32_t data);

@ -24,10 +24,12 @@
#define NORCOW_SECTOR_COUNT 2 #define NORCOW_SECTOR_COUNT 2
#define NORCOW_SECTOR_SIZE (64 * 1024) #define NORCOW_SECTOR_SIZE (64 * 1024)
#define NORCOW_SECTORS {4, 16} #define NORCOW_SECTORS \
{ 4, 16 }
/* /*
* The length of the sector header in bytes. The header is preserved between sector erasures. * The length of the sector header in bytes. The header is preserved between
* sector erasures.
*/ */
#if TREZOR_MODEL == 1 #if TREZOR_MODEL == 1
#define NORCOW_HEADER_LEN (0x100) #define NORCOW_HEADER_LEN (0x100)

@ -17,8 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
@ -55,23 +55,16 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
const uint32_t FLASH_SIZE = 0x200000; const uint32_t FLASH_SIZE = 0x200000;
uint8_t *FLASH_BUFFER = NULL; uint8_t *FLASH_BUFFER = NULL;
void flash_init(void) void flash_init(void) {
{ assert(FLASH_SIZE ==
assert(FLASH_SIZE == FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]); FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
} }
secbool flash_unlock(void) secbool flash_unlock(void) { return sectrue; }
{
return sectrue;
}
secbool flash_lock(void) secbool flash_lock(void) { return sectrue; }
{
return sectrue;
}
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size) const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size) {
{
if (sector >= FLASH_SECTOR_COUNT) { if (sector >= FLASH_SECTOR_COUNT) {
return NULL; return NULL;
} }
@ -83,15 +76,16 @@ const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
return FLASH_BUFFER + addr - FLASH_SECTOR_TABLE[0]; return FLASH_BUFFER + addr - FLASH_SECTOR_TABLE[0];
} }
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)) secbool flash_erase_sectors(const uint8_t *sectors, int len,
{ void (*progress)(int pos, int len)) {
if (progress) { if (progress) {
progress(0, len); progress(0, len);
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
const uint8_t sector = sectors[i]; const uint8_t sector = sectors[i];
const uint32_t offset = FLASH_SECTOR_TABLE[sector] - FLASH_SECTOR_TABLE[0]; const uint32_t offset = FLASH_SECTOR_TABLE[sector] - FLASH_SECTOR_TABLE[0];
const uint32_t size = FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector]; const uint32_t size =
FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector];
memset(FLASH_BUFFER + offset, 0xFF, size); memset(FLASH_BUFFER + offset, 0xFF, size);
if (progress) { if (progress) {
progress(i + 1, len); progress(i + 1, len);
@ -100,8 +94,7 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
return sectrue; return sectrue;
} }
secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) {
{
uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, 1); uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, 1);
if (!flash) { if (!flash) {
return secfalse; return secfalse;
@ -113,8 +106,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data)
return sectrue; return sectrue;
} }
secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data) secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data) {
{
if (offset % 4) { // we write only at 4-byte boundary if (offset % 4) { // we write only at 4-byte boundary
return secfalse; return secfalse;
} }

@ -33,8 +33,11 @@ secbool __wur flash_lock(void);
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size); const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size);
secbool __wur flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)); secbool __wur flash_erase_sectors(const uint8_t *sectors, int len,
static inline secbool flash_erase_sector(uint8_t sector) { return flash_erase_sectors(&sector, 1, NULL); } void (*progress)(int pos, int len));
static inline secbool flash_erase_sector(uint8_t sector) {
return flash_erase_sectors(&sector, 1, NULL);
}
secbool __wur flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data); secbool __wur flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data);
secbool __wur flash_write_word(uint8_t sector, uint32_t offset, uint32_t data); secbool __wur flash_write_word(uint8_t sector, uint32_t offset, uint32_t data);

@ -19,9 +19,9 @@
#include <string.h> #include <string.h>
#include "norcow.h"
#include "flash.h"
#include "common.h" #include "common.h"
#include "flash.h"
#include "norcow.h"
// NRCW = 4e524357 // NRCW = 4e524357
#define NORCOW_MAGIC ((uint32_t)0x5743524e) #define NORCOW_MAGIC ((uint32_t)0x5743524e)
@ -35,8 +35,7 @@ static uint32_t norcow_active_offset = NORCOW_MAGIC_LEN;
* Returns pointer to sector, starting with offset * Returns pointer to sector, starting with offset
* Fails when there is not enough space for data of given size * Fails when there is not enough space for data of given size
*/ */
static const void *norcow_ptr(uint8_t sector, uint32_t offset, uint32_t size) static const void *norcow_ptr(uint8_t sector, uint32_t offset, uint32_t size) {
{
ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector"); ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector");
return flash_get_address(norcow_sectors[sector], offset, size); return flash_get_address(norcow_sectors[sector], offset, size);
} }
@ -44,8 +43,8 @@ static const void *norcow_ptr(uint8_t sector, uint32_t offset, uint32_t size)
/* /*
* Writes data to given sector, starting from offset * Writes data to given sector, starting from offset
*/ */
static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, const uint8_t *data, uint16_t len) static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix,
{ const uint8_t *data, uint16_t len) {
if (sector >= NORCOW_SECTOR_COUNT) { if (sector >= NORCOW_SECTOR_COUNT) {
return secfalse; return secfalse;
} }
@ -72,8 +71,7 @@ static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, co
/* /*
* Erases sector (and sets a magic) * Erases sector (and sets a magic)
*/ */
static void norcow_erase(uint8_t sector, secbool set_magic) static void norcow_erase(uint8_t sector, secbool set_magic) {
{
ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector"); ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector");
ensure(flash_erase_sector(norcow_sectors[sector]), "erase failed"); ensure(flash_erase_sector(norcow_sectors[sector]), "erase failed");
if (sectrue == set_magic) { if (sectrue == set_magic) {
@ -86,8 +84,8 @@ static void norcow_erase(uint8_t sector, secbool set_magic)
/* /*
* Reads one item starting from offset * Reads one item starting from offset
*/ */
static secbool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const void **val, uint16_t *len, uint32_t *pos) static secbool read_item(uint8_t sector, uint32_t offset, uint16_t *key,
{ const void **val, uint16_t *len, uint32_t *pos) {
*pos = offset; *pos = offset;
const void *k = norcow_ptr(sector, *pos, 2); const void *k = norcow_ptr(sector, *pos, 2);
@ -113,8 +111,8 @@ static secbool read_item(uint8_t sector, uint32_t offset, uint16_t *key, const v
/* /*
* Writes one item starting from offset * Writes one item starting from offset
*/ */
static secbool write_item(uint8_t sector, uint32_t offset, uint16_t key, const void *val, uint16_t len, uint32_t *pos) static secbool write_item(uint8_t sector, uint32_t offset, uint16_t key,
{ const void *val, uint16_t len, uint32_t *pos) {
uint32_t prefix = (len << 16) | key; uint32_t prefix = (len << 16) | key;
*pos = offset + sizeof(uint32_t) + len; *pos = offset + sizeof(uint32_t) + len;
ALIGN4(*pos); ALIGN4(*pos);
@ -124,8 +122,8 @@ static secbool write_item(uint8_t sector, uint32_t offset, uint16_t key, const v
/* /*
* Finds item in given sector * Finds item in given sector
*/ */
static secbool find_item(uint8_t sector, uint16_t key, const void **val, uint16_t *len) static secbool find_item(uint8_t sector, uint16_t key, const void **val,
{ uint16_t *len) {
*val = 0; *val = 0;
*len = 0; *len = 0;
uint32_t offset = NORCOW_MAGIC_LEN; uint32_t offset = NORCOW_MAGIC_LEN;
@ -148,8 +146,7 @@ static secbool find_item(uint8_t sector, uint16_t key, const void **val, uint16_
/* /*
* Finds first unused offset in given sector * Finds first unused offset in given sector
*/ */
static uint32_t find_free_offset(uint8_t sector) static uint32_t find_free_offset(uint8_t sector) {
{
uint32_t offset = NORCOW_MAGIC_LEN; uint32_t offset = NORCOW_MAGIC_LEN;
for (;;) { for (;;) {
uint16_t key, len; uint16_t key, len;
@ -166,8 +163,7 @@ static uint32_t find_free_offset(uint8_t sector)
/* /*
* Compacts active sector and sets new active sector * Compacts active sector and sets new active sector
*/ */
static void compact() static void compact() {
{
uint8_t norcow_next_sector = (norcow_active_sector + 1) % NORCOW_SECTOR_COUNT; uint8_t norcow_next_sector = (norcow_active_sector + 1) % NORCOW_SECTOR_COUNT;
norcow_erase(norcow_next_sector, sectrue); norcow_erase(norcow_next_sector, sectrue);
@ -210,7 +206,8 @@ static void compact()
// copy the last item // copy the last item
uint32_t posw; uint32_t posw;
ensure(write_item(norcow_next_sector, offsetw, k, v, l, &posw), "compaction write failed"); ensure(write_item(norcow_next_sector, offsetw, k, v, l, &posw),
"compaction write failed");
offsetw = posw; offsetw = posw;
} }
@ -222,8 +219,7 @@ static void compact()
/* /*
* Initializes storage * Initializes storage
*/ */
void norcow_init(void) void norcow_init(void) {
{
flash_init(); flash_init();
secbool found = secfalse; secbool found = secfalse;
// detect active sector - starts with magic // detect active sector - starts with magic
@ -246,8 +242,7 @@ void norcow_init(void)
/* /*
* Wipe the storage * Wipe the storage
*/ */
void norcow_wipe(void) void norcow_wipe(void) {
{
norcow_erase(0, sectrue); norcow_erase(0, sectrue);
for (uint8_t i = 1; i < NORCOW_SECTOR_COUNT; i++) { for (uint8_t i = 1; i < NORCOW_SECTOR_COUNT; i++) {
norcow_erase(i, secfalse); norcow_erase(i, secfalse);
@ -259,16 +254,14 @@ void norcow_wipe(void)
/* /*
* Looks for the given key, returns status of the operation * Looks for the given key, returns status of the operation
*/ */
secbool norcow_get(uint16_t key, const void **val, uint16_t *len) secbool norcow_get(uint16_t key, const void **val, uint16_t *len) {
{
return find_item(norcow_active_sector, key, val, len); return find_item(norcow_active_sector, key, val, len);
} }
/* /*
* Sets the given key, returns status of the operation * Sets the given key, returns status of the operation
*/ */
secbool norcow_set(uint16_t key, const void *val, uint16_t len) secbool norcow_set(uint16_t key, const void *val, uint16_t len) {
{
// check whether there is enough free space // check whether there is enough free space
// and compact if full // and compact if full
if (norcow_active_offset + sizeof(uint32_t) + len > NORCOW_SECTOR_SIZE) { if (norcow_active_offset + sizeof(uint32_t) + len > NORCOW_SECTOR_SIZE) {
@ -276,7 +269,8 @@ secbool norcow_set(uint16_t key, const void *val, uint16_t len)
} }
// write item // write item
uint32_t pos; uint32_t pos;
secbool r = write_item(norcow_active_sector, norcow_active_offset, key, val, len, &pos); secbool r = write_item(norcow_active_sector, norcow_active_offset, key, val,
len, &pos);
if (sectrue == r) { if (sectrue == r) {
norcow_active_offset = pos; norcow_active_offset = pos;
} }
@ -287,8 +281,7 @@ secbool norcow_set(uint16_t key, const void *val, uint16_t len)
* Update a word in flash at the given pointer. The pointer must point * Update a word in flash at the given pointer. The pointer must point
* into the NORCOW area. * into the NORCOW area.
*/ */
secbool norcow_update(uint16_t key, uint16_t offset, uint32_t value) secbool norcow_update(uint16_t key, uint16_t offset, uint32_t value) {
{
const void *ptr; const void *ptr;
uint16_t len; uint16_t len;
if (sectrue != find_item(norcow_active_sector, key, &ptr, &len)) { if (sectrue != find_item(norcow_active_sector, key, &ptr, &len)) {
@ -297,9 +290,14 @@ secbool norcow_update(uint16_t key, uint16_t offset, uint32_t value)
if ((offset & 3) != 0 || offset >= len) { if ((offset & 3) != 0 || offset >= len) {
return secfalse; return secfalse;
} }
uint32_t sector_offset = (const uint8_t*) ptr - (const uint8_t *)norcow_ptr(norcow_active_sector, 0, NORCOW_SECTOR_SIZE) + offset; uint32_t sector_offset =
(const uint8_t *)ptr -
(const uint8_t *)norcow_ptr(norcow_active_sector, 0, NORCOW_SECTOR_SIZE) +
offset;
ensure(flash_unlock(), NULL); ensure(flash_unlock(), NULL);
ensure(flash_write_word(norcow_sectors[norcow_active_sector], sector_offset, value), NULL); ensure(flash_write_word(norcow_sectors[norcow_active_sector], sector_offset,
value),
NULL);
ensure(flash_lock(), NULL); ensure(flash_lock(), NULL);
return sectrue; return sectrue;
} }

@ -24,6 +24,7 @@
#define NORCOW_SECTOR_COUNT 2 #define NORCOW_SECTOR_COUNT 2
#define NORCOW_SECTOR_SIZE (64 * 1024) #define NORCOW_SECTOR_SIZE (64 * 1024)
#define NORCOW_SECTORS {4, 16} #define NORCOW_SECTORS \
{ 4, 16 }
#endif #endif

@ -40,8 +40,7 @@ static secbool initialized = secfalse;
static secbool unlocked = secfalse; static secbool unlocked = secfalse;
static PIN_UI_WAIT_CALLBACK ui_callback = NULL; static PIN_UI_WAIT_CALLBACK ui_callback = NULL;
void storage_init(PIN_UI_WAIT_CALLBACK callback) void storage_init(PIN_UI_WAIT_CALLBACK callback) {
{
initialized = secfalse; initialized = secfalse;
unlocked = secfalse; unlocked = secfalse;
norcow_init(); norcow_init();
@ -49,13 +48,11 @@ void storage_init(PIN_UI_WAIT_CALLBACK callback)
ui_callback = callback; ui_callback = callback;
} }
static secbool pin_fails_reset(uint16_t ofs) static secbool pin_fails_reset(uint16_t ofs) {
{
return norcow_update(PIN_FAIL_KEY, ofs, 0); return norcow_update(PIN_FAIL_KEY, ofs, 0);
} }
static secbool pin_fails_increase(const uint32_t *ptr, uint16_t ofs) static secbool pin_fails_increase(const uint32_t *ptr, uint16_t ofs) {
{
uint32_t ctr = *ptr; uint32_t ctr = *ptr;
ctr = ctr << 1; ctr = ctr << 1;
@ -70,16 +67,14 @@ static secbool pin_fails_increase(const uint32_t *ptr, uint16_t ofs)
return sectrue; return sectrue;
} }
static void pin_fails_check_max(uint32_t ctr) static void pin_fails_check_max(uint32_t ctr) {
{
if (~ctr >= (1 << PIN_MAX_TRIES)) { if (~ctr >= (1 << PIN_MAX_TRIES)) {
norcow_wipe(); norcow_wipe();
ensure(secfalse, "pin_fails_check_max"); ensure(secfalse, "pin_fails_check_max");
} }
} }
static secbool pin_cmp(const uint32_t pin) static secbool pin_cmp(const uint32_t pin) {
{
const void *spin = NULL; const void *spin = NULL;
uint16_t spinlen = 0; uint16_t spinlen = 0;
norcow_get(PIN_KEY, &spin, &spinlen); norcow_get(PIN_KEY, &spin, &spinlen);
@ -90,8 +85,7 @@ static secbool pin_cmp(const uint32_t pin)
} }
} }
static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs) static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs) {
{
const void *vpinfail; const void *vpinfail;
uint16_t pinfaillen; uint16_t pinfaillen;
unsigned int ofs; unsigned int ofs;
@ -129,8 +123,7 @@ static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs)
return sectrue; return sectrue;
} }
secbool storage_check_pin(const uint32_t pin) secbool storage_check_pin(const uint32_t pin) {
{
const uint32_t *pinfail = NULL; const uint32_t *pinfail = NULL;
uint32_t ofs; uint32_t ofs;
uint32_t ctr; uint32_t ctr;
@ -180,8 +173,7 @@ secbool storage_check_pin(const uint32_t pin)
return pin_fails_reset(ofs * sizeof(uint32_t)); return pin_fails_reset(ofs * sizeof(uint32_t));
} }
secbool storage_unlock(const uint32_t pin) secbool storage_unlock(const uint32_t pin) {
{
unlocked = secfalse; unlocked = secfalse;
if (sectrue == initialized && sectrue == storage_check_pin(pin)) { if (sectrue == initialized && sectrue == storage_check_pin(pin)) {
unlocked = sectrue; unlocked = sectrue;
@ -189,8 +181,7 @@ secbool storage_unlock(const uint32_t pin)
return unlocked; return unlocked;
} }
secbool storage_get(const uint16_t key, const void **val, uint16_t *len) secbool storage_get(const uint16_t key, const void **val, uint16_t *len) {
{
const uint8_t app = key >> 8; const uint8_t app = key >> 8;
// APP == 0 is reserved for PIN related values // APP == 0 is reserved for PIN related values
if (sectrue != initialized || app == 0) { if (sectrue != initialized || app == 0) {
@ -203,8 +194,7 @@ secbool storage_get(const uint16_t key, const void **val, uint16_t *len)
return norcow_get(key, val, len); return norcow_get(key, val, len);
} }
secbool storage_set(const uint16_t key, const void *val, uint16_t len) secbool storage_set(const uint16_t key, const void *val, uint16_t len) {
{
const uint8_t app = key >> 8; const uint8_t app = key >> 8;
// APP == 0 is reserved for PIN related values // APP == 0 is reserved for PIN related values
if (sectrue != initialized || sectrue != unlocked || app == 0) { if (sectrue != initialized || sectrue != unlocked || app == 0) {
@ -213,16 +203,14 @@ secbool storage_set(const uint16_t key, const void *val, uint16_t len)
return norcow_set(key, val, len); return norcow_set(key, val, len);
} }
secbool storage_has_pin(void) secbool storage_has_pin(void) {
{
if (sectrue != initialized) { if (sectrue != initialized) {
return secfalse; return secfalse;
} }
return sectrue == pin_cmp(1) ? secfalse : sectrue; return sectrue == pin_cmp(1) ? secfalse : sectrue;
} }
secbool storage_change_pin(const uint32_t oldpin, const uint32_t newpin) secbool storage_change_pin(const uint32_t oldpin, const uint32_t newpin) {
{
if (sectrue != initialized || sectrue != unlocked) { if (sectrue != initialized || sectrue != unlocked) {
return secfalse; return secfalse;
} }
@ -232,7 +220,4 @@ secbool storage_change_pin(const uint32_t oldpin, const uint32_t newpin)
return norcow_set(PIN_KEY, &newpin, sizeof(uint32_t)); return norcow_set(PIN_KEY, &newpin, sizeof(uint32_t));
} }
void storage_wipe(void) void storage_wipe(void) { norcow_wipe(); }
{
norcow_wipe();
}

@ -20,8 +20,8 @@
#ifndef __STORAGE_H__ #ifndef __STORAGE_H__
#define __STORAGE_H__ #define __STORAGE_H__
#include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include "secbool.h" #include "secbool.h"
typedef void (*PIN_UI_WAIT_CALLBACK)(uint32_t wait, uint32_t progress); typedef void (*PIN_UI_WAIT_CALLBACK)(uint32_t wait, uint32_t progress);

Loading…
Cancel
Save