fils_aead.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * FILS AEAD for (Re)Association Request/Response frames
  3. * Copyright 2016, Qualcomm Atheros, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <crypto/aes.h>
  10. #include <crypto/algapi.h>
  11. #include <crypto/skcipher.h>
  12. #include "ieee80211_i.h"
  13. #include "aes_cmac.h"
  14. #include "fils_aead.h"
  15. static int aes_s2v(struct crypto_cipher *tfm,
  16. size_t num_elem, const u8 *addr[], size_t len[], u8 *v)
  17. {
  18. u8 d[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
  19. size_t i;
  20. const u8 *data[2];
  21. size_t data_len[2], data_elems;
  22. /* D = AES-CMAC(K, <zero>) */
  23. memset(tmp, 0, AES_BLOCK_SIZE);
  24. data[0] = tmp;
  25. data_len[0] = AES_BLOCK_SIZE;
  26. aes_cmac_vector(tfm, 1, data, data_len, d, AES_BLOCK_SIZE);
  27. for (i = 0; i < num_elem - 1; i++) {
  28. /* D = dbl(D) xor AES_CMAC(K, Si) */
  29. gf_mulx(d); /* dbl */
  30. aes_cmac_vector(tfm, 1, &addr[i], &len[i], tmp,
  31. AES_BLOCK_SIZE);
  32. crypto_xor(d, tmp, AES_BLOCK_SIZE);
  33. }
  34. if (len[i] >= AES_BLOCK_SIZE) {
  35. /* len(Sn) >= 128 */
  36. size_t j;
  37. const u8 *pos;
  38. /* T = Sn xorend D */
  39. /* Use a temporary buffer to perform xorend on Sn (addr[i]) to
  40. * avoid modifying the const input argument.
  41. */
  42. data[0] = addr[i];
  43. data_len[0] = len[i] - AES_BLOCK_SIZE;
  44. pos = addr[i] + data_len[0];
  45. for (j = 0; j < AES_BLOCK_SIZE; j++)
  46. tmp[j] = pos[j] ^ d[j];
  47. data[1] = tmp;
  48. data_len[1] = AES_BLOCK_SIZE;
  49. data_elems = 2;
  50. } else {
  51. /* len(Sn) < 128 */
  52. /* T = dbl(D) xor pad(Sn) */
  53. gf_mulx(d); /* dbl */
  54. memset(tmp, 0, AES_BLOCK_SIZE);
  55. memcpy(tmp, addr[i], len[i]);
  56. tmp[len[i]] = 0x80;
  57. crypto_xor(d, tmp, AES_BLOCK_SIZE);
  58. data[0] = d;
  59. data_len[0] = sizeof(d);
  60. data_elems = 1;
  61. }
  62. /* V = AES-CMAC(K, T) */
  63. aes_cmac_vector(tfm, data_elems, data, data_len, v, AES_BLOCK_SIZE);
  64. return 0;
  65. }
  66. /* Note: addr[] and len[] needs to have one extra slot at the end. */
  67. static int aes_siv_encrypt(const u8 *key, size_t key_len,
  68. const u8 *plain, size_t plain_len,
  69. size_t num_elem, const u8 *addr[],
  70. size_t len[], u8 *out)
  71. {
  72. u8 v[AES_BLOCK_SIZE];
  73. struct crypto_cipher *tfm;
  74. struct crypto_skcipher *tfm2;
  75. struct skcipher_request *req;
  76. int res;
  77. struct scatterlist src[1], dst[1];
  78. u8 *tmp;
  79. key_len /= 2; /* S2V key || CTR key */
  80. addr[num_elem] = plain;
  81. len[num_elem] = plain_len;
  82. num_elem++;
  83. /* S2V */
  84. tfm = crypto_alloc_cipher("aes", 0, 0);
  85. if (IS_ERR(tfm))
  86. return PTR_ERR(tfm);
  87. /* K1 for S2V */
  88. res = crypto_cipher_setkey(tfm, key, key_len);
  89. if (!res)
  90. res = aes_s2v(tfm, num_elem, addr, len, v);
  91. crypto_free_cipher(tfm);
  92. if (res)
  93. return res;
  94. /* Use a temporary buffer of the plaintext to handle need for
  95. * overwriting this during AES-CTR.
  96. */
  97. tmp = kmemdup(plain, plain_len, GFP_KERNEL);
  98. if (!tmp)
  99. return -ENOMEM;
  100. /* IV for CTR before encrypted data */
  101. memcpy(out, v, AES_BLOCK_SIZE);
  102. /* Synthetic IV to be used as the initial counter in CTR:
  103. * Q = V bitand (1^64 || 0^1 || 1^31 || 0^1 || 1^31)
  104. */
  105. v[8] &= 0x7f;
  106. v[12] &= 0x7f;
  107. /* CTR */
  108. tfm2 = crypto_alloc_skcipher("ctr(aes)", 0, 0);
  109. if (IS_ERR(tfm2)) {
  110. kfree(tmp);
  111. return PTR_ERR(tfm2);
  112. }
  113. /* K2 for CTR */
  114. res = crypto_skcipher_setkey(tfm2, key + key_len, key_len);
  115. if (res)
  116. goto fail;
  117. req = skcipher_request_alloc(tfm2, GFP_KERNEL);
  118. if (!req) {
  119. res = -ENOMEM;
  120. goto fail;
  121. }
  122. sg_init_one(src, tmp, plain_len);
  123. sg_init_one(dst, out + AES_BLOCK_SIZE, plain_len);
  124. skcipher_request_set_crypt(req, src, dst, plain_len, v);
  125. res = crypto_skcipher_encrypt(req);
  126. skcipher_request_free(req);
  127. fail:
  128. kfree(tmp);
  129. crypto_free_skcipher(tfm2);
  130. return res;
  131. }
  132. /* Note: addr[] and len[] needs to have one extra slot at the end. */
  133. static int aes_siv_decrypt(const u8 *key, size_t key_len,
  134. const u8 *iv_crypt, size_t iv_c_len,
  135. size_t num_elem, const u8 *addr[], size_t len[],
  136. u8 *out)
  137. {
  138. struct crypto_cipher *tfm;
  139. struct crypto_skcipher *tfm2;
  140. struct skcipher_request *req;
  141. struct scatterlist src[1], dst[1];
  142. size_t crypt_len;
  143. int res;
  144. u8 frame_iv[AES_BLOCK_SIZE], iv[AES_BLOCK_SIZE];
  145. u8 check[AES_BLOCK_SIZE];
  146. crypt_len = iv_c_len - AES_BLOCK_SIZE;
  147. key_len /= 2; /* S2V key || CTR key */
  148. addr[num_elem] = out;
  149. len[num_elem] = crypt_len;
  150. num_elem++;
  151. memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
  152. memcpy(frame_iv, iv_crypt, AES_BLOCK_SIZE);
  153. /* Synthetic IV to be used as the initial counter in CTR:
  154. * Q = V bitand (1^64 || 0^1 || 1^31 || 0^1 || 1^31)
  155. */
  156. iv[8] &= 0x7f;
  157. iv[12] &= 0x7f;
  158. /* CTR */
  159. tfm2 = crypto_alloc_skcipher("ctr(aes)", 0, 0);
  160. if (IS_ERR(tfm2))
  161. return PTR_ERR(tfm2);
  162. /* K2 for CTR */
  163. res = crypto_skcipher_setkey(tfm2, key + key_len, key_len);
  164. if (res) {
  165. crypto_free_skcipher(tfm2);
  166. return res;
  167. }
  168. req = skcipher_request_alloc(tfm2, GFP_KERNEL);
  169. if (!req) {
  170. crypto_free_skcipher(tfm2);
  171. return -ENOMEM;
  172. }
  173. sg_init_one(src, iv_crypt + AES_BLOCK_SIZE, crypt_len);
  174. sg_init_one(dst, out, crypt_len);
  175. skcipher_request_set_crypt(req, src, dst, crypt_len, iv);
  176. res = crypto_skcipher_decrypt(req);
  177. skcipher_request_free(req);
  178. crypto_free_skcipher(tfm2);
  179. if (res)
  180. return res;
  181. /* S2V */
  182. tfm = crypto_alloc_cipher("aes", 0, 0);
  183. if (IS_ERR(tfm))
  184. return PTR_ERR(tfm);
  185. /* K1 for S2V */
  186. res = crypto_cipher_setkey(tfm, key, key_len);
  187. if (!res)
  188. res = aes_s2v(tfm, num_elem, addr, len, check);
  189. crypto_free_cipher(tfm);
  190. if (res)
  191. return res;
  192. if (memcmp(check, frame_iv, AES_BLOCK_SIZE) != 0)
  193. return -EINVAL;
  194. return 0;
  195. }
  196. int fils_encrypt_assoc_req(struct sk_buff *skb,
  197. struct ieee80211_mgd_assoc_data *assoc_data)
  198. {
  199. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  200. u8 *capab, *ies, *encr;
  201. const u8 *addr[5 + 1], *session;
  202. size_t len[5 + 1];
  203. size_t crypt_len;
  204. if (ieee80211_is_reassoc_req(mgmt->frame_control)) {
  205. capab = (u8 *)&mgmt->u.reassoc_req.capab_info;
  206. ies = mgmt->u.reassoc_req.variable;
  207. } else {
  208. capab = (u8 *)&mgmt->u.assoc_req.capab_info;
  209. ies = mgmt->u.assoc_req.variable;
  210. }
  211. session = cfg80211_find_ext_ie(WLAN_EID_EXT_FILS_SESSION,
  212. ies, skb->data + skb->len - ies);
  213. if (!session || session[1] != 1 + 8)
  214. return -EINVAL;
  215. /* encrypt after FILS Session element */
  216. encr = (u8 *)session + 2 + 1 + 8;
  217. /* AES-SIV AAD vectors */
  218. /* The STA's MAC address */
  219. addr[0] = mgmt->sa;
  220. len[0] = ETH_ALEN;
  221. /* The AP's BSSID */
  222. addr[1] = mgmt->da;
  223. len[1] = ETH_ALEN;
  224. /* The STA's nonce */
  225. addr[2] = assoc_data->fils_nonces;
  226. len[2] = FILS_NONCE_LEN;
  227. /* The AP's nonce */
  228. addr[3] = &assoc_data->fils_nonces[FILS_NONCE_LEN];
  229. len[3] = FILS_NONCE_LEN;
  230. /* The (Re)Association Request frame from the Capability Information
  231. * field to the FILS Session element (both inclusive).
  232. */
  233. addr[4] = capab;
  234. len[4] = encr - capab;
  235. crypt_len = skb->data + skb->len - encr;
  236. skb_put(skb, AES_BLOCK_SIZE);
  237. return aes_siv_encrypt(assoc_data->fils_kek, assoc_data->fils_kek_len,
  238. encr, crypt_len, 1, addr, len, encr);
  239. }
  240. int fils_decrypt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  241. u8 *frame, size_t *frame_len,
  242. struct ieee80211_mgd_assoc_data *assoc_data)
  243. {
  244. struct ieee80211_mgmt *mgmt = (void *)frame;
  245. u8 *capab, *ies, *encr;
  246. const u8 *addr[5 + 1], *session;
  247. size_t len[5 + 1];
  248. int res;
  249. size_t crypt_len;
  250. if (*frame_len < 24 + 6)
  251. return -EINVAL;
  252. capab = (u8 *)&mgmt->u.assoc_resp.capab_info;
  253. ies = mgmt->u.assoc_resp.variable;
  254. session = cfg80211_find_ext_ie(WLAN_EID_EXT_FILS_SESSION,
  255. ies, frame + *frame_len - ies);
  256. if (!session || session[1] != 1 + 8) {
  257. mlme_dbg(sdata,
  258. "No (valid) FILS Session element in (Re)Association Response frame from %pM",
  259. mgmt->sa);
  260. return -EINVAL;
  261. }
  262. /* decrypt after FILS Session element */
  263. encr = (u8 *)session + 2 + 1 + 8;
  264. /* AES-SIV AAD vectors */
  265. /* The AP's BSSID */
  266. addr[0] = mgmt->sa;
  267. len[0] = ETH_ALEN;
  268. /* The STA's MAC address */
  269. addr[1] = mgmt->da;
  270. len[1] = ETH_ALEN;
  271. /* The AP's nonce */
  272. addr[2] = &assoc_data->fils_nonces[FILS_NONCE_LEN];
  273. len[2] = FILS_NONCE_LEN;
  274. /* The STA's nonce */
  275. addr[3] = assoc_data->fils_nonces;
  276. len[3] = FILS_NONCE_LEN;
  277. /* The (Re)Association Response frame from the Capability Information
  278. * field to the FILS Session element (both inclusive).
  279. */
  280. addr[4] = capab;
  281. len[4] = encr - capab;
  282. crypt_len = frame + *frame_len - encr;
  283. if (crypt_len < AES_BLOCK_SIZE) {
  284. mlme_dbg(sdata,
  285. "Not enough room for AES-SIV data after FILS Session element in (Re)Association Response frame from %pM",
  286. mgmt->sa);
  287. return -EINVAL;
  288. }
  289. res = aes_siv_decrypt(assoc_data->fils_kek, assoc_data->fils_kek_len,
  290. encr, crypt_len, 5, addr, len, encr);
  291. if (res != 0) {
  292. mlme_dbg(sdata,
  293. "AES-SIV decryption of (Re)Association Response frame from %pM failed",
  294. mgmt->sa);
  295. return res;
  296. }
  297. *frame_len -= AES_BLOCK_SIZE;
  298. return 0;
  299. }