use super::test_support::*;
use super::*;
use crate::test_util::{hex, provider};
use crypto::{HmacAlgorithm, StreamCipherAlgorithm};
const SSRC: u32 = 0xCAFE_BABE;
struct KeyVector {
master_key: &'static str,
master_salt: &'static str,
cipher_key: &'static str,
cipher_salt: &'static str,
auth_key: &'static str,
algorithm: StreamCipherAlgorithm,
}
const AES_128: KeyVector = KeyVector {
master_key: "E1F97A0D3E018BE0D64FA32C06DE4139",
master_salt: "0EC675AD498AFEEBB6960B3AABE6",
cipher_key: "C61E7A93744F39EE10734AFE3FF7A087",
cipher_salt: "30CBBC08863D8C85D49DB34A9AE1",
auth_key: "CEBE321F6FF7716B6FD4AB49AF256A156D38BAA4",
algorithm: StreamCipherAlgorithm::Aes128Ctr,
};
const AES_192: KeyVector = KeyVector {
master_key: "73edc66c4fa15776fb57f9505c17136550ffda71f3e8e5f1",
master_salt: "c8522f3acd4ce86d5add78edbb11",
cipher_key: "31874736a8f1143870c26e4857d8a5b2c4a354407faadabb",
cipher_salt: "2372b82d639b6d8503a47adc0a6c",
auth_key: "355b10973cd95b9eacf4061c7e1a7151e7cfbfcb",
algorithm: StreamCipherAlgorithm::Aes192Ctr,
};
const AES_256: KeyVector = KeyVector {
master_key: "f0f04914b513f2763a1b1fa130f10e2998f6f6e43e4309d1e622a0e332b9f1b6",
master_salt: "3b04803de51ee7c96423ab5b78d2",
cipher_key: "5ba1064e30ec51613cad926c5a28ef731ec7fb397f70a960653caf06554cd8c4",
cipher_salt: "fa31791685ca444a9e07c6c64e93",
auth_key: "fd9c32d39ed5fbb5a9dc96b30818454d1313dc05",
algorithm: StreamCipherAlgorithm::Aes256Ctr,
};
fn expected_srtp(vector: &KeyVector, packet: &[u8], encrypt: bool, tag_len: usize) -> Vec<u8> {
let provider = provider();
let mut iv = [0u8; 16];
iv[..14].copy_from_slice(&hex(vector.cipher_salt));
for (dst, src) in iv[4..8].iter_mut().zip(&packet[8..12]) {
*dst ^= src;
}
iv[12] ^= packet[2];
iv[13] ^= packet[3];
let mut out = packet.to_vec();
if encrypt {
provider
.crypto()
.new_stream_cipher(vector.algorithm, &hex(vector.cipher_key))
.unwrap()
.apply_keystream(&iv, &mut out[12..])
.unwrap();
}
let mut tag = [0u8; 20];
provider
.crypto()
.new_hmac(HmacAlgorithm::Sha1, &hex(vector.auth_key))
.unwrap()
.sign(&[&out, &[0, 0, 0, 0]], &mut tag)
.unwrap();
out.extend_from_slice(&tag[..tag_len]);
out
}
fn vector_context(vector: &KeyVector, profile: ProtectionProfile) -> Context {
Context::new(
&hex(vector.master_key),
&hex(vector.master_salt),
profile,
None,
None,
provider().crypto(),
)
.unwrap()
}
#[test]
fn aes_cm_packets_match_the_rfc_key_derivation_vectors() {
let packet = rtp(SSRC, 0x1234, b"Gallia est omnis divisa in partes tres");
for (vector, profile, tag_len) in [
(&AES_128, ProtectionProfile::Aes128CmHmacSha1_80, 10),
(&AES_128, ProtectionProfile::Aes128CmHmacSha1_32, 4),
(&AES_192, ProtectionProfile::Aes192CmHmacSha1_80, 10),
(&AES_192, ProtectionProfile::Aes192CmHmacSha1_32, 4),
(&AES_256, ProtectionProfile::Aes256CmHmacSha1_80, 10),
(&AES_256, ProtectionProfile::Aes256CmHmacSha1_32, 4),
] {
let expected = expected_srtp(vector, &packet, true, tag_len);
let protected = vector_context(vector, profile)
.encrypt_rtp(&packet)
.unwrap();
assert_eq!(&protected[..], &expected[..], "{profile:?}");
assert_ne!(&protected[12..packet.len()], &packet[12..], "{profile:?}");
assert_eq!(
&vector_context(vector, profile)
.decrypt_rtp(&expected)
.unwrap()[..],
&packet[..],
"{profile:?}"
);
}
}
#[test]
fn null_cipher_leaves_the_payload_and_authenticates_the_packet() {
let packet = rtp(SSRC, 0x1234, b"in the clear, by negotiation");
for (profile, tag_len) in [
(ProtectionProfile::NullHmacSha1_80, 10),
(ProtectionProfile::NullHmacSha1_32, 4),
] {
assert!(profile.is_null_cipher());
let expected = expected_srtp(&AES_128, &packet, false, tag_len);
let protected = vector_context(&AES_128, profile)
.encrypt_rtp(&packet)
.unwrap();
assert_eq!(&protected[..], &expected[..], "{profile:?}");
assert_eq!(&protected[..packet.len()], &packet[..], "payload as it was");
let mut receiver = vector_context(&AES_128, profile);
assert_eq!(&receiver.decrypt_rtp(&protected).unwrap()[..], &packet[..]);
assert_eq!(
receiver.decrypt_rtp(&protected).unwrap_err(),
Error::SrtpSsrcDuplicated(SSRC, 0x1234)
);
let mut forged = protected.to_vec();
forged[14] ^= 1;
assert_eq!(
vector_context(&AES_128, profile)
.decrypt_rtp(&forged)
.unwrap_err(),
Error::RtpFailedToVerifyAuthTag
);
let report = vector_context(&AES_128, profile)
.encrypt_rtcp(&rtcp())
.unwrap();
assert_eq!(report.len(), rtcp().len() + 4 + 10);
assert_eq!(&report[..rtcp().len()], &rtcp()[..]);
assert_eq!(esrtcp(profile, &report, 0), 0);
assert_eq!(
&vector_context(&AES_128, profile)
.decrypt_rtcp(&report)
.unwrap()[..],
&rtcp()[..]
);
let mut forged = report.to_vec();
forged[9] ^= 1;
assert_eq!(
vector_context(&AES_128, profile)
.decrypt_rtcp(&forged)
.unwrap_err(),
Error::RtcpFailedToVerifyAuthTag
);
}
}
#[test]
fn the_32_bit_tag_suites_keep_an_80_bit_srtcp_tag() {
for (short, full) in [
(
ProtectionProfile::Aes128CmHmacSha1_32,
ProtectionProfile::Aes128CmHmacSha1_80,
),
(
ProtectionProfile::Aes192CmHmacSha1_32,
ProtectionProfile::Aes192CmHmacSha1_80,
),
(
ProtectionProfile::Aes256CmHmacSha1_32,
ProtectionProfile::Aes256CmHmacSha1_80,
),
(
ProtectionProfile::NullHmacSha1_32,
ProtectionProfile::NullHmacSha1_80,
),
] {
assert_eq!(
(short.rtp_auth_tag_len(), short.rtcp_auth_tag_len()),
(4, 10)
);
assert_eq!(
(full.rtp_auth_tag_len(), full.rtcp_auth_tag_len()),
(10, 10)
);
let packet = rtp(SSRC, 77, b"tag lengths");
let with_full = context(full).encrypt_rtp(&packet).unwrap();
let with_short = context(short).encrypt_rtp(&packet).unwrap();
assert_eq!(with_short.len(), packet.len() + 4);
assert_eq!(
&with_short[..],
&with_full[..with_full.len() - 6],
"{short:?}"
);
let report = context(short).encrypt_rtcp(&rtcp()).unwrap();
assert_eq!(
report,
context(full).encrypt_rtcp(&rtcp()).unwrap(),
"{short:?}"
);
assert_eq!(report.len(), rtcp().len() + 4 + 10);
assert!(
context(short)
.decrypt_rtcp(&report[..report.len() - 6])
.is_err()
);
}
}
#[test]
fn a_context_without_options_has_replay_protection() {
for profile in ProtectionProfile::ALL {
let mut sender = context(profile);
let mut receiver = context(profile);
let packet = sender.encrypt_rtp(&rtp(SSRC, 1, b"once")).unwrap();
receiver.decrypt_rtp(&packet).unwrap();
assert_eq!(
receiver.decrypt_rtp(&packet).unwrap_err(),
Error::SrtpSsrcDuplicated(SSRC, 1),
"{profile:?}"
);
let report = sender.encrypt_rtcp(&rtcp()).unwrap();
receiver.decrypt_rtcp(&report).unwrap();
assert_eq!(
receiver.decrypt_rtcp(&report).unwrap_err(),
Error::SrtcpSsrcDuplicated(RTCP_SSRC, 0),
"{profile:?}"
);
}
}
#[test]
fn the_default_replay_window_is_64_packets() {
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let mut sender = context(profile);
let packets: Vec<_> = (0..=100u16)
.map(|seq| sender.encrypt_rtp(&rtp(SSRC, seq, b"x")).unwrap())
.collect();
let mut receiver = context(profile);
receiver.decrypt_rtp(&packets[100]).unwrap();
receiver.decrypt_rtp(&packets[37]).unwrap();
assert!(receiver.decrypt_rtp(&packets[37]).is_err());
assert!(receiver.decrypt_rtp(&packets[36]).is_err());
}
#[test]
fn replay_protection_is_off_only_when_asked() {
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let (key, salt) = key_material(profile);
let mut receiver = Context::new(
&key,
&salt,
profile,
Some(srtp_no_replay_protection()),
Some(srtcp_no_replay_protection()),
provider().crypto(),
)
.unwrap();
let mut sender = context(profile);
let packet = sender.encrypt_rtp(&rtp(SSRC, 1, b"x")).unwrap();
let report = sender.encrypt_rtcp(&rtcp()).unwrap();
for _ in 0..2 {
receiver.decrypt_rtp(&packet).unwrap();
receiver.decrypt_rtcp(&report).unwrap();
}
}
#[test]
fn a_replay_window_below_64_is_refused() {
for window in [0, 1, 32, 63] {
assert!(srtp_replay_protection(window).is_err(), "{window}");
assert!(srtcp_replay_protection(window).is_err(), "{window}");
}
for window in [64, 65, 1024, 4096] {
assert!(srtp_replay_protection(window).is_ok(), "{window}");
assert!(srtcp_replay_protection(window).is_ok(), "{window}");
}
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let (key, salt) = key_material(profile);
let mut sender = context(profile);
let first = sender.encrypt_rtp(&rtp(SSRC, 1, b"x")).unwrap();
let second = sender.encrypt_rtp(&rtp(SSRC, 2, b"x")).unwrap();
let mut receiver = Context::new(
&key,
&salt,
profile,
Some(srtp_replay_protection(MIN_REPLAY_PROTECTION_WINDOW).unwrap()),
None,
provider().crypto(),
)
.unwrap();
receiver.decrypt_rtp(&second).unwrap();
receiver.decrypt_rtp(&first).unwrap();
}
#[test]
fn a_forged_packet_leaves_an_existing_stream_untouched() {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let mut sender = context(profile);
let mut receiver = context(profile);
let packet = sender.encrypt_rtp(&rtp(SSRC, 0xFFF0, b"x")).unwrap();
receiver.decrypt_rtp(&packet).unwrap();
let before = receiver.srtp_ssrc_states[&SSRC].index;
let mut forged = context(profile)
.encrypt_rtp(&rtp(SSRC, 5, b"x"))
.unwrap()
.to_vec();
let last = forged.len() - 1;
forged[last] ^= 1;
assert!(receiver.decrypt_rtp(&forged).is_err());
assert_eq!(
receiver.srtp_ssrc_states[&SSRC].index, before,
"{profile:?}"
);
assert_eq!(receiver.get_roc(SSRC), Some(0));
let next = sender.encrypt_rtp(&rtp(SSRC, 0xFFF1, b"x")).unwrap();
receiver.decrypt_rtp(&next).unwrap();
assert!(receiver.decrypt_rtp(&packet).is_err());
assert_eq!(receiver.key_usage().srtp_packets, 2);
}
}
#[test]
fn aes_cm_refuses_more_than_2_pow_16_keystream_blocks() {
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let limit = (1 << 16) * 16;
let largest = rtp(SSRC, 1, &vec![0x5A; limit]);
let protected = context(profile).encrypt_rtp(&largest).unwrap();
assert_eq!(
&context(profile).decrypt_rtp(&protected).unwrap()[..],
&largest[..]
);
let too_large = rtp(SSRC, 2, &vec![0x5A; limit + 1]);
assert!(context(profile).encrypt_rtp(&too_large).is_err());
let mut padded = too_large.clone();
padded.extend_from_slice(&[0; 10]);
assert!(context(profile).decrypt_rtp(&padded).is_err());
let mut report = rtcp();
report.resize(8 + limit + 4, 0);
assert!(context(profile).encrypt_rtcp(&report).is_err());
report.truncate(8 + limit);
context(profile).encrypt_rtcp(&report).unwrap();
}
#[test]
fn dtls_srtp_code_points_are_the_registered_ones() {
let registered = [
(0x0001, ProtectionProfile::Aes128CmHmacSha1_80),
(0x0002, ProtectionProfile::Aes128CmHmacSha1_32),
(0x0005, ProtectionProfile::NullHmacSha1_80),
(0x0006, ProtectionProfile::NullHmacSha1_32),
(0x0007, ProtectionProfile::AeadAes128Gcm),
(0x0008, ProtectionProfile::AeadAes256Gcm),
];
for (id, profile) in registered {
assert_eq!(profile.dtls_srtp_id(), Some(id));
assert_eq!(ProtectionProfile::from_dtls_srtp_id(id), Some(profile));
}
for profile in ProtectionProfile::ALL {
if !registered.iter().any(|(_, known)| *known == profile) {
assert_eq!(profile.dtls_srtp_id(), None, "{profile:?}");
}
}
for id in [0x0000, 0x0003, 0x0004, 0x0009, 0x000B, 0xFFFF] {
assert_eq!(ProtectionProfile::from_dtls_srtp_id(id), None, "{id:#06x}");
}
}
#[test]
fn key_and_salt_lengths_are_those_of_the_suite() {
use ProtectionProfile::*;
for (profile, key_len, salt_len, rtp_tag, rtcp_tag, aead_tag) in [
(Aes128CmHmacSha1_80, 16, 14, 10, 10, 0),
(Aes128CmHmacSha1_32, 16, 14, 4, 10, 0),
(Aes192CmHmacSha1_80, 24, 14, 10, 10, 0),
(Aes192CmHmacSha1_32, 24, 14, 4, 10, 0),
(Aes256CmHmacSha1_80, 32, 14, 10, 10, 0),
(Aes256CmHmacSha1_32, 32, 14, 4, 10, 0),
(NullHmacSha1_80, 16, 14, 10, 10, 0),
(NullHmacSha1_32, 16, 14, 4, 10, 0),
(AeadAes128Gcm, 16, 12, 0, 0, 16),
(AeadAes256Gcm, 32, 12, 0, 0, 16),
] {
assert_eq!(
(
profile.key_len(),
profile.salt_len(),
profile.rtp_auth_tag_len(),
profile.rtcp_auth_tag_len(),
profile.aead_auth_tag_len(),
profile.auth_key_len(),
),
(
key_len,
salt_len,
rtp_tag,
rtcp_tag,
aead_tag,
if aead_tag == 0 { 20 } else { 0 }
),
"{profile:?}"
);
let crypto_provider = provider();
let new = |key_len: usize, salt_len: usize| {
Context::new(
&vec![1; key_len],
&vec![2; salt_len],
profile,
None,
None,
crypto_provider.crypto(),
)
};
assert_eq!(new(key_len, salt_len).unwrap().profile(), profile);
assert!(new(key_len + 8, salt_len).is_err(), "{profile:?}");
assert!(new(key_len, salt_len + 2).is_err(), "{profile:?}");
assert!(new(key_len, salt_len - 2).is_err(), "{profile:?}");
}
}