2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 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
|
|
|
from binascii import unhexlify
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
import pytest
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from trezorlib import btc, debuglink, device, messages as proto, misc
|
2014-02-22 01:15:21 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from .common import TrezorTest
|
2017-05-15 12:14:45 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
TXHASH_d5f65e = unhexlify(
|
|
|
|
"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
|
|
|
|
)
|
2017-05-15 12:14:45 +00:00
|
|
|
|
|
|
|
|
2017-12-19 18:24:18 +00:00
|
|
|
@pytest.mark.skip_t2
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestProtectionLevels(TrezorTest):
|
2014-02-22 01:15:21 +00:00
|
|
|
def test_initialize(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
self.client.set_expected_responses([proto.Features()])
|
|
|
|
self.client.init_device()
|
|
|
|
|
|
|
|
def test_apply_settings(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.ButtonRequest(),
|
|
|
|
proto.Success(),
|
|
|
|
proto.Features(),
|
|
|
|
]
|
|
|
|
) # TrezorClient reinitializes device
|
|
|
|
device.apply_settings(self.client, label="nazdar")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_change_pin(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.Success(),
|
|
|
|
proto.Features(),
|
|
|
|
]
|
|
|
|
)
|
2018-08-10 12:04:58 +00:00
|
|
|
device.change_pin(self.client)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_ping(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PassphraseRequest(),
|
|
|
|
proto.Success(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.client.ping("msg", True, True, True)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_get_entropy(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses([proto.ButtonRequest(), proto.Entropy()])
|
2018-08-10 12:04:58 +00:00
|
|
|
misc.get_entropy(self.client, 10)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_get_public_key(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.PinMatrixRequest(), proto.PassphraseRequest(), proto.PublicKey()]
|
|
|
|
)
|
2018-08-10 12:04:58 +00:00
|
|
|
btc.get_public_node(self.client, [])
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_get_address(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.PinMatrixRequest(), proto.PassphraseRequest(), proto.Address()]
|
|
|
|
)
|
|
|
|
btc.get_address(self.client, "Bitcoin", [])
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_wipe_device(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.ButtonRequest(), proto.Success(), proto.Features()]
|
|
|
|
)
|
2018-08-10 12:04:58 +00:00
|
|
|
device.wipe(self.client)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_load_device(self):
|
|
|
|
with self.client:
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.ButtonRequest(), proto.Success(), proto.Features()]
|
|
|
|
)
|
|
|
|
debuglink.load_device_by_mnemonic(
|
|
|
|
self.client,
|
|
|
|
"this is mnemonic",
|
|
|
|
"1234",
|
|
|
|
True,
|
|
|
|
"label",
|
|
|
|
"english",
|
|
|
|
skip_checksum=True,
|
|
|
|
)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
# This must fail, because device is already initialized
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
2018-08-13 16:21:24 +00:00
|
|
|
debuglink.load_device_by_mnemonic(
|
|
|
|
self.client,
|
|
|
|
"this is mnemonic",
|
|
|
|
"1234",
|
|
|
|
True,
|
|
|
|
"label",
|
|
|
|
"english",
|
|
|
|
skip_checksum=True,
|
|
|
|
)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_reset_device(self):
|
|
|
|
with self.client:
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.EntropyRequest()]
|
|
|
|
+ [proto.ButtonRequest()] * 24
|
|
|
|
+ [proto.Success(), proto.Features()]
|
|
|
|
)
|
|
|
|
device.reset(self.client, False, 128, True, False, "label", "english")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
# This must fail, because device is already initialized
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
2018-08-13 16:21:24 +00:00
|
|
|
device.reset(self.client, False, 128, True, False, "label", "english")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_recovery_device(self):
|
|
|
|
with self.client:
|
|
|
|
self.client.set_mnemonic(self.mnemonic12)
|
2018-05-22 17:01:34 +00:00
|
|
|
self.client.set_expected_responses(
|
2018-08-13 16:21:24 +00:00
|
|
|
[proto.ButtonRequest()]
|
|
|
|
+ [proto.WordRequest()] * 24
|
|
|
|
+ [proto.Success(), proto.Features()]
|
|
|
|
)
|
|
|
|
device.recover(self.client, 12, False, False, "label", "english")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
# This must fail, because device is already initialized
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
2018-08-13 16:21:24 +00:00
|
|
|
device.recover(self.client, 12, False, False, "label", "english")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_sign_message(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.ButtonRequest(),
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PassphraseRequest(),
|
|
|
|
proto.MessageSignature(),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
btc.sign_message(self.client, "Bitcoin", [], "testing message")
|
2014-02-22 01:15:21 +00:00
|
|
|
|
|
|
|
def test_verify_message(self):
|
|
|
|
with self.client:
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[proto.ButtonRequest(), proto.ButtonRequest(), proto.Success()]
|
|
|
|
)
|
2018-08-10 12:04:58 +00:00
|
|
|
btc.verify_message(
|
|
|
|
self.client,
|
2018-08-13 16:21:24 +00:00
|
|
|
"Bitcoin",
|
|
|
|
"14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e",
|
|
|
|
unhexlify(
|
|
|
|
"209e23edf0e4e47ff1dec27f32cd78c50e74ef018ee8a6adf35ae17c7a9b0dd96f48b493fd7dbab03efb6f439c6383c9523b3bbc5f1a7d158a6af90ab154e9be80"
|
|
|
|
),
|
|
|
|
"This is an example of a signed message.",
|
|
|
|
)
|
2014-02-22 01:15:21 +00:00
|
|
|
|
2014-04-27 21:06:06 +00:00
|
|
|
def test_signtx(self):
|
|
|
|
self.setup_mnemonic_pin_passphrase()
|
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
inp1 = proto.TxInputType(
|
2017-06-23 19:31:42 +00:00
|
|
|
address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e
|
|
|
|
prev_hash=TXHASH_d5f65e,
|
|
|
|
prev_index=0,
|
|
|
|
)
|
2014-04-27 21:06:06 +00:00
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
out1 = proto.TxOutputType(
|
2018-08-13 16:21:24 +00:00
|
|
|
address="1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1",
|
2017-06-23 19:31:42 +00:00
|
|
|
amount=390000 - 10000,
|
2017-12-12 15:40:11 +00:00
|
|
|
script_type=proto.OutputScriptType.PAYTOADDRESS,
|
2017-06-23 19:31:42 +00:00
|
|
|
)
|
2014-04-27 21:06:06 +00:00
|
|
|
|
|
|
|
with self.client:
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
self.client.set_expected_responses(
|
|
|
|
[
|
|
|
|
proto.PinMatrixRequest(),
|
|
|
|
proto.PassphraseRequest(),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXINPUT,
|
|
|
|
details=proto.TxRequestDetailsType(request_index=0),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXMETA,
|
|
|
|
details=proto.TxRequestDetailsType(tx_hash=TXHASH_d5f65e),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXINPUT,
|
|
|
|
details=proto.TxRequestDetailsType(
|
|
|
|
request_index=0, tx_hash=TXHASH_d5f65e
|
|
|
|
),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXINPUT,
|
|
|
|
details=proto.TxRequestDetailsType(
|
|
|
|
request_index=1, tx_hash=TXHASH_d5f65e
|
|
|
|
),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXOUTPUT,
|
|
|
|
details=proto.TxRequestDetailsType(
|
|
|
|
request_index=0, tx_hash=TXHASH_d5f65e
|
|
|
|
),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXOUTPUT,
|
|
|
|
details=proto.TxRequestDetailsType(request_index=0),
|
|
|
|
),
|
|
|
|
proto.ButtonRequest(code=proto.ButtonRequestType.ConfirmOutput),
|
|
|
|
proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXINPUT,
|
|
|
|
details=proto.TxRequestDetailsType(request_index=0),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXOUTPUT,
|
|
|
|
details=proto.TxRequestDetailsType(request_index=0),
|
|
|
|
),
|
|
|
|
proto.TxRequest(
|
|
|
|
request_type=proto.RequestType.TXOUTPUT,
|
|
|
|
details=proto.TxRequestDetailsType(request_index=0),
|
|
|
|
),
|
|
|
|
proto.TxRequest(request_type=proto.RequestType.TXFINISHED),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
btc.sign_tx(self.client, "Bitcoin", [inp1], [out1])
|
2014-04-27 21:06:06 +00:00
|
|
|
|
2014-02-22 01:15:21 +00:00
|
|
|
# def test_firmware_erase(self):
|
|
|
|
# pass
|
|
|
|
|
|
|
|
# def test_firmware_upload(self):
|
|
|
|
# pass
|