srtp

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SrtcpAuthTagLen = 10
	SrtcpTrailerLen = 4 + SrtcpAuthTagLen
)
View Source
const (
	KDFLabelE2ESframe = "e2e sframe key"
	KDFLabelWarpAuth  = "warp auth key"
)

SFrame KDF labels and lengths.

View Source
const (
	WarpMITagLen             = 4
	WarpPiggybackStartPacket = 2
)
View Source
const WarpExtProfile uint16 = 0xdebe

Variables

View Source
var WarpAudioPiggybackExt = [4]byte{0x30, 0x01, 0x00, 0x00}

WarpAudioPiggybackExt is the audio piggyback extension word (big-endian bytes).

Functions

func AppendWarpMITag

func AppendWarpMITag(authKey, packetWithoutTag []byte, roc uint32, tagLen int, log ...zerolog.Logger) []byte

AppendWarpMITag appends the WARP MI tag to a protected packet.

func AudioPiggybackExtensionFor

func AudioPiggybackExtensionFor(packetIndex int, enabled bool, startPacket int, log ...zerolog.Logger) *uint32

AudioPiggybackExtensionFor returns the audio piggyback extension word for packetIndex, or nil for the first packets / when disabled.

func BuildE2eRtpIV

func BuildE2eRtpIV(salt []byte, ssrc uint32, roc uint32, seq uint16) [16]byte

BuildE2eRtpIV builds the E2E RTP IV: salt right-aligned into 16 bytes, SSRC XORed at bytes 4-7, and the 48-bit packet index (ROC<<16 | seq) XORed at bytes 8-13.

func BuildRtpICMNonce

func BuildRtpICMNonce(ssrc uint32, packetIndex uint64) [16]byte

BuildRtpICMNonce builds the RTP AES-ICM nonce: zero, SSRC at bytes 4-7 (BE), (packetIndex << 16) at bytes 8-15 (BE).

func ComputeWarpMITag

func ComputeWarpMITag(authKey, packetWithoutTag []byte, roc uint32, tagLen int, log ...zerolog.Logger) []byte

ComputeWarpMITag is the WARP MI tag: the first tagLen bytes of HMAC-SHA1(authKey, packetWithoutTag || roc_be32).

func CryptPayload

func CryptPayload(keys *E2eSrtpKeys, ssrc uint32, seq uint16, roc uint32, payload []byte, log ...zerolog.Logger) ([]byte, error)

CryptPayload AES-128-CTR encrypts/decrypts an RTP payload (the cipher is symmetric).

func CryptRtpPayload

func CryptRtpPayload(session *LibsrtpSessionKeys, ssrc uint32, packetIndex uint64, payload []byte, log ...zerolog.Logger) ([]byte, error)

CryptRtpPayload encrypts/decrypts an RTP payload with the expanded session key (symmetric).

func DeriveE2eSframeKeyForParticipant

func DeriveE2eSframeKeyForParticipant(callKey []byte, participantID string, log ...zerolog.Logger) ([]byte, error)

DeriveE2eSframeKeyForParticipant derives the 32-byte per-participant SFrame key from callKey (exactly 32B), salt = callKey[0:16], ikm = callKey[16:32].

func DeriveHbhSrtpKeyDownlink(hbhKey []byte, log ...zerolog.Logger) ([]byte, error)

DeriveHbhSrtpKeyDownlink derives the 30-byte downlink HBH SRTP key from hbhKey (30B).

func DeriveHbhSrtpKeyUplink(hbhKey []byte, log ...zerolog.Logger) ([]byte, error)

DeriveHbhSrtpKeyUplink derives the 30-byte uplink HBH SRTP key from hbhKey (30B).

func DeriveWarpAuthKey

func DeriveWarpAuthKey(callKey []byte, log ...zerolog.Logger) ([]byte, error)

DeriveWarpAuthKey derives the 32-byte WARP auth key from callKey (32B), empty salt, label "warp auth key".

func FormatSframeParticipantID

func FormatSframeParticipantID(jid string) string

FormatSframeParticipantID formats the participant id used as the SFrame HKDF info.

func ProtectSrtcp

func ProtectSrtcp(keys *E2eSrtpKeys, senderSsrc, index uint32, rtcp []byte) ([]byte, error)

ProtectSrtcp encrypts and authenticates one RTCP packet.

func SframeInfoLabel

func SframeInfoLabel(participantID string) string

SframeInfoLabel builds the HKDF info label "e2e sframe key<participantID>".

func UnprotectSrtcp

func UnprotectSrtcp(keys *E2eSrtpKeys, senderSsrc uint32, packet []byte) ([]byte, uint32, bool)

UnprotectSrtcp authenticates and decrypts one SRTCP packet.

func VerifyWarpMITag

func VerifyWarpMITag(authKey, packetWithoutTag []byte, roc uint32, tagLen int, receivedTag []byte, log ...zerolog.Logger) bool

VerifyWarpMITag authenticates a protected WARP packet and its rollover counter.

Types

type E2eSrtpKeys

type E2eSrtpKeys struct {
	CipherKey [16]byte
	Salt      [14]byte
	AuthKey   [20]byte
}

E2eSrtpKeys holds the per-participant session keys for the end-to-end 1:1 SRTP cipher: the AES-128 cipher key, the 14-byte master salt, and the auth key.

func DeriveE2eKeys

func DeriveE2eKeys(callKey []byte, participantLid string, log ...zerolog.Logger) (E2eSrtpKeys, error)

DeriveE2eKeys derives the E2E 1:1 keys from callKey (>=32B) using participantLid as the HKDF info. It errors when callKey is shorter than 32 bytes.

func DeriveE2eKeysFromRaw

func DeriveE2eKeysFromRaw(rawE2e []byte, participantLid string, log ...zerolog.Logger) (E2eSrtpKeys, error)

DeriveE2eKeysFromRaw derives the E2E 1:1 keys from a keygen-v2 <raw_e2e> blob (>=32B) in place of callKey. It errors when rawE2e is shorter than 32 bytes.

func DeriveE2eSRTCPKeysFromRaw

func DeriveE2eSRTCPKeysFromRaw(rawE2E []byte, participantLID string, log ...zerolog.Logger) (E2eSrtpKeys, error)

DeriveE2eSRTCPKeysFromRaw derives per-participant SRTCP keys from a keygen-v2 raw E2E root using the RFC 3711 SRTCP labels.

func DeriveE2eSrtcpKeys

func DeriveE2eSrtcpKeys(callKey []byte, participantLid string, log ...zerolog.Logger) (E2eSrtpKeys, error)

DeriveE2eSrtcpKeys derives the per-participant SRTCP keys using RFC 3711 labels.

type LibsrtpSessionKeys

type LibsrtpSessionKeys struct {
	SessionKey  [16]byte
	SessionSalt [14]byte
	AuthKey     [20]byte
}

LibsrtpSessionKeys is the expanded per-session keying (AES_CM_128_HMAC_SHA1_80).

func ExpandLibsrtpSessionKeys

func ExpandLibsrtpSessionKeys(keying *SrtpKeyingMaterial, log ...zerolog.Logger) (LibsrtpSessionKeys, error)

ExpandLibsrtpSessionKeys runs the libsrtp session-key expansion (labels 0x00 enc, 0x01 auth, 0x02 salt).

type Option

type Option func(*config)

Option configures optional, non-behavioral aspects of the keying/protection types — currently the diagnostic logger. The zero configuration logs nothing.

func WithLogger

func WithLogger(l zerolog.Logger) Option

WithLogger sets the zerolog logger for debug/trace diagnostics. The library never configures logging itself; without this option the types are silent at zero cost. Pass the logger from a context, e.g. WithLogger(*zerolog.Ctx(ctx)).

type RecvRocTracker

type RecvRocTracker struct {
	// contains filtered or unexported fields
}

RecvRocTracker is the recv-side ROC estimator (RFC 3711 guess-index): it tolerates reorder/loss by guessing each packet's ROC from the highest seq seen.

func (*RecvRocTracker) CommitRoc

func (t *RecvRocTracker) CommitRoc(v uint32, seq uint16, log ...zerolog.Logger)

CommitRoc folds an authenticated packet's estimated ROC and sequence into state.

func (*RecvRocTracker) EstimateRoc

func (t *RecvRocTracker) EstimateRoc(seq uint16, log ...zerolog.Logger) uint32

EstimateRoc estimates the ROC for seq without mutating receive state.

func (*RecvRocTracker) GuessRoc

func (t *RecvRocTracker) GuessRoc(seq uint16, log ...zerolog.Logger) uint32

GuessRoc estimates and commits seq in one step.

type RocTracker

type RocTracker struct {
	// contains filtered or unexported fields
}

RocTracker is the send-side ROC tracker for monotonic 16-bit sequence numbers.

func (*RocTracker) Advance

func (t *RocTracker) Advance(seq uint16, log ...zerolog.Logger) uint32

Advance folds seq into the tracker and returns the current ROC, bumping it on the 0xFFFF->0x0000 wrap (a signed 16-bit gap below -32768).

type SframeSession

type SframeSession struct {
	SelfParticipantID string
	PeerParticipantID string
	// contains filtered or unexported fields
}

SframeSession holds the per-direction SFrame keys (encrypt for peer, decrypt for self) and the send-side counter.

func NewSframeSession

func NewSframeSession(callKey []byte, selfJID, peerJID string, opts ...Option) (*SframeSession, error)

NewSframeSession builds a session from callKey and the self/peer JIDs.

func (*SframeSession) Decrypt

func (s *SframeSession) Decrypt(frame []byte) ([]byte, bool)

Decrypt classifies one frame. It returns (plaintext, true) when the trailing SFrame header parses and the GCM tag authenticates; otherwise (nil, false), meaning the frame is plain Opus the caller must use verbatim.

func (*SframeSession) Encrypt

func (s *SframeSession) Encrypt(plaintext []byte) ([]byte, error)

Encrypt seals one frame as [ciphertext || 16-byte tag || varint-header].

type SrtpKeyingMaterial

type SrtpKeyingMaterial struct {
	MasterKey  [16]byte
	MasterSalt [14]byte
}

SrtpKeyingMaterial is the 16-byte master key + 14-byte master salt split.

func KeyingFromHbhKeyDownlink(hbhKey []byte, log ...zerolog.Logger) (SrtpKeyingMaterial, error)

KeyingFromHbhKeyDownlink derives the downlink key and splits it into keying material.

func KeyingFromHbhKeyUplink(hbhKey []byte, log ...zerolog.Logger) (SrtpKeyingMaterial, error)

KeyingFromHbhKeyUplink derives the uplink key and splits it into keying material.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL