Skip to content

Commit 393f2bd

Browse files
logist322rainliu
authored andcommitted
Wip
1 parent 6eca9d5 commit 393f2bd

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

srtp/benches/srtp_bench.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ use util::Marshal;
44
use webrtc_srtp::{context::Context, protection_profile::ProtectionProfile};
55

66
fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
7+
let mut setup_ctx = Context::new(
8+
&vec![
9+
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
10+
],
11+
&vec![247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216, 62, 195],
12+
ProtectionProfile::Aes128CmHmacSha1_80,
13+
None,
14+
None,
15+
)
16+
.unwrap();
17+
718
let mut ctx = Context::new(
819
&vec![
920
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
@@ -20,7 +31,7 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
2031
pld.extend_from_slice(&[i as u8]);
2132
}
2233

23-
c.bench_function("Benchmark context ", |b| {
34+
c.bench_function("Benchmark encrypt", |b| {
2435
let mut seq = 1;
2536
b.iter_batched(
2637
|| {
@@ -46,6 +57,33 @@ fn benchmark_encrypt_rtp_aes_128_cm_hmac_sha1(c: &mut Criterion) {
4657
criterion::BatchSize::LargeInput,
4758
);
4859
});
60+
61+
c.bench_function("Benchmark decrypt", |b| {
62+
let mut seq = 1;
63+
b.iter_batched(
64+
|| {
65+
let pkt = rtp::packet::Packet {
66+
header: rtp::header::Header {
67+
sequence_number: seq,
68+
timestamp: seq.into(),
69+
extension_profile: 48862,
70+
marker: true,
71+
padding: false,
72+
extension: true,
73+
payload_type: 96,
74+
..Default::default()
75+
},
76+
payload: pld.clone().into(),
77+
};
78+
seq += 1;
79+
setup_ctx.encrypt_rtp(&pkt.marshal().unwrap()).unwrap()
80+
},
81+
|encrypted| {
82+
ctx.decrypt_rtp(&encrypted).unwrap()
83+
},
84+
criterion::BatchSize::LargeInput,
85+
);
86+
});
4987
}
5088

5189
criterion_group!(benches, benchmark_encrypt_rtp_aes_128_cm_hmac_sha1);

srtp/src/cipher/cipher_aes_cm_hmac_sha1.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ impl Cipher for CipherAesCmHmacSha1 {
207207
if encrypted.len() < self.auth_tag_len() {
208208
return Err(Error::SrtpTooSmall(encrypted.len(), self.auth_tag_len()));
209209
}
210+
let header_len = header.marshal_size();
210211

211212
let mut writer = BytesMut::with_capacity(encrypted.len() - self.auth_tag_len());
212213

@@ -224,22 +225,20 @@ impl Cipher for CipherAesCmHmacSha1 {
224225
}
225226

226227
// Write cipher_text to the destination buffer.
227-
writer.extend_from_slice(cipher_text);
228+
writer.extend_from_slice(&cipher_text[..header_len]);
228229

229230
// Decrypt the ciphertext for the payload.
230-
let counter = generate_counter(
231+
let nonce = generate_counter(
231232
header.sequence_number,
232233
roc,
233234
header.ssrc,
234235
&self.srtp_session_salt,
235236
);
236237

237-
let key = GenericArray::from_slice(&self.srtp_session_key);
238-
let nonce = GenericArray::from_slice(&counter);
239-
let mut stream = Aes128Ctr::new(key, nonce);
240-
let payload_offset = header.marshal_size();
241-
stream.seek(0);
242-
stream.apply_keystream(&mut writer[payload_offset..]);
238+
writer.put_bytes(0, encrypted.len() - header_len - self.auth_tag_len());
239+
self.ctx.encrypt_init(None, None, Some(&nonce)).unwrap();
240+
let count = self.ctx.cipher_update(&cipher_text[header_len..], Some(&mut writer[header_len..])).unwrap();
241+
self.ctx.cipher_final(&mut writer[count..]).unwrap();
243242

244243
Ok(writer.freeze())
245244
}

srtp/src/session/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Session {
9797
tokio::select! {
9898
result = incoming_stream => match result{
9999
Ok(()) => {},
100-
Err(err) => log::error!("{}", err),
100+
Err(err) => log::info!("{}", err),
101101
},
102102
opt = close_stream => if let Some(ssrc) = opt {
103103
Session::close_stream(&cloned_streams_map, ssrc).await

util/src/replay_detector/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#[cfg(test)]
22
mod replay_detector_test;
33

4-
use std::collections::HashMap;
5-
64
use super::fixed_big_int::*;
75

86
// ReplayDetector is the interface of sequence replay detector.
@@ -86,7 +84,6 @@ pub struct WrappedSlidingWindowDetector {
8684
window_size: usize,
8785
mask: FixedBigInt,
8886
init: bool,
89-
duplicated: HashMap<u64, u8>,
9087
}
9188

9289
impl WrappedSlidingWindowDetector {
@@ -101,27 +98,16 @@ impl WrappedSlidingWindowDetector {
10198
window_size,
10299
mask: FixedBigInt::new(window_size),
103100
init: false,
104-
duplicated: HashMap::new(),
105101
}
106102
}
107103
}
108104

109105
impl ReplayDetector for WrappedSlidingWindowDetector {
110106
fn check(&mut self, seq: u64) -> bool {
111-
self.duplicated
112-
.entry(seq)
113-
.and_modify(|i| *i += 1)
114-
.or_insert(1);
115-
self.accepted = false;
116-
117107
self.accepted = false;
118108

119109
if seq > self.max_seq {
120110
// Exceeded upper limit.
121-
log::error!(
122-
"checked {seq} {}\nupper limit",
123-
self.duplicated.get(&seq).unwrap_or(&0)
124-
);
125111
return false;
126112
}
127113
if !self.init {
@@ -143,18 +129,10 @@ impl ReplayDetector for WrappedSlidingWindowDetector {
143129

144130
if diff >= self.window_size as i64 {
145131
// Too old.
146-
log::error!(
147-
"checked {seq} {}\nold",
148-
self.duplicated.get(&seq).unwrap_or(&0)
149-
);
150132
return false;
151133
}
152134
if diff >= 0 && self.mask.bit(diff as usize) != 0 {
153135
// The sequence number is duplicated.
154-
log::error!(
155-
"checked {seq} {}\nduplicated",
156-
self.duplicated.get(&seq).unwrap_or(&0)
157-
);
158136
return false;
159137
}
160138

0 commit comments

Comments
 (0)