11 releases (7 breaking)

0.8.0 Nov 19, 2024
0.7.2 Jul 18, 2024
0.7.1 May 13, 2024
0.7.0 Jan 5, 2024
0.1.0 May 26, 2020

#65 in Cryptography

Download history 577/week @ 2024-08-19 669/week @ 2024-08-26 517/week @ 2024-09-02 542/week @ 2024-09-09 561/week @ 2024-09-16 534/week @ 2024-09-23 517/week @ 2024-09-30 342/week @ 2024-10-07 639/week @ 2024-10-14 448/week @ 2024-10-21 388/week @ 2024-10-28 426/week @ 2024-11-04 259/week @ 2024-11-11 614/week @ 2024-11-18 623/week @ 2024-11-25 646/week @ 2024-12-02

2,169 downloads per month
Used in 13 crates (6 directly)

Apache-2.0

2.5MB
44K SLoC

A no-network-IO implementation of a state machine that handles E2EE for Matrix clients.

Usage

If you're just trying to write a Matrix client or bot in Rust, you're probably looking for matrix-sdk instead.

However, if you're looking to add E2EE to an existing Matrix client or library, read on.

The state machine works in a push/pull manner:

  • you push state changes and events retrieved from a Matrix homeserver /sync response into the state machine
  • you pull requests that you'll need to send back to the homeserver out of the state machine
use std::collections::BTreeMap;

use matrix_sdk_crypto::{EncryptionSyncChanges, OlmMachine, OlmError};
use ruma::{
    api::client::sync::sync_events::{v3::ToDevice, DeviceLists},
    device_id, user_id,
};

#[tokio::main]
async fn main() -> Result<(), OlmError> {
    let alice = user_id!("@alice:example.org");
    let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;

    let changed_devices = DeviceLists::default();
    let one_time_key_counts = BTreeMap::default();
    let unused_fallback_keys = Some(Vec::new());
    let next_batch_token = "T0K3N".to_owned();

    // Push changes that the server sent to us in a sync response.
    let decrypted_to_device = machine.receive_sync_changes(EncryptionSyncChanges {
        to_device_events: vec![],
        changed_devices: &changed_devices,
        one_time_keys_counts: &one_time_key_counts,
        unused_fallback_keys: unused_fallback_keys.as_deref(),
        next_batch_token: Some(next_batch_token),
    }).await?;

    // Pull requests that we need to send out.
    let outgoing_requests = machine.outgoing_requests().await?;

    // Send the requests here out and call machine.mark_request_as_sent().

    Ok(())
}

Room key forwarding algorithm

The decision tree below visualizes the way this crate decides whether a message key ("room key") will be forwarded to a requester upon a key request, provided the automatic-room-key-forwarding feature is enabled. Key forwarding is sometimes also referred to as key gossiping.

Crate Feature Flags

The following crate feature flags are available:

  • qrcode: Enbles QRcode generation and reading code

  • testing: Provides facilities and functions for tests, in particular for integration testing store implementations. ATTENTION: do not ever use outside of tests, we do not provide any stability warantees on these, these are merely helpers. If you find you need any function provided here outside of tests, please open a Github Issue and inform us about your use case for us to consider.

  • _disable-minimum-rotation-period-ms: Do not use except for testing. Disables the floor on the rotation period of room keys.

Dependencies

~23–35MB
~518K SLoC