diff options
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
-rw-r--r-- | src/lib/libc/stdlib/random.3 | 110 |
1 files changed, 56 insertions, 54 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 index b3bc560ad3..ad92b60387 100644 --- a/src/lib/libc/stdlib/random.3 +++ b/src/lib/libc/stdlib/random.3 | |||
@@ -25,14 +25,15 @@ | |||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
26 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
27 | .\" | 27 | .\" |
28 | .\" $OpenBSD: random.3,v 1.26 2014/11/25 17:40:38 millert Exp $ | 28 | .\" $OpenBSD: random.3,v 1.27 2014/12/08 21:45:20 deraadt Exp $ |
29 | .\" | 29 | .\" |
30 | .Dd $Mdocdate: November 25 2014 $ | 30 | .Dd $Mdocdate: December 8 2014 $ |
31 | .Dt RANDOM 3 | 31 | .Dt RANDOM 3 |
32 | .Os | 32 | .Os |
33 | .Sh NAME | 33 | .Sh NAME |
34 | .Nm random , | 34 | .Nm random , |
35 | .Nm srandom , | 35 | .Nm srandom , |
36 | .Nm srandom_deterministic , | ||
36 | .Nm srandomdev , | 37 | .Nm srandomdev , |
37 | .Nm initstate , | 38 | .Nm initstate , |
38 | .Nm setstate | 39 | .Nm setstate |
@@ -44,6 +45,8 @@ | |||
44 | .Ft void | 45 | .Ft void |
45 | .Fn srandom "unsigned int seed" | 46 | .Fn srandom "unsigned int seed" |
46 | .Ft void | 47 | .Ft void |
48 | .Fn srandom_deterministic "unsigned int seed" | ||
49 | .Ft void | ||
47 | .Fn srandomdev void | 50 | .Fn srandomdev void |
48 | .Ft char * | 51 | .Ft char * |
49 | .Fn initstate "unsigned int seed" "char *state" "size_t n" | 52 | .Fn initstate "unsigned int seed" "char *state" "size_t n" |
@@ -51,58 +54,48 @@ | |||
51 | .Fn setstate "char *state" | 54 | .Fn setstate "char *state" |
52 | .Sh DESCRIPTION | 55 | .Sh DESCRIPTION |
53 | .Bf -symbolic | 56 | .Bf -symbolic |
54 | This interface is not cryptographically secure, so consider using | 57 | Standards insist that this interface return deterministic results. |
55 | .Xr arc4random 3 | 58 | Unsafe usage is very common, so |
56 | instead. | 59 | .Ox |
60 | changed the subsystem to return non-deterministic results by default. | ||
57 | .Ef | 61 | .Ef |
58 | .Pp | 62 | .Pp |
59 | The | 63 | To satisfy portable code, |
60 | .Fn random | ||
61 | function uses a non-linear additive feedback random number generator employing | ||
62 | a default table of size 31 long integers to return successive pseudo-random | ||
63 | numbers in the range from 0 to (2**31)\-1. | ||
64 | The period of this random number generator is very large, approximately | ||
65 | 16*((2**31)\-1). | ||
66 | .Pp | ||
67 | The | ||
68 | .Fn random | ||
69 | and | ||
70 | .Fn srandom | 64 | .Fn srandom |
71 | functions have (almost) the same calling sequence and initialization | 65 | or |
72 | properties as | 66 | .Fn srandomdev |
73 | .Xr rand 3 Ns / Ns Xr srand 3 . | 67 | may be called to initialize the subsystem. |
74 | The difference is that | 68 | In |
75 | .Xr rand | 69 | .Ox |
76 | produces a much less random sequence \(em in fact, the low dozen bits | 70 | the |
77 | generated by rand go through a cyclic pattern. | 71 | .Ar seed |
78 | All the bits generated by | 72 | variable is ignored, and strong random number results will be provided from |
73 | .Xr arc4random 3. | ||
74 | In other systems, the | ||
75 | .Ar seed | ||
76 | variable primes a simplistic deterministic algorithm. | ||
77 | .Pp | ||
78 | If the standardized behavior is required | ||
79 | .Fn srandom_deterministic | ||
80 | can be substituted for | ||
81 | .Fn srandom , | ||
82 | then subsequent | ||
79 | .Fn random | 83 | .Fn random |
80 | are usable. | 84 | calls will return results using the deterministic algorithm. |
81 | For example, | ||
82 | .Sq Li random()&01 | ||
83 | will produce a random binary | ||
84 | value. | ||
85 | .Pp | 85 | .Pp |
86 | Like | 86 | In non-deterministic (default) mode, the |
87 | .Xr rand 3 , | ||
88 | .Fn random | 87 | .Fn random |
89 | will by default produce a sequence of numbers that can be duplicated | 88 | function returns results from |
90 | by calling | 89 | .Xr arc4random 3 |
91 | .Fn srandom | 90 | in the range from 0 to (2**31)\-1. |
92 | with | ||
93 | .Ql 1 | ||
94 | as the seed. | ||
95 | .Pp | 91 | .Pp |
96 | The | 92 | In deterministic mode, the |
97 | .Fn srandomdev | 93 | .Fn random |
98 | routine switches to an algorithm using state derived from | 94 | function uses a non-linear additive feedback random number generator employing |
99 | random numbers obtained from the kernel. | 95 | a default table of size 31 long integers to return successive pseudo-random |
100 | Note that this particular seeding procedure can generate | 96 | numbers in the range from 0 to (2**31)\-1. |
101 | states which are impossible to reproduce by calling | 97 | The period of this random number generator is very large, approximately |
102 | .Fn srandom | 98 | 16*((2**31)\-1), but the results are a deterministic sequence from the seed. |
103 | with any value, since the succeeding terms in the | ||
104 | state buffer are no longer derived from the LC algorithm applied to | ||
105 | a fixed seed. | ||
106 | .Pp | 99 | .Pp |
107 | The | 100 | The |
108 | .Fn initstate | 101 | .Fn initstate |
@@ -151,9 +144,12 @@ and | |||
151 | is that the size of the state array does not have to be remembered after | 144 | is that the size of the state array does not have to be remembered after |
152 | it is initialized. | 145 | it is initialized. |
153 | .Pp | 146 | .Pp |
154 | With 256 bytes of state information, the period of the random number | 147 | Use of |
155 | generator is greater than 2**69 | 148 | .Fn srandom_deterministic , |
156 | which should be sufficient for most purposes. | 149 | .Fn initstate , |
150 | or | ||
151 | .Fn setstate | ||
152 | forces the subsystem into deterministic mode. | ||
157 | .Sh DIAGNOSTICS | 153 | .Sh DIAGNOSTICS |
158 | If | 154 | If |
159 | .Fn initstate | 155 | .Fn initstate |
@@ -169,7 +165,6 @@ messages are printed on the standard error output. | |||
169 | .Sh STANDARDS | 165 | .Sh STANDARDS |
170 | The | 166 | The |
171 | .Fn random , | 167 | .Fn random , |
172 | .Fn srandom , | ||
173 | .Fn initstate , | 168 | .Fn initstate , |
174 | and | 169 | and |
175 | .Fn setstate | 170 | .Fn setstate |
@@ -177,14 +172,21 @@ functions conform to | |||
177 | .St -xpg4.2 . | 172 | .St -xpg4.2 . |
178 | .Pp | 173 | .Pp |
179 | The | 174 | The |
175 | .Fn srandom | ||
176 | function does not conform to | ||
177 | .St -xpg4.2 , | ||
178 | intentionally. | ||
179 | .Pp | ||
180 | The | ||
180 | .Fn srandomdev | 181 | .Fn srandomdev |
181 | function is an extension. | 182 | function is an extension. |
183 | .Pp | ||
184 | The | ||
185 | .Fn srandom_deterministic | ||
186 | function is an OpenBSD extension. | ||
182 | .Sh HISTORY | 187 | .Sh HISTORY |
183 | These | 188 | These |
184 | functions appeared in | 189 | functions appeared in |
185 | .Bx 4.2 . | 190 | .Bx 4.2 . |
186 | .Sh AUTHORS | 191 | .Sh AUTHORS |
187 | .An Earl T. Cohen | 192 | .An Earl T. Cohen |
188 | .Sh BUGS | ||
189 | The historical implementation used to have very weak seeding. | ||
190 | As a result, the random sequence did not vary much with the seed. | ||