Loading...
1/*
2 * lib80211 crypt: host-based CCMP encryption implementation for lib80211
3 *
4 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
5 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. See README and COPYING for
10 * more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/err.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/random.h>
19#include <linux/skbuff.h>
20#include <linux/netdevice.h>
21#include <linux/if_ether.h>
22#include <linux/if_arp.h>
23#include <asm/string.h>
24#include <linux/wireless.h>
25
26#include <linux/ieee80211.h>
27
28#include <linux/crypto.h>
29
30#include <net/lib80211.h>
31
32MODULE_AUTHOR("Jouni Malinen");
33MODULE_DESCRIPTION("Host AP crypt: CCMP");
34MODULE_LICENSE("GPL");
35
36#define AES_BLOCK_LEN 16
37#define CCMP_HDR_LEN 8
38#define CCMP_MIC_LEN 8
39#define CCMP_TK_LEN 16
40#define CCMP_PN_LEN 6
41
42struct lib80211_ccmp_data {
43 u8 key[CCMP_TK_LEN];
44 int key_set;
45
46 u8 tx_pn[CCMP_PN_LEN];
47 u8 rx_pn[CCMP_PN_LEN];
48
49 u32 dot11RSNAStatsCCMPFormatErrors;
50 u32 dot11RSNAStatsCCMPReplays;
51 u32 dot11RSNAStatsCCMPDecryptErrors;
52
53 int key_idx;
54
55 struct crypto_cipher *tfm;
56
57 /* scratch buffers for virt_to_page() (crypto API) */
58 u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
59 tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
60 u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
61};
62
63static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher *tfm,
64 const u8 pt[16], u8 ct[16])
65{
66 crypto_cipher_encrypt_one(tfm, ct, pt);
67}
68
69static void *lib80211_ccmp_init(int key_idx)
70{
71 struct lib80211_ccmp_data *priv;
72
73 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
74 if (priv == NULL)
75 goto fail;
76 priv->key_idx = key_idx;
77
78 priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
79 if (IS_ERR(priv->tfm)) {
80 printk(KERN_DEBUG "lib80211_crypt_ccmp: could not allocate "
81 "crypto API aes\n");
82 priv->tfm = NULL;
83 goto fail;
84 }
85
86 return priv;
87
88 fail:
89 if (priv) {
90 if (priv->tfm)
91 crypto_free_cipher(priv->tfm);
92 kfree(priv);
93 }
94
95 return NULL;
96}
97
98static void lib80211_ccmp_deinit(void *priv)
99{
100 struct lib80211_ccmp_data *_priv = priv;
101 if (_priv && _priv->tfm)
102 crypto_free_cipher(_priv->tfm);
103 kfree(priv);
104}
105
106static 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
113static void ccmp_init_blocks(struct crypto_cipher *tfm,
114 struct ieee80211_hdr *hdr,
115 u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0)
116{
117 u8 *pos, qc = 0;
118 size_t aad_len;
119 int a4_included, qc_included;
120 u8 aad[2 * AES_BLOCK_LEN];
121
122 a4_included = ieee80211_has_a4(hdr->frame_control);
123 qc_included = ieee80211_is_data_qos(hdr->frame_control);
124
125 aad_len = 22;
126 if (a4_included)
127 aad_len += 6;
128 if (qc_included) {
129 pos = (u8 *) & hdr->addr4;
130 if (a4_included)
131 pos += 6;
132 qc = *pos & 0x0f;
133 aad_len += 2;
134 }
135
136 /* CCM Initial Block:
137 * Flag (Include authentication header, M=3 (8-octet MIC),
138 * L=1 (2-octet Dlen))
139 * Nonce: 0x00 | A2 | PN
140 * Dlen */
141 b0[0] = 0x59;
142 b0[1] = qc;
143 memcpy(b0 + 2, hdr->addr2, ETH_ALEN);
144 memcpy(b0 + 8, pn, CCMP_PN_LEN);
145 b0[14] = (dlen >> 8) & 0xff;
146 b0[15] = dlen & 0xff;
147
148 /* AAD:
149 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
150 * A1 | A2 | A3
151 * SC with bits 4..15 (seq#) masked to zero
152 * A4 (if present)
153 * QC (if present)
154 */
155 pos = (u8 *) hdr;
156 aad[0] = 0; /* aad_len >> 8 */
157 aad[1] = aad_len & 0xff;
158 aad[2] = pos[0] & 0x8f;
159 aad[3] = pos[1] & 0xc7;
160 memcpy(aad + 4, hdr->addr1, 3 * ETH_ALEN);
161 pos = (u8 *) & hdr->seq_ctrl;
162 aad[22] = pos[0] & 0x0f;
163 aad[23] = 0; /* all bits masked */
164 memset(aad + 24, 0, 8);
165 if (a4_included)
166 memcpy(aad + 24, hdr->addr4, ETH_ALEN);
167 if (qc_included) {
168 aad[a4_included ? 30 : 24] = qc;
169 /* rest of QC masked */
170 }
171
172 /* Start with the first block and AAD */
173 lib80211_ccmp_aes_encrypt(tfm, b0, auth);
174 xor_block(auth, aad, AES_BLOCK_LEN);
175 lib80211_ccmp_aes_encrypt(tfm, auth, auth);
176 xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
177 lib80211_ccmp_aes_encrypt(tfm, auth, auth);
178 b0[0] &= 0x07;
179 b0[14] = b0[15] = 0;
180 lib80211_ccmp_aes_encrypt(tfm, b0, s0);
181}
182
183static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
184 u8 *aeskey, int keylen, void *priv)
185{
186 struct lib80211_ccmp_data *key = priv;
187 int i;
188 u8 *pos;
189
190 if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
191 return -1;
192
193 if (aeskey != NULL && keylen >= CCMP_TK_LEN)
194 memcpy(aeskey, key->key, CCMP_TK_LEN);
195
196 pos = skb_push(skb, CCMP_HDR_LEN);
197 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
198 pos += hdr_len;
199
200 i = CCMP_PN_LEN - 1;
201 while (i >= 0) {
202 key->tx_pn[i]++;
203 if (key->tx_pn[i] != 0)
204 break;
205 i--;
206 }
207
208 *pos++ = key->tx_pn[5];
209 *pos++ = key->tx_pn[4];
210 *pos++ = 0;
211 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
212 *pos++ = key->tx_pn[3];
213 *pos++ = key->tx_pn[2];
214 *pos++ = key->tx_pn[1];
215 *pos++ = key->tx_pn[0];
216
217 return CCMP_HDR_LEN;
218}
219
220static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
221{
222 struct lib80211_ccmp_data *key = priv;
223 int data_len, i, blocks, last, len;
224 u8 *pos, *mic;
225 struct ieee80211_hdr *hdr;
226 u8 *b0 = key->tx_b0;
227 u8 *b = key->tx_b;
228 u8 *e = key->tx_e;
229 u8 *s0 = key->tx_s0;
230
231 if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
232 return -1;
233
234 data_len = skb->len - hdr_len;
235 len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
236 if (len < 0)
237 return -1;
238
239 pos = skb->data + hdr_len + CCMP_HDR_LEN;
240 hdr = (struct ieee80211_hdr *)skb->data;
241 ccmp_init_blocks(key->tfm, hdr, key->tx_pn, data_len, b0, b, s0);
242
243 blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN);
244 last = data_len % AES_BLOCK_LEN;
245
246 for (i = 1; i <= blocks; i++) {
247 len = (i == blocks && last) ? last : AES_BLOCK_LEN;
248 /* Authentication */
249 xor_block(b, pos, len);
250 lib80211_ccmp_aes_encrypt(key->tfm, b, b);
251 /* Encryption, with counter */
252 b0[14] = (i >> 8) & 0xff;
253 b0[15] = i & 0xff;
254 lib80211_ccmp_aes_encrypt(key->tfm, b0, e);
255 xor_block(pos, e, len);
256 pos += len;
257 }
258
259 mic = skb_put(skb, CCMP_MIC_LEN);
260 for (i = 0; i < CCMP_MIC_LEN; i++)
261 mic[i] = b[i] ^ s0[i];
262
263 return 0;
264}
265
266/*
267 * deal with seq counter wrapping correctly.
268 * refer to timer_after() for jiffies wrapping handling
269 */
270static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
271{
272 u32 iv32_n, iv16_n;
273 u32 iv32_o, iv16_o;
274
275 iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
276 iv16_n = (pn_n[4] << 8) | pn_n[5];
277
278 iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
279 iv16_o = (pn_o[4] << 8) | pn_o[5];
280
281 if ((s32)iv32_n - (s32)iv32_o < 0 ||
282 (iv32_n == iv32_o && iv16_n <= iv16_o))
283 return 1;
284 return 0;
285}
286
287static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
288{
289 struct lib80211_ccmp_data *key = priv;
290 u8 keyidx, *pos;
291 struct ieee80211_hdr *hdr;
292 u8 *b0 = key->rx_b0;
293 u8 *b = key->rx_b;
294 u8 *a = key->rx_a;
295 u8 pn[6];
296 int i, blocks, last, len;
297 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN - CCMP_MIC_LEN;
298 u8 *mic = skb->data + skb->len - CCMP_MIC_LEN;
299
300 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
301 key->dot11RSNAStatsCCMPFormatErrors++;
302 return -1;
303 }
304
305 hdr = (struct ieee80211_hdr *)skb->data;
306 pos = skb->data + hdr_len;
307 keyidx = pos[3];
308 if (!(keyidx & (1 << 5))) {
309 if (net_ratelimit()) {
310 printk(KERN_DEBUG "CCMP: received packet without ExtIV"
311 " flag from %pM\n", hdr->addr2);
312 }
313 key->dot11RSNAStatsCCMPFormatErrors++;
314 return -2;
315 }
316 keyidx >>= 6;
317 if (key->key_idx != keyidx) {
318 printk(KERN_DEBUG "CCMP: RX tkey->key_idx=%d frame "
319 "keyidx=%d priv=%p\n", key->key_idx, keyidx, priv);
320 return -6;
321 }
322 if (!key->key_set) {
323 if (net_ratelimit()) {
324 printk(KERN_DEBUG "CCMP: received packet from %pM"
325 " with keyid=%d that does not have a configured"
326 " key\n", hdr->addr2, keyidx);
327 }
328 return -3;
329 }
330
331 pn[0] = pos[7];
332 pn[1] = pos[6];
333 pn[2] = pos[5];
334 pn[3] = pos[4];
335 pn[4] = pos[1];
336 pn[5] = pos[0];
337 pos += 8;
338
339 if (ccmp_replay_check(pn, key->rx_pn)) {
340#ifdef CONFIG_LIB80211_DEBUG
341 if (net_ratelimit()) {
342 printk(KERN_DEBUG "CCMP: replay detected: STA=%pM "
343 "previous PN %02x%02x%02x%02x%02x%02x "
344 "received PN %02x%02x%02x%02x%02x%02x\n",
345 hdr->addr2,
346 key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
347 key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
348 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
349 }
350#endif
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 lib80211_ccmp_aes_encrypt(key->tfm, b0, b);
367 xor_block(pos, b, len);
368 /* Authentication */
369 xor_block(a, pos, len);
370 lib80211_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 "%pM\n", 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
393static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
394{
395 struct lib80211_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
423static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
424{
425 struct lib80211_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
446static char *lib80211_ccmp_print_stats(char *p, void *priv)
447{
448 struct lib80211_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
466static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
467 .name = "CCMP",
468 .init = lib80211_ccmp_init,
469 .deinit = lib80211_ccmp_deinit,
470 .encrypt_mpdu = lib80211_ccmp_encrypt,
471 .decrypt_mpdu = lib80211_ccmp_decrypt,
472 .encrypt_msdu = NULL,
473 .decrypt_msdu = NULL,
474 .set_key = lib80211_ccmp_set_key,
475 .get_key = lib80211_ccmp_get_key,
476 .print_stats = lib80211_ccmp_print_stats,
477 .extra_mpdu_prefix_len = CCMP_HDR_LEN,
478 .extra_mpdu_postfix_len = CCMP_MIC_LEN,
479 .owner = THIS_MODULE,
480};
481
482static int __init lib80211_crypto_ccmp_init(void)
483{
484 return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
485}
486
487static void __exit lib80211_crypto_ccmp_exit(void)
488{
489 lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
490}
491
492module_init(lib80211_crypto_ccmp_init);
493module_exit(lib80211_crypto_ccmp_exit);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * lib80211 crypt: host-based CCMP encryption implementation for lib80211
4 *
5 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
7 */
8
9#include <linux/kernel.h>
10#include <linux/err.h>
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/random.h>
15#include <linux/skbuff.h>
16#include <linux/netdevice.h>
17#include <linux/if_ether.h>
18#include <linux/if_arp.h>
19#include <asm/string.h>
20#include <linux/wireless.h>
21
22#include <linux/ieee80211.h>
23
24#include <linux/crypto.h>
25#include <crypto/aead.h>
26
27#include <net/lib80211.h>
28
29MODULE_AUTHOR("Jouni Malinen");
30MODULE_DESCRIPTION("Host AP crypt: CCMP");
31MODULE_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
39struct lib80211_ccmp_data {
40 u8 key[CCMP_TK_LEN];
41 int key_set;
42
43 u8 tx_pn[CCMP_PN_LEN];
44 u8 rx_pn[CCMP_PN_LEN];
45
46 u32 dot11RSNAStatsCCMPFormatErrors;
47 u32 dot11RSNAStatsCCMPReplays;
48 u32 dot11RSNAStatsCCMPDecryptErrors;
49
50 int key_idx;
51
52 struct crypto_aead *tfm;
53
54 /* scratch buffers for virt_to_page() (crypto API) */
55 u8 tx_aad[2 * AES_BLOCK_LEN];
56 u8 rx_aad[2 * AES_BLOCK_LEN];
57};
58
59static void *lib80211_ccmp_init(int key_idx)
60{
61 struct lib80211_ccmp_data *priv;
62
63 priv = kzalloc(sizeof(*priv), GFP_ATOMIC);
64 if (priv == NULL)
65 goto fail;
66 priv->key_idx = key_idx;
67
68 priv->tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
69 if (IS_ERR(priv->tfm)) {
70 priv->tfm = NULL;
71 goto fail;
72 }
73
74 return priv;
75
76 fail:
77 if (priv) {
78 if (priv->tfm)
79 crypto_free_aead(priv->tfm);
80 kfree(priv);
81 }
82
83 return NULL;
84}
85
86static void lib80211_ccmp_deinit(void *priv)
87{
88 struct lib80211_ccmp_data *_priv = priv;
89 if (_priv && _priv->tfm)
90 crypto_free_aead(_priv->tfm);
91 kfree(priv);
92}
93
94static int ccmp_init_iv_and_aad(const struct ieee80211_hdr *hdr,
95 const u8 *pn, u8 *iv, u8 *aad)
96{
97 u8 *pos, qc = 0;
98 size_t aad_len;
99 int a4_included, qc_included;
100
101 a4_included = ieee80211_has_a4(hdr->frame_control);
102 qc_included = ieee80211_is_data_qos(hdr->frame_control);
103
104 aad_len = 22;
105 if (a4_included)
106 aad_len += 6;
107 if (qc_included) {
108 pos = (u8 *) & hdr->addr4;
109 if (a4_included)
110 pos += 6;
111 qc = *pos & 0x0f;
112 aad_len += 2;
113 }
114
115 /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
116 * mode authentication are not allowed to collide, yet both are derived
117 * from the same vector. We only set L := 1 here to indicate that the
118 * data size can be represented in (L+1) bytes. The CCM layer will take
119 * care of storing the data length in the top (L+1) bytes and setting
120 * and clearing the other bits as is required to derive the two IVs.
121 */
122 iv[0] = 0x1;
123
124 /* Nonce: QC | A2 | PN */
125 iv[1] = qc;
126 memcpy(iv + 2, hdr->addr2, ETH_ALEN);
127 memcpy(iv + 8, pn, CCMP_PN_LEN);
128
129 /* AAD:
130 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
131 * A1 | A2 | A3
132 * SC with bits 4..15 (seq#) masked to zero
133 * A4 (if present)
134 * QC (if present)
135 */
136 pos = (u8 *) hdr;
137 aad[0] = pos[0] & 0x8f;
138 aad[1] = pos[1] & 0xc7;
139 memcpy(aad + 2, hdr->addr1, 3 * ETH_ALEN);
140 pos = (u8 *) & hdr->seq_ctrl;
141 aad[20] = pos[0] & 0x0f;
142 aad[21] = 0; /* all bits masked */
143 memset(aad + 22, 0, 8);
144 if (a4_included)
145 memcpy(aad + 22, hdr->addr4, ETH_ALEN);
146 if (qc_included) {
147 aad[a4_included ? 28 : 22] = qc;
148 /* rest of QC masked */
149 }
150 return aad_len;
151}
152
153static int lib80211_ccmp_hdr(struct sk_buff *skb, int hdr_len,
154 u8 *aeskey, int keylen, void *priv)
155{
156 struct lib80211_ccmp_data *key = priv;
157 int i;
158 u8 *pos;
159
160 if (skb_headroom(skb) < CCMP_HDR_LEN || skb->len < hdr_len)
161 return -1;
162
163 if (aeskey != NULL && keylen >= CCMP_TK_LEN)
164 memcpy(aeskey, key->key, CCMP_TK_LEN);
165
166 pos = skb_push(skb, CCMP_HDR_LEN);
167 memmove(pos, pos + CCMP_HDR_LEN, hdr_len);
168 pos += hdr_len;
169
170 i = CCMP_PN_LEN - 1;
171 while (i >= 0) {
172 key->tx_pn[i]++;
173 if (key->tx_pn[i] != 0)
174 break;
175 i--;
176 }
177
178 *pos++ = key->tx_pn[5];
179 *pos++ = key->tx_pn[4];
180 *pos++ = 0;
181 *pos++ = (key->key_idx << 6) | (1 << 5) /* Ext IV included */ ;
182 *pos++ = key->tx_pn[3];
183 *pos++ = key->tx_pn[2];
184 *pos++ = key->tx_pn[1];
185 *pos++ = key->tx_pn[0];
186
187 return CCMP_HDR_LEN;
188}
189
190static int lib80211_ccmp_encrypt(struct sk_buff *skb, int hdr_len, void *priv)
191{
192 struct lib80211_ccmp_data *key = priv;
193 struct ieee80211_hdr *hdr;
194 struct aead_request *req;
195 struct scatterlist sg[2];
196 u8 *aad = key->tx_aad;
197 u8 iv[AES_BLOCK_LEN];
198 int len, data_len, aad_len;
199 int ret;
200
201 if (skb_tailroom(skb) < CCMP_MIC_LEN || skb->len < hdr_len)
202 return -1;
203
204 data_len = skb->len - hdr_len;
205 len = lib80211_ccmp_hdr(skb, hdr_len, NULL, 0, priv);
206 if (len < 0)
207 return -1;
208
209 req = aead_request_alloc(key->tfm, GFP_ATOMIC);
210 if (!req)
211 return -ENOMEM;
212
213 hdr = (struct ieee80211_hdr *)skb->data;
214 aad_len = ccmp_init_iv_and_aad(hdr, key->tx_pn, iv, aad);
215
216 skb_put(skb, CCMP_MIC_LEN);
217
218 sg_init_table(sg, 2);
219 sg_set_buf(&sg[0], aad, aad_len);
220 sg_set_buf(&sg[1], skb->data + hdr_len + CCMP_HDR_LEN,
221 data_len + CCMP_MIC_LEN);
222
223 aead_request_set_callback(req, 0, NULL, NULL);
224 aead_request_set_ad(req, aad_len);
225 aead_request_set_crypt(req, sg, sg, data_len, iv);
226
227 ret = crypto_aead_encrypt(req);
228 aead_request_free(req);
229
230 return ret;
231}
232
233/*
234 * deal with seq counter wrapping correctly.
235 * refer to timer_after() for jiffies wrapping handling
236 */
237static inline int ccmp_replay_check(u8 *pn_n, u8 *pn_o)
238{
239 u32 iv32_n, iv16_n;
240 u32 iv32_o, iv16_o;
241
242 iv32_n = (pn_n[0] << 24) | (pn_n[1] << 16) | (pn_n[2] << 8) | pn_n[3];
243 iv16_n = (pn_n[4] << 8) | pn_n[5];
244
245 iv32_o = (pn_o[0] << 24) | (pn_o[1] << 16) | (pn_o[2] << 8) | pn_o[3];
246 iv16_o = (pn_o[4] << 8) | pn_o[5];
247
248 if ((s32)iv32_n - (s32)iv32_o < 0 ||
249 (iv32_n == iv32_o && iv16_n <= iv16_o))
250 return 1;
251 return 0;
252}
253
254static int lib80211_ccmp_decrypt(struct sk_buff *skb, int hdr_len, void *priv)
255{
256 struct lib80211_ccmp_data *key = priv;
257 u8 keyidx, *pos;
258 struct ieee80211_hdr *hdr;
259 struct aead_request *req;
260 struct scatterlist sg[2];
261 u8 *aad = key->rx_aad;
262 u8 iv[AES_BLOCK_LEN];
263 u8 pn[6];
264 int aad_len, ret;
265 size_t data_len = skb->len - hdr_len - CCMP_HDR_LEN;
266
267 if (skb->len < hdr_len + CCMP_HDR_LEN + CCMP_MIC_LEN) {
268 key->dot11RSNAStatsCCMPFormatErrors++;
269 return -1;
270 }
271
272 hdr = (struct ieee80211_hdr *)skb->data;
273 pos = skb->data + hdr_len;
274 keyidx = pos[3];
275 if (!(keyidx & (1 << 5))) {
276 net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
277 hdr->addr2);
278 key->dot11RSNAStatsCCMPFormatErrors++;
279 return -2;
280 }
281 keyidx >>= 6;
282 if (key->key_idx != keyidx) {
283 net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
284 key->key_idx, keyidx);
285 return -6;
286 }
287 if (!key->key_set) {
288 net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
289 hdr->addr2, keyidx);
290 return -3;
291 }
292
293 pn[0] = pos[7];
294 pn[1] = pos[6];
295 pn[2] = pos[5];
296 pn[3] = pos[4];
297 pn[4] = pos[1];
298 pn[5] = pos[0];
299 pos += 8;
300
301 if (ccmp_replay_check(pn, key->rx_pn)) {
302#ifdef CONFIG_LIB80211_DEBUG
303 net_dbg_ratelimited("CCMP: replay detected: STA=%pM previous PN %02x%02x%02x%02x%02x%02x received PN %02x%02x%02x%02x%02x%02x\n",
304 hdr->addr2,
305 key->rx_pn[0], key->rx_pn[1], key->rx_pn[2],
306 key->rx_pn[3], key->rx_pn[4], key->rx_pn[5],
307 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
308#endif
309 key->dot11RSNAStatsCCMPReplays++;
310 return -4;
311 }
312
313 req = aead_request_alloc(key->tfm, GFP_ATOMIC);
314 if (!req)
315 return -ENOMEM;
316
317 aad_len = ccmp_init_iv_and_aad(hdr, pn, iv, aad);
318
319 sg_init_table(sg, 2);
320 sg_set_buf(&sg[0], aad, aad_len);
321 sg_set_buf(&sg[1], pos, data_len);
322
323 aead_request_set_callback(req, 0, NULL, NULL);
324 aead_request_set_ad(req, aad_len);
325 aead_request_set_crypt(req, sg, sg, data_len, iv);
326
327 ret = crypto_aead_decrypt(req);
328 aead_request_free(req);
329
330 if (ret) {
331 net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM (%d)\n",
332 hdr->addr2, ret);
333 key->dot11RSNAStatsCCMPDecryptErrors++;
334 return -5;
335 }
336
337 memcpy(key->rx_pn, pn, CCMP_PN_LEN);
338
339 /* Remove hdr and MIC */
340 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdr_len);
341 skb_pull(skb, CCMP_HDR_LEN);
342 skb_trim(skb, skb->len - CCMP_MIC_LEN);
343
344 return keyidx;
345}
346
347static int lib80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
348{
349 struct lib80211_ccmp_data *data = priv;
350 int keyidx;
351 struct crypto_aead *tfm = data->tfm;
352
353 keyidx = data->key_idx;
354 memset(data, 0, sizeof(*data));
355 data->key_idx = keyidx;
356 data->tfm = tfm;
357 if (len == CCMP_TK_LEN) {
358 memcpy(data->key, key, CCMP_TK_LEN);
359 data->key_set = 1;
360 if (seq) {
361 data->rx_pn[0] = seq[5];
362 data->rx_pn[1] = seq[4];
363 data->rx_pn[2] = seq[3];
364 data->rx_pn[3] = seq[2];
365 data->rx_pn[4] = seq[1];
366 data->rx_pn[5] = seq[0];
367 }
368 if (crypto_aead_setauthsize(data->tfm, CCMP_MIC_LEN) ||
369 crypto_aead_setkey(data->tfm, data->key, CCMP_TK_LEN))
370 return -1;
371 } else if (len == 0)
372 data->key_set = 0;
373 else
374 return -1;
375
376 return 0;
377}
378
379static int lib80211_ccmp_get_key(void *key, int len, u8 * seq, void *priv)
380{
381 struct lib80211_ccmp_data *data = priv;
382
383 if (len < CCMP_TK_LEN)
384 return -1;
385
386 if (!data->key_set)
387 return 0;
388 memcpy(key, data->key, CCMP_TK_LEN);
389
390 if (seq) {
391 seq[0] = data->tx_pn[5];
392 seq[1] = data->tx_pn[4];
393 seq[2] = data->tx_pn[3];
394 seq[3] = data->tx_pn[2];
395 seq[4] = data->tx_pn[1];
396 seq[5] = data->tx_pn[0];
397 }
398
399 return CCMP_TK_LEN;
400}
401
402static void lib80211_ccmp_print_stats(struct seq_file *m, void *priv)
403{
404 struct lib80211_ccmp_data *ccmp = priv;
405
406 seq_printf(m,
407 "key[%d] alg=CCMP key_set=%d "
408 "tx_pn=%02x%02x%02x%02x%02x%02x "
409 "rx_pn=%02x%02x%02x%02x%02x%02x "
410 "format_errors=%d replays=%d decrypt_errors=%d\n",
411 ccmp->key_idx, ccmp->key_set,
412 ccmp->tx_pn[0], ccmp->tx_pn[1], ccmp->tx_pn[2],
413 ccmp->tx_pn[3], ccmp->tx_pn[4], ccmp->tx_pn[5],
414 ccmp->rx_pn[0], ccmp->rx_pn[1], ccmp->rx_pn[2],
415 ccmp->rx_pn[3], ccmp->rx_pn[4], ccmp->rx_pn[5],
416 ccmp->dot11RSNAStatsCCMPFormatErrors,
417 ccmp->dot11RSNAStatsCCMPReplays,
418 ccmp->dot11RSNAStatsCCMPDecryptErrors);
419}
420
421static struct lib80211_crypto_ops lib80211_crypt_ccmp = {
422 .name = "CCMP",
423 .init = lib80211_ccmp_init,
424 .deinit = lib80211_ccmp_deinit,
425 .encrypt_mpdu = lib80211_ccmp_encrypt,
426 .decrypt_mpdu = lib80211_ccmp_decrypt,
427 .encrypt_msdu = NULL,
428 .decrypt_msdu = NULL,
429 .set_key = lib80211_ccmp_set_key,
430 .get_key = lib80211_ccmp_get_key,
431 .print_stats = lib80211_ccmp_print_stats,
432 .extra_mpdu_prefix_len = CCMP_HDR_LEN,
433 .extra_mpdu_postfix_len = CCMP_MIC_LEN,
434 .owner = THIS_MODULE,
435};
436
437static int __init lib80211_crypto_ccmp_init(void)
438{
439 return lib80211_register_crypto_ops(&lib80211_crypt_ccmp);
440}
441
442static void __exit lib80211_crypto_ccmp_exit(void)
443{
444 lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp);
445}
446
447module_init(lib80211_crypto_ccmp_init);
448module_exit(lib80211_crypto_ccmp_exit);