Cotton code
78 lines · 3.7 KB · RustRaw
//! The passkey vector the phone's Swift encoder is held to, byte for byte.//!//! Cotton Keys' iOS credential provider writes its registration in Swift//! (CryptoKit, no Rust core in the extension), so the one WebAuthn rule has a//! second writer. This test makes the vector with `cotton_core::keys::passkey`//! over a FIXED, synthetic key and holds the committed file to it; the Swift//! side (`CottonKitTests/PasskeyAuthenticatorTests`) reads the same file and//! must produce the same COSE key, authenticatorData and attestation object.//! The assertion's authenticatorData is held too; its signature is Rust's//! deterministic one, which the Swift side verifies (CryptoKit's own ECDSA is//! randomised, so it is checked by verification, not by bytes).//!//! Rewrite after a deliberate change: `PASSKEY_VECTOR_WRITE=1 cargo test -p//! cotton-core --test passkey_vector`. The key is synthetic and opens nothing.use base64::engine::general_purpose::URL_SAFE_NO_PAD;use base64::Engine as _;use cotton_core::keys::passkey::{assert, attest, make_from, Algorithm};use serde_json::{json, Value};use zeroize::Zeroizing;/// The committed vector, under this crate's directory read at run time/// (manifest-dir-at-run-time: a cached test binary must not carry a gone checkout).fn fixture() -> std::path::PathBuf {    cotton_store::manifest_dir().join("tests/fixtures/passkey_vector.json")}/// The synthetic inputs: a made-up site, account, id, scalar and challenge hash.fn inputs() -> (String, String, [u8; 16], [u8; 32], [u8; 32]) {    let rp_id = "passkey.example".to_string();    let user_handle = URL_SAFE_NO_PAD.encode(b"synthetic-user-1");    let mut credential_id = [0u8; 16];    for (i, b) in credential_id.iter_mut().enumerate() {        *b = 0xc0 + i as u8;    }    let scalar = [0x5au8; 32];    let client_data_hash = [0x42u8; 32];    (rp_id, user_handle, credential_id, scalar, client_data_hash)}fn vector() -> Value {    let (rp_id, user_handle, credential_id, scalar, hash) = inputs();    let passkey = make_from(&rp_id, &user_handle, Algorithm::Es256, credential_id, Zeroizing::new(scalar)).unwrap();    let attestation = attest(&passkey, &rp_id, &hash).unwrap();    let assertion = assert(&passkey, &rp_id, &hash, 0).unwrap();    json!({        "note": "synthetic: made by crates/cotton-core/tests/passkey_vector.rs; opens nothing",        "rp_id": rp_id,        "user_handle": user_handle,        "credential_id": URL_SAFE_NO_PAD.encode(credential_id),        "private_scalar_hex": hex::encode(scalar),        "client_data_hash_hex": hex::encode(hash),        "public_key": passkey.public_key,        "attestation": {            "authenticator_data_hex": hex::encode(&attestation.authenticator_data),            "attestation_object_hex": hex::encode(&attestation.attestation_object),        },        "assertion": {            "counter": 0,            "authenticator_data_hex": hex::encode(&assertion.authenticator_data),            "signature_der_hex": hex::encode(&assertion.signature),        },    })}// RULE: the committed passkey vector is exactly what the core's attest and// assert make over the fixed synthetic key, so the Swift encoder held to the// file is held to the core (Cotton Keys provider, 8 Nov 2026).#[test]fn the_committed_passkey_vector_is_the_cores_own() {    let made = vector();    let text = serde_json::to_string_pretty(&made).unwrap() + "\n";    if std::env::var_os("PASSKEY_VECTOR_WRITE").is_some() {        std::fs::write(fixture(), &text).unwrap();    }    let committed = std::fs::read_to_string(fixture()).expect("the vector is committed (PASSKEY_VECTOR_WRITE=1 writes it)");    assert_eq!(committed, text, "the committed vector drifted from the core: rewrite it deliberately");}