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: entropyFileUnix.c
22 Contains: Module to maintain MacYarrow's entropy file, UNIX 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 "entropyFile.h"
41 #include <sys/types.h>
43 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
46 * For now we use the same file location for all builds. Generally for
47 * debugging - when this code is not running as root - you need to do
48 * the following once per system before using this code:
51 * # touch /var/db/SystemEntropyCache
52 * # chmod 666 /var/db/SystemEntropyCache
54 #define DEFAULT_ENTROPY_FILE_PATH "/var/db/SystemEntropyCache"
56 /* NULL ==> use default, else use caller-specified path */
57 static char *entropyFilePath
= NULL
;
59 static OSErr
errNoToOSErr(int err
)
66 /* anything else interesting? */
72 static char *getEntropyFilePath()
75 return entropyFilePath
;
78 return DEFAULT_ENTROPY_FILE_PATH
;
83 * Specify optional entropy file path. If this is never called,
84 * this module will use its own default path.
86 OSErr
setEntropyFilePath(
92 free(entropyFilePath
);
93 entropyFilePath
= NULL
;
103 entropyFilePath
= malloc(len
+ 1);
104 if(entropyFilePath
== NULL
) {
107 memmove(entropyFilePath
, path
, len
+ 1);
112 * Write specified data to entropy file. A new file will be created
113 * if none exists. Existing file's data is replaced with caller's data.
115 OSErr
writeEntropyFile(
123 fd
= open(getEntropyFilePath(), O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
126 errorLog1("writeEntropyFile: open returned %d\n", rtn
);
127 return errNoToOSErr(rtn
);
129 rtn
= lseek(fd
, 0, SEEK_SET
);
132 errorLog1("writeEntropyFile: lseek returned %d\n", rtn
);
133 return errNoToOSErr(rtn
);
135 rtn
= write(fd
, bytes
, (size_t)numBytes
);
136 if(rtn
!= (int)numBytes
) {
138 errorLog1("writeEntropyFile: write() returned %d\n", rtn
);
139 ortn
= errNoToOSErr(errno
);
142 errorLog0("writeEntropyFile(): short write\n");
154 * Read data from entropy file.
156 OSErr
readEntropyFile(
158 UInt32 numBytes
, // max # of bytes to read
159 UInt32
*actualBytes
) // RETURNED - number of bytes actually read
166 fd
= open(getEntropyFilePath(), O_RDONLY
, 0);
169 errorLog1("readEntropyFile: open returned %d\n", rtn
);
170 return errNoToOSErr(rtn
);
172 rtn
= lseek(fd
, 0, SEEK_SET
);
175 errorLog1("readEntropyFile: lseek returned %d\n", rtn
);
176 return errNoToOSErr(rtn
);
178 rtn
= read(fd
, bytes
, (size_t)numBytes
);
180 errorLog1("readEntropyFile: read() returned %d\n", rtn
);
181 ortn
= errNoToOSErr(errno
);
184 *actualBytes
= (UInt32
)rtn
;