89b5329f06
X-Wing is composed from ml-kem, x25519-dalek, sha3 and shake and matches the draft's vectors. The PSK comes from a mutual exchange (each peer encapsulates to the other, QA A-15) bound to the full transcript in Arcanum's own HKDF layer (QA D-QA-1). Low-order X25519 values are refused. Deterministic entry points sit behind the off-by-default hazmat feature, enabled only for the tests.
84 lines
2.9 KiB
Rust
84 lines
2.9 KiB
Rust
//! Typed errors. No variant carries secret material, so an error can always be logged.
|
|
|
|
use core::fmt;
|
|
|
|
/// Which input was malformed.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
#[non_exhaustive]
|
|
pub enum Input {
|
|
/// A hybrid public key (`pk_M || pk_X`).
|
|
PublicKey,
|
|
/// A hybrid secret key (the 32-byte seed).
|
|
SecretKey,
|
|
/// A hybrid ciphertext (`ct_M || ct_X`).
|
|
Ciphertext,
|
|
/// A sealed box.
|
|
SealedBox,
|
|
}
|
|
|
|
impl fmt::Display for Input {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.write_str(match self {
|
|
Input::PublicKey => "public key",
|
|
Input::SecretKey => "secret key",
|
|
Input::Ciphertext => "ciphertext",
|
|
Input::SealedBox => "sealed box",
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Everything that can go wrong in Arcanum. Every parser returns one of these; none panics.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
#[non_exhaustive]
|
|
pub enum Error {
|
|
/// The input has a fixed length and this is not it.
|
|
InvalidLength {
|
|
/// What was being parsed.
|
|
input: Input,
|
|
/// The required length in bytes.
|
|
expected: usize,
|
|
/// The length that was given.
|
|
actual: usize,
|
|
},
|
|
/// The input is shorter than the smallest valid value.
|
|
TooShort {
|
|
/// What was being parsed.
|
|
input: Input,
|
|
/// The minimum length in bytes.
|
|
minimum: usize,
|
|
/// The length that was given.
|
|
actual: usize,
|
|
},
|
|
/// The ML-KEM-768 part of a public key fails the FIPS 203 §7.2 encapsulation key check.
|
|
InvalidPublicKey,
|
|
/// An X25519 value is a low-order point, so the classical half would contribute nothing.
|
|
NonContributory,
|
|
/// A sealed box was made by a format version this build does not know.
|
|
UnsupportedVersion(u8),
|
|
/// A sealed box failed authentication: wrong key, wrong associated data, or tampered.
|
|
DecryptionFailed,
|
|
}
|
|
|
|
impl fmt::Display for Error {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Error::InvalidLength { input, expected, actual } => {
|
|
write!(f, "{input} has the wrong length: expected {expected} bytes, got {actual}")
|
|
}
|
|
Error::TooShort { input, minimum, actual } => {
|
|
write!(f, "{input} is too short: at least {minimum} bytes, got {actual}")
|
|
}
|
|
Error::InvalidPublicKey => f.write_str("public key fails the ML-KEM-768 encapsulation key check"),
|
|
Error::NonContributory => f.write_str("X25519 value is a low-order point"),
|
|
Error::UnsupportedVersion(v) => write!(f, "sealed box version {v} is not supported"),
|
|
Error::DecryptionFailed => f.write_str("sealed box could not be opened"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl core::error::Error for Error {}
|
|
|
|
pub(crate) fn exact<const N: usize>(input: Input, bytes: &[u8]) -> Result<&[u8; N], Error> {
|
|
bytes.try_into().map_err(|_| Error::InvalidLength { input, expected: N, actual: bytes.len() })
|
|
}
|