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