def _digest_check_func(self, payload, digest): digest_clone = sha1_stream_clone(self._backward_digest) sha1_stream_update(digest_clone, payload) new_digest = sha1_stream_finalize(digest_clone)[:4] if new_digest != digest: logger.debug( 'received cell digest not equal ({!r} != {!r}); payload = {!r}' .format(to_hex(new_digest), to_hex(digest), to_hex(payload))) return False sha1_stream_update(self._backward_digest, payload) return True
def parse_header(payload): try: header = struct.unpack('!BHH4sH498s', payload) fields = ['cell_num', 'is_recognized', 'stream_id', 'digest', 'relay_payload_len', 'relay_payload_raw'] return dict(zip(fields, header)) except struct.error: logger.error("Can't unpack: %r", to_hex(payload)) raise
def set_decrypted(self, cell_num, stream_id, digest, relay_payload_len, relay_payload_raw, **kwargs): relay_payload = relay_payload_raw[:relay_payload_len] padding = relay_payload_raw[relay_payload_len:] if all([b == 0 for b in padding]): padding = b'' try: cell_type = TorCommands.get_relay_by_num(cell_num) logger.debug('Deserialize %s relay cell', cell_type.__name__) inner_cell = TorCell.deserialize(cell_type, 0, relay_payload, 0) except BaseException: logger.error("Can't deserialize %i cell: %r", cell_num, to_hex(relay_payload)) raise self._set_inits(inner_cell, padding, stream_id, digest) self._encrypted = None self._checked = True