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