def generate(cls): """ :return: :rtype: KeyPair """ keypair = X25519DH().generate_keypair() return KeyPair(PublicKey(keypair.public.data), PrivateKey(keypair.private.data))
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)
""" This following demonstrates a Noise_XX_25519_AESGCM_SHA256 handshake and initial transport messages. """ from dissononce.processing.impl.handshakestate import HandshakeState 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())
""" from dissononce.processing.impl.handshakestate import HandshakeState from dissononce.processing.impl.symmetricstate import SymmetricState from dissononce.processing.impl.cipherstate import CipherState from dissononce.processing.handshakepatterns.interactive.NN import NNHandshakePattern from dissononce.processing.modifiers.psk import PSKPatternModifier 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,