use crate::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum Suite {
XWing = 0x01,
#[default]
Max = 0x02,
#[cfg(feature = "paranoid")]
Paranoid = 0x03,
}
const PARANOID_NAME: &str = "pqbox-paranoid";
impl Suite {
#[cfg(feature = "paranoid")]
pub const ALL: [Suite; 3] = [Suite::XWing, Suite::Max, Suite::Paranoid];
#[cfg(not(feature = "paranoid"))]
pub const ALL: [Suite; 2] = [Suite::XWing, Suite::Max];
pub fn from_id(id: u8) -> Result<Self> {
match id {
0x01 => Ok(Self::XWing),
0x02 => Ok(Self::Max),
#[cfg(feature = "paranoid")]
0x03 => Ok(Self::Paranoid),
#[cfg(not(feature = "paranoid"))]
0x03 => Err(Error::SuiteNotEnabled(PARANOID_NAME)),
other => Err(Error::UnknownSuite(other)),
}
}
#[must_use]
pub fn id(self) -> u8 {
self as u8
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::XWing => "xwing",
Self::Max => "pqbox-max",
#[cfg(feature = "paranoid")]
Self::Paranoid => PARANOID_NAME,
}
}
#[must_use]
pub fn kem_components(self) -> &'static [&'static str] {
match self {
Self::XWing => &["ML-KEM-768", "X25519"],
Self::Max => &["ML-KEM-1024", "ECDH-P384"],
#[cfg(feature = "paranoid")]
Self::Paranoid => &["ML-KEM-1024", "Classic-McEliece-8192128", "ECDH-P384"],
}
}
#[must_use]
pub fn signature_components(self) -> &'static [&'static str] {
match self {
Self::XWing => &["ML-DSA-65", "Ed25519"],
Self::Max => &["ML-DSA-87", "ECDSA-P384-SHA384"],
#[cfg(feature = "paranoid")]
Self::Paranoid => &["ML-DSA-87", "SLH-DSA-SHAKE-256s", "ECDSA-P384-SHA384"],
}
}
#[must_use]
pub fn is_compat_only(self) -> bool {
self == Self::XWing
}
pub fn from_name(name: &str) -> Result<Self> {
match name.to_ascii_lowercase().replace('_', "-").as_str() {
"xwing" | "x-wing" => Ok(Self::XWing),
"pqbox-max" | "max" => Ok(Self::Max),
#[cfg(feature = "paranoid")]
"pqbox-paranoid" | "paranoid" => Ok(Self::Paranoid),
#[cfg(not(feature = "paranoid"))]
"pqbox-paranoid" | "paranoid" => Err(Error::SuiteNotEnabled(PARANOID_NAME)),
_ => Err(Error::InvalidParams("unknown suite")),
}
}
}
impl core::fmt::Display for Suite {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.name())
}
}
impl core::str::FromStr for Suite {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Self::from_name(s)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SuitePolicy {
mask: u8,
}
impl SuitePolicy {
pub const DEFAULT: Self = Self::only(Suite::Max);
pub const NONE: Self = Self { mask: 0 };
pub const ALL: Self = {
let p = Self::NONE.allow(Suite::XWing).allow(Suite::Max);
#[cfg(feature = "paranoid")]
let p = p.allow(Suite::Paranoid);
p
};
#[must_use]
pub const fn only(suite: Suite) -> Self {
Self::NONE.allow(suite)
}
#[must_use]
pub const fn allow(self, suite: Suite) -> Self {
Self {
mask: self.mask | (1 << suite as u8),
}
}
#[must_use]
pub const fn allows(self, suite: Suite) -> bool {
self.mask & (1 << suite as u8) != 0
}
pub fn suites(self) -> impl Iterator<Item = Suite> {
Suite::ALL.into_iter().filter(move |s| self.allows(*s))
}
pub fn check(self, suite: Suite) -> Result<Suite> {
if self.allows(suite) {
Ok(suite)
} else {
Err(Error::SuiteNotAllowed(suite.name()))
}
}
pub fn allow_names<'a>(self, names: impl IntoIterator<Item = &'a str>) -> Result<Self> {
names
.into_iter()
.try_fold(self, |p, name| Ok(p.allow(Suite::from_name(name)?)))
}
pub fn suite_from_id(self, id: u8) -> Result<Suite> {
self.check(Suite::from_id(id)?)
}
}
impl Default for SuitePolicy {
fn default() -> Self {
Self::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_and_names_round_trip() {
for s in Suite::ALL {
assert_eq!(Suite::from_id(s.id()).unwrap(), s);
assert_eq!(Suite::from_name(s.name()).unwrap(), s);
}
assert_eq!(Suite::default(), Suite::Max);
assert_eq!(Suite::from_id(0), Err(Error::UnknownSuite(0)));
assert_eq!(Suite::from_id(4), Err(Error::UnknownSuite(4)));
assert!(Suite::from_name("rsa").is_err());
#[cfg(not(feature = "paranoid"))]
{
assert_eq!(Suite::from_id(3), Err(Error::SuiteNotEnabled("pqbox-paranoid")));
assert_eq!(
Suite::from_name("paranoid"),
Err(Error::SuiteNotEnabled("pqbox-paranoid"))
);
}
}
#[test]
fn default_policy_accepts_only_max() {
let p = SuitePolicy::default();
assert_eq!(p, SuitePolicy::DEFAULT);
assert!(p.allows(Suite::Max));
assert!(!p.allows(Suite::XWing));
assert_eq!(p.check(Suite::XWing), Err(Error::SuiteNotAllowed("xwing")));
assert_eq!(p.suite_from_id(2), Ok(Suite::Max));
assert_eq!(p.suite_from_id(1), Err(Error::SuiteNotAllowed("xwing")));
assert_eq!(p.suite_from_id(9), Err(Error::UnknownSuite(9)));
assert_eq!(p.suites().collect::<Vec<_>>(), [Suite::Max]);
#[cfg(feature = "paranoid")]
{
assert!(!p.allows(Suite::Paranoid));
assert_eq!(p.suite_from_id(3), Err(Error::SuiteNotAllowed("pqbox-paranoid")));
}
let x = p.allow(Suite::XWing);
assert!(x.allows(Suite::XWing) && x.allows(Suite::Max));
assert_eq!(
SuitePolicy::only(Suite::XWing).check(Suite::Max),
Err(Error::SuiteNotAllowed("pqbox-max"))
);
assert!(!SuitePolicy::NONE.allows(Suite::Max));
assert_eq!(SuitePolicy::ALL.suites().collect::<Vec<_>>(), Suite::ALL.to_vec());
}
#[test]
fn non_compat_suites_are_post_quantum_with_p384() {
assert!(!Suite::default().is_compat_only());
for s in Suite::ALL.into_iter().filter(|s| !s.is_compat_only()) {
assert!(s.kem_components().contains(&"ML-KEM-1024"), "{s}");
assert!(s.signature_components().contains(&"ML-DSA-87"), "{s}");
for c in s.kem_components().iter().chain(s.signature_components()) {
assert!(!["X25519", "Ed25519", "P-256", "secp256k1"].contains(c), "{s}: {c}");
}
assert!(s.kem_components().contains(&"ECDH-P384"), "{s}");
assert!(s.signature_components().contains(&"ECDSA-P384-SHA384"), "{s}");
}
for s in Suite::ALL {
assert!(s.kem_components()[0].starts_with("ML-KEM"), "{s}");
assert!(s.signature_components()[0].starts_with("ML-DSA"), "{s}");
}
}
}