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