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