blob: 34fac1d28291613c006ea776421741ecdac6234e [file] [log] [blame]
David McGrew3c45e0c2006-07-12 00:50:56 +00001/*
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
Teerapap Changwichukarn6cffe242014-09-24 11:24:07 +080048#include "config.h"
David McGrew3c45e0c2006-07-12 00:50:56 +000049#include "srtp.h"
50#include "rdbx.h"
51#include "rdb.h"
52#include "integers.h"
jfigus44947602014-10-08 13:08:52 -040053#include "cipher.h"
54#include "auth.h"
55#include "aes.h"
56#include "key.h"
57#include "crypto_kernel.h"
David McGrew3c45e0c2006-07-12 00:50:56 +000058
David Benjamin5f1b9822016-07-26 18:18:01 -040059#ifdef __cplusplus
60extern "C" {
61#endif
62
jfigusf62b64d2014-10-08 13:53:57 -040063#define SRTP_VER_STRING PACKAGE_STRING
64#define SRTP_VERSION PACKAGE_VERSION
65
Pascal Bühlerf15c7e02017-01-25 13:15:21 +010066typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t;
67typedef srtp_stream_ctx_t *srtp_stream_t;
68
David McGrew3c45e0c2006-07-12 00:50:56 +000069/*
David McGrew3c45e0c2006-07-12 00:50:56 +000070 * the following declarations are libSRTP internal functions
71 */
72
73/*
74 * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
75 * to ssrc, or NULL if no stream exists for that ssrc
76 */
jfigus857009c2014-11-05 11:17:43 -050077srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
David McGrew3c45e0c2006-07-12 00:50:56 +000078
79
80/*
81 * srtp_stream_init_keys(s, k) (re)initializes the srtp_stream_t s by
82 * deriving all of the needed keys using the KDF and the key k.
83 */
Geir Istad626e9e82017-02-20 18:15:05 +010084srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp,
85 srtp_master_key_t *master_key,
86 const unsigned int current_mki_index);
Ryan Hooperfe5d8b82016-12-15 14:53:58 -050087
88/*
89 * srtp_stream_init_all_master_keys(s, k, m) (re)initializes the srtp_stream_t s by
90 * deriving all of the needed keys for all the master keys using the KDF and the keys from k.
91 */
Ryan Hooper89a288b2016-12-22 16:22:47 -050092srtp_err_status_t srtp_steam_init_all_master_keys(srtp_stream_ctx_t *srtp,
Geir Istad626e9e82017-02-20 18:15:05 +010093 unsigned char *key,
94 srtp_master_key_t **keys,
Ryan Hooperfe5d8b82016-12-15 14:53:58 -050095 const unsigned int max_master_keys);
David McGrew3c45e0c2006-07-12 00:50:56 +000096
97/*
David McGrew79870d62007-06-15 18:17:39 +000098 * srtp_stream_init(s, p) initializes the srtp_stream_t s to
99 * use the policy at the location p
100 */
jfigus857009c2014-11-05 11:17:43 -0500101srtp_err_status_t srtp_stream_init(srtp_stream_t srtp, const srtp_policy_t *p);
David McGrew79870d62007-06-15 18:17:39 +0000102
103
104/*
David McGrew3c45e0c2006-07-12 00:50:56 +0000105 * libsrtp internal datatypes
106 */
107
108typedef enum direction_t {
109 dir_unknown = 0,
110 dir_srtp_sender = 1,
111 dir_srtp_receiver = 2
112} direction_t;
113
Ryan Hooperfe5d8b82016-12-15 14:53:58 -0500114/*
115 * srtp_session_keys_t will contain the encryption, hmac, salt keys
116 * for both SRTP and SRTCP. The session keys will also contain the
117 * MKI ID which is used to identify the session keys.
118 */
119typedef struct srtp_session_keys_t {
120 srtp_cipher_t *rtp_cipher;
121 srtp_cipher_t *rtp_xtn_hdr_cipher;
122 srtp_auth_t *rtp_auth;
123 srtp_cipher_t *rtcp_cipher;
124 srtp_auth_t *rtcp_auth;
125 uint8_t salt[SRTP_AEAD_SALT_LEN];
126 uint8_t c_salt[SRTP_AEAD_SALT_LEN];
127 uint8_t *mki_id;
128 unsigned int mki_size;
Ryan Hooperf38d8932017-01-31 16:11:54 -0500129 srtp_key_limit_ctx_t *limit;
Ryan Hooperfe5d8b82016-12-15 14:53:58 -0500130} srtp_session_keys_t;
131
132
David McGrew3c45e0c2006-07-12 00:50:56 +0000133/*
134 * an srtp_stream_t has its own SSRC, encryption key, authentication
135 * key, sequence number, and replay database
136 *
137 * note that the keys might not actually be unique, in which case the
jfigus9a840432014-11-19 15:48:21 -0500138 * srtp_cipher_t and srtp_auth_t pointers will point to the same structures
David McGrew3c45e0c2006-07-12 00:50:56 +0000139 */
140
jfigusa9ac8982014-10-31 14:49:31 -0400141typedef struct srtp_stream_ctx_t_ {
David McGrew3c45e0c2006-07-12 00:50:56 +0000142 uint32_t ssrc;
Ryan Hooper89a288b2016-12-22 16:22:47 -0500143 srtp_session_keys_t *session_keys;
Ryan Hooperfe5d8b82016-12-15 14:53:58 -0500144 unsigned int num_master_keys;
jfigusde8deb32014-11-25 12:58:11 -0500145 srtp_rdbx_t rtp_rdbx;
jfigus857009c2014-11-05 11:17:43 -0500146 srtp_sec_serv_t rtp_services;
jfigusde8deb32014-11-25 12:58:11 -0500147 srtp_rdb_t rtcp_rdb;
jfigus857009c2014-11-05 11:17:43 -0500148 srtp_sec_serv_t rtcp_services;
David McGrew3c45e0c2006-07-12 00:50:56 +0000149 direction_t direction;
Jonathan Lennoxdcee5c62010-05-17 22:08:40 +0000150 int allow_repeat_tx;
jfigusc5887e72014-11-06 09:46:18 -0500151 srtp_ekt_stream_t ekt;
Joachim Bauch99a74822015-11-17 00:08:19 +0100152 int *enc_xtn_hdr;
153 int enc_xtn_hdr_count;
jfigusa9ac8982014-10-31 14:49:31 -0400154 struct srtp_stream_ctx_t_ *next; /* linked list of streams */
155} strp_stream_ctx_t_;
David McGrew3c45e0c2006-07-12 00:50:56 +0000156
157
158/*
159 * an srtp_ctx_t holds a stream list and a service description
160 */
161
jfigusa9ac8982014-10-31 14:49:31 -0400162typedef struct srtp_ctx_t_ {
163 struct srtp_stream_ctx_t_ *stream_list; /* linked list of streams */
164 struct srtp_stream_ctx_t_ *stream_template; /* act as template for other streams */
Iñaki Baz Castillo241fec32014-08-21 00:51:00 +0200165 void *user_data; /* user custom data */
jfigusa9ac8982014-10-31 14:49:31 -0400166} srtp_ctx_t_;
David McGrew3c45e0c2006-07-12 00:50:56 +0000167
168
169
170/*
171 * srtp_handle_event(srtp, srtm, evnt) calls the event handling
172 * function, if there is one.
173 *
174 * This macro is not included in the documentation as it is
175 * an internal-only function.
176 */
177
178#define srtp_handle_event(srtp, strm, evnt) \
179 if(srtp_event_handler) { \
180 srtp_event_data_t data; \
181 data.session = srtp; \
Pascal Bühlerf15c7e02017-01-25 13:15:21 +0100182 data.ssrc = ntohl(strm->ssrc); \
David McGrew3c45e0c2006-07-12 00:50:56 +0000183 data.event = evnt; \
184 srtp_event_handler(&data); \
185}
186
David Benjamin5f1b9822016-07-26 18:18:01 -0400187#ifdef __cplusplus
188}
189#endif
David McGrew3c45e0c2006-07-12 00:50:56 +0000190
191#endif /* SRTP_PRIV_H */