2026-09-10 14:50:01 -04:00
|
|
|
//! Encrypted identity backup. Onion (locator) is not included.
|
|
|
|
|
|
|
|
|
|
use argon2::{Algorithm, Argon2, Params, Version};
|
2026-09-10 19:41:00 -04:00
|
|
|
use chacha20poly1305::aead::{Aead, AeadCore, KeyInit, OsRng, Payload};
|
2026-09-10 14:50:01 -04:00
|
|
|
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
|
|
|
|
|
use rand::RngCore;
|
2026-09-12 13:36:21 -04:00
|
|
|
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
2026-09-10 14:50:01 -04:00
|
|
|
|
|
|
|
|
pub const MAGIC: &[u8] = b"owbak1";
|
|
|
|
|
const SALT_LEN: usize = 16;
|
|
|
|
|
const NONCE_LEN: usize = 12;
|
|
|
|
|
const KEY_LEN: usize = 32;
|
|
|
|
|
const PLAIN_LEN: usize = KEY_LEN * 4;
|
|
|
|
|
|
2026-09-12 13:36:21 -04:00
|
|
|
/// The identity bundle. Secret halves wipe on drop; `Debug` never prints them.
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
2026-09-10 14:50:01 -04:00
|
|
|
pub struct BackupKeys {
|
|
|
|
|
pub identity_sk: [u8; 32],
|
2026-09-12 13:36:21 -04:00
|
|
|
#[zeroize(skip)]
|
2026-09-10 14:50:01 -04:00
|
|
|
pub identity_pk: [u8; 32],
|
|
|
|
|
pub prekey_sk: [u8; 32],
|
2026-09-12 13:36:21 -04:00
|
|
|
#[zeroize(skip)]
|
2026-09-10 14:50:01 -04:00
|
|
|
pub prekey_pk: [u8; 32],
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 13:36:21 -04:00
|
|
|
impl std::fmt::Debug for BackupKeys {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
f.debug_struct("BackupKeys")
|
|
|
|
|
.field("identity_sk", &"<redacted>")
|
|
|
|
|
.field("identity_pk", &self.identity_pk)
|
|
|
|
|
.field("prekey_sk", &"<redacted>")
|
|
|
|
|
.field("prekey_pk", &self.prekey_pk)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 14:50:01 -04:00
|
|
|
impl BackupKeys {
|
2026-09-12 13:36:21 -04:00
|
|
|
pub fn to_bytes(&self) -> Zeroizing<[u8; PLAIN_LEN]> {
|
|
|
|
|
let mut out = Zeroizing::new([0u8; PLAIN_LEN]);
|
2026-09-10 14:50:01 -04:00
|
|
|
out[0..32].copy_from_slice(&self.identity_sk);
|
|
|
|
|
out[32..64].copy_from_slice(&self.identity_pk);
|
|
|
|
|
out[64..96].copy_from_slice(&self.prekey_sk);
|
|
|
|
|
out[96..128].copy_from_slice(&self.prekey_pk);
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_bytes(b: &[u8]) -> Result<Self, String> {
|
|
|
|
|
if b.len() != PLAIN_LEN {
|
|
|
|
|
return Err("backup plaintext length".into());
|
|
|
|
|
}
|
|
|
|
|
Ok(Self {
|
|
|
|
|
identity_sk: b[0..32].try_into().unwrap(),
|
|
|
|
|
identity_pk: b[32..64].try_into().unwrap(),
|
|
|
|
|
prekey_sk: b[64..96].try_into().unwrap(),
|
|
|
|
|
prekey_pk: b[96..128].try_into().unwrap(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 13:36:21 -04:00
|
|
|
pub(crate) fn kdf(passphrase: &str, salt: &[u8]) -> Result<Zeroizing<[u8; 32]>, String> {
|
2026-09-10 14:50:01 -04:00
|
|
|
if passphrase.is_empty() {
|
|
|
|
|
return Err("empty passphrase".into());
|
|
|
|
|
}
|
|
|
|
|
if salt.len() != SALT_LEN {
|
|
|
|
|
return Err("salt length".into());
|
|
|
|
|
}
|
|
|
|
|
let params = Params::new(19_456, 2, 1, Some(32)).map_err(|e| e.to_string())?;
|
|
|
|
|
let argon = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
|
2026-09-12 13:36:21 -04:00
|
|
|
let mut key = Zeroizing::new([0u8; 32]);
|
2026-09-10 14:50:01 -04:00
|
|
|
argon
|
2026-09-12 13:36:21 -04:00
|
|
|
.hash_password_into(passphrase.as_bytes(), salt, &mut key[..])
|
2026-09-10 14:50:01 -04:00
|
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
|
Ok(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn seal(passphrase: &str, keys: &BackupKeys) -> Result<Vec<u8>, String> {
|
|
|
|
|
let mut salt = [0u8; SALT_LEN];
|
|
|
|
|
rand::rngs::OsRng.fill_bytes(&mut salt);
|
|
|
|
|
let key = kdf(passphrase, &salt)?;
|
2026-09-12 13:36:21 -04:00
|
|
|
let cipher = ChaCha20Poly1305::new(Key::from_slice(&key[..]));
|
2026-09-10 14:50:01 -04:00
|
|
|
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
|
|
|
|
|
if nonce.len() != NONCE_LEN {
|
|
|
|
|
return Err("nonce length".into());
|
|
|
|
|
}
|
|
|
|
|
let ct = cipher
|
2026-09-12 13:36:21 -04:00
|
|
|
.encrypt(&nonce, &keys.to_bytes()[..])
|
2026-09-10 14:50:01 -04:00
|
|
|
.map_err(|_| "encrypt failed".to_string())?;
|
|
|
|
|
let mut out = Vec::with_capacity(MAGIC.len() + SALT_LEN + NONCE_LEN + ct.len());
|
|
|
|
|
out.extend_from_slice(MAGIC);
|
|
|
|
|
out.extend_from_slice(&salt);
|
|
|
|
|
out.extend_from_slice(&nonce);
|
|
|
|
|
out.extend_from_slice(&ct);
|
|
|
|
|
Ok(out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn open(passphrase: &str, blob: &[u8]) -> Result<BackupKeys, String> {
|
|
|
|
|
let min = MAGIC.len() + SALT_LEN + NONCE_LEN + 16;
|
|
|
|
|
if blob.len() < min || !blob.starts_with(MAGIC) {
|
|
|
|
|
return Err("not an onionwire backup".into());
|
|
|
|
|
}
|
|
|
|
|
let salt = &blob[MAGIC.len()..MAGIC.len() + SALT_LEN];
|
|
|
|
|
let nonce_off = MAGIC.len() + SALT_LEN;
|
|
|
|
|
let nonce = Nonce::from_slice(&blob[nonce_off..nonce_off + NONCE_LEN]);
|
|
|
|
|
let ct = &blob[nonce_off + NONCE_LEN..];
|
|
|
|
|
let key = kdf(passphrase, salt)?;
|
2026-09-12 13:36:21 -04:00
|
|
|
let cipher = ChaCha20Poly1305::new(Key::from_slice(&key[..]));
|
|
|
|
|
let pt = Zeroizing::new(
|
|
|
|
|
cipher
|
|
|
|
|
.decrypt(nonce, ct)
|
|
|
|
|
.map_err(|_| "wrong passphrase or corrupt backup".to_string())?,
|
|
|
|
|
);
|
|
|
|
|
BackupKeys::from_bytes(&pt[..])
|
2026-09-10 14:50:01 -04:00
|
|
|
}
|
2026-09-10 17:19:50 -04:00
|
|
|
|
2026-09-10 19:41:00 -04:00
|
|
|
pub(crate) fn aead_encrypt(
|
|
|
|
|
key: &[u8; 32],
|
|
|
|
|
plaintext: &[u8],
|
|
|
|
|
aad: &[u8],
|
|
|
|
|
) -> Result<Vec<u8>, String> {
|
2026-09-10 17:19:50 -04:00
|
|
|
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
|
|
|
|
|
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
|
|
|
|
|
let ct = cipher
|
2026-09-10 19:41:00 -04:00
|
|
|
.encrypt(
|
|
|
|
|
&nonce,
|
|
|
|
|
Payload {
|
|
|
|
|
msg: plaintext,
|
|
|
|
|
aad,
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-09-10 17:19:50 -04:00
|
|
|
.map_err(|_| "encrypt failed".to_string())?;
|
|
|
|
|
let mut out = Vec::with_capacity(NONCE_LEN + ct.len());
|
|
|
|
|
out.extend_from_slice(&nonce);
|
|
|
|
|
out.extend_from_slice(&ct);
|
|
|
|
|
Ok(out)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 19:41:00 -04:00
|
|
|
pub(crate) fn aead_decrypt(key: &[u8; 32], blob: &[u8], aad: &[u8]) -> Result<Vec<u8>, String> {
|
2026-09-10 17:19:50 -04:00
|
|
|
if blob.len() < NONCE_LEN + 16 {
|
|
|
|
|
return Err("ciphertext length".into());
|
|
|
|
|
}
|
|
|
|
|
let nonce = Nonce::from_slice(&blob[..NONCE_LEN]);
|
|
|
|
|
let cipher = ChaCha20Poly1305::new(Key::from_slice(key));
|
|
|
|
|
cipher
|
2026-09-10 19:41:00 -04:00
|
|
|
.decrypt(
|
|
|
|
|
nonce,
|
|
|
|
|
Payload {
|
|
|
|
|
msg: &blob[NONCE_LEN..],
|
|
|
|
|
aad,
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-09-10 17:19:50 -04:00
|
|
|
.map_err(|_| "wrong passphrase or corrupt".to_string())
|
|
|
|
|
}
|
2026-09-12 13:36:21 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
fn keys(tag: u8) -> BackupKeys {
|
|
|
|
|
BackupKeys {
|
|
|
|
|
identity_sk: [tag; 32],
|
|
|
|
|
identity_pk: [tag.wrapping_add(1); 32],
|
|
|
|
|
prekey_sk: [tag.wrapping_add(2); 32],
|
|
|
|
|
prekey_pk: [tag.wrapping_add(3); 32],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn derived_wrap_key_is_a_zeroizing_buffer() {
|
|
|
|
|
let mut key = kdf("correct horse", &[7u8; 16]).expect("kdf");
|
|
|
|
|
let _: &Zeroizing<[u8; 32]> = &key;
|
|
|
|
|
let before = *key;
|
|
|
|
|
assert_ne!(before, [0u8; 32]);
|
|
|
|
|
key.zeroize();
|
|
|
|
|
assert_eq!(*key, [0u8; 32], "derived key not wiped");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn backup_plaintext_buffer_is_a_zeroizing_buffer() {
|
|
|
|
|
let mut bytes = keys(5).to_bytes();
|
|
|
|
|
let _: &Zeroizing<[u8; PLAIN_LEN]> = &bytes;
|
|
|
|
|
assert_eq!(&bytes[..32], &[5u8; 32][..]);
|
|
|
|
|
bytes.zeroize();
|
|
|
|
|
assert_eq!(&bytes[..], &[0u8; PLAIN_LEN][..], "backup plaintext kept");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn backup_keys_debug_redacts_the_secret_halves() {
|
|
|
|
|
let dbg = format!("{:?}", keys(0xAB));
|
|
|
|
|
assert!(!dbg.contains("171"), "identity secret in Debug: {dbg}");
|
|
|
|
|
assert!(!dbg.contains("173"), "prekey secret in Debug: {dbg}");
|
|
|
|
|
assert!(dbg.contains("<redacted>"), "{dbg}");
|
|
|
|
|
assert!(dbg.contains("174"), "public half should stay: {dbg}");
|
|
|
|
|
}
|
|
|
|
|
}
|