pub fn encrypt_record_inplace<W5500: Registers>(
    w5500: &mut W5500,
    sn: Sn,
    client_key: &[u8; 16],
    client_nonce: &[u8; 12],
    mut head: u16,
    tail: u16,
    content_type: ContentType,
) -> Result<(), W5500::Error> {
    const CONTENT_TYPE_LEN: u16 = 1;

    // data length without tag, header, and content type
    let data_len: u16 = tail.wrapping_sub(head);

    let header: RecordHeader = RecordHeader::ser(
        ContentType::ApplicationData,
        tail.wrapping_add(CONTENT_TYPE_LEN + (GCM_TAG_LEN as u16))
            .wrapping_sub(head),
    );

    w5500.set_sn_tx_buf(
        sn,
        head.wrapping_sub(RecordHeader::LEN as u16),
        header.as_bytes(),
    )?;

    let mut cipher = Aes128Gcm::new(client_key, client_nonce, header.as_bytes());

    for _ in 0..(data_len / 16) {
        let mut block: [u8; 16] = [0; 16];

        // read plaintext
        w5500.sn_tx_buf(sn, head, &mut block)?;

        // encrypt
        cipher.encrypt_block_inplace(&mut block);

        // write ciphertext
        w5500.set_sn_tx_buf(sn, head, &block)?;

        head = head.wrapping_add(16);
    }

    let mut remain: u16 = data_len % 16;
    let mut block: [u8; 16] = [0; 16];

    // read remaining plaintext
    w5500.sn_tx_buf(sn, head, &mut block[..remain.into()])?;

    // append content type
    block[usize::from(remain)] = content_type.into();
    remain += 1;

    // encrypt
    cipher.encrypt_remainder_inplace(&mut block, remain.into());

    // write ciphertext
    w5500.set_sn_tx_buf(sn, head, &block[..remain.into()])?;

    head = head.wrapping_add(remain);

    // write tag
    let tag: [u8; GCM_TAG_LEN] = cipher.finish();
    w5500.set_sn_tx_buf(sn, head, &tag)?;

    w5500.set_sn_tx_wr(sn, head.wrapping_add(GCM_TAG_LEN as u16))?;
    w5500.set_sn_cr(sn, SocketCommand::Send)?;

    Ok(())
}
// This will also send the record.
This will also send the record.
This will also send the record .
pub fn encrypt_record_inplace<W5500: Registers>(
    w5500: &mut W5500,
    sn: Sn,
    client_key: &[u8; 16],
    client_nonce: &[u8; 12],
    mut head: u16,
    tail: u16,
    content_type: ContentType,
) -> Result<(), W5500::Error> {
    const CONTENT_TYPE_LEN: u16 = 1;
    // data length without tag, header, and content type
    let data_len: u16 = tail.wrapping_sub(head);
    let header: RecordHeader = RecordHeader::ser(
        ContentType::ApplicationData,
        tail.wrapping_add(CONTENT_TYPE_LEN + (GCM_TAG_LEN as u16))
            .wrapping_sub(head),
    );
    w5500.set_sn_tx_buf(
        sn,
        head.wrapping_sub(RecordHeader::LEN as u16),
        header.as_bytes(),
    )?;
    let mut cipher = Aes128Gcm::new(client_key, client_nonce, header.as_bytes());
    for _ in 0..(data_len / 16) {
        let mut block: [u8; 16] = [0; 16];
        // read plaintext
        w5500.sn_tx_buf(sn, head, &mut block)?;
        // encrypt
        cipher.encrypt_block_inplace(&mut block);
        // write ciphertext
        w5500.set_sn_tx_buf(sn, head, &block)?;
        head = head.wrapping_add(16);
    }
    let mut remain: u16 = data_len % 16;
    let mut block: [u8; 16] = [0; 16];
    // read remaining plaintext
    w5500.sn_tx_buf(sn, head, &mut block[..remain.into()])?;
    // append content type
    block[usize::from(remain)] = content_type.into();
    remain += 1;
    // encrypt
    cipher.encrypt_remainder_inplace(&mut block, remain.into());
    // write ciphertext
    w5500.set_sn_tx_buf(sn, head, &block[..remain.into()])?;
    head = head.wrapping_add(remain);
    // write tag
    let tag: [u8; GCM_TAG_LEN] = cipher.finish();
    w5500.set_sn_tx_buf(sn, head, &tag)?;
    w5500.set_sn_tx_wr(sn, head.wrapping_add(GCM_TAG_LEN as u16))?;
    w5500.set_sn_cr(sn, SocketCommand::Send)?;
    Ok(())
}
pub fn encrypt_record_inplace < W5500 : Registers > ( w5500 : & mut W5500 , sn : Sn , client_key : & [ u8 ; 16 ] , client_nonce : & [ u8 ; 12 ] , mut head : u16 , tail : u16 , content_type : ContentType , ) -> Result < ( ) , W5500 :: Error > { const CONTENT_TYPE_LEN : u16 = 1 ; // data length without tag, header, and content type let data_len : u16 = tail . wrapping_sub ( head ) ; let header : RecordHeader = RecordHeader :: ser ( ContentType :: ApplicationData , tail . wrapping_add ( CONTENT_TYPE_LEN + ( GCM_TAG_LEN as u16 ) ) . wrapping_sub ( head ) , ) ; w5500 . set_sn_tx_buf ( sn , head . wrapping_sub ( RecordHeader :: LEN as u16 ) , header . as_bytes ( ) , ) ? ; let mut cipher = Aes128Gcm :: new ( client_key , client_nonce , header . as_bytes ( ) ) ; for _ in 0 .. ( data_len / 16 ) { let mut block : [ u8 ; 16 ] = [ 0 ; 16 ] ; // read plaintext w5500 . sn_tx_buf ( sn , head , & mut block ) ? ; // encrypt cipher . encrypt_block_inplace ( & mut block ) ; // write ciphertext w5500 . set_sn_tx_buf ( sn , head , & block ) ? ; head = head . wrapping_add ( 16 ) ; } let mut remain : u16 = data_len % 16 ; let mut block : [ u8 ; 16 ] = [ 0 ; 16 ] ; // read remaining plaintext w5500 . sn_tx_buf ( sn , head , & mut block [ .. remain . into ( ) ] ) ? ; // append content type block [ usize :: from ( remain ) ] = content_type . into ( ) ; remain += 1 ; // encrypt cipher . encrypt_remainder_inplace ( & mut block , remain . into ( ) ) ; // write ciphertext w5500 . set_sn_tx_buf ( sn , head , & block [ .. remain . into ( ) ] ) ? ; head = head . wrapping_add ( remain ) ; // write tag let tag : [ u8 ; GCM_TAG_LEN ] = cipher . finish ( ) ; w5500 . set_sn_tx_buf ( sn , head , & tag ) ? ; w5500 . set_sn_tx_wr ( sn , head . wrapping_add ( GCM_TAG_LEN as u16 ) ) ? ; w5500 . set_sn_cr ( sn , SocketCommand :: Send ) ? ; Ok ( ( ) ) }
This will also send the record.
This will also send the record .
w5500
&mut W5500
sn
Sn
client_key
&[u8; 16]
client_nonce
&[u8; 12]
head
u16
tail
u16
content_type
ContentType
w5500
&mut W5500
sn
Sn
client_key
&[u8; 16]
client_nonce
&[u8; 12]
head
u16
tail
u16
content_type
ContentType