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 
9 #ifdef CONFIG_IEEE80211W
10 
11 #include "utils/includes.h"
12 
13 #include "utils/common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "crypto/aes.h"
16 #include "crypto/aes_wrap.h"
17 
18 
ccmp_aad_nonce(const struct ieee80211_hdr * hdr,const u8 * data,u8 * aad,size_t * aad_len,u8 * nonce)19 static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
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 	nonce[0] = 0;
27 
28 	fc = le_to_host16(hdr->frame_control);
29 	stype = WLAN_FC_GET_STYPE(fc);
30 	if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
31 	    (WLAN_FC_TODS | WLAN_FC_FROMDS))
32 		addr4 = 1;
33 
34 	if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
35 		fc &= ~0x0070; /* Mask subtype bits */
36 		if (stype & 0x08) {
37 			const u8 *qc;
38 			qos = 1;
39 			fc &= ~WLAN_FC_ORDER;
40 			qc = (const u8 *) (hdr + 1);
41 			if (addr4)
42 				qc += ETH_ALEN;
43 			nonce[0] = qc[0] & 0x0f;
44 		}
45 	} else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
46 		nonce[0] |= 0x10; /* Management */
47 
48 	fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
49 	fc |= WLAN_FC_ISWEP;
50 	WPA_PUT_LE16(aad, fc);
51 	pos = aad + 2;
52 	os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
53 	pos += 3 * ETH_ALEN;
54 	seq = le_to_host16(hdr->seq_ctrl);
55 	seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
56 	WPA_PUT_LE16(pos, seq);
57 	pos += 2;
58 
59 	os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
60 	pos += addr4 * ETH_ALEN;
61 	if (qos) {
62 		pos[0] &= ~0x70;
63 		if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
64 			pos[0] &= ~0x80;
65 		pos++;
66 		*pos++ = 0x00;
67 	}
68 
69 	*aad_len = pos - aad;
70 
71 	os_memcpy(nonce + 1, hdr->addr2, ETH_ALEN);
72 	nonce[7] = data[7]; /* PN5 */
73 	nonce[8] = data[6]; /* PN4 */
74 	nonce[9] = data[5]; /* PN3 */
75 	nonce[10] = data[4]; /* PN2 */
76 	nonce[11] = data[1]; /* PN1 */
77 	nonce[12] = data[0]; /* PN0 */
78 }
79 
80 
ccmp_aad_nonce_pv1(const u8 * hdr,const u8 * a1,const u8 * a2,const u8 * a3,const u8 * pn,u8 * aad,size_t * aad_len,u8 * nonce)81 static void ccmp_aad_nonce_pv1(const u8 *hdr, const u8 *a1, const u8 *a2,
82 			       const u8 *a3, const u8 *pn,
83 			       u8 *aad, size_t *aad_len, u8 *nonce)
84 {
85 	u16 fc, type;
86 	u8 *pos;
87 
88 	nonce[0] = BIT(5); /* PV1 */
89 	/* TODO: Priority for QMF; 0 is used for Data frames */
90 
91 	fc = WPA_GET_LE16(hdr);
92 	type = (fc & (BIT(2) | BIT(3) | BIT(4))) >> 2;
93 
94 	if (type == 1)
95 		nonce[0] |= 0x10; /* Management */
96 
97 	fc &= ~(BIT(10) | BIT(11) | BIT(13) | BIT(14) | BIT(15));
98 	fc |= BIT(12);
99 	WPA_PUT_LE16(aad, fc);
100 	pos = aad + 2;
101 	if (type == 0 || type == 3) {
102 		const u8 *sc;
103 
104 		os_memcpy(pos, a1, ETH_ALEN);
105 		pos += ETH_ALEN;
106 		os_memcpy(pos, a2, ETH_ALEN);
107 		pos += ETH_ALEN;
108 
109 		if (type == 0) {
110 			/* Either A1 or A2 contains SID */
111 			sc = hdr + 2 + 2 + ETH_ALEN;
112 		} else {
113 			/* Both A1 and A2 contain full addresses */
114 			sc = hdr + 2 + 2 * ETH_ALEN;
115 		}
116 		/* SC with Sequence Number subfield (bits 4-15 of the Sequence
117 		 * Control field) masked to 0. */
118 		*pos++ = *sc & 0x0f;
119 		*pos++ = 0;
120 
121 		if (a3) {
122 			os_memcpy(pos, a3, ETH_ALEN);
123 			pos += ETH_ALEN;
124 		}
125 	}
126 
127 	*aad_len = pos - aad;
128 
129 	os_memcpy(nonce + 1, a2, ETH_ALEN);
130 	nonce[7] = pn[5]; /* PN5 */
131 	nonce[8] = pn[4]; /* PN4 */
132 	nonce[9] = pn[3]; /* PN3 */
133 	nonce[10] = pn[2]; /* PN2 */
134 	nonce[11] = pn[1]; /* PN1 */
135 	nonce[12] = pn[0]; /* PN0 */
136 }
137 
138 
ccmp_decrypt(const u8 * tk,const u8 * hdr,const u8 * data,size_t data_len,size_t * decrypted_len,bool espnow_pkt)139 u8 * ccmp_decrypt(const u8 *tk, const u8 *hdr,
140 		  const u8 *data, size_t data_len, size_t *decrypted_len, bool espnow_pkt)
141 {
142 	u8 aad[30], nonce[13];
143 	size_t aad_len;
144 	size_t mlen;
145 	size_t tag_len = 8;
146 	u8 *plain;
147 
148 	if (data_len < 8 + 8)
149 		return NULL;
150 
151 	plain = os_malloc(data_len + AES_BLOCK_SIZE);
152 	if (plain == NULL)
153 		return NULL;
154 
155 	mlen = data_len - 8 - 8;
156 
157 	os_memset(aad, 0, sizeof(aad));
158 	ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad, &aad_len,
159                    nonce);
160 	wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len);
161 	wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, 13);
162 
163 #ifdef ESPRESSIF_USE
164 	if (espnow_pkt) {
165 		tag_len = 0;
166 		nonce[0] = 0;
167 	}
168 #endif
169 	if (aes_ccm_ad(tk, 16, nonce, tag_len, data + 8, mlen, aad, aad_len,
170                    data + 8 + mlen, plain) < 0) {
171 		os_free(plain);
172 		return NULL;
173 	}
174 	wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen);
175 
176 	*decrypted_len = mlen;
177 	return plain;
178 }
179 
ccmp_get_pn(u8 * pn,const u8 * data)180 void ccmp_get_pn(u8 *pn, const u8 *data)
181 {
182 	pn[0] = data[7]; /* PN5 */
183 	pn[1] = data[6]; /* PN4 */
184 	pn[2] = data[5]; /* PN3 */
185 	pn[3] = data[4]; /* PN2 */
186 	pn[4] = data[1]; /* PN1 */
187 	pn[5] = data[0]; /* PN0 */
188 }
189 
190 
ccmp_encrypt(const u8 * tk,u8 * frame,size_t len,size_t hdrlen,u8 * pn,int keyid,size_t * encrypted_len)191 u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen,
192 		  u8 *pn, int keyid, size_t *encrypted_len)
193 {
194 	u8 aad[30], nonce[13];
195 	size_t aad_len, plen;
196 	u8 *crypt, *pos;
197 	struct ieee80211_hdr *hdr;
198 
199 	if (len < hdrlen || hdrlen < 24)
200 		return NULL;
201 	plen = len - hdrlen;
202 
203 	crypt = os_malloc(hdrlen + 8 + plen + 8 + AES_BLOCK_SIZE);
204 	if (crypt == NULL)
205 		return NULL;
206 
207 	os_memcpy(crypt, frame, hdrlen);
208 	hdr = (struct ieee80211_hdr *) crypt;
209 	hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
210 	pos = crypt + hdrlen;
211 	*pos++ = pn[5]; /* PN0 */
212 	*pos++ = pn[4]; /* PN1 */
213 	*pos++ = 0x00; /* Rsvd */
214 	*pos++ = 0x20 | (keyid << 6);
215 	*pos++ = pn[3]; /* PN2 */
216 	*pos++ = pn[2]; /* PN3 */
217 	*pos++ = pn[1]; /* PN4 */
218 	*pos++ = pn[0]; /* PN5 */
219 
220 	os_memset(aad, 0, sizeof(aad));
221 	ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
222 	wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len);
223 	wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, 13);
224 
225 	if (aes_ccm_ae(tk, 16, nonce, 8, frame + hdrlen, plen, aad, aad_len,
226 		       pos, pos + plen) < 0) {
227 		wpa_printf(MSG_ERROR, "aes ccm ae failed");
228 		os_free(crypt);
229 		return NULL;
230 	}
231 
232 	wpa_hexdump(MSG_DEBUG, "CCMP encrypted", crypt + hdrlen + 8, plen);
233 
234 	*encrypted_len = hdrlen + 8 + plen + 8;
235 
236 	return crypt;
237 }
238 
239 
ccmp_encrypt_pv1(const u8 * tk,const u8 * a1,const u8 * a2,const u8 * a3,const u8 * frame,size_t len,size_t hdrlen,const u8 * pn,int keyid,size_t * encrypted_len)240 u8 * ccmp_encrypt_pv1(const u8 *tk, const u8 *a1, const u8 *a2, const u8 *a3,
241 		      const u8 *frame, size_t len,
242 		      size_t hdrlen, const u8 *pn, int keyid,
243 		      size_t *encrypted_len)
244 {
245 	u8 aad[24], nonce[13];
246 	size_t aad_len, plen;
247 	u8 *crypt, *pos;
248 	struct ieee80211_hdr *hdr;
249 
250 	if (len < hdrlen || hdrlen < 12)
251 		return NULL;
252 	plen = len - hdrlen;
253 
254 	crypt = os_malloc(hdrlen + plen + 8 + AES_BLOCK_SIZE);
255 	if (crypt == NULL)
256 		return NULL;
257 
258 	os_memcpy(crypt, frame, hdrlen);
259 	hdr = (struct ieee80211_hdr *) crypt;
260 	hdr->frame_control |= host_to_le16(BIT(12)); /* Protected Frame */
261 	pos = crypt + hdrlen;
262 
263 	os_memset(aad, 0, sizeof(aad));
264 	ccmp_aad_nonce_pv1(crypt, a1, a2, a3, pn, aad, &aad_len, nonce);
265 	wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len);
266 	wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, sizeof(nonce));
267 
268 	if (aes_ccm_ae(tk, 16, nonce, 8, frame + hdrlen, plen, aad, aad_len,
269 		       pos, pos + plen) < 0) {
270 		os_free(crypt);
271 		return NULL;
272 	}
273 
274 	wpa_hexdump(MSG_DEBUG, "CCMP encrypted", crypt + hdrlen, plen);
275 
276 	*encrypted_len = hdrlen + plen + 8;
277 
278 	return crypt;
279 }
280 
281 
ccmp_256_decrypt(const u8 * tk,const u8 * hdr,const u8 * data,size_t data_len,size_t * decrypted_len)282 u8 * ccmp_256_decrypt(const u8 *tk, const u8 *hdr, const u8 *data,
283 		      size_t data_len, size_t *decrypted_len)
284 {
285 	u8 aad[30], nonce[13];
286 	size_t aad_len;
287 	size_t mlen;
288 	u8 *plain;
289 
290 	if (data_len < 8 + 16)
291 		return NULL;
292 
293 	plain = os_malloc(data_len + AES_BLOCK_SIZE);
294 	if (plain == NULL)
295 		return NULL;
296 
297 	mlen = data_len - 8 - 16;
298 
299 	os_memset(aad, 0, sizeof(aad));
300 	ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad,
301                    &aad_len, nonce);
302 	wpa_hexdump(MSG_DEBUG, "CCMP-256 AAD", aad, aad_len);
303 	wpa_hexdump(MSG_DEBUG, "CCMP-256 nonce", nonce, 13);
304 
305 	if (aes_ccm_ad(tk, 32, nonce, 16, data + 8, mlen, aad, aad_len,
306 		       data + 8 + mlen, plain) < 0) {
307 		os_free(plain);
308 		return NULL;
309 	}
310 	wpa_hexdump(MSG_DEBUG, "CCMP-256 decrypted", plain, mlen);
311 
312 	*decrypted_len = mlen;
313 	return plain;
314 }
315 
316 
ccmp_256_encrypt(const u8 * tk,u8 * frame,size_t len,size_t hdrlen,u8 * pn,int keyid,size_t * encrypted_len)317 u8 * ccmp_256_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen,
318 		      u8 *pn, int keyid, size_t *encrypted_len)
319 {
320 	u8 aad[30], nonce[13];
321 	size_t aad_len, plen;
322 	u8 *crypt, *pos;
323 	struct ieee80211_hdr *hdr;
324 
325 	if (len < hdrlen || hdrlen < 24)
326 		return NULL;
327 	plen = len - hdrlen;
328 
329 	crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
330 	if (crypt == NULL)
331 		return NULL;
332 
333 	os_memcpy(crypt, frame, hdrlen);
334 	hdr = (struct ieee80211_hdr *) crypt;
335 	hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
336 	pos = crypt + hdrlen;
337 	*pos++ = pn[5]; /* PN0 */
338 	*pos++ = pn[4]; /* PN1 */
339 	*pos++ = 0x00; /* Rsvd */
340 	*pos++ = 0x20 | (keyid << 6);
341 	*pos++ = pn[3]; /* PN2 */
342 	*pos++ = pn[2]; /* PN3 */
343 	*pos++ = pn[1]; /* PN4 */
344 	*pos++ = pn[0]; /* PN5 */
345 
346 	os_memset(aad, 0, sizeof(aad));
347 	ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
348 	wpa_hexdump(MSG_DEBUG, "CCMP-256 AAD", aad, aad_len);
349 	wpa_hexdump(MSG_DEBUG, "CCMP-256 nonce", nonce, 13);
350 
351 	if (aes_ccm_ae(tk, 32, nonce, 16, frame + hdrlen, plen, aad, aad_len,
352 		       pos, pos + plen) < 0) {
353 		os_free(crypt);
354 		return NULL;
355 	}
356 
357 	wpa_hexdump(MSG_DEBUG, "CCMP-256 encrypted", crypt + hdrlen + 8,
358 		    plen);
359 
360 	*encrypted_len = hdrlen + 8 + plen + 16;
361 
362 	return crypt;
363 }
364 #endif /* CONFIG_IEEE80211W */
365