pub mod counter;
pub mod keys;
mod protect;
#[cfg(any(feature = "client", feature = "server"))]
pub(crate) mod wrap;
#[cfg(feature = "suite0")]
mod rustcrypto;
pub use counter::{InvocationCounter, REPLAY_WIDTH_MAX, REPLAY_WINDOW_MAX, ReplayWindow};
pub use keys::{Key, KeyRef, KeyRing, KeyUsage, Secret, SystemTitle};
pub use protect::{KeySet, Protector, SecurityPolicy, constant_time_eq};
#[cfg(all(feature = "std", feature = "suite0"))]
pub use rustcrypto::OsRandom;
#[cfg(feature = "suite0")]
pub use rustcrypto::{FixedRandom, RandomSource, RustCryptoProvider};
use crate::codec::{Error, ErrorKind, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum SecuritySuite {
#[default]
Suite0,
Suite1,
Suite2,
}
impl SecuritySuite {
#[must_use]
pub const fn id(self) -> u8 {
match self {
Self::Suite0 => 0,
Self::Suite1 => 1,
Self::Suite2 => 2,
}
}
pub fn from_id(v: u8) -> Result<Self> {
Ok(match v {
0 => Self::Suite0,
1 => Self::Suite1,
2 => Self::Suite2,
_ => return Err(Error::new(ErrorKind::Unsupported, 0)),
})
}
#[must_use]
pub const fn key_len(self) -> usize {
match self {
Self::Suite0 | Self::Suite1 => 16,
Self::Suite2 => 32,
}
}
pub const TAG_LEN: usize = 12;
pub const NONCE_LEN: usize = 12;
}
pub trait CryptoProvider {
fn aead_seal(
&self,
key: keys::KeyRef<'_>,
suite: SecuritySuite,
nonce: &[u8; 12],
aad: &[u8],
buf: &mut [u8],
) -> Result<[u8; 12]>;
fn aead_open(
&self,
key: keys::KeyRef<'_>,
suite: SecuritySuite,
nonce: &[u8; 12],
aad: &[u8],
buf: &mut [u8],
tag: &[u8; 12],
) -> Result<()>;
fn gmac(
&self,
key: keys::KeyRef<'_>,
suite: SecuritySuite,
nonce: &[u8; 12],
aad: &[&[u8]],
) -> Result<[u8; 12]>;
fn key_wrap(&self, _kek: keys::KeyRef<'_>, _key: &[u8], _out: &mut [u8]) -> Result<usize> {
Err(Error::new(ErrorKind::Unsupported, 0))
}
fn key_unwrap(&self, _kek: keys::KeyRef<'_>, _wrapped: &[u8], _out: &mut [u8]) -> Result<usize> {
Err(Error::new(ErrorKind::Unsupported, 0))
}
fn random(&self, out: &mut [u8]) -> Result<()>;
fn authentication_key(&self) -> Option<&[u8]>;
fn dedicated_key(&self) -> Option<&[u8]> {
None
}
fn set_dedicated_key(&mut self, _key: keys::Key) -> Result<()> {
Err(Error::new(ErrorKind::Unsupported, 0))
}
fn clear_dedicated_key(&mut self) {}
}