Skip to main content

tuwunel_core/config/
encryption.rs

1//! Configures encrypted rooms and device-key distribution.
2//!
3//! Room defaults select when encryption is enabled automatically. Key
4//! limits and update-recipient policy govern device encryption data.
5
6use serde::Deserialize;
7use tuwunel_macros::config_example_generator;
8
9use super::true_fn;
10
11/// End-to-end encryption
12///
13/// Controls whether local users may use end-to-end encryption, whether the
14/// server turns it on by itself in new rooms, and how device keys are
15/// shared. Encryption is allowed by default, and with
16/// `enabled_by_default_for_room_type` at its default the server never
17/// enables it on its own; clients decide per room.
18///
19/// Tips:
20/// - `enabled_by_default_for_room_type` ignores `is_direct`: `invite`
21///   applies to every room created with a private preset, DM or not,
22///   including a non-public room created without a preset.
23/// - `allow = false` also stops local users from sending encrypted messages
24///   in rooms that are already encrypted.
25#[expect(rustdoc::broken_intra_doc_links, rustdoc::bare_urls)]
26#[derive(Clone, Debug, Deserialize)]
27#[config_example_generator(
28	filename = "tuwunel-example.toml",
29	section = "global.encryption",
30	group = "true"
31)]
32pub struct Encryption {
33	/// Allows encrypted rooms and events.
34	///
35	/// reloadable: yes
36	/// legacy: allow_encryption
37	#[serde(default = "true_fn")]
38	pub allow: bool,
39
40	/// Selects which locally created rooms use end-to-end encryption by
41	/// default.
42	///
43	/// "all" encrypts every created room, while "invite" encrypts rooms using
44	/// the `private_chat` or `trusted_private_chat` preset. The explicit "none"
45	/// value and all other values have no effect.
46	///
47	/// reloadable: yes
48	/// default: "none"
49	/// legacy: encryption_enabled_by_default_for_room_type
50	#[serde(default)]
51	pub enabled_by_default_for_room_type: Option<String>,
52
53	/// Limits one-time keys per device across all algorithms.
54	///
55	/// The default allows 256 keys; there is no known reason for an
56	/// administrator to change it.
57	///
58	/// reloadable: yes
59	/// default: 256
60	/// legacy: one_time_key_limit
61	#[serde(default = "default_one_time_key_limit")]
62	pub one_time_key_limit: usize,
63
64	/// Excludes unencrypted rooms when selecting common-room recipients of
65	/// device-list updates.
66	///
67	/// Enabling this can improve performance on very large servers but may
68	/// violate the specification and client expectations.
69	///
70	/// reloadable: yes
71	/// legacy: device_key_update_encrypted_rooms_only
72	#[serde(default)]
73	pub device_key_update_encrypted_rooms_only: bool,
74}
75
76impl Default for Encryption {
77	fn default() -> Self {
78		Self {
79			allow: true_fn(),
80			enabled_by_default_for_room_type: Default::default(),
81			one_time_key_limit: default_one_time_key_limit(),
82			device_key_update_encrypted_rooms_only: Default::default(),
83		}
84	}
85}
86
87fn default_one_time_key_limit() -> usize { 256 }