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 */
27void CKRaise(const char *reason) {
28	#if	FEE_DEBUG
29	printf("CryptKit fatal error: %s\n", reason);
30	#endif
31	exit(1);
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
44unsigned 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
60extern int getpid();
61
62unsigned 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
82void CKRaise(const char *reason) {
83	#if	FEE_DEBUG
84	printf("CryptKit fatal error: %s\n", reason);
85	#endif
86	exit(1);
87}
88
89extern void time(unsigned *tp);
90
91unsigned 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
110void CKRaise(const char *reason) {
111	#if	FEE_DEBUG
112	printf("CryptKit fatal error: %s\n", reason);
113	#endif
114	exit(1);
115}
116
117/* for X, this isn't used except for testing when SecurityServer when
118 * Yarrow is not running. So let's strip it down so we don't have
119 * to link against CarbonCore.
120 */
121#define BARE_BONES_SEED		1
122#if 	BARE_BONES_SEED
123
124#include <sys/types.h>
125
126extern int getpid();
127
128unsigned createRandomSeed()
129{
130	time_t curTime;
131	unsigned thisPid;
132
133  	time(&curTime);
134	thisPid = (unsigned)getpid();
135
136	return (unsigned)curTime ^ (unsigned)thisPid;
137}
138
139#else	/* BARE_BONES_SEED */
140
141#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/Timer.h>
142#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/LowMem.h>
143
144// this is mighty pitiful anyway...
145unsigned createRandomSeed()
146{
147	UnsignedWide curTime;
148	//unsigned ticks;				/* use 16 bits */
149	unsigned rtnHi;
150	unsigned rtnLo;
151
152	/* FIXME - need a way to distinguish OS9x from Carbon. Carbon
153	 * doesn't have LMGetTicks(). */
154
155	Microseconds(&curTime);		/* low 16 bits are pretty good */
156
157	// Carbon hack
158	// rtnHi = LMGetTicks();
159	rtnHi = 0x5a5aa5a5;
160	rtnLo = curTime.lo & 0xffff;
161	return (rtnHi ^ rtnLo);
162}
163#endif	/* BARE_BONES_SEED */
164
165#elif unix
166
167/* try for generic UNIX */
168
169void CKRaise(const char *reason) {
170	#if	FEE_DEBUG
171	printf("CryptKit fatal error: %s\n", reason);
172	#endif
173	exit(1);
174}
175
176#include <sys/types.h>
177#include <time.h>
178
179extern int getpid();
180
181unsigned createRandomSeed()
182{
183	time_t curTime;
184	unsigned thisPid;
185
186  	time(&curTime);
187	thisPid = (unsigned)getpid();
188
189	return (unsigned)curTime ^ (unsigned)thisPid;
190}
191
192
193#else
194
195#error platform-specific work needed in security_cryptkit/platform.c
196
197#endif
198