1 // SPDX-License-Identifier: GPL-2.0-only
3 * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
4 * Copyright 2008, Jouni Malinen <j@w1.fi>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/crypto.h>
10 #include <linux/export.h>
11 #include <linux/err.h>
12 #include <crypto/aes.h>
14 #include <net/mac80211.h>
18 #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
19 #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
22 static const u8 zero[CMAC_TLEN_256];
24 void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
25 const u8 *data, size_t data_len, u8 *mic)
27 SHASH_DESC_ON_STACK(desc, tfm);
28 u8 out[AES_BLOCK_SIZE];
33 crypto_shash_init(desc);
34 crypto_shash_update(desc, aad, AAD_LEN);
35 fc = (const __le16 *)aad;
36 if (ieee80211_is_beacon(*fc)) {
37 /* mask Timestamp field to zero */
38 crypto_shash_update(desc, zero, 8);
39 crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN);
41 crypto_shash_update(desc, data, data_len - CMAC_TLEN);
43 crypto_shash_finup(desc, zero, CMAC_TLEN, out);
45 memcpy(mic, out, CMAC_TLEN);
48 void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
49 const u8 *data, size_t data_len, u8 *mic)
51 SHASH_DESC_ON_STACK(desc, tfm);
56 crypto_shash_init(desc);
57 crypto_shash_update(desc, aad, AAD_LEN);
58 fc = (const __le16 *)aad;
59 if (ieee80211_is_beacon(*fc)) {
60 /* mask Timestamp field to zero */
61 crypto_shash_update(desc, zero, 8);
62 crypto_shash_update(desc, data + 8,
63 data_len - 8 - CMAC_TLEN_256);
65 crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
67 crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
70 struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
73 struct crypto_shash *tfm;
75 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
77 crypto_shash_setkey(tfm, key, key_len);
82 void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
84 crypto_free_shash(tfm);