kdf: version Argon2id profiles and raise the default to 64 MiB / t=3
The store and identity backup shared one hard-coded Argon2id profile
(m=19456 KiB, t=2, p=1) with nothing recorded on disk, so changing it
would have bricked every existing store and backup.
Record the profile with the data and raise the default:
- KdfProfile { Legacy = 0, Current = 1 }; Current is m=65536 KiB,
t=3, p=1, out=32.
- Backups: seal() writes a profile byte after owbak1. Legacy blobs
(exactly SEALED_BODY_LEN after MAGIC) still open; an unknown id is
an error, never a fallback.
- Stores: store_meta gains kdf_profile (additive, default 0). The
SQLCipher file key has no place to record a profile before the db
opens, so a legacy file is detected by trial-opening with the
legacy profile, then rekeyed to Current in one SQLCipher
transaction; store_meta is re-wrapped (salt + ciphertext + profile)
in a single transaction on the first successful unlock. Both are
one-shot: the next start takes the current path.
- docs/THREAT_MODEL.md states the new params and the versioning.
Measured in release on this host: kdf legacy 0.018s / current 0.095s;
Store reopen current 0.354s, legacy upgrade path 0.528s.
This commit is contained in:
parent
36969a7e95
commit
8f959e937f
3 changed files with 460 additions and 32 deletions
165
src/backup.rs
165
src/backup.rs
|
|
@ -1,4 +1,9 @@
|
|||
//! Encrypted identity backup. Onion (locator) is not included.
|
||||
//!
|
||||
//! `seal()` writes `owbak1 || profile(1) || salt(16) || nonce(12) || ct`.
|
||||
//! The pre-profile `owbak1 || salt(16) || nonce(12) || ct` layout is still
|
||||
//! accepted as [`KdfProfile::Legacy`]; the two are unambiguous because the
|
||||
//! plaintext is a fixed [`PLAIN_LEN`], so only two total lengths are legal.
|
||||
|
||||
use argon2::{Algorithm, Argon2, Params, Version};
|
||||
use chacha20poly1305::aead::{Aead, AeadCore, KeyInit, OsRng, Payload};
|
||||
|
|
@ -11,6 +16,52 @@ const SALT_LEN: usize = 16;
|
|||
const NONCE_LEN: usize = 12;
|
||||
const KEY_LEN: usize = 32;
|
||||
const PLAIN_LEN: usize = KEY_LEN * 4;
|
||||
/// Salt + nonce + ciphertext + Poly1305 tag, i.e. everything after `MAGIC`.
|
||||
const SEALED_BODY_LEN: usize = SALT_LEN + NONCE_LEN + PLAIN_LEN + 16;
|
||||
|
||||
/// Which Argon2id parameters a stored blob was written with.
|
||||
///
|
||||
/// The id is recorded with the data (a byte in the backup header, a
|
||||
/// `store_meta.kdf_profile` column in the store). `Legacy` is id `0` and also
|
||||
/// covers every blob written before profiles existed — those have no recorded
|
||||
/// id at all. Never change an existing profile's parameters: a new cost means
|
||||
/// a new id.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum KdfProfile {
|
||||
/// m = 19_456 KiB, t = 2, p = 1.
|
||||
Legacy,
|
||||
/// m = 65_536 KiB (64 MiB), t = 3, p = 1.
|
||||
Current,
|
||||
}
|
||||
|
||||
impl KdfProfile {
|
||||
pub(crate) const LEGACY_ID: u8 = 0;
|
||||
pub(crate) const CURRENT_ID: u8 = 1;
|
||||
|
||||
pub(crate) fn id(self) -> u8 {
|
||||
match self {
|
||||
Self::Legacy => Self::LEGACY_ID,
|
||||
Self::Current => Self::CURRENT_ID,
|
||||
}
|
||||
}
|
||||
|
||||
/// Strict: an id this build does not know is an error, never a fallback.
|
||||
pub(crate) fn from_id(id: u8) -> Result<Self, String> {
|
||||
match id {
|
||||
Self::LEGACY_ID => Ok(Self::Legacy),
|
||||
Self::CURRENT_ID => Ok(Self::Current),
|
||||
other => Err(format!("unknown kdf profile {other}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn params(self) -> Result<Params, String> {
|
||||
let (m_cost, t_cost, p_cost) = match self {
|
||||
Self::Legacy => (19_456, 2, 1),
|
||||
Self::Current => (65_536, 3, 1),
|
||||
};
|
||||
Params::new(m_cost, t_cost, p_cost, Some(KEY_LEN)).map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// The identity bundle. Secret halves wipe on drop; `Debug` never prints them.
|
||||
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
||||
|
|
@ -58,14 +109,21 @@ impl BackupKeys {
|
|||
}
|
||||
|
||||
pub(crate) fn kdf(passphrase: &str, salt: &[u8]) -> Result<Zeroizing<[u8; 32]>, String> {
|
||||
kdf_with(KdfProfile::Current, passphrase, salt)
|
||||
}
|
||||
|
||||
pub(crate) fn kdf_with(
|
||||
profile: KdfProfile,
|
||||
passphrase: &str,
|
||||
salt: &[u8],
|
||||
) -> Result<Zeroizing<[u8; 32]>, String> {
|
||||
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);
|
||||
let argon = Argon2::new(Algorithm::Argon2id, Version::V0x13, profile.params()?);
|
||||
let mut key = Zeroizing::new([0u8; 32]);
|
||||
argon
|
||||
.hash_password_into(passphrase.as_bytes(), salt, &mut key[..])
|
||||
|
|
@ -85,8 +143,9 @@ pub fn seal(passphrase: &str, keys: &BackupKeys) -> Result<Vec<u8>, String> {
|
|||
let ct = cipher
|
||||
.encrypt(&nonce, &keys.to_bytes()[..])
|
||||
.map_err(|_| "encrypt failed".to_string())?;
|
||||
let mut out = Vec::with_capacity(MAGIC.len() + SALT_LEN + NONCE_LEN + ct.len());
|
||||
let mut out = Vec::with_capacity(MAGIC.len() + 1 + SEALED_BODY_LEN);
|
||||
out.extend_from_slice(MAGIC);
|
||||
out.push(KdfProfile::Current.id());
|
||||
out.extend_from_slice(&salt);
|
||||
out.extend_from_slice(&nonce);
|
||||
out.extend_from_slice(&ct);
|
||||
|
|
@ -94,15 +153,25 @@ pub fn seal(passphrase: &str, keys: &BackupKeys) -> Result<Vec<u8>, String> {
|
|||
}
|
||||
|
||||
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) {
|
||||
if !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)?;
|
||||
let rest = &blob[MAGIC.len()..];
|
||||
// The sealed plaintext is a fixed size, so a legacy blob is exactly
|
||||
// SEALED_BODY_LEN bytes after MAGIC and a versioned one is one byte longer.
|
||||
let (profile, body) = match rest.len() {
|
||||
SEALED_BODY_LEN => (KdfProfile::Legacy, rest),
|
||||
n if n == SEALED_BODY_LEN + 1 => {
|
||||
let profile = KdfProfile::from_id(rest[0])?;
|
||||
(profile, &rest[1..])
|
||||
}
|
||||
_ => return Err("not an onionwire backup".into()),
|
||||
};
|
||||
let salt = &body[..SALT_LEN];
|
||||
let nonce_off = SALT_LEN;
|
||||
let nonce = Nonce::from_slice(&body[nonce_off..nonce_off + NONCE_LEN]);
|
||||
let ct = &body[nonce_off + NONCE_LEN..];
|
||||
let key = kdf_with(profile, passphrase, salt)?;
|
||||
let cipher = ChaCha20Poly1305::new(Key::from_slice(&key[..]));
|
||||
let pt = Zeroizing::new(
|
||||
cipher
|
||||
|
|
@ -191,4 +260,80 @@ mod tests {
|
|||
assert!(dbg.contains("<redacted>"), "{dbg}");
|
||||
assert!(dbg.contains("174"), "public half should stay: {dbg}");
|
||||
}
|
||||
|
||||
/// The pre-profile layout: `MAGIC || salt || nonce || ct`, no id byte.
|
||||
fn legacy_blob(passphrase: &str, keys: &BackupKeys) -> Vec<u8> {
|
||||
let salt = [0x5Au8; SALT_LEN];
|
||||
let key = kdf_with(KdfProfile::Legacy, passphrase, &salt).expect("legacy kdf");
|
||||
let cipher = ChaCha20Poly1305::new(Key::from_slice(&key[..]));
|
||||
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
|
||||
let ct = cipher.encrypt(&nonce, &keys.to_bytes()[..]).expect("seal");
|
||||
let mut out = Vec::new();
|
||||
out.extend_from_slice(MAGIC);
|
||||
out.extend_from_slice(&salt);
|
||||
out.extend_from_slice(&nonce);
|
||||
out.extend_from_slice(&ct);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sealed_blob_records_the_current_profile_and_roundtrips() {
|
||||
let k = keys(9);
|
||||
let blob = seal("correct horse", &k).expect("seal");
|
||||
assert_eq!(blob.len(), MAGIC.len() + 1 + SEALED_BODY_LEN);
|
||||
assert_eq!(
|
||||
blob[MAGIC.len()],
|
||||
KdfProfile::Current.id(),
|
||||
"new blobs must name their KDF profile"
|
||||
);
|
||||
assert_eq!(open("correct horse", &blob).expect("open"), k);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_profile_blob_still_opens() {
|
||||
let k = keys(4);
|
||||
let blob = legacy_blob("correct horse", &k);
|
||||
assert_eq!(blob.len(), MAGIC.len() + SEALED_BODY_LEN);
|
||||
assert_eq!(open("correct horse", &blob).expect("legacy open"), k);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_passphrase_on_a_current_profile_blob_is_the_existing_error() {
|
||||
let blob = seal("right", &keys(1)).expect("seal");
|
||||
let err = open("wrong", &blob).expect_err("wrong passphrase must fail");
|
||||
assert_eq!(err, "wrong passphrase or corrupt backup");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_profile_id_is_a_clear_error_not_a_fallback() {
|
||||
let mut blob = seal("pw", &keys(2)).expect("seal");
|
||||
blob[MAGIC.len()] = 9;
|
||||
let err = open("pw", &blob).expect_err("unknown profile must fail");
|
||||
assert_eq!(err, "unknown kdf profile 9");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailing_garbage_is_not_an_onionwire_backup() {
|
||||
let mut blob = seal("pw", &keys(3)).expect("seal");
|
||||
blob.push(0xff);
|
||||
assert_eq!(
|
||||
open("pw", &blob).expect_err("must reject"),
|
||||
"not an onionwire backup"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "manual KDF timing measurement; run with --release --ignored"]
|
||||
fn kdf_profile_timings() {
|
||||
use std::time::Instant;
|
||||
let salt = [0x11u8; SALT_LEN];
|
||||
for (label, profile) in [
|
||||
("legacy m=19456 t=2 p=1", KdfProfile::Legacy),
|
||||
("current m=65536 t=3 p=1", KdfProfile::Current),
|
||||
] {
|
||||
let started = Instant::now();
|
||||
let _ = kdf_with(profile, "correct horse battery staple", &salt).expect("kdf");
|
||||
eprintln!("{label}: {:.3}s", started.elapsed().as_secs_f64());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue