def test_set_failed(self): self.setup_mnemonic_nopin_nopassphrase() features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is False # Check that there's no PIN protection ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.Success) # Let's set new PIN ret = self.client.call_raw(proto.ChangePin()) assert isinstance(ret, proto.ButtonRequest) # Press button self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # Send the PIN for first time assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.encode_pin(self.pin6) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Send the PIN for second time, but with typo assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.encode_pin(self.pin4) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Now it should fail, because pins are different assert isinstance(ret, proto.Failure) # Check that there's still no PIN protection now features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is False ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.Success)
def test_remove_pin(self): self.setup_mnemonic_pin_passphrase() features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is True # Check that there's PIN protection ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.PinMatrixRequest) self.client.call_raw(proto.Cancel()) # Let's remove PIN ret = self.client.call_raw(proto.ChangePin(remove=True)) assert isinstance(ret, proto.ButtonRequest) # Press button self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # Send current PIN assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.read_pin_encoded() ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Now we're done assert isinstance(ret, proto.Success) # Check that there's no PIN protection now features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is False ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.Success)
def test_pin_passphrase(self): mnemonic = self.mnemonic12.split(' ') ret = self.client.call_raw( proto.RecoveryDevice(word_count=12, passphrase_protection=True, pin_protection=True, label='label', language='english', enforce_wordlist=True)) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for first time pin_encoded = self.client.debug.encode_pin(self.pin6) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for second time pin_encoded = self.client.debug.encode_pin(self.pin6) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) fakes = 0 for _ in range(int(12 * 2)): assert isinstance(ret, proto.WordRequest) (word, pos) = self.client.debug.read_recovery_word() if pos != 0: ret = self.client.call_raw( proto.WordAck(word=mnemonic[pos - 1])) mnemonic[pos - 1] = None else: ret = self.client.call_raw(proto.WordAck(word=word)) fakes += 1 print(mnemonic) # Workflow succesfully ended assert isinstance(ret, proto.Success) # 12 expected fake words and all words of mnemonic are used assert fakes == 12 assert mnemonic == [None] * 12 # Mnemonic is the same self.client.init_device() assert self.client.debug.read_mnemonic() == self.mnemonic12 assert self.client.features.pin_protection is True assert self.client.features.passphrase_protection is True # Do passphrase-protected action, PassphraseRequest should be raised resp = self.client.call_raw(proto.Ping(passphrase_protection=True)) assert isinstance(resp, proto.PassphraseRequest) self.client.call_raw(proto.Cancel()) # Do PIN-protected action, PinRequest should be raised resp = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(resp, proto.PinMatrixRequest) self.client.call_raw(proto.Cancel())
def test_nopin_nopassphrase(self): mnemonic = self.mnemonic12.split(" ") ret = self.client.call_raw( proto.RecoveryDevice( word_count=12, passphrase_protection=False, pin_protection=False, label="label", language="english", enforce_wordlist=True, )) # click through confirmation assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) fakes = 0 for _ in range(int(12 * 2)): assert isinstance(ret, proto.WordRequest) (word, pos) = self.client.debug.read_recovery_word() if pos != 0: ret = self.client.call_raw( proto.WordAck(word=mnemonic[pos - 1])) mnemonic[pos - 1] = None else: ret = self.client.call_raw(proto.WordAck(word=word)) fakes += 1 print(mnemonic) # Workflow succesfully ended assert isinstance(ret, proto.Success) # 12 expected fake words and all words of mnemonic are used assert fakes == 12 assert mnemonic == [None] * 12 # Mnemonic is the same self.client.init_device() assert self.client.debug.read_mnemonic_secret( ) == self.mnemonic12.encode() assert self.client.features.pin_protection is False assert self.client.features.passphrase_protection is False # Do passphrase-protected action, PassphraseRequest should NOT be raised resp = self.client.call_raw(proto.Ping(passphrase_protection=True)) assert isinstance(resp, proto.Success) # Do PIN-protected action, PinRequest should NOT be raised resp = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(resp, proto.Success)
def test_change_pin(self): self.setup_mnemonic_pin_passphrase() features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is True # Check that there's PIN protection ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.PinMatrixRequest) self.client.call_raw(proto.Cancel()) # Check current PIN value assert self.client.debug.read_pin()[0] == self.pin4 # Let's change PIN ret = self.client.call_raw(proto.ChangePin()) assert isinstance(ret, proto.ButtonRequest) # Press button self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # Send current PIN assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.read_pin_encoded() ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Send new PIN for first time assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.encode_pin(self.pin6) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Send the PIN for second time assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = self.client.debug.encode_pin(self.pin6) ret = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Now we're done assert isinstance(ret, proto.Success) # Check that there's still PIN protection now features = self.client.call_raw(proto.Initialize()) assert features.pin_protection is True ret = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.PinMatrixRequest) self.client.call_raw(proto.Cancel()) # Check that the PIN is correct assert self.client.debug.read_pin()[0] == self.pin6
def test_set_pin(self, client): features = client.call_raw(proto.Initialize()) assert features.pin_protection is False # Check that there's no PIN protection ret = client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.Success) # Let's set new PIN ret = client.call_raw(proto.ChangePin()) assert isinstance(ret, proto.ButtonRequest) # Press button client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) # Send the PIN for first time assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = client.debug.encode_pin(self.pin6) ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Send the PIN for second time assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = client.debug.encode_pin(self.pin6) ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Now we're done assert isinstance(ret, proto.Success) # Check that there's PIN protection now features = client.call_raw(proto.Initialize()) assert features.pin_protection is True # Check that the PIN is correct self.check_pin(client, self.pin6)
def check_pin(self, client, pin): client.clear_session() ret = client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(ret, proto.PinMatrixRequest) pin_encoded = client.debug.encode_pin(pin) ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(ret, proto.Success)
def test_set_pin_to_wipe_code(client): # Set wipe code. _set_wipe_code(client, WIPE_CODE4) # Try to set the PIN to the current wipe code value. ret = client.call_raw(messages.ChangePin()) assert isinstance(ret, messages.ButtonRequest) # Confirm intent to set PIN. client.debug.press_yes() ret = client.call_raw(messages.ButtonAck()) # Send the new PIN for the first time. assert isinstance(ret, messages.PinMatrixRequest) pin_encoded = client.debug.encode_pin(WIPE_CODE4) ret = client.call_raw(messages.PinMatrixAck(pin=pin_encoded)) # Send the new PIN for the second time. assert isinstance(ret, messages.PinMatrixRequest) pin_encoded = client.debug.encode_pin(WIPE_CODE4) ret = client.call_raw(messages.PinMatrixAck(pin=pin_encoded)) # The operation should fail, because the PIN must be different from the wipe code. assert isinstance(ret, messages.Failure) assert ret.code == messages.FailureType.ProcessError # Check that there is no PIN protection. client.init_device() assert client.features.pin_protection is False ret = client.call_raw(messages.Ping(pin_protection=True)) assert isinstance(ret, messages.Success)
def test_pin_passphrase(self, client): mnemonic = MNEMONIC12.split(" ") ret = client.call_raw( proto.RecoveryDevice( word_count=12, passphrase_protection=True, pin_protection=True, label="label", language="english", enforce_wordlist=True, )) # click through confirmation assert isinstance(ret, proto.ButtonRequest) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for first time pin_encoded = client.debug.encode_pin(PIN6) ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for second time pin_encoded = client.debug.encode_pin(PIN6) ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) fakes = 0 for _ in range(int(12 * 2)): assert isinstance(ret, proto.WordRequest) (word, pos) = client.debug.read_recovery_word() if pos != 0: ret = client.call_raw(proto.WordAck(word=mnemonic[pos - 1])) mnemonic[pos - 1] = None else: ret = client.call_raw(proto.WordAck(word=word)) fakes += 1 print(mnemonic) # Workflow succesfully ended assert isinstance(ret, proto.Success) # 12 expected fake words and all words of mnemonic are used assert fakes == 12 assert mnemonic == [None] * 12 # Mnemonic is the same client.init_device() assert client.debug.read_mnemonic_secret() == MNEMONIC12.encode() assert client.features.pin_protection is True assert client.features.passphrase_protection is True # Do passphrase-protected action, PassphraseRequest should be raised resp = client.call_raw(proto.Ping(passphrase_protection=True)) assert isinstance(resp, proto.PassphraseRequest) client.call_raw(proto.Cancel())
def test_pin(self, client): resp = client.call_raw( messages.Ping(message="test", pin_protection=True)) assert isinstance(resp, messages.PinMatrixRequest) pin, matrix = client.debug.read_pin() assert pin == "1234" assert matrix != "" pin_encoded = client.debug.read_pin_encoded() resp = client.call_raw(messages.PinMatrixAck(pin=pin_encoded)) assert isinstance(resp, messages.Success)
def test_pin(self): self.setup_mnemonic_pin_passphrase() # Manually trigger PinMatrixRequest resp = self.client.call_raw( proto.Ping(message='test', pin_protection=True)) assert isinstance(resp, proto.PinMatrixRequest) pin = self.client.debug.read_pin() assert pin[0] == '1234' assert pin[1] != '' pin_encoded = self.client.debug.read_pin_encoded() resp = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(resp, proto.Success)
def test_pin(self): self.setup_mnemonic_pin_passphrase() # Manually trigger PinMatrixRequest resp = self.client.call_raw( proto.Ping(message="test", pin_protection=True)) assert isinstance(resp, proto.PinMatrixRequest) pin, matrix = self.client.debug.read_pin() assert pin == "1234" assert matrix != "" pin_encoded = self.client.debug.read_pin_encoded() resp = self.client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(resp, proto.Success)
# # 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>. import pytest import trezorlib.messages as m from trezorlib.exceptions import Cancelled from ..common import TEST_ADDRESS_N @pytest.mark.parametrize( "message", [ m.Ping(message="hello", button_protection=True), m.GetAddress( address_n=TEST_ADDRESS_N, coin_name="Testnet", script_type=m.InputScriptType.SPENDADDRESS, show_display=True, ), ], ) def test_cancel_message_via_cancel(client, message): def input_flow(): yield client.cancel() with client, pytest.raises(Cancelled): client.set_expected_responses([m.ButtonRequest(), m.Failure()])
def test_ping(self, client): ping = client.call(messages.Ping(message="ahoj!")) assert ping == messages.Success(message="ahoj!")
def test_reset_device(self): # No PIN, no passphrase external_entropy = b"zlutoucky kun upel divoke ody" * 2 strength = 128 ret = self.client.call_raw( proto.ResetDevice( display_random=False, strength=strength, passphrase_protection=False, pin_protection=False, language="english", label="test", ) ) assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # Provide entropy assert isinstance(ret, proto.EntropyRequest) internal_entropy = self.client.debug.read_reset_entropy() ret = self.client.call_raw(proto.EntropyAck(entropy=external_entropy)) # Generate mnemonic locally entropy = generate_entropy(strength, internal_entropy, external_entropy) expected_mnemonic = Mnemonic("english").to_mnemonic(entropy) mnemonic = [] for _ in range(strength // 32 * 3): assert isinstance(ret, proto.ButtonRequest) mnemonic.append(self.client.debug.read_reset_word()) self.client.debug.press_yes() self.client.call_raw(proto.ButtonAck()) mnemonic = " ".join(mnemonic) # Compare that device generated proper mnemonic for given entropies assert mnemonic == expected_mnemonic mnemonic = [] for _ in range(strength // 32 * 3): assert isinstance(ret, proto.ButtonRequest) mnemonic.append(self.client.debug.read_reset_word()) self.client.debug.press_yes() resp = self.client.call_raw(proto.ButtonAck()) assert isinstance(resp, proto.Success) mnemonic = " ".join(mnemonic) # Compare that second pass printed out the same mnemonic once again assert mnemonic == expected_mnemonic # Check if device is properly initialized resp = self.client.call_raw(proto.Initialize()) assert resp.initialized is True assert resp.needs_backup is False assert resp.pin_protection is False assert resp.passphrase_protection is False # Do passphrase-protected action, PassphraseRequest should NOT be raised resp = self.client.call_raw(proto.Ping(passphrase_protection=True)) assert isinstance(resp, proto.Success) # Do PIN-protected action, PinRequest should NOT be raised resp = self.client.call_raw(proto.Ping(pin_protection=True)) assert isinstance(resp, proto.Success)
def test_reset_device_pin(self, client): external_entropy = b"zlutoucky kun upel divoke ody" * 2 strength = 128 ret = client.call_raw( proto.ResetDevice( display_random=True, strength=strength, passphrase_protection=True, pin_protection=True, language="english", label="test", )) # Do you want ... ? assert isinstance(ret, proto.ButtonRequest) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) # Entropy screen #1 assert isinstance(ret, proto.ButtonRequest) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) # Entropy screen #2 assert isinstance(ret, proto.ButtonRequest) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for first time pin_encoded = client.debug.encode_pin("654") ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for second time pin_encoded = client.debug.encode_pin("654") ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) # Provide entropy assert isinstance(ret, proto.EntropyRequest) internal_entropy = client.debug.read_reset_entropy() ret = client.call_raw(proto.EntropyAck(entropy=external_entropy)) # Generate mnemonic locally entropy = generate_entropy(strength, internal_entropy, external_entropy) expected_mnemonic = Mnemonic("english").to_mnemonic(entropy) mnemonic = [] for _ in range(strength // 32 * 3): assert isinstance(ret, proto.ButtonRequest) mnemonic.append(client.debug.read_reset_word()) client.debug.press_yes() client.call_raw(proto.ButtonAck()) mnemonic = " ".join(mnemonic) # Compare that device generated proper mnemonic for given entropies assert mnemonic == expected_mnemonic mnemonic = [] for _ in range(strength // 32 * 3): assert isinstance(ret, proto.ButtonRequest) mnemonic.append(client.debug.read_reset_word()) client.debug.press_yes() resp = client.call_raw(proto.ButtonAck()) assert isinstance(resp, proto.Success) mnemonic = " ".join(mnemonic) # Compare that second pass printed out the same mnemonic once again assert mnemonic == expected_mnemonic # Check if device is properly initialized resp = client.call_raw(proto.Initialize()) assert resp.initialized is True assert resp.needs_backup is False assert resp.pin_protection is True assert resp.passphrase_protection is True # Do passphrase-protected action, PassphraseRequest should be raised resp = client.call_raw(proto.Ping(passphrase_protection=True)) assert isinstance(resp, proto.PassphraseRequest) client.call_raw(proto.Cancel())
def test_ping(self): ping = self.client.call(messages.Ping(message='ahoj!')) assert ping == messages.Success(message='ahoj!')