1 /*
2  *	osdep.c
3  *	Release $Name: MATRIXSSL-3-3-0-OPEN $
4  *
5  *	WIN32 platform PScore
6  *		Windows XP Pro Service Pack 3 (Visual C++ 2008 Express Edition)
7  */
8 /*
9  *	Copyright (c) AuthenTec, Inc. 2011-2012
10  *	Copyright (c) PeerSec Networks, 2002-2011
11  *	All Rights Reserved
12  *
13  *	The latest version of this code is available at http://www.matrixssl.org
14  *
15  *	This software is open source; you can redistribute it and/or modify
16  *	it under the terms of the GNU General Public License as published by
17  *	the Free Software Foundation; either version 2 of the License, or
18  *	(at your option) any later version.
19  *
20  *	This General Public License does NOT permit incorporating this software
21  *	into proprietary programs.  If you are unable to comply with the GPL, a
22  *	commercial license for this software may be purchased from AuthenTec at
23  *	http://www.authentec.com/Products/EmbeddedSecurity/SecurityToolkits.aspx
24  *
25  *	This program is distributed in WITHOUT ANY WARRANTY; without even the
26  *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27  *	See the GNU General Public License for more details.
28  *
29  *	You should have received a copy of the GNU General Public License
30  *	along with this program; if not, write to the Free Software
31  *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32  *	http://www.gnu.org/copyleft/gpl.html
33  */
34 /******************************************************************************/
35 
36 /******************************************************************************/
37 #ifdef WIN32
38 /******************************************************************************/
39 
40 #include <windows.h>
41 #include "../coreApi.h"
42 
43 /******************************************************************************/
44 /*
45     TIME FUNCTIONS
46 */
47 /******************************************************************************/
48 static LARGE_INTEGER	hiresStart; /* zero-time */
49 static LARGE_INTEGER	hiresFreq; /* tics per second */
50 /*
51     Module open and close
52 */
osdepTimeOpen(void)53 int osdepTimeOpen(void)
54 {
55 	if (QueryPerformanceFrequency(&hiresFreq) != PS_TRUE) {
56 		return PS_FAILURE;
57 	}
58 	if (QueryPerformanceCounter(&hiresStart) != PS_TRUE) {
59 		return PS_FAILURE;
60 	}
61 	return PS_SUCCESS;
62 }
63 
osdepTimeClose(void)64 void osdepTimeClose(void)
65 {
66 }
67 
68 /*
69     PScore Public API implementations
70 */
psGetTime(psTime_t * t)71 int32 psGetTime(psTime_t *t)
72 {
73 	psTime_t	lt;
74     __int64		diff;
75 	int32			d;
76 
77 	if (t == NULL) {
78 		QueryPerformanceCounter(&lt);
79 		diff = lt.QuadPart - hiresStart.QuadPart;
80 		d = (int32)((diff * 1000) / hiresFreq.QuadPart);
81 		return d;
82 	}
83 
84 	QueryPerformanceCounter(t);
85 	diff = t->QuadPart - hiresStart.QuadPart;
86 	d = (int32)((diff * 1000) / hiresFreq.QuadPart);
87 	return d;
88 }
89 
psDiffMsecs(psTime_t then,psTime_t now)90 int32 psDiffMsecs(psTime_t then, psTime_t now)
91 {
92     __int64	diff;
93 
94 	diff = now.QuadPart - then.QuadPart;
95 	return (int32)((diff*1000) / hiresFreq.QuadPart);
96 }
97 
psCompareTime(psTime_t a,psTime_t b)98 int32 psCompareTime(psTime_t a, psTime_t b)
99 {
100 	if (a.QuadPart <= b.QuadPart) {
101 		return 1;
102 	}
103 	return 0;
104 }
105 
106 
107 #ifdef USE_MULTITHREADING
108 /******************************************************************************/
109 /*
110     MUTEX FUNCTIONS
111 */
112 /******************************************************************************/
113 /*
114     Module open and close
115 */
osdepMutexOpen(void)116 int osdepMutexOpen(void)
117 {
118 	return PS_SUCCESS;
119 }
120 
osdepMutexClose(void)121 void osdepMutexClose(void)
122 {
123 }
124 
125 /*
126     PScore Public API implementations
127 */
psCreateMutex(psMutex_t * mutex)128 int32 psCreateMutex(psMutex_t *mutex)
129 {
130 	InitializeCriticalSection(mutex);
131 	return PS_SUCCESS;
132 }
133 
psLockMutex(psMutex_t * mutex)134 int32 psLockMutex(psMutex_t *mutex)
135 {
136 	EnterCriticalSection(mutex);
137 	return PS_SUCCESS;
138 }
139 
psUnlockMutex(psMutex_t * mutex)140 int32 psUnlockMutex(psMutex_t *mutex)
141 {
142 	LeaveCriticalSection(mutex);
143 	return PS_SUCCESS;
144 }
145 
psDestroyMutex(psMutex_t * mutex)146 void psDestroyMutex(psMutex_t *mutex)
147 {
148 	DeleteCriticalSection(mutex);
149 }
150 #endif /* USE_MULTITHREADING */
151 /******************************************************************************/
152 
153 
154 /******************************************************************************/
155 /*
156     ENTROPY FUNCTIONS
157 */
158 /******************************************************************************/
159 static HCRYPTPROV		hProv;	/* Crypto context for random bytes */
160 
osdepEntropyOpen(void)161 int osdepEntropyOpen(void)
162 {
163 	if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
164 			CRYPT_VERIFYCONTEXT))  {
165 		return PS_FAILURE;
166 	}
167 	return PS_SUCCESS;
168 }
169 
osdepEntropyClose(void)170 void osdepEntropyClose(void)
171 {
172 	CryptReleaseContext(hProv, 0);
173 }
174 
psGetEntropy(unsigned char * bytes,uint32 size)175 int32 psGetEntropy(unsigned char *bytes, uint32 size)
176 {
177 	if (CryptGenRandom(hProv, size, bytes)) {
178 		return size;
179 	}
180 	return PS_FAILURE;
181 }
182 
183 
184 /******************************************************************************/
185 /*
186     TRACE FUNCTIONS
187 */
188 /******************************************************************************/
osdepTraceOpen(void)189 int osdepTraceOpen(void)
190 {
191 	return PS_SUCCESS;
192 }
193 
osdepTraceClose(void)194 void osdepTraceClose(void)
195 {
196 }
197 
_psTrace(char * msg)198 void _psTrace(char *msg)
199 {
200     printf(msg);
201 }
202 
203 /* message should contain one %s, unless value is NULL */
_psTraceStr(char * message,char * value)204 void _psTraceStr(char *message, char *value)
205 {
206     if (value) {
207         printf(message, value);
208     } else {
209         printf(message);
210     }
211 }
212 
213 /* message should contain one %d */
_psTraceInt(char * message,int32 value)214 void _psTraceInt(char *message, int32 value)
215 {
216     printf(message, value);
217 }
218 
219 /* message should contain one %p */
_psTracePtr(char * message,void * value)220 void _psTracePtr( char *message, void *value)
221 {
222     printf(message, value);
223 }
224 
225 
226 #ifdef HALT_ON_PS_ERROR
227 /******************************************************************************/
228 /*
229     system halt on psError when built HALT_ON_PS_ERROR
230 */
osdepBreak(void)231 void osdepBreak(void)
232 {
233      DebugBreak();
234 }
235 #endif /* HALT_ON_PS_ERROR */
236 
237 
238 #ifdef MATRIX_USE_FILE_SYSTEM
239 /******************************************************************************/
240 /*
241     FILE ACCESS FUNCTION
242 */
243 /******************************************************************************/
244 /*
245     Memory info:
246     Caller must free 'buf' parameter on success
247     Callers do not need to free buf on function failure
248 */
psGetFileBuf(psPool_t * pool,const char * fileName,unsigned char ** buf,int32 * bufLen)249 int32 psGetFileBuf(psPool_t *pool, const char *fileName, unsigned char **buf,
250                 int32 *bufLen)
251 {
252 	DWORD	dwAttributes;
253 	HANDLE	hFile;
254 	int32	size;
255 	DWORD	tmp = 0;
256 
257 	*bufLen = 0;
258 	*buf = NULL;
259 
260 	dwAttributes = GetFileAttributesA(fileName);
261 	if (dwAttributes != 0xFFFFFFFF && dwAttributes & FILE_ATTRIBUTE_DIRECTORY) {
262 		psTraceStrCore("Unable to find %s\n", (char*)fileName);
263         return PS_PLATFORM_FAIL;
264 	}
265 
266 	if ((hFile = CreateFileA(fileName, GENERIC_READ,
267 			FILE_SHARE_READ && FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
268 			FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
269 		psTraceStrCore("Unable to open %s\n", (char*)fileName);
270         return PS_PLATFORM_FAIL;
271 	}
272 
273 /*
274  *	 Get the file size.
275  */
276 	size = GetFileSize(hFile, NULL);
277 
278 	*buf = psMalloc(pool, size + 1);
279 	if (*buf == NULL) {
280 		CloseHandle(hFile);
281 		return PS_MEM_FAIL;
282 	}
283 	memset(*buf, 0x0, size + 1);
284 
285 	while (*bufLen < size) {
286 		if (ReadFile(hFile, *buf + *bufLen, (size-*bufLen > 512 ? 512 : size-*bufLen), &tmp, NULL) == FALSE) {
287 			psFree(*buf);
288 			psTraceStrCore("Unable to read %s\n", (char*)fileName);
289 			CloseHandle(hFile);
290 			return PS_PLATFORM_FAIL;
291 		}
292 		*bufLen += (int32)tmp;
293 	}
294 
295 	CloseHandle(hFile);
296 	return PS_SUCCESS;
297 }
298 #endif /* MATRIX_USE_FILE_SYSTEM */
299 
300 
301 /******************************************************************************/
302 #endif /* WIN32 */
303 /******************************************************************************/
304 
305 
306 
307 
308 
309