^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) * AES-GMAC for IEEE 802.11 BIP-GMAC-128 and BIP-GMAC-256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2015, 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 <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/mac80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "key.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "aes_gmac.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) const u8 *data, size_t data_len, u8 *mic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct scatterlist sg[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) u8 *zero, *__aad, iv[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct aead_request *aead_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const __le16 *fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (data_len < GMAC_MIC_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) aead_req = kzalloc(reqsize + GMAC_MIC_LEN + GMAC_AAD_LEN, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!aead_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) zero = (u8 *)aead_req + reqsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __aad = zero + GMAC_MIC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) memcpy(__aad, aad, GMAC_AAD_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) fc = (const __le16 *)aad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (ieee80211_is_beacon(*fc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* mask Timestamp field to zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) sg_init_table(sg, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) sg_set_buf(&sg[1], zero, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) sg_set_buf(&sg[2], data + 8, data_len - 8 - GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) sg_set_buf(&sg[3], zero, GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) sg_set_buf(&sg[4], mic, GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) sg_init_table(sg, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sg_set_buf(&sg[1], data, data_len - GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sg_set_buf(&sg[2], zero, GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) sg_set_buf(&sg[3], mic, GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memcpy(iv, nonce, GMAC_NONCE_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) memset(iv + GMAC_NONCE_LEN, 0, sizeof(iv) - GMAC_NONCE_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) iv[AES_BLOCK_SIZE - 1] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) aead_request_set_tfm(aead_req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) aead_request_set_crypt(aead_req, sg, sg, 0, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) aead_request_set_ad(aead_req, GMAC_AAD_LEN + data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = crypto_aead_encrypt(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree_sensitive(aead_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) size_t key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct crypto_aead *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (IS_ERR(tfm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) err = crypto_aead_setkey(tfm, key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) crypto_free_aead(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) crypto_free_aead(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }