fix: AppContext::new is a const fn over a &'static label (QA S-09)

A label is one fixed string per purpose; taking any runtime bytes let a
consumer derive it from a peer name, a file name or a message field, and
two purposes could then collide. AppContext::new now takes
&'static [u8] and is a const fn, so contexts are built as consts and a bad
label fails the consumer's build; there is no runtime constructor (the
simplest API: one constructor, no escape hatch until a need appears).

Tests: a const OK and a const EMPTY context; sig_hygiene refuses a
consumer that passes a runtime Vec (E0597/E0716); the one test that needs
runtime labels (flipping label bytes) leaks them on purpose. The random
labels in sig_malformed and fuzz mode 5's AppContext part are dropped (the
raw-ctx part of mode 5 stays). A doctest shows the const pattern, and
docs/02_signatures.md says so.
This commit is contained in:
2026-11-26 03:45:39 +01:00
parent 2329eb0719
commit abcc375741
7 changed files with 50 additions and 16 deletions
+26 -4
View File
@@ -85,6 +85,19 @@ const _: () = assert!(SIGNATURE_LEN == ML_DSA_SIGNATURE_LEN + ed25519_dalek::SIG
/// An application context: [`APP_CONTEXT_PREFIX`] followed by the caller's label, 25 to 255 bytes
/// in all. It can only be built with that prefix, so a caller cannot pass an empty context, the
/// key-binding context or a foreign one by accident.
///
/// The label is `&'static` (QA S-09): one fixed label per purpose, written in the source, never
/// derived from a peer name, a file name or a message field (two purposes whose labels came from
/// data could collide). Build it once, at compile time:
///
/// ```
/// use arcanum::sign::AppContext;
/// const PLANS: AppContext = match AppContext::new(b"plans") {
/// Ok(ctx) => ctx,
/// Err(_) => panic!("label is 1 to 231 bytes"),
/// };
/// assert_eq!(PLANS.as_bytes(), b"domi/arcanum/v2/sig/app/plans");
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct AppContext {
bytes: [u8; MAX_CONTEXT_LEN],
@@ -92,12 +105,13 @@ pub struct AppContext {
}
impl AppContext {
/// `domi/arcanum/v2/sig/app/` + `label`. Use one fixed label per purpose (Day 3: `plans`).
/// `domi/arcanum/v2/sig/app/` + `label`, for a fixed, compile-time label (Day 3: `plans`).
/// A `const fn`, so a `const` context is checked when the consumer compiles.
///
/// # Errors
/// [`Error::TooShort`] for an empty label; [`Error::TooLong`] for one over
/// [`MAX_APP_LABEL_LEN`] (231) bytes.
pub fn new(label: &[u8]) -> Result<Self, Error> {
pub const fn new(label: &'static [u8]) -> Result<Self, Error> {
if label.is_empty() {
return Err(Error::TooShort { input: Input::Context, minimum: 1, actual: 0 });
}
@@ -106,8 +120,16 @@ impl AppContext {
}
let mut bytes = [0u8; MAX_CONTEXT_LEN];
let p = APP_CONTEXT_PREFIX.len();
bytes[..p].copy_from_slice(APP_CONTEXT_PREFIX);
bytes[p..p + label.len()].copy_from_slice(label);
let mut i = 0;
while i < p {
bytes[i] = APP_CONTEXT_PREFIX[i];
i += 1;
}
let mut j = 0;
while j < label.len() {
bytes[p + j] = label[j];
j += 1;
}
Ok(Self { bytes, len: p + label.len() })
}