def create_handshakestate(self, symmetricstate=None, dh=None): """ :param symmetricstate: :type symmetricstate: SymmetricState :param dh: :type dh: DH :return: :rtype: HandshakeState """ return HandshakeState(symmetricstate or self.create_symmetricstate(), dh or self._dh)
def perform(self, client_config, stream, s, rs=None, e=None): """ :param client_config: :type client_config: :param stream: :type stream: :param s: :type s: consonance.structs.keypair.KeyPair :param rs: :type rs: consonance.structs.publickey.PublicKey | None :type e: consonance.structs.keypair.KeyPair | None :return: :rtype: """ logger.debug( "perform(client_config=%s, stream=%s, s=%s, rs=%s, e=%s)" % (client_config, stream, s, rs, e)) dh = X25519DH() if e is not None: dh = NoGenDH(dh, PrivateKey(e.private.data)) self._handshakestate = SwitchableHandshakeState( GuardedHandshakeState( HandshakeState( WASymmetricState(CipherState(AESGCMCipher()), SHA256Hash()), dh))) # type: SwitchableHandshakeState dissononce_s = KeyPair(PublicKey(s.public.data), PrivateKey(s.private.data)) dissononce_rs = PublicKey(rs.data) if rs else None client_payload = self._create_full_payload(client_config) logger.debug("Create client_payload=%s" % client_payload) try: if rs is not None: try: cipherstatepair = self._start_handshake_ik( stream, client_payload, dissononce_s, dissononce_rs) except NewRemoteStaticException as ex: cipherstatepair = self._switch_handshake_xxfallback( stream, dissononce_s, client_payload, ex.server_hello) else: cipherstatepair = self._start_handshake_xx( stream, client_payload, dissononce_s) return cipherstatepair except DecryptFailedException as e: logger.exception(e) raise HandshakeFailedException(e) except DecodeError as e: logger.exception(e) raise HandshakeFailedException(e)
from dissononce.processing.impl.symmetricstate import SymmetricState from dissononce.processing.impl.cipherstate import CipherState from dissononce.processing.handshakepatterns.interactive.XX import XXHandshakePattern from dissononce.cipher.aesgcm import AESGCMCipher from dissononce.dh.x25519.x25519 import X25519DH from dissononce.hash.sha256 import SHA256Hash import dissononce, logging if __name__ == "__main__": dissononce.logger.setLevel(logging.DEBUG) # setup initiator and responder variables alice_s = X25519DH().generate_keypair() bob_s = X25519DH().generate_keypair() # prepare handshakestate objects for initiator and responder alice_handshakestate = HandshakeState( SymmetricState(CipherState(AESGCMCipher()), SHA256Hash()), X25519DH()) bob_handshakestate = HandshakeState( SymmetricState(CipherState(AESGCMCipher()), SHA256Hash()), X25519DH()) # initialize handshakestate objects alice_handshakestate.initialize(XXHandshakePattern(), True, b'', s=alice_s) bob_handshakestate.initialize(XXHandshakePattern(), False, b'', s=bob_s) # -> e message_buffer = bytearray() alice_handshakestate.write_message(b'', message_buffer) bob_handshakestate.read_message(bytes(message_buffer), bytearray()) # <- e, ee, s, es message_buffer = bytearray() bob_handshakestate.write_message(b'', message_buffer) alice_handshakestate.read_message(bytes(message_buffer), bytearray())
from dissononce.cipher.chachapoly import ChaChaPolyCipher from dissononce.dh.x25519.x25519 import X25519DH from dissononce.hash.blake2s import Blake2sHash import dissononce, logging import os if __name__ == "__main__": dissononce.logger.setLevel(logging.DEBUG) # setup initiator and responder variables alice_s = X25519DH().generate_keypair() bob_s = X25519DH().generate_keypair() psks = (os.urandom(32), os.urandom(32)) # prepare handshakestate objects for initiator and responder alice_handshakestate = HandshakeState( SymmetricState(CipherState(ChaChaPolyCipher()), Blake2sHash()), X25519DH()) bob_handshakestate = HandshakeState( SymmetricState(CipherState(ChaChaPolyCipher()), Blake2sHash()), X25519DH()) # modify NNHandshakePattern nn_psk0_pattern = PSKPatternModifier(0).modify(NNHandshakePattern()) nn_psk0_psk2_pattern = PSKPatternModifier(2).modify(nn_psk0_pattern) # initialize handshakestate objects alice_handshakestate.initialize(nn_psk0_psk2_pattern, True, b'prologue', s=alice_s, psks=psks) bob_handshakestate.initialize(nn_psk0_psk2_pattern, False,