2 * Secure RTP with libgcrypt
3 * Copyright (C) 2007 RĂ©mi Denis-Courmont <rdenis # simphalempin , com>
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
32 #include <netinet/in.h>
36 int fd = socket (AF_INET, SOCK_DGRAM, 0);
37 struct sockaddr_in addr;
38 memset (&addr, 0, sizeof (addr));
39 addr.sin_family = AF_INET;
41 addr.sin_len = sizeof (addr);
43 addr.sin_port = htons (10000);
44 addr.sin_addr.s_addr = htonl (0x7f000001);
45 if (bind (fd, (struct sockaddr *)&addr, sizeof (addr)))
48 static const uint8_t key[16] =
49 "\x12\x34\x56\x78\x9A\xBC\xDE\xF0"
50 "\x12\x34\x56\x78\x9A\xBC\xDE\xF0";
51 static const uint8_t salt[14] =
52 "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78\x90" "\x12\x34\x56\x78";
54 srtp_session_t *s = srtp_create ("AES_CM_128_HMAC_SHA1_80", 0, 0, 0);
57 if (srtp_setkey (s, key, 16, salt, 14))
63 memset (buf, 0, sizeof (buf));
66 memcpy (buf + 2, &(uint16_t){ htons (9527) }, 2);
67 memcpy (buf + 8, "\xde\xad\xbe\xef", 4);
68 memcpy (buf + 4, &(uint32_t){ htonl (1) }, 4);
69 strcpy ((char *)buf + 12, "a\n");
70 len = 12 + strlen ((char *)buf + 12) + 1;
74 len = read (fd, buf, sizeof (buf));
75 if (srtp_recv (s, buf, &len))
76 fputs ("Cannot decrypt!\n", stderr);
77 puts ((char *)buf + 12);
78 if (srtp_send (s, buf, &len, sizeof (buf)) || srtp_recv (s, buf, &len))
80 puts ((char *)buf + 12);