#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
#![cfg_attr(feature = "getrandom", doc = "```")]
#![cfg_attr(not(feature = "getrandom"), doc = "```ignore")]
#![cfg_attr(all(feature = "getrandom", feature = "arrayvec"), doc = "```")]
#![cfg_attr(
not(all(feature = "getrandom", feature = "arrayvec")),
doc = "```ignore"
)]
mod asconcore;
pub use aead::{self, Error, Key, Nonce, Tag};
#[cfg(feature = "zeroize")]
pub use zeroize;
use aead::{AeadCore, AeadInOut, KeyInit, KeySizeUser, TagPosition, consts::U16, inout::InOutBuf};
use asconcore::{AsconCore, Parameters, Parameters128};
use core::fmt;
#[derive(Clone)]
struct Ascon<P: Parameters> {
key: P::InternalKey,
}
impl<P: Parameters> KeySizeUser for Ascon<P> {
type KeySize = P::KeySize;
}
impl<P: Parameters> KeyInit for Ascon<P> {
fn new(key: &Key<Self>) -> Self {
Self {
key: P::InternalKey::from(key),
}
}
}
impl<P: Parameters> AeadCore for Ascon<P> {
type NonceSize = U16;
type TagSize = U16;
const TAG_POSITION: TagPosition = TagPosition::Postfix;
}
impl<P: Parameters> AeadInOut for Ascon<P> {
fn encrypt_inout_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: InOutBuf<'_, '_, u8>,
) -> Result<Tag<Self>, Error> {
if (buffer.len() as u64)
.checked_add(associated_data.len() as u64)
.is_none()
{
return Err(Error);
}
let mut core = AsconCore::<P>::new(&self.key, nonce);
Ok(core.encrypt_inout(buffer, associated_data))
}
fn decrypt_inout_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: InOutBuf<'_, '_, u8>,
tag: &Tag<Self>,
) -> Result<(), Error> {
if (buffer.len() as u64)
.checked_add(associated_data.len() as u64)
.is_none()
{
return Err(Error);
}
let mut core = AsconCore::<P>::new(&self.key, nonce);
core.decrypt_inout(buffer, associated_data, tag)
}
}
pub struct AsconAead128(Ascon<Parameters128>);
pub type AsconAead128Key = Key<AsconAead128>;
pub type AsconAead128Nonce = Nonce<AsconAead128>;
pub type AsconAead128Tag = Tag<AsconAead128>;
impl KeySizeUser for AsconAead128 {
type KeySize = U16;
}
impl KeyInit for AsconAead128 {
fn new(key: &Key<Self>) -> Self {
Self(Ascon::<Parameters128>::new(key))
}
}
impl AeadCore for AsconAead128 {
type NonceSize = U16;
type TagSize = U16;
const TAG_POSITION: TagPosition = TagPosition::Postfix;
}
impl AeadInOut for AsconAead128 {
#[inline(always)]
fn encrypt_inout_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: InOutBuf<'_, '_, u8>,
) -> Result<Tag<Self>, Error> {
self.0
.encrypt_inout_detached(nonce, associated_data, buffer)
}
#[inline(always)]
fn decrypt_inout_detached(
&self,
nonce: &Nonce<Self>,
associated_data: &[u8],
buffer: InOutBuf<'_, '_, u8>,
tag: &Tag<Self>,
) -> Result<(), Error> {
self.0
.decrypt_inout_detached(nonce, associated_data, buffer, tag)
}
}
impl fmt::Debug for AsconAead128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AsconAead128").finish_non_exhaustive()
}
}