]> git.saurik.com Git - apple/securityd.git/blob - src/entropy.cpp
securityd-26232.tar.gz
[apple/securityd.git] / src / entropy.cpp
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // EntropyManager - manage entropy on the system.
27 //
28 // Here is our mission:
29 // (1) On startup, read the entropy file and seed it into the RNG for initial use
30 // (2) Periodically, collect entropy from the system and seed it into the RNG
31 // (3) Once in a while, take entropy from the RNG and write it to the entropy file
32 // for use across reboots.
33 //
34 // This class will fail to operate if the process has (and retains) root privileges.
35 // We re-open the entropy file on each use so that we don't work with a "phantom"
36 // file that some fool administrator removed yesterday.
37 //
38 #include "entropy.h"
39 #include <sys/sysctl.h>
40 #include <mach/clock_types.h>
41 #include <errno.h>
42 #include <security_utilities/logging.h>
43 #include <sys/sysctl.h>
44 #include <security_utilities/debugging.h>
45
46 /* when true, action() called every 15 seconds */
47 #define ENTROPY_QUICK_UPDATE 0
48 #if ENTROPY_QUICK_UPDATE
49 #define COLLECT_INTERVAL 15
50 #else
51 #define COLLECT_INTERVAL collectInterval
52 #endif //ENTROPY_QUICK_UPDATE
53
54 using namespace UnixPlusPlus;
55
56
57 //
58 // During construction, we perform initial entropy file recovery.
59 //
60 EntropyManager::EntropyManager(MachPlusPlus::MachServer &srv, const char *entropyFile)
61 : DevRandomGenerator(true), server(srv),
62 mEntropyFilePath(entropyFile), mNextUpdate(Time::now())
63 {
64 // Read the entropy file and seed the RNG. It is not an error if we can't find one.
65 try {
66 AutoFileDesc oldEntropyFile(entropyFile, O_RDONLY);
67 char buffer[entropyFileSize];
68 if (size_t size = oldEntropyFile.read(buffer))
69 addEntropy(buffer, size);
70 } catch (...) { }
71
72 // go through a collect/update/reschedule cycle immediately
73 action();
74 }
75
76
77 //
78 // Timer action
79 //
80 void EntropyManager::action()
81 {
82 collectEntropy();
83 updateEntropyFile();
84
85 server.setTimer(this, Time::Interval(COLLECT_INTERVAL)); // drifting reschedule (desired)
86 }
87
88
89 //
90 // Collect system timings and seed into the RNG.
91 // Note that the sysctl will block until the buffer is full or the timeout expires.
92 // We currently use a 1ms timeout, which almost always fills the buffer and
93 // does not provide enough of a delay to worry about it. If we ever get worried,
94 // we could call longTermActivity on the server object to get another thread going.
95 //
96 void EntropyManager::collectEntropy()
97 {
98 int mib[4];
99 mib[0] = CTL_KERN;
100 mib[1] = KERN_KDEBUG;
101 mib[2] = KERN_KDGETENTROPY;
102 mib[3] = 1; // milliseconds maximum delay
103 mach_timespec_t timings[timingsToCollect];
104 size_t size = sizeof(timings);
105 int ret = sysctl(mib, 4, timings, &size, NULL, 0);
106 if (ret == -1) {
107 Syslog::alert("entropy collection failed (errno=%d)", errno);
108 return;
109 }
110 char buffer[timingsToCollect];
111 for (unsigned n = 0; n < size; n++)
112 buffer[n] = timings[n].tv_nsec; // truncating to LSB
113 secdebug("entropy", "Entropy size %d: %02x %02x %02x %02x %02x %02x %02x %02x...",
114 (int)size,
115 (unsigned char)buffer[0], (unsigned char)buffer[1], (unsigned char)buffer[2],
116 (unsigned char)buffer[3], (unsigned char)buffer[4], (unsigned char)buffer[5],
117 (unsigned char)buffer[6], (unsigned char)buffer[7]);
118 addEntropy(buffer, size);
119 }
120
121
122 //
123 // (Re)write the entropy file with random data pulled from the RNG
124 //
125 void EntropyManager::updateEntropyFile()
126 {
127 if (Time::now() >= mNextUpdate) {
128 try {
129 mNextUpdate = Time::now() + Time::Interval(updateInterval);
130 secdebug("entropy", "updating %s", mEntropyFilePath.c_str());
131 char buffer[entropyFileSize];
132 random(buffer, entropyFileSize);
133 AutoFileDesc entropyFile(mEntropyFilePath.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0600);
134 if (entropyFile.write(buffer) != entropyFileSize)
135 Syslog::warning("short write on entropy file %s", mEntropyFilePath.c_str());
136 } catch (...) {
137 Syslog::warning("error writing entropy file %s", mEntropyFilePath.c_str());
138 }
139 }
140 }
141