From: Chien Wong <m@xv97.com>
To: johannes@sipsolutions.net
Cc: linux-wireless@vger.kernel.org
Subject: [PATCH 5/6] wifi: mac80211: refactor CMAC crypt functions
Date: Sun, 9 Nov 2025 22:04:49 +0800 [thread overview]
Message-ID: <20251109140450.118106-6-m@xv97.com> (raw)
In-Reply-To: <20251109140450.118106-1-m@xv97.com>
ieee80211_aes_cmac() and ieee80211_aes_cmac_256() are almost the same.
Merge them. This removes duplication.
All references to the refactored functions in the tree are adapted.
Complie test only.
Signed-off-by: Chien Wong <m@xv97.com>
---
net/mac80211/aes_cmac.c | 49 +++++++----------------------------------
net/mac80211/aes_cmac.h | 4 +---
net/mac80211/wpa.c | 16 +++++++++-----
3 files changed, 19 insertions(+), 50 deletions(-)
diff --git a/net/mac80211/aes_cmac.c b/net/mac80211/aes_cmac.c
index 2c2d15c50b65..a44dda08aaa4 100644
--- a/net/mac80211/aes_cmac.c
+++ b/net/mac80211/aes_cmac.c
@@ -16,52 +16,20 @@
#include "key.h"
#include "aes_cmac.h"
-#define CMAC_TLEN IEEE80211_CMAC_128_MIC_LEN
-#define CMAC_TLEN_256 IEEE80211_CMAC_256_MIC_LEN
#define AAD_LEN 20
-static const u8 zero[CMAC_TLEN_256];
+static const u8 zero[IEEE80211_CMAC_256_MIC_LEN];
int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
- const u8 *data, size_t data_len, u8 *mic)
+ const u8 *data, size_t data_len, u8 *mic, unsigned int mic_len)
{
int err;
SHASH_DESC_ON_STACK(desc, tfm);
const __le16 *fc;
- desc->tfm = tfm;
-
- err = crypto_shash_init(desc);
- if (err)
- goto out;
- err = crypto_shash_update(desc, aad, AAD_LEN);
- if (err)
- goto out;
- fc = (const __le16 *)aad;
- if (ieee80211_is_beacon(*fc)) {
- /* mask Timestamp field to zero */
- err = crypto_shash_update(desc, zero, 8);
- if (err)
- goto out;
- err = crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN);
- if (err)
- goto out;
- } else {
- err = crypto_shash_update(desc, data, data_len - CMAC_TLEN);
- if (err)
- goto out;
- }
- err = crypto_shash_finup(desc, zero, CMAC_TLEN, mic);
-out:
- return err;
-}
-
-int ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
- const u8 *data, size_t data_len, u8 *mic)
-{
- int err;
- SHASH_DESC_ON_STACK(desc, tfm);
- const __le16 *fc;
+ if (mic_len != IEEE80211_CMAC_128_MIC_LEN &&
+ mic_len != IEEE80211_CMAC_256_MIC_LEN)
+ return -EINVAL;
desc->tfm = tfm;
@@ -77,16 +45,15 @@ int ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
err = crypto_shash_update(desc, zero, 8);
if (err)
goto out;
- err = crypto_shash_update(desc, data + 8,
- data_len - 8 - CMAC_TLEN_256);
+ err = crypto_shash_update(desc, data + 8, data_len - 8 - mic_len);
if (err)
goto out;
} else {
- err = crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
+ err = crypto_shash_update(desc, data, data_len - mic_len);
if (err)
goto out;
}
- err = crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
+ err = crypto_shash_finup(desc, zero, mic_len, mic);
out:
return err;
}
diff --git a/net/mac80211/aes_cmac.h b/net/mac80211/aes_cmac.h
index f74150542142..631fc3033576 100644
--- a/net/mac80211/aes_cmac.h
+++ b/net/mac80211/aes_cmac.h
@@ -12,9 +12,7 @@
struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
size_t key_len);
int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
- const u8 *data, size_t data_len, u8 *mic);
-int ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
- const u8 *data, size_t data_len, u8 *mic);
+ const u8 *data, size_t data_len, u8 *mic, unsigned int mic_len);
void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
#endif /* AES_CMAC_H */
diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c
index bb0fa505cdca..2c1ee4b8e205 100644
--- a/net/mac80211/wpa.c
+++ b/net/mac80211/wpa.c
@@ -870,7 +870,8 @@ ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
* MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
*/
if (ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
- skb->data + 24, skb->len - 24, mmie->mic))
+ skb->data + 24, skb->len - 24, mmie->mic,
+ IEEE80211_CMAC_128_MIC_LEN))
return TX_DROP;
return TX_CONTINUE;
@@ -917,8 +918,9 @@ ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx)
/* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128)
*/
- if (ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
- skb->data + 24, skb->len - 24, mmie->mic))
+ if (ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
+ skb->data + 24, skb->len - 24, mmie->mic,
+ IEEE80211_CMAC_256_MIC_LEN))
return TX_DROP;
return TX_CONTINUE;
@@ -959,7 +961,8 @@ ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
/* hardware didn't decrypt/verify MIC */
bip_aad(skb, aad);
if (ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
- skb->data + 24, skb->len - 24, mic))
+ skb->data + 24, skb->len - 24, mic,
+ IEEE80211_CMAC_128_MIC_LEN))
return RX_DROP_U_DECRYPT_FAIL;
if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
key->u.aes_cmac.icverrors++;
@@ -1009,8 +1012,9 @@ ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx)
if (!(status->flag & RX_FLAG_DECRYPTED)) {
/* hardware didn't decrypt/verify MIC */
bip_aad(skb, aad);
- if (ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
- skb->data + 24, skb->len - 24, mic))
+ if (ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
+ skb->data + 24, skb->len - 24, mic,
+ IEEE80211_CMAC_256_MIC_LEN))
return RX_DROP_U_DECRYPT_FAIL;
if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
key->u.aes_cmac.icverrors++;
--
2.51.2
next prev parent reply other threads:[~2025-11-09 14:05 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-09 14:04 [PATCH 0/6] wifi: mac80211: refactor CMAC implementation Chien Wong
2025-11-09 14:04 ` [PATCH 1/6] wifi: mac80211: remove an unnecessary copy Chien Wong
2025-11-10 7:34 ` Johannes Berg
2025-11-10 13:33 ` Chien Wong
2025-11-10 13:35 ` Johannes Berg
2025-11-10 13:42 ` Chien Wong
2025-11-09 14:04 ` [PATCH 2/6] wifi: mac80211: fix CMAC functions not handling errors Chien Wong
2025-11-09 14:04 ` [PATCH 3/6] wifi: mac80211: add generic MMIE struct defines Chien Wong
2025-11-09 14:04 ` [PATCH 4/6] wifi: mac80211: utilize the newly defined CMAC constants Chien Wong
2025-11-09 14:04 ` Chien Wong [this message]
2025-11-09 14:04 ` [PATCH 6/6] wifi: mac80211: refactor CMAC packet handlers Chien Wong
2025-11-10 7:43 ` Johannes Berg
2025-11-10 14:03 ` Chien Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251109140450.118106-6-m@xv97.com \
--to=m@xv97.com \
--cc=johannes@sipsolutions.net \
--cc=linux-wireless@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox