fn write(&self, packet_content: &[u8], buffer: &mut SSHBuffer) {

        // http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD
        let offset = buffer.buffer.len();
        // - Compute the length, by chacha20-stream-xoring the first 4 bytes with the last 32 bytes of the client key.

        // println!("seqnr = {}", seq);
        let block_size = 8;
        let padding_len = if packet_content.len() + 5 <= 16 {
            16 - packet_content.len() - 1
        } else {
            (block_size - ((1 + packet_content.len()) % block_size))
        };
        // println!("padding_len {:?} {:?} {:?}", packet_content.len(), padding_len, poly1305::TAGBYTES);
        let padding_len = if padding_len < 4 {
            padding_len + block_size
        } else {
            padding_len
        };

        // println!("pushing len: {:?}", packet_content.len() + padding_len + 1);
        buffer.buffer.push_u32_be((packet_content.len() + padding_len + 1) as u32);

        let mut nonce = [0; 8];
        BigEndian::write_u32(&mut nonce[4..], buffer.seqn as u32);
        let nonce = chacha20::Nonce::copy_from_slice(&nonce);

        {
            let buffer = buffer.buffer.as_mut_slice();
            chacha20::stream_xor_inplace(&mut buffer[offset..(offset + 4)], &nonce, &self.k1);
        }
        // the first 4 bytes of buffer now contain the encrypted length.
        // - Append the encrypted packet

        // Compute the amount of padding.
        // println!("padding_len {:?}", padding_len);
        buffer.buffer.push(padding_len as u8);

        buffer.buffer.extend(packet_content);

        // println!("buffer before padding: {:?}", &(buffer.as_slice())[offset..]);

        let mut padding = [0; 256];

        randombytes::into(&mut padding[0..padding_len]);
        buffer.buffer.extend(&padding[0..padding_len]);

        // println!("buffer before encryption: {:?}", &(buffer.as_slice())[offset..]);

        {
            let buffer = buffer.buffer.as_mut_slice();
            chacha20::xor_inplace(&mut buffer[offset + 4..], &nonce, 1, &self.k2);
        }
        // println!("buffer before tag: {:?}", &(buffer.as_slice())[offset..]);
        // - Compute the Poly1305 auth on the first (4+length) first bytes of the packet.

        let mut poly_key = [0; poly1305::KEYBYTES];
        chacha20::stream_xor_inplace(&mut poly_key, &nonce, &self.k2);
        let poly_key = poly1305::Key::copy_from_slice(&poly_key);

        // println!("key: {:?}", poly_key);
        let mut tag = poly1305::Tag::new_blank();
        {
            let buffer = buffer.buffer.as_slice();
            poly1305::authenticate(&mut tag, &buffer[offset..], &poly_key);
        }
        // println!("== Final buffer(len = {:?}): {:?}", buffer.len()-offset, &(buffer.as_slice())[offset..]);
        // try!(stream.write(&buffer));
        // try!(stream.write(tag.as_bytes()));

        buffer.buffer.extend(tag.as_bytes());
        buffer.seqn += 1;
    }
/// Append an encrypted packet with contents `packet_content` at the end of `buffer`.
Append an encrypted packet with contents `packet_content` at the end of `buffer`.
Append an encrypted packet with contents ` packet_content ` at the end of ` buffer ` .
fn write(&self, packet_content: &[u8], buffer: &mut SSHBuffer) {
        // http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD
        let offset = buffer.buffer.len();
        // - Compute the length, by chacha20-stream-xoring the first 4 bytes with the last 32 bytes of the client key.
        // println!("seqnr = {}", seq);
        let block_size = 8;
        let padding_len = if packet_content.len() + 5 <= 16 {
            16 - packet_content.len() - 1
        } else {
            (block_size - ((1 + packet_content.len()) % block_size))
        };
        // println!("padding_len {:?} {:?} {:?}", packet_content.len(), padding_len, poly1305::TAGBYTES);
        let padding_len = if padding_len < 4 {
            padding_len + block_size
        } else {
            padding_len
        };
        // println!("pushing len: {:?}", packet_content.len() + padding_len + 1);
        buffer.buffer.push_u32_be((packet_content.len() + padding_len + 1) as u32);
        let mut nonce = [0; 8];
        BigEndian::write_u32(&mut nonce[4..], buffer.seqn as u32);
        let nonce = chacha20::Nonce::copy_from_slice(&nonce);
        {
            let buffer = buffer.buffer.as_mut_slice();
            chacha20::stream_xor_inplace(&mut buffer[offset..(offset + 4)], &nonce, &self.k1);
        }
        // the first 4 bytes of buffer now contain the encrypted length.
        // - Append the encrypted packet
        // Compute the amount of padding.
        // println!("padding_len {:?}", padding_len);
        buffer.buffer.push(padding_len as u8);
        buffer.buffer.extend(packet_content);
        // println!("buffer before padding: {:?}", &(buffer.as_slice())[offset..]);
        let mut padding = [0; 256];
        randombytes::into(&mut padding[0..padding_len]);
        buffer.buffer.extend(&padding[0..padding_len]);
        // println!("buffer before encryption: {:?}", &(buffer.as_slice())[offset..]);
        {
            let buffer = buffer.buffer.as_mut_slice();
            chacha20::xor_inplace(&mut buffer[offset + 4..], &nonce, 1, &self.k2);
        }
        // println!("buffer before tag: {:?}", &(buffer.as_slice())[offset..]);
        // - Compute the Poly1305 auth on the first (4+length) first bytes of the packet.
        let mut poly_key = [0; poly1305::KEYBYTES];
        chacha20::stream_xor_inplace(&mut poly_key, &nonce, &self.k2);
        let poly_key = poly1305::Key::copy_from_slice(&poly_key);
        // println!("key: {:?}", poly_key);
        let mut tag = poly1305::Tag::new_blank();
        {
            let buffer = buffer.buffer.as_slice();
            poly1305::authenticate(&mut tag, &buffer[offset..], &poly_key);
        }
        // println!("== Final buffer(len = {:?}): {:?}", buffer.len()-offset, &(buffer.as_slice())[offset..]);
        // try!(stream.write(&buffer));
        // try!(stream.write(tag.as_bytes()));
        buffer.buffer.extend(tag.as_bytes());
        buffer.seqn += 1;
    }
fn write ( & self , packet_content : & [ u8 ] , buffer : & mut SSHBuffer ) { // http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD let offset = buffer . buffer . len ( ) ; // - Compute the length, by chacha20-stream-xoring the first 4 bytes with the last 32 bytes of the client key. // println!("seqnr = {}", seq); let block_size = 8 ; let padding_len = if packet_content . len ( ) + 5 <= 16 { 16 - packet_content . len ( ) - 1 } else { ( block_size - ( ( 1 + packet_content . len ( ) ) % block_size ) ) } ; // println!("padding_len {:?} {:?} {:?}", packet_content.len(), padding_len, poly1305::TAGBYTES); let padding_len = if padding_len < 4 { padding_len + block_size } else { padding_len } ; // println!("pushing len: {:?}", packet_content.len() + padding_len + 1); buffer . buffer . push_u32_be ( ( packet_content . len ( ) + padding_len + 1 ) as u32 ) ; let mut nonce = [ 0 ; 8 ] ; BigEndian :: write_u32 ( & mut nonce [ 4 .. ] , buffer . seqn as u32 ) ; let nonce = chacha20 :: Nonce :: copy_from_slice ( & nonce ) ; { let buffer = buffer . buffer . as_mut_slice ( ) ; chacha20 :: stream_xor_inplace ( & mut buffer [ offset .. ( offset + 4 ) ] , & nonce , & self . k1 ) ; } // the first 4 bytes of buffer now contain the encrypted length. // - Append the encrypted packet // Compute the amount of padding. // println!("padding_len {:?}", padding_len); buffer . buffer . push ( padding_len as u8 ) ; buffer . buffer . extend ( packet_content ) ; // println!("buffer before padding: {:?}", &(buffer.as_slice())[offset..]); let mut padding = [ 0 ; 256 ] ; randombytes :: into ( & mut padding [ 0 .. padding_len ] ) ; buffer . buffer . extend ( & padding [ 0 .. padding_len ] ) ; // println!("buffer before encryption: {:?}", &(buffer.as_slice())[offset..]); { let buffer = buffer . buffer . as_mut_slice ( ) ; chacha20 :: xor_inplace ( & mut buffer [ offset + 4 .. ] , & nonce , 1 , & self . k2 ) ; } // println!("buffer before tag: {:?}", &(buffer.as_slice())[offset..]); // - Compute the Poly1305 auth on the first (4+length) first bytes of the packet. let mut poly_key = [ 0 ; poly1305 :: KEYBYTES ] ; chacha20 :: stream_xor_inplace ( & mut poly_key , & nonce , & self . k2 ) ; let poly_key = poly1305 :: Key :: copy_from_slice ( & poly_key ) ; // println!("key: {:?}", poly_key); let mut tag = poly1305 :: Tag :: new_blank ( ) ; { let buffer = buffer . buffer . as_slice ( ) ; poly1305 :: authenticate ( & mut tag , & buffer [ offset .. ] , & poly_key ) ; } // println!("== Final buffer(len = {:?}): {:?}", buffer.len()-offset, &(buffer.as_slice())[offset..]); // try!(stream.write(&buffer)); // try!(stream.write(tag.as_bytes())); buffer . buffer . extend ( tag . as_bytes ( ) ) ; buffer . seqn += 1 ; }
Append an encrypted packet with contents `packet_content` at the end of `buffer`.
Append an encrypted packet with contents ` packet_content ` at the end of ` buffer ` .
&self
packet_content
&[u8]
buffer
&mut SSHBuffer
&self
packet_content
&[u8]
buffer
&mut SSHBuffer