def test_failed_pin(self): # external_entropy = b'zlutoucky kun upel divoke ody' * 2 strength = 128 ret = self.client.call_raw( proto.ResetDevice( display_random=True, strength=strength, passphrase_protection=True, pin_protection=True, language="english", label="test", ) ) assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.PinMatrixRequest) # Enter PIN for first time pin_encoded = self.client.debug.encode_pin(self.pin4) 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)) assert isinstance(ret, proto.Failure)
def test_reset_device_skip_backup_break(self): 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', skip_backup=True)) # Provide entropy assert isinstance(ret, proto.EntropyRequest) ret = self.client.call_raw(proto.EntropyAck(entropy=external_entropy)) assert isinstance(ret, proto.Success) # Check if device is properly initialized resp = self.client.call_raw(proto.Initialize()) assert resp.initialized is True assert resp.needs_backup is True # start Backup workflow ret = self.client.call_raw(proto.BackupDevice()) # send Initialize -> break workflow ret = self.client.call_raw(proto.Initialize()) assert isinstance(resp, proto.Features) # start backup again - should fail ret = self.client.call_raw(proto.BackupDevice()) assert isinstance(ret, proto.Failure)
def test_reset_device(client: Client): assert client.features.pin_protection is False assert client.features.passphrase_protection is False os_urandom = mock.Mock(return_value=EXTERNAL_ENTROPY) with mock.patch("os.urandom", os_urandom), client: client.set_expected_responses( [messages.ButtonRequest] + [messages.EntropyRequest] + [messages.ButtonRequest] * 24 + [messages.Success, messages.Features] ) device.reset(client, False, 128, True, False, "label", "en-US") with pytest.raises(TrezorFailure): # This must fail, because device is already initialized # Using direct call because `device.reset` has its own check client.call( messages.ResetDevice( display_random=False, strength=128, passphrase_protection=True, pin_protection=False, label="label", language="en-US", ) )
def test_reset_device_no_backup(self): ret = self.client.call_raw( proto.ResetDevice( display_random=False, strength=self.strength, passphrase_protection=False, pin_protection=False, language="english", label="test", no_backup=True, )) assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # Provide entropy assert isinstance(ret, proto.EntropyRequest) ret = self.client.call_raw( proto.EntropyAck(entropy=self.external_entropy)) assert isinstance(ret, proto.Success) # Check if device is properly initialized ret = self.client.call_raw(proto.Initialize()) assert ret.initialized is True assert ret.needs_backup is False assert ret.unfinished_backup is False assert ret.no_backup is True # start backup - should fail ret = self.client.call_raw(proto.BackupDevice()) assert isinstance(ret, proto.Failure)
def test_reset_device_skip_backup_show_entropy_fail(self, client): ret = client.call_raw( proto.ResetDevice( display_random=True, strength=self.strength, passphrase_protection=False, pin_protection=False, language="english", label="test", skip_backup=True, )) assert isinstance(ret, proto.Failure)
def test_reset_device_skip_backup_show_entropy_fail(client: Client): ret = client.call_raw( messages.ResetDevice( display_random=True, strength=STRENGTH, passphrase_protection=False, pin_protection=False, language="en-US", label="test", skip_backup=True, )) assert isinstance(ret, messages.Failure)
def test_reset_device_skip_backup_break(client): ret = client.call_raw( messages.ResetDevice( display_random=False, strength=STRENGTH, passphrase_protection=False, pin_protection=False, language="en-US", label="test", skip_backup=True, ) ) assert isinstance(ret, messages.ButtonRequest) client.debug.press_yes() ret = client.call_raw(messages.ButtonAck()) # Provide entropy assert isinstance(ret, messages.EntropyRequest) ret = client.call_raw(messages.EntropyAck(entropy=EXTERNAL_ENTROPY)) assert isinstance(ret, messages.Success) # Check if device is properly initialized ret = client.call_raw(messages.Initialize()) assert ret.initialized is True assert ret.needs_backup is True assert ret.unfinished_backup is False assert ret.no_backup is False # start Backup workflow ret = client.call_raw(messages.BackupDevice()) # send Initialize -> break workflow ret = client.call_raw(messages.Initialize()) assert isinstance(ret, messages.Features) assert ret.initialized is True assert ret.needs_backup is False assert ret.unfinished_backup is True assert ret.no_backup is False # start backup again - should fail ret = client.call_raw(messages.BackupDevice()) assert isinstance(ret, messages.Failure) # read Features again ret = client.call_raw(messages.Initialize()) assert isinstance(ret, messages.Features) assert ret.initialized is True assert ret.needs_backup is False assert ret.unfinished_backup is True assert ret.no_backup is False
def test_reset_device_skip_backup_break(self): ret = self.client.call_raw( proto.ResetDevice( display_random=False, strength=self.strength, passphrase_protection=False, pin_protection=False, language="english", label="test", skip_backup=True, ) ) # Provide entropy assert isinstance(ret, proto.EntropyRequest) ret = self.client.call_raw(proto.EntropyAck(entropy=self.external_entropy)) assert isinstance(ret, proto.Success) # Check if device is properly initialized ret = self.client.call_raw(proto.Initialize()) assert ret.initialized is True assert ret.needs_backup is True assert ret.unfinished_backup is False assert ret.no_backup is False # start Backup workflow ret = self.client.call_raw(proto.BackupDevice()) # send Initialize -> break workflow ret = self.client.call_raw(proto.Initialize()) assert isinstance(ret, proto.Features) assert ret.initialized is True assert ret.needs_backup is False assert ret.unfinished_backup is True assert ret.no_backup is False # start backup again - should fail ret = self.client.call_raw(proto.BackupDevice()) assert isinstance(ret, proto.Failure) # read Features again ret = self.client.call_raw(proto.Initialize()) assert isinstance(ret, proto.Features) assert ret.initialized is True assert ret.needs_backup is False assert ret.unfinished_backup is True assert ret.no_backup is False
def test_failed_pin(self): # external_entropy = b'zlutoucky kun upel divoke ody' * 2 strength = 128 ret = self.client.call_raw( proto.ResetDevice(strength=strength, pin_protection=True, label="test") ) # Enter PIN for first time assert isinstance(ret, proto.ButtonRequest) self.client.debug.input("654") ret = self.client.call_raw(proto.ButtonAck()) # Enter PIN for second time assert isinstance(ret, proto.ButtonRequest) self.client.debug.input("456") ret = self.client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.ButtonRequest)
def test_reset_device(self, client): with client: client.set_expected_responses( [proto.ButtonRequest()] + [proto.EntropyRequest()] + [proto.ButtonRequest()] * 24 + [proto.Success(), proto.Features()]) device.reset(client, False, 128, True, False, "label", "en-US") with pytest.raises(TrezorFailure): # This must fail, because device is already initialized # Using direct call because `device.reset` has its own check client.call( proto.ResetDevice( display_random=False, strength=128, passphrase_protection=True, pin_protection=False, label="label", language="en-US", ))
def test_failed_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="en-US", 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("1234") 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("6789") ret = client.call_raw(proto.PinMatrixAck(pin=pin_encoded)) assert isinstance(ret, proto.Failure)
def test_failed_pin(client: Client): # external_entropy = b'zlutoucky kun upel divoke ody' * 2 strength = 128 ret = client.call_raw( messages.ResetDevice(strength=strength, pin_protection=True, label="test")) # Confirm Reset assert isinstance(ret, messages.ButtonRequest) client.debug.press_yes() ret = client.call_raw(messages.ButtonAck()) # Enter PIN for first time assert isinstance(ret, messages.ButtonRequest) client.debug.input("654") ret = client.call_raw(messages.ButtonAck()) # Enter PIN for second time assert isinstance(ret, messages.ButtonRequest) client.debug.input("456") ret = client.call_raw(messages.ButtonAck()) assert isinstance(ret, messages.ButtonRequest)
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_reset_device_skip_backup(self, client): ret = client.call_raw( proto.ResetDevice( display_random=False, strength=self.strength, passphrase_protection=False, pin_protection=False, language="english", label="test", skip_backup=True, )) assert isinstance(ret, proto.ButtonRequest) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) # Provide entropy assert isinstance(ret, proto.EntropyRequest) internal_entropy = client.debug.read_reset_entropy() ret = client.call_raw(proto.EntropyAck(entropy=self.external_entropy)) assert isinstance(ret, proto.Success) # Check if device is properly initialized ret = client.call_raw(proto.Initialize()) assert ret.initialized is True assert ret.needs_backup is True assert ret.unfinished_backup is False assert ret.no_backup is False # Generate mnemonic locally entropy = generate_entropy(self.strength, internal_entropy, self.external_entropy) expected_mnemonic = Mnemonic("english").to_mnemonic(entropy) # start Backup workflow ret = client.call_raw(proto.BackupDevice()) mnemonic = [] for _ in range(self.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(self.strength // 32 * 3): assert isinstance(ret, proto.ButtonRequest) mnemonic.append(client.debug.read_reset_word()) client.debug.press_yes() ret = client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.Success) mnemonic = " ".join(mnemonic) # Compare that second pass printed out the same mnemonic once again assert mnemonic == expected_mnemonic # start backup again - should fail ret = client.call_raw(proto.BackupDevice()) assert isinstance(ret, proto.Failure)
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 reset_device(client: Client, strength): # No PIN, no passphrase external_entropy = b"zlutoucky kun upel divoke ody" * 2 ret = client.call_raw( messages.ResetDevice( display_random=False, strength=strength, passphrase_protection=False, pin_protection=False, language="en-US", label="test", )) assert isinstance(ret, messages.ButtonRequest) client.debug.press_yes() ret = client.call_raw(messages.ButtonAck()) # Provide entropy assert isinstance(ret, messages.EntropyRequest) internal_entropy = client.debug.state().reset_entropy ret = client.call_raw(messages.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, messages.ButtonRequest) mnemonic.append(client.debug.read_reset_word()) client.debug.press_yes() client.call_raw(messages.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, messages.ButtonRequest) mnemonic.append(client.debug.read_reset_word()) client.debug.press_yes() resp = client.call_raw(messages.ButtonAck()) assert isinstance(resp, messages.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(messages.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 pin & passphrase-protected action, PassphraseRequest should NOT be raised resp = client.call_raw( messages.GetAddress(address_n=parse_path("m/44'/0'/0'/0/0"))) assert isinstance(resp, messages.Address)
def test_reset_device_pin(self): # PIN, passphrase, display random external_entropy = b'zlutoucky kun upel divoke ody' * 2 strength = 128 ret = self.client.call_raw( proto.ResetDevice(display_random=True, strength=strength, passphrase_protection=True, pin_protection=True, label='test')) # Enter PIN for first time assert isinstance(ret, proto.ButtonRequest) self.client.debug.input('654') ret = self.client.call_raw(proto.ButtonAck()) # Enter PIN for second time assert isinstance(ret, proto.ButtonRequest) self.client.debug.input('654') ret = self.client.call_raw(proto.ButtonAck()) # Confirm entropy 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) # Safety warning assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) # List through mnemonic pages assert isinstance(ret, proto.ButtonRequest) self.client.transport.write(proto.ButtonAck()) self.client.debug.press_yes() words = [] time.sleep(1) words.extend(self.client.debug.read_reset_word().split()) self.client.debug.swipe_down() time.sleep(1) words.extend(self.client.debug.read_reset_word().split()) self.client.debug.swipe_down() time.sleep(1) words.extend(self.client.debug.read_reset_word().split()) # Compare that device generated proper mnemonic for given entropies assert ' '.join(words) == expected_mnemonic # Confirm the mnemonic self.client.debug.press_yes() # Check mnemonic words time.sleep(1) index = self.client.debug.read_reset_word_pos() self.client.debug.input(words[index]) time.sleep(1) index = self.client.debug.read_reset_word_pos() self.client.debug.input(words[index]) ret = self.client.transport.read() # Safety warning assert isinstance(ret, proto.ButtonRequest) self.client.debug.press_yes() ret = self.client.call_raw(proto.ButtonAck()) assert isinstance(ret, proto.Success) # 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 True assert resp.passphrase_protection is True
def test_reset_device_skip_backup(self): 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', skip_backup=True)) # 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)) assert isinstance(ret, proto.Success) # Check if device is properly initialized resp = self.client.call_raw(proto.Initialize()) assert resp.initialized is True assert resp.needs_backup is True # Generate mnemonic locally entropy = generate_entropy(strength, internal_entropy, external_entropy) expected_mnemonic = Mnemonic('english').to_mnemonic(entropy) # start Backup workflow ret = self.client.call_raw(proto.BackupDevice()) 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 # start backup again - should fail ret = self.client.call_raw(proto.BackupDevice()) assert isinstance(ret, proto.Failure)