|
@@ -3785,49 +3785,38 @@ find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options,
|
|
|
}
|
|
|
|
|
|
|
|
|
- * compute an increment. Consuming one byte of entropy per step, we use 7
|
|
|
- * bits to construct an increment between 0 and (127/128)*delay by adding
|
|
|
- * right-shifted copies of delay, controlled by each bit. Then, to prevent
|
|
|
- * getting stuck at zero if we start from zero, we use one last bit to add
|
|
|
- * 1 with probability 50%. Finally, we add the increment to the original
|
|
|
- * delay, clamp the value <= max_delay, and return it.
|
|
|
+ * compute an increment, we construct a value uniformly at random between
|
|
|
+ * delay and MAX(delay*2,delay+1). We then clamp that value to be no larger
|
|
|
+ * than max_delay, and return it.
|
|
|
+ *
|
|
|
+ * Requires that delay is less than INT_MAX, and delay is in [0,max_delay].
|
|
|
*/
|
|
|
STATIC int
|
|
|
next_random_exponential_delay(int delay, int max_delay)
|
|
|
{
|
|
|
- int delay_increment, i;
|
|
|
- uint8_t entropy;
|
|
|
+
|
|
|
+ if (BUG(delay > max_delay))
|
|
|
+ delay = max_delay;
|
|
|
+ if (BUG(delay == INT_MAX))
|
|
|
+ delay -= 1;
|
|
|
+ if (BUG(delay < 0))
|
|
|
+ delay = 0;
|
|
|
+
|
|
|
+
|
|
|
+ int max_increment;
|
|
|
+
|
|
|
+ if (delay)
|
|
|
+ max_increment = delay;
|
|
|
+ else
|
|
|
+ max_increment = 1;
|
|
|
|
|
|
-
|
|
|
- * Backoff step: we want to multiply by something ~1.5, and then add
|
|
|
- * 1 with non-zero probability so we can't get stuck at zero even if
|
|
|
- * we start out with zero delay. To do this, pick a uint8_t of
|
|
|
- * entropy in the range [0,255], and use it to construct an
|
|
|
- * increment.
|
|
|
- */
|
|
|
- delay_increment = 0;
|
|
|
-
|
|
|
- crypto_rand((char *)(&entropy), sizeof(entropy));
|
|
|
-
|
|
|
- entropy &= 0xff;
|
|
|
-
|
|
|
- if (delay > 0) {
|
|
|
-
|
|
|
- for (i = 0; i < 7; ++i) {
|
|
|
- if (entropy & (0x1 << i)) delay_increment += (delay >> (i + 1));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- * Using the remaining bit of entropy, add 1 with probability 50% so
|
|
|
- * we can't get stuck at 0
|
|
|
- */
|
|
|
- if (entropy & 0x80) delay_increment += 1;
|
|
|
-
|
|
|
- if (delay_increment < max_delay - delay) delay += delay_increment;
|
|
|
- else delay = max_delay;
|
|
|
+
|
|
|
+ int increment = crypto_rand_int(max_increment+1);
|
|
|
|
|
|
-
|
|
|
- return delay;
|
|
|
+ if (increment < max_delay - delay)
|
|
|
+ return delay + increment;
|
|
|
+ else
|
|
|
+ return max_delay;
|
|
|
}
|
|
|
|
|
|
|