pub async fn do_handshake(&mut self, identity: &Identity) -> Result<(), PsecError> {
        let mut handshake_sent_buff = Vec::new();
        let mut handshake_recv_buff = Vec::new();

        //ECDHE initial exchange
        //generate random bytes
        let mut handshake_buffer = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH];
        OsRng.fill_bytes(&mut handshake_buffer[..RANDOM_LEN]);
        //generate ephemeral x25519 keys
        let ephemeral_secret = x25519_dalek::EphemeralSecret::new(OsRng);
        let ephemeral_public_key = x25519_dalek::PublicKey::from(&ephemeral_secret);
        handshake_buffer[RANDOM_LEN..].copy_from_slice(&ephemeral_public_key.to_bytes());
        //exchange public keys
        self.handshake_write(&handshake_buffer, &mut handshake_sent_buff).await?;
        self.handshake_read(&mut handshake_buffer, &mut handshake_recv_buff).await?;
        let peer_ephemeral_public_key = slice_to_public_key(&handshake_buffer[RANDOM_LEN..]);
        //compute handshake keys
        let i_am_bob = handshake_sent_buff < handshake_recv_buff; //mutual consensus for keys attribution
        let handshake_hash = Session::hash_handshake(i_am_bob, &handshake_sent_buff, &handshake_recv_buff);
        let shared_secret = ephemeral_secret.diffie_hellman(&peer_ephemeral_public_key);
        let handshake_keys = HandshakeKeys::derive_keys(shared_secret.to_bytes(), handshake_hash, i_am_bob);

        //authentication
        //create auth_msg: random bytes, public key & ephemeral public key signature
        let mut auth_msg = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH+SIGNATURE_LENGTH];
        OsRng.fill_bytes(&mut auth_msg[..RANDOM_LEN]);
        auth_msg[RANDOM_LEN..RANDOM_LEN+PUBLIC_KEY_LENGTH].copy_from_slice(&identity.public.to_bytes());
        auth_msg[RANDOM_LEN+PUBLIC_KEY_LENGTH..].copy_from_slice(&identity.sign(ephemeral_public_key.as_bytes()).to_bytes());
        //encrypt auth_msg
        let local_cipher = Aes128Gcm::new_from_slice(&handshake_keys.local_key).unwrap();
        let encrypted_auth_msg = local_cipher.encrypt(Nonce::from_slice(&handshake_keys.local_iv), auth_msg.as_ref()).unwrap();
        self.handshake_write(&encrypted_auth_msg, &mut handshake_sent_buff).await?;

        let mut encrypted_peer_auth_msg = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH+SIGNATURE_LENGTH+AES_TAG_LEN];
        self.handshake_read(&mut encrypted_peer_auth_msg, &mut handshake_recv_buff).await?;
        //decrypt peer_auth_msg
        let peer_cipher = Aes128Gcm::new_from_slice(&handshake_keys.peer_key).unwrap();
        match peer_cipher.decrypt(Nonce::from_slice(&handshake_keys.peer_iv), encrypted_peer_auth_msg.as_ref()) {
            Ok(peer_auth_msg) => {
                //verify ephemeral public key signature
                self.peer_public_key = Some(peer_auth_msg[RANDOM_LEN..RANDOM_LEN+PUBLIC_KEY_LENGTH].try_into().unwrap());
                let peer_public_key = ed25519_dalek::PublicKey::from_bytes(&self.peer_public_key.unwrap()).unwrap();
                let peer_signature = Signature::from_bytes(&peer_auth_msg[RANDOM_LEN+PUBLIC_KEY_LENGTH..]).unwrap();
                if peer_public_key.verify(peer_ephemeral_public_key.as_bytes(), &peer_signature).is_ok() {
                    //compute handshake finished
                    let handshake_hash = Session::hash_handshake(i_am_bob, &handshake_sent_buff, &handshake_recv_buff);
                    let handshake_finished = crypto::compute_handshake_finished(handshake_keys.local_handshake_traffic_secret, handshake_hash);
                    self.send(&handshake_finished).await?;
                    //verify handshake finished
                    let mut peer_handshake_finished = [0; crypto::HASH_OUTPUT_LEN];
                    self.receive(&mut peer_handshake_finished).await?;
                    if crypto::verify_handshake_finished(peer_handshake_finished, handshake_keys.peer_handshake_traffic_secret, handshake_hash) {
                        //compute application keys
                        let application_keys = ApplicationKeys::derive_keys(handshake_keys.handshake_secret, handshake_hash, i_am_bob);
                        self.init_ciphers(application_keys);
                        return Ok(());
                    }
                }
            }
            Err(_) => {}
        }
        Err(PsecError::TransmissionCorrupted)
    }
/** Performing a PSEC handshake.
    
    If successful, the `Session` is ready to send and receive data and you can retrieve the peer public key with the [`peer_public_key`](Session::peer_public_key) attribute. Otherwise, trying to encrypt or decrypt data with this session will panic.*/
Performing a PSEC handshake.
If successful, the `Session` is ready to send and receive data and you can retrieve the peer public key with the [`peer_public_key`](Session::peer_public_key) attribute. Otherwise, trying to encrypt or decrypt data with this session will panic.
Performing a PSEC handshake . If successful the ` Session ` is ready to send and receive data and you can retrieve the peer public key with the [ ` peer_public_key ` ] ( Session :: peer_public_key ) attribute . Otherwise trying to encrypt or decrypt data with this session will panic .
pub async fn do_handshake(&mut self, identity: &Identity) -> Result<(), PsecError> {
        let mut handshake_sent_buff = Vec::new();
        let mut handshake_recv_buff = Vec::new();
        //ECDHE initial exchange
        //generate random bytes
        let mut handshake_buffer = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH];
        OsRng.fill_bytes(&mut handshake_buffer[..RANDOM_LEN]);
        //generate ephemeral x25519 keys
        let ephemeral_secret = x25519_dalek::EphemeralSecret::new(OsRng);
        let ephemeral_public_key = x25519_dalek::PublicKey::from(&ephemeral_secret);
        handshake_buffer[RANDOM_LEN..].copy_from_slice(&ephemeral_public_key.to_bytes());
        //exchange public keys
        self.handshake_write(&handshake_buffer, &mut handshake_sent_buff).await?;
        self.handshake_read(&mut handshake_buffer, &mut handshake_recv_buff).await?;
        let peer_ephemeral_public_key = slice_to_public_key(&handshake_buffer[RANDOM_LEN..]);
        //compute handshake keys
        let i_am_bob = handshake_sent_buff < handshake_recv_buff; //mutual consensus for keys attribution
        let handshake_hash = Session::hash_handshake(i_am_bob, &handshake_sent_buff, &handshake_recv_buff);
        let shared_secret = ephemeral_secret.diffie_hellman(&peer_ephemeral_public_key);
        let handshake_keys = HandshakeKeys::derive_keys(shared_secret.to_bytes(), handshake_hash, i_am_bob);
        //authentication
        //create auth_msg: random bytes, public key & ephemeral public key signature
        let mut auth_msg = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH+SIGNATURE_LENGTH];
        OsRng.fill_bytes(&mut auth_msg[..RANDOM_LEN]);
        auth_msg[RANDOM_LEN..RANDOM_LEN+PUBLIC_KEY_LENGTH].copy_from_slice(&identity.public.to_bytes());
        auth_msg[RANDOM_LEN+PUBLIC_KEY_LENGTH..].copy_from_slice(&identity.sign(ephemeral_public_key.as_bytes()).to_bytes());
        //encrypt auth_msg
        let local_cipher = Aes128Gcm::new_from_slice(&handshake_keys.local_key).unwrap();
        let encrypted_auth_msg = local_cipher.encrypt(Nonce::from_slice(&handshake_keys.local_iv), auth_msg.as_ref()).unwrap();
        self.handshake_write(&encrypted_auth_msg, &mut handshake_sent_buff).await?;
        let mut encrypted_peer_auth_msg = [0; RANDOM_LEN+PUBLIC_KEY_LENGTH+SIGNATURE_LENGTH+AES_TAG_LEN];
        self.handshake_read(&mut encrypted_peer_auth_msg, &mut handshake_recv_buff).await?;
        //decrypt peer_auth_msg
        let peer_cipher = Aes128Gcm::new_from_slice(&handshake_keys.peer_key).unwrap();
        match peer_cipher.decrypt(Nonce::from_slice(&handshake_keys.peer_iv), encrypted_peer_auth_msg.as_ref()) {
            Ok(peer_auth_msg) => {
                //verify ephemeral public key signature
                self.peer_public_key = Some(peer_auth_msg[RANDOM_LEN..RANDOM_LEN+PUBLIC_KEY_LENGTH].try_into().unwrap());
                let peer_public_key = ed25519_dalek::PublicKey::from_bytes(&self.peer_public_key.unwrap()).unwrap();
                let peer_signature = Signature::from_bytes(&peer_auth_msg[RANDOM_LEN+PUBLIC_KEY_LENGTH..]).unwrap();
                if peer_public_key.verify(peer_ephemeral_public_key.as_bytes(), &peer_signature).is_ok() {
                    //compute handshake finished
                    let handshake_hash = Session::hash_handshake(i_am_bob, &handshake_sent_buff, &handshake_recv_buff);
                    let handshake_finished = crypto::compute_handshake_finished(handshake_keys.local_handshake_traffic_secret, handshake_hash);
                    self.send(&handshake_finished).await?;
                    //verify handshake finished
                    let mut peer_handshake_finished = [0; crypto::HASH_OUTPUT_LEN];
                    self.receive(&mut peer_handshake_finished).await?;
                    if crypto::verify_handshake_finished(peer_handshake_finished, handshake_keys.peer_handshake_traffic_secret, handshake_hash) {
                        //compute application keys
                        let application_keys = ApplicationKeys::derive_keys(handshake_keys.handshake_secret, handshake_hash, i_am_bob);
                        self.init_ciphers(application_keys);
                        return Ok(());
                    }
                }
            }
            Err(_) => {}
        }
        Err(PsecError::TransmissionCorrupted)
    }
pub async fn do_handshake ( & mut self , identity : & Identity ) -> Result < ( ) , PsecError > { let mut handshake_sent_buff = Vec :: new ( ) ; let mut handshake_recv_buff = Vec :: new ( ) ; //ECDHE initial exchange //generate random bytes let mut handshake_buffer = [ 0 ; RANDOM_LEN + PUBLIC_KEY_LENGTH ] ; OsRng . fill_bytes ( & mut handshake_buffer [ .. RANDOM_LEN ] ) ; //generate ephemeral x25519 keys let ephemeral_secret = x25519_dalek :: EphemeralSecret :: new ( OsRng ) ; let ephemeral_public_key = x25519_dalek :: PublicKey :: from ( & ephemeral_secret ) ; handshake_buffer [ RANDOM_LEN .. ] . copy_from_slice ( & ephemeral_public_key . to_bytes ( ) ) ; //exchange public keys self . handshake_write ( & handshake_buffer , & mut handshake_sent_buff ) . await ? ; self . handshake_read ( & mut handshake_buffer , & mut handshake_recv_buff ) . await ? ; let peer_ephemeral_public_key = slice_to_public_key ( & handshake_buffer [ RANDOM_LEN .. ] ) ; //compute handshake keys let i_am_bob = handshake_sent_buff < handshake_recv_buff ; //mutual consensus for keys attribution let handshake_hash = Session :: hash_handshake ( i_am_bob , & handshake_sent_buff , & handshake_recv_buff ) ; let shared_secret = ephemeral_secret . diffie_hellman ( & peer_ephemeral_public_key ) ; let handshake_keys = HandshakeKeys :: derive_keys ( shared_secret . to_bytes ( ) , handshake_hash , i_am_bob ) ; //authentication //create auth_msg: random bytes, public key & ephemeral public key signature let mut auth_msg = [ 0 ; RANDOM_LEN + PUBLIC_KEY_LENGTH + SIGNATURE_LENGTH ] ; OsRng . fill_bytes ( & mut auth_msg [ .. RANDOM_LEN ] ) ; auth_msg [ RANDOM_LEN .. RANDOM_LEN + PUBLIC_KEY_LENGTH ] . copy_from_slice ( & identity . public . to_bytes ( ) ) ; auth_msg [ RANDOM_LEN + PUBLIC_KEY_LENGTH .. ] . copy_from_slice ( & identity . sign ( ephemeral_public_key . as_bytes ( ) ) . to_bytes ( ) ) ; //encrypt auth_msg let local_cipher = Aes128Gcm :: new_from_slice ( & handshake_keys . local_key ) . unwrap ( ) ; let encrypted_auth_msg = local_cipher . encrypt ( Nonce :: from_slice ( & handshake_keys . local_iv ) , auth_msg . as_ref ( ) ) . unwrap ( ) ; self . handshake_write ( & encrypted_auth_msg , & mut handshake_sent_buff ) . await ? ; let mut encrypted_peer_auth_msg = [ 0 ; RANDOM_LEN + PUBLIC_KEY_LENGTH + SIGNATURE_LENGTH + AES_TAG_LEN ] ; self . handshake_read ( & mut encrypted_peer_auth_msg , & mut handshake_recv_buff ) . await ? ; //decrypt peer_auth_msg let peer_cipher = Aes128Gcm :: new_from_slice ( & handshake_keys . peer_key ) . unwrap ( ) ; match peer_cipher . decrypt ( Nonce :: from_slice ( & handshake_keys . peer_iv ) , encrypted_peer_auth_msg . as_ref ( ) ) { Ok ( peer_auth_msg ) => { //verify ephemeral public key signature self . peer_public_key = Some ( peer_auth_msg [ RANDOM_LEN .. RANDOM_LEN + PUBLIC_KEY_LENGTH ] . try_into ( ) . unwrap ( ) ) ; let peer_public_key = ed25519_dalek :: PublicKey :: from_bytes ( & self . peer_public_key . unwrap ( ) ) . unwrap ( ) ; let peer_signature = Signature :: from_bytes ( & peer_auth_msg [ RANDOM_LEN + PUBLIC_KEY_LENGTH .. ] ) . unwrap ( ) ; if peer_public_key . verify ( peer_ephemeral_public_key . as_bytes ( ) , & peer_signature ) . is_ok ( ) { //compute handshake finished let handshake_hash = Session :: hash_handshake ( i_am_bob , & handshake_sent_buff , & handshake_recv_buff ) ; let handshake_finished = crypto :: compute_handshake_finished ( handshake_keys . local_handshake_traffic_secret , handshake_hash ) ; self . send ( & handshake_finished ) . await ? ; //verify handshake finished let mut peer_handshake_finished = [ 0 ; crypto :: HASH_OUTPUT_LEN ] ; self . receive ( & mut peer_handshake_finished ) . await ? ; if crypto :: verify_handshake_finished ( peer_handshake_finished , handshake_keys . peer_handshake_traffic_secret , handshake_hash ) { //compute application keys let application_keys = ApplicationKeys :: derive_keys ( handshake_keys . handshake_secret , handshake_hash , i_am_bob ) ; self . init_ciphers ( application_keys ) ; return Ok ( ( ) ) ; } } } Err ( _ ) => { } } Err ( PsecError :: TransmissionCorrupted ) }
Performing a PSEC handshake.
Performing a PSEC handshake .
&mut self
identity
&Identity