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