2
0

rdb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * rdb.c
  3. *
  4. * Implements a replay database for packet security
  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 "rdb.h"
  48. #define rdb_bits_in_bitmask (8 * sizeof(v128_t))
  49. /*
  50. * this implementation of a replay database works as follows:
  51. *
  52. * window_start is the index of the first packet in the window
  53. * bitmask a bit-buffer, containing the most recently entered
  54. * index as the leftmost bit
  55. *
  56. */
  57. /* srtp_rdb_init initalizes rdb */
  58. srtp_err_status_t srtp_rdb_init(srtp_rdb_t *rdb)
  59. {
  60. v128_set_to_zero(&rdb->bitmask);
  61. rdb->window_start = 0;
  62. return srtp_err_status_ok;
  63. }
  64. /*
  65. * srtp_rdb_check checks to see if index appears in rdb
  66. */
  67. srtp_err_status_t srtp_rdb_check(const srtp_rdb_t *rdb, uint32_t p_index)
  68. {
  69. /* if the index appears after (or at very end of) the window, its good */
  70. if (p_index >= rdb->window_start + rdb_bits_in_bitmask) {
  71. return srtp_err_status_ok;
  72. }
  73. /* if the index appears before the window, its bad */
  74. if (p_index < rdb->window_start) {
  75. return srtp_err_status_replay_old;
  76. }
  77. /* otherwise, the index appears within the window, so check the bitmask */
  78. if (v128_get_bit(&rdb->bitmask, (p_index - rdb->window_start)) == 1) {
  79. return srtp_err_status_replay_fail;
  80. }
  81. /* otherwise, the index is okay */
  82. return srtp_err_status_ok;
  83. }
  84. /*
  85. * srtp_rdb_add_index adds index to srtp_rdb_t (and does *not* check if
  86. * index appears in db)
  87. *
  88. * this function should be called only after srtp_rdb_check has
  89. * indicated that the index does not appear in the rdb, e.g., a mutex
  90. * should protect the rdb between these calls
  91. */
  92. srtp_err_status_t srtp_rdb_add_index(srtp_rdb_t *rdb, uint32_t p_index)
  93. {
  94. unsigned int delta;
  95. if (p_index < rdb->window_start)
  96. return srtp_err_status_replay_fail;
  97. delta = (p_index - rdb->window_start);
  98. if (delta < rdb_bits_in_bitmask) {
  99. /* if the p_index is within the window, set the appropriate bit */
  100. v128_set_bit(&rdb->bitmask, delta);
  101. } else {
  102. delta -= rdb_bits_in_bitmask - 1;
  103. /* shift the window forward by delta bits*/
  104. v128_left_shift(&rdb->bitmask, delta);
  105. v128_set_bit(&rdb->bitmask, rdb_bits_in_bitmask - 1);
  106. rdb->window_start += delta;
  107. }
  108. return srtp_err_status_ok;
  109. }
  110. srtp_err_status_t srtp_rdb_increment(srtp_rdb_t *rdb)
  111. {
  112. if (rdb->window_start >= 0x7fffffff) {
  113. return srtp_err_status_key_expired;
  114. }
  115. ++rdb->window_start;
  116. return srtp_err_status_ok;
  117. }
  118. uint32_t srtp_rdb_get_value(const srtp_rdb_t *rdb)
  119. {
  120. return rdb->window_start;
  121. }