replay_driver.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * replay_driver.c
  3. *
  4. * A driver for the replay_database implementation
  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>
  48. #include "rdb.h"
  49. #include "ut_sim.h"
  50. #include "cipher_priv.h"
  51. /*
  52. * num_trials defines the number of trials that are used in the
  53. * validation functions below
  54. */
  55. unsigned num_trials = 1 << 16;
  56. srtp_err_status_t test_rdb_db(void);
  57. double rdb_check_adds_per_second(void);
  58. int main(void)
  59. {
  60. srtp_err_status_t err;
  61. printf("testing anti-replay database (srtp_rdb_t)...\n");
  62. err = test_rdb_db();
  63. if (err) {
  64. printf("failed\n");
  65. exit(1);
  66. }
  67. printf("done\n");
  68. printf("rdb_check/rdb_adds per second: %e\n", rdb_check_adds_per_second());
  69. return 0;
  70. }
  71. void print_rdb(srtp_rdb_t *rdb)
  72. {
  73. printf("rdb: {%u, %s}\n", rdb->window_start,
  74. v128_bit_string(&rdb->bitmask));
  75. }
  76. srtp_err_status_t rdb_check_add(srtp_rdb_t *rdb, uint32_t idx)
  77. {
  78. if (srtp_rdb_check(rdb, idx) != srtp_err_status_ok) {
  79. printf("rdb_check failed at index %u\n", idx);
  80. return srtp_err_status_fail;
  81. }
  82. if (srtp_rdb_add_index(rdb, idx) != srtp_err_status_ok) {
  83. printf("rdb_add_index failed at index %u\n", idx);
  84. return srtp_err_status_fail;
  85. }
  86. return srtp_err_status_ok;
  87. }
  88. srtp_err_status_t rdb_check_expect_failure(srtp_rdb_t *rdb, uint32_t idx)
  89. {
  90. srtp_err_status_t err;
  91. err = srtp_rdb_check(rdb, idx);
  92. if ((err != srtp_err_status_replay_old) &&
  93. (err != srtp_err_status_replay_fail)) {
  94. printf("rdb_check failed at index %u (false positive)\n", idx);
  95. return srtp_err_status_fail;
  96. }
  97. return srtp_err_status_ok;
  98. }
  99. srtp_err_status_t rdb_check_add_unordered(srtp_rdb_t *rdb, uint32_t idx)
  100. {
  101. srtp_err_status_t rstat;
  102. /* printf("index: %u\n", idx); */
  103. rstat = srtp_rdb_check(rdb, idx);
  104. if ((rstat != srtp_err_status_ok) &&
  105. (rstat != srtp_err_status_replay_old)) {
  106. printf("rdb_check_add_unordered failed at index %u\n", idx);
  107. return rstat;
  108. }
  109. if (rstat == srtp_err_status_replay_old) {
  110. return srtp_err_status_ok;
  111. }
  112. if (srtp_rdb_add_index(rdb, idx) != srtp_err_status_ok) {
  113. printf("rdb_add_index failed at index %u\n", idx);
  114. return srtp_err_status_fail;
  115. }
  116. return srtp_err_status_ok;
  117. }
  118. srtp_err_status_t test_rdb_db()
  119. {
  120. srtp_rdb_t rdb;
  121. uint32_t idx, ircvd;
  122. ut_connection utc;
  123. srtp_err_status_t err;
  124. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  125. printf("rdb_init failed\n");
  126. return srtp_err_status_init_fail;
  127. }
  128. /* test sequential insertion */
  129. for (idx = 0; idx < num_trials; idx++) {
  130. err = rdb_check_add(&rdb, idx);
  131. if (err)
  132. return err;
  133. }
  134. /* test for false positives */
  135. for (idx = 0; idx < num_trials; idx++) {
  136. err = rdb_check_expect_failure(&rdb, idx);
  137. if (err)
  138. return err;
  139. }
  140. /* re-initialize */
  141. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  142. printf("rdb_init failed\n");
  143. return srtp_err_status_fail;
  144. }
  145. /* test non-sequential insertion */
  146. ut_init(&utc);
  147. for (idx = 0; idx < num_trials; idx++) {
  148. ircvd = ut_next_index(&utc);
  149. err = rdb_check_add_unordered(&rdb, ircvd);
  150. if (err)
  151. return err;
  152. err = rdb_check_expect_failure(&rdb, ircvd);
  153. if (err)
  154. return err;
  155. }
  156. /* re-initialize */
  157. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  158. printf("rdb_init failed\n");
  159. return srtp_err_status_fail;
  160. }
  161. /* test insertion with large gaps */
  162. for (idx = 0, ircvd = 0; idx < num_trials;
  163. idx++, ircvd += (1 << (srtp_cipher_rand_u32_for_tests() % 10))) {
  164. err = rdb_check_add(&rdb, ircvd);
  165. if (err)
  166. return err;
  167. err = rdb_check_expect_failure(&rdb, ircvd);
  168. if (err)
  169. return err;
  170. }
  171. /* re-initialize */
  172. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  173. printf("rdb_init failed\n");
  174. return srtp_err_status_fail;
  175. }
  176. /* test loss of first 513 packets */
  177. for (idx = 0; idx < num_trials; idx++) {
  178. err = rdb_check_add(&rdb, idx + 513);
  179. if (err)
  180. return err;
  181. }
  182. /* test for false positives */
  183. for (idx = 0; idx < num_trials + 513; idx++) {
  184. err = rdb_check_expect_failure(&rdb, idx);
  185. if (err)
  186. return err;
  187. }
  188. /* test for key expired */
  189. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  190. printf("rdb_init failed\n");
  191. return srtp_err_status_fail;
  192. }
  193. rdb.window_start = 0x7ffffffe;
  194. if (srtp_rdb_increment(&rdb) != srtp_err_status_ok) {
  195. printf("srtp_rdb_increment of 0x7ffffffe failed\n");
  196. return srtp_err_status_fail;
  197. }
  198. if (srtp_rdb_get_value(&rdb) != 0x7fffffff) {
  199. printf("rdb valiue was not 0x7fffffff\n");
  200. return srtp_err_status_fail;
  201. }
  202. if (srtp_rdb_increment(&rdb) != srtp_err_status_key_expired) {
  203. printf("srtp_rdb_increment of 0x7fffffff did not return "
  204. "srtp_err_status_key_expired\n");
  205. return srtp_err_status_fail;
  206. }
  207. if (srtp_rdb_get_value(&rdb) != 0x7fffffff) {
  208. printf("rdb valiue was not 0x7fffffff\n");
  209. return srtp_err_status_fail;
  210. }
  211. return srtp_err_status_ok;
  212. }
  213. #include <time.h> /* for clock() */
  214. #include <stdlib.h> /* for random() */
  215. #define REPLAY_NUM_TRIALS 10000000
  216. double rdb_check_adds_per_second(void)
  217. {
  218. uint32_t i;
  219. srtp_rdb_t rdb;
  220. clock_t timer;
  221. int failures = 0; /* count number of failures */
  222. if (srtp_rdb_init(&rdb) != srtp_err_status_ok) {
  223. printf("rdb_init failed\n");
  224. exit(1);
  225. }
  226. timer = clock();
  227. for (i = 0; i < REPLAY_NUM_TRIALS; i += 3) {
  228. if (srtp_rdb_check(&rdb, i + 2) != srtp_err_status_ok)
  229. ++failures;
  230. if (srtp_rdb_add_index(&rdb, i + 2) != srtp_err_status_ok)
  231. ++failures;
  232. if (srtp_rdb_check(&rdb, i + 1) != srtp_err_status_ok)
  233. ++failures;
  234. if (srtp_rdb_add_index(&rdb, i + 1) != srtp_err_status_ok)
  235. ++failures;
  236. if (srtp_rdb_check(&rdb, i) != srtp_err_status_ok)
  237. ++failures;
  238. if (srtp_rdb_add_index(&rdb, i) != srtp_err_status_ok)
  239. ++failures;
  240. }
  241. timer = clock() - timer;
  242. return (double)CLOCKS_PER_SEC * REPLAY_NUM_TRIALS / timer;
  243. }