Secure RTP (SRTP) and Secure RTCP (SRTCP) encryption and authentication.
What problem this solves
WebRTC and many VoIP systems encrypt RTP payloads with SRTP after DTLS or ZRTP key agreement. Senders must AES-CM encrypt and HMAC-authenticate each packet; receivers must verify tags and maintain rollover counters per SSRC. This module wraps those protect/unprotect steps and SRTCP index handling for media nodes built on XMediaLib.
RTP alone does not encrypt media. SRTP adds confidentiality and integrity on top of RTP using session keys derived from a shared master key and salt (often exchanged via DTLS-SRTP in WebRTC). Only the RTP payload is encrypted; the fixed header, CSRC list, and header extension remain readable so receivers can route and decrypt packets. SRTCP protects control traffic with separate keys and an explicit SRTCP index in each compound packet trailer.
Typical usage:
- Build
%Srtp.Srtp_Crypto_Ctx{}withnew_ctx/6for each SSRC. - Call
encrypt/2before sending anddecrypt/2after receiving. - Thread the updated context returned from each call (ROC and RTCP index advance with traffic).
Pass :passthru as the context to skip crypto (plaintext debugging).
Algorithms: encryption :aes_cm (AES counter mode, RFC 3711) or :null;
authentication :hmac_sha1, :skein_hmac, or :null. WebRTC defaults to
AES-CM + HMAC-SHA1 with a 10-byte tag (HMAC_SHA1_80). Authentication
failure returns {:error, :auth}.
RFCs
Summary
Functions
Appends an SRTP authentication tag to data using aalg and session key.
Verifies the authentication tag on an SRTP packet (RFC 3711).
RFC 3711 AES-CM IV for key derivation or packet encryption.
Decrypts an SRTP/SRTCP binary or struct, or passes through with :passthru.
Decrypts a payload encrypted by encrypt_payload/8.
Derives a session key from the master key and salt (RFC 3711 section 4.3.1).
Encrypts %Rtp{} or %Rtcp{} with ctx, or passes through when ctx is :passthru.
Encrypts an RTP or SRTCP payload with the session encryption algorithm.
Returns one AES-CTR keystream block for debugging or custom crypto paths.
Estimates the 48-bit SRTP packet index from sequence number and ROC (RFC 3711).
Builds an SRTP crypto context for one SSRC.
Same as new_ctx/6 with explicit key_derivation_rate (RFC 3711 master key update).
RFC 3711 SRTCP authentication KDF label (0x04).
RFC 3711 key derivation label for SRTCP encryption (0x03).
RFC 3711 SRTCP salt KDF label (0x05).
RFC 3711 key derivation label for RTP authentication (0x01).
RFC 3711 key derivation label for RTP encryption (0x00).
RFC 3711 key derivation label for RTP salt (0x02).
Updates ROC and highest sequence number in ctx after a successful RTP op.
Functions
Appends an SRTP authentication tag to data using aalg and session key.
Verifies the authentication tag on an SRTP packet (RFC 3711).
roc is appended to the authenticated portion for RTP. Returns
{:ok, data_without_tag} or {:error, :auth}.
RFC 3711 AES-CM IV for key derivation or packet encryption.
Decrypts an SRTP/SRTCP binary or struct, or passes through with :passthru.
Returns {:ok, rtp_or_rtcp, updated_ctx} or {:error, :auth}.
Decrypts a payload encrypted by encrypt_payload/8.
Derives a session key from the master key and salt (RFC 3711 section 4.3.1).
label is one of the srtp_label_* constants. Default length is 16 octets.
Encrypts %Rtp{} or %Rtcp{} with ctx, or passes through when ctx is :passthru.
Returns {:ok, binary, updated_ctx} on success.
Encrypts an RTP or SRTCP payload with the session encryption algorithm.
For AES-CM, index is the packet index (RTP) or SRTCP index. Unsupported
algorithms raise via throw/1.
Returns one AES-CTR keystream block for debugging or custom crypto paths.
Estimates the 48-bit SRTP packet index from sequence number and ROC (RFC 3711).
Used when decrypting packets that did not carry the full index on the wire.
@spec new_ctx( non_neg_integer(), atom(), atom(), binary(), binary(), non_neg_integer() ) :: XMediaLib.Srtp.Srtp_Crypto_Ctx.t()
Builds an SRTP crypto context for one SSRC.
ealg is :aes_cm or :null. aalg is :hmac_sha1, :skein_hmac, or :null.
tag_length is 10 for HMAC_SHA1_80 (WebRTC default). The 6-argument form sets
key_derivation_rate to 0; pass a seventh argument to override.
@spec new_ctx( non_neg_integer(), atom(), atom(), binary(), binary(), non_neg_integer(), non_neg_integer() ) :: XMediaLib.Srtp.Srtp_Crypto_Ctx.t()
Same as new_ctx/6 with explicit key_derivation_rate (RFC 3711 master key update).
RFC 3711 SRTCP authentication KDF label (0x04).
Examples
iex> XMediaLib.Srtp.srtp_label_rtcp_auth()
4
RFC 3711 key derivation label for SRTCP encryption (0x03).
RFC 3711 SRTCP salt KDF label (0x05).
Examples
iex> XMediaLib.Srtp.srtp_label_rtcp_salt()
5
RFC 3711 key derivation label for RTP authentication (0x01).
RFC 3711 key derivation label for RTP encryption (0x00).
Examples
iex> XMediaLib.Srtp.srtp_label_rtp_encr()
0
RFC 3711 key derivation label for RTP salt (0x02).
Updates ROC and highest sequence number in ctx after a successful RTP op.