[core] TOTP, QR payloads, device key wrap: custos-totp, custos-qr, Vault::wrap_for_device (v0.2.0)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
//! Device unlock (docs/FORMAT.md, "Device key wrap"): the vault key wrapped with a key derived from
|
||||
//! material only this device can produce (the desktop app uses a Windows Hello signature). The blob is
|
||||
//! kept on that device, never in the vault file.
|
||||
|
||||
use crate::Error;
|
||||
use crate::crypto::{self, KEY_LEN, NONCE_LEN, TAG_LEN};
|
||||
use hkdf::Hkdf;
|
||||
use sha2::Sha256;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
const MAGIC: &[u8; 4] = b"CDW1";
|
||||
const SALT_LEN: usize = 32;
|
||||
const INFO: &[u8] = b"custos device wrap v1";
|
||||
pub const BLOB_LEN: usize = 4 + SALT_LEN + NONCE_LEN + KEY_LEN + TAG_LEN;
|
||||
|
||||
fn kek(material: &[u8], salt: &[u8]) -> Zeroizing<[u8; KEY_LEN]> {
|
||||
let mut k = Zeroizing::new([0u8; KEY_LEN]);
|
||||
Hkdf::<Sha256>::new(Some(salt), material).expand(INFO, k.as_mut()).expect("32 bytes is a valid HKDF length");
|
||||
k
|
||||
}
|
||||
|
||||
/// `CDW1 ‖ salt ‖ nonce ‖ XChaCha20-Poly1305(HKDF-SHA256(material, salt), vault key, aad = CDW1 ‖ salt ‖ nonce)`.
|
||||
pub fn wrap(key: &[u8; KEY_LEN], material: &[u8]) -> Vec<u8> {
|
||||
let mut salt = [0u8; SALT_LEN];
|
||||
crypto::fill_random(&mut salt);
|
||||
let nonce = crypto::random_nonce();
|
||||
let mut blob = Vec::with_capacity(BLOB_LEN);
|
||||
blob.extend_from_slice(MAGIC);
|
||||
blob.extend_from_slice(&salt);
|
||||
blob.extend_from_slice(&nonce);
|
||||
let sealed = crypto::seal(&kek(material, &salt), &nonce, key, &blob);
|
||||
blob.extend_from_slice(&sealed);
|
||||
blob
|
||||
}
|
||||
|
||||
pub fn unwrap(blob: &[u8], material: &[u8]) -> Result<Zeroizing<[u8; KEY_LEN]>, Error> {
|
||||
if blob.len() != BLOB_LEN || &blob[..4] != MAGIC {
|
||||
return Err(Error::DeviceKeyRejected);
|
||||
}
|
||||
let salt = &blob[4..4 + SALT_LEN];
|
||||
let nonce: [u8; NONCE_LEN] = blob[4 + SALT_LEN..4 + SALT_LEN + NONCE_LEN].try_into().unwrap();
|
||||
let head = 4 + SALT_LEN + NONCE_LEN;
|
||||
let k = crypto::open(&kek(material, salt), &nonce, &blob[head..], &blob[..head]).ok_or(Error::DeviceKeyRejected)?;
|
||||
Ok(Zeroizing::new(k.as_slice().try_into().map_err(|_| Error::DeviceKeyRejected)?))
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
//! ```
|
||||
|
||||
mod crypto;
|
||||
mod device;
|
||||
mod format;
|
||||
mod item;
|
||||
mod save;
|
||||
@@ -42,6 +43,8 @@ pub enum Error {
|
||||
WrongPassword,
|
||||
#[error("the vault file was changed or damaged after it was written")]
|
||||
Corrupted,
|
||||
#[error("this device's unlock key no longer opens the vault; use the master password")]
|
||||
DeviceKeyRejected,
|
||||
#[error("{0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
@@ -160,6 +163,20 @@ impl Vault {
|
||||
Ok(Vault { key, slots: header.slots, items: body.items })
|
||||
}
|
||||
|
||||
/// Wraps the vault key for device unlock with key material only this device can produce (e.g. a
|
||||
/// Windows Hello signature). Keep the blob on the device; see docs/FORMAT.md, "Device key wrap".
|
||||
pub fn wrap_for_device(&self, material: &[u8]) -> Result<Vec<u8>, Error> {
|
||||
Ok(device::wrap(&self.key, material))
|
||||
}
|
||||
|
||||
/// Unlocks with a device blob instead of the password. Fails with `DeviceKeyRejected` when the
|
||||
/// material is wrong or the blob was changed, and with `Corrupted` when it belongs to another vault.
|
||||
pub fn open_with_device(bytes: &[u8], blob: &[u8], material: &[u8]) -> Result<Vault, Error> {
|
||||
let (header, body_at) = Header::parse(bytes)?;
|
||||
let key = device::unwrap(blob, material)?;
|
||||
Vault::open_body(bytes, header, body_at, key)
|
||||
}
|
||||
|
||||
/// The encrypted file contents, with a fresh body nonce.
|
||||
pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
|
||||
let body_nonce = crypto::random_nonce();
|
||||
|
||||
Reference in New Issue
Block a user