v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.git] / package / kernel / mac80211 / patches / 131-Revert-mac80211-aes-cmac-switch-to-shash-CMAC-driver.patch
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
4  driver"
5
6 This reverts commit 26717828b75dd5c46e97f7f4a9b937d038bb2852.
7 Reduces mac80211 dependencies for LEDE
8
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 ---
11
12 --- a/net/mac80211/aes_cmac.c
13 +++ b/net/mac80211/aes_cmac.c
14 @@ -22,50 +22,126 @@
15  #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
16  #define AAD_LEN 20
17  
18 -static const u8 zero[CMAC_TLEN_256];
19  
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)
23  {
24 -       SHASH_DESC_ON_STACK(desc, tfm);
25 -       u8 out[AES_BLOCK_SIZE];
26 +       int i, carry;
27  
28 -       desc->tfm = tfm;
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;
33 +       if (carry)
34 +               pad[AES_BLOCK_SIZE - 1] ^= 0x87;
35 +}
36  
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,
43 +                    size_t mac_len)
44 +{
45 +       u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
46 +       const u8 *pos, *end;
47 +       size_t i, e, left, total_len;
48  
49 -       memcpy(mic, out, CMAC_TLEN);
50 +       memset(cbc, 0, AES_BLOCK_SIZE);
51 +
52 +       total_len = 0;
53 +       for (e = 0; e < num_elem; e++)
54 +               total_len += len[e];
55 +       left = total_len;
56 +
57 +       e = 0;
58 +       pos = addr[0];
59 +       end = pos + len[0];
60 +
61 +       while (left >= AES_BLOCK_SIZE) {
62 +               for (i = 0; i < AES_BLOCK_SIZE; i++) {
63 +                       cbc[i] ^= *pos++;
64 +                       if (pos >= end) {
65 +                               e++;
66 +                               pos = addr[e];
67 +                               end = pos + len[e];
68 +                       }
69 +               }
70 +               if (left > AES_BLOCK_SIZE)
71 +                       crypto_cipher_encrypt_one(tfm, cbc, cbc);
72 +               left -= AES_BLOCK_SIZE;
73 +       }
74 +
75 +       memset(pad, 0, AES_BLOCK_SIZE);
76 +       crypto_cipher_encrypt_one(tfm, pad, pad);
77 +       gf_mulx(pad);
78 +
79 +       if (left || total_len == 0) {
80 +               for (i = 0; i < left; i++) {
81 +                       cbc[i] ^= *pos++;
82 +                       if (pos >= end) {
83 +                               e++;
84 +                               pos = addr[e];
85 +                               end = pos + len[e];
86 +                       }
87 +               }
88 +               cbc[left] ^= 0x80;
89 +               gf_mulx(pad);
90 +       }
91 +
92 +       for (i = 0; i < AES_BLOCK_SIZE; i++)
93 +               pad[i] ^= cbc[i];
94 +       crypto_cipher_encrypt_one(tfm, pad, pad);
95 +       memcpy(mac, pad, mac_len);
96  }
97  
98 -void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
99 +
100 +void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad,
101 +                       const u8 *data, size_t data_len, u8 *mic)
102 +{
103 +       const u8 *addr[3];
104 +       size_t len[3];
105 +       u8 zero[CMAC_TLEN];
106 +
107 +       memset(zero, 0, CMAC_TLEN);
108 +       addr[0] = aad;
109 +       len[0] = AAD_LEN;
110 +       addr[1] = data;
111 +       len[1] = data_len - CMAC_TLEN;
112 +       addr[2] = zero;
113 +       len[2] = CMAC_TLEN;
114 +
115 +       aes_cmac_vector(tfm, 3, addr, len, mic, CMAC_TLEN);
116 +}
117 +
118 +void ieee80211_aes_cmac_256(struct crypto_cipher *tfm, const u8 *aad,
119                             const u8 *data, size_t data_len, u8 *mic)
120  {
121 -       SHASH_DESC_ON_STACK(desc, tfm);
122 +       const u8 *addr[3];
123 +       size_t len[3];
124 +       u8 zero[CMAC_TLEN_256];
125  
126 -       desc->tfm = tfm;
127 +       memset(zero, 0, CMAC_TLEN_256);
128 +       addr[0] = aad;
129 +       len[0] = AAD_LEN;
130 +       addr[1] = data;
131 +       len[1] = data_len - CMAC_TLEN_256;
132 +       addr[2] = zero;
133 +       len[2] = CMAC_TLEN_256;
134  
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);
140  }
141  
142 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
143 -                                                 size_t key_len)
144 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
145 +                                                  size_t key_len)
146  {
147 -       struct crypto_shash *tfm;
148 +       struct crypto_cipher *tfm;
149  
150 -       tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
151 +       tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
152         if (!IS_ERR(tfm))
153 -               crypto_shash_setkey(tfm, key, key_len);
154 +               crypto_cipher_setkey(tfm, key, key_len);
155  
156         return tfm;
157  }
158  
159 -void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
160 +
161 +void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm)
162  {
163 -       crypto_free_shash(tfm);
164 +       crypto_free_cipher(tfm);
165  }
166 --- a/net/mac80211/aes_cmac.h
167 +++ b/net/mac80211/aes_cmac.h
168 @@ -10,14 +10,13 @@
169  #define AES_CMAC_H
170  
171  #include <linux/crypto.h>
172 -#include <crypto/hash.h>
173  
174 -struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
175 -                                                 size_t key_len);
176 -void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
177 +struct crypto_cipher *ieee80211_aes_cmac_key_setup(const u8 key[],
178 +                                                  size_t key_len);
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);
186  
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 {
191                 } ccmp;
192                 struct {
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 */
198                 } aes_cmac;