From: Chien Wong <m@xv97.com>
To: johannes@sipsolutions.net
Cc: linux-wireless@vger.kernel.org
Subject: [PATCH v3 4/5] wifi: mac80211: refactor CMAC crypt functions
Date: Tue, 11 Nov 2025 22:57:58 +0800 [thread overview]
Message-ID: <20251111145759.111691-5-m@xv97.com> (raw)
In-Reply-To: <20251111145759.111691-1-m@xv97.com>
ieee80211_aes_cmac() and ieee80211_aes_cmac_256() are almost the same.
Merge them. This removes duplication.
It should be noted that the refactored ieee80211_aes_cmac() permits
8 bytes output for CMAC-256 and 16 bytes output for CMAC-128. In such
cases, it would generate result correctly as expected.
All references to the refactored functions in the tree are adapted.
Signed-off-by: Chien Wong <m@xv97.com>
---
net/mac80211/aes_cmac.c | 53 ++++++++++-------------------------------
net/mac80211/aes_cmac.h | 4 +---
net/mac80211/wpa.c | 16 ++++++++-----
3 files changed, 24 insertions(+), 49 deletions(-)
diff --git a/net/mac80211/aes_cmac.c b/net/mac80211/aes_cmac.c
index 01fb8b6c5dfb..588e3c9879b3 100644
--- a/net/mac80211/aes_cmac.c
+++ b/net/mac80211/aes_cmac.c
@@ -16,20 +16,21 @@
#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);
- u8 out[AES_BLOCK_SIZE];
const __le16 *fc;
+ if (mic_len != IEEE80211_CMAC_128_MIC_LEN &&
+ mic_len != IEEE80211_CMAC_256_MIC_LEN)
+ return -EINVAL;
+
desc->tfm = tfm;
err = crypto_shash_init(desc);
@@ -44,53 +45,25 @@ int ieee80211_aes_cmac(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);
+ 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);
+ err = crypto_shash_update(desc, data, data_len - mic_len);
if (err)
goto out;
}
- err = crypto_shash_finup(desc, zero, CMAC_TLEN, out);
- if (err)
- goto out;
- memcpy(mic, out, CMAC_TLEN);
-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) {
+ u8 out[AES_BLOCK_SIZE];
- 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_256);
+ err = crypto_shash_finup(desc, zero, mic_len, out);
if (err)
goto out;
+ memcpy(mic, out, mic_len);
} else {
- err = crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
- if (err)
- goto out;
+ err = crypto_shash_finup(desc, zero, mic_len, mic);
}
- err = crypto_shash_finup(desc, zero, CMAC_TLEN_256, 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-11 14:58 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-11 14:57 [PATCH v3 0/5] wifi: mac80211: refactor CMAC implementation Chien Wong
2025-11-11 14:57 ` [PATCH v3 1/5] wifi: mac80211: fix CMAC functions not handling errors Chien Wong
2025-11-12 13:11 ` Johannes Berg
2025-11-13 13:15 ` Chien Wong
2025-11-11 14:57 ` [PATCH v3 2/5] wifi: mac80211: add generic MMIE struct defines Chien Wong
2025-11-11 14:57 ` [PATCH v3 3/5] wifi: mac80211: utilize the newly defined CMAC constants Chien Wong
2025-11-12 13:12 ` Johannes Berg
2025-11-13 13:16 ` Chien Wong
2025-11-11 14:57 ` Chien Wong [this message]
2025-11-12 13:19 ` [PATCH v3 4/5] wifi: mac80211: refactor CMAC crypt functions Johannes Berg
2025-11-13 13:24 ` Chien Wong
2025-11-11 14:57 ` [PATCH v3 5/5] wifi: mac80211: refactor CMAC packet handlers 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=20251111145759.111691-5-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