1 // SPDX-License-Identifier: GPL-2.0-only
3 * lib80211 crypt: host-based CCMP encryption implementation for lib80211
5 * Copyright (c) 2003-2004, Jouni Malinen <j@w1.fi>
6 * Copyright (c) 2008, John W. Linville <linville@tuxdriver.com>
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>
22 #include <linux/ieee80211.h>
24 #include <linux/crypto.h>
26 #include <net/lib80211.h>
28 MODULE_AUTHOR("Jouni Malinen");
29 MODULE_DESCRIPTION("Host AP crypt: CCMP");
30 MODULE_LICENSE("GPL");
32 #define AES_BLOCK_LEN 16
33 #define CCMP_HDR_LEN 8
34 #define CCMP_MIC_LEN 8
35 #define CCMP_TK_LEN 16
38 struct lib80211_ccmp_data
{
42 u8 tx_pn
[CCMP_PN_LEN
];
43 u8 rx_pn
[CCMP_PN_LEN
];
45 u32 dot11RSNAStatsCCMPFormatErrors
;
46 u32 dot11RSNAStatsCCMPReplays
;
47 u32 dot11RSNAStatsCCMPDecryptErrors
;
51 struct crypto_cipher
*tfm
;
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
];
59 static inline void lib80211_ccmp_aes_encrypt(struct crypto_cipher
*tfm
,
60 const u8 pt
[16], u8 ct
[16])
62 crypto_cipher_encrypt_one(tfm
, ct
, pt
);
65 static void *lib80211_ccmp_init(int key_idx
)
67 struct lib80211_ccmp_data
*priv
;
69 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
72 priv
->key_idx
= key_idx
;
74 priv
->tfm
= crypto_alloc_cipher("aes", 0, 0);
75 if (IS_ERR(priv
->tfm
)) {
85 crypto_free_cipher(priv
->tfm
);
92 static void lib80211_ccmp_deinit(void *priv
)
94 struct lib80211_ccmp_data
*_priv
= priv
;
95 if (_priv
&& _priv
->tfm
)
96 crypto_free_cipher(_priv
->tfm
);
100 static inline void xor_block(u8
* b
, u8
* a
, size_t len
)
103 for (i
= 0; i
< len
; i
++)
107 static void ccmp_init_blocks(struct crypto_cipher
*tfm
,
108 struct ieee80211_hdr
*hdr
,
109 u8
* pn
, size_t dlen
, u8
* b0
, u8
* auth
, u8
* s0
)
113 int a4_included
, qc_included
;
114 u8 aad
[2 * AES_BLOCK_LEN
];
116 a4_included
= ieee80211_has_a4(hdr
->frame_control
);
117 qc_included
= ieee80211_is_data_qos(hdr
->frame_control
);
123 pos
= (u8
*) & hdr
->addr4
;
130 /* CCM Initial Block:
131 * Flag (Include authentication header, M=3 (8-octet MIC),
132 * L=1 (2-octet Dlen))
133 * Nonce: 0x00 | A2 | PN
137 memcpy(b0
+ 2, hdr
->addr2
, ETH_ALEN
);
138 memcpy(b0
+ 8, pn
, CCMP_PN_LEN
);
139 b0
[14] = (dlen
>> 8) & 0xff;
140 b0
[15] = dlen
& 0xff;
143 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
145 * SC with bits 4..15 (seq#) masked to zero
150 aad
[0] = 0; /* aad_len >> 8 */
151 aad
[1] = aad_len
& 0xff;
152 aad
[2] = pos
[0] & 0x8f;
153 aad
[3] = pos
[1] & 0xc7;
154 memcpy(aad
+ 4, hdr
->addr1
, 3 * ETH_ALEN
);
155 pos
= (u8
*) & hdr
->seq_ctrl
;
156 aad
[22] = pos
[0] & 0x0f;
157 aad
[23] = 0; /* all bits masked */
158 memset(aad
+ 24, 0, 8);
160 memcpy(aad
+ 24, hdr
->addr4
, ETH_ALEN
);
162 aad
[a4_included
? 30 : 24] = qc
;
163 /* rest of QC masked */
166 /* Start with the first block and AAD */
167 lib80211_ccmp_aes_encrypt(tfm
, b0
, auth
);
168 xor_block(auth
, aad
, AES_BLOCK_LEN
);
169 lib80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
170 xor_block(auth
, &aad
[AES_BLOCK_LEN
], AES_BLOCK_LEN
);
171 lib80211_ccmp_aes_encrypt(tfm
, auth
, auth
);
174 lib80211_ccmp_aes_encrypt(tfm
, b0
, s0
);
177 static int lib80211_ccmp_hdr(struct sk_buff
*skb
, int hdr_len
,
178 u8
*aeskey
, int keylen
, void *priv
)
180 struct lib80211_ccmp_data
*key
= priv
;
184 if (skb_headroom(skb
) < CCMP_HDR_LEN
|| skb
->len
< hdr_len
)
187 if (aeskey
!= NULL
&& keylen
>= CCMP_TK_LEN
)
188 memcpy(aeskey
, key
->key
, CCMP_TK_LEN
);
190 pos
= skb_push(skb
, CCMP_HDR_LEN
);
191 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
197 if (key
->tx_pn
[i
] != 0)
202 *pos
++ = key
->tx_pn
[5];
203 *pos
++ = key
->tx_pn
[4];
205 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */ ;
206 *pos
++ = key
->tx_pn
[3];
207 *pos
++ = key
->tx_pn
[2];
208 *pos
++ = key
->tx_pn
[1];
209 *pos
++ = key
->tx_pn
[0];
214 static int lib80211_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
216 struct lib80211_ccmp_data
*key
= priv
;
217 int data_len
, i
, blocks
, last
, len
;
219 struct ieee80211_hdr
*hdr
;
225 if (skb_tailroom(skb
) < CCMP_MIC_LEN
|| skb
->len
< hdr_len
)
228 data_len
= skb
->len
- hdr_len
;
229 len
= lib80211_ccmp_hdr(skb
, hdr_len
, NULL
, 0, priv
);
233 pos
= skb
->data
+ hdr_len
+ CCMP_HDR_LEN
;
234 hdr
= (struct ieee80211_hdr
*)skb
->data
;
235 ccmp_init_blocks(key
->tfm
, hdr
, key
->tx_pn
, data_len
, b0
, b
, s0
);
237 blocks
= DIV_ROUND_UP(data_len
, AES_BLOCK_LEN
);
238 last
= data_len
% AES_BLOCK_LEN
;
240 for (i
= 1; i
<= blocks
; i
++) {
241 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
243 xor_block(b
, pos
, len
);
244 lib80211_ccmp_aes_encrypt(key
->tfm
, b
, b
);
245 /* Encryption, with counter */
246 b0
[14] = (i
>> 8) & 0xff;
248 lib80211_ccmp_aes_encrypt(key
->tfm
, b0
, e
);
249 xor_block(pos
, e
, len
);
253 mic
= skb_put(skb
, CCMP_MIC_LEN
);
254 for (i
= 0; i
< CCMP_MIC_LEN
; i
++)
255 mic
[i
] = b
[i
] ^ s0
[i
];
261 * deal with seq counter wrapping correctly.
262 * refer to timer_after() for jiffies wrapping handling
264 static inline int ccmp_replay_check(u8
*pn_n
, u8
*pn_o
)
269 iv32_n
= (pn_n
[0] << 24) | (pn_n
[1] << 16) | (pn_n
[2] << 8) | pn_n
[3];
270 iv16_n
= (pn_n
[4] << 8) | pn_n
[5];
272 iv32_o
= (pn_o
[0] << 24) | (pn_o
[1] << 16) | (pn_o
[2] << 8) | pn_o
[3];
273 iv16_o
= (pn_o
[4] << 8) | pn_o
[5];
275 if ((s32
)iv32_n
- (s32
)iv32_o
< 0 ||
276 (iv32_n
== iv32_o
&& iv16_n
<= iv16_o
))
281 static int lib80211_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
283 struct lib80211_ccmp_data
*key
= priv
;
285 struct ieee80211_hdr
*hdr
;
290 int i
, blocks
, last
, len
;
291 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
292 u8
*mic
= skb
->data
+ skb
->len
- CCMP_MIC_LEN
;
294 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
295 key
->dot11RSNAStatsCCMPFormatErrors
++;
299 hdr
= (struct ieee80211_hdr
*)skb
->data
;
300 pos
= skb
->data
+ hdr_len
;
302 if (!(keyidx
& (1 << 5))) {
303 net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
305 key
->dot11RSNAStatsCCMPFormatErrors
++;
309 if (key
->key_idx
!= keyidx
) {
310 net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
311 key
->key_idx
, keyidx
);
315 net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
328 if (ccmp_replay_check(pn
, key
->rx_pn
)) {
329 #ifdef CONFIG_LIB80211_DEBUG
330 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",
332 key
->rx_pn
[0], key
->rx_pn
[1], key
->rx_pn
[2],
333 key
->rx_pn
[3], key
->rx_pn
[4], key
->rx_pn
[5],
334 pn
[0], pn
[1], pn
[2], pn
[3], pn
[4], pn
[5]);
336 key
->dot11RSNAStatsCCMPReplays
++;
340 ccmp_init_blocks(key
->tfm
, hdr
, pn
, data_len
, b0
, a
, b
);
341 xor_block(mic
, b
, CCMP_MIC_LEN
);
343 blocks
= DIV_ROUND_UP(data_len
, AES_BLOCK_LEN
);
344 last
= data_len
% AES_BLOCK_LEN
;
346 for (i
= 1; i
<= blocks
; i
++) {
347 len
= (i
== blocks
&& last
) ? last
: AES_BLOCK_LEN
;
348 /* Decrypt, with counter */
349 b0
[14] = (i
>> 8) & 0xff;
351 lib80211_ccmp_aes_encrypt(key
->tfm
, b0
, b
);
352 xor_block(pos
, b
, len
);
354 xor_block(a
, pos
, len
);
355 lib80211_ccmp_aes_encrypt(key
->tfm
, a
, a
);
359 if (memcmp(mic
, a
, CCMP_MIC_LEN
) != 0) {
360 net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM\n",
362 key
->dot11RSNAStatsCCMPDecryptErrors
++;
366 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
368 /* Remove hdr and MIC */
369 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
370 skb_pull(skb
, CCMP_HDR_LEN
);
371 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
376 static int lib80211_ccmp_set_key(void *key
, int len
, u8
* seq
, void *priv
)
378 struct lib80211_ccmp_data
*data
= priv
;
380 struct crypto_cipher
*tfm
= data
->tfm
;
382 keyidx
= data
->key_idx
;
383 memset(data
, 0, sizeof(*data
));
384 data
->key_idx
= keyidx
;
386 if (len
== CCMP_TK_LEN
) {
387 memcpy(data
->key
, key
, CCMP_TK_LEN
);
390 data
->rx_pn
[0] = seq
[5];
391 data
->rx_pn
[1] = seq
[4];
392 data
->rx_pn
[2] = seq
[3];
393 data
->rx_pn
[3] = seq
[2];
394 data
->rx_pn
[4] = seq
[1];
395 data
->rx_pn
[5] = seq
[0];
397 crypto_cipher_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
);
406 static int lib80211_ccmp_get_key(void *key
, int len
, u8
* seq
, void *priv
)
408 struct lib80211_ccmp_data
*data
= priv
;
410 if (len
< CCMP_TK_LEN
)
415 memcpy(key
, data
->key
, CCMP_TK_LEN
);
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];
429 static void lib80211_ccmp_print_stats(struct seq_file
*m
, void *priv
)
431 struct lib80211_ccmp_data
*ccmp
= priv
;
434 "key[%d] alg=CCMP key_set=%d "
435 "tx_pn=%02x%02x%02x%02x%02x%02x "
436 "rx_pn=%02x%02x%02x%02x%02x%02x "
437 "format_errors=%d replays=%d decrypt_errors=%d\n",
438 ccmp
->key_idx
, ccmp
->key_set
,
439 ccmp
->tx_pn
[0], ccmp
->tx_pn
[1], ccmp
->tx_pn
[2],
440 ccmp
->tx_pn
[3], ccmp
->tx_pn
[4], ccmp
->tx_pn
[5],
441 ccmp
->rx_pn
[0], ccmp
->rx_pn
[1], ccmp
->rx_pn
[2],
442 ccmp
->rx_pn
[3], ccmp
->rx_pn
[4], ccmp
->rx_pn
[5],
443 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
444 ccmp
->dot11RSNAStatsCCMPReplays
,
445 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
448 static struct lib80211_crypto_ops lib80211_crypt_ccmp
= {
450 .init
= lib80211_ccmp_init
,
451 .deinit
= lib80211_ccmp_deinit
,
452 .encrypt_mpdu
= lib80211_ccmp_encrypt
,
453 .decrypt_mpdu
= lib80211_ccmp_decrypt
,
454 .encrypt_msdu
= NULL
,
455 .decrypt_msdu
= NULL
,
456 .set_key
= lib80211_ccmp_set_key
,
457 .get_key
= lib80211_ccmp_get_key
,
458 .print_stats
= lib80211_ccmp_print_stats
,
459 .extra_mpdu_prefix_len
= CCMP_HDR_LEN
,
460 .extra_mpdu_postfix_len
= CCMP_MIC_LEN
,
461 .owner
= THIS_MODULE
,
464 static int __init
lib80211_crypto_ccmp_init(void)
466 return lib80211_register_crypto_ops(&lib80211_crypt_ccmp
);
469 static void __exit
lib80211_crypto_ccmp_exit(void)
471 lib80211_unregister_crypto_ops(&lib80211_crypt_ccmp
);
474 module_init(lib80211_crypto_ccmp_init
);
475 module_exit(lib80211_crypto_ccmp_exit
);