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