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