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
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "crypto/aes.h"
14 #include "crypto/aes_wrap.h"
15 #include "wlantest.h"
16
17
gcmp_aad_nonce(const struct ieee80211_hdr * hdr,const u8 * data,const u8 * a1,const u8 * a2,const u8 * a3,u8 * aad,size_t * aad_len,u8 * nonce)18 static void gcmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
19 const u8 *a1, const u8 *a2, const u8 *a3,
20 u8 *aad, size_t *aad_len, u8 *nonce)
21 {
22 u16 fc, stype, seq;
23 int qos = 0, addr4 = 0;
24 u8 *pos;
25
26 fc = le_to_host16(hdr->frame_control);
27 stype = WLAN_FC_GET_STYPE(fc);
28 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
29 (WLAN_FC_TODS | WLAN_FC_FROMDS))
30 addr4 = 1;
31
32 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
33 fc &= ~0x0070; /* Mask subtype bits */
34 if (stype & 0x08) {
35 const u8 *qc;
36 qos = 1;
37 fc &= ~WLAN_FC_HTC;
38 qc = (const u8 *) (hdr + 1);
39 if (addr4)
40 qc += ETH_ALEN;
41 }
42 }
43
44 fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
45 WPA_PUT_LE16(aad, fc);
46 pos = aad + 2;
47 os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
48 if (a1)
49 os_memcpy(pos, a1, ETH_ALEN);
50 if (a2)
51 os_memcpy(pos + ETH_ALEN, a2, ETH_ALEN);
52 if (a3)
53 os_memcpy(pos + 2 * ETH_ALEN, a3, ETH_ALEN);
54 pos += 3 * ETH_ALEN;
55 seq = le_to_host16(hdr->seq_ctrl);
56 seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
57 WPA_PUT_LE16(pos, seq);
58 pos += 2;
59
60 os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
61 pos += addr4 * ETH_ALEN;
62 if (qos) {
63 pos[0] &= ~0x70;
64 if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
65 pos[0] &= ~0x80;
66 pos++;
67 *pos++ = 0x00;
68 }
69
70 *aad_len = pos - aad;
71
72 if (a2)
73 os_memcpy(nonce, a2, ETH_ALEN);
74 else
75 os_memcpy(nonce, hdr->addr2, ETH_ALEN);
76 nonce[6] = data[7]; /* PN5 */
77 nonce[7] = data[6]; /* PN4 */
78 nonce[8] = data[5]; /* PN3 */
79 nonce[9] = data[4]; /* PN2 */
80 nonce[10] = data[1]; /* PN1 */
81 nonce[11] = data[0]; /* PN0 */
82 }
83
84
gcmp_decrypt(const u8 * tk,size_t tk_len,const struct ieee80211_hdr * hdr,const u8 * a1,const u8 * a2,const u8 * a3,const u8 * data,size_t data_len,size_t * decrypted_len)85 u8 * gcmp_decrypt(const u8 *tk, size_t tk_len, const struct ieee80211_hdr *hdr,
86 const u8 *a1, const u8 *a2, const u8 *a3,
87 const u8 *data, size_t data_len, size_t *decrypted_len)
88 {
89 u8 aad[30], nonce[12], *plain;
90 size_t aad_len, mlen;
91 const u8 *m;
92
93 if (data_len < 8 + 16)
94 return NULL;
95
96 plain = os_malloc(data_len + AES_BLOCK_SIZE);
97 if (plain == NULL)
98 return NULL;
99
100 m = data + 8;
101 mlen = data_len - 8 - 16;
102
103 os_memset(aad, 0, sizeof(aad));
104 gcmp_aad_nonce(hdr, data, a1, a2, a3, aad, &aad_len, nonce);
105 wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
106 wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
107
108 if (aes_gcm_ad(tk, tk_len, nonce, sizeof(nonce), m, mlen, aad, aad_len,
109 m + mlen, plain) < 0) {
110 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
111 wpa_printf(MSG_INFO, "Invalid GCMP frame: A1=" MACSTR
112 " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
113 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
114 MAC2STR(hdr->addr3),
115 WLAN_GET_SEQ_SEQ(seq_ctrl),
116 WLAN_GET_SEQ_FRAG(seq_ctrl));
117 os_free(plain);
118 return NULL;
119 }
120
121 *decrypted_len = mlen;
122 return plain;
123 }
124
125
gcmp_encrypt(const u8 * tk,size_t tk_len,const u8 * frame,size_t len,size_t hdrlen,const u8 * qos,const u8 * a1,const u8 * a2,const u8 * a3,const u8 * pn,int keyid,size_t * encrypted_len)126 u8 * gcmp_encrypt(const u8 *tk, size_t tk_len, const u8 *frame, size_t len,
127 size_t hdrlen, const u8 *qos, const u8 *a1, const u8 *a2,
128 const u8 *a3, const u8 *pn, int keyid, size_t *encrypted_len)
129 {
130 u8 aad[30], nonce[12], *crypt, *pos;
131 size_t aad_len, plen;
132 struct ieee80211_hdr *hdr;
133
134 if (len < hdrlen || hdrlen < 24)
135 return NULL;
136 plen = len - hdrlen;
137
138 crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
139 if (crypt == NULL)
140 return NULL;
141
142 os_memcpy(crypt, frame, hdrlen);
143 hdr = (struct ieee80211_hdr *) crypt;
144 pos = crypt + hdrlen;
145 *pos++ = pn[5]; /* PN0 */
146 *pos++ = pn[4]; /* PN1 */
147 *pos++ = 0x00; /* Rsvd */
148 *pos++ = 0x20 | (keyid << 6);
149 *pos++ = pn[3]; /* PN2 */
150 *pos++ = pn[2]; /* PN3 */
151 *pos++ = pn[1]; /* PN4 */
152 *pos++ = pn[0]; /* PN5 */
153
154 os_memset(aad, 0, sizeof(aad));
155 gcmp_aad_nonce(hdr, crypt + hdrlen, a1, a2, a3, aad, &aad_len, nonce);
156 wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
157 wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
158
159 if (aes_gcm_ae(tk, tk_len, nonce, sizeof(nonce), frame + hdrlen, plen,
160 aad, aad_len, pos, pos + plen) < 0) {
161 os_free(crypt);
162 return NULL;
163 }
164
165 wpa_hexdump(MSG_EXCESSIVE, "GCMP MIC", pos + plen, 16);
166 wpa_hexdump(MSG_EXCESSIVE, "GCMP encrypted", pos, plen);
167
168 *encrypted_len = hdrlen + 8 + plen + 16;
169
170 return crypt;
171 }
172