1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

style: improve makefile output, format test files

This commit is contained in:
matejcik 2019-05-13 17:20:35 +02:00
parent 6776ad56d2
commit 9b168daa19
16 changed files with 603 additions and 611 deletions

View File

@ -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 )
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
isort --version | awk '/VERSION/{print $$2}'
black --version
flake8 $(PY_FILES)
isort --check-only $(PY_FILES)
black --check $(PY_FILES)
@echo [FLAKE8]
@flake8 $(PY_FILES)
@echo [ISORT]
@isort --check-only $(PY_FILES)
@echo [BLACK]
@black --check $(PY_FILES)
make -C python style_check
style: ## apply code style on application sources and tests
isort $(PY_FILES)
black $(PY_FILES)
pystyle: ## apply code style on application sources and tests
@echo [ISORT]
@isort $(PY_FILES)
@echo [BLACK]
@black $(PY_FILES)
make -C python style
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
clang-format -i $(C_FILES)
@echo [CLANG-FORMAT]
@clang-format -i $(C_FILES)

View File

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

View File

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

View File

@ -23,14 +23,13 @@
#include "common.h"
void __shutdown(void)
{
void __shutdown(void) {
printf("SHUTDOWN\n");
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");
if (expr) {
printf("expr: %s\n", expr);
@ -47,11 +46,12 @@ void __fatal_error(const char *expr, const char *msg, const char *file, int line
__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.
(void) line1;
(void) line2;
(void) line3;
(void) line4;
(void)line1;
(void)line2;
(void)line3;
(void)line4;
return;
}

View File

@ -22,10 +22,15 @@
#include "secbool.h"
void __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func);
void error_shutdown(const char *line1, const char *line2, const char *line3, const char *line4);
void __fatal_error(const char *expr, const char *msg, const char *file,
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;

View File

@ -17,24 +17,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "flash.h"
static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
[ 0] = 0x08000000, // - 0x08003FFF | 16 KiB
[ 1] = 0x08004000, // - 0x08007FFF | 16 KiB
[ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB
[ 3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
[ 4] = 0x08010000, // - 0x0801FFFF | 64 KiB
[ 5] = 0x08020000, // - 0x0803FFFF | 128 KiB
[ 6] = 0x08040000, // - 0x0805FFFF | 128 KiB
[ 7] = 0x08060000, // - 0x0807FFFF | 128 KiB
[ 8] = 0x08080000, // - 0x0809FFFF | 128 KiB
[ 9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
[0] = 0x08000000, // - 0x08003FFF | 16 KiB
[1] = 0x08004000, // - 0x08007FFF | 16 KiB
[2] = 0x08008000, // - 0x0800BFFF | 16 KiB
[3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
[4] = 0x08010000, // - 0x0801FFFF | 64 KiB
[5] = 0x08020000, // - 0x0803FFFF | 128 KiB
[6] = 0x08040000, // - 0x0805FFFF | 128 KiB
[7] = 0x08060000, // - 0x0807FFFF | 128 KiB
[8] = 0x08080000, // - 0x0809FFFF | 128 KiB
[9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
[10] = 0x080C0000, // - 0x080DFFFF | 128 KiB
[11] = 0x080E0000, // - 0x080FFFFF | 128 KiB
[12] = 0x08100000, // - 0x08103FFF | 16 KiB
@ -54,23 +54,16 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
const uint32_t FLASH_SIZE = 0x200000;
uint8_t *FLASH_BUFFER = NULL;
void flash_init(void)
{
assert(FLASH_SIZE == FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
void flash_init(void) {
assert(FLASH_SIZE ==
FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
}
secbool flash_unlock_write(void)
{
return sectrue;
}
secbool flash_unlock_write(void) { return sectrue; }
secbool flash_lock_write(void)
{
return sectrue;
}
secbool flash_lock_write(void) { 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) {
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];
}
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) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
const uint8_t sector = sectors[i];
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);
if (progress) {
progress(i + 1, len);
@ -99,8 +93,7 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
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);
if (!flash) {
return secfalse;
@ -112,8 +105,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data)
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
return secfalse;
}

View File

@ -33,8 +33,11 @@ secbool __wur flash_lock_write(void);
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));
static inline secbool flash_erase(uint8_t sector) { return flash_erase_sectors(&sector, 1, NULL); }
secbool __wur flash_erase_sectors(const uint8_t *sectors, int len,
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_word(uint8_t sector, uint32_t offset, uint32_t data);

View File

@ -23,11 +23,13 @@
#include "flash.h"
#define NORCOW_SECTOR_COUNT 2
#define NORCOW_SECTOR_SIZE (64*1024)
#define NORCOW_SECTORS {4, 16}
#define NORCOW_SECTOR_SIZE (64 * 1024)
#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
#define NORCOW_HEADER_LEN (0x100)

View File

@ -27,7 +27,7 @@ typedef uint32_t secbool;
#define secfalse 0x00000000U
#ifndef __wur
#define __wur __attribute__ ((warn_unused_result))
#define __wur __attribute__((warn_unused_result))
#endif
#endif

View File

@ -17,24 +17,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "flash.h"
static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
[ 0] = 0x08000000, // - 0x08003FFF | 16 KiB
[ 1] = 0x08004000, // - 0x08007FFF | 16 KiB
[ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB
[ 3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
[ 4] = 0x08010000, // - 0x0801FFFF | 64 KiB
[ 5] = 0x08020000, // - 0x0803FFFF | 128 KiB
[ 6] = 0x08040000, // - 0x0805FFFF | 128 KiB
[ 7] = 0x08060000, // - 0x0807FFFF | 128 KiB
[ 8] = 0x08080000, // - 0x0809FFFF | 128 KiB
[ 9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
[0] = 0x08000000, // - 0x08003FFF | 16 KiB
[1] = 0x08004000, // - 0x08007FFF | 16 KiB
[2] = 0x08008000, // - 0x0800BFFF | 16 KiB
[3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
[4] = 0x08010000, // - 0x0801FFFF | 64 KiB
[5] = 0x08020000, // - 0x0803FFFF | 128 KiB
[6] = 0x08040000, // - 0x0805FFFF | 128 KiB
[7] = 0x08060000, // - 0x0807FFFF | 128 KiB
[8] = 0x08080000, // - 0x0809FFFF | 128 KiB
[9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
[10] = 0x080C0000, // - 0x080DFFFF | 128 KiB
[11] = 0x080E0000, // - 0x080FFFFF | 128 KiB
[12] = 0x08100000, // - 0x08103FFF | 16 KiB
@ -55,23 +55,16 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
const uint32_t FLASH_SIZE = 0x200000;
uint8_t *FLASH_BUFFER = NULL;
void flash_init(void)
{
assert(FLASH_SIZE == FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
void flash_init(void) {
assert(FLASH_SIZE ==
FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]);
}
secbool flash_unlock(void)
{
return sectrue;
}
secbool flash_unlock(void) { return sectrue; }
secbool flash_lock(void)
{
return sectrue;
}
secbool flash_lock(void) { 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) {
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];
}
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) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
const uint8_t sector = sectors[i];
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);
if (progress) {
progress(i + 1, len);
@ -100,8 +94,7 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
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);
if (!flash) {
return secfalse;
@ -113,8 +106,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data)
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
return secfalse;
}

View File

@ -33,8 +33,11 @@ secbool __wur flash_lock(void);
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));
static inline secbool flash_erase_sector(uint8_t sector) { return flash_erase_sectors(&sector, 1, NULL); }
secbool __wur flash_erase_sectors(const uint8_t *sectors, int len,
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_word(uint8_t sector, uint32_t offset, uint32_t data);

View File

@ -19,9 +19,9 @@
#include <string.h>
#include "norcow.h"
#include "flash.h"
#include "common.h"
#include "flash.h"
#include "norcow.h"
// NRCW = 4e524357
#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
* 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");
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
*/
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) {
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)
*/
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(flash_erase_sector(norcow_sectors[sector]), "erase failed");
if (sectrue == set_magic) {
@ -86,8 +84,8 @@ static void norcow_erase(uint8_t sector, secbool set_magic)
/*
* 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;
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
*/
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;
*pos = offset + sizeof(uint32_t) + len;
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
*/
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;
*len = 0;
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
*/
static uint32_t find_free_offset(uint8_t sector)
{
static uint32_t find_free_offset(uint8_t sector) {
uint32_t offset = NORCOW_MAGIC_LEN;
for (;;) {
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
*/
static void compact()
{
static void compact() {
uint8_t norcow_next_sector = (norcow_active_sector + 1) % NORCOW_SECTOR_COUNT;
norcow_erase(norcow_next_sector, sectrue);
@ -210,7 +206,8 @@ static void compact()
// copy the last item
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;
}
@ -222,8 +219,7 @@ static void compact()
/*
* Initializes storage
*/
void norcow_init(void)
{
void norcow_init(void) {
flash_init();
secbool found = secfalse;
// detect active sector - starts with magic
@ -246,8 +242,7 @@ void norcow_init(void)
/*
* Wipe the storage
*/
void norcow_wipe(void)
{
void norcow_wipe(void) {
norcow_erase(0, sectrue);
for (uint8_t i = 1; i < NORCOW_SECTOR_COUNT; i++) {
norcow_erase(i, secfalse);
@ -259,16 +254,14 @@ void norcow_wipe(void)
/*
* 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);
}
/*
* 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
// and compact if full
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
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) {
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
* 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;
uint16_t 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) {
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_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);
return sectrue;
}

View File

@ -23,7 +23,8 @@
#include "flash.h"
#define NORCOW_SECTOR_COUNT 2
#define NORCOW_SECTOR_SIZE (64*1024)
#define NORCOW_SECTORS {4, 16}
#define NORCOW_SECTOR_SIZE (64 * 1024)
#define NORCOW_SECTORS \
{ 4, 16 }
#endif

View File

@ -27,7 +27,7 @@ typedef uint32_t secbool;
#define secfalse 0x00000000U
#ifndef __wur
#define __wur __attribute__ ((warn_unused_result))
#define __wur __attribute__((warn_unused_result))
#endif
#endif

View File

@ -40,8 +40,7 @@ static secbool initialized = secfalse;
static secbool unlocked = secfalse;
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;
unlocked = secfalse;
norcow_init();
@ -49,13 +48,11 @@ void storage_init(PIN_UI_WAIT_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);
}
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;
ctr = ctr << 1;
@ -70,28 +67,25 @@ static secbool pin_fails_increase(const uint32_t *ptr, uint16_t ofs)
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)) {
norcow_wipe();
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;
uint16_t spinlen = 0;
norcow_get(PIN_KEY, &spin, &spinlen);
if (NULL != spin && spinlen == sizeof(uint32_t)) {
return sectrue * (pin == *(const uint32_t*)spin);
return sectrue * (pin == *(const uint32_t *)spin);
} else {
return sectrue * (1 == 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;
uint16_t pinfaillen;
unsigned int ofs;
@ -107,7 +101,7 @@ static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs)
if (secfalse != norcow_get(PIN_FAIL_KEY, &vpinfail, &pinfaillen)) {
*pinfail = vpinfail;
for (ofs = 0; ofs < pinfaillen / sizeof(uint32_t); ofs++) {
if (((const uint32_t *) vpinfail)[ofs]) {
if (((const uint32_t *)vpinfail)[ofs]) {
*pinfail = vpinfail;
*pofs = ofs;
return sectrue;
@ -129,8 +123,7 @@ static secbool pin_get_fails(const uint32_t **pinfail, uint32_t *pofs)
return sectrue;
}
secbool storage_check_pin(const uint32_t pin)
{
secbool storage_check_pin(const uint32_t pin) {
const uint32_t *pinfail = NULL;
uint32_t ofs;
uint32_t ctr;
@ -180,8 +173,7 @@ secbool storage_check_pin(const uint32_t pin)
return pin_fails_reset(ofs * sizeof(uint32_t));
}
secbool storage_unlock(const uint32_t pin)
{
secbool storage_unlock(const uint32_t pin) {
unlocked = secfalse;
if (sectrue == initialized && sectrue == storage_check_pin(pin)) {
unlocked = sectrue;
@ -189,8 +181,7 @@ secbool storage_unlock(const uint32_t pin)
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;
// APP == 0 is reserved for PIN related values
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);
}
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;
// APP == 0 is reserved for PIN related values
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);
}
secbool storage_has_pin(void)
{
secbool storage_has_pin(void) {
if (sectrue != initialized) {
return secfalse;
}
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) {
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));
}
void storage_wipe(void)
{
norcow_wipe();
}
void storage_wipe(void) { norcow_wipe(); }

View File

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