]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/platform.c
Security-58286.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_cryptkit / lib / platform.c
1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
2 *
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
10 *
11 * platform.c - platform-dependent C functions
12 *
13 * Revision History
14 * ----------------
15 * 6 Sep 96 at NeXT
16 * Created.
17 */
18
19 #include "platform.h"
20 #include <stdio.h>
21 #include "feeDebug.h"
22 #ifdef NeXT
23
24 /*
25 * OpenStep....
26 */
27 void CKRaise(const char *reason) {
28 #if FEE_DEBUG
29 printf("CryptKit fatal error: %s\n", reason);
30 #endif
31 abort();
32 }
33
34 #import "feeDebug.h"
35
36 #if !defined(NeXT_PDO) && FEE_DEBUG
37
38 /*
39 * Mach, private build. use quick microsecond-accurate system clock.
40 */
41
42 #include <kern/time_stamp.h>
43
44 unsigned createRandomSeed()
45 {
46 struct tsval tsp;
47
48 (void)kern_timestamp(&tsp);
49 return tsp.low_val;
50 }
51
52 #else
53
54 /*
55 * OpenStep, normal case.
56 */
57 #include <sys/types.h>
58 #include <time.h>
59
60 extern int getpid();
61
62 unsigned createRandomSeed(void)
63 {
64 time_t curTime;
65 unsigned thisPid;
66
67 time(&curTime);
68 thisPid = (unsigned)getpid();
69
70 return (unsigned)curTime ^ (unsigned)thisPid;
71 }
72
73 #endif /* FEE_DEBUG */
74
75 #elif WIN32
76
77 /*
78 * OpenStep on Windows.
79 */
80 #include <process.h> /* for _getpid() */
81
82 void CKRaise(const char *reason) {
83 #if FEE_DEBUG
84 printf("CryptKit fatal error: %s\n", reason);
85 #endif
86 abort();
87 }
88
89 extern void time(unsigned *tp);
90
91 unsigned createRandomSeed()
92 {
93 unsigned curTime;
94 unsigned thisPid;
95
96 time(&curTime);
97 thisPid = _getpid();
98 return (unsigned)curTime ^ (unsigned)thisPid;
99 }
100
101
102 #elif __MAC_BUILD__
103
104 /*
105 * Macintosh, all flavors.
106 */
107 #include <stdlib.h>
108 #include <time.h>
109 #include <CrashReporterClient.h>
110
111 void CKRaise(const char *reason) {
112 #if FEE_DEBUG
113 printf("CryptKit fatal error: %s\n", reason);
114 #endif
115 char * msg = NULL;
116 if(asprintf(&msg, "CryptKit fatal error: %s", reason)) {
117 CRSetCrashLogMessage(msg);
118 } else {
119 CRSetCrashLogMessage("CryptKit fatal error");
120 }
121 abort();
122 }
123
124 /* for X, this isn't used except for testing when SecurityServer when
125 * Yarrow is not running. So let's strip it down so we don't have
126 * to link against CarbonCore.
127 */
128 #define BARE_BONES_SEED 1
129 #if BARE_BONES_SEED
130
131 #include <sys/types.h>
132
133 extern int getpid();
134
135 unsigned createRandomSeed()
136 {
137 time_t curTime;
138 unsigned thisPid;
139
140 time(&curTime);
141 thisPid = (unsigned)getpid();
142
143 return (unsigned)curTime ^ (unsigned)thisPid;
144 }
145
146 #else /* BARE_BONES_SEED */
147
148 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/Timer.h>
149 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/LowMem.h>
150
151 // this is mighty pitiful anyway...
152 unsigned createRandomSeed()
153 {
154 UnsignedWide curTime;
155 //unsigned ticks; /* use 16 bits */
156 unsigned rtnHi;
157 unsigned rtnLo;
158
159 /* FIXME - need a way to distinguish OS9x from Carbon. Carbon
160 * doesn't have LMGetTicks(). */
161
162 Microseconds(&curTime); /* low 16 bits are pretty good */
163
164 // Carbon hack
165 // rtnHi = LMGetTicks();
166 rtnHi = 0x5a5aa5a5;
167 rtnLo = curTime.lo & 0xffff;
168 return (rtnHi ^ rtnLo);
169 }
170 #endif /* BARE_BONES_SEED */
171
172 #elif unix
173
174 /* try for generic UNIX */
175
176 void CKRaise(const char *reason) {
177 #if FEE_DEBUG
178 printf("CryptKit fatal error: %s\n", reason);
179 #endif
180 abort();
181 }
182
183 #include <sys/types.h>
184 #include <time.h>
185
186 extern int getpid();
187
188 unsigned createRandomSeed()
189 {
190 time_t curTime;
191 unsigned thisPid;
192
193 time(&curTime);
194 thisPid = (unsigned)getpid();
195
196 return (unsigned)curTime ^ (unsigned)thisPid;
197 }
198
199
200 #else
201
202 #error platform-specific work needed in security_cryptkit/platform.c
203
204 #endif