rtci-dtls 0.4.0

Sans-I/O DTLS 1.2 and 1.3 (RFC 6347, RFC 9147) with DTLS-SRTP key export, for WebRTC
Documentation
//! The beginnings of the datagrams an association sent last, for checking the quote of a
//! transport report against them (RFC 8899 section 4.6.1: a PTB message is validated by "the
//! value of a protocol header field known only to the two PL endpoints"; RFC 8201 section 4:
//! a node "SHOULD" check what is quoted). A DTLS record header carries the epoch and a 48-bit
//! sequence number (DTLS 1.3: encrypted sequence number bits and ciphertext), which an
//! off-path attacker does not know.

/// Bytes kept of each datagram.
const KEPT: usize = 32;
/// Datagrams kept: a report comes back within a round trip, while more datagrams may have
/// been sent since.
const DATAGRAMS: usize = 64;
/// The shortest quote that can be checked: a DTLS 1.2 record header (RFC 6347 section 4.1).
pub(crate) const MIN_QUOTE: usize = 13;

/// A ring of the first [`KEPT`] bytes of the last [`DATAGRAMS`] datagrams sent. Allocated
/// once, written without allocating.
pub(crate) struct SentDatagrams {
    starts: Box<[[u8; KEPT]; DATAGRAMS]>,
    lengths: [u8; DATAGRAMS],
    next: usize,
}

impl Default for SentDatagrams {
    fn default() -> Self {
        Self {
            starts: Box::new([[0; KEPT]; DATAGRAMS]),
            lengths: [0; DATAGRAMS],
            next: 0,
        }
    }
}

impl SentDatagrams {
    pub(crate) fn record(&mut self, datagram: &[u8]) {
        let kept = datagram.len().min(KEPT);
        self.starts[self.next][..kept].copy_from_slice(&datagram[..kept]);
        self.lengths[self.next] = kept as u8;
        self.next = (self.next + 1) % DATAGRAMS;
    }

    /// Whether `quoted` begins one of the datagrams kept (compared over what both have, at
    /// most [`KEPT`] bytes). `None` when it is shorter than [`MIN_QUOTE`]: it cannot be told.
    pub(crate) fn begins_with(&self, quoted: &[u8]) -> Option<bool> {
        if quoted.len() < MIN_QUOTE {
            return None;
        }
        Some(self.starts.iter().zip(self.lengths).any(|(start, length)| {
            let compared = quoted.len().min(usize::from(length));
            compared >= MIN_QUOTE && start[..compared] == quoted[..compared]
        }))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn a_quote_matches_only_the_beginning_of_a_datagram_sent() {
        let mut sent = SentDatagrams::default();
        let datagram: Vec<u8> = (0..100).collect();
        sent.record(&datagram);
        assert_eq!(sent.begins_with(&datagram[..12]), None, "too short to tell");
        assert_eq!(sent.begins_with(&datagram[..13]), Some(true));
        assert_eq!(sent.begins_with(&datagram), Some(true), "a longer quote");
        assert_eq!(sent.begins_with(&datagram[1..40]), Some(false));
        for other in 0..DATAGRAMS as u8 {
            sent.record(&[other + 101; 40]);
        }
        assert_eq!(sent.begins_with(&datagram), Some(false), "pushed out");
    }
}