// pktptr points to the start of the ESP header
// after calling, output points to the start of the decrypted ip header.
// This function will return outlen: u16
pktptr points to the start of the ESP header
after calling, output points to the start of the decrypted ip header.
This function will return outlen: u16
pktptr points to the start of the ESP header after calling output points to the start of the decrypted ip header . This function will return outlen : u16
pub fn aes_cbc_sha256_decrypt_opt(pktptr: &mut [u8], compdigest: bool) -> Result<usize, CryptoError> 
{
    let pktlen = pktptr.len();    
    if pktlen < (ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH + ICV_LEN_SHA256) {
        println!("Decrypt: Packet length is not proper");
        stdout().flush().unwrap();
        return Err(CryptoError::PktlenError);
    }
    let key = PKey::hmac(SHA_KEY).unwrap();
    let mut signer = Signer::new(MessageDigest::sha256(), &key).unwrap();
    signer.update(&pktptr[..(pktlen - ICV_LEN_SHA256)]).unwrap();
    let hmac = signer.sign_to_vec().unwrap();
    if compdigest
    {
        if !memcmp::eq(&hmac[..ICV_LEN_SHA256], &pktptr[(pktlen - ICV_LEN_SHA256)..])
        {
            println!("INBOUND Mac Mismatch");
            stdout().flush().unwrap();
            return Err(CryptoError::HmacMismatch);
        }
    }
    let cipher = Cipher::aes_128_cbc();
    // let cleartext = decrypt(cipher, AES_KEY, Some(AES_IV), &pktptr[(ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH)..(pktlen - ICV_LEN_SHA256)]).unwrap();
    let cleartext = my_decrypt(cipher, AES_KEY, Some(AES_IV), &pktptr[(ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH)..(pktlen - ICV_LEN_SHA256)]).unwrap();
    let cleartext_len = cleartext.len();
    if cleartext_len != pktlen - ESP_HEADER_LENGTH - AES_CBC_IV_LENGTH - ICV_LEN_SHA256
    {
        println!("ciphertext pktlen: {} vs. cleartext pktlen: {}", pktlen - ESP_HEADER_LENGTH - AES_CBC_IV_LENGTH - ICV_LEN_SHA256, cleartext_len);
        println!("AES decryption errors");
        stdout().flush().unwrap();
        return Err(CryptoError::AESDecryptError);
    }
    pktptr[..(cleartext_len)].copy_from_slice(&cleartext[..]);
    Ok(cleartext_len + ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH)
}
pub fn aes_cbc_sha256_decrypt_opt ( pktptr : & mut [ u8 ] , compdigest : bool ) -> Result < usize , CryptoError > { let pktlen = pktptr . len ( ) ; if pktlen < ( ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH + ICV_LEN_SHA256 ) { println ! ( " " ) ; stdout ( ) . flush ( ) . unwrap ( ) ; return Err ( CryptoError :: PktlenError ) ; } let key = PKey :: hmac ( SHA_KEY ) . unwrap ( ) ; let mut signer = Signer :: new ( MessageDigest :: sha256 ( ) , & key ) . unwrap ( ) ; signer . update ( & pktptr [ .. ( pktlen - ICV_LEN_SHA256 ) ] ) . unwrap ( ) ; let hmac = signer . sign_to_vec ( ) . unwrap ( ) ; if compdigest { if ! memcmp :: eq ( & hmac [ .. ICV_LEN_SHA256 ] , & pktptr [ ( pktlen - ICV_LEN_SHA256 ) .. ] ) { println ! ( " " ) ; stdout ( ) . flush ( ) . unwrap ( ) ; return Err ( CryptoError :: HmacMismatch ) ; } } let cipher = Cipher :: aes_128_cbc ( ) ; // let cleartext = decrypt(cipher, AES_KEY, Some(AES_IV), &pktptr[(ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH)..(pktlen - ICV_LEN_SHA256)]).unwrap(); let cleartext = my_decrypt ( cipher , AES_KEY , Some ( AES_IV ) , & pktptr [ ( ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH ) .. ( pktlen - ICV_LEN_SHA256 ) ] ) . unwrap ( ) ; let cleartext_len = cleartext . len ( ) ; if cleartext_len != pktlen - ESP_HEADER_LENGTH - AES_CBC_IV_LENGTH - ICV_LEN_SHA256 { println ! ( " " pktlen ESP_HEADER_LENGTH AES_CBC_IV_LENGTH ICV_LEN_SHA256 cleartext_len ) ; println ! ( " " ) ; stdout ( ) . flush ( ) . unwrap ( ) ; return Err ( CryptoError :: AESDecryptError ) ; } pktptr [ .. ( cleartext_len ) ] . copy_from_slice ( & cleartext [ .. ] ) ; Ok ( cleartext_len + ESP_HEADER_LENGTH + AES_CBC_IV_LENGTH ) }
pktptr points to the start of the ESP header
after calling, output points to the start of the decrypted ip header.
pktptr points to the start of the ESP header after calling output points to the start of the decrypted ip header .
pktptr
&mut [u8]
compdigest
bool