2 * Secure RTP with libgcrypt
3 * Copyright (C) 2007 RĂ©mi Denis-Courmont
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 static const char key[] =
39 "123456789ABCDEF0" "123456789ABCDEF0";
40 static const char salt[] =
41 "1234567890" "1234567890" "12345678";
43 srtp_session_t *sd, *se;
45 /* Too big tag length */
46 se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 21,
50 /* Too short tag length */
51 se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 3,
52 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
55 /* Initializes encryption and decryption contexts */
56 se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 20,
57 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
60 sd = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 20,
61 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
64 srtp_setrcc_rate (se, 1);
65 srtp_setrcc_rate (sd, 1);
67 val = srtp_setkeystring (se, key, salt);
69 val = srtp_setkeystring (sd, key, salt);
72 uint8_t buf[1500], buf2[1500];
75 /* Invalid SRTP packet */
78 val = srtp_send (se, buf, &len, sizeof (buf));
79 assert (val == EINVAL);
83 srtp_recv (sd, buf, &len);
84 assert (val == EINVAL);
86 /* Too short packet */
89 val = srtp_send (se, buf, &len, sizeof (buf));
90 assert (val == EINVAL);
93 val = srtp_recv (sd, buf, &len);
94 assert (val == EINVAL);
96 /* Too short when taking tag into account */
98 val = srtp_recv (sd, buf, &len);
99 assert (val == EINVAL);
101 /* Too short when taking RTP extensions into account */
104 val = srtp_send (se, buf, &len, sizeof (buf));
105 assert (val == EINVAL);
110 val = srtp_send (se, buf, &len, sizeof (buf));
111 assert (val == EINVAL);
113 /* Too small buffer (seq=1) */
115 memset (buf, 0, len);
118 val = srtp_send (se, buf, &len, 39);
119 assert (val == ENOSPC);
123 val = srtp_recv (sd, buf, &len);
124 assert (val == EINVAL);
129 for (unsigned i = 0; i < 256; i++)
132 val = srtp_send (se, buf, &len, 0x120);
134 assert (len == 0x120);
136 memcpy (buf2, buf, len);
137 val = srtp_recv (sd, buf2, &len);
139 assert (len == 0x10c);
140 assert (!memcmp (buf2, "\x80\x00\x00\x03" "\x00\x00\x00\x00"
141 "\x00\x00\x00\x00", 12));
142 for (unsigned i = 0; i < 256; i++)
143 assert (buf2[i + 12] == i); // test actual decryption
145 /* Replay attack (seq=3) */
147 val = srtp_recv (sd, buf, &len);
148 assert (val == EACCES);
149 assert (len == 0x10c);
151 /* OK but late (seq=2) */
154 val = srtp_send (se, buf, &len, 0x120);
156 assert (len == 0x120);
158 memcpy (buf2, buf, len);
159 val = srtp_recv (sd, buf2, &len);
161 assert (len == 0x10c);
163 /* Late replay attack (seq=3) */
165 val = srtp_recv (sd, buf, &len);
166 assert (val == EACCES);
167 assert (len == 0x10c);