gcmp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * GCM with GMAC Protocol (GCMP)
  3. * Copyright (c) 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 inc32(u8 *block)
  14. {
  15. u32 val;
  16. val = WPA_GET_BE32(block + AES_BLOCK_SIZE - 4);
  17. val++;
  18. WPA_PUT_BE32(block + AES_BLOCK_SIZE - 4, val);
  19. }
  20. static void xor_block(u8 *dst, const u8 *src)
  21. {
  22. u32 *d = (u32 *) dst;
  23. u32 *s = (u32 *) src;
  24. *d++ ^= *s++;
  25. *d++ ^= *s++;
  26. *d++ ^= *s++;
  27. *d++ ^= *s++;
  28. }
  29. static void shift_right_block(u8 *v)
  30. {
  31. u32 val;
  32. val = WPA_GET_BE32(v + 12);
  33. val >>= 1;
  34. if (v[11] & 0x01)
  35. val |= 0x80000000;
  36. WPA_PUT_BE32(v + 12, val);
  37. val = WPA_GET_BE32(v + 8);
  38. val >>= 1;
  39. if (v[7] & 0x01)
  40. val |= 0x80000000;
  41. WPA_PUT_BE32(v + 8, val);
  42. val = WPA_GET_BE32(v + 4);
  43. val >>= 1;
  44. if (v[3] & 0x01)
  45. val |= 0x80000000;
  46. WPA_PUT_BE32(v + 4, val);
  47. val = WPA_GET_BE32(v);
  48. val >>= 1;
  49. WPA_PUT_BE32(v, val);
  50. }
  51. /* Multiplication in GF(2^128) */
  52. static void gf_mult(const u8 *x, const u8 *y, u8 *z)
  53. {
  54. u8 v[16];
  55. int i, j;
  56. os_memset(z, 0, 16); /* Z_0 = 0^128 */
  57. os_memcpy(v, y, 16); /* V_0 = Y */
  58. for (i = 0; i < 16; i++) {
  59. for (j = 0; j < 8; j++) {
  60. if (x[i] & BIT(7 - j)) {
  61. /* Z_(i + 1) = Z_i XOR V_i */
  62. xor_block(z, v);
  63. } else {
  64. /* Z_(i + 1) = Z_i */
  65. }
  66. if (v[15] & 0x01) {
  67. /* V_(i + 1) = (V_i >> 1) XOR R */
  68. shift_right_block(v);
  69. /* R = 11100001 || 0^120 */
  70. v[0] ^= 0xe1;
  71. } else {
  72. /* V_(i + 1) = V_i >> 1 */
  73. shift_right_block(v);
  74. }
  75. }
  76. }
  77. }
  78. static void ghash_start(u8 *y)
  79. {
  80. /* Y_0 = 0^128 */
  81. os_memset(y, 0, 16);
  82. }
  83. static void ghash(const u8 *h, const u8 *x, size_t xlen, u8 *y)
  84. {
  85. size_t m, i;
  86. const u8 *xpos = x;
  87. u8 tmp[16];
  88. m = xlen / 16;
  89. for (i = 0; i < m; i++) {
  90. /* Y_i = (Y^(i-1) XOR X_i) dot H */
  91. xor_block(y, xpos);
  92. xpos += 16;
  93. /* dot operation:
  94. * multiplication operation for binary Galois (finite) field of
  95. * 2^128 elements */
  96. gf_mult(y, h, tmp);
  97. os_memcpy(y, tmp, 16);
  98. }
  99. if (x + xlen > xpos) {
  100. /* Add zero padded last block */
  101. size_t last = x + xlen - xpos;
  102. os_memcpy(tmp, xpos, last);
  103. os_memset(tmp + last, 0, sizeof(tmp) - last);
  104. /* Y_i = (Y^(i-1) XOR X_i) dot H */
  105. xor_block(y, tmp);
  106. /* dot operation:
  107. * multiplication operation for binary Galois (finite) field of
  108. * 2^128 elements */
  109. gf_mult(y, h, tmp);
  110. os_memcpy(y, tmp, 16);
  111. }
  112. /* Return Y_m */
  113. }
  114. static void aes_gctr(void *aes, const u8 *icb, const u8 *x, size_t xlen, u8 *y)
  115. {
  116. size_t i, n, last;
  117. u8 cb[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
  118. const u8 *xpos = x;
  119. u8 *ypos = y;
  120. if (xlen == 0)
  121. return;
  122. n = xlen / 16;
  123. os_memcpy(cb, icb, AES_BLOCK_SIZE);
  124. /* Full blocks */
  125. for (i = 0; i < n; i++) {
  126. aes_encrypt(aes, cb, ypos);
  127. xor_block(ypos, xpos);
  128. xpos += AES_BLOCK_SIZE;
  129. ypos += AES_BLOCK_SIZE;
  130. inc32(cb);
  131. }
  132. last = x + xlen - xpos;
  133. if (last) {
  134. /* Last, partial block */
  135. aes_encrypt(aes, cb, tmp);
  136. for (i = 0; i < last; i++)
  137. *ypos++ = *xpos++ ^ tmp[i];
  138. }
  139. }
  140. /**
  141. * aes_gcm_ae - GCM-AE_K(IV, P, A) with len(IV) = 96
  142. */
  143. static int aes_gcm_ae(const u8 *key, const u8 *iv,
  144. const u8 *plain, size_t plain_len,
  145. const u8 *aad, size_t aad_len,
  146. u8 *crypt, u8 *tag)
  147. {
  148. u8 H[AES_BLOCK_SIZE];
  149. u8 J0[AES_BLOCK_SIZE];
  150. u8 S[16], len_buf[16];
  151. void *aes;
  152. size_t iv_len = 12;
  153. aes = aes_encrypt_init(key, 16);
  154. if (aes == NULL)
  155. return -1;
  156. /* 1. Generate hash subkey H = AES_K(0^128) */
  157. os_memset(H, 0, sizeof(H));
  158. aes_encrypt(aes, H, H);
  159. wpa_hexdump_key(MSG_EXCESSIVE, "Hash subkey H for GHASH", H, sizeof(H));
  160. /* 2. Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
  161. os_memcpy(J0, iv, iv_len);
  162. os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
  163. J0[AES_BLOCK_SIZE - 1] = 0x01;
  164. /* 3. C = GCTR_K(inc_32(J_0), P) */
  165. inc32(J0);
  166. aes_gctr(aes, J0, plain, plain_len, crypt);
  167. /*
  168. * 4. u = 128 * ceil[len(C)/128] - len(C)
  169. * v = 128 * ceil[len(A)/128] - len(A)
  170. * 5. S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
  171. * (i.e., zero padded to block size A || C and lengths of each in bits)
  172. */
  173. ghash_start(S);
  174. ghash(H, aad, aad_len, S);
  175. ghash(H, crypt, plain_len, S);
  176. WPA_PUT_BE64(len_buf, aad_len * 8);
  177. WPA_PUT_BE64(len_buf + 8, plain_len * 8);
  178. ghash(H, len_buf, sizeof(len_buf), S);
  179. wpa_hexdump_key(MSG_EXCESSIVE, "S = GHASH_H(...)", S, 16);
  180. /* 6. T = MSB_t(GCTR_K(J_0, S)) */
  181. J0[AES_BLOCK_SIZE - 1] = 0x01;
  182. aes_gctr(aes, J0, S, sizeof(S), tag);
  183. /* 7. Return (C, T) */
  184. aes_encrypt_deinit(aes);
  185. return 0;
  186. }
  187. /**
  188. * aes_gcm_ad - GCM-AD_K(IV, C, A, T) with len(IV) = 96
  189. */
  190. static int aes_gcm_ad(const u8 *key, const u8 *iv,
  191. const u8 *crypt, size_t crypt_len,
  192. const u8 *aad, size_t aad_len, const u8 *tag,
  193. u8 *plain)
  194. {
  195. u8 H[AES_BLOCK_SIZE];
  196. u8 J0[AES_BLOCK_SIZE];
  197. u8 S[16], T[16], len_buf[16];
  198. void *aes;
  199. size_t iv_len = 12;
  200. aes = aes_encrypt_init(key, 16);
  201. if (aes == NULL)
  202. return -1;
  203. /* 2. Generate hash subkey H = AES_K(0^128) */
  204. os_memset(H, 0, sizeof(H));
  205. aes_encrypt(aes, H, H);
  206. wpa_hexdump_key(MSG_EXCESSIVE, "Hash subkey H for GHASH", H, sizeof(H));
  207. /* 3. Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
  208. os_memcpy(J0, iv, iv_len);
  209. os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
  210. J0[AES_BLOCK_SIZE - 1] = 0x01;
  211. /* 4. C = GCTR_K(inc_32(J_0), C) */
  212. inc32(J0);
  213. aes_gctr(aes, J0, crypt, crypt_len, plain);
  214. /*
  215. * 5. u = 128 * ceil[len(C)/128] - len(C)
  216. * v = 128 * ceil[len(A)/128] - len(A)
  217. * 6. S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
  218. * (i.e., zero padded to block size A || C and lengths of each in bits)
  219. */
  220. ghash_start(S);
  221. ghash(H, aad, aad_len, S);
  222. ghash(H, crypt, crypt_len, S);
  223. WPA_PUT_BE64(len_buf, aad_len * 8);
  224. WPA_PUT_BE64(len_buf + 8, crypt_len * 8);
  225. ghash(H, len_buf, sizeof(len_buf), S);
  226. wpa_hexdump_key(MSG_EXCESSIVE, "S = GHASH_H(...)", S, 16);
  227. /* 7. T' = MSB_t(GCTR_K(J_0, S)) */
  228. J0[AES_BLOCK_SIZE - 1] = 0x01;
  229. aes_gctr(aes, J0, S, sizeof(S), T);
  230. aes_encrypt_deinit(aes);
  231. if (os_memcmp(tag, T, 16) != 0) {
  232. wpa_printf(MSG_EXCESSIVE, "GCM: Tag mismatch");
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. static void gcmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
  238. u8 *aad, size_t *aad_len, u8 *nonce)
  239. {
  240. u16 fc, stype, seq;
  241. int qos = 0, addr4 = 0;
  242. u8 *pos;
  243. fc = le_to_host16(hdr->frame_control);
  244. stype = WLAN_FC_GET_STYPE(fc);
  245. if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
  246. (WLAN_FC_TODS | WLAN_FC_FROMDS))
  247. addr4 = 1;
  248. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
  249. fc &= ~0x0070; /* Mask subtype bits */
  250. if (stype & 0x08) {
  251. const u8 *qc;
  252. qos = 1;
  253. fc &= ~WLAN_FC_ORDER;
  254. qc = (const u8 *) (hdr + 1);
  255. if (addr4)
  256. qc += ETH_ALEN;
  257. }
  258. }
  259. fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
  260. fc |= WLAN_FC_ISWEP;
  261. WPA_PUT_LE16(aad, fc);
  262. pos = aad + 2;
  263. os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
  264. pos += 3 * ETH_ALEN;
  265. seq = le_to_host16(hdr->seq_ctrl);
  266. seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
  267. WPA_PUT_LE16(pos, seq);
  268. pos += 2;
  269. os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
  270. pos += addr4 * ETH_ALEN;
  271. if (qos) {
  272. pos[0] &= ~0x70;
  273. if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
  274. pos[0] &= ~0x80;
  275. pos++;
  276. *pos++ = 0x00;
  277. }
  278. *aad_len = pos - aad;
  279. os_memcpy(nonce, hdr->addr2, ETH_ALEN);
  280. nonce[6] = data[7]; /* PN5 */
  281. nonce[7] = data[6]; /* PN4 */
  282. nonce[8] = data[5]; /* PN3 */
  283. nonce[9] = data[4]; /* PN2 */
  284. nonce[10] = data[1]; /* PN1 */
  285. nonce[11] = data[0]; /* PN0 */
  286. }
  287. u8 * gcmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
  288. const u8 *data, size_t data_len, size_t *decrypted_len)
  289. {
  290. u8 aad[30], nonce[12], *plain;
  291. size_t aad_len, mlen;
  292. const u8 *m;
  293. if (data_len < 8 + 16)
  294. return NULL;
  295. plain = os_malloc(data_len + AES_BLOCK_SIZE);
  296. if (plain == NULL)
  297. return NULL;
  298. m = data + 8;
  299. mlen = data_len - 8 - 16;
  300. os_memset(aad, 0, sizeof(aad));
  301. gcmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
  302. wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
  303. wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
  304. if (aes_gcm_ad(tk, nonce, m, mlen, aad, aad_len, m + mlen, plain) <
  305. 0) {
  306. u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
  307. wpa_printf(MSG_INFO, "Invalid GCMP frame: A1=" MACSTR
  308. " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
  309. MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
  310. MAC2STR(hdr->addr3),
  311. WLAN_GET_SEQ_SEQ(seq_ctrl),
  312. WLAN_GET_SEQ_FRAG(seq_ctrl));
  313. os_free(plain);
  314. return NULL;
  315. }
  316. *decrypted_len = mlen;
  317. return plain;
  318. }
  319. u8 * gcmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
  320. u8 *pn, int keyid, size_t *encrypted_len)
  321. {
  322. u8 aad[30], nonce[12], *crypt, *pos;
  323. size_t aad_len, plen;
  324. struct ieee80211_hdr *hdr;
  325. if (len < hdrlen || hdrlen < 24)
  326. return NULL;
  327. plen = len - hdrlen;
  328. crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
  329. if (crypt == NULL)
  330. return NULL;
  331. os_memcpy(crypt, frame, hdrlen);
  332. hdr = (struct ieee80211_hdr *) crypt;
  333. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  334. pos = crypt + hdrlen;
  335. *pos++ = pn[5]; /* PN0 */
  336. *pos++ = pn[4]; /* PN1 */
  337. *pos++ = 0x00; /* Rsvd */
  338. *pos++ = 0x20 | (keyid << 6);
  339. *pos++ = pn[3]; /* PN2 */
  340. *pos++ = pn[2]; /* PN3 */
  341. *pos++ = pn[1]; /* PN4 */
  342. *pos++ = pn[0]; /* PN5 */
  343. os_memset(aad, 0, sizeof(aad));
  344. gcmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
  345. wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
  346. wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
  347. if (aes_gcm_ae(tk, nonce, frame + hdrlen, plen, aad, aad_len,
  348. pos, pos + plen) < 0) {
  349. os_free(crypt);
  350. return NULL;
  351. }
  352. wpa_hexdump(MSG_EXCESSIVE, "GCMP MIC", pos + plen, 16);
  353. wpa_hexdump(MSG_EXCESSIVE, "GCMP encrypted", pos, plen);
  354. *encrypted_len = hdrlen + 8 + plen + 16;
  355. return crypt;
  356. }