mod cmac;
mod crc;
mod encrypted;
mod mock;
mod plain;
pub use cmac::{cmac_out_cmac_in, plain_out_cmac_in};
pub(crate) use crc::check_crc32;
pub use encrypted::{encrypted_out_cmac_in, encrypted_out_plain_in, plain_out_encrypted_in};
pub use mock::MockBackend;
#[allow(unused_imports)]
pub(crate) use mock::mock_backend;
pub use plain::{plain, plain_multi};
use crate::{
StatusCode,
std::{fmt::Debug, future::Future},
};
pub trait Backend
where
Self::Error: Debug,
Self: Send,
Self: Sync,
{
type Error;
fn exchange_raw(
&self,
output: &mut [u8],
input: &[u8],
) -> impl Future<Output = Result<usize, Self::Error>> + Send + Sync;
#[allow(async_fn_in_trait)]
async fn exchange<'a>(
&self,
output: &'a mut [u8],
input: &[u8],
) -> Result<(StatusCode, &'a [u8]), Self::Error> {
let n = self.exchange_raw(output, input).await?;
assert!(n > 0);
let status_code = output[0];
let data = &output[1..n];
Ok((status_code.into(), data))
}
}
impl<T> Backend for &T
where
T: Backend,
{
type Error = T::Error;
async fn exchange_raw(&self, output: &mut [u8], input: &[u8]) -> Result<usize, Self::Error> {
<T as Backend>::exchange_raw(self, output, input).await
}
}
impl<T> Backend for &mut T
where
T: Backend,
{
type Error = T::Error;
async fn exchange_raw(&self, output: &mut [u8], input: &[u8]) -> Result<usize, Self::Error> {
<T as Backend>::exchange_raw(self, output, input).await
}
}