2
0

stat_driver.c 8.7 KB

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