Expand description

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


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, convert::TryFrom};

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

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

    let to_device_events = ToDevice::default();
    let changed_devices = DeviceLists::default();
    let one_time_key_counts = BTreeMap::default();
    let unused_fallback_keys = Some(Vec::new());

    // Push changes that the server sent to us in a sync response.
    let decrypted_to_device = machine.receive_sync_changes(

    // 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().


Room key sharing algorithm

The decision tree below visualizes the way this crate decides whether a room key will be shared with a requester upon a key request.

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.


pub use store::CrossSigningKeyExport;
pub use store::CryptoStoreError;
pub use store::SecretImportError;


The crypto specific Olm objects.

Types and traits to implement the storage layer for the OlmMachine

Module containing customized types modeling Matrix keys.


Customize the accept-reply for a verification process

A wrapper that transparently encrypts anything that implements Read as an Matrix attachment.

A wrapper that transparently encrypts anything that implements Read.

Information about the cancellation of a verification request or verification flow.

Struct representing the state of our private cross signing keys, it shows which private cross signing keys we have locally stored.

A device represents a E2EE capable client of an user.

An emoji that is used for interactive verification using a short auth string.

Settings for an encrypted room.

A struct describing an outgoing key request.

A request that will back up a batch of room keys to the server.

Customized version of ruma_client_api::keys::get_keys::v3::Request, without any references.

Wrapper for a cross signing key marking it as the master key.

Struct holding all the information that is needed to decrypt an encrypted file.

State machine implementation of the Olm/Megolm encryption protocol used for Matrix end to end encryption.

Outgoing request type, holds the unique ID of the request and the actual request.

Struct representing a cross signing identity of a user.

Account holding identity keys for which sessions can be created.

A read-only version of a Device.

Struct representing a cross signing identity of our own user.

Struct representing a cross signing identity of a user.

Return type for the room key importing.

Customized owned request type for sending out room messages.

Short authentication string object.

Customized version of ruma_client_api::to_device::send_event_to_device::v3::Request

Request that will publish a cross signing identity.

A read only view over all devices belonging to a user.

Struct representing a cross signing identity of a user.

An object controlling key verification requests.


Error type for attachment decryption.

Enum over all the incoming responses we need to receive.

Error representing a failure during key export or import.

The local trust state of a device.

Error representing a failure during a group encryption operation.

Error representing a failure during a device to device cryptographic operation.

Enum over the different outgoing requests we can have.

An enum over the different outgoing verification based requests.

Enum over the different user identity types we can have.

An enum over the various secret request types we can have.

Error type describin different errors that happen when we check or create signatures for a Matrix JSON object.

Enum over the different user identity types we can have.

An enum over the different verification types the SDK supports.


Try to decrypt a reader into a list of exported room keys.

Encrypt the list of exported room keys using the given passphrase.