1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

storage: add has_pin and change_pin

TODO: finish change_pin
This commit is contained in:
Jan Pochyla 2017-10-24 13:55:29 +02:00
parent c2ec40bebf
commit 418f035811
3 changed files with 68 additions and 6 deletions

View File

@ -44,6 +44,36 @@ STATIC mp_obj_t mod_trezorconfig_unlock(mp_obj_t pin) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorconfig_unlock_obj, mod_trezorconfig_unlock);
/// def has_pin() -> bool:
/// '''
/// Returns True if storage has a configured PIN, False otherwise.
/// '''
STATIC mp_obj_t mod_trezorconfig_has_pin(void) {
if (storage_has_pin()) {
return mp_const_true;
} else {
return mp_const_false;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_has_pin_obj, mod_trezorconfig_has_pin);
/// def change_pin(pin: str, newpin: str) -> bool:
/// '''
/// Change PIN. Returns True on success, False on failure.
/// '''
STATIC mp_obj_t mod_trezorconfig_change_pin(mp_obj_t pin, mp_obj_t newpin) {
mp_buffer_info_t pinbuf;
mp_get_buffer_raise(pin, &pinbuf, MP_BUFFER_READ);
mp_buffer_info_t newbuf;
mp_get_buffer_raise(newpin, &newbuf, MP_BUFFER_READ);
bool r = storage_change_pin(pinbuf.buf, pinbuf.len, newbuf.buf, newbuf.len);
if (!r) {
return mp_const_false;
}
return mp_const_true;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_change_pin_obj, mod_trezorconfig_change_pin);
/// def get(app: int, key: int) -> bytes:
/// '''
/// Gets a value of given key for given app (or empty bytes if not set).
@ -97,6 +127,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorconfig_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorconfig) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mod_trezorconfig_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&mod_trezorconfig_unlock_obj) },
{ MP_ROM_QSTR(MP_QSTR_has_pin), MP_ROM_PTR(&mod_trezorconfig_has_pin_obj) },
{ MP_ROM_QSTR(MP_QSTR_change_pin), MP_ROM_PTR(&mod_trezorconfig_change_pin_obj) },
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&mod_trezorconfig_get_obj) },
{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&mod_trezorconfig_set_obj) },
{ MP_ROM_QSTR(MP_QSTR_wipe), MP_ROM_PTR(&mod_trezorconfig_wipe_obj) },

View File

@ -24,6 +24,9 @@ static bool unlocked = false;
bool storage_init(void)
{
if (!flash_init()) {
return false;
}
if (!norcow_init()) {
return false;
}
@ -112,12 +115,10 @@ static bool const_cmp(const uint8_t *pub, size_t publen, const uint8_t *sec, siz
static bool pin_check(const uint8_t *pin, size_t pinlen)
{
const void *st_pin;
uint16_t st_pinlen;
if (!norcow_get(PIN_KEY, &st_pin, &st_pinlen)) {
return false;
}
return const_cmp(pin, pinlen, st_pin, (size_t)st_pinlen);
const void *spin = NULL;
uint16_t spinlen = 0;
norcow_get(PIN_KEY, &spin, &spinlen);
return const_cmp(pin, pinlen, spin, (size_t)spinlen);
}
bool storage_unlock(const uint8_t *pin, size_t len)
@ -182,6 +183,33 @@ bool storage_set(uint16_t key, const void *val, uint16_t len)
return norcow_set(key, val, len);
}
bool storage_has_pin(void)
{
if (!initialized) {
return false;
}
const void *spin = NULL;
uint16_t spinlen = 0;
norcow_get(PIN_KEY, &spin, &spinlen);
return spinlen != 0;
}
bool storage_change_pin(const uint8_t *pin, size_t len, const uint8_t *newpin, size_t newlen)
{
if (!initialized) {
return false;
}
if (!unlocked) {
// shutdown();
return false;
}
if (!pin_check(pin, len)) {
return false;
}
// TODO
return true;
}
bool storage_wipe(void)
{
return norcow_wipe();

View File

@ -11,5 +11,7 @@
bool storage_init(void);
bool storage_wipe(void);
bool storage_unlock(const uint8_t *pin, size_t len);
bool storage_has_pin(void);
bool storage_change_pin(const uint8_t *pin, size_t len, const uint8_t *newpin, size_t newlen);
bool storage_get(uint16_t key, const void **val, uint16_t *len);
bool storage_set(uint16_t key, const void *val, uint16_t len);