FreeBSD kernel ATH device code
ar5212_keycache.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Copyright (c) 2002-2008 Atheros Communications, Inc.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * $FreeBSD$
20 */
21#include "opt_ah.h"
22
23#include "ah.h"
24#include "ah_internal.h"
25
26#include "ar5212/ar5212.h"
27#include "ar5212/ar5212reg.h"
28#include "ar5212/ar5212desc.h"
29
30/*
31 * Note: The key cache hardware requires that each double-word
32 * pair be written in even/odd order (since the destination is
33 * a 64-bit register). Don't reorder the writes in this code
34 * w/o considering this!
35 */
36#define KEY_XOR 0xaa
37
38#define IS_MIC_ENABLED(ah) \
39 (AH5212(ah)->ah_staId1Defaults & AR_STA_ID1_CRPT_MIC_ENABLE)
40
41/*
42 * Return the size of the hardware key cache.
43 */
44uint32_t
46{
47 return AH_PRIVATE(ah)->ah_caps.halKeyCacheSize;
48}
49
50/*
51 * Return true if the specific key cache entry is valid.
52 */
54ar5212IsKeyCacheEntryValid(struct ath_hal *ah, uint16_t entry)
55{
56 if (entry < AH_PRIVATE(ah)->ah_caps.halKeyCacheSize) {
57 uint32_t val = OS_REG_READ(ah, AR_KEYTABLE_MAC1(entry));
58 if (val & AR_KEYTABLE_VALID)
59 return AH_TRUE;
60 }
61 return AH_FALSE;
62}
63
64/*
65 * Clear the specified key cache entry and any associated MIC entry.
66 */
68ar5212ResetKeyCacheEntry(struct ath_hal *ah, uint16_t entry)
69{
70 uint32_t keyType;
71
72 if (entry >= AH_PRIVATE(ah)->ah_caps.halKeyCacheSize) {
73 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: entry %u out of range\n",
74 __func__, entry);
75 return AH_FALSE;
76 }
78
79 /* XXX why not clear key type/valid bit first? */
80 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
81 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
82 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
83 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
84 OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
86 OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
87 OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
89 uint16_t micentry = entry+64; /* MIC goes at slot+64 */
90
91 HALASSERT(micentry < AH_PRIVATE(ah)->ah_caps.halKeyCacheSize);
92 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
93 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
94 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
95 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
96 /* NB: key type and MAC are known to be ok */
97 }
98 return AH_TRUE;
99}
100
101/*
102 * Sets the mac part of the specified key cache entry (and any
103 * associated MIC entry) and mark them valid.
104 *
105 * Since mac[0] is shifted off and not presented to the hardware,
106 * it does double duty as a "don't use for unicast, use for multicast
107 * matching" flag. This interface should later be extended to
108 * explicitly do that rather than overloading a bit in the MAC
109 * address.
110 */
112ar5212SetKeyCacheEntryMac(struct ath_hal *ah, uint16_t entry, const uint8_t *mac)
113{
114 uint32_t macHi, macLo;
115 uint32_t unicast_flag = AR_KEYTABLE_VALID;
116
117 if (entry >= AH_PRIVATE(ah)->ah_caps.halKeyCacheSize) {
118 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: entry %u out of range\n",
119 __func__, entry);
120 return AH_FALSE;
121 }
122 /*
123 * Set MAC address -- shifted right by 1. MacLo is
124 * the 4 MSBs, and MacHi is the 2 LSBs.
125 */
126 if (mac != AH_NULL) {
127 /*
128 * AR_KEYTABLE_VALID indicates that the address is a unicast
129 * address, which must match the transmitter address for
130 * decrypting frames.
131 * Not setting this bit allows the hardware to use the key
132 * for multicast frame decryption.
133 */
134 if (mac[0] & 0x01)
135 unicast_flag = 0;
136
137 macHi = (mac[5] << 8) | mac[4];
138 macLo = (mac[3] << 24)| (mac[2] << 16)
139 | (mac[1] << 8) | mac[0];
140 macLo >>= 1;
141 macLo |= (macHi & 1) << 31; /* carry */
142 macHi >>= 1;
143 } else {
144 macLo = macHi = 0;
145 }
146 OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
147 OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | unicast_flag);
148 return AH_TRUE;
149}
150
151/*
152 * Sets the contents of the specified key cache entry
153 * and any associated MIC entry.
154 */
156ar5212SetKeyCacheEntry(struct ath_hal *ah, uint16_t entry,
157 const HAL_KEYVAL *k, const uint8_t *mac,
158 int xorKey)
159{
160 struct ath_hal_5212 *ahp = AH5212(ah);
161 const HAL_CAPABILITIES *pCap = &AH_PRIVATE(ah)->ah_caps;
162 uint32_t key0, key1, key2, key3, key4;
163 uint32_t keyType;
164 uint32_t xorMask = xorKey ?
165 (KEY_XOR << 24 | KEY_XOR << 16 | KEY_XOR << 8 | KEY_XOR) : 0;
166
167 if (entry >= pCap->halKeyCacheSize) {
168 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: entry %u out of range\n",
169 __func__, entry);
170 return AH_FALSE;
171 }
172 switch (k->kv_type) {
175 break;
177 if (!pCap->halCipherAesCcmSupport) {
179 "%s: AES-CCM not supported by mac rev 0x%x\n",
180 __func__, AH_PRIVATE(ah)->ah_macRev);
181 return AH_FALSE;
182 }
184 break;
185 case HAL_CIPHER_TKIP:
187 if (IS_MIC_ENABLED(ah) && entry+64 >= pCap->halKeyCacheSize) {
189 "%s: entry %u inappropriate for TKIP\n",
190 __func__, entry);
191 return AH_FALSE;
192 }
193 break;
194 case HAL_CIPHER_WEP:
195 if (k->kv_len < 40 / NBBY) {
197 "%s: WEP key length %u too small\n",
198 __func__, k->kv_len);
199 return AH_FALSE;
200 }
201 if (k->kv_len <= 40 / NBBY)
203 else if (k->kv_len <= 104 / NBBY)
205 else
207 break;
208 case HAL_CIPHER_CLR:
210 break;
211 default:
212 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: cipher %u not supported\n",
213 __func__, k->kv_type);
214 return AH_FALSE;
215 }
216
217 key0 = LE_READ_4(k->kv_val+0) ^ xorMask;
218 key1 = (LE_READ_2(k->kv_val+4) ^ xorMask) & 0xffff;
219 key2 = LE_READ_4(k->kv_val+6) ^ xorMask;
220 key3 = (LE_READ_2(k->kv_val+10) ^ xorMask) & 0xffff;
221 key4 = LE_READ_4(k->kv_val+12) ^ xorMask;
222 if (k->kv_len <= 104 / NBBY)
223 key4 &= 0xff;
224
225 /*
226 * Note: key cache hardware requires that each double-word
227 * pair be written in even/odd order (since the destination is
228 * a 64-bit register). Don't reorder these writes w/o
229 * considering this!
230 */
232 uint16_t micentry = entry+64; /* MIC goes at slot+64 */
233 uint32_t mic0, mic1, mic2, mic3, mic4;
234
235 /*
236 * Invalidate the encrypt/decrypt key until the MIC
237 * key is installed so pending rx frames will fail
238 * with decrypt errors rather than a MIC error.
239 */
240 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
241 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
242 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
243 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
244 OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
246 (void) ar5212SetKeyCacheEntryMac(ah, entry, mac);
247
248 /*
249 * Write MIC entry according to new or old key layout.
250 * The MISC_MODE register is assumed already set so
251 * these writes will be handled properly (happens on
252 * attach and at every reset).
253 */
254 /* RX mic */
255 mic0 = LE_READ_4(k->kv_mic+0);
256 mic2 = LE_READ_4(k->kv_mic+4);
258 /*
259 * Both RX and TX mic values can be combined into
260 * one cache slot entry:
261 * 8*N + 800 31:0 RX Michael key 0
262 * 8*N + 804 15:0 TX Michael key 0 [31:16]
263 * 8*N + 808 31:0 RX Michael key 1
264 * 8*N + 80C 15:0 TX Michael key 0 [15:0]
265 * 8*N + 810 31:0 TX Michael key 1
266 * 8*N + 814 15:0 reserved
267 * 8*N + 818 31:0 reserved
268 * 8*N + 81C 14:0 reserved
269 * 15 key valid == 0
270 */
271 /* TX mic */
272 mic1 = LE_READ_2(k->kv_txmic+2) & 0xffff;
273 mic3 = LE_READ_2(k->kv_txmic+0) & 0xffff;
274 mic4 = LE_READ_4(k->kv_txmic+4);
275 } else {
276 mic1 = mic3 = mic4 = 0;
277 }
278 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
279 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
280 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
281 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
282 OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
283 OS_REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
285 /* NB: MIC key is not marked valid and has no MAC address */
286 OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
287 OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
288
289 /* correct intentionally corrupted key */
290 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
291 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
292 } else {
293 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
294 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
295 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
296 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
297 OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
299
300 (void) ar5212SetKeyCacheEntryMac(ah, entry, mac);
301 }
302 return AH_TRUE;
303}
@ HAL_CIPHER_CLR
Definition: ah.h:810
@ HAL_CIPHER_AES_CCM
Definition: ah.h:807
@ HAL_CIPHER_TKIP
Definition: ah.h:809
@ HAL_CIPHER_WEP
Definition: ah.h:805
@ HAL_CIPHER_AES_OCB
Definition: ah.h:806
HAL_BOOL
Definition: ah.h:93
@ AH_FALSE
Definition: ah.h:94
@ AH_TRUE
Definition: ah.h:95
@ HAL_DEBUG_ANY
Definition: ah_debug.h:62
#define LE_READ_4(p)
Definition: ah_internal.h:577
#define LE_READ_2(p)
Definition: ah_internal.h:574
#define AH_PRIVATE(_ah)
Definition: ah_internal.h:442
#define AH_NULL
Definition: ah_internal.h:28
#define HALASSERT(_x)
Definition: ah_internal.h:683
#define HALDEBUG(_ah, __m,...)
Definition: ah_internal.h:658
#define NBBY
Definition: ah_internal.h:36
#define OS_REG_WRITE(_ah, _reg, _val)
Definition: ah_osdep.h:139
#define OS_REG_READ(_ah, _reg)
Definition: ah_osdep.h:140
#define AR_KEYTABLE_TYPE_40
Definition: ar5210reg.h:408
#define AR_KEYTABLE_MAC0(n)
Definition: ar5210reg.h:411
#define AR_KEYTABLE_KEY0(n)
Definition: ar5210reg.h:402
#define AR_KEYTABLE_KEY2(n)
Definition: ar5210reg.h:404
#define AR_KEYTABLE_KEY1(n)
Definition: ar5210reg.h:403
#define AR_KEYTABLE_TYPE_104
Definition: ar5210reg.h:409
#define AR_KEYTABLE_TYPE(n)
Definition: ar5210reg.h:407
#define AR_KEYTABLE_MAC1(n)
Definition: ar5210reg.h:412
#define AR_KEYTABLE_VALID
Definition: ar5210reg.h:413
#define AR_KEYTABLE_KEY4(n)
Definition: ar5210reg.h:406
#define AR_KEYTABLE_TYPE_128
Definition: ar5210reg.h:410
#define AR_KEYTABLE_KEY3(n)
Definition: ar5210reg.h:405
#define AR_KEYTABLE_TYPE_AES
Definition: ar5211reg.h:857
#define AR_KEYTABLE_TYPE_CLR
Definition: ar5211reg.h:858
#define AH5212(_ah)
Definition: ar5212.h:354
HAL_BOOL ar5212SetKeyCacheEntryMac(struct ath_hal *ah, uint16_t entry, const uint8_t *mac)
HAL_BOOL ar5212SetKeyCacheEntry(struct ath_hal *ah, uint16_t entry, const HAL_KEYVAL *k, const uint8_t *mac, int xorKey)
#define KEY_XOR
uint32_t ar5212GetKeyCacheSize(struct ath_hal *ah)
#define IS_MIC_ENABLED(ah)
HAL_BOOL ar5212IsKeyCacheEntryValid(struct ath_hal *ah, uint16_t entry)
HAL_BOOL ar5212ResetKeyCacheEntry(struct ath_hal *ah, uint16_t entry)
#define AR_MISC_MODE_MIC_NEW_LOC_ENABLE
Definition: ar5212reg.h:964
#define AR_KEYTABLE_TYPE_TKIP
Definition: ar5212reg.h:976
#define AR_KEYTABLE_TYPE_CCM
Definition: ar5212reg.h:978
static const int keyType[]
uint32_t halCipherAesCcmSupport
Definition: ah_internal.h:235
uint16_t halKeyCacheSize
Definition: ah_internal.h:297
Definition: ah.h:783
uint8_t kv_txmic[8]
Definition: ah.h:789
uint16_t kv_len
Definition: ah.h:786
uint8_t kv_val[16]
Definition: ah.h:787
uint8_t kv_mic[8]
Definition: ah.h:788
uint8_t kv_type
Definition: ah.h:784
uint32_t ah_miscMode
Definition: ar5212.h:287
Definition: ah.h:1219