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