|
|
@@ -0,0 +1,226 @@
|
|
|
+/* Software CCMP (AES-CCM) RX decryption: see ccmp.h.
|
|
|
+ *
|
|
|
+ * Self-contained AES-128 + CCM (RFC 3610, L=2, M=8, 13-byte nonce) for the
|
|
|
+ * 802.11 CCMP frame format. The firmware normally does this, but the 7265D
|
|
|
+ * path can report SEC_ENC_ERR for both the pairwise and group keys even
|
|
|
+ * though the keys are installed and the same keys encrypt TX correctly. */
|
|
|
+
|
|
|
+#include "ccmp.h"
|
|
|
+
|
|
|
+static void cpy(unsigned char *d, const unsigned char *s, unsigned int n)
|
|
|
+{
|
|
|
+ while (n--)
|
|
|
+ *d++ = *s++;
|
|
|
+}
|
|
|
+
|
|
|
+static void zero(unsigned char *d, unsigned int n)
|
|
|
+{
|
|
|
+ while (n--)
|
|
|
+ *d++ = 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void xorb(unsigned char *d, const unsigned char *s, unsigned int n)
|
|
|
+{
|
|
|
+ while (n--)
|
|
|
+ *d++ ^= *s++;
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- AES-128 ------------------------------------------------------------- */
|
|
|
+static const unsigned char sbox[256] = {
|
|
|
+ 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
|
|
|
+ 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
|
|
|
+ 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
|
|
|
+ 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
|
|
|
+ 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
|
|
|
+ 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
|
|
|
+ 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
|
|
|
+ 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
|
|
|
+ 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
|
|
|
+ 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
|
|
|
+ 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
|
|
|
+ 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
|
|
|
+ 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
|
|
|
+ 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
|
|
|
+ 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
|
|
|
+ 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
|
|
|
+};
|
|
|
+
|
|
|
+static unsigned char xt(unsigned char x)
|
|
|
+{
|
|
|
+ return (unsigned char)((x << 1) ^ ((x & 0x80) ? 0x1b : 0));
|
|
|
+}
|
|
|
+
|
|
|
+static void aes_setkey(unsigned char rk[176], const unsigned char key[16])
|
|
|
+{
|
|
|
+ unsigned char rcon = 1;
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ cpy(rk, key, 16);
|
|
|
+ for (i = 16; i < 176; i += 4) {
|
|
|
+ unsigned char t[4];
|
|
|
+
|
|
|
+ cpy(t, rk + i - 4, 4);
|
|
|
+ if (i % 16 == 0) {
|
|
|
+ unsigned char u = t[0];
|
|
|
+
|
|
|
+ t[0] = sbox[t[1]] ^ rcon;
|
|
|
+ t[1] = sbox[t[2]];
|
|
|
+ t[2] = sbox[t[3]];
|
|
|
+ t[3] = sbox[u];
|
|
|
+ rcon = xt(rcon);
|
|
|
+ }
|
|
|
+ rk[i] = rk[i - 16] ^ t[0];
|
|
|
+ rk[i + 1] = rk[i - 15] ^ t[1];
|
|
|
+ rk[i + 2] = rk[i - 14] ^ t[2];
|
|
|
+ rk[i + 3] = rk[i - 13] ^ t[3];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void aes_encrypt(const unsigned char rk[176], const unsigned char in[16],
|
|
|
+ unsigned char out[16])
|
|
|
+{
|
|
|
+ unsigned char s[16], t[16];
|
|
|
+ unsigned int r, i, c;
|
|
|
+
|
|
|
+ cpy(s, in, 16);
|
|
|
+ for (i = 0; i < 16; i++)
|
|
|
+ s[i] ^= rk[i];
|
|
|
+ for (r = 1; r <= 10; r++) {
|
|
|
+ for (i = 0; i < 16; i++) /* sub + shift rows */
|
|
|
+ t[i] = sbox[s[(i + 4 * (i % 4)) % 16]];
|
|
|
+ if (r < 10) {
|
|
|
+ for (c = 0; c < 4; c++) { /* mix columns */
|
|
|
+ unsigned char *col = t + 4 * c;
|
|
|
+ unsigned char a0 = col[0], a1 = col[1];
|
|
|
+ unsigned char a2 = col[2], a3 = col[3];
|
|
|
+
|
|
|
+ col[0] = xt(a0) ^ (xt(a1) ^ a1) ^ a2 ^ a3;
|
|
|
+ col[1] = a0 ^ xt(a1) ^ (xt(a2) ^ a2) ^ a3;
|
|
|
+ col[2] = a0 ^ a1 ^ xt(a2) ^ (xt(a3) ^ a3);
|
|
|
+ col[3] = (xt(a0) ^ a0) ^ a1 ^ a2 ^ xt(a3);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cpy(s, t, 16);
|
|
|
+ for (i = 0; i < 16; i++)
|
|
|
+ s[i] ^= rk[16 * r + i];
|
|
|
+ }
|
|
|
+ cpy(out, s, 16);
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- CCM ----------------------------------------------------------------- */
|
|
|
+/* RFC 3610 CCM with L=2, M=8 and a 13-byte nonce. `mic` is the encrypted
|
|
|
+ * tag (the last 8 bytes of the CCMP frame). */
|
|
|
+static int ccm_decrypt(const unsigned char key[16],
|
|
|
+ const unsigned char nonce[13],
|
|
|
+ const unsigned char *aad, unsigned int alen,
|
|
|
+ unsigned char *data, unsigned int clen,
|
|
|
+ const unsigned char *mic)
|
|
|
+{
|
|
|
+ unsigned char rk[176], b0[16], x[16], a[16], s[16], blk[16];
|
|
|
+ unsigned int i, j, n, take;
|
|
|
+
|
|
|
+ if (alen > 0xff00)
|
|
|
+ return -1;
|
|
|
+ aes_setkey(rk, key);
|
|
|
+
|
|
|
+ /* B0: Adata | (M-2)/2 << 3 | L-1, nonce, length (big endian). */
|
|
|
+ b0[0] = 0x59;
|
|
|
+ cpy(b0 + 1, nonce, 13);
|
|
|
+ b0[14] = (unsigned char)(clen >> 8);
|
|
|
+ b0[15] = (unsigned char)clen;
|
|
|
+ aes_encrypt(rk, b0, x);
|
|
|
+
|
|
|
+ /* AAD: 2-byte length prefix, then the bytes, zero padded. */
|
|
|
+ zero(blk, 16);
|
|
|
+ blk[0] = (unsigned char)(alen >> 8);
|
|
|
+ blk[1] = (unsigned char)alen;
|
|
|
+ n = alen < 14 ? alen : 14;
|
|
|
+ cpy(blk + 2, aad, n);
|
|
|
+ xorb(blk, x, 16);
|
|
|
+ aes_encrypt(rk, blk, x);
|
|
|
+ for (i = n; i < alen; i += take) {
|
|
|
+ take = alen - i < 16 ? alen - i : 16;
|
|
|
+ zero(blk, 16);
|
|
|
+ cpy(blk, aad + i, take);
|
|
|
+ xorb(blk, x, 16);
|
|
|
+ aes_encrypt(rk, blk, x);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* CTR decrypt, then CBC-MAC over the plaintext. */
|
|
|
+ for (i = 0; i < clen; i += 16) {
|
|
|
+ take = clen - i < 16 ? clen - i : 16;
|
|
|
+ a[0] = 1; /* L-1 */
|
|
|
+ cpy(a + 1, nonce, 13);
|
|
|
+ a[14] = (unsigned char)((i / 16 + 1) >> 8);
|
|
|
+ a[15] = (unsigned char)(i / 16 + 1);
|
|
|
+ aes_encrypt(rk, a, s);
|
|
|
+ for (j = 0; j < take; j++)
|
|
|
+ data[i + j] ^= s[j];
|
|
|
+
|
|
|
+ zero(blk, 16);
|
|
|
+ cpy(blk, data + i, take);
|
|
|
+ xorb(blk, x, 16);
|
|
|
+ aes_encrypt(rk, blk, x);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* T = CBC-MAC, tag = T ^ AES(nonce || counter 0). */
|
|
|
+ a[0] = 1;
|
|
|
+ cpy(a + 1, nonce, 13);
|
|
|
+ a[14] = 0;
|
|
|
+ a[15] = 0;
|
|
|
+ aes_encrypt(rk, a, s);
|
|
|
+ for (j = 0; j < 8; j++)
|
|
|
+ if ((x[j] ^ s[j]) != mic[j])
|
|
|
+ return -1;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int ccmp_rx_decrypt(unsigned char *f, unsigned int len, unsigned int hdr,
|
|
|
+ const unsigned char key[16])
|
|
|
+{
|
|
|
+ unsigned char nonce[13], aad[30];
|
|
|
+ unsigned int clen, aad_len, fc, seq;
|
|
|
+
|
|
|
+ if (hdr != 24 && hdr != 26)
|
|
|
+ return -1; /* no A4 frames in this driver */
|
|
|
+ if (len < hdr + 16)
|
|
|
+ return -1;
|
|
|
+ clen = len - hdr - 16; /* minus CCMP header and MIC */
|
|
|
+
|
|
|
+ /* AAD: FC (mute Retry/PwrMgmt/MoreData, force Protected), A1, A2,
|
|
|
+ * A3, SC (sequence number zeroed, fragment kept). The 2-byte
|
|
|
+ * Duration/ID field is deliberately not part of the AAD. */
|
|
|
+ fc = f[0] | (f[1] << 8);
|
|
|
+ fc &= ~0x3800u;
|
|
|
+ fc |= 0x4000u;
|
|
|
+ aad[0] = (unsigned char)fc;
|
|
|
+ aad[1] = (unsigned char)(fc >> 8);
|
|
|
+ cpy(aad + 2, f + 4, 6); /* A1 */
|
|
|
+ cpy(aad + 8, f + 10, 6); /* A2 */
|
|
|
+ cpy(aad + 14, f + 16, 6); /* A3 */
|
|
|
+ seq = f[22] | (f[23] << 8);
|
|
|
+ seq &= 0x000fu;
|
|
|
+ aad[20] = (unsigned char)seq;
|
|
|
+ aad[21] = (unsigned char)(seq >> 8);
|
|
|
+ aad_len = 22;
|
|
|
+ if (hdr == 26) {
|
|
|
+ /* QoS control: keep TID, clear ack policy/A-MSDU/TXOP. */
|
|
|
+ aad[22] = f[24] & 0x0fu;
|
|
|
+ aad[23] = 0;
|
|
|
+ aad_len = 24;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* CCM nonce: priority/QoS TID, A2, then the PN most significant
|
|
|
+ * byte first (the CCMP header stores it least significant first). */
|
|
|
+ nonce[0] = hdr == 26 ? (f[24] & 0x0fu) : 0;
|
|
|
+ cpy(nonce + 1, f + 10, 6); /* A2 */
|
|
|
+ nonce[7] = f[hdr + 7]; /* PN5 */
|
|
|
+ nonce[8] = f[hdr + 6]; /* PN4 */
|
|
|
+ nonce[9] = f[hdr + 5]; /* PN3 */
|
|
|
+ nonce[10] = f[hdr + 4]; /* PN2 */
|
|
|
+ nonce[11] = f[hdr + 1]; /* PN1 */
|
|
|
+ nonce[12] = f[hdr + 0]; /* PN0 */
|
|
|
+
|
|
|
+ return ccm_decrypt(key, nonce, aad, aad_len, f + hdr + 8, clen,
|
|
|
+ f + len - 8);
|
|
|
+}
|