-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprivacy.rs
More file actions
208 lines (182 loc) · 5.84 KB
/
privacy.rs
File metadata and controls
208 lines (182 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use serde::{Deserialize, Serialize};
use std::fmt;
/// Version identifier for room secrets
pub type SecretVersion = u32;
/// Privacy mode for a chat room
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
pub enum PrivacyMode {
/// Room content is visible to all network participants
#[default]
Public,
/// Room content is encrypted and only visible to members
Private,
}
/// Cipher specification for encrypted room content
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub enum RoomCipherSpec {
/// AES-256-GCM with 12-byte nonce
Aes256Gcm,
}
/// A value that may be public or encrypted
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub enum SealedBytes {
/// Plaintext value (only for public rooms)
Public { value: Vec<u8> },
/// Encrypted value with metadata
Private {
ciphertext: Vec<u8>,
nonce: [u8; 12],
secret_version: SecretVersion,
declared_len_bytes: u32,
},
}
impl SealedBytes {
/// Create a new public sealed bytes value
pub fn public(value: Vec<u8>) -> Self {
Self::Public { value }
}
/// Create a new private sealed bytes value
pub fn private(
ciphertext: Vec<u8>,
nonce: [u8; 12],
secret_version: SecretVersion,
declared_len_bytes: u32,
) -> Self {
Self::Private {
ciphertext,
nonce,
secret_version,
declared_len_bytes,
}
}
/// Check if this is a public value
pub fn is_public(&self) -> bool {
matches!(self, Self::Public { .. })
}
/// Check if this is a private value
pub fn is_private(&self) -> bool {
matches!(self, Self::Private { .. })
}
/// Get the declared length in bytes for validation
pub fn declared_len(&self) -> usize {
match self {
Self::Public { value } => value.len(),
Self::Private {
declared_len_bytes, ..
} => *declared_len_bytes as usize,
}
}
/// Get the secret version (if private)
pub fn secret_version(&self) -> Option<SecretVersion> {
match self {
Self::Public { .. } => None,
Self::Private { secret_version, .. } => Some(*secret_version),
}
}
/// Get the value if public, otherwise return a placeholder
/// This is a temporary helper for UI integration during development
pub fn to_string_lossy(&self) -> String {
match self {
Self::Public { value } => String::from_utf8_lossy(value).to_string(),
Self::Private {
declared_len_bytes,
secret_version,
..
} => {
format!(
"[Encrypted: {} bytes, v{}]",
declared_len_bytes, secret_version
)
}
}
}
/// Try to get the public value as bytes, returns None if private
pub fn as_public_bytes(&self) -> Option<&[u8]> {
match self {
Self::Public { value } => Some(value),
Self::Private { .. } => None,
}
}
}
impl fmt::Display for SealedBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string_lossy())
}
}
/// Display metadata for a room (name and optional description)
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct RoomDisplayMetadata {
pub name: SealedBytes,
pub description: Option<SealedBytes>,
}
impl RoomDisplayMetadata {
/// Create public display metadata
pub fn public(name: String, description: Option<String>) -> Self {
Self {
name: SealedBytes::public(name.into_bytes()),
description: description.map(|d| SealedBytes::public(d.into_bytes())),
}
}
/// Create private display metadata
pub fn private(
name_ciphertext: Vec<u8>,
name_nonce: [u8; 12],
name_declared_len: u32,
description: Option<(Vec<u8>, [u8; 12], u32)>,
secret_version: SecretVersion,
) -> Self {
Self {
name: SealedBytes::private(
name_ciphertext,
name_nonce,
secret_version,
name_declared_len,
),
description: description.map(|(ciphertext, nonce, declared_len)| {
SealedBytes::private(ciphertext, nonce, secret_version, declared_len)
}),
}
}
/// Check if both name and description are public
pub fn is_public(&self) -> bool {
self.name.is_public() && self.description.as_ref().is_none_or(|d| d.is_public())
}
/// Check if name is private
pub fn is_private(&self) -> bool {
self.name.is_private()
}
}
impl Default for RoomDisplayMetadata {
fn default() -> Self {
Self::public("Default Room Name".to_string(), None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_privacy_mode_default() {
assert_eq!(PrivacyMode::default(), PrivacyMode::Public);
}
#[test]
fn test_sealed_bytes_public() {
let data = b"test data".to_vec();
let sealed = SealedBytes::public(data.clone());
assert!(sealed.is_public());
assert!(!sealed.is_private());
assert_eq!(sealed.declared_len(), data.len());
assert_eq!(sealed.secret_version(), None);
}
#[test]
fn test_sealed_bytes_private() {
let ciphertext = vec![1, 2, 3, 4];
let nonce = [0u8; 12];
let secret_version = 1;
let declared_len = 10;
let sealed = SealedBytes::private(ciphertext.clone(), nonce, secret_version, declared_len);
assert!(!sealed.is_public());
assert!(sealed.is_private());
assert_eq!(sealed.declared_len(), declared_len as usize);
assert_eq!(sealed.secret_version(), Some(secret_version));
}
}