1 /**
2  * @file srtp.h  Secure Real-time Transport Protocol (SRTP) -- internal
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 
7 
8 /** SRTP Protocol values */
9 enum {
10 	GCM_TAGLEN  = 16,  /**< GCM taglength in bytes         */
11 };
12 
13 
14 /** Defines a 128-bit vector in network order */
15 union vect128 {
16 	uint64_t u64[ 2];
17 	uint32_t u32[ 4];
18 	uint16_t u16[ 8];
19 	uint8_t   u8[16];
20 };
21 
22 /** Replay protection */
23 struct replay {
24 	uint64_t bitmap;   /**< Session state - must be 64 bits */
25 	uint64_t lix;      /**< Last received index             */
26 };
27 
28 /** SRTP stream/context -- shared state between RTP/RTCP */
29 struct srtp_stream {
30 	struct le le;              /**< Linked-list element                */
31 	struct replay replay_rtp;  /**< recv -- replay protection for RTP  */
32 	struct replay replay_rtcp; /**< recv -- replay protection for RTCP */
33 	uint32_t ssrc;             /**< SSRC -- lookup key                 */
34 	uint32_t roc;              /**< send/recv Roll-Over Counter (ROC)  */
35 	uint16_t s_l;              /**< send/recv -- highest SEQ number    */
36 	bool s_l_set;              /**< True if s_l has been set           */
37 	uint32_t rtcp_index;       /**< RTCP-index for sending (31-bits)   */
38 };
39 
40 /** SRTP Session */
41 struct srtp {
42 	struct comp {
43 		struct aes *aes;    /**< AES Context                       */
44 		enum aes_mode mode; /**< AES encryption mode               */
45 		struct hmac *hmac;  /**< HMAC Context                      */
46 		union vect128 k_s;  /**< Derived salting key (14 bytes)    */
47 		size_t tag_len;     /**< CTR Auth. tag length [bytes]      */
48 	} rtp, rtcp;
49 
50 	struct list streaml;        /**< SRTP-streams (struct srtp_stream) */
51 };
52 
53 
54 int stream_get(struct srtp_stream **strmp, struct srtp *srtp, uint32_t ssrc);
55 int stream_get_seq(struct srtp_stream **strmp, struct srtp *srtp,
56 		   uint32_t ssrc, uint16_t seq);
57 
stun_response_handler(int err,uint16_t scode,const char * reason,const struct stun_msg * msg,void * arg)58 
59 int  srtp_derive(uint8_t *out, size_t out_len, uint8_t label,
60 		 const uint8_t *master_key, size_t key_bytes,
61 		 const uint8_t *master_salt, size_t salt_bytes);
62 void srtp_iv_calc(union vect128 *iv, const union vect128 *k_s,
63 		  uint32_t ssrc, uint64_t ix);
64 void srtp_iv_calc_gcm(union vect128 *iv, const union vect128 *k_s,
65 		      uint32_t ssrc, uint64_t ix);
66 uint64_t srtp_get_index(uint32_t roc, uint16_t s_l, uint16_t seq);
67 
68 
69 /* Replay protection */
70 
71 void srtp_replay_init(struct replay *replay);
72 bool srtp_replay_check(struct replay *replay, uint64_t ix);
73