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
+5 -3
View File
@@ -43,12 +43,14 @@ fn flipping_any_byte_of_the_message_fails() {
fn flipping_any_byte_of_the_context_fails() {
let (sk, c, sig) = setup(51);
let vk = sk.verifying_key();
// Through the release API: every byte of the caller's label.
// Through the release API: every byte of the caller's label. Labels are `&'static` (QA S-09);
// a test may leak a few to build them at run time, which a consumer must not do.
let label = b"every-byte";
for i in 0..label.len() {
let mut l = label.to_vec();
l[i] ^= 0x01;
assert_eq!(vk.verify(MSG, &AppContext::new(&l).unwrap(), &sig), Err(Error::InvalidSignature), "label byte {i}");
let l: &'static [u8] = Box::leak(l.into_boxed_slice());
assert_eq!(vk.verify(MSG, &AppContext::new(l).unwrap(), &sig), Err(Error::InvalidSignature), "label byte {i}");
}
// Through the raw interface: every byte of the whole ctx, Arcanum's prefix included.
let full = c.as_bytes().to_vec();
@@ -148,7 +150,7 @@ fn a_key_binding_never_verifies_as_an_app_message_and_back() {
assert_eq!(sign::verify_composite_raw(vk, &msg, KEM_KEY_CONTEXT, &binding), Ok(()), "documented encoding");
// As an application message, under any application label: refused.
for label in [&b"kem-key"[..], b"", b"domi/arcanum/v2/sig/kem-key", b"x"] {
for label in [b"kem-key".as_slice(), b"", b"domi/arcanum/v2/sig/kem-key", b"x"] {
if let Ok(c) = AppContext::new(label) {
assert_eq!(vk.verify(&msg, &c, &binding), Err(Error::InvalidSignature), "label {label:?}");
}