Example #1
0
    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'))

        # Provide entropy
        self.assertIsInstance(ret, proto.EntropyRequest)
        internal_entropy = self.client.debug.read_reset_entropy()
        resp = 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)

        # Explainer Dialog
        self.assertIsInstance(resp, proto.ButtonRequest)
        self.client.debug.press_yes()
        resp = self.client.call_raw(proto.ButtonAck())

        mnemonic = []
        while isinstance(resp, proto.ButtonRequest):
            mnemonic.append(self.client.debug.read_reset_word())
            self.client.debug.press_yes()
            resp = self.client.call_raw(proto.ButtonAck())

        mnemonic = ' '.join(mnemonic)

        # Compare that device generated proper mnemonic for given entropies
        self.assertEqual(mnemonic, expected_mnemonic)

        self.assertIsInstance(resp, proto.Success)

        # Compare that second pass printed out the same mnemonic once again
        self.assertEqual(mnemonic, expected_mnemonic)

        # Check if device is properly initialized
        resp = self.client.call_raw(proto.Initialize())
        self.assertFalse(resp.pin_protection)
        self.assertFalse(resp.passphrase_protection)

        # Do passphrase-protected action, PassphraseRequest should NOT be raised
        resp = self.client.call_raw(proto.Ping(passphrase_protection=True))
        self.assertIsInstance(resp, proto.Success)

        # Do PIN-protected action, PinRequest should NOT be raised
        resp = self.client.call_raw(proto.Ping(pin_protection=True))
        self.assertIsInstance(resp, proto.Success)
Example #2
0
    def test_reset_and_recover(self):
        for strength in (128, 192, 256):
            external_entropy = self.client._get_local_entropy()

            ret = self.client.call_raw(proto.ResetDevice(display_random=False,
                                                strength=strength,
                                                passphrase_protection=False,
                                                pin_protection=False,
                                                language='english',
                                                label='test'))

            # Provide entropy
            self.assertIsInstance(ret, proto.EntropyRequest)
            resp = self.client.call_raw(proto.EntropyAck(entropy=external_entropy))

            mnemonic = []
            while isinstance(resp, proto.ButtonRequest):
                mnemonic.append(self.client.debug.read_reset_word())
                self.client.debug.press_yes()
                resp = self.client.call_raw(proto.ButtonAck())

            mnemonic = ' '.join(mnemonic)
            
            # wipe device
            ret = self.client.call_raw(proto.WipeDevice())
            self.client.debug.press_yes()
            ret = self.client.call_raw(proto.ButtonAck())

            # recover devce
            ret = self.client.call_raw(proto.RecoveryDevice(word_count=(strength/32*3),
                                   passphrase_protection=False,
                                   pin_protection=False,
                                   label='label',
                                   language='english',
                                   enforce_wordlist=True,
                                   use_character_cipher=True))

            mnemonic_words = mnemonic.split(' ')

            for index, word in enumerate(mnemonic_words):
                for character in word:
                    self.assertIsInstance(ret, proto.CharacterRequest)
                    cipher = self.client.debug.read_recovery_cipher()

                    encoded_character = cipher[ord(character) - 97]
                    ret = self.client.call_raw(proto.CharacterAck(character=encoded_character))
                
                    auto_completed = self.client.debug.read_recovery_auto_completed_word()       

                    if word == auto_completed:
                        if len(mnemonic_words) != index + 1:
                            ret = self.client.call_raw(proto.CharacterAck(character=' '))
                        break

            # Send final ack
            self.assertIsInstance(ret, proto.CharacterRequest)
            ret = self.client.call_raw(proto.CharacterAck(done=True))

            # Workflow succesfully ended
            self.assertIsInstance(ret, proto.Success)
           
            self.client.init_device()
            self.assertEqual(self.client.debug.read_mnemonic(), mnemonic) 
            
            # wipe device
            ret = self.client.call_raw(proto.WipeDevice())
            self.client.debug.press_yes()
            ret = self.client.call_raw(proto.ButtonAck())