]>
git.saurik.com Git - apple/securityd.git/blob - src/entropy.cpp
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 // EntropyManager - manage entropy on the system.
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.
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.
39 #include <sys/sysctl.h>
40 #include <mach/clock_types.h>
42 #include <security_utilities/logging.h>
43 #include <sys/sysctl.h>
44 #include <security_utilities/debugging.h>
46 /* when true, action() called every 15 seconds */
47 #define ENTROPY_QUICK_UPDATE 0
48 #if ENTROPY_QUICK_UPDATE
49 #define COLLECT_INTERVAL 15
51 #define COLLECT_INTERVAL collectInterval
52 #endif //ENTROPY_QUICK_UPDATE
54 using namespace UnixPlusPlus
;
58 // During construction, we perform initial entropy file recovery.
60 EntropyManager::EntropyManager(MachPlusPlus::MachServer
&srv
, const char *entropyFile
)
61 : DevRandomGenerator(true), server(srv
),
62 mEntropyFilePath(entropyFile
), mNextUpdate(Time::now())
64 // Read the entropy file and seed the RNG. It is not an error if we can't find one.
66 AutoFileDesc
oldEntropyFile(entropyFile
, O_RDONLY
);
67 char buffer
[entropyFileSize
];
68 if (size_t size
= oldEntropyFile
.read(buffer
))
69 addEntropy(buffer
, size
);
72 // go through a collect/update/reschedule cycle immediately
80 void EntropyManager::action()
85 server
.setTimer(this, Time::Interval(COLLECT_INTERVAL
)); // drifting reschedule (desired)
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.
96 void EntropyManager::collectEntropy()
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);
107 Syslog::alert("entropy collection failed (errno=%d)", errno
);
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...",
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
);
123 // (Re)write the entropy file with random data pulled from the RNG
125 void EntropyManager::updateEntropyFile()
127 if (Time::now() >= mNextUpdate
) {
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());
137 Syslog::warning("error writing entropy file %s", mEntropyFilePath
.c_str());