#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![forbid(unsafe_code)]
pub mod hazmat;
#[cfg(feature = "rand_core")]
mod generate;
pub use hybrid_array as array;
pub use hybrid_array::typenum;
#[cfg(feature = "getrandom")]
pub use getrandom;
#[cfg(feature = "rand_core")]
pub use {generate::Generate, rand_core};
use core::fmt;
use hybrid_array::{
Array, ArraySize,
typenum::{Diff, Sum, Unsigned},
};
#[cfg(feature = "rand_core")]
use rand_core::CryptoRng;
pub type Block<B> = Array<u8, <B as BlockSizeUser>::BlockSize>;
pub type ParBlocks<T> = Array<Block<T>, <T as ParBlocksSizeUser>::ParBlocksSize>;
pub type Output<T> = Array<u8, OutputSize<T>>;
pub type OutputSize<T> = <T as OutputSizeUser>::OutputSize;
pub type Key<B> = Array<u8, <B as KeySizeUser>::KeySize>;
pub type Iv<B> = Array<u8, <B as IvSizeUser>::IvSize>;
pub type AddBlockSize<T, B> = Sum<T, <B as BlockSizeUser>::BlockSize>;
pub type SubBlockSize<T, B> = Diff<T, <B as BlockSizeUser>::BlockSize>;
pub trait BlockSizeUser {
type BlockSize: ArraySize;
#[inline(always)]
#[must_use]
fn block_size() -> usize {
Self::BlockSize::USIZE
}
}
impl<T: BlockSizeUser> BlockSizeUser for &T {
type BlockSize = T::BlockSize;
}
impl<T: BlockSizeUser> BlockSizeUser for &mut T {
type BlockSize = T::BlockSize;
}
pub trait ParBlocksSizeUser: BlockSizeUser {
type ParBlocksSize: ArraySize;
}
pub trait OutputSizeUser {
type OutputSize: ArraySize;
#[inline(always)]
#[must_use]
fn output_size() -> usize {
Self::OutputSize::USIZE
}
}
pub trait KeySizeUser {
type KeySize: ArraySize;
#[inline(always)]
#[must_use]
fn key_size() -> usize {
Self::KeySize::USIZE
}
}
pub trait IvSizeUser {
type IvSize: ArraySize;
#[inline(always)]
#[must_use]
fn iv_size() -> usize {
Self::IvSize::USIZE
}
}
pub trait InnerUser {
type Inner;
}
pub trait Reset {
fn reset(&mut self);
}
pub trait AlgorithmName {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result;
}
pub trait KeyExport: KeySizeUser {
fn to_bytes(&self) -> Key<Self>;
}
pub trait KeyInit: KeySizeUser + Sized {
fn new(key: &Key<Self>) -> Self;
#[inline]
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
<&Key<Self>>::try_from(key)
.map(Self::new)
.map_err(|_| InvalidLength)
}
#[deprecated(
since = "0.2.0",
note = "use the `Generate` trait impl on `Key` instead"
)]
#[cfg(feature = "rand_core")]
fn generate_key<R: CryptoRng>(rng: &mut R) -> Key<Self> {
Key::<Self>::generate_from_rng(rng)
}
}
pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
fn new(key: &Key<Self>, iv: &Iv<Self>) -> Self;
#[inline]
fn new_from_slices(key: &[u8], iv: &[u8]) -> Result<Self, InvalidLength> {
let key = <&Key<Self>>::try_from(key).map_err(|_| InvalidLength)?;
let iv = <&Iv<Self>>::try_from(iv).map_err(|_| InvalidLength)?;
Ok(Self::new(key, iv))
}
#[deprecated(
since = "0.2.0",
note = "use the `Generate` trait impl on `Key` instead"
)]
#[cfg(feature = "rand_core")]
fn generate_key<R: CryptoRng>(rng: &mut R) -> Key<Self> {
Key::<Self>::generate_from_rng(rng)
}
#[deprecated(
since = "0.2.0",
note = "use the `Generate` trait impl on `Iv` instead"
)]
#[cfg(feature = "rand_core")]
fn generate_iv<R: CryptoRng>(rng: &mut R) -> Iv<Self> {
Iv::<Self>::generate_from_rng(rng)
}
#[deprecated(
since = "0.2.0",
note = "use the `Generate` trait impls on `Key` and `Iv` instead"
)]
#[cfg(feature = "rand_core")]
fn generate_key_iv<R: CryptoRng>(rng: &mut R) -> (Key<Self>, Iv<Self>) {
let key = Key::<Self>::generate_from_rng(rng);
let iv = Iv::<Self>::generate_from_rng(rng);
(key, iv)
}
}
pub trait TryKeyInit: KeySizeUser + Sized {
fn new(key: &Key<Self>) -> Result<Self, InvalidKey>;
#[inline]
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidKey> {
<&Key<Self>>::try_from(key)
.map_err(|_| InvalidKey)
.and_then(Self::new)
}
}
pub trait InnerInit: InnerUser + Sized {
fn inner_init(inner: Self::Inner) -> Self;
}
pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
fn inner_iv_init(inner: Self::Inner, iv: &Iv<Self>) -> Self;
#[inline]
fn inner_iv_slice_init(inner: Self::Inner, iv: &[u8]) -> Result<Self, InvalidLength> {
let iv = <&Iv<Self>>::try_from(iv).map_err(|_| InvalidLength)?;
Ok(Self::inner_iv_init(inner, iv))
}
}
pub trait IvState: IvSizeUser {
fn iv_state(&self) -> Iv<Self>;
}
impl<T> KeySizeUser for T
where
T: InnerUser,
T::Inner: KeySizeUser,
{
type KeySize = <T::Inner as KeySizeUser>::KeySize;
}
impl<T> KeyIvInit for T
where
T: InnerIvInit,
T::Inner: KeyInit,
{
#[inline]
fn new(key: &Key<Self>, iv: &Iv<Self>) -> Self {
Self::inner_iv_init(T::Inner::new(key), iv)
}
#[inline]
fn new_from_slices(key: &[u8], iv: &[u8]) -> Result<Self, InvalidLength> {
T::Inner::new_from_slice(key).and_then(|i| T::inner_iv_slice_init(i, iv))
}
}
impl<T> KeyInit for T
where
T: InnerInit,
T::Inner: KeyInit,
{
#[inline]
fn new(key: &Key<Self>) -> Self {
Self::inner_init(T::Inner::new(key))
}
#[inline]
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
T::Inner::new_from_slice(key)
.map_err(|_| InvalidLength)
.map(Self::inner_init)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct InvalidKey;
impl fmt::Display for InvalidKey {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("InvalidKey")
}
}
impl core::error::Error for InvalidKey {}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct InvalidLength;
impl fmt::Display for InvalidLength {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("Invalid Length")
}
}
impl core::error::Error for InvalidLength {}