summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-28 11:39:49 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-28 11:39:49 -0400
commit48ebd9bf76a0e5ff60b88f8906919016de82e819 (patch)
tree76047a0a4227be696a54987d7d6f4430ec4597e3 /src/common
parentaa3edfd2053bb418907f204e171ae8f7e78c30eb (diff)
downloadtor-48ebd9bf76a0e5ff60b88f8906919016de82e819.tar.gz
tor-48ebd9bf76a0e5ff60b88f8906919016de82e819.zip
Move weakrng into lib/intmath
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c42
-rw-r--r--src/common/util.h14
-rw-r--r--src/common/workqueue.c1
3 files changed, 1 insertions, 56 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 4a26998c19..304101ec0e 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -460,48 +460,6 @@ load_windows_system_library(const TCHAR *library_name)
}
#endif /* defined(_WIN32) */
-/** Initialize the insecure RNG <b>rng</b> from a seed value <b>seed</b>. */
-void
-tor_init_weak_random(tor_weak_rng_t *rng, unsigned seed)
-{
- rng->state = (uint32_t)(seed & 0x7fffffff);
-}
-
-/** Return a randomly chosen value in the range 0..TOR_WEAK_RANDOM_MAX based
- * on the RNG state of <b>rng</b>. This entropy will not be cryptographically
- * strong; do not rely on it for anything an adversary should not be able to
- * predict. */
-int32_t
-tor_weak_random(tor_weak_rng_t *rng)
-{
- /* Here's a linear congruential generator. OpenBSD and glibc use these
- * parameters; they aren't too bad, and should have maximal period over the
- * range 0..INT32_MAX. We don't want to use the platform rand() or random(),
- * since some platforms have bad weak RNGs that only return values in the
- * range 0..INT16_MAX, which just isn't enough. */
- rng->state = (rng->state * 1103515245 + 12345) & 0x7fffffff;
- return (int32_t) rng->state;
-}
-
-/** Return a random number in the range [0 , <b>top</b>). {That is, the range
- * of integers i such that 0 <= i < top.} Chooses uniformly. Requires that
- * top is greater than 0. This randomness is not cryptographically strong; do
- * not rely on it for anything an adversary should not be able to predict. */
-int32_t
-tor_weak_random_range(tor_weak_rng_t *rng, int32_t top)
-{
- /* We don't want to just do tor_weak_random() % top, since random() is often
- * implemented with an LCG whose modulus is a power of 2, and those are
- * cyclic in their low-order bits. */
- int divisor, result;
- tor_assert(top > 0);
- divisor = TOR_WEAK_RANDOM_MAX / top;
- do {
- result = (int32_t)(tor_weak_random(rng) / divisor);
- } while (result >= top);
- return result;
-}
-
/** Cast a given double value to a int64_t. Return 0 if number is NaN.
* Returns either INT64_MIN or INT64_MAX if number is outside of the int64_t
* range. */
diff --git a/src/common/util.h b/src/common/util.h
index 1967d23e41..7d75440062 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -95,18 +95,4 @@ int64_t tv_to_msec(const struct timeval *tv);
HANDLE load_windows_system_library(const TCHAR *library_name);
#endif
-/* ===== Insecure rng */
-typedef struct tor_weak_rng_t {
- uint32_t state;
-} tor_weak_rng_t;
-
-#define TOR_WEAK_RNG_INIT {383745623}
-#define TOR_WEAK_RANDOM_MAX (INT_MAX)
-void tor_init_weak_random(tor_weak_rng_t *weak_rng, unsigned seed);
-int32_t tor_weak_random(tor_weak_rng_t *weak_rng);
-int32_t tor_weak_random_range(tor_weak_rng_t *rng, int32_t top);
-/** Randomly return true according to <b>rng</b> with probability 1 in
- * <b>n</b> */
-#define tor_weak_random_one_in_n(rng, n) (0==tor_weak_random_range((rng),(n)))
-
#endif /* !defined(TOR_UTIL_H) */
diff --git a/src/common/workqueue.c b/src/common/workqueue.c
index 4735aadd73..0e8628da98 100644
--- a/src/common/workqueue.c
+++ b/src/common/workqueue.c
@@ -33,6 +33,7 @@
#include "tor_queue.h"
#include "lib/net/alertsock.h"
#include "lib/log/torlog.h"
+#include "lib/intmath/weakrng.h"
#include <event2/event.h>