]> git.sesse.net Git - vlc/blob - libs/srtp/test-recv.c
Add real SRTP unit test (85% coverage let alone SRTCP)
[vlc] / libs / srtp / test-recv.c
1 /*
2  * Secure RTP with libgcrypt
3  * Copyright (C) 2007  RĂ©mi Denis-Courmont <rdenis # simphalempin , com>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdint.h>
25 #include <stddef.h>
26 #include "srtp.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #undef NDEBUG
33 #include <assert.h>
34
35
36 int main (void)
37 {
38     static const uint8_t key[16] =
39         "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
40         "\x12\x34\x56\x78\x9A\xBC\xDE\xF0";
41     static const uint8_t salt[14] =
42         "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78";
43     int val;
44     srtp_session_t *sd, *se;
45
46     /* Too big tag length */
47     se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 21,
48                       SRTP_PRF_AES_CM, 0);
49     assert (se == NULL);
50
51     /* Too short tag length */
52     se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 3,
53                       SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
54     assert (se == NULL);
55
56     /* Initializes encryption and decryption contexts */
57     se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 20,
58                       SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
59     assert (se != NULL);
60
61     sd = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 20,
62                       SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
63     assert (sd != NULL);
64
65     srtp_setrcc_rate (se, 1);
66     srtp_setrcc_rate (sd, 1);
67
68     val = srtp_setkey (se, key, 16, salt, 14);
69     assert (val == 0);
70     val = srtp_setkey (sd, key, 16, salt, 14);
71     assert (val == 0);
72
73     uint8_t buf[1500], buf2[1500];
74     size_t len;
75
76     /* Invalid SRTP packet */
77     len = 12;
78     memset (buf, 0, len);
79     val = srtp_send (se, buf, &len, sizeof (buf));
80     assert (val == EINVAL);
81
82     len = 32;
83     memset (buf, 0, len);
84     srtp_recv (sd, buf, &len);
85     assert (val == EINVAL);
86
87     /* Too short packet */
88     len = 11;
89     buf[0] = 0x80;
90     val = srtp_send (se, buf, &len, sizeof (buf));
91     assert (val == EINVAL);
92
93     len = 11;
94     val = srtp_recv (sd, buf, &len);
95     assert (val == EINVAL);
96
97     /* Too short when taking tag into account */
98     len = 31;
99     val = srtp_recv (sd, buf, &len);
100     assert (val == EINVAL);
101
102     /* Too short when taking RTP extensions into account */
103     len = 15;
104     buf[0] = 0x90;
105     val = srtp_send (se, buf, &len, sizeof (buf));
106     assert (val == EINVAL);
107
108     len = 16;
109     buf[0] = 0x90;
110     buf[15] = 1;
111     val = srtp_send (se, buf, &len, sizeof (buf));
112     assert (val == EINVAL);
113
114     /* Too small buffer (seq=1) */
115     len = 20;
116     memset (buf, 0, len);
117     buf[0] = 0x80;
118     buf[3] = 1;
119     val = srtp_send (se, buf, &len, 39);
120     assert (val == ENOSPC);
121     assert (len == 40);
122
123     len = 31;
124     val = srtp_recv (sd, buf, &len);
125     assert (val == EINVAL);
126
127     /* OK (seq=3) */
128     buf[0] = 0x80;
129     buf[3] = 3;
130     for (unsigned i = 0; i < 256; i++)
131         buf[i + 12] = i;
132     len = 0x10c;
133     val = srtp_send (se, buf, &len, 0x120);
134     assert (val == 0);
135     assert (len == 0x120);
136
137     memcpy (buf2, buf, len);
138     val = srtp_recv (sd, buf2, &len);
139     assert (val == 0);
140     assert (len == 0x10c);
141     assert (!memcmp (buf2, "\x80\x00\x00\x03" "\x00\x00\x00\x00"
142                            "\x00\x00\x00\x00", 12));
143     for (unsigned i = 0; i < 256; i++)
144         assert (buf2[i + 12] == i); // test actual decryption
145
146     /* Replay attack (seq=3) */
147     len = 0x120;
148     val = srtp_recv (sd, buf, &len);
149     assert (val == EACCES);
150     assert (len == 0x10c);
151
152     /* OK but late (seq=2) */
153     buf[0] = 0x80;
154     buf[3] = 2;
155     val = srtp_send (se, buf, &len, 0x120);
156     assert (val == 0);
157     assert (len == 0x120);
158
159     memcpy (buf2, buf, len);
160     val = srtp_recv (sd, buf2, &len);
161     assert (val == 0);
162     assert (len == 0x10c);
163
164     /* Late replay attack (seq=3) */
165     len = 0x120;
166     val = srtp_recv (sd, buf, &len);
167     assert (val == EACCES);
168     assert (len == 0x10c);
169
170     srtp_destroy (se);
171     srtp_destroy (sd);
172     return 0;
173 }