]> git.sesse.net Git - vlc/blob - libs/srtp/test-aes.c
Remove my old email address
[vlc] / libs / srtp / test-aes.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 #include <stdio.h>
21 #include "srtp.c"
22
23 static void printhex (const void *buf, size_t len)
24 {
25     for (size_t i = 0; i < len; i++)
26         printf ("%02X", ((uint8_t *)buf)[i]);
27     fputc ('\n', stdout);
28 }
29
30 static void fatal (const char *msg)
31 {
32     puts (msg);
33     exit (1);
34 }
35
36 /** AES-CM key derivation test vectors */
37 static void test_derivation (void)
38 {
39     static const uint8_t key[16] =
40         "\xE1\xF9\x7A\x0D\x3E\x01\x8B\xE0\xD6\x4F\xA3\x2C\x06\xDE\x41\x39";
41     static const uint8_t salt[14] =
42         "\x0E\xC6\x75\xAD\x49\x8A\xFE\xEB\xB6\x96\x0B\x3A\xAB\xE6";
43
44     static const uint8_t good_cipher[16] =
45         "\xC6\x1E\x7A\x93\x74\x4F\x39\xEE\x10\x73\x4A\xFE\x3F\xF7\xA0\x87";
46     static const uint8_t good_salt[14] =
47         "\x30\xCB\xBC\x08\x86\x3D\x8C\x85\xD4\x9D\xB3\x4A\x9A\xE1";
48     static const uint8_t good_auth[94] =
49         "\xCE\xBE\x32\x1F\x6F\xF7\x71\x6B\x6F\xD4\xAB\x49\xAF\x25\x6A\x15"
50         "\x6D\x38\xBA\xA4\x8F\x0A\x0A\xCF\x3C\x34\xE2\x35\x9E\x6C\xDB\xCE"
51         "\xE0\x49\x64\x6C\x43\xD9\x32\x7A\xD1\x75\x57\x8E\xF7\x22\x70\x98"
52         "\x63\x71\xC1\x0C\x9A\x36\x9A\xC2\xF9\x4A\x8C\x5F\xBC\xDD\xDC\x25"
53         "\x6D\x6E\x91\x9A\x48\xB6\x10\xEF\x17\xC2\x04\x1E\x47\x40\x35\x76"
54         "\x6B\x68\x64\x2C\x59\xBB\xFC\x2F\x34\xDB\x60\xDB\xDF\xB2";
55
56     static const uint8_t r[6] = { 0, 0, 0, 0, 0, 0 };
57     gcry_cipher_hd_t prf;
58     uint8_t out[94];
59
60     puts ("AES-CM key derivation test...");
61     printf (" master key:  ");
62     printhex (key, sizeof (key));
63     printf (" master salt: ");
64     printhex (salt, sizeof (salt));
65
66     if (gcry_cipher_open (&prf, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, 0)
67      || gcry_cipher_setkey (prf, key, sizeof (key)))
68         fatal ("Internal PRF error");
69
70     if (derive (prf, salt, r, sizeof (r), SRTP_CRYPT, out, 16))
71         fatal ("Internal cipher derivation error");
72     printf (" cipher key:  ");
73     printhex (out, 16);
74     if (memcmp (out, good_cipher, 16))
75         fatal ("Test failed");
76
77     if (derive (prf, salt, r, sizeof (r), SRTP_SALT, out, 14))
78         fatal ("Internal salt derivation error");
79     printf (" cipher salt: ");
80     printhex (out, 14);
81     if (memcmp (out, good_salt, 14))
82         fatal ("Test failed");
83
84     if (derive (prf, salt, r, sizeof (r), SRTP_AUTH, out, 94))
85         fatal ("Internal auth key derivation error");
86     printf (" auth key:    ");
87     printhex (out, 94);
88     if (memcmp (out, good_auth, 94))
89         fatal ("Test failed");
90
91     gcry_cipher_close (prf);
92 }
93
94 /** AES-CM key derivation test vectors */
95 static void test_keystream (void)
96 {
97     static const uint8_t key[16] =
98         "\x2B\x7E\x15\x16\x28\xAE\xD2\xA6\xAB\xF7\x15\x88\x09\xCF\x4F\x3C";
99     const uint32_t salt[4]=
100         { htonl (0xf0f1f2f3), htonl (0xf4f5f6f7),
101           htonl (0xf8f9fafb), htonl (0xfcfd0000) };
102
103     puts ("AES-CM key stream test...");
104     uint8_t *buf = calloc (0xff02, 16);
105     if (buf == NULL)
106     {
107         fputs ("Not enough memory for test\n", stderr);
108         return;
109     }
110
111     printf (" session key: ");
112     printhex (key, sizeof (key));
113
114     gcry_cipher_hd_t hd;
115     if (gcry_cipher_open (&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, 0))
116         fatal ("Cipher initialization error");
117     if (gcry_cipher_setkey (hd, key, sizeof (key)))
118         fatal ("Cipher key error");
119
120     if (rtp_crypt (hd, 0, 0, 0, salt, buf, 0xff020))
121         fatal ("Encryption failure");
122     gcry_cipher_close (hd);
123
124     static const uint8_t good_start[48] =
125         "\xE0\x3E\xAD\x09\x35\xC9\x5E\x80\xE1\x66\xB1\x6D\xD9\x2B\x4E\xB4"
126         "\xD2\x35\x13\x16\x2B\x02\xD0\xF7\x2A\x43\xA2\xFE\x4A\x5F\x97\xAB"
127         "\x41\xE9\x5B\x3B\xB0\xA2\xE8\xDD\x47\x79\x01\xE4\xFC\xA8\x94\xC0";
128     static const uint8_t good_end[48] =
129         "\xEC\x8C\xDF\x73\x98\x60\x7C\xB0\xF2\xD2\x16\x75\xEA\x9E\xA1\xE4"
130         "\x36\x2B\x7C\x3C\x67\x73\x51\x63\x18\xA0\x77\xD7\xFC\x50\x73\xAE"
131         "\x6A\x2C\xC3\x78\x78\x89\x37\x4F\xBE\xB4\xC8\x1B\x17\xBA\x6C\x44";
132
133     printf (" key stream:  ");
134     printhex (buf, sizeof (good_start));
135     printf (" ... cont'd : ");
136     printhex (buf + 0xff020 - sizeof (good_end), sizeof (good_end));
137     if (memcmp (buf, good_start, sizeof (good_start))
138      || memcmp (buf + 0xff020 - sizeof (good_end), good_end,
139                 sizeof (good_end)))
140         fatal ("Key stream test failed");
141     free (buf);
142 }
143
144 static void srtp_test (void)
145 {
146     if (init_libgcrypt ())
147         fatal ("Libgcrypt initialization error");
148     test_derivation ();
149     test_keystream ();
150 }
151
152 int main (void)
153 {
154     srtp_test ();
155     return 0;
156 }