]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/YarrowServer/YarrowServer_OS9.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 File: YarrowServer_OS9.c
22 Contains: Yarrow Server, OS 9 version.
24 Written by: Doug Mitchell
26 Copyright: (c) 2000 by Apple Computer, Inc., all rights reserved.
28 Change History (most recent first):
34 #include <yarrowUtils.h>
35 #include "YarrowServer_OS9.h"
36 #include "entropyFile.h"
40 #include <Timer.h> /* Microseconds() */
41 #include <LowMem.h> /* LMGetTicks() */
43 /* the single system-wide yarrow PRNG object */
44 static PrngRef prng
= NULL
;
47 * We collect system entropy every ENTROPY_COLLECT_INTERVAL seconds.
49 #define ENTROPY_COLLECT_INTERVAL (10 * 60)
52 * When collecting system entropy, try for this many bytes.
54 #define SYSTEM_ENTROPY_SIZE 20
57 * Maintain an entropy file of this size.
59 #define ENTROPY_FILE_SIZE 20
62 * Microseconds to crunch in prngAllowReseed()
64 #define RESEED_TICKS 100
68 #pragma mark * * * Private Functions * * *
73 OSErr
_init(void *initBlk
);
84 UInt32
*numBytes
, // RETURNED - number of bytes obtained
85 UInt32
*bitsOfEntropy
); // RETURNED - est. amount of entropy
89 * Called once on initial library load.
94 prng_error_status prtn
;
95 UInt8 entropyFileData
[ENTROPY_FILE_SIZE
];
96 UInt8 sysEntropyData
[SYSTEM_ENTROPY_SIZE
];
101 /* set up prng and its lock */
102 prtn
= prngInitialize(&prng
);
104 errorLog1("_init: prngInitialize returned %s\n", perrorString(prtn
));
105 return perrorToOSErr(prtn
);
108 /* TBD - the mutex */
111 * read entropy file, add contents to system entropy pool.
112 * It's not an error if there is no entropy file; this
113 * should only happen the first time this server runs on a given
116 ortn
= readEntropyFile(entropyFileData
,
119 if((ortn
== noErr
) && (actLen
> 0)) {
120 prtn
= prngInput(prng
,
124 actLen
* 8); // assume total entropy here
126 errorLog1("_init: prngInput returned %s\n",
128 return perrorToOSErr(prtn
);
131 trashMemory(entropyFileData
, actLen
);
134 * collect system entropy, add to system entropy pool
136 systemEntropy(sysEntropyData
,
141 prtn
= prngInput(prng
,
147 errorLog1("_init: prngInput returned %s\n",
149 return perrorToOSErr(prtn
);
152 trashMemory(sysEntropyData
, actLen
);
157 prtn
= prngForceReseed(prng
, RESEED_TICKS
);
159 errorLog1("_init: prngForceReseed returned %s\n",
161 return perrorToOSErr(prtn
);
165 * get 20 bytes of random data, write to entropy file
167 prtn
= prngOutput(prng
, entropyFileData
, ENTROPY_FILE_SIZE
);
169 errorLog1("_init: prngOutput returned %s\n",
171 return perrorToOSErr(prtn
);
173 ortn
= writeEntropyFile(entropyFileData
, ENTROPY_FILE_SIZE
, false);
177 /* FIXME - schedule an entropyCollector() call; */
185 /* free prng and lock */
193 * FIXME - RuntimePPC.dll is referring to this somehow...
197 errorLog0("YarrowServer main() called\n");
202 * Lock/unlock prngMutex - I guess these are not technically necessary
218 * Get some system entropy. On OS 9 this is pretty lame.
224 UInt32
*numBytes
, // RETURNED - number of bytes obtained
225 UInt32
*bitsOfEntropy
) // RETURNED - est. amount of entropy
227 UnsignedWide curTime
; /* low 16 bits are pretty good, use 32 */
228 unsigned ticks
= 0; /* low 8 bits are OK, use 16 bits */
232 Microseconds(&curTime
); /* low 16 bits are pretty good */
233 //ticks = LMGetTicks();
234 *pp
++ = curTime
.lo
& 0xff;
235 *pp
++ = curTime
.lo
>> 8;
236 *pp
++ = curTime
.lo
>> 16;
237 *pp
++ = curTime
.lo
>> 24;
238 *pp
++ = ticks
& 0xff;
243 BlockMove(pool
, buf
, bufSize
);
245 *bitsOfEntropy
= 3 * 8; /* three bytes worth */
249 * Entropy collector - called every ENTROPY_COLLECT_INTERVAL seconds.
254 /* grab some system entropy
257 * if enough time has elapsed {
260 * schedule another call
265 #pragma mark * * * Public Functions * * *
268 * Add some entropy to the pool. The only "known" failure here is a
269 * result of a failure of this library'e early init.
271 OSErr
yarrowAddEntropy(
274 UInt32 bitsOfEntropy
)
276 UInt8 sysEntropy
[SYSTEM_ENTROPY_SIZE
];
278 UInt32 numSysEntropyBits
;
279 prng_error_status prtn
;
287 /* add client entropy */
288 prtn
= prngInput(prng
, bytes
, numBytes
, CLIENT_SOURCE
, bitsOfEntropy
);
290 errorLog1("prngInput returned %s\n", perrorString(prtn
));
295 /* and some system entropy too - this prevents client from overwhelming
296 * the entropy pool with its own (untrusted) data */
297 systemEntropy(sysEntropy
, SYSTEM_ENTROPY_SIZE
, &numSysBytes
,
299 prtn
= prngInput(prng
, sysEntropy
, numSysBytes
, SYSTEM_SOURCE
,
302 errorLog1("prngInput returned %s\n", perrorString(prtn
));
306 prngAllowReseed(prng
, RESEED_TICKS
);
314 * Get some random data. Caller mallocs the memory.
316 OSErr
yarrowGetRandomBytes(
324 prngOutput(prng
, bytes
, numBytes
);