blob: 12e4e8f83ef2430f3a0c287cc19a4c2a8d184ef5 [file] [log] [blame]
Alexandre Lision67916dd2014-01-24 13:33:04 -05001/*
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 *
12 * Copyright (c) 2001-2006, 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 "rdbx.h"
47
48#define rdbx_high_bit_in_bitmask 127
49
50/*
51 * from draft-ietf-avt-srtp-00.txt:
52 *
53 * A receiver reconstructs the index i of a packet with sequence
54 * number s using the estimate
55 *
56 * i = 65,536 * t + s,
57 *
58 * where t is chosen from the set { r-1, r, r+1 } such that i is
59 * closest to the value 65,536 * r + 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
66/*
67 * rdbx implementation notes
68 *
69 * A xtd_seq_num_t is essentially a sequence number for which some of
70 * the data on the wire are implicit. It logically consists of a
71 * rollover counter and a sequence number; the sequence number is the
72 * explicit part, and the rollover counter is the implicit part.
73 *
74 * Upon receiving a sequence_number (e.g. in a newly received SRTP
75 * packet), the complete xtd_seq_num_t can be estimated by using a
76 * local xtd_seq_num_t as a basis. This is done using the function
77 * index_guess(&local, &guess, seq_from_packet). This function
78 * returns the difference of the guess and the local value. The local
79 * xtd_seq_num_t can be moved forward to the guess using the function
80 * index_advance(&guess, delta), where delta is the difference.
81 *
82 *
83 * A rdbx_t consists of a xtd_seq_num_t and a bitmask. The index is highest
84 * sequence number that has been received, and the bitmask indicates
85 * which of the recent indicies have been received as well. The
86 * highest bit in the bitmask corresponds to the index in the bitmask.
87 */
88
89
90void
91index_init(xtd_seq_num_t *pi) {
92#ifdef NO_64BIT_MATH
93 *pi = make64(0,0);
94#else
95 *pi = 0;
96#endif
97}
98
99void
100index_advance(xtd_seq_num_t *pi, sequence_number_t s) {
101#ifdef NO_64BIT_MATH
102 /* a > ~b means a+b will generate a carry */
103 /* s is uint16 here */
104 *pi = make64(high32(*pi) + (s > ~low32(*pi) ? 1 : 0),low32(*pi) + s);
105#else
106 *pi += s;
107#endif
108}
109
110
111/*
112 * index_guess(local, guess, s)
113 *
114 * given a xtd_seq_num_t local (which represents the last
115 * known-to-be-good received xtd_seq_num_t) and a sequence number s
116 * (from a newly arrived packet), sets the contents of *guess to
117 * contain the best guess of the packet index to which s corresponds,
118 * and returns the difference between *guess and *local
119 *
120 * nota bene - the output is a signed integer, DON'T cast it to a
121 * unsigned integer!
122 */
123
124int
125index_guess(const xtd_seq_num_t *local,
126 xtd_seq_num_t *guess,
127 sequence_number_t s) {
128#ifdef NO_64BIT_MATH
129 uint32_t local_roc = ((high32(*local) << 16) |
130 (low32(*local) >> 16));
131 uint16_t local_seq = (uint16_t) (low32(*local));
132#else
133 uint32_t local_roc = (uint32_t)(*local >> 16);
134 uint16_t local_seq = (uint16_t) *local;
135#endif
136#ifdef NO_64BIT_MATH
137 uint32_t guess_roc = ((high32(*guess) << 16) |
138 (low32(*guess) >> 16));
139 uint16_t guess_seq = (uint16_t) (low32(*guess));
140#else
141 uint32_t guess_roc = (uint32_t)(*guess >> 16);
142 uint16_t guess_seq = (uint16_t) *guess;
143#endif
144 int difference;
145
146 if (local_seq < seq_num_median) {
147 if (s - local_seq > seq_num_median) {
148 guess_roc = local_roc - 1;
149 difference = seq_num_max - s + local_seq;
150 } else {
151 guess_roc = local_roc;
152 difference = s - local_seq;
153 }
154 } else {
155 if (local_seq - seq_num_median > s) {
156 guess_roc = local_roc+1;
157 difference = seq_num_max - local_seq + s;
158 } else {
159 difference = s - local_seq;
160 guess_roc = local_roc;
161 }
162 }
163 guess_seq = s;
164
165 /* Note: guess_roc is 32 bits, so this generates a 48-bit result! */
166#ifdef NO_64BIT_MATH
167 *guess = make64(guess_roc >> 16,
168 (guess_roc << 16) | guess_seq);
169#else
170 *guess = (((uint64_t) guess_roc) << 16) | guess_seq;
171#endif
172
173 return difference;
174}
175
176/*
177 * rdbx
178 *
179 */
180
181
182/*
183 * rdbx_init(&r) initalizes the rdbx_t pointed to by r
184 */
185
186err_status_t
187rdbx_init(rdbx_t *rdbx) {
188 v128_set_to_zero(&rdbx->bitmask);
189 index_init(&rdbx->index);
190
191 return err_status_ok;
192}
193
194
195/*
196 * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
197 * which is at rdbx->index + delta is in the rdb
198 */
199
200err_status_t
201rdbx_check(const rdbx_t *rdbx, int delta) {
202
203 if (delta > 0) { /* if delta is positive, it's good */
204 return err_status_ok;
205 } else if (rdbx_high_bit_in_bitmask + delta < 0) {
206 /* if delta is lower than the bitmask, it's bad */
207 return err_status_replay_old;
208 } else if (v128_get_bit(&rdbx->bitmask,
209 rdbx_high_bit_in_bitmask + delta) == 1) {
210 /* delta is within the window, so check the bitmask */
211 return err_status_replay_fail;
212 }
213 /* otherwise, the index is okay */
214
215 return err_status_ok;
216}
217
218/*
219 * rdbx_add_index adds the xtd_seq_num_t at rdbx->window_start + d to
220 * replay_db (and does *not* check if that xtd_seq_num_t appears in db)
221 *
222 * this function should be called only after replay_check has
223 * indicated that the index does not appear in the rdbx, e.g., a mutex
224 * should protect the rdbx between these calls if need be
225 */
226
227err_status_t
228rdbx_add_index(rdbx_t *rdbx, int delta) {
229
230 if (delta > 0) {
231 /* shift forward by delta */
232 index_advance(&rdbx->index, delta);
233 v128_left_shift(&rdbx->bitmask, delta);
234 v128_set_bit(&rdbx->bitmask, 127);
235 } else {
236 /* delta is in window, so flip bit in bitmask */
237 v128_set_bit(&rdbx->bitmask, -delta);
238 }
239
240 /* note that we need not consider the case that delta == 0 */
241
242 return err_status_ok;
243}
244
245
246
247/*
248 * rdbx_estimate_index(rdbx, guess, s)
249 *
250 * given an rdbx and a sequence number s (from a newly arrived packet),
251 * sets the contents of *guess to contain the best guess of the packet
252 * index to which s corresponds, and returns the difference between
253 * *guess and the locally stored synch info
254 */
255
256int
257rdbx_estimate_index(const rdbx_t *rdbx,
258 xtd_seq_num_t *guess,
259 sequence_number_t s) {
260
261 /*
262 * if the sequence number and rollover counter in the rdbx are
263 * non-zero, then use the index_guess(...) function, otherwise, just
264 * set the rollover counter to zero (since the index_guess(...)
265 * function might incorrectly guess that the rollover counter is
266 * 0xffffffff)
267 */
268
269#ifdef NO_64BIT_MATH
270 /* seq_num_median = 0x8000 */
271 if (high32(rdbx->index) > 0 ||
272 low32(rdbx->index) > seq_num_median)
273#else
274 if (rdbx->index > seq_num_median)
275#endif
276 return index_guess(&rdbx->index, guess, s);
277
278#ifdef NO_64BIT_MATH
279 *guess = make64(0,(uint32_t) s);
280#else
281 *guess = s;
282#endif
283
284#ifdef NO_64BIT_MATH
285 return s - (uint16_t) low32(rdbx->index);
286#else
287 return s - (uint16_t) rdbx->index;
288#endif
289}