1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2765cb46aSJouni Malinen /* 3765cb46aSJouni Malinen * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP 4765cb46aSJouni Malinen * Copyright 2008, Jouni Malinen <j@w1.fi> 5*8de85704SJohannes Berg * Copyright (C) 2020 Intel Corporation 6765cb46aSJouni Malinen */ 7765cb46aSJouni Malinen 8765cb46aSJouni Malinen #include <linux/kernel.h> 9765cb46aSJouni Malinen #include <linux/types.h> 10765cb46aSJouni Malinen #include <linux/crypto.h> 114afebd63SEmmanuel Grumbach #include <linux/export.h> 12765cb46aSJouni Malinen #include <linux/err.h> 130cd20a27SJohannes Berg #include <crypto/aes.h> 14765cb46aSJouni Malinen 15765cb46aSJouni Malinen #include <net/mac80211.h> 16765cb46aSJouni Malinen #include "key.h" 17765cb46aSJouni Malinen #include "aes_cmac.h" 18765cb46aSJouni Malinen 19765cb46aSJouni Malinen #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ 2056c52da2SJouni Malinen #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */ 21765cb46aSJouni Malinen #define AAD_LEN 20 22765cb46aSJouni Malinen 2326717828SArd Biesheuvel static const u8 zero[CMAC_TLEN_256]; 24765cb46aSJouni Malinen 2526717828SArd Biesheuvel void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, 26765cb46aSJouni Malinen const u8 *data, size_t data_len, u8 *mic) 27765cb46aSJouni Malinen { 2826717828SArd Biesheuvel SHASH_DESC_ON_STACK(desc, tfm); 2926717828SArd Biesheuvel u8 out[AES_BLOCK_SIZE]; 302d5d4b0aSJouni Malinen const __le16 *fc; 31765cb46aSJouni Malinen 3226717828SArd Biesheuvel desc->tfm = tfm; 33765cb46aSJouni Malinen 3426717828SArd Biesheuvel crypto_shash_init(desc); 3526717828SArd Biesheuvel crypto_shash_update(desc, aad, AAD_LEN); 362d5d4b0aSJouni Malinen fc = (const __le16 *)aad; 372d5d4b0aSJouni Malinen if (ieee80211_is_beacon(*fc)) { 382d5d4b0aSJouni Malinen /* mask Timestamp field to zero */ 392d5d4b0aSJouni Malinen crypto_shash_update(desc, zero, 8); 402d5d4b0aSJouni Malinen crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN); 412d5d4b0aSJouni Malinen } else { 4226717828SArd Biesheuvel crypto_shash_update(desc, data, data_len - CMAC_TLEN); 432d5d4b0aSJouni Malinen } 4426717828SArd Biesheuvel crypto_shash_finup(desc, zero, CMAC_TLEN, out); 4526717828SArd Biesheuvel 4626717828SArd Biesheuvel memcpy(mic, out, CMAC_TLEN); 47765cb46aSJouni Malinen } 48765cb46aSJouni Malinen 4926717828SArd Biesheuvel void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad, 5056c52da2SJouni Malinen const u8 *data, size_t data_len, u8 *mic) 5156c52da2SJouni Malinen { 5226717828SArd Biesheuvel SHASH_DESC_ON_STACK(desc, tfm); 532d5d4b0aSJouni Malinen const __le16 *fc; 54765cb46aSJouni Malinen 5526717828SArd Biesheuvel desc->tfm = tfm; 5656c52da2SJouni Malinen 5726717828SArd Biesheuvel crypto_shash_init(desc); 5826717828SArd Biesheuvel crypto_shash_update(desc, aad, AAD_LEN); 592d5d4b0aSJouni Malinen fc = (const __le16 *)aad; 602d5d4b0aSJouni Malinen if (ieee80211_is_beacon(*fc)) { 612d5d4b0aSJouni Malinen /* mask Timestamp field to zero */ 622d5d4b0aSJouni Malinen crypto_shash_update(desc, zero, 8); 632d5d4b0aSJouni Malinen crypto_shash_update(desc, data + 8, 642d5d4b0aSJouni Malinen data_len - 8 - CMAC_TLEN_256); 652d5d4b0aSJouni Malinen } else { 6626717828SArd Biesheuvel crypto_shash_update(desc, data, data_len - CMAC_TLEN_256); 672d5d4b0aSJouni Malinen } 6826717828SArd Biesheuvel crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic); 6956c52da2SJouni Malinen } 7056c52da2SJouni Malinen 7126717828SArd Biesheuvel struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[], 7256c52da2SJouni Malinen size_t key_len) 73765cb46aSJouni Malinen { 7426717828SArd Biesheuvel struct crypto_shash *tfm; 75765cb46aSJouni Malinen 7626717828SArd Biesheuvel tfm = crypto_alloc_shash("cmac(aes)", 0, 0); 77*8de85704SJohannes Berg if (!IS_ERR(tfm)) { 78*8de85704SJohannes Berg int err = crypto_shash_setkey(tfm, key, key_len); 79*8de85704SJohannes Berg 80*8de85704SJohannes Berg if (err) { 81*8de85704SJohannes Berg crypto_free_shash(tfm); 82*8de85704SJohannes Berg return ERR_PTR(err); 83*8de85704SJohannes Berg } 84*8de85704SJohannes Berg } 85765cb46aSJouni Malinen 86765cb46aSJouni Malinen return tfm; 87765cb46aSJouni Malinen } 88765cb46aSJouni Malinen 8926717828SArd Biesheuvel void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm) 90765cb46aSJouni Malinen { 9126717828SArd Biesheuvel crypto_free_shash(tfm); 92765cb46aSJouni Malinen } 93