Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: crypto/replay/rdbx.c

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

Powered by Google App Engine
This is Rietveld 408576698