• 如何理解MIC?


    1. References

    1.1 引用笔记

    2. 引入话题

    众所周知,在Wi-Fi的使用过程中,为了确保数据的安全性,我们会对其数据进行加密。为了达到加密的目的,就需要加密密钥,对于Wi-Fi来说,最主要的两个加密密钥是PTK(成对临时密钥)和GTK(组临时密钥),其PTK用于加密单播数据,而GTK用于加密广播和多播数据。其PTK和GTK的生成,就不得不提Wi-Fi的四步握手了。本篇我们不过多的介绍四步握手,而是对四步握手的EAPOL帧中的MIC字段进行一个详细的理解。

    3. MIC详解

    消息完整性码(Message Integrity Code,MIC)是用于确保数据完整性和真实性的安全机制。在Wi-Fi通信中,MIC的主要作用是检测和防止数据在传输过程中被篡改。

    如上是Wi-Fi的4-way handshake过程,我们可以清晰的看到M2/M3/M4都包含了MIC,以确保数据的准确性。接下来我们详细的讲解下MIC的生成过程。

    3.1 EAPOL帧

    EAPOL(Extensible Authentication Protocol over LAN)是用于在局域网(LAN)中传输EAP消息的协议。EAPOL帧在IEEE 802.1X认证过程中起着关键作用,特别是在Wi-Fi网络中用于WPA、WPA2和WPA3的四次握手协议。EAPOL帧的格式具体如下所示:

    EAPOL帧的字段详解如下所示:

    • Descriptor Type: 这个字段的值定义在IEEE 802.1X-2010中,具体如下所示:

    • Key Information: 这个字段描述了密钥的特性,具体如下所示:

      • Key Descriptor Version (bits 0–2): 应在所有的EAPOL-Key帧上设置为0,表明EAPOL-Key帧是根据下表所述的AKM构建和处理的,除以下情况外:
        • 当协商的AKM是00-0F-AC:1或00-0F-AC:2且“the pairwise cipher”密码是TKIP或“Use group cipher suite”时,此值应该为1,且应用于所有发送到STA的EAPOL-Key帧。在这种情况下,使用下表中的“Deprecated”行。
        • 当协商的AKM是00-0F-AC:1或00-0F-AC:2且“the pairwise or the group cipher”密码是除TKIP之外的增强数据加密机制时,此值应该为2, 且应用于所有发送到STA的EAPOL-Key帧。在这种情况下,使用下表中相应的行。
        • 当协商的AKM是00-0F-AC:3、00-0F-AC:4、00-0F-AC:5或00-0F-AC:6时,此值应该为3,且应用于所有发送到STA的EAPOL-Key帧。在这种情况下,使用下表中相应的行。

          • AKM的组合如下图所示:

      • Key Type (bit 3): 指定此EAPOL-Key帧是否属于生成PTK的四步握手的一部分。
        • 0(Group)表示该消息不属于PTK派生的一部分。
        • 1(Pairwise)表示该消息属于PTK派生的一部分。
      • Reserved (bits 4–5).
      • Install (bit 6).
        • 如果"Key Type"的值为1,则Install如下:
          • 1表示IEEE 802.1X组件应将从该消息派生的临时密钥配置到其IEEE 802.11 MAC中。
          • 0表示IEEE 802.1X组件不应将临时密钥配置到IEEE 802.11 MAC中。
        • 如果"Key Type"的值为0,那么此位保留。

    3.2 MIC的生成

    MIC的计算方式如下所示,根据AKM的不同,MIC的生成算法和长度也不一样。

    3.2.1 hostapd/wpa_supplicant code

    1. /**
    2. * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
    3. * @key: EAPOL-Key Key Confirmation Key (KCK)
    4. * @key_len: KCK length in octets
    5. * @akmp: WPA_KEY_MGMT_* used in key derivation
    6. * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
    7. * @buf: Pointer to the beginning of the EAPOL header (version field)
    8. * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
    9. * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
    10. * Returns: 0 on success, -1 on failure
    11. *
    12. * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
    13. * to be cleared (all zeroes) when calling this function.
    14. *
    15. * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
    16. * description of the Key MIC calculation. It includes packet data from the
    17. * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
    18. * happened during final editing of the standard and the correct behavior is
    19. * defined in the last draft (IEEE 802.11i/D10).
    20. */
    21. int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver,
    22. const u8 *buf, size_t len, u8 *mic)
    23. {
    24. u8 hash[SHA512_MAC_LEN];
    25. if (key_len == 0) {
    26. wpa_printf(MSG_DEBUG,
    27. "WPA: KCK not set - cannot calculate MIC");
    28. return -1;
    29. }
    30. switch (ver) {
    31. #ifndef CONFIG_FIPS
    32. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
    33. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-MD5");
    34. return hmac_md5(key, key_len, buf, len, mic);
    35. #endif /* CONFIG_FIPS */
    36. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
    37. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using HMAC-SHA1");
    38. if (hmac_sha1(key, key_len, buf, len, hash))
    39. return -1;
    40. os_memcpy(mic, hash, MD5_MAC_LEN);
    41. break;
    42. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
    43. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key MIC using AES-CMAC");
    44. return omac1_aes_128(key, buf, len, mic);
    45. case WPA_KEY_INFO_TYPE_AKM_DEFINED:
    46. switch (akmp) {
    47. #ifdef CONFIG_SAE
    48. case WPA_KEY_MGMT_SAE:
    49. case WPA_KEY_MGMT_FT_SAE:
    50. wpa_printf(MSG_DEBUG,
    51. "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - SAE)");
    52. return omac1_aes_128(key, buf, len, mic);
    53. case WPA_KEY_MGMT_SAE_EXT_KEY:
    54. case WPA_KEY_MGMT_FT_SAE_EXT_KEY:
    55. wpa_printf(MSG_DEBUG,
    56. "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - SAE-EXT-KEY)",
    57. (unsigned int) key_len * 8 * 2);
    58. if (key_len == 128 / 8) {
    59. if (hmac_sha256(key, key_len, buf, len, hash))
    60. return -1;
    61. #ifdef CONFIG_SHA384
    62. } else if (key_len == 192 / 8) {
    63. if (hmac_sha384(key, key_len, buf, len, hash))
    64. return -1;
    65. #endif /* CONFIG_SHA384 */
    66. #ifdef CONFIG_SHA512
    67. } else if (key_len == 256 / 8) {
    68. if (hmac_sha512(key, key_len, buf, len, hash))
    69. return -1;
    70. #endif /* CONFIG_SHA512 */
    71. } else {
    72. wpa_printf(MSG_INFO,
    73. "SAE: Unsupported KCK length: %u",
    74. (unsigned int) key_len);
    75. return -1;
    76. }
    77. os_memcpy(mic, hash, key_len);
    78. break;
    79. #endif /* CONFIG_SAE */
    80. #ifdef CONFIG_HS20
    81. case WPA_KEY_MGMT_OSEN:
    82. wpa_printf(MSG_DEBUG,
    83. "WPA: EAPOL-Key MIC using AES-CMAC (AKM-defined - OSEN)");
    84. return omac1_aes_128(key, buf, len, mic);
    85. #endif /* CONFIG_HS20 */
    86. #ifdef CONFIG_SUITEB
    87. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
    88. wpa_printf(MSG_DEBUG,
    89. "WPA: EAPOL-Key MIC using HMAC-SHA256 (AKM-defined - Suite B)");
    90. if (hmac_sha256(key, key_len, buf, len, hash))
    91. return -1;
    92. os_memcpy(mic, hash, MD5_MAC_LEN);
    93. break;
    94. #endif /* CONFIG_SUITEB */
    95. #ifdef CONFIG_SUITEB192
    96. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
    97. wpa_printf(MSG_DEBUG,
    98. "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - Suite B 192-bit)");
    99. if (hmac_sha384(key, key_len, buf, len, hash))
    100. return -1;
    101. os_memcpy(mic, hash, 24);
    102. break;
    103. #endif /* CONFIG_SUITEB192 */
    104. #ifdef CONFIG_OWE
    105. case WPA_KEY_MGMT_OWE:
    106. wpa_printf(MSG_DEBUG,
    107. "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - OWE)",
    108. (unsigned int) key_len * 8 * 2);
    109. if (key_len == 128 / 8) {
    110. if (hmac_sha256(key, key_len, buf, len, hash))
    111. return -1;
    112. } else if (key_len == 192 / 8) {
    113. if (hmac_sha384(key, key_len, buf, len, hash))
    114. return -1;
    115. } else if (key_len == 256 / 8) {
    116. if (hmac_sha512(key, key_len, buf, len, hash))
    117. return -1;
    118. } else {
    119. wpa_printf(MSG_INFO,
    120. "OWE: Unsupported KCK length: %u",
    121. (unsigned int) key_len);
    122. return -1;
    123. }
    124. os_memcpy(mic, hash, key_len);
    125. break;
    126. #endif /* CONFIG_OWE */
    127. #ifdef CONFIG_DPP
    128. case WPA_KEY_MGMT_DPP:
    129. wpa_printf(MSG_DEBUG,
    130. "WPA: EAPOL-Key MIC using HMAC-SHA%u (AKM-defined - DPP)",
    131. (unsigned int) key_len * 8 * 2);
    132. if (key_len == 128 / 8) {
    133. if (hmac_sha256(key, key_len, buf, len, hash))
    134. return -1;
    135. } else if (key_len == 192 / 8) {
    136. if (hmac_sha384(key, key_len, buf, len, hash))
    137. return -1;
    138. } else if (key_len == 256 / 8) {
    139. if (hmac_sha512(key, key_len, buf, len, hash))
    140. return -1;
    141. } else {
    142. wpa_printf(MSG_INFO,
    143. "DPP: Unsupported KCK length: %u",
    144. (unsigned int) key_len);
    145. return -1;
    146. }
    147. os_memcpy(mic, hash, key_len);
    148. break;
    149. #endif /* CONFIG_DPP */
    150. #ifdef CONFIG_SHA384
    151. case WPA_KEY_MGMT_IEEE8021X_SHA384:
    152. #ifdef CONFIG_IEEE80211R
    153. case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
    154. #endif /* CONFIG_IEEE80211R */
    155. wpa_printf(MSG_DEBUG,
    156. "WPA: EAPOL-Key MIC using HMAC-SHA384 (AKM-defined - 802.1X SHA384)");
    157. if (hmac_sha384(key, key_len, buf, len, hash))
    158. return -1;
    159. os_memcpy(mic, hash, 24);
    160. break;
    161. #endif /* CONFIG_SHA384 */
    162. default:
    163. wpa_printf(MSG_DEBUG,
    164. "WPA: EAPOL-Key MIC algorithm not known (AKM-defined - akmp=0x%x)",
    165. akmp);
    166. return -1;
    167. }
    168. break;
    169. default:
    170. wpa_printf(MSG_DEBUG,
    171. "WPA: EAPOL-Key MIC algorithm not known (ver=%d)",
    172. ver);
    173. return -1;
    174. }
    175. return 0;
    176. }
  • 相关阅读:
    https机制原理
    Spring管理Bean(XML与注解方式)
    前端模块化学习:CommonJS规范、AMD规范、CMD规范、ES6规范
    开学教师自我介绍模版:打造个人品牌,轻松赢得学生喜爱
    什么是原型链
    微信支付服务商模式(电商收付通)实现分账操作
    RUST学习过程
    视频领域 CLIP4clip:An Empirical Study of CLIP for End to End Video Clip Retrieval
    有什么最受欢迎的干葡萄酒选择?
    临沂大学张继群-智慧农业项目招募
  • 原文地址:https://blog.csdn.net/weixin_47877869/article/details/140464873