rdbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * rdbx.c
  3. *
  4. * a replay database with extended range, using a rollover counter
  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 "rdbx.h"
  48. /*
  49. * from RFC 3711:
  50. *
  51. * A receiver reconstructs the index i of a packet with sequence
  52. * number SEQ using the estimate
  53. *
  54. * i = 2^16 * v + SEQ,
  55. *
  56. * where v is chosen from the set { ROC-1, ROC, ROC+1 } such that i is
  57. * closest to the value 2^16 * ROC + s_l. If the value r+1 is used,
  58. * then the rollover counter r in the cryptographic context is
  59. * incremented by one (if the packet containing s is authentic).
  60. */
  61. /*
  62. * rdbx implementation notes
  63. *
  64. * A srtp_xtd_seq_num_t is essentially a sequence number for which some of
  65. * the data on the wire are implicit. It logically consists of a
  66. * rollover counter and a sequence number; the sequence number is the
  67. * explicit part, and the rollover counter is the implicit part.
  68. *
  69. * Upon receiving a sequence_number (e.g. in a newly received SRTP
  70. * packet), the complete srtp_xtd_seq_num_t can be estimated by using a
  71. * local srtp_xtd_seq_num_t as a basis. This is done using the function
  72. * srtp_index_guess(&local, &guess, seq_from_packet). This function
  73. * returns the difference of the guess and the local value. The local
  74. * srtp_xtd_seq_num_t can be moved forward to the guess using the function
  75. * srtp_index_advance(&guess, delta), where delta is the difference.
  76. *
  77. *
  78. * A srtp_rdbx_t consists of a srtp_xtd_seq_num_t and a bitmask. The index is
  79. * highest sequence number that has been received, and the bitmask indicates
  80. * which of the recent indicies have been received as well. The
  81. * highest bit in the bitmask corresponds to the index in the bitmask.
  82. */
  83. void srtp_index_init(srtp_xtd_seq_num_t *pi)
  84. {
  85. #ifdef NO_64BIT_MATH
  86. *pi = make64(0, 0);
  87. #else
  88. *pi = 0;
  89. #endif
  90. }
  91. void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s)
  92. {
  93. #ifdef NO_64BIT_MATH
  94. /* a > ~b means a+b will generate a carry */
  95. /* s is uint16 here */
  96. *pi = make64(high32(*pi) + (s > ~low32(*pi) ? 1 : 0), low32(*pi) + s);
  97. #else
  98. *pi += s;
  99. #endif
  100. }
  101. /*
  102. * srtp_index_guess(local, guess, s)
  103. *
  104. * given a srtp_xtd_seq_num_t local (which represents the last
  105. * known-to-be-good received srtp_xtd_seq_num_t) and a sequence number s
  106. * (from a newly arrived packet), sets the contents of *guess to
  107. * contain the best guess of the packet index to which s corresponds,
  108. * and returns the difference between *guess and *local
  109. *
  110. * nota bene - the output is a signed integer, DON'T cast it to a
  111. * unsigned integer!
  112. */
  113. int32_t srtp_index_guess(const srtp_xtd_seq_num_t *local,
  114. srtp_xtd_seq_num_t *guess,
  115. srtp_sequence_number_t s)
  116. {
  117. #ifdef NO_64BIT_MATH
  118. uint32_t local_roc = ((high32(*local) << 16) | (low32(*local) >> 16));
  119. uint16_t local_seq = (uint16_t)(low32(*local));
  120. #else
  121. uint32_t local_roc = (uint32_t)(*local >> 16);
  122. uint16_t local_seq = (uint16_t)*local;
  123. #endif
  124. uint32_t guess_roc;
  125. uint16_t guess_seq;
  126. int32_t difference;
  127. if (local_seq < seq_num_median) {
  128. if (s - local_seq > seq_num_median) {
  129. guess_roc = local_roc - 1;
  130. difference = s - local_seq - seq_num_max;
  131. } else {
  132. guess_roc = local_roc;
  133. difference = s - local_seq;
  134. }
  135. } else {
  136. if (local_seq - seq_num_median > s) {
  137. guess_roc = local_roc + 1;
  138. difference = s - local_seq + seq_num_max;
  139. } else {
  140. guess_roc = local_roc;
  141. difference = s - local_seq;
  142. }
  143. }
  144. guess_seq = s;
  145. /* Note: guess_roc is 32 bits, so this generates a 48-bit result! */
  146. #ifdef NO_64BIT_MATH
  147. *guess = make64(guess_roc >> 16, (guess_roc << 16) | guess_seq);
  148. #else
  149. *guess = (((uint64_t)guess_roc) << 16) | guess_seq;
  150. #endif
  151. return difference;
  152. }
  153. /*
  154. * rdbx
  155. *
  156. */
  157. /*
  158. * srtp_rdbx_init(&r, ws) initializes the srtp_rdbx_t pointed to by r with
  159. * window size ws
  160. */
  161. srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, unsigned long ws)
  162. {
  163. if (ws == 0) {
  164. return srtp_err_status_bad_param;
  165. }
  166. if (bitvector_alloc(&rdbx->bitmask, ws) != 0) {
  167. return srtp_err_status_alloc_fail;
  168. }
  169. srtp_index_init(&rdbx->index);
  170. return srtp_err_status_ok;
  171. }
  172. /*
  173. * srtp_rdbx_dealloc(&r) frees memory for the srtp_rdbx_t pointed to by r
  174. */
  175. srtp_err_status_t srtp_rdbx_dealloc(srtp_rdbx_t *rdbx)
  176. {
  177. bitvector_dealloc(&rdbx->bitmask);
  178. return srtp_err_status_ok;
  179. }
  180. /*
  181. * srtp_rdbx_set_roc(rdbx, roc) initalizes the srtp_rdbx_t at the location rdbx
  182. * to have the rollover counter value roc. If that value is less than
  183. * the current rollover counter value, then the function returns
  184. * srtp_err_status_replay_old; otherwise, srtp_err_status_ok is returned.
  185. *
  186. */
  187. srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc)
  188. {
  189. bitvector_set_to_zero(&rdbx->bitmask);
  190. #ifdef NO_64BIT_MATH
  191. #error not yet implemented
  192. #else
  193. /* make sure that we're not moving backwards */
  194. if (roc < (rdbx->index >> 16)) {
  195. return srtp_err_status_replay_old;
  196. }
  197. rdbx->index &= 0xffff; /* retain lowest 16 bits */
  198. rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
  199. #endif
  200. return srtp_err_status_ok;
  201. }
  202. /*
  203. * srtp_rdbx_get_packet_index(rdbx) returns the value of the packet index
  204. * for the srtp_rdbx_t pointed to by rdbx
  205. *
  206. */
  207. srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx)
  208. {
  209. return rdbx->index;
  210. }
  211. /*
  212. * srtp_rdbx_get_window_size(rdbx) returns the value of the window size
  213. * for the srtp_rdbx_t pointed to by rdbx
  214. *
  215. */
  216. unsigned long srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx)
  217. {
  218. return bitvector_get_length(&rdbx->bitmask);
  219. }
  220. /*
  221. * srtp_rdbx_check(&r, delta) checks to see if the srtp_xtd_seq_num_t
  222. * which is at rdbx->index + delta is in the rdb
  223. */
  224. srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, int delta)
  225. {
  226. if (delta > 0) { /* if delta is positive, it's good */
  227. return srtp_err_status_ok;
  228. } else if ((int)(bitvector_get_length(&rdbx->bitmask) - 1) + delta < 0) {
  229. /* if delta is lower than the bitmask, it's bad */
  230. return srtp_err_status_replay_old;
  231. } else if (bitvector_get_bit(
  232. &rdbx->bitmask,
  233. (int)(bitvector_get_length(&rdbx->bitmask) - 1) + delta) ==
  234. 1) {
  235. /* delta is within the window, so check the bitmask */
  236. return srtp_err_status_replay_fail;
  237. }
  238. /* otherwise, the index is okay */
  239. return srtp_err_status_ok;
  240. }
  241. /*
  242. * srtp_rdbx_add_index adds the srtp_xtd_seq_num_t at rdbx->window_start + d to
  243. * replay_db (and does *not* check if that srtp_xtd_seq_num_t appears in db)
  244. *
  245. * this function should be called only after replay_check has
  246. * indicated that the index does not appear in the rdbx, e.g., a mutex
  247. * should protect the rdbx between these calls if need be
  248. */
  249. srtp_err_status_t srtp_rdbx_add_index(srtp_rdbx_t *rdbx, int delta)
  250. {
  251. if (delta > 0) {
  252. /* shift forward by delta */
  253. srtp_index_advance(&rdbx->index, (srtp_sequence_number_t)delta);
  254. bitvector_left_shift(&rdbx->bitmask, delta);
  255. bitvector_set_bit(&rdbx->bitmask,
  256. bitvector_get_length(&rdbx->bitmask) - 1);
  257. } else {
  258. /* delta is in window */
  259. bitvector_set_bit(&rdbx->bitmask,
  260. bitvector_get_length(&rdbx->bitmask) - 1 + delta);
  261. }
  262. /* note that we need not consider the case that delta == 0 */
  263. return srtp_err_status_ok;
  264. }
  265. /*
  266. * srtp_rdbx_estimate_index(rdbx, guess, s)
  267. *
  268. * given an rdbx and a sequence number s (from a newly arrived packet),
  269. * sets the contents of *guess to contain the best guess of the packet
  270. * index to which s corresponds, and returns the difference between
  271. * *guess and the locally stored synch info
  272. */
  273. int32_t srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx,
  274. srtp_xtd_seq_num_t *guess,
  275. srtp_sequence_number_t s)
  276. {
  277. /*
  278. * if the sequence number and rollover counter in the rdbx are
  279. * non-zero, then use the srtp_index_guess(...) function, otherwise, just
  280. * set the rollover counter to zero (since the srtp_index_guess(...)
  281. * function might incorrectly guess that the rollover counter is
  282. * 0xffffffff)
  283. */
  284. #ifdef NO_64BIT_MATH
  285. /* seq_num_median = 0x8000 */
  286. if (high32(rdbx->index) > 0 || low32(rdbx->index) > seq_num_median)
  287. #else
  288. if (rdbx->index > seq_num_median)
  289. #endif
  290. {
  291. return srtp_index_guess(&rdbx->index, guess, s);
  292. }
  293. #ifdef NO_64BIT_MATH
  294. *guess = make64(0, (uint32_t)s);
  295. #else
  296. *guess = s;
  297. #endif
  298. #ifdef NO_64BIT_MATH
  299. return s - (uint16_t)low32(rdbx->index);
  300. #else
  301. return s - (uint16_t)rdbx->index;
  302. #endif
  303. }
  304. /*
  305. * srtp_rdbx_get_roc(rdbx)
  306. *
  307. * Get the current rollover counter
  308. *
  309. */
  310. uint32_t srtp_rdbx_get_roc(const srtp_rdbx_t *rdbx)
  311. {
  312. uint32_t roc;
  313. #ifdef NO_64BIT_MATH
  314. roc = ((high32(rdbx->index) << 16) | (low32(rdbx->index) >> 16));
  315. #else
  316. roc = (uint32_t)(rdbx->index >> 16);
  317. #endif
  318. return roc;
  319. }
  320. /*
  321. * srtp_rdbx_set_roc_seq(rdbx, roc, seq) initalizes the srtp_rdbx_t at the
  322. * location rdbx to have the rollover counter value roc and packet sequence
  323. * number seq. If the new rollover counter value is less than the current
  324. * rollover counter value, then the function returns
  325. * srtp_err_status_replay_old, otherwise, srtp_err_status_ok is returned.
  326. */
  327. srtp_err_status_t srtp_rdbx_set_roc_seq(srtp_rdbx_t *rdbx,
  328. uint32_t roc,
  329. uint16_t seq)
  330. {
  331. #ifdef NO_64BIT_MATH
  332. #error not yet implemented
  333. #else
  334. /* make sure that we're not moving backwards */
  335. if (roc < (rdbx->index >> 16)) {
  336. return srtp_err_status_replay_old;
  337. }
  338. rdbx->index = seq;
  339. rdbx->index |= ((uint64_t)roc) << 16; /* set ROC */
  340. #endif
  341. bitvector_set_to_zero(&rdbx->bitmask);
  342. return srtp_err_status_ok;
  343. }