2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library 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 Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2020-05-28 09:57:04 +00:00
|
|
|
from trezorlib import debuglink, device, messages, misc
|
2024-11-15 16:31:22 +00:00
|
|
|
from trezorlib.debuglink import SessionDebugWrapper as Session
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2022-02-03 12:16:26 +00:00
|
|
|
from trezorlib.tools import parse_path
|
2020-11-25 14:01:57 +00:00
|
|
|
from trezorlib.transport import udp
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2019-09-11 12:43:32 +00:00
|
|
|
from ..common import MNEMONIC12
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2014-02-06 22:34:13 +00:00
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("legacy")
|
2022-01-31 12:25:30 +00:00
|
|
|
def test_layout(client: Client):
|
2021-11-10 21:19:53 +00:00
|
|
|
layout = client.debug.state().layout
|
|
|
|
assert len(layout) == 1024
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("legacy")
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12)
|
2024-11-26 14:57:24 +00:00
|
|
|
def test_mnemonic(session: Session):
|
|
|
|
session.ensure_unlocked()
|
|
|
|
mnemonic = session.client.debug.state().mnemonic_secret
|
2021-11-10 21:19:53 +00:00
|
|
|
assert mnemonic == MNEMONIC12.encode()
|
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("legacy")
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.setup_client(mnemonic=MNEMONIC12, pin="1234", passphrase="")
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_pin(session: Session):
|
|
|
|
resp = session.call_raw(
|
|
|
|
messages.GetAddress(address_n=parse_path("m/44'/0'/0'/0/0"))
|
|
|
|
)
|
2021-11-10 21:19:53 +00:00
|
|
|
assert isinstance(resp, messages.PinMatrixRequest)
|
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
with session.client as client:
|
|
|
|
state = client.debug.state()
|
|
|
|
assert state.pin == "1234"
|
|
|
|
assert state.matrix != ""
|
2021-11-10 21:19:53 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
pin_encoded = client.debug.encode_pin("1234")
|
|
|
|
resp = session.call_raw(messages.PinMatrixAck(pin=pin_encoded))
|
|
|
|
assert isinstance(resp, messages.PassphraseRequest)
|
2021-11-10 21:19:53 +00:00
|
|
|
|
2024-11-15 16:31:22 +00:00
|
|
|
resp = session.call_raw(messages.PassphraseAck(passphrase=""))
|
|
|
|
assert isinstance(resp, messages.Address)
|
2020-05-28 09:57:04 +00:00
|
|
|
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
@pytest.mark.models("core")
|
2024-11-15 16:31:22 +00:00
|
|
|
def test_softlock_instability(session: Session):
|
2024-11-25 13:42:27 +00:00
|
|
|
if session.session_version == Session.THP_V2:
|
|
|
|
raise Exception("THIS NEEDS TO BE CHANGED FOR THP")
|
2024-11-15 16:31:22 +00:00
|
|
|
|
2020-05-28 09:57:04 +00:00
|
|
|
def load_device():
|
|
|
|
debuglink.load_device(
|
2024-11-15 16:31:22 +00:00
|
|
|
session,
|
2020-05-28 09:57:04 +00:00
|
|
|
mnemonic=MNEMONIC12,
|
|
|
|
pin="1234",
|
|
|
|
passphrase_protection=False,
|
|
|
|
label="test",
|
|
|
|
)
|
|
|
|
|
|
|
|
# start from a clean slate:
|
2024-11-15 16:31:22 +00:00
|
|
|
resp = session.client.debug.reseed(0)
|
2020-11-25 14:01:57 +00:00
|
|
|
if isinstance(resp, messages.Failure) and not isinstance(
|
2024-11-15 16:31:22 +00:00
|
|
|
session.client.transport, udp.UdpTransport
|
2020-11-25 14:01:57 +00:00
|
|
|
):
|
|
|
|
pytest.xfail("reseed only supported on emulator")
|
2024-11-15 16:31:22 +00:00
|
|
|
device.wipe(session)
|
|
|
|
entropy_after_wipe = misc.get_entropy(session, 16)
|
2024-11-25 13:42:27 +00:00
|
|
|
session.refresh_features()
|
2020-05-28 09:57:04 +00:00
|
|
|
|
|
|
|
# configure and wipe the device
|
|
|
|
load_device()
|
2024-11-15 16:31:22 +00:00
|
|
|
session.client.debug.reseed(0)
|
|
|
|
device.wipe(session)
|
|
|
|
assert misc.get_entropy(session, 16) == entropy_after_wipe
|
2024-11-25 13:42:27 +00:00
|
|
|
session.refresh_features()
|
2020-05-28 09:57:04 +00:00
|
|
|
|
|
|
|
load_device()
|
|
|
|
# the device has PIN -> lock it
|
2024-11-15 16:31:22 +00:00
|
|
|
session.call(messages.LockDevice())
|
|
|
|
session.client.debug.reseed(0)
|
2020-05-28 09:57:04 +00:00
|
|
|
# wipe_device should succeed with no need to unlock
|
2024-11-15 16:31:22 +00:00
|
|
|
device.wipe(session)
|
2020-05-28 09:57:04 +00:00
|
|
|
# the device is now trying to run the lockscreen, which attempts to unlock.
|
|
|
|
# If the device actually called config.unlock(), it would use additional randomness.
|
|
|
|
# That is undesirable. Assert that the returned entropy is still the same.
|
2024-11-15 16:31:22 +00:00
|
|
|
assert misc.get_entropy(session, 16) == entropy_after_wipe
|