OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / system / wlan / ti / sta_dk_4_0_4_32 / common / src / hal / security / whalSecurity.c
1 /****************************************************************************
2 **+-----------------------------------------------------------------------+**
3 **|                                                                       |**
4 **| Copyright(c) 1998 - 2008 Texas Instruments. All rights reserved.      |**
5 **| All rights reserved.                                                  |**
6 **|                                                                       |**
7 **| Redistribution and use in source and binary forms, with or without    |**
8 **| modification, are permitted provided that the following conditions    |**
9 **| are met:                                                              |**
10 **|                                                                       |**
11 **|  * Redistributions of source code must retain the above copyright     |**
12 **|    notice, this list of conditions and the following disclaimer.      |**
13 **|  * Redistributions in binary form must reproduce the above copyright  |**
14 **|    notice, this list of conditions and the following disclaimer in    |**
15 **|    the documentation and/or other materials provided with the         |**
16 **|    distribution.                                                      |**
17 **|  * Neither the name Texas Instruments nor the names of its            |**
18 **|    contributors may be used to endorse or promote products derived    |**
19 **|    from this software without specific prior written permission.      |**
20 **|                                                                       |**
21 **| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |**
22 **| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |**
23 **| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |**
24 **| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |**
25 **| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |**
26 **| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |**
27 **| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |**
28 **| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |**
29 **| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |**
30 **| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |**
31 **| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |**
32 **|                                                                       |**
33 **+-----------------------------------------------------------------------+**
34 ****************************************************************************/
35
36 #include "whalCommon.h"
37 #include "whalCtrl_api.h"
38 #include "whalCtrl.h"
39 #include "whalSecurity.h"
40
41
42 /*
43  * ----------------------------------------------------------------------------
44  * Function : whalSecur_Create
45  *
46  * Input    : 
47  * Output   :
48  * Process  :
49  * Note(s)  :  
50  * -----------------------------------------------------------------------------
51  */
52 TI_HANDLE whalSecur_Create (TI_HANDLE hOs, TI_HANDLE hWhalCtrl, UINT16 numOfStations)
53 {
54         WHAL_SECURITY* pWhalSecur;
55
56         pWhalSecur = (WHAL_SECURITY *)os_memoryAlloc (hOs, sizeof(WHAL_SECURITY));
57         if (pWhalSecur == NULL)
58                 return NULL;
59
60         os_memoryZero (hOs, (void *)pWhalSecur, sizeof(WHAL_SECURITY));
61
62         pWhalSecur->pWhalCtrl = (WHAL_CTRL*)hWhalCtrl; 
63         pWhalSecur->hOs = hOs;
64         pWhalSecur->securityMode = RSN_CIPHER_NONE;
65
66         pWhalSecur->pWhalWep = (WHAL_WEP*)whalWep_Create (hOs, hWhalCtrl);
67         if (pWhalSecur->pWhalWep == NULL)
68         {
69                 whalSecur_Destroy(pWhalSecur, numOfStations);
70                 return NULL;
71         }
72         pWhalSecur->pWhalWpa = (WHAL_WPA*)whalWpa_Create (hOs, hWhalCtrl);
73         if (pWhalSecur->pWhalWpa == NULL)
74         {
75                 whalSecur_Destroy(pWhalSecur, numOfStations);
76                 return NULL;
77         }
78 #ifdef CKIP_ENABLED
79         pWhalSecur->pWhalPrivacy = (privacy_t*)privacy_create (hOs);
80         pWhalSecur->pWhalExc = (WHAL_EXC*)whalExc_Create(hOs, hWhalCtrl);
81         if (pWhalSecur->pWhalExc == NULL)
82         {
83                 whalSecur_Destroy(pWhalSecur, numOfStations);
84                 return NULL;
85         }
86 #endif /* CKIP_ENABLED */
87         pWhalSecur->numOfStations = numOfStations;
88         pWhalSecur->reconfData.reconfKeys = (securityKeys_t*)os_memoryAlloc (hOs, 
89                                                                                 (sizeof(securityKeys_t))*(numOfStations*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS));
90         os_memoryZero (hOs, (void *)pWhalSecur->reconfData.reconfKeys, 
91                                                                                 (sizeof(securityKeys_t))*(numOfStations*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS));
92         
93         return ((TI_HANDLE)pWhalSecur);
94 }
95
96 /*
97  * ----------------------------------------------------------------------------
98  * Function : whalSecur_Config
99  *
100  * Input    : 
101  * Output   :
102  * Process  :
103  * Note(s)  :
104  * -----------------------------------------------------------------------------
105  */
106 int whalSecur_Config (TI_HANDLE hWhalSecur, whalSecur_config_t* pWhalSecurCfg)
107 {
108         UINT32 index;
109         whalWep_config_t wepCfg;
110         whalWpa_config_t wpaCfg;
111 #ifdef CKIP_ENABLED
112         whalExc_config_t excCfg;
113 #endif /* CKIP_ENABLED*/
114
115         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
116
117         /* Reset all reconfig valid fields*/
118         pWhalSecur->reconfData.isHwEncDecrEnableValid = FALSE;
119         pWhalSecur->reconfData.isDefaultKeyIdValid = FALSE;  
120         for (index=0; index < ((pWhalSecur->numOfStations)*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS); index++)
121                         (pWhalSecur->reconfData.reconfKeys+index)->keyType = NULL_KEY;
122
123         /* Save configuration parameters */
124         pWhalSecur->hMemMgr = pWhalSecurCfg->hMemMgr;
125         pWhalSecur->hReport = pWhalSecurCfg->hReport;
126         
127
128         /* Config the wep sub-module*/
129         wepCfg.hMemMgr = pWhalSecur->hMemMgr;
130         wepCfg.hReport = pWhalSecur->hReport;
131         if (whalWep_Config (pWhalSecur->pWhalWep, &wepCfg) != OK)
132                 return (NOK);                                                                                     
133
134         /* Config the wpa sub-module*/
135         wpaCfg.hMemMgr = pWhalSecur->hMemMgr; 
136         wpaCfg.hReport = pWhalSecur->hReport; 
137         if (whalWpa_Config (pWhalSecur->pWhalWpa, &wpaCfg) != OK)
138                 return (NOK);
139         
140 #ifdef CKIP_ENABLED
141     wpaCfg.pWhalPrivacy = pWhalSecur->pWhalPrivacy;
142         /* Config the privacy sub-module*/
143         if (privacy_config (pWhalSecur->pWhalPrivacy, 
144                                                 pWhalSecur->hReport, pWhalSecur->hOs) != OK)
145                 return (NOK);
146         excCfg.hMemMgr = pWhalSecur->hMemMgr;   
147         excCfg.hReport = pWhalSecur->hReport;  
148         excCfg.pWhalPrivacy = pWhalSecur->pWhalPrivacy;  
149         if (whalExc_Config (pWhalSecur->pWhalExc, &excCfg) != OK)
150                 return (NOK);
151 #endif /*CKIP_ENABLED */
152
153         return (OK);
154 }
155
156
157 /*
158  * ----------------------------------------------------------------------------
159  * Function : whalSecur_KeyAdd
160  *
161  * Input    : 
162  * Output   :
163  * Process  :
164  * Note(s)  :
165  * -----------------------------------------------------------------------------
166  */
167 int whalSecur_KeyAdd (TI_HANDLE hWhalSecur, securityKeys_t* pKey, BOOL reconfFlag, void *CB_Func, TI_HANDLE CB_handle)
168 {
169         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
170         UINT8          keyIdx     = (UINT8)pKey->keyIndex;
171
172         /* store the security key for reconfigure time (FW reload)*/
173         if (reconfFlag != TRUE)
174         {
175                 if (keyIdx >= ((pWhalSecur->numOfStations)*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS))
176                 {
177                         WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,  
178                                 ("whalSecur_KeyAdd: ERROR Key keyIndex field out of range =%d, range is (0 to %d)\n",
179                                  pKey->keyIndex, (pWhalSecur->numOfStations)*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS-1));
180                         
181                         return (NOK);
182                 }
183
184                 if (pKey->keyType == NULL_KEY)
185                 {
186                         WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,  
187                                                                 ("whalSecur_KeyAdd: ERROR KeyType is NULL_KEY\n"));
188                         
189                         return (NOK);
190                 }
191
192                 os_memoryCopy (pWhalSecur->hOs, 
193                                 (void *)(pWhalSecur->reconfData.reconfKeys + keyIdx),
194                                 (void *)pKey, sizeof(securityKeys_t));
195         }
196         
197         switch (pWhalSecur->securityMode)
198         {
199                 case RSN_CIPHER_WEP:
200                 case RSN_CIPHER_WEP104:
201                         return (whalWep_KeyAdd (pWhalSecur->pWhalWep, pKey, CB_Func, CB_handle));
202         
203                 case RSN_CIPHER_TKIP:
204                 case RSN_CIPHER_AES_CCMP:
205                         return (whalWpa_KeyAdd (pWhalSecur->pWhalWpa, pKey, CB_Func, CB_handle));
206
207                 default:
208                         return (NOK);
209         }
210
211 }
212
213 /*
214  * ----------------------------------------------------------------------------
215  * Function : whalSecur_KeyRemove
216  *
217  * Input    : 
218  * Output   :
219  * Process  :
220  * Note(s)  :
221  * -----------------------------------------------------------------------------
222  */
223 int whalSecur_KeyRemove (TI_HANDLE hWhalSecur, securityKeys_t* pKey, BOOL reconfFlag, void *CB_Func, TI_HANDLE CB_handle)
224 {
225         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
226
227         switch (pWhalSecur->securityMode)
228         {
229                 case RSN_CIPHER_WEP:
230                 case RSN_CIPHER_WEP104:
231                         return (whalWep_KeyRemove (pWhalSecur->pWhalWep, pKey, CB_Func, CB_handle));
232         
233                 case RSN_CIPHER_TKIP:
234                 case RSN_CIPHER_AES_CCMP:
235                         return (whalWpa_KeyRemove (pWhalSecur->pWhalWpa, pKey, CB_Func, CB_handle));
236                 
237                 default:
238                         return (NOK);
239         }
240
241 }
242
243 /*
244  * ----------------------------------------------------------------------------
245  * Function : whalSecur_DefaultKeyIdSet
246  *
247  * Input    : 
248  * Output   :
249  * Process  :
250  * Note(s)  :
251  * -----------------------------------------------------------------------------
252  */
253 int whalSecur_DefaultKeyIdSet (TI_HANDLE hWhalSecur, UINT8 aKeyId, void *CB_Func, TI_HANDLE CB_handle)
254 {
255         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
256
257         /* store the default key ID value for reconfigure time (FW reload)*/
258         pWhalSecur->reconfData.reconfDefaultKeyId = aKeyId;
259         
260         switch (pWhalSecur->securityMode)
261         {
262                 case RSN_CIPHER_WEP:
263                 case RSN_CIPHER_WEP104:
264                         pWhalSecur->reconfData.isDefaultKeyIdValid = TRUE;
265                         return (whalWep_DefaultKeyIdSet (pWhalSecur->pWhalWep, aKeyId, CB_Func, CB_handle));
266         
267                 case RSN_CIPHER_TKIP:
268                 case RSN_CIPHER_AES_CCMP:
269                         pWhalSecur->reconfData.isDefaultKeyIdValid = TRUE;
270                         return (whalWpa_DefaultKeyIdSet (pWhalSecur->pWhalWpa, aKeyId, CB_Func, CB_handle));
271
272                 default:
273                         return (NOK);
274         }
275
276 }
277
278 /*
279  * ----------------------------------------------------------------------------
280  * Function : whalSecur_HwEncDecrEnable
281  *
282  * Input    : 
283  * Output   :
284  * Process  :
285  * Note(s)  :
286  * -----------------------------------------------------------------------------
287  */
288 int whalSecur_HwEncDecrEnable (TI_HANDLE hWhalSecur, BOOL aHwEncEnable)
289 {
290         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
291         
292         /* store the HW encryption Enable flag for reconfigure time (FW reload)*/
293         pWhalSecur->reconfData.reconfHwEncEnable = aHwEncEnable;
294
295         pWhalSecur->reconfData.isHwEncDecrEnableValid = TRUE;
296
297     return (whal_hwCtrl_EncDecrSet (pWhalSecur->pWhalCtrl->pHwCtrl, aHwEncEnable, aHwEncEnable));
298 }
299
300 #ifdef CKIP_ENABLED
301 /*
302  * ----------------------------------------------------------------------------
303  * Function : whalSecur_SwEncEnable
304  *
305  * Input    : 
306  * Output   :
307  * Process  :
308  * Note(s)  :
309  * -----------------------------------------------------------------------------
310  */
311 int whalSecur_SwEncEnable (TI_HANDLE hWhalSecur, BOOL aSwEncEnable)
312 {
313         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
314         
315         return (whalExc_swEncEnable (pWhalSecur->pWhalExc, aSwEncEnable));
316 }
317
318 /*
319  * ----------------------------------------------------------------------------
320  * Function : whalSecur_MicFieldEnable
321  *
322  * Input    : 
323  * Output   :
324  * Process  :
325  * Note(s)  :
326  * -----------------------------------------------------------------------------
327  */
328 int whalSecur_MicFieldEnable (TI_HANDLE hWhalSecur, BOOL aMicFieldEnable)
329 {
330         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
331
332         return (whalExc_micFieldEnable (pWhalSecur->pWhalExc, aMicFieldEnable));
333 }
334 #endif /*CKIP_ENABLED*/
335
336 /*
337  * ----------------------------------------------------------------------------
338  * Function : whalSecur_SecurModeSet
339  *
340  * Input    : 
341  * Output   :
342  * Process  :
343  * Note(s)  :
344  * -----------------------------------------------------------------------------
345  */
346 int whalSecur_SecurModeSet (TI_HANDLE hWhalSecur, cipherSuite_e aSecurMode)
347 {
348         UINT32 index;
349         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
350         if (aSecurMode<=RSN_CIPHER_CKIP)
351         {
352                 WLAN_REPORT_INFORMATION (pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,
353                                                                 ("whalSecur_SecurModeSet: change security mode from %d --> %d\n",
354                                                              pWhalSecur->securityMode, aSecurMode));
355                 /* check if security mode is equal to previous one*/
356                 if (pWhalSecur->securityMode == aSecurMode)
357                         return (OK);
358
359                 /* Reset all reconfig valid fields*/
360                 pWhalSecur->reconfData.isHwEncDecrEnableValid = FALSE;
361                 pWhalSecur->reconfData.isDefaultKeyIdValid = FALSE;  
362                 for (index=0; index < ((pWhalSecur->numOfStations)*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS); index++)
363                                 (pWhalSecur->reconfData.reconfKeys+index)->keyType = NULL_KEY;
364                 
365         /* set the new security mode*/
366                 pWhalSecur->securityMode = aSecurMode;
367
368 #ifdef CKIP_ENABLED
369                 /* Upon entering to RSN_PRIVACY_EXC mode, disable the excSwEnc and excMicField flags, 
370                    reset the privacy Enc sub-module */
371                 if (aSecurMode == RSN_CIPHER_CKIP)
372                 {
373                         if (whalExc_swEncEnable (pWhalSecur->pWhalExc, FALSE) != OK)
374                                 return (NOK);
375
376                         if (whalExc_micFieldEnable (pWhalSecur->pWhalExc, FALSE) != OK)
377                                 return (NOK);
378
379                         if (privacy_resetEnc (pWhalSecur->pWhalPrivacy) != OK)
380                                 return (NOK);
381                 }
382
383                 privacy_setPrivacyMode(pWhalSecur->pWhalPrivacy, aSecurMode);
384 #endif /* CKIP_ENABLED*/
385
386                 /* disable defrag, duplicate detection on TNETW+EXC on chip level*/
387                 if (pWhalSecur->securityMode==RSN_CIPHER_CKIP)
388                         /* YV- to add fragmentation control (if there is- artur ?)*/
389                         return (whal_hwCtrl_RxMsduFormatSet (pWhalSecur->pWhalCtrl->pHwCtrl, FALSE));
390                 else
391                         /* YV- to add fragmentation control (if there is- artur ?)*/
392                         return (whal_hwCtrl_RxMsduFormatSet (pWhalSecur->pWhalCtrl->pHwCtrl, TRUE));
393                 
394
395         }
396         else
397                 return (NOK);
398 }
399
400 /*
401  * ----------------------------------------------------------------------------
402  * Function : whalSecur_SecurModeGet
403  *
404  * Input    : 
405  * Output   :   security mode
406  * Process  :
407  * Note(s)  :
408  * -----------------------------------------------------------------------------
409  */
410 cipherSuite_e whalSecur_SecurModeGet (TI_HANDLE hWhalSecur)
411 {
412         WHAL_SECURITY* pWhalSecur = (TI_HANDLE)hWhalSecur;
413
414         return pWhalSecur->securityMode;
415 }
416
417
418 /*
419  * ----------------------------------------------------------------------------
420  * Function : whalSecur_KeysReconfig
421  *
422  * Input    : 
423  * Output   :
424  * Process  : Reconfig security keys, default key Id and encryption/decryption 
425  *                        control to the FW
426  * Note(s)  :
427  * -----------------------------------------------------------------------------
428  */
429 int whalSecur_KeysReconfig (TI_HANDLE hWhalSecur)
430 {
431         UINT32 index;
432         WHAL_SECURITY* pWhalSecur = (WHAL_SECURITY *)hWhalSecur;
433
434
435         if (pWhalSecur->securityMode != RSN_CIPHER_NONE)
436         {
437
438                 /* set the keys to the HW*/
439                 for (index=0; index < ((pWhalSecur->numOfStations)*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS); index++)
440                 {
441                         if ((pWhalSecur->reconfData.reconfKeys+index)->keyType != NULL_KEY)
442                         {
443                                 if (whalSecur_KeyAdd (pWhalSecur, pWhalSecur->reconfData.reconfKeys+index, TRUE, NULL, NULL) != OK)
444                                 {
445                                         WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,  
446                                                 ("whalSecur_KeysReconfig: ERROR whalSecur_KeyAdd failure index=%d\n", index));
447                                         return (NOK);
448                                 }   
449                         }
450                 }
451         
452                 if (pWhalSecur->reconfData.isDefaultKeyIdValid == TRUE)
453                 {
454                         /* set the deafult key ID to the HW*/
455                         if (whalSecur_DefaultKeyIdSet (pWhalSecur, pWhalSecur->reconfData.reconfDefaultKeyId, NULL, NULL) != OK)
456                         {
457                                 WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,  
458                                         ("whalSecur_KeysReconfig: ERROR whalSecur_DefaultKeyIdSet failure \n"));
459                                 return (NOK);
460                         }   
461                 }
462         } /* pWhalSecur->securityMode != RSN_CIPHER_NONE */
463
464
465         /* set the encryption/decryption control on the HW*/   
466         if (whalSecur_HwEncDecrEnable (pWhalSecur, pWhalSecur->reconfData.reconfHwEncEnable) != OK)
467         {
468                 WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_SECURITY_MODULE_LOG,  
469                         ("whalSecur_KeysReconfig: ERROR whalSecur_HwEncDecrEnable failure \n"));
470                 return (NOK);
471         }   
472         
473         return (OK);
474 }
475
476 /*
477  * ----------------------------------------------------------------------------
478  * Function : whalSecur_Destroy
479  *
480  * Input    : 
481  * Output   :                                                                    
482  * Process  : Unload the HAL security module
483  * Note(s)  :
484  * -----------------------------------------------------------------------------
485  */
486 int whalSecur_Destroy (TI_HANDLE hWhalSecur, UINT16 numOfStations)
487 {
488         WHAL_SECURITY* pWhalSecur = (WHAL_SECURITY *)hWhalSecur;
489
490         if (!pWhalSecur)
491                 return OK;
492 #ifdef CKIP_ENABLED
493         whalExc_Destroy (pWhalSecur->pWhalExc);
494         if (privacy_unload(pWhalSecur->pWhalPrivacy) != OK)
495                 WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_CTRL_MODULE_LOG,  (" whalSecur_Destroy: privacy_unload failure \n"));
496 #endif /* CKIP_ENABLED*/
497
498         if (whalWpa_Destroy (pWhalSecur->pWhalWpa) != OK)
499                 WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_CTRL_MODULE_LOG,  (" whalSecur_Destroy: whalWpa_Destroy failure \n"));
500         if (whalWep_Destroy (pWhalSecur->pWhalWep) != OK)
501                 WLAN_REPORT_ERROR(pWhalSecur->hReport, HAL_CTRL_MODULE_LOG,  (" whalSecur_Destroy: whalWep_Destroy failure \n"));
502
503         if (pWhalSecur->reconfData.reconfKeys)
504                 os_memoryFree (pWhalSecur->hOs, pWhalSecur->reconfData.reconfKeys, 
505                                    (sizeof(securityKeys_t))*(numOfStations*NO_OF_RECONF_SECUR_KEYS_PER_STATION+NO_OF_EXTRA_RECONF_SECUR_KEYS));
506         
507         os_memoryFree (pWhalSecur->hOs, pWhalSecur, sizeof(WHAL_SECURITY));
508         
509         return (OK);
510 }
511