]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/ctaocrypt/src/wc_port.c
Update CyaSSL to latest version.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / ctaocrypt / src / wc_port.c
1 /* port.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/error-crypt.h>
29
30
31 #ifdef _MSC_VER
32     /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
33     #pragma warning(disable: 4996)
34 #endif
35
36
37
38 #ifdef SINGLE_THREADED
39
40 int InitMutex(CyaSSL_Mutex* m)
41 {
42     (void)m;
43     return 0;
44 }
45
46
47 int FreeMutex(CyaSSL_Mutex *m)
48 {
49     (void)m;
50     return 0;
51 }
52
53
54 int LockMutex(CyaSSL_Mutex *m)
55 {
56     (void)m;
57     return 0;
58 }
59
60
61 int UnLockMutex(CyaSSL_Mutex *m)
62 {
63     (void)m;
64     return 0;
65 }
66
67 #else /* MULTI_THREAD */
68
69     #if defined(FREERTOS)
70
71         int InitMutex(CyaSSL_Mutex* m)
72         {
73             int iReturn;
74
75             *m = ( CyaSSL_Mutex ) xSemaphoreCreateMutex();
76             if( *m != NULL )
77                 iReturn = 0;
78             else
79                 iReturn = BAD_MUTEX_E;
80
81             return iReturn;
82         }
83
84         int FreeMutex(CyaSSL_Mutex* m)
85         {
86             vSemaphoreDelete( *m );
87             return 0;
88         }
89
90         int LockMutex(CyaSSL_Mutex* m)
91         {
92             /* Assume an infinite block, or should there be zero block? */
93             xSemaphoreTake( *m, portMAX_DELAY );
94             return 0;
95         }
96
97         int UnLockMutex(CyaSSL_Mutex* m)
98         {
99             xSemaphoreGive( *m );
100             return 0;
101         }
102
103     #elif defined(CYASSL_SAFERTOS)
104
105         int InitMutex(CyaSSL_Mutex* m)
106         {
107             vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
108             if (m->mutex == NULL)
109                 return BAD_MUTEX_E;
110
111             return 0;
112         }
113
114         int FreeMutex(CyaSSL_Mutex* m)
115         {
116             (void)m;
117             return 0;
118         }
119
120         int LockMutex(CyaSSL_Mutex* m)
121         {
122             /* Assume an infinite block */
123             xSemaphoreTake(m->mutex, portMAX_DELAY);
124             return 0;
125         }
126
127         int UnLockMutex(CyaSSL_Mutex* m)
128         {
129             xSemaphoreGive(m->mutex);
130             return 0;
131         }
132
133
134     #elif defined(USE_WINDOWS_API)
135
136         int InitMutex(CyaSSL_Mutex* m)
137         {
138             InitializeCriticalSection(m);
139             return 0;
140         }
141
142
143         int FreeMutex(CyaSSL_Mutex* m)
144         {
145             DeleteCriticalSection(m);
146             return 0;
147         }
148
149
150         int LockMutex(CyaSSL_Mutex* m)
151         {
152             EnterCriticalSection(m);
153             return 0;
154         }
155
156
157         int UnLockMutex(CyaSSL_Mutex* m)
158         {
159             LeaveCriticalSection(m);
160             return 0;
161         }
162
163     #elif defined(CYASSL_PTHREADS)
164
165         int InitMutex(CyaSSL_Mutex* m)
166         {
167             if (pthread_mutex_init(m, 0) == 0)
168                 return 0;
169             else
170                 return BAD_MUTEX_E;
171         }
172
173
174         int FreeMutex(CyaSSL_Mutex* m)
175         {
176             if (pthread_mutex_destroy(m) == 0)
177                 return 0;
178             else
179                 return BAD_MUTEX_E;
180         }
181
182
183         int LockMutex(CyaSSL_Mutex* m)
184         {
185             if (pthread_mutex_lock(m) == 0)
186                 return 0;
187             else
188                 return BAD_MUTEX_E;
189         }
190
191
192         int UnLockMutex(CyaSSL_Mutex* m)
193         {
194             if (pthread_mutex_unlock(m) == 0)
195                 return 0;
196             else
197                 return BAD_MUTEX_E;
198         }
199
200     #elif defined(THREADX)
201
202         int InitMutex(CyaSSL_Mutex* m)
203         {
204             if (tx_mutex_create(m, "CyaSSL Mutex", TX_NO_INHERIT) == 0)
205                 return 0;
206             else
207                 return BAD_MUTEX_E;
208         }
209
210
211         int FreeMutex(CyaSSL_Mutex* m)
212         {
213             if (tx_mutex_delete(m) == 0)
214                 return 0;
215             else
216                 return BAD_MUTEX_E;
217         }
218
219
220         int LockMutex(CyaSSL_Mutex* m)
221         {
222             if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
223                 return 0;
224             else
225                 return BAD_MUTEX_E;
226         }
227
228
229         int UnLockMutex(CyaSSL_Mutex* m)
230         {
231             if (tx_mutex_put(m) == 0)
232                 return 0;
233             else
234                 return BAD_MUTEX_E;
235         }
236
237     #elif defined(MICRIUM)
238
239         int InitMutex(CyaSSL_Mutex* m)
240         {
241             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
242                 if (NetSecure_OS_MutexCreate(m) == 0)
243                     return 0;
244                 else
245                     return BAD_MUTEX_E;
246             #else
247                 return 0;
248             #endif
249         }
250
251
252         int FreeMutex(CyaSSL_Mutex* m)
253         {
254             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
255                 if (NetSecure_OS_FreeMutex(m) == 0)
256                     return 0;
257                 else
258                     return BAD_MUTEX_E;
259             #else
260                 return 0;
261             #endif
262         }
263
264
265         int LockMutex(CyaSSL_Mutex* m)
266         {
267             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
268                 if (NetSecure_OS_LockMutex(m) == 0)
269                     return 0;
270                 else
271                     return BAD_MUTEX_E;
272             #else
273                 return 0;
274             #endif
275         }
276
277
278         int UnLockMutex(CyaSSL_Mutex* m)
279         {
280             #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
281                 if (NetSecure_OS_UnLockMutex(m) == 0)
282                     return 0;
283                 else
284                     return BAD_MUTEX_E;
285             #else
286                 return 0;
287             #endif
288
289         }
290
291     #elif defined(EBSNET)
292
293         int InitMutex(CyaSSL_Mutex* m)
294         {
295             if (rtp_sig_mutex_alloc(m, "CyaSSL Mutex") == -1)
296                 return BAD_MUTEX_E;
297             else
298                 return 0;
299         }
300
301         int FreeMutex(CyaSSL_Mutex* m)
302         {
303             rtp_sig_mutex_free(*m);
304             return 0;
305         }
306
307         int LockMutex(CyaSSL_Mutex* m)
308         {
309             if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
310                 return 0;
311             else
312                 return BAD_MUTEX_E;
313         }
314
315         int UnLockMutex(CyaSSL_Mutex* m)
316         {
317             rtp_sig_mutex_release(*m);
318             return 0;
319         }
320
321     #elif defined(FREESCALE_MQX)
322
323         int InitMutex(CyaSSL_Mutex* m)
324         {
325             if (_mutex_init(m, NULL) == MQX_EOK)
326                 return 0;
327             else
328                 return BAD_MUTEX_E;
329         }
330
331         int FreeMutex(CyaSSL_Mutex* m)
332         {
333             if (_mutex_destroy(m) == MQX_EOK)
334                 return 0;
335             else
336                 return BAD_MUTEX_E;
337         }
338
339         int LockMutex(CyaSSL_Mutex* m)
340         {
341             if (_mutex_lock(m) == MQX_EOK)
342                 return 0;
343             else
344                 return BAD_MUTEX_E;
345         }
346
347         int UnLockMutex(CyaSSL_Mutex* m)
348         {
349             if (_mutex_unlock(m) == MQX_EOK)
350                 return 0;
351             else
352                 return BAD_MUTEX_E;
353         }
354         
355     #elif defined(CYASSL_MDK_ARM)|| defined(CYASSL_CMSIS_RTOS)
356     
357         #if defined(CYASSL_CMSIS_RTOS)
358             #include "cmsis_os.h"
359             #define CMSIS_NMUTEX 10
360             osMutexDef(CyaSSL_mt0) ;  osMutexDef(CyaSSL_mt1) ;  osMutexDef(CyaSSL_mt2) ;
361             osMutexDef(CyaSSL_mt3) ;  osMutexDef(CyaSSL_mt4) ;  osMutexDef(CyaSSL_mt5) ;  
362             osMutexDef(CyaSSL_mt6) ;  osMutexDef(CyaSSL_mt7) ;  osMutexDef(CyaSSL_mt8) ;  
363             osMutexDef(CyaSSL_mt9) ;  
364             
365             static const osMutexDef_t *CMSIS_mutex[] = { osMutex(CyaSSL_mt0),   
366                 osMutex(CyaSSL_mt1),    osMutex(CyaSSL_mt2),   osMutex(CyaSSL_mt3),    
367                 osMutex(CyaSSL_mt4),    osMutex(CyaSSL_mt5),   osMutex(CyaSSL_mt6),
368                 osMutex(CyaSSL_mt7),    osMutex(CyaSSL_mt8),    osMutex(CyaSSL_mt9) } ;                 
369             
370             static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0} ;
371
372             int InitMutex(CyaSSL_Mutex* m)
373             {
374                 int i ;
375                 for (i=0; i<CMSIS_NMUTEX; i++) {
376                     if(CMSIS_mutexID[i] == 0) {
377                         CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]) ;
378                         (*m) = CMSIS_mutexID[i] ;
379                     return 0 ;
380                     }
381                 }
382                 return -1 ;
383             }
384
385             int FreeMutex(CyaSSL_Mutex* m)
386             {
387                 int i ;
388                 osMutexDelete   (*m) ;
389                 for (i=0; i<CMSIS_NMUTEX; i++) {
390                     if(CMSIS_mutexID[i] == (*m)) {
391                         CMSIS_mutexID[i] = 0 ;
392                         return(0) ;
393                     }
394                 }
395                 return(-1) ;
396             }
397             
398             int LockMutex(CyaSSL_Mutex* m)
399             {
400                 osMutexWait(*m, osWaitForever) ;
401                 return(0) ;
402             }
403
404             int UnLockMutex(CyaSSL_Mutex* m)
405             {
406                 osMutexRelease (*m);
407                 return 0;
408             }
409         #else
410
411         int InitMutex(CyaSSL_Mutex* m)
412         {
413             os_mut_init (m); 
414             return 0;
415         }
416
417         int FreeMutex(CyaSSL_Mutex* m)
418         {
419             return(0) ;
420         }
421
422         int LockMutex(CyaSSL_Mutex* m)
423         {
424             os_mut_wait (m, 0xffff);
425             return(0) ;
426         }
427
428         int UnLockMutex(CyaSSL_Mutex* m)
429         {
430             os_mut_release (m);
431             return 0;
432         }
433         #endif
434     #endif /* USE_WINDOWS_API */
435 #endif /* SINGLE_THREADED */
436