git clone https://garage44.eu/jeroen/pinnacle.git.. /p2p/src/identity.rs
//! Who a device is: an Ed25519 key that signs (its identity, which is how
//! contacts know it) and an X25519 key for key exchange, bound to it by a
//! signature. A contact card carries both public halves.
use pinnacle_crypto::{ed25519, sha2, x25519};
pub type Key = [u8; 32];
/// The key a contact is known by, in short readable form: the first 8
/// bytes of its identity key in hex, grouped.
pub fn fingerprint(key: &Key) -> [u8; 19] {
let hex = b"0123456789abcdef";
let mut out = [b' '; 19];
let mut at = 0;
for (i, b) in key[..8].iter().enumerate() {
if i > 0 && i % 2 == 0 {
at += 1;
}
out[at] = hex[usize::from(b >> 4)];
out[at + 1] = hex[usize::from(b & 15)];
at += 2;
}
out
}
pub struct Identity {
seed: [u8; 32],
pub public: Key,
dh_secret: [u8; 32],
pub dh_public: Key,
}
impl Identity {
/// The identity a 32-byte secret seed gives: kept on the device, never
/// sent anywhere.
pub fn from_seed(seed: [u8; 32]) -> Identity {
let mut dh_secret = [0u8; 32];
sha2::hkdf(b"pinnacle identity", &seed, b"x25519", &mut dh_secret);
Identity {
seed,
public: ed25519::public(&seed),
dh_secret,
dh_public: x25519::public(&dh_secret),
}
}
pub fn seed(&self) -> &[u8; 32] {
&self.seed
}
pub fn sign(&self, message: &[u8]) -> [u8; 64] {
ed25519::sign(&self.seed, message)
}
pub fn dh(&self, public: &Key) -> [u8; 32] {
x25519::x25519(&self.dh_secret, public)
}
/// This device's card for others to add as a contact.
pub fn card(&self, name: &str) -> Card {
let mut card = Card {
key: self.public,
dh: self.dh_public,
name: [0; 32],
signature: [0; 64],
};
let n = name.len().min(32);
card.name[..n].copy_from_slice(&name.as_bytes()[..n]);
card.signature = self.sign(&card.signed_part());
card
}
}
/// A contact: identity key, key-exchange key, a chosen name, signed by the
/// identity key.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Card {
pub key: Key,
pub dh: Key,
pub name: [u8; 32],
pub signature: [u8; 64],
}
impl Card {
pub const SIZE: usize = 32 + 32 + 32 + 64;
fn signed_part(&self) -> [u8; 96] {
let mut m = [0u8; 96];
m[..32].copy_from_slice(&self.key);
m[32..64].copy_from_slice(&self.dh);
m[64..].copy_from_slice(&self.name);
m
}
pub fn to_bytes(&self) -> [u8; Card::SIZE] {
let mut b = [0u8; Card::SIZE];
b[..96].copy_from_slice(&self.signed_part());
b[96..].copy_from_slice(&self.signature);
b
}
/// A card from bytes, if its signature holds.
pub fn from_bytes(b: &[u8]) -> Option<Card> {
let b: &[u8; Card::SIZE] = b.get(..Card::SIZE)?.try_into().ok()?;
let card = Card {
key: b[..32].try_into().unwrap(),
dh: b[32..64].try_into().unwrap(),
name: b[64..96].try_into().unwrap(),
signature: b[96..].try_into().unwrap(),
};
ed25519::verify(&card.key, &card.signed_part(), &card.signature).then_some(card)
}
pub fn name(&self) -> &str {
let len = self.name.iter().position(|&c| c == 0).unwrap_or(32);
core::str::from_utf8(&self.name[..len]).unwrap_or("?")
}
}
/// A cryptographic random number generator: ChaCha20 keyed by a seed from
/// the kernel's entropy, rekeyed from its own output after every use, so a
/// later compromise reveals nothing drawn before it.
pub struct Rng {
key: [u8; 32],
}
impl Rng {
pub fn new(seed: [u8; 32]) -> Rng {
Rng { key: seed }
}
pub fn fill(&mut self, out: &mut [u8]) {
let mut stream = alloc::vec![0u8; out.len() + 32];
pinnacle_crypto::aead::chacha20(&self.key, 0, &[0; 12], &mut stream);
self.key.copy_from_slice(&stream[..32]);
out.copy_from_slice(&stream[32..]);
}
pub fn bytes<const N: usize>(&mut self) -> [u8; N] {
let mut b = [0u8; N];
self.fill(&mut b);
b
}
}