Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the dehydrated device format implemented by vodozemac #4421

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ tracing-core = { git = "https://github.com/element-hq/tracing.git", rev = "ca943
tracing-subscriber = { git = "https://github.com/element-hq/tracing.git", rev = "ca9431f74d37c9d3b5e6a9f35b2c706711dab7dd" }
tracing-appender = { git = "https://github.com/element-hq/tracing.git", rev = "ca9431f74d37c9d3b5e6a9f35b2c706711dab7dd" }
paranoid-android = { git = "https://github.com/element-hq/paranoid-android.git", rev = "69388ac5b4afeed7be4401c70ce17f6d9a2cf19b" }
vodozemac = { git = "https://github.com/uhoreg/vodozemac.git", branch = "dehydration_format" }


[workspace.lints.rust]
rust_2018_idioms = "warn"
Expand Down
40 changes: 3 additions & 37 deletions crates/matrix-sdk-crypto/src/dehydrated_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@

use std::sync::Arc;

use hkdf::Hkdf;
use ruma::{
api::client::dehydrated_device::{put_dehydrated_device, DehydratedDeviceData},
assign,
events::AnyToDeviceEvent,
serde::Raw,
DeviceId,
};
use sha2::Sha256;
use thiserror::Error;
use tracing::{instrument, trace};
use vodozemac::LibolmPickleError;
Expand Down Expand Up @@ -136,8 +134,8 @@ impl DehydratedDevices {
device_id: &DeviceId,
device_data: Raw<DehydratedDeviceData>,
) -> Result<RehydratedDevice, DehydrationError> {
let pickle_key = expand_pickle_key(pickle_key.inner.as_ref(), device_id);
let rehydrated = self.inner.rehydrate(&pickle_key, device_id, device_data).await?;
let rehydrated =
self.inner.rehydrate(pickle_key.inner.as_ref(), device_id, device_data).await?;

Ok(RehydratedDevice { rehydrated, original: self.inner.to_owned() })
}
Expand Down Expand Up @@ -369,10 +367,8 @@ impl DehydratedDevice {

trace!("Creating an upload request for a dehydrated device");

let pickle_key =
expand_pickle_key(pickle_key.inner.as_ref(), &self.store.static_account().device_id);
let device_id = self.store.static_account().device_id.clone();
let device_data = account.dehydrate(&pickle_key);
let device_data = account.dehydrate(pickle_key.inner.as_ref());
let initial_device_display_name = Some(initial_device_display_name);

transaction.commit().await?;
Expand All @@ -385,36 +381,6 @@ impl DehydratedDevice {
}
}

/// We're using the libolm-compatible pickle format and its encryption scheme.
///
/// The libolm pickle encryption scheme uses HKDF to deterministically expand an
/// input key material, usually 32 bytes, into a AES key, MAC key, and the
/// initialization vector (IV).
///
/// This means that the same input key material will always end up producing the
/// same AES key, and IV.
///
/// This encryption scheme is used in the Olm double ratchet and was designed to
/// minimize the size of the ciphertext. As a tradeof, it requires a unique
/// input key material for each plaintext that gets encrypted, otherwise IV
/// reuse happens.
///
/// To combat the IV reuse, we're going to create a per-dehydrated-device unique
/// pickle key by expanding the key itself with the device ID used as the salt.
fn expand_pickle_key(key: &[u8; 32], device_id: &DeviceId) -> Box<[u8; 32]> {
// TODO: Perhaps we should put this into vodozemac with a new pickle
// minimalistic pickle format using the [`matrix_pickle`] crate.
//
// [`matrix_pickle`]: https://docs.rs/matrix-pickle/latest/matrix_pickle/
let kdf: Hkdf<Sha256> = Hkdf::new(Some(device_id.as_bytes()), key);
let mut key = Box::new([0u8; 32]);

kdf.expand(b"dehydrated-device-pickle-key", key.as_mut_slice())
.expect("We should be able to expand the 32 byte pickle key");

key
}

#[cfg(test)]
mod tests {
use std::{collections::BTreeMap, iter};
Expand Down
8 changes: 6 additions & 2 deletions crates/matrix-sdk-crypto/src/olm/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl Account {
pub(crate) fn dehydrate(&self, pickle_key: &[u8; 32]) -> Raw<DehydratedDeviceData> {
let device_pickle = self
.inner
.to_libolm_pickle(pickle_key)
.to_dehydrated_device(pickle_key)
.expect("We should be able to convert a freshly created Account into a libolm pickle");

let data = DehydratedDeviceData::V1(DehydratedDeviceV1::new(device_pickle));
Expand All @@ -711,7 +711,11 @@ impl Account {

match data {
DehydratedDeviceData::V1(d) => {
let account = InnerAccount::from_libolm_pickle(&d.device_pickle, pickle_key)?;
let account = InnerAccount::from_dehydrated_device(
&d.device_pickle,
device_id.as_str(),
pickle_key,
)?;
Ok(Self::new_helper(account, user_id, device_id))
}
_ => Err(DehydrationError::Json(serde_json::Error::custom(format!(
Expand Down
Loading