100-remove-cryptoapi-dependencies.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. --- a/net/mac80211/Kconfig
  2. +++ b/net/mac80211/Kconfig
  3. @@ -5,8 +5,6 @@ config MAC80211
  4. depends on CRYPTO
  5. depends on CRYPTO_ARC4
  6. depends on CRYPTO_AES
  7. - select BPAUTO_CRYPTO_CCM
  8. - depends on CRYPTO_GCM
  9. depends on CRC32
  10. ---help---
  11. This option enables the hardware independent IEEE 802.11
  12. --- a/net/mac80211/Makefile
  13. +++ b/net/mac80211/Makefile
  14. @@ -16,9 +16,7 @@ mac80211-y := \
  15. michael.o \
  16. tkip.o \
  17. aes_ccm.o \
  18. - aes_gcm.o \
  19. aes_cmac.o \
  20. - aes_gmac.o \
  21. cfg.o \
  22. ethtool.o \
  23. rx.o \
  24. --- a/net/mac80211/aes_ccm.c
  25. +++ b/net/mac80211/aes_ccm.c
  26. @@ -13,89 +13,132 @@
  27. #include <linux/types.h>
  28. #include <linux/err.h>
  29. #include <crypto/aead.h>
  30. +#include <crypto/aes.h>
  31. #include <net/mac80211.h>
  32. #include "key.h"
  33. #include "aes_ccm.h"
  34. -void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  35. +static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad, u8 *s_0,
  36. + u8 *a, u8 *b)
  37. +{
  38. + int i;
  39. +
  40. + crypto_cipher_encrypt_one(tfm, b, b_0);
  41. +
  42. + /* Extra Authenticate-only data (always two AES blocks) */
  43. + for (i = 0; i < AES_BLOCK_SIZE; i++)
  44. + aad[i] ^= b[i];
  45. + crypto_cipher_encrypt_one(tfm, b, aad);
  46. +
  47. + aad += AES_BLOCK_SIZE;
  48. +
  49. + for (i = 0; i < AES_BLOCK_SIZE; i++)
  50. + aad[i] ^= b[i];
  51. + crypto_cipher_encrypt_one(tfm, a, aad);
  52. +
  53. + /* Mask out bits from auth-only-b_0 */
  54. + b_0[0] &= 0x07;
  55. +
  56. + /* S_0 is used to encrypt T (= MIC) */
  57. + b_0[14] = 0;
  58. + b_0[15] = 0;
  59. + crypto_cipher_encrypt_one(tfm, s_0, b_0);
  60. +}
  61. +
  62. +
  63. +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
  64. u8 *data, size_t data_len, u8 *mic,
  65. size_t mic_len)
  66. {
  67. - struct scatterlist sg[3];
  68. + int i, j, last_len, num_blocks;
  69. + u8 b[AES_BLOCK_SIZE];
  70. + u8 s_0[AES_BLOCK_SIZE];
  71. + u8 e[AES_BLOCK_SIZE];
  72. + u8 *pos, *cpos;
  73. - char aead_req_data[sizeof(struct aead_request) +
  74. - crypto_aead_reqsize(tfm)]
  75. - __aligned(__alignof__(struct aead_request));
  76. - struct aead_request *aead_req = (void *) aead_req_data;
  77. + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
  78. + last_len = data_len % AES_BLOCK_SIZE;
  79. + aes_ccm_prepare(tfm, b_0, aad, s_0, b, b);
  80. - memset(aead_req, 0, sizeof(aead_req_data));
  81. + /* Process payload blocks */
  82. + pos = data;
  83. + cpos = data;
  84. + for (j = 1; j <= num_blocks; j++) {
  85. + int blen = (j == num_blocks && last_len) ?
  86. + last_len : AES_BLOCK_SIZE;
  87. - sg_init_table(sg, 3);
  88. - sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
  89. - sg_set_buf(&sg[1], data, data_len);
  90. - sg_set_buf(&sg[2], mic, mic_len);
  91. + /* Authentication followed by encryption */
  92. + for (i = 0; i < blen; i++)
  93. + b[i] ^= pos[i];
  94. + crypto_cipher_encrypt_one(tfm, b, b);
  95. - aead_request_set_tfm(aead_req, tfm);
  96. - aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
  97. - aead_request_set_ad(aead_req, sg[0].length);
  98. + b_0[14] = (j >> 8) & 0xff;
  99. + b_0[15] = j & 0xff;
  100. + crypto_cipher_encrypt_one(tfm, e, b_0);
  101. + for (i = 0; i < blen; i++)
  102. + *cpos++ = *pos++ ^ e[i];
  103. + }
  104. - crypto_aead_encrypt(aead_req);
  105. + for (i = 0; i < mic_len; i++)
  106. + mic[i] = b[i] ^ s_0[i];
  107. }
  108. -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  109. +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
  110. u8 *data, size_t data_len, u8 *mic,
  111. size_t mic_len)
  112. {
  113. - struct scatterlist sg[3];
  114. - char aead_req_data[sizeof(struct aead_request) +
  115. - crypto_aead_reqsize(tfm)]
  116. - __aligned(__alignof__(struct aead_request));
  117. - struct aead_request *aead_req = (void *) aead_req_data;
  118. + int i, j, last_len, num_blocks;
  119. + u8 *pos, *cpos;
  120. + u8 a[AES_BLOCK_SIZE];
  121. + u8 b[AES_BLOCK_SIZE];
  122. + u8 s_0[AES_BLOCK_SIZE];
  123. - if (data_len == 0)
  124. - return -EINVAL;
  125. + num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
  126. + last_len = data_len % AES_BLOCK_SIZE;
  127. + aes_ccm_prepare(tfm, b_0, aad, s_0, a, b);
  128. - memset(aead_req, 0, sizeof(aead_req_data));
  129. + /* Process payload blocks */
  130. + cpos = data;
  131. + pos = data;
  132. + for (j = 1; j <= num_blocks; j++) {
  133. + int blen = (j == num_blocks && last_len) ?
  134. + last_len : AES_BLOCK_SIZE;
  135. - sg_init_table(sg, 3);
  136. - sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
  137. - sg_set_buf(&sg[1], data, data_len);
  138. - sg_set_buf(&sg[2], mic, mic_len);
  139. + /* Decryption followed by authentication */
  140. + b_0[14] = (j >> 8) & 0xff;
  141. + b_0[15] = j & 0xff;
  142. + crypto_cipher_encrypt_one(tfm, b, b_0);
  143. + for (i = 0; i < blen; i++) {
  144. + *pos = *cpos++ ^ b[i];
  145. + a[i] ^= *pos++;
  146. + }
  147. + crypto_cipher_encrypt_one(tfm, a, a);
  148. + }
  149. - aead_request_set_tfm(aead_req, tfm);
  150. - aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
  151. - aead_request_set_ad(aead_req, sg[0].length);
  152. + for (i = 0; i < mic_len; i++) {
  153. + if ((mic[i] ^ s_0[i]) != a[i])
  154. + return -1;
  155. + }
  156. - return crypto_aead_decrypt(aead_req);
  157. + return 0;
  158. }
  159. -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
  160. - size_t key_len,
  161. - size_t mic_len)
  162. +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
  163. + size_t key_len,
  164. + size_t mic_len)
  165. {
  166. - struct crypto_aead *tfm;
  167. - int err;
  168. -
  169. - tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
  170. - if (IS_ERR(tfm))
  171. - return tfm;
  172. + struct crypto_cipher *tfm;
  173. - err = crypto_aead_setkey(tfm, key, key_len);
  174. - if (err)
  175. - goto free_aead;
  176. - err = crypto_aead_setauthsize(tfm, mic_len);
  177. - if (err)
  178. - goto free_aead;
  179. + tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  180. + if (!IS_ERR(tfm))
  181. + crypto_cipher_setkey(tfm, key, key_len);
  182. return tfm;
  183. -
  184. -free_aead:
  185. - crypto_free_aead(tfm);
  186. - return ERR_PTR(err);
  187. }
  188. -void ieee80211_aes_key_free(struct crypto_aead *tfm)
  189. +
  190. +void ieee80211_aes_key_free(struct crypto_cipher *tfm)
  191. {
  192. - crypto_free_aead(tfm);
  193. + crypto_free_cipher(tfm);
  194. }
  195. --- a/net/mac80211/aes_ccm.h
  196. +++ b/net/mac80211/aes_ccm.h
  197. @@ -12,15 +12,15 @@
  198. #include <linux/crypto.h>
  199. -struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
  200. - size_t key_len,
  201. - size_t mic_len);
  202. -void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  203. +struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[],
  204. + size_t key_len,
  205. + size_t mic_len);
  206. +void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
  207. u8 *data, size_t data_len, u8 *mic,
  208. size_t mic_len);
  209. -int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
  210. +int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *b_0, u8 *aad,
  211. u8 *data, size_t data_len, u8 *mic,
  212. size_t mic_len);
  213. -void ieee80211_aes_key_free(struct crypto_aead *tfm);
  214. +void ieee80211_aes_key_free(struct crypto_cipher *tfm);
  215. #endif /* AES_CCM_H */
  216. --- a/net/mac80211/aes_gcm.h
  217. +++ b/net/mac80211/aes_gcm.h
  218. @@ -11,12 +11,28 @@
  219. #include <linux/crypto.h>
  220. -void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  221. - u8 *data, size_t data_len, u8 *mic);
  222. -int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  223. - u8 *data, size_t data_len, u8 *mic);
  224. -struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
  225. - size_t key_len);
  226. -void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm);
  227. +static inline void
  228. +ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  229. + u8 *data, size_t data_len, u8 *mic)
  230. +{
  231. +}
  232. +
  233. +static inline int
  234. +ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
  235. + u8 *data, size_t data_len, u8 *mic)
  236. +{
  237. + return -EOPNOTSUPP;
  238. +}
  239. +
  240. +static inline struct crypto_aead *
  241. +ieee80211_aes_gcm_key_setup_encrypt(const u8 key[], size_t key_len)
  242. +{
  243. + return NULL;
  244. +}
  245. +
  246. +static inline void
  247. +ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
  248. +{
  249. +}
  250. #endif /* AES_GCM_H */
  251. --- a/net/mac80211/aes_gmac.h
  252. +++ b/net/mac80211/aes_gmac.h
  253. @@ -11,10 +11,22 @@
  254. #include <linux/crypto.h>
  255. -struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
  256. - size_t key_len);
  257. -int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
  258. - const u8 *data, size_t data_len, u8 *mic);
  259. -void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm);
  260. +static inline struct crypto_aead *
  261. +ieee80211_aes_gmac_key_setup(const u8 key[], size_t key_len)
  262. +{
  263. + return NULL;
  264. +}
  265. +
  266. +static inline int
  267. +ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
  268. + const u8 *data, size_t data_len, u8 *mic)
  269. +{
  270. + return -EOPNOTSUPP;
  271. +}
  272. +
  273. +static inline void
  274. +ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
  275. +{
  276. +}
  277. #endif /* AES_GMAC_H */
  278. --- a/net/mac80211/key.h
  279. +++ b/net/mac80211/key.h
  280. @@ -88,7 +88,7 @@ struct ieee80211_key {
  281. * Management frames.
  282. */
  283. u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN];
  284. - struct crypto_aead *tfm;
  285. + struct crypto_cipher *tfm;
  286. u32 replays; /* dot11RSNAStatsCCMPReplays */
  287. } ccmp;
  288. struct {
  289. --- a/net/mac80211/wpa.c
  290. +++ b/net/mac80211/wpa.c
  291. @@ -304,7 +304,8 @@ ieee80211_crypto_tkip_decrypt(struct iee
  292. }
  293. -static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
  294. +static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
  295. + u16 data_len)
  296. {
  297. __le16 mask_fc;
  298. int a4_included, mgmt;
  299. @@ -334,14 +335,8 @@ static void ccmp_special_blocks(struct s
  300. else
  301. qos_tid = 0;
  302. - /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  303. - * mode authentication are not allowed to collide, yet both are derived
  304. - * from this vector b_0. We only set L := 1 here to indicate that the
  305. - * data size can be represented in (L+1) bytes. The CCM layer will take
  306. - * care of storing the data length in the top (L+1) bytes and setting
  307. - * and clearing the other bits as is required to derive the two IVs.
  308. - */
  309. - b_0[0] = 0x1;
  310. + /* First block, b_0 */
  311. + b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  312. /* Nonce: Nonce Flags | A2 | PN
  313. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  314. @@ -349,6 +344,8 @@ static void ccmp_special_blocks(struct s
  315. b_0[1] = qos_tid | (mgmt << 4);
  316. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  317. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  318. + /* l(m) */
  319. + put_unaligned_be16(data_len, &b_0[14]);
  320. /* AAD (extra authenticate-only data) / masked 802.11 header
  321. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  322. @@ -460,7 +457,7 @@ static int ccmp_encrypt_skb(struct ieee8
  323. return 0;
  324. pos += IEEE80211_CCMP_HDR_LEN;
  325. - ccmp_special_blocks(skb, pn, b_0, aad);
  326. + ccmp_special_blocks(skb, pn, b_0, aad, len);
  327. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  328. skb_put(skb, mic_len), mic_len);
  329. @@ -537,7 +534,7 @@ ieee80211_crypto_ccmp_decrypt(struct iee
  330. u8 aad[2 * AES_BLOCK_SIZE];
  331. u8 b_0[AES_BLOCK_SIZE];
  332. /* hardware didn't decrypt/verify MIC */
  333. - ccmp_special_blocks(skb, pn, b_0, aad);
  334. + ccmp_special_blocks(skb, pn, b_0, aad, data_len);
  335. if (ieee80211_aes_ccm_decrypt(
  336. key->u.ccmp.tfm, b_0, aad,