Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / appstore / src / main / rust / tests / tamper_variants.rs
tamper_variants.rs80 lines · 2.6 KiBRaw
| 1 | //! Tamper variants for the Accrescent signify trust anchor. |
| 2 | //! |
| 3 | //! Extends `src/tests.rs` without duplicating it: the existing suite flips byte 3 and the final |
| 4 | //! bytes, checks one truncation shape per input and one oversized shape per input. These tests |
| 5 | //! cover the middle of the message and signature, an all-zero key, an oversized message, and a |
| 6 | //! sweep over garbage lengths proving `verify` fails closed instead of panicking. |
| 7 | |
| 8 | use appstore_signify::verify; |
| 9 | use base64::engine::general_purpose::STANDARD; |
| 10 | use base64::Engine as _; |
| 11 | |
| 12 | /// The exact bytes that were signed. |
| 13 | const REPODATA: &[u8] = include_bytes!("../testvectors/repodata.1.json"); |
| 14 | |
| 15 | /// The full `.sig` file: an `untrusted comment:` line plus the base64 blob. |
| 16 | const SIG_FILE: &str = include_str!("../testvectors/repodata.1.json.sig"); |
| 17 | |
| 18 | /// Must stay equal to `AccrescentRepo.REPODATA_PUBKEY`. |
| 19 | const PUBKEY_B64: &str = "RWT8aZ/NdUmXCPqQ0we7UyCe34q1xRfncBFVK5dI3ok9BkL1bFF3mgh3"; |
| 20 | |
| 21 | /// `2-byte algo id || 8-byte key id || 32-byte ed25519 public key`. |
| 22 | fn pubkey() -> Vec<u8> { |
| 23 | STANDARD.decode(PUBKEY_B64).unwrap()[10..42].to_vec() |
| 24 | } |
| 25 | |
| 26 | /// `2-byte algo id || 8-byte key id || 64-byte ed25519 signature`. |
| 27 | fn signature() -> Vec<u8> { |
| 28 | let blob = SIG_FILE |
| 29 | .lines() |
| 30 | .map(str::trim) |
| 31 | .filter(|l| !l.is_empty() && !l.starts_with("untrusted comment:")) |
| 32 | .last() |
| 33 | .unwrap(); |
| 34 | STANDARD.decode(blob).unwrap()[10..74].to_vec() |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn rejects_bit_flip_in_middle_of_message() { |
| 39 | let mut tampered = REPODATA.to_vec(); |
| 40 | let mid = tampered.len() / 2; |
| 41 | tampered[mid] ^= 0x40; |
| 42 | assert!(!verify(&pubkey(), &tampered, &signature())); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn rejects_bit_flip_in_middle_of_signature() { |
| 47 | let mut sig = signature(); |
| 48 | sig[32] ^= 0x40; |
| 49 | assert!(!verify(&pubkey(), REPODATA, &sig)); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn rejects_all_zero_public_key() { |
| 54 | assert!(!verify(&[0u8; 32], REPODATA, &signature())); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn rejects_message_with_appended_byte() { |
| 59 | let mut longer = REPODATA.to_vec(); |
| 60 | longer.push(b'\n'); |
| 61 | assert!(!verify(&pubkey(), &longer, &signature())); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn rejects_garbage_lengths_without_panicking() { |
| 66 | let (pk, sig) = (pubkey(), signature()); |
| 67 | for key_len in [0usize, 1, 31, 33, 64] { |
| 68 | let key = vec![0xA5u8; key_len]; |
| 69 | assert!(!verify(&key, REPODATA, &sig), "key len {key_len}"); |
| 70 | } |
| 71 | for sig_len in [0usize, 1, 63, 65, 128] { |
| 72 | let bad_sig = vec![0x5Au8; sig_len]; |
| 73 | assert!(!verify(&pk, REPODATA, &bad_sig), "sig len {sig_len}"); |
| 74 | } |
| 75 | for msg_len in [0usize, 1, 16] { |
| 76 | let msg = vec![0x00u8; msg_len]; |
| 77 | assert!(!verify(&pk, &msg, &sig), "msg len {msg_len}"); |
| 78 | } |
| 79 | assert!(!verify(&pk, &[], &sig)); |
| 80 | } |