100-remove-cryptoapi-dependencies.patch 11 KB

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