1/*
2 * keyDeriveTkip.c
3 *
4 * Copyright(c) 1998 - 2009 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/** \file wepBroadcastKeyDerivation.c
35 * \brief WEP broadcast key derivation implementation.
36 *
37 * \see wepBroadcastKeyDerivation.h
38*/
39
40/****************************************************************************
41 *                                                                          *
42 *   MODULE:	WEP broadcast key derivation                                *
43 *   PURPOSE:   WEP broadcast key derivation                                *
44 *                                                                          *
45 ****************************************************************************/
46
47#define __FILE_ID__  FILE_ID_31
48#include "osApi.h"
49#include "report.h"
50#include "rsnApi.h"
51
52#include "keyDerive.h"
53#include "keyDeriveTkip.h"
54
55#include "mainKeysSm.h"
56
57/**
58*
59* keyDeriveTkip_config
60*
61* \b Description:
62*
63* TKIP key derivation init function:
64*							- Initializes the derive & remove callback functions
65*							- Resets the key material in the system control block
66*
67* \b ARGS:
68*
69*  None
70*
71* \b RETURNS:
72*
73*  TI_OK on success, TI_NOK otherwise.
74*/
75
76TI_STATUS keyDeriveTkip_config(struct _keyDerive_t *pKeyDerive)
77{
78    pKeyDerive->derive = keyDeriveTkip_derive;
79    pKeyDerive->remove = keyDeriveTkip_remove;
80
81    return TI_OK;
82}
83
84
85/**
86*
87* keyDeriveTkip_derive
88*
89* \b Description:
90*
91* TKIP key derivation function:
92*							- Decodes the key material.
93*							- Distribute the decoded key material to the driver.
94*
95* \b ARGS:
96*
97*  I - p - Pointer to the encoded key material.
98*
99* \b RETURNS:
100*
101*  TI_OK on success, TI_NOK otherwise.
102*/
103
104TI_STATUS keyDeriveTkip_derive(struct _keyDerive_t *pKeyDerive, encodedKeyMaterial_t *pEncodedKey)
105{
106    TI_STATUS           status;
107    TSecurityKeys      key;
108    keyMaterialTkip_t   *keyMaterialTkip;
109
110    if (pEncodedKey==NULL)
111    {
112        return TI_NOK;
113    }
114
115    key.keyType = KEY_TKIP;
116    key.keyIndex = (TI_UINT8)pEncodedKey->keyId;
117    key.encLen = KEY_DERIVE_TKIP_ENC_LEN;
118
119    /* Note: Reduce 2 bytes from the size of keyMaterialTkip_t in the following check,
120	         because it is added as padding at the end due to the OS_PACKED removal. */
121    if ( pEncodedKey->keyLen < (sizeof(keyMaterialTkip_t) - 2) )
122    {
123        TRACE1(pKeyDerive->hReport, REPORT_SEVERITY_ERROR, "KEY_DERIVE_TKIP: ERROR: wrong key length %d !!!\n", pEncodedKey->keyLen);
124        return TI_NOK;
125    }
126
127    keyMaterialTkip = (keyMaterialTkip_t*)pEncodedKey->pData;
128    /* Copy encryption key */
129    os_memoryCopy(pKeyDerive->hOs, (void *)key.encKey, (void *)keyMaterialTkip->encKey, KEY_DERIVE_TKIP_ENC_LEN);
130    if (pEncodedKey->keyId & 0x10000000)
131    {   /* Copy MIC RX */
132        os_memoryCopy(pKeyDerive->hOs, (void *)key.micTxKey, (void *)keyMaterialTkip->micRxKey, KEY_DERIVE_TKIP_MIC_LEN);
133        /* Copy MIC RX */
134        os_memoryCopy(pKeyDerive->hOs, (void *)key.micRxKey, (void *)keyMaterialTkip->micTxKey, KEY_DERIVE_TKIP_MIC_LEN);
135    }
136    else
137    {  /* Copy MIC RX */
138        os_memoryCopy(pKeyDerive->hOs, (void *)key.micRxKey, (void *)keyMaterialTkip->micRxKey, KEY_DERIVE_TKIP_MIC_LEN);
139        /* Copy MIC RX */
140        os_memoryCopy(pKeyDerive->hOs, (void *)key.micTxKey, (void *)keyMaterialTkip->micTxKey, KEY_DERIVE_TKIP_MIC_LEN);
141    }
142
143    /* Copy MAC address key */
144    MAC_COPY (key.macAddress, keyMaterialTkip->macAddress);
145    /* Copy RSC */
146    os_memoryCopy(pKeyDerive->hOs, (void *)key.keyRsc, (void *)keyMaterialTkip->keyRSC, KEY_RSC_LEN);
147
148
149    status = pKeyDerive->pMainKeys->setKey(pKeyDerive->pMainKeys, &key);
150    if (status == TI_OK)
151    {
152        os_memoryCopy(pKeyDerive->hOs, &pKeyDerive->key, pEncodedKey, sizeof(encodedKeyMaterial_t));
153    }
154
155    return status;
156}
157
158/**
159*
160* wepBroadcastKeyDerivationRemove
161*
162* \b Description:
163*
164* WEP broadcast key removal function:
165*							- Remove the key material from the driver.
166*
167* \b ARGS:
168*
169*  None.
170*
171* \b RETURNS:
172*
173*  TI_OK on success, TI_NOK otherwise.
174*/
175
176TI_STATUS keyDeriveTkip_remove(struct _keyDerive_t *pKeyDerive, encodedKeyMaterial_t *pEncodedKey)
177{
178    TI_STATUS status;
179    TSecurityKeys  key;
180
181    os_memoryZero(pKeyDerive->hOs, &key, sizeof(TSecurityKeys));
182    key.keyType = KEY_TKIP;
183    key.keyIndex = (TI_UINT8)pEncodedKey->keyId;
184    key.encLen = KEY_DERIVE_TKIP_ENC_LEN;
185	MAC_COPY (key.macAddress, pEncodedKey->pData);
186
187    status = pKeyDerive->pMainKeys->removeKey(pKeyDerive->pMainKeys, &key);
188    if (status == TI_OK)
189    {
190        os_memoryZero(pKeyDerive->hOs, &pKeyDerive->key, sizeof(encodedKeyMaterial_t));
191    }
192
193    return status;
194}
195
196