• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Host AP crypt: host-based CCMP encryption implementation for Host AP driver
3  *
4  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/random.h>
11 #include <linux/skbuff.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_arp.h>
15 #include <linux/string.h>
16 #include <linux/wireless.h>
17 #include "rtllib.h"
18 
19 #include <linux/crypto.h>
20 #include <crypto/aead.h>
21 
22 #include <linux/scatterlist.h>
23 
24 #define AES_BLOCK_LEN 16
25 #define CCMP_HDR_LEN 8
26 #define CCMP_MIC_LEN 8
27 #define CCMP_TK_LEN 16
28 #define CCMP_PN_LEN 6
29 
30 struct rtllib_ccmp_data {
31 	u8 key[CCMP_TK_LEN];
32 	int key_set;
33 
34 	u8 tx_pn[CCMP_PN_LEN];
35 	u8 rx_pn[CCMP_PN_LEN];
36 
37 	u32 dot11RSNAStatsCCMPFormatErrors;
38 	u32 dot11RSNAStatsCCMPReplays;
39 	u32 dot11RSNAStatsCCMPDecryptErrors;
40 
41 	int key_idx;
42 
43 	struct crypto_aead *tfm;
44 
45 	/* scratch buffers for virt_to_page() (crypto API) */
46 	u8 tx_aad[2 * AES_BLOCK_LEN];
47 	u8 rx_aad[2 * AES_BLOCK_LEN];
48 };
49 
rtllib_ccmp_init(int key_idx)50 static void *rtllib_ccmp_init(int key_idx)
51 {
52 	struct rtllib_ccmp_data *priv;
53 
54 	priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
55 	if (priv == NULL)
56 		goto fail;
57 	priv->key_idx = key_idx;
58 
59 	priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
60 	if (IS_ERR(priv->tfm)) {
61 		pr_debug("Could not allocate crypto API aes\n");
62 		priv->tfm = NULL;
63 		goto fail;
64 	}
65 	return priv;
66 
67 fail:
68 	if (priv) {
69 		if (priv->tfm)
70 			crypto_free_aead(priv->tfm);
71 		kfree(priv);
72 	}
73 
74 	return NULL;
75 }
76 
77 
rtllib_ccmp_deinit(void * priv)78 static void rtllib_ccmp_deinit(void *priv)
79 {
80 	struct rtllib_ccmp_data *_priv = priv;
81 
82 	if (_priv && _priv->tfm)
83 		crypto_free_aead(_priv->tfm);
84 	kfree(priv);
85 }
86 
87 
ccmp_init_iv_and_aad(struct rtllib_hdr_4addr * hdr,u8 * pn,u8 * iv,u8 * aad)88 static int ccmp_init_iv_and_aad(struct rtllib_hdr_4addr *hdr,
89 				u8 *pn, u8 *iv, u8 *aad)
90 {
91 	u8 *pos, qc = 0;
92 	size_t aad_len;
93 	u16 fc;
94 	int a4_included, qc_included;
95 
96 	fc = le16_to_cpu(hdr->frame_ctl);
97 	a4_included = ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
98 		       (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS));
99 
100 	qc_included = ((WLAN_FC_GET_TYPE(fc) == RTLLIB_FTYPE_DATA) &&
101 		       (WLAN_FC_GET_STYPE(fc) & 0x80));
102 	aad_len = 22;
103 	if (a4_included)
104 		aad_len += 6;
105 	if (qc_included) {
106 		pos = (u8 *) &hdr->addr4;
107 		if (a4_included)
108 			pos += 6;
109 		qc = *pos & 0x0f;
110 		aad_len += 2;
111 	}
112 	/* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
113 	 * mode authentication are not allowed to collide, yet both are derived
114 	 * from the same vector. We only set L := 1 here to indicate that the
115 	 * data size can be represented in (L+1) bytes. The CCM layer will take
116 	 * care of storing the data length in the top (L+1) bytes and setting
117 	 * and clearing the other bits as is required to derive the two IVs.
118 	 */
119 	iv[0] = 0x1;
120 
121 	/* Nonce: QC | A2 | PN */
122 	iv[1] = qc;
123 	memcpy(iv + 2, hdr->addr2, ETH_ALEN);
124 	memcpy(iv + 8, pn, CCMP_PN_LEN);
125 
126 	/* AAD:
127 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
128 	 * A1 | A2 | A3
129 	 * SC with bits 4..15 (seq#) masked to zero
130 	 * A4 (if present)
131 	 * QC (if present)
132 	 */
133 	pos = (u8 *) hdr;
134 	aad[0] = pos[0] & 0x8f;
135 	aad[1] = pos[1] & 0xc7;
136 	memcpy(aad + 2, hdr->addr1, 3 * ETH_ALEN);
137 	pos = (u8 *) &hdr->seq_ctl;
138 	aad[20] = pos[0] & 0x0f;
139 	aad[21] = 0; /* all bits masked */
140 	memset(aad + 22, 0, 8);
141 	if (a4_included)
142 		memcpy(aad + 22, hdr->addr4, ETH_ALEN);
143 	if (qc_included) {
144 		aad[a4_included ? 28 : 22] = qc;
145 		/* rest of QC masked */
146 	}
147 
148 	return aad_len;
149 }
150 
151 
152 
rtllib_ccmp_encrypt(struct sk_buff * skb,int hdr_len,void * priv)153 static int rtllib_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
154 {
155 	struct rtllib_ccmp_data *key = priv;
156 	int i;
157 	u8 *pos;
158 	struct rtllib_hdr_4addr *hdr;
159 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
160 				    MAX_DEV_ADDR_SIZE);
161 	if (skb_headroom(skb) < CCMP_HDR_LEN ||
162 	    skb_tailroom(skb) < CCMP_MIC_LEN ||
163 	    skb->len < hdr_len)
164 		return -1;
165 
166 	pos = skb_push(skb, CCMP_HDR_LEN);
167 	memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
168 	pos += hdr_len;
169 
170 	i = CCMP_PN_LEN - 1;
171 	while (i >= 0) {
172 		key->tx_pn[i]++;
173 		if (key->tx_pn[i] != 0)
174 			break;
175 		i--;
176 	}
177 
178 	*pos++ = key->tx_pn[5];
179 	*pos++ = key->tx_pn[4];
180 	*pos++ = 0;
181 	*pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */;
182 	*pos++ = key->tx_pn[3];
183 	*pos++ = key->tx_pn[2];
184 	*pos++ = key->tx_pn[1];
185 	*pos++ = key->tx_pn[0];
186 
187 	hdr = (struct rtllib_hdr_4addr *) skb->data;
188 	if (!tcb_desc->bHwSec) {
189 		struct aead_request *req;
190 		struct scatterlist sg[2];
191 		u8 *aad = key->tx_aad;
192 		u8 iv[AES_BLOCK_LEN];
193 		int aad_len, ret;
194 		int data_len = skb->len - hdr_len - CCMP_HDR_LEN;
195 
196 		req = aead_request_alloc(key->tfm, GFP_ATOMIC);
197 		if (!req)
198 			return -ENOMEM;
199 
200 		aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
201 
202 		skb_put(skb, CCMP_MIC_LEN);
203 		sg_init_table(sg, 2);
204 		sg_set_buf(&sg[0], aad, aad_len);
205 		sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
206 			   data_len + CCMP_MIC_LEN);
207 
208 		aead_request_set_callback(req, 0, NULL, NULL);
209 		aead_request_set_ad(req, aad_len);
210 		aead_request_set_crypt(req, sg, sg, data_len, iv);
211 
212 		ret = crypto_aead_encrypt(req);
213 		aead_request_free(req);
214 
215 		return ret;
216 	}
217 
218 	return 0;
219 }
220 
221 
rtllib_ccmp_decrypt(struct sk_buff * skb,int hdr_len,void * priv)222 static int rtllib_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
223 {
224 	struct rtllib_ccmp_data *key = priv;
225 	u8 keyidx, *pos;
226 	struct rtllib_hdr_4addr *hdr;
227 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
228 				    MAX_DEV_ADDR_SIZE);
229 	u8 pn[6];
230 
231 	if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
232 		key->dot11RSNAStatsCCMPFormatErrors++;
233 		return -1;
234 	}
235 
236 	hdr = (struct rtllib_hdr_4addr *) skb->data;
237 	pos = skb->data + hdr_len;
238 	keyidx = pos[3];
239 	if (!(keyidx & (1 << 5))) {
240 		if (net_ratelimit()) {
241 			pr_debug("CCMP: received packet without ExtIV flag from %pM\n",
242 				 hdr->addr2);
243 		}
244 		key->dot11RSNAStatsCCMPFormatErrors++;
245 		return -2;
246 	}
247 	keyidx >>= 6;
248 	if (key->key_idx != keyidx) {
249 		pr_debug("CCMP: RX tkey->key_idx=%d frame keyidx=%d priv=%p\n",
250 			 key->key_idx, keyidx, priv);
251 		return -6;
252 	}
253 	if (!key->key_set) {
254 		if (net_ratelimit()) {
255 			pr_debug("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
256 				 hdr->addr2, keyidx);
257 		}
258 		return -3;
259 	}
260 
261 	pn[0] = pos[7];
262 	pn[1] = pos[6];
263 	pn[2] = pos[5];
264 	pn[3] = pos[4];
265 	pn[4] = pos[1];
266 	pn[5] = pos[0];
267 	pos += 8;
268 	if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
269 		key->dot11RSNAStatsCCMPReplays++;
270 		return -4;
271 	}
272 	if (!tcb_desc->bHwSec) {
273 		size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
274 		struct aead_request *req;
275 		struct scatterlist sg[2];
276 		u8 *aad = key->rx_aad;
277 		u8 iv[AES_BLOCK_LEN];
278 		int aad_len, ret;
279 
280 		req = aead_request_alloc(key->tfm, GFP_ATOMIC);
281 		if(!req)
282 			return -ENOMEM;
283 
284 		aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
285 
286 		sg_init_table(sg, 2);
287 		sg_set_buf(&sg[0], aad, aad_len);
288 		sg_set_buf(&sg[1], pos, data_len);
289 
290 		aead_request_set_callback(req, 0, NULL, NULL);
291 		aead_request_set_ad(req, aad_len);
292 		aead_request_set_crypt(req, sg, sg, data_len, iv);
293 
294 		ret = crypto_aead_decrypt(req);
295 		aead_request_free(req);
296 
297 		if (ret) {
298 			if (net_ratelimit()) {
299 				pr_debug("CCMP: decrypt failed: STA= %pM\n",
300 					 hdr->addr2);
301 			}
302 			key->dot11RSNAStatsCCMPDecryptErrors++;
303 			return -5;
304 		}
305 
306 		memcpy(key->rx_pn, pn, CCMP_PN_LEN);
307 	}
308 	/* Remove hdr and MIC */
309 	memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
310 	skb_pull(skb, CCMP_HDR_LEN);
311 	skb_trim(skb, skb->len - CCMP_MIC_LEN);
312 
313 	return keyidx;
314 }
315 
316 
rtllib_ccmp_set_key(void * key,int len,u8 * seq,void * priv)317 static int rtllib_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
318 {
319 	struct rtllib_ccmp_data *data = priv;
320 	int keyidx;
321 	struct crypto_aead *tfm = data->tfm;
322 
323 	keyidx = data->key_idx;
324 	memset(data, 0, sizeof(*data));
325 	data->key_idx = keyidx;
326 	data->tfm = tfm;
327 	if (len == CCMP_TK_LEN) {
328 		memcpy(data->key, key, CCMP_TK_LEN);
329 		data->key_set = 1;
330 		if (seq) {
331 			data->rx_pn[0] = seq[5];
332 			data->rx_pn[1] = seq[4];
333 			data->rx_pn[2] = seq[3];
334 			data->rx_pn[3] = seq[2];
335 			data->rx_pn[4] = seq[1];
336 			data->rx_pn[5] = seq[0];
337 		}
338 		if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) ||
339 			crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN))
340 				return -1;
341 	} else if (len == 0) {
342 		data->key_set = 0;
343 	} else {
344 		return -1;
345 	}
346 
347 	return 0;
348 }
349 
350 
rtllib_ccmp_get_key(void * key,int len,u8 * seq,void * priv)351 static int rtllib_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
352 {
353 	struct rtllib_ccmp_data *data = priv;
354 
355 	if (len < CCMP_TK_LEN)
356 		return -1;
357 
358 	if (!data->key_set)
359 		return 0;
360 	memcpy(key, data->key, CCMP_TK_LEN);
361 
362 	if (seq) {
363 		seq[0] = data->tx_pn[5];
364 		seq[1] = data->tx_pn[4];
365 		seq[2] = data->tx_pn[3];
366 		seq[3] = data->tx_pn[2];
367 		seq[4] = data->tx_pn[1];
368 		seq[5] = data->tx_pn[0];
369 	}
370 
371 	return CCMP_TK_LEN;
372 }
373 
374 
rtllib_ccmp_print_stats(struct seq_file * m,void * priv)375 static void rtllib_ccmp_print_stats(struct seq_file *m, void *priv)
376 {
377 	struct rtllib_ccmp_data *ccmp = priv;
378 
379 	seq_printf(m,
380 		   "key[%d] alg=CCMP key_set=%d tx_pn=%pM rx_pn=%pM format_errors=%d replays=%d decrypt_errors=%d\n",
381 		   ccmp->key_idx, ccmp->key_set,
382 		   ccmp->tx_pn, ccmp->rx_pn,
383 		   ccmp->dot11RSNAStatsCCMPFormatErrors,
384 		   ccmp->dot11RSNAStatsCCMPReplays,
385 		   ccmp->dot11RSNAStatsCCMPDecryptErrors);
386 }
387 
388 static struct lib80211_crypto_ops rtllib_crypt_ccmp = {
389 	.name			= "R-CCMP",
390 	.init			= rtllib_ccmp_init,
391 	.deinit			= rtllib_ccmp_deinit,
392 	.encrypt_mpdu		= rtllib_ccmp_encrypt,
393 	.decrypt_mpdu		= rtllib_ccmp_decrypt,
394 	.encrypt_msdu		= NULL,
395 	.decrypt_msdu		= NULL,
396 	.set_key		= rtllib_ccmp_set_key,
397 	.get_key		= rtllib_ccmp_get_key,
398 	.print_stats		= rtllib_ccmp_print_stats,
399 	.extra_mpdu_prefix_len = CCMP_HDR_LEN,
400 	.extra_mpdu_postfix_len = CCMP_MIC_LEN,
401 	.owner			= THIS_MODULE,
402 };
403 
404 
rtllib_crypto_ccmp_init(void)405 static int __init rtllib_crypto_ccmp_init(void)
406 {
407 	return lib80211_register_crypto_ops(&rtllib_crypt_ccmp);
408 }
409 
410 
rtllib_crypto_ccmp_exit(void)411 static void __exit rtllib_crypto_ccmp_exit(void)
412 {
413 	lib80211_unregister_crypto_ops(&rtllib_crypt_ccmp);
414 }
415 
416 module_init(rtllib_crypto_ccmp_init);
417 module_exit(rtllib_crypto_ccmp_exit);
418 
419 MODULE_LICENSE("GPL");
420