//! Linking two of a person's devices by a code one shows and the other is //! given (`docs/backup-and-sync.md`). Neither knows the other yet, and only //! contacts open sessions, so the cards cross through the DHT, sealed //! under a key from the code: //! 1. the device that shows the code stores its card at [`Code::offer`]; //! 2. the one given the code finds it there, and stores its own at //! [`Code::answer`], which the first looks for until it is there. //! //! The nodes that hold the records cannot open them; a record opens only //! under the code, so finding one proves the other had the code. use crate::identity::Card; use alloc::string::String; use alloc::vec::Vec; use pinnacle_crypto::aead::{open, seal}; use pinnacle_crypto::sha2::{Sha256, hkdf}; /// Crockford's base 32: no I, L, O or U, which read as other letters. const ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ"; /// The code's characters: 60 bits. pub const LENGTH: usize = 12; /// A link record: a nonce, the sealed card and its tag. pub const RECORD: usize = 12 + Card::SIZE + 16; #[derive(Clone, PartialEq, Eq, Debug)] pub struct Code([u8; LENGTH]); impl Code { /// A code from 8 random bytes (60 of their bits). pub fn new(random: [u8; 8]) -> Code { let bits = u64::from_le_bytes(random); Code(core::array::from_fn(|i| { ALPHABET[((bits >> (5 * i)) & 31) as usize] })) } /// A code as typed: any case, dashes and spaces ignored, the letters /// people mistake read as what they meant (O as 0, I and L as 1). pub fn parse(text: &str) -> Option { let mut out = [0u8; LENGTH]; let mut n = 0; for c in text.bytes() { let c = match c.to_ascii_uppercase() { b'-' | b' ' => continue, b'O' => b'0', b'I' | b'L' => b'1', c => c, }; if !ALPHABET.contains(&c) || n == LENGTH { return None; } out[n] = c; n += 1; } (n == LENGTH).then_some(Code(out)) } /// As shown: three groups of four. pub fn text(&self) -> String { let s = core::str::from_utf8(&self.0).unwrap(); alloc::format!("{}-{}-{}", &s[..4], &s[4..8], &s[8..]) } fn key(&self) -> [u8; 32] { let mut k = [0u8; 32]; hkdf(b"pinnacle link", &self.0, b"pinnacle link key", &mut k); k } fn place(&self, what: &[u8]) -> [u8; 32] { Sha256::digest(&[what, &self.0].concat()) } /// Where the device showing the code puts its card. pub fn offer(&self) -> [u8; 32] { self.place(b"pinnacle link offer") } /// Where the device given the code puts its card. pub fn answer(&self) -> [u8; 32] { self.place(b"pinnacle link answer") } /// `card` sealed for the place `place` (`nonce` random). pub fn seal(&self, place: &[u8; 32], card: &Card, nonce: [u8; 12]) -> Vec { let mut body = card.to_bytes().to_vec(); let tag = seal(&self.key(), &nonce, place, &mut body); [&nonce[..], &body, &tag].concat() } /// The card in a record found at `place`, if it opens under this code /// and its signature holds. pub fn open(&self, place: &[u8; 32], record: &[u8]) -> Option { if record.len() != RECORD { return None; } let nonce: [u8; 12] = record[..12].try_into().ok()?; let mut body = record[12..12 + Card::SIZE].to_vec(); let tag: [u8; 16] = record[12 + Card::SIZE..].try_into().ok()?; if !open(&self.key(), &nonce, place, &mut body, &tag) { return None; } Card::from_bytes(&body) } }