This source file includes following definitions.
- ieee80211_ccmp_init
- ieee80211_ccmp_deinit
- ccmp_init_iv_and_aad
- ieee80211_ccmp_encrypt
- ieee80211_ccmp_decrypt
- ieee80211_ccmp_set_key
- ieee80211_ccmp_get_key
- ieee80211_ccmp_print_stats
- ieee80211_crypto_ccmp_init
- ieee80211_crypto_ccmp_exit
1
2
3
4
5
6
7
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/random.h>
12 #include <linux/skbuff.h>
13 #include <linux/netdevice.h>
14 #include <linux/if_ether.h>
15 #include <linux/if_arp.h>
16 #include <linux/string.h>
17 #include <linux/wireless.h>
18
19 #include "ieee80211.h"
20
21 #include <linux/crypto.h>
22 #include <crypto/aead.h>
23 #include <linux/scatterlist.h>
24
25 MODULE_AUTHOR("Jouni Malinen");
26 MODULE_DESCRIPTION("Host AP crypt: CCMP");
27 MODULE_LICENSE("GPL");
28
29 #define AES_BLOCK_LEN 16
30 #define CCMP_HDR_LEN 8
31 #define CCMP_MIC_LEN 8
32 #define CCMP_TK_LEN 16
33 #define CCMP_PN_LEN 6
34
35 struct ieee80211_ccmp_data {
36 u8 key[CCMP_TK_LEN];
37 int key_set;
38
39 u8 tx_pn[CCMP_PN_LEN];
40 u8 rx_pn[CCMP_PN_LEN];
41
42 u32 dot11RSNAStatsCCMPFormatErrors;
43 u32 dot11RSNAStatsCCMPReplays;
44 u32 dot11RSNAStatsCCMPDecryptErrors;
45
46 int key_idx;
47
48 struct crypto_aead *tfm;
49
50
51 u8 tx_aad[2 * AES_BLOCK_LEN];
52 u8 rx_aad[2 * AES_BLOCK_LEN];
53 };
54
55 static void *ieee80211_ccmp_init(int key_idx)
56 {
57 struct ieee80211_ccmp_data *priv;
58
59 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
60 if (!priv)
61 goto fail;
62 priv->key_idx = key_idx;
63
64 priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
65 if (IS_ERR(priv->tfm)) {
66 pr_debug("ieee80211_crypt_ccmp: could not allocate crypto API aes\n");
67 priv->tfm = NULL;
68 goto fail;
69 }
70
71 return priv;
72
73 fail:
74 if (priv) {
75 if (priv->tfm)
76 crypto_free_aead(priv->tfm);
77 kfree(priv);
78 }
79
80 return NULL;
81 }
82
83 static void ieee80211_ccmp_deinit(void *priv)
84 {
85 struct ieee80211_ccmp_data *_priv = priv;
86
87 if (_priv && _priv->tfm)
88 crypto_free_aead(_priv->tfm);
89 kfree(priv);
90 }
91
92 static int ccmp_init_iv_and_aad(struct rtl_80211_hdr_4addr *hdr,
93 u8 *pn, u8 *iv, u8 *aad)
94 {
95 u8 *pos, qc = 0;
96 size_t aad_len;
97 u16 fc;
98 int a4_included, qc_included;
99
100 fc = le16_to_cpu(hdr->frame_ctl);
101 a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
102 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS));
103
104
105
106
107 qc_included = (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
108 (WLAN_FC_GET_STYPE(fc) & 0x80);
109 aad_len = 22;
110 if (a4_included)
111 aad_len += 6;
112 if (qc_included) {
113 pos = (u8 *)&hdr->addr4;
114 if (a4_included)
115 pos += 6;
116 qc = *pos & 0x0f;
117 aad_len += 2;
118 }
119
120
121
122
123
124
125
126
127 iv[0] = 0x1;
128
129
130 iv[1] = qc;
131 memcpy(iv + 2, hdr->addr2, ETH_ALEN);
132 memcpy(iv + 8, pn, CCMP_PN_LEN);
133
134
135
136
137
138
139
140
141 pos = (u8 *)hdr;
142 aad[0] = pos[0] & 0x8f;
143 aad[1] = pos[1] & 0xc7;
144 memcpy(aad + 2, hdr->addr1, 3 * ETH_ALEN);
145 pos = (u8 *)&hdr->seq_ctl;
146 aad[20] = pos[0] & 0x0f;
147 aad[21] = 0;
148 memset(aad + 22, 0, 8);
149 if (a4_included)
150 memcpy(aad + 22, hdr->addr4, ETH_ALEN);
151 if (qc_included) {
152 aad[a4_included ? 28 : 22] = qc;
153
154 }
155
156 return aad_len;
157 }
158
159 static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
160 {
161 struct ieee80211_ccmp_data *key = priv;
162 int i;
163 u8 *pos;
164 struct rtl_80211_hdr_4addr *hdr;
165 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
166
167 if (skb_headroom(skb) < CCMP_HDR_LEN ||
168 skb_tailroom(skb) < CCMP_MIC_LEN ||
169 skb->len < hdr_len)
170 return -1;
171
172 pos = skb_push(skb, CCMP_HDR_LEN);
173 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
174 pos += hdr_len;
175
176
177 i = CCMP_PN_LEN - 1;
178 while (i >= 0) {
179 key->tx_pn[i]++;
180 if (key->tx_pn[i] != 0)
181 break;
182 i--;
183 }
184
185 *pos++ = key->tx_pn[5];
186 *pos++ = key->tx_pn[4];
187 *pos++ = 0;
188 *pos++ = (key->key_idx << 6) | BIT(5) ;
189 *pos++ = key->tx_pn[3];
190 *pos++ = key->tx_pn[2];
191 *pos++ = key->tx_pn[1];
192 *pos++ = key->tx_pn[0];
193
194 hdr = (struct rtl_80211_hdr_4addr *)skb->data;
195 if (!tcb_desc->bHwSec) {
196 struct aead_request *req;
197 struct scatterlist sg[2];
198 u8 *aad = key->tx_aad;
199 u8 iv[AES_BLOCK_LEN];
200 int aad_len, ret;
201 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
202
203 req = aead_request_alloc(key->tfm, GFP_ATOMIC);
204 if (!req)
205 return -ENOMEM;
206
207 aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
208
209 skb_put(skb, CCMP_MIC_LEN);
210
211 sg_init_table(sg, 2);
212 sg_set_buf(&sg[0], aad, aad_len);
213 sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
214 data_len + CCMP_MIC_LEN);
215
216 aead_request_set_callback(req, 0, NULL, NULL);
217 aead_request_set_ad(req, aad_len);
218 aead_request_set_crypt(req, sg, sg, data_len, iv);
219
220 ret = crypto_aead_encrypt(req);
221 aead_request_free(req);
222
223 return ret;
224 }
225 return 0;
226 }
227
228 static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
229 {
230 struct ieee80211_ccmp_data *key = priv;
231 u8 keyidx, *pos;
232 struct rtl_80211_hdr_4addr *hdr;
233 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
234 u8 pn[6];
235
236 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
237 key->dot11RSNAStatsCCMPFormatErrors++;
238 return -1;
239 }
240
241 hdr = (struct rtl_80211_hdr_4addr *)skb->data;
242 pos = skb->data + hdr_len;
243 keyidx = pos[3];
244 if (!(keyidx & BIT(5))) {
245 if (net_ratelimit()) {
246 netdev_dbg(skb->dev, "CCMP: received packet without ExtIV flag from %pM\n",
247 hdr->addr2);
248 }
249 key->dot11RSNAStatsCCMPFormatErrors++;
250 return -2;
251 }
252 keyidx >>= 6;
253 if (key->key_idx != keyidx) {
254 netdev_dbg(skb->dev, "CCMP: RX tkey->key_idx=%d frame keyidx=%d priv=%p\n",
255 key->key_idx, keyidx, priv);
256 return -6;
257 }
258 if (!key->key_set) {
259 if (net_ratelimit()) {
260 netdev_dbg(skb->dev, "CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
261 hdr->addr2, keyidx);
262 }
263 return -3;
264 }
265
266 pn[0] = pos[7];
267 pn[1] = pos[6];
268 pn[2] = pos[5];
269 pn[3] = pos[4];
270 pn[4] = pos[1];
271 pn[5] = pos[0];
272 pos += 8;
273
274 if (memcmp(pn, key->rx_pn, CCMP_PN_LEN) <= 0) {
275 if (net_ratelimit()) {
276 netdev_dbg(skb->dev, "CCMP: replay detected: STA=%pM previous PN %pm received PN %pm\n",
277 hdr->addr2, key->rx_pn, pn);
278 }
279 key->dot11RSNAStatsCCMPReplays++;
280 return -4;
281 }
282 if (!tcb_desc->bHwSec) {
283 struct aead_request *req;
284 struct scatterlist sg[2];
285 u8 *aad = key->rx_aad;
286 u8 iv[AES_BLOCK_LEN];
287 int aad_len, ret;
288 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
289
290 req = aead_request_alloc(key->tfm, GFP_ATOMIC);
291 if (!req)
292 return -ENOMEM;
293
294 aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
295
296 sg_init_table(sg, 2);
297 sg_set_buf(&sg[0], aad, aad_len);
298 sg_set_buf(&sg[1], pos, data_len);
299
300 aead_request_set_callback(req, 0, NULL, NULL);
301 aead_request_set_ad(req, aad_len);
302 aead_request_set_crypt(req, sg, sg, data_len, iv);
303
304 ret = crypto_aead_decrypt(req);
305 aead_request_free(req);
306
307 if (ret) {
308 if (net_ratelimit()) {
309 netdev_dbg(skb->dev, "CCMP: decrypt failed: STA=%pM\n",
310 hdr->addr2);
311 }
312 key->dot11RSNAStatsCCMPDecryptErrors++;
313 return -5;
314 }
315
316 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
317 }
318
319 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
320 skb_pull(skb, CCMP_HDR_LEN);
321 skb_trim(skb, skb->len - CCMP_MIC_LEN);
322
323 return keyidx;
324 }
325
326 static int ieee80211_ccmp_set_key(void *key, int len, u8 *seq, void *priv)
327 {
328 struct ieee80211_ccmp_data *data = priv;
329 int keyidx;
330 struct crypto_aead *tfm = data->tfm;
331
332 keyidx = data->key_idx;
333 memset(data, 0, sizeof(*data));
334 data->key_idx = keyidx;
335 if (len == CCMP_TK_LEN) {
336 memcpy(data->key, key, CCMP_TK_LEN);
337 data->key_set = 1;
338 if (seq) {
339 data->rx_pn[0] = seq[5];
340 data->rx_pn[1] = seq[4];
341 data->rx_pn[2] = seq[3];
342 data->rx_pn[3] = seq[2];
343 data->rx_pn[4] = seq[1];
344 data->rx_pn[5] = seq[0];
345 }
346 if (crypto_aead_setauthsize(tfm, CCMP_MIC_LEN) ||
347 crypto_aead_setkey(tfm, data->key, CCMP_TK_LEN))
348 return -1;
349 } else if (len == 0) {
350 data->key_set = 0;
351 } else {
352 return -1;
353 }
354
355 return 0;
356 }
357
358 static int ieee80211_ccmp_get_key(void *key, int len, u8 *seq, void *priv)
359 {
360 struct ieee80211_ccmp_data *data = priv;
361
362 if (len < CCMP_TK_LEN)
363 return -1;
364
365 if (!data->key_set)
366 return 0;
367 memcpy(key, data->key, CCMP_TK_LEN);
368
369 if (seq) {
370 seq[0] = data->tx_pn[5];
371 seq[1] = data->tx_pn[4];
372 seq[2] = data->tx_pn[3];
373 seq[3] = data->tx_pn[2];
374 seq[4] = data->tx_pn[1];
375 seq[5] = data->tx_pn[0];
376 }
377
378 return CCMP_TK_LEN;
379 }
380
381 static char *ieee80211_ccmp_print_stats(char *p, void *priv)
382 {
383 struct ieee80211_ccmp_data *ccmp = priv;
384
385 p += sprintf(p, "key[%d] alg=CCMP key_set=%d tx_pn=%pm rx_pn=%pm format_errors=%d replays=%d decrypt_errors=%d\n",
386 ccmp->key_idx, ccmp->key_set,
387 ccmp->tx_pn, ccmp->rx_pn,
388 ccmp->dot11RSNAStatsCCMPFormatErrors,
389 ccmp->dot11RSNAStatsCCMPReplays,
390 ccmp->dot11RSNAStatsCCMPDecryptErrors);
391
392 return p;
393 }
394
395 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = {
396 .name = "CCMP",
397 .init = ieee80211_ccmp_init,
398 .deinit = ieee80211_ccmp_deinit,
399 .encrypt_mpdu = ieee80211_ccmp_encrypt,
400 .decrypt_mpdu = ieee80211_ccmp_decrypt,
401 .encrypt_msdu = NULL,
402 .decrypt_msdu = NULL,
403 .set_key = ieee80211_ccmp_set_key,
404 .get_key = ieee80211_ccmp_get_key,
405 .print_stats = ieee80211_ccmp_print_stats,
406 .extra_prefix_len = CCMP_HDR_LEN,
407 .extra_postfix_len = CCMP_MIC_LEN,
408 .owner = THIS_MODULE,
409 };
410
411 int __init ieee80211_crypto_ccmp_init(void)
412 {
413 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
414 }
415
416 void __exit ieee80211_crypto_ccmp_exit(void)
417 {
418 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);
419 }