Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / 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 .\"
33 .Dd April 15, 1997
34 .Dt ARC4RANDOM 3
35 .Os
36 .Sh NAME
37 .Nm arc4random ,
38 .Nm arc4random_stir ,
39 .Nm arc4random_addrandom
40 .Nd arc4 random number generator
41 .Sh LIBRARY
42 .Lb libc
43 .Sh SYNOPSIS
44 .In stdlib.h
45 .Ft u_int32_t
46 .Fn arc4random "void"
47 .Ft void
48 .Fn arc4random_stir "void"
49 .Ft void
50 .Fn arc4random_addrandom "unsigned char *dat" "int datlen"
51 .Sh DESCRIPTION
52 The
53 .Fn arc4random
54 function uses the key stream generator employed by the
55 arc4 cipher, which uses 8*8 8 bit S-Boxes.
56 The S-Boxes
57 can be in about
58 .if t 2\u\s71700\s10\d
59 .if n (2**1700)
60 states. The
61 .Fn arc4random
62 function returns pseudo-random numbers in the range of 0 to
63 .if t 2\u\s731\s10\d\(mi1,
64 .if n (2**31)\(mi1,
65 and therefore has twice the range of
66 .Dv RAND_MAX .
67 .Pp
68 The
69 .Fn arc4random_stir
70 function reads data from
71 .Pa /dev/urandom
72 and uses it to permute the S-Boxes via
73 .Fn arc4random_addrandom .
74 .Pp
75 There is no need to call
76 .Fn arc4random_stir
77 before using
78 .Fn arc4random ,
79 since
80 .Fn arc4random
81 automatically initializes itself.
82 .Sh EXAMPLES
83 The following produces a drop-in replacement for the traditional
84 .Fn rand
85 and
86 .Fn random
87 functions using
88 .Fn arc4random :
89 .Pp
90 .Dl #define foo4random()  (arc4random()  % ((unsigned)RAND_MAX + 1))
91 .Sh SEE ALSO
92 .Xr rand 3 ,
93 .Xr random 3 ,
94 .Xr srandomdev 3
95 .Sh HISTORY
96 .Pa RC4
97 has been designed by RSA Data Security, Inc.
98 It was posted anonymously
99 to the USENET and was confirmed to be equivalent by several sources who
100 had access to the original cipher.
101 Since
102 .Pa RC4
103 used to be a trade secret, the cipher is now referred to as
104 .Pa ARC4 .