2018-02-27 15:35:21 +00:00
|
|
|
from trezor.crypto import hashlib, hmac, random
|
|
|
|
from apps.common import storage
|
2018-02-24 17:58:02 +00:00
|
|
|
|
2018-02-21 14:04:49 +00:00
|
|
|
memory = {}
|
|
|
|
_seed = None
|
2018-02-24 17:58:02 +00:00
|
|
|
_passphrase = None
|
|
|
|
_state_salt = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_state(salt: bytes=None, passphrase: str=None):
|
|
|
|
global _passphrase, _state_salt
|
|
|
|
if salt is None:
|
|
|
|
# generate a random salt if not provided and not already cached
|
|
|
|
if _state_salt is None:
|
|
|
|
_state_salt = random.bytes(32)
|
|
|
|
else:
|
|
|
|
# otherwise copy provided salt to cached salt
|
|
|
|
_state_salt = salt
|
2017-05-08 20:31:21 +00:00
|
|
|
|
2018-02-24 17:58:02 +00:00
|
|
|
# state = HMAC(passphrase, salt || device_id)
|
|
|
|
if passphrase is None:
|
|
|
|
key = _passphrase if _passphrase is not None else ''
|
|
|
|
else:
|
|
|
|
key = passphrase
|
2018-02-27 15:35:21 +00:00
|
|
|
msg = _state_salt + storage.get_device_id().encode()
|
2018-02-24 17:58:02 +00:00
|
|
|
state = hmac.new(key.encode(), msg, hashlib.sha256).digest()
|
2017-05-08 20:31:21 +00:00
|
|
|
|
2018-02-24 17:58:02 +00:00
|
|
|
return _state_salt + state
|
2018-02-09 17:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_seed():
|
|
|
|
global _seed
|
|
|
|
return _seed
|
|
|
|
|
|
|
|
|
2018-02-24 17:58:02 +00:00
|
|
|
def set_seed(seed, passphrase):
|
|
|
|
global _seed, _passphrase
|
|
|
|
_seed, _passphrase = seed, _passphrase
|
2018-02-09 17:07:47 +00:00
|
|
|
|
|
|
|
|
2018-02-27 19:48:01 +00:00
|
|
|
def has_passphrase():
|
|
|
|
global _passphrase
|
|
|
|
return _passphrase is not None
|
|
|
|
|
|
|
|
|
2017-05-08 20:31:21 +00:00
|
|
|
def clear():
|
2018-02-24 17:58:02 +00:00
|
|
|
global _seed, _passphrase
|
|
|
|
global _state_salt
|
|
|
|
_seed, _passphrase = None, None
|
|
|
|
_state_salt = None
|