Cotton code
35 lines · 1.4 KB · RustRaw
//! A KEY SUCCESSION's proof: the key a device is replacing signs, once, that//! `successor` takes its place in `database` ("Re-key this device").//!//! The approval is still the owner's, typed on another approved device; this//! is what tells that approval the new key is the SAME device re-keyed rather//! than a device starting over on the same hardware. A re-key is a succession//! (what the old key minted follows the new one); a start-over is a revoke.//! Bound to the database and to the successor, so a proof is good for one//! succession and nowhere else.use base64::engine::general_purpose::URL_SAFE_NO_PAD;use base64::Engine;const DOMAIN: &str = "cotton-device-succession-v1";/// The bytes the predecessor signs.pub fn message(database: &str, successor: &str) -> Vec<u8> {    format!("{DOMAIN}\n{database}\n{successor}").into_bytes()}/// Does `signature` (base64url) verify as `predecessor`'s over this/// succession? `false` for anything that is not a key or not a signature.pub fn verify(predecessor: &str, database: &str, successor: &str, signature: &str) -> bool {    if predecessor == successor {        return false;    }    let Ok(signature) = URL_SAFE_NO_PAD.decode(signature) else {        return false;    };    crate::keys::person::verify(predecessor, &message(database, successor), &signature)}#[cfg(test)]#[path = "succession_tests.rs"]mod tests;