1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 7 Oct 2017 09:37:28 +0200
3 Subject: [PATCH] Revert "mac80211: aes-cmac: switch to shash CMAC
6 This reverts commit 26717828b75dd5c46e97f7f4a9b937d038bb2852.
7 Reduces mac80211 dependencies for LEDE
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
12 --- a/net/mac80211/aes_cmac.c
13 +++ b/net/mac80211/aes_cmac.c
15 #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
18 -static const u8 zero[CMAC_TLEN_256];
20 -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
21 - const u8 *data, size_t data_len, u8 *mic)
22 +void gf_mulx(u8 *pad)
24 - SHASH_DESC_ON_STACK(desc, tfm);
25 - u8 out[AES_BLOCK_SIZE];
29 + carry = pad[0] & 0x80;
30 + for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
31 + pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
32 + pad[AES_BLOCK_SIZE - 1] <<= 1;
34 + pad[AES_BLOCK_SIZE - 1] ^= 0x87;
37 - crypto_shash_init(desc);
38 - crypto_shash_update(desc, aad, AAD_LEN);
39 - crypto_shash_update(desc, data, data_len - CMAC_TLEN);
40 - crypto_shash_finup(desc, zero, CMAC_TLEN, out);
41 +void aes_cmac_vector(struct crypto_cipher *tfm, size_t num_elem,
42 + const u8 *addr[], const size_t *len, u8 *mac,
45 + u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
46 + const u8 *pos, *end;
47 + size_t i, e, left, total_len;
49 - memcpy(mic, out, CMAC_TLEN);
50 + memset(cbc, 0, AES_BLOCK_SIZE);
53 + for (e = 0; e < num_elem; e++)
54 + total_len += len[e];
61 + while (left >= AES_BLOCK_SIZE) {
62 + for (i = 0; i < AES_BLOCK_SIZE; i++) {
70 + if (left > AES_BLOCK_SIZE)
71 + crypto_cipher_encrypt_one(tfm, cbc, cbc);
72 + left -= AES_BLOCK_SIZE;
75 + memset(pad, 0, AES_BLOCK_SIZE);
76 + crypto_cipher_encrypt_one(tfm, pad, pad);
79 + if (left || total_len == 0) {
80 + for (i = 0; i < left; i++) {
92 + for (i = 0; i < AES_BLOCK_SIZE; i++)
94 + crypto_cipher_encrypt_one(tfm, pad, pad);
95 + memcpy(mac, pad, mac_len);
98 -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
100 +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
101 + const u8 *data, size_t data_len, u8 *mic)
105 + u8 zero[CMAC_TLEN];
107 + memset(zero, 0, CMAC_TLEN);
111 + len[1] = data_len - CMAC_TLEN;
113 + len[2] = CMAC_TLEN;
115 + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN);
118 +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
119 const u8 *data, size_t data_len, u8 *mic)
121 - SHASH_DESC_ON_STACK(desc, tfm);
124 + u8 zero[CMAC_TLEN_256];
127 + memset(zero, 0, CMAC_TLEN_256);
131 + len[1] = data_len - CMAC_TLEN_256;
133 + len[2] = CMAC_TLEN_256;
135 - crypto_shash_init(desc);
136 - crypto_shash_update(desc, aad, AAD_LEN);
137 - crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
138 - crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
139 + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN_256);
142 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
144 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
147 - struct crypto_shash *tfm;
148 + struct crypto_cipher *tfm;
150 - tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
151 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
153 - crypto_shash_setkey(tfm, key, key_len);
154 + crypto_cipher_setkey(tfm, key, key_len);
159 -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
161 +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
163 - crypto_free_shash(tfm);
164 + crypto_free_cipher(tfm);
166 --- a/net/mac80211/aes_cmac.h
167 +++ b/net/mac80211/aes_cmac.h
171 #include <linux/crypto.h>
172 -#include <crypto/hash.h>
174 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
176 -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
177 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
179 +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
180 const u8 *data, size_t data_len, u8 *mic);
181 -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
182 +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
183 const u8 *data, size_t data_len, u8 *mic);
184 -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
185 +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm);
187 #endif /* AES_CMAC_H */
188 --- a/net/mac80211/key.h
189 +++ b/net/mac80211/key.h
190 @@ -93,7 +93,7 @@ struct ieee80211_key {
193 u8 rx_pn[IEEE80211_CMAC_PN_LEN];
194 - struct crypto_shash *tfm;
195 + struct crypto_cipher *tfm;
196 u32 replays; /* dot11RSNAStatsCMACReplays */
197 u32 icverrors; /* dot11RSNAStatsCMACICVErrors */