def circuitConnected(self): """ Do the obfs2 handshake: SEED | E_PAD_KEY( UINT32(MAGIC_VALUE) | UINT32(PADLEN) | WR(PADLEN) ) """ # Generate keys for outgoing padding. self.send_padding_crypto = \ self._derive_padding_crypto(self.initiator_seed if self.we_are_initiator else self.responder_seed, self.send_pad_keytype) padding_length = random.randint(0, MAX_PADDING) seed = self.initiator_seed if self.we_are_initiator else self.responder_seed handshake_message = seed + self.send_padding_crypto.crypt(srlz.htonl(MAGIC_VALUE) + srlz.htonl(padding_length) + rand.random_bytes(padding_length)) log.debug("obfs2 handshake: %s queued %d bytes (padding_length: %d).", "initiator" if self.we_are_initiator else "responder", len(handshake_message), padding_length) self.circuit.downstream.write(handshake_message)