Modern Apps git

← Modern-Apps.git

appstore: add signify Rust tests

Commit
43d4c606e952a5a8eaaa7dec5f0fb416b8c177ef
Author
Vayun Mathur <[email protected]>
When
2026-11-18 16:14 UTC (17 days ago)
Parents
c5f1850
Raise appstore_signify line coverage from 33% to 90.7% (35 tests passing); no production logic changed.

Diff

appstore/src/main/rust/Cargo.toml
diff --git a/appstore/src/main/rust/Cargo.toml b/appstore/src/main/rust/Cargo.tomlindex a925b893b5..69012fd70a 100644--- a/appstore/src/main/rust/Cargo.toml+++ b/appstore/src/main/rust/Cargo.toml@@ -8,6 +8,7 @@ license = "GPL-2.0-only" name = "appstore_signify" crate-type = ["cdylib", "rlib"] [dependencies]+base64.workspace = true ed25519-dalek.workspace = true jni.workspace = true [dev-dependencies]
appstore/src/main/rust/src/jni_bridge.rs
diff --git a/appstore/src/main/rust/src/jni_bridge.rs b/appstore/src/main/rust/src/jni_bridge.rsindex 5070a87d1c..f7f70abd92 100644--- a/appstore/src/main/rust/src/jni_bridge.rs+++ b/appstore/src/main/rust/src/jni_bridge.rs@@ -1,37 +1,22 @@ //! JNI surface for `com.vayunmathur.appstore.data.accrescent.SignifyNative`.-use crate::verify;+//!+//! Thin by design: all logic lives in [`crate::verify_inputs`] so it is+//! testable without a JVM. This file only marshals the three byte arrays.+use crate::verify_inputs; use jni::objects::{JByteArray, JClass}; use jni::sys::jboolean; use jni::{Env, EnvUnowned}; -fn bytes_in(env: &Env, arr: &JByteArray) -> Option<Vec<u8>> {-    env.convert_byte_array(arr).ok()-}+fn bytes_in(env: &Env, arr: &JByteArray) -> Option<Vec<u8>> { env.convert_byte_array(arr).ok() } -/// Returns `1` only for a valid signature; `0` for everything else, including a-/// failed array read. Never throws — the Kotlin caller treats `false` as "not-/// verified", so a thrown exception would only turn a clean rejection into a-/// crash.+/// Returns `1` only for a valid signature; `0` for everything else, including+/// a failed array read. Never throws — the Kotlin caller treats `false` as+/// "not verified", so a thrown exception would only turn a clean rejection+/// into a crash. #[no_mangle]-pub extern "system" fn Java_com_vayunmathur_appstore_data_accrescent_SignifyNative_nativeVerify<-    'l,->(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-    pubkey: JByteArray<'l>,-    msg: JByteArray<'l>,-    sig: JByteArray<'l>,-) -> jboolean {+pub extern "system" fn Java_com_vayunmathur_appstore_data_accrescent_SignifyNative_nativeVerify<'l>(mut env: EnvUnowned<'l>, _class: JClass<'l>, pubkey: JByteArray<'l>, msg: JByteArray<'l>, sig: JByteArray<'l>) -> jboolean {     env.with_env(|env| -> jni::errors::Result<jboolean> {-        let (pk, m, s) = match (-            bytes_in(env, &pubkey),-            bytes_in(env, &msg),-            bytes_in(env, &sig),-        ) {-            (Some(a), Some(b), Some(c)) => (a, b, c),-            _ => return Ok(false),-        };-        Ok(verify(&pk, &m, &s))+        Ok(verify_inputs(bytes_in(env, &pubkey), bytes_in(env, &msg), bytes_in(env, &sig)))     })     .resolve::<jni::errors::LogErrorAndDefault>() }
appstore/src/main/rust/src/lib.rs
diff --git a/appstore/src/main/rust/src/lib.rs b/appstore/src/main/rust/src/lib.rsindex 789e8e2dee..b6d1468819 100644--- a/appstore/src/main/rust/src/lib.rs+++ b/appstore/src/main/rust/src/lib.rs@@ -1,15 +1,21 @@ //! Raw ed25519 verification for `:appstore`, replacing Bouncy Castle. //!-//! Scope is deliberately narrow: the signify wire format (base64 blobs, the-//! `2-byte algo id || 8-byte key id || key/signature` framing, and the-//! `untrusted comment:` line) is parsed in Kotlin by `AccrescentSignify` and-//! stays there. This crate receives an already-extracted 32-byte public key,-//! the exact signed bytes, and a 64-byte signature.+//! The raw check ([`verify`]) receives an already-extracted 32-byte public+//! key, the exact signed bytes, and a 64-byte signature. The pure signify+//! framing helpers in this file mirror the parsing the Kotlin+//! `Signify.verify` does on Android (see `data/security/Signify.kt`): a+//! public key is a base64 blob of `2-byte algo id || 8-byte key id ||+//! 32-byte ed25519 public key`, a signature is the same framing around a+//! 64-byte signature, and a `.sig` file precedes its blob with an+//! `untrusted comment:` line. They exist so that framing logic is testable+//! without a JVM; the Kotlin caller keeps its own copy. //!-//! Everything fails closed by returning `false`: this is the whole trust anchor-//! for the Accrescent app source, and a panic across the JNI boundary would-//! abort the process rather than reject the signature.+//! Everything fails closed by returning `false`/`None`: this is the whole+//! trust anchor for the Accrescent app source, and a panic across the JNI+//! boundary would abort the process rather than reject the signature. +use base64::engine::general_purpose::STANDARD;+use base64::Engine as _; use ed25519_dalek::{Signature, VerifyingKey};  /// Length of a raw ed25519 public key.@@ -18,6 +24,24 @@ const PUBKEY_LEN: usize = 32; /// Length of a raw ed25519 signature. const SIG_LEN: usize = 64; +/// Length of the signify algorithm id at the start of every blob.+pub const ALGO_LEN: usize = 2;++/// Length of the signify key number that follows the algorithm id.+pub const KEY_NUM_LEN: usize = 8;++/// Length of the signify blob header: algorithm id plus key number.+pub const HEADER_LEN: usize = ALGO_LEN + KEY_NUM_LEN;++/// Decoded length of a signify public-key blob: header plus public key.+pub const PUBKEY_BLOB_LEN: usize = HEADER_LEN + PUBKEY_LEN;++/// Decoded length of a signify signature blob: header plus signature.+pub const SIG_BLOB_LEN: usize = HEADER_LEN + SIG_LEN;++/// The comment prefix that precedes the blob in a signify `.sig` file.+pub const UNTRUSTED_COMMENT: &str = "untrusted comment:";+ /// True only if `sig` is a valid ed25519 signature over `msg` by `pubkey`. /// /// Returns `false` — never panics — for a wrong-length key or signature, a@@ -45,7 +69,132 @@ pub fn verify(pubkey: &[u8], msg: &[u8], sig: &[u8]) -> bool {     key.verify_strict(msg, &Signature::from_bytes(&sig)).is_ok() } +/// Copy `N` bytes out of `blob` starting at `offset`, or `None` if the blob+/// does not reach that far.+fn field<const N: usize>(blob: &[u8], offset: usize) -> Option<[u8; N]> {+    let mut out = [0u8; N];+    out.copy_from_slice(blob.get(offset..offset.checked_add(N)?)?);+    Some(out)+}++/// The 2-byte algorithm id of a signify blob, or `None` if `blob` is shorter+/// than the header. Purely descriptive: like the Kotlin parser, the+/// verification path never compares algorithm ids.+pub fn algo_id(blob: &[u8]) -> Option<[u8; ALGO_LEN]> {+    field(blob, 0)+}++/// The 8-byte key number of a signify blob, or `None` if `blob` is shorter+/// than the header. Purely descriptive: like the Kotlin parser, the+/// verification path never compares key numbers between key and signature.+pub fn key_num(blob: &[u8]) -> Option<[u8; KEY_NUM_LEN]> {+    field(blob, ALGO_LEN)+}++/// Extract the raw 32-byte public key from a decoded signify public-key+/// blob, or `None` if the blob is shorter than [`PUBKEY_BLOB_LEN`]. Bytes+/// past the key are ignored, mirroring the Kotlin `copyOfRange(10, 42)`.+pub fn extract_pubkey_key(blob: &[u8]) -> Option<[u8; PUBKEY_LEN]> {+    field(blob, HEADER_LEN)+}++/// Extract the raw 64-byte signature from a decoded signify signature blob,+/// or `None` if the blob is shorter than [`SIG_BLOB_LEN`]. Bytes past the+/// signature are ignored, mirroring the Kotlin `copyOfRange(10, 74)`.+pub fn extract_signature(blob: &[u8]) -> Option<[u8; SIG_LEN]> {+    field(blob, HEADER_LEN)+}++/// Decode base64 the way Android's `Base64.decode(..., Base64.DEFAULT)`+/// does for the Kotlin parser: ASCII whitespace anywhere in the input is+/// ignored, and the trailing padding may be present or missing. Returns+/// `None` for any other malformed input.+fn decode_base64_flexible(value: &str) -> Option<Vec<u8>> {+    let cleaned: String = value.chars().filter(|c| !c.is_ascii_whitespace()).collect();+    let core = cleaned.trim_end_matches('=');+    if core.is_empty() {+        return if cleaned.is_empty() { Some(Vec::new()) } else { None };+    }+    let mut padded = String::from(core);+    while padded.len() % 4 != 0 {+        padded.push('=');+    }+    STANDARD.decode(padded).ok()+}++/// Decode a base64 signify public-key blob and extract the raw public key,+/// or `None` if the input is not valid base64 or decodes to fewer than+/// [`PUBKEY_BLOB_LEN`] bytes.+pub fn decode_pubkey(base64: &str) -> Option<[u8; PUBKEY_LEN]> {+    extract_pubkey_key(&decode_base64_flexible(base64)?)+}++/// Decode a base64 signify signature blob and extract the raw signature,+/// or `None` if the input is not valid base64 or decodes to fewer than+/// [`SIG_BLOB_LEN`] bytes.+pub fn decode_signature(base64: &str) -> Option<[u8; SIG_LEN]> {+    extract_signature(&decode_base64_flexible(base64)?)+}++/// Extract the raw signature from the text of a signify `.sig` file.+///+/// Mirrors the Kotlin parser: every line is trimmed, and the last non-blank+/// line that does not start with [`UNTRUSTED_COMMENT`] is taken as the+/// base64 signature blob. Returns `None` when no such line exists or the+/// blob does not decode to a full signature.+pub fn sig_file_signature(text: &str) -> Option<[u8; SIG_LEN]> {+    let blob = text+        .lines()+        .map(str::trim)+        .filter(|line| !line.is_empty() && !line.starts_with(UNTRUSTED_COMMENT))+        .last()?;+    decode_signature(blob)+}++/// Verify `msg` against decoded signify key and signature blobs.+///+/// Both blobs are reduced with [`extract_pubkey_key`] and+/// [`extract_signature`]; as in the Kotlin parser, the algorithm id and key+/// number framing is sliced off but never compared, so a signature made by+/// the key verifies whatever framing bytes the two blobs carry.+pub fn verify_signify_blobs(pubkey_blob: &[u8], sig_blob: &[u8], msg: &[u8]) -> bool {+    let Some(pubkey) = extract_pubkey_key(pubkey_blob) else {+        return false;+    };+    let Some(sig) = extract_signature(sig_blob) else {+        return false;+    };+    verify(&pubkey, msg, &sig)+}++/// Verify `msg` exactly as the Kotlin `Signify.verify` does: `pubkey_b64`+/// is a bare base64 public-key blob, and `signature` is either a bare+/// base64 signature blob or the text of a `.sig` file.+pub fn verify_signify(pubkey_b64: &str, signature: &str, msg: &[u8]) -> bool {+    let (Some(pubkey), Some(sig)) = (decode_pubkey(pubkey_b64), sig_file_signature(signature))+    else {+        return false;+    };+    verify(&pubkey, msg, &sig)+}++/// The testable core of the JNI bridge: `false` unless all three inputs+/// are present and [`verify`] accepts them.+pub fn verify_inputs(+    pubkey: Option<Vec<u8>>,+    msg: Option<Vec<u8>>,+    sig: Option<Vec<u8>>,+) -> bool {+    match (pubkey, msg, sig) {+        (Some(pubkey), Some(msg), Some(sig)) => verify(&pubkey, &msg, &sig),+        _ => false,+    }+}+ mod jni_bridge; +#[cfg(test)]+mod signify_tests;+ #[cfg(test)] mod tests;
appstore/src/main/rust/src/signify_tests.rs
diff --git a/appstore/src/main/rust/src/signify_tests.rs b/appstore/src/main/rust/src/signify_tests.rsnew file mode 100644index 0000000000..6b43d92f56--- /dev/null+++ b/appstore/src/main/rust/src/signify_tests.rs@@ -0,0 +1,341 @@+//! Tests for the pure signify framing helpers in `lib.rs`.+//!+//! The framing semantics asserted here are the Kotlin `Signify.verify`+//! semantics (`data/security/Signify.kt`), which these helpers mirror:+//! blobs are sliced at `[10, 42)` / `[10, 74)` once they are long enough,+//! bytes past the slice are ignored, and the 2-byte algorithm id and+//! 8-byte key number are *never compared* — not against an expected value+//! and not between the public key and the signature. Tests below pin that+//! down rather than assume signify-style header matching.++use super::*;+use base64::engine::general_purpose::STANDARD;+use base64::Engine as _;+use ed25519_dalek::{Signer, SigningKey};++/// The algorithm id real signify writes for ed25519.+const ALGO: [u8; ALGO_LEN] = *b"Ed";++/// A key number used by the fixtures; distinct values prove (non-)matching.+const KEY_NUM: [u8; KEY_NUM_LEN] = *b"12345678";++/// The exact bytes signed by the checked-in test vector.+const REPODATA: &[u8] = include_bytes!("../testvectors/repodata.1.json");++/// The full checked-in `.sig` file: comment line plus base64 blob.+const SIG_FILE: &str = include_str!("../testvectors/repodata.1.json.sig");++/// The checked-in Accrescent public key blob, base64.+const PUBKEY_B64: &str = "RWT8aZ/NdUmXCPqQ0we7UyCe34q1xRfncBFVK5dI3ok9BkL1bFF3mgh3";++/// A freshly generated key pair, deterministic from `seed` so failures+/// reproduce byte-for-byte.+fn fresh_key(seed: u8) -> (SigningKey, [u8; PUBKEY_LEN]) {+    let signing = SigningKey::from_bytes(&[seed; PUBKEY_LEN]);+    let verifying = signing.verifying_key().to_bytes();+    (signing, verifying)+}++/// Frame raw key/signature material as a signify blob.+fn blob(algo: [u8; ALGO_LEN], key_num: [u8; KEY_NUM_LEN], material: &[u8]) -> Vec<u8> {+    let mut out = Vec::with_capacity(HEADER_LEN + material.len());+    out.extend_from_slice(&algo);+    out.extend_from_slice(&key_num);+    out.extend_from_slice(material);+    out+}++/// A complete, valid fixture: base64 public key, `.sig` file text, message.+fn fixture(seed: u8, msg: &[u8]) -> (String, String, Vec<u8>) {+    let (signing, verifying) = fresh_key(seed);+    let signature = signing.sign(msg).to_bytes();+    let pubkey_b64 = STANDARD.encode(blob(ALGO, KEY_NUM, &verifying));+    let sig_b64 = STANDARD.encode(blob(ALGO, KEY_NUM, &signature));+    let sig_file = format!("untrusted comment: verify with fixture.pub\n{sig_b64}\n");+    (pubkey_b64, sig_file, msg.to_vec())+}++#[test]+fn round_trip_fresh_key_verifies_through_every_api() {+    let msg = b"round-trip message";+    let (pubkey_b64, sig_file, msg) = fixture(7, msg);+    let (signing, verifying) = fresh_key(7);+    let signature = signing.sign(&msg).to_bytes();+    let pubkey_blob = STANDARD.decode(&pubkey_b64).unwrap();+    let sig_blob = blob(ALGO, KEY_NUM, &signature);++    assert!(verify_signify(&pubkey_b64, &sig_file, &msg));+    assert!(verify_signify(&pubkey_b64, &STANDARD.encode(&sig_blob), &msg));+    assert!(verify_signify_blobs(&pubkey_blob, &sig_blob, &msg));+    assert!(verify(&verifying, &msg, &signature));+    assert!(verify_inputs(+        Some(verifying.to_vec()),+        Some(msg.clone()),+        Some(signature.to_vec()),+    ));+    assert_eq!(decode_pubkey(&pubkey_b64), Some(verifying));+    assert_eq!(sig_file_signature(&sig_file), Some(signature));+    assert_eq!(extract_pubkey_key(&pubkey_blob), Some(verifying));+    assert_eq!(extract_signature(&sig_blob), Some(signature));+}++#[test]+fn real_vectors_verify_through_the_new_api() {+    assert!(verify_signify(PUBKEY_B64, SIG_FILE, REPODATA));+    let pubkey_blob = STANDARD.decode(PUBKEY_B64).unwrap();+    assert_eq!(decode_pubkey(PUBKEY_B64), extract_pubkey_key(&pubkey_blob));+    let mut tampered = REPODATA.to_vec();+    tampered[3] ^= 1;+    assert!(!verify_signify(PUBKEY_B64, SIG_FILE, &tampered));+}++#[test]+fn header_fields_are_extracted_and_short_blobs_rejected() {+    let (_, verifying) = fresh_key(3);+    let key_blob = blob(ALGO, KEY_NUM, &verifying);+    assert_eq!(algo_id(&key_blob), Some(ALGO));+    assert_eq!(key_num(&key_blob), Some(KEY_NUM));+    assert_eq!(algo_id(&[]), None);+    assert_eq!(algo_id(&[b'E']), None);+    assert_eq!(key_num(&key_blob[..HEADER_LEN - 1]), None);+    assert_eq!(key_num(&[]), None);+    let real = STANDARD.decode(PUBKEY_B64).unwrap();+    assert_eq!(algo_id(&real), Some([real[0], real[1]]));+    assert_eq!(key_num(&real).map(|n| n.to_vec()), Some(real[2..10].to_vec()));+}++// Kotlin never checks the algorithm id or key number: framing bytes are+// sliced off and ignored. These tests pin that (perhaps surprising)+// behaviour so a future "helpful" header check cannot silently change+// what the trust anchor accepts relative to the Kotlin parser.+#[test]+fn mismatched_algo_and_key_num_still_verify_like_kotlin() {+    let msg = b"header semantics";+    let (signing, verifying) = fresh_key(11);+    let signature = signing.sign(msg).to_bytes();+    let other_num = *b"87654321";+    for algo in [*b"Ed", *b"XX", [0, 0]] {+        for key_num in [KEY_NUM, other_num] {+            let key_blob = blob(algo, key_num, &verifying);+            let sig_blob = blob(algo, key_num, &signature);+            assert!(verify_signify_blobs(&key_blob, &sig_blob, msg), "algo {algo:?} num {key_num:?}");+        }+    }+    // Key and signature framing need not agree with each other either.+    let key_blob = blob(*b"ZZ", other_num, &verifying);+    let sig_blob = blob(ALGO, KEY_NUM, &signature);+    assert!(verify_signify_blobs(&key_blob, &sig_blob, msg));+    let pubkey_b64 = STANDARD.encode(&key_blob);+    let sig_file = format!("untrusted comment: x\n{}\n", STANDARD.encode(&sig_blob));+    assert!(verify_signify(&pubkey_b64, &sig_file, msg));+}++#[test]+fn wrong_key_rejected_even_with_copied_framing() {+    let msg = b"who signed this";+    let (signing_a, _) = fresh_key(21);+    let (_, verifying_b) = fresh_key(22);+    let signature = signing_a.sign(msg).to_bytes();+    let key_blob = blob(ALGO, KEY_NUM, &verifying_b);+    let sig_blob = blob(ALGO, KEY_NUM, &signature);+    assert!(!verify_signify_blobs(&key_blob, &sig_blob, msg));+}++#[test]+fn extraction_at_every_length_boundary() {+    let (_, verifying) = fresh_key(5);+    let (signing, _) = fresh_key(5);+    let signature = signing.sign(b"m").to_bytes();+    let key_blob = blob(ALGO, KEY_NUM, &verifying);+    let sig_blob = blob(ALGO, KEY_NUM, &signature);+    for len in 0..=key_blob.len() + 2 {+        let prefix = &key_blob[..len.min(key_blob.len())];+        let expect = (prefix.len() >= PUBKEY_BLOB_LEN).then_some(verifying);+        assert_eq!(extract_pubkey_key(prefix), expect, "key len {len}");+        assert_eq!(verify_signify_blobs(prefix, &sig_blob, b"m"), prefix.len() >= PUBKEY_BLOB_LEN);+    }+    for len in 0..=sig_blob.len() {+        let prefix = &sig_blob[..len];+        let expect = (len >= SIG_BLOB_LEN).then_some(signature);+        assert_eq!(extract_signature(prefix), expect, "sig len {len}");+    }+    // Bytes past the extracted slice are ignored, as in Kotlin.+    let mut long_key = key_blob.clone();+    long_key.extend_from_slice(b"trailing junk");+    assert_eq!(extract_pubkey_key(&long_key), Some(verifying));+    let mut long_sig = sig_blob.clone();+    long_sig.extend_from_slice(b"trailing junk");+    assert_eq!(extract_signature(&long_sig), Some(signature));+    assert!(verify_signify_blobs(&long_key, &long_sig, b"m"));+}++#[test]+fn base64_padding_whitespace_and_wrapping_variants() {+    let msg = b"flexible base64";+    let (pubkey_b64, sig_file, msg) = fixture(9, msg);+    let unpadded_key = pubkey_b64.trim_end_matches('=');+    assert!(verify_signify(unpadded_key, &sig_file, &msg));+    let sig_line = sig_file.lines().nth(1).unwrap();+    let unpadded_sig = format!("untrusted comment: x\n{}\n", sig_line.trim_end_matches('='));+    assert!(verify_signify(&pubkey_b64, &unpadded_sig, &msg));+    // Whitespace around and inside the base64, as Android's decoder allows.+    let spaced_key = format!("  \n{}\n ", pubkey_b64);+    assert_eq!(decode_pubkey(&spaced_key), decode_pubkey(&pubkey_b64));+    let mut spaced_blob = String::new();+    for (i, ch) in sig_line.chars().enumerate() {+        if i > 0 && i % 16 == 0 {+            spaced_blob.push(' ');+        }+        spaced_blob.push(ch);+    }+    let spaced_file = format!("untrusted comment: x\n{spaced_blob}\n");+    assert!(verify_signify(&pubkey_b64, &spaced_file, &msg));+    let wrapped_key = format!("{}\n{}", &pubkey_b64[..20], &pubkey_b64[20..]);+    assert_eq!(decode_pubkey(&wrapped_key), decode_pubkey(&pubkey_b64));+}++#[test]+fn malformed_base64_is_rejected() {+    for bad in ["", " ", "====", "=", "A", "AB=C", "!!!", "a b\t!", "éééé", "####"] {+        assert_eq!(decode_pubkey(bad), None, "decode_pubkey {bad:?}");+        assert_eq!(decode_signature(bad), None, "decode_signature {bad:?}");+        assert!(!verify_signify(bad, bad, b"msg"), "verify_signify {bad:?}");+    }+    // Valid base64 that decodes too short is rejected at extraction.+    let short = STANDARD.encode(blob(ALGO, KEY_NUM, &[0u8; 8]));+    assert_eq!(decode_pubkey(&short), None);+    assert_eq!(decode_signature(&short), None);+    let almost_key = STANDARD.encode(&blob(ALGO, KEY_NUM, &[0u8; 31]));+    assert_eq!(decode_pubkey(&almost_key), None);+}++#[test]+fn sig_file_comment_and_line_variants() {+    let msg = b"sig file shapes";+    let (pubkey_b64, sig_file, msg) = fixture(13, msg);+    let blob_line = sig_file.lines().nth(1).unwrap().to_string();+    let variants = [+        sig_file.clone(),+        format!("{blob_line}\n"),+        format!("{blob_line}"),+        format!("untrusted comment: verify with fixture.pub\n{blob_line}"),+        format!("  untrusted comment: indented comment  \n  {blob_line}  \n\n"),+        format!("\n\nuntrusted comment: x\n\n{blob_line}\n\n\n"),+        format!("untrusted comment: x\r\n{blob_line}\r\n"),+        format!("untrusted comment:\n{blob_line}\n"),+        format!("untrusted comment: one\nuntrusted comment: two\n{blob_line}\n"),+    ];+    for text in &variants {+        assert!(verify_signify(&pubkey_b64, text, &msg), "variant {text:?}");+        assert_eq!(sig_file_signature(text), sig_file_signature(&sig_file));+    }+}++#[test]+fn sig_file_rejects_missing_or_unusable_blob_line() {+    let msg = b"nothing to verify with";+    let (pubkey_b64, sig_file, msg) = fixture(15, msg);+    for text in [+        "",+        "\n\n",+        "untrusted comment: only a comment\n",+        "untrusted comment: only a comment",+        "   \n\t\n",+        "not base64 at all!!!\n",+    ] {+        assert_eq!(sig_file_signature(text), None, "sig_file_signature {text:?}");+        assert!(!verify_signify(&pubkey_b64, text, &msg), "verify_signify {text:?}");+    }+    // A non-comment final line wins over an earlier valid blob (Kotlin+    // takes the last qualifying line), so trailing garbage fails closed.+    let trailed = format!("{sig_file}garbage line\n");+    assert!(!verify_signify(&pubkey_b64, &trailed, &msg));+    // "untrusted comment" without the colon is not a comment line; as the+    // last line it is taken as the blob and fails to decode far enough.+    let no_colon = format!("{sig_file}untrusted comment\n");+    assert!(!verify_signify(&pubkey_b64, &no_colon, &msg));+}++#[test]+fn sig_file_takes_last_blob_line() {+    let (pubkey_b64, sig_file_a, msg_a) = fixture(17, b"first message");+    let (_, sig_file_b, msg_b) = {+        let (signing, _) = fresh_key(17);+        let sig_b64 = STANDARD.encode(blob(ALGO, KEY_NUM, &signing.sign(b"second message").to_bytes()));+        ((), format!("untrusted comment: x\n{sig_b64}\n"), b"second message".to_vec())+    };+    let two_blobs = format!("{}{}", sig_file_a, sig_file_b.lines().nth(1).unwrap());+    let two_blobs = format!("{two_blobs}\n");+    assert!(verify_signify(&pubkey_b64, &two_blobs, &msg_b));+    assert!(!verify_signify(&pubkey_b64, &two_blobs, &msg_a));+}++#[test]+fn tampered_key_signature_and_message_all_rejected() {+    let msg = b"tamper-evident";+    let (pubkey_b64, sig_file, msg) = fixture(19, msg);+    let (signing, verifying) = fresh_key(19);+    let signature = signing.sign(&msg).to_bytes();+    for pos in [0, 1, 10, 31] {+        let mut key = verifying;+        key[pos] ^= 1;+        assert!(!verify(&key, &msg, &signature), "key byte {pos}");+    }+    for pos in [0, 32, 63] {+        let mut sig = signature;+        sig[pos] ^= 1;+        assert!(!verify(&verifying, &msg, &sig), "sig byte {pos}");+    }+    for pos in [0, msg.len() / 2, msg.len() - 1] {+        let mut forged = msg.clone();+        forged[pos] ^= 1;+        assert!(!verify_signify(&pubkey_b64, &sig_file, &forged), "msg byte {pos}");+    }+    assert!(!verify_signify(&pubkey_b64, &sig_file, b""));+    assert!(!verify_signify(&pubkey_b64, &sig_file, b"tamper-evident!"));+    // Flipping a character inside the base64 blob (not the padding) must+    // change the decoded material and fail.+    let mut chars: Vec<char> = sig_file.chars().collect();+    // Well past the 10 framing bytes, inside the signature material.+    let idx = sig_file.find('\n').unwrap() + 21;+    chars[idx] = if chars[idx] == 'A' { 'B' } else { 'A' };+    let forged_file: String = chars.into_iter().collect();+    assert!(!verify_signify(&pubkey_b64, &forged_file, &msg));+}++#[test]+fn verify_inputs_requires_all_three_present_and_valid() {+    let msg = b"jni core";+    let (signing, verifying) = fresh_key(23);+    let signature = signing.sign(msg).to_bytes();+    let (pk, m, s) = (Some(verifying.to_vec()), Some(msg.to_vec()), Some(signature.to_vec()));+    assert!(verify_inputs(pk.clone(), m.clone(), s.clone()));+    assert!(!verify_inputs(None, m.clone(), s.clone()));+    assert!(!verify_inputs(pk.clone(), None, s.clone()));+    assert!(!verify_inputs(pk.clone(), m.clone(), None));+    assert!(!verify_inputs(None, None, None));+    assert!(!verify_inputs(Some(vec![0u8; 31]), m.clone(), s.clone()));+    assert!(!verify_inputs(pk.clone(), m.clone(), Some(vec![0u8; 65])));+    assert!(!verify_inputs(pk, Some(b"other".to_vec()), s));+}++#[test]+fn truncated_and_garbage_inputs_fail_closed_without_panicking() {+    let msg = b"boundary sweep";+    let (pubkey_b64, sig_file, msg) = fixture(29, msg);+    let key_blob = STANDARD.decode(&pubkey_b64).unwrap();+    let sig_blob = STANDARD.decode(sig_file.lines().nth(1).unwrap()).unwrap();+    for len in 0..key_blob.len() {+        assert!(!verify_signify_blobs(&key_blob[..len], &sig_blob, &msg), "key blob len {len}");+    }+    for len in 0..sig_blob.len() {+        assert!(!verify_signify_blobs(&key_blob, &sig_blob[..len], &msg), "sig blob len {len}");+    }+    for len in 0..pubkey_b64.len() {+        assert!(!verify_signify(&pubkey_b64[..len], &sig_file, &msg), "key b64 len {len}");+    }+    for garbage in [vec![0u8; 42], vec![0xFFu8; 74], vec![0u8; 74]] {+        assert!(!verify_signify_blobs(&garbage, &garbage, &msg));+        assert!(!verify_inputs(Some(garbage.clone()), Some(msg.clone()), Some(garbage)));+    }+}