rdbx.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * rdbx.h
  3. *
  4. * replay database with extended packet indices, using a rollover counter
  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. #ifndef RDBX_H
  46. #define RDBX_H
  47. #include "datatypes.h"
  48. #include "err.h"
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /* #define ROC_TEST */
  53. #ifndef ROC_TEST
  54. typedef uint16_t srtp_sequence_number_t; /* 16 bit sequence number */
  55. typedef uint32_t srtp_rollover_counter_t; /* 32 bit rollover counter */
  56. #else /* use small seq_num and roc datatypes for testing purposes */
  57. typedef unsigned char srtp_sequence_number_t; /* 8 bit sequence number */
  58. typedef uint16_t srtp_rollover_counter_t; /* 16 bit rollover counter */
  59. #endif
  60. #define seq_num_median (1 << (8 * sizeof(srtp_sequence_number_t) - 1))
  61. #define seq_num_max (1 << (8 * sizeof(srtp_sequence_number_t)))
  62. /*
  63. * An rtp_xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended'
  64. * sequence number.
  65. */
  66. typedef uint64_t srtp_xtd_seq_num_t;
  67. /*
  68. * An srtp_rdbx_t is a replay database with extended range; it uses an
  69. * xtd_seq_num_t and a bitmask of recently received indices.
  70. */
  71. typedef struct {
  72. srtp_xtd_seq_num_t index;
  73. bitvector_t bitmask;
  74. } srtp_rdbx_t;
  75. /*
  76. * srtp_rdbx_init(rdbx_ptr, ws)
  77. *
  78. * initializes the rdbx pointed to by its argument with the window size ws,
  79. * setting the rollover counter and sequence number to zero
  80. */
  81. srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, unsigned long ws);
  82. /*
  83. * srtp_rdbx_dealloc(rdbx_ptr)
  84. *
  85. * frees memory associated with the rdbx
  86. */
  87. srtp_err_status_t srtp_rdbx_dealloc(srtp_rdbx_t *rdbx);
  88. /*
  89. * srtp_rdbx_estimate_index(rdbx, guess, s)
  90. *
  91. * given an rdbx and a sequence number s (from a newly arrived packet),
  92. * sets the contents of *guess to contain the best guess of the packet
  93. * index to which s corresponds, and returns the difference between
  94. * *guess and the locally stored synch info
  95. */
  96. int32_t srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx,
  97. srtp_xtd_seq_num_t *guess,
  98. srtp_sequence_number_t s);
  99. /*
  100. * srtp_rdbx_check(rdbx, delta);
  101. *
  102. * srtp_rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
  103. * which is at rdbx->window_start + delta is in the rdb
  104. *
  105. */
  106. srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, int difference);
  107. /*
  108. * srtp_replay_add_index(rdbx, delta)
  109. *
  110. * adds the srtp_xtd_seq_num_t at rdbx->window_start + delta to replay_db
  111. * (and does *not* check if that xtd_seq_num_t appears in db)
  112. *
  113. * this function should be called *only* after replay_check has
  114. * indicated that the index does not appear in the rdbx, and a mutex
  115. * should protect the rdbx between these calls if necessary.
  116. */
  117. srtp_err_status_t srtp_rdbx_add_index(srtp_rdbx_t *rdbx, int delta);
  118. /*
  119. * srtp_rdbx_set_roc(rdbx, roc) initalizes the srtp_rdbx_t at the location rdbx
  120. * to have the rollover counter value roc. If that value is less than
  121. * the current rollover counter value, then the function returns
  122. * srtp_err_status_replay_old; otherwise, srtp_err_status_ok is returned.
  123. *
  124. */
  125. srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc);
  126. /*
  127. * srtp_rdbx_get_packet_index(rdbx) returns the value of the rollover counter
  128. * for
  129. * the srtp_rdbx_t pointed to by rdbx
  130. *
  131. */
  132. srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx);
  133. /*
  134. * srtp_xtd_seq_num_t functions - these are *internal* functions of rdbx, and
  135. * shouldn't be used to manipulate rdbx internal values. use the rdbx
  136. * api instead!
  137. */
  138. /*
  139. * srtp_rdbx_get_ws(rdbx_ptr)
  140. *
  141. * gets the window size which was used to initialize the rdbx
  142. */
  143. unsigned long srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx);
  144. /* index_init(&pi) initializes a packet index pi (sets it to zero) */
  145. void srtp_index_init(srtp_xtd_seq_num_t *pi);
  146. /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
  147. void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s);
  148. /*
  149. * srtp_index_guess(local, guess, s)
  150. *
  151. * given a srtp_xtd_seq_num_t local (which represents the highest
  152. * known-to-be-good index) and a sequence number s (from a newly
  153. * arrived packet), sets the contents of *guess to contain the best
  154. * guess of the packet index to which s corresponds, and returns the
  155. * difference between *guess and *local
  156. */
  157. int32_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
  158. srtp_xtd_seq_num_t *guess,
  159. srtp_sequence_number_t s);
  160. /*
  161. * srtp_rdbx_get_roc(rdbx)
  162. *
  163. * Get the current rollover counter
  164. *
  165. */
  166. uint32_t srtp_rdbx_get_roc(const srtp_rdbx_t *rdbx);
  167. /*
  168. * srtp_rdbx_set_roc_seq(rdbx, roc, seq) initalizes the srtp_rdbx_t at the
  169. * location rdbx to have the rollover counter value roc and packet sequence
  170. * number seq. If the new rollover counter value is less than the current
  171. * rollover counter value, then the function returns
  172. * srtp_err_status_replay_old, otherwise, srtp_err_status_ok is returned.
  173. */
  174. srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx,
  175. uint32_t roc,
  176. uint16_t seq);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif /* RDBX_H */