100-revert_aes_ccm_port.patch 10 KB

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