5a52854ccc
arcanum::sign, as docs/02_signatures.md sets out. M' = Prefix || Label || u8(len ctx) || ctx || SHA-512(M), built on the stack. The ML-DSA-65 half is ExpandedSigningKey::sign_randomized(M', ctx = Label, rng), hedged; the Ed25519 half is pure Ed25519 over M' (try_sign), checked with verify_strict. Verification computes both halves before answering, and returns one InvalidSignature whichever failed. ml-dsa's Signer/Verifier traits, sign_deterministic and sign_mu_deterministic are never called. - SigningKey: two 32-byte seeds (RFC 10073's private key format), the expanded ML-DSA key boxed; ZeroizeOnDrop, not Clone, Debug redacted. - VerifyingKey: 1984 bytes; refuses an Ed25519 half that is not a point, not its canonical encoding, or of small order (InvalidVerifyingKey). - AppContext: only "domi/arcanum/v2/sig/app/" + a 1..=231-byte label. - bind_kem_key / verify_binding: an X-Wing public key bound to a key id and an inclusive validity window under "domi/arcanum/v2/sig/kem-key"; the message is hashed field by field; window and id are checked first, then the signature, then now (InvalidValidity, TooLong, InvalidSignature, OutsideValidity). - sign_composite_raw / verify_composite_raw: the bare RFC ctx, hazmat only. They still take an RNG (the ML-DSA half stays hedged), not the deterministic variant the doc mentions. - Error gains TooLong, InvalidVerifyingKey, InvalidSignature, SigningFailed, InvalidValidity, OutsideValidity; Input gains SigningKey, VerifyingKey, Signature, Context, KeyId. Also fixes tests/kat_mldsa65.rs, which passed a &[&Vec<u8>] to sign_internal and did not compile. cargo test (debug): 121 passed, 0 failed (82 from v0.1.0, golden_v1 unchanged). clippy --all-targets --all-features -D warnings: clean.
131 lines
5.2 KiB
Rust
131 lines
5.2 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,
|
|
/// A composite signing key (ML-DSA-65 seed || Ed25519 seed).
|
|
SigningKey,
|
|
/// A composite verifying key (ML-DSA-65 public key || Ed25519 public key).
|
|
VerifyingKey,
|
|
/// A composite signature (ML-DSA-65 signature || Ed25519 signature).
|
|
Signature,
|
|
/// A signature context, or the caller's label inside one.
|
|
Context,
|
|
/// The key id in a key binding.
|
|
KeyId,
|
|
}
|
|
|
|
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",
|
|
Input::SigningKey => "signing key",
|
|
Input::VerifyingKey => "verifying key",
|
|
Input::Signature => "signature",
|
|
Input::Context => "signature context",
|
|
Input::KeyId => "key id",
|
|
})
|
|
}
|
|
}
|
|
|
|
/// 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 input is longer than the largest valid value.
|
|
TooLong {
|
|
/// What was being parsed.
|
|
input: Input,
|
|
/// The maximum length in bytes.
|
|
maximum: 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,
|
|
/// The plaintext is longer than ChaCha20-Poly1305 allows (about 256 GiB).
|
|
MessageTooLong,
|
|
/// The Ed25519 half of a verifying key is not a canonically encoded point, or is a point of
|
|
/// small order (a weak key).
|
|
InvalidVerifyingKey,
|
|
/// A signature does not verify: one half, the other, or both (deliberately not said which).
|
|
InvalidSignature,
|
|
/// ML-DSA signing failed (the randomness source or the rejection-sampling bound). Retry with
|
|
/// fresh randomness.
|
|
SigningFailed,
|
|
/// A key binding's validity window ends before it starts (`not_before > not_after`).
|
|
InvalidValidity,
|
|
/// A key binding is authentic, but `now` lies outside its validity window.
|
|
OutsideValidity,
|
|
}
|
|
|
|
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::TooLong { input, maximum, actual } => {
|
|
write!(f, "{input} is too long: at most {maximum} 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"),
|
|
Error::MessageTooLong => f.write_str("message is too long to seal"),
|
|
Error::InvalidVerifyingKey => f.write_str("verifying key has a weak or non-canonical Ed25519 point"),
|
|
Error::InvalidSignature => f.write_str("signature does not verify"),
|
|
Error::SigningFailed => f.write_str("signing failed; retry with fresh randomness"),
|
|
Error::InvalidValidity => f.write_str("key binding validity ends before it starts"),
|
|
Error::OutsideValidity => f.write_str("key binding is not valid at this time"),
|
|
}
|
|
}
|
|
}
|
|
|
|
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() })
|
|
}
|