xref: /openbmc/linux/drivers/staging/vt6656/key.c (revision e2c75e76)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * File: key.c
7  *
8  * Purpose: Implement functions for 802.11i Key management
9  *
10  * Author: Jerry Chen
11  *
12  * Date: May 29, 2003
13  *
14  * Functions:
15  *
16  * Revision History:
17  *
18  */
19 
20 #include "mac.h"
21 #include "key.h"
22 #include "usbpipe.h"
23 
24 int vnt_key_init_table(struct vnt_private *priv)
25 {
26 	u8 i;
27 	u8 data[MAX_KEY_TABLE];
28 
29 	for (i = 0; i < MAX_KEY_TABLE; i++)
30 		data[i] = i;
31 
32 	return vnt_control_out(priv, MESSAGE_TYPE_CLRKEYENTRY,
33 			0, 0, ARRAY_SIZE(data), data);
34 }
35 
36 static int vnt_set_keymode(struct ieee80211_hw *hw, u8 *mac_addr,
37 			   struct ieee80211_key_conf *key, u32 key_type,
38 			   u32 mode, bool onfly_latch)
39 {
40 	struct vnt_private *priv = hw->priv;
41 	u8 broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
42 	u16 key_mode = 0;
43 	u32 entry = 0;
44 	u8 *bssid;
45 	u8 key_inx = key->keyidx;
46 	u8 i;
47 
48 	if (mac_addr)
49 		bssid = mac_addr;
50 	else
51 		bssid = &broadcast[0];
52 
53 	if (key_type != VNT_KEY_DEFAULTKEY) {
54 		for (i = 0; i < (MAX_KEY_TABLE - 1); i++) {
55 			if (!test_bit(i, &priv->key_entry_inuse)) {
56 				set_bit(i, &priv->key_entry_inuse);
57 
58 				key->hw_key_idx = i;
59 				entry = key->hw_key_idx;
60 				break;
61 			}
62 		}
63 	}
64 
65 	switch (key_type) {
66 		/* fallthrough */
67 	case VNT_KEY_DEFAULTKEY:
68 		/* default key last entry */
69 		entry = MAX_KEY_TABLE - 1;
70 		key->hw_key_idx = entry;
71 	case VNT_KEY_ALLGROUP:
72 		key_mode |= VNT_KEY_ALLGROUP;
73 		if (onfly_latch)
74 			key_mode |= VNT_KEY_ONFLY_ALL;
75 	case VNT_KEY_GROUP_ADDRESS:
76 		key_mode |= mode;
77 	case VNT_KEY_GROUP:
78 		key_mode |= (mode << 4);
79 		key_mode |= VNT_KEY_GROUP;
80 		break;
81 	case  VNT_KEY_PAIRWISE:
82 		key_mode |= mode;
83 		key_inx = 4;
84 		/* Don't save entry for pairwise key for station mode */
85 		if (priv->op_mode == NL80211_IFTYPE_STATION)
86 			clear_bit(entry, &priv->key_entry_inuse);
87 		break;
88 	default:
89 		return -EINVAL;
90 	}
91 
92 	if (onfly_latch)
93 		key_mode |= VNT_KEY_ONFLY;
94 
95 	if (mode == KEY_CTL_WEP) {
96 		if (key->keylen == WLAN_KEY_LEN_WEP40)
97 			key->key[15] &= 0x7f;
98 		if (key->keylen == WLAN_KEY_LEN_WEP104)
99 			key->key[15] |= 0x80;
100 	}
101 
102 	vnt_mac_set_keyentry(priv, key_mode, entry, key_inx, bssid, key->key);
103 
104 	return 0;
105 }
106 
107 int vnt_set_keys(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
108 		 struct ieee80211_vif *vif, struct ieee80211_key_conf *key)
109 {
110 	struct ieee80211_bss_conf *conf = &vif->bss_conf;
111 	struct vnt_private *priv = hw->priv;
112 	u8 *mac_addr = NULL;
113 	u8 key_dec_mode = 0;
114 	int ret = 0, u;
115 
116 	if (sta)
117 		mac_addr = &sta->addr[0];
118 
119 	switch (key->cipher) {
120 	case 0:
121 		for (u = 0 ; u < MAX_KEY_TABLE; u++)
122 			vnt_mac_disable_keyentry(priv, u);
123 		return ret;
124 
125 	case WLAN_CIPHER_SUITE_WEP40:
126 	case WLAN_CIPHER_SUITE_WEP104:
127 		for (u = 0; u < MAX_KEY_TABLE; u++)
128 			vnt_mac_disable_keyentry(priv, u);
129 
130 		vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
131 				KEY_CTL_WEP, true);
132 
133 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
134 
135 		return ret;
136 	case WLAN_CIPHER_SUITE_TKIP:
137 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
138 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
139 
140 		key_dec_mode = KEY_CTL_TKIP;
141 
142 		break;
143 	case WLAN_CIPHER_SUITE_CCMP:
144 		if (priv->local_id <= MAC_REVISION_A1)
145 			return -EINVAL;
146 
147 		key_dec_mode = KEY_CTL_CCMP;
148 
149 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
150 	}
151 
152 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
153 		vnt_set_keymode(hw, mac_addr, key, VNT_KEY_PAIRWISE,
154 				key_dec_mode, true);
155 	} else {
156 		vnt_set_keymode(hw, mac_addr, key, VNT_KEY_DEFAULTKEY,
157 				key_dec_mode, true);
158 
159 		vnt_set_keymode(hw, (u8 *)conf->bssid, key,
160 				VNT_KEY_GROUP_ADDRESS, key_dec_mode, true);
161 	}
162 
163 	return 0;
164 }
165