]> git.sesse.net Git - vlc/blob - libs/srtp/test-recv.c
Remove my old email address
[vlc] / libs / srtp / test-recv.c
1 /*
2  * Secure RTP with libgcrypt
3  * Copyright (C) 2007  RĂ©mi Denis-Courmont
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 char key[] =
39         "123456789ABCDEF0" "123456789ABCDEF0";
40     static const char salt[] =
41         "1234567890" "1234567890" "12345678";
42     int val;
43     srtp_session_t *sd, *se;
44
45     /* Too big tag length */
46     se = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 21,
47                       SRTP_PRF_AES_CM, 0);
48     assert (se == NULL);
49
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);
53     assert (se == NULL);
54
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);
58     assert (se != NULL);
59
60     sd = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 20,
61                       SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
62     assert (sd != NULL);
63
64     srtp_setrcc_rate (se, 1);
65     srtp_setrcc_rate (sd, 1);
66
67     val = srtp_setkeystring (se, key, salt);
68     assert (val == 0);
69     val = srtp_setkeystring (sd, key, salt);
70     assert (val == 0);
71
72     uint8_t buf[1500], buf2[1500];
73     size_t len;
74
75     /* Invalid SRTP packet */
76     len = 12;
77     memset (buf, 0, len);
78     val = srtp_send (se, buf, &len, sizeof (buf));
79     assert (val == EINVAL);
80
81     len = 32;
82     memset (buf, 0, len);
83     srtp_recv (sd, buf, &len);
84     assert (val == EINVAL);
85
86     /* Too short packet */
87     len = 11;
88     buf[0] = 0x80;
89     val = srtp_send (se, buf, &len, sizeof (buf));
90     assert (val == EINVAL);
91
92     len = 11;
93     val = srtp_recv (sd, buf, &len);
94     assert (val == EINVAL);
95
96     /* Too short when taking tag into account */
97     len = 31;
98     val = srtp_recv (sd, buf, &len);
99     assert (val == EINVAL);
100
101     /* Too short when taking RTP extensions into account */
102     len = 15;
103     buf[0] = 0x90;
104     val = srtp_send (se, buf, &len, sizeof (buf));
105     assert (val == EINVAL);
106
107     len = 16;
108     buf[0] = 0x90;
109     buf[15] = 1;
110     val = srtp_send (se, buf, &len, sizeof (buf));
111     assert (val == EINVAL);
112
113     /* Too small buffer (seq=1) */
114     len = 20;
115     memset (buf, 0, len);
116     buf[0] = 0x80;
117     buf[3] = 1;
118     val = srtp_send (se, buf, &len, 39);
119     assert (val == ENOSPC);
120     assert (len == 40);
121
122     len = 31;
123     val = srtp_recv (sd, buf, &len);
124     assert (val == EINVAL);
125
126     /* OK (seq=3) */
127     buf[0] = 0x80;
128     buf[3] = 3;
129     for (unsigned i = 0; i < 256; i++)
130         buf[i + 12] = i;
131     len = 0x10c;
132     val = srtp_send (se, buf, &len, 0x120);
133     assert (val == 0);
134     assert (len == 0x120);
135
136     memcpy (buf2, buf, len);
137     val = srtp_recv (sd, buf2, &len);
138     assert (val == 0);
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
144
145     /* Replay attack (seq=3) */
146     len = 0x120;
147     val = srtp_recv (sd, buf, &len);
148     assert (val == EACCES);
149     assert (len == 0x10c);
150
151     /* OK but late (seq=2) */
152     buf[0] = 0x80;
153     buf[3] = 2;
154     val = srtp_send (se, buf, &len, 0x120);
155     assert (val == 0);
156     assert (len == 0x120);
157
158     memcpy (buf2, buf, len);
159     val = srtp_recv (sd, buf2, &len);
160     assert (val == 0);
161     assert (len == 0x10c);
162
163     /* Late replay attack (seq=3) */
164     len = 0x120;
165     val = srtp_recv (sd, buf, &len);
166     assert (val == EACCES);
167     assert (len == 0x10c);
168
169     srtp_destroy (se);
170     srtp_destroy (sd);
171     return 0;
172 }