1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef _wlc_key_h_
18 #define _wlc_key_h_
19 
20 struct scb;
21 struct wlc_info;
22 struct wlc_bsscfg;
23 /* Maximum # of keys that wl driver supports in S/W.
24  * Keys supported in H/W is less than or equal to WSEC_MAX_KEYS.
25  */
26 #define WSEC_MAX_KEYS		54	/* Max # of keys (50 + 4 default keys) */
27 #define WLC_DEFAULT_KEYS	4	/* Default # of keys */
28 
29 #define WSEC_MAX_WOWL_KEYS 5	/* Max keys in WOWL mode (1 + 4 default keys) */
30 
31 #define WPA2_GTK_MAX	3
32 
33 /*
34 * Max # of keys currently supported:
35 *
36 *     s/w keys if WSEC_SW(wlc->wsec).
37 *     h/w keys otherwise.
38 */
39 #define WLC_MAX_WSEC_KEYS(wlc) WSEC_MAX_KEYS
40 
41 /* number of 802.11 default (non-paired, group keys) */
42 #define WSEC_MAX_DEFAULT_KEYS	4	/* # of default keys */
43 
44 /* Max # of hardware keys supported */
45 #define WLC_MAX_WSEC_HW_KEYS(wlc) WSEC_MAX_RCMTA_KEYS
46 
47 /* Max # of hardware TKIP MIC keys supported */
48 #define WLC_MAX_TKMIC_HW_KEYS(wlc) (WSEC_MAX_TKMIC_ENGINE_KEYS)
49 
50 #define WSEC_HW_TKMIC_KEY(wlc, key, bsscfg) \
51 	((((wlc)->machwcap & MCAP_TKIPMIC)) && \
52 	 (key) && ((key)->algo == CRYPTO_ALGO_TKIP) && \
53 	 !WSEC_SOFTKEY(wlc, key, bsscfg) && \
54 	WSEC_KEY_INDEX(wlc, key) >= WLC_DEFAULT_KEYS && \
55 	(WSEC_KEY_INDEX(wlc, key) < WSEC_MAX_TKMIC_ENGINE_KEYS))
56 
57 /* index of key in key table */
58 #define WSEC_KEY_INDEX(wlc, key)	((key)->idx)
59 
60 #define WSEC_SOFTKEY(wlc, key, bsscfg) (WLC_SW_KEYS(wlc, bsscfg) || \
61 	WSEC_KEY_INDEX(wlc, key) >= WLC_MAX_WSEC_HW_KEYS(wlc))
62 
63 /* get a key, non-NULL only if key allocated and not clear */
64 #define WSEC_KEY(wlc, i)	(((wlc)->wsec_keys[i] && (wlc)->wsec_keys[i]->len) ? \
65 	(wlc)->wsec_keys[i] : NULL)
66 
67 #define WSEC_SCB_KEY_VALID(scb)	(((scb)->key && (scb)->key->len) ? true : false)
68 
69 /* default key */
70 #define WSEC_BSS_DEFAULT_KEY(bsscfg) (((bsscfg)->wsec_index == -1) ? \
71 	(struct wsec_key *)NULL:(bsscfg)->bss_def_keys[(bsscfg)->wsec_index])
72 
73 /* Macros for key management in IBSS mode */
74 #define WSEC_IBSS_MAX_PEERS	16	/* Max # of IBSS Peers */
75 #define WSEC_IBSS_RCMTA_INDEX(idx) \
76 	(((idx - WSEC_MAX_DEFAULT_KEYS) % WSEC_IBSS_MAX_PEERS) + WSEC_MAX_DEFAULT_KEYS)
77 
78 /* contiguous # key slots for infrastructure mode STA */
79 #define WSEC_BSS_STA_KEY_GROUP_SIZE	5
80 
81 typedef struct wsec_iv {
82 	u32 hi;		/* upper 32 bits of IV */
83 	u16 lo;		/* lower 16 bits of IV */
84 } wsec_iv_t;
85 
86 #define WLC_NUMRXIVS	16	/* # rx IVs (one per 802.11e TID) */
87 
88 typedef struct wsec_key {
89 	u8 ea[ETH_ALEN];	/* per station */
90 	u8 idx;		/* key index in wsec_keys array */
91 	u8 id;		/* key ID [0-3] */
92 	u8 algo;		/* CRYPTO_ALGO_AES_CCM, CRYPTO_ALGO_WEP128, etc */
93 	u8 rcmta;		/* rcmta entry index, same as idx by default */
94 	u16 flags;		/* misc flags */
95 	u8 algo_hw;		/* cache for hw register */
96 	u8 aes_mode;		/* cache for hw register */
97 	s8 iv_len;		/* IV length */
98 	s8 icv_len;		/* ICV length */
99 	u32 len;		/* key length..don't move this var */
100 	/* data is 4byte aligned */
101 	u8 data[WLAN_MAX_KEY_LEN];	/* key data */
102 	wsec_iv_t rxiv[WLC_NUMRXIVS];	/* Rx IV (one per TID) */
103 	wsec_iv_t txiv;		/* Tx IV */
104 
105 } wsec_key_t;
106 
107 #define broken_roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
108 
109 /* For use with wsec_key_t.flags */
110 
111 #define WSEC_BS_UPDATE		(1 << 0)	/* Indicates hw needs key update on BS switch */
112 #define WSEC_PRIMARY_KEY	(1 << 1)	/* Indicates this key is the primary (ie tx) key */
113 #define WSEC_TKIP_ERROR		(1 << 2)	/* Provoke deliberate MIC error */
114 #define WSEC_REPLAY_ERROR	(1 << 3)	/* Provoke deliberate replay */
115 #define WSEC_IBSS_PEER_GROUP_KEY	(1 << 7)	/* Flag: group key for a IBSS PEER */
116 #define WSEC_ICV_ERROR		(1 << 8)	/* Provoke deliberate ICV error */
117 
118 #define wlc_key_insert(a, b, c, d, e, f, g, h, i, j) (BCME_ERROR)
119 #define wlc_key_update(a, b, c) do {} while (0)
120 #define wlc_key_remove(a, b, c) do {} while (0)
121 #define wlc_key_remove_all(a, b) do {} while (0)
122 #define wlc_key_delete(a, b, c) do {} while (0)
123 #define wlc_scb_key_delete(a, b) do {} while (0)
124 #define wlc_key_lookup(a, b, c, d, e) (NULL)
125 #define wlc_key_hw_init_all(a) do {} while (0)
126 #define wlc_key_hw_init(a, b, c)  do {} while (0)
127 #define wlc_key_hw_wowl_init(a, b, c, d) do {} while (0)
128 #define wlc_key_sw_wowl_update(a, b, c, d, e) do {} while (0)
129 #define wlc_key_sw_wowl_create(a, b, c) (BCME_ERROR)
130 #define wlc_key_iv_update(a, b, c, d, e) do {(void)e; } while (0)
131 #define wlc_key_iv_init(a, b, c) do {} while (0)
132 #define wlc_key_set_error(a, b, c) (BCME_ERROR)
133 #define wlc_key_dump_hw(a, b) (BCME_ERROR)
134 #define wlc_key_dump_sw(a, b) (BCME_ERROR)
135 #define wlc_key_defkeyflag(a) (0)
136 #define wlc_rcmta_add_bssid(a, b) do {} while (0)
137 #define wlc_rcmta_del_bssid(a, b) do {} while (0)
138 #define wlc_key_scb_delete(a, b) do {} while (0)
139 
140 #endif				/* _wlc_key_h_ */
141