Merge tag 'input-for-v6.10-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-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>
8de85704 5 * Copyright (C) 2020 Intel Corporation
765cb46a
JM
6 */
7
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/crypto.h>
4afebd63 11#include <linux/export.h>
765cb46a 12#include <linux/err.h>
0cd20a27 13#include <crypto/aes.h>
765cb46a
JM
14
15#include <net/mac80211.h>
16#include "key.h"
17#include "aes_cmac.h"
18
765cb46a 19#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
56c52da2 20#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
765cb46a
JM
21#define AAD_LEN 20
22
26717828 23static const u8 zero[CMAC_TLEN_256];
765cb46a 24
26717828 25void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
765cb46a
JM
26 const u8 *data, size_t data_len, u8 *mic)
27{
26717828
AB
28 SHASH_DESC_ON_STACK(desc, tfm);
29 u8 out[AES_BLOCK_SIZE];
2d5d4b0a 30 const __le16 *fc;
765cb46a 31
26717828 32 desc->tfm = tfm;
765cb46a 33
26717828
AB
34 crypto_shash_init(desc);
35 crypto_shash_update(desc, aad, AAD_LEN);
2d5d4b0a
JM
36 fc = (const __le16 *)aad;
37 if (ieee80211_is_beacon(*fc)) {
38 /* mask Timestamp field to zero */
39 crypto_shash_update(desc, zero, 8);
40 crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN);
41 } else {
42 crypto_shash_update(desc, data, data_len - CMAC_TLEN);
43 }
26717828
AB
44 crypto_shash_finup(desc, zero, CMAC_TLEN, out);
45
46 memcpy(mic, out, CMAC_TLEN);
765cb46a
JM
47}
48
26717828 49void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
56c52da2
JM
50 const u8 *data, size_t data_len, u8 *mic)
51{
26717828 52 SHASH_DESC_ON_STACK(desc, tfm);
2d5d4b0a 53 const __le16 *fc;
56c52da2 54
26717828 55 desc->tfm = tfm;
56c52da2 56
26717828
AB
57 crypto_shash_init(desc);
58 crypto_shash_update(desc, aad, AAD_LEN);
2d5d4b0a
JM
59 fc = (const __le16 *)aad;
60 if (ieee80211_is_beacon(*fc)) {
61 /* mask Timestamp field to zero */
62 crypto_shash_update(desc, zero, 8);
63 crypto_shash_update(desc, data + 8,
64 data_len - 8 - CMAC_TLEN_256);
65 } else {
66 crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
67 }
26717828 68 crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
56c52da2 69}
765cb46a 70
26717828
AB
71struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
72 size_t key_len)
765cb46a 73{
26717828 74 struct crypto_shash *tfm;
765cb46a 75
26717828 76 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
8de85704
JB
77 if (!IS_ERR(tfm)) {
78 int err = crypto_shash_setkey(tfm, key, key_len);
79
80 if (err) {
81 crypto_free_shash(tfm);
82 return ERR_PTR(err);
83 }
84 }
765cb46a
JM
85
86 return tfm;
87}
88
26717828 89void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
765cb46a 90{
26717828 91 crypto_free_shash(tfm);
765cb46a 92}