netwerk/srtp/src/crypto/include/rdbx.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * rdbx.h
michael@0 3 *
michael@0 4 * replay database with extended packet indices, using a rollover counter
michael@0 5 *
michael@0 6 * David A. McGrew
michael@0 7 * Cisco Systems, Inc.
michael@0 8 *
michael@0 9 */
michael@0 10 /*
michael@0 11 *
michael@0 12 * Copyright (c) 2001-2006, Cisco Systems, Inc.
michael@0 13 * All rights reserved.
michael@0 14 *
michael@0 15 * Redistribution and use in source and binary forms, with or without
michael@0 16 * modification, are permitted provided that the following conditions
michael@0 17 * are met:
michael@0 18 *
michael@0 19 * Redistributions of source code must retain the above copyright
michael@0 20 * notice, this list of conditions and the following disclaimer.
michael@0 21 *
michael@0 22 * Redistributions in binary form must reproduce the above
michael@0 23 * copyright notice, this list of conditions and the following
michael@0 24 * disclaimer in the documentation and/or other materials provided
michael@0 25 * with the distribution.
michael@0 26 *
michael@0 27 * Neither the name of the Cisco Systems, Inc. nor the names of its
michael@0 28 * contributors may be used to endorse or promote products derived
michael@0 29 * from this software without specific prior written permission.
michael@0 30 *
michael@0 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
michael@0 36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
michael@0 42 * OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 43 *
michael@0 44 */
michael@0 45
michael@0 46 #ifndef RDBX_H
michael@0 47 #define RDBX_H
michael@0 48
michael@0 49 #include "datatypes.h"
michael@0 50 #include "err.h"
michael@0 51
michael@0 52 /* #define ROC_TEST */
michael@0 53
michael@0 54 #ifndef ROC_TEST
michael@0 55
michael@0 56 typedef uint16_t sequence_number_t; /* 16 bit sequence number */
michael@0 57 typedef uint32_t rollover_counter_t; /* 32 bit rollover counter */
michael@0 58
michael@0 59 #else /* use small seq_num and roc datatypes for testing purposes */
michael@0 60
michael@0 61 typedef unsigned char sequence_number_t; /* 8 bit sequence number */
michael@0 62 typedef uint16_t rollover_counter_t; /* 16 bit rollover counter */
michael@0 63
michael@0 64 #endif
michael@0 65
michael@0 66 #define seq_num_median (1 << (8*sizeof(sequence_number_t) - 1))
michael@0 67 #define seq_num_max (1 << (8*sizeof(sequence_number_t)))
michael@0 68
michael@0 69 /*
michael@0 70 * An xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended'
michael@0 71 * sequence number.
michael@0 72 */
michael@0 73
michael@0 74 typedef uint64_t xtd_seq_num_t;
michael@0 75
michael@0 76
michael@0 77 /*
michael@0 78 * An rdbx_t is a replay database with extended range; it uses an
michael@0 79 * xtd_seq_num_t and a bitmask of recently received indices.
michael@0 80 */
michael@0 81
michael@0 82 typedef struct {
michael@0 83 xtd_seq_num_t index;
michael@0 84 bitvector_t bitmask;
michael@0 85 } rdbx_t;
michael@0 86
michael@0 87
michael@0 88 /*
michael@0 89 * rdbx_init(rdbx_ptr, ws)
michael@0 90 *
michael@0 91 * initializes the rdbx pointed to by its argument with the window size ws,
michael@0 92 * setting the rollover counter and sequence number to zero
michael@0 93 */
michael@0 94
michael@0 95 err_status_t
michael@0 96 rdbx_init(rdbx_t *rdbx, unsigned long ws);
michael@0 97
michael@0 98
michael@0 99 /*
michael@0 100 * rdbx_dealloc(rdbx_ptr)
michael@0 101 *
michael@0 102 * frees memory associated with the rdbx
michael@0 103 */
michael@0 104
michael@0 105 err_status_t
michael@0 106 rdbx_dealloc(rdbx_t *rdbx);
michael@0 107
michael@0 108
michael@0 109 /*
michael@0 110 * rdbx_estimate_index(rdbx, guess, s)
michael@0 111 *
michael@0 112 * given an rdbx and a sequence number s (from a newly arrived packet),
michael@0 113 * sets the contents of *guess to contain the best guess of the packet
michael@0 114 * index to which s corresponds, and returns the difference between
michael@0 115 * *guess and the locally stored synch info
michael@0 116 */
michael@0 117
michael@0 118 int
michael@0 119 rdbx_estimate_index(const rdbx_t *rdbx,
michael@0 120 xtd_seq_num_t *guess,
michael@0 121 sequence_number_t s);
michael@0 122
michael@0 123 /*
michael@0 124 * rdbx_check(rdbx, delta);
michael@0 125 *
michael@0 126 * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
michael@0 127 * which is at rdbx->window_start + delta is in the rdb
michael@0 128 *
michael@0 129 */
michael@0 130
michael@0 131 err_status_t
michael@0 132 rdbx_check(const rdbx_t *rdbx, int difference);
michael@0 133
michael@0 134 /*
michael@0 135 * replay_add_index(rdbx, delta)
michael@0 136 *
michael@0 137 * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db
michael@0 138 * (and does *not* check if that xtd_seq_num_t appears in db)
michael@0 139 *
michael@0 140 * this function should be called *only* after replay_check has
michael@0 141 * indicated that the index does not appear in the rdbx, and a mutex
michael@0 142 * should protect the rdbx between these calls if necessary.
michael@0 143 */
michael@0 144
michael@0 145 err_status_t
michael@0 146 rdbx_add_index(rdbx_t *rdbx, int delta);
michael@0 147
michael@0 148
michael@0 149 /*
michael@0 150 * rdbx_set_roc(rdbx, roc) initalizes the rdbx_t at the location rdbx
michael@0 151 * to have the rollover counter value roc. If that value is less than
michael@0 152 * the current rollover counter value, then the function returns
michael@0 153 * err_status_replay_old; otherwise, err_status_ok is returned.
michael@0 154 *
michael@0 155 */
michael@0 156
michael@0 157 err_status_t
michael@0 158 rdbx_set_roc(rdbx_t *rdbx, uint32_t roc);
michael@0 159
michael@0 160 /*
michael@0 161 * rdbx_get_roc(rdbx) returns the value of the rollover counter for
michael@0 162 * the rdbx_t pointed to by rdbx
michael@0 163 *
michael@0 164 */
michael@0 165
michael@0 166 xtd_seq_num_t
michael@0 167 rdbx_get_packet_index(const rdbx_t *rdbx);
michael@0 168
michael@0 169 /*
michael@0 170 * xtd_seq_num_t functions - these are *internal* functions of rdbx, and
michael@0 171 * shouldn't be used to manipulate rdbx internal values. use the rdbx
michael@0 172 * api instead!
michael@0 173 */
michael@0 174
michael@0 175 /*
michael@0 176 * rdbx_get_ws(rdbx_ptr)
michael@0 177 *
michael@0 178 * gets the window size which was used to initialize the rdbx
michael@0 179 */
michael@0 180
michael@0 181 unsigned long
michael@0 182 rdbx_get_window_size(const rdbx_t *rdbx);
michael@0 183
michael@0 184
michael@0 185 /* index_init(&pi) initializes a packet index pi (sets it to zero) */
michael@0 186
michael@0 187 void
michael@0 188 index_init(xtd_seq_num_t *pi);
michael@0 189
michael@0 190 /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
michael@0 191
michael@0 192 void
michael@0 193 index_advance(xtd_seq_num_t *pi, sequence_number_t s);
michael@0 194
michael@0 195
michael@0 196 /*
michael@0 197 * index_guess(local, guess, s)
michael@0 198 *
michael@0 199 * given a xtd_seq_num_t local (which represents the highest
michael@0 200 * known-to-be-good index) and a sequence number s (from a newly
michael@0 201 * arrived packet), sets the contents of *guess to contain the best
michael@0 202 * guess of the packet index to which s corresponds, and returns the
michael@0 203 * difference between *guess and *local
michael@0 204 */
michael@0 205
michael@0 206 int
michael@0 207 index_guess(const xtd_seq_num_t *local,
michael@0 208 xtd_seq_num_t *guess,
michael@0 209 sequence_number_t s);
michael@0 210
michael@0 211
michael@0 212 #endif /* RDBX_H */
michael@0 213
michael@0 214
michael@0 215
michael@0 216
michael@0 217
michael@0 218
michael@0 219
michael@0 220
michael@0 221

mercurial