ExSRTP (ExSRTP v0.4.1)

View Source

ExSRTP

Hex.pm Hex Docs

Elixir implementation of Secure Real-time Transport Protocol (SRTP) and Secure Real-time Transport Control Protocol (SRTCP).

It implements the following references:

Supported Crypto Profiles

The library currently supports the following SRTP crypto profiles:

  • AES_CM_128_HMAC_SHA1_80
  • AES_CM_128_HMAC_SHA1_32
  • AES_GCM_128

Backends

The library supports multiple backends for cryptographic operations:

  • elixir - Using Erlang's built-in crypto module (default)
  • rust - A Rust-based backend for improved performance.

Rust Backend

For the rust backend, we offer precompiled NIFs for various platforms, so if your platform is supported, you can use the rust backend without needing to compile anything. However, if your platform is not supported or you want to compile from source, you need to have the rust toolchain installed on your system. You need aslo to add rustler dependency and set force build config:

{:ex_srtp, "~> 0.4.1", system_env: %{"EXSRTP_BUILD" => "1"}}
{:rustler, "~> 0.37.0"}

Rust Backend and AES-GCM

The rust backend is using graviola for aes-gcm which only works on aarch64 and x86_64 architecture with some CPU features. If you are using an older CPU or different architecture, you should use the elixir backend or do not use AES_GCM crypto profile.

Installation

The package can be installed by adding ex_srtp to your list of dependencies in mix.exs:

def deps do
  [
    {:rustler, "~> 0.37", runtime: false} # Optional, if you want to compile the rust backend from source
    {:ex_srtp, "~> 0.4.1"}
  ]
end

Summary

Functions

Creates a new SRTP session.

Creates a new SRTP session from a key and profile.

Same as new/1 but raises an error in case of failure.

Same as new/2 but raises an error in case of failure.

Protects (encrypts and authenticates) an RTP packet.

Same as protect/2 but raises an error in case of failure.

Protects (encrypts and authenticates) RTCP packets.

Same as protect_rtcp/2 but raises an error in case of failure.

Unprotects (decrypts and verifies) an RTP packet.

Same as unprotect/2 but raises an error in case of failure.

Unprotects (decrypts and verifies) RTCP packets.

Same as unprotect_rtcp/2 but raises an error in case of failure

Types

t()

@type t() :: ExSRTP.Backend.state()

Functions

new(policy)

@spec new(ExSRTP.Policy.t()) :: {:ok, t()} | {:error, term()}

Creates a new SRTP session.

new(key, profile)

@spec new(binary(), ExSRTP.Policy.profile()) :: {:ok, t()} | {:error, term()}

Creates a new SRTP session from a key and profile.

iex> {:ok, srtp} = ExSRTP.new(<<1::128>>, :aes_cm_128_hmac_sha1_80)
iex> srtp != nil
true

iex> {:ok, srtp} = ExSRTP.new(<<1::128, 2::112>>, :aes_cm_128_hmac_sha1_80)
iex> srtp != nil
true

iex> ExSRTP.new(<<0::128>>, :invalid_profile)
{:error, :invalid_profile}

iex> ExSRTP.new(<<2::64>>, :aes_cm_128_hmac_sha1_80)
{:error, :invalid_key_size}

new!(policy)

@spec new!(ExSRTP.Policy.t()) :: t()

Same as new/1 but raises an error in case of failure.

new!(key, profile)

@spec new!(binary(), ExSRTP.Policy.profile()) :: t()

Same as new/2 but raises an error in case of failure.

protect(packet, srtp)

Protects (encrypts and authenticates) an RTP packet.

protect!(packet, srtp)

@spec protect!(ExRTP.Packet.t(), t()) :: {binary(), t()}

Same as protect/2 but raises an error in case of failure.

protect_rtcp(compound_packet, srtp)

@spec protect_rtcp([ExRTCP.Packet.packet()], t()) :: ExSRTP.Backend.protect_return()

Protects (encrypts and authenticates) RTCP packets.

protect_rtcp!(compound_packet, srtp)

@spec protect_rtcp!([ExRTCP.Packet.packet()], t()) :: {binary(), t()}

Same as protect_rtcp/2 but raises an error in case of failure.

unprotect(data, srtp)

@spec unprotect(binary(), t()) :: {:ok, ExRTP.Packet.t(), t()} | {:error, term()}

Unprotects (decrypts and verifies) an RTP packet.

unprotect!(data, srtp)

@spec unprotect!(binary(), t()) :: {ExRTP.Packet.t(), t()}

Same as unprotect/2 but raises an error in case of failure.

unprotect_rtcp(data, srtp)

@spec unprotect_rtcp(binary(), t()) ::
  {:ok, [ExRTCP.Packet.packet()], t()} | {:error, term()}

Unprotects (decrypts and verifies) RTCP packets.

unprotect_rtcp!(data, srtp)

@spec unprotect_rtcp!(binary(), t()) :: {[ExRTCP.Packet.packet()], t()}

Same as unprotect_rtcp/2 but raises an error in case of failure