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