srtp_priv.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2006 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. #include "config.h"
  47. #include "srtp.h"
  48. #include "rdbx.h"
  49. #include "rdb.h"
  50. #include "integers.h"
  51. #include "crypto.h"
  52. #include "cipher.h"
  53. #include "auth.h"
  54. #include "aes.h"
  55. #include "key.h"
  56. #include "crypto_kernel.h"
  57. #define SRTP_VER_STRING PACKAGE_STRING
  58. #define SRTP_VERSION PACKAGE_VERSION
  59. /*
  60. * an srtp_hdr_t represents the srtp header
  61. *
  62. * in this implementation, an srtp_hdr_t is assumed to be 32-bit aligned
  63. *
  64. * (note that this definition follows that of RFC 1889 Appendix A, but
  65. * is not identical)
  66. */
  67. #ifndef WORDS_BIGENDIAN
  68. /*
  69. * srtp_hdr_t represents an RTP or SRTP header. The bit-fields in
  70. * this structure should be declared "unsigned int" instead of
  71. * "unsigned char", but doing so causes the MS compiler to not
  72. * fully pack the bit fields.
  73. */
  74. typedef struct {
  75. unsigned char cc:4; /* CSRC count */
  76. unsigned char x:1; /* header extension flag */
  77. unsigned char p:1; /* padding flag */
  78. unsigned char version:2; /* protocol version */
  79. unsigned char pt:7; /* payload type */
  80. unsigned char m:1; /* marker bit */
  81. uint16_t seq; /* sequence number */
  82. uint32_t ts; /* timestamp */
  83. uint32_t ssrc; /* synchronization source */
  84. } srtp_hdr_t;
  85. #else /* BIG_ENDIAN */
  86. typedef struct {
  87. unsigned char version:2; /* protocol version */
  88. unsigned char p:1; /* padding flag */
  89. unsigned char x:1; /* header extension flag */
  90. unsigned char cc:4; /* CSRC count */
  91. unsigned char m:1; /* marker bit */
  92. unsigned char pt:7; /* payload type */
  93. uint16_t seq; /* sequence number */
  94. uint32_t ts; /* timestamp */
  95. uint32_t ssrc; /* synchronization source */
  96. } srtp_hdr_t;
  97. #endif
  98. typedef struct {
  99. uint16_t profile_specific; /* profile-specific info */
  100. uint16_t length; /* number of 32-bit words in extension */
  101. } srtp_hdr_xtnd_t;
  102. /*
  103. * srtcp_hdr_t represents a secure rtcp header
  104. *
  105. * in this implementation, an srtcp header is assumed to be 32-bit
  106. * alinged
  107. */
  108. #ifndef WORDS_BIGENDIAN
  109. typedef struct {
  110. unsigned char rc:5; /* reception report count */
  111. unsigned char p:1; /* padding flag */
  112. unsigned char version:2; /* protocol version */
  113. unsigned char pt:8; /* payload type */
  114. uint16_t len; /* length */
  115. uint32_t ssrc; /* synchronization source */
  116. } srtcp_hdr_t;
  117. typedef struct {
  118. unsigned int index:31; /* srtcp packet index in network order! */
  119. unsigned int e:1; /* encrypted? 1=yes */
  120. /* optional mikey/etc go here */
  121. /* and then the variable-length auth tag */
  122. } srtcp_trailer_t;
  123. #else /* BIG_ENDIAN */
  124. typedef struct {
  125. unsigned char version:2; /* protocol version */
  126. unsigned char p:1; /* padding flag */
  127. unsigned char rc:5; /* reception report count */
  128. unsigned char pt:8; /* payload type */
  129. uint16_t len; /* length */
  130. uint32_t ssrc; /* synchronization source */
  131. } srtcp_hdr_t;
  132. typedef struct {
  133. unsigned int version:2; /* protocol version */
  134. unsigned int p:1; /* padding flag */
  135. unsigned int count:5; /* varies by packet type */
  136. unsigned int pt:8; /* payload type */
  137. uint16_t length; /* len of uint32s of packet less header */
  138. } rtcp_common_t;
  139. typedef struct {
  140. unsigned int e:1; /* encrypted? 1=yes */
  141. unsigned int index:31; /* srtcp packet index */
  142. /* optional mikey/etc go here */
  143. /* and then the variable-length auth tag */
  144. } srtcp_trailer_t;
  145. #endif
  146. /*
  147. * the following declarations are libSRTP internal functions
  148. */
  149. /*
  150. * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
  151. * to ssrc, or NULL if no stream exists for that ssrc
  152. */
  153. srtp_stream_t
  154. srtp_get_stream(srtp_t srtp, uint32_t ssrc);
  155. /*
  156. * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by
  157. * deriving all of the needed keys using the KDF and the key k.
  158. */
  159. err_status_t
  160. srtp_stream_init_keys(srtp_stream_t srtp, const void *key);
  161. /*
  162. * srtp_stream_init(s, p) initializes the srtp_stream_t s to
  163. * use the policy at the location p
  164. */
  165. err_status_t
  166. srtp_stream_init(srtp_stream_t srtp,
  167. const srtp_policy_t *p);
  168. /*
  169. * libsrtp internal datatypes
  170. */
  171. typedef enum direction_t {
  172. dir_unknown = 0,
  173. dir_srtp_sender = 1,
  174. dir_srtp_receiver = 2
  175. } direction_t;
  176. /*
  177. * an srtp_stream_t has its own SSRC, encryption key, authentication
  178. * key, sequence number, and replay database
  179. *
  180. * note that the keys might not actually be unique, in which case the
  181. * cipher_t and auth_t pointers will point to the same structures
  182. */
  183. typedef struct srtp_stream_ctx_t {
  184. uint32_t ssrc;
  185. cipher_t *rtp_cipher;
  186. auth_t *rtp_auth;
  187. rdbx_t rtp_rdbx;
  188. sec_serv_t rtp_services;
  189. cipher_t *rtcp_cipher;
  190. auth_t *rtcp_auth;
  191. rdb_t rtcp_rdb;
  192. sec_serv_t rtcp_services;
  193. key_limit_ctx_t *limit;
  194. direction_t direction;
  195. int allow_repeat_tx;
  196. ekt_stream_t ekt;
  197. uint8_t salt[SRTP_AEAD_SALT_LEN]; /* used with GCM mode for SRTP */
  198. uint8_t c_salt[SRTP_AEAD_SALT_LEN]; /* used with GCM mode for SRTCP */
  199. struct srtp_stream_ctx_t *next; /* linked list of streams */
  200. } srtp_stream_ctx_t;
  201. /*
  202. * an srtp_ctx_t holds a stream list and a service description
  203. */
  204. typedef struct srtp_ctx_t {
  205. srtp_stream_ctx_t *stream_list; /* linked list of streams */
  206. srtp_stream_ctx_t *stream_template; /* act as template for other streams */
  207. void *user_data; /* user custom data */
  208. } srtp_ctx_t;
  209. /*
  210. * srtp_handle_event(srtp, srtm, evnt) calls the event handling
  211. * function, if there is one.
  212. *
  213. * This macro is not included in the documentation as it is
  214. * an internal-only function.
  215. */
  216. #define srtp_handle_event(srtp, strm, evnt) \
  217. if(srtp_event_handler) { \
  218. srtp_event_data_t data; \
  219. data.session = srtp; \
  220. data.stream = strm; \
  221. data.event = evnt; \
  222. srtp_event_handler(&data); \
  223. }
  224. #endif /* SRTP_PRIV_H */