srtp_priv.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * srtp_priv.h
  3. *
  4. * private internal data structures and functions for libSRTP
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017 Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef SRTP_PRIV_H
  45. #define SRTP_PRIV_H
  46. // Leave this as the top level import. Ensures the existence of defines
  47. #include "config.h"
  48. #include "srtp.h"
  49. #include "rdbx.h"
  50. #include "rdb.h"
  51. #include "integers.h"
  52. #include "cipher.h"
  53. #include "auth.h"
  54. #include "aes.h"
  55. #include "crypto_kernel.h"
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. #define SRTP_VER_STRING PACKAGE_STRING
  60. #define SRTP_VERSION PACKAGE_VERSION
  61. typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t;
  62. typedef srtp_stream_ctx_t *srtp_stream_t;
  63. /*
  64. * the following declarations are libSRTP internal functions
  65. */
  66. /*
  67. * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
  68. * to ssrc, or NULL if no stream exists for that ssrc
  69. */
  70. srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
  71. /*
  72. * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by
  73. * deriving all of the needed keys using the KDF and the key k.
  74. */
  75. srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp,
  76. srtp_master_key_t *master_key,
  77. const unsigned int current_mki_index);
  78. /*
  79. * srtp_stream_init_all_master_keys(s, k, m) (re)initializes the srtp_stream_t s
  80. * by deriving all of the needed keys for all the master keys using the KDF and
  81. * the keys from k.
  82. */
  83. srtp_err_status_t srtp_steam_init_all_master_keys(
  84. srtp_stream_ctx_t *srtp,
  85. unsigned char *key,
  86. srtp_master_key_t **keys,
  87. const unsigned int max_master_keys);
  88. /*
  89. * srtp_stream_init(s, p) initializes the srtp_stream_t s to
  90. * use the policy at the location p
  91. */
  92. srtp_err_status_t srtp_stream_init(srtp_stream_t srtp, const srtp_policy_t *p);
  93. /*
  94. * libsrtp internal datatypes
  95. */
  96. typedef enum direction_t {
  97. dir_unknown = 0,
  98. dir_srtp_sender = 1,
  99. dir_srtp_receiver = 2
  100. } direction_t;
  101. /*
  102. * srtp_session_keys_t will contain the encryption, hmac, salt keys
  103. * for both SRTP and SRTCP. The session keys will also contain the
  104. * MKI ID which is used to identify the session keys.
  105. */
  106. typedef struct srtp_session_keys_t {
  107. srtp_cipher_t *rtp_cipher;
  108. srtp_cipher_t *rtp_xtn_hdr_cipher;
  109. srtp_auth_t *rtp_auth;
  110. srtp_cipher_t *rtcp_cipher;
  111. srtp_auth_t *rtcp_auth;
  112. uint8_t salt[SRTP_AEAD_SALT_LEN];
  113. uint8_t c_salt[SRTP_AEAD_SALT_LEN];
  114. uint8_t *mki_id;
  115. unsigned int mki_size;
  116. srtp_key_limit_ctx_t *limit;
  117. } srtp_session_keys_t;
  118. /*
  119. * an srtp_stream_t has its own SSRC, encryption key, authentication
  120. * key, sequence number, and replay database
  121. *
  122. * note that the keys might not actually be unique, in which case the
  123. * srtp_cipher_t and srtp_auth_t pointers will point to the same structures
  124. */
  125. typedef struct srtp_stream_ctx_t_ {
  126. uint32_t ssrc;
  127. srtp_session_keys_t *session_keys;
  128. unsigned int num_master_keys;
  129. srtp_rdbx_t rtp_rdbx;
  130. srtp_sec_serv_t rtp_services;
  131. srtp_rdb_t rtcp_rdb;
  132. srtp_sec_serv_t rtcp_services;
  133. direction_t direction;
  134. int allow_repeat_tx;
  135. int *enc_xtn_hdr;
  136. int enc_xtn_hdr_count;
  137. uint32_t pending_roc;
  138. struct srtp_stream_ctx_t_ *next; /* linked list of streams */
  139. } strp_stream_ctx_t_;
  140. /*
  141. * an srtp_ctx_t holds a stream list and a service description
  142. */
  143. typedef struct srtp_ctx_t_ {
  144. struct srtp_stream_ctx_t_ *stream_list; /* linked list of streams */
  145. struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */
  146. /* streams */
  147. void *user_data; /* user custom data */
  148. } srtp_ctx_t_;
  149. /*
  150. * srtp_hdr_t represents an RTP or SRTP header. The bit-fields in
  151. * this structure should be declared "unsigned int" instead of
  152. * "unsigned char", but doing so causes the MS compiler to not
  153. * fully pack the bit fields.
  154. *
  155. * In this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
  156. *
  157. * (note that this definition follows that of RFC 1889 Appendix A, but
  158. * is not identical)
  159. */
  160. #ifdef _MSC_VER
  161. #pragma pack(push, r1, 1)
  162. #endif
  163. #ifndef WORDS_BIGENDIAN
  164. typedef struct {
  165. unsigned cc : 4; /* CSRC count */
  166. unsigned x : 1; /* header extension flag */
  167. unsigned p : 1; /* padding flag */
  168. unsigned version : 2; /* protocol version */
  169. unsigned pt : 7; /* payload type */
  170. unsigned m : 1; /* marker bit */
  171. unsigned seq : 16; /* sequence number */
  172. unsigned ts : 32; /* timestamp */
  173. uint32_t ssrc; /* synchronization source */
  174. } srtp_hdr_t;
  175. #else /* BIG_ENDIAN */
  176. typedef struct {
  177. unsigned version : 2; /* protocol version */
  178. unsigned p : 1; /* padding flag */
  179. unsigned x : 1; /* header extension flag */
  180. unsigned cc : 4; /* CSRC count */
  181. unsigned m : 1; /* marker bit */
  182. unsigned pt : 7; /* payload type */
  183. unsigned seq: 16; /* sequence number */
  184. unsigned ts : 32; /* timestamp */
  185. uint32_t ssrc; /* synchronization source */
  186. } srtp_hdr_t;
  187. #endif
  188. typedef struct {
  189. uint16_t profile_specific; /* profile-specific info */
  190. uint16_t length; /* number of 32-bit words in extension */
  191. } srtp_hdr_xtnd_t;
  192. /*
  193. * srtcp_hdr_t represents a secure rtcp header
  194. *
  195. * in this implementation, an srtcp header is assumed to be 32-bit
  196. * aligned
  197. */
  198. #ifndef WORDS_BIGENDIAN
  199. typedef struct {
  200. unsigned rc : 5; /* reception report count */
  201. unsigned p : 1; /* padding flag */
  202. unsigned version : 2; /* protocol version */
  203. unsigned pt : 8; /* payload type */
  204. unsigned len : 16; /* length */
  205. uint32_t ssrc; /* synchronization source */
  206. } srtcp_hdr_t;
  207. typedef struct {
  208. unsigned int index : 31; /* srtcp packet index in network order! */
  209. unsigned int e : 1; /* encrypted? 1=yes */
  210. /* optional mikey/etc go here */
  211. /* and then the variable-length auth tag */
  212. } srtcp_trailer_t;
  213. #else /* BIG_ENDIAN */
  214. typedef struct {
  215. unsigned version : 2; /* protocol version */
  216. unsigned p : 1; /* padding flag */
  217. unsigned rc : 5; /* reception report count */
  218. unsigned pt : 8; /* payload type */
  219. unsigned len : 16; /* length */
  220. uint32_t ssrc; /* synchronization source */
  221. } srtcp_hdr_t;
  222. typedef struct {
  223. unsigned int e : 1; /* encrypted? 1=yes */
  224. unsigned int index : 31; /* srtcp packet index */
  225. /* optional mikey/etc go here */
  226. /* and then the variable-length auth tag */
  227. } srtcp_trailer_t;
  228. #endif
  229. #ifdef _MSC_VER
  230. #pragma pack(pop, r1)
  231. #endif
  232. /*
  233. * srtp_handle_event(srtp, srtm, evnt) calls the event handling
  234. * function, if there is one.
  235. *
  236. * This macro is not included in the documentation as it is
  237. * an internal-only function.
  238. */
  239. #define srtp_handle_event(srtp, strm, evnt) \
  240. if (srtp_event_handler) { \
  241. srtp_event_data_t data; \
  242. data.session = srtp; \
  243. data.ssrc = ntohl(strm->ssrc); \
  244. data.event = evnt; \
  245. srtp_event_handler(&data); \
  246. }
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif /* SRTP_PRIV_H */