Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * FILS AEAD for (Re)Association Request/Response frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2016, Qualcomm Atheros, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "ieee80211_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "aes_cmac.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "fils_aead.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static void gf_mulx(u8 *pad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u64 a = get_unaligned_be64(pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	u64 b = get_unaligned_be64(pad + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	put_unaligned_be64((a << 1) | (b >> 63), pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	put_unaligned_be64((b << 1) ^ ((a >> 63) ? 0x87 : 0), pad + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int aes_s2v(struct crypto_shash *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		   size_t num_elem, const u8 *addr[], size_t len[], u8 *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8 d[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	SHASH_DESC_ON_STACK(desc, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	desc->tfm = tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	/* D = AES-CMAC(K, <zero>) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	crypto_shash_digest(desc, tmp, AES_BLOCK_SIZE, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	for (i = 0; i < num_elem - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		/* D = dbl(D) xor AES_CMAC(K, Si) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		gf_mulx(d); /* dbl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		crypto_shash_digest(desc, addr[i], len[i], tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		crypto_xor(d, tmp, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	crypto_shash_init(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (len[i] >= AES_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		/* len(Sn) >= 128 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		/* T = Sn xorend D */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		crypto_shash_update(desc, addr[i], len[i] - AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		crypto_xor(d, addr[i] + len[i] - AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			   AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		/* len(Sn) < 128 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		/* T = dbl(D) xor pad(Sn) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		gf_mulx(d); /* dbl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		crypto_xor(d, addr[i], len[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		d[len[i]] ^= 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	/* V = AES-CMAC(K, T) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	crypto_shash_finup(desc, d, AES_BLOCK_SIZE, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /* Note: addr[] and len[] needs to have one extra slot at the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static int aes_siv_encrypt(const u8 *key, size_t key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			   const u8 *plain, size_t plain_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			   size_t num_elem, const u8 *addr[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			   size_t len[], u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	u8 v[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct crypto_shash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct crypto_skcipher *tfm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct skcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct scatterlist src[1], dst[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u8 *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	key_len /= 2; /* S2V key || CTR key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	addr[num_elem] = plain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	len[num_elem] = plain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	num_elem++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* S2V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* K1 for S2V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	res = crypto_shash_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		res = aes_s2v(tfm, num_elem, addr, len, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	crypto_free_shash(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/* Use a temporary buffer of the plaintext to handle need for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * overwriting this during AES-CTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	tmp = kmemdup(plain, plain_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	/* IV for CTR before encrypted data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	memcpy(out, v, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Synthetic IV to be used as the initial counter in CTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * Q = V bitand (1^64 || 0^1 || 1^31 || 0^1 || 1^31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	v[8] &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	v[12] &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* CTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	tfm2 = crypto_alloc_skcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (IS_ERR(tfm2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return PTR_ERR(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* K2 for CTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	res = crypto_skcipher_setkey(tfm2, key + key_len, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	req = skcipher_request_alloc(tfm2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		res = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	sg_init_one(src, tmp, plain_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	sg_init_one(dst, out + AES_BLOCK_SIZE, plain_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	skcipher_request_set_crypt(req, src, dst, plain_len, v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	res = crypto_skcipher_encrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	skcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	crypto_free_skcipher(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Note: addr[] and len[] needs to have one extra slot at the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int aes_siv_decrypt(const u8 *key, size_t key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			   const u8 *iv_crypt, size_t iv_c_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			   size_t num_elem, const u8 *addr[], size_t len[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			   u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct crypto_shash *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct crypto_skcipher *tfm2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct skcipher_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct scatterlist src[1], dst[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	size_t crypt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	u8 frame_iv[AES_BLOCK_SIZE], iv[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	u8 check[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	crypt_len = iv_c_len - AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	key_len /= 2; /* S2V key || CTR key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	addr[num_elem] = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	len[num_elem] = crypt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	num_elem++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	memcpy(frame_iv, iv_crypt, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* Synthetic IV to be used as the initial counter in CTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * Q = V bitand (1^64 || 0^1 || 1^31 || 0^1 || 1^31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	iv[8] &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	iv[12] &= 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* CTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	tfm2 = crypto_alloc_skcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (IS_ERR(tfm2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return PTR_ERR(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* K2 for CTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	res = crypto_skcipher_setkey(tfm2, key + key_len, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		crypto_free_skcipher(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	req = skcipher_request_alloc(tfm2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		crypto_free_skcipher(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	sg_init_one(src, iv_crypt + AES_BLOCK_SIZE, crypt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	sg_init_one(dst, out, crypt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	skcipher_request_set_crypt(req, src, dst, crypt_len, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	res = crypto_skcipher_decrypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	skcipher_request_free(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	crypto_free_skcipher(tfm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/* S2V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return PTR_ERR(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	/* K1 for S2V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	res = crypto_shash_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		res = aes_s2v(tfm, num_elem, addr, len, check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	crypto_free_shash(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (memcmp(check, frame_iv, AES_BLOCK_SIZE) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int fils_encrypt_assoc_req(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			   struct ieee80211_mgd_assoc_data *assoc_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	u8 *capab, *ies, *encr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	const u8 *addr[5 + 1], *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	size_t len[5 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	size_t crypt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (ieee80211_is_reassoc_req(mgmt->frame_control)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		capab = (u8 *)&mgmt->u.reassoc_req.capab_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		ies = mgmt->u.reassoc_req.variable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		capab = (u8 *)&mgmt->u.assoc_req.capab_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		ies = mgmt->u.assoc_req.variable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	session = cfg80211_find_ext_ie(WLAN_EID_EXT_FILS_SESSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				       ies, skb->data + skb->len - ies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!session || session[1] != 1 + 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/* encrypt after FILS Session element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	encr = (u8 *)session + 2 + 1 + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	/* AES-SIV AAD vectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* The STA's MAC address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	addr[0] = mgmt->sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	len[0] = ETH_ALEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* The AP's BSSID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	addr[1] = mgmt->da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	len[1] = ETH_ALEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	/* The STA's nonce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	addr[2] = assoc_data->fils_nonces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	len[2] = FILS_NONCE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/* The AP's nonce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	addr[3] = &assoc_data->fils_nonces[FILS_NONCE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	len[3] = FILS_NONCE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* The (Re)Association Request frame from the Capability Information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * field to the FILS Session element (both inclusive).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	addr[4] = capab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	len[4] = encr - capab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	crypt_len = skb->data + skb->len - encr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	skb_put(skb, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	return aes_siv_encrypt(assoc_data->fils_kek, assoc_data->fils_kek_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			       encr, crypt_len, 5, addr, len, encr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int fils_decrypt_assoc_resp(struct ieee80211_sub_if_data *sdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			    u8 *frame, size_t *frame_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			    struct ieee80211_mgd_assoc_data *assoc_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct ieee80211_mgmt *mgmt = (void *)frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	u8 *capab, *ies, *encr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	const u8 *addr[5 + 1], *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	size_t len[5 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	size_t crypt_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (*frame_len < 24 + 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	capab = (u8 *)&mgmt->u.assoc_resp.capab_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	ies = mgmt->u.assoc_resp.variable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	session = cfg80211_find_ext_ie(WLAN_EID_EXT_FILS_SESSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				       ies, frame + *frame_len - ies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!session || session[1] != 1 + 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		mlme_dbg(sdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			 "No (valid) FILS Session element in (Re)Association Response frame from %pM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			 mgmt->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	/* decrypt after FILS Session element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	encr = (u8 *)session + 2 + 1 + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* AES-SIV AAD vectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	/* The AP's BSSID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	addr[0] = mgmt->sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	len[0] = ETH_ALEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	/* The STA's MAC address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	addr[1] = mgmt->da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	len[1] = ETH_ALEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	/* The AP's nonce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	addr[2] = &assoc_data->fils_nonces[FILS_NONCE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	len[2] = FILS_NONCE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	/* The STA's nonce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	addr[3] = assoc_data->fils_nonces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	len[3] = FILS_NONCE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/* The (Re)Association Response frame from the Capability Information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 * field to the FILS Session element (both inclusive).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	addr[4] = capab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	len[4] = encr - capab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	crypt_len = frame + *frame_len - encr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (crypt_len < AES_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		mlme_dbg(sdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			 "Not enough room for AES-SIV data after FILS Session element in (Re)Association Response frame from %pM",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			 mgmt->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	res = aes_siv_decrypt(assoc_data->fils_kek, assoc_data->fils_kek_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			      encr, crypt_len, 5, addr, len, encr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (res != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		mlme_dbg(sdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			 "AES-SIV decryption of (Re)Association Response frame from %pM failed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			 mgmt->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	*frame_len -= AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }