1 // SPDX-License-Identifier: GPL-2.0-only
3 * libipw crypt: host-based CCMP encryption implementation for libipw
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>
21 #include <linux/ieee80211.h>
22 #include <linux/crypto.h>
23 #include <crypto/aead.h>
26 #define AES_BLOCK_LEN 16
27 #define CCMP_HDR_LEN 8
28 #define CCMP_MIC_LEN 8
29 #define CCMP_TK_LEN 16
32 struct libipw_ccmp_data
{
36 u8 tx_pn
[CCMP_PN_LEN
];
37 u8 rx_pn
[CCMP_PN_LEN
];
39 u32 dot11RSNAStatsCCMPFormatErrors
;
40 u32 dot11RSNAStatsCCMPReplays
;
41 u32 dot11RSNAStatsCCMPDecryptErrors
;
45 struct crypto_aead
*tfm
;
47 /* scratch buffers for virt_to_page() (crypto API) */
48 u8 tx_aad
[2 * AES_BLOCK_LEN
];
49 u8 rx_aad
[2 * AES_BLOCK_LEN
];
52 static void *libipw_ccmp_init(int key_idx
)
54 struct libipw_ccmp_data
*priv
;
56 priv
= kzalloc(sizeof(*priv
), GFP_ATOMIC
);
59 priv
->key_idx
= key_idx
;
61 priv
->tfm
= crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC
);
62 if (IS_ERR(priv
->tfm
)) {
72 crypto_free_aead(priv
->tfm
);
79 static void libipw_ccmp_deinit(void *priv
)
81 struct libipw_ccmp_data
*_priv
= priv
;
82 if (_priv
&& _priv
->tfm
)
83 crypto_free_aead(_priv
->tfm
);
87 static int ccmp_init_iv_and_aad(const struct ieee80211_hdr
*hdr
,
88 const u8
*pn
, u8
*iv
, u8
*aad
)
92 int a4_included
, qc_included
;
94 a4_included
= ieee80211_has_a4(hdr
->frame_control
);
95 qc_included
= ieee80211_is_data_qos(hdr
->frame_control
);
101 pos
= (u8
*) & hdr
->addr4
;
108 /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
109 * mode authentication are not allowed to collide, yet both are derived
110 * from the same vector. We only set L := 1 here to indicate that the
111 * data size can be represented in (L+1) bytes. The CCM layer will take
112 * care of storing the data length in the top (L+1) bytes and setting
113 * and clearing the other bits as is required to derive the two IVs.
117 /* Nonce: QC | A2 | PN */
119 memcpy(iv
+ 2, hdr
->addr2
, ETH_ALEN
);
120 memcpy(iv
+ 8, pn
, CCMP_PN_LEN
);
123 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one
125 * SC with bits 4..15 (seq#) masked to zero
130 aad
[0] = pos
[0] & 0x8f;
131 aad
[1] = pos
[1] & 0xc7;
132 memcpy(aad
+ 2, &hdr
->addrs
, 3 * ETH_ALEN
);
133 pos
= (u8
*) & hdr
->seq_ctrl
;
134 aad
[20] = pos
[0] & 0x0f;
135 aad
[21] = 0; /* all bits masked */
136 memset(aad
+ 22, 0, 8);
138 memcpy(aad
+ 22, hdr
->addr4
, ETH_ALEN
);
140 aad
[a4_included
? 28 : 22] = qc
;
141 /* rest of QC masked */
146 static int libipw_ccmp_hdr(struct sk_buff
*skb
, int hdr_len
,
147 u8
*aeskey
, int keylen
, void *priv
)
149 struct libipw_ccmp_data
*key
= priv
;
153 if (skb_headroom(skb
) < CCMP_HDR_LEN
|| skb
->len
< hdr_len
)
156 if (aeskey
!= NULL
&& keylen
>= CCMP_TK_LEN
)
157 memcpy(aeskey
, key
->key
, CCMP_TK_LEN
);
159 pos
= skb_push(skb
, CCMP_HDR_LEN
);
160 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdr_len
);
166 if (key
->tx_pn
[i
] != 0)
171 *pos
++ = key
->tx_pn
[5];
172 *pos
++ = key
->tx_pn
[4];
174 *pos
++ = (key
->key_idx
<< 6) | (1 << 5) /* Ext IV included */ ;
175 *pos
++ = key
->tx_pn
[3];
176 *pos
++ = key
->tx_pn
[2];
177 *pos
++ = key
->tx_pn
[1];
178 *pos
++ = key
->tx_pn
[0];
183 static int libipw_ccmp_encrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
185 struct libipw_ccmp_data
*key
= priv
;
186 struct ieee80211_hdr
*hdr
;
187 struct aead_request
*req
;
188 struct scatterlist sg
[2];
189 u8
*aad
= key
->tx_aad
;
190 u8 iv
[AES_BLOCK_LEN
];
191 int len
, data_len
, aad_len
;
194 if (skb_tailroom(skb
) < CCMP_MIC_LEN
|| skb
->len
< hdr_len
)
197 data_len
= skb
->len
- hdr_len
;
198 len
= libipw_ccmp_hdr(skb
, hdr_len
, NULL
, 0, priv
);
202 req
= aead_request_alloc(key
->tfm
, GFP_ATOMIC
);
206 hdr
= (struct ieee80211_hdr
*)skb
->data
;
207 aad_len
= ccmp_init_iv_and_aad(hdr
, key
->tx_pn
, iv
, aad
);
209 skb_put(skb
, CCMP_MIC_LEN
);
211 sg_init_table(sg
, 2);
212 sg_set_buf(&sg
[0], aad
, aad_len
);
213 sg_set_buf(&sg
[1], skb
->data
+ hdr_len
+ CCMP_HDR_LEN
,
214 data_len
+ CCMP_MIC_LEN
);
216 aead_request_set_callback(req
, 0, NULL
, NULL
);
217 aead_request_set_ad(req
, aad_len
);
218 aead_request_set_crypt(req
, sg
, sg
, data_len
, iv
);
220 ret
= crypto_aead_encrypt(req
);
221 aead_request_free(req
);
227 * deal with seq counter wrapping correctly.
228 * refer to timer_after() for jiffies wrapping handling
230 static inline int ccmp_replay_check(u8
*pn_n
, u8
*pn_o
)
235 iv32_n
= (pn_n
[0] << 24) | (pn_n
[1] << 16) | (pn_n
[2] << 8) | pn_n
[3];
236 iv16_n
= (pn_n
[4] << 8) | pn_n
[5];
238 iv32_o
= (pn_o
[0] << 24) | (pn_o
[1] << 16) | (pn_o
[2] << 8) | pn_o
[3];
239 iv16_o
= (pn_o
[4] << 8) | pn_o
[5];
241 if ((s32
)iv32_n
- (s32
)iv32_o
< 0 ||
242 (iv32_n
== iv32_o
&& iv16_n
<= iv16_o
))
247 static int libipw_ccmp_decrypt(struct sk_buff
*skb
, int hdr_len
, void *priv
)
249 struct libipw_ccmp_data
*key
= priv
;
251 struct ieee80211_hdr
*hdr
;
252 struct aead_request
*req
;
253 struct scatterlist sg
[2];
254 u8
*aad
= key
->rx_aad
;
255 u8 iv
[AES_BLOCK_LEN
];
258 size_t data_len
= skb
->len
- hdr_len
- CCMP_HDR_LEN
;
260 if (skb
->len
< hdr_len
+ CCMP_HDR_LEN
+ CCMP_MIC_LEN
) {
261 key
->dot11RSNAStatsCCMPFormatErrors
++;
265 hdr
= (struct ieee80211_hdr
*)skb
->data
;
266 pos
= skb
->data
+ hdr_len
;
268 if (!(keyidx
& (1 << 5))) {
269 net_dbg_ratelimited("CCMP: received packet without ExtIV flag from %pM\n",
271 key
->dot11RSNAStatsCCMPFormatErrors
++;
275 if (key
->key_idx
!= keyidx
) {
276 net_dbg_ratelimited("CCMP: RX tkey->key_idx=%d frame keyidx=%d\n",
277 key
->key_idx
, keyidx
);
281 net_dbg_ratelimited("CCMP: received packet from %pM with keyid=%d that does not have a configured key\n",
294 if (ccmp_replay_check(pn
, key
->rx_pn
)) {
295 #ifdef CONFIG_LIBIPW_DEBUG
296 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",
298 key
->rx_pn
[0], key
->rx_pn
[1], key
->rx_pn
[2],
299 key
->rx_pn
[3], key
->rx_pn
[4], key
->rx_pn
[5],
300 pn
[0], pn
[1], pn
[2], pn
[3], pn
[4], pn
[5]);
302 key
->dot11RSNAStatsCCMPReplays
++;
306 req
= aead_request_alloc(key
->tfm
, GFP_ATOMIC
);
310 aad_len
= ccmp_init_iv_and_aad(hdr
, pn
, iv
, aad
);
312 sg_init_table(sg
, 2);
313 sg_set_buf(&sg
[0], aad
, aad_len
);
314 sg_set_buf(&sg
[1], pos
, data_len
);
316 aead_request_set_callback(req
, 0, NULL
, NULL
);
317 aead_request_set_ad(req
, aad_len
);
318 aead_request_set_crypt(req
, sg
, sg
, data_len
, iv
);
320 ret
= crypto_aead_decrypt(req
);
321 aead_request_free(req
);
324 net_dbg_ratelimited("CCMP: decrypt failed: STA=%pM (%d)\n",
326 key
->dot11RSNAStatsCCMPDecryptErrors
++;
330 memcpy(key
->rx_pn
, pn
, CCMP_PN_LEN
);
332 /* Remove hdr and MIC */
333 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdr_len
);
334 skb_pull(skb
, CCMP_HDR_LEN
);
335 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
340 static int libipw_ccmp_set_key(void *key
, int len
, u8
* seq
, void *priv
)
342 struct libipw_ccmp_data
*data
= priv
;
344 struct crypto_aead
*tfm
= data
->tfm
;
346 keyidx
= data
->key_idx
;
347 memset(data
, 0, sizeof(*data
));
348 data
->key_idx
= keyidx
;
350 if (len
== CCMP_TK_LEN
) {
351 memcpy(data
->key
, key
, CCMP_TK_LEN
);
354 data
->rx_pn
[0] = seq
[5];
355 data
->rx_pn
[1] = seq
[4];
356 data
->rx_pn
[2] = seq
[3];
357 data
->rx_pn
[3] = seq
[2];
358 data
->rx_pn
[4] = seq
[1];
359 data
->rx_pn
[5] = seq
[0];
361 if (crypto_aead_setauthsize(data
->tfm
, CCMP_MIC_LEN
) ||
362 crypto_aead_setkey(data
->tfm
, data
->key
, CCMP_TK_LEN
))
372 static int libipw_ccmp_get_key(void *key
, int len
, u8
* seq
, void *priv
)
374 struct libipw_ccmp_data
*data
= priv
;
376 if (len
< CCMP_TK_LEN
)
381 memcpy(key
, data
->key
, CCMP_TK_LEN
);
384 seq
[0] = data
->tx_pn
[5];
385 seq
[1] = data
->tx_pn
[4];
386 seq
[2] = data
->tx_pn
[3];
387 seq
[3] = data
->tx_pn
[2];
388 seq
[4] = data
->tx_pn
[1];
389 seq
[5] = data
->tx_pn
[0];
395 static void libipw_ccmp_print_stats(struct seq_file
*m
, void *priv
)
397 struct libipw_ccmp_data
*ccmp
= priv
;
400 "key[%d] alg=CCMP key_set=%d "
401 "tx_pn=%02x%02x%02x%02x%02x%02x "
402 "rx_pn=%02x%02x%02x%02x%02x%02x "
403 "format_errors=%d replays=%d decrypt_errors=%d\n",
404 ccmp
->key_idx
, ccmp
->key_set
,
405 ccmp
->tx_pn
[0], ccmp
->tx_pn
[1], ccmp
->tx_pn
[2],
406 ccmp
->tx_pn
[3], ccmp
->tx_pn
[4], ccmp
->tx_pn
[5],
407 ccmp
->rx_pn
[0], ccmp
->rx_pn
[1], ccmp
->rx_pn
[2],
408 ccmp
->rx_pn
[3], ccmp
->rx_pn
[4], ccmp
->rx_pn
[5],
409 ccmp
->dot11RSNAStatsCCMPFormatErrors
,
410 ccmp
->dot11RSNAStatsCCMPReplays
,
411 ccmp
->dot11RSNAStatsCCMPDecryptErrors
);
414 static const struct libipw_crypto_ops libipw_crypt_ccmp
= {
416 .init
= libipw_ccmp_init
,
417 .deinit
= libipw_ccmp_deinit
,
418 .encrypt_mpdu
= libipw_ccmp_encrypt
,
419 .decrypt_mpdu
= libipw_ccmp_decrypt
,
420 .encrypt_msdu
= NULL
,
421 .decrypt_msdu
= NULL
,
422 .set_key
= libipw_ccmp_set_key
,
423 .get_key
= libipw_ccmp_get_key
,
424 .print_stats
= libipw_ccmp_print_stats
,
425 .extra_mpdu_prefix_len
= CCMP_HDR_LEN
,
426 .extra_mpdu_postfix_len
= CCMP_MIC_LEN
,
427 .owner
= THIS_MODULE
,
430 int __init
libipw_crypto_ccmp_init(void)
432 return libipw_register_crypto_ops(&libipw_crypt_ccmp
);
435 void libipw_crypto_ccmp_exit(void)
437 libipw_unregister_crypto_ops(&libipw_crypt_ccmp
);