blob: b48030d205d8c71ad1d7c6935674623727c937a1 [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
69srtp_err_status_t
Jonathan Lennox5df951a2010-05-20 20:55:54 +000070srtp_validate_aes_256(void);
71
jfigus857009c2014-11-05 11:17:43 -050072srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +000073srtp_create_big_policy(srtp_policy_t **list);
74
jfigus857009c2014-11-05 11:17:43 -050075srtp_err_status_t
Jonathan Lennox80c4c832010-05-17 19:30:28 +000076srtp_dealloc_big_policy(srtp_policy_t *list);
77
jfigus857009c2014-11-05 11:17:43 -050078srtp_err_status_t
Marcus Sundberga3f95fe2005-09-29 12:48:41 +000079srtp_test_remove_stream(void);
Cullen Jennings235513a2005-09-21 22:51:36 +000080
81double
82srtp_bits_per_second(int msg_len_octets, const srtp_policy_t *policy);
83
84double
85srtp_rejections_per_second(int msg_len_octets, const srtp_policy_t *policy);
86
87void
88srtp_do_timing(const srtp_policy_t *policy);
89
90void
91srtp_do_rejection_timing(const srtp_policy_t *policy);
92
jfigus857009c2014-11-05 11:17:43 -050093srtp_err_status_t
Joachim Bauch99a74822015-11-17 00:08:19 +010094srtp_test(const srtp_policy_t *policy, int extension_header);
Cullen Jennings235513a2005-09-21 22:51:36 +000095
jfigus857009c2014-11-05 11:17:43 -050096srtp_err_status_t
David McGrew9c70f292006-05-03 19:38:38 +000097srtcp_test(const srtp_policy_t *policy);
98
jfigus857009c2014-11-05 11:17:43 -050099srtp_err_status_t
Cullen Jennings235513a2005-09-21 22:51:36 +0000100srtp_session_print_policy(srtp_t srtp);
101
jfigus857009c2014-11-05 11:17:43 -0500102srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -0500103srtp_print_policy(const srtp_policy_t *policy);
Cullen Jennings235513a2005-09-21 22:51:36 +0000104
105char *
106srtp_packet_to_string(srtp_hdr_t *hdr, int packet_len);
107
108double
109mips_estimate(int num_trials, int *ignore);
110
jfigus8c36da22013-10-01 16:41:19 -0400111extern uint8_t test_key[46];
Cullen Jennings235513a2005-09-21 22:51:36 +0000112
113void
jfigus67b9c732014-11-20 10:17:21 -0500114usage (char *prog_name)
115{
116 printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
117 " -t run timing test\n"
118 " -r run rejection timing test\n"
119 " -c run codec timing test\n"
120 " -v run validation tests\n"
121 " -d <mod> turn on debugging module <mod>\n"
122 " -l list debugging modules\n", prog_name);
123 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000124}
125
126/*
127 * The policy_array is a null-terminated array of policy structs. it
128 * is declared at the end of this file
129 */
130
131extern const srtp_policy_t *policy_array[];
132
133
134/* the wildcard_policy is declared below; it has a wildcard ssrc */
135
136extern const srtp_policy_t wildcard_policy;
137
138/*
139 * mod_driver debug module - debugging module for this test driver
140 *
jfigus67b9c732014-11-20 10:17:21 -0500141 * we use the crypto_kernel debugging system in this driver, which
Cullen Jennings235513a2005-09-21 22:51:36 +0000142 * makes the interface uniform and increases portability
jfigus67b9c732014-11-20 10:17:21 -0500143 */
Cullen Jennings235513a2005-09-21 22:51:36 +0000144
jfigus02d6f032014-11-21 10:56:42 -0500145srtp_debug_module_t mod_driver = {
jfigus67b9c732014-11-20 10:17:21 -0500146 0, /* debugging is off by default */
147 "driver" /* printable name for module */
Cullen Jennings235513a2005-09-21 22:51:36 +0000148};
149
150int
jfigus67b9c732014-11-20 10:17:21 -0500151main (int argc, char *argv[])
152{
153 int q;
154 unsigned do_timing_test = 0;
155 unsigned do_rejection_test = 0;
156 unsigned do_codec_timing = 0;
157 unsigned do_validation = 0;
158 unsigned do_list_mods = 0;
159 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000160
161 /*
jfigus67b9c732014-11-20 10:17:21 -0500162 * verify that the compiler has interpreted the header data
163 * structure srtp_hdr_t correctly
Cullen Jennings235513a2005-09-21 22:51:36 +0000164 */
jfigus67b9c732014-11-20 10:17:21 -0500165 if (sizeof(srtp_hdr_t) != 12) {
166 printf("error: srtp_hdr_t has incorrect size"
167 "(size is %ld bytes, expected 12)\n",
168 (long)sizeof(srtp_hdr_t));
169 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000170 }
171
jfigus67b9c732014-11-20 10:17:21 -0500172 /* initialize srtp library */
173 status = srtp_init();
174 if (status) {
175 printf("error: srtp init failed with error code %d\n", status);
176 exit(1);
177 }
178
179 /* load srtp_driver debug module */
jfigus92736bc2014-11-21 10:30:54 -0500180 status = srtp_crypto_kernel_load_debug_module(&mod_driver);
jfigus67b9c732014-11-20 10:17:21 -0500181 if (status) {
182 printf("error: load of srtp_driver debug module failed "
183 "with error code %d\n", status);
184 exit(1);
185 }
186
187 /* process input arguments */
188 while (1) {
189 q = getopt_s(argc, argv, "trcvld:");
190 if (q == -1) {
191 break;
192 }
193 switch (q) {
194 case 't':
195 do_timing_test = 1;
196 break;
197 case 'r':
198 do_rejection_test = 1;
199 break;
200 case 'c':
201 do_codec_timing = 1;
202 break;
203 case 'v':
204 do_validation = 1;
205 break;
206 case 'l':
207 do_list_mods = 1;
208 break;
209 case 'd':
jfigus92736bc2014-11-21 10:30:54 -0500210 status = srtp_crypto_kernel_set_debug_module(optarg_s, 1);
jfigus67b9c732014-11-20 10:17:21 -0500211 if (status) {
212 printf("error: set debug module (%s) failed\n", optarg_s);
213 exit(1);
214 }
215 break;
216 default:
217 usage(argv[0]);
218 }
219 }
220
221 if (!do_validation && !do_timing_test && !do_codec_timing
222 && !do_list_mods && !do_rejection_test) {
223 usage(argv[0]);
224 }
225
226 if (do_list_mods) {
jfigus92736bc2014-11-21 10:30:54 -0500227 status = srtp_crypto_kernel_list_debug_modules();
jfigus67b9c732014-11-20 10:17:21 -0500228 if (status) {
229 printf("error: list of debug modules failed\n");
230 exit(1);
231 }
232 }
233
234 if (do_validation) {
235 const srtp_policy_t **policy = policy_array;
236 srtp_policy_t *big_policy;
237
238 /* loop over policy array, testing srtp and srtcp for each policy */
239 while (*policy != NULL) {
240 printf("testing srtp_protect and srtp_unprotect\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100241 if (srtp_test(*policy, 0) == srtp_err_status_ok) {
242 printf("passed\n\n");
243 } else{
244 printf("failed\n");
245 exit(1);
246 }
247 printf("testing srtp_protect and srtp_unprotect with encrypted extensions headers\n");
248 if (srtp_test(*policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500249 printf("passed\n\n");
250 } else{
251 printf("failed\n");
252 exit(1);
253 }
254 printf("testing srtp_protect_rtcp and srtp_unprotect_rtcp\n");
255 if (srtcp_test(*policy) == srtp_err_status_ok) {
256 printf("passed\n\n");
257 } else{
258 printf("failed\n");
259 exit(1);
260 }
261 policy++;
262 }
263
264 /* create a big policy list and run tests on it */
265 status = srtp_create_big_policy(&big_policy);
266 if (status) {
267 printf("unexpected failure with error code %d\n", status);
268 exit(1);
269 }
270 printf("testing srtp_protect and srtp_unprotect with big policy\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100271 if (srtp_test(big_policy, 0) == srtp_err_status_ok) {
272 printf("passed\n\n");
273 } else{
274 printf("failed\n");
275 exit(1);
276 }
277 printf("testing srtp_protect and srtp_unprotect with big policy and encrypted extensions headers\n");
278 if (srtp_test(big_policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500279 printf("passed\n\n");
280 } else{
281 printf("failed\n");
282 exit(1);
283 }
284 status = srtp_dealloc_big_policy(big_policy);
285 if (status) {
286 printf("unexpected failure with error code %d\n", status);
287 exit(1);
288 }
289
290 /* run test on wildcard policy */
291 printf("testing srtp_protect and srtp_unprotect on "
292 "wildcard ssrc policy\n");
Joachim Bauch99a74822015-11-17 00:08:19 +0100293 if (srtp_test(&wildcard_policy, 0) == srtp_err_status_ok) {
294 printf("passed\n\n");
295 } else{
296 printf("failed\n");
297 exit(1);
298 }
299 printf("testing srtp_protect and srtp_unprotect on "
300 "wildcard ssrc policy and encrypted extensions headers\n");
301 if (srtp_test(&wildcard_policy, 1) == srtp_err_status_ok) {
jfigus67b9c732014-11-20 10:17:21 -0500302 printf("passed\n\n");
303 } else{
304 printf("failed\n");
305 exit(1);
306 }
307
308 /*
309 * run validation test against the reference packets - note
310 * that this test only covers the default policy
311 */
312 printf("testing srtp_protect and srtp_unprotect against "
313 "reference packets\n");
314 if (srtp_validate() == srtp_err_status_ok) {
315 printf("passed\n\n");
316 } else{
317 printf("failed\n");
318 exit(1);
319 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100320 printf("testing srtp_protect and srtp_unprotect against "
321 "reference packets with encrypted extensions headers\n");
322 if (srtp_validate_encrypted_extensions_headers() == srtp_err_status_ok)
323 printf("passed\n\n");
324 else {
325 printf("failed\n");
326 exit(1);
327 }
jfigus67b9c732014-11-20 10:17:21 -0500328
jfigusa14b5a02013-03-29 12:24:12 -0400329//FIXME: need to get this working with the OpenSSL AES module
330#ifndef OPENSSL
jfigus67b9c732014-11-20 10:17:21 -0500331 /*
332 * run validation test against the reference packets for
333 * AES-256
334 */
335 printf("testing srtp_protect and srtp_unprotect against "
336 "reference packets (AES-256)\n");
337 if (srtp_validate_aes_256() == srtp_err_status_ok) {
338 printf("passed\n\n");
339 } else{
340 printf("failed\n");
341 exit(1);
342 }
jfigusa14b5a02013-03-29 12:24:12 -0400343#endif
Jonathan Lennox5df951a2010-05-20 20:55:54 +0000344
jfigus67b9c732014-11-20 10:17:21 -0500345 /*
346 * test the function srtp_remove_stream()
347 */
348 printf("testing srtp_remove_stream()...");
349 if (srtp_test_remove_stream() == srtp_err_status_ok) {
350 printf("passed\n");
351 } else{
352 printf("failed\n");
353 exit(1);
354 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000355 }
jfigus67b9c732014-11-20 10:17:21 -0500356
357 if (do_timing_test) {
358 const srtp_policy_t **policy = policy_array;
359
360 /* loop over policies, run timing test for each */
361 while (*policy != NULL) {
362 srtp_print_policy(*policy);
363 srtp_do_timing(*policy);
364 policy++;
365 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000366 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000367
jfigus67b9c732014-11-20 10:17:21 -0500368 if (do_rejection_test) {
369 const srtp_policy_t **policy = policy_array;
370
371 /* loop over policies, run rejection timing test for each */
372 while (*policy != NULL) {
373 srtp_print_policy(*policy);
374 srtp_do_rejection_timing(*policy);
375 policy++;
376 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000377 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000378
jfigus67b9c732014-11-20 10:17:21 -0500379 if (do_codec_timing) {
380 srtp_policy_t policy;
381 int ignore;
382 double mips = mips_estimate(1000000000, &ignore);
Cullen Jennings235513a2005-09-21 22:51:36 +0000383
Joachim Bauch99a74822015-11-17 00:08:19 +0100384 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -0500385 srtp_crypto_policy_set_rtp_default(&policy.rtp);
386 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
387 policy.ssrc.type = ssrc_specific;
388 policy.ssrc.value = 0xdecafbad;
389 policy.key = test_key;
390 policy.ekt = NULL;
391 policy.window_size = 128;
392 policy.allow_repeat_tx = 0;
393 policy.next = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +0000394
jfigus67b9c732014-11-20 10:17:21 -0500395 printf("mips estimate: %e\n", mips);
Cullen Jennings235513a2005-09-21 22:51:36 +0000396
jfigus67b9c732014-11-20 10:17:21 -0500397 printf("testing srtp processing time for voice codecs:\n");
398 printf("codec\t\tlength (octets)\t\tsrtp instructions/second\n");
399 printf("G.711\t\t%d\t\t\t%e\n", 80,
400 (double)mips * (80 * 8) /
401 srtp_bits_per_second(80, &policy) / .01 );
402 printf("G.711\t\t%d\t\t\t%e\n", 160,
403 (double)mips * (160 * 8) /
404 srtp_bits_per_second(160, &policy) / .02);
405 printf("G.726-32\t%d\t\t\t%e\n", 40,
406 (double)mips * (40 * 8) /
407 srtp_bits_per_second(40, &policy) / .01 );
408 printf("G.726-32\t%d\t\t\t%e\n", 80,
409 (double)mips * (80 * 8) /
410 srtp_bits_per_second(80, &policy) / .02);
411 printf("G.729\t\t%d\t\t\t%e\n", 10,
412 (double)mips * (10 * 8) /
413 srtp_bits_per_second(10, &policy) / .01 );
414 printf("G.729\t\t%d\t\t\t%e\n", 20,
415 (double)mips * (20 * 8) /
416 srtp_bits_per_second(20, &policy) / .02 );
417 printf("Wideband\t%d\t\t\t%e\n", 320,
418 (double)mips * (320 * 8) /
419 srtp_bits_per_second(320, &policy) / .01 );
420 printf("Wideband\t%d\t\t\t%e\n", 640,
421 (double)mips * (640 * 8) /
422 srtp_bits_per_second(640, &policy) / .02 );
423 }
Jonathan Lennoxdfb30842010-05-15 04:52:01 +0000424
jfigus67b9c732014-11-20 10:17:21 -0500425 status = srtp_shutdown();
426 if (status) {
427 printf("error: srtp shutdown failed with error code %d\n", status);
428 exit(1);
429 }
430
431 return 0;
Cullen Jennings235513a2005-09-21 22:51:36 +0000432}
433
434
435
436/*
437 * srtp_create_test_packet(len, ssrc) returns a pointer to a
438 * (malloced) example RTP packet whose data field has the length given
439 * by pkt_octet_len and the SSRC value ssrc. The total length of the
440 * packet is twelve octets longer, since the header is at the
441 * beginning. There is room at the end of the packet for a trailer,
442 * and the four octets following the packet are filled with 0xff
443 * values to enable testing for overwrites.
444 *
445 * note that the location of the test packet can (and should) be
446 * deallocated with the free() call once it is no longer needed.
447 */
448
449srtp_hdr_t *
jfigus67b9c732014-11-20 10:17:21 -0500450srtp_create_test_packet (int pkt_octet_len, uint32_t ssrc)
451{
452 int i;
453 uint8_t *buffer;
454 srtp_hdr_t *hdr;
455 int bytes_in_hdr = 12;
Cullen Jennings235513a2005-09-21 22:51:36 +0000456
jfigus67b9c732014-11-20 10:17:21 -0500457 /* allocate memory for test packet */
458 hdr = (srtp_hdr_t*)malloc(pkt_octet_len + bytes_in_hdr
459 + SRTP_MAX_TRAILER_LEN + 4);
460 if (!hdr) {
461 return NULL;
462 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000463
jfigus67b9c732014-11-20 10:17:21 -0500464 hdr->version = 2; /* RTP version two */
465 hdr->p = 0; /* no padding needed */
466 hdr->x = 0; /* no header extension */
467 hdr->cc = 0; /* no CSRCs */
468 hdr->m = 0; /* marker bit */
469 hdr->pt = 0xf; /* payload type */
470 hdr->seq = htons(0x1234); /* sequence number */
471 hdr->ts = htonl(0xdecafbad); /* timestamp */
472 hdr->ssrc = htonl(ssrc); /* synch. source */
Cullen Jennings235513a2005-09-21 22:51:36 +0000473
jfigus67b9c732014-11-20 10:17:21 -0500474 buffer = (uint8_t*)hdr;
475 buffer += bytes_in_hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +0000476
jfigus67b9c732014-11-20 10:17:21 -0500477 /* set RTP data to 0xab */
478 for (i = 0; i < pkt_octet_len; i++) {
479 *buffer++ = 0xab;
480 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000481
jfigus67b9c732014-11-20 10:17:21 -0500482 /* set post-data value to 0xffff to enable overrun checking */
483 for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++) {
484 *buffer++ = 0xff;
485 }
486
487 return hdr;
Cullen Jennings235513a2005-09-21 22:51:36 +0000488}
489
Joachim Bauch99a74822015-11-17 00:08:19 +0100490srtp_hdr_t *
491srtp_create_test_packet_ext_hdr(int pkt_octet_len, uint32_t ssrc) {
492 int i;
493 uint8_t *buffer;
494 srtp_hdr_t *hdr;
495 int bytes_in_hdr = 12;
496 uint8_t extension_header[12] = {
497 /* one-byte header */
498 0xbe, 0xde,
499 /* size */
500 0x00, 0x02,
501 /* id 1, length 1 (i.e. 2 bytes) */
502 0x11,
503 /* payload */
504 0xca,
505 0xfe,
506 /* padding */
507 0x00,
508 /* id 2, length 0 (i.e. 1 byte) */
509 0x20,
510 /* payload */
511 0xba,
512 /* padding */
513 0x00,
514 0x00
515 };
516
517 /* allocate memory for test packet */
518 hdr = (srtp_hdr_t*) malloc(pkt_octet_len + bytes_in_hdr
519 + sizeof(extension_header) + SRTP_MAX_TRAILER_LEN + 4);
520 if (!hdr)
521 return NULL;
522
523 hdr->version = 2; /* RTP version two */
524 hdr->p = 0; /* no padding needed */
525 hdr->x = 1; /* no header extension */
526 hdr->cc = 0; /* no CSRCs */
527 hdr->m = 0; /* marker bit */
528 hdr->pt = 0xf; /* payload type */
529 hdr->seq = htons(0x1234); /* sequence number */
530 hdr->ts = htonl(0xdecafbad); /* timestamp */
531 hdr->ssrc = htonl(ssrc); /* synch. source */
532
533 buffer = (uint8_t *)hdr;
534 buffer += bytes_in_hdr;
535
536 memcpy(buffer, extension_header, sizeof(extension_header));
537 buffer += sizeof(extension_header);
538
539 /* set RTP data to 0xab */
540 for (i=0; i < pkt_octet_len; i++)
541 *buffer++ = 0xab;
542
543 /* set post-data value to 0xffff to enable overrun checking */
544 for (i=0; i < SRTP_MAX_TRAILER_LEN+4; i++)
545 *buffer++ = 0xff;
546
547 return hdr;
548}
549
Cullen Jennings235513a2005-09-21 22:51:36 +0000550void
jfigus67b9c732014-11-20 10:17:21 -0500551srtp_do_timing (const srtp_policy_t *policy)
552{
553 int len;
Cullen Jennings235513a2005-09-21 22:51:36 +0000554
jfigus67b9c732014-11-20 10:17:21 -0500555 /*
556 * note: the output of this function is formatted so that it
557 * can be used in gnuplot. '#' indicates a comment, and "\r\n"
558 * terminates a record
559 */
560
561 printf("# testing srtp throughput:\r\n");
562 printf("# mesg length (octets)\tthroughput (megabits per second)\r\n");
563
564 for (len = 16; len <= 2048; len *= 2) {
565 printf("%d\t\t\t%f\r\n", len,
566 srtp_bits_per_second(len, policy) / 1.0E6);
567 }
568
569 /* these extra linefeeds let gnuplot know that a dataset is done */
570 printf("\r\n\r\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000571
572}
573
574void
jfigus67b9c732014-11-20 10:17:21 -0500575srtp_do_rejection_timing (const srtp_policy_t *policy)
576{
577 int len;
Cullen Jennings235513a2005-09-21 22:51:36 +0000578
jfigus67b9c732014-11-20 10:17:21 -0500579 /*
580 * note: the output of this function is formatted so that it
581 * can be used in gnuplot. '#' indicates a comment, and "\r\n"
582 * terminates a record
583 */
584
585 printf("# testing srtp rejection throughput:\r\n");
586 printf("# mesg length (octets)\trejections per second\r\n");
587
588 for (len = 8; len <= 2048; len *= 2) {
589 printf("%d\t\t\t%e\r\n", len, srtp_rejections_per_second(len, policy));
590 }
591
592 /* these extra linefeeds let gnuplot know that a dataset is done */
593 printf("\r\n\r\n");
Cullen Jennings235513a2005-09-21 22:51:36 +0000594
595}
596
597
598#define MAX_MSG_LEN 1024
599
600double
jfigus67b9c732014-11-20 10:17:21 -0500601srtp_bits_per_second (int msg_len_octets, const srtp_policy_t *policy)
602{
603 srtp_t srtp;
604 srtp_hdr_t *mesg;
605 int i;
606 clock_t timer;
607 int num_trials = 100000;
608 int len;
609 uint32_t ssrc;
610 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000611
jfigus67b9c732014-11-20 10:17:21 -0500612 /*
613 * allocate and initialize an srtp session
614 */
615 status = srtp_create(&srtp, policy);
Cullen Jennings235513a2005-09-21 22:51:36 +0000616 if (status) {
jfigus67b9c732014-11-20 10:17:21 -0500617 printf("error: srtp_create() failed with error code %d\n", status);
618 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000619 }
620
jfigus67b9c732014-11-20 10:17:21 -0500621 /*
622 * if the ssrc is unspecified, use a predetermined one
623 */
624 if (policy->ssrc.type != ssrc_specific) {
625 ssrc = 0xdeadbeef;
626 } else {
627 ssrc = policy->ssrc.value;
Jonathan Lennox75b36872010-05-21 00:30:21 +0000628 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000629
jfigus67b9c732014-11-20 10:17:21 -0500630 /*
631 * create a test packet
632 */
633 mesg = srtp_create_test_packet(msg_len_octets, ssrc);
634 if (mesg == NULL) {
635 return 0.0; /* indicate failure by returning zero */
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000636
jfigus67b9c732014-11-20 10:17:21 -0500637 }
638 timer = clock();
639 for (i = 0; i < num_trials; i++) {
640 len = msg_len_octets + 12; /* add in rtp header length */
641
642 /* srtp protect message */
643 status = srtp_protect(srtp, mesg, &len);
644 if (status) {
645 printf("error: srtp_protect() failed with error code %d\n", status);
646 exit(1);
647 }
648
649 /* increment message number */
650 {
651 /* hack sequence to avoid problems with macros for htons/ntohs on some systems */
652 short new_seq = ntohs(mesg->seq) + 1;
653 mesg->seq = htons(new_seq);
654 }
655 }
656 timer = clock() - timer;
657
658 free(mesg);
659
660 status = srtp_dealloc(srtp);
661 if (status) {
662 printf("error: srtp_dealloc() failed with error code %d\n", status);
663 exit(1);
664 }
665
666 return (double)(msg_len_octets) * 8 *
667 num_trials * CLOCKS_PER_SEC / timer;
Cullen Jennings235513a2005-09-21 22:51:36 +0000668}
669
670double
jfigus67b9c732014-11-20 10:17:21 -0500671srtp_rejections_per_second (int msg_len_octets, const srtp_policy_t *policy)
672{
673 srtp_ctx_t *srtp;
674 srtp_hdr_t *mesg;
675 int i;
676 int len;
677 clock_t timer;
678 int num_trials = 1000000;
679 uint32_t ssrc = policy->ssrc.value;
680 srtp_err_status_t status;
Cullen Jennings235513a2005-09-21 22:51:36 +0000681
jfigus67b9c732014-11-20 10:17:21 -0500682 /*
683 * allocate and initialize an srtp session
684 */
685 status = srtp_create(&srtp, policy);
686 if (status) {
687 printf("error: srtp_create() failed with error code %d\n", status);
688 exit(1);
689 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000690
jfigus67b9c732014-11-20 10:17:21 -0500691 mesg = srtp_create_test_packet(msg_len_octets, ssrc);
692 if (mesg == NULL) {
693 return 0.0; /* indicate failure by returning zero */
694
695 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000696 len = msg_len_octets;
jfigus67b9c732014-11-20 10:17:21 -0500697 srtp_protect(srtp, (srtp_hdr_t*)mesg, &len);
Cullen Jennings235513a2005-09-21 22:51:36 +0000698
jfigus67b9c732014-11-20 10:17:21 -0500699 timer = clock();
700 for (i = 0; i < num_trials; i++) {
701 len = msg_len_octets;
702 srtp_unprotect(srtp, (srtp_hdr_t*)mesg, &len);
703 }
704 timer = clock() - timer;
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000705
jfigus67b9c732014-11-20 10:17:21 -0500706 free(mesg);
Jonathan Lennox80c4c832010-05-17 19:30:28 +0000707
jfigus67b9c732014-11-20 10:17:21 -0500708 status = srtp_dealloc(srtp);
709 if (status) {
710 printf("error: srtp_dealloc() failed with error code %d\n", status);
711 exit(1);
712 }
713
714 return (double)num_trials * CLOCKS_PER_SEC / timer;
Cullen Jennings235513a2005-09-21 22:51:36 +0000715}
716
717
718void
jfigus67b9c732014-11-20 10:17:21 -0500719err_check (srtp_err_status_t s)
720{
721 if (s == srtp_err_status_ok) {
722 return;
723 } else{
724 fprintf(stderr, "error: unexpected srtp failure (code %d)\n", s);
725 }
726 exit(1);
Cullen Jennings235513a2005-09-21 22:51:36 +0000727}
728
jfigus857009c2014-11-05 11:17:43 -0500729srtp_err_status_t
Joachim Bauch99a74822015-11-17 00:08:19 +0100730srtp_test (const srtp_policy_t *policy, int extension_header)
jfigus67b9c732014-11-20 10:17:21 -0500731{
732 int i;
733 srtp_t srtp_sender;
734 srtp_t srtp_rcvr;
735 srtp_err_status_t status = srtp_err_status_ok;
736 srtp_hdr_t *hdr, *hdr2;
737 uint8_t hdr_enc[64];
738 uint8_t *pkt_end;
739 int msg_len_octets, msg_len_enc;
740 int len;
741 int tag_length = policy->rtp.auth_tag_len;
742 uint32_t ssrc;
743 srtp_policy_t *rcvr_policy;
Joachim Bauch99a74822015-11-17 00:08:19 +0100744 srtp_policy_t tmp_policy;
745 int header = 1;
Cullen Jennings235513a2005-09-21 22:51:36 +0000746
Joachim Bauch99a74822015-11-17 00:08:19 +0100747 if (extension_header) {
748 memcpy(&tmp_policy, policy, sizeof(srtp_policy_t));
749 tmp_policy.enc_xtn_hdr = &header;
750 tmp_policy.enc_xtn_hdr_count = 1;
751 err_check(srtp_create(&srtp_sender, &tmp_policy));
752 } else {
753 err_check(srtp_create(&srtp_sender, policy));
754 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000755
jfigus67b9c732014-11-20 10:17:21 -0500756 /* print out policy */
757 err_check(srtp_session_print_policy(srtp_sender));
Cullen Jennings235513a2005-09-21 22:51:36 +0000758
jfigus67b9c732014-11-20 10:17:21 -0500759 /*
760 * initialize data buffer, using the ssrc in the policy unless that
761 * value is a wildcard, in which case we'll just use an arbitrary
762 * one
763 */
764 if (policy->ssrc.type != ssrc_specific) {
765 ssrc = 0xdecafbad;
766 } else{
767 ssrc = policy->ssrc.value;
Cullen Jennings235513a2005-09-21 22:51:36 +0000768 }
jfigus67b9c732014-11-20 10:17:21 -0500769 msg_len_octets = 28;
Joachim Bauch99a74822015-11-17 00:08:19 +0100770 if (extension_header) {
771 hdr = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc);
772 hdr2 = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc);
773 } else {
774 hdr = srtp_create_test_packet(msg_len_octets, ssrc);
775 hdr2 = srtp_create_test_packet(msg_len_octets, ssrc);
776 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000777
jfigus67b9c732014-11-20 10:17:21 -0500778 if (hdr == NULL) {
Joachim Bauch99a74822015-11-17 00:08:19 +0100779 free(hdr2);
jfigus67b9c732014-11-20 10:17:21 -0500780 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000781 }
jfigus67b9c732014-11-20 10:17:21 -0500782 if (hdr2 == NULL) {
783 free(hdr);
784 return srtp_err_status_alloc_fail;
Cullen Jennings235513a2005-09-21 22:51:36 +0000785 }
786
Marcus Sundberg5c40da82005-10-08 18:15:00 +0000787 /* set message length */
788 len = msg_len_octets;
Joachim Bauch99a74822015-11-17 00:08:19 +0100789 if (extension_header) {
790 len += 12;
791 }
Marcus Sundberg5c40da82005-10-08 18:15:00 +0000792
jfigus67b9c732014-11-20 10:17:21 -0500793 debug_print(mod_driver, "before protection:\n%s",
794 srtp_packet_to_string(hdr, len));
795
796#if PRINT_REFERENCE_PACKET
797 debug_print(mod_driver, "reference packet before protection:\n%s",
798 octet_string_hex_string((uint8_t*)hdr, len));
799#endif
Cullen Jennings235513a2005-09-21 22:51:36 +0000800 err_check(srtp_protect(srtp_sender, hdr, &len));
Cullen Jennings235513a2005-09-21 22:51:36 +0000801
jfigus67b9c732014-11-20 10:17:21 -0500802 debug_print(mod_driver, "after protection:\n%s",
803 srtp_packet_to_string(hdr, len));
804#if PRINT_REFERENCE_PACKET
805 debug_print(mod_driver, "after protection:\n%s",
806 octet_string_hex_string((uint8_t*)hdr, len));
807#endif
808
809 /* save protected message and length */
810 memcpy(hdr_enc, hdr, len);
811 msg_len_enc = len;
812
813 /*
814 * check for overrun of the srtp_protect() function
815 *
816 * The packet is followed by a value of 0xfffff; if the value of the
817 * data following the packet is different, then we know that the
818 * protect function is overwriting the end of the packet.
819 */
820 pkt_end = (uint8_t*)hdr + sizeof(srtp_hdr_t)
821 + msg_len_octets + tag_length;
Joachim Bauch99a74822015-11-17 00:08:19 +0100822 if (extension_header) {
823 pkt_end += 12;
824 }
jfigus67b9c732014-11-20 10:17:21 -0500825 for (i = 0; i < 4; i++) {
826 if (pkt_end[i] != 0xff) {
827 fprintf(stdout, "overwrite in srtp_protect() function "
828 "(expected %x, found %x in trailing octet %d)\n",
829 0xff, ((uint8_t*)hdr)[i], i);
830 free(hdr);
831 free(hdr2);
832 return srtp_err_status_algo_fail;
833 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000834 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000835
jfigus67b9c732014-11-20 10:17:21 -0500836 /*
837 * if the policy includes confidentiality, check that ciphertext is
838 * different than plaintext
839 *
840 * Note that this check will give false negatives, with some small
841 * probability, especially if the packets are short. For that
842 * reason, we skip this check if the plaintext is less than four
843 * octets long.
844 */
845 if ((policy->rtp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
846 printf("testing that ciphertext is distinct from plaintext...");
847 status = srtp_err_status_algo_fail;
848 for (i = 12; i < msg_len_octets + 12; i++) {
849 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
850 status = srtp_err_status_ok;
851 }
852 }
853 if (status) {
854 printf("failed\n");
855 free(hdr);
856 free(hdr2);
857 return status;
858 }
859 printf("passed\n");
860 }
Cullen Jennings235513a2005-09-21 22:51:36 +0000861
jfigus67b9c732014-11-20 10:17:21 -0500862 /*
863 * if the policy uses a 'wildcard' ssrc, then we need to make a copy
864 * of the policy that changes the direction to inbound
865 *
866 * we always copy the policy into the rcvr_policy, since otherwise
867 * the compiler would fret about the constness of the policy
868 */
869 rcvr_policy = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
870 if (rcvr_policy == NULL) {
871 free(hdr);
872 free(hdr2);
873 return srtp_err_status_alloc_fail;
874 }
Joachim Bauch99a74822015-11-17 00:08:19 +0100875 if (extension_header) {
876 memcpy(rcvr_policy, &tmp_policy, sizeof(srtp_policy_t));
877 if (tmp_policy.ssrc.type == ssrc_any_outbound) {
878 rcvr_policy->ssrc.type = ssrc_any_inbound;
879 }
880 } else {
881 memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
882 if (policy->ssrc.type == ssrc_any_outbound) {
883 rcvr_policy->ssrc.type = ssrc_any_inbound;
884 }
jfigus67b9c732014-11-20 10:17:21 -0500885 }
886
887 err_check(srtp_create(&srtp_rcvr, rcvr_policy));
888
889 err_check(srtp_unprotect(srtp_rcvr, hdr, &len));
890
891 debug_print(mod_driver, "after unprotection:\n%s",
892 srtp_packet_to_string(hdr, len));
893
894 /* verify that the unprotected packet matches the origial one */
895 for (i = 0; i < msg_len_octets; i++) {
896 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
897 fprintf(stdout, "mismatch at octet %d\n", i);
898 status = srtp_err_status_algo_fail;
899 }
900 }
901 if (status) {
902 free(hdr);
903 free(hdr2);
904 free(rcvr_policy);
905 return status;
906 }
907
908 /*
909 * if the policy includes authentication, then test for false positives
910 */
911 if (policy->rtp.sec_serv & sec_serv_auth) {
912 char *data = ((char*)hdr) + 12;
913
914 printf("testing for false positives in replay check...");
915
916 /* set message length */
917 len = msg_len_enc;
918
919 /* unprotect a second time - should fail with a replay error */
920 status = srtp_unprotect(srtp_rcvr, hdr_enc, &len);
921 if (status != srtp_err_status_replay_fail) {
922 printf("failed with error code %d\n", status);
923 free(hdr);
924 free(hdr2);
925 free(rcvr_policy);
926 return status;
927 } else {
928 printf("passed\n");
929 }
930
931 printf("testing for false positives in auth check...");
932
933 /* increment sequence number in header */
934 hdr->seq++;
935
936 /* set message length */
937 len = msg_len_octets;
Joachim Bauch99a74822015-11-17 00:08:19 +0100938 if (extension_header) {
939 len += 12;
940 }
jfigus67b9c732014-11-20 10:17:21 -0500941
942 /* apply protection */
943 err_check(srtp_protect(srtp_sender, hdr, &len));
944
945 /* flip bits in packet */
946 data[0] ^= 0xff;
947
948 /* unprotect, and check for authentication failure */
949 status = srtp_unprotect(srtp_rcvr, hdr, &len);
950 if (status != srtp_err_status_auth_fail) {
951 printf("failed\n");
952 free(hdr);
953 free(hdr2);
954 free(rcvr_policy);
955 return status;
956 } else {
957 printf("passed\n");
958 }
959
960 }
961
962 err_check(srtp_dealloc(srtp_sender));
963 err_check(srtp_dealloc(srtp_rcvr));
964
965 free(hdr);
966 free(hdr2);
967 free(rcvr_policy);
968 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +0000969}
970
971
jfigus857009c2014-11-05 11:17:43 -0500972srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -0500973srtcp_test (const srtp_policy_t *policy)
974{
975 int i;
976 srtp_t srtcp_sender;
977 srtp_t srtcp_rcvr;
978 srtp_err_status_t status = srtp_err_status_ok;
979 srtp_hdr_t *hdr, *hdr2;
980 uint8_t hdr_enc[64];
981 uint8_t *pkt_end;
982 int msg_len_octets, msg_len_enc;
983 int len;
984 int tag_length = policy->rtp.auth_tag_len;
985 uint32_t ssrc;
986 srtp_policy_t *rcvr_policy;
David McGrew9c70f292006-05-03 19:38:38 +0000987
jfigus67b9c732014-11-20 10:17:21 -0500988 err_check(srtp_create(&srtcp_sender, policy));
David McGrew9c70f292006-05-03 19:38:38 +0000989
jfigus67b9c732014-11-20 10:17:21 -0500990 /* print out policy */
991 err_check(srtp_session_print_policy(srtcp_sender));
David McGrew9c70f292006-05-03 19:38:38 +0000992
jfigus67b9c732014-11-20 10:17:21 -0500993 /*
994 * initialize data buffer, using the ssrc in the policy unless that
995 * value is a wildcard, in which case we'll just use an arbitrary
996 * one
997 */
998 if (policy->ssrc.type != ssrc_specific) {
999 ssrc = 0xdecafbad;
1000 } else{
1001 ssrc = policy->ssrc.value;
David McGrew9c70f292006-05-03 19:38:38 +00001002 }
jfigus67b9c732014-11-20 10:17:21 -05001003 msg_len_octets = 28;
1004 hdr = srtp_create_test_packet(msg_len_octets, ssrc);
David McGrew9c70f292006-05-03 19:38:38 +00001005
jfigus67b9c732014-11-20 10:17:21 -05001006 if (hdr == NULL) {
1007 return srtp_err_status_alloc_fail;
David McGrew9c70f292006-05-03 19:38:38 +00001008 }
jfigus67b9c732014-11-20 10:17:21 -05001009 hdr2 = srtp_create_test_packet(msg_len_octets, ssrc);
1010 if (hdr2 == NULL) {
1011 free(hdr);
1012 return srtp_err_status_alloc_fail;
David McGrew9c70f292006-05-03 19:38:38 +00001013 }
1014
David McGrew9c70f292006-05-03 19:38:38 +00001015 /* set message length */
1016 len = msg_len_octets;
1017
jfigus67b9c732014-11-20 10:17:21 -05001018 debug_print(mod_driver, "before protection:\n%s",
1019 srtp_packet_to_string(hdr, len));
1020
1021#if PRINT_REFERENCE_PACKET
1022 debug_print(mod_driver, "reference packet before protection:\n%s",
1023 octet_string_hex_string((uint8_t*)hdr, len));
1024#endif
David McGrew9c70f292006-05-03 19:38:38 +00001025 err_check(srtp_protect_rtcp(srtcp_sender, hdr, &len));
David McGrew9c70f292006-05-03 19:38:38 +00001026
jfigus67b9c732014-11-20 10:17:21 -05001027 debug_print(mod_driver, "after protection:\n%s",
1028 srtp_packet_to_string(hdr, len));
1029#if PRINT_REFERENCE_PACKET
1030 debug_print(mod_driver, "after protection:\n%s",
1031 octet_string_hex_string((uint8_t*)hdr, len));
1032#endif
1033
1034 /* save protected message and length */
1035 memcpy(hdr_enc, hdr, len);
1036 msg_len_enc = len;
1037
1038 /*
1039 * check for overrun of the srtp_protect() function
1040 *
1041 * The packet is followed by a value of 0xfffff; if the value of the
1042 * data following the packet is different, then we know that the
1043 * protect function is overwriting the end of the packet.
1044 */
1045 pkt_end = (uint8_t*)hdr + sizeof(srtp_hdr_t)
1046 + msg_len_octets + tag_length;
1047 for (i = 0; i < 4; i++) {
1048 if (pkt_end[i] != 0xff) {
1049 fprintf(stdout, "overwrite in srtp_protect_rtcp() function "
1050 "(expected %x, found %x in trailing octet %d)\n",
1051 0xff, ((uint8_t*)hdr)[i], i);
1052 free(hdr);
1053 free(hdr2);
1054 return srtp_err_status_algo_fail;
1055 }
David McGrew9c70f292006-05-03 19:38:38 +00001056 }
David McGrew9c70f292006-05-03 19:38:38 +00001057
jfigus67b9c732014-11-20 10:17:21 -05001058 /*
1059 * if the policy includes confidentiality, check that ciphertext is
1060 * different than plaintext
1061 *
1062 * Note that this check will give false negatives, with some small
1063 * probability, especially if the packets are short. For that
1064 * reason, we skip this check if the plaintext is less than four
1065 * octets long.
1066 */
1067 if ((policy->rtp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
1068 printf("testing that ciphertext is distinct from plaintext...");
1069 status = srtp_err_status_algo_fail;
1070 for (i = 12; i < msg_len_octets + 12; i++) {
1071 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
1072 status = srtp_err_status_ok;
1073 }
1074 }
1075 if (status) {
1076 printf("failed\n");
1077 free(hdr);
1078 free(hdr2);
1079 return status;
1080 }
1081 printf("passed\n");
1082 }
David McGrew9c70f292006-05-03 19:38:38 +00001083
jfigus67b9c732014-11-20 10:17:21 -05001084 /*
1085 * if the policy uses a 'wildcard' ssrc, then we need to make a copy
1086 * of the policy that changes the direction to inbound
1087 *
1088 * we always copy the policy into the rcvr_policy, since otherwise
1089 * the compiler would fret about the constness of the policy
1090 */
1091 rcvr_policy = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
1092 if (rcvr_policy == NULL) {
1093 return srtp_err_status_alloc_fail;
1094 }
1095 memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
1096 if (policy->ssrc.type == ssrc_any_outbound) {
1097 rcvr_policy->ssrc.type = ssrc_any_inbound;
1098 }
1099
1100 err_check(srtp_create(&srtcp_rcvr, rcvr_policy));
1101
1102 err_check(srtp_unprotect_rtcp(srtcp_rcvr, hdr, &len));
1103
1104 debug_print(mod_driver, "after unprotection:\n%s",
1105 srtp_packet_to_string(hdr, len));
1106
1107 /* verify that the unprotected packet matches the origial one */
1108 for (i = 0; i < msg_len_octets; i++) {
1109 if (((uint8_t*)hdr)[i] != ((uint8_t*)hdr2)[i]) {
1110 fprintf(stdout, "mismatch at octet %d\n", i);
1111 status = srtp_err_status_algo_fail;
1112 }
1113 }
1114 if (status) {
1115 free(hdr);
1116 free(hdr2);
1117 free(rcvr_policy);
1118 return status;
1119 }
1120
1121 /*
1122 * if the policy includes authentication, then test for false positives
1123 */
1124 if (policy->rtp.sec_serv & sec_serv_auth) {
1125 char *data = ((char*)hdr) + 12;
1126
1127 printf("testing for false positives in replay check...");
1128
1129 /* set message length */
1130 len = msg_len_enc;
1131
1132 /* unprotect a second time - should fail with a replay error */
1133 status = srtp_unprotect_rtcp(srtcp_rcvr, hdr_enc, &len);
1134 if (status != srtp_err_status_replay_fail) {
1135 printf("failed with error code %d\n", status);
1136 free(hdr);
1137 free(hdr2);
1138 free(rcvr_policy);
1139 return status;
1140 } else {
1141 printf("passed\n");
1142 }
1143
1144 printf("testing for false positives in auth check...");
1145
1146 /* increment sequence number in header */
1147 hdr->seq++;
1148
1149 /* set message length */
1150 len = msg_len_octets;
1151
1152 /* apply protection */
1153 err_check(srtp_protect_rtcp(srtcp_sender, hdr, &len));
1154
1155 /* flip bits in packet */
1156 data[0] ^= 0xff;
1157
1158 /* unprotect, and check for authentication failure */
1159 status = srtp_unprotect_rtcp(srtcp_rcvr, hdr, &len);
1160 if (status != srtp_err_status_auth_fail) {
1161 printf("failed\n");
1162 free(hdr);
1163 free(hdr2);
1164 free(rcvr_policy);
1165 return status;
1166 } else {
1167 printf("passed\n");
1168 }
1169
1170 }
1171
1172 err_check(srtp_dealloc(srtcp_sender));
1173 err_check(srtp_dealloc(srtcp_rcvr));
1174
1175 free(hdr);
1176 free(hdr2);
1177 free(rcvr_policy);
1178 return srtp_err_status_ok;
David McGrew9c70f292006-05-03 19:38:38 +00001179}
1180
1181
jfigus857009c2014-11-05 11:17:43 -05001182srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001183srtp_session_print_policy (srtp_t srtp)
1184{
1185 char *serv_descr[4] = {
1186 "none",
1187 "confidentiality",
1188 "authentication",
1189 "confidentiality and authentication"
1190 };
1191 char *direction[3] = {
1192 "unknown",
1193 "outbound",
1194 "inbound"
1195 };
1196 srtp_stream_t stream;
Cullen Jennings235513a2005-09-21 22:51:36 +00001197
jfigus67b9c732014-11-20 10:17:21 -05001198 /* sanity checking */
1199 if (srtp == NULL) {
1200 return srtp_err_status_fail;
1201 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001202
jfigus67b9c732014-11-20 10:17:21 -05001203 /* if there's a template stream, print it out */
1204 if (srtp->stream_template != NULL) {
1205 stream = srtp->stream_template;
1206 printf("# SSRC: any %s\r\n"
1207 "# rtp cipher: %s\r\n"
1208 "# rtp auth: %s\r\n"
1209 "# rtp services: %s\r\n"
1210 "# rtcp cipher: %s\r\n"
1211 "# rtcp auth: %s\r\n"
1212 "# rtcp services: %s\r\n"
1213 "# window size: %lu\r\n"
1214 "# tx rtx allowed:%s\r\n",
1215 direction[stream->direction],
1216 stream->rtp_cipher->type->description,
1217 stream->rtp_auth->type->description,
1218 serv_descr[stream->rtp_services],
1219 stream->rtcp_cipher->type->description,
1220 stream->rtcp_auth->type->description,
1221 serv_descr[stream->rtcp_services],
jfigusde8deb32014-11-25 12:58:11 -05001222 srtp_rdbx_get_window_size(&stream->rtp_rdbx),
jfigus67b9c732014-11-20 10:17:21 -05001223 stream->allow_repeat_tx ? "true" : "false");
Joachim Bauch99a74822015-11-17 00:08:19 +01001224
1225 printf("# Encrypted extension headers: ");
1226 if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
1227 int* enc_xtn_hdr = stream->enc_xtn_hdr;
1228 int count = stream->enc_xtn_hdr_count;
1229 while (count > 0) {
1230 printf("%d ", *enc_xtn_hdr);
1231 enc_xtn_hdr++;
1232 count--;
1233 }
1234 printf("\n");
1235 } else {
1236 printf("none\n");
1237 }
jfigus67b9c732014-11-20 10:17:21 -05001238 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001239
jfigus67b9c732014-11-20 10:17:21 -05001240 /* loop over streams in session, printing the policy of each */
1241 stream = srtp->stream_list;
1242 while (stream != NULL) {
1243 if (stream->rtp_services > sec_serv_conf_and_auth) {
1244 return srtp_err_status_bad_param;
1245 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001246
jfigus67b9c732014-11-20 10:17:21 -05001247 printf("# SSRC: 0x%08x\r\n"
1248 "# rtp cipher: %s\r\n"
1249 "# rtp auth: %s\r\n"
1250 "# rtp services: %s\r\n"
1251 "# rtcp cipher: %s\r\n"
1252 "# rtcp auth: %s\r\n"
1253 "# rtcp services: %s\r\n"
1254 "# window size: %lu\r\n"
1255 "# tx rtx allowed:%s\r\n",
1256 stream->ssrc,
1257 stream->rtp_cipher->type->description,
1258 stream->rtp_auth->type->description,
1259 serv_descr[stream->rtp_services],
1260 stream->rtcp_cipher->type->description,
1261 stream->rtcp_auth->type->description,
1262 serv_descr[stream->rtcp_services],
jfigusde8deb32014-11-25 12:58:11 -05001263 srtp_rdbx_get_window_size(&stream->rtp_rdbx),
jfigus67b9c732014-11-20 10:17:21 -05001264 stream->allow_repeat_tx ? "true" : "false");
1265
Joachim Bauch99a74822015-11-17 00:08:19 +01001266 printf("# Encrypted extension headers: ");
1267 if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
1268 int* enc_xtn_hdr = stream->enc_xtn_hdr;
1269 int count = stream->enc_xtn_hdr_count;
1270 while (count > 0) {
1271 printf("%d ", *enc_xtn_hdr);
1272 enc_xtn_hdr++;
1273 count--;
1274 }
1275 printf("\n");
1276 } else {
1277 printf("none\n");
1278 }
1279
jfigus67b9c732014-11-20 10:17:21 -05001280 /* advance to next stream in the list */
1281 stream = stream->next;
1282 }
1283 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001284}
1285
jfigus857009c2014-11-05 11:17:43 -05001286srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001287srtp_print_policy (const srtp_policy_t *policy)
1288{
1289 srtp_err_status_t status;
1290 srtp_t session;
Cullen Jennings235513a2005-09-21 22:51:36 +00001291
jfigus67b9c732014-11-20 10:17:21 -05001292 status = srtp_create(&session, policy);
1293 if (status) {
1294 return status;
1295 }
1296 status = srtp_session_print_policy(session);
1297 if (status) {
1298 return status;
1299 }
1300 status = srtp_dealloc(session);
1301 if (status) {
1302 return status;
1303 }
1304 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001305}
1306
jfigus67b9c732014-11-20 10:17:21 -05001307/*
1308 * srtp_print_packet(...) is for debugging only
Cullen Jennings235513a2005-09-21 22:51:36 +00001309 * it prints an RTP packet to the stdout
1310 *
1311 * note that this function is *not* threadsafe
1312 */
1313
1314#include <stdio.h>
1315
1316#define MTU 2048
1317
1318char packet_string[MTU];
1319
1320char *
jfigus67b9c732014-11-20 10:17:21 -05001321srtp_packet_to_string (srtp_hdr_t *hdr, int pkt_octet_len)
1322{
1323 int octets_in_rtp_header = 12;
1324 uint8_t *data = ((uint8_t*)hdr) + octets_in_rtp_header;
1325 int hex_len = pkt_octet_len - octets_in_rtp_header;
Cullen Jennings235513a2005-09-21 22:51:36 +00001326
jfigus67b9c732014-11-20 10:17:21 -05001327 /* sanity checking */
1328 if ((hdr == NULL) || (pkt_octet_len > MTU)) {
1329 return NULL;
1330 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001331
jfigus67b9c732014-11-20 10:17:21 -05001332 /* write packet into string */
1333 sprintf(packet_string,
1334 "(s)rtp packet: {\n"
1335 " version:\t%d\n"
1336 " p:\t\t%d\n"
1337 " x:\t\t%d\n"
1338 " cc:\t\t%d\n"
1339 " m:\t\t%d\n"
1340 " pt:\t\t%x\n"
1341 " seq:\t\t%x\n"
1342 " ts:\t\t%x\n"
1343 " ssrc:\t%x\n"
1344 " data:\t%s\n"
1345 "} (%d octets in total)\n",
1346 hdr->version,
1347 hdr->p,
1348 hdr->x,
1349 hdr->cc,
1350 hdr->m,
1351 hdr->pt,
1352 hdr->seq,
1353 hdr->ts,
1354 hdr->ssrc,
1355 octet_string_hex_string(data, hex_len),
1356 pkt_octet_len);
Cullen Jennings235513a2005-09-21 22:51:36 +00001357
jfigus67b9c732014-11-20 10:17:21 -05001358 return packet_string;
Cullen Jennings235513a2005-09-21 22:51:36 +00001359}
1360
1361/*
1362 * mips_estimate() is a simple function to estimate the number of
1363 * instructions per second that the host can perform. note that this
1364 * function can be grossly wrong; you may want to have a manual sanity
1365 * check of its output!
1366 *
1367 * the 'ignore' pointer is there to convince the compiler to not just
1368 * optimize away the function
1369 */
1370
1371double
jfigus67b9c732014-11-20 10:17:21 -05001372mips_estimate (int num_trials, int *ignore)
1373{
1374 clock_t t;
1375 volatile int i, sum;
Cullen Jennings235513a2005-09-21 22:51:36 +00001376
jfigus67b9c732014-11-20 10:17:21 -05001377 sum = 0;
1378 t = clock();
1379 for (i = 0; i < num_trials; i++) {
1380 sum += i;
1381 }
1382 t = clock() - t;
Cullen Jennings235513a2005-09-21 22:51:36 +00001383
1384/* printf("%d\n", sum); */
jfigus67b9c732014-11-20 10:17:21 -05001385 *ignore = sum;
Cullen Jennings235513a2005-09-21 22:51:36 +00001386
jfigus67b9c732014-11-20 10:17:21 -05001387 return (double)num_trials * CLOCKS_PER_SEC / t;
Cullen Jennings235513a2005-09-21 22:51:36 +00001388}
1389
1390
1391/*
1392 * srtp_validate() verifies the correctness of libsrtp by comparing
1393 * some computed packets against some pre-computed reference values.
1394 * These packets were made with the default SRTP policy.
1395 */
1396
1397
jfigus857009c2014-11-05 11:17:43 -05001398srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001399srtp_validate ()
1400{
1401 uint8_t srtp_plaintext_ref[28] = {
1402 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1403 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1404 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1405 0xab, 0xab, 0xab, 0xab
1406 };
1407 uint8_t srtp_plaintext[38] = {
1408 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1409 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1410 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1411 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
1412 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1413 };
1414 uint8_t srtp_ciphertext[38] = {
1415 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1416 0xca, 0xfe, 0xba, 0xbe, 0x4e, 0x55, 0xdc, 0x4c,
1417 0xe7, 0x99, 0x78, 0xd8, 0x8c, 0xa4, 0xd2, 0x15,
1418 0x94, 0x9d, 0x24, 0x02, 0xb7, 0x8d, 0x6a, 0xcc,
1419 0x99, 0xea, 0x17, 0x9b, 0x8d, 0xbb
1420 };
1421 srtp_t srtp_snd, srtp_recv;
1422 srtp_err_status_t status;
1423 int len;
1424 srtp_policy_t policy;
Cullen Jennings235513a2005-09-21 22:51:36 +00001425
jfigus67b9c732014-11-20 10:17:21 -05001426 /*
1427 * create a session with a single stream using the default srtp
1428 * policy and with the SSRC value 0xcafebabe
1429 */
Joachim Bauch99a74822015-11-17 00:08:19 +01001430 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05001431 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1432 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1433 policy.ssrc.type = ssrc_specific;
1434 policy.ssrc.value = 0xcafebabe;
1435 policy.key = test_key;
1436 policy.ekt = NULL;
1437 policy.window_size = 128;
1438 policy.allow_repeat_tx = 0;
1439 policy.next = NULL;
Cullen Jennings235513a2005-09-21 22:51:36 +00001440
jfigus67b9c732014-11-20 10:17:21 -05001441 status = srtp_create(&srtp_snd, &policy);
1442 if (status) {
1443 return status;
1444 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001445
jfigus67b9c732014-11-20 10:17:21 -05001446 /*
1447 * protect plaintext, then compare with ciphertext
1448 */
1449 len = 28;
1450 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1451 if (status || (len != 38)) {
1452 return srtp_err_status_fail;
1453 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001454
jfigus67b9c732014-11-20 10:17:21 -05001455 debug_print(mod_driver, "ciphertext:\n %s",
1456 octet_string_hex_string(srtp_plaintext, len));
1457 debug_print(mod_driver, "ciphertext reference:\n %s",
1458 octet_string_hex_string(srtp_ciphertext, len));
Cullen Jennings235513a2005-09-21 22:51:36 +00001459
jfigus67b9c732014-11-20 10:17:21 -05001460 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
1461 return srtp_err_status_fail;
1462 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001463
jfigus67b9c732014-11-20 10:17:21 -05001464 /*
1465 * create a receiver session context comparable to the one created
1466 * above - we need to do this so that the replay checking doesn't
1467 * complain
1468 */
1469 status = srtp_create(&srtp_recv, &policy);
1470 if (status) {
1471 return status;
1472 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001473
jfigus67b9c732014-11-20 10:17:21 -05001474 /*
1475 * unprotect ciphertext, then compare with plaintext
1476 */
1477 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
1478 if (status || (len != 28)) {
1479 return status;
1480 }
1481
1482 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
1483 return srtp_err_status_fail;
1484 }
1485
1486 status = srtp_dealloc(srtp_snd);
1487 if (status) {
1488 return status;
1489 }
1490
1491 status = srtp_dealloc(srtp_recv);
1492 if (status) {
1493 return status;
1494 }
1495
1496 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001497}
1498
Joachim Bauch99a74822015-11-17 00:08:19 +01001499/*
1500 * Test vectors taken from RFC 6904, Appendix A
1501 */
1502srtp_err_status_t
1503srtp_validate_encrypted_extensions_headers() {
1504 unsigned char test_key_ext_headers[30] = {
1505 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
1506 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
1507 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
1508 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
1509 };
1510 uint8_t srtp_plaintext_ref[56] = {
1511 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1512 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1513 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1514 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1515 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1516 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1517 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab
1518 };
1519 uint8_t srtp_plaintext[66] = {
1520 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1521 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1522 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
1523 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
1524 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
1525 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1526 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1527 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1528 0x00, 0x00
1529 };
1530 uint8_t srtp_ciphertext[66] = {
1531 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1532 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
1533 0x17, 0x58, 0x8A, 0x92, 0x70, 0xF4, 0xE1, 0x5E,
1534 0x1C, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x95, 0x46,
1535 0xA9, 0x94, 0xF0, 0xBC, 0x54, 0x78, 0x97, 0x00,
1536 0x4e, 0x55, 0xdc, 0x4c, 0xe7, 0x99, 0x78, 0xd8,
1537 0x8c, 0xa4, 0xd2, 0x15, 0x94, 0x9d, 0x24, 0x02,
1538 0x5a, 0x46, 0xb3, 0xca, 0x35, 0xc5, 0x35, 0xa8,
1539 0x91, 0xc7
1540 };
1541 srtp_t srtp_snd, srtp_recv;
1542 srtp_err_status_t status;
1543 int len;
1544 srtp_policy_t policy;
1545 int headers[3] = {1, 3, 4};
1546
1547 /*
1548 * create a session with a single stream using the default srtp
1549 * policy and with the SSRC value 0xcafebabe
1550 */
1551 memset(&policy, 0, sizeof(policy));
1552 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1553 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1554 policy.ssrc.type = ssrc_specific;
1555 policy.ssrc.value = 0xcafebabe;
1556 policy.key = test_key_ext_headers;
1557 policy.ekt = NULL;
1558 policy.window_size = 128;
1559 policy.allow_repeat_tx = 0;
1560 policy.enc_xtn_hdr = headers;
1561 policy.enc_xtn_hdr_count = sizeof(headers) / sizeof(headers[0]);
1562 policy.next = NULL;
1563
1564 status = srtp_create(&srtp_snd, &policy);
1565 if (status)
1566 return status;
1567
1568 /*
1569 * protect plaintext, then compare with ciphertext
1570 */
1571 len = sizeof(srtp_plaintext_ref);
1572 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1573 if (status || (len != sizeof(srtp_plaintext)))
1574 return srtp_err_status_fail;
1575
1576 debug_print(mod_driver, "ciphertext:\n %s",
1577 srtp_octet_string_hex_string(srtp_plaintext, len));
1578 debug_print(mod_driver, "ciphertext reference:\n %s",
1579 srtp_octet_string_hex_string(srtp_ciphertext, len));
1580
1581 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len))
1582 return srtp_err_status_fail;
1583
1584 /*
1585 * create a receiver session context comparable to the one created
1586 * above - we need to do this so that the replay checking doesn't
1587 * complain
1588 */
1589 status = srtp_create(&srtp_recv, &policy);
1590 if (status)
1591 return status;
1592
1593 /*
1594 * unprotect ciphertext, then compare with plaintext
1595 */
1596 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
1597 if (status || (len != 28))
1598 return status;
1599
1600 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len))
1601 return srtp_err_status_fail;
1602
1603 status = srtp_dealloc(srtp_snd);
1604 if (status)
1605 return status;
1606
1607 status = srtp_dealloc(srtp_recv);
1608 if (status)
1609 return status;
1610
1611 return srtp_err_status_ok;
1612}
1613
Cullen Jennings235513a2005-09-21 22:51:36 +00001614
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001615/*
1616 * srtp_validate_aes_256() verifies the correctness of libsrtp by comparing
1617 * some computed packets against some pre-computed reference values.
1618 * These packets were made with the AES-CM-256/HMAC-SHA-1-80 policy.
1619 */
1620
1621
jfigus857009c2014-11-05 11:17:43 -05001622srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001623srtp_validate_aes_256 ()
1624{
1625 unsigned char aes_256_test_key[46] = {
1626 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
1627 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
1628 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
1629 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001630
jfigus67b9c732014-11-20 10:17:21 -05001631 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
1632 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
1633 };
1634 uint8_t srtp_plaintext_ref[28] = {
1635 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1636 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1637 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1638 0xab, 0xab, 0xab, 0xab
1639 };
1640 uint8_t srtp_plaintext[38] = {
1641 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1642 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
1643 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
1644 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
1645 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1646 };
1647 uint8_t srtp_ciphertext[38] = {
1648 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
1649 0xca, 0xfe, 0xba, 0xbe, 0xf1, 0xd9, 0xde, 0x17,
1650 0xff, 0x25, 0x1f, 0xf1, 0xaa, 0x00, 0x77, 0x74,
1651 0xb0, 0xb4, 0xb4, 0x0d, 0xa0, 0x8d, 0x9d, 0x9a,
1652 0x5b, 0x3a, 0x55, 0xd8, 0x87, 0x3b
1653 };
1654 srtp_t srtp_snd, srtp_recv;
1655 srtp_err_status_t status;
1656 int len;
1657 srtp_policy_t policy;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001658
jfigus67b9c732014-11-20 10:17:21 -05001659 /*
1660 * create a session with a single stream using the default srtp
1661 * policy and with the SSRC value 0xcafebabe
1662 */
Joachim Bauch99a74822015-11-17 00:08:19 +01001663 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05001664 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
1665 srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtcp);
1666 policy.ssrc.type = ssrc_specific;
1667 policy.ssrc.value = 0xcafebabe;
1668 policy.key = aes_256_test_key;
1669 policy.ekt = NULL;
1670 policy.window_size = 128;
1671 policy.allow_repeat_tx = 0;
1672 policy.next = NULL;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001673
jfigus67b9c732014-11-20 10:17:21 -05001674 status = srtp_create(&srtp_snd, &policy);
1675 if (status) {
1676 return status;
1677 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001678
jfigus67b9c732014-11-20 10:17:21 -05001679 /*
1680 * protect plaintext, then compare with ciphertext
1681 */
1682 len = 28;
1683 status = srtp_protect(srtp_snd, srtp_plaintext, &len);
1684 if (status || (len != 38)) {
1685 return srtp_err_status_fail;
1686 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001687
jfigus67b9c732014-11-20 10:17:21 -05001688 debug_print(mod_driver, "ciphertext:\n %s",
1689 octet_string_hex_string(srtp_plaintext, len));
1690 debug_print(mod_driver, "ciphertext reference:\n %s",
1691 octet_string_hex_string(srtp_ciphertext, len));
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001692
jfigus67b9c732014-11-20 10:17:21 -05001693 if (octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
1694 return srtp_err_status_fail;
1695 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001696
jfigus67b9c732014-11-20 10:17:21 -05001697 /*
1698 * create a receiver session context comparable to the one created
1699 * above - we need to do this so that the replay checking doesn't
1700 * complain
1701 */
1702 status = srtp_create(&srtp_recv, &policy);
1703 if (status) {
1704 return status;
1705 }
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001706
jfigus67b9c732014-11-20 10:17:21 -05001707 /*
1708 * unprotect ciphertext, then compare with plaintext
1709 */
1710 status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
1711 if (status || (len != 28)) {
1712 return status;
1713 }
1714
1715 if (octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
1716 return srtp_err_status_fail;
1717 }
1718
1719 status = srtp_dealloc(srtp_snd);
1720 if (status) {
1721 return status;
1722 }
1723
1724 status = srtp_dealloc(srtp_recv);
1725 if (status) {
1726 return status;
1727 }
1728
1729 return srtp_err_status_ok;
Jonathan Lennox5df951a2010-05-20 20:55:54 +00001730}
1731
1732
jfigus857009c2014-11-05 11:17:43 -05001733srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001734srtp_create_big_policy (srtp_policy_t **list)
1735{
1736 extern const srtp_policy_t *policy_array[];
1737 srtp_policy_t *p, *tmp;
1738 int i = 0;
1739 uint32_t ssrc = 0;
Cullen Jennings235513a2005-09-21 22:51:36 +00001740
jfigus67b9c732014-11-20 10:17:21 -05001741 /* sanity checking */
1742 if ((list == NULL) || (policy_array[0] == NULL)) {
1743 return srtp_err_status_bad_param;
1744 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001745
jfigus67b9c732014-11-20 10:17:21 -05001746 /*
1747 * loop over policy list, mallocing a new list and copying values
1748 * into it (and incrementing the SSRC value as we go along)
1749 */
1750 tmp = NULL;
1751 while (policy_array[i] != NULL) {
1752 p = (srtp_policy_t*)malloc(sizeof(srtp_policy_t));
1753 if (p == NULL) {
1754 return srtp_err_status_bad_param;
1755 }
1756 memcpy(p, policy_array[i], sizeof(srtp_policy_t));
1757 p->ssrc.type = ssrc_specific;
1758 p->ssrc.value = ssrc++;
1759 p->next = tmp;
1760 tmp = p;
1761 i++;
1762 }
1763 *list = p;
1764
1765 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001766}
1767
jfigus857009c2014-11-05 11:17:43 -05001768srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001769srtp_dealloc_big_policy (srtp_policy_t *list)
1770{
1771 srtp_policy_t *p, *next;
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001772
jfigus67b9c732014-11-20 10:17:21 -05001773 for (p = list; p != NULL; p = next) {
1774 next = p->next;
1775 free(p);
1776 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001777
jfigus67b9c732014-11-20 10:17:21 -05001778 return srtp_err_status_ok;
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001779}
1780
1781
jfigus857009c2014-11-05 11:17:43 -05001782srtp_err_status_t
jfigus67b9c732014-11-20 10:17:21 -05001783srtp_test_remove_stream ()
1784{
1785 srtp_err_status_t status;
1786 srtp_policy_t *policy_list, policy;
1787 srtp_t session;
1788 srtp_stream_t stream;
Cullen Jennings235513a2005-09-21 22:51:36 +00001789
jfigus67b9c732014-11-20 10:17:21 -05001790 /*
1791 * srtp_get_stream() is a libSRTP internal function that we declare
1792 * here so that we can use it to verify the correct operation of the
1793 * library
1794 */
1795 extern srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
Cullen Jennings235513a2005-09-21 22:51:36 +00001796
Cullen Jennings235513a2005-09-21 22:51:36 +00001797
jfigus67b9c732014-11-20 10:17:21 -05001798 status = srtp_create_big_policy(&policy_list);
1799 if (status) {
1800 return status;
1801 }
Cullen Jennings235513a2005-09-21 22:51:36 +00001802
jfigus67b9c732014-11-20 10:17:21 -05001803 status = srtp_create(&session, policy_list);
1804 if (status) {
1805 return status;
1806 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001807
jfigus67b9c732014-11-20 10:17:21 -05001808 /*
1809 * check for false positives by trying to remove a stream that's not
1810 * in the session
1811 */
1812 status = srtp_remove_stream(session, htonl(0xaaaaaaaa));
1813 if (status != srtp_err_status_no_ctx) {
1814 return srtp_err_status_fail;
1815 }
Jonathan Lennox80c4c832010-05-17 19:30:28 +00001816
jfigus67b9c732014-11-20 10:17:21 -05001817 /*
1818 * check for false negatives by removing stream 0x1, then
1819 * searching for streams 0x0 and 0x2
1820 */
1821 status = srtp_remove_stream(session, htonl(0x1));
1822 if (status != srtp_err_status_ok) {
1823 return srtp_err_status_fail;
1824 }
1825 stream = srtp_get_stream(session, htonl(0x0));
1826 if (stream == NULL) {
1827 return srtp_err_status_fail;
1828 }
1829 stream = srtp_get_stream(session, htonl(0x2));
1830 if (stream == NULL) {
1831 return srtp_err_status_fail;
1832 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00001833
jfigus67b9c732014-11-20 10:17:21 -05001834 status = srtp_dealloc(session);
1835 if (status != srtp_err_status_ok) {
1836 return status;
1837 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00001838
jfigus67b9c732014-11-20 10:17:21 -05001839 status = srtp_dealloc_big_policy(policy_list);
1840 if (status != srtp_err_status_ok) {
1841 return status;
1842 }
Jonathan Lennoxad741b22010-05-27 19:23:05 +00001843
jfigus67b9c732014-11-20 10:17:21 -05001844 /* Now test adding and removing a single stream */
Joachim Bauch99a74822015-11-17 00:08:19 +01001845 memset(&policy, 0, sizeof(policy));
jfigus67b9c732014-11-20 10:17:21 -05001846 srtp_crypto_policy_set_rtp_default(&policy.rtp);
1847 srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
1848 policy.ssrc.type = ssrc_specific;
1849 policy.ssrc.value = 0xcafebabe;
1850 policy.key = test_key;
1851 policy.ekt = NULL;
1852 policy.window_size = 128;
1853 policy.allow_repeat_tx = 0;
1854 policy.next = NULL;
Jonathan Lennoxad741b22010-05-27 19:23:05 +00001855
jfigus67b9c732014-11-20 10:17:21 -05001856 status = srtp_create(&session, NULL);
1857 if (status != srtp_err_status_ok) {
1858 return status;
1859 }
1860
1861 status = srtp_add_stream(session, &policy);
1862 if (status != srtp_err_status_ok) {
1863 return status;
1864 }
1865
1866 status = srtp_remove_stream(session, htonl(0xcafebabe));
1867 if (status != srtp_err_status_ok) {
1868 return status;
1869 }
1870
1871 status = srtp_dealloc(session);
1872 if (status != srtp_err_status_ok) {
1873 return status;
1874 }
1875
1876 return srtp_err_status_ok;
Cullen Jennings235513a2005-09-21 22:51:36 +00001877}
1878
1879/*
1880 * srtp policy definitions - these definitions are used above
1881 */
1882
jfigus8c36da22013-10-01 16:41:19 -04001883unsigned char test_key[46] = {
Cullen Jennings235513a2005-09-21 22:51:36 +00001884 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
1885 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
1886 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
jfigus8c36da22013-10-01 16:41:19 -04001887 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73,
1888 0xc3, 0x17, 0xf2, 0xda, 0xbe, 0x35, 0x77, 0x93,
Cullen Jennings235513a2005-09-21 22:51:36 +00001889 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
1890};
1891
1892
1893const srtp_policy_t default_policy = {
jfigus67b9c732014-11-20 10:17:21 -05001894 { ssrc_any_outbound, 0 }, /* SSRC */
1895 { /* SRTP policy */
1896 SRTP_AES_128_ICM, /* cipher type */
1897 30, /* cipher key length in octets */
1898 SRTP_HMAC_SHA1, /* authentication func type */
1899 16, /* auth key length in octets */
1900 10, /* auth tag length in octets */
1901 sec_serv_conf_and_auth /* security services flag */
1902 },
1903 { /* SRTCP policy */
1904 SRTP_AES_128_ICM, /* cipher type */
1905 30, /* cipher key length in octets */
1906 SRTP_HMAC_SHA1, /* authentication func type */
1907 16, /* auth key length in octets */
1908 10, /* auth tag length in octets */
1909 sec_serv_conf_and_auth /* security services flag */
1910 },
1911 test_key,
1912 NULL, /* indicates that EKT is not in use */
1913 128, /* replay window size */
1914 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01001915 NULL, /* no encrypted extension headers */
1916 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05001917 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00001918};
1919
1920const srtp_policy_t aes_only_policy = {
jfigus67b9c732014-11-20 10:17:21 -05001921 { ssrc_any_outbound, 0 }, /* SSRC */
1922 {
1923 SRTP_AES_128_ICM, /* cipher type */
1924 30, /* cipher key length in octets */
1925 SRTP_NULL_AUTH, /* authentication func type */
1926 0, /* auth key length in octets */
1927 0, /* auth tag length in octets */
1928 sec_serv_conf /* security services flag */
1929 },
1930 {
1931 SRTP_AES_128_ICM, /* cipher type */
1932 30, /* cipher key length in octets */
1933 SRTP_NULL_AUTH, /* authentication func type */
1934 0, /* auth key length in octets */
1935 0, /* auth tag length in octets */
1936 sec_serv_conf /* security services flag */
1937 },
1938 test_key,
1939 NULL, /* indicates that EKT is not in use */
1940 128, /* replay window size */
1941 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01001942 NULL, /* no encrypted extension headers */
1943 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05001944 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00001945};
1946
1947const srtp_policy_t hmac_only_policy = {
jfigus67b9c732014-11-20 10:17:21 -05001948 { ssrc_any_outbound, 0 }, /* SSRC */
1949 {
1950 SRTP_NULL_CIPHER, /* cipher type */
1951 0, /* cipher key length in octets */
1952 SRTP_HMAC_SHA1, /* authentication func type */
1953 20, /* auth key length in octets */
1954 4, /* auth tag length in octets */
1955 sec_serv_auth /* security services flag */
1956 },
1957 {
1958 SRTP_NULL_CIPHER, /* cipher type */
1959 0, /* cipher key length in octets */
1960 SRTP_HMAC_SHA1, /* authentication func type */
1961 20, /* auth key length in octets */
1962 4, /* auth tag length in octets */
1963 sec_serv_auth /* security services flag */
1964 },
1965 test_key,
1966 NULL, /* indicates that EKT is not in use */
1967 128, /* replay window size */
1968 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01001969 NULL, /* no encrypted extension headers */
1970 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05001971 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00001972};
1973
jfigus8c36da22013-10-01 16:41:19 -04001974#ifdef OPENSSL
1975const srtp_policy_t aes128_gcm_8_policy = {
jfigus67b9c732014-11-20 10:17:21 -05001976 { ssrc_any_outbound, 0 }, /* SSRC */
1977 { /* SRTP policy */
1978 SRTP_AES_128_GCM, /* cipher type */
1979 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
1980 SRTP_NULL_AUTH, /* authentication func type */
1981 0, /* auth key length in octets */
1982 8, /* auth tag length in octets */
1983 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04001984 },
jfigus67b9c732014-11-20 10:17:21 -05001985 { /* SRTCP policy */
1986 SRTP_AES_128_GCM, /* cipher type */
1987 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
1988 SRTP_NULL_AUTH, /* authentication func type */
1989 0, /* auth key length in octets */
1990 8, /* auth tag length in octets */
1991 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04001992 },
1993 test_key,
1994 NULL, /* indicates that EKT is not in use */
1995 128, /* replay window size */
1996 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01001997 NULL, /* no encrypted extension headers */
1998 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04001999 NULL
2000};
2001
2002const srtp_policy_t aes128_gcm_8_cauth_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002003 { ssrc_any_outbound, 0 }, /* SSRC */
2004 { /* SRTP policy */
2005 SRTP_AES_128_GCM, /* cipher type */
2006 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2007 SRTP_NULL_AUTH, /* authentication func type */
2008 0, /* auth key length in octets */
2009 8, /* auth tag length in octets */
2010 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002011 },
jfigus67b9c732014-11-20 10:17:21 -05002012 { /* SRTCP policy */
2013 SRTP_AES_128_GCM, /* cipher type */
2014 SRTP_AES_128_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2015 SRTP_NULL_AUTH, /* authentication func type */
2016 0, /* auth key length in octets */
2017 8, /* auth tag length in octets */
2018 sec_serv_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002019 },
2020 test_key,
2021 NULL, /* indicates that EKT is not in use */
2022 128, /* replay window size */
2023 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002024 NULL, /* no encrypted extension headers */
2025 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002026 NULL
2027};
jfigus67b9c732014-11-20 10:17:21 -05002028
jfigus8c36da22013-10-01 16:41:19 -04002029const srtp_policy_t aes256_gcm_8_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002030 { ssrc_any_outbound, 0 }, /* SSRC */
2031 { /* SRTP policy */
2032 SRTP_AES_256_GCM, /* cipher type */
2033 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2034 SRTP_NULL_AUTH, /* authentication func type */
2035 0, /* auth key length in octets */
2036 8, /* auth tag length in octets */
2037 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002038 },
jfigus67b9c732014-11-20 10:17:21 -05002039 { /* SRTCP policy */
2040 SRTP_AES_256_GCM, /* cipher type */
2041 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2042 SRTP_NULL_AUTH, /* authentication func type */
2043 0, /* auth key length in octets */
2044 8, /* auth tag length in octets */
2045 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002046 },
2047 test_key,
2048 NULL, /* indicates that EKT is not in use */
2049 128, /* replay window size */
2050 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002051 NULL, /* no encrypted extension headers */
2052 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002053 NULL
2054};
jfigus67b9c732014-11-20 10:17:21 -05002055
jfigus8c36da22013-10-01 16:41:19 -04002056const srtp_policy_t aes256_gcm_8_cauth_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002057 { ssrc_any_outbound, 0 }, /* SSRC */
2058 { /* SRTP policy */
2059 SRTP_AES_256_GCM, /* cipher type */
2060 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2061 SRTP_NULL_AUTH, /* authentication func type */
2062 0, /* auth key length in octets */
2063 8, /* auth tag length in octets */
2064 sec_serv_conf_and_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002065 },
jfigus67b9c732014-11-20 10:17:21 -05002066 { /* SRTCP policy */
2067 SRTP_AES_256_GCM, /* cipher type */
2068 SRTP_AES_256_GCM_KEYSIZE_WSALT, /* cipher key length in octets */
2069 SRTP_NULL_AUTH, /* authentication func type */
2070 0, /* auth key length in octets */
2071 8, /* auth tag length in octets */
2072 sec_serv_auth /* security services flag */
jfigus8c36da22013-10-01 16:41:19 -04002073 },
2074 test_key,
2075 NULL, /* indicates that EKT is not in use */
2076 128, /* replay window size */
2077 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002078 NULL, /* no encrypted extension headers */
2079 0, /* list of encrypted extension headers is empty */
jfigus8c36da22013-10-01 16:41:19 -04002080 NULL
2081};
2082#endif
2083
Cullen Jennings235513a2005-09-21 22:51:36 +00002084const srtp_policy_t null_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002085 { ssrc_any_outbound, 0 }, /* SSRC */
2086 {
2087 SRTP_NULL_CIPHER, /* cipher type */
2088 0, /* cipher key length in octets */
2089 SRTP_NULL_AUTH, /* authentication func type */
2090 0, /* auth key length in octets */
2091 0, /* auth tag length in octets */
2092 sec_serv_none /* security services flag */
2093 },
2094 {
2095 SRTP_NULL_CIPHER, /* cipher type */
2096 0, /* cipher key length in octets */
2097 SRTP_NULL_AUTH, /* authentication func type */
2098 0, /* auth key length in octets */
2099 0, /* auth tag length in octets */
2100 sec_serv_none /* security services flag */
2101 },
2102 test_key,
2103 NULL, /* indicates that EKT is not in use */
2104 128, /* replay window size */
2105 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002106 NULL, /* no encrypted extension headers */
2107 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002108 NULL
David McGrew79870d62007-06-15 18:17:39 +00002109};
2110
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002111unsigned char test_256_key[46] = {
jfigus67b9c732014-11-20 10:17:21 -05002112 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
2113 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
2114 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
2115 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002116
jfigus67b9c732014-11-20 10:17:21 -05002117 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
2118 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002119};
2120
2121const srtp_policy_t aes_256_hmac_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002122 { ssrc_any_outbound, 0 }, /* SSRC */
2123 { /* SRTP policy */
2124 SRTP_AES_ICM, /* cipher type */
2125 46, /* cipher key length in octets */
2126 SRTP_HMAC_SHA1, /* authentication func type */
2127 20, /* auth key length in octets */
2128 10, /* auth tag length in octets */
2129 sec_serv_conf_and_auth /* security services flag */
2130 },
2131 { /* SRTCP policy */
2132 SRTP_AES_ICM, /* cipher type */
2133 46, /* cipher key length in octets */
2134 SRTP_HMAC_SHA1, /* authentication func type */
2135 20, /* auth key length in octets */
2136 10, /* auth tag length in octets */
2137 sec_serv_conf_and_auth /* security services flag */
2138 },
2139 test_256_key,
2140 NULL, /* indicates that EKT is not in use */
2141 128, /* replay window size */
2142 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002143 NULL, /* no encrypted extension headers */
2144 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002145 NULL
Jonathan Lennox5df951a2010-05-20 20:55:54 +00002146};
2147
David McGrew79870d62007-06-15 18:17:39 +00002148uint8_t ekt_test_key[16] = {
jfigus67b9c732014-11-20 10:17:21 -05002149 0x77, 0x26, 0x9d, 0xac, 0x16, 0xa3, 0x28, 0xca,
2150 0x8e, 0xc9, 0x68, 0x4b, 0xcc, 0xc4, 0xd2, 0x1b
David McGrew79870d62007-06-15 18:17:39 +00002151};
2152
2153#include "ekt.h"
2154
jfigusc5887e72014-11-06 09:46:18 -05002155srtp_ekt_policy_ctx_t ekt_test_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002156 0xa5a5, /* SPI */
2157 SRTP_EKT_CIPHER_AES_128_ECB,
2158 ekt_test_key,
2159 NULL
David McGrew79870d62007-06-15 18:17:39 +00002160};
2161
2162const srtp_policy_t hmac_only_with_ekt_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002163 { ssrc_any_outbound, 0 }, /* SSRC */
2164 {
2165 SRTP_NULL_CIPHER, /* cipher type */
2166 0, /* cipher key length in octets */
2167 SRTP_HMAC_SHA1, /* authentication func type */
2168 20, /* auth key length in octets */
2169 4, /* auth tag length in octets */
2170 sec_serv_auth /* security services flag */
2171 },
2172 {
2173 SRTP_NULL_CIPHER, /* cipher type */
2174 0, /* cipher key length in octets */
2175 SRTP_HMAC_SHA1, /* authentication func type */
2176 20, /* auth key length in octets */
2177 4, /* auth tag length in octets */
2178 sec_serv_auth /* security services flag */
2179 },
2180 test_key,
2181 &ekt_test_policy, /* indicates that EKT is not in use */
2182 128, /* replay window size */
2183 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002184 NULL, /* no encrypted extension headers */
2185 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002186 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002187};
2188
2189
2190/*
2191 * an array of pointers to the policies listed above
2192 *
2193 * This array is used to test various aspects of libSRTP for
2194 * different cryptographic policies. The order of the elements
2195 * matters - the timing test generates output that can be used
jfigus67b9c732014-11-20 10:17:21 -05002196 * in a plot (see the gnuplot script file 'timing'). If you
Cullen Jennings235513a2005-09-21 22:51:36 +00002197 * add to this list, you should do it at the end.
2198 */
2199
Cullen Jennings235513a2005-09-21 22:51:36 +00002200const srtp_policy_t *
2201policy_array[] = {
jfigus67b9c732014-11-20 10:17:21 -05002202 &hmac_only_policy,
2203 &aes_only_policy,
2204 &default_policy,
jfigus8c36da22013-10-01 16:41:19 -04002205#ifdef OPENSSL
jfigus67b9c732014-11-20 10:17:21 -05002206 &aes128_gcm_8_policy,
2207 &aes128_gcm_8_cauth_policy,
2208 &aes256_gcm_8_policy,
2209 &aes256_gcm_8_cauth_policy,
jfigus8c36da22013-10-01 16:41:19 -04002210#endif
jfigus67b9c732014-11-20 10:17:21 -05002211 &null_policy,
2212 &aes_256_hmac_policy,
2213 &hmac_only_with_ekt_policy,
2214 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002215};
2216
2217const srtp_policy_t wildcard_policy = {
jfigus67b9c732014-11-20 10:17:21 -05002218 { ssrc_any_outbound, 0 }, /* SSRC */
2219 { /* SRTP policy */
2220 SRTP_AES_128_ICM, /* cipher type */
2221 30, /* cipher key length in octets */
2222 SRTP_HMAC_SHA1, /* authentication func type */
2223 16, /* auth key length in octets */
2224 10, /* auth tag length in octets */
2225 sec_serv_conf_and_auth /* security services flag */
2226 },
2227 { /* SRTCP policy */
2228 SRTP_AES_128_ICM, /* cipher type */
2229 30, /* cipher key length in octets */
2230 SRTP_HMAC_SHA1, /* authentication func type */
2231 16, /* auth key length in octets */
2232 10, /* auth tag length in octets */
2233 sec_serv_conf_and_auth /* security services flag */
2234 },
2235 test_key,
2236 NULL,
2237 128, /* replay window size */
2238 0, /* retransmission not allowed */
Joachim Bauch99a74822015-11-17 00:08:19 +01002239 NULL, /* no encrypted extension headers */
2240 0, /* list of encrypted extension headers is empty */
jfigus67b9c732014-11-20 10:17:21 -05002241 NULL
Cullen Jennings235513a2005-09-21 22:51:36 +00002242};