xref: /freebsd/sys/dev/random/random_infra.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2015 Mark R V Murray
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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/random.h>
36 #include <sys/sysctl.h>
37 
38 #include <dev/random/randomdev.h>
39 
40 /* Set up the sysctl root node for the entropy device */
41 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
42     "Cryptographically Secure Random Number Generator");
43 SYSCTL_NODE(_kern_random, OID_AUTO, initial_seeding,
44     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
45     "Initial seeding control and information");
46 
47 /*
48  * N.B., this is a dangerous default, but it matches the behavior prior to
49  * r346250 (and, say, OpenBSD -- although they get some guaranteed saved
50  * entropy from the prior boot because of their KARL system, on RW media).
51  */
52 bool random_bypass_before_seeding = true;
53 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
54     bypass_before_seeding, CTLFLAG_RDTUN, &random_bypass_before_seeding,
55     0, "If set non-zero, bypass the random device in requests for random "
56     "data when the random device is not yet seeded.  This is considered "
57     "dangerous.  Ordinarily, the random device will block requests until "
58     "it is seeded by sufficient entropy.");
59 
60 /*
61  * This is a read-only diagnostic that reports the combination of the former
62  * tunable and actual bypass.  It is intended for programmatic inspection by
63  * userspace administrative utilities after boot.
64  */
65 bool read_random_bypassed_before_seeding = false;
66 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
67     read_random_bypassed_before_seeding, CTLFLAG_RD,
68     &read_random_bypassed_before_seeding, 0, "If non-zero, the random device "
69     "was bypassed because the 'bypass_before_seeding' knob was enabled and a "
70     "request was submitted prior to initial seeding.");
71 
72 /*
73  * This is a read-only diagnostic that reports the combination of the former
74  * tunable and actual bypass for arc4random initial seeding.  It is intended
75  * for programmatic inspection by userspace administrative utilities after
76  * boot.
77  */
78 bool arc4random_bypassed_before_seeding = false;
79 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
80     arc4random_bypassed_before_seeding, CTLFLAG_RD,
81     &arc4random_bypassed_before_seeding, 0, "If non-zero, the random device "
82     "was bypassed when initially seeding the kernel arc4random(9), because "
83     "the 'bypass_before_seeding' knob was enabled and a request was submitted "
84     "prior to initial seeding.");
85 
86 /*
87  * This knob is for users who do not want additional warnings in their logs
88  * because they intend to handle bypass by inspecting the status of the
89  * diagnostic sysctls.
90  */
91 bool random_bypass_disable_warnings = false;
92 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
93     disable_bypass_warnings, CTLFLAG_RDTUN,
94     &random_bypass_disable_warnings, 0, "If non-zero, do not log a warning "
95     "if the 'bypass_before_seeding' knob is enabled and a request is "
96     "submitted prior to initial seeding.");
97 
98 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
99 
100 #if defined(RANDOM_LOADABLE)
101 const struct random_algorithm *p_random_alg_context;
102 void (*_read_random)(void *, u_int);
103 int (*_read_random_uio)(struct uio *, bool);
104 bool (*_is_random_seeded)(void);
105 #endif /* defined(RANDOM_LOADABLE) */
106