use std::hint::black_box;
use bytes::BytesMut;
use rtc_srtp::option::srtp_replay_protection;
use rtc_srtp::{context::Context, protection_profile::ProtectionProfile};
use shared::marshal::Marshal;
const MASTER_KEY: &[u8] = &[
96, 180, 31, 4, 119, 137, 128, 252, 75, 194, 252, 44, 63, 56, 61, 55,
];
const MASTER_SALT: &[u8] = &[247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216, 62, 195];
fn new_ctx() -> Context {
Context::new(
MASTER_KEY,
MASTER_SALT,
ProtectionProfile::Aes128CmHmacSha1_80,
None,
None,
)
.unwrap()
}
const GCM_MASTER_SALT: &[u8] = &[247, 26, 49, 94, 99, 29, 79, 94, 5, 111, 252, 216];
fn new_gcm_ctx() -> Context {
Context::new(
MASTER_KEY,
GCM_MASTER_SALT,
ProtectionProfile::AeadAes128Gcm,
None,
None,
)
.unwrap()
}
fn new_ctx_replay() -> Context {
Context::new(
MASTER_KEY,
MASTER_SALT,
ProtectionProfile::Aes128CmHmacSha1_80,
Some(srtp_replay_protection(128)),
None,
)
.unwrap()
}
fn sample_packet() -> BytesMut {
let mut pld = BytesMut::new();
for i in 0..1200 {
pld.extend_from_slice(&[i as u8]);
}
let pkt = rtp::packet::Packet {
header: rtp::header::Header {
sequence_number: 1,
timestamp: 1,
extension_profile: 48862,
marker: true,
padding: false,
extension: true,
payload_type: 96,
..Default::default()
},
payload: pld.freeze(),
};
pkt.marshal().unwrap()
}
fn main() {
let mut args = std::env::args().skip(1);
let mode = args.next().unwrap_or_else(|| "encrypt".to_string());
let iters: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(50_000);
let plaintext = sample_packet();
match mode.as_str() {
"encrypt" => {
let mut ctx = new_ctx();
for _ in 0..iters {
let out = ctx.encrypt_rtp(black_box(&plaintext)).unwrap();
black_box(&out);
}
}
"encrypt-replay" => {
let mut ctx = new_ctx_replay();
for _ in 0..iters {
let out = ctx.encrypt_rtp(black_box(&plaintext)).unwrap();
black_box(&out);
}
}
"decrypt" => {
let encrypted = new_ctx().encrypt_rtp(&plaintext).unwrap();
let mut ctx = new_ctx();
for _ in 0..iters {
let out = ctx.decrypt_rtp(black_box(&encrypted)).unwrap();
black_box(&out);
}
}
"gcm-encrypt" => {
let mut ctx = new_gcm_ctx();
for _ in 0..iters {
let out = ctx.encrypt_rtp(black_box(&plaintext)).unwrap();
black_box(&out);
}
}
"gcm-decrypt" => {
let encrypted = new_gcm_ctx().encrypt_rtp(&plaintext).unwrap();
let mut ctx = new_gcm_ctx();
for _ in 0..iters {
let out = ctx.decrypt_rtp(black_box(&encrypted)).unwrap();
black_box(&out);
}
}
other => {
eprintln!(
"unknown mode: {other} (expected encrypt|encrypt-replay|decrypt|gcm-encrypt|gcm-decrypt)"
);
std::process::exit(2);
}
}
}