]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "random.h" | |
19 | ||
78f3f316 | 20 | #include <assert.h> |
064af421 BP |
21 | #include <errno.h> |
22 | #include <stdlib.h> | |
23 | #include <sys/time.h> | |
24 | ||
10df718e | 25 | #include "entropy.h" |
279c9e03 | 26 | #include "timeval.h" |
064af421 BP |
27 | #include "util.h" |
28 | ||
e519ea28 BP |
29 | /* This is the 32-bit PRNG recommended in G. Marsaglia, "Xorshift RNGs", |
30 | * _Journal of Statistical Software_ 8:14 (July 2003). According to the paper, | |
31 | * it has a period of 2**32 - 1 and passes almost all tests of randomness. | |
32 | * | |
33 | * We use this PRNG instead of libc's rand() because rand() varies in quality | |
34 | * and because its maximum value also varies between 32767 and INT_MAX, whereas | |
10df718e BP |
35 | * we often want random numbers in the full range of uint32_t. |
36 | * | |
37 | * This random number generator is intended for purposes that do not require | |
38 | * cryptographic-quality randomness. */ | |
e519ea28 BP |
39 | |
40 | /* Current random state. */ | |
41 | static uint32_t seed; | |
42 | ||
43 | static uint32_t random_next(void); | |
44 | ||
064af421 BP |
45 | void |
46 | random_init(void) | |
47 | { | |
10df718e | 48 | while (!seed) { |
064af421 | 49 | struct timeval tv; |
10df718e | 50 | uint32_t entropy; |
e519ea28 | 51 | |
279c9e03 | 52 | xgettimeofday(&tv); |
10df718e | 53 | get_entropy_or_die(&entropy, 4); |
e519ea28 | 54 | |
10df718e | 55 | seed = tv.tv_sec ^ tv.tv_usec ^ entropy; |
064af421 BP |
56 | } |
57 | } | |
58 | ||
78f3f316 BP |
59 | void |
60 | random_set_seed(uint32_t seed_) | |
61 | { | |
62 | assert(seed_); | |
63 | seed = seed_; | |
64 | } | |
65 | ||
064af421 BP |
66 | void |
67 | random_bytes(void *p_, size_t n) | |
68 | { | |
69 | uint8_t *p = p_; | |
e519ea28 | 70 | |
064af421 | 71 | random_init(); |
e519ea28 BP |
72 | |
73 | for (; n > 4; p += 4, n -= 4) { | |
74 | uint32_t x = random_next(); | |
75 | memcpy(p, &x, 4); | |
76 | } | |
77 | ||
78 | if (n) { | |
79 | uint32_t x = random_next(); | |
80 | memcpy(p, &x, n); | |
064af421 BP |
81 | } |
82 | } | |
83 | ||
84 | uint8_t | |
85 | random_uint8(void) | |
86 | { | |
e519ea28 | 87 | return random_uint32(); |
064af421 BP |
88 | } |
89 | ||
90 | uint16_t | |
91 | random_uint16(void) | |
92 | { | |
e519ea28 | 93 | return random_uint32(); |
064af421 BP |
94 | } |
95 | ||
96 | uint32_t | |
97 | random_uint32(void) | |
98 | { | |
e519ea28 BP |
99 | random_init(); |
100 | return random_next(); | |
064af421 BP |
101 | } |
102 | ||
ddc4f8e2 BP |
103 | uint64_t |
104 | random_uint64(void) | |
105 | { | |
106 | uint64_t x; | |
107 | ||
108 | random_init(); | |
109 | ||
110 | x = random_next(); | |
111 | x |= (uint64_t) random_next() << 32; | |
112 | return x; | |
113 | } | |
114 | ||
064af421 | 115 | int |
e519ea28 | 116 | random_range(int max) |
064af421 BP |
117 | { |
118 | return random_uint32() % max; | |
119 | } | |
e519ea28 BP |
120 | |
121 | static uint32_t | |
122 | random_next(void) | |
123 | { | |
124 | seed ^= seed << 13; | |
7918636f | 125 | seed ^= seed >> 17; |
e519ea28 BP |
126 | seed ^= seed << 5; |
127 | ||
128 | return seed; | |
129 | } |