Skip to content

Commit b6b3e47

Browse files
committed
Init padding
1 parent 3e946c0 commit b6b3e47

9 files changed

Lines changed: 73 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

error/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ jsonwebtoken = { workspace = true }
1414
url = "*"
1515
tungstenite = { workspace = true }
1616
reqwest = "0.12"
17-
webrtc = { workspace = true }
17+
webrtc = { workspace = true }
18+
rand = "*"
19+
rand_chacha = "*"

error/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub enum Error {
3939
AuthenticationFailed,
4040
#[error("sess-id does not exist on sdp")]
4141
MissingSessionId,
42+
#[error(transparent)]
43+
RandOs(#[from] rand::rand_core::OsError),
4244
}
4345

4446
impl From<tungstenite::Error> for Error {

libturms/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ p2p = { path = "../p2p" }
2424
error = { path = "../error" }
2525
vodozemac = "0.9"
2626
blake3 = "1.8"
27+
rand = "0.9"
28+
rand_chacha = "0.9"
2729
hex = "0.4"
2830
serde = { workspace = true }
2931
serde_yaml = "0.9"

libturms/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub extern crate error;
55
pub extern crate p2p;
66

77
mod channel;
8+
mod padding;
89

910
use discover::spawn_heartbeat;
1011
use discover::websocket::WebSocket;

libturms/src/padding.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Random crypto-secure padding.
2+
3+
use rand::{RngCore, SeedableRng, TryRngCore};
4+
use rand::rngs::OsRng;
5+
use rand_chacha::ChaCha20Rng;
6+
use error::Result;
7+
8+
// Numbers from specification.
9+
const MIN_LENGTH: usize = 1000; // 1kB.
10+
const PADDING_LENGTH: [usize; 2] = [0, 8192];
11+
12+
/// Padding structure.
13+
#[derive(Debug, Clone)]
14+
pub(crate) struct Padding {
15+
min_length: usize,
16+
padding_length: [usize; 2],
17+
fill_padding: u8,
18+
}
19+
20+
impl Default for Padding {
21+
fn default() -> Self {
22+
Padding {
23+
min_length: MIN_LENGTH,
24+
padding_length: PADDING_LENGTH,
25+
fill_padding: 0, // adds lots of zeros.
26+
}
27+
}
28+
}
29+
30+
impl Padding {
31+
/// Fill an entry with bunch of paddings.
32+
pub fn fill_zero(entry: impl AsRef<[u8]>) -> Result<Vec<u8>> {
33+
let config = Self::default();
34+
let data = entry.as_ref();
35+
let data_len = data.len();
36+
37+
let mut seed = [0u8; 32];
38+
OsRng.try_fill_bytes(&mut seed)?;
39+
40+
let mut rng = ChaCha20Rng::from_seed(seed);
41+
rng.fill_bytes(&mut data);
42+
43+
let base_target = std::cmp::max(data_len, config.min_length);
44+
let total_size = base_target + data.len();
45+
46+
let mut padded_data = Vec::with_capacity(total_size);
47+
padded_data.extend_from_slice(data);
48+
padded_data.resize(total_size, config.fill_padding);
49+
50+
Ok(padded_data)
51+
}
52+
}

p2p/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
//! peer-to-peer communication via WebRTC.
2-
/// Models.
3-
pub mod models;
42
#[forbid(unsafe_code)]
53
#[deny(missing_docs, missing_debug_implementations)]
4+
5+
/// Models.
6+
pub mod models;
67
/// WebRTC interface.
78
pub mod webrtc;
89
/// X3DH over WebRTC for Turms.
910
mod x3dh;
1011

1112
pub use x3dh::triple_diffie_hellman;
1213

13-
use tokio::sync::Mutex;
14+
use parking_lot::Mutex;
1415
use vodozemac::olm::Account;
1516

1617
use std::sync::OnceLock;
@@ -26,7 +27,7 @@ pub fn get_account() -> &'static Mutex<Account> {
2627
pub async fn save_account() -> error::Result<String> {
2728
let account = get_account();
2829

29-
Ok(serde_json::to_string(&account.lock().await.pickle())?)
30+
Ok(serde_json::to_string(&account.lock().pickle())?)
3031
}
3132

3233
/// Set user account.

p2p/src/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum Event {
1717
Typing,
1818
}
1919

20+
/// Triple-diffie Hellman exchange.
2021
#[derive(Debug, Clone, Serialize, Deserialize)]
2122
pub struct X3DH {
2223
/// Curve25519 public key.
@@ -76,6 +77,7 @@ bitflags! {
7677
/// Represents a set of message/attachment flags.
7778
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7879
pub struct Flags: u32 {
80+
/// Message flagged as urgent.
7981
const URGENT = 1 << 0;
8082
/// Message MUST NOT be saved.
8183
const EPHEMERAL = 1 << 1;

p2p/src/x3dh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub async fn triple_diffie_hellman(
1515

1616
// Generate public key and one-time key.
1717
let (public_key, otk) = {
18-
let mut account = account.lock().await;
18+
let mut account = account.lock();
1919

2020
account.generate_one_time_keys(1);
2121

@@ -38,7 +38,7 @@ pub async fn triple_diffie_hellman(
3838
}))?;
3939

4040
if acc.send(message).await.is_ok() {
41-
account.lock().await.mark_keys_as_published();
41+
account.lock().mark_keys_as_published();
4242
tracing::debug!("public key and one-time key published");
4343
};
4444

0 commit comments

Comments
 (0)