Skip to contentSkip to conversation

wifi: mac80211: refactor CMAC implementation

PATCH Superseded Chien Wong 2 participants 12 messages Nov 11, 2025 – Nov 13, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Chien Wong

author
#0 lore raw

v3 cover wifi: mac80211: refactor CMAC implementation

This series refactors the CMAC implementation in mac80211 to:
1. Add proper error handling
2. Remove duplication

Every patch in the patchset has been tested with mac80211_hwsim and the result looks good.
---
Changes in v3:
- Drop patch 1/6("wifi: mac80211: remove an unnecessary copy").
- Rework patch 2/6("wifi: mac80211: fix CMAC functions not handling errors") and patch 5/6("wifi: mac80211: refactor CMAC crypt functions") accordingly.
---
Changes in v2:
- Remove words in commit messages stating that only compile is tested.
- Remove Fixes line in patch 1/6.
---
Chien Wong (5):
wifi: mac80211: fix CMAC functions not handling errors
wifi: mac80211: add generic MMIE struct defines
wifi: mac80211: utilize the newly defined CMAC constants
wifi: mac80211: refactor CMAC crypt functions
wifi: mac80211: refactor CMAC packet handlers

 include/linux/ieee80211.h |  14 +++-
 net/mac80211/aes_cmac.c   |  64 ++++++++---------
 net/mac80211/aes_cmac.h   |   6 +-
 net/mac80211/aes_gmac.h   |   2 +-
 net/mac80211/rx.c         |   6 +-
 net/mac80211/tx.c         |   6 +-
 net/mac80211/wpa.c        | 144 +++++++-------------------------------
 net/mac80211/wpa.h        |  10 ++-
 8 files changed, 87 insertions(+), 165 deletions(-)
Signature · 1 line
2.51.2
author
#1 lore raw

PATCH v3 1/5 wifi: mac80211: fix CMAC functions not handling errors

The called hash functions could fail thus we should check return values. All references to the changed functions in the tree are adapted.

Fixes 26717828b75d ("mac80211: aes-cmac: switch to shash CMAC driver")
---
 net/mac80211/aes_cmac.c | 63 +++++++++++++++++++++++++++++------------
 net/mac80211/aes_cmac.h |  8 +++---
 net/mac80211/wpa.c      | 20 +++++++------
 3 files changed, 61 insertions(+), 30 deletions(-)
net/mac80211/aes_cmac.c +45 -18
index 48c04f89de20..adce68ea0981 100644
@@ -22,50 +22,77 @@
 
 static const u8 zero[CMAC_TLEN_256];
 
-void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
-			const u8 *data, size_t data_len, u8 *mic)
+int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
+		       const u8 *data, size_t data_len, u8 *mic)
 {
+	int err;
 	SHASH_DESC_ON_STACK(desc, tfm);
 	u8 out[AES_BLOCK_SIZE];
 	const __le16 *fc;
 
 	desc->tfm = tfm;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
+	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 */
-		crypto_shash_update(desc, zero, 8);
-		crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN);
+		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 {
-		crypto_shash_update(desc, data, data_len - CMAC_TLEN);
+		err = crypto_shash_update(desc, data, data_len - CMAC_TLEN);
+		if (err)
+			goto out;
 	}
-	crypto_shash_finup(desc, zero, CMAC_TLEN, out);
-
+	err = crypto_shash_finup(desc, zero, CMAC_TLEN, out);
+	if (err)
+		goto out;
 	memcpy(mic, out, CMAC_TLEN);
+out:
+	return err;
 }
 
-void ieee80211_aes_cmac_256(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)
 {
+	int err;
 	SHASH_DESC_ON_STACK(desc, tfm);
 	const __le16 *fc;
 
 	desc->tfm = tfm;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
+	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 */
-		crypto_shash_update(desc, zero, 8);
-		crypto_shash_update(desc, data + 8,
-				    data_len - 8 - CMAC_TLEN_256);
+		err = crypto_shash_update(desc, zero, 8);
+		if (err)
+			goto out;
+		err = crypto_shash_update(desc, data + 8,
+					  data_len - 8 - CMAC_TLEN_256);
+		if (err)
+			goto out;
 	} else {
-		crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
+		err = crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
+		if (err)
+			goto out;
 	}
-	crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
+	err = crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
+out:
+	return err;
 }
 
 struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
net/mac80211/aes_cmac.h +4 -4
index 76817446fb83..f74150542142 100644
@@ -11,10 +11,10 @@
 
 struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
 						  size_t key_len);
-void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
-			const u8 *data, size_t data_len, u8 *mic);
-void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
-			    const u8 *data, size_t data_len, u8 *mic);
+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);
 void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
 
 #endif /* AES_CMAC_H */
net/mac80211/wpa.c +12 -8
index 40d5d9e48479..bb0fa505cdca 100644
@@ -869,8 +869,9 @@ ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
 	/*
 	 * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
 	 */
-	ieee80211_aes_cmac(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))
+		return TX_DROP;
 
 	return TX_CONTINUE;
 }
@@ -916,8 +917,9 @@ ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx)
 
 	/* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128)
 	 */
-	ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
-			       skb->data + 24, skb->len - 24, mmie->mic);
+	if (ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
+				   skb->data + 24, skb->len - 24, mmie->mic))
+		return TX_DROP;
 
 	return TX_CONTINUE;
 }
@@ -956,8 +958,9 @@ ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
 	if (!(status->flag & RX_FLAG_DECRYPTED)) {
 		/* hardware didn't decrypt/verify MIC */
 		bip_aad(skb, aad);
-		ieee80211_aes_cmac(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))
+			return RX_DROP_U_DECRYPT_FAIL;
 		if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
 			key->u.aes_cmac.icverrors++;
 			return RX_DROP_U_MIC_FAIL;
@@ -1006,8 +1009,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);
-		ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
-				       skb->data + 24, skb->len - 24, mic);
+		if (ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
+					   skb->data + 24, skb->len - 24, mic))
+			return RX_DROP_U_DECRYPT_FAIL;
 		if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
 			key->u.aes_cmac.icverrors++;
 			return RX_DROP_U_MIC_FAIL;
Signature · 1 line
2.51.2
maintainer
#2 lore raw

Re: [PATCH v3 1/5] wifi: mac80211: fix CMAC functions not handling errors

Quoted 11 lines · Chien Wong
net/mac80211/aes_cmac.c +3 -2
-void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
-			const u8 *data, size_t data_len, u8 *mic)
+int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
+		       const u8 *data, size_t data_len, u8 *mic)
 {
+	int err;
 	SHASH_DESC_ON_STACK(desc, tfm);
 	u8 out[AES_BLOCK_SIZE];
 	const __le16 *fc;
 

net/mac80211/aes_cmac.c line 40

Quoted 10 lines
net/mac80211/aes_cmac.c +6 -2
 	desc->tfm = tfm;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
+	err = crypto_shash_init(desc);
+	if (err)
+		goto out;
+	err = crypto_shash_update(desc, aad, AAD_LEN);
+	if (err)
+		goto out;

Not sure all the 'goto', without

Review

Quoted 2 lines
(quoted diff) +2 -0
+out:
+	return err;

anything other than a return is really better than just returning early? It's not like this code will change soon again and need more error handling ;-)

johannes

author
#3 lore raw

Re: [PATCH v3 1/5] wifi: mac80211: fix CMAC functions not handling errors

Quoted 26 lines · Johannes Berg
Quoted 11 lines · Chien Wong
net/mac80211/aes_cmac.c +3 -2
-void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
-			const u8 *data, size_t data_len, u8 *mic)
+int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
+		       const u8 *data, size_t data_len, u8 *mic)
  {
+	int err;
  	SHASH_DESC_ON_STACK(desc, tfm);
  	u8 out[AES_BLOCK_SIZE];
  	const __le16 *fc;
 

net/mac80211/aes_cmac.c line 40

Quoted 10 lines
net/mac80211/aes_cmac.c +6 -2
  	desc->tfm = tfm;
 
-	crypto_shash_init(desc);
-	crypto_shash_update(desc, aad, AAD_LEN);
+	err = crypto_shash_init(desc);
+	if (err)
+		goto out;
+	err = crypto_shash_update(desc, aad, AAD_LEN);
+	if (err)
+		goto out;

Not sure all the 'goto', without

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+out:
+	return err;

anything other than a return is really better than just returning early? It's not like this code will change soon again and need more error handling ;-)

johannes

OK. They will be change into simply returning.

author
#4 lore raw

PATCH v3 2/5 wifi: mac80211: add generic MMIE struct defines

In reply to Chien Wong

The added struct is needed when writing generic handler for both CMAC-128 and CMAC-256.

---
 include/linux/ieee80211.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/linux/ieee80211.h +13 -1
index ddff9102f633..9392fc11d4fb 100644
@@ -1633,7 +1633,7 @@ struct ieee80211_mgmt {
 #define IEEE80211_MIN_ACTION_SIZE offsetof(struct ieee80211_mgmt, u.action.u)
 
 
-/* Management MIC information element (IEEE 802.11w) */
+/* Management MIC information element (IEEE 802.11w) for CMAC */
 struct ieee80211_mmie {
 	u8 element_id;
 	u8 length;
@@ -1651,6 +1651,15 @@ struct ieee80211_mmie_16 {
 	u8 mic[16];
 } __packed;
 
+/* Management MIC information element (IEEE 802.11w) for all variants */
+struct ieee80211_mmie_var {
+	u8 element_id;
+	u8 length;
+	__le16 key_id;
+	u8 sequence_number[6];
+	u8 mic[]; /* 8 or 16 bytes */
+} __packed;
+
 struct ieee80211_vendor_ie {
 	u8 element_id;
 	u8 len;
@@ -4064,6 +4073,9 @@ enum ieee80211_radio_measurement_actioncode {
 #define IEEE80211_GCMP_HDR_LEN		8
 #define IEEE80211_GCMP_MIC_LEN		16
 #define IEEE80211_GCMP_PN_LEN		6
+#define IEEE80211_CMAC_128_MIC_LEN	8
+#define IEEE80211_CMAC_256_MIC_LEN	16
+#define IEEE80211_GMAC_MIC_LEN		16
 
 #define FILS_NONCE_LEN			16
 #define FILS_MAX_KEK_LEN		64
Signature · 1 line
2.51.2
author
#5 lore raw

PATCH v3 3/5 wifi: mac80211: utilize the newly defined CMAC constants

In reply to Chien Wong

Make use of the added constants to reduce duplication.

---
 net/mac80211/aes_cmac.c | 4 ++--
 net/mac80211/aes_gmac.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
net/mac80211/aes_cmac.c +2 -2
index adce68ea0981..01fb8b6c5dfb 100644
@@ -16,8 +16,8 @@
 #include "key.h"
 #include "aes_cmac.h"
 
-#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
-#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
+#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];
net/mac80211/aes_gmac.h +1 -1
index c739356bae2a..09378e52c7a6 100644
@@ -9,7 +9,7 @@
 #include <linux/crypto.h>
 
 #define GMAC_AAD_LEN	20
-#define GMAC_MIC_LEN	16
+#define GMAC_MIC_LEN	IEEE80211_GMAC_MIC_LEN
 #define GMAC_NONCE_LEN	12
 
 struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
Signature · 1 line
2.51.2
maintainer
#6 lore raw

Re: [PATCH v3 3/5] wifi: mac80211: utilize the newly defined CMAC constants

Quoted 28 lines · Chien Wong

Make use of the added constants to reduce duplication.

---
 net/mac80211/aes_cmac.c | 4 ++--
 net/mac80211/aes_gmac.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
net/mac80211/aes_cmac.c +2 -2
index adce68ea0981..01fb8b6c5dfb 100644
@@ -16,8 +16,8 @@
 #include "key.h"
 #include "aes_cmac.h"
 
-#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
-#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
+#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];
net/mac80211/aes_gmac.h +0 -0
index c739356bae2a..09378e52c7a6 100644

net/mac80211/aes_gmac.h line 12

Quoted 5 lines
net/mac80211/aes_gmac.h +1 -1
@@ -9,7 +9,7 @@
 #include <linux/crypto.h>
 
 #define GMAC_AAD_LEN	20
-#define GMAC_MIC_LEN	16
+#define GMAC_MIC_LEN	IEEE80211_GMAC_MIC_LEN

Might really be better to just switch the code to the new ones?

johannes

author
#7 lore raw

Re: [PATCH v3 3/5] wifi: mac80211: utilize the newly defined CMAC constants

Quoted 31 lines · Johannes Berg
Quoted 30 lines · Chien Wong

Make use of the added constants to reduce duplication.

---
  net/mac80211/aes_cmac.c | 4 ++--
  net/mac80211/aes_gmac.h | 2 +-
  2 files changed, 3 insertions(+), 3 deletions(-)
net/mac80211/aes_cmac.c +2 -2
index adce68ea0981..01fb8b6c5dfb 100644
@@ -16,8 +16,8 @@
  #include "key.h"
  #include "aes_cmac.h"
 
-#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
-#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
+#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];
net/mac80211/aes_gmac.h +0 -0
index c739356bae2a..09378e52c7a6 100644
@@ -9,7 +9,7 @@
  #include <linux/crypto.h>
 
Quoted 7 lines

net/mac80211/aes_gmac.h line 12

Quoted 3 lines
net/mac80211/aes_gmac.h +1 -1
  #define GMAC_AAD_LEN	20
-#define GMAC_MIC_LEN	16
+#define GMAC_MIC_LEN	IEEE80211_GMAC_MIC_LEN

Might really be better to just switch the code to the new ones?

johannes

They will be replaced by direct usage in code.

author
#8 lore raw

PATCH v3 4/5 wifi: mac80211: refactor CMAC crypt functions

In reply to Chien Wong

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.

---
 net/mac80211/aes_cmac.c | 53 ++++++++++-------------------------------
 net/mac80211/aes_cmac.h |  4 +---
 net/mac80211/wpa.c      | 16 ++++++++-----
 3 files changed, 24 insertions(+), 49 deletions(-)
net/mac80211/aes_cmac.c +13 -40
index 01fb8b6c5dfb..588e3c9879b3 100644
@@ -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;
 }
net/mac80211/aes_cmac.h +1 -3
index f74150542142..631fc3033576 100644
@@ -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 */
net/mac80211/wpa.c +10 -6
index bb0fa505cdca..2c1ee4b8e205 100644
@@ -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++;
Signature · 1 line
2.51.2
maintainer
#9 lore raw

Re: [PATCH v3 4/5] wifi: mac80211: refactor CMAC crypt functions

Quoted 6 lines · Chien Wong

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.

I think you got that the wrong way around? Or I don't understand it.

Quoted 1 line

All references to the refactored functions in the tree are adapted.

Not sure what that means? You also had that comment in another place? Do you literally mean "I had to change the callers"? That seems obvious?

Quoted 13 lines
---
 net/mac80211/aes_cmac.c | 53 ++++++++++-------------------------------
 net/mac80211/aes_cmac.h |  4 +---
 net/mac80211/wpa.c      | 16 ++++++++-----
 3 files changed, 24 insertions(+), 49 deletions(-)
net/mac80211/aes_cmac.c +0 -0
index 01fb8b6c5dfb..588e3c9879b3 100644

net/mac80211/aes_cmac.c line 20

Quoted 5 lines
net/mac80211/aes_cmac.c +0 -2
@@ -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

Oh, now you did? OK I guess that's fine too.

net/mac80211/aes_cmac.c line 24

Quoted 8 lines
net/mac80211/aes_cmac.c +2 -2
 #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)

could trivially keep that in 80 cols, and that would even look better :)

net/mac80211/aes_cmac.c line 32

Quoted 9 lines
net/mac80211/aes_cmac.c +3 -1
 {
 	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;

I guess it's a bit annoying that we need this, but OTOH this is not the optimal path (it's SW crypto...) so I guess doesn't matter.

net/mac80211/aes_cmac.c int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, · line 66

Quoted 11 lines
net/mac80211/aes_cmac.c +5 -0
+	if (mic_len == IEEE80211_CMAC_128_MIC_LEN) {
+		u8 out[AES_BLOCK_SIZE];
 
+		err = crypto_shash_finup(desc, zero, mic_len, out);
 		if (err)
 			goto out;
+		memcpy(mic, out, mic_len);
 	} else {
 
+		err = crypto_shash_finup(desc, zero, mic_len, mic);
 	}

Arguably, since it's a SW fixup path, you could also just always copy, using the mic_len == IEEE80211_CMAC_128_MIC_LEN path (since it copies mic_len).

johannes

author
#10 lore raw

Re: [PATCH v3 4/5] wifi: mac80211: refactor CMAC crypt functions

Quoted 9 lines · Johannes Berg
Quoted 6 lines · Chien Wong

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.

I think you got that the wrong way around? Or I don't understand it.

I mean that the functions do allow caller using in uncommon ways and the behavior is not changed after refactor. Anyway, this is obvious from the code. I'd remove the words.>> All references to the refactored functions in the tree are adapted.

Quoted 3 lines

Not sure what that means? You also had that comment in another place? Do you literally mean "I had to change the callers"? That seems obvious?

Yes, I mean I have changed the callers. This is obvious and the words should be removed.

Quoted 6 lines

net/mac80211/aes_cmac.c line 24

Quoted 3 lines

int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,

net/mac80211/aes_cmac.c +1 -1
-		       const u8 *data, size_t data_len, u8 *mic)
+		       const u8 *data, size_t data_len, u8 *mic, unsigned int mic_len)

could trivially keep that in 80 cols, and that would even look better :)

Sure.>> {

Quoted 5 lines
Quoted 5 lines
  	int err;
  	SHASH_DESC_ON_STACK(desc, tfm);
net/mac80211/aes_cmac.c +0 -1
-	u8 out[AES_BLOCK_SIZE];
  	const __le16 *fc;
Quoted 7 lines

net/mac80211/aes_cmac.c line 32

Quoted 3 lines
net/mac80211/aes_cmac.c +3 -0
+	if (mic_len != IEEE80211_CMAC_128_MIC_LEN &&
+	    mic_len != IEEE80211_CMAC_256_MIC_LEN)
+		return -EINVAL;

I guess it's a bit annoying that we need this, but OTOH this is not the optimal path (it's SW crypto...) so I guess doesn't matter.

I feel the same. Validating parameters is unnecessary here.

Quoted 9 lines
Quoted 9 lines
net/mac80211/aes_cmac.c +4 -0
+	if (mic_len == IEEE80211_CMAC_128_MIC_LEN) {
+		u8 out[AES_BLOCK_SIZE];
 
+		err = crypto_shash_finup(desc, zero, mic_len, out);
  		if (err)
  			goto out;
+		memcpy(mic, out, mic_len);
  	} else {
Quoted 6 lines

net/mac80211/aes_cmac.c int ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, · line 66

Quoted 2 lines
net/mac80211/aes_cmac.c +1 -0
+		err = crypto_shash_finup(desc, zero, mic_len, mic);
  	}

Arguably, since it's a SW fixup path, you could also just always copy, using the mic_len == IEEE80211_CMAC_128_MIC_LEN path (since it copies mic_len).

OK. The little optimization here is unnecessary.

author
#11 lore raw

PATCH v3 5/5 wifi: mac80211: refactor CMAC packet handlers

In reply to Chien Wong

Merge CMAC-128 and CMAC-256 handlers since they are almost the same. This removes duplication.
All references to the refactored functions in the tree are adapted.

The comment 'MIC = AES-128-CMAC(IGTK, AAD ...' is out-dated since CMAC is also used with BIGTK, as is the comment for CMAC-256. Simply remove the comments.

Tested-on: mac80211_hwsim

---
 net/mac80211/rx.c  |   6 +-
 net/mac80211/tx.c  |   6 +-
 net/mac80211/wpa.c | 144 ++++++++-------------------------------------
 net/mac80211/wpa.h |  10 ++--
 4 files changed, 35 insertions(+), 131 deletions(-)
net/mac80211/rx.c +4 -2
index 80067ed1da2f..4c1b649b844a 100644
@@ -2215,10 +2215,12 @@ ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
 			rx, IEEE80211_CCMP_256_MIC_LEN);
 		break;
 	case WLAN_CIPHER_SUITE_AES_CMAC:
-		result = ieee80211_crypto_aes_cmac_decrypt(rx);
+		result = ieee80211_crypto_aes_cmac_decrypt(
+			rx, IEEE80211_CMAC_128_MIC_LEN);
 		break;
 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
-		result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
+		result = ieee80211_crypto_aes_cmac_decrypt(
+			rx, IEEE80211_CMAC_256_MIC_LEN);
 		break;
 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
net/mac80211/tx.c +4 -2
index e7b141c55f7a..9d8b0a25f73c 100644
@@ -1062,9 +1062,11 @@ ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
 		return ieee80211_crypto_ccmp_encrypt(
 			tx, IEEE80211_CCMP_256_MIC_LEN);
 	case WLAN_CIPHER_SUITE_AES_CMAC:
-		return ieee80211_crypto_aes_cmac_encrypt(tx);
+		return ieee80211_crypto_aes_cmac_encrypt(
+			tx, IEEE80211_CMAC_128_MIC_LEN);
 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
-		return ieee80211_crypto_aes_cmac_256_encrypt(tx);
+		return ieee80211_crypto_aes_cmac_encrypt(
+			tx, IEEE80211_CMAC_256_MIC_LEN);
 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 		return ieee80211_crypto_aes_gmac_encrypt(tx);
net/mac80211/wpa.c +23 -121

204 lines not on this page · raw message

net/mac80211/wpa.h +4 -6
index a9a81abb5479..6e8846dfe710 100644
@@ -29,13 +29,11 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx,
 			      unsigned int mic_len);
 
 ieee80211_tx_result
-ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx);
-ieee80211_tx_result
-ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx);
-ieee80211_rx_result
-ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx);
+ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx,
+				  unsigned int mic_len);
 ieee80211_rx_result
-ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx);
+ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx,
+				  unsigned int mic_len);
 ieee80211_tx_result
 ieee80211_crypto_aes_gmac_encrypt(struct ieee80211_tx_data *tx);
 ieee80211_rx_result
Signature · 1 line
2.51.2