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