xref: /openbmc/linux/net/wireless/lib80211_crypt_ccmp.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
121042e41SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2274bfb8dSJohn W. Linville /*
3274bfb8dSJohn W. Linville  * lib80211 crypt: host-based CCMP encryption implementation for lib80211
4274bfb8dSJohn W. Linville  *
5274bfb8dSJohn W. Linville  * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
6274bfb8dSJohn W. Linville  * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
7274bfb8dSJohn W. Linville  */
8274bfb8dSJohn W. Linville 
9274bfb8dSJohn W. Linville #include <linux/kernel.h>
10274bfb8dSJohn W. Linville #include <linux/err.h>
11274bfb8dSJohn W. Linville #include <linux/module.h>
12274bfb8dSJohn W. Linville #include <linux/init.h>
13274bfb8dSJohn W. Linville #include <linux/slab.h>
14274bfb8dSJohn W. Linville #include <linux/random.h>
15274bfb8dSJohn W. Linville #include <linux/skbuff.h>
16274bfb8dSJohn W. Linville #include <linux/netdevice.h>
17274bfb8dSJohn W. Linville #include <linux/if_ether.h>
18274bfb8dSJohn W. Linville #include <linux/if_arp.h>
19274bfb8dSJohn W. Linville #include <asm/string.h>
20274bfb8dSJohn W. Linville #include <linux/wireless.h>
21274bfb8dSJohn W. Linville 
22274bfb8dSJohn W. Linville #include <linux/ieee80211.h>
23274bfb8dSJohn W. Linville 
24274bfb8dSJohn W. Linville #include <linux/crypto.h>
25a11e2f85SArd Biesheuvel #include <crypto/aead.h>
26274bfb8dSJohn W. Linville 
27274bfb8dSJohn W. Linville #include <net/lib80211.h>
28274bfb8dSJohn W. Linville 
29274bfb8dSJohn W. Linville MODULE_AUTHOR("Jouni Malinen");
30274bfb8dSJohn W. Linville MODULE_DESCRIPTION("Host AP crypt: CCMP");
31274bfb8dSJohn W. Linville MODULE_LICENSE("GPL");
32274bfb8dSJohn W. Linville 
33274bfb8dSJohn W. Linville #define AES_BLOCK_LEN 16
34274bfb8dSJohn W. Linville #define CCMP_HDR_LEN 8
35274bfb8dSJohn W. Linville #define CCMP_MIC_LEN 8
36274bfb8dSJohn W. Linville #define CCMP_TK_LEN 16
37274bfb8dSJohn W. Linville #define CCMP_PN_LEN 6
38274bfb8dSJohn W. Linville 
39274bfb8dSJohn W. Linville struct lib80211_ccmp_data {
40274bfb8dSJohn W. Linville 	u8 key[CCMP_TK_LEN];
41274bfb8dSJohn W. Linville 	int key_set;
42274bfb8dSJohn W. Linville 
43274bfb8dSJohn W. Linville 	u8 tx_pn[CCMP_PN_LEN];
44274bfb8dSJohn W. Linville 	u8 rx_pn[CCMP_PN_LEN];
45274bfb8dSJohn W. Linville 
46274bfb8dSJohn W. Linville 	u32 dot11RSNAStatsCCMPFormatErrors;
47274bfb8dSJohn W. Linville 	u32 dot11RSNAStatsCCMPReplays;
48274bfb8dSJohn W. Linville 	u32 dot11RSNAStatsCCMPDecryptErrors;
49274bfb8dSJohn W. Linville 
50274bfb8dSJohn W. Linville 	int key_idx;
51274bfb8dSJohn W. Linville 
52a11e2f85SArd Biesheuvel 	struct crypto_aead *tfm;
53274bfb8dSJohn W. Linville 
54274bfb8dSJohn W. Linville 	/* scratch buffers for virt_to_page() (crypto API) */
55a11e2f85SArd Biesheuvel 	u8 tx_aad[2 * AES_BLOCK_LEN];
56a11e2f85SArd Biesheuvel 	u8 rx_aad[2 * AES_BLOCK_LEN];
57274bfb8dSJohn W. Linville };
58274bfb8dSJohn W. Linville 
lib80211_ccmp_init(int key_idx)59274bfb8dSJohn W. Linville static void *lib80211_ccmp_init(int key_idx)
60274bfb8dSJohn W. Linville {
61274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *priv;
62274bfb8dSJohn W. Linville 
63274bfb8dSJohn W. Linville 	priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
64274bfb8dSJohn W. Linville 	if (priv == NULL)
65274bfb8dSJohn W. Linville 		goto fail;
66274bfb8dSJohn W. Linville 	priv->key_idx = key_idx;
67274bfb8dSJohn W. Linville 
68a11e2f85SArd Biesheuvel 	priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
69274bfb8dSJohn W. Linville 	if (IS_ERR(priv->tfm)) {
70274bfb8dSJohn W. Linville 		priv->tfm = NULL;
71274bfb8dSJohn W. Linville 		goto fail;
72274bfb8dSJohn W. Linville 	}
73274bfb8dSJohn W. Linville 
74274bfb8dSJohn W. Linville 	return priv;
75274bfb8dSJohn W. Linville 
76274bfb8dSJohn W. Linville       fail:
77274bfb8dSJohn W. Linville 	if (priv) {
78274bfb8dSJohn W. Linville 		if (priv->tfm)
79a11e2f85SArd Biesheuvel 			crypto_free_aead(priv->tfm);
80274bfb8dSJohn W. Linville 		kfree(priv);
81274bfb8dSJohn W. Linville 	}
82274bfb8dSJohn W. Linville 
83274bfb8dSJohn W. Linville 	return NULL;
84274bfb8dSJohn W. Linville }
85274bfb8dSJohn W. Linville 
lib80211_ccmp_deinit(void * priv)86274bfb8dSJohn W. Linville static void lib80211_ccmp_deinit(void *priv)
87274bfb8dSJohn W. Linville {
88274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *_priv = priv;
89274bfb8dSJohn W. Linville 	if (_priv && _priv->tfm)
90a11e2f85SArd Biesheuvel 		crypto_free_aead(_priv->tfm);
91274bfb8dSJohn W. Linville 	kfree(priv);
92274bfb8dSJohn W. Linville }
93274bfb8dSJohn W. Linville 
ccmp_init_iv_and_aad(const struct ieee80211_hdr * hdr,const u8 * pn,u8 * iv,u8 * aad)94a11e2f85SArd Biesheuvel static int ccmp_init_iv_and_aad(const struct ieee80211_hdr *hdr,
95a11e2f85SArd Biesheuvel 				const u8 *pn, u8 *iv, u8 *aad)
96274bfb8dSJohn W. Linville {
97274bfb8dSJohn W. Linville 	u8 *pos, qc = 0;
98274bfb8dSJohn W. Linville 	size_t aad_len;
99274bfb8dSJohn W. Linville 	int a4_included, qc_included;
100274bfb8dSJohn W. Linville 
101274bfb8dSJohn W. Linville 	a4_included = ieee80211_has_a4(hdr->frame_control);
102274bfb8dSJohn W. Linville 	qc_included = ieee80211_is_data_qos(hdr->frame_control);
103274bfb8dSJohn W. Linville 
104274bfb8dSJohn W. Linville 	aad_len = 22;
105274bfb8dSJohn W. Linville 	if (a4_included)
106274bfb8dSJohn W. Linville 		aad_len += 6;
107274bfb8dSJohn W. Linville 	if (qc_included) {
108274bfb8dSJohn W. Linville 		pos = (u8 *) & hdr->addr4;
109274bfb8dSJohn W. Linville 		if (a4_included)
110274bfb8dSJohn W. Linville 			pos += 6;
111274bfb8dSJohn W. Linville 		qc = *pos & 0x0f;
112274bfb8dSJohn W. Linville 		aad_len += 2;
113274bfb8dSJohn W. Linville 	}
114274bfb8dSJohn W. Linville 
115a11e2f85SArd Biesheuvel 	/* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
116a11e2f85SArd Biesheuvel 	 * mode authentication are not allowed to collide, yet both are derived
117a11e2f85SArd Biesheuvel 	 * from the same vector. We only set L := 1 here to indicate that the
118a11e2f85SArd Biesheuvel 	 * data size can be represented in (L+1) bytes. The CCM layer will take
119a11e2f85SArd Biesheuvel 	 * care of storing the data length in the top (L+1) bytes and setting
120a11e2f85SArd Biesheuvel 	 * and clearing the other bits as is required to derive the two IVs.
121a11e2f85SArd Biesheuvel 	 */
122a11e2f85SArd Biesheuvel 	iv[0] = 0x1;
123a11e2f85SArd Biesheuvel 
124a11e2f85SArd Biesheuvel 	/* Nonce: QC | A2 | PN */
125a11e2f85SArd Biesheuvel 	iv[1] = qc;
126a11e2f85SArd Biesheuvel 	memcpy(iv + 2, hdr->addr2, ETH_ALEN);
127a11e2f85SArd Biesheuvel 	memcpy(iv + 8, pn, CCMP_PN_LEN);
128274bfb8dSJohn W. Linville 
129274bfb8dSJohn W. Linville 	/* AAD:
130274bfb8dSJohn W. Linville 	 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
131274bfb8dSJohn W. Linville 	 * A1 | A2 | A3
132274bfb8dSJohn W. Linville 	 * SC with bits 4..15 (seq#) masked to zero
133274bfb8dSJohn W. Linville 	 * A4 (if present)
134274bfb8dSJohn W. Linville 	 * QC (if present)
135274bfb8dSJohn W. Linville 	 */
136274bfb8dSJohn W. Linville 	pos = (u8 *) hdr;
137a11e2f85SArd Biesheuvel 	aad[0] = pos[0] & 0x8f;
138a11e2f85SArd Biesheuvel 	aad[1] = pos[1] & 0xc7;
139*2aec9099SJohannes Berg 	memcpy(aad + 2, &hdr->addrs, 3 * ETH_ALEN);
140274bfb8dSJohn W. Linville 	pos = (u8 *) & hdr->seq_ctrl;
141a11e2f85SArd Biesheuvel 	aad[20] = pos[0] & 0x0f;
142a11e2f85SArd Biesheuvel 	aad[21] = 0;		/* all bits masked */
143a11e2f85SArd Biesheuvel 	memset(aad + 22, 0, 8);
144274bfb8dSJohn W. Linville 	if (a4_included)
145a11e2f85SArd Biesheuvel 		memcpy(aad + 22, hdr->addr4, ETH_ALEN);
146274bfb8dSJohn W. Linville 	if (qc_included) {
147a11e2f85SArd Biesheuvel 		aad[a4_included ? 28 : 22] = qc;
148274bfb8dSJohn W. Linville 		/* rest of QC masked */
149274bfb8dSJohn W. Linville 	}
150a11e2f85SArd Biesheuvel 	return aad_len;
151274bfb8dSJohn W. Linville }
152274bfb8dSJohn W. Linville 
lib80211_ccmp_hdr(struct sk_buff * skb,int hdr_len,u8 * aeskey,int keylen,void * priv)153274bfb8dSJohn W. Linville static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
154274bfb8dSJohn W. Linville 			      u8 *aeskey, int keylen, void *priv)
155274bfb8dSJohn W. Linville {
156274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *key = priv;
157274bfb8dSJohn W. Linville 	int i;
158274bfb8dSJohn W. Linville 	u8 *pos;
159274bfb8dSJohn W. Linville 
160274bfb8dSJohn W. Linville 	if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
161274bfb8dSJohn W. Linville 		return -1;
162274bfb8dSJohn W. Linville 
163274bfb8dSJohn W. Linville 	if (aeskey != NULL && keylen >= CCMP_TK_LEN)
164274bfb8dSJohn W. Linville 		memcpy(aeskey, key->key, CCMP_TK_LEN);
165274bfb8dSJohn W. Linville 
166274bfb8dSJohn W. Linville 	pos = skb_push(skb, CCMP_HDR_LEN);
167274bfb8dSJohn W. Linville 	memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
168274bfb8dSJohn W. Linville 	pos += hdr_len;
169274bfb8dSJohn W. Linville 
170274bfb8dSJohn W. Linville 	i = CCMP_PN_LEN - 1;
171274bfb8dSJohn W. Linville 	while (i >= 0) {
172274bfb8dSJohn W. Linville 		key->tx_pn[i]++;
173274bfb8dSJohn W. Linville 		if (key->tx_pn[i] != 0)
174274bfb8dSJohn W. Linville 			break;
175274bfb8dSJohn W. Linville 		i--;
176274bfb8dSJohn W. Linville 	}
177274bfb8dSJohn W. Linville 
178274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[5];
179274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[4];
180274bfb8dSJohn W. Linville 	*pos++ = 0;
181274bfb8dSJohn W. Linville 	*pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
182274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[3];
183274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[2];
184274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[1];
185274bfb8dSJohn W. Linville 	*pos++ = key->tx_pn[0];
186274bfb8dSJohn W. Linville 
187274bfb8dSJohn W. Linville 	return CCMP_HDR_LEN;
188274bfb8dSJohn W. Linville }
189274bfb8dSJohn W. Linville 
lib80211_ccmp_encrypt(struct sk_buff * skb,int hdr_len,void * priv)190274bfb8dSJohn W. Linville static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
191274bfb8dSJohn W. Linville {
192274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *key = priv;
193274bfb8dSJohn W. Linville 	struct ieee80211_hdr *hdr;
194a11e2f85SArd Biesheuvel 	struct aead_request *req;
195a11e2f85SArd Biesheuvel 	struct scatterlist sg[2];
196a11e2f85SArd Biesheuvel 	u8 *aad = key->tx_aad;
197a11e2f85SArd Biesheuvel 	u8 iv[AES_BLOCK_LEN];
198a11e2f85SArd Biesheuvel 	int len, data_len, aad_len;
199a11e2f85SArd Biesheuvel 	int ret;
200274bfb8dSJohn W. Linville 
201274bfb8dSJohn W. Linville 	if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
202274bfb8dSJohn W. Linville 		return -1;
203274bfb8dSJohn W. Linville 
204274bfb8dSJohn W. Linville 	data_len = skb->len - hdr_len;
205274bfb8dSJohn W. Linville 	len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
206274bfb8dSJohn W. Linville 	if (len < 0)
207274bfb8dSJohn W. Linville 		return -1;
208274bfb8dSJohn W. Linville 
209a11e2f85SArd Biesheuvel 	req = aead_request_alloc(key->tfm, GFP_ATOMIC);
210a11e2f85SArd Biesheuvel 	if (!req)
211a11e2f85SArd Biesheuvel 		return -ENOMEM;
212a11e2f85SArd Biesheuvel 
213274bfb8dSJohn W. Linville 	hdr = (struct ieee80211_hdr *)skb->data;
214a11e2f85SArd Biesheuvel 	aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
215274bfb8dSJohn W. Linville 
216a11e2f85SArd Biesheuvel 	skb_put(skb, CCMP_MIC_LEN);
217274bfb8dSJohn W. Linville 
218a11e2f85SArd Biesheuvel 	sg_init_table(sg, 2);
219a11e2f85SArd Biesheuvel 	sg_set_buf(&sg[0], aad, aad_len);
220a11e2f85SArd Biesheuvel 	sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
221a11e2f85SArd Biesheuvel 		   data_len + CCMP_MIC_LEN);
222274bfb8dSJohn W. Linville 
223a11e2f85SArd Biesheuvel 	aead_request_set_callback(req, 0, NULL, NULL);
224a11e2f85SArd Biesheuvel 	aead_request_set_ad(req, aad_len);
225a11e2f85SArd Biesheuvel 	aead_request_set_crypt(req, sg, sg, data_len, iv);
226274bfb8dSJohn W. Linville 
227a11e2f85SArd Biesheuvel 	ret = crypto_aead_encrypt(req);
228a11e2f85SArd Biesheuvel 	aead_request_free(req);
229a11e2f85SArd Biesheuvel 
230a11e2f85SArd Biesheuvel 	return ret;
231274bfb8dSJohn W. Linville }
232274bfb8dSJohn W. Linville 
233274bfb8dSJohn W. Linville /*
234274bfb8dSJohn W. Linville  * deal with seq counter wrapping correctly.
235274bfb8dSJohn W. Linville  * refer to timer_after() for jiffies wrapping handling
236274bfb8dSJohn W. Linville  */
ccmp_replay_check(u8 * pn_n,u8 * pn_o)237274bfb8dSJohn W. Linville static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
238274bfb8dSJohn W. Linville {
239274bfb8dSJohn W. Linville 	u32 iv32_n, iv16_n;
240274bfb8dSJohn W. Linville 	u32 iv32_o, iv16_o;
241274bfb8dSJohn W. Linville 
242274bfb8dSJohn W. Linville 	iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
243274bfb8dSJohn W. Linville 	iv16_n = (pn_n[4] << 8) | pn_n[5];
244274bfb8dSJohn W. Linville 
245274bfb8dSJohn W. Linville 	iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
246274bfb8dSJohn W. Linville 	iv16_o = (pn_o[4] << 8) | pn_o[5];
247274bfb8dSJohn W. Linville 
248274bfb8dSJohn W. Linville 	if ((s32)iv32_n - (s32)iv32_o < 0 ||
249274bfb8dSJohn W. Linville 	    (iv32_n == iv32_o && iv16_n <= iv16_o))
250274bfb8dSJohn W. Linville 		return 1;
251274bfb8dSJohn W. Linville 	return 0;
252274bfb8dSJohn W. Linville }
253274bfb8dSJohn W. Linville 
lib80211_ccmp_decrypt(struct sk_buff * skb,int hdr_len,void * priv)254274bfb8dSJohn W. Linville static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
255274bfb8dSJohn W. Linville {
256274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *key = priv;
257274bfb8dSJohn W. Linville 	u8 keyidx, *pos;
258274bfb8dSJohn W. Linville 	struct ieee80211_hdr *hdr;
259a11e2f85SArd Biesheuvel 	struct aead_request *req;
260a11e2f85SArd Biesheuvel 	struct scatterlist sg[2];
261a11e2f85SArd Biesheuvel 	u8 *aad = key->rx_aad;
262a11e2f85SArd Biesheuvel 	u8 iv[AES_BLOCK_LEN];
263274bfb8dSJohn W. Linville 	u8 pn[6];
264a11e2f85SArd Biesheuvel 	int aad_len, ret;
265a11e2f85SArd Biesheuvel 	size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
266274bfb8dSJohn W. Linville 
267274bfb8dSJohn W. Linville 	if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
268274bfb8dSJohn W. Linville 		key->dot11RSNAStatsCCMPFormatErrors++;
269274bfb8dSJohn W. Linville 		return -1;
270274bfb8dSJohn W. Linville 	}
271274bfb8dSJohn W. Linville 
272274bfb8dSJohn W. Linville 	hdr = (struct ieee80211_hdr *)skb->data;
273274bfb8dSJohn W. Linville 	pos = skb->data + hdr_len;
274274bfb8dSJohn W. Linville 	keyidx = pos[3];
275274bfb8dSJohn W. Linville 	if (!(keyidx & (1 << 5))) {
276e87cc472SJoe Perches 		net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
277e87cc472SJoe Perches 				    hdr->addr2);
278274bfb8dSJohn W. Linville 		key->dot11RSNAStatsCCMPFormatErrors++;
279274bfb8dSJohn W. Linville 		return -2;
280274bfb8dSJohn W. Linville 	}
281274bfb8dSJohn W. Linville 	keyidx >>= 6;
282274bfb8dSJohn W. Linville 	if (key->key_idx != keyidx) {
283996bf99cSJohannes Berg 		net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
284996bf99cSJohannes Berg 				    key->key_idx, keyidx);
285274bfb8dSJohn W. Linville 		return -6;
286274bfb8dSJohn W. Linville 	}
287274bfb8dSJohn W. Linville 	if (!key->key_set) {
288e87cc472SJoe Perches 		net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
289e87cc472SJoe Perches 				    hdr->addr2, keyidx);
290274bfb8dSJohn W. Linville 		return -3;
291274bfb8dSJohn W. Linville 	}
292274bfb8dSJohn W. Linville 
293274bfb8dSJohn W. Linville 	pn[0] = pos[7];
294274bfb8dSJohn W. Linville 	pn[1] = pos[6];
295274bfb8dSJohn W. Linville 	pn[2] = pos[5];
296274bfb8dSJohn W. Linville 	pn[3] = pos[4];
297274bfb8dSJohn W. Linville 	pn[4] = pos[1];
298274bfb8dSJohn W. Linville 	pn[5] = pos[0];
299274bfb8dSJohn W. Linville 	pos += 8;
300274bfb8dSJohn W. Linville 
301274bfb8dSJohn W. Linville 	if (ccmp_replay_check(pn, key->rx_pn)) {
3026f16bf3bSJohn W. Linville #ifdef CONFIG_LIB80211_DEBUG
303e87cc472SJoe Perches 		net_dbg_ratelimited("CCMP: replay detected: STA=%pM previous PN %02x%02x%02x%02x%02x%02x received PN %02x%02x%02x%02x%02x%02x\n",
304274bfb8dSJohn W. Linville 				    hdr->addr2,
305274bfb8dSJohn W. Linville 				    key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
306274bfb8dSJohn W. Linville 				    key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
307274bfb8dSJohn W. Linville 				    pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
3086f16bf3bSJohn W. Linville #endif
309274bfb8dSJohn W. Linville 		key->dot11RSNAStatsCCMPReplays++;
310274bfb8dSJohn W. Linville 		return -4;
311274bfb8dSJohn W. Linville 	}
312274bfb8dSJohn W. Linville 
313a11e2f85SArd Biesheuvel 	req = aead_request_alloc(key->tfm, GFP_ATOMIC);
314a11e2f85SArd Biesheuvel 	if (!req)
315a11e2f85SArd Biesheuvel 		return -ENOMEM;
316274bfb8dSJohn W. Linville 
317a11e2f85SArd Biesheuvel 	aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
318274bfb8dSJohn W. Linville 
319a11e2f85SArd Biesheuvel 	sg_init_table(sg, 2);
320a11e2f85SArd Biesheuvel 	sg_set_buf(&sg[0], aad, aad_len);
321a11e2f85SArd Biesheuvel 	sg_set_buf(&sg[1], pos, data_len);
322274bfb8dSJohn W. Linville 
323a11e2f85SArd Biesheuvel 	aead_request_set_callback(req, 0, NULL, NULL);
324a11e2f85SArd Biesheuvel 	aead_request_set_ad(req, aad_len);
325a11e2f85SArd Biesheuvel 	aead_request_set_crypt(req, sg, sg, data_len, iv);
326a11e2f85SArd Biesheuvel 
327a11e2f85SArd Biesheuvel 	ret = crypto_aead_decrypt(req);
328a11e2f85SArd Biesheuvel 	aead_request_free(req);
329a11e2f85SArd Biesheuvel 
330a11e2f85SArd Biesheuvel 	if (ret) {
331a11e2f85SArd Biesheuvel 		net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM (%d)\n",
332a11e2f85SArd Biesheuvel 				    hdr->addr2, ret);
333274bfb8dSJohn W. Linville 		key->dot11RSNAStatsCCMPDecryptErrors++;
334274bfb8dSJohn W. Linville 		return -5;
335274bfb8dSJohn W. Linville 	}
336274bfb8dSJohn W. Linville 
337274bfb8dSJohn W. Linville 	memcpy(key->rx_pn, pn, CCMP_PN_LEN);
338274bfb8dSJohn W. Linville 
339274bfb8dSJohn W. Linville 	/* Remove hdr and MIC */
340274bfb8dSJohn W. Linville 	memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
341274bfb8dSJohn W. Linville 	skb_pull(skb, CCMP_HDR_LEN);
342274bfb8dSJohn W. Linville 	skb_trim(skb, skb->len - CCMP_MIC_LEN);
343274bfb8dSJohn W. Linville 
344274bfb8dSJohn W. Linville 	return keyidx;
345274bfb8dSJohn W. Linville }
346274bfb8dSJohn W. Linville 
lib80211_ccmp_set_key(void * key,int len,u8 * seq,void * priv)347274bfb8dSJohn W. Linville static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
348274bfb8dSJohn W. Linville {
349274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *data = priv;
350274bfb8dSJohn W. Linville 	int keyidx;
351a11e2f85SArd Biesheuvel 	struct crypto_aead *tfm = data->tfm;
352274bfb8dSJohn W. Linville 
353274bfb8dSJohn W. Linville 	keyidx = data->key_idx;
354274bfb8dSJohn W. Linville 	memset(data, 0, sizeof(*data));
355274bfb8dSJohn W. Linville 	data->key_idx = keyidx;
356274bfb8dSJohn W. Linville 	data->tfm = tfm;
357274bfb8dSJohn W. Linville 	if (len == CCMP_TK_LEN) {
358274bfb8dSJohn W. Linville 		memcpy(data->key, key, CCMP_TK_LEN);
359274bfb8dSJohn W. Linville 		data->key_set = 1;
360274bfb8dSJohn W. Linville 		if (seq) {
361274bfb8dSJohn W. Linville 			data->rx_pn[0] = seq[5];
362274bfb8dSJohn W. Linville 			data->rx_pn[1] = seq[4];
363274bfb8dSJohn W. Linville 			data->rx_pn[2] = seq[3];
364274bfb8dSJohn W. Linville 			data->rx_pn[3] = seq[2];
365274bfb8dSJohn W. Linville 			data->rx_pn[4] = seq[1];
366274bfb8dSJohn W. Linville 			data->rx_pn[5] = seq[0];
367274bfb8dSJohn W. Linville 		}
368a11e2f85SArd Biesheuvel 		if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) ||
369a11e2f85SArd Biesheuvel 		    crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN))
370a11e2f85SArd Biesheuvel 			return -1;
371274bfb8dSJohn W. Linville 	} else if (len == 0)
372274bfb8dSJohn W. Linville 		data->key_set = 0;
373274bfb8dSJohn W. Linville 	else
374274bfb8dSJohn W. Linville 		return -1;
375274bfb8dSJohn W. Linville 
376274bfb8dSJohn W. Linville 	return 0;
377274bfb8dSJohn W. Linville }
378274bfb8dSJohn W. Linville 
lib80211_ccmp_get_key(void * key,int len,u8 * seq,void * priv)379274bfb8dSJohn W. Linville static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
380274bfb8dSJohn W. Linville {
381274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *data = priv;
382274bfb8dSJohn W. Linville 
383274bfb8dSJohn W. Linville 	if (len < CCMP_TK_LEN)
384274bfb8dSJohn W. Linville 		return -1;
385274bfb8dSJohn W. Linville 
386274bfb8dSJohn W. Linville 	if (!data->key_set)
387274bfb8dSJohn W. Linville 		return 0;
388274bfb8dSJohn W. Linville 	memcpy(key, data->key, CCMP_TK_LEN);
389274bfb8dSJohn W. Linville 
390274bfb8dSJohn W. Linville 	if (seq) {
391274bfb8dSJohn W. Linville 		seq[0] = data->tx_pn[5];
392274bfb8dSJohn W. Linville 		seq[1] = data->tx_pn[4];
393274bfb8dSJohn W. Linville 		seq[2] = data->tx_pn[3];
394274bfb8dSJohn W. Linville 		seq[3] = data->tx_pn[2];
395274bfb8dSJohn W. Linville 		seq[4] = data->tx_pn[1];
396274bfb8dSJohn W. Linville 		seq[5] = data->tx_pn[0];
397274bfb8dSJohn W. Linville 	}
398274bfb8dSJohn W. Linville 
399274bfb8dSJohn W. Linville 	return CCMP_TK_LEN;
400274bfb8dSJohn W. Linville }
401274bfb8dSJohn W. Linville 
lib80211_ccmp_print_stats(struct seq_file * m,void * priv)4026bbefe86SDavid Howells static void lib80211_ccmp_print_stats(struct seq_file *m, void *priv)
403274bfb8dSJohn W. Linville {
404274bfb8dSJohn W. Linville 	struct lib80211_ccmp_data *ccmp = priv;
405274bfb8dSJohn W. Linville 
4066bbefe86SDavid Howells 	seq_printf(m,
4076bbefe86SDavid Howells 		   "key[%d] alg=CCMP key_set=%d "
408274bfb8dSJohn W. Linville 		   "tx_pn=%02x%02x%02x%02x%02x%02x "
409274bfb8dSJohn W. Linville 		   "rx_pn=%02x%02x%02x%02x%02x%02x "
410274bfb8dSJohn W. Linville 		   "format_errors=%d replays=%d decrypt_errors=%d\n",
411274bfb8dSJohn W. Linville 		   ccmp->key_idx, ccmp->key_set,
412274bfb8dSJohn W. Linville 		   ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
413274bfb8dSJohn W. Linville 		   ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
414274bfb8dSJohn W. Linville 		   ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
415274bfb8dSJohn W. Linville 		   ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
416274bfb8dSJohn W. Linville 		   ccmp->dot11RSNAStatsCCMPFormatErrors,
417274bfb8dSJohn W. Linville 		   ccmp->dot11RSNAStatsCCMPReplays,
418274bfb8dSJohn W. Linville 		   ccmp->dot11RSNAStatsCCMPDecryptErrors);
419274bfb8dSJohn W. Linville }
420274bfb8dSJohn W. Linville 
421274bfb8dSJohn W. Linville static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
422274bfb8dSJohn W. Linville 	.name = "CCMP",
423274bfb8dSJohn W. Linville 	.init = lib80211_ccmp_init,
424274bfb8dSJohn W. Linville 	.deinit = lib80211_ccmp_deinit,
425274bfb8dSJohn W. Linville 	.encrypt_mpdu = lib80211_ccmp_encrypt,
426274bfb8dSJohn W. Linville 	.decrypt_mpdu = lib80211_ccmp_decrypt,
427274bfb8dSJohn W. Linville 	.encrypt_msdu = NULL,
428274bfb8dSJohn W. Linville 	.decrypt_msdu = NULL,
429274bfb8dSJohn W. Linville 	.set_key = lib80211_ccmp_set_key,
430274bfb8dSJohn W. Linville 	.get_key = lib80211_ccmp_get_key,
431274bfb8dSJohn W. Linville 	.print_stats = lib80211_ccmp_print_stats,
432274bfb8dSJohn W. Linville 	.extra_mpdu_prefix_len = CCMP_HDR_LEN,
433274bfb8dSJohn W. Linville 	.extra_mpdu_postfix_len = CCMP_MIC_LEN,
434274bfb8dSJohn W. Linville 	.owner = THIS_MODULE,
435274bfb8dSJohn W. Linville };
436274bfb8dSJohn W. Linville 
lib80211_crypto_ccmp_init(void)437274bfb8dSJohn W. Linville static int __init lib80211_crypto_ccmp_init(void)
438274bfb8dSJohn W. Linville {
439274bfb8dSJohn W. Linville 	return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
440274bfb8dSJohn W. Linville }
441274bfb8dSJohn W. Linville 
lib80211_crypto_ccmp_exit(void)442274bfb8dSJohn W. Linville static void __exit lib80211_crypto_ccmp_exit(void)
443274bfb8dSJohn W. Linville {
444274bfb8dSJohn W. Linville 	lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
445274bfb8dSJohn W. Linville }
446274bfb8dSJohn W. Linville 
447274bfb8dSJohn W. Linville module_init(lib80211_crypto_ccmp_init);
448274bfb8dSJohn W. Linville module_exit(lib80211_crypto_ccmp_exit);
449