xref: /netbsd-src/share/man/man9/cprng.9 (revision 4821cee19b87f65dd190602b6e97fc411fdea2e0)
1.\"	$NetBSD: cprng.9,v 1.8 2013/07/18 14:35:30 riastradh Exp $
2.\"
3.\" Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Thor Lancelot Simon and Taylor R. Campbell.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd July 18, 2013
31.Dt CPRNG 9
32.Os
33.Sh NAME
34.Nm cprng ,
35.Nm cprng_strong_create ,
36.Nm cprng_strong_destroy ,
37.Nm cprng_strong ,
38.Nm cprng_strong32 ,
39.Nm cprng_strong64 ,
40.Nm cprng_fast ,
41.Nm cprng_fast32 ,
42.Nm cprng_fast64 ,
43.Nd cryptographic pseudorandom number generators
44.Sh SYNOPSIS
45.In sys/cprng.h
46.Ft cprng_strong_t *
47.Fn cprng_strong_create "const char *name" "int ipl" "int flags"
48.Ft void
49.Fn cprng_strong_destroy "cprng_strong_t *cprng"
50.Ft size_t
51.Fn cprng_strong "cprng_strong_t *cprng" "void *buf" "size_t len" "int flags"
52.Ft uint32_t
53.Fn cprng_strong32 "void"
54.Ft uint64_t
55.Fn cprng_strong64 "void"
56.Ft size_t
57.Fn cprng_fast "void *buf" "size_t len"
58.Ft uint32_t
59.Fn cprng_fast32 "void"
60.Ft uint32_t
61.Fn cprng_fast64 "void"
62.Bd -literal
63#define CPRNG_MAX_LEN   524288
64.Ed
65.Sh DESCRIPTION
66The
67.Nm
68family of functions provide cryptographic pseudorandom number
69generators automatically seeded from the kernel entropy pool.
70They replace the
71.Xr arc4random 9
72and
73.Xr rnd_extract_data 9
74functions for this purpose.
75The
76.Nx
77kernel no longer supports direct reading from the kernel entropy pool; all
78access is mediated by the
79.Nm
80functions.
81.Pp
82The
83.Dq strong
84family of functions use cryptographically strong pseudorandom number
85generators suitable for keying crypto systems and similar purposes.
86Calls to
87.Xr rnd_extract_data 9
88should be replaced by calls to
89.Fn cprng_strong .
90.Pp
91The
92.Dq fast
93family of functions use cryptographically weaker pseudorandom number
94generators suitable for initialization vectors, nonces in certain
95protocols, and other similar purposes, using a faster but less secure
96stream-cipher-based generator.
97Calls to
98.Xr arc4random 9
99should be replaced by calls to
100.Fn cprng_fast32 ,
101and calls to
102.Xr arc4randbytes 9
103should be replaced by calls to
104.Fn cprng_fast .
105.Pp
106A single instance of the fast generator serves the entire kernel.
107A well-known instance of the strong generator,
108.Dv kern_cprng ,
109may be used by any in-kernel caller, but separately seeded instances of
110the strong generator can also be created by calling
111.Fn cprng_strong_create .
112.Sh FUNCTIONS
113.Bl -tag -width abcd
114.It Fn cprng_strong_create "name" "ipl" "flags"
115Create an instance of the cprng_strong generator.
116This generator implements the NIST SP 800-90 CTR_DRBG with AES128 as
117the block transform.
118.Pp
119The
120.Fa name
121argument is used to
122.Dq personalize
123the CTR_DRBG according to the standard, so that its initial state will
124depend both on seed material from the entropy pool and also on the
125personalization string (name).
126.Pp
127The
128.Fa ipl
129argument specifies the interrupt priority level for the mutex which
130will serialize access to the new instance of the generator (see
131.Xr spl 9 ) ,
132and must be no higher than
133.Dv IPL_VM .
134.Pp
135The
136.Fa flags
137argument controls the behavior of the generator:
138.Bl -tag -width CPRNG_REKEY_ANY
139.It Dv CPRNG_INIT_ANY
140Suppress a warning message to the console if, during
141.Fn cprng_strong_create ,
142only partial entropy for the generator is available from the entropy
143pool.
144.It Dv CPRNG_REKEY_ANY
145Suppress a warning message to the console if, during
146.Fn cprng_strong
147after the generator has been exhausted and must be reseeded, only
148partial entropy for the generator is available from the entropy pool.
149.It Dv CPRNG_USE_CV
150Make
151.Fn cprng_strong
152sleep if the generator has not been seeded with full entropy until full
153entropy is available.
154Otherwise,
155.Fn cprng_strong
156will never sleep when passed this generator.
157.It Dv CPRNG_HARD
158Limit the number of bits of output from the generator before reseeding
159to the number of bits in its seed, so that it approximates the
160information-theoretic entropy of its seed.
161Otherwise, the generator may provide many more bits of output than it
162was seeded with.
163.El
164.Pp
165Creation will succeed even if full entropy for the generator is not
166available.
167In this case, the first request to read from the generator may cause
168reseeding.
169.Pp
170.Fn cprng_strong_create
171may sleep to allocate memory.
172.It Fn cprng_strong_destroy "cprng"
173Destroy
174.Fa cprng .
175.Pp
176.Fn cprng_strong_destroy
177may sleep.
178.It Fn cprng_strong "cprng" "buf" "len" "flags"
179Fill memory location
180.Fa buf
181with up to
182.Fa len
183bytes from the generator
184.Fa cprng ,
185and return the number of bytes.
186.Fa len
187must be at most
188.Dv CPRNG_MAX_LEN .
189.Pp
190If
191.Fa cprng
192was created with the
193.Dv CPRNG_USE_CV
194flag and has been exhausted, then
195.Fn cprng_strong
196may sleep until full entropy can be obtained from the entropy pool to
197reseed it.
198However, if
199.Fa flags
200includes the
201.Dv FNONBLOCK
202flag, then
203.Fn cprng_strong
204will immediately return zero in this case instead.
205.Pp
206If
207.Fa cprng
208was created with the
209.Dv CPRNG_HARD
210flag, then
211.Fn cprng_strong
212will return at most as many bytes as are left from its seed size since
213the last reseeding.
214.Pp
215If
216.Fa cprng
217was created with neither the
218.Dv CPRNG_USE_CV
219flag nor the
220.Dv CPRNG_HARD
221flag, then
222.Fn cprng_strong
223is guaranteed to return as many bytes as requested, up to
224.Dv CPRNG_MAX_LEN ,
225without sleeping.
226.It Fn cprng_strong32
227Generate 32 bits using the
228.Dv kern_cprng
229strong generator.
230.Pp
231.Fn cprng_strong32
232does not sleep.
233.It Fn cprng_strong64
234Generate 64 bits using the
235.Dv kern_cprng
236strong generator.
237.Pp
238.Fn cprng_strong64
239does not sleep.
240.It Fn cprng_fast "buf" "len"
241Fill memory location
242.Fa buf
243with
244.Fa len
245bytes from the fast generator.
246.Pp
247.Fn cprng_fast
248does not sleep.
249.It Fn cprng_fast32
250Generate 32 bits using the fast generator.
251.Pp
252.Fn cprng_fast32
253does not sleep.
254.It Fn cprng_fast64
255Generate 64 bits using the fast generator.
256.Pp
257.Fn cprng_fast64
258does not sleep.
259.El
260.Sh CODE REFERENCES
261The cprng API is implemented by
262.Pa sys/kern/subr_cprng.c
263and
264.Pa sys/sys/cprng.h .
265The
266.Dq strong
267generator uses the CTR_DRBG implementation in
268.Pa sys/crypto/nist_ctr_drbg .
269The
270.Dq fast
271generator uses the arc4random implementation in
272.Pa sys/lib/libkern/arc4random.c .
273.Sh SEE ALSO
274.Xr condvar 9 ,
275.Xr rnd 9 ,
276.Xr spl 9
277.Rs
278.%A Elaine Barker
279.%A John Kelsey
280.%T Recommendation for Random Number Generation Using Deterministic Random Bit Generators (Revised)
281.%I National Institute of Standards and Technology
282.%D 2011
283.%O NIST Special Publication 800-90A, Rev 1
284.Re
285.Sh HISTORY
286The cprng family of functions first appeared in
287.Nx 6.0 .
288