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.

wpa_ccmp.c 13KB

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