blob: 48c04f89de20af1b29572b4a712d04cca9c2e8f9 [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>
Johannes Berg8de85702021-04-09 12:40:28 +03005 * Copyright (C) 2020 Intel Corporation
Jouni Malinen765cb462009-01-08 13:32:01 +02006 */
7
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/crypto.h>
Emmanuel Grumbach4afebd62012-11-07 11:13:58 +020011#include <linux/export.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020012#include <linux/err.h>
Johannes Berg0cd20a22011-07-06 22:02:14 +020013#include <crypto/aes.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020014
15#include <net/mac80211.h>
16#include "key.h"
17#include "aes_cmac.h"
18
Jouni Malinen765cb462009-01-08 13:32:01 +020019#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
Jouni Malinen56c52da2015-01-24 19:52:08 +020020#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
Jouni Malinen765cb462009-01-08 13:32:01 +020021#define AAD_LEN 20
22
Ard Biesheuvel26717822017-02-06 10:49:28 +000023static const u8 zero[CMAC_TLEN_256];
Jouni Malinen765cb462009-01-08 13:32:01 +020024
Ard Biesheuvel26717822017-02-06 10:49:28 +000025void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen765cb462009-01-08 13:32:01 +020026 const u8 *data, size_t data_len, u8 *mic)
27{
Ard Biesheuvel26717822017-02-06 10:49:28 +000028 SHASH_DESC_ON_STACK(desc, tfm);
29 u8 out[AES_BLOCK_SIZE];
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020030 const __le16 *fc;
Jouni Malinen765cb462009-01-08 13:32:01 +020031
Ard Biesheuvel26717822017-02-06 10:49:28 +000032 desc->tfm = tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020033
Ard Biesheuvel26717822017-02-06 10:49:28 +000034 crypto_shash_init(desc);
35 crypto_shash_update(desc, aad, AAD_LEN);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020036 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 }
Ard Biesheuvel26717822017-02-06 10:49:28 +000044 crypto_shash_finup(desc, zero, CMAC_TLEN, out);
45
46 memcpy(mic, out, CMAC_TLEN);
Jouni Malinen765cb462009-01-08 13:32:01 +020047}
48
Ard Biesheuvel26717822017-02-06 10:49:28 +000049void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen56c52da2015-01-24 19:52:08 +020050 const u8 *data, size_t data_len, u8 *mic)
51{
Ard Biesheuvel26717822017-02-06 10:49:28 +000052 SHASH_DESC_ON_STACK(desc, tfm);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020053 const __le16 *fc;
Jouni Malinen765cb462009-01-08 13:32:01 +020054
Ard Biesheuvel26717822017-02-06 10:49:28 +000055 desc->tfm = tfm;
Jouni Malinen56c52da2015-01-24 19:52:08 +020056
Ard Biesheuvel26717822017-02-06 10:49:28 +000057 crypto_shash_init(desc);
58 crypto_shash_update(desc, aad, AAD_LEN);
Jouni Malinen2d5d4b02020-02-22 15:25:45 +020059 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 }
Ard Biesheuvel26717822017-02-06 10:49:28 +000068 crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
Jouni Malinen56c52da2015-01-24 19:52:08 +020069}
70
Ard Biesheuvel26717822017-02-06 10:49:28 +000071struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
72 size_t key_len)
Jouni Malinen765cb462009-01-08 13:32:01 +020073{
Ard Biesheuvel26717822017-02-06 10:49:28 +000074 struct crypto_shash *tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020075
Ard Biesheuvel26717822017-02-06 10:49:28 +000076 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
Johannes Berg8de85702021-04-09 12:40:28 +030077 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 }
Jouni Malinen765cb462009-01-08 13:32:01 +020085
86 return tfm;
87}
88
Ard Biesheuvel26717822017-02-06 10:49:28 +000089void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
Jouni Malinen765cb462009-01-08 13:32:01 +020090{
Ard Biesheuvel26717822017-02-06 10:49:28 +000091 crypto_free_shash(tfm);
Jouni Malinen765cb462009-01-08 13:32:01 +020092}