mod aes;
mod memory;
mod random;
mod sha;
mod signing;
mod verifying;
pub use aes::Aes;
pub use memory::Memory;
pub use random::Random;
pub use sha::Sha;
pub use signing::SigningKey;
pub use verifying::VerifyingKey;
use core::future::Future;
use crate::clock_divider::ClockDivider;
use crate::command::{
self, Ecdh, GenKey, Info, NonceCtx, PrivWrite, PublicKey, SharedSecret, Word,
};
use crate::datalink::{I2c, I2cConfig};
use crate::error::{Error, ErrorKind};
use crate::memory::{Slot, Zone};
use crate::packet::{Packet, PacketBuilder, Response};
use crate::tngtls::TrustAndGo;
use crate::{Block, Digest};
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex;
use heapless::Vec;
pub(crate) struct Inner<PHY> {
pub i2c: I2c<PHY>,
pub buffer: Vec<u8, 192>,
pub clock_divider: ClockDivider,
}
impl<PHY> Inner<PHY> {
pub(crate) fn packet_builder(&mut self) -> PacketBuilder<'_> {
let capacity = self.buffer.capacity();
self.buffer.clear();
self.buffer
.resize(capacity, 0x00u8)
.unwrap_or_else(|_| unreachable!("Input length equals to the current capacity."));
PacketBuilder::new(&mut self.buffer)
}
}
impl<PHY> Inner<PHY>
where
PHY: embedded_hal_async::i2c::I2c,
{
pub(crate) fn execute(
&mut self,
packet: Packet,
) -> impl Future<Output = Result<Response<'_>, Error>> {
let exec_time = self.clock_divider.execution_time(packet.opcode());
self.i2c.execute(&mut self.buffer, packet, exec_time)
}
}
impl<PHY> Inner<PHY>
where
PHY: embedded_hal::i2c::I2c,
{
pub(crate) fn execute_blocking(&mut self, packet: Packet) -> Result<Response<'_>, Error> {
let exec_time = self.clock_divider.execution_time(packet.opcode());
self.i2c
.execute_blocking(&mut self.buffer, packet, exec_time)
}
}
pub struct AtCaClient<M: RawMutex, PHY> {
pub(crate) inner: Mutex<M, Inner<PHY>>,
}
impl<M: RawMutex, PHY> AtCaClient<M, PHY> {
pub fn new(phy: PHY) -> Self {
Self::with_config(phy, I2cConfig::default())
}
pub fn with_config(phy: PHY, config: I2cConfig) -> Self {
let i2c = I2c::with_config(phy, config);
let buffer = Vec::new();
Self {
inner: Mutex::new(Inner {
i2c,
buffer,
clock_divider: ClockDivider::Zero,
}),
}
}
pub fn memory(&self) -> Memory<'_, M, PHY> {
Memory { atca: self }
}
pub fn aes(&self, key_id: Slot) -> Aes<'_, M, PHY> {
Aes { atca: self, key_id }
}
pub fn sha(&self) -> Sha<'_, M, PHY> {
let remaining_bytes = Vec::new();
Sha {
atca: self,
remaining_bytes,
}
}
pub fn signer(&self, key_id: Slot) -> SigningKey<'_, M, PHY> {
SigningKey { atca: self, key_id }
}
pub fn verifier(&self, key_id: Slot) -> VerifyingKey<'_, M, PHY> {
VerifyingKey { atca: self, key_id }
}
pub fn random(&self) -> Random<'_, M, PHY> {
Random::new(self)
}
}
impl<M, PHY> AtCaClient<M, PHY>
where
PHY: embedded_hal_async::i2c::I2c,
M: RawMutex,
{
pub async fn tng(&self) -> Result<TrustAndGo<'_, M, PHY>, Error> {
let mut tng = TrustAndGo { atca: self };
if !tng.atca.memory().is_locked(Zone::Config).await? {
tng.configure_permissions().await?;
tng.configure_chip_options().await?;
tng.configure_key_types().await?;
tng.atca.memory().lock(Zone::Config).await?;
}
if !tng.atca.memory().is_locked(Zone::Data).await? {
#[cfg(not(debug_assertions))]
tng.atca.memory().lock(Zone::Data).await?;
}
Ok(tng)
}
pub async fn sleep(&self) -> Result<(), Error> {
self.inner.lock().await.i2c.sleep().await
}
pub async fn info(&self) -> Result<Word, Error> {
let mut inner = self.inner.lock().await;
let packet = Info::new(inner.packet_builder()).revision()?;
inner.execute(packet).await?.as_ref().try_into()
}
pub(crate) async fn update_seed(&self) -> Result<(), Error> {
let mut inner = self.inner.lock().await;
let packet = command::Random::new(inner.packet_builder()).random()?;
inner.execute(packet).await.map(drop)
}
pub async fn write_message_digest_buffer(&self, msg: &Digest) -> Result<(), Error> {
let mut inner = self.inner.lock().await;
let packet = NonceCtx::new(inner.packet_builder()).message_digest_buffer(msg)?;
inner.execute(packet).await.map(drop)
}
pub async fn create_private_key(&self, key_id: Slot) -> Result<PublicKey, Error> {
let mut inner = self.inner.lock().await;
let packet = GenKey::new(inner.packet_builder()).private_key(key_id)?;
inner.execute(packet).await?.as_ref().try_into()
}
pub async fn write_private_key(&self, key_id: Slot, private_key: &Block) -> Result<(), Error> {
let mut inner = self.inner.lock().await;
let packet =
PrivWrite::new(inner.packet_builder()).write_private_key(key_id, private_key)?;
inner.execute(packet).await.map(drop)
}
pub async fn generate_pubkey(&self, key_id: Slot) -> Result<PublicKey, Error> {
let mut inner = self.inner.lock().await;
let packet = GenKey::new(inner.packet_builder()).public_key(key_id)?;
inner.execute(packet).await?.as_ref().try_into()
}
pub async fn diffie_hellman(
&self,
key_id: Slot,
public_key: PublicKey,
) -> Result<SharedSecret, Error> {
let mut inner = self.inner.lock().await;
let packet = Ecdh::new(inner.packet_builder()).diffie_hellman(key_id, public_key)?;
inner.execute(packet).await?.as_ref().try_into()
}
pub async fn verify_external(
&self,
digest: &Digest,
signature: &p256::ecdsa::Signature,
public_key: &PublicKey,
) -> Result<(), Error> {
let mut inner = self.inner.lock().await;
let packet = NonceCtx::new(inner.packet_builder()).message_digest_buffer(digest)?;
inner.execute(packet).await?;
let packet =
command::Verify::new(inner.packet_builder()).external(signature, public_key)?;
inner.execute(packet).await?;
Ok(())
}
}
impl<M, PHY> AtCaClient<M, PHY>
where
PHY: embedded_hal::i2c::I2c,
M: RawMutex,
{
pub fn sleep_blocking(&self) -> Result<(), Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
inner.i2c.sleep_blocking()
}
pub fn info_blocking(&self) -> Result<Word, Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = Info::new(inner.packet_builder()).revision()?;
inner.execute_blocking(packet)?.as_ref().try_into()
}
pub(crate) fn update_seed_blocking(&self) -> Result<(), Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = command::Random::new(inner.packet_builder()).random()?;
inner.execute_blocking(packet).map(drop)
}
pub fn write_message_digest_buffer_blocking(&self, msg: &Digest) -> Result<(), Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = NonceCtx::new(inner.packet_builder()).message_digest_buffer(msg)?;
inner.execute_blocking(packet).map(drop)
}
pub fn create_private_key_blocking(&self, key_id: Slot) -> Result<PublicKey, Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = GenKey::new(inner.packet_builder()).private_key(key_id)?;
inner.execute_blocking(packet)?.as_ref().try_into()
}
pub fn write_private_key_blocking(
&self,
key_id: Slot,
private_key: &Block,
) -> Result<(), Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet =
PrivWrite::new(inner.packet_builder()).write_private_key(key_id, private_key)?;
inner.execute_blocking(packet).map(drop)
}
pub fn generate_pubkey_blocking(&self, key_id: Slot) -> Result<PublicKey, Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = GenKey::new(inner.packet_builder()).public_key(key_id)?;
inner.execute_blocking(packet)?.as_ref().try_into()
}
pub fn diffie_hellman_blocking(
&self,
key_id: Slot,
public_key: PublicKey,
) -> Result<SharedSecret, Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = Ecdh::new(inner.packet_builder()).diffie_hellman(key_id, public_key)?;
inner.execute_blocking(packet)?.as_ref().try_into()
}
pub fn verify_external_blocking(
&self,
digest: &Digest,
signature: &p256::ecdsa::Signature,
public_key: &PublicKey,
) -> Result<(), Error> {
let mut inner = self.inner.try_lock().map_err(|_| ErrorKind::MutexLocked)?;
let packet = NonceCtx::new(inner.packet_builder()).message_digest_buffer(digest)?;
inner.execute_blocking(packet)?;
let packet =
command::Verify::new(inner.packet_builder()).external(signature, public_key)?;
inner.execute_blocking(packet)?;
Ok(())
}
}