replay_driver.c 7.8 KB

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