[core] Vault and generators: Argon2id key slot + XChaCha20-Poly1305 file, atomic save, item templates, memorable/nickname/random/PIN/strict generators

This commit is contained in:
2026-10-31 10:12:57 +01:00
parent 42ddcf0287
commit eb710c3135
21 changed files with 9710 additions and 4 deletions
+125
View File
@@ -0,0 +1,125 @@
//! Reading and writing the header of a `.custos` file (docs/FORMAT.md, "Layout").
use crate::crypto::{KEY_LEN, NONCE_LEN, TAG_LEN};
use crate::{Error, KdfParams};
pub const MAGIC: &[u8; 6] = b"CUSTOS";
pub const VERSION: u16 = 1;
pub const MAX_SLOTS: usize = 8;
pub const SLOT_PASSWORD: u8 = 1;
const KDF_ARGON2ID: u8 = 1;
pub const SALT_LEN: usize = 32;
/// kdf(1) m(4) t(4) p(4) salt(32) nonce(24): the part of a password slot that is its own AAD.
const PW_AAD_LEN: usize = 1 + 12 + SALT_LEN + NONCE_LEN;
const PW_PAYLOAD_LEN: usize = PW_AAD_LEN + KEY_LEN + TAG_LEN;
/// One key slot as stored. Unknown kinds are kept and written back unchanged.
#[derive(Clone)]
pub struct RawSlot {
pub kind: u8,
pub payload: Vec<u8>,
}
pub struct PasswordSlot {
pub kdf: KdfParams,
pub salt: [u8; SALT_LEN],
pub nonce: [u8; NONCE_LEN],
pub wrapped: Vec<u8>,
}
impl PasswordSlot {
/// The bytes the wrapped key is authenticated against: magic ‖ version ‖ kind ‖ payload before the key.
pub fn aad(&self) -> Vec<u8> {
let mut a = Vec::with_capacity(9 + PW_AAD_LEN);
a.extend_from_slice(MAGIC);
a.extend_from_slice(&VERSION.to_le_bytes());
a.push(SLOT_PASSWORD);
a.extend_from_slice(&self.payload()[..PW_AAD_LEN]);
a
}
pub fn payload(&self) -> Vec<u8> {
let mut p = Vec::with_capacity(PW_PAYLOAD_LEN);
p.push(KDF_ARGON2ID);
p.extend_from_slice(&self.kdf.m_cost_kib.to_le_bytes());
p.extend_from_slice(&self.kdf.t_cost.to_le_bytes());
p.extend_from_slice(&self.kdf.p_cost.to_le_bytes());
p.extend_from_slice(&self.salt);
p.extend_from_slice(&self.nonce);
p.extend_from_slice(&self.wrapped);
p
}
pub fn parse(payload: &[u8]) -> Result<Self, Error> {
if payload.len() != PW_PAYLOAD_LEN {
return Err(Error::Malformed);
}
if payload[0] != KDF_ARGON2ID {
return Err(Error::UnsupportedKdf);
}
let u32_at = |i: usize| u32::from_le_bytes(payload[i..i + 4].try_into().unwrap());
let kdf = KdfParams { m_cost_kib: u32_at(1), t_cost: u32_at(5), p_cost: u32_at(9) };
kdf.check()?;
Ok(PasswordSlot {
kdf,
salt: payload[13..13 + SALT_LEN].try_into().unwrap(),
nonce: payload[13 + SALT_LEN..PW_AAD_LEN].try_into().unwrap(),
wrapped: payload[PW_AAD_LEN..].to_vec(),
})
}
pub fn raw(&self) -> RawSlot {
RawSlot { kind: SLOT_PASSWORD, payload: self.payload() }
}
}
pub struct Header {
pub slots: Vec<RawSlot>,
pub body_nonce: [u8; NONCE_LEN],
}
impl Header {
pub fn to_bytes(&self) -> Vec<u8> {
let mut h = Vec::new();
h.extend_from_slice(MAGIC);
h.extend_from_slice(&VERSION.to_le_bytes());
h.push(self.slots.len() as u8);
for s in &self.slots {
h.push(s.kind);
h.extend_from_slice(&(s.payload.len() as u16).to_le_bytes());
h.extend_from_slice(&s.payload);
}
h.extend_from_slice(&self.body_nonce);
h
}
/// Parses the header; returns it and its length (where the body starts).
pub fn parse(bytes: &[u8]) -> Result<(Header, usize), Error> {
if bytes.len() < 9 || &bytes[..6] != MAGIC {
return Err(Error::NotAVault);
}
let version = u16::from_le_bytes([bytes[6], bytes[7]]);
if version != VERSION {
return Err(Error::UnsupportedVersion(version));
}
let count = bytes[8] as usize;
if count == 0 || count > MAX_SLOTS {
return Err(Error::Malformed);
}
let mut at = 9;
let mut slots = Vec::with_capacity(count);
for _ in 0..count {
let head = bytes.get(at..at + 3).ok_or(Error::Malformed)?;
let len = u16::from_le_bytes([head[1], head[2]]) as usize;
let payload = bytes.get(at + 3..at + 3 + len).ok_or(Error::Malformed)?;
slots.push(RawSlot { kind: head[0], payload: payload.to_vec() });
at += 3 + len;
}
let body_nonce = bytes.get(at..at + NONCE_LEN).ok_or(Error::Malformed)?.try_into().unwrap();
at += NONCE_LEN;
if bytes.len() < at + TAG_LEN {
return Err(Error::Malformed);
}
Ok((Header { slots, body_nonce }, at))
}
}