1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/include/rdbx.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,221 @@ 1.4 +/* 1.5 + * rdbx.h 1.6 + * 1.7 + * replay database with extended packet indices, using a rollover counter 1.8 + * 1.9 + * David A. McGrew 1.10 + * Cisco Systems, Inc. 1.11 + * 1.12 + */ 1.13 +/* 1.14 + * 1.15 + * Copyright (c) 2001-2006, Cisco Systems, Inc. 1.16 + * All rights reserved. 1.17 + * 1.18 + * Redistribution and use in source and binary forms, with or without 1.19 + * modification, are permitted provided that the following conditions 1.20 + * are met: 1.21 + * 1.22 + * Redistributions of source code must retain the above copyright 1.23 + * notice, this list of conditions and the following disclaimer. 1.24 + * 1.25 + * Redistributions in binary form must reproduce the above 1.26 + * copyright notice, this list of conditions and the following 1.27 + * disclaimer in the documentation and/or other materials provided 1.28 + * with the distribution. 1.29 + * 1.30 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.31 + * contributors may be used to endorse or promote products derived 1.32 + * from this software without specific prior written permission. 1.33 + * 1.34 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.35 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.36 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.37 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.38 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.39 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.40 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.41 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.42 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.43 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.44 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.45 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.46 + * 1.47 + */ 1.48 + 1.49 +#ifndef RDBX_H 1.50 +#define RDBX_H 1.51 + 1.52 +#include "datatypes.h" 1.53 +#include "err.h" 1.54 + 1.55 +/* #define ROC_TEST */ 1.56 + 1.57 +#ifndef ROC_TEST 1.58 + 1.59 +typedef uint16_t sequence_number_t; /* 16 bit sequence number */ 1.60 +typedef uint32_t rollover_counter_t; /* 32 bit rollover counter */ 1.61 + 1.62 +#else /* use small seq_num and roc datatypes for testing purposes */ 1.63 + 1.64 +typedef unsigned char sequence_number_t; /* 8 bit sequence number */ 1.65 +typedef uint16_t rollover_counter_t; /* 16 bit rollover counter */ 1.66 + 1.67 +#endif 1.68 + 1.69 +#define seq_num_median (1 << (8*sizeof(sequence_number_t) - 1)) 1.70 +#define seq_num_max (1 << (8*sizeof(sequence_number_t))) 1.71 + 1.72 +/* 1.73 + * An xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended' 1.74 + * sequence number. 1.75 + */ 1.76 + 1.77 +typedef uint64_t xtd_seq_num_t; 1.78 + 1.79 + 1.80 +/* 1.81 + * An rdbx_t is a replay database with extended range; it uses an 1.82 + * xtd_seq_num_t and a bitmask of recently received indices. 1.83 + */ 1.84 + 1.85 +typedef struct { 1.86 + xtd_seq_num_t index; 1.87 + bitvector_t bitmask; 1.88 +} rdbx_t; 1.89 + 1.90 + 1.91 +/* 1.92 + * rdbx_init(rdbx_ptr, ws) 1.93 + * 1.94 + * initializes the rdbx pointed to by its argument with the window size ws, 1.95 + * setting the rollover counter and sequence number to zero 1.96 + */ 1.97 + 1.98 +err_status_t 1.99 +rdbx_init(rdbx_t *rdbx, unsigned long ws); 1.100 + 1.101 + 1.102 +/* 1.103 + * rdbx_dealloc(rdbx_ptr) 1.104 + * 1.105 + * frees memory associated with the rdbx 1.106 + */ 1.107 + 1.108 +err_status_t 1.109 +rdbx_dealloc(rdbx_t *rdbx); 1.110 + 1.111 + 1.112 +/* 1.113 + * rdbx_estimate_index(rdbx, guess, s) 1.114 + * 1.115 + * given an rdbx and a sequence number s (from a newly arrived packet), 1.116 + * sets the contents of *guess to contain the best guess of the packet 1.117 + * index to which s corresponds, and returns the difference between 1.118 + * *guess and the locally stored synch info 1.119 + */ 1.120 + 1.121 +int 1.122 +rdbx_estimate_index(const rdbx_t *rdbx, 1.123 + xtd_seq_num_t *guess, 1.124 + sequence_number_t s); 1.125 + 1.126 +/* 1.127 + * rdbx_check(rdbx, delta); 1.128 + * 1.129 + * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t 1.130 + * which is at rdbx->window_start + delta is in the rdb 1.131 + * 1.132 + */ 1.133 + 1.134 +err_status_t 1.135 +rdbx_check(const rdbx_t *rdbx, int difference); 1.136 + 1.137 +/* 1.138 + * replay_add_index(rdbx, delta) 1.139 + * 1.140 + * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db 1.141 + * (and does *not* check if that xtd_seq_num_t appears in db) 1.142 + * 1.143 + * this function should be called *only* after replay_check has 1.144 + * indicated that the index does not appear in the rdbx, and a mutex 1.145 + * should protect the rdbx between these calls if necessary. 1.146 + */ 1.147 + 1.148 +err_status_t 1.149 +rdbx_add_index(rdbx_t *rdbx, int delta); 1.150 + 1.151 + 1.152 +/* 1.153 + * rdbx_set_roc(rdbx, roc) initalizes the rdbx_t at the location rdbx 1.154 + * to have the rollover counter value roc. If that value is less than 1.155 + * the current rollover counter value, then the function returns 1.156 + * err_status_replay_old; otherwise, err_status_ok is returned. 1.157 + * 1.158 + */ 1.159 + 1.160 +err_status_t 1.161 +rdbx_set_roc(rdbx_t *rdbx, uint32_t roc); 1.162 + 1.163 +/* 1.164 + * rdbx_get_roc(rdbx) returns the value of the rollover counter for 1.165 + * the rdbx_t pointed to by rdbx 1.166 + * 1.167 + */ 1.168 + 1.169 +xtd_seq_num_t 1.170 +rdbx_get_packet_index(const rdbx_t *rdbx); 1.171 + 1.172 +/* 1.173 + * xtd_seq_num_t functions - these are *internal* functions of rdbx, and 1.174 + * shouldn't be used to manipulate rdbx internal values. use the rdbx 1.175 + * api instead! 1.176 + */ 1.177 + 1.178 +/* 1.179 + * rdbx_get_ws(rdbx_ptr) 1.180 + * 1.181 + * gets the window size which was used to initialize the rdbx 1.182 + */ 1.183 + 1.184 +unsigned long 1.185 +rdbx_get_window_size(const rdbx_t *rdbx); 1.186 + 1.187 + 1.188 +/* index_init(&pi) initializes a packet index pi (sets it to zero) */ 1.189 + 1.190 +void 1.191 +index_init(xtd_seq_num_t *pi); 1.192 + 1.193 +/* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */ 1.194 + 1.195 +void 1.196 +index_advance(xtd_seq_num_t *pi, sequence_number_t s); 1.197 + 1.198 + 1.199 +/* 1.200 + * index_guess(local, guess, s) 1.201 + * 1.202 + * given a xtd_seq_num_t local (which represents the highest 1.203 + * known-to-be-good index) and a sequence number s (from a newly 1.204 + * arrived packet), sets the contents of *guess to contain the best 1.205 + * guess of the packet index to which s corresponds, and returns the 1.206 + * difference between *guess and *local 1.207 + */ 1.208 + 1.209 +int 1.210 +index_guess(const xtd_seq_num_t *local, 1.211 + xtd_seq_num_t *guess, 1.212 + sequence_number_t s); 1.213 + 1.214 + 1.215 +#endif /* RDBX_H */ 1.216 + 1.217 + 1.218 + 1.219 + 1.220 + 1.221 + 1.222 + 1.223 + 1.224 +