use super::*;
fn test_crypto_provider() -> std::sync::Arc<dyn crypto::RTCCryptoProvider> {
crypto::default_provider().expect("a built-in crypto provider must be enabled for tests")
}
type Vector = (&'static str, &'static [u8], &'static [u8]);
const AES_CM_KEY: [u8; 16] = [
0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0, 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
];
const AES_CM_SALT: [u8; 14] = [
0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb, 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6,
];
const GCM_KEY: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
];
const GCM_SALT: [u8; 12] = [
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab,
];
const AES_CM_VECTORS: &[Vector] = &[
(
"A.1.1",
&[
0x90, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xbe, 0xde,
0x00, 0x01, 0x51, 0x00, 0x02, 0x00, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x90, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xc0, 0xde,
0x00, 0x01, 0xeb, 0x92, 0x36, 0x52, 0x51, 0xc3, 0xe0, 0x36, 0xf8, 0xde, 0x27, 0xe9,
0xc2, 0x7e, 0xe3, 0xe0, 0xb4, 0x65, 0x1d, 0x9f, 0xbc, 0x42, 0x18, 0xa7, 0x02, 0x44,
0x52, 0x2f, 0x34, 0xa5,
],
),
(
"A.1.2",
&[
0x90, 0x0f, 0x12, 0x36, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x10, 0x00,
0x00, 0x01, 0x05, 0x02, 0x00, 0x02, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x90, 0x0f, 0x12, 0x36, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xc2, 0xde,
0x00, 0x01, 0x4e, 0xd9, 0xcc, 0x4e, 0x6a, 0x71, 0x2b, 0x30, 0x96, 0xc5, 0xca, 0x77,
0x33, 0x9d, 0x42, 0x04, 0xce, 0x0d, 0x77, 0x39, 0x6c, 0xab, 0x69, 0x58, 0x5f, 0xbc,
0xe3, 0x81, 0x94, 0xa5,
],
),
(
"A.1.3",
&[
0x92, 0x0f, 0x12, 0x38, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0xbe, 0xde, 0x00, 0x01, 0x51, 0x00, 0x02, 0x00,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x38, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x8b, 0xb6,
0xe1, 0x2b, 0x5c, 0xff, 0x16, 0xdd, 0xc0, 0xde, 0x00, 0x01, 0x92, 0x83, 0x8c, 0x8c,
0x09, 0xe5, 0x83, 0x93, 0xe1, 0xde, 0x3a, 0x9a, 0x74, 0x73, 0x4d, 0x67, 0x45, 0x67,
0x13, 0x38, 0xc3, 0xac, 0xf1, 0x1d, 0xa2, 0xdf, 0x84, 0x23, 0xbe, 0xe0,
],
),
(
"A.1.4",
&[
0x92, 0x0f, 0x12, 0x39, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0x10, 0x00, 0x00, 0x01, 0x05, 0x02, 0x00, 0x02,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x39, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xf7, 0x0e,
0x51, 0x3e, 0xb9, 0x0b, 0x9b, 0x25, 0xc2, 0xde, 0x00, 0x01, 0xbb, 0xed, 0x48, 0x48,
0xfa, 0xa6, 0x44, 0x66, 0x5f, 0x3d, 0x7f, 0x34, 0x12, 0x59, 0x14, 0xe9, 0xf4, 0xd0,
0xae, 0x92, 0x3c, 0x6f, 0x47, 0x9b, 0x95, 0xa0, 0xf7, 0xb5, 0x31, 0x33,
],
),
(
"A.1.5",
&[
0x92, 0x0f, 0x12, 0x3a, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0xbe, 0xde, 0x00, 0x00, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x3a, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x71, 0x30,
0xb6, 0xab, 0xfe, 0x2a, 0xb0, 0xe3, 0xc0, 0xde, 0x00, 0x00, 0xe3, 0xd9, 0xf6, 0x4b,
0x25, 0xc9, 0xe7, 0x4c, 0xb4, 0xcf, 0x8e, 0x43, 0xfb, 0x92, 0xe3, 0x78, 0x1c, 0x2c,
0x0c, 0xea, 0xb6, 0xb3, 0xa4, 0x99, 0xa1, 0x4c,
],
),
(
"A.1.6",
&[
0x92, 0x0f, 0x12, 0x3b, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0x10, 0x00, 0x00, 0x00, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x3b, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xcb, 0xf2,
0x4c, 0x12, 0x43, 0x30, 0xe1, 0xc8, 0xc2, 0xde, 0x00, 0x00, 0x59, 0x9d, 0xd4, 0x5b,
0xc9, 0xd6, 0x87, 0xb6, 0x03, 0xe8, 0xb5, 0x9d, 0x77, 0x1f, 0xd3, 0x8e, 0x88, 0xb1,
0x70, 0xe0, 0xcd, 0x31, 0xe1, 0x25, 0xea, 0xbe,
],
),
];
const GCM_VECTORS: &[Vector] = &[
(
"A.2.1",
&[
0x90, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xbe, 0xde,
0x00, 0x01, 0x51, 0x00, 0x02, 0x00, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x90, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xc0, 0xde,
0x00, 0x01, 0x39, 0x97, 0x2d, 0xc9, 0x57, 0x2c, 0x4d, 0x99, 0xe8, 0xfc, 0x35, 0x5d,
0xe7, 0x43, 0xfb, 0x2e, 0x94, 0xf9, 0xd8, 0xff, 0x54, 0xe7, 0x2f, 0x41, 0x93, 0xbb,
0xc5, 0xc7, 0x4f, 0xfa, 0xb0, 0xfa, 0x9f, 0xa0, 0xfb, 0xeb,
],
),
(
"A.2.2",
&[
0x90, 0x0f, 0x12, 0x36, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x10, 0x00,
0x00, 0x01, 0x05, 0x02, 0x00, 0x02, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x90, 0x0f, 0x12, 0x36, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xc2, 0xde,
0x00, 0x01, 0xbb, 0x75, 0xa4, 0xc5, 0x45, 0xcd, 0x1f, 0x41, 0x3b, 0xdb, 0x7d, 0xaa,
0x2b, 0x1e, 0x32, 0x63, 0xde, 0x31, 0x36, 0x67, 0xc9, 0x63, 0x24, 0x90, 0x81, 0xb3,
0x5a, 0x65, 0xf5, 0xcb, 0x6c, 0x88, 0xb3, 0x94, 0x23, 0x5f,
],
),
(
"A.2.3",
&[
0x92, 0x0f, 0x12, 0x38, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0xbe, 0xde, 0x00, 0x01, 0x51, 0x00, 0x02, 0x00,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x38, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x63, 0xbb,
0xcc, 0xc4, 0xa7, 0xf6, 0x95, 0xc4, 0xc0, 0xde, 0x00, 0x01, 0x8a, 0xd7, 0xc7, 0x1f,
0xac, 0x70, 0xa8, 0x0c, 0x92, 0x86, 0x6b, 0x4c, 0x6b, 0xa9, 0x85, 0x46, 0xef, 0x91,
0x35, 0x86, 0xe9, 0x5f, 0xfa, 0xaf, 0xfe, 0x95, 0x68, 0x85, 0xbb, 0x06, 0x47, 0xa8,
0xbc, 0x09, 0x4a, 0xc8,
],
),
(
"A.2.4",
&[
0x92, 0x0f, 0x12, 0x39, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0x10, 0x00, 0x00, 0x01, 0x05, 0x02, 0x00, 0x02,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x39, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x36, 0x80,
0x52, 0x4f, 0x8d, 0x31, 0x2b, 0x00, 0xc2, 0xde, 0x00, 0x01, 0xc7, 0x8d, 0x12, 0x00,
0x38, 0x42, 0x2b, 0xc1, 0x11, 0xa7, 0x18, 0x7a, 0x18, 0x24, 0x6f, 0x98, 0x0c, 0x05,
0x9c, 0xc6, 0xbc, 0x9d, 0xf8, 0xb6, 0x26, 0x39, 0x4e, 0xca, 0x34, 0x4e, 0x4b, 0x05,
0xd8, 0x0f, 0xea, 0x83,
],
),
(
"A.2.5",
&[
0x92, 0x0f, 0x12, 0x3a, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0xbe, 0xde, 0x00, 0x00, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x3a, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x15, 0xb6,
0xbb, 0x43, 0x37, 0x90, 0x6f, 0xff, 0xc0, 0xde, 0x00, 0x00, 0xb7, 0xb9, 0x64, 0x53,
0x7a, 0x2b, 0x03, 0xab, 0x7b, 0xa5, 0x38, 0x9c, 0xe9, 0x33, 0x17, 0x12, 0x6b, 0x5d,
0x97, 0x4d, 0xf3, 0x0c, 0x68, 0x84, 0xdc, 0xb6, 0x51, 0xc5, 0xe1, 0x20, 0xc1, 0xda,
],
),
(
"A.2.6",
&[
0x92, 0x0f, 0x12, 0x3b, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01,
0xe2, 0x40, 0x00, 0x00, 0xb2, 0x6e, 0x10, 0x00, 0x00, 0x00, 0xab, 0xab, 0xab, 0xab,
0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
],
&[
0x92, 0x0f, 0x12, 0x3b, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xdc, 0xb3,
0x8c, 0x9e, 0x48, 0xbf, 0x95, 0xf4, 0xc2, 0xde, 0x00, 0x00, 0x61, 0xee, 0x43, 0x2c,
0xf9, 0x20, 0x31, 0x70, 0x76, 0x61, 0x32, 0x58, 0xd3, 0xce, 0x42, 0x36, 0xc0, 0x6a,
0xc4, 0x29, 0x68, 0x1a, 0xd0, 0x84, 0x13, 0x51, 0x2d, 0xc9, 0x8b, 0x52, 0x07, 0xd8,
],
),
];
fn context(profile: ProtectionProfile, cryptex: Cryptex) -> Context {
let (key, salt): (&[u8], &[u8]) = match profile {
ProtectionProfile::AeadAes128Gcm => (&GCM_KEY, &GCM_SALT),
_ => (&AES_CM_KEY, &AES_CM_SALT),
};
let mut context = Context::new(
key,
salt,
profile,
None,
None,
test_crypto_provider().crypto(),
)
.expect("context");
context.set_cryptex(cryptex);
context
}
const SEND: Cryptex = Cryptex {
send: true,
require: false,
};
fn check(profile: ProtectionProfile, vectors: &[Vector]) {
for (name, plain, encrypted) in vectors {
let out = context(profile, SEND)
.encrypt_rtp(plain)
.unwrap_or_else(|e| panic!("{name}: encrypt: {e}"));
assert_eq!(&out[..], *encrypted, "{name}: encrypt");
let out = context(profile, Cryptex::default())
.decrypt_rtp(encrypted)
.unwrap_or_else(|e| panic!("{name}: decrypt: {e}"));
assert_eq!(&out[..], *plain, "{name}: decrypt");
}
}
#[test]
fn cryptex_aes_cm_test_vectors() {
assert_eq!(AES_CM_VECTORS.len(), 6);
check(ProtectionProfile::Aes128CmHmacSha1_80, AES_CM_VECTORS);
}
#[test]
fn cryptex_aead_aes_gcm_test_vectors() {
assert_eq!(GCM_VECTORS.len(), 6);
check(ProtectionProfile::AeadAes128Gcm, GCM_VECTORS);
}
#[test]
fn cryptex_leaves_a_bare_packet_alone() {
let plain: &[u8] = &[
0x80, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab,
0xab,
];
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let with = context(profile, SEND).encrypt_rtp(plain).unwrap();
let without = context(profile, Cryptex::default())
.encrypt_rtp(plain)
.unwrap();
assert_eq!(with, without);
}
}
#[test]
fn cryptex_required_refuses_clear_header_extensions() {
let require = Cryptex {
send: false,
require: true,
};
for (profile, vectors) in [
(ProtectionProfile::Aes128CmHmacSha1_80, AES_CM_VECTORS),
(ProtectionProfile::AeadAes128Gcm, GCM_VECTORS),
] {
let (_, plain, encrypted) = vectors[0];
let clear = context(profile, Cryptex::default())
.encrypt_rtp(plain)
.unwrap();
assert!(context(profile, require).decrypt_rtp(&clear).is_err());
assert_eq!(
&context(profile, require).decrypt_rtp(encrypted).unwrap()[..],
plain
);
}
}
#[test]
fn cryptex_adds_an_empty_extension_for_csrcs() {
let plain: &[u8] = &[
0x81, 0x0f, 0x12, 0x35, 0xde, 0xca, 0xfb, 0xad, 0xca, 0xfe, 0xba, 0xbe, 0x00, 0x01, 0xe2,
0x40, 0xab, 0xab, 0xab, 0xab,
];
let encrypted = context(ProtectionProfile::Aes128CmHmacSha1_80, SEND)
.encrypt_rtp(plain)
.unwrap();
assert_eq!(encrypted[0], 0x91);
assert_eq!(&encrypted[16..20], &[0xc0, 0xde, 0x00, 0x00]);
let decrypted = context(ProtectionProfile::Aes128CmHmacSha1_80, Cryptex::default())
.decrypt_rtp(&encrypted)
.unwrap();
let mut header: Vec<u8> = plain[..16].to_vec();
header[0] = 0x91;
assert_eq!(&decrypted[..16], &header[..]);
assert_eq!(&decrypted[16..20], &[0xbe, 0xde, 0x00, 0x00]);
assert_eq!(&decrypted[20..], &plain[16..]);
}
fn shaped(csrcs: u8, profile: Option<u16>, seq: u16) -> Vec<u8> {
let mut packet = vec![0x80 | csrcs | if profile.is_some() { 0x10 } else { 0 }, 96];
packet.extend_from_slice(&seq.to_be_bytes());
packet.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 7]);
for csrc in 0..csrcs {
packet.extend_from_slice(&[0xC0, 0, 0, csrc]);
}
if let Some(profile) = profile {
packet.extend_from_slice(&profile.to_be_bytes());
packet.extend_from_slice(&[0, 1]);
packet.extend_from_slice(if profile & 0xFFF0 == 0x1000 {
&[0x01, 0x01, 0xAA, 0]
} else {
&[0x10, 0xAA, 0, 0]
});
}
packet.extend_from_slice(b"payload");
packet
}
#[test]
fn cryptex_extensions_are_encrypted_and_authenticated_like_the_payload() {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let plain = shaped(1, Some(0xBEDE), 1);
let protected = context(profile, SEND).encrypt_rtp(&plain).unwrap();
assert_eq!(
&protected[16..18],
&[0xc0, 0xde],
"{profile:?}: marked Cryptex"
);
assert_ne!(
&protected[12..16],
&plain[12..16],
"{profile:?}: CSRC in clear"
);
assert_ne!(
&protected[20..24],
&plain[20..24],
"{profile:?}: element in clear"
);
for at in [13, 21] {
let mut forged = protected.to_vec();
forged[at] ^= 0x01;
assert!(
context(profile, Cryptex::default())
.decrypt_rtp(&forged)
.is_err(),
"{profile:?}: octet {at} changed and still read"
);
}
assert_eq!(
&context(profile, Cryptex::default())
.decrypt_rtp(&protected)
.unwrap()[..20],
&plain[..20]
);
}
}
#[test]
fn cryptex_required_never_sends_csrcs_or_extensions_in_the_clear() {
let required = Cryptex {
send: true,
require: true,
};
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
for (name, packet) in [
("two-byte with appbits", shaped(0, Some(0x1001), 1)),
("profile-specific extension", shaped(0, Some(0xABCD), 2)),
("the same with CSRCs", shaped(2, Some(0xABCD), 3)),
("already marked 0xC0DE", shaped(0, Some(0xC0DE), 4)),
(
"extension cut short",
shaped(1, Some(0xBEDE), 5)[..17].to_vec(),
),
] {
let mut sender = context(profile, required);
assert!(sender.encrypt_rtp(&packet).is_err(), "{profile:?}: {name}");
assert!(sender.get_roc(7).is_none_or(|roc| roc == 0));
}
let mut sender = context(profile, required);
for (packet, marker) in [
(shaped(0, Some(0xBEDE), 10), Some([0xC0, 0xDE])),
(shaped(0, Some(0x1000), 11), Some([0xC2, 0xDE])),
(shaped(3, None, 12), Some([0xC0, 0xDE])),
(shaped(0, None, 13), None),
] {
let protected = sender.encrypt_rtp(&packet).unwrap();
let csrc_end = 12 + 4 * (packet[0] & 0x0F) as usize;
if let Some(marker) = marker {
assert_eq!(&protected[csrc_end..csrc_end + 2], &marker);
}
let mut receiver = context(profile, required);
let received = receiver.decrypt_rtp(&protected).unwrap();
assert_eq!(&received[received.len() - 7..], b"payload");
}
let mut sender = context(
profile,
Cryptex {
send: false,
require: true,
},
);
assert!(sender.encrypt_rtp(&shaped(0, Some(0xBEDE), 20)).is_err());
assert!(sender.encrypt_rtp(&shaped(1, None, 21)).is_err());
sender.encrypt_rtp(&shaped(0, None, 22)).unwrap();
let mut sender = context(profile, SEND);
let packet = shaped(0, Some(0xABCD), 30);
let protected = sender.encrypt_rtp(&packet).unwrap();
assert_eq!(&protected[..20], &packet[..20]);
assert_eq!(
&context(profile, Cryptex::default())
.decrypt_rtp(&protected)
.unwrap()[..],
&packet[..]
);
}
}
#[test]
fn cryptex_required_reports_an_authentic_violation_once() {
let required = Cryptex {
send: true,
require: true,
};
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let mut sender = context(profile, Cryptex::default());
let mut receiver = context(profile, required);
let mut forged = sender
.encrypt_rtp(&shaped(1, Some(0xBEDE), 1))
.unwrap()
.to_vec();
let last = forged.len() - 1;
forged[last] ^= 1;
assert!(receiver.decrypt_rtp(&forged).is_err());
assert!(matches!(
receiver.poll_event(),
Some(ContextEvent::PacketDiscarded {
reason: DiscardReason::AuthenticationFailed,
..
})
));
assert_eq!(receiver.poll_event(), None);
for seq in 2..5 {
let clear = sender.encrypt_rtp(&shaped(0, Some(0xBEDE), seq)).unwrap();
assert!(receiver.decrypt_rtp(&clear).is_err());
}
assert_eq!(
receiver.poll_event(),
Some(ContextEvent::CryptexRequiredViolation { ssrc: 7 })
);
assert_eq!(receiver.poll_event(), None);
assert_eq!(receiver.get_roc(7), None);
assert_eq!(receiver.key_usage().srtp_packets, 0);
receiver.set_cryptex(required);
let clear = sender.encrypt_rtp(&shaped(0, Some(0xBEDE), 9)).unwrap();
assert!(receiver.decrypt_rtp(&clear).is_err());
assert!(
receiver.poll_event().is_some(),
"reported again after a change"
);
}
}
#[test]
fn cryptex_round_trips_a_padded_packet_with_csrcs() {
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let mut packet = shaped(2, Some(0xBEDE), 40);
packet[0] |= 0x20;
packet.extend_from_slice(&[0, 0, 0, 0, 5]);
let protected = context(profile, SEND).encrypt_rtp(&packet).unwrap();
assert_ne!(&protected[12..20], &packet[12..20], "CSRCs");
assert_eq!(&protected[20..24], &[0xC0, 0xDE, 0, 1]);
assert_ne!(
&protected[24..packet.len()],
&packet[24..],
"extension data to padding"
);
assert_eq!(
&context(profile, Cryptex::default())
.decrypt_rtp(&protected)
.unwrap()[..],
&packet[..]
);
}
}
#[test]
fn a_changed_cryptex_extension_block_fails_authentication() {
let plain = AES_CM_VECTORS[0].1;
for profile in [
ProtectionProfile::Aes128CmHmacSha1_80,
ProtectionProfile::AeadAes128Gcm,
] {
let protected = context(profile, SEND).encrypt_rtp(plain).unwrap();
assert_eq!(
&protected[12..14],
&[0xC0, 0xDE],
"{profile:?}: Cryptex profile"
);
assert_ne!(
&protected[16..20],
&plain[16..20],
"{profile:?}: the extension element is not in the clear"
);
assert_eq!(
&context(profile, Cryptex::default())
.decrypt_rtp(&protected)
.unwrap()[..],
plain
);
for offset in 16..20 {
let mut changed = protected.to_vec();
changed[offset] ^= 0x01;
assert!(
context(profile, Cryptex::default())
.decrypt_rtp(&changed)
.is_err(),
"{profile:?}: a change at octet {offset} of the extension block is detected"
);
}
}
}