ccmp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * CTR with CBC-MAC Protocol (CCMP)
  3. * Copyright (c) 2010-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/ieee802_11_defs.h"
  11. #include "crypto/aes.h"
  12. #include "wlantest.h"
  13. static void xor_aes_block(u8 *dst, const u8 *src)
  14. {
  15. u32 *d = (u32 *) dst;
  16. u32 *s = (u32 *) src;
  17. *d++ ^= *s++;
  18. *d++ ^= *s++;
  19. *d++ ^= *s++;
  20. *d++ ^= *s++;
  21. }
  22. static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const u8 *nonce,
  23. const u8 *aad, size_t aad_len, size_t plain_len,
  24. u8 *x)
  25. {
  26. u8 aad_buf[2 * AES_BLOCK_SIZE];
  27. u8 b[AES_BLOCK_SIZE];
  28. /* Authentication */
  29. /* B_0: Flags | Nonce N | l(m) */
  30. b[0] = aad_len ? 0x40 : 0 /* Adata */;
  31. b[0] |= (((M - 2) / 2) /* M' */ << 3);
  32. b[0] |= (L - 1) /* L' */;
  33. os_memcpy(&b[1], nonce, 15 - L);
  34. WPA_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);
  35. wpa_hexdump_key(MSG_EXCESSIVE, "CCM B_0", b, AES_BLOCK_SIZE);
  36. aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
  37. if (!aad_len)
  38. return;
  39. WPA_PUT_BE16(aad_buf, aad_len);
  40. os_memcpy(aad_buf + 2, aad, aad_len);
  41. os_memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);
  42. xor_aes_block(aad_buf, x);
  43. aes_encrypt(aes, aad_buf, x); /* X_2 = E(K, X_1 XOR B_1) */
  44. if (aad_len > AES_BLOCK_SIZE - 2) {
  45. xor_aes_block(&aad_buf[AES_BLOCK_SIZE], x);
  46. /* X_3 = E(K, X_2 XOR B_2) */
  47. aes_encrypt(aes, &aad_buf[AES_BLOCK_SIZE], x);
  48. }
  49. }
  50. static void aes_ccm_auth(void *aes, const u8 *data, size_t len, u8 *x)
  51. {
  52. size_t last = len % AES_BLOCK_SIZE;
  53. size_t i;
  54. for (i = 0; i < len / AES_BLOCK_SIZE; i++) {
  55. /* X_i+1 = E(K, X_i XOR B_i) */
  56. xor_aes_block(x, data);
  57. data += AES_BLOCK_SIZE;
  58. aes_encrypt(aes, x, x);
  59. }
  60. if (last) {
  61. /* XOR zero-padded last block */
  62. for (i = 0; i < last; i++)
  63. x[i] ^= *data++;
  64. aes_encrypt(aes, x, x);
  65. }
  66. }
  67. static void aes_ccm_encr_start(size_t L, const u8 *nonce, u8 *a)
  68. {
  69. /* A_i = Flags | Nonce N | Counter i */
  70. a[0] = L - 1; /* Flags = L' */
  71. os_memcpy(&a[1], nonce, 15 - L);
  72. }
  73. static void aes_ccm_encr(void *aes, size_t L, const u8 *in, size_t len, u8 *out,
  74. u8 *a)
  75. {
  76. size_t last = len % AES_BLOCK_SIZE;
  77. size_t i;
  78. /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
  79. for (i = 1; i <= len / AES_BLOCK_SIZE; i++) {
  80. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
  81. /* S_i = E(K, A_i) */
  82. aes_encrypt(aes, a, out);
  83. xor_aes_block(out, in);
  84. out += AES_BLOCK_SIZE;
  85. in += AES_BLOCK_SIZE;
  86. }
  87. if (last) {
  88. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
  89. aes_encrypt(aes, a, out);
  90. /* XOR zero-padded last block */
  91. for (i = 0; i < last; i++)
  92. *out++ ^= *in++;
  93. }
  94. }
  95. static void aes_ccm_encr_auth(void *aes, size_t M, u8 *x, u8 *a, u8 *auth)
  96. {
  97. size_t i;
  98. u8 tmp[AES_BLOCK_SIZE];
  99. wpa_hexdump_key(MSG_EXCESSIVE, "CCM T", x, M);
  100. /* U = T XOR S_0; S_0 = E(K, A_0) */
  101. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
  102. aes_encrypt(aes, a, tmp);
  103. for (i = 0; i < M; i++)
  104. auth[i] = x[i] ^ tmp[i];
  105. wpa_hexdump_key(MSG_EXCESSIVE, "CCM U", auth, M);
  106. }
  107. static void aes_ccm_decr_auth(void *aes, size_t M, u8 *a, const u8 *auth, u8 *t)
  108. {
  109. size_t i;
  110. u8 tmp[AES_BLOCK_SIZE];
  111. wpa_hexdump_key(MSG_EXCESSIVE, "CCM U", auth, M);
  112. /* U = T XOR S_0; S_0 = E(K, A_0) */
  113. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
  114. aes_encrypt(aes, a, tmp);
  115. for (i = 0; i < M; i++)
  116. t[i] = auth[i] ^ tmp[i];
  117. wpa_hexdump_key(MSG_EXCESSIVE, "CCM T", t, M);
  118. }
  119. /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
  120. static int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
  121. size_t M, const u8 *plain, size_t plain_len,
  122. const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth)
  123. {
  124. const size_t L = 2;
  125. void *aes;
  126. u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  127. if (aad_len > 30 || M > AES_BLOCK_SIZE)
  128. return -1;
  129. aes = aes_encrypt_init(key, key_len);
  130. if (aes == NULL)
  131. return -1;
  132. aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, plain_len, x);
  133. aes_ccm_auth(aes, plain, plain_len, x);
  134. /* Encryption */
  135. aes_ccm_encr_start(L, nonce, a);
  136. aes_ccm_encr(aes, L, plain, plain_len, crypt, a);
  137. aes_ccm_encr_auth(aes, M, x, a, auth);
  138. aes_encrypt_deinit(aes);
  139. return 0;
  140. }
  141. /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
  142. static int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
  143. size_t M, const u8 *crypt, size_t crypt_len,
  144. const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain)
  145. {
  146. const size_t L = 2;
  147. void *aes;
  148. u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  149. u8 t[AES_BLOCK_SIZE];
  150. if (aad_len > 30 || M > AES_BLOCK_SIZE)
  151. return -1;
  152. aes = aes_encrypt_init(key, key_len);
  153. if (aes == NULL)
  154. return -1;
  155. /* Decryption */
  156. aes_ccm_encr_start(L, nonce, a);
  157. aes_ccm_decr_auth(aes, M, a, auth, t);
  158. /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
  159. aes_ccm_encr(aes, L, crypt, crypt_len, plain, a);
  160. aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x);
  161. aes_ccm_auth(aes, plain, crypt_len, x);
  162. aes_encrypt_deinit(aes);
  163. if (os_memcmp(x, t, M) != 0) {
  164. wpa_printf(MSG_EXCESSIVE, "CCM: Auth mismatch");
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
  170. u8 *aad, size_t *aad_len, u8 *nonce)
  171. {
  172. u16 fc, stype, seq;
  173. int qos = 0, addr4 = 0;
  174. u8 *pos;
  175. nonce[0] = 0;
  176. fc = le_to_host16(hdr->frame_control);
  177. stype = WLAN_FC_GET_STYPE(fc);
  178. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  179. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  180. addr4 = 1;
  181. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  182. fc &= ~0x0070; /* Mask subtype bits */
  183. if (stype & 0x08) {
  184. const u8 *qc;
  185. qos = 1;
  186. fc &= ~WLAN_FC_ORDER;
  187. qc = (const u8 *) (hdr + 1);
  188. if (addr4)
  189. qc += ETH_ALEN;
  190. nonce[0] = qc[0] & 0x0f;
  191. }
  192. } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
  193. nonce[0] |= 0x10; /* Management */
  194. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  195. fc |= WLAN_FC_ISWEP;
  196. WPA_PUT_LE16(aad, fc);
  197. pos = aad + 2;
  198. os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
  199. pos += 3 * ETH_ALEN;
  200. seq = le_to_host16(hdr->seq_ctrl);
  201. seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
  202. WPA_PUT_LE16(pos, seq);
  203. pos += 2;
  204. os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
  205. pos += addr4 * ETH_ALEN;
  206. if (qos) {
  207. pos[0] &= ~0x70;
  208. if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
  209. pos[0] &= ~0x80;
  210. pos++;
  211. *pos++ = 0x00;
  212. }
  213. *aad_len = pos - aad;
  214. os_memcpy(nonce + 1, hdr->addr2, ETH_ALEN);
  215. nonce[7] = data[7]; /* PN5 */
  216. nonce[8] = data[6]; /* PN4 */
  217. nonce[9] = data[5]; /* PN3 */
  218. nonce[10] = data[4]; /* PN2 */
  219. nonce[11] = data[1]; /* PN1 */
  220. nonce[12] = data[0]; /* PN0 */
  221. }
  222. u8 * ccmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  223. const u8 *data, size_t data_len, size_t *decrypted_len)
  224. {
  225. u8 aad[30], nonce[13];
  226. size_t aad_len;
  227. size_t mlen;
  228. u8 *plain;
  229. if (data_len < 8 + 8)
  230. return NULL;
  231. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  232. if (plain == NULL)
  233. return NULL;
  234. mlen = data_len - 8 - 8;
  235. os_memset(aad, 0, sizeof(aad));
  236. ccmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
  237. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", aad, aad_len);
  238. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  239. if (aes_ccm_ad(tk, 16, nonce, 8, data + 8, mlen, aad, aad_len,
  240. data + 8 + mlen, plain) < 0) {
  241. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  242. wpa_printf(MSG_INFO, "Invalid CCMP MIC in frame: A1=" MACSTR
  243. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  244. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  245. MAC2STR(hdr->addr3),
  246. WLAN_GET_SEQ_SEQ(seq_ctrl),
  247. WLAN_GET_SEQ_FRAG(seq_ctrl));
  248. os_free(plain);
  249. return NULL;
  250. }
  251. wpa_hexdump(MSG_EXCESSIVE, "CCMP decrypted", plain, mlen);
  252. *decrypted_len = mlen;
  253. return plain;
  254. }
  255. void ccmp_get_pn(u8 *pn, const u8 *data)
  256. {
  257. pn[0] = data[7]; /* PN5 */
  258. pn[1] = data[6]; /* PN4 */
  259. pn[2] = data[5]; /* PN3 */
  260. pn[3] = data[4]; /* PN2 */
  261. pn[4] = data[1]; /* PN1 */
  262. pn[5] = data[0]; /* PN0 */
  263. }
  264. u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  265. u8 *pn, int keyid, size_t *encrypted_len)
  266. {
  267. u8 aad[30], nonce[13];
  268. size_t aad_len, plen;
  269. u8 *crypt, *pos;
  270. struct ieee80211_hdr *hdr;
  271. if (len < hdrlen || hdrlen < 24)
  272. return NULL;
  273. plen = len - hdrlen;
  274. crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
  275. if (crypt == NULL)
  276. return NULL;
  277. os_memcpy(crypt, frame, hdrlen);
  278. hdr = (struct ieee80211_hdr *) crypt;
  279. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  280. pos = crypt + hdrlen;
  281. *pos++ = pn[5]; /* PN0 */
  282. *pos++ = pn[4]; /* PN1 */
  283. *pos++ = 0x00; /* Rsvd */
  284. *pos++ = 0x20 | (keyid << 6);
  285. *pos++ = pn[3]; /* PN2 */
  286. *pos++ = pn[2]; /* PN3 */
  287. *pos++ = pn[1]; /* PN4 */
  288. *pos++ = pn[0]; /* PN5 */
  289. os_memset(aad, 0, sizeof(aad));
  290. ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
  291. wpa_hexdump(MSG_EXCESSIVE, "CCMP AAD", aad, aad_len);
  292. wpa_hexdump(MSG_EXCESSIVE, "CCMP nonce", nonce, 13);
  293. if (aes_ccm_ae(tk, 16, nonce, 8, frame + hdrlen, plen, aad, aad_len,
  294. pos, pos + plen) < 0) {
  295. os_free(crypt);
  296. return NULL;
  297. }
  298. wpa_hexdump(MSG_EXCESSIVE, "CCMP encrypted", crypt + hdrlen + 8, plen);
  299. *encrypted_len = hdrlen + 8 + plen + 8;
  300. return crypt;
  301. }