Skip to content

Commit

Permalink
Reduce the range of initial sequence number
Browse files Browse the repository at this point in the history
  • Loading branch information
xnorpx committed Aug 6, 2024
1 parent ae5bcd7 commit 426e74c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/rtp/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ impl SeqNo {
}
}

impl Default for SeqNo {
fn default() -> Self {
// https://www.rfc-editor.org/rfc/rfc3550#page-13
// The initial value of the sequence number SHOULD be random (unpredictable)
// to make known-plaintext attacks on encryption more difficult
// Upper half of range is avoided in order to prevent SRTP wraparound issues
// during startup.
// Sequence number 0 is avoided for historical reasons, presumably to avoid
// debugability or test usage conflicts.
// i.e the range is (1, 2^15-1)
Self((NonCryptographicRng::u16() % 32767 + 1) as u64)
}
}

impl Pt {
/// Create a PT with a specific value.
///
Expand Down
12 changes: 3 additions & 9 deletions src/streams/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use crate::session::PacketReceipt;
use crate::stats::MediaEgressStats;
use crate::stats::StatsSnapshot;
use crate::util::value_history::ValueHistory;
use crate::util::InstantExt;
use crate::util::{already_happened, calculate_rtt_ms, not_happening};
use crate::util::{InstantExt, NonCryptographicRng};
use crate::RtcError;

use super::rtx_cache::RtxCache;
Expand Down Expand Up @@ -152,12 +152,6 @@ pub(crate) struct StreamTxStats {

impl StreamTx {
pub(crate) fn new(ssrc: Ssrc, rtx: Option<Ssrc>, mid: Mid, rid: Option<Rid>) -> Self {
// https://www.rfc-editor.org/rfc/rfc3550#page-13
// The initial value of the sequence number SHOULD be random (unpredictable)
// to make known-plaintext attacks on encryption more difficult
let seq_no = (NonCryptographicRng::u16() as u64).into();
let seq_no_rtx = (NonCryptographicRng::u16() as u64).into();

debug!("Create StreamTx for SSRC: {}", ssrc);

StreamTx {
Expand All @@ -168,8 +162,8 @@ impl StreamTx {
kind: None,
cname: None,
clock_rate: None,
seq_no,
seq_no_rtx,
seq_no: SeqNo::default(),
seq_no_rtx: SeqNo::default(),
last_used: already_happened(),
rtp_and_wallclock: None,
send_queue: SendQueue::new(),
Expand Down

0 comments on commit 426e74c

Please sign in to comment.