use crate::utils::*;
use botan_sys::*;
use crate::mp::MPI;
#[derive(Debug)]
pub struct FPE {
obj: botan_fpe_t,
}
impl Drop for FPE {
fn drop(&mut self) {
unsafe {
botan_fpe_destroy(self.obj);
}
}
}
impl FPE {
pub fn new_fe1(modulus: &MPI, key: &[u8], rounds: usize, compat_mode: bool) -> Result<FPE> {
let mut obj = ptr::null_mut();
let flags = if compat_mode { 1 } else { 0 };
call_botan! {
botan_fpe_fe1_init(&mut obj, modulus.handle(),
key.as_ptr(), key.len(),
rounds, flags)
}
Ok(FPE { obj })
}
pub fn encrypt(&self, x: &MPI, tweak: &[u8]) -> Result<MPI> {
let r = x.duplicate()?;
call_botan! {
botan_fpe_encrypt(self.obj, r.handle(), tweak.as_ptr(), tweak.len())
}
Ok(r)
}
pub fn decrypt(&self, x: &MPI, tweak: &[u8]) -> Result<MPI> {
let r = x.duplicate()?;
call_botan! {
botan_fpe_decrypt(self.obj, r.handle(), tweak.as_ptr(), tweak.len())
}
Ok(r)
}
}