blob: 26d206b335e02f3762a02f8441859d7b77c08cad [file] [log] [blame]
Cullen Jennings235513a2005-09-21 22:51:36 +00001/*
2 * srtp_driver.c
jfigus67b9c732014-11-20 10:17:21 -05003 *
Cullen Jennings235513a2005-09-21 22:51:36 +00004 * a test driver for libSRTP
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9/*
jfigus67b9c732014-11-20 10:17:21 -050010 *
David McGrew7629bf22006-06-08 17:00:25 +000011 * Copyright (c) 2001-2006, Cisco Systems, Inc.
Cullen Jennings235513a2005-09-21 22:51:36 +000012 * All rights reserved.
jfigus67b9c732014-11-20 10:17:21 -050013 *
Cullen Jennings235513a2005-09-21 22:51:36 +000014 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
jfigus67b9c732014-11-20 10:17:21 -050017 *
Cullen Jennings235513a2005-09-21 22:51:36 +000018 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
jfigus67b9c732014-11-20 10:17:21 -050020 *
Cullen Jennings235513a2005-09-21 22:51:36 +000021 * 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.
jfigus67b9c732014-11-20 10:17:21 -050025 *
Cullen Jennings235513a2005-09-21 22:51:36 +000026 * 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.
jfigus67b9c732014-11-20 10:17:21 -050029 *
Cullen Jennings235513a2005-09-21 22:51:36 +000030 * 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
46#include <string.h> /* for memcpy() */
47#include <time.h> /* for clock() */
48#include <stdlib.h> /* for malloc(), free() */
49#include <stdio.h> /* for print(), fflush() */
David McGrew79bd3012006-07-17 20:41:21 +000050#include "getopt_s.h" /* for local getopt() */
jfigus46d6b472014-11-14 16:42:01 -050051#include "util.h"
Cullen Jennings235513a2005-09-21 22:51:36 +000052
David McGrew3c45e0c2006-07-12 00:50:56 +000053#include "srtp_priv.h"
Cullen Jennings235513a2005-09-21 22:51:36 +000054
Marcus Sundberg1cbca882005-10-02 20:50:06 +000055#ifdef HAVE_NETINET_IN_H
56# include <netinet/in.h>
57#elif defined HAVE_WINSOCK2_H
58# include <winsock2.h>
59#endif
60
Cullen Jennings235513a2005-09-21 22:51:36 +000061#define PRINT_REFERENCE_PACKET 1
62
jfigus857009c2014-11-05 11:17:43 -050063srtp_err_status_t
Marcus Sundberga3f95fe2005-09-29 12:48:41 +000064srtp_validate(void);
Cullen Jennings235513a2005-09-21 22:51:36 +000065
jfigus857009c2014-11-05 11:17:43 -050066srtp_err_status_t
Joachim Bauch99a74822015-11-17 00:08:19 +010067srtp_validate_encrypted_extensions_headers(void);
68
Joachim Bauch80a45b52015-12-06 22:57:58 +010069#ifdef OPENSSL
Joachim Bauch99a74822015-11-17 00:08:19 +010070srtp_err_status_t
Joachim Bauchfa1e8c22015-11-24 23:38:48 +010071srtp_validate_encrypted_extensions_headers_gcm(void);
Joachim Bauch80a45b52015-12-06 22:57:58 +010072#endif
Joachim Bauchfa1e8c22015-11-24 23:38:48 +010073
74srtp_err_status_t
Jonathan Lennox5df951a2010-05-20 20:55:54 +000075srtp_validate_aes_256(void);
76
jfigus857009c2014-11-05 11:17:43 -050077srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +000078srtp_create_big_policy(srtp_policy_t **list);
79
jfigus857009c2014-11-05 11:17:43 -050080srtp_err_status_t
Jonathan Lennox80c4c832010-05-17 19:30:28 +000081srtp_dealloc_big_policy(srtp_policy_t *list);
82
jfigus857009c2014-11-05 11:17:43 -050083srtp_err_status_t
Joachim Bauchc8a19ae2015-12-14 22:50:36 +010084srtp_test_empty_payload(void);
85
86#ifdef OPENSSL
87srtp_err_status_t
88srtp_test_empty_payload_gcm(void);
89#endif
90
91srtp_err_status_t
Marcus Sundberga3f95fe2005-09-29 12:48:41 +000092srtp_test_remove_stream(void);
Cullen Jennings235513a2005-09-21 22:51:36 +000093
Pascal Bühlerbd3112a2015-11-06 20:29:15 +010094srtp_err_status_t
95srtp_test_update(void);
96
Cullen Jennings235513a2005-09-21 22:51:36 +000097double
98srtp_bits_per_second(int msg_len_octets, const srtp_policy_t *policy);
99
100double
101srtp_rejections_per_second(int msg_len_octets, const srtp_policy_t *policy);
102
103void
104srtp_do_timing(const srtp_policy_t *policy);
105
106void
107srtp_do_rejection_timing(const srtp_policy_t *policy);
108
jfigus857009c2014-11-05 11:17:43 -0500109srtp_err_status_t
Joachim Bauch99a74822015-11-17 00:08:19 +0100110srtp_test(const srtp_policy_t *policy, int extension_header);
Cullen Jennings235513a2005-09-21 22:51:36 +0000111
jfigus857009c2014-11-05 11:17:43 -0500112srtp_err_status_t
David McGrew9c70f292006-05-03 19:38:38 +0000113srtcp_test(const srtp_policy_t *policy);
114
jfigus857009c2014-11-05 11:17:43 -0500115srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000116srtp_session_print_policy(srtp_t srtp);
117
jfigus857009c2014-11-05 11:17:43 -0500118srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -0500119srtp_print_policy(const srtp_policy_t *policy);
Cullen Jennings235513a2005-09-21 22:51:36 +0000120
121char *
122srtp_packet_to_string(srtp_hdr_t *hdr, int packet_len);
123
124double
125mips_estimate(int num_trials, int *ignore);
126
jfigus8c36da22013-10-01 16:41:19 -0400127extern uint8_t test_key[46];
Cullen Jennings235513a2005-09-21 22:51:36 +0000128
129void
jfigus67b9c732014-11-20 10:17:21 -0500130usage (char *prog_name)
131{
132 printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
133 " -t run timing test\n"
134 " -r run rejection timing test\n"
135 " -c run codec timing test\n"
136 " -v run validation tests\n"
137 " -d <mod> turn on debugging module <mod>\n"
138 " -l list debugging modules\n", prog_name);
139 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000140}
141
142/*
143 * The policy_array is a null-terminated array of policy structs. it
144 * is declared at the end of this file
145 */
146
147extern const srtp_policy_t *policy_array[];
148
149
150/* the wildcard_policy is declared below; it has a wildcard ssrc */
151
152extern const srtp_policy_t wildcard_policy;
153
154/*
155 * mod_driver debug module - debugging module for this test driver
156 *
jfigus67b9c732014-11-20 10:17:21 -0500157 * we use the crypto_kernel debugging system in this driver, which
Cullen Jennings235513a2005-09-21 22:51:36 +0000158 * makes the interface uniform and increases portability
jfigus67b9c732014-11-20 10:17:21 -0500159 */
Cullen Jennings235513a2005-09-21 22:51:36 +0000160
jfigus02d6f032014-11-21 10:56:42 -0500161srtp_debug_module_t mod_driver = {
jfigus67b9c732014-11-20 10:17:21 -0500162 0, /* debugging is off by default */
163 "driver" /* printable name for module */
Cullen Jennings235513a2005-09-21 22:51:36 +0000164};
165
166int
jfigus67b9c732014-11-20 10:17:21 -0500167main (int argc, char *argv[])
168{
169 int q;
170 unsigned do_timing_test = 0;
171 unsigned do_rejection_test = 0;
172 unsigned do_codec_timing = 0;
173 unsigned do_validation = 0;
174 unsigned do_list_mods = 0;
175 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000176
177 /*
jfigus67b9c732014-11-20 10:17:21 -0500178 * verify that the compiler has interpreted the header data
179 * structure srtp_hdr_t correctly
Cullen Jennings235513a2005-09-21 22:51:36 +0000180 */
jfigus67b9c732014-11-20 10:17:21 -0500181 if (sizeof(srtp_hdr_t) != 12) {
182 printf("error: srtp_hdr_t has incorrect size"
183 "(size is %ld bytes, expected 12)\n",
184 (long)sizeof(srtp_hdr_t));
185 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000186 }
187
jfigus67b9c732014-11-20 10:17:21 -0500188 /* initialize srtp library */
189 status = srtp_init();
190 if (status) {
191 printf("error: srtp init failed with error code %d\n", status);
192 exit(1);
193 }
194
195 /* load srtp_driver debug module */
jfigus92736bc2014-11-21 10:30:54 -0500196 status = srtp_crypto_kernel_load_debug_module(&mod_driver);
jfigus67b9c732014-11-20 10:17:21 -0500197 if (status) {
198 printf("error: load of srtp_driver debug module failed "
199 "with error code %d\n", status);
200 exit(1);
201 }
202
203 /* process input arguments */
204 while (1) {
205 q = getopt_s(argc, argv, "trcvld:");
206 if (q == -1) {
207 break;
208 }
209 switch (q) {
210 case 't':
211 do_timing_test = 1;
212 break;
213 case 'r':
214 do_rejection_test = 1;
215 break;
216 case 'c':
217 do_codec_timing = 1;
218 break;
219 case 'v':
220 do_validation = 1;
221 break;
222 case 'l':
223 do_list_mods = 1;
224 break;
225 case 'd':
jfigus92736bc2014-11-21 10:30:54 -0500226 status = srtp_crypto_kernel_set_debug_module(optarg_s, 1);
jfigus67b9c732014-11-20 10:17:21 -0500227 if (status) {
228 printf("error: set debug module (%s) failed\n", optarg_s);
229 exit(1);
230 }
231 break;
232 default:
233 usage(argv[0]);
234 }
235 }
236
237 if (!do_validation && !do_timing_test && !do_codec_timing
238 && !do_list_mods && !do_rejection_test) {
239 usage(argv[0]);
240 }
241
242 if (do_list_mods) {
jfigus92736bc2014-11-21 10:30:54 -0500243 status = srtp_crypto_kernel_list_debug_modules();
jfigus67b9c732014-11-20 10:17:21 -0500244 if (status) {
245 printf("error: list of debug modules failed\n");
246 exit(1);
247 }
248 }
249
250 if (do_validation) {
251 const srtp_policy_t **policy = policy_array;
252 srtp_policy_t *big_policy;
253
254 /* loop over policy array, testing srtp and srtcp for each policy */
255 while (*policy != NULL) {
256 printf("testing srtp_protect and srtp_unprotect\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100257 if (srtp_test(*policy, 0) == srtp_err_status_ok) {
258 printf("passed\n\n");
259 } else{
260 printf("failed\n");
261 exit(1);
262 }
263 printf("testing srtp_protect and srtp_unprotect with encrypted extensions headers\n");
264 if (srtp_test(*policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500265 printf("passed\n\n");
266 } else{
267 printf("failed\n");
268 exit(1);
269 }
270 printf("testing srtp_protect_rtcp and srtp_unprotect_rtcp\n");
271 if (srtcp_test(*policy) == srtp_err_status_ok) {
272 printf("passed\n\n");
273 } else{
274 printf("failed\n");
275 exit(1);
276 }
277 policy++;
278 }
279
280 /* create a big policy list and run tests on it */
281 status = srtp_create_big_policy(&big_policy);
282 if (status) {
283 printf("unexpected failure with error code %d\n", status);
284 exit(1);
285 }
286 printf("testing srtp_protect and srtp_unprotect with big policy\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100287 if (srtp_test(big_policy, 0) == srtp_err_status_ok) {
288 printf("passed\n\n");
289 } else{
290 printf("failed\n");
291 exit(1);
292 }
293 printf("testing srtp_protect and srtp_unprotect with big policy and encrypted extensions headers\n");
294 if (srtp_test(big_policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500295 printf("passed\n\n");
296 } else{
297 printf("failed\n");
298 exit(1);
299 }
300 status = srtp_dealloc_big_policy(big_policy);
301 if (status) {
302 printf("unexpected failure with error code %d\n", status);
303 exit(1);
304 }
305
306 /* run test on wildcard policy */
307 printf("testing srtp_protect and srtp_unprotect on "
308 "wildcard ssrc policy\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100309 if (srtp_test(&wildcard_policy, 0) == srtp_err_status_ok) {
310 printf("passed\n\n");
311 } else{
312 printf("failed\n");
313 exit(1);
314 }
315 printf("testing srtp_protect and srtp_unprotect on "
316 "wildcard ssrc policy and encrypted extensions headers\n");
317 if (srtp_test(&wildcard_policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500318 printf("passed\n\n");
319 } else{
320 printf("failed\n");
321 exit(1);
322 }
323
324 /*
325 * run validation test against the reference packets - note
326 * that this test only covers the default policy
327 */
328 printf("testing srtp_protect and srtp_unprotect against "
329 "reference packets\n");
330 if (srtp_validate() == srtp_err_status_ok) {
331 printf("passed\n\n");
332 } else{
333 printf("failed\n");
334 exit(1);
335 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100336 printf("testing srtp_protect and srtp_unprotect against "
337 "reference packets with encrypted extensions headers\n");
338 if (srtp_validate_encrypted_extensions_headers() == srtp_err_status_ok)
339 printf("passed\n\n");
340 else {
341 printf("failed\n");
342 exit(1);
343 }
jfigus67b9c732014-11-20 10:17:21 -0500344
Joachim Bauchb8cb5772015-11-24 21:46:25 +0100345#ifdef OPENSSL
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100346 printf("testing srtp_protect and srtp_unprotect against "
347 "reference packets with encrypted extension headers (GCM)\n");
348 if (srtp_validate_encrypted_extensions_headers_gcm() == srtp_err_status_ok) {
349 printf("passed\n\n");
350 } else{
351 printf("failed\n");
352 exit(1);
353 }
Joachim Bauch80a45b52015-12-06 22:57:58 +0100354#endif
Joachim Bauchfa1e8c22015-11-24 23:38:48 +0100355
jfigus67b9c732014-11-20 10:17:21 -0500356 /*
357 * run validation test against the reference packets for
358 * AES-256
359 */
360 printf("testing srtp_protect and srtp_unprotect against "
361 "reference packets (AES-256)\n");
362 if (srtp_validate_aes_256() == srtp_err_status_ok) {
363 printf("passed\n\n");
364 } else{
365 printf("failed\n");
366 exit(1);
367 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000368
jfigus67b9c732014-11-20 10:17:21 -0500369 /*
Joachim Bauchc8a19ae2015-12-14 22:50:36 +0100370 * test packets with empty payload
371 */
372 printf("testing srtp_protect and srtp_unprotect against "
373 "packets with empty payload\n");
374 if (srtp_test_empty_payload() == srtp_err_status_ok) {
375 printf("passed\n");
376 } else{
377 printf("failed\n");
378 exit(1);
379 }
380#ifdef OPENSSL
381 printf("testing srtp_protect and srtp_unprotect against "
382 "packets with empty payload (GCM)\n");
383 if (srtp_test_empty_payload_gcm() == srtp_err_status_ok) {
384 printf("passed\n");
385 } else{
386 printf("failed\n");
387 exit(1);
388 }
389#endif
390
391 /*
jfigus67b9c732014-11-20 10:17:21 -0500392 * test the function srtp_remove_stream()
393 */
394 printf("testing srtp_remove_stream()...");
395 if (srtp_test_remove_stream() == srtp_err_status_ok) {
396 printf("passed\n");
397 } else{
398 printf("failed\n");
399 exit(1);
400 }
Pascal Bühlerbd3112a2015-11-06 20:29:15 +0100401
402 /*
403 * test the function srtp_update()
404 */
405 printf("testing srtp_update()...");
406 if (srtp_test_update() == srtp_err_status_ok) {
407 printf("passed\n");
408 } else {
409 printf("failed\n");
410 exit(1);
411 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000412 }
jfigus67b9c732014-11-20 10:17:21 -0500413
414 if (do_timing_test) {
415 const srtp_policy_t **policy = policy_array;
416
417 /* loop over policies, run timing test for each */
418 while (*policy != NULL) {
419 srtp_print_policy(*policy);
420 srtp_do_timing(*policy);
421 policy++;
422 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000423 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000424
jfigus67b9c732014-11-20 10:17:21 -0500425 if (do_rejection_test) {
426 const srtp_policy_t **policy = policy_array;
427
428 /* loop over policies, run rejection timing test for each */
429 while (*policy != NULL) {
430 srtp_print_policy(*policy);
431 srtp_do_rejection_timing(*policy);
432 policy++;
433 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000434 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000435
jfigus67b9c732014-11-20 10:17:21 -0500436 if (do_codec_timing) {
437 srtp_policy_t policy;
438 int ignore;
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100439 double mips_value = mips_estimate(1000000000, &ignore);
Cullen Jennings235513a2005-09-21 22:51:36 +0000440
Joachim Bauch99a74822015-11-17 00:08:19 +0100441 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -0500442 srtp_crypto_policy_set_rtp_default(&policy.rtp);
443 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
444 policy.ssrc.type = ssrc_specific;
445 policy.ssrc.value = 0xdecafbad;
446 policy.key = test_key;
447 policy.ekt = NULL;
448 policy.window_size = 128;
449 policy.allow_repeat_tx = 0;
450 policy.next = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000451
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100452 printf("mips estimate: %e\n", mips_value);
Cullen Jennings235513a2005-09-21 22:51:36 +0000453
jfigus67b9c732014-11-20 10:17:21 -0500454 printf("testing srtp processing time for voice codecs:\n");
455 printf("codec\t\tlength (octets)\t\tsrtp instructions/second\n");
456 printf("G.711\t\t%d\t\t\t%e\n", 80,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100457 (double)mips_value * (80 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500458 srtp_bits_per_second(80, &policy) / .01 );
459 printf("G.711\t\t%d\t\t\t%e\n", 160,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100460 (double)mips_value * (160 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500461 srtp_bits_per_second(160, &policy) / .02);
462 printf("G.726-32\t%d\t\t\t%e\n", 40,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100463 (double)mips_value * (40 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500464 srtp_bits_per_second(40, &policy) / .01 );
465 printf("G.726-32\t%d\t\t\t%e\n", 80,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100466 (double)mips_value * (80 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500467 srtp_bits_per_second(80, &policy) / .02);
468 printf("G.729\t\t%d\t\t\t%e\n", 10,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100469 (double)mips_value * (10 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500470 srtp_bits_per_second(10, &policy) / .01 );
471 printf("G.729\t\t%d\t\t\t%e\n", 20,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100472 (double)mips_value * (20 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500473 srtp_bits_per_second(20, &policy) / .02 );
474 printf("Wideband\t%d\t\t\t%e\n", 320,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100475 (double)mips_value * (320 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500476 srtp_bits_per_second(320, &policy) / .01 );
477 printf("Wideband\t%d\t\t\t%e\n", 640,
Joachim Bauchdc53dcb2016-03-10 20:32:44 +0100478 (double)mips_value * (640 * 8) /
jfigus67b9c732014-11-20 10:17:21 -0500479 srtp_bits_per_second(640, &policy) / .02 );
480 }
Jonathan Lennoxdfb30842010-05-15 04:52:01 +0000481
jfigus67b9c732014-11-20 10:17:21 -0500482 status = srtp_shutdown();
483 if (status) {
484 printf("error: srtp shutdown failed with error code %d\n", status);
485 exit(1);
486 }
487
488 return 0;
Cullen Jennings235513a2005-09-21 22:51:36 +0000489}
490
491
492
493/*
494 * srtp_create_test_packet(len, ssrc) returns a pointer to a
495 * (malloced) example RTP packet whose data field has the length given
496 * by pkt_octet_len and the SSRC value ssrc. The total length of the
497 * packet is twelve octets longer, since the header is at the
498 * beginning. There is room at the end of the packet for a trailer,
499 * and the four octets following the packet are filled with 0xff
500 * values to enable testing for overwrites.
501 *
502 * note that the location of the test packet can (and should) be
503 * deallocated with the free() call once it is no longer needed.
504 */
505
506srtp_hdr_t *
jfigus67b9c732014-11-20 10:17:21 -0500507srtp_create_test_packet (int pkt_octet_len, uint32_t ssrc)
508{
509 int i;
510 uint8_t *buffer;
511 srtp_hdr_t *hdr;
512 int bytes_in_hdr = 12;
Cullen Jennings235513a2005-09-21 22:51:36 +0000513
jfigus67b9c732014-11-20 10:17:21 -0500514 /* allocate memory for test packet */
515 hdr = (srtp_hdr_t*)malloc(pkt_octet_len + bytes_in_hdr
516 + SRTP_MAX_TRAILER_LEN + 4);
517 if (!hdr) {
518 return NULL;
519 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000520
jfigus67b9c732014-11-20 10:17:21 -0500521 hdr->version = 2; /* RTP version two */
522 hdr->p = 0; /* no padding needed */
523 hdr->x = 0; /* no header extension */
524 hdr->cc = 0; /* no CSRCs */
525 hdr->m = 0; /* marker bit */
526 hdr->pt = 0xf; /* payload type */
527 hdr->seq = htons(0x1234); /* sequence number */
528 hdr->ts = htonl(0xdecafbad); /* timestamp */
529 hdr->ssrc = htonl(ssrc); /* synch. source */
Cullen Jennings235513a2005-09-21 22:51:36 +0000530
jfigus67b9c732014-11-20 10:17:21 -0500531 buffer = (uint8_t*)hdr;
532 buffer += bytes_in_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +0000533
jfigus67b9c732014-11-20 10:17:21 -0500534 /* set RTP data to 0xab */
535 for (i = 0; i < pkt_octet_len; i++) {
536 *buffer++ = 0xab;
537 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000538
jfigus67b9c732014-11-20 10:17:21 -0500539 /* set post-data value to 0xffff to enable overrun checking */
540 for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++) {
541 *buffer++ = 0xff;
542 }
543
544 return hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +0000545}
546
Joachim Bauch99a74822015-11-17 00:08:19 +0100547srtp_hdr_t *
548srtp_create_test_packet_ext_hdr(int pkt_octet_len, uint32_t ssrc) {
549 int i;
550 uint8_t *buffer;
551 srtp_hdr_t *hdr;
552 int bytes_in_hdr = 12;
553 uint8_t extension_header[12] = {
554 /* one-byte header */
555 0xbe, 0xde,
556 /* size */
557 0x00, 0x02,
558 /* id 1, length 1 (i.e. 2 bytes) */
559 0x11,
560 /* payload */
561 0xca,
562 0xfe,
563 /* padding */
564 0x00,
565 /* id 2, length 0 (i.e. 1 byte) */
566 0x20,
567 /* payload */
568 0xba,
569 /* padding */
570 0x00,
571 0x00
572 };
573
574 /* allocate memory for test packet */
575 hdr = (srtp_hdr_t*) malloc(pkt_octet_len + bytes_in_hdr
576 + sizeof(extension_header) + SRTP_MAX_TRAILER_LEN + 4);
577 if (!hdr)
578 return NULL;
579
580 hdr->version = 2; /* RTP version two */
581 hdr->p = 0; /* no padding needed */
582 hdr->x = 1; /* no header extension */
583 hdr->cc = 0; /* no CSRCs */
584 hdr->m = 0; /* marker bit */
585 hdr->pt = 0xf; /* payload type */
586 hdr->seq = htons(0x1234); /* sequence number */
587 hdr->ts = htonl(0xdecafbad); /* timestamp */
588 hdr->ssrc = htonl(ssrc); /* synch. source */
589
590 buffer = (uint8_t *)hdr;
591 buffer += bytes_in_hdr;
592
593 memcpy(buffer, extension_header, sizeof(extension_header));
594 buffer += sizeof(extension_header);
595
596 /* set RTP data to 0xab */
597 for (i=0; i < pkt_octet_len; i++)
598 *buffer++ = 0xab;
599
600 /* set post-data value to 0xffff to enable overrun checking */
601 for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++)
602 *buffer++ = 0xff;
603
604 return hdr;
605}
606
Cullen Jennings235513a2005-09-21 22:51:36 +0000607void
jfigus67b9c732014-11-20 10:17:21 -0500608srtp_do_timing (const srtp_policy_t *policy)
609{
610 int len;
Cullen Jennings235513a2005-09-21 22:51:36 +0000611
jfigus67b9c732014-11-20 10:17:21 -0500612 /*
613 * note: the output of this function is formatted so that it
614 * can be used in gnuplot. '#' indicates a comment, and "\r\n"
615 * terminates a record
616 */
617
618 printf("# testing srtp throughput:\r\n");
619 printf("# mesg length (octets)\tthroughput (megabits per second)\r\n");
620
621 for (len = 16; len <= 2048; len *= 2) {
622 printf("%d\t\t\t%f\r\n", len,
623 srtp_bits_per_second(len, policy) / 1.0E6);
624 }
625
626 /* these extra linefeeds let gnuplot know that a dataset is done */
627 printf("\r\n\r\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000628
629}
630
631void
jfigus67b9c732014-11-20 10:17:21 -0500632srtp_do_rejection_timing (const srtp_policy_t *policy)
633{
634 int len;
Cullen Jennings235513a2005-09-21 22:51:36 +0000635
jfigus67b9c732014-11-20 10:17:21 -0500636 /*
637 * note: the output of this function is formatted so that it
638 * can be used in gnuplot. '#' indicates a comment, and "\r\n"
639 * terminates a record
640 */
641
642 printf("# testing srtp rejection throughput:\r\n");
643 printf("# mesg length (octets)\trejections per second\r\n");
644
645 for (len = 8; len <= 2048; len *= 2) {
646 printf("%d\t\t\t%e\r\n", len, srtp_rejections_per_second(len, policy));
647 }
648
649 /* these extra linefeeds let gnuplot know that a dataset is done */
650 printf("\r\n\r\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000651
652}
653
654
655#define MAX_MSG_LEN 1024
656
657double
jfigus67b9c732014-11-20 10:17:21 -0500658srtp_bits_per_second (int msg_len_octets, const srtp_policy_t *policy)
659{
660 srtp_t srtp;
661 srtp_hdr_t *mesg;
662 int i;
663 clock_t timer;
664 int num_trials = 100000;
665 int len;
666 uint32_t ssrc;
667 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000668
jfigus67b9c732014-11-20 10:17:21 -0500669 /*
670 * allocate and initialize an srtp session
671 */
672 status = srtp_create(&srtp, policy);
Cullen Jennings235513a2005-09-21 22:51:36 +0000673 if (status) {
jfigus67b9c732014-11-20 10:17:21 -0500674 printf("error: srtp_create() failed with error code %d\n", status);
675 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000676 }
677
jfigus67b9c732014-11-20 10:17:21 -0500678 /*
679 * if the ssrc is unspecified, use a predetermined one
680 */
681 if (policy->ssrc.type != ssrc_specific) {
682 ssrc = 0xdeadbeef;
683 } else {
684 ssrc = policy->ssrc.value;
Jonathan Lennox75b36872010-05-21 00:30:21 +0000685 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000686
jfigus67b9c732014-11-20 10:17:21 -0500687 /*
688 * create a test packet
689 */
690 mesg = srtp_create_test_packet(msg_len_octets, ssrc);
691 if (mesg == NULL) {
692 return 0.0; /* indicate failure by returning zero */
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000693
jfigus67b9c732014-11-20 10:17:21 -0500694 }
695 timer = clock();
696 for (i = 0; i < num_trials; i++) {
697 len = msg_len_octets + 12; /* add in rtp header length */
698
699 /* srtp protect message */
700 status = srtp_protect(srtp, mesg, &len);
701 if (status) {
702 printf("error: srtp_protect() failed with error code %d\n", status);
703 exit(1);
704 }
705
706 /* increment message number */
707 {
708 /* hack sequence to avoid problems with macros for htons/ntohs on some systems */
709 short new_seq = ntohs(mesg->seq) + 1;
710 mesg->seq = htons(new_seq);
711 }
712 }
713 timer = clock() - timer;
714
715 free(mesg);
716
717 status = srtp_dealloc(srtp);
718 if (status) {
719 printf("error: srtp_dealloc() failed with error code %d\n", status);
720 exit(1);
721 }
722
723 return (double)(msg_len_octets) * 8 *
724 num_trials * CLOCKS_PER_SEC / timer;
Cullen Jennings235513a2005-09-21 22:51:36 +0000725}
726
727double
jfigus67b9c732014-11-20 10:17:21 -0500728srtp_rejections_per_second (int msg_len_octets, const srtp_policy_t *policy)
729{
730 srtp_ctx_t *srtp;
731 srtp_hdr_t *mesg;
732 int i;
733 int len;
734 clock_t timer;
735 int num_trials = 1000000;
736 uint32_t ssrc = policy->ssrc.value;
737 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000738
jfigus67b9c732014-11-20 10:17:21 -0500739 /*
740 * allocate and initialize an srtp session
741 */
742 status = srtp_create(&srtp, policy);
743 if (status) {
744 printf("error: srtp_create() failed with error code %d\n", status);
745 exit(1);
746 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000747
jfigus67b9c732014-11-20 10:17:21 -0500748 mesg = srtp_create_test_packet(msg_len_octets, ssrc);
749 if (mesg == NULL) {
750 return 0.0; /* indicate failure by returning zero */
751
752 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000753 len = msg_len_octets;
jfigus67b9c732014-11-20 10:17:21 -0500754 srtp_protect(srtp, (srtp_hdr_t*)mesg, &len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000755
jfigus67b9c732014-11-20 10:17:21 -0500756 timer = clock();
757 for (i = 0; i < num_trials; i++) {
758 len = msg_len_octets;
759 srtp_unprotect(srtp, (srtp_hdr_t*)mesg, &len);
760 }
761 timer = clock() - timer;
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000762
jfigus67b9c732014-11-20 10:17:21 -0500763 free(mesg);
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000764
jfigus67b9c732014-11-20 10:17:21 -0500765 status = srtp_dealloc(srtp);
766 if (status) {
767 printf("error: srtp_dealloc() failed with error code %d\n", status);
768 exit(1);
769 }
770
771 return (double)num_trials * CLOCKS_PER_SEC / timer;
Cullen Jennings235513a2005-09-21 22:51:36 +0000772}
773
774
775void
jfigus67b9c732014-11-20 10:17:21 -0500776err_check (srtp_err_status_t s)
777{
778 if (s == srtp_err_status_ok) {
779 return;
780 } else{
781 fprintf(stderr, "error: unexpected srtp failure (code %d)\n", s);
782 }
783 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000784}
785
jfigus857009c2014-11-05 11:17:43 -0500786srtp_err_status_t
Joachim Bauch99a74822015-11-17 00:08:19 +0100787srtp_test (const srtp_policy_t *policy, int extension_header)
jfigus67b9c732014-11-20 10:17:21 -0500788{
789 int i;
790 srtp_t srtp_sender;
791 srtp_t srtp_rcvr;
792 srtp_err_status_t status = srtp_err_status_ok;
793 srtp_hdr_t *hdr, *hdr2;
794 uint8_t hdr_enc[64];
795 uint8_t *pkt_end;
796 int msg_len_octets, msg_len_enc;
797 int len;
798 int tag_length = policy->rtp.auth_tag_len;
799 uint32_t ssrc;
800 srtp_policy_t *rcvr_policy;
Joachim Bauch99a74822015-11-17 00:08:19 +0100801 srtp_policy_t tmp_policy;
802 int header = 1;
Cullen Jennings235513a2005-09-21 22:51:36 +0000803
Joachim Bauch99a74822015-11-17 00:08:19 +0100804 if (extension_header) {
805 memcpy(&tmp_policy, policy, sizeof(srtp_policy_t));
806 tmp_policy.enc_xtn_hdr = &header;
807 tmp_policy.enc_xtn_hdr_count = 1;
808 err_check(srtp_create(&srtp_sender, &tmp_policy));
809 } else {
810 err_check(srtp_create(&srtp_sender, policy));
811 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000812
jfigus67b9c732014-11-20 10:17:21 -0500813 /* print out policy */
814 err_check(srtp_session_print_policy(srtp_sender));
Cullen Jennings235513a2005-09-21 22:51:36 +0000815
jfigus67b9c732014-11-20 10:17:21 -0500816 /*
817 * initialize data buffer, using the ssrc in the policy unless that
818 * value is a wildcard, in which case we'll just use an arbitrary
819 * one
820 */
821 if (policy->ssrc.type != ssrc_specific) {
822 ssrc = 0xdecafbad;
823 } else{
824 ssrc = policy->ssrc.value;
Cullen Jennings235513a2005-09-21 22:51:36 +0000825 }
jfigus67b9c732014-11-20 10:17:21 -0500826 msg_len_octets = 28;
Joachim Bauch99a74822015-11-17 00:08:19 +0100827 if (extension_header) {
828 hdr = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc);
829 hdr2 = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc);
830 } else {
831 hdr = srtp_create_test_packet(msg_len_octets, ssrc);
832 hdr2 = srtp_create_test_packet(msg_len_octets, ssrc);
833 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000834
jfigus67b9c732014-11-20 10:17:21 -0500835 if (hdr == NULL) {
Joachim Bauch99a74822015-11-17 00:08:19 +0100836 free(hdr2);
jfigus67b9c732014-11-20 10:17:21 -0500837 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000838 }
jfigus67b9c732014-11-20 10:17:21 -0500839 if (hdr2 == NULL) {
840 free(hdr);
841 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000842 }
843
Marcus Sundberg5c40da82005-10-08 18:15:00 +0000844 /* set message length */
845 len = msg_len_octets;
Joachim Bauch99a74822015-11-17 00:08:19 +0100846 if (extension_header) {
847 len += 12;
848 }
Marcus Sundberg5c40da82005-10-08 18:15:00 +0000849
jfigus67b9c732014-11-20 10:17:21 -0500850 debug_print(mod_driver, "before protection:\n%s",
851 srtp_packet_to_string(hdr, len));
852
853#if PRINT_REFERENCE_PACKET
854 debug_print(mod_driver, "reference packet before protection:\n%s",
855 octet_string_hex_string((uint8_t*)hdr, len));
856#endif
Cullen Jennings235513a2005-09-21 22:51:36 +0000857 err_check(srtp_protect(srtp_sender, hdr, &len));
Cullen Jennings235513a2005-09-21 22:51:36 +0000858
jfigus67b9c732014-11-20 10:17:21 -0500859 debug_print(mod_driver, "after protection:\n%s",
860 srtp_packet_to_string(hdr, len));
861#if PRINT_REFERENCE_PACKET
862 debug_print(mod_driver, "after protection:\n%s",
863 octet_string_hex_string((uint8_t*)hdr, len));
864#endif
865
866 /* save protected message and length */
867 memcpy(hdr_enc, hdr, len);
868 msg_len_enc = len;
869
870 /*
871 * check for overrun of the srtp_protect() function
872 *
873 * The packet is followed by a value of 0xfffff; if the value of the
874 * data following the packet is different, then we know that the
875 * protect function is overwriting the end of the packet.
876 */
877 pkt_end = (uint8_t*)hdr + sizeof(srtp_hdr_t)
878 + msg_len_octets + tag_length;
Joachim Bauch99a74822015-11-17 00:08:19 +0100879 if (extension_header) {
880 pkt_end += 12;
881 }
jfigus67b9c732014-11-20 10:17:21 -0500882 for (i = 0; i < 4; i++) {
883 if (pkt_end[i] != 0xff) {
884 fprintf(stdout, "overwrite in srtp_protect() function "
885 "(expected %x, found %x in trailing octet %d)\n",
886 0xff, ((uint8_t*)hdr)[i], i);
887 free(hdr);
888 free(hdr2);
889 return srtp_err_status_algo_fail;
890 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000891 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000892
jfigus67b9c732014-11-20 10:17:21 -0500893 /*
894 * if the policy includes confidentiality, check that ciphertext is
895 * different than plaintext
896 *
897 * Note that this check will give false negatives, with some small
898 * probability, especially if the packets are short. For that
899 * reason, we skip this check if the plaintext is less than four
900 * octets long.
901 */
902 if ((policy->rtp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
903 printf("testing that ciphertext is distinct from plaintext...");
904 status = srtp_err_status_algo_fail;
905 for (i = 12; i < msg_len_octets + 12; i++) {
906 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
907 status = srtp_err_status_ok;
908 }
909 }
910 if (status) {
911 printf("failed\n");
912 free(hdr);
913 free(hdr2);
914 return status;
915 }
916 printf("passed\n");
917 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000918
jfigus67b9c732014-11-20 10:17:21 -0500919 /*
920 * if the policy uses a 'wildcard' ssrc, then we need to make a copy
921 * of the policy that changes the direction to inbound
922 *
923 * we always copy the policy into the rcvr_policy, since otherwise
924 * the compiler would fret about the constness of the policy
925 */
926 rcvr_policy = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
927 if (rcvr_policy == NULL) {
928 free(hdr);
929 free(hdr2);
930 return srtp_err_status_alloc_fail;
931 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100932 if (extension_header) {
933 memcpy(rcvr_policy, &tmp_policy, sizeof(srtp_policy_t));
934 if (tmp_policy.ssrc.type == ssrc_any_outbound) {
935 rcvr_policy->ssrc.type = ssrc_any_inbound;
936 }
937 } else {
938 memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
939 if (policy->ssrc.type == ssrc_any_outbound) {
940 rcvr_policy->ssrc.type = ssrc_any_inbound;
941 }
jfigus67b9c732014-11-20 10:17:21 -0500942 }
943
944 err_check(srtp_create(&srtp_rcvr, rcvr_policy));
945
946 err_check(srtp_unprotect(srtp_rcvr, hdr, &len));
947
948 debug_print(mod_driver, "after unprotection:\n%s",
949 srtp_packet_to_string(hdr, len));
950
951 /* verify that the unprotected packet matches the origial one */
952 for (i = 0; i < msg_len_octets; i++) {
953 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
954 fprintf(stdout, "mismatch at octet %d\n", i);
955 status = srtp_err_status_algo_fail;
956 }
957 }
958 if (status) {
959 free(hdr);
960 free(hdr2);
961 free(rcvr_policy);
962 return status;
963 }
964
965 /*
966 * if the policy includes authentication, then test for false positives
967 */
968 if (policy->rtp.sec_serv & sec_serv_auth) {
969 char *data = ((char*)hdr) + 12;
970
971 printf("testing for false positives in replay check...");
972
973 /* set message length */
974 len = msg_len_enc;
975
976 /* unprotect a second time - should fail with a replay error */
977 status = srtp_unprotect(srtp_rcvr, hdr_enc, &len);
978 if (status != srtp_err_status_replay_fail) {
979 printf("failed with error code %d\n", status);
980 free(hdr);
981 free(hdr2);
982 free(rcvr_policy);
983 return status;
984 } else {
985 printf("passed\n");
986 }
987
988 printf("testing for false positives in auth check...");
989
990 /* increment sequence number in header */
991 hdr->seq++;
992
993 /* set message length */
994 len = msg_len_octets;
Joachim Bauch99a74822015-11-17 00:08:19 +0100995 if (extension_header) {
996 len += 12;
997 }
jfigus67b9c732014-11-20 10:17:21 -0500998
999 /* apply protection */
1000 err_check(srtp_protect(srtp_sender, hdr, &len));
1001
1002 /* flip bits in packet */
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001003 data[extension_header ? 12 : 0] ^= 0xff;
jfigus67b9c732014-11-20 10:17:21 -05001004
1005 /* unprotect, and check for authentication failure */
1006 status = srtp_unprotect(srtp_rcvr, hdr, &len);
1007 if (status != srtp_err_status_auth_fail) {
1008 printf("failed\n");
1009 free(hdr);
1010 free(hdr2);
1011 free(rcvr_policy);
1012 return status;
1013 } else {
1014 printf("passed\n");
1015 }
1016
1017 }
1018
1019 err_check(srtp_dealloc(srtp_sender));
1020 err_check(srtp_dealloc(srtp_rcvr));
1021
1022 free(hdr);
1023 free(hdr2);
1024 free(rcvr_policy);
1025 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001026}
1027
1028
jfigus857009c2014-11-05 11:17:43 -05001029srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001030srtcp_test (const srtp_policy_t *policy)
1031{
1032 int i;
1033 srtp_t srtcp_sender;
1034 srtp_t srtcp_rcvr;
1035 srtp_err_status_t status = srtp_err_status_ok;
1036 srtp_hdr_t *hdr, *hdr2;
1037 uint8_t hdr_enc[64];
1038 uint8_t *pkt_end;
1039 int msg_len_octets, msg_len_enc;
1040 int len;
1041 int tag_length = policy->rtp.auth_tag_len;
1042 uint32_t ssrc;
1043 srtp_policy_t *rcvr_policy;
David McGrew9c70f292006-05-03 19:38:38 +00001044
jfigus67b9c732014-11-20 10:17:21 -05001045 err_check(srtp_create(&srtcp_sender, policy));
David McGrew9c70f292006-05-03 19:38:38 +00001046
jfigus67b9c732014-11-20 10:17:21 -05001047 /* print out policy */
1048 err_check(srtp_session_print_policy(srtcp_sender));
David McGrew9c70f292006-05-03 19:38:38 +00001049
jfigus67b9c732014-11-20 10:17:21 -05001050 /*
1051 * initialize data buffer, using the ssrc in the policy unless that
1052 * value is a wildcard, in which case we'll just use an arbitrary
1053 * one
1054 */
1055 if (policy->ssrc.type != ssrc_specific) {
1056 ssrc = 0xdecafbad;
1057 } else{
1058 ssrc = policy->ssrc.value;
David McGrew9c70f292006-05-03 19:38:38 +00001059 }
jfigus67b9c732014-11-20 10:17:21 -05001060 msg_len_octets = 28;
1061 hdr = srtp_create_test_packet(msg_len_octets, ssrc);
David McGrew9c70f292006-05-03 19:38:38 +00001062
jfigus67b9c732014-11-20 10:17:21 -05001063 if (hdr == NULL) {
1064 return srtp_err_status_alloc_fail;
David McGrew9c70f292006-05-03 19:38:38 +00001065 }
jfigus67b9c732014-11-20 10:17:21 -05001066 hdr2 = srtp_create_test_packet(msg_len_octets, ssrc);
1067 if (hdr2 == NULL) {
1068 free(hdr);
1069 return srtp_err_status_alloc_fail;
David McGrew9c70f292006-05-03 19:38:38 +00001070 }
1071
David McGrew9c70f292006-05-03 19:38:38 +00001072 /* set message length */
1073 len = msg_len_octets;
1074
jfigus67b9c732014-11-20 10:17:21 -05001075 debug_print(mod_driver, "before protection:\n%s",
1076 srtp_packet_to_string(hdr, len));
1077
1078#if PRINT_REFERENCE_PACKET
1079 debug_print(mod_driver, "reference packet before protection:\n%s",
1080 octet_string_hex_string((uint8_t*)hdr, len));
1081#endif
David McGrew9c70f292006-05-03 19:38:38 +00001082 err_check(srtp_protect_rtcp(srtcp_sender, hdr, &len));
David McGrew9c70f292006-05-03 19:38:38 +00001083
jfigus67b9c732014-11-20 10:17:21 -05001084 debug_print(mod_driver, "after protection:\n%s",
1085 srtp_packet_to_string(hdr, len));
1086#if PRINT_REFERENCE_PACKET
1087 debug_print(mod_driver, "after protection:\n%s",
1088 octet_string_hex_string((uint8_t*)hdr, len));
1089#endif
1090
1091 /* save protected message and length */
1092 memcpy(hdr_enc, hdr, len);
1093 msg_len_enc = len;
1094
1095 /*
1096 * check for overrun of the srtp_protect() function
1097 *
1098 * The packet is followed by a value of 0xfffff; if the value of the
1099 * data following the packet is different, then we know that the
1100 * protect function is overwriting the end of the packet.
1101 */
1102 pkt_end = (uint8_t*)hdr + sizeof(srtp_hdr_t)
1103 + msg_len_octets + tag_length;
1104 for (i = 0; i < 4; i++) {
1105 if (pkt_end[i] != 0xff) {
1106 fprintf(stdout, "overwrite in srtp_protect_rtcp() function "
1107 "(expected %x, found %x in trailing octet %d)\n",
1108 0xff, ((uint8_t*)hdr)[i], i);
1109 free(hdr);
1110 free(hdr2);
1111 return srtp_err_status_algo_fail;
1112 }
David McGrew9c70f292006-05-03 19:38:38 +00001113 }
David McGrew9c70f292006-05-03 19:38:38 +00001114
jfigus67b9c732014-11-20 10:17:21 -05001115 /*
1116 * if the policy includes confidentiality, check that ciphertext is
1117 * different than plaintext
1118 *
1119 * Note that this check will give false negatives, with some small
1120 * probability, especially if the packets are short. For that
1121 * reason, we skip this check if the plaintext is less than four
1122 * octets long.
1123 */
1124 if ((policy->rtp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
1125 printf("testing that ciphertext is distinct from plaintext...");
1126 status = srtp_err_status_algo_fail;
1127 for (i = 12; i < msg_len_octets + 12; i++) {
1128 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
1129 status = srtp_err_status_ok;
1130 }
1131 }
1132 if (status) {
1133 printf("failed\n");
1134 free(hdr);
1135 free(hdr2);
1136 return status;
1137 }
1138 printf("passed\n");
1139 }
David McGrew9c70f292006-05-03 19:38:38 +00001140
jfigus67b9c732014-11-20 10:17:21 -05001141 /*
1142 * if the policy uses a 'wildcard' ssrc, then we need to make a copy
1143 * of the policy that changes the direction to inbound
1144 *
1145 * we always copy the policy into the rcvr_policy, since otherwise
1146 * the compiler would fret about the constness of the policy
1147 */
1148 rcvr_policy = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
1149 if (rcvr_policy == NULL) {
1150 return srtp_err_status_alloc_fail;
1151 }
1152 memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
1153 if (policy->ssrc.type == ssrc_any_outbound) {
1154 rcvr_policy->ssrc.type = ssrc_any_inbound;
1155 }
1156
1157 err_check(srtp_create(&srtcp_rcvr, rcvr_policy));
1158
1159 err_check(srtp_unprotect_rtcp(srtcp_rcvr, hdr, &len));
1160
1161 debug_print(mod_driver, "after unprotection:\n%s",
1162 srtp_packet_to_string(hdr, len));
1163
1164 /* verify that the unprotected packet matches the origial one */
1165 for (i = 0; i < msg_len_octets; i++) {
1166 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
1167 fprintf(stdout, "mismatch at octet %d\n", i);
1168 status = srtp_err_status_algo_fail;
1169 }
1170 }
1171 if (status) {
1172 free(hdr);
1173 free(hdr2);
1174 free(rcvr_policy);
1175 return status;
1176 }
1177
1178 /*
1179 * if the policy includes authentication, then test for false positives
1180 */
1181 if (policy->rtp.sec_serv & sec_serv_auth) {
1182 char *data = ((char*)hdr) + 12;
1183
1184 printf("testing for false positives in replay check...");
1185
1186 /* set message length */
1187 len = msg_len_enc;
1188
1189 /* unprotect a second time - should fail with a replay error */
1190 status = srtp_unprotect_rtcp(srtcp_rcvr, hdr_enc, &len);
1191 if (status != srtp_err_status_replay_fail) {
1192 printf("failed with error code %d\n", status);
1193 free(hdr);
1194 free(hdr2);
1195 free(rcvr_policy);
1196 return status;
1197 } else {
1198 printf("passed\n");
1199 }
1200
1201 printf("testing for false positives in auth check...");
1202
1203 /* increment sequence number in header */
1204 hdr->seq++;
1205
1206 /* set message length */
1207 len = msg_len_octets;
1208
1209 /* apply protection */
1210 err_check(srtp_protect_rtcp(srtcp_sender, hdr, &len));
1211
1212 /* flip bits in packet */
1213 data[0] ^= 0xff;
1214
1215 /* unprotect, and check for authentication failure */
1216 status = srtp_unprotect_rtcp(srtcp_rcvr, hdr, &len);
1217 if (status != srtp_err_status_auth_fail) {
1218 printf("failed\n");
1219 free(hdr);
1220 free(hdr2);
1221 free(rcvr_policy);
1222 return status;
1223 } else {
1224 printf("passed\n");
1225 }
1226
1227 }
1228
1229 err_check(srtp_dealloc(srtcp_sender));
1230 err_check(srtp_dealloc(srtcp_rcvr));
1231
1232 free(hdr);
1233 free(hdr2);
1234 free(rcvr_policy);
1235 return srtp_err_status_ok;
David McGrew9c70f292006-05-03 19:38:38 +00001236}
1237
1238
jfigus857009c2014-11-05 11:17:43 -05001239srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001240srtp_session_print_policy (srtp_t srtp)
1241{
1242 char *serv_descr[4] = {
1243 "none",
1244 "confidentiality",
1245 "authentication",
1246 "confidentiality and authentication"
1247 };
1248 char *direction[3] = {
1249 "unknown",
1250 "outbound",
1251 "inbound"
1252 };
1253 srtp_stream_t stream;
Cullen Jennings235513a2005-09-21 22:51:36 +00001254
jfigus67b9c732014-11-20 10:17:21 -05001255 /* sanity checking */
1256 if (srtp == NULL) {
1257 return srtp_err_status_fail;
1258 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001259
jfigus67b9c732014-11-20 10:17:21 -05001260 /* if there's a template stream, print it out */
1261 if (srtp->stream_template != NULL) {
1262 stream = srtp->stream_template;
1263 printf("# SSRC: any %s\r\n"
1264 "# rtp cipher: %s\r\n"
1265 "# rtp auth: %s\r\n"
1266 "# rtp services: %s\r\n"
1267 "# rtcp cipher: %s\r\n"
1268 "# rtcp auth: %s\r\n"
1269 "# rtcp services: %s\r\n"
1270 "# window size: %lu\r\n"
1271 "# tx rtx allowed:%s\r\n",
1272 direction[stream->direction],
1273 stream->rtp_cipher->type->description,
1274 stream->rtp_auth->type->description,
1275 serv_descr[stream->rtp_services],
1276 stream->rtcp_cipher->type->description,
1277 stream->rtcp_auth->type->description,
1278 serv_descr[stream->rtcp_services],
jfigusde8deb32014-11-25 12:58:11 -05001279 srtp_rdbx_get_window_size(&stream->rtp_rdbx),
jfigus67b9c732014-11-20 10:17:21 -05001280 stream->allow_repeat_tx ? "true" : "false");
Joachim Bauch99a74822015-11-17 00:08:19 +01001281
1282 printf("# Encrypted extension headers: ");
1283 if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
1284 int* enc_xtn_hdr = stream->enc_xtn_hdr;
1285 int count = stream->enc_xtn_hdr_count;
1286 while (count > 0) {
1287 printf("%d ", *enc_xtn_hdr);
1288 enc_xtn_hdr++;
1289 count--;
1290 }
1291 printf("\n");
1292 } else {
1293 printf("none\n");
1294 }
jfigus67b9c732014-11-20 10:17:21 -05001295 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001296
jfigus67b9c732014-11-20 10:17:21 -05001297 /* loop over streams in session, printing the policy of each */
1298 stream = srtp->stream_list;
1299 while (stream != NULL) {
1300 if (stream->rtp_services > sec_serv_conf_and_auth) {
1301 return srtp_err_status_bad_param;
1302 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001303
jfigus67b9c732014-11-20 10:17:21 -05001304 printf("# SSRC: 0x%08x\r\n"
1305 "# rtp cipher: %s\r\n"
1306 "# rtp auth: %s\r\n"
1307 "# rtp services: %s\r\n"
1308 "# rtcp cipher: %s\r\n"
1309 "# rtcp auth: %s\r\n"
1310 "# rtcp services: %s\r\n"
1311 "# window size: %lu\r\n"
1312 "# tx rtx allowed:%s\r\n",
1313 stream->ssrc,
1314 stream->rtp_cipher->type->description,
1315 stream->rtp_auth->type->description,
1316 serv_descr[stream->rtp_services],
1317 stream->rtcp_cipher->type->description,
1318 stream->rtcp_auth->type->description,
1319 serv_descr[stream->rtcp_services],
jfigusde8deb32014-11-25 12:58:11 -05001320 srtp_rdbx_get_window_size(&stream->rtp_rdbx),
jfigus67b9c732014-11-20 10:17:21 -05001321 stream->allow_repeat_tx ? "true" : "false");
1322
Joachim Bauch99a74822015-11-17 00:08:19 +01001323 printf("# Encrypted extension headers: ");
1324 if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
1325 int* enc_xtn_hdr = stream->enc_xtn_hdr;
1326 int count = stream->enc_xtn_hdr_count;
1327 while (count > 0) {
1328 printf("%d ", *enc_xtn_hdr);
1329 enc_xtn_hdr++;
1330 count--;
1331 }
1332 printf("\n");
1333 } else {
1334 printf("none\n");
1335 }
1336
jfigus67b9c732014-11-20 10:17:21 -05001337 /* advance to next stream in the list */
1338 stream = stream->next;
1339 }
1340 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001341}
1342
jfigus857009c2014-11-05 11:17:43 -05001343srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001344srtp_print_policy (const srtp_policy_t *policy)
1345{
1346 srtp_err_status_t status;
1347 srtp_t session;
Cullen Jennings235513a2005-09-21 22:51:36 +00001348
jfigus67b9c732014-11-20 10:17:21 -05001349 status = srtp_create(&session, policy);
1350 if (status) {
1351 return status;
1352 }
1353 status = srtp_session_print_policy(session);
1354 if (status) {
1355 return status;
1356 }
1357 status = srtp_dealloc(session);
1358 if (status) {
1359 return status;
1360 }
1361 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001362}
1363
jfigus67b9c732014-11-20 10:17:21 -05001364/*
1365 * srtp_print_packet(...) is for debugging only
Cullen Jennings235513a2005-09-21 22:51:36 +00001366 * it prints an RTP packet to the stdout
1367 *
1368 * note that this function is *not* threadsafe
1369 */
1370
1371#include <stdio.h>
1372
1373#define MTU 2048
1374
1375char packet_string[MTU];
1376
1377char *
jfigus67b9c732014-11-20 10:17:21 -05001378srtp_packet_to_string (srtp_hdr_t *hdr, int pkt_octet_len)
1379{
1380 int octets_in_rtp_header = 12;
1381 uint8_t *data = ((uint8_t*)hdr) + octets_in_rtp_header;
1382 int hex_len = pkt_octet_len - octets_in_rtp_header;
Cullen Jennings235513a2005-09-21 22:51:36 +00001383
jfigus67b9c732014-11-20 10:17:21 -05001384 /* sanity checking */
1385 if ((hdr == NULL) || (pkt_octet_len > MTU)) {
1386 return NULL;
1387 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001388
jfigus67b9c732014-11-20 10:17:21 -05001389 /* write packet into string */
1390 sprintf(packet_string,
1391 "(s)rtp packet: {\n"
1392 " version:\t%d\n"
1393 " p:\t\t%d\n"
1394 " x:\t\t%d\n"
1395 " cc:\t\t%d\n"
1396 " m:\t\t%d\n"
1397 " pt:\t\t%x\n"
1398 " seq:\t\t%x\n"
1399 " ts:\t\t%x\n"
1400 " ssrc:\t%x\n"
1401 " data:\t%s\n"
1402 "} (%d octets in total)\n",
1403 hdr->version,
1404 hdr->p,
1405 hdr->x,
1406 hdr->cc,
1407 hdr->m,
1408 hdr->pt,
1409 hdr->seq,
1410 hdr->ts,
1411 hdr->ssrc,
1412 octet_string_hex_string(data, hex_len),
1413 pkt_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001414
jfigus67b9c732014-11-20 10:17:21 -05001415 return packet_string;
Cullen Jennings235513a2005-09-21 22:51:36 +00001416}
1417
1418/*
1419 * mips_estimate() is a simple function to estimate the number of
1420 * instructions per second that the host can perform. note that this
1421 * function can be grossly wrong; you may want to have a manual sanity
1422 * check of its output!
1423 *
1424 * the 'ignore' pointer is there to convince the compiler to not just
1425 * optimize away the function
1426 */
1427
1428double
jfigus67b9c732014-11-20 10:17:21 -05001429mips_estimate (int num_trials, int *ignore)
1430{
1431 clock_t t;
1432 volatile int i, sum;
Cullen Jennings235513a2005-09-21 22:51:36 +00001433
jfigus67b9c732014-11-20 10:17:21 -05001434 sum = 0;
1435 t = clock();
1436 for (i = 0; i < num_trials; i++) {
1437 sum += i;
1438 }
1439 t = clock() - t;
Cullen Jennings235513a2005-09-21 22:51:36 +00001440
1441/* printf("%d\n", sum); */
jfigus67b9c732014-11-20 10:17:21 -05001442 *ignore = sum;
Cullen Jennings235513a2005-09-21 22:51:36 +00001443
jfigus67b9c732014-11-20 10:17:21 -05001444 return (double)num_trials * CLOCKS_PER_SEC / t;
Cullen Jennings235513a2005-09-21 22:51:36 +00001445}
1446
1447
1448/*
1449 * srtp_validate() verifies the correctness of libsrtp by comparing
1450 * some computed packets against some pre-computed reference values.
1451 * These packets were made with the default SRTP policy.
1452 */
1453
1454
jfigus857009c2014-11-05 11:17:43 -05001455srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001456srtp_validate ()
1457{
1458 uint8_t srtp_plaintext_ref[28] = {
1459 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1460 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1461 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1462 0xab, 0xab, 0xab, 0xab
1463 };
1464 uint8_t srtp_plaintext[38] = {
1465 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1466 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1467 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1468 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
1469 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1470 };
1471 uint8_t srtp_ciphertext[38] = {
1472 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1473 0xca, 0xfe, 0xba, 0xbe, 0x4e, 0x55, 0xdc, 0x4c,
1474 0xe7, 0x99, 0x78, 0xd8, 0x8c, 0xa4, 0xd2, 0x15,
1475 0x94, 0x9d, 0x24, 0x02, 0xb7, 0x8d, 0x6a, 0xcc,
1476 0x99, 0xea, 0x17, 0x9b, 0x8d, 0xbb
1477 };
1478 srtp_t srtp_snd, srtp_recv;
1479 srtp_err_status_t status;
1480 int len;
1481 srtp_policy_t policy;
Cullen Jennings235513a2005-09-21 22:51:36 +00001482
jfigus67b9c732014-11-20 10:17:21 -05001483 /*
1484 * create a session with a single stream using the default srtp
1485 * policy and with the SSRC value 0xcafebabe
1486 */
Joachim Bauch99a74822015-11-17 00:08:19 +01001487 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05001488 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1489 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1490 policy.ssrc.type = ssrc_specific;
1491 policy.ssrc.value = 0xcafebabe;
1492 policy.key = test_key;
1493 policy.ekt = NULL;
1494 policy.window_size = 128;
1495 policy.allow_repeat_tx = 0;
1496 policy.next = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001497
jfigus67b9c732014-11-20 10:17:21 -05001498 status = srtp_create(&srtp_snd, &policy);
1499 if (status) {
1500 return status;
1501 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001502
jfigus67b9c732014-11-20 10:17:21 -05001503 /*
1504 * protect plaintext, then compare with ciphertext
1505 */
1506 len = 28;
1507 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1508 if (status || (len != 38)) {
1509 return srtp_err_status_fail;
1510 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001511
jfigus67b9c732014-11-20 10:17:21 -05001512 debug_print(mod_driver, "ciphertext:\n %s",
1513 octet_string_hex_string(srtp_plaintext, len));
1514 debug_print(mod_driver, "ciphertext reference:\n %s",
1515 octet_string_hex_string(srtp_ciphertext, len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001516
jfigus67b9c732014-11-20 10:17:21 -05001517 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
1518 return srtp_err_status_fail;
1519 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001520
jfigus67b9c732014-11-20 10:17:21 -05001521 /*
1522 * create a receiver session context comparable to the one created
1523 * above - we need to do this so that the replay checking doesn't
1524 * complain
1525 */
1526 status = srtp_create(&srtp_recv, &policy);
1527 if (status) {
1528 return status;
1529 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001530
jfigus67b9c732014-11-20 10:17:21 -05001531 /*
1532 * unprotect ciphertext, then compare with plaintext
1533 */
1534 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
1535 if (status || (len != 28)) {
1536 return status;
1537 }
1538
1539 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
1540 return srtp_err_status_fail;
1541 }
1542
1543 status = srtp_dealloc(srtp_snd);
1544 if (status) {
1545 return status;
1546 }
1547
1548 status = srtp_dealloc(srtp_recv);
1549 if (status) {
1550 return status;
1551 }
1552
1553 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001554}
1555
Joachim Bauch99a74822015-11-17 00:08:19 +01001556/*
1557 * Test vectors taken from RFC 6904, Appendix A
1558 */
1559srtp_err_status_t
1560srtp_validate_encrypted_extensions_headers() {
1561 unsigned char test_key_ext_headers[30] = {
1562 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
1563 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
1564 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
1565 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
1566 };
1567 uint8_t srtp_plaintext_ref[56] = {
1568 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1569 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1570 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1571 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1572 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1573 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1574 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab
1575 };
1576 uint8_t srtp_plaintext[66] = {
1577 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1578 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1579 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1580 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1581 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1582 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1583 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1584 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1585 0x00, 0x00
1586 };
1587 uint8_t srtp_ciphertext[66] = {
1588 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1589 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1590 0x17, 0x58, 0x8A, 0x92, 0x70, 0xF4, 0xE1, 0x5E,
1591 0x1C, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x95, 0x46,
1592 0xA9, 0x94, 0xF0, 0xBC, 0x54, 0x78, 0x97, 0x00,
1593 0x4e, 0x55, 0xdc, 0x4c, 0xe7, 0x99, 0x78, 0xd8,
1594 0x8c, 0xa4, 0xd2, 0x15, 0x94, 0x9d, 0x24, 0x02,
1595 0x5a, 0x46, 0xb3, 0xca, 0x35, 0xc5, 0x35, 0xa8,
1596 0x91, 0xc7
1597 };
1598 srtp_t srtp_snd, srtp_recv;
1599 srtp_err_status_t status;
1600 int len;
1601 srtp_policy_t policy;
1602 int headers[3] = {1, 3, 4};
1603
1604 /*
1605 * create a session with a single stream using the default srtp
1606 * policy and with the SSRC value 0xcafebabe
1607 */
1608 memset(&policy, 0, sizeof(policy));
1609 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1610 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1611 policy.ssrc.type = ssrc_specific;
1612 policy.ssrc.value = 0xcafebabe;
1613 policy.key = test_key_ext_headers;
1614 policy.ekt = NULL;
1615 policy.window_size = 128;
1616 policy.allow_repeat_tx = 0;
1617 policy.enc_xtn_hdr = headers;
1618 policy.enc_xtn_hdr_count = sizeof(headers) / sizeof(headers[0]);
1619 policy.next = NULL;
1620
1621 status = srtp_create(&srtp_snd, &policy);
1622 if (status)
1623 return status;
1624
1625 /*
1626 * protect plaintext, then compare with ciphertext
1627 */
1628 len = sizeof(srtp_plaintext_ref);
1629 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1630 if (status || (len != sizeof(srtp_plaintext)))
1631 return srtp_err_status_fail;
1632
1633 debug_print(mod_driver, "ciphertext:\n %s",
1634 srtp_octet_string_hex_string(srtp_plaintext, len));
1635 debug_print(mod_driver, "ciphertext reference:\n %s",
1636 srtp_octet_string_hex_string(srtp_ciphertext, len));
1637
1638 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len))
1639 return srtp_err_status_fail;
1640
1641 /*
1642 * create a receiver session context comparable to the one created
1643 * above - we need to do this so that the replay checking doesn't
1644 * complain
1645 */
1646 status = srtp_create(&srtp_recv, &policy);
1647 if (status)
1648 return status;
1649
1650 /*
1651 * unprotect ciphertext, then compare with plaintext
1652 */
1653 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
Joachim Baucha9ecefe2015-12-14 21:40:42 +01001654 if (status) {
Joachim Bauch99a74822015-11-17 00:08:19 +01001655 return status;
Joachim Baucha9ecefe2015-12-14 21:40:42 +01001656 } else if (len != sizeof(srtp_plaintext_ref)) {
1657 return srtp_err_status_fail;
1658 }
Joachim Bauch99a74822015-11-17 00:08:19 +01001659
1660 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len))
1661 return srtp_err_status_fail;
1662
1663 status = srtp_dealloc(srtp_snd);
1664 if (status)
1665 return status;
1666
1667 status = srtp_dealloc(srtp_recv);
1668 if (status)
1669 return status;
1670
1671 return srtp_err_status_ok;
1672}
1673
Cullen Jennings235513a2005-09-21 22:51:36 +00001674
Joachim Bauch80a45b52015-12-06 22:57:58 +01001675#ifdef OPENSSL
Joachim Baucha9ecefe2015-12-14 21:40:42 +01001676
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001677/*
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001678 * Headers of test vectors taken from RFC 6904, Appendix A
1679 */
1680srtp_err_status_t
1681srtp_validate_encrypted_extensions_headers_gcm() {
1682 unsigned char test_key_ext_headers[30] = {
1683 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
1684 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
1685 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
1686 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
1687 };
1688 uint8_t srtp_plaintext_ref[56] = {
1689 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1690 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1691 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1692 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1693 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1694 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1695 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab
1696 };
1697 uint8_t srtp_plaintext[64] = {
1698 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1699 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1700 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1701 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1702 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1703 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1704 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1705 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1706 };
1707 uint8_t srtp_ciphertext[64] = {
1708 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1709 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1710 0x17, 0x58, 0x8A, 0x92, 0x70, 0xF4, 0xE1, 0x5E,
1711 0x1C, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x95, 0x46,
1712 0xA9, 0x94, 0xF0, 0xBC, 0x54, 0x78, 0x97, 0x00,
1713 0x0e, 0xca, 0x0c, 0xf9, 0x5e, 0xe9, 0x55, 0xb2,
1714 0x6c, 0xd3, 0xd2, 0x88, 0xb4, 0x9f, 0x6c, 0xa9,
1715 0xbb, 0x4e, 0x15, 0xc2, 0xe9, 0xf2, 0x66, 0x78
1716 };
1717 srtp_t srtp_snd, srtp_recv;
1718 srtp_err_status_t status;
1719 int len;
1720 srtp_policy_t policy;
1721 int headers[3] = {1, 3, 4};
1722
1723 /*
1724 * create a session with a single stream using the default srtp
1725 * policy and with the SSRC value 0xcafebabe
1726 */
1727 memset(&policy, 0, sizeof(policy));
1728 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
1729 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
1730 policy.ssrc.type = ssrc_specific;
1731 policy.ssrc.value = 0xcafebabe;
1732 policy.key = test_key_ext_headers;
1733 policy.ekt = NULL;
1734 policy.window_size = 128;
1735 policy.allow_repeat_tx = 0;
1736 policy.enc_xtn_hdr = headers;
1737 policy.enc_xtn_hdr_count = sizeof(headers) / sizeof(headers[0]);
1738 policy.next = NULL;
1739
1740 status = srtp_create(&srtp_snd, &policy);
1741 if (status)
1742 return status;
1743
1744 /*
1745 * protect plaintext, then compare with ciphertext
1746 */
1747 len = sizeof(srtp_plaintext_ref);
1748 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1749 if (status || (len != sizeof(srtp_plaintext)))
1750 return srtp_err_status_fail;
1751
1752 debug_print(mod_driver, "ciphertext:\n %s",
1753 srtp_octet_string_hex_string(srtp_plaintext, len));
1754 debug_print(mod_driver, "ciphertext reference:\n %s",
1755 srtp_octet_string_hex_string(srtp_ciphertext, len));
1756
1757 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len))
1758 return srtp_err_status_fail;
1759
1760 /*
1761 * create a receiver session context comparable to the one created
1762 * above - we need to do this so that the replay checking doesn't
1763 * complain
1764 */
1765 status = srtp_create(&srtp_recv, &policy);
1766 if (status)
1767 return status;
1768
1769 /*
1770 * unprotect ciphertext, then compare with plaintext
1771 */
1772 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
Joachim Baucha9ecefe2015-12-14 21:40:42 +01001773 if (status) {
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001774 return status;
Joachim Baucha9ecefe2015-12-14 21:40:42 +01001775 } else if (len != sizeof(srtp_plaintext_ref)) {
1776 return srtp_err_status_fail;
1777 }
Joachim Bauchfa1e8c22015-11-24 23:38:48 +01001778
1779 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len))
1780 return srtp_err_status_fail;
1781
1782 status = srtp_dealloc(srtp_snd);
1783 if (status)
1784 return status;
1785
1786 status = srtp_dealloc(srtp_recv);
1787 if (status)
1788 return status;
1789
1790 return srtp_err_status_ok;
1791}
Joachim Bauch80a45b52015-12-06 22:57:58 +01001792#endif
Cullen Jennings235513a2005-09-21 22:51:36 +00001793
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001794/*
1795 * srtp_validate_aes_256() verifies the correctness of libsrtp by comparing
1796 * some computed packets against some pre-computed reference values.
1797 * These packets were made with the AES-CM-256/HMAC-SHA-1-80 policy.
1798 */
1799
1800
jfigus857009c2014-11-05 11:17:43 -05001801srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001802srtp_validate_aes_256 ()
1803{
1804 unsigned char aes_256_test_key[46] = {
1805 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
1806 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
1807 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
1808 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001809
jfigus67b9c732014-11-20 10:17:21 -05001810 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
1811 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
1812 };
1813 uint8_t srtp_plaintext_ref[28] = {
1814 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1815 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1816 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1817 0xab, 0xab, 0xab, 0xab
1818 };
1819 uint8_t srtp_plaintext[38] = {
1820 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1821 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1822 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1823 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
1824 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1825 };
1826 uint8_t srtp_ciphertext[38] = {
1827 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1828 0xca, 0xfe, 0xba, 0xbe, 0xf1, 0xd9, 0xde, 0x17,
1829 0xff, 0x25, 0x1f, 0xf1, 0xaa, 0x00, 0x77, 0x74,
1830 0xb0, 0xb4, 0xb4, 0x0d, 0xa0, 0x8d, 0x9d, 0x9a,
1831 0x5b, 0x3a, 0x55, 0xd8, 0x87, 0x3b
1832 };
1833 srtp_t srtp_snd, srtp_recv;
1834 srtp_err_status_t status;
1835 int len;
1836 srtp_policy_t policy;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001837
jfigus67b9c732014-11-20 10:17:21 -05001838 /*
1839 * create a session with a single stream using the default srtp
1840 * policy and with the SSRC value 0xcafebabe
1841 */
Joachim Bauch99a74822015-11-17 00:08:19 +01001842 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05001843 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
1844 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtcp);
1845 policy.ssrc.type = ssrc_specific;
1846 policy.ssrc.value = 0xcafebabe;
1847 policy.key = aes_256_test_key;
1848 policy.ekt = NULL;
1849 policy.window_size = 128;
1850 policy.allow_repeat_tx = 0;
1851 policy.next = NULL;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001852
jfigus67b9c732014-11-20 10:17:21 -05001853 status = srtp_create(&srtp_snd, &policy);
1854 if (status) {
1855 return status;
1856 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001857
jfigus67b9c732014-11-20 10:17:21 -05001858 /*
1859 * protect plaintext, then compare with ciphertext
1860 */
1861 len = 28;
1862 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1863 if (status || (len != 38)) {
1864 return srtp_err_status_fail;
1865 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001866
jfigus67b9c732014-11-20 10:17:21 -05001867 debug_print(mod_driver, "ciphertext:\n %s",
1868 octet_string_hex_string(srtp_plaintext, len));
1869 debug_print(mod_driver, "ciphertext reference:\n %s",
1870 octet_string_hex_string(srtp_ciphertext, len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001871
jfigus67b9c732014-11-20 10:17:21 -05001872 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
1873 return srtp_err_status_fail;
1874 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001875
jfigus67b9c732014-11-20 10:17:21 -05001876 /*
1877 * create a receiver session context comparable to the one created
1878 * above - we need to do this so that the replay checking doesn't
1879 * complain
1880 */
1881 status = srtp_create(&srtp_recv, &policy);
1882 if (status) {
1883 return status;
1884 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001885
jfigus67b9c732014-11-20 10:17:21 -05001886 /*
1887 * unprotect ciphertext, then compare with plaintext
1888 */
1889 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
1890 if (status || (len != 28)) {
1891 return status;
1892 }
1893
1894 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
1895 return srtp_err_status_fail;
1896 }
1897
1898 status = srtp_dealloc(srtp_snd);
1899 if (status) {
1900 return status;
1901 }
1902
1903 status = srtp_dealloc(srtp_recv);
1904 if (status) {
1905 return status;
1906 }
1907
1908 return srtp_err_status_ok;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001909}
1910
1911
jfigus857009c2014-11-05 11:17:43 -05001912srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001913srtp_create_big_policy (srtp_policy_t **list)
1914{
1915 extern const srtp_policy_t *policy_array[];
1916 srtp_policy_t *p, *tmp;
1917 int i = 0;
1918 uint32_t ssrc = 0;
Cullen Jennings235513a2005-09-21 22:51:36 +00001919
jfigus67b9c732014-11-20 10:17:21 -05001920 /* sanity checking */
1921 if ((list == NULL) || (policy_array[0] == NULL)) {
1922 return srtp_err_status_bad_param;
1923 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001924
jfigus67b9c732014-11-20 10:17:21 -05001925 /*
1926 * loop over policy list, mallocing a new list and copying values
1927 * into it (and incrementing the SSRC value as we go along)
1928 */
1929 tmp = NULL;
1930 while (policy_array[i] != NULL) {
1931 p = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
1932 if (p == NULL) {
1933 return srtp_err_status_bad_param;
1934 }
1935 memcpy(p, policy_array[i], sizeof(srtp_policy_t));
1936 p->ssrc.type = ssrc_specific;
1937 p->ssrc.value = ssrc++;
1938 p->next = tmp;
1939 tmp = p;
1940 i++;
1941 }
1942 *list = p;
1943
1944 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001945}
1946
jfigus857009c2014-11-05 11:17:43 -05001947srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001948srtp_dealloc_big_policy (srtp_policy_t *list)
1949{
1950 srtp_policy_t *p, *next;
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001951
jfigus67b9c732014-11-20 10:17:21 -05001952 for (p = list; p != NULL; p = next) {
1953 next = p->next;
1954 free(p);
1955 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001956
jfigus67b9c732014-11-20 10:17:21 -05001957 return srtp_err_status_ok;
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001958}
1959
Joachim Bauchc8a19ae2015-12-14 22:50:36 +01001960srtp_err_status_t
1961srtp_test_empty_payload()
1962{
1963 srtp_t srtp_snd, srtp_recv;
1964 srtp_err_status_t status;
1965 int len;
1966 srtp_policy_t policy;
1967 srtp_hdr_t *mesg;
1968
1969 /*
1970 * create a session with a single stream using the default srtp
1971 * policy and with the SSRC value 0xcafebabe
1972 */
1973 memset(&policy, 0, sizeof(policy));
1974 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1975 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1976 policy.ssrc.type = ssrc_specific;
1977 policy.ssrc.value = 0xcafebabe;
1978 policy.key = test_key;
1979 policy.ekt = NULL;
1980 policy.window_size = 128;
1981 policy.allow_repeat_tx = 0;
1982 policy.next = NULL;
1983
1984 status = srtp_create(&srtp_snd, &policy);
1985 if (status) {
1986 return status;
1987 }
1988
1989 mesg = srtp_create_test_packet(0, policy.ssrc.value);
1990 if (mesg == NULL) {
1991 return srtp_err_status_fail;
1992 }
1993
1994 len = 12; /* only the header */
1995 status = srtp_protect(srtp_snd, mesg, &len);
1996 if (status) {
1997 return status;
1998 } else if (len != 12 + 10) {
1999 return srtp_err_status_fail;
2000 }
2001
2002 /*
2003 * create a receiver session context comparable to the one created
2004 * above - we need to do this so that the replay checking doesn't
2005 * complain
2006 */
2007 status = srtp_create(&srtp_recv, &policy);
2008 if (status) {
2009 return status;
2010 }
2011
2012 /*
2013 * unprotect ciphertext, then compare with plaintext
2014 */
2015 status = srtp_unprotect(srtp_recv, mesg, &len);
2016 if (status) {
2017 return status;
2018 } else if (len != 12) {
2019 return srtp_err_status_fail;
2020 }
2021
2022 status = srtp_dealloc(srtp_snd);
2023 if (status) {
2024 return status;
2025 }
2026
2027 status = srtp_dealloc(srtp_recv);
2028 if (status) {
2029 return status;
2030 }
2031
2032 free(mesg);
2033
2034 return srtp_err_status_ok;
2035}
2036
2037#ifdef OPENSSL
2038srtp_err_status_t
2039srtp_test_empty_payload_gcm()
2040{
2041 srtp_t srtp_snd, srtp_recv;
2042 srtp_err_status_t status;
2043 int len;
2044 srtp_policy_t policy;
2045 srtp_hdr_t *mesg;
2046
2047 /*
2048 * create a session with a single stream using the default srtp
2049 * policy and with the SSRC value 0xcafebabe
2050 */
2051 memset(&policy, 0, sizeof(policy));
2052 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
2053 srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
2054 policy.ssrc.type = ssrc_specific;
2055 policy.ssrc.value = 0xcafebabe;
2056 policy.key = test_key;
2057 policy.ekt = NULL;
2058 policy.window_size = 128;
2059 policy.allow_repeat_tx = 0;
2060 policy.next = NULL;
2061
2062 status = srtp_create(&srtp_snd, &policy);
2063 if (status) {
2064 return status;
2065 }
2066
2067 mesg = srtp_create_test_packet(0, policy.ssrc.value);
2068 if (mesg == NULL) {
2069 return srtp_err_status_fail;
2070 }
2071
2072 len = 12; /* only the header */
2073 status = srtp_protect(srtp_snd, mesg, &len);
2074 if (status) {
2075 return status;
2076 } else if (len != 12 + 8) {
2077 return srtp_err_status_fail;
2078 }
2079
2080 /*
2081 * create a receiver session context comparable to the one created
2082 * above - we need to do this so that the replay checking doesn't
2083 * complain
2084 */
2085 status = srtp_create(&srtp_recv, &policy);
2086 if (status) {
2087 return status;
2088 }
2089
2090 /*
2091 * unprotect ciphertext, then compare with plaintext
2092 */
2093 status = srtp_unprotect(srtp_recv, mesg, &len);
2094 if (status) {
2095 return status;
2096 } else if (len != 12) {
2097 return srtp_err_status_fail;
2098 }
2099
2100 status = srtp_dealloc(srtp_snd);
2101 if (status) {
2102 return status;
2103 }
2104
2105 status = srtp_dealloc(srtp_recv);
2106 if (status) {
2107 return status;
2108 }
2109
2110 free(mesg);
2111
2112 return srtp_err_status_ok;
2113}
2114#endif // OPENSSL
Jonathan Lennox80c4c832010-05-17 19:30:28 +00002115
jfigus857009c2014-11-05 11:17:43 -05002116srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05002117srtp_test_remove_stream ()
2118{
2119 srtp_err_status_t status;
2120 srtp_policy_t *policy_list, policy;
2121 srtp_t session;
2122 srtp_stream_t stream;
Cullen Jennings235513a2005-09-21 22:51:36 +00002123
jfigus67b9c732014-11-20 10:17:21 -05002124 /*
2125 * srtp_get_stream() is a libSRTP internal function that we declare
2126 * here so that we can use it to verify the correct operation of the
2127 * library
2128 */
2129 extern srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
Cullen Jennings235513a2005-09-21 22:51:36 +00002130
Cullen Jennings235513a2005-09-21 22:51:36 +00002131
jfigus67b9c732014-11-20 10:17:21 -05002132 status = srtp_create_big_policy(&policy_list);
2133 if (status) {
2134 return status;
2135 }
Cullen Jennings235513a2005-09-21 22:51:36 +00002136
jfigus67b9c732014-11-20 10:17:21 -05002137 status = srtp_create(&session, policy_list);
2138 if (status) {
2139 return status;
2140 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00002141
jfigus67b9c732014-11-20 10:17:21 -05002142 /*
2143 * check for false positives by trying to remove a stream that's not
2144 * in the session
2145 */
2146 status = srtp_remove_stream(session, htonl(0xaaaaaaaa));
2147 if (status != srtp_err_status_no_ctx) {
2148 return srtp_err_status_fail;
2149 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00002150
jfigus67b9c732014-11-20 10:17:21 -05002151 /*
2152 * check for false negatives by removing stream 0x1, then
2153 * searching for streams 0x0 and 0x2
2154 */
2155 status = srtp_remove_stream(session, htonl(0x1));
2156 if (status != srtp_err_status_ok) {
2157 return srtp_err_status_fail;
2158 }
2159 stream = srtp_get_stream(session, htonl(0x0));
2160 if (stream == NULL) {
2161 return srtp_err_status_fail;
2162 }
2163 stream = srtp_get_stream(session, htonl(0x2));
2164 if (stream == NULL) {
2165 return srtp_err_status_fail;
2166 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00002167
jfigus67b9c732014-11-20 10:17:21 -05002168 status = srtp_dealloc(session);
2169 if (status != srtp_err_status_ok) {
2170 return status;
2171 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00002172
jfigus67b9c732014-11-20 10:17:21 -05002173 status = srtp_dealloc_big_policy(policy_list);
2174 if (status != srtp_err_status_ok) {
2175 return status;
2176 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00002177
jfigus67b9c732014-11-20 10:17:21 -05002178 /* Now test adding and removing a single stream */
Joachim Bauch99a74822015-11-17 00:08:19 +01002179 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05002180 srtp_crypto_policy_set_rtp_default(&policy.rtp);
2181 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
2182 policy.ssrc.type = ssrc_specific;
2183 policy.ssrc.value = 0xcafebabe;
2184 policy.key = test_key;
2185 policy.ekt = NULL;
2186 policy.window_size = 128;
2187 policy.allow_repeat_tx = 0;
2188 policy.next = NULL;
Jonathan Lennoxad741b22010-05-27 19:23:05 +00002189
jfigus67b9c732014-11-20 10:17:21 -05002190 status = srtp_create(&session, NULL);
2191 if (status != srtp_err_status_ok) {
2192 return status;
2193 }
2194
2195 status = srtp_add_stream(session, &policy);
2196 if (status != srtp_err_status_ok) {
2197 return status;
2198 }
2199
2200 status = srtp_remove_stream(session, htonl(0xcafebabe));
2201 if (status != srtp_err_status_ok) {
2202 return status;
2203 }
2204
2205 status = srtp_dealloc(session);
2206 if (status != srtp_err_status_ok) {
2207 return status;
2208 }
2209
2210 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00002211}
2212
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002213
2214unsigned char test_alt_key[46] = {
2215 0xe5, 0x19, 0x6f, 0x01, 0x5e, 0xf1, 0x9b, 0xe1,
2216 0xd7, 0x47, 0xa7, 0x27, 0x07, 0xd7, 0x47, 0x33,
2217 0x01, 0xc2, 0x35, 0x4d, 0x59, 0x6a, 0xf7, 0x84,
2218 0x96, 0x98, 0xeb, 0xaa, 0xac, 0xf6, 0xa1, 0x45,
2219 0xc7, 0x15, 0xe2, 0xea, 0xfe, 0x55, 0x67, 0x96,
2220 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
2221};
2222
2223/*
2224 * srtp_test_update() verifies updating/rekeying exsisting streams.
2225 * As stated in https://tools.ietf.org/html/rfc3711#section-3.3.1
2226 * the value of the ROC must not be reset after a rekey, this test
2227 * atempts to prove that srtp_update does not reset the ROC.
2228 */
2229
2230srtp_err_status_t
2231srtp_test_update() {
2232
2233 srtp_err_status_t status;
2234 uint32_t ssrc = 0x12121212;
2235 int msg_len_octets = 32;
2236 int protected_msg_len_octets;
2237 srtp_hdr_t * msg;
2238 srtp_t srtp_snd, srtp_recv;
2239 srtp_policy_t policy;
2240
Joachim Bauch1b793352015-12-14 21:30:44 +01002241 memset(&policy, 0, sizeof(policy));
Pascal Bühlerbd3112a2015-11-06 20:29:15 +01002242 srtp_crypto_policy_set_rtp_default(&policy.rtp);
2243 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
2244 policy.ekt = NULL;
2245 policy.window_size = 128;
2246 policy.allow_repeat_tx = 0;
2247 policy.next = NULL;
2248 policy.ssrc.type = ssrc_any_outbound;
2249 policy.key = test_key;
2250
2251 /* create a send and recive ctx with defualt profile and test_key */
2252 status = srtp_create(&srtp_recv, &policy);
2253 if (status)
2254 return status;
2255
2256 policy.ssrc.type = ssrc_any_inbound;
2257 status = srtp_create(&srtp_snd, &policy);
2258 if (status)
2259 return status;
2260
2261 /* protect and unprotect two msg's that will cause the ROC to be equal to 1 */
2262 msg = srtp_create_test_packet(msg_len_octets, ssrc);
2263 if (msg == NULL)
2264 return srtp_err_status_alloc_fail;
2265 msg->seq = htons(65535);
2266
2267 protected_msg_len_octets = msg_len_octets;
2268 status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
2269 if (status)
2270 return srtp_err_status_fail;
2271
2272 status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
2273 if (status)
2274 return status;
2275
2276 free(msg);
2277
2278 msg = srtp_create_test_packet(msg_len_octets, ssrc);
2279 if (msg == NULL)
2280 return srtp_err_status_alloc_fail;
2281 msg->seq = htons(1);
2282
2283 protected_msg_len_octets = msg_len_octets;
2284 status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
2285 if (status)
2286 return srtp_err_status_fail;
2287
2288 status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
2289 if (status)
2290 return status;
2291
2292 free(msg);
2293
2294 /* update send ctx to use test_alt_key */
2295 policy.ssrc.type = ssrc_any_outbound;
2296 policy.key = test_alt_key;
2297 status = srtp_update(srtp_snd, &policy);
2298 if (status)
2299 return status;
2300
2301 /* create and protect msg with new key and ROC still equal to 1 */
2302 msg = srtp_create_test_packet(msg_len_octets, ssrc);
2303 if (msg == NULL)
2304 return srtp_err_status_alloc_fail;
2305 msg->seq = htons(2);
2306
2307 protected_msg_len_octets = msg_len_octets;
2308 status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
2309 if (status)
2310 return srtp_err_status_fail;
2311
2312 /* verify that recive ctx will fail to unprotect as it still uses test_key */
2313 status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
2314 if (status == srtp_err_status_ok)
2315 return srtp_err_status_fail;
2316
2317 /* create a new recvieve ctx with test_alt_key but since it is new it will have ROC equal to 1
2318 * and therefore should fail to unprotected */
2319 {
2320 srtp_t srtp_recv_roc_0;
2321
2322 policy.ssrc.type = ssrc_any_inbound;
2323 policy.key = test_alt_key;
2324 status = srtp_create(&srtp_recv_roc_0, &policy);
2325 if (status)
2326 return status;
2327
2328 status = srtp_unprotect(srtp_recv_roc_0, msg, &protected_msg_len_octets);
2329 if (status == srtp_err_status_ok)
2330 return srtp_err_status_fail;
2331
2332 status = srtp_dealloc(srtp_recv_roc_0);
2333 if (status)
2334 return status;
2335 }
2336
2337 /* update recive ctx to use test_alt_key */
2338 policy.ssrc.type = ssrc_any_inbound;
2339 policy.key = test_alt_key;
2340 status = srtp_update(srtp_recv, &policy);
2341 if (status)
2342 return status;
2343
2344 /* verify that can still unprotect, therfore key is updated and ROC value is preserved */
2345 status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
2346 if (status)
2347 return status;
2348
2349 free(msg);
2350
2351 status = srtp_dealloc(srtp_snd);
2352 if (status)
2353 return status;
2354
2355 status = srtp_dealloc(srtp_recv);
2356 if (status)
2357 return status;
2358
2359 return srtp_err_status_ok;
2360}
2361
Cullen Jennings235513a2005-09-21 22:51:36 +00002362/*
2363 * srtp policy definitions - these definitions are used above
2364 */
2365
jfigus8c36da22013-10-01 16:41:19 -04002366unsigned char test_key[46] = {
Cullen Jennings235513a2005-09-21 22:51:36 +00002367 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
2368 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
2369 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
jfigus8c36da22013-10-01 16:41:19 -04002370 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73,
2371 0xc3, 0x17, 0xf2, 0xda, 0xbe, 0x35, 0x77, 0x93,
Cullen Jennings235513a2005-09-21 22:51:36 +00002372 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
2373};
2374
2375
2376const srtp_policy_t default_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002377 { ssrc_any_outbound, 0 }, /* SSRC */
2378 { /* SRTP policy */
2379 SRTP_AES_128_ICM, /* cipher type */
2380 30, /* cipher key length in octets */
2381 SRTP_HMAC_SHA1, /* authentication func type */
2382 16, /* auth key length in octets */
2383 10, /* auth tag length in octets */
2384 sec_serv_conf_and_auth /* security services flag */
2385 },
2386 { /* SRTCP policy */
2387 SRTP_AES_128_ICM, /* cipher type */
2388 30, /* cipher key length in octets */
2389 SRTP_HMAC_SHA1, /* authentication func type */
2390 16, /* auth key length in octets */
2391 10, /* auth tag length in octets */
2392 sec_serv_conf_and_auth /* security services flag */
2393 },
2394 test_key,
2395 NULL, /* indicates that EKT is not in use */
2396 128, /* replay window size */
2397 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002398 NULL, /* no encrypted extension headers */
2399 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002400 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002401};
2402
2403const srtp_policy_t aes_only_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002404 { ssrc_any_outbound, 0 }, /* SSRC */
2405 {
2406 SRTP_AES_128_ICM, /* cipher type */
2407 30, /* cipher key length in octets */
2408 SRTP_NULL_AUTH, /* authentication func type */
2409 0, /* auth key length in octets */
2410 0, /* auth tag length in octets */
2411 sec_serv_conf /* security services flag */
2412 },
2413 {
2414 SRTP_AES_128_ICM, /* cipher type */
2415 30, /* cipher key length in octets */
2416 SRTP_NULL_AUTH, /* authentication func type */
2417 0, /* auth key length in octets */
2418 0, /* auth tag length in octets */
2419 sec_serv_conf /* security services flag */
2420 },
2421 test_key,
2422 NULL, /* indicates that EKT is not in use */
2423 128, /* replay window size */
2424 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002425 NULL, /* no encrypted extension headers */
2426 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002427 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002428};
2429
2430const srtp_policy_t hmac_only_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002431 { ssrc_any_outbound, 0 }, /* SSRC */
2432 {
2433 SRTP_NULL_CIPHER, /* cipher type */
2434 0, /* cipher key length in octets */
2435 SRTP_HMAC_SHA1, /* authentication func type */
2436 20, /* auth key length in octets */
2437 4, /* auth tag length in octets */
2438 sec_serv_auth /* security services flag */
2439 },
2440 {
2441 SRTP_NULL_CIPHER, /* cipher type */
2442 0, /* cipher key length in octets */
2443 SRTP_HMAC_SHA1, /* authentication func type */
2444 20, /* auth key length in octets */
2445 4, /* auth tag length in octets */
2446 sec_serv_auth /* security services flag */
2447 },
2448 test_key,
2449 NULL, /* indicates that EKT is not in use */
2450 128, /* replay window size */
2451 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002452 NULL, /* no encrypted extension headers */
2453 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002454 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002455};
2456
jfigus8c36da22013-10-01 16:41:19 -04002457#ifdef OPENSSL
2458const srtp_policy_t aes128_gcm_8_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002459 { ssrc_any_outbound, 0 }, /* SSRC */
2460 { /* SRTP policy */
2461 SRTP_AES_128_GCM, /* cipher type */
2462 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2463 SRTP_NULL_AUTH, /* authentication func type */
2464 0, /* auth key length in octets */
2465 8, /* auth tag length in octets */
2466 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002467 },
jfigus67b9c732014-11-20 10:17:21 -05002468 { /* SRTCP policy */
2469 SRTP_AES_128_GCM, /* cipher type */
2470 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2471 SRTP_NULL_AUTH, /* authentication func type */
2472 0, /* auth key length in octets */
2473 8, /* auth tag length in octets */
2474 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002475 },
2476 test_key,
2477 NULL, /* indicates that EKT is not in use */
2478 128, /* replay window size */
2479 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002480 NULL, /* no encrypted extension headers */
2481 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002482 NULL
2483};
2484
2485const srtp_policy_t aes128_gcm_8_cauth_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002486 { ssrc_any_outbound, 0 }, /* SSRC */
2487 { /* SRTP policy */
2488 SRTP_AES_128_GCM, /* cipher type */
2489 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2490 SRTP_NULL_AUTH, /* authentication func type */
2491 0, /* auth key length in octets */
2492 8, /* auth tag length in octets */
2493 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002494 },
jfigus67b9c732014-11-20 10:17:21 -05002495 { /* SRTCP policy */
2496 SRTP_AES_128_GCM, /* cipher type */
2497 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2498 SRTP_NULL_AUTH, /* authentication func type */
2499 0, /* auth key length in octets */
2500 8, /* auth tag length in octets */
2501 sec_serv_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002502 },
2503 test_key,
2504 NULL, /* indicates that EKT is not in use */
2505 128, /* replay window size */
2506 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002507 NULL, /* no encrypted extension headers */
2508 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002509 NULL
2510};
jfigus67b9c732014-11-20 10:17:21 -05002511
jfigus8c36da22013-10-01 16:41:19 -04002512const srtp_policy_t aes256_gcm_8_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002513 { ssrc_any_outbound, 0 }, /* SSRC */
2514 { /* SRTP policy */
2515 SRTP_AES_256_GCM, /* cipher type */
2516 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2517 SRTP_NULL_AUTH, /* authentication func type */
2518 0, /* auth key length in octets */
2519 8, /* auth tag length in octets */
2520 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002521 },
jfigus67b9c732014-11-20 10:17:21 -05002522 { /* SRTCP policy */
2523 SRTP_AES_256_GCM, /* cipher type */
2524 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2525 SRTP_NULL_AUTH, /* authentication func type */
2526 0, /* auth key length in octets */
2527 8, /* auth tag length in octets */
2528 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002529 },
2530 test_key,
2531 NULL, /* indicates that EKT is not in use */
2532 128, /* replay window size */
2533 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002534 NULL, /* no encrypted extension headers */
2535 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002536 NULL
2537};
jfigus67b9c732014-11-20 10:17:21 -05002538
jfigus8c36da22013-10-01 16:41:19 -04002539const srtp_policy_t aes256_gcm_8_cauth_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002540 { ssrc_any_outbound, 0 }, /* SSRC */
2541 { /* SRTP policy */
2542 SRTP_AES_256_GCM, /* cipher type */
2543 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2544 SRTP_NULL_AUTH, /* authentication func type */
2545 0, /* auth key length in octets */
2546 8, /* auth tag length in octets */
2547 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002548 },
jfigus67b9c732014-11-20 10:17:21 -05002549 { /* SRTCP policy */
2550 SRTP_AES_256_GCM, /* cipher type */
2551 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2552 SRTP_NULL_AUTH, /* authentication func type */
2553 0, /* auth key length in octets */
2554 8, /* auth tag length in octets */
2555 sec_serv_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002556 },
2557 test_key,
2558 NULL, /* indicates that EKT is not in use */
2559 128, /* replay window size */
2560 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002561 NULL, /* no encrypted extension headers */
2562 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002563 NULL
2564};
2565#endif
2566
Cullen Jennings235513a2005-09-21 22:51:36 +00002567const srtp_policy_t null_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002568 { ssrc_any_outbound, 0 }, /* SSRC */
2569 {
2570 SRTP_NULL_CIPHER, /* cipher type */
2571 0, /* cipher key length in octets */
2572 SRTP_NULL_AUTH, /* authentication func type */
2573 0, /* auth key length in octets */
2574 0, /* auth tag length in octets */
2575 sec_serv_none /* security services flag */
2576 },
2577 {
2578 SRTP_NULL_CIPHER, /* cipher type */
2579 0, /* cipher key length in octets */
2580 SRTP_NULL_AUTH, /* authentication func type */
2581 0, /* auth key length in octets */
2582 0, /* auth tag length in octets */
2583 sec_serv_none /* security services flag */
2584 },
2585 test_key,
2586 NULL, /* indicates that EKT is not in use */
2587 128, /* replay window size */
2588 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002589 NULL, /* no encrypted extension headers */
2590 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002591 NULL
David McGrew79870d62007-06-15 18:17:39 +00002592};
2593
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002594unsigned char test_256_key[46] = {
jfigus67b9c732014-11-20 10:17:21 -05002595 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
2596 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
2597 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
2598 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002599
jfigus67b9c732014-11-20 10:17:21 -05002600 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
2601 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002602};
2603
2604const srtp_policy_t aes_256_hmac_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002605 { ssrc_any_outbound, 0 }, /* SSRC */
2606 { /* SRTP policy */
2607 SRTP_AES_ICM, /* cipher type */
2608 46, /* cipher key length in octets */
2609 SRTP_HMAC_SHA1, /* authentication func type */
2610 20, /* auth key length in octets */
2611 10, /* auth tag length in octets */
2612 sec_serv_conf_and_auth /* security services flag */
2613 },
2614 { /* SRTCP policy */
2615 SRTP_AES_ICM, /* cipher type */
2616 46, /* cipher key length in octets */
2617 SRTP_HMAC_SHA1, /* authentication func type */
2618 20, /* auth key length in octets */
2619 10, /* auth tag length in octets */
2620 sec_serv_conf_and_auth /* security services flag */
2621 },
2622 test_256_key,
2623 NULL, /* indicates that EKT is not in use */
2624 128, /* replay window size */
2625 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002626 NULL, /* no encrypted extension headers */
2627 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002628 NULL
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002629};
2630
David McGrew79870d62007-06-15 18:17:39 +00002631uint8_t ekt_test_key[16] = {
jfigus67b9c732014-11-20 10:17:21 -05002632 0x77, 0x26, 0x9d, 0xac, 0x16, 0xa3, 0x28, 0xca,
2633 0x8e, 0xc9, 0x68, 0x4b, 0xcc, 0xc4, 0xd2, 0x1b
David McGrew79870d62007-06-15 18:17:39 +00002634};
2635
2636#include "ekt.h"
2637
jfigusc5887e72014-11-06 09:46:18 -05002638srtp_ekt_policy_ctx_t ekt_test_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002639 0xa5a5, /* SPI */
2640 SRTP_EKT_CIPHER_AES_128_ECB,
2641 ekt_test_key,
2642 NULL
David McGrew79870d62007-06-15 18:17:39 +00002643};
2644
2645const srtp_policy_t hmac_only_with_ekt_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002646 { ssrc_any_outbound, 0 }, /* SSRC */
2647 {
2648 SRTP_NULL_CIPHER, /* cipher type */
2649 0, /* cipher key length in octets */
2650 SRTP_HMAC_SHA1, /* authentication func type */
2651 20, /* auth key length in octets */
2652 4, /* auth tag length in octets */
2653 sec_serv_auth /* security services flag */
2654 },
2655 {
2656 SRTP_NULL_CIPHER, /* cipher type */
2657 0, /* cipher key length in octets */
2658 SRTP_HMAC_SHA1, /* authentication func type */
2659 20, /* auth key length in octets */
2660 4, /* auth tag length in octets */
2661 sec_serv_auth /* security services flag */
2662 },
2663 test_key,
2664 &ekt_test_policy, /* indicates that EKT is not in use */
2665 128, /* replay window size */
2666 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002667 NULL, /* no encrypted extension headers */
2668 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002669 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002670};
2671
2672
2673/*
2674 * an array of pointers to the policies listed above
2675 *
2676 * This array is used to test various aspects of libSRTP for
2677 * different cryptographic policies. The order of the elements
2678 * matters - the timing test generates output that can be used
jfigus67b9c732014-11-20 10:17:21 -05002679 * in a plot (see the gnuplot script file 'timing'). If you
Cullen Jennings235513a2005-09-21 22:51:36 +00002680 * add to this list, you should do it at the end.
2681 */
2682
Cullen Jennings235513a2005-09-21 22:51:36 +00002683const srtp_policy_t *
2684policy_array[] = {
jfigus67b9c732014-11-20 10:17:21 -05002685 &hmac_only_policy,
2686 &aes_only_policy,
2687 &default_policy,
jfigus8c36da22013-10-01 16:41:19 -04002688#ifdef OPENSSL
jfigus67b9c732014-11-20 10:17:21 -05002689 &aes128_gcm_8_policy,
2690 &aes128_gcm_8_cauth_policy,
2691 &aes256_gcm_8_policy,
2692 &aes256_gcm_8_cauth_policy,
jfigus8c36da22013-10-01 16:41:19 -04002693#endif
jfigus67b9c732014-11-20 10:17:21 -05002694 &null_policy,
2695 &aes_256_hmac_policy,
2696 &hmac_only_with_ekt_policy,
2697 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002698};
2699
2700const srtp_policy_t wildcard_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002701 { ssrc_any_outbound, 0 }, /* SSRC */
2702 { /* SRTP policy */
2703 SRTP_AES_128_ICM, /* cipher type */
2704 30, /* cipher key length in octets */
2705 SRTP_HMAC_SHA1, /* authentication func type */
2706 16, /* auth key length in octets */
2707 10, /* auth tag length in octets */
2708 sec_serv_conf_and_auth /* security services flag */
2709 },
2710 { /* SRTCP policy */
2711 SRTP_AES_128_ICM, /* cipher type */
2712 30, /* cipher key length in octets */
2713 SRTP_HMAC_SHA1, /* authentication func type */
2714 16, /* auth key length in octets */
2715 10, /* auth tag length in octets */
2716 sec_serv_conf_and_auth /* security services flag */
2717 },
2718 test_key,
2719 NULL,
2720 128, /* replay window size */
2721 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002722 NULL, /* no encrypted extension headers */
2723 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002724 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002725};