feat: X-Wing (draft -11), mutual exchange, WireGuard PSK per epoch, sealed box
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.
This commit is contained in:
+121
@@ -0,0 +1,121 @@
|
||||
//! One-shot sealed box to a recipient's hybrid public key, for Custos sealing and Tabellarius.
|
||||
//!
|
||||
//! ```text
|
||||
//! sealed = 0x01 || ct (1120 B) || ChaCha20-Poly1305(key, nonce, plaintext, aad = 0x01) || tag
|
||||
//! th = SHA-256("domi/arcanum/v1/transcript/seal" || pk || ct || u64be(len aad) || aad)
|
||||
//! prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss || th)
|
||||
//! key = HKDF-Expand(prk, "domi/arcanum/v1/seal/key", 32)
|
||||
//! nonce = HKDF-Expand(prk, "domi/arcanum/v1/seal/nonce", 12)
|
||||
//! ```
|
||||
//!
|
||||
//! Every seal runs a fresh encapsulation, so a key and nonce pair is never used twice.
|
||||
//!
|
||||
//! **Anyone who knows the recipient's public key can seal.** There is no sender authentication,
|
||||
//! and ChaCha20-Poly1305 is not key-committing: a successful [`open`] says nothing about who
|
||||
//! sealed. This is a documented construction, not HPKE (RFC 9180).
|
||||
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use chacha20poly1305::aead::AeadInOut;
|
||||
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, Nonce};
|
||||
use rand_core::CryptoRng;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::error::{Error, Input};
|
||||
use crate::schedule::{LABEL_SEAL_KEY, LABEL_SEAL_NONCE, Prk, expand, seal_prk};
|
||||
use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, ENCAPSULATION_SEED_LEN, PublicKey, SecretKey};
|
||||
|
||||
/// The format version written as the first byte.
|
||||
pub const VERSION: u8 = 1;
|
||||
const TAG_LEN: usize = 16;
|
||||
const HEADER_LEN: usize = 1 + CIPHERTEXT_LEN;
|
||||
/// Bytes a sealed box adds to the plaintext: version, ciphertext and the Poly1305 tag.
|
||||
pub const OVERHEAD: usize = HEADER_LEN + TAG_LEN;
|
||||
|
||||
fn cipher(prk: &Prk) -> (ChaCha20Poly1305, Nonce) {
|
||||
let key = expand::<32>(prk, &[LABEL_SEAL_KEY]);
|
||||
let nonce = expand::<12>(prk, &[LABEL_SEAL_NONCE]);
|
||||
// Infallible: the key is exactly 32 bytes.
|
||||
let aead = ChaCha20Poly1305::new_from_slice(key.as_slice()).expect("32-byte key");
|
||||
(aead, Nonce::from(*nonce))
|
||||
}
|
||||
|
||||
/// Seal `plaintext` to `recipient`, bound to `aad` (which is not encrypted and must be given again
|
||||
/// to open).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`Error::NonContributory`] if the recipient's X25519 key is a low-order point.
|
||||
pub fn seal<R: CryptoRng + ?Sized>(
|
||||
recipient: &PublicKey,
|
||||
plaintext: &[u8],
|
||||
aad: &[u8],
|
||||
rng: &mut R,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let mut eseed = Zeroizing::new([0u8; ENCAPSULATION_SEED_LEN]);
|
||||
rng.fill_bytes(eseed.as_mut_slice());
|
||||
seal_inner(recipient, plaintext, aad, &eseed)
|
||||
}
|
||||
|
||||
/// [`seal`] with explicit randomness. **Hazmat** (feature `hazmat`), for tests: reusing an
|
||||
/// `eseed` reuses key and nonce.
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`seal`].
|
||||
#[cfg(feature = "hazmat")]
|
||||
pub fn seal_derand(
|
||||
recipient: &PublicKey,
|
||||
plaintext: &[u8],
|
||||
aad: &[u8],
|
||||
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
seal_inner(recipient, plaintext, aad, eseed)
|
||||
}
|
||||
|
||||
fn seal_inner(
|
||||
recipient: &PublicKey,
|
||||
plaintext: &[u8],
|
||||
aad: &[u8],
|
||||
eseed: &[u8; ENCAPSULATION_SEED_LEN],
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
let (ct, ss) = recipient.encapsulate_inner(eseed)?;
|
||||
let (aead, nonce) = cipher(&seal_prk(&ss, recipient, &ct, aad));
|
||||
|
||||
// Allocated once at its final size, so no buffer that held plaintext is ever reallocated.
|
||||
let mut out = Vec::with_capacity(plaintext.len() + OVERHEAD);
|
||||
out.push(VERSION);
|
||||
out.extend_from_slice(ct.as_bytes());
|
||||
out.extend_from_slice(plaintext);
|
||||
// ChaCha20-Poly1305 refuses only messages over 2^38 bytes; that is reported, not a panic.
|
||||
let tag = aead
|
||||
.encrypt_inout_detached(&nonce, &[VERSION], (&mut out[HEADER_LEN..]).into())
|
||||
.map_err(|_| Error::DecryptionFailed)?;
|
||||
out.extend_from_slice(tag.as_slice());
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Open a sealed box with the recipient's secret key and the same `aad`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`Error::TooShort`], [`Error::UnsupportedVersion`], [`Error::NonContributory`], or
|
||||
/// [`Error::DecryptionFailed`] for a wrong key, wrong `aad`, or any tampering.
|
||||
pub fn open(recipient: &SecretKey, sealed: &[u8], aad: &[u8]) -> Result<Zeroizing<Vec<u8>>, Error> {
|
||||
if sealed.len() < OVERHEAD {
|
||||
return Err(Error::TooShort { input: Input::SealedBox, minimum: OVERHEAD, actual: sealed.len() });
|
||||
}
|
||||
let (header, rest) = sealed.split_at(HEADER_LEN);
|
||||
let (&version, ct) = header.split_first().ok_or(Error::DecryptionFailed)?;
|
||||
if version != VERSION {
|
||||
return Err(Error::UnsupportedVersion(version));
|
||||
}
|
||||
let ct = Ciphertext::from_bytes(ct)?;
|
||||
let ss = recipient.decapsulate(&ct)?;
|
||||
let (aead, nonce) = cipher(&seal_prk(&ss, recipient.public_key(), &ct, aad));
|
||||
|
||||
let (body, tag) = rest.split_at(rest.len() - TAG_LEN);
|
||||
let tag = chacha20poly1305::Tag::try_from(tag).map_err(|_| Error::DecryptionFailed)?;
|
||||
// The tag is checked before anything is decrypted; on failure the buffer holds only ciphertext.
|
||||
let mut plaintext = Zeroizing::new(body.to_vec());
|
||||
aead.decrypt_inout_detached(&nonce, &[VERSION], plaintext.as_mut_slice().into(), &tag)
|
||||
.map_err(|_| Error::DecryptionFailed)?;
|
||||
Ok(plaintext)
|
||||
}
|
||||
Reference in New Issue
Block a user