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