use super::*;
use crate::test_util::provider;
use shared::marshal::Marshal;
fn context(profile: ProtectionProfile) -> Result<Context> {
Context::new(
&vec![0x2b; profile.key_len()],
&vec![0x7e; profile.salt_len()],
profile,
Some(srtp_replay_protection(64)?),
None,
provider().crypto(),
)
}
fn rtp(ssrc: u32, sequence_number: u16, payload: &[u8]) -> Result<Vec<u8>> {
Ok(rtp::Packet {
header: rtp::Header {
version: 2,
payload_type: 96,
sequence_number,
ssrc,
..Default::default()
},
payload: bytes::Bytes::copy_from_slice(payload),
}
.marshal()?
.to_vec())
}
#[test]
fn each_ssrc_has_its_own_cryptographic_state() -> Result<()> {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let mut sender = context(profile)?;
let mut receiver = context(profile)?;
let a = sender.encrypt_rtp(&rtp(0xa, 100, b"same payload")?)?;
let b = sender.encrypt_rtp(&rtp(0xb, 100, b"same payload")?)?;
assert_ne!(a[12..], b[12..], "{profile:?}: another keystream per SSRC");
assert_eq!(
receiver.decrypt_rtp(&a)?.to_vec(),
rtp(0xa, 100, b"same payload")?
);
assert_eq!(
receiver.decrypt_rtp(&b)?.to_vec(),
rtp(0xb, 100, b"same payload")?
);
assert!(
receiver.decrypt_rtp(&a).is_err(),
"{profile:?}: a replay of a"
);
let later_b = sender.encrypt_rtp(&rtp(0xb, 101, b"b again")?)?;
assert_eq!(
receiver.decrypt_rtp(&later_b)?.to_vec(),
rtp(0xb, 101, b"b again")?
);
assert_eq!(receiver.srtp_ssrc_states.len(), 2, "{profile:?}");
assert_eq!(receiver.srtp_ssrc_states[&0xa].index, 100);
assert_eq!(receiver.srtp_ssrc_states[&0xb].index, 101);
}
Ok(())
}
#[test]
fn gcm_protection_of_a_packet_does_not_depend_on_the_ones_before() -> Result<()> {
for profile in [
ProtectionProfile::AeadAes128Gcm,
ProtectionProfile::AeadAes256Gcm,
] {
let mut busy = context(profile)?;
for sequence_number in 1..40u16 {
busy.encrypt_rtp(&rtp(0xc, sequence_number, &[sequence_number as u8; 1200])?)?;
}
let packet = rtp(0xc, 40, &[0x55; 1200])?;
let after_others = busy.encrypt_rtp(&packet)?;
let mut fresh = context(profile)?;
let alone = fresh.encrypt_rtp(&packet)?;
assert_eq!(after_others, alone, "{profile:?}");
}
Ok(())
}