2016-05-23 13:44:09 +00:00
|
|
|
/*
|
2018-02-26 13:06:10 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2016-05-23 13:44:09 +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/>.
|
2016-05-23 13:44:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2017-10-16 16:33:05 +00:00
|
|
|
#include "py/mphal.h"
|
2017-10-17 17:18:16 +00:00
|
|
|
#include "py/objstr.h"
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2016-11-21 18:56:23 +00:00
|
|
|
#if MICROPY_PY_TREZORCONFIG
|
2016-10-14 16:40:30 +00:00
|
|
|
|
2018-05-16 12:42:41 +00:00
|
|
|
#include "embed/extmod/trezorobj.h"
|
|
|
|
|
2017-06-20 11:24:12 +00:00
|
|
|
#include "norcow.h"
|
2017-10-17 17:18:16 +00:00
|
|
|
#include "storage.h"
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-20 15:44:38 +00:00
|
|
|
/// def init() -> None:
|
2017-06-14 15:40:50 +00:00
|
|
|
/// '''
|
2017-10-17 17:18:16 +00:00
|
|
|
/// Initializes the storage. Must be called before any other method is
|
|
|
|
/// called from this module!
|
2017-06-14 15:40:50 +00:00
|
|
|
/// '''
|
2017-06-20 11:24:12 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_init(void) {
|
2017-12-09 13:48:49 +00:00
|
|
|
storage_init();
|
2017-06-20 11:24:12 +00:00
|
|
|
return mp_const_none;
|
2016-05-23 13:44:09 +00:00
|
|
|
}
|
2017-06-20 11:24:12 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_init_obj, mod_trezorconfig_init);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2018-02-26 22:02:44 +00:00
|
|
|
/// def check_pin(pin: int, waitcallback: (int, int -> None)) -> bool:
|
|
|
|
/// '''
|
|
|
|
/// Check the given PIN. Returns True on success, False on failure.
|
|
|
|
/// '''
|
|
|
|
STATIC mp_obj_t mod_trezorconfig_check_pin(mp_obj_t pin, mp_obj_t waitcallback) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint32_t pin_i = trezor_obj_get_uint(pin);
|
2018-02-26 22:02:44 +00:00
|
|
|
if (sectrue != storage_check_pin(pin_i, waitcallback)) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_check_pin_obj, mod_trezorconfig_check_pin);
|
|
|
|
|
2017-12-15 09:27:36 +00:00
|
|
|
/// def unlock(pin: int, waitcallback: (int, int -> None)) -> bool:
|
2017-10-16 16:33:05 +00:00
|
|
|
/// '''
|
2017-10-17 17:18:16 +00:00
|
|
|
/// Attempts to unlock the storage with given PIN. Returns True on
|
|
|
|
/// success, False on failure.
|
2017-10-16 16:33:05 +00:00
|
|
|
/// '''
|
2017-12-14 16:14:15 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_unlock(mp_obj_t pin, mp_obj_t waitcallback) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint32_t pin_i = trezor_obj_get_uint(pin);
|
2017-12-15 09:27:36 +00:00
|
|
|
if (sectrue != storage_unlock(pin_i, waitcallback)) {
|
2017-10-16 16:33:05 +00:00
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
2017-12-14 16:14:15 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_unlock_obj, mod_trezorconfig_unlock);
|
2017-10-16 16:33:05 +00:00
|
|
|
|
2017-10-24 11:55:29 +00:00
|
|
|
/// def has_pin() -> bool:
|
|
|
|
/// '''
|
|
|
|
/// Returns True if storage has a configured PIN, False otherwise.
|
|
|
|
/// '''
|
|
|
|
STATIC mp_obj_t mod_trezorconfig_has_pin(void) {
|
2017-11-06 16:26:25 +00:00
|
|
|
if (sectrue != storage_has_pin()) {
|
2017-10-24 11:55:29 +00:00
|
|
|
return mp_const_false;
|
|
|
|
}
|
2017-11-06 16:26:25 +00:00
|
|
|
return mp_const_true;
|
2017-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_has_pin_obj, mod_trezorconfig_has_pin);
|
|
|
|
|
2017-12-15 09:27:36 +00:00
|
|
|
/// def change_pin(pin: int, newpin: int, waitcallback: (int, int -> None)) -> bool:
|
2017-10-24 11:55:29 +00:00
|
|
|
/// '''
|
|
|
|
/// Change PIN. Returns True on success, False on failure.
|
|
|
|
/// '''
|
2017-12-14 16:14:15 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_change_pin(mp_obj_t pin, mp_obj_t newpin, mp_obj_t waitcallback) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint32_t pin_i = trezor_obj_get_uint(pin);
|
|
|
|
uint32_t newpin_i = trezor_obj_get_uint(newpin);
|
2017-12-15 09:27:36 +00:00
|
|
|
if (sectrue != storage_change_pin(pin_i, newpin_i, waitcallback)) {
|
2017-10-24 11:55:29 +00:00
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
2017-12-14 16:14:15 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorconfig_change_pin_obj, mod_trezorconfig_change_pin);
|
2017-10-24 11:55:29 +00:00
|
|
|
|
2018-01-13 14:21:40 +00:00
|
|
|
/// def get(app: int, key: int, public: bool=False) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2016-11-23 13:45:58 +00:00
|
|
|
/// Gets a value of given key for given app (or empty bytes if not set).
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2018-01-13 14:21:40 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_get(size_t n_args, const mp_obj_t *args) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint8_t app = trezor_obj_get_uint8(args[0]) & 0x7F;
|
|
|
|
uint8_t key = trezor_obj_get_uint8(args[1]);
|
2018-01-13 14:21:40 +00:00
|
|
|
if (n_args > 2 && args[2] == mp_const_true) {
|
|
|
|
app |= 0x80;
|
|
|
|
}
|
|
|
|
uint16_t appkey = (app << 8) | key;
|
2017-10-17 17:18:16 +00:00
|
|
|
uint16_t len = 0;
|
2016-10-14 16:40:30 +00:00
|
|
|
const void *val;
|
2017-11-05 22:32:32 +00:00
|
|
|
if (sectrue != storage_get(appkey, &val, &len) || len == 0) {
|
2016-11-23 13:45:58 +00:00
|
|
|
return mp_const_empty_bytes;
|
2016-11-21 20:15:57 +00:00
|
|
|
}
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_bytes(val, len);
|
2016-05-23 13:44:09 +00:00
|
|
|
}
|
2018-01-13 14:21:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_get_obj, 2, 3, mod_trezorconfig_get);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2018-01-13 14:21:40 +00:00
|
|
|
/// def set(app: int, key: int, value: bytes, public: bool=False) -> None:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
|
|
|
/// Sets a value of given key for given app.
|
|
|
|
/// '''
|
2018-01-13 14:21:40 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_set(size_t n_args, const mp_obj_t *args) {
|
2018-05-16 12:42:41 +00:00
|
|
|
uint8_t app = trezor_obj_get_uint8(args[0]) & 0x7F;
|
|
|
|
uint8_t key = trezor_obj_get_uint8(args[1]);
|
2018-01-13 14:21:40 +00:00
|
|
|
if (n_args > 3 && args[3] == mp_const_true) {
|
|
|
|
app |= 0x80;
|
|
|
|
}
|
|
|
|
uint16_t appkey = (app << 8) | key;
|
|
|
|
mp_buffer_info_t value;
|
|
|
|
mp_get_buffer_raise(args[2], &value, MP_BUFFER_READ);
|
|
|
|
if (sectrue != storage_set(appkey, value.buf, value.len)) {
|
2016-11-21 20:15:57 +00:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, "Could not save value");
|
2016-10-14 16:40:30 +00:00
|
|
|
}
|
2016-05-23 13:44:09 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-01-13 14:21:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_set_obj, 3, 4, mod_trezorconfig_set);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-20 15:44:38 +00:00
|
|
|
/// def wipe() -> None:
|
2016-11-06 11:38:33 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Erases the whole config. Use with caution!
|
2016-11-06 11:38:33 +00:00
|
|
|
/// '''
|
2017-06-20 11:24:12 +00:00
|
|
|
STATIC mp_obj_t mod_trezorconfig_wipe(void) {
|
2017-12-14 15:27:18 +00:00
|
|
|
storage_wipe();
|
2016-11-06 11:38:33 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-20 11:24:12 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_wipe_obj, mod_trezorconfig_wipe);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_trezorconfig_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorconfig) },
|
2017-06-20 11:24:12 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mod_trezorconfig_init_obj) },
|
2018-02-26 22:02:44 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_check_pin), MP_ROM_PTR(&mod_trezorconfig_check_pin_obj) },
|
2017-10-16 16:33:05 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&mod_trezorconfig_unlock_obj) },
|
2017-10-24 11:55:29 +00:00
|
|
|
{ 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) },
|
2017-06-20 11:24:12 +00:00
|
|
|
{ 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) },
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
2017-06-14 16:47:38 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorconfig_globals, mp_module_trezorconfig_globals_table);
|
2016-05-23 13:44:09 +00:00
|
|
|
|
2017-06-14 16:47:38 +00:00
|
|
|
const mp_obj_module_t mp_module_trezorconfig = {
|
2016-05-23 13:44:09 +00:00
|
|
|
.base = { &mp_type_module },
|
2017-06-14 16:47:38 +00:00
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_trezorconfig_globals,
|
2016-05-23 13:44:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_TREZORCONFIG
|