xref: /freebsd/sys/dev/random/random_infra.c (revision 315ee00f)
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 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/random.h>
34 #include <sys/sysctl.h>
35 
36 #include <dev/random/randomdev.h>
37 
38 /* Set up the sysctl root node for the entropy device */
39 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
40     "Cryptographically Secure Random Number Generator");
41 SYSCTL_NODE(_kern_random, OID_AUTO, initial_seeding,
42     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
43     "Initial seeding control and information");
44 
45 /*
46  * N.B., this is a dangerous default, but it matches the behavior prior to
47  * r346250 (and, say, OpenBSD -- although they get some guaranteed saved
48  * entropy from the prior boot because of their KARL system, on RW media).
49  */
50 bool random_bypass_before_seeding = true;
51 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
52     bypass_before_seeding, CTLFLAG_RDTUN, &random_bypass_before_seeding,
53     0, "If set non-zero, bypass the random device in requests for random "
54     "data when the random device is not yet seeded.  This is considered "
55     "dangerous.  Ordinarily, the random device will block requests until "
56     "it is seeded by sufficient entropy.");
57 
58 /*
59  * This is a read-only diagnostic that reports the combination of the former
60  * tunable and actual bypass.  It is intended for programmatic inspection by
61  * userspace administrative utilities after boot.
62  */
63 bool read_random_bypassed_before_seeding = false;
64 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
65     read_random_bypassed_before_seeding, CTLFLAG_RD,
66     &read_random_bypassed_before_seeding, 0, "If non-zero, the random device "
67     "was bypassed because the 'bypass_before_seeding' knob was enabled and a "
68     "request was submitted prior to initial seeding.");
69 
70 /*
71  * This is a read-only diagnostic that reports the combination of the former
72  * tunable and actual bypass for arc4random initial seeding.  It is intended
73  * for programmatic inspection by userspace administrative utilities after
74  * boot.
75  */
76 bool arc4random_bypassed_before_seeding = false;
77 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
78     arc4random_bypassed_before_seeding, CTLFLAG_RD,
79     &arc4random_bypassed_before_seeding, 0, "If non-zero, the random device "
80     "was bypassed when initially seeding the kernel arc4random(9), because "
81     "the 'bypass_before_seeding' knob was enabled and a request was submitted "
82     "prior to initial seeding.");
83 
84 /*
85  * This knob is for users who do not want additional warnings in their logs
86  * because they intend to handle bypass by inspecting the status of the
87  * diagnostic sysctls.
88  */
89 bool random_bypass_disable_warnings = false;
90 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
91     disable_bypass_warnings, CTLFLAG_RDTUN,
92     &random_bypass_disable_warnings, 0, "If non-zero, do not log a warning "
93     "if the 'bypass_before_seeding' knob is enabled and a request is "
94     "submitted prior to initial seeding.");
95 
96 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
97 
98 #if defined(RANDOM_LOADABLE)
99 const struct random_algorithm *p_random_alg_context;
100 void (*_read_random)(void *, u_int);
101 int (*_read_random_uio)(struct uio *, bool);
102 bool (*_is_random_seeded)(void);
103 #endif /* defined(RANDOM_LOADABLE) */
104