blob: b31f1021ad9c47c70d3db43086d6a394b29a7ac5 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jouni Malinen765cb462009-01-08 13:32:01 +02002/*
3 * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
4 * Copyright 2008, Jouni Malinen <j@w1.fi>
Jouni Malinen765cb462009-01-08 13:32:01 +02005 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/crypto.h>
Emmanuel Grumbach4afebd62012-11-07 11:13:58 +020010#include <linux/export.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020011#include <linux/err.h>
Johannes Berg0cd20a22011-07-06 22:02:14 +020012#include <crypto/aes.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020013
14#include <net/mac80211.h>
15#include "key.h"
16#include "aes_cmac.h"
17
Jouni Malinen765cb462009-01-08 13:32:01 +020018#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
Jouni Malinen56c52da2015-01-24 19:52:08 +020019#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
Jouni Malinen765cb462009-01-08 13:32:01 +020020#define AAD_LEN 20
21
Ard Biesheuvel26717822017-02-06 10:49:28 +000022static const u8 zero[CMAC_TLEN_256];
Jouni Malinen765cb462009-01-08 13:32:01 +020023
Ard Biesheuvel26717822017-02-06 10:49:28 +000024void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen765cb462009-01-08 13:32:01 +020025 const u8 *data, size_t data_len, u8 *mic)
26{
Ard Biesheuvel26717822017-02-06 10:49:28 +000027 SHASH_DESC_ON_STACK(desc, tfm);
28 u8 out[AES_BLOCK_SIZE];
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020029 const __le16 *fc;
Jouni Malinen765cb462009-01-08 13:32:01 +020030
Ard Biesheuvel26717822017-02-06 10:49:28 +000031 desc->tfm = tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020032
Ard Biesheuvel26717822017-02-06 10:49:28 +000033 crypto_shash_init(desc);
34 crypto_shash_update(desc, aad, AAD_LEN);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020035 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 }
Ard Biesheuvel26717822017-02-06 10:49:28 +000043 crypto_shash_finup(desc, zero, CMAC_TLEN, out);
44
45 memcpy(mic, out, CMAC_TLEN);
Jouni Malinen765cb462009-01-08 13:32:01 +020046}
47
Ard Biesheuvel26717822017-02-06 10:49:28 +000048void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen56c52da2015-01-24 19:52:08 +020049 const u8 *data, size_t data_len, u8 *mic)
50{
Ard Biesheuvel26717822017-02-06 10:49:28 +000051 SHASH_DESC_ON_STACK(desc, tfm);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020052 const __le16 *fc;
Jouni Malinen765cb462009-01-08 13:32:01 +020053
Ard Biesheuvel26717822017-02-06 10:49:28 +000054 desc->tfm = tfm;
Jouni Malinen56c52da2015-01-24 19:52:08 +020055
Ard Biesheuvel26717822017-02-06 10:49:28 +000056 crypto_shash_init(desc);
57 crypto_shash_update(desc, aad, AAD_LEN);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020058 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 }
Ard Biesheuvel26717822017-02-06 10:49:28 +000067 crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
Jouni Malinen56c52da2015-01-24 19:52:08 +020068}
69
Ard Biesheuvel26717822017-02-06 10:49:28 +000070struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
71 size_t key_len)
Jouni Malinen765cb462009-01-08 13:32:01 +020072{
Ard Biesheuvel26717822017-02-06 10:49:28 +000073 struct crypto_shash *tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020074
Ard Biesheuvel26717822017-02-06 10:49:28 +000075 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +010076 if (!IS_ERR(tfm))
Ard Biesheuvel26717822017-02-06 10:49:28 +000077 crypto_shash_setkey(tfm, key, key_len);
Jouni Malinen765cb462009-01-08 13:32:01 +020078
79 return tfm;
80}
81
Ard Biesheuvel26717822017-02-06 10:49:28 +000082void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
Jouni Malinen765cb462009-01-08 13:32:01 +020083{
Ard Biesheuvel26717822017-02-06 10:49:28 +000084 crypto_free_shash(tfm);
Jouni Malinen765cb462009-01-08 13:32:01 +020085}