use super::test_support::*;
use super::*;
use crate::cryptex::Cryptex;
use crate::test_util::{hex, provider};
const MASTER_KEY: &str = "E1F97A0D3E018BE0D64FA32C06DE4139";
const MASTER_SALT: &str = "0EC675AD498AFEEBB6960B3AABE6";
const PLAIN: &str = "900f1234 decafbad cafebabe BEDE0006
17414273 A4752627 48220000 C8308E46 55996386 B395FB00
abababab abababab abababab abababab";
fn ids(ids: &[u8]) -> EncryptedHeaderExtensions {
EncryptedHeaderExtensions::new(ids).unwrap()
}
fn rfc_context(profile: ProtectionProfile, encrypted: &[u8]) -> Context {
let salt = hex(MASTER_SALT);
let mut context = Context::new(
&hex(MASTER_KEY),
&salt[..profile.salt_len()],
profile,
None,
None,
provider().crypto(),
)
.unwrap();
context.set_encrypted_header_extensions(ids(encrypted));
context
}
#[test]
fn aes_cm_header_extension_encryption_matches_rfc_6904_appendix_a2() {
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let plain = hex(PLAIN);
let protected = rfc_context(profile, &[1, 3, 4])
.encrypt_rtp(&plain)
.unwrap();
assert_eq!(
&protected[16..40],
&hex("17588A9270F4E15E1C220000C8309546A994F0BC54789700")[..]
);
assert_eq!(
&protected[..],
&hex("900f1234 decafbad cafebabe BEDE0006
17588A92 70F4E15E 1C220000 C8309546 A994F0BC 54789700
4e55dc4c e79978d8 8ca4d215 949d2402
5a46b3ca 35c535a8 91c7")[..]
);
assert_eq!(
&rfc_context(profile, &[1, 3, 4])
.decrypt_rtp(&protected)
.unwrap()[..],
&plain[..]
);
}
#[test]
fn aead_aes_128_gcm_header_extension_encryption_interoperates_with_libsrtp() {
let profile = ProtectionProfile::AeadAes128Gcm;
let plain = hex(PLAIN);
let protected = rfc_context(profile, &[1, 3, 4])
.encrypt_rtp(&plain)
.unwrap();
assert_eq!(protected.len(), plain.len() + 16);
assert_eq!(
&protected[..plain.len() + 8],
&hex("900f1234 decafbad cafebabe BEDE0006
1712e020 5bfa949b 1C220000 C830bb46 732778d9 929aab00
0eca0cf9 5ee955b2 6cd3d288 b49f6ca9
f4b1b759 719eb5bc")[..]
);
assert_eq!(
&rfc_context(profile, &[1, 3, 4])
.decrypt_rtp(&protected)
.unwrap()[..],
&plain[..]
);
}
fn packet_with_extensions(two_byte: bool, seq: u16) -> (Vec<u8>, std::ops::Range<usize>) {
let mut packet = vec![0x92, 96];
packet.extend_from_slice(&seq.to_be_bytes());
packet.extend_from_slice(&[0, 0, 0, 1, 0xCA, 0xFE, 0xBA, 0xBE]);
packet.extend_from_slice(&[0, 0, 0, 0x11, 0, 0, 0, 0x22]);
let data: Vec<u8> = if two_byte {
hex("01 03 A1A2A3 02 01 B1 03 00 04 04 D1D2D3D4 00 05 02 E1E2 00 00 00")
} else {
hex("12 A1A2A3 20 B1 33 C1C2C3C4 00 43 D1D2D3D4 51 E1E2 00 00 00 00")
};
assert_eq!(data.len() % 4, 0);
packet.extend_from_slice(if two_byte {
&[0x10, 0x00]
} else {
&[0xBE, 0xDE]
});
packet.extend_from_slice(&((data.len() / 4) as u16).to_be_bytes());
let start = packet.len();
packet.extend_from_slice(&data);
let end = packet.len();
packet.extend_from_slice(b"the payload of the packet");
(packet, start..end)
}
#[test]
fn only_the_selected_elements_are_encrypted_and_they_round_trip() {
for profile in ProtectionProfile::ALL {
for two_byte in [false, true] {
let (plain, extension) = packet_with_extensions(two_byte, 100);
let selected = [2u8, 4];
let mut sender = context(profile);
sender.set_encrypted_header_extensions(ids(&selected));
assert_eq!(
sender
.encrypted_header_extensions()
.ids()
.collect::<Vec<_>>(),
selected
);
let protected = sender.encrypt_rtp(&plain).unwrap();
let changed: Vec<usize> = extension
.clone()
.filter(|&at| protected[at] != plain[at])
.map(|at| at - extension.start)
.collect();
let expected: Vec<usize> = if profile.is_null_cipher() {
vec![]
} else if two_byte {
vec![7, 12, 13, 14, 15]
} else {
vec![5, 13, 14, 15, 16]
};
assert_eq!(changed, expected, "{profile:?} two-byte {two_byte}");
assert_eq!(&protected[..extension.start], &plain[..extension.start]);
let mut receiver = context(profile);
receiver.set_encrypted_header_extensions(ids(&selected));
assert_eq!(&receiver.decrypt_rtp(&protected).unwrap()[..], &plain[..]);
let unaware = context(profile).decrypt_rtp(&protected).unwrap();
assert_eq!(&unaware[extension.clone()], &protected[extension.clone()]);
assert_eq!(&unaware[extension.end..], &plain[extension.end..]);
}
}
}
#[test]
fn encrypted_header_extensions_are_authenticated_as_sent() {
for profile in ProtectionProfile::ALL {
let (plain, extension) = packet_with_extensions(false, 7);
let mut sender = context(profile);
sender.set_encrypted_header_extensions(ids(&[1, 2, 3, 4, 5]));
let protected = sender.encrypt_rtp(&plain).unwrap();
for at in [extension.start + 1, extension.start + 5, extension.end - 4] {
let mut forged = protected.to_vec();
forged[at] ^= 0x40;
let mut receiver = context(profile);
receiver.set_encrypted_header_extensions(ids(&[1, 2, 3, 4, 5]));
let error = receiver.decrypt_rtp(&forged).unwrap_err();
assert!(
matches!(
error,
Error::RtpFailedToVerifyAuthTag | Error::ErrFailedToVerifyAuthTag
),
"{profile:?}: {error}"
);
}
}
}
#[test]
fn clear_header_extensions_are_authenticated_as_sent() {
for profile in ProtectionProfile::ALL {
for two_byte in [false, true] {
let (plain, extension) = packet_with_extensions(two_byte, 9);
let protected = context(profile).encrypt_rtp(&plain).unwrap();
assert_eq!(
&protected[extension.clone()],
&plain[extension.clone()],
"{profile:?}: the extensions stay readable"
);
let data = if two_byte { [2, 3, 7] } else { [1, 2, 5] };
for at in data.map(|offset| extension.start + offset) {
let mut forged = protected.to_vec();
forged[at] ^= 0x01;
let error = context(profile).decrypt_rtp(&forged).unwrap_err();
assert!(
matches!(
error,
Error::RtpFailedToVerifyAuthTag | Error::ErrFailedToVerifyAuthTag
),
"{profile:?} two-byte {two_byte}, octet {at}: {error}"
);
}
}
}
}
#[test]
fn header_keystream_is_per_packet_and_separate_from_the_payload_keystream() {
let profile = ProtectionProfile::Aes128CmHmacSha1_80;
let mut sender = context(profile);
sender.set_encrypted_header_extensions(ids(&[1]));
let element = |seq: u16| -> Vec<u8> {
let mut packet = vec![0x90, 96];
packet.extend_from_slice(&seq.to_be_bytes());
packet.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 9, 0xBE, 0xDE, 0, 5]);
packet.push(0x1F);
packet.extend_from_slice(&[0x77; 16]);
packet.extend_from_slice(&[0, 0, 0]);
packet.extend_from_slice(&[0x77; 16]);
packet
};
let first = sender.encrypt_rtp(&element(1)).unwrap();
let second = sender.encrypt_rtp(&element(2)).unwrap();
assert_ne!(&first[17..33], &[0x77; 16][..], "the element is encrypted");
assert_ne!(&first[17..33], &second[17..33], "per-packet keystream");
assert_ne!(&first[17..33], &first[36..52], "not the payload keystream");
}
#[test]
fn a_cryptex_packet_is_not_also_encrypted_as_rfc_6904_has_it() {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let (plain, extension) = packet_with_extensions(false, 55);
let send = Cryptex {
send: true,
require: false,
};
let mut both = context(profile);
both.set_cryptex(send);
both.set_encrypted_header_extensions(ids(&[2, 4]));
let mut cryptex_only = context(profile);
cryptex_only.set_cryptex(send);
let protected = both.encrypt_rtp(&plain).unwrap();
assert_eq!(
protected,
cryptex_only.encrypt_rtp(&plain).unwrap(),
"{profile:?}: Cryptex alone"
);
let mut receiver = context(profile);
receiver.set_encrypted_header_extensions(ids(&[2, 4]));
assert_eq!(&receiver.decrypt_rtp(&protected).unwrap()[..], &plain[..]);
let mut rfc6904 = context(profile);
rfc6904.set_encrypted_header_extensions(ids(&[2, 4]));
let (plain, _) = packet_with_extensions(false, 56);
let protected = rfc6904.encrypt_rtp(&plain).unwrap();
assert_eq!(
&protected[extension.start - 4..extension.start - 2],
&[0xBE, 0xDE]
);
assert_ne!(&protected[extension.clone()], &plain[extension.clone()]);
assert_eq!(&receiver.decrypt_rtp(&protected).unwrap()[..], &plain[..]);
}
}
#[test]
fn packets_without_selected_elements_are_protected_as_usual() {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes256Gcm,
] {
let (with_extension, _) = packet_with_extensions(true, 1);
let mut profile_specific = with_extension.clone();
profile_specific[20..22].copy_from_slice(&[0xAB, 0xCD]);
for (packet, selected) in [
(rtp(1, 1, b"no extension"), vec![1u8, 2, 3]),
(profile_specific, vec![1, 2, 3, 4, 5]),
(with_extension, vec![9, 200]),
] {
let mut sender = context(profile);
sender.set_encrypted_header_extensions(ids(&selected));
assert_eq!(
sender.encrypt_rtp(&packet).unwrap(),
context(profile).encrypt_rtp(&packet).unwrap(),
"{profile:?}"
);
}
}
}
#[test]
fn a_header_that_is_not_the_packets_gets_no_header_extension_encryption() {
use shared::marshal::Unmarshal;
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let (packet, _) = packet_with_extensions(false, 3);
let mut without_extension = packet.clone();
without_extension[0] = 0x80;
let header = rtp::Header::unmarshal(&mut &without_extension[..]).unwrap();
let mut sender = context(profile);
sender.set_encrypted_header_extensions(ids(&[1, 2, 3, 4, 5]));
assert_eq!(
sender.encrypt_rtp_with_header(&packet, &header).unwrap(),
context(profile)
.encrypt_rtp_with_header(&packet, &header)
.unwrap(),
"{profile:?}"
);
}
}