xref: /src/sys/net80211/ieee80211_crypto_gcm.c (revision 2d4583c462a5d7904e2bb4c77f521d16fd2e7140)
12d4583c4SAdrian Chadd /*-
22d4583c4SAdrian Chadd  * SPDX-License-Identifier: BSD-2-Clause
32d4583c4SAdrian Chadd  *
42d4583c4SAdrian Chadd  * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
52d4583c4SAdrian Chadd  * All rights reserved.
62d4583c4SAdrian Chadd  *
72d4583c4SAdrian Chadd  * Galois/Counter Mode (GCM) and GMAC with AES
82d4583c4SAdrian Chadd  *
92d4583c4SAdrian Chadd  * Originally sourced from hostapd 2.11 (src/crypto/aes-gcm.c).
102d4583c4SAdrian Chadd  *
112d4583c4SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
122d4583c4SAdrian Chadd  * modification, are permitted provided that the following conditions
132d4583c4SAdrian Chadd  * are met:
142d4583c4SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
152d4583c4SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
162d4583c4SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
172d4583c4SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
182d4583c4SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
192d4583c4SAdrian Chadd  *
202d4583c4SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
212d4583c4SAdrian Chadd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
222d4583c4SAdrian Chadd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
232d4583c4SAdrian Chadd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
242d4583c4SAdrian Chadd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
252d4583c4SAdrian Chadd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
262d4583c4SAdrian Chadd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
272d4583c4SAdrian Chadd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
282d4583c4SAdrian Chadd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
292d4583c4SAdrian Chadd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
302d4583c4SAdrian Chadd  */
312d4583c4SAdrian Chadd 
322d4583c4SAdrian Chadd #include "opt_wlan.h"
332d4583c4SAdrian Chadd 
342d4583c4SAdrian Chadd #include <sys/param.h>
352d4583c4SAdrian Chadd #include <sys/systm.h>
362d4583c4SAdrian Chadd #include <sys/kernel.h>
372d4583c4SAdrian Chadd 
382d4583c4SAdrian Chadd #include <crypto/rijndael/rijndael.h>
392d4583c4SAdrian Chadd #include <net80211/ieee80211_crypto_gcm.h>
402d4583c4SAdrian Chadd 
412d4583c4SAdrian Chadd #define AES_BLOCK_LEN 16
422d4583c4SAdrian Chadd 
432d4583c4SAdrian Chadd #define BIT(x)	(1U << (x))
442d4583c4SAdrian Chadd 
452d4583c4SAdrian Chadd static __inline void
xor_block(uint8_t * b,const uint8_t * a,size_t len)462d4583c4SAdrian Chadd xor_block(uint8_t *b, const uint8_t *a, size_t len)
472d4583c4SAdrian Chadd {
482d4583c4SAdrian Chadd 	int i;
492d4583c4SAdrian Chadd 	for (i = 0; i < len; i++)
502d4583c4SAdrian Chadd 		b[i] ^= a[i];
512d4583c4SAdrian Chadd }
522d4583c4SAdrian Chadd 
532d4583c4SAdrian Chadd static inline
WPA_PUT_BE64(uint8_t * a,uint64_t val)542d4583c4SAdrian Chadd void WPA_PUT_BE64(uint8_t *a, uint64_t val)
552d4583c4SAdrian Chadd {
562d4583c4SAdrian Chadd 	a[0] = val >> 56;
572d4583c4SAdrian Chadd 	a[1] = val >> 48;
582d4583c4SAdrian Chadd 	a[2] = val >> 40;
592d4583c4SAdrian Chadd 	a[3] = val >> 32;
602d4583c4SAdrian Chadd 	a[4] = val >> 24;
612d4583c4SAdrian Chadd 	a[5] = val >> 16;
622d4583c4SAdrian Chadd 	a[6] = val >> 8;
632d4583c4SAdrian Chadd 	a[7] = val & 0xff;
642d4583c4SAdrian Chadd }
652d4583c4SAdrian Chadd 
662d4583c4SAdrian Chadd static inline void
WPA_PUT_BE32(uint8_t * a,uint32_t val)672d4583c4SAdrian Chadd WPA_PUT_BE32(uint8_t *a, uint32_t val)
682d4583c4SAdrian Chadd {
692d4583c4SAdrian Chadd 	a[0] = (val >> 24) & 0xff;
702d4583c4SAdrian Chadd 	a[1] = (val >> 16) & 0xff;
712d4583c4SAdrian Chadd 	a[2] = (val >> 8) & 0xff;
722d4583c4SAdrian Chadd 	a[3] = val & 0xff;
732d4583c4SAdrian Chadd }
742d4583c4SAdrian Chadd 
752d4583c4SAdrian Chadd static inline uint32_t
WPA_GET_BE32(const uint8_t * a)762d4583c4SAdrian Chadd WPA_GET_BE32(const uint8_t *a)
772d4583c4SAdrian Chadd {
782d4583c4SAdrian Chadd 	return (((uint32_t) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
792d4583c4SAdrian Chadd }
802d4583c4SAdrian Chadd 
812d4583c4SAdrian Chadd static void
inc32(uint8_t * block)822d4583c4SAdrian Chadd inc32(uint8_t *block)
832d4583c4SAdrian Chadd {
842d4583c4SAdrian Chadd 	uint32_t val;
852d4583c4SAdrian Chadd 
862d4583c4SAdrian Chadd 	val = WPA_GET_BE32(block + AES_BLOCK_LEN - 4);
872d4583c4SAdrian Chadd 	val++;
882d4583c4SAdrian Chadd 	WPA_PUT_BE32(block + AES_BLOCK_LEN - 4, val);
892d4583c4SAdrian Chadd }
902d4583c4SAdrian Chadd 
912d4583c4SAdrian Chadd static void
shift_right_block(uint8_t * v)922d4583c4SAdrian Chadd shift_right_block(uint8_t *v)
932d4583c4SAdrian Chadd {
942d4583c4SAdrian Chadd 	uint32_t val;
952d4583c4SAdrian Chadd 
962d4583c4SAdrian Chadd 	val = WPA_GET_BE32(v + 12);
972d4583c4SAdrian Chadd 	val >>= 1;
982d4583c4SAdrian Chadd 	if (v[11] & 0x01)
992d4583c4SAdrian Chadd 		val |= 0x80000000;
1002d4583c4SAdrian Chadd 	WPA_PUT_BE32(v + 12, val);
1012d4583c4SAdrian Chadd 
1022d4583c4SAdrian Chadd 	val = WPA_GET_BE32(v + 8);
1032d4583c4SAdrian Chadd 	val >>= 1;
1042d4583c4SAdrian Chadd 	if (v[7] & 0x01)
1052d4583c4SAdrian Chadd 		val |= 0x80000000;
1062d4583c4SAdrian Chadd 	WPA_PUT_BE32(v + 8, val);
1072d4583c4SAdrian Chadd 
1082d4583c4SAdrian Chadd 	val = WPA_GET_BE32(v + 4);
1092d4583c4SAdrian Chadd 	val >>= 1;
1102d4583c4SAdrian Chadd 	if (v[3] & 0x01)
1112d4583c4SAdrian Chadd 		val |= 0x80000000;
1122d4583c4SAdrian Chadd 	WPA_PUT_BE32(v + 4, val);
1132d4583c4SAdrian Chadd 
1142d4583c4SAdrian Chadd 	val = WPA_GET_BE32(v);
1152d4583c4SAdrian Chadd 	val >>= 1;
1162d4583c4SAdrian Chadd 	WPA_PUT_BE32(v, val);
1172d4583c4SAdrian Chadd }
1182d4583c4SAdrian Chadd 
1192d4583c4SAdrian Chadd 
1202d4583c4SAdrian Chadd /* Multiplication in GF(2^128) */
1212d4583c4SAdrian Chadd static void
gf_mult(const uint8_t * x,const uint8_t * y,uint8_t * z)1222d4583c4SAdrian Chadd gf_mult(const uint8_t *x, const uint8_t *y, uint8_t *z)
1232d4583c4SAdrian Chadd {
1242d4583c4SAdrian Chadd 	uint8_t v[16];
1252d4583c4SAdrian Chadd 	int i, j;
1262d4583c4SAdrian Chadd 
1272d4583c4SAdrian Chadd 	memset(z, 0, 16); /* Z_0 = 0^128 */
1282d4583c4SAdrian Chadd 	memcpy(v, y, 16); /* V_0 = Y */
1292d4583c4SAdrian Chadd 
1302d4583c4SAdrian Chadd 	for (i = 0; i < 16; i++) {
1312d4583c4SAdrian Chadd 		for (j = 0; j < 8; j++) {
1322d4583c4SAdrian Chadd 			if (x[i] & BIT(7 - j)) {
1332d4583c4SAdrian Chadd 				/* Z_(i + 1) = Z_i XOR V_i */
1342d4583c4SAdrian Chadd 				xor_block(z, v, AES_BLOCK_LEN);
1352d4583c4SAdrian Chadd 			} else {
1362d4583c4SAdrian Chadd 				/* Z_(i + 1) = Z_i */
1372d4583c4SAdrian Chadd 			}
1382d4583c4SAdrian Chadd 
1392d4583c4SAdrian Chadd 			if (v[15] & 0x01) {
1402d4583c4SAdrian Chadd 				/* V_(i + 1) = (V_i >> 1) XOR R */
1412d4583c4SAdrian Chadd 				shift_right_block(v);
1422d4583c4SAdrian Chadd 				/* R = 11100001 || 0^120 */
1432d4583c4SAdrian Chadd 				v[0] ^= 0xe1;
1442d4583c4SAdrian Chadd 			} else {
1452d4583c4SAdrian Chadd 				/* V_(i + 1) = V_i >> 1 */
1462d4583c4SAdrian Chadd 				shift_right_block(v);
1472d4583c4SAdrian Chadd 			}
1482d4583c4SAdrian Chadd 		}
1492d4583c4SAdrian Chadd 	}
1502d4583c4SAdrian Chadd }
1512d4583c4SAdrian Chadd 
1522d4583c4SAdrian Chadd static void
ghash_start(uint8_t * y)1532d4583c4SAdrian Chadd ghash_start(uint8_t *y)
1542d4583c4SAdrian Chadd {
1552d4583c4SAdrian Chadd 	/* Y_0 = 0^128 */
1562d4583c4SAdrian Chadd 	memset(y, 0, 16);
1572d4583c4SAdrian Chadd }
1582d4583c4SAdrian Chadd 
1592d4583c4SAdrian Chadd static void
ghash(const uint8_t * h,const uint8_t * x,size_t xlen,uint8_t * y)1602d4583c4SAdrian Chadd ghash(const uint8_t *h, const uint8_t *x, size_t xlen, uint8_t *y)
1612d4583c4SAdrian Chadd {
1622d4583c4SAdrian Chadd 	size_t m, i;
1632d4583c4SAdrian Chadd 	const uint8_t *xpos = x;
1642d4583c4SAdrian Chadd 	uint8_t tmp[16];
1652d4583c4SAdrian Chadd 
1662d4583c4SAdrian Chadd 	m = xlen / 16;
1672d4583c4SAdrian Chadd 
1682d4583c4SAdrian Chadd 	for (i = 0; i < m; i++) {
1692d4583c4SAdrian Chadd 		/* Y_i = (Y^(i-1) XOR X_i) dot H */
1702d4583c4SAdrian Chadd 		xor_block(y, xpos, AES_BLOCK_LEN);
1712d4583c4SAdrian Chadd 		xpos += 16;
1722d4583c4SAdrian Chadd 
1732d4583c4SAdrian Chadd 		/* dot operation:
1742d4583c4SAdrian Chadd 		 * multiplication operation for binary Galois (finite) field of
1752d4583c4SAdrian Chadd 		 * 2^128 elements */
1762d4583c4SAdrian Chadd 		gf_mult(y, h, tmp);
1772d4583c4SAdrian Chadd 		memcpy(y, tmp, 16);
1782d4583c4SAdrian Chadd 	}
1792d4583c4SAdrian Chadd 
1802d4583c4SAdrian Chadd 	if (x + xlen > xpos) {
1812d4583c4SAdrian Chadd 		/* Add zero padded last block */
1822d4583c4SAdrian Chadd 		size_t last = x + xlen - xpos;
1832d4583c4SAdrian Chadd 		memcpy(tmp, xpos, last);
1842d4583c4SAdrian Chadd 		memset(tmp + last, 0, sizeof(tmp) - last);
1852d4583c4SAdrian Chadd 
1862d4583c4SAdrian Chadd 		/* Y_i = (Y^(i-1) XOR X_i) dot H */
1872d4583c4SAdrian Chadd 		xor_block(y, tmp, AES_BLOCK_LEN);
1882d4583c4SAdrian Chadd 
1892d4583c4SAdrian Chadd 		/* dot operation:
1902d4583c4SAdrian Chadd 		 * multiplication operation for binary Galois (finite) field of
1912d4583c4SAdrian Chadd 		 * 2^128 elements */
1922d4583c4SAdrian Chadd 		gf_mult(y, h, tmp);
1932d4583c4SAdrian Chadd 		memcpy(y, tmp, 16);
1942d4583c4SAdrian Chadd 	}
1952d4583c4SAdrian Chadd 
1962d4583c4SAdrian Chadd 	/* Return Y_m */
1972d4583c4SAdrian Chadd }
1982d4583c4SAdrian Chadd 
1992d4583c4SAdrian Chadd /*
2002d4583c4SAdrian Chadd  * Execute the GCTR call with the counter block icb
2012d4583c4SAdrian Chadd  * on payload x (size len), output into y.
2022d4583c4SAdrian Chadd  */
2032d4583c4SAdrian Chadd static void
aes_gctr(rijndael_ctx * aes,const uint8_t * icb,const uint8_t * x,size_t xlen,uint8_t * y)2042d4583c4SAdrian Chadd aes_gctr(rijndael_ctx *aes, const uint8_t *icb,
2052d4583c4SAdrian Chadd     const uint8_t *x, size_t xlen, uint8_t *y)
2062d4583c4SAdrian Chadd {
2072d4583c4SAdrian Chadd 	size_t i, n, last;
2082d4583c4SAdrian Chadd 	uint8_t cb[AES_BLOCK_LEN], tmp[AES_BLOCK_LEN];
2092d4583c4SAdrian Chadd 	const uint8_t *xpos = x;
2102d4583c4SAdrian Chadd 	uint8_t *ypos = y;
2112d4583c4SAdrian Chadd 
2122d4583c4SAdrian Chadd 	if (xlen == 0)
2132d4583c4SAdrian Chadd 		return;
2142d4583c4SAdrian Chadd 
2152d4583c4SAdrian Chadd 	n = xlen / 16;
2162d4583c4SAdrian Chadd 
2172d4583c4SAdrian Chadd 	memcpy(cb, icb, AES_BLOCK_LEN);
2182d4583c4SAdrian Chadd 
2192d4583c4SAdrian Chadd 	/* Full blocks */
2202d4583c4SAdrian Chadd 	for (i = 0; i < n; i++) {
2212d4583c4SAdrian Chadd 		rijndael_encrypt(aes, cb, ypos);
2222d4583c4SAdrian Chadd 		xor_block(ypos, xpos, AES_BLOCK_LEN);
2232d4583c4SAdrian Chadd 		xpos += AES_BLOCK_LEN;
2242d4583c4SAdrian Chadd 		ypos += AES_BLOCK_LEN;
2252d4583c4SAdrian Chadd 		inc32(cb);
2262d4583c4SAdrian Chadd 	}
2272d4583c4SAdrian Chadd 
2282d4583c4SAdrian Chadd 	last = x + xlen - xpos;
2292d4583c4SAdrian Chadd 	if (last) {
2302d4583c4SAdrian Chadd 		/* Last, partial block */
2312d4583c4SAdrian Chadd 		rijndael_encrypt(aes, cb, tmp);
2322d4583c4SAdrian Chadd 		for (i = 0; i < last; i++)
2332d4583c4SAdrian Chadd 			*ypos++ = *xpos++ ^ tmp[i];
2342d4583c4SAdrian Chadd 	}
2352d4583c4SAdrian Chadd }
2362d4583c4SAdrian Chadd 
2372d4583c4SAdrian Chadd static void
aes_gcm_init_hash_subkey(rijndael_ctx * aes,uint8_t * H)2382d4583c4SAdrian Chadd aes_gcm_init_hash_subkey(rijndael_ctx *aes, uint8_t *H)
2392d4583c4SAdrian Chadd {
2402d4583c4SAdrian Chadd 	/* Generate hash subkey H = AES_K(0^128) */
2412d4583c4SAdrian Chadd 	memset(H, 0, AES_BLOCK_LEN);
2422d4583c4SAdrian Chadd 
2432d4583c4SAdrian Chadd 	rijndael_encrypt(aes, H, H);
2442d4583c4SAdrian Chadd }
2452d4583c4SAdrian Chadd 
2462d4583c4SAdrian Chadd static void
aes_gcm_prepare_j0(const uint8_t * iv,size_t iv_len,const uint8_t * H,uint8_t * J0)2472d4583c4SAdrian Chadd aes_gcm_prepare_j0(const uint8_t *iv, size_t iv_len, const uint8_t *H,
2482d4583c4SAdrian Chadd     uint8_t *J0)
2492d4583c4SAdrian Chadd {
2502d4583c4SAdrian Chadd 	uint8_t len_buf[16];
2512d4583c4SAdrian Chadd 
2522d4583c4SAdrian Chadd 	if (iv_len == 12) {
2532d4583c4SAdrian Chadd 		/* Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
2542d4583c4SAdrian Chadd 		memcpy(J0, iv, iv_len);
2552d4583c4SAdrian Chadd 		memset(J0 + iv_len, 0, AES_BLOCK_LEN - iv_len);
2562d4583c4SAdrian Chadd 		J0[AES_BLOCK_LEN - 1] = 0x01;
2572d4583c4SAdrian Chadd 	} else {
2582d4583c4SAdrian Chadd 		/*
2592d4583c4SAdrian Chadd 		 * s = 128 * ceil(len(IV)/128) - len(IV)
2602d4583c4SAdrian Chadd 		 * J_0 = GHASH_H(IV || 0^(s+64) || [len(IV)]_64)
2612d4583c4SAdrian Chadd 		 */
2622d4583c4SAdrian Chadd 		ghash_start(J0);
2632d4583c4SAdrian Chadd 		ghash(H, iv, iv_len, J0);
2642d4583c4SAdrian Chadd 		WPA_PUT_BE64(len_buf, 0);
2652d4583c4SAdrian Chadd 		WPA_PUT_BE64(len_buf + 8, iv_len * 8);
2662d4583c4SAdrian Chadd 		ghash(H, len_buf, sizeof(len_buf), J0);
2672d4583c4SAdrian Chadd 	}
2682d4583c4SAdrian Chadd }
2692d4583c4SAdrian Chadd 
2702d4583c4SAdrian Chadd static void
aes_gcm_gctr(rijndael_ctx * aes,const uint8_t * J0,const uint8_t * in,size_t len,uint8_t * out)2712d4583c4SAdrian Chadd aes_gcm_gctr(rijndael_ctx *aes, const uint8_t *J0, const uint8_t *in,
2722d4583c4SAdrian Chadd     size_t len, uint8_t *out)
2732d4583c4SAdrian Chadd {
2742d4583c4SAdrian Chadd 	uint8_t J0inc[AES_BLOCK_LEN];
2752d4583c4SAdrian Chadd 
2762d4583c4SAdrian Chadd 	if (len == 0)
2772d4583c4SAdrian Chadd 		return;
2782d4583c4SAdrian Chadd 
2792d4583c4SAdrian Chadd 	memcpy(J0inc, J0, AES_BLOCK_LEN);
2802d4583c4SAdrian Chadd 	inc32(J0inc);
2812d4583c4SAdrian Chadd 
2822d4583c4SAdrian Chadd 	aes_gctr(aes, J0inc, in, len, out);
2832d4583c4SAdrian Chadd }
2842d4583c4SAdrian Chadd 
2852d4583c4SAdrian Chadd static void
aes_gcm_ghash(const uint8_t * H,const uint8_t * aad,size_t aad_len,const uint8_t * crypt,size_t crypt_len,uint8_t * S)2862d4583c4SAdrian Chadd aes_gcm_ghash(const uint8_t *H, const uint8_t *aad, size_t aad_len,
2872d4583c4SAdrian Chadd     const uint8_t *crypt, size_t crypt_len, uint8_t *S)
2882d4583c4SAdrian Chadd {
2892d4583c4SAdrian Chadd 	uint8_t len_buf[16];
2902d4583c4SAdrian Chadd 
2912d4583c4SAdrian Chadd 	/*
2922d4583c4SAdrian Chadd 	 * u = 128 * ceil[len(C)/128] - len(C)
2932d4583c4SAdrian Chadd 	 * v = 128 * ceil[len(A)/128] - len(A)
2942d4583c4SAdrian Chadd 	 * S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
2952d4583c4SAdrian Chadd 	 * (i.e., zero padded to block size A || C and lengths of each in bits)
2962d4583c4SAdrian Chadd 	 */
2972d4583c4SAdrian Chadd 	ghash_start(S);
2982d4583c4SAdrian Chadd 	ghash(H, aad, aad_len, S);
2992d4583c4SAdrian Chadd 	ghash(H, crypt, crypt_len, S);
3002d4583c4SAdrian Chadd 	WPA_PUT_BE64(len_buf, aad_len * 8);
3012d4583c4SAdrian Chadd 	WPA_PUT_BE64(len_buf + 8, crypt_len * 8);
3022d4583c4SAdrian Chadd 	ghash(H, len_buf, sizeof(len_buf), S);
3032d4583c4SAdrian Chadd }
3042d4583c4SAdrian Chadd 
3052d4583c4SAdrian Chadd /**
3062d4583c4SAdrian Chadd  * aes_gcm_ae - GCM-AE_K(IV, P, A)
3072d4583c4SAdrian Chadd  */
3082d4583c4SAdrian Chadd void
ieee80211_crypto_aes_gcm_ae(rijndael_ctx * aes,const uint8_t * iv,size_t iv_len,const uint8_t * plain,size_t plain_len,const uint8_t * aad,size_t aad_len,uint8_t * crypt,uint8_t * tag)3092d4583c4SAdrian Chadd ieee80211_crypto_aes_gcm_ae(rijndael_ctx *aes, const uint8_t *iv, size_t iv_len,
3102d4583c4SAdrian Chadd     const uint8_t *plain, size_t plain_len,
3112d4583c4SAdrian Chadd     const uint8_t *aad, size_t aad_len, uint8_t *crypt, uint8_t *tag)
3122d4583c4SAdrian Chadd {
3132d4583c4SAdrian Chadd 	uint8_t H[AES_BLOCK_LEN];
3142d4583c4SAdrian Chadd 	uint8_t J0[AES_BLOCK_LEN];
3152d4583c4SAdrian Chadd 	uint8_t S[GCMP_MIC_LEN];
3162d4583c4SAdrian Chadd 
3172d4583c4SAdrian Chadd 	aes_gcm_init_hash_subkey(aes, H);
3182d4583c4SAdrian Chadd 
3192d4583c4SAdrian Chadd 	aes_gcm_prepare_j0(iv, iv_len, H, J0);
3202d4583c4SAdrian Chadd 
3212d4583c4SAdrian Chadd 	/* C = GCTR_K(inc_32(J_0), P) */
3222d4583c4SAdrian Chadd 	aes_gcm_gctr(aes, J0, plain, plain_len, crypt);
3232d4583c4SAdrian Chadd 
3242d4583c4SAdrian Chadd 	aes_gcm_ghash(H, aad, aad_len, crypt, plain_len, S);
3252d4583c4SAdrian Chadd 
3262d4583c4SAdrian Chadd 	/* T = MSB_t(GCTR_K(J_0, S)) */
3272d4583c4SAdrian Chadd 	aes_gctr(aes, J0, S, sizeof(S), tag);
3282d4583c4SAdrian Chadd 
3292d4583c4SAdrian Chadd 	/* Return (C, T) */
3302d4583c4SAdrian Chadd }
3312d4583c4SAdrian Chadd 
3322d4583c4SAdrian Chadd /**
3332d4583c4SAdrian Chadd  * aes_gcm_ad - GCM-AD_K(IV, C, A, T)
3342d4583c4SAdrian Chadd  *
3352d4583c4SAdrian Chadd  * Return 0 if OK, -1 if decrypt failure.
3362d4583c4SAdrian Chadd  */
3372d4583c4SAdrian Chadd int
ieee80211_crypto_aes_gcm_ad(rijndael_ctx * aes,const uint8_t * iv,size_t iv_len,const uint8_t * crypt,size_t crypt_len,const uint8_t * aad,size_t aad_len,const uint8_t * tag,uint8_t * plain)3382d4583c4SAdrian Chadd ieee80211_crypto_aes_gcm_ad(rijndael_ctx *aes, const uint8_t *iv, size_t iv_len,
3392d4583c4SAdrian Chadd     const uint8_t *crypt, size_t crypt_len,
3402d4583c4SAdrian Chadd     const uint8_t *aad, size_t aad_len, const uint8_t *tag, uint8_t *plain)
3412d4583c4SAdrian Chadd {
3422d4583c4SAdrian Chadd 	uint8_t H[AES_BLOCK_LEN];
3432d4583c4SAdrian Chadd 	uint8_t J0[AES_BLOCK_LEN];
3442d4583c4SAdrian Chadd 	uint8_t S[16], T[GCMP_MIC_LEN];
3452d4583c4SAdrian Chadd 
3462d4583c4SAdrian Chadd 	aes_gcm_init_hash_subkey(aes, H);
3472d4583c4SAdrian Chadd 
3482d4583c4SAdrian Chadd 	aes_gcm_prepare_j0(iv, iv_len, H, J0);
3492d4583c4SAdrian Chadd 
3502d4583c4SAdrian Chadd 	/* P = GCTR_K(inc_32(J_0), C) */
3512d4583c4SAdrian Chadd 	aes_gcm_gctr(aes, J0, crypt, crypt_len, plain);
3522d4583c4SAdrian Chadd 
3532d4583c4SAdrian Chadd 	aes_gcm_ghash(H, aad, aad_len, crypt, crypt_len, S);
3542d4583c4SAdrian Chadd 
3552d4583c4SAdrian Chadd 	/* T' = MSB_t(GCTR_K(J_0, S)) */
3562d4583c4SAdrian Chadd 	aes_gctr(aes, J0, S, sizeof(S), T);
3572d4583c4SAdrian Chadd 
3582d4583c4SAdrian Chadd 	if (memcmp(tag, T, 16) != 0) {
3592d4583c4SAdrian Chadd 		return (-1);
3602d4583c4SAdrian Chadd 	}
3612d4583c4SAdrian Chadd 
3622d4583c4SAdrian Chadd 	return (0);
3632d4583c4SAdrian Chadd }
364