Merge tag 'sched-core-2021-04-28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / net / mac80211 / aes_cmac.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
765cb46a
JM
2/*
3 * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
4 * Copyright 2008, Jouni Malinen <j@w1.fi>
765cb46a
JM
5 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/crypto.h>
4afebd63 10#include <linux/export.h>
765cb46a 11#include <linux/err.h>
0cd20a27 12#include <crypto/aes.h>
765cb46a
JM
13
14#include <net/mac80211.h>
15#include "key.h"
16#include "aes_cmac.h"
17
765cb46a 18#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
56c52da2 19#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
765cb46a
JM
20#define AAD_LEN 20
21
26717828 22static const u8 zero[CMAC_TLEN_256];
765cb46a 23
26717828 24void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
765cb46a
JM
25 const u8 *data, size_t data_len, u8 *mic)
26{
26717828
AB
27 SHASH_DESC_ON_STACK(desc, tfm);
28 u8 out[AES_BLOCK_SIZE];
2d5d4b0a 29 const __le16 *fc;
765cb46a 30
26717828 31 desc->tfm = tfm;
765cb46a 32
26717828
AB
33 crypto_shash_init(desc);
34 crypto_shash_update(desc, aad, AAD_LEN);
2d5d4b0a
JM
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);
40 } else {
41 crypto_shash_update(desc, data, data_len - CMAC_TLEN);
42 }
26717828
AB
43 crypto_shash_finup(desc, zero, CMAC_TLEN, out);
44
45 memcpy(mic, out, CMAC_TLEN);
765cb46a
JM
46}
47
26717828 48void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
56c52da2
JM
49 const u8 *data, size_t data_len, u8 *mic)
50{
26717828 51 SHASH_DESC_ON_STACK(desc, tfm);
2d5d4b0a 52 const __le16 *fc;
56c52da2 53
26717828 54 desc->tfm = tfm;
56c52da2 55
26717828
AB
56 crypto_shash_init(desc);
57 crypto_shash_update(desc, aad, AAD_LEN);
2d5d4b0a
JM
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);
64 } else {
65 crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
66 }
26717828 67 crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
56c52da2 68}
765cb46a 69
26717828
AB
70struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
71 size_t key_len)
765cb46a 72{
26717828 73 struct crypto_shash *tfm;
765cb46a 74
26717828 75 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
1ac62ba7 76 if (!IS_ERR(tfm))
26717828 77 crypto_shash_setkey(tfm, key, key_len);
765cb46a
JM
78
79 return tfm;
80}
81
26717828 82void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
765cb46a 83{
26717828 84 crypto_free_shash(tfm);
765cb46a 85}