ieee80211_crypto_bip.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* $OpenBSD: ieee80211_crypto_bip.c,v 1.6 2014/12/23 03:24:08 tedu Exp $ */
  2. /*-
  3. * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * This code implements the Broadcast/Multicast Integrity Protocol (BIP)
  19. * defined in IEEE P802.11w/D7.0 section 8.3.4.
  20. */
  21. #include <sys/param.h>
  22. #include <sys/systm.h>
  23. #include <sys/mbuf.h>
  24. #include <sys/malloc.h>
  25. #include <sys/kernel.h>
  26. #include <sys/socket.h>
  27. #include <sys/endian.h>
  28. #include <net/if.h>
  29. #include <net/if_dl.h>
  30. #include <net/if_media.h>
  31. #include <net/if_arp.h>
  32. #include <netinet/in.h>
  33. #include <netinet/if_ether.h>
  34. #include <net80211/ieee80211_var.h>
  35. #include <net80211/ieee80211_crypto.h>
  36. #include <net80211/ieee80211_priv.h>
  37. #include <crypto/rijndael.h>
  38. #include <crypto/cmac.h>
  39. /* BIP software crypto context */
  40. struct ieee80211_bip_ctx {
  41. AES_CMAC_CTX cmac;
  42. };
  43. /*
  44. * Initialize software crypto context. This function can be overridden
  45. * by drivers doing hardware crypto.
  46. */
  47. int
  48. ieee80211_bip_set_key(struct ieee80211com *ic, struct ieee80211_key *k)
  49. {
  50. struct ieee80211_bip_ctx *ctx;
  51. ctx = malloc(sizeof(*ctx), M_DEVBUF, M_NOWAIT | M_ZERO);
  52. if (ctx == NULL)
  53. return ENOMEM;
  54. AES_CMAC_SetKey(&ctx->cmac, k->k_key);
  55. k->k_priv = ctx;
  56. return 0;
  57. }
  58. void
  59. ieee80211_bip_delete_key(struct ieee80211com *ic, struct ieee80211_key *k)
  60. {
  61. if (k->k_priv != NULL)
  62. free(k->k_priv, M_DEVBUF, 0);
  63. k->k_priv = NULL;
  64. }
  65. /* pseudo-header used for BIP MIC computation */
  66. struct ieee80211_bip_frame {
  67. u_int8_t i_fc[2];
  68. u_int8_t i_addr1[IEEE80211_ADDR_LEN];
  69. u_int8_t i_addr2[IEEE80211_ADDR_LEN];
  70. u_int8_t i_addr3[IEEE80211_ADDR_LEN];
  71. } __packed;
  72. struct mbuf *
  73. ieee80211_bip_encap(struct ieee80211com *ic, struct mbuf *m0,
  74. struct ieee80211_key *k)
  75. {
  76. struct ieee80211_bip_ctx *ctx = k->k_priv;
  77. struct ieee80211_bip_frame aad;
  78. struct ieee80211_frame *wh;
  79. u_int8_t *mmie, mic[AES_CMAC_DIGEST_LENGTH];
  80. struct mbuf *m;
  81. wh = mtod(m0, struct ieee80211_frame *);
  82. KASSERT((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
  83. IEEE80211_FC0_TYPE_MGT);
  84. /* clear Protected bit from group management frames */
  85. wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
  86. /* construct AAD (additional authenticated data) */
  87. aad.i_fc[0] = wh->i_fc[0];
  88. aad.i_fc[1] = wh->i_fc[1] & ~(IEEE80211_FC1_RETRY |
  89. IEEE80211_FC1_PWR_MGT | IEEE80211_FC1_MORE_DATA);
  90. /* XXX 11n may require clearing the Order bit too */
  91. IEEE80211_ADDR_COPY(aad.i_addr1, wh->i_addr1);
  92. IEEE80211_ADDR_COPY(aad.i_addr2, wh->i_addr2);
  93. IEEE80211_ADDR_COPY(aad.i_addr3, wh->i_addr3);
  94. AES_CMAC_Init(&ctx->cmac);
  95. AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&aad, sizeof aad);
  96. AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&wh[1],
  97. m0->m_len - sizeof(*wh));
  98. m = m0;
  99. /* reserve trailing space for MMIE */
  100. if (M_TRAILINGSPACE(m) < IEEE80211_MMIE_LEN) {
  101. MGET(m->m_next, M_DONTWAIT, m->m_type);
  102. if (m->m_next == NULL)
  103. goto nospace;
  104. m = m->m_next;
  105. m->m_len = 0;
  106. }
  107. /* construct Management MIC IE */
  108. mmie = mtod(m, u_int8_t *) + m->m_len;
  109. mmie[0] = IEEE80211_ELEMID_MMIE;
  110. mmie[1] = 16;
  111. LE_WRITE_2(&mmie[2], k->k_id);
  112. LE_WRITE_6(&mmie[4], k->k_tsc);
  113. memset(&mmie[10], 0, 8); /* MMIE MIC field set to 0 */
  114. AES_CMAC_Update(&ctx->cmac, mmie, IEEE80211_MMIE_LEN);
  115. AES_CMAC_Final(mic, &ctx->cmac);
  116. /* truncate AES-128-CMAC to 64-bit */
  117. memcpy(&mmie[10], mic, 8);
  118. m->m_len += IEEE80211_MMIE_LEN;
  119. m0->m_pkthdr.len += IEEE80211_MMIE_LEN;
  120. k->k_tsc++;
  121. return m0;
  122. nospace:
  123. ic->ic_stats.is_tx_nombuf++;
  124. m_freem(m0);
  125. return NULL;
  126. }
  127. struct mbuf *
  128. ieee80211_bip_decap(struct ieee80211com *ic, struct mbuf *m0,
  129. struct ieee80211_key *k)
  130. {
  131. struct ieee80211_bip_ctx *ctx = k->k_priv;
  132. struct ieee80211_frame *wh;
  133. struct ieee80211_bip_frame aad;
  134. u_int8_t *mmie, mic0[8], mic[AES_CMAC_DIGEST_LENGTH];
  135. u_int64_t ipn;
  136. wh = mtod(m0, struct ieee80211_frame *);
  137. KASSERT((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
  138. IEEE80211_FC0_TYPE_MGT);
  139. /*
  140. * It is assumed that management frames are contiguous and that
  141. * the mbuf length has already been checked to contain at least
  142. * a header and a MMIE (checked in ieee80211_decrypt()).
  143. */
  144. KASSERT(m0->m_len >= sizeof(*wh) + IEEE80211_MMIE_LEN);
  145. mmie = mtod(m0, u_int8_t *) + m0->m_len - IEEE80211_MMIE_LEN;
  146. ipn = LE_READ_6(&mmie[4]);
  147. if (ipn <= k->k_mgmt_rsc) {
  148. /* replayed frame, discard */
  149. ic->ic_stats.is_cmac_replays++;
  150. m_freem(m0);
  151. return NULL;
  152. }
  153. /* save and mask MMIE MIC field to 0 */
  154. memcpy(mic0, &mmie[10], 8);
  155. memset(&mmie[10], 0, 8);
  156. /* construct AAD (additional authenticated data) */
  157. aad.i_fc[0] = wh->i_fc[0];
  158. aad.i_fc[1] = wh->i_fc[1] & ~(IEEE80211_FC1_RETRY |
  159. IEEE80211_FC1_PWR_MGT | IEEE80211_FC1_MORE_DATA);
  160. /* XXX 11n may require clearing the Order bit too */
  161. IEEE80211_ADDR_COPY(aad.i_addr1, wh->i_addr1);
  162. IEEE80211_ADDR_COPY(aad.i_addr2, wh->i_addr2);
  163. IEEE80211_ADDR_COPY(aad.i_addr3, wh->i_addr3);
  164. /* compute MIC */
  165. AES_CMAC_Init(&ctx->cmac);
  166. AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&aad, sizeof aad);
  167. AES_CMAC_Update(&ctx->cmac, (u_int8_t *)&wh[1],
  168. m0->m_len - sizeof(*wh));
  169. AES_CMAC_Final(mic, &ctx->cmac);
  170. /* check that MIC matches the one in MMIE */
  171. if (timingsafe_bcmp(mic, mic0, 8) != 0) {
  172. ic->ic_stats.is_cmac_icv_errs++;
  173. m_freem(m0);
  174. return NULL;
  175. }
  176. /*
  177. * There is no need to trim the MMIE from the mbuf since it is
  178. * an information element and will be ignored by upper layers.
  179. * We do it anyway as it is cheap to do it here and because it
  180. * may be confused with fixed fields by upper layers.
  181. */
  182. m_adj(m0, -IEEE80211_MMIE_LEN);
  183. /* update last seen packet number (MIC is validated) */
  184. k->k_mgmt_rsc = ipn;
  185. return m0;
  186. }