ccmp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Software CCMP (AES-CCM) RX decryption: see ccmp.h.
  2. *
  3. * Self-contained AES-128 + CCM (RFC 3610, L=2, M=8, 13-byte nonce) for the
  4. * 802.11 CCMP frame format. The firmware normally does this, but the 7265D
  5. * path can report SEC_ENC_ERR for both the pairwise and group keys even
  6. * though the keys are installed and the same keys encrypt TX correctly. */
  7. #include "ccmp.h"
  8. static void cpy(unsigned char *d, const unsigned char *s, unsigned int n)
  9. {
  10. while (n--)
  11. *d++ = *s++;
  12. }
  13. static void zero(unsigned char *d, unsigned int n)
  14. {
  15. while (n--)
  16. *d++ = 0;
  17. }
  18. static void xorb(unsigned char *d, const unsigned char *s, unsigned int n)
  19. {
  20. while (n--)
  21. *d++ ^= *s++;
  22. }
  23. /* ---- AES-128 ------------------------------------------------------------- */
  24. static const unsigned char sbox[256] = {
  25. 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
  26. 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
  27. 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  28. 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
  29. 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  30. 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  31. 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
  32. 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
  33. 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
  34. 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  35. 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
  36. 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
  37. 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
  38. 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
  39. 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  40. 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
  41. };
  42. static unsigned char xt(unsigned char x)
  43. {
  44. return (unsigned char)((x << 1) ^ ((x & 0x80) ? 0x1b : 0));
  45. }
  46. static void aes_setkey(unsigned char rk[176], const unsigned char key[16])
  47. {
  48. unsigned char rcon = 1;
  49. unsigned int i;
  50. cpy(rk, key, 16);
  51. for (i = 16; i < 176; i += 4) {
  52. unsigned char t[4];
  53. cpy(t, rk + i - 4, 4);
  54. if (i % 16 == 0) {
  55. unsigned char u = t[0];
  56. t[0] = sbox[t[1]] ^ rcon;
  57. t[1] = sbox[t[2]];
  58. t[2] = sbox[t[3]];
  59. t[3] = sbox[u];
  60. rcon = xt(rcon);
  61. }
  62. rk[i] = rk[i - 16] ^ t[0];
  63. rk[i + 1] = rk[i - 15] ^ t[1];
  64. rk[i + 2] = rk[i - 14] ^ t[2];
  65. rk[i + 3] = rk[i - 13] ^ t[3];
  66. }
  67. }
  68. static void aes_encrypt(const unsigned char rk[176], const unsigned char in[16],
  69. unsigned char out[16])
  70. {
  71. unsigned char s[16], t[16];
  72. unsigned int r, i, c;
  73. cpy(s, in, 16);
  74. for (i = 0; i < 16; i++)
  75. s[i] ^= rk[i];
  76. for (r = 1; r <= 10; r++) {
  77. for (i = 0; i < 16; i++) /* sub + shift rows */
  78. t[i] = sbox[s[(i + 4 * (i % 4)) % 16]];
  79. if (r < 10) {
  80. for (c = 0; c < 4; c++) { /* mix columns */
  81. unsigned char *col = t + 4 * c;
  82. unsigned char a0 = col[0], a1 = col[1];
  83. unsigned char a2 = col[2], a3 = col[3];
  84. col[0] = xt(a0) ^ (xt(a1) ^ a1) ^ a2 ^ a3;
  85. col[1] = a0 ^ xt(a1) ^ (xt(a2) ^ a2) ^ a3;
  86. col[2] = a0 ^ a1 ^ xt(a2) ^ (xt(a3) ^ a3);
  87. col[3] = (xt(a0) ^ a0) ^ a1 ^ a2 ^ xt(a3);
  88. }
  89. }
  90. cpy(s, t, 16);
  91. for (i = 0; i < 16; i++)
  92. s[i] ^= rk[16 * r + i];
  93. }
  94. cpy(out, s, 16);
  95. }
  96. /* ---- CCM ----------------------------------------------------------------- */
  97. /* RFC 3610 CCM with L=2, M=8 and a 13-byte nonce. `mic` is the encrypted
  98. * tag (the last 8 bytes of the CCMP frame). */
  99. static int ccm_decrypt(const unsigned char key[16],
  100. const unsigned char nonce[13],
  101. const unsigned char *aad, unsigned int alen,
  102. unsigned char *data, unsigned int clen,
  103. const unsigned char *mic)
  104. {
  105. unsigned char rk[176], b0[16], x[16], a[16], s[16], blk[16];
  106. unsigned int i, j, n, take;
  107. if (alen > 0xff00)
  108. return -1;
  109. aes_setkey(rk, key);
  110. /* B0: Adata | (M-2)/2 << 3 | L-1, nonce, length (big endian). */
  111. b0[0] = 0x59;
  112. cpy(b0 + 1, nonce, 13);
  113. b0[14] = (unsigned char)(clen >> 8);
  114. b0[15] = (unsigned char)clen;
  115. aes_encrypt(rk, b0, x);
  116. /* AAD: 2-byte length prefix, then the bytes, zero padded. */
  117. zero(blk, 16);
  118. blk[0] = (unsigned char)(alen >> 8);
  119. blk[1] = (unsigned char)alen;
  120. n = alen < 14 ? alen : 14;
  121. cpy(blk + 2, aad, n);
  122. xorb(blk, x, 16);
  123. aes_encrypt(rk, blk, x);
  124. for (i = n; i < alen; i += take) {
  125. take = alen - i < 16 ? alen - i : 16;
  126. zero(blk, 16);
  127. cpy(blk, aad + i, take);
  128. xorb(blk, x, 16);
  129. aes_encrypt(rk, blk, x);
  130. }
  131. /* CTR decrypt, then CBC-MAC over the plaintext. */
  132. for (i = 0; i < clen; i += 16) {
  133. take = clen - i < 16 ? clen - i : 16;
  134. a[0] = 1; /* L-1 */
  135. cpy(a + 1, nonce, 13);
  136. a[14] = (unsigned char)((i / 16 + 1) >> 8);
  137. a[15] = (unsigned char)(i / 16 + 1);
  138. aes_encrypt(rk, a, s);
  139. for (j = 0; j < take; j++)
  140. data[i + j] ^= s[j];
  141. zero(blk, 16);
  142. cpy(blk, data + i, take);
  143. xorb(blk, x, 16);
  144. aes_encrypt(rk, blk, x);
  145. }
  146. /* T = CBC-MAC, tag = T ^ AES(nonce || counter 0). */
  147. a[0] = 1;
  148. cpy(a + 1, nonce, 13);
  149. a[14] = 0;
  150. a[15] = 0;
  151. aes_encrypt(rk, a, s);
  152. for (j = 0; j < 8; j++)
  153. if ((x[j] ^ s[j]) != mic[j])
  154. return -1;
  155. return 0;
  156. }
  157. int ccmp_rx_decrypt(unsigned char *f, unsigned int len, unsigned int hdr,
  158. const unsigned char key[16])
  159. {
  160. unsigned char nonce[13], aad[30];
  161. unsigned int clen, aad_len, fc, seq;
  162. if (hdr != 24 && hdr != 26)
  163. return -1; /* no A4 frames in this driver */
  164. if (len < hdr + 16)
  165. return -1;
  166. clen = len - hdr - 16; /* minus CCMP header and MIC */
  167. /* AAD: FC (mute Retry/PwrMgmt/MoreData, force Protected), A1, A2,
  168. * A3, SC (sequence number zeroed, fragment kept). The 2-byte
  169. * Duration/ID field is deliberately not part of the AAD. */
  170. fc = f[0] | (f[1] << 8);
  171. fc &= ~0x3800u;
  172. fc |= 0x4000u;
  173. aad[0] = (unsigned char)fc;
  174. aad[1] = (unsigned char)(fc >> 8);
  175. cpy(aad + 2, f + 4, 6); /* A1 */
  176. cpy(aad + 8, f + 10, 6); /* A2 */
  177. cpy(aad + 14, f + 16, 6); /* A3 */
  178. seq = f[22] | (f[23] << 8);
  179. seq &= 0x000fu;
  180. aad[20] = (unsigned char)seq;
  181. aad[21] = (unsigned char)(seq >> 8);
  182. aad_len = 22;
  183. if (hdr == 26) {
  184. /* QoS control: keep TID, clear ack policy/A-MSDU/TXOP. */
  185. aad[22] = f[24] & 0x0fu;
  186. aad[23] = 0;
  187. aad_len = 24;
  188. }
  189. /* CCM nonce: priority/QoS TID, A2, then the PN most significant
  190. * byte first (the CCMP header stores it least significant first). */
  191. nonce[0] = hdr == 26 ? (f[24] & 0x0fu) : 0;
  192. cpy(nonce + 1, f + 10, 6); /* A2 */
  193. nonce[7] = f[hdr + 7]; /* PN5 */
  194. nonce[8] = f[hdr + 6]; /* PN4 */
  195. nonce[9] = f[hdr + 5]; /* PN3 */
  196. nonce[10] = f[hdr + 4]; /* PN2 */
  197. nonce[11] = f[hdr + 1]; /* PN1 */
  198. nonce[12] = f[hdr + 0]; /* PN0 */
  199. return ccm_decrypt(key, nonce, aad, aad_len, f + hdr + 8, clen,
  200. f + len - 8);
  201. }