class CircuitNode: def __init__(self, router, handshake_type=TorHandshakeType.NTOR): self._router = router self._handshake_type = handshake_type self._key_agreement = None self._crypto_state = None self._window = TorWindow() @property def router(self): return self._router @property def window(self): return self._window @property def handshake_type(self): return self._handshake_type @property def key_agreement(self): if not self._key_agreement: self._key_agreement = CircuitNode._get_key_agreement( self._router, self._handshake_type) return self._key_agreement @staticmethod def _get_key_agreement(onion_router, handshake_type): if handshake_type == TorHandshakeType.NTOR: return NtorKeyAgreement(onion_router) elif handshake_type == TorHandshakeType.TAP: return TapKeyAgreement(onion_router) else: raise NotImplementedError('Unknown key agreement') def create_onion_skin(self): return self.key_agreement.handshake def complete_handshake(self, handshake_data): shared_secret = self.key_agreement.complete_handshake(handshake_data) self._crypto_state = CryptoState(shared_secret) def encrypt_forward(self, relay_cell): self._crypto_state.encrypt_forward(relay_cell) def decrypt_backward(self, relay_cell): self._crypto_state.decrypt_backward(relay_cell)
class CircuitNode: def __init__(self, router, key_agreement_cls: Type[KeyAgreement] = NtorKeyAgreement): self._router = router self._key_agreement_cls = key_agreement_cls self._crypto_state = None self._window = TorWindow() @property def router(self): return self._router @property def window(self): return self._window @property def handshake_type(self): return self._key_agreement_cls.TYPE @cached_property def key_agreement(self): return self._key_agreement_cls(self._router) def create_onion_skin(self): return self.key_agreement.handshake def complete_handshake(self, handshake_response): shared_secret = self.key_agreement.complete_handshake( handshake_response) self._crypto_state = CryptoState(shared_secret) def encrypt_forward(self, relay_cell): self._crypto_state.encrypt_forward(relay_cell) def decrypt_backward(self, relay_cell): self._crypto_state.decrypt_backward(relay_cell)
def complete_handshake(self, handshake_data): shared_secret = self.key_agreement.complete_handshake(handshake_data) self._crypto_state = CryptoState(shared_secret)