Skip to main content

rtc_srtp/
protection_profile.rs

1/// ProtectionProfile specifies Cipher and AuthTag details, similar to TLS cipher suite
2#[derive(Default, Debug, Clone, Copy)]
3#[repr(u8)]
4pub enum ProtectionProfile {
5    #[default]
6    /// `SRTP_AES128_CM_HMAC_SHA1_80`: AES-128 counter mode with an 80-bit HMAC-SHA1 tag.
7    ///
8    /// The profile every WebRTC implementation supports.
9    Aes128CmHmacSha1_80 = 0x0001,
10    /// `SRTP_AES128_CM_HMAC_SHA1_32`: as above with a truncated 32-bit tag, trading
11    /// authentication strength for 6 bytes per packet.
12    Aes128CmHmacSha1_32 = 0x0002,
13    /// `SRTP_AES256_CM_HMAC_SHA1_80`: AES-256 counter mode with an 80-bit HMAC-SHA1 tag.
14    Aes256CmHmacSha1_80 = 0x0003,
15    /// `SRTP_AES256_CM_HMAC_SHA1_32`: AES-256 counter mode with a 32-bit HMAC-SHA1 tag.
16    Aes256CmHmacSha1_32 = 0x0004,
17    /// `SRTP_AEAD_AES_128_GCM`: AES-128 in GCM, which authenticates as part of encryption
18    /// rather than with a separate HMAC.
19    AeadAes128Gcm = 0x0007,
20    /// `SRTP_AEAD_AES_256_GCM`: AES-256 in GCM.
21    AeadAes256Gcm = 0x0008,
22}
23
24impl ProtectionProfile {
25    /// The master key length in bytes for this profile.
26    pub fn key_len(&self) -> usize {
27        match *self {
28            ProtectionProfile::Aes128CmHmacSha1_32
29            | ProtectionProfile::Aes128CmHmacSha1_80
30            | ProtectionProfile::AeadAes128Gcm => 16,
31            ProtectionProfile::Aes256CmHmacSha1_32 | ProtectionProfile::Aes256CmHmacSha1_80 => 32,
32            ProtectionProfile::AeadAes256Gcm => 32,
33        }
34    }
35
36    /// The master salt length in bytes for this profile.
37    pub fn salt_len(&self) -> usize {
38        match *self {
39            ProtectionProfile::Aes128CmHmacSha1_32
40            | ProtectionProfile::Aes128CmHmacSha1_80
41            | ProtectionProfile::Aes256CmHmacSha1_32
42            | ProtectionProfile::Aes256CmHmacSha1_80 => 14,
43            ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 12,
44        }
45    }
46
47    /// The authentication tag length appended to each SRTP packet, in bytes.
48    pub fn rtp_auth_tag_len(&self) -> usize {
49        match *self {
50            ProtectionProfile::Aes128CmHmacSha1_80 | ProtectionProfile::Aes256CmHmacSha1_80 => 10,
51            ProtectionProfile::Aes128CmHmacSha1_32 | ProtectionProfile::Aes256CmHmacSha1_32 => 4,
52            ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 0,
53        }
54    }
55
56    /// The authentication tag length appended to each SRTCP packet, in bytes.
57    pub fn rtcp_auth_tag_len(&self) -> usize {
58        match *self {
59            ProtectionProfile::Aes128CmHmacSha1_80
60            | ProtectionProfile::Aes128CmHmacSha1_32
61            | ProtectionProfile::Aes256CmHmacSha1_80
62            | ProtectionProfile::Aes256CmHmacSha1_32 => 10,
63            ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 0,
64        }
65    }
66
67    /// The AEAD tag length in bytes, for the GCM profiles; `0` for the HMAC-SHA1 ones.
68    pub fn aead_auth_tag_len(&self) -> usize {
69        match *self {
70            ProtectionProfile::Aes128CmHmacSha1_80
71            | ProtectionProfile::Aes128CmHmacSha1_32
72            | ProtectionProfile::Aes256CmHmacSha1_80
73            | ProtectionProfile::Aes256CmHmacSha1_32 => 0,
74            ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 16,
75        }
76    }
77
78    /// The HMAC authentication key length in bytes; `0` for the AEAD profiles, which derive
79    /// authentication from the cipher itself.
80    pub fn auth_key_len(&self) -> usize {
81        match *self {
82            ProtectionProfile::Aes128CmHmacSha1_80
83            | ProtectionProfile::Aes128CmHmacSha1_32
84            | ProtectionProfile::Aes256CmHmacSha1_80
85            | ProtectionProfile::Aes256CmHmacSha1_32 => 20,
86            ProtectionProfile::AeadAes128Gcm | ProtectionProfile::AeadAes256Gcm => 0,
87        }
88    }
89}