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)
26 + carry = pad[0] & 0x80;
27 + for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
28 + pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
29 + pad[AES_BLOCK_SIZE - 1] <<= 1;
31 + pad[AES_BLOCK_SIZE - 1] ^= 0x87;
34 +void aes_cmac_vector(struct crypto_cipher *tfm, size_t num_elem,
35 + const u8 *addr[], const size_t *len, u8 *mac,
38 - SHASH_DESC_ON_STACK(desc, tfm);
39 - u8 out[AES_BLOCK_SIZE];
40 + u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
41 + const u8 *pos, *end;
42 + size_t i, e, left, total_len;
44 + memset(cbc, 0, AES_BLOCK_SIZE);
47 + for (e = 0; e < num_elem; e++)
48 + total_len += len[e];
55 + while (left >= AES_BLOCK_SIZE) {
56 + for (i = 0; i < AES_BLOCK_SIZE; i++) {
64 + if (left > AES_BLOCK_SIZE)
65 + crypto_cipher_encrypt_one(tfm, cbc, cbc);
66 + left -= AES_BLOCK_SIZE;
69 + memset(pad, 0, AES_BLOCK_SIZE);
70 + crypto_cipher_encrypt_one(tfm, pad, pad);
73 + if (left || total_len == 0) {
74 + for (i = 0; i < left; i++) {
86 + for (i = 0; i < AES_BLOCK_SIZE; i++)
88 + crypto_cipher_encrypt_one(tfm, pad, pad);
89 + memcpy(mac, pad, mac_len);
94 - crypto_shash_init(desc);
95 - crypto_shash_update(desc, aad, AAD_LEN);
96 - crypto_shash_update(desc, data, data_len - CMAC_TLEN);
97 - crypto_shash_finup(desc, zero, CMAC_TLEN, out);
98 +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
99 + const u8 *data, size_t data_len, u8 *mic)
103 + u8 zero[CMAC_TLEN];
105 + memset(zero, 0, CMAC_TLEN);
109 + len[1] = data_len - CMAC_TLEN;
111 + len[2] = CMAC_TLEN;
113 - memcpy(mic, out, CMAC_TLEN);
114 + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN);
117 -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
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];
126 + memset(zero, 0, CMAC_TLEN_256);
130 + len[1] = data_len - CMAC_TLEN_256;
132 + len[2] = CMAC_TLEN_256;
136 - crypto_shash_init(desc);
137 - crypto_shash_update(desc, aad, AAD_LEN);
138 - crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
139 - crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
140 + aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN_256);
143 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
145 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
148 - struct crypto_shash *tfm;
149 + struct crypto_cipher *tfm;
151 - tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
152 + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
154 - crypto_shash_setkey(tfm, key, key_len);
155 + crypto_cipher_setkey(tfm, key, key_len);
160 -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
162 +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
164 - crypto_free_shash(tfm);
165 + crypto_free_cipher(tfm);
167 --- a/net/mac80211/aes_cmac.h
168 +++ b/net/mac80211/aes_cmac.h
172 #include <linux/crypto.h>
173 -#include <crypto/hash.h>
175 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
177 -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
178 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
180 +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
181 const u8 *data, size_t data_len, u8 *mic);
182 -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
183 +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
184 const u8 *data, size_t data_len, u8 *mic);
185 -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm);
186 +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm);
188 #endif /* AES_CMAC_H */
189 --- a/net/mac80211/key.h
190 +++ b/net/mac80211/key.h
191 @@ -92,7 +92,7 @@ struct ieee80211_key {
194 u8 rx_pn[IEEE80211_CMAC_PN_LEN];
195 - struct crypto_shash *tfm;
196 + struct crypto_cipher *tfm;
197 u32 replays; /* dot11RSNAStatsCMACReplays */
198 u32 icverrors; /* dot11RSNAStatsCMACICVErrors */