Merge from vendor branch LESS:
[dragonfly.git] / lib / libc / gen / arc4random.3
1 .\" $OpenBSD: arc4random.3,v 1.2 1997/04/27 22:40:25 angelos Exp $
2 .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"      This product includes software developed by Niels Provos.
16 .\" 4. The name of the author may not be used to endorse or promote products
17 .\"    derived from this software without specific prior written permission.
18 .\"
19 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 .\"
30 .\" Manual page, using -mandoc macros
31 .\" $FreeBSD: src/lib/libc/gen/arc4random.3,v 1.8.2.5 2003/05/06 21:40:19 trhodes Exp $
32 .\" $DragonFly: src/lib/libc/gen/arc4random.3,v 1.3 2005/04/26 10:16:16 joerg Exp $
33 .\"
34 .Dd April 15, 1997
35 .Dt ARC4RANDOM 3
36 .Os
37 .Sh NAME
38 .Nm arc4random ,
39 .Nm arc4random_stir ,
40 .Nm arc4random_addrandom
41 .Nd arc4 random number generator
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In stdlib.h
46 .Ft uint32_t
47 .Fn arc4random "void"
48 .Ft void
49 .Fn arc4random_stir "void"
50 .Ft void
51 .Fn arc4random_addrandom "uint8_t *dat" "size_t datlen"
52 .Sh DESCRIPTION
53 The
54 .Fn arc4random
55 function uses the key stream generator employed by the
56 arc4 cipher, which uses 8*8 8 bit S-Boxes.
57 The S-Boxes
58 can be in about
59 .if t 2\u\s71700\s10\d
60 .if n (2**1700)
61 states. The
62 .Fn arc4random
63 function returns pseudo-random numbers in the range of 0 to
64 .if t 2\u\s731\s10\d\(mi1,
65 .if n (2**31)\(mi1,
66 and therefore has twice the range of
67 .Dv RAND_MAX .
68 .Pp
69 The
70 .Fn arc4random_stir
71 function reads data from
72 .Pa /dev/urandom
73 and uses it to permute the S-Boxes via
74 .Fn arc4random_addrandom .
75 .Pp
76 There is no need to call
77 .Fn arc4random_stir
78 before using
79 .Fn arc4random ,
80 since
81 .Fn arc4random
82 automatically initializes itself.
83 .Sh EXAMPLES
84 The following produces a drop-in replacement for the traditional
85 .Fn rand
86 and
87 .Fn random
88 functions using
89 .Fn arc4random :
90 .Pp
91 .Dl #define foo4random()  (arc4random()  % ((unsigned)RAND_MAX + 1))
92 .Sh SEE ALSO
93 .Xr rand 3 ,
94 .Xr random 3 ,
95 .Xr srandomdev 3
96 .Sh HISTORY
97 .Pa RC4
98 has been designed by RSA Data Security, Inc.
99 It was posted anonymously
100 to the USENET and was confirmed to be equivalent by several sources who
101 had access to the original cipher.
102 Since
103 .Pa RC4
104 used to be a trade secret, the cipher is now referred to as
105 .Pa ARC4 .