You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <gpxe/net80211.h>
  20. #include <gpxe/crypto.h>
  21. #include <gpxe/hmac.h>
  22. #include <gpxe/sha1.h>
  23. #include <gpxe/aes.h>
  24. #include <gpxe/wpa.h>
  25. #include <byteswap.h>
  26. #include <errno.h>
  27. /** @file
  28. *
  29. * Backend for WPA using the CCMP encryption method
  30. */
  31. /** Context for CCMP encryption and decryption */
  32. struct ccmp_ctx
  33. {
  34. /** AES context - only ever used for encryption */
  35. u8 aes_ctx[AES_CTX_SIZE];
  36. /** Most recently sent packet number */
  37. u64 tx_seq;
  38. /** Most recently received packet number */
  39. u64 rx_seq;
  40. };
  41. /** Header structure at the beginning of CCMP frame data */
  42. struct ccmp_head
  43. {
  44. u8 pn_lo[2]; /**< Bytes 0 and 1 of packet number */
  45. u8 _rsvd; /**< Reserved byte */
  46. u8 kid; /**< Key ID and ExtIV byte */
  47. u8 pn_hi[4]; /**< Bytes 2-5 (2 first) of packet number */
  48. } __attribute__ (( packed ));
  49. /** CCMP header overhead */
  50. #define CCMP_HEAD_LEN 8
  51. /** CCMP MIC trailer overhead */
  52. #define CCMP_MIC_LEN 8
  53. /** CCMP nonce length */
  54. #define CCMP_NONCE_LEN 13
  55. /** CCMP nonce structure */
  56. struct ccmp_nonce
  57. {
  58. u8 prio; /**< Packet priority, 0 for non-QoS */
  59. u8 a2[ETH_ALEN]; /**< Address 2 from packet header (sender) */
  60. u8 pn[6]; /**< Packet number */
  61. } __attribute__ (( packed ));
  62. /** CCMP additional authentication data length (for non-QoS, non-WDS frames) */
  63. #define CCMP_AAD_LEN 22
  64. /** CCMP additional authentication data structure */
  65. struct ccmp_aad
  66. {
  67. u16 fc; /**< Frame Control field */
  68. u8 a1[6]; /**< Address 1 */
  69. u8 a2[6]; /**< Address 2 */
  70. u8 a3[6]; /**< Address 3 */
  71. u16 seq; /**< Sequence Control field */
  72. /* Address 4 and QoS Control are included if present */
  73. } __attribute__ (( packed ));
  74. /** Mask for Frame Control field in AAD */
  75. #define CCMP_AAD_FC_MASK 0xC38F
  76. /** Mask for Sequence Control field in AAD */
  77. #define CCMP_AAD_SEQ_MASK 0x000F
  78. /**
  79. * Convert 6-byte LSB packet number to 64-bit integer
  80. *
  81. * @v pn Pointer to 6-byte packet number
  82. * @ret v 64-bit integer value of @a pn
  83. */
  84. static u64 pn_to_u64 ( const u8 *pn )
  85. {
  86. int i;
  87. u64 ret = 0;
  88. for ( i = 5; i >= 0; i-- ) {
  89. ret <<= 8;
  90. ret |= pn[i];
  91. }
  92. return ret;
  93. }
  94. /**
  95. * Convert 64-bit integer to 6-byte packet number
  96. *
  97. * @v v 64-bit integer
  98. * @v msb If TRUE, reverse the output PN to be in MSB order
  99. * @ret pn 6-byte packet number
  100. *
  101. * The PN is stored in LSB order in the packet header and in MSB order
  102. * in the nonce. WHYYYYY?
  103. */
  104. static void u64_to_pn ( u64 v, u8 *pn, int msb )
  105. {
  106. int i;
  107. u8 *pnp = pn + ( msb ? 5 : 0 );
  108. int delta = ( msb ? -1 : +1 );
  109. for ( i = 0; i < 6; i++ ) {
  110. *pnp = v & 0xFF;
  111. pnp += delta;
  112. v >>= 8;
  113. }
  114. }
  115. /** Value for @a msb argument of u64_to_pn() for MSB output */
  116. #define PN_MSB 1
  117. /** Value for @a msb argument of u64_to_pn() for LSB output */
  118. #define PN_LSB 0
  119. /**
  120. * Initialise CCMP state and install key
  121. *
  122. * @v crypto CCMP cryptosystem structure
  123. * @v key Pointer to 16-byte temporal key to install
  124. * @v keylen Length of key (16 bytes)
  125. * @v rsc Initial receive sequence counter
  126. */
  127. static int ccmp_init ( struct net80211_crypto *crypto, const void *key,
  128. int keylen, const void *rsc )
  129. {
  130. struct ccmp_ctx *ctx = crypto->priv;
  131. if ( keylen != 16 )
  132. return -EINVAL;
  133. if ( rsc )
  134. ctx->rx_seq = pn_to_u64 ( rsc );
  135. cipher_setkey ( &aes_algorithm, ctx->aes_ctx, key, keylen );
  136. return 0;
  137. }
  138. /**
  139. * Encrypt or decrypt data stream using AES in Counter mode
  140. *
  141. * @v ctx CCMP cryptosystem context
  142. * @v nonce Nonce value, 13 bytes
  143. * @v srcv Data to encrypt or decrypt
  144. * @v len Number of bytes pointed to by @a src
  145. * @v msrcv MIC value to encrypt or decrypt (may be NULL)
  146. * @ret destv Encrypted or decrypted data
  147. * @ret mdestv Encrypted or decrypted MIC value
  148. *
  149. * This assumes CCMP parameters of L=2 and M=8. The algorithm is
  150. * defined in RFC 3610.
  151. */
  152. static void ccmp_ctr_xor ( struct ccmp_ctx *ctx, const void *nonce,
  153. const void *srcv, void *destv, int len,
  154. const void *msrcv, void *mdestv )
  155. {
  156. u8 A[16], S[16];
  157. u16 ctr;
  158. int i;
  159. const u8 *src = srcv, *msrc = msrcv;
  160. u8 *dest = destv, *mdest = mdestv;
  161. A[0] = 0x01; /* flags, L' = L - 1 = 1, other bits rsvd */
  162. memcpy ( A + 1, nonce, CCMP_NONCE_LEN );
  163. if ( msrcv ) {
  164. A[14] = A[15] = 0;
  165. cipher_encrypt ( &aes_algorithm, ctx->aes_ctx, A, S, 16 );
  166. for ( i = 0; i < 8; i++ ) {
  167. *mdest++ = *msrc++ ^ S[i];
  168. }
  169. }
  170. for ( ctr = 1 ;; ctr++ ) {
  171. A[14] = ctr >> 8;
  172. A[15] = ctr & 0xFF;
  173. cipher_encrypt ( &aes_algorithm, ctx->aes_ctx, A, S, 16 );
  174. for ( i = 0; i < len && i < 16; i++ )
  175. *dest++ = *src++ ^ S[i];
  176. if ( len <= 16 )
  177. break; /* we're done */
  178. len -= 16;
  179. }
  180. }
  181. /**
  182. * Advance one block in CBC-MAC calculation
  183. *
  184. * @v aes_ctx AES encryption context with key set
  185. * @v B Cleartext block to incorporate (16 bytes)
  186. * @v X Previous ciphertext block (16 bytes)
  187. * @ret B Clobbered
  188. * @ret X New ciphertext block (16 bytes)
  189. *
  190. * This function does X := E[key] ( X ^ B ).
  191. */
  192. static void ccmp_feed_cbc_mac ( void *aes_ctx, u8 *B, u8 *X )
  193. {
  194. int i;
  195. for ( i = 0; i < 16; i++ )
  196. B[i] ^= X[i];
  197. cipher_encrypt ( &aes_algorithm, aes_ctx, B, X, 16 );
  198. }
  199. /**
  200. * Calculate MIC on plaintext data using CBC-MAC
  201. *
  202. * @v ctx CCMP cryptosystem context
  203. * @v nonce Nonce value, 13 bytes
  204. * @v data Data to calculate MIC over
  205. * @v datalen Length of @a data
  206. * @v aad Additional authentication data, for MIC but not encryption
  207. * @ret mic MIC value (unencrypted), 8 bytes
  208. *
  209. * @a aadlen is assumed to be 22 bytes long, as it always is for
  210. * 802.11 use when transmitting non-QoS, not-between-APs frames (the
  211. * only type we deal with).
  212. */
  213. static void ccmp_cbc_mac ( struct ccmp_ctx *ctx, const void *nonce,
  214. const void *data, u16 datalen,
  215. const void *aad, void *mic )
  216. {
  217. u8 X[16], B[16];
  218. /* Zeroth block: flags, nonce, length */
  219. /* Rsv AAD - M'- - L'-
  220. * 0 1 0 1 1 0 0 1 for an 8-byte MAC and 2-byte message length
  221. */
  222. B[0] = 0x59;
  223. memcpy ( B + 1, nonce, CCMP_NONCE_LEN );
  224. B[14] = datalen >> 8;
  225. B[15] = datalen & 0xFF;
  226. cipher_encrypt ( &aes_algorithm, ctx->aes_ctx, B, X, 16 );
  227. /* First block: AAD length field and 14 bytes of AAD */
  228. B[0] = 0;
  229. B[1] = CCMP_AAD_LEN;
  230. memcpy ( B + 2, aad, 14 );
  231. ccmp_feed_cbc_mac ( ctx->aes_ctx, B, X );
  232. /* Second block: Remaining 8 bytes of AAD, 8 bytes zero pad */
  233. memcpy ( B, aad + 14, 8 );
  234. memset ( B + 8, 0, 8 );
  235. ccmp_feed_cbc_mac ( ctx->aes_ctx, B, X );
  236. /* Message blocks */
  237. while ( datalen ) {
  238. if ( datalen >= 16 ) {
  239. memcpy ( B, data, 16 );
  240. datalen -= 16;
  241. } else {
  242. memcpy ( B, data, datalen );
  243. memset ( B + datalen, 0, 16 - datalen );
  244. datalen = 0;
  245. }
  246. ccmp_feed_cbc_mac ( ctx->aes_ctx, B, X );
  247. data += 16;
  248. }
  249. /* Get MIC from final value of X */
  250. memcpy ( mic, X, 8 );
  251. }
  252. /**
  253. * Encapsulate and encrypt a packet using CCMP
  254. *
  255. * @v crypto CCMP cryptosystem
  256. * @v iob I/O buffer containing cleartext packet
  257. * @ret eiob I/O buffer containing encrypted packet
  258. */
  259. struct io_buffer * ccmp_encrypt ( struct net80211_crypto *crypto,
  260. struct io_buffer *iob )
  261. {
  262. struct ccmp_ctx *ctx = crypto->priv;
  263. struct ieee80211_frame *hdr = iob->data;
  264. struct io_buffer *eiob;
  265. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  266. int datalen = iob_len ( iob ) - hdrlen;
  267. struct ccmp_head head;
  268. struct ccmp_nonce nonce;
  269. struct ccmp_aad aad;
  270. u8 mic[8], tx_pn[6];
  271. void *edata, *emic;
  272. ctx->tx_seq++;
  273. u64_to_pn ( ctx->tx_seq, tx_pn, PN_LSB );
  274. /* Allocate memory */
  275. eiob = alloc_iob ( iob_len ( iob ) + CCMP_HEAD_LEN + CCMP_MIC_LEN );
  276. if ( ! eiob )
  277. return NULL;
  278. /* Copy frame header */
  279. memcpy ( iob_put ( eiob, hdrlen ), iob->data, hdrlen );
  280. hdr = eiob->data;
  281. hdr->fc |= IEEE80211_FC_PROTECTED;
  282. /* Fill in packet number and extended IV */
  283. memcpy ( head.pn_lo, tx_pn, 2 );
  284. memcpy ( head.pn_hi, tx_pn + 2, 4 );
  285. head.kid = 0x20; /* have Extended IV, key ID 0 */
  286. head._rsvd = 0;
  287. memcpy ( iob_put ( eiob, sizeof ( head ) ), &head, sizeof ( head ) );
  288. /* Form nonce */
  289. nonce.prio = 0;
  290. memcpy ( nonce.a2, hdr->addr2, ETH_ALEN );
  291. u64_to_pn ( ctx->tx_seq, nonce.pn, PN_MSB );
  292. /* Form additional authentication data */
  293. aad.fc = hdr->fc & CCMP_AAD_FC_MASK;
  294. memcpy ( aad.a1, hdr->addr1, 3 * ETH_ALEN ); /* all 3 at once */
  295. aad.seq = hdr->seq & CCMP_AAD_SEQ_MASK;
  296. /* Calculate MIC over the data */
  297. ccmp_cbc_mac ( ctx, &nonce, iob->data + hdrlen, datalen, &aad, mic );
  298. /* Copy and encrypt data and MIC */
  299. edata = iob_put ( eiob, datalen );
  300. emic = iob_put ( eiob, CCMP_MIC_LEN );
  301. ccmp_ctr_xor ( ctx, &nonce,
  302. iob->data + hdrlen, edata, datalen,
  303. mic, emic );
  304. /* Done! */
  305. DBGC2 ( ctx, "WPA-CCMP %p: encrypted packet %p -> %p\n", ctx,
  306. iob, eiob );
  307. return eiob;
  308. }
  309. /**
  310. * Decrypt a packet using CCMP
  311. *
  312. * @v crypto CCMP cryptosystem
  313. * @v eiob I/O buffer containing encrypted packet
  314. * @ret iob I/O buffer containing cleartext packet
  315. */
  316. static struct io_buffer * ccmp_decrypt ( struct net80211_crypto *crypto,
  317. struct io_buffer *eiob )
  318. {
  319. struct ccmp_ctx *ctx = crypto->priv;
  320. struct ieee80211_frame *hdr;
  321. struct io_buffer *iob;
  322. const int hdrlen = IEEE80211_TYP_FRAME_HEADER_LEN;
  323. int datalen = iob_len ( eiob ) - hdrlen - CCMP_HEAD_LEN - CCMP_MIC_LEN;
  324. struct ccmp_head *head;
  325. struct ccmp_nonce nonce;
  326. struct ccmp_aad aad;
  327. u8 rx_pn[6], their_mic[8], our_mic[8];
  328. iob = alloc_iob ( hdrlen + datalen );
  329. if ( ! iob )
  330. return NULL;
  331. /* Copy frame header */
  332. memcpy ( iob_put ( iob, hdrlen ), eiob->data, hdrlen );
  333. hdr = iob->data;
  334. hdr->fc &= ~IEEE80211_FC_PROTECTED;
  335. /* Check and update RX packet number */
  336. head = eiob->data + hdrlen;
  337. memcpy ( rx_pn, head->pn_lo, 2 );
  338. memcpy ( rx_pn + 2, head->pn_hi, 4 );
  339. if ( pn_to_u64 ( rx_pn ) <= ctx->rx_seq ) {
  340. DBGC ( ctx, "WPA-CCMP %p: packet received out of order "
  341. "(%012llx <= %012llx)\n", ctx, pn_to_u64 ( rx_pn ),
  342. ctx->rx_seq );
  343. free_iob ( iob );
  344. return NULL;
  345. }
  346. ctx->rx_seq = pn_to_u64 ( rx_pn );
  347. DBGC2 ( ctx, "WPA-CCMP %p: RX packet number %012llx\n", ctx, ctx->rx_seq );
  348. /* Form nonce */
  349. nonce.prio = 0;
  350. memcpy ( nonce.a2, hdr->addr2, ETH_ALEN );
  351. u64_to_pn ( ctx->rx_seq, nonce.pn, PN_MSB );
  352. /* Form additional authentication data */
  353. aad.fc = ( hdr->fc & CCMP_AAD_FC_MASK ) | IEEE80211_FC_PROTECTED;
  354. memcpy ( aad.a1, hdr->addr1, 3 * ETH_ALEN ); /* all 3 at once */
  355. aad.seq = hdr->seq & CCMP_AAD_SEQ_MASK;
  356. /* Copy-decrypt data and MIC */
  357. ccmp_ctr_xor ( ctx, &nonce, eiob->data + hdrlen + sizeof ( *head ),
  358. iob_put ( iob, datalen ), datalen,
  359. eiob->tail - CCMP_MIC_LEN, their_mic );
  360. /* Check MIC */
  361. ccmp_cbc_mac ( ctx, &nonce, iob->data + hdrlen, datalen, &aad,
  362. our_mic );
  363. if ( memcmp ( their_mic, our_mic, CCMP_MIC_LEN ) != 0 ) {
  364. DBGC2 ( ctx, "WPA-CCMP %p: MIC failure\n", ctx );
  365. free_iob ( iob );
  366. return NULL;
  367. }
  368. DBGC2 ( ctx, "WPA-CCMP %p: decrypted packet %p -> %p\n", ctx,
  369. eiob, iob );
  370. return iob;
  371. }
  372. /** CCMP cryptosystem */
  373. struct net80211_crypto ccmp_crypto __net80211_crypto = {
  374. .algorithm = NET80211_CRYPT_CCMP,
  375. .init = ccmp_init,
  376. .encrypt = ccmp_encrypt,
  377. .decrypt = ccmp_decrypt,
  378. .priv_len = sizeof ( struct ccmp_ctx ),
  379. };
  380. /**
  381. * Calculate HMAC-SHA1 MIC for EAPOL-Key frame
  382. *
  383. * @v kck Key Confirmation Key, 16 bytes
  384. * @v msg Message to calculate MIC over
  385. * @v len Number of bytes to calculate MIC over
  386. * @ret mic Calculated MIC, 16 bytes long
  387. */
  388. static void ccmp_kie_mic ( const void *kck, const void *msg, size_t len,
  389. void *mic )
  390. {
  391. u8 sha1_ctx[SHA1_CTX_SIZE];
  392. u8 kckb[16];
  393. u8 hash[SHA1_SIZE];
  394. size_t kck_len = 16;
  395. memcpy ( kckb, kck, kck_len );
  396. hmac_init ( &sha1_algorithm, sha1_ctx, kckb, &kck_len );
  397. hmac_update ( &sha1_algorithm, sha1_ctx, msg, len );
  398. hmac_final ( &sha1_algorithm, sha1_ctx, kckb, &kck_len, hash );
  399. memcpy ( mic, hash, 16 );
  400. }
  401. /**
  402. * Decrypt key data in EAPOL-Key frame
  403. *
  404. * @v kek Key Encryption Key, 16 bytes
  405. * @v iv Initialisation vector, 16 bytes (unused)
  406. * @v msg Message to decrypt
  407. * @v len Length of message
  408. * @ret msg Decrypted message in place of original
  409. * @ret len Adjusted downward for 8 bytes of overhead
  410. * @ret rc Return status code
  411. *
  412. * The returned message may still contain padding of 0xDD followed by
  413. * zero or more 0x00 octets. It is impossible to remove the padding
  414. * without parsing the IEs in the packet (another design decision that
  415. * tends to make one question the 802.11i committee's intelligence...)
  416. */
  417. static int ccmp_kie_decrypt ( const void *kek, const void *iv __unused,
  418. void *msg, u16 *len )
  419. {
  420. if ( *len % 8 != 0 )
  421. return -EINVAL;
  422. if ( aes_unwrap ( kek, msg, msg, *len / 8 - 1 ) != 0 )
  423. return -EINVAL;
  424. *len -= 8;
  425. return 0;
  426. }
  427. /** CCMP-style key integrity and encryption handler */
  428. struct wpa_kie ccmp_kie __wpa_kie = {
  429. .version = EAPOL_KEY_VERSION_WPA2,
  430. .mic = ccmp_kie_mic,
  431. .decrypt = ccmp_kie_decrypt,
  432. };