#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms, missing_debug_implementations)]
pub mod hazmat;
#[cfg(feature = "rand_core")]
pub use rand_core;
pub use hybrid_array as array;
pub use hybrid_array::typenum;
use core::fmt;
use hybrid_array::{
Array, ArraySize,
typenum::{Diff, Sum, Unsigned},
};
#[cfg(feature = "rand_core")]
use rand_core::{CryptoRng, TryCryptoRng};
#[cfg(feature = "os_rng")]
use rand_core::{OsError, OsRng, TryRngCore};
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: BlockSizes;
#[inline(always)]
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 BlockSizes: ArraySize + sealed::BlockSizes {}
impl<T: ArraySize + sealed::BlockSizes> BlockSizes for T {}
mod sealed {
use crate::typenum::{IsLess, NonZero, True, U256, Unsigned};
pub trait BlockSizes {}
impl<T: Unsigned> BlockSizes for T where Self: IsLess<U256, Output = True> + NonZero {}
}
pub trait ParBlocksSizeUser: BlockSizeUser {
type ParBlocksSize: ArraySize;
}
pub trait OutputSizeUser {
type OutputSize: ArraySize;
#[inline(always)]
fn output_size() -> usize {
Self::OutputSize::USIZE
}
}
pub trait KeySizeUser {
type KeySize: ArraySize;
#[inline(always)]
fn key_size() -> usize {
Self::KeySize::USIZE
}
}
pub trait IvSizeUser {
type IvSize: ArraySize;
#[inline(always)]
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 KeyInit: KeySizeUser + Sized {
fn new(key: &Key<Self>) -> Self;
#[inline]
fn weak_key_test(_key: &Key<Self>) -> Result<(), WeakKeyError> {
Ok(())
}
#[inline]
fn new_checked(key: &Key<Self>) -> Result<Self, WeakKeyError> {
Self::weak_key_test(key)?;
Ok(Self::new(key))
}
#[inline]
fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> {
<&Key<Self>>::try_from(key)
.map(Self::new)
.map_err(|_| InvalidLength)
}
#[cfg(feature = "os_rng")]
#[inline]
fn generate_key() -> Result<Key<Self>, OsError> {
let mut key = Key::<Self>::default();
OsRng.try_fill_bytes(&mut key)?;
Ok(key)
}
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
key
}
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Key<Self>, R::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
}
}
pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
fn new(key: &Key<Self>, iv: &Iv<Self>) -> Self;
#[inline]
fn weak_key_test(_key: &Key<Self>) -> Result<(), WeakKeyError> {
Ok(())
}
#[inline]
fn new_checked(key: &Key<Self>, iv: &Iv<Self>) -> Result<Self, WeakKeyError> {
Self::weak_key_test(key)?;
Ok(Self::new(key, iv))
}
#[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))
}
#[cfg(feature = "os_rng")]
#[inline]
fn generate_key() -> Result<Key<Self>, OsError> {
let mut key = Key::<Self>::default();
OsRng.try_fill_bytes(&mut key)?;
Ok(key)
}
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Key<Self> {
let mut key = Key::<Self>::default();
rng.fill_bytes(&mut key);
key
}
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Key<Self>, R::Error> {
let mut key = Key::<Self>::default();
rng.try_fill_bytes(&mut key)?;
Ok(key)
}
#[cfg(feature = "os_rng")]
#[inline]
fn generate_iv() -> Result<Iv<Self>, OsError> {
let mut iv = Iv::<Self>::default();
OsRng.try_fill_bytes(&mut iv)?;
Ok(iv)
}
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
}
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Iv<Self>, R::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(iv)
}
#[cfg(feature = "os_rng")]
#[inline]
fn generate_key_iv() -> Result<(Key<Self>, Iv<Self>), OsError> {
let key = Self::generate_key()?;
let iv = Self::generate_iv()?;
Ok((key, iv))
}
#[cfg(feature = "rand_core")]
#[inline]
fn generate_key_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> (Key<Self>, Iv<Self>) {
let key = Self::generate_key_with_rng(rng);
let iv = Self::generate_iv_with_rng(rng);
(key, iv)
}
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_key_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<(Key<Self>, Iv<Self>), R::Error> {
let key = Self::try_generate_key_with_rng(rng)?;
let iv = Self::try_generate_iv_with_rng(rng)?;
Ok((key, iv))
}
}
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))
}
#[cfg(feature = "os_rng")]
#[inline]
fn generate_iv() -> Result<Iv<Self>, OsError> {
let mut iv = Iv::<Self>::default();
OsRng.try_fill_bytes(&mut iv)?;
Ok(iv)
}
#[cfg(feature = "rand_core")]
#[inline]
fn generate_iv_with_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Iv<Self> {
let mut iv = Iv::<Self>::default();
rng.fill_bytes(&mut iv);
iv
}
#[cfg(feature = "rand_core")]
#[inline]
fn try_generate_iv_with_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Iv<Self>, R::Error> {
let mut iv = Iv::<Self>::default();
rng.try_fill_bytes(&mut iv)?;
Ok(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))
}
#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
T::Inner::weak_key_test(key)
}
}
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)
}
#[inline]
fn weak_key_test(key: &Key<Self>) -> Result<(), WeakKeyError> {
T::Inner::weak_key_test(key)
}
}
#[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 {}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct WeakKeyError;
impl fmt::Display for WeakKeyError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("WeakKey")
}
}
impl core::error::Error for WeakKeyError {}