示例#1
0
    def test_state_dump_to_bytes(self):
        """Tests dumping State to bytes."""
        data = bytearray(self.data)
        state_ = state.State.load(data, self.offset)
        steps.add_round_key(state_, self.key.schedule, current_round=2)

        state_.dump(data, self.offset)

        self.assertNotEqual(bytearray(self.data), data)
示例#2
0
    def test_dump(self):
        """Tests dumping state to file."""
        key = Key.load(self.key)

        for test in range(self.tests):
            offset = test * self.size
            state = State.load(self.file, offset)
            steps.add_round_key(state, key.schedule, current_round=2)

            with self.subTest(offset=offset):
                state.dump(self.file, offset)

                self.assertNotEqual(state, State.load(self.file, offset))
示例#3
0
    def test_add_round_key(self):
        """Tests adding current_round keys."""
        previous = copy.deepcopy(self.state)
        current_round = random.randrange(constants.ROUNDS[self.key.size])

        steps.add_round_key(self.state,
                            self.key.schedule,
                            current_round=current_round)

        for row_id, row in enumerate(previous):
            for col_id, elem in enumerate(row):
                key_schedule_col_id = 4 * current_round + col_id

                with self.subTest(ids=(row_id, col_id)):
                    self.assertEqual(
                        elem ^ self.key.schedule[row_id][key_schedule_col_id],
                        self.state[row_id][col_id])
示例#4
0
    def test_state_dump_to_file(self):
        """Tests dumping State to files."""
        file_mock = mock.MagicMock(spec=pathlib.Path)
        open_mock = mock.mock_open(file_mock, self.data)
        file_handler_mock = open_mock.return_value

        with mock.patch('builtins.open', open_mock):
            state_ = state.State.load(file_mock, self.offset)
            steps.add_round_key(state_, self.key.schedule, current_round=2)

            state_.dump(file_mock, self.offset)

        self.assertEqual(2, open_mock.call_count)
        open_mock.assert_any_call(file_mock, 'rb')
        open_mock.assert_any_call(file_mock, 'rb+')
        self.assertEqual(2, file_handler_mock.seek.call_count)
        file_handler_mock.seek.assert_any_call(self.offset)
        file_handler_mock.read.assert_called_once_with(self.size)
        self.assertEqual(4, file_handler_mock.write.call_count)
        file_handler_mock.write.assert_any_call(bytes(c[0] for c in state_))
        file_handler_mock.write.assert_any_call(bytes(c[1] for c in state_))
        file_handler_mock.write.assert_any_call(bytes(c[2] for c in state_))
        file_handler_mock.write.assert_any_call(bytes(c[3] for c in state_))
示例#5
0
def decrypt(state, key):
    """Decrypts state."""
    rounds = constants.ROUNDS[key.size]
    steps.add_round_key(state, key.schedule, current_round=rounds)

    for current_round in range(rounds - 1, 0, -1):
        steps.shift_rows(state, inverse=True)
        steps.sub_bytes(state, inverse=True)
        steps.add_round_key(state, key.schedule, current_round=current_round)
        steps.mix_columns(state, inverse=True)

    steps.shift_rows(state, inverse=True)
    steps.sub_bytes(state, inverse=True)
    steps.add_round_key(state, key.schedule, current_round=0)
示例#6
0
def encrypt(state, key):
    """Encrypts state."""
    rounds = constants.ROUNDS[key.size]

    steps.add_round_key(state, key.schedule, current_round=0)

    for current_round in range(1, rounds):
        steps.sub_bytes(state)
        steps.shift_rows(state)
        steps.mix_columns(state)
        steps.add_round_key(state, key.schedule, current_round=current_round)

    steps.sub_bytes(state)
    steps.shift_rows(state)
    steps.add_round_key(state, key.schedule, current_round=rounds)