VLC  4.0.0-dev
srtp.h
Go to the documentation of this file.
1 /*
2  * Secure RTP with libgcrypt
3  * Copyright (C) 2007 RĂ©mi Denis-Courmont
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  ****************************************************************************/
19 
20 #ifndef LIBVLC_SRTP_H
21 # define LIBVLC_SRTP_H 1
22 
24 
25 enum
26 {
27  SRTP_UNENCRYPTED=0x1, //< do not encrypt SRTP packets
28  SRTCP_UNENCRYPTED=0x2, //< do not encrypt SRTCP packets
29  SRTP_UNAUTHENTICATED=0x4, //< authenticate only SRTCP packets
30 
31  SRTP_RCC_MODE1=0x10, //< use Roll-over-Counter Carry mode 1
32  SRTP_RCC_MODE2=0x20, //< use Roll-over-Counter Carry mode 2
33  SRTP_RCC_MODE3=0x30, //< use Roll-over-Counter Carry mode 3 (insecure)
34 
35  SRTP_FLAGS_MASK=0x37 //< mask for valid flags
36 };
37 
38 /** SRTP encryption algorithms (ciphers); same values as MIKEY */
39 enum
40 {
41  SRTP_ENCR_NULL=0, //< no encryption
42  SRTP_ENCR_AES_CM=1, //< AES counter mode
43  SRTP_ENCR_AES_F8=2, //< AES F8 mode (not implemented)
44 };
45 
46 /** SRTP authenticaton algorithms; same values as MIKEY */
47 enum
48 {
49  SRTP_AUTH_NULL=0, //< no authentication code
50  SRTP_AUTH_HMAC_SHA1=1, //< HMAC-SHA1
51 };
52 
53 /** SRTP pseudo random function; same values as MIKEY */
54 enum
55 {
56  SRTP_PRF_AES_CM=0, //< AES counter mode
57 };
58 
59 # ifdef __cplusplus
60 extern "C" {
61 # endif
62 
63 srtp_session_t *srtp_create (int encr, int auth, unsigned tag_len, int prf,
64  unsigned flags);
66 
67 int srtp_setkey (srtp_session_t *s, const void *key, size_t keylen,
68  const void *salt, size_t saltlen);
69 int srtp_setkeystring (srtp_session_t *s, const char *key, const char *salt);
70 
71 void srtp_setrcc_rate (srtp_session_t *s, uint16_t rate);
72 
73 int srtp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t maxsize);
74 int srtp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp);
75 int srtcp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t maxsiz);
76 int srtcp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp);
77 
78 # ifdef __cplusplus
79 }
80 # endif
81 #endif
82 
rtp_pt_t::header
void(* header)(demux_t *, void *, block_t *)
Definition: rtp.h:33
srtp_setrcc_rate
void srtp_setrcc_rate(srtp_session_t *s, uint16_t rate)
Sets Roll-over-Counter Carry (RCC) rate for the SRTP session.
Definition: srtp.c:384
rtp_pt_t
Definition: rtp.h:29
GetDWBE
#define GetDWBE(p)
Definition: vlc_common.h:1014
rtp_source_t::last_rx
vlc_tick_t last_rx
Definition: session.c:142
srtp_setkeystring
int srtp_setkeystring(srtp_session_t *s, const char *key, const char *salt)
Sets (or resets) the master key and master salt for a SRTP session from hexadecimal strings.
Definition: srtp.c:352
rtp_pt_t::frequency
uint32_t frequency
Definition: rtp.h:35
vlc_demux.h
block_ChainRelease
static void block_ChainRelease(block_t *p_block)
Definition: vlc_block.h:358
rtp_session_t::srcc
unsigned srcc
Definition: session.c:42
SRTCP_UNENCRYPTED
@ SRTCP_UNENCRYPTED
Definition: srtp.h:28
proto_destroy
static void proto_destroy(srtp_proto_t *p)
Definition: srtp.c:88
srtp_proto_t::cipher
gcry_cipher_hd_t cipher
Definition: srtp.c:52
srtcp_recv
int srtcp_recv(srtp_session_t *s, uint8_t *buf, size_t *lenp)
Turns a SRTCP packet into a RTCP packet: authenticates the packet, then decrypts it.
Definition: srtp.c:821
rtp_session_t::srcv
rtp_source_t ** srcv
Definition: session.c:41
rtp_timestamp
static uint32_t rtp_timestamp(const block_t *block)
Definition: session.c:207
vlc_common.h
srtp_session_t
Definition: srtp.c:58
no_destroy
static void no_destroy(demux_t *demux, void *opaque)
Definition: session.c:94
srtp_destroy
void srtp_destroy(srtp_session_t *s)
Releases all resources associated with a Secure RTP session.
Definition: srtp.c:98
srtp.h
srtp_create
srtp_session_t * srtp_create(int encr, int auth, unsigned tag_len, int prf, unsigned flags)
Allocates a Secure RTP one-way session.
Definition: srtp.c:134
srtp_session_t::rtcp
srtp_proto_t rtcp
Definition: srtp.c:61
block_t::i_buffer
size_t i_buffer
Payload length.
Definition: vlc_block.h:122
SRTP_ENCR_AES_F8
@ SRTP_ENCR_AES_F8
Definition: srtp.h:43
hexstring
static ssize_t hexstring(const char *in, uint8_t *out, size_t outlen)
Definition: srtp.c:327
rtp_session_destroy
void rtp_session_destroy(demux_t *demux, rtp_session_t *session)
Destroys an RTP session.
Definition: session.c:77
rtp_seq
static uint16_t rtp_seq(const uint8_t *buf)
Returns RTP sequence (in host-byte order)
Definition: srtp.c:431
rtp_source_t::max_seq
uint16_t max_seq
Definition: session.c:149
vlc_tick_from_samples
static vlc_tick_t vlc_tick_from_samples(int64_t samples, int samp_rate)
Definition: vlc_tick.h:101
rtp_source_t::ref_rtp
uint32_t ref_rtp
Definition: session.c:145
rtcp_digest
static const uint8_t * rtcp_digest(gcry_md_hd_t md, const void *data, size_t len)
Message Authentication and Integrity for RTCP.
Definition: srtp.c:700
srtp_proto_t::mac
gcry_md_hd_t mac
Definition: srtp.c:53
SRTP_ENCR_AES_CM
@ SRTP_ENCR_AES_CM
Definition: srtp.h:42
SRTP_UNAUTHENTICATED
@ SRTP_UNAUTHENTICATED
Definition: srtp.h:29
proto_create
static int proto_create(srtp_proto_t *p, int gcipher, int gmd)
Definition: srtp.c:108
srtp.c
do_derive
static int do_derive(gcry_cipher_hd_t prf, const void *salt, const uint8_t *r, size_t rlen, uint8_t label, void *out, size_t outlen)
AES-CM key derivation (saltlen = 14 bytes)
Definition: srtp.c:236
SRTP_ENCR_NULL
@ SRTP_ENCR_NULL
Definition: srtp.h:41
srtp_proto_t
struct srtp_proto_t srtp_proto_t
freq
LARGE_INTEGER freq
Definition: thread.c:556
rtp_source_t::ref_ntp
vlc_tick_t ref_ntp
Definition: session.c:146
msg_Dbg
#define msg_Dbg(p_this,...)
Definition: vlc_messages.h:106
srtp_destroy
void srtp_destroy(srtp_session_t *s)
Releases all resources associated with a Secure RTP session.
Definition: srtp.c:98
srtp_setkey
int srtp_setkey(srtp_session_t *s, const void *key, size_t keylen, const void *salt, size_t saltlen)
Sets (or resets) the master key and master salt for a SRTP session.
Definition: srtp.c:266
srtp_proto_t
Definition: srtp.c:50
rtcp_crypt
static int rtcp_crypt(gcry_cipher_hd_t hd, uint32_t ssrc, uint32_t index, const uint32_t *salt, uint8_t *data, size_t len)
AES-CM for RTCP (salt = 14 bytes + 2 nul bytes)
Definition: srtp.c:691
srtp_recv
int srtp_recv(srtp_session_t *s, uint8_t *buf, size_t *lenp)
Turns a SRTP packet into a RTP packet: authenticates the packet, then decrypts it.
Definition: srtp.c:623
srtp_test
static void srtp_test(void)
Definition: srtp-test-aes.c:144
SRTP_RCC_MODE3
@ SRTP_RCC_MODE3
Definition: srtp.h:33
SRTP_AUTH
@ SRTP_AUTH
Definition: srtp.c:74
srtcp_send
int srtcp_send(srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
Turns a RTCP packet into a SRTCP packet: encrypt it, then computes the authentication tag and appends...
Definition: srtp.c:781
demux_sys_t::max_misorder
uint16_t max_misorder
Max packet backward misordering.
Definition: rtp.h:76
rtp_source_create
static rtp_source_t * rtp_source_create(demux_t *, const rtp_session_t *, uint32_t, uint16_t)
Initializes a new RTP source within an RTP session.
Definition: session.c:160
srtp_session_t::rtp_seq
uint16_t rtp_seq
Definition: srtp.c:66
srtp_session_t::rtp
srtp_proto_t rtp
Definition: srtp.c:60
msg_Warn
#define msg_Warn(p_this,...)
Definition: vlc_messages.h:104
no_init
static void * no_init(demux_t *demux)
Definition: session.c:88
srtp_setkey
int srtp_setkey(srtp_session_t *s, const void *key, size_t keylen, const void *salt, size_t saltlen)
Sets (or resets) the master key and master salt for a SRTP session.
Definition: srtp.c:266
do_ctr_crypt
static int do_ctr_crypt(gcry_cipher_hd_t hd, const void *ctr, uint8_t *data, size_t len)
Counter Mode encryption/decryption (ctr length = 16 bytes) with non-padded (truncated) text.
Definition: srtp.c:206
printhex
static void printhex(const void *buf, size_t len)
Definition: srtp-test-aes.c:23
rcc_mode
static unsigned rcc_mode(const srtp_session_t *s)
Definition: srtp.c:82
block_t::p_next
block_t * p_next
Definition: vlc_block.h:119
rtp_source_t::ssrc
uint32_t ssrc
Definition: session.c:140
test_derivation
static void test_derivation(void)
AES-CM key derivation test vectors.
Definition: srtp-test-aes.c:37
rtp_source_t::blocks
block_t * blocks
Definition: session.c:152
vlc_tick_now
vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition: thread.c:261
demux_sys_t::timeout
vlc_tick_t timeout
Definition: rtp.h:74
rtp_seq
static uint16_t rtp_seq(const block_t *block)
Definition: session.c:201
vlc_tick_t
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
fatal
static void fatal(const char *msg)
Definition: srtp-test-aes.c:30
block_t::i_flags
uint32_t i_flags
Definition: vlc_block.h:126
srtcp_send
int srtcp_send(srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t maxsiz)
Turns a RTCP packet into a SRTCP packet: encrypt it, then computes the authentication tag and appends...
Definition: srtp.c:781
hexdigit
static int hexdigit(char c)
Definition: srtp.c:316
rtp_session_t::ptc
uint8_t ptc
Definition: session.c:43
rtp_source_destroy
static void rtp_source_destroy(demux_t *, const rtp_session_t *, rtp_source_t *)
Destroys an RTP source and its associated streams.
Definition: session.c:190
rtp_digest
static const uint8_t * rtp_digest(gcry_md_hd_t md, const uint8_t *data, size_t len, uint32_t roc)
Message Authentication and Integrity for RTP.
Definition: srtp.c:439
srtcp_crypt
static int srtcp_crypt(srtp_session_t *s, uint8_t *buf, size_t len)
Encrypts/decrypts a RTCP packet and updates SRTCP context (CTR block cypher mode of operation has ide...
Definition: srtp.c:719
srtp_proto_t::salt
uint32_t salt[4]
Definition: srtp.c:55
SRTCP_AUTH
@ SRTCP_AUTH
Definition: srtp.c:77
rtp_session_t
State for a RTP session:
Definition: session.c:39
rtp_decode
static void rtp_decode(demux_t *, const rtp_session_t *, rtp_source_t *)
Decodes one RTP packet.
Definition: session.c:468
rtp_source_t::opaque
void * opaque[]
Definition: session.c:153
srtp_crypt
static int srtp_crypt(srtp_session_t *s, uint8_t *buf, size_t len)
Encrypts/decrypts a RTP packet and updates SRTP context (CTR block cypher mode of operation has ident...
Definition: srtp.c:461
rtp_pt_t::init
void *(* init)(demux_t *)
Definition: rtp.h:31
stream_t
stream_t definition
Definition: vlc_stream.h:46
block_t::i_pts
vlc_tick_t i_pts
Definition: vlc_block.h:129
srtp_proto_t::window
uint64_t window
Definition: srtp.c:54
srtp_session_t::rtp_rcc
uint16_t rtp_rcc
Definition: srtp.c:67
SRTP_FLAGS_MASK
@ SRTP_FLAGS_MASK
Definition: srtp.h:35
SRTP_UNENCRYPTED
@ SRTP_UNENCRYPTED
Definition: srtp.h:27
srtp_session_t::rtcp_index
uint32_t rtcp_index
Definition: srtp.c:64
SRTP_RCC_MODE2
@ SRTP_RCC_MODE2
Definition: srtp.h:32
rtp_find_ptype
static const struct rtp_pt_t * rtp_find_ptype(const rtp_session_t *session, rtp_source_t *source, const block_t *block, void **pt_data)
Definition: session.c:214
rtp_dequeue
bool rtp_dequeue(demux_t *demux, const rtp_session_t *session, vlc_tick_t *restrict deadlinep)
Dequeues RTP packets and pass them to decoder.
Definition: session.c:393
SRTCP_CRYPT
@ SRTCP_CRYPT
Definition: srtp.c:76
rtp_crypt
static int rtp_crypt(gcry_cipher_hd_t hd, uint32_t ssrc, uint32_t roc, uint16_t seq, const uint32_t *salt, uint8_t *data, size_t len)
AES-CM for RTP (salt = 14 bytes + 2 nul bytes)
Definition: srtp.c:393
srtp_create
srtp_session_t * srtp_create(int encr, int auth, unsigned tag_len, int prf, unsigned flags)
Allocates a Secure RTP one-way session.
Definition: srtp.c:134
rtp.h
RTP demux module shared declarations.
rtp_pt_t::decode
void(* decode)(demux_t *, void *, block_t *)
Definition: rtp.h:34
srtp_session_t::flags
unsigned flags
Definition: srtp.c:62
SRTP_PRF_AES_CM
@ SRTP_PRF_AES_CM
Definition: srtp.h:56
samples_from_vlc_tick
static int64_t samples_from_vlc_tick(vlc_tick_t t, int samp_rate)
Definition: vlc_tick.h:105
rtp_source_t
State for an RTP source.
Definition: session.c:138
VLC_TICK_FROM_MS
#define VLC_TICK_FROM_MS(ms)
Definition: vlc_tick.h:122
GetWBE
#define GetWBE(p)
Definition: vlc_common.h:1013
rtp_source_t::last_seq
uint16_t last_seq
Definition: session.c:151
srtp_session_t::tag_len
uint8_t tag_len
Definition: srtp.c:68
SRTCP_SALT
@ SRTCP_SALT
Definition: srtp.c:78
demux_sys_t
Definition: rtp.h:63
main
int main(void)
Definition: srtp-test-recv.c:36
rtp_source_t::jitter
uint32_t jitter
Definition: session.c:141
srtp_send
int srtp_send(srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t maxsize)
Turns a RTP packet into a SRTP packet: encrypt it, then computes the authentication tag and appends i...
Definition: srtp.c:540
srtp_recv
int srtp_recv(srtp_session_t *s, uint8_t *buf, size_t *lenp)
Turns a SRTP packet into a RTP packet: authenticates the packet, then decrypts it.
Definition: srtp.c:623
SRTP_CRYPT
@ SRTP_CRYPT
Definition: srtp.c:73
srtp_session_t::rtp_roc
uint32_t rtp_roc
Definition: srtp.c:65
srtp_session_t::kdr
unsigned kdr
Definition: srtp.c:63
SRTP_SALT
@ SRTP_SALT
Definition: srtp.c:75
block_t::p_buffer
uint8_t * p_buffer
Payload start.
Definition: vlc_block.h:121
srtp_send
int srtp_send(srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
Turns a RTP packet into a SRTP packet: encrypt it, then computes the authentication tag and appends i...
Definition: srtp.c:540
rtp_session_create
rtp_session_t * rtp_session_create(demux_t *demux)
Creates a new RTP session.
Definition: session.c:58
rtp_pt_t::destroy
void(* destroy)(demux_t *, void *)
Definition: rtp.h:32
rtp_session_t::ptv
rtp_pt_t * ptv
Definition: session.c:44
rtp_pt_t::number
uint8_t number
Definition: rtp.h:36
rtp_ptype
static uint8_t rtp_ptype(const block_t *block)
Definition: rtp.h:40
SRTP_AUTH_NULL
@ SRTP_AUTH_NULL
Definition: srtp.h:49
test_keystream
static void test_keystream(void)
AES-CM key derivation test vectors.
Definition: srtp-test-aes.c:95
srtp_compute_roc
static uint32_t srtp_compute_roc(const srtp_session_t *s, uint16_t seq)
Determines SRTP Roll-Over-Counter (in host-byte order)
Definition: srtp.c:410
BLOCK_FLAG_DISCONTINUITY
#define BLOCK_FLAG_DISCONTINUITY
The content doesn't follow the last block, possible some blocks in between have been lost.
Definition: vlc_block.h:67
SRTP_RCC_MODE1
@ SRTP_RCC_MODE1
Definition: srtp.h:31
rtp_add_type
int rtp_add_type(demux_t *demux, rtp_session_t *ses, const rtp_pt_t *pt)
Adds a payload type to an RTP session.
Definition: session.c:108
SRTP_AUTH_HMAC_SHA1
@ SRTP_AUTH_HMAC_SHA1
Definition: srtp.h:50
block_Release
void block_Release(block_t *block)
Releases a block.
Definition: block.c:135
no_decode
static void no_decode(demux_t *demux, void *opaque, block_t *block)
Definition: session.c:99
demux_sys_t::max_src
uint8_t max_src
Max simultaneous RTP sources.
Definition: rtp.h:77
stream_t::p_sys
void * p_sys
Private data pointer.
Definition: vlc_stream.h:138
block_t
Definition: vlc_block.h:117
msg_Err
#define msg_Err(p_this,...)
Definition: vlc_messages.h:102
rtp_source_t::last_ts
uint32_t last_ts
Definition: session.c:143
rtp_queue
void rtp_queue(demux_t *demux, rtp_session_t *session, block_t *block)
Receives an RTP packet and queues it.
Definition: session.c:239
srtcp_recv
int srtcp_recv(srtp_session_t *s, uint8_t *buf, size_t *lenp)
Turns a SRTCP packet into a RTCP packet: authenticates the packet, then decrypts it.
Definition: srtp.c:821
srtp_setrcc_rate
void srtp_setrcc_rate(srtp_session_t *s, uint16_t rate)
Sets Roll-over-Counter Carry (RCC) rate for the SRTP session.
Definition: srtp.c:384
rtp_source_t::bad_seq
uint16_t bad_seq
Definition: session.c:148
p
#define p(t)
main
int main(void)
Definition: srtp-test-aes.c:150
demux_sys_t::max_dropout
uint16_t max_dropout
Max packet forward misordering.
Definition: rtp.h:75
srtp_setkeystring
int srtp_setkeystring(srtp_session_t *s, const char *key, const char *salt)
Sets (or resets) the master key and master salt for a SRTP session from hexadecimal strings.
Definition: srtp.c:352