[PATCH] ieee80211: TKIP and CCMP replay check rework
[deliverable/linux.git] / net / ieee80211 / ieee80211_crypt_ccmp.c
1 /*
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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. See README and COPYING for
9 * more details.
10 */
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_arp.h>
20 #include <asm/string.h>
21 #include <linux/wireless.h>
22
23 #include <net/ieee80211.h>
24
25 #include <linux/crypto.h>
26 #include <asm/scatterlist.h>
27
28 MODULE_AUTHOR("Jouni Malinen");
29 MODULE_DESCRIPTION("Host AP crypt: CCMP");
30 MODULE_LICENSE("GPL");
31
32 #define AES_BLOCK_LEN 16
33 #define CCMP_HDR_LEN 8
34 #define CCMP_MIC_LEN 8
35 #define CCMP_TK_LEN 16
36 #define CCMP_PN_LEN 6
37
38 struct ieee80211_ccmp_data {
39 u8 key[CCMP_TK_LEN];
40 int key_set;
41
42 u8 tx_pn[CCMP_PN_LEN];
43 u8 rx_pn[CCMP_PN_LEN];
44
45 u32 dot11RSNAStatsCCMPFormatErrors;
46 u32 dot11RSNAStatsCCMPReplays;
47 u32 dot11RSNAStatsCCMPDecryptErrors;
48
49 int key_idx;
50
51 struct crypto_tfm *tfm;
52
53 /* scratch buffers for virt_to_page() (crypto API) */
54 u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
55 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
56 u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
57 };
58
59 static void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm,
60 const u8 pt[16], u8 ct[16])
61 {
62 struct scatterlist src, dst;
63
64 src.page = virt_to_page(pt);
65 src.offset = offset_in_page(pt);
66 src.length = AES_BLOCK_LEN;
67
68 dst.page = virt_to_page(ct);
69 dst.offset = offset_in_page(ct);
70 dst.length = AES_BLOCK_LEN;
71
72 crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
73 }
74
75 static void *ieee80211_ccmp_init(int key_idx)
76 {
77 struct ieee80211_ccmp_data *priv;
78
79 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
80 if (priv == NULL)
81 goto fail;
82 priv->key_idx = key_idx;
83
84 priv->tfm = crypto_alloc_tfm("aes", 0);
85 if (priv->tfm == NULL) {
86 printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate "
87 "crypto API aes\n");
88 goto fail;
89 }
90
91 return priv;
92
93 fail:
94 if (priv) {
95 if (priv->tfm)
96 crypto_free_tfm(priv->tfm);
97 kfree(priv);
98 }
99
100 return NULL;
101 }
102
103 static void ieee80211_ccmp_deinit(void *priv)
104 {
105 struct ieee80211_ccmp_data *_priv = priv;
106 if (_priv && _priv->tfm)
107 crypto_free_tfm(_priv->tfm);
108 kfree(priv);
109 }
110
111 static inline void xor_block(u8 * b, u8 * a, size_t len)
112 {
113 int i;
114 for (i = 0; i < len; i++)
115 b[i] ^= a[i];
116 }
117
118 static void ccmp_init_blocks(struct crypto_tfm *tfm,
119 struct ieee80211_hdr_4addr *hdr,
120 u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0)
121 {
122 u8 *pos, qc = 0;
123 size_t aad_len;
124 u16 fc;
125 int a4_included, qc_included;
126 u8 aad[2 * AES_BLOCK_LEN];
127
128 fc = le16_to_cpu(hdr->frame_ctl);
129 a4_included = ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
130 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS));
131 qc_included = ((WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) &&
132 (WLAN_FC_GET_STYPE(fc) & IEEE80211_STYPE_QOS_DATA));
133 aad_len = 22;
134 if (a4_included)
135 aad_len += 6;
136 if (qc_included) {
137 pos = (u8 *) & hdr->addr4;
138 if (a4_included)
139 pos += 6;
140 qc = *pos & 0x0f;
141 aad_len += 2;
142 }
143
144 /* CCM Initial Block:
145 * Flag (Include authentication header, M=3 (8-octet MIC),
146 * L=1 (2-octet Dlen))
147 * Nonce: 0x00 | A2 | PN
148 * Dlen */
149 b0[0] = 0x59;
150 b0[1] = qc;
151 memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
152 memcpy(b0 + 8, pn, CCMP_PN_LEN);
153 b0[14] = (dlen >> 8) & 0xff;
154 b0[15] = dlen & 0xff;
155
156 /* AAD:
157 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
158 * A1 | A2 | A3
159 * SC with bits 4..15 (seq#) masked to zero
160 * A4 (if present)
161 * QC (if present)
162 */
163 pos = (u8 *) hdr;
164 aad[0] = 0; /* aad_len >> 8 */
165 aad[1] = aad_len & 0xff;
166 aad[2] = pos[0] & 0x8f;
167 aad[3] = pos[1] & 0xc7;
168 memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
169 pos = (u8 *) & hdr->seq_ctl;
170 aad[22] = pos[0] & 0x0f;
171 aad[23] = 0; /* all bits masked */
172 memset(aad + 24, 0, 8);
173 if (a4_included)
174 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
175 if (qc_included) {
176 aad[a4_included ? 30 : 24] = qc;
177 /* rest of QC masked */
178 }
179
180 /* Start with the first block and AAD */
181 ieee80211_ccmp_aes_encrypt(tfm, b0, auth);
182 xor_block(auth, aad, AES_BLOCK_LEN);
183 ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
184 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
185 ieee80211_ccmp_aes_encrypt(tfm, auth, auth);
186 b0[0] &= 0x07;
187 b0[14] = b0[15] = 0;
188 ieee80211_ccmp_aes_encrypt(tfm, b0, s0);
189 }
190
191 static int ieee80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
192 u8 *aeskey, int keylen, void *priv)
193 {
194 struct ieee80211_ccmp_data *key = priv;
195 int i;
196 u8 *pos;
197
198 if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
199 return -1;
200
201 if (aeskey != NULL && keylen >= CCMP_TK_LEN)
202 memcpy(aeskey, key->key, CCMP_TK_LEN);
203
204 pos = skb_push(skb, CCMP_HDR_LEN);
205 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
206 pos += hdr_len;
207
208 i = CCMP_PN_LEN - 1;
209 while (i >= 0) {
210 key->tx_pn[i]++;
211 if (key->tx_pn[i] != 0)
212 break;
213 i--;
214 }
215
216 *pos++ = key->tx_pn[5];
217 *pos++ = key->tx_pn[4];
218 *pos++ = 0;
219 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
220 *pos++ = key->tx_pn[3];
221 *pos++ = key->tx_pn[2];
222 *pos++ = key->tx_pn[1];
223 *pos++ = key->tx_pn[0];
224
225 return CCMP_HDR_LEN;
226 }
227
228 static int ieee80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
229 {
230 struct ieee80211_ccmp_data *key = priv;
231 int data_len, i, blocks, last, len;
232 u8 *pos, *mic;
233 struct ieee80211_hdr_4addr *hdr;
234 u8 *b0 = key->tx_b0;
235 u8 *b = key->tx_b;
236 u8 *e = key->tx_e;
237 u8 *s0 = key->tx_s0;
238
239 if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
240 return -1;
241
242 data_len = skb->len - hdr_len;
243 len = ieee80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
244 if (len < 0)
245 return -1;
246
247 pos = skb->data + hdr_len + CCMP_HDR_LEN;
248 mic = skb_put(skb, CCMP_MIC_LEN);
249 hdr = (struct ieee80211_hdr_4addr *)skb->data;
250 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
251
252 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
253 last = data_len % AES_BLOCK_LEN;
254
255 for (i = 1; i <= blocks; i++) {
256 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
257 /* Authentication */
258 xor_block(b, pos, len);
259 ieee80211_ccmp_aes_encrypt(key->tfm, b, b);
260 /* Encryption, with counter */
261 b0[14] = (i >> 8) & 0xff;
262 b0[15] = i & 0xff;
263 ieee80211_ccmp_aes_encrypt(key->tfm, b0, e);
264 xor_block(pos, e, len);
265 pos += len;
266 }
267
268 for (i = 0; i < CCMP_MIC_LEN; i++)
269 mic[i] = b[i] ^ s0[i];
270
271 return 0;
272 }
273
274 /*
275 * deal with seq counter wrapping correctly.
276 * refer to timer_after() for jiffies wrapping handling
277 */
278 static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
279 {
280 u32 iv32_n, iv16_n;
281 u32 iv32_o, iv16_o;
282
283 iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
284 iv16_n = (pn_n[4] << 8) | pn_n[5];
285
286 iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
287 iv16_o = (pn_o[4] << 8) | pn_o[5];
288
289 if ((s32)iv32_n - (s32)iv32_o < 0 ||
290 (iv32_n == iv32_o && iv16_n <= iv16_o))
291 return 1;
292 return 0;
293 }
294
295 static int ieee80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
296 {
297 struct ieee80211_ccmp_data *key = priv;
298 u8 keyidx, *pos;
299 struct ieee80211_hdr_4addr *hdr;
300 u8 *b0 = key->rx_b0;
301 u8 *b = key->rx_b;
302 u8 *a = key->rx_a;
303 u8 pn[6];
304 int i, blocks, last, len;
305 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
306 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
307
308 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
309 key->dot11RSNAStatsCCMPFormatErrors++;
310 return -1;
311 }
312
313 hdr = (struct ieee80211_hdr_4addr *)skb->data;
314 pos = skb->data + hdr_len;
315 keyidx = pos[3];
316 if (!(keyidx & (1 << 5))) {
317 if (net_ratelimit()) {
318 printk(KERN_DEBUG "CCMP: received packet without ExtIV"
319 " flag from " MAC_FMT "\n", MAC_ARG(hdr->addr2));
320 }
321 key->dot11RSNAStatsCCMPFormatErrors++;
322 return -2;
323 }
324 keyidx >>= 6;
325 if (key->key_idx != keyidx) {
326 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
327 "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
328 return -6;
329 }
330 if (!key->key_set) {
331 if (net_ratelimit()) {
332 printk(KERN_DEBUG "CCMP: received packet from " MAC_FMT
333 " with keyid=%d that does not have a configured"
334 " key\n", MAC_ARG(hdr->addr2), keyidx);
335 }
336 return -3;
337 }
338
339 pn[0] = pos[7];
340 pn[1] = pos[6];
341 pn[2] = pos[5];
342 pn[3] = pos[4];
343 pn[4] = pos[1];
344 pn[5] = pos[0];
345 pos += 8;
346
347 if (ccmp_replay_check(pn, key->rx_pn)) {
348 if (net_ratelimit()) {
349 printk(KERN_DEBUG "CCMP: replay detected: STA=" MAC_FMT
350 " previous PN %02x%02x%02x%02x%02x%02x "
351 "received PN %02x%02x%02x%02x%02x%02x\n",
352 MAC_ARG(hdr->addr2), MAC_ARG(key->rx_pn),
353 MAC_ARG(pn));
354 }
355 key->dot11RSNAStatsCCMPReplays++;
356 return -4;
357 }
358
359 ccmp_init_blocks(key->tfm, hdr, pn, data_len, b0, a, b);
360 xor_block(mic, b, CCMP_MIC_LEN);
361
362 blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
363 last = data_len % AES_BLOCK_LEN;
364
365 for (i = 1; i <= blocks; i++) {
366 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
367 /* Decrypt, with counter */
368 b0[14] = (i >> 8) & 0xff;
369 b0[15] = i & 0xff;
370 ieee80211_ccmp_aes_encrypt(key->tfm, b0, b);
371 xor_block(pos, b, len);
372 /* Authentication */
373 xor_block(a, pos, len);
374 ieee80211_ccmp_aes_encrypt(key->tfm, a, a);
375 pos += len;
376 }
377
378 if (memcmp(mic, a, CCMP_MIC_LEN) != 0) {
379 if (net_ratelimit()) {
380 printk(KERN_DEBUG "CCMP: decrypt failed: STA="
381 MAC_FMT "\n", MAC_ARG(hdr->addr2));
382 }
383 key->dot11RSNAStatsCCMPDecryptErrors++;
384 return -5;
385 }
386
387 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
388
389 /* Remove hdr and MIC */
390 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
391 skb_pull(skb, CCMP_HDR_LEN);
392 skb_trim(skb, skb->len - CCMP_MIC_LEN);
393
394 return keyidx;
395 }
396
397 static int ieee80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
398 {
399 struct ieee80211_ccmp_data *data = priv;
400 int keyidx;
401 struct crypto_tfm *tfm = data->tfm;
402
403 keyidx = data->key_idx;
404 memset(data, 0, sizeof(*data));
405 data->key_idx = keyidx;
406 data->tfm = tfm;
407 if (len == CCMP_TK_LEN) {
408 memcpy(data->key, key, CCMP_TK_LEN);
409 data->key_set = 1;
410 if (seq) {
411 data->rx_pn[0] = seq[5];
412 data->rx_pn[1] = seq[4];
413 data->rx_pn[2] = seq[3];
414 data->rx_pn[3] = seq[2];
415 data->rx_pn[4] = seq[1];
416 data->rx_pn[5] = seq[0];
417 }
418 crypto_cipher_setkey(data->tfm, data->key, CCMP_TK_LEN);
419 } else if (len == 0)
420 data->key_set = 0;
421 else
422 return -1;
423
424 return 0;
425 }
426
427 static int ieee80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
428 {
429 struct ieee80211_ccmp_data *data = priv;
430
431 if (len < CCMP_TK_LEN)
432 return -1;
433
434 if (!data->key_set)
435 return 0;
436 memcpy(key, data->key, CCMP_TK_LEN);
437
438 if (seq) {
439 seq[0] = data->tx_pn[5];
440 seq[1] = data->tx_pn[4];
441 seq[2] = data->tx_pn[3];
442 seq[3] = data->tx_pn[2];
443 seq[4] = data->tx_pn[1];
444 seq[5] = data->tx_pn[0];
445 }
446
447 return CCMP_TK_LEN;
448 }
449
450 static char *ieee80211_ccmp_print_stats(char *p, void *priv)
451 {
452 struct ieee80211_ccmp_data *ccmp = priv;
453 p += sprintf(p, "key[%d] alg=CCMP key_set=%d "
454 "tx_pn=%02x%02x%02x%02x%02x%02x "
455 "rx_pn=%02x%02x%02x%02x%02x%02x "
456 "format_errors=%d replays=%d decrypt_errors=%d\n",
457 ccmp->key_idx, ccmp->key_set,
458 MAC_ARG(ccmp->tx_pn), MAC_ARG(ccmp->rx_pn),
459 ccmp->dot11RSNAStatsCCMPFormatErrors,
460 ccmp->dot11RSNAStatsCCMPReplays,
461 ccmp->dot11RSNAStatsCCMPDecryptErrors);
462
463 return p;
464 }
465
466 static struct ieee80211_crypto_ops ieee80211_crypt_ccmp = {
467 .name = "CCMP",
468 .init = ieee80211_ccmp_init,
469 .deinit = ieee80211_ccmp_deinit,
470 .build_iv = ieee80211_ccmp_hdr,
471 .encrypt_mpdu = ieee80211_ccmp_encrypt,
472 .decrypt_mpdu = ieee80211_ccmp_decrypt,
473 .encrypt_msdu = NULL,
474 .decrypt_msdu = NULL,
475 .set_key = ieee80211_ccmp_set_key,
476 .get_key = ieee80211_ccmp_get_key,
477 .print_stats = ieee80211_ccmp_print_stats,
478 .extra_mpdu_prefix_len = CCMP_HDR_LEN,
479 .extra_mpdu_postfix_len = CCMP_MIC_LEN,
480 .owner = THIS_MODULE,
481 };
482
483 static int __init ieee80211_crypto_ccmp_init(void)
484 {
485 return ieee80211_register_crypto_ops(&ieee80211_crypt_ccmp);
486 }
487
488 static void __exit ieee80211_crypto_ccmp_exit(void)
489 {
490 ieee80211_unregister_crypto_ops(&ieee80211_crypt_ccmp);
491 }
492
493 module_init(ieee80211_crypto_ccmp_init);
494 module_exit(ieee80211_crypto_ccmp_exit);
This page took 0.040679 seconds and 6 git commands to generate.