示例#1
0
    def _update(self, assoc_data_pt=b""):
        """Update the MAC with associated data or plaintext
           (without FSM checks)"""

        # If MAC has not started yet, we just park the data into a list.
        # If the data is mutable, we create a copy and store that instead.
        if self._mac_status == MacStatus.NOT_STARTED:
            if _is_mutable(assoc_data_pt):
                assoc_data_pt = _copy_bytes(None, None, assoc_data_pt)
            self._cache.append(assoc_data_pt)
            return

        assert(len(self._cache) < self.block_size)

        if len(self._cache) > 0:
            filler = min(self.block_size - len(self._cache),
                         len(assoc_data_pt))
            self._cache += _copy_bytes(None, filler, assoc_data_pt)
            assoc_data_pt = _copy_bytes(filler, None, assoc_data_pt)

            if len(self._cache) < self.block_size:
                return

            # The cache is exactly one block
            self._t = self._mac.encrypt(self._cache)
            self._cache = b""

        update_len = len(assoc_data_pt) // self.block_size * self.block_size
        self._cache = _copy_bytes(update_len, None, assoc_data_pt)
        if update_len > 0:
            self._t = self._mac.encrypt(assoc_data_pt[:update_len])[-16:]
示例#2
0
    def _update(self, assoc_data_pt=b""):
        """Update the MAC with associated data or plaintext
           (without FSM checks)"""

        # If MAC has not started yet, we just park the data into a list.
        # If the data is mutable, we create a copy and store that instead.
        if self._mac_status == MacStatus.NOT_STARTED:
            if _is_mutable(assoc_data_pt):
                assoc_data_pt = _copy_bytes(None, None, assoc_data_pt)
            self._cache.append(assoc_data_pt)
            return

        assert (len(self._cache) < self.block_size)

        if len(self._cache) > 0:
            filler = min(self.block_size - len(self._cache),
                         len(assoc_data_pt))
            self._cache += _copy_bytes(None, filler, assoc_data_pt)
            assoc_data_pt = _copy_bytes(filler, None, assoc_data_pt)

            if len(self._cache) < self.block_size:
                return

            # The cache is exactly one block
            self._t = self._mac.encrypt(self._cache)
            self._cache = b""

        update_len = len(assoc_data_pt) // self.block_size * self.block_size
        self._cache = _copy_bytes(update_len, None, assoc_data_pt)
        if update_len > 0:
            self._t = self._mac.encrypt(assoc_data_pt[:update_len])[-16:]