summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libc/crypt/arc4random.3108
1 files changed, 45 insertions, 63 deletions
diff --git a/src/lib/libc/crypt/arc4random.3 b/src/lib/libc/crypt/arc4random.3
index 3cc26bbf18..c74c9cc121 100644
--- a/src/lib/libc/crypt/arc4random.3
+++ b/src/lib/libc/crypt/arc4random.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: arc4random.3,v 1.29 2013/06/05 03:39:22 tedu Exp $ 1.\" $OpenBSD: arc4random.3,v 1.30 2013/10/21 20:35:09 deraadt Exp $
2.\" 2.\"
3.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 3.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4.\" All rights reserved. 4.\" All rights reserved.
@@ -30,16 +30,14 @@
30.\" 30.\"
31.\" Manual page, using -mandoc macros 31.\" Manual page, using -mandoc macros
32.\" 32.\"
33.Dd $Mdocdate: June 5 2013 $ 33.Dd $Mdocdate: October 21 2013 $
34.Dt ARC4RANDOM 3 34.Dt ARC4RANDOM 3
35.Os 35.Os
36.Sh NAME 36.Sh NAME
37.Nm arc4random , 37.Nm arc4random ,
38.Nm arc4random_buf , 38.Nm arc4random_buf ,
39.Nm arc4random_uniform , 39.Nm arc4random_uniform
40.Nm arc4random_stir , 40.Nd random number generator
41.Nm arc4random_addrandom
42.Nd arc4 random number generator
43.Sh SYNOPSIS 41.Sh SYNOPSIS
44.In stdlib.h 42.In stdlib.h
45.Ft u_int32_t 43.Ft u_int32_t
@@ -48,65 +46,54 @@
48.Fn arc4random_buf "void *buf" "size_t nbytes" 46.Fn arc4random_buf "void *buf" "size_t nbytes"
49.Ft u_int32_t 47.Ft u_int32_t
50.Fn arc4random_uniform "u_int32_t upper_bound" 48.Fn arc4random_uniform "u_int32_t upper_bound"
51.Ft void
52.Fn arc4random_stir "void"
53.Ft void
54.Fn arc4random_addrandom "u_char *dat" "int datlen"
55.Sh DESCRIPTION 49.Sh DESCRIPTION
56The 50This family of functions provides higher quality data than those
57.Fn arc4random 51described in
58function provides a high quality 32-bit pseudo-random
59number very quickly.
60.Fn arc4random
61seeds itself on a regular basis from the kernel strong random number
62subsystem described in
63.Xr random 4 .
64On each call, an ARC4 generator is used to generate a new result.
65The
66.Fn arc4random
67function uses the ARC4 cipher key stream generator,
68which uses 8*8 8-bit S-Boxes.
69The S-Boxes can be in about (2**1700) states.
70.Pp
71.Fn arc4random
72provides a cryptographically secure pseudo-random number generator,
73as opposed to the fast but poor quality interfaces described in
74.Xr rand 3 , 52.Xr rand 3 ,
75.Xr random 3 , 53.Xr random 3 ,
76and 54and
77.Xr drand48 3 . 55.Xr drand48 3 .
78.Pp 56.Pp
57Use of these functions is encouraged for almost all random number
58consumption because the other interfaces are deficient in either
59quality, portability, standardization, or availability.
60These functions can be called in almost all coding environments,
61including
62.Xr pthreads 3
63and
64.Xr chroot 2 .
65.Pp
66High quality 32-bit pseudo-random numbers are generated very quickly.
67On each call, a cryptographic pseudo-random number generator is used
68to generate a new result.
69One data pool is used for all consumers in a process, so that consumption
70under program flow can act as additional stirring.
71The subsystem is re-seeded from the kernel random number subsystem using
72.Xr sysctl 3
73on a regular basis, and also upon
74.Xr fork 2 .
75.Pp
76The
77.Fn arc4random
78function returns a single 32-bit value.
79.Pp
79.Fn arc4random_buf 80.Fn arc4random_buf
80fills the region 81fills the region
81.Fa buf 82.Fa buf
82of length 83of length
83.Fa nbytes 84.Fa nbytes
84with ARC4-derived random data. 85with random data.
85.Pp 86.Pp
86.Fn arc4random_uniform 87.Fn arc4random_uniform
87will return a uniformly distributed random number less than 88will return a single 32-bit value, uniformly distributed but less than
88.Fa upper_bound . 89.Fa upper_bound .
89.Fn arc4random_uniform 90This is recommended over constructions like
90is recommended over constructions like
91.Dq Li arc4random() % upper_bound 91.Dq Li arc4random() % upper_bound
92as it avoids "modulo bias" when the upper bound is not a power of two. 92as it avoids "modulo bias" when the upper bound is not a power of two.
93In the worst case, this function may consume multiple iterations
94to ensure uniformity; see the source code to understand the problem
95and solution.
93.Pp 96.Pp
94The
95.Fn arc4random_stir
96function reads data using
97.Xr sysctl 3
98from
99.Va kern.arandom
100and uses it to permute the S-Boxes via
101.Fn arc4random_addrandom .
102.Pp
103There is no need to call
104.Fn arc4random_stir
105before using
106.Fn arc4random ,
107since
108.Fn arc4random
109automatically initializes itself.
110.Sh RETURN VALUES 97.Sh RETURN VALUES
111These functions are always successful, and no return value is 98These functions are always successful, and no return value is
112reserved to indicate an error. 99reserved to indicate an error.
@@ -115,21 +102,16 @@ reserved to indicate an error.
115.Xr rand48 3 , 102.Xr rand48 3 ,
116.Xr random 3 103.Xr random 3
117.Sh HISTORY 104.Sh HISTORY
118An algorithm called
119.Pa RC4
120was designed by RSA Data Security, Inc.
121It was considered a trade secret.
122Because it was a trade secret, it obviously could not be patented.
123A clone of this was posted anonymously to USENET and confirmed to
124be equivalent by several sources who had access to the original cipher.
125Because of the trade secret situation, RSA Data Security, Inc. could
126do nothing about the release of the
127.Ql Alleged RC4
128algorithm.
129Since
130.Pa RC4
131was trademarked, the cipher is now referred to as
132.Pa ARC4 .
133.Pp 105.Pp
134These functions first appeared in 106These functions first appeared in
135.Ox 2.1 . 107.Ox 2.1 .
108.Pp
109The original version of this random number generator used the
110RC4 (also known as ARC4) algorithm.
111In
112.Ox 5.5 ,
113it was replaced with the ChaCha20 cipher, and it may be replaced
114again in the future as cryptographic techniques advance.
115techniques advance.
116A good mnemonic is
117.Dq A Replacement Call for Random .