wlantest: Remove CCMP specific AAD handling from GCMP
[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 "wlantest.h"
15
16
17 static void inc32(u8 *block)
18 {
19         u32 val;
20         val = WPA_GET_BE32(block + AES_BLOCK_SIZE - 4);
21         val++;
22         WPA_PUT_BE32(block + AES_BLOCK_SIZE - 4, val);
23 }
24
25
26 static void xor_block(u8 *dst, const u8 *src)
27 {
28         u32 *d = (u32 *) dst;
29         u32 *s = (u32 *) src;
30         *d++ ^= *s++;
31         *d++ ^= *s++;
32         *d++ ^= *s++;
33         *d++ ^= *s++;
34 }
35
36
37 static void shift_right_block(u8 *v)
38 {
39         u32 val;
40
41         val = WPA_GET_BE32(v + 12);
42         val >>= 1;
43         if (v[11] & 0x01)
44                 val |= 0x80000000;
45         WPA_PUT_BE32(v + 12, val);
46
47         val = WPA_GET_BE32(v + 8);
48         val >>= 1;
49         if (v[7] & 0x01)
50                 val |= 0x80000000;
51         WPA_PUT_BE32(v + 8, val);
52
53         val = WPA_GET_BE32(v + 4);
54         val >>= 1;
55         if (v[3] & 0x01)
56                 val |= 0x80000000;
57         WPA_PUT_BE32(v + 4, val);
58
59         val = WPA_GET_BE32(v);
60         val >>= 1;
61         WPA_PUT_BE32(v, val);
62 }
63
64
65 /* Multiplication in GF(2^128) */
66 static void gf_mult(const u8 *x, const u8 *y, u8 *z)
67 {
68         u8 v[16];
69         int i, j;
70
71         os_memset(z, 0, 16); /* Z_0 = 0^128 */
72         os_memcpy(v, y, 16); /* V_0 = Y */
73
74         for (i = 0; i < 16; i++) {
75                 for (j = 0; j < 8; j++) {
76                         if (x[i] & BIT(7 - j)) {
77                                 /* Z_(i + 1) = Z_i XOR V_i */
78                                 xor_block(z, v);
79                         } else {
80                                 /* Z_(i + 1) = Z_i */
81                         }
82
83                         if (v[15] & 0x01) {
84                                 /* V_(i + 1) = (V_i >> 1) XOR R */
85                                 shift_right_block(v);
86                                 /* R = 11100001 || 0^120 */
87                                 v[0] ^= 0xe1;
88                         } else {
89                                 /* V_(i + 1) = V_i >> 1 */
90                                 shift_right_block(v);
91                         }
92                 }
93         }
94 }
95
96
97 static void ghash(const u8 *h, const u8 *x, size_t xlen, u8 *y)
98 {
99         size_t m, i;
100         const u8 *xpos = x;
101         u8 tmp[16];
102
103         m = xlen / 16;
104
105         /* Y_0 = 0^128 */
106         os_memset(y, 0, 16);
107         for (i = 0; i < m; i++) {
108                 /* Y_i = (Y^(i-1) XOR X_i) dot H */
109                 xor_block(y, xpos);
110                 xpos += 16;
111
112                 /* dot operation:
113                  * multiplication operation for binary Galois (finite) field of
114                  * 2^128 elements */
115                 gf_mult(y, h, tmp);
116                 os_memcpy(y, tmp, 16);
117         }
118
119         /* Return Y_m */
120 }
121
122
123 static void aes_gctr(void *aes, const u8 *icb, const u8 *x, size_t xlen, u8 *y)
124 {
125         size_t i, n, last;
126         u8 cb[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
127         const u8 *xpos = x;
128         u8 *ypos = y;
129
130         if (xlen == 0)
131                 return;
132
133         n = xlen / 16;
134
135         os_memcpy(cb, icb, AES_BLOCK_SIZE);
136         /* Full blocks */
137         for (i = 0; i < n; i++) {
138                 aes_encrypt(aes, cb, ypos);
139                 xor_block(ypos, xpos);
140                 xpos += AES_BLOCK_SIZE;
141                 ypos += AES_BLOCK_SIZE;
142                 inc32(cb);
143         }
144
145         last = x + xlen - xpos;
146         if (last) {
147                 /* Last, partial block */
148                 aes_encrypt(aes, cb, tmp);
149                 for (i = 0; i < last; i++)
150                         *ypos++ = *xpos++ ^ tmp[i];
151         }
152 }
153
154
155 /**
156  * aes_gcm_ae - GCM-AE_K(IV, P, A) with len(IV) = 96
157  */
158 static int aes_gcm_ae(const u8 *key, const u8 *iv,
159                       const u8 *plain, size_t plain_len,
160                       const u8 *aad, size_t aad_len,
161                       u8 *crypt, u8 *tag)
162 {
163         u8 *auth, *apos;
164         u8 H[AES_BLOCK_SIZE];
165         u8 J0[AES_BLOCK_SIZE];
166         u8 S[16];
167         void *aes;
168         size_t padlen;
169         size_t iv_len = 12;
170
171         aes = aes_encrypt_init(key, 16);
172         if (aes == NULL)
173                 return -1;
174
175         /* 1. Generate hash subkey H = AES_K(0^128) */
176         os_memset(H, 0, sizeof(H));
177         aes_encrypt(aes, H, H);
178         wpa_hexdump_key(MSG_EXCESSIVE, "Hash subkey H for GHASH", H, sizeof(H));
179
180         /* 2. Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
181         os_memcpy(J0, iv, iv_len);
182         os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
183         J0[AES_BLOCK_SIZE - 1] = 0x01;
184
185         /* 3. C = GCTR_K(inc_32(J_0), P) */
186         inc32(J0);
187         aes_gctr(aes, J0, plain, plain_len, crypt);
188
189         /*
190          * 4. u = 128 * ceil[len(C)/128] - len(C)
191          *    v = 128 * ceil[len(A)/128] - len(A)
192          * 5. S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
193          * (i.e., zero padded to block size A || C and lengths of each in bits)
194          */
195         auth = os_malloc(32 + 16 + plain_len + 8 + 8);
196         if (auth == NULL) {
197                 aes_encrypt_deinit(aes);
198                 return -1;
199         }
200
201         apos = auth;
202
203         /* Zero-padded AAD */
204         os_memcpy(apos, aad, aad_len);
205         apos += aad_len;
206         padlen = (16 - aad_len % 16) % 16;
207         os_memset(apos, 0, padlen);
208         apos += padlen;
209
210         /* Zero-padded C */
211         os_memcpy(apos, crypt, plain_len);
212         apos += plain_len;
213         padlen = (16 - plain_len % 16) % 16;
214         os_memset(apos, 0, padlen);
215         apos += padlen;
216
217         /* Length of AAD and C in bits */
218         WPA_PUT_BE64(apos, aad_len * 8);
219         apos += 8;
220         WPA_PUT_BE64(apos, plain_len * 8);
221         apos += 8;
222
223         wpa_hexdump_key(MSG_EXCESSIVE, "GHASH_H input", auth, apos - auth);
224         ghash(H, auth, apos - auth, S);
225         wpa_hexdump_key(MSG_EXCESSIVE, "S = GHASH_H(...)", S, 16);
226         os_free(auth);
227
228         /* 6. T = MSB_t(GCTR_K(J_0, S)) */
229         J0[AES_BLOCK_SIZE - 1] = 0x01;
230         aes_gctr(aes, J0, S, sizeof(S), tag);
231
232         /* 7. Return (C, T) */
233
234         aes_encrypt_deinit(aes);
235
236         return 0;
237 }
238
239
240 /**
241  * aes_gcm_ad - GCM-AD_K(IV, C, A, T) with len(IV) = 96
242  */
243 static int aes_gcm_ad(const u8 *key, const u8 *iv,
244                       const u8 *crypt, size_t crypt_len,
245                       const u8 *aad, size_t aad_len, const u8 *tag,
246                       u8 *plain)
247 {
248         u8 *auth, *apos;
249         u8 H[AES_BLOCK_SIZE];
250         u8 J0[AES_BLOCK_SIZE];
251         u8 S[16], T[16];
252         void *aes;
253         size_t padlen;
254         size_t iv_len = 12;
255
256         aes = aes_encrypt_init(key, 16);
257         if (aes == NULL)
258                 return -1;
259
260         /* 2. Generate hash subkey H = AES_K(0^128) */
261         os_memset(H, 0, sizeof(H));
262         aes_encrypt(aes, H, H);
263         wpa_hexdump(MSG_EXCESSIVE, "Hash subkey H for GHASH", H, sizeof(H));
264
265         /* 3. Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
266         os_memcpy(J0, iv, iv_len);
267         os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
268         J0[AES_BLOCK_SIZE - 1] = 0x01;
269
270         /* 4. C = GCTR_K(inc_32(J_0), C) */
271         inc32(J0);
272         aes_gctr(aes, J0, crypt, crypt_len, plain);
273
274         /*
275          * 5. u = 128 * ceil[len(C)/128] - len(C)
276          *    v = 128 * ceil[len(A)/128] - len(A)
277          * 6. S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
278          * (i.e., zero padded to block size A || C and lengths of each in bits)
279          */
280         auth = os_malloc(32 + 16 + crypt_len + 8 + 8);
281         if (auth == NULL) {
282                 aes_encrypt_deinit(aes);
283                 return -1;
284         }
285
286         apos = auth;
287
288         /* Zero-padded AAD */
289         os_memcpy(apos, aad, aad_len);
290         apos += aad_len;
291         padlen = (16 - aad_len % 16) % 16;
292         os_memset(apos, 0, padlen);
293         apos += padlen;
294
295         /* Zero-padded C */
296         os_memcpy(apos, crypt, crypt_len);
297         apos += crypt_len;
298         padlen = (16 - crypt_len % 16) % 16;
299         os_memset(apos, 0, padlen);
300         apos += padlen;
301
302         /* Length of AAD and C in bits */
303         WPA_PUT_BE64(apos, aad_len * 8);
304         apos += 8;
305         WPA_PUT_BE64(apos, crypt_len * 8);
306         apos += 8;
307
308         wpa_hexdump(MSG_EXCESSIVE, "GHASH_H input", auth, apos - auth);
309         ghash(H, auth, apos - auth, S);
310         wpa_hexdump(MSG_EXCESSIVE, "S = GHASH_H(...)", S, 16);
311         os_free(auth);
312
313         /* 7. T' = MSB_t(GCTR_K(J_0, S)) */
314         J0[AES_BLOCK_SIZE - 1] = 0x01;
315         aes_gctr(aes, J0, S, sizeof(S), T);
316
317         aes_encrypt_deinit(aes);
318
319         if (os_memcmp(tag, T, 16) != 0) {
320                 wpa_printf(MSG_EXCESSIVE, "GCM: Tag mismatch");
321                 return -1;
322         }
323
324         return 0;
325 }
326
327
328 static void gcmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data,
329                            u8 *aad, size_t *aad_len, u8 *nonce)
330 {
331         u16 fc, stype, seq;
332         int qos = 0, addr4 = 0;
333         u8 *pos;
334
335         fc = le_to_host16(hdr->frame_control);
336         stype = WLAN_FC_GET_STYPE(fc);
337         if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
338             (WLAN_FC_TODS | WLAN_FC_FROMDS))
339                 addr4 = 1;
340
341         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
342                 fc &= ~0x0070; /* Mask subtype bits */
343                 if (stype & 0x08) {
344                         const u8 *qc;
345                         qos = 1;
346                         fc &= ~WLAN_FC_ORDER;
347                         qc = (const u8 *) (hdr + 1);
348                         if (addr4)
349                                 qc += ETH_ALEN;
350                 }
351         }
352
353         fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
354         fc |= WLAN_FC_ISWEP;
355         WPA_PUT_LE16(aad, fc);
356         pos = aad + 2;
357         os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
358         pos += 3 * ETH_ALEN;
359         seq = le_to_host16(hdr->seq_ctrl);
360         seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
361         WPA_PUT_LE16(pos, seq);
362         pos += 2;
363
364         os_memcpy(pos, hdr + 1, addr4 * ETH_ALEN + qos * 2);
365         pos += addr4 * ETH_ALEN;
366         if (qos) {
367                 pos[0] &= ~0x70;
368                 if (1 /* FIX: either device has SPP A-MSDU Capab = 0 */)
369                         pos[0] &= ~0x80;
370                 pos++;
371                 *pos++ = 0x00;
372         }
373
374         *aad_len = pos - aad;
375
376         os_memcpy(nonce, hdr->addr2, ETH_ALEN);
377         nonce[6] = data[7]; /* PN5 */
378         nonce[7] = data[6]; /* PN4 */
379         nonce[8] = data[5]; /* PN3 */
380         nonce[9] = data[4]; /* PN2 */
381         nonce[10] = data[1]; /* PN1 */
382         nonce[11] = data[0]; /* PN0 */
383 }
384
385
386 u8 * gcmp_decrypt(const u8 *tk, const struct ieee80211_hdr *hdr,
387                   const u8 *data, size_t data_len, size_t *decrypted_len)
388 {
389         u8 aad[30], nonce[12], *plain;
390         size_t aad_len, mlen;
391         const u8 *m;
392
393         if (data_len < 8 + 16)
394                 return NULL;
395
396         plain = os_malloc(data_len + AES_BLOCK_SIZE);
397         if (plain == NULL)
398                 return NULL;
399
400         m = data + 8;
401         mlen = data_len - 8 - 16;
402
403         os_memset(aad, 0, sizeof(aad));
404         gcmp_aad_nonce(hdr, data, aad, &aad_len, nonce);
405         wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
406         wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
407
408         if (aes_gcm_ad(tk, nonce, m, mlen, aad, aad_len, m + mlen, plain) <
409             0) {
410                 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
411                 wpa_printf(MSG_INFO, "Invalid GCMP frame: A1=" MACSTR
412                            " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
413                            MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
414                            MAC2STR(hdr->addr3),
415                            WLAN_GET_SEQ_SEQ(seq_ctrl),
416                            WLAN_GET_SEQ_FRAG(seq_ctrl));
417                 os_free(plain);
418                 return NULL;
419         }
420
421         *decrypted_len = mlen;
422         return plain;
423 }
424
425
426 u8 * gcmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *qos,
427                   u8 *pn, int keyid, size_t *encrypted_len)
428 {
429         u8 aad[30], nonce[12], *crypt, *pos;
430         size_t aad_len, plen;
431         struct ieee80211_hdr *hdr;
432
433         if (len < hdrlen || hdrlen < 24)
434                 return NULL;
435         plen = len - hdrlen;
436
437         crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
438         if (crypt == NULL)
439                 return NULL;
440
441         os_memcpy(crypt, frame, hdrlen);
442         hdr = (struct ieee80211_hdr *) crypt;
443         hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
444         pos = crypt + hdrlen;
445         *pos++ = pn[5]; /* PN0 */
446         *pos++ = pn[4]; /* PN1 */
447         *pos++ = 0x00; /* Rsvd */
448         *pos++ = 0x20 | (keyid << 6);
449         *pos++ = pn[3]; /* PN2 */
450         *pos++ = pn[2]; /* PN3 */
451         *pos++ = pn[1]; /* PN4 */
452         *pos++ = pn[0]; /* PN5 */
453
454         os_memset(aad, 0, sizeof(aad));
455         gcmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce);
456         wpa_hexdump(MSG_EXCESSIVE, "GCMP AAD", aad, aad_len);
457         wpa_hexdump(MSG_EXCESSIVE, "GCMP nonce", nonce, sizeof(nonce));
458
459         if (aes_gcm_ae(tk, nonce, frame + hdrlen, plen, aad, aad_len,
460                        pos, pos + plen) < 0) {
461                 os_free(crypt);
462                 return NULL;
463         }
464
465         wpa_hexdump(MSG_EXCESSIVE, "GCMP MIC", pos + plen, 16);
466         wpa_hexdump(MSG_EXCESSIVE, "GCMP encrypted", pos, plen);
467
468         *encrypted_len = hdrlen + 8 + plen + 16;
469
470         return crypt;
471 }