• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * stat-driver.c
3  *
4  * test driver for the stat_test functions
5  *
6  * David A. McGrew
7  * Cisco Systems, Inc.
8  */
9 
10 /*
11  *
12  * Copyright (c) 2001-2017, Cisco Systems, Inc.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  *
19  *   Redistributions of source code must retain the above copyright
20  *   notice, this list of conditions and the following disclaimer.
21  *
22  *   Redistributions in binary form must reproduce the above
23  *   copyright notice, this list of conditions and the following
24  *   disclaimer in the documentation and/or other materials provided
25  *   with the distribution.
26  *
27  *   Neither the name of the Cisco Systems, Inc. nor the names of its
28  *   contributors may be used to endorse or promote products derived
29  *   from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49 
50 #include <stdio.h> /* for printf() */
51 
52 #include "err.h"
53 #include "stat.h"
54 #include "srtp.h"
55 
56 #include "cipher.h"
57 #include "cipher_priv.h"
58 
err_check(srtp_err_status_t s)59 void err_check(srtp_err_status_t s)
60 {
61     if (s) {
62         printf("error (code %d)\n", s);
63         exit(1);
64     }
65 }
66 
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69     uint8_t buffer[2532];
70     unsigned int buf_len = 2500;
71     int i, j;
72     extern srtp_cipher_type_t srtp_aes_icm_128;
73     extern srtp_cipher_type_t srtp_aes_icm_256;
74 #ifdef GCM
75     extern srtp_cipher_type_t srtp_aes_gcm_128;
76     extern srtp_cipher_type_t srtp_aes_gcm_256;
77 #endif
78     srtp_cipher_t *c;
79     /* clang-format off */
80     uint8_t key[46] = {
81         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
82         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
83         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
84         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
85         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
86         0x00, 0x01, 0x02, 0x03, 0x04, 0x05
87     };
88     /* clang-format on */
89     v128_t nonce;
90     int num_trials = 500;
91     int num_fail;
92 
93     printf("statistical tests driver\n");
94 
95     v128_set_to_zero(&nonce);
96     for (i = 0; i < 2500; i++)
97         buffer[i] = 0;
98 
99     /* run tests */
100     printf("running stat_tests on all-null buffer, expecting failure\n");
101     printf("monobit %d\n", stat_test_monobit(buffer));
102     printf("poker   %d\n", stat_test_poker(buffer));
103     printf("runs    %d\n", stat_test_runs(buffer));
104 
105     srtp_cipher_rand_for_tests(buffer, 2500);
106     printf("running stat_tests on rand(), expecting success\n");
107     printf("monobit %d\n", stat_test_monobit(buffer));
108     printf("poker   %d\n", stat_test_poker(buffer));
109     printf("runs    %d\n", stat_test_runs(buffer));
110 
111     printf("running stat_tests on AES-128-ICM, expecting success\n");
112     /* set buffer to cipher output */
113     for (i = 0; i < 2500; i++)
114         buffer[i] = 0;
115     err_check(srtp_cipher_type_alloc(&srtp_aes_icm_128, &c,
116                                      SRTP_AES_ICM_128_KEY_LEN_WSALT, 0));
117     err_check(srtp_cipher_init(c, key));
118     err_check(srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
119     err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
120     /* run tests on cipher outout */
121     printf("monobit %d\n", stat_test_monobit(buffer));
122     printf("poker   %d\n", stat_test_poker(buffer));
123     printf("runs    %d\n", stat_test_runs(buffer));
124 
125     printf("runs test (please be patient): ");
126     fflush(stdout);
127     num_fail = 0;
128     v128_set_to_zero(&nonce);
129     for (j = 0; j < num_trials; j++) {
130         for (i = 0; i < 2500; i++)
131             buffer[i] = 0;
132         nonce.v32[3] = i;
133         err_check(
134             srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
135         err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
136         if (stat_test_runs(buffer)) {
137             num_fail++;
138         }
139     }
140 
141     printf("%d failures in %d tests\n", num_fail, num_trials);
142     printf("(nota bene: a small fraction of stat_test failures does not \n"
143            "indicate that the random source is invalid)\n");
144 
145     err_check(srtp_cipher_dealloc(c));
146 
147     printf("running stat_tests on AES-256-ICM, expecting success\n");
148     /* set buffer to cipher output */
149     for (i = 0; i < 2500; i++)
150         buffer[i] = 0;
151     err_check(srtp_cipher_type_alloc(&srtp_aes_icm_256, &c,
152                                      SRTP_AES_ICM_256_KEY_LEN_WSALT, 0));
153     err_check(srtp_cipher_init(c, key));
154     err_check(srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
155     err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
156     /* run tests on cipher outout */
157     printf("monobit %d\n", stat_test_monobit(buffer));
158     printf("poker   %d\n", stat_test_poker(buffer));
159     printf("runs    %d\n", stat_test_runs(buffer));
160 
161     printf("runs test (please be patient): ");
162     fflush(stdout);
163     num_fail = 0;
164     v128_set_to_zero(&nonce);
165     for (j = 0; j < num_trials; j++) {
166         for (i = 0; i < 2500; i++)
167             buffer[i] = 0;
168         nonce.v32[3] = i;
169         err_check(
170             srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
171         err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
172         if (stat_test_runs(buffer)) {
173             num_fail++;
174         }
175     }
176 
177 #ifdef GCM
178     {
179         printf("running stat_tests on AES-128-GCM, expecting success\n");
180         /* set buffer to cipher output */
181         for (i = 0; i < 2500; i++) {
182             buffer[i] = 0;
183         }
184         err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_128, &c,
185                                          SRTP_AES_GCM_128_KEY_LEN_WSALT, 8));
186         err_check(srtp_cipher_init(c, key));
187         err_check(
188             srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
189         err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
190         /* run tests on cipher outout */
191         printf("monobit %d\n", stat_test_monobit(buffer));
192         printf("poker   %d\n", stat_test_poker(buffer));
193         printf("runs    %d\n", stat_test_runs(buffer));
194         fflush(stdout);
195         num_fail = 0;
196         v128_set_to_zero(&nonce);
197         for (j = 0; j < num_trials; j++) {
198             for (i = 0; i < 2500; i++) {
199                 buffer[i] = 0;
200             }
201             nonce.v32[3] = i;
202             err_check(srtp_cipher_set_iv(c, (uint8_t *)&nonce,
203                                          srtp_direction_encrypt));
204             err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
205             buf_len = 2500;
206             if (stat_test_runs(buffer)) {
207                 num_fail++;
208             }
209         }
210 
211         printf("running stat_tests on AES-256-GCM, expecting success\n");
212         /* set buffer to cipher output */
213         for (i = 0; i < 2500; i++) {
214             buffer[i] = 0;
215         }
216         err_check(srtp_cipher_type_alloc(&srtp_aes_gcm_256, &c,
217                                          SRTP_AES_GCM_256_KEY_LEN_WSALT, 16));
218         err_check(srtp_cipher_init(c, key));
219         err_check(
220             srtp_cipher_set_iv(c, (uint8_t *)&nonce, srtp_direction_encrypt));
221         err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
222         /* run tests on cipher outout */
223         printf("monobit %d\n", stat_test_monobit(buffer));
224         printf("poker   %d\n", stat_test_poker(buffer));
225         printf("runs    %d\n", stat_test_runs(buffer));
226         fflush(stdout);
227         num_fail = 0;
228         v128_set_to_zero(&nonce);
229         for (j = 0; j < num_trials; j++) {
230             for (i = 0; i < 2500; i++) {
231                 buffer[i] = 0;
232             }
233             nonce.v32[3] = i;
234             err_check(srtp_cipher_set_iv(c, (uint8_t *)&nonce,
235                                          srtp_direction_encrypt));
236             err_check(srtp_cipher_encrypt(c, buffer, &buf_len));
237             buf_len = 2500;
238             if (stat_test_runs(buffer)) {
239                 num_fail++;
240             }
241         }
242     }
243 #endif
244 
245     printf("%d failures in %d tests\n", num_fail, num_trials);
246     printf("(nota bene: a small fraction of stat_test failures does not \n"
247            "indicate that the random source is invalid)\n");
248 
249     err_check(srtp_cipher_dealloc(c));
250 
251     return 0;
252 }
253