usesuper::*;const BYTES: [u8; 14] = [0x9c, 0x41, 0x07, 0xee, 0x23, 0x5a, 0xb0, 0x11, 0x7f, 0x80, 0x02, 0xd4, 0x6e, 0x39];/// A cheap stretch for the tests that are not about the stretch itself.const QUICK: Kdf = Kdf::Scrypt { log_n: 10, r: 8, p: 1 };#[test]fn a_code_is_six_groups_of_four_readable_characters() {let code = mint_with(&BYTES);let groups: Vec<&str> = code.split('-').collect(); assert_eq!(groups.len(), 6, "{code}"); assert!(groups.iter().all(|g| g.len() == 4), "{code}"); assert!(code.chars().filter(|c| *c != '-').all(|c| ALPHABET.contains(&(c as u8))), "{code}"); assert!(!code.contains(['I', 'L', 'O', 'U']), "no character that reads as another: {code}"); assert_eq!(normalize(&code).map(|n| grouped(&n)), Ok(code.clone()), "a minted code reads back as itself"); assert_ne!(mint(), mint(), "two codes from the OS are two codes");}#[test]fn reading_a_code_back_forgives_case_spacing_and_lookalikes() {let code = mint_with(&BYTES);let plain = normalize(&code).expect("minted");let spoken = code.to_lowercase().replace('-', " "); assert_eq!(normalize(&spoken), Ok(plain.clone()), "lower case, spaces");let zero = plain.find('0');let one = plain.find('1');ifletSome(at) = zero {letmut typed = plain.clone(); typed.replace_range(at..at + 1, "O"); assert_eq!(normalize(&typed), Ok(plain.clone()), "O reads as 0"); }ifletSome(at) = one {letmut typed = plain.clone(); typed.replace_range(at..at + 1, "l"); assert_eq!(normalize(&typed), Ok(plain.clone()), "l reads as 1"); }}#[test]fn a_typo_is_caught_before_it_is_tried() {let plain = normalize(&mint_with(&BYTES)).expect("minted");// Every single-character slip in the data is caught by the check (or reads as another symbol entirely).letmut caught = 0;letmut tried = 0;for at in0..DATA_CHARS {for &symbol in ALPHABET.iter() {if plain.as_bytes()[at] == symbol {continue; }letmut typed = plain.clone().into_bytes(); typed[at] = symbol; tried += 1;if normalize(std::str::from_utf8(&typed).unwrap()) == Err(Problem::Check) { caught += 1; } } } assert!(caught as f64 / tried as f64 > 0.99, "{caught} of {tried} slips caught");// Two neighbours swapped.letmut swapped = plain.clone().into_bytes();let at = (0..DATA_CHARS - 1).find(|&i| swapped[i] != swapped[i + 1]).expect("two different neighbours"); swapped.swap(at, at + 1); assert_eq!(normalize(std::str::from_utf8(&swapped).unwrap()), Err(Problem::Check)); assert_eq!(normalize(&plain[..20]), Err(Problem::Length), "too short"); assert_eq!(normalize(&format!("{plain}7")), Err(Problem::Length), "too long"); assert_eq!(normalize(&format!("{}!", &plain[..23])), Err(Problem::Character), "a character in no code");}#[test]fn the_derivation_is_fixed_by_secret_salt_and_parameters() {let code = normalize(&mint_with(&BYTES)).expect("minted");let salt = [7u8; 16];let a = derive(code.as_bytes(), &salt, QUICK).expect("derived");let b = derive(code.as_bytes(), &salt, QUICK).expect("derived"); assert_eq!(*a.wrap, *b.wrap); assert_eq!(*a.auth, *b.auth); assert_ne!(*a.wrap, *a.auth, "the key that opens is not the key that proves");let other_salt = derive(code.as_bytes(), &[8u8; 16], QUICK).expect("derived"); assert_ne!(*a.wrap, *other_salt.wrap);let harder = derive(code.as_bytes(), &salt, Kdf::Scrypt { log_n: 11, r: 8, p: 1 }).expect("derived"); assert_ne!(*a.wrap, *harder.wrap); assert_eq!(auth_hash(&a.auth).len(), 64); assert_ne!(auth_hash(&a.auth), auth_hash(&a.wrap));}#[test]fn the_stretch_a_code_is_made_with_is_stored_and_read_back() { assert_eq!(Kdf::SCRYPT.text(), "scrypt:15:8:1"); assert_eq!(Kdf::parse("scrypt:15:8:1"), Some(Kdf::SCRYPT)); assert_eq!(Kdf::parse("none"), Some(Kdf::None)); assert_eq!(Kdf::parse("scrypt:4:8:1"), None, "too weak to take"); assert_eq!(Kdf::parse("scrypt:30:8:1"), None, "too heavy for a phone"); assert_eq!(Kdf::parse("argon2"), None); assert_eq!(Kind::Code.kdf(), Kdf::SCRYPT); assert_eq!(Kind::Google.kdf(), Kdf::None); assert_eq!(Kind::Apple.kdf(), Kdf::None); assert_eq!(Kind::parse("google"), Some(Kind::Google)); assert_eq!(Kind::parse("platform"), None);let keys = derive(b"7K3Q9XWDABCDEFGH0123456789", &[1u8; 16], Kdf::SCRYPT).expect("the shipped stretch runs"); assert_eq!(keys.wrap.len(), 32);}#[test]fn a_wrap_opens_only_under_its_key_kind_and_version() {let vmk = [0x42u8; 32];let keys = derive(b"secret", &[3u8; 16], QUICK).expect("derived");let wrapped = wrap(&keys.wrap, Kind::Code, 2, &vmk).expect("wrapped"); assert!(wrapped.starts_with(WRAP_PREFIX)); assert!(!wrapped.contains(&URL_SAFE_NO_PAD.encode(vmk)), "the key is not in the wrap"); assert_eq!(unwrap(&keys.wrap, Kind::Code, 2, &wrapped).map(|k| *k), Some(vmk)); assert!(unwrap(&keys.auth, Kind::Code, 2, &wrapped).is_none(), "the auth key the object sees opens nothing"); assert!(unwrap(&keys.wrap, Kind::Google, 2, &wrapped).is_none(), "a code's wrap is no platform wrap"); assert!(unwrap(&keys.wrap, Kind::Code, 3, &wrapped).is_none(), "bound to its version");letmut tampered = wrapped.clone(); tampered.pop(); tampered.push(if wrapped.ends_with('A') { 'B' } else { 'A' }); assert!(unwrap(&keys.wrap, Kind::Code, 2, &tampered).is_none());}#[test]fn a_platform_key_needs_no_stretch() {let key = platform_key();let salt = salt();let keys = derive(key.as_ref(), &salt, Kdf::None).expect("derived");let again = derive(key.as_ref(), &salt, Kdf::None).expect("derived"); assert_eq!(*keys.wrap, *again.wrap); assert_ne!(*keys.wrap, *key, "the stored key is not itself the wrap key");}#[test]fn a_trusted_secret_splits_into_halves_that_alone_open_nothing() {let secret = platform_key();let (theirs, held) = split(&secret); assert_ne!(*theirs, *secret); assert_ne!(*held, *secret); assert_eq!(*join(&theirs, &held), *secret, "the two halves give the secret back");let (again, _) = split(&secret); assert_ne!(*again, *theirs, "each split draws a new half");let vmk = [0x42u8; 32];let salt = [4u8; 16];let keys = derive(secret.as_ref(), &salt, Kind::Trusted.kdf()).expect("derived");let wrapped = wrap(&keys.wrap, Kind::Trusted, 1, &vmk).expect("wrapped");for half in [&theirs, &held] {let alone = derive(half.as_ref(), &salt, Kdf::None).expect("derived"); assert!(unwrap(&alone.wrap, Kind::Trusted, 1, &wrapped).is_none(), "a half alone opens nothing"); assert_ne!(auth_hash(&alone.auth), auth_hash(&keys.auth), "nor proves the secret"); } assert_eq!(unwrap(&keys.wrap, Kind::Trusted, 1, &wrapped).map(|k| *k), Some(vmk)); assert_eq!(Kind::parse("trusted"), Some(Kind::Trusted)); assert_eq!(*split_with(&[7; 32], &[7; 32]).1, [0u8; 32]);}/// TRUSTED RECOVERY BUILD 1, step 1: the helper's phone verifies the asking device by six digits worked out on both/// phones from the device's key and two nonces, never a code the server relays. A key swapped in by the server, or a/// nonce other than the one committed to, gives other digits.#[test]fn the_helpers_code_is_worked_out_from_the_asking_devices_key_and_both_nonces() {let at = HelpFor { account: "+15550100", setup: "trusted", index: 0, request: "ask-1" };let (key, nonce_a, nonce_b) = ([1u8; 32], [2u8; 32], [3u8; 32]);let commit = help_commit(&at, &key, &nonce_a); assert_eq!(commit, help_commit(&at, &key, &nonce_a), "the commit is fixed by what it commits to"); assert_ne!(commit, help_commit(&at, &[9u8; 32], &nonce_a), "a swapped key breaks the commit"); assert_ne!(commit, help_commit(&at, &key, &[9u8; 32]), "so does another nonce");let code = help_sas(&at, &key, &nonce_a, &nonce_b); assert_eq!(code.len(), 6); assert!(code.chars().all(|c| c.is_ascii_digit()), "{code}"); assert_ne!(code, help_sas(&at, &[9u8; 32], &nonce_a, &nonce_b), "the device's key is in the code"); assert_ne!(code, help_sas(&at, &key, &[9u8; 32], &nonce_b)); assert_ne!(code, help_sas(&at, &key, &nonce_a, &[9u8; 32])); assert_ne!(help_commit(&at, &key, &nonce_a), crate::vault::approval::commit_of(&key, None, &nonce_a), "never an approval's commit"); assert!(help_code_matches(&code, &format!(" {} {} ", &code[..3], &code[3..])), "spaces do not matter"); assert!(!help_code_matches(&code, "000000") || code == "000000"); assert!(!help_code_matches(&code, ""));}/// TRUSTED RECOVERY BUILD 2, review M1: the digits say WHOSE recovery they approve. The person's account, the setup,/// the helper's place and the request are in the commit and the digits, so an ask the server shows for one person/// under another's name, or moves from one setup, place or request to another, gives other digits.#[test]fn the_digits_say_whose_recovery_they_approve() {let at = HelpFor { account: "+15550100", setup: "s1", index: 1, request: "r1" };let (key, nonce_a, nonce_b) = ([1u8; 32], [2u8; 32], [3u8; 32]);let others = [HelpFor { account: "+15550199", ..at },HelpFor { setup: "s2", ..at },HelpFor { index: 2, ..at },HelpFor { request: "r2", ..at },// Fields run together never collide: each is said with its length.HelpFor { account: "+1555010", setup: "0s1", ..at }, ];for other in &others { assert_ne!(help_commit(&at, &key, &nonce_a), help_commit(other, &key, &nonce_a), "{other:?}"); assert_ne!(help_sas(&at, &key, &nonce_a, &nonce_b), help_sas(other, &key, &nonce_a, &nonce_b), "{other:?}"); }}/// RULE (TRUSTED RECOVERY BUILD 1, step 4): the circle's secret is the object's half AND the contacts' part, Q, shared/// any k of n by replicated XOR: 1 of 1 (Q itself) or 2 of 3 (three parts, each contact holding the two that are not/// theirs). Any k pieces give Q back; fewer give nothing, and a piece says nothing of Q.#[test]fn the_circles_part_comes_back_from_any_k_pieces_and_never_from_fewer() {let q = [0x5au8; 32];let one = circle_pieces(&q, 1, 1).expect("1 of 1"); assert_eq!(one.len(), 1); assert_eq!(circle_join(1, 1, &[one[0].as_slice()]).map(|q| *q), Some(q));let three = circle_pieces(&q, 2, 3).expect("2 of 3"); assert_eq!(three.len(), 3);for (a, b) in [(0, 1), (0, 2), (1, 2), (2, 0)] { assert_eq!(circle_join(2, 3, &[three[a].as_slice(), three[b].as_slice()]).map(|q| *q), Some(q), "pieces {a} and {b}"); }for alone in &three { assert!(circle_join(2, 3, &[alone.as_slice()]).is_none(), "one piece alone gives nothing"); assert!(!alone.windows(32).any(|w| w == q), "a piece never carries Q itself"); } assert!(circle_join(2, 3, &[three[0].as_slice(), three[0].as_slice()]).is_none(), "the same piece twice is still one"); assert_ne!(circle_pieces(&q, 2, 3).unwrap()[0], three[0], "each split draws new parts"); assert!(circle_pieces(&q, 2, 2).is_none() && circle_pieces(&q, 3, 3).is_none(), "only 1 of 1 and 2 of 3");letmut tampered = three[1].clone(); tampered[0] = 9; assert!(circle_join(2, 3, &[three[0].as_slice(), tampered.as_slice()]).is_none(), "a part index out of range is refused");// The circle's kind: its own wrap, never a trusted person's.let keys = derive(&[3u8; 32], &[4u8; 16], Kind::Circle.kdf()).unwrap();let wrapped = wrap(&keys.wrap, Kind::Circle, 1, &[0x42u8; 32]).unwrap(); assert!(unwrap(&keys.wrap, Kind::Trusted, 1, &wrapped).is_none()); assert_eq!(Kind::parse("circle"), Some(Kind::Circle)); assert_eq!(circle_context("setup-1", 2), "help-circle:setup-1:2");}