srtp_priv.h (9759B)
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 45 #ifndef SRTP_PRIV_H 46 #define SRTP_PRIV_H 47 48 // Leave this as the top level import. Ensures the existence of defines 49 #include "config.h" 50 51 #include "srtp.h" 52 #include "rdbx.h" 53 #include "rdb.h" 54 #include "integers.h" 55 #include "cipher.h" 56 #include "auth.h" 57 #include "aes.h" 58 #include "crypto_kernel.h" 59 60 #ifdef __cplusplus 61 extern "C" { 62 #endif 63 64 #define SRTP_VER_STRING PACKAGE_STRING 65 #define SRTP_VERSION PACKAGE_VERSION 66 67 typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t; 68 typedef srtp_stream_ctx_t *srtp_stream_t; 69 typedef struct srtp_stream_list_ctx_t_ *srtp_stream_list_t; 70 71 /* 72 * the following declarations are libSRTP internal functions 73 */ 74 75 /* 76 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding 77 * to ssrc, or NULL if no stream exists for that ssrc 78 */ 79 srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc); 80 81 /* 82 * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by 83 * deriving all of the needed keys using the KDF and the key k. 84 */ 85 srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp, 86 srtp_master_key_t *master_key, 87 const unsigned int current_mki_index); 88 89 /* 90 * srtp_stream_init_all_master_keys(s, k, m) (re)initializes the srtp_stream_t s 91 * by deriving all of the needed keys for all the master keys using the KDF and 92 * the keys from k. 93 */ 94 srtp_err_status_t srtp_steam_init_all_master_keys( 95 srtp_stream_ctx_t *srtp, 96 unsigned char *key, 97 srtp_master_key_t **keys, 98 const unsigned int max_master_keys); 99 100 /* 101 * libsrtp internal datatypes 102 */ 103 typedef enum direction_t { 104 dir_unknown = 0, 105 dir_srtp_sender = 1, 106 dir_srtp_receiver = 2 107 } direction_t; 108 109 /* 110 * srtp_session_keys_t will contain the encryption, hmac, salt keys 111 * for both SRTP and SRTCP. The session keys will also contain the 112 * MKI ID which is used to identify the session keys. 113 */ 114 typedef struct srtp_session_keys_t { 115 srtp_cipher_t *rtp_cipher; 116 srtp_cipher_t *rtp_xtn_hdr_cipher; 117 srtp_auth_t *rtp_auth; 118 srtp_cipher_t *rtcp_cipher; 119 srtp_auth_t *rtcp_auth; 120 uint8_t salt[SRTP_AEAD_SALT_LEN]; 121 uint8_t c_salt[SRTP_AEAD_SALT_LEN]; 122 uint8_t *mki_id; 123 unsigned int mki_size; 124 srtp_key_limit_ctx_t *limit; 125 } srtp_session_keys_t; 126 127 /* 128 * an srtp_stream_t has its own SSRC, encryption key, authentication 129 * key, sequence number, and replay database 130 * 131 * note that the keys might not actually be unique, in which case the 132 * srtp_cipher_t and srtp_auth_t pointers will point to the same structures 133 */ 134 typedef struct srtp_stream_ctx_t_ { 135 uint32_t ssrc; 136 srtp_session_keys_t *session_keys; 137 unsigned int num_master_keys; 138 srtp_rdbx_t rtp_rdbx; 139 srtp_sec_serv_t rtp_services; 140 srtp_rdb_t rtcp_rdb; 141 srtp_sec_serv_t rtcp_services; 142 direction_t direction; 143 int allow_repeat_tx; 144 int *enc_xtn_hdr; 145 int enc_xtn_hdr_count; 146 uint32_t pending_roc; 147 /* 148 The next and prev pointers are here to allow for a stream list to be 149 implemented as an intrusive doubly-linked list (the former being the 150 default). Other stream list implementations can ignore these fields or use 151 them for some other purpose specific to the stream list implementation. 152 */ 153 struct srtp_stream_ctx_t_ *next; 154 struct srtp_stream_ctx_t_ *prev; 155 } strp_stream_ctx_t_; 156 157 /* 158 * an srtp_ctx_t holds a stream list and a service description 159 */ 160 typedef struct srtp_ctx_t_ { 161 srtp_stream_list_t stream_list; /* linked list of streams */ 162 struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */ 163 /* streams */ 164 void *user_data; /* user custom data */ 165 } srtp_ctx_t_; 166 167 /* 168 * srtp_hdr_t represents an RTP or SRTP header. The bit-fields in 169 * this structure should be declared "unsigned int" instead of 170 * "unsigned char", but doing so causes the MS compiler to not 171 * fully pack the bit fields. 172 * 173 * In this implementation, an srtp_hdr_t is assumed to be 32-bit aligned 174 * 175 * (note that this definition follows that of RFC 1889 Appendix A, but 176 * is not identical) 177 */ 178 179 #ifndef WORDS_BIGENDIAN 180 181 typedef struct { 182 unsigned char cc : 4; /* CSRC count */ 183 unsigned char x : 1; /* header extension flag */ 184 unsigned char p : 1; /* padding flag */ 185 unsigned char version : 2; /* protocol version */ 186 unsigned char pt : 7; /* payload type */ 187 unsigned char m : 1; /* marker bit */ 188 uint16_t seq; /* sequence number */ 189 uint32_t ts; /* timestamp */ 190 uint32_t ssrc; /* synchronization source */ 191 } srtp_hdr_t; 192 193 #else /* BIG_ENDIAN */ 194 195 typedef struct { 196 unsigned char version : 2; /* protocol version */ 197 unsigned char p : 1; /* padding flag */ 198 unsigned char x : 1; /* header extension flag */ 199 unsigned char cc : 4; /* CSRC count */ 200 unsigned char m : 1; /* marker bit */ 201 unsigned char pt : 7; /* payload type */ 202 uint16_t seq; /* sequence number */ 203 uint32_t ts; /* timestamp */ 204 uint32_t ssrc; /* synchronization source */ 205 } srtp_hdr_t; 206 207 #endif 208 209 typedef struct { 210 uint16_t profile_specific; /* profile-specific info */ 211 uint16_t length; /* number of 32-bit words in extension */ 212 } srtp_hdr_xtnd_t; 213 214 /* 215 * srtcp_hdr_t represents a secure rtcp header 216 * 217 * in this implementation, an srtcp header is assumed to be 32-bit 218 * aligned 219 */ 220 221 #ifndef WORDS_BIGENDIAN 222 223 typedef struct { 224 unsigned char rc : 5; /* reception report count */ 225 unsigned char p : 1; /* padding flag */ 226 unsigned char version : 2; /* protocol version */ 227 unsigned char pt : 8; /* payload type */ 228 uint16_t len; /* length */ 229 uint32_t ssrc; /* synchronization source */ 230 } srtcp_hdr_t; 231 232 typedef struct { 233 unsigned int index : 31; /* srtcp packet index in network order! */ 234 unsigned int e : 1; /* encrypted? 1=yes */ 235 /* optional mikey/etc go here */ 236 /* and then the variable-length auth tag */ 237 } srtcp_trailer_t; 238 239 #else /* BIG_ENDIAN */ 240 241 typedef struct { 242 unsigned char version : 2; /* protocol version */ 243 unsigned char p : 1; /* padding flag */ 244 unsigned char rc : 5; /* reception report count */ 245 unsigned char pt : 8; /* payload type */ 246 uint16_t len; /* length */ 247 uint32_t ssrc; /* synchronization source */ 248 } srtcp_hdr_t; 249 250 typedef struct { 251 unsigned int e : 1; /* encrypted? 1=yes */ 252 unsigned int index : 31; /* srtcp packet index */ 253 /* optional mikey/etc go here */ 254 /* and then the variable-length auth tag */ 255 } srtcp_trailer_t; 256 257 #endif 258 259 /* 260 * srtp_handle_event(srtp, srtm, evnt) calls the event handling 261 * function, if there is one. 262 * 263 * This macro is not included in the documentation as it is 264 * an internal-only function. 265 */ 266 267 #define srtp_handle_event(srtp, strm, evnt) \ 268 if (srtp_event_handler) { \ 269 srtp_event_data_t data; \ 270 data.session = srtp; \ 271 data.ssrc = ntohl(strm->ssrc); \ 272 data.event = evnt; \ 273 srtp_event_handler(&data); \ 274 } 275 276 #ifdef __cplusplus 277 } 278 #endif 279 280 #endif /* SRTP_PRIV_H */